xref: /netbsd-src/external/gpl3/gdb/dist/gdbsupport/gdb_vecs.h (revision 5ba1f45f2a09259cc846f20c7c5501604d633c90)
18dffb485Schristos /* Some commonly-used VEC types.
28dffb485Schristos 
3*5ba1f45fSchristos    Copyright (C) 2012-2024 Free Software Foundation, Inc.
48dffb485Schristos 
58dffb485Schristos    This file is part of GDB.
68dffb485Schristos 
78dffb485Schristos    This program is free software; you can redistribute it and/or modify
88dffb485Schristos    it under the terms of the GNU General Public License as published by
98dffb485Schristos    the Free Software Foundation; either version 3 of the License, or
108dffb485Schristos    (at your option) any later version.
118dffb485Schristos 
128dffb485Schristos    This program is distributed in the hope that it will be useful,
138dffb485Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
148dffb485Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
158dffb485Schristos    GNU General Public License for more details.
168dffb485Schristos 
178dffb485Schristos    You should have received a copy of the GNU General Public License
188dffb485Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
198dffb485Schristos 
208dffb485Schristos #ifndef COMMON_GDB_VECS_H
218dffb485Schristos #define COMMON_GDB_VECS_H
228dffb485Schristos 
238dffb485Schristos /* Split STR, a list of DELIMITER-separated fields, into a char pointer vector.
248dffb485Schristos 
258dffb485Schristos    You may modify the returned strings.  */
268dffb485Schristos 
278dffb485Schristos extern std::vector<gdb::unique_xmalloc_ptr<char>>
288dffb485Schristos   delim_string_to_char_ptr_vec (const char *str, char delimiter);
298dffb485Schristos 
308dffb485Schristos /* Like dirnames_to_char_ptr_vec, but append the directories to *VECP.  */
318dffb485Schristos 
328dffb485Schristos extern void dirnames_to_char_ptr_vec_append
338dffb485Schristos   (std::vector<gdb::unique_xmalloc_ptr<char>> *vecp, const char *dirnames);
348dffb485Schristos 
358dffb485Schristos /* Split DIRNAMES by DIRNAME_SEPARATOR delimiter and return a list of all the
368dffb485Schristos    elements in their original order.  For empty string ("") DIRNAMES return
378dffb485Schristos    list of one empty string ("") element.
388dffb485Schristos 
398dffb485Schristos    You may modify the returned strings.  */
408dffb485Schristos 
418dffb485Schristos extern std::vector<gdb::unique_xmalloc_ptr<char>>
428dffb485Schristos   dirnames_to_char_ptr_vec (const char *dirnames);
438dffb485Schristos 
448dffb485Schristos /* Remove the element pointed by iterator IT from VEC, not preserving the order
458dffb485Schristos    of the remaining elements.  Return the removed element.  */
468dffb485Schristos 
478dffb485Schristos template <typename T>
488dffb485Schristos T
498dffb485Schristos unordered_remove (std::vector<T> &vec, typename std::vector<T>::iterator it)
508dffb485Schristos {
518dffb485Schristos   gdb_assert (it >= vec.begin () && it < vec.end ());
528dffb485Schristos 
538dffb485Schristos   T removed = std::move (*it);
548dffb485Schristos   if (it != vec.end () - 1)
558dffb485Schristos     *it = std::move (vec.back ());
568dffb485Schristos   vec.pop_back ();
578dffb485Schristos 
588dffb485Schristos   return removed;
598dffb485Schristos }
608dffb485Schristos 
618dffb485Schristos /* Remove the element at position IX from VEC, not preserving the order of the
628dffb485Schristos    remaining elements.  Return the removed element.  */
638dffb485Schristos 
648dffb485Schristos template <typename T>
658dffb485Schristos T
668dffb485Schristos unordered_remove (std::vector<T> &vec, typename std::vector<T>::size_type ix)
678dffb485Schristos {
688dffb485Schristos   gdb_assert (ix < vec.size ());
698dffb485Schristos 
708dffb485Schristos   return unordered_remove (vec, vec.begin () + ix);
718dffb485Schristos }
728dffb485Schristos 
738dffb485Schristos /* Remove the element at position IX from VEC, preserving the order the
748dffb485Schristos    remaining elements.  Return the removed element.  */
758dffb485Schristos 
768dffb485Schristos template <typename T>
778dffb485Schristos T
788dffb485Schristos ordered_remove (std::vector<T> &vec, typename std::vector<T>::size_type ix)
798dffb485Schristos {
808dffb485Schristos   gdb_assert (ix < vec.size ());
818dffb485Schristos 
828dffb485Schristos   T removed = std::move (vec[ix]);
838dffb485Schristos   vec.erase (vec.begin () + ix);
848dffb485Schristos 
858dffb485Schristos   return removed;
868dffb485Schristos }
878dffb485Schristos 
888dffb485Schristos #endif /* COMMON_GDB_VECS_H */
89