1/* Some commonly-used VEC types.
2
3   Copyright (C) 2012-2023 Free Software Foundation, Inc.
4
5   This file is part of GDB.
6
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 3 of the License, or
10   (at your option) any later version.
11
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20#ifndef COMMON_GDB_VECS_H
21#define COMMON_GDB_VECS_H
22
23/* Split STR, a list of DELIMITER-separated fields, into a char pointer vector.
24
25   You may modify the returned strings.  */
26
27extern std::vector<gdb::unique_xmalloc_ptr<char>>
28  delim_string_to_char_ptr_vec (const char *str, char delimiter);
29
30/* Like dirnames_to_char_ptr_vec, but append the directories to *VECP.  */
31
32extern void dirnames_to_char_ptr_vec_append
33  (std::vector<gdb::unique_xmalloc_ptr<char>> *vecp, const char *dirnames);
34
35/* Split DIRNAMES by DIRNAME_SEPARATOR delimiter and return a list of all the
36   elements in their original order.  For empty string ("") DIRNAMES return
37   list of one empty string ("") element.
38
39   You may modify the returned strings.  */
40
41extern std::vector<gdb::unique_xmalloc_ptr<char>>
42  dirnames_to_char_ptr_vec (const char *dirnames);
43
44/* Remove the element pointed by iterator IT from VEC, not preserving the order
45   of the remaining elements.  Return the removed element.  */
46
47template <typename T>
48T
49unordered_remove (std::vector<T> &vec, typename std::vector<T>::iterator it)
50{
51  gdb_assert (it >= vec.begin () && it < vec.end ());
52
53  T removed = std::move (*it);
54  if (it != vec.end () - 1)
55    *it = std::move (vec.back ());
56  vec.pop_back ();
57
58  return removed;
59}
60
61/* Remove the element at position IX from VEC, not preserving the order of the
62   remaining elements.  Return the removed element.  */
63
64template <typename T>
65T
66unordered_remove (std::vector<T> &vec, typename std::vector<T>::size_type ix)
67{
68  gdb_assert (ix < vec.size ());
69
70  return unordered_remove (vec, vec.begin () + ix);
71}
72
73/* Remove the element at position IX from VEC, preserving the order the
74   remaining elements.  Return the removed element.  */
75
76template <typename T>
77T
78ordered_remove (std::vector<T> &vec, typename std::vector<T>::size_type ix)
79{
80  gdb_assert (ix < vec.size ());
81
82  T removed = std::move (vec[ix]);
83  vec.erase (vec.begin () + ix);
84
85  return removed;
86}
87
88#endif /* COMMON_GDB_VECS_H */
89