Debian Codegraph @

src/arraylist.h

raw
1 /* This may look like C code, but it is really -*- C++ -*- */
2
3 /* Array-list container.
4
5 Copyright (C) 2025 Free Software Foundation, Inc.
6 Written by Bruno Haible <[email protected]>.
7
8 This file is part of GNU GPERF.
9
10 This program is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <https://www.gnu.org/licenses/>. */
22
23 #ifndef arraylist_h
24 #define arraylist_h 1
25
26 #include <stdlib.h> /* defines size_t, malloc(), realloc(), free(), abort() */
27 #include <new>
28 #if __cplusplus >= 201103L
29 # include <type_traits>
30 #endif
31
32 /* ArrayList<T> is a list of elements of type T, stored contiguously in memory
33 (in order to make good use of memory caches in a CPU).
34 T must be a type whose contents may be freely moved in memory (i.e. without
35 fields that contain backpointers to the element itself).
36 It is like std::vector<T>, but we don't want the bloated C++ standard
37 library.
38 It is like gnulib's "gl_list.hh" with the GL_ARRAY_LIST implementation.
39 But here we don't want polymorphic containers (that force function calls at
40 runtime); instead we want optimal inlining. */
41
42 /* In order to avoid the need for explicit instantiation for each possible
43 template parameter, we make the class ArrayList<T> entirely inline.
44 The class ArrayListRepresentation is used to attach methods which we want
45 to be compiled only once, avoiding duplicated code for each possible
46 template parameter. */
47
48 class ArrayListRepresentation
49 {
50 template <class T>
51 friend class ArrayList;
52 public:
53 // ------------------------------ Constructors ------------------------------
54
55 ArrayListRepresentation ()
56 {
57 _item = NULL;
58 _nitems = 0;
59 _nitems_max = 0;
60 }
61
62 // ------------------------------ Private stuff ------------------------------
63
64 private:
65 /* Vector of entries. */
66 void * _item;
67 /* Number of elements of the vector that are used. */
68 size_t _nitems;
69 /* Number of elements of the vector that can be used at most. */
70 size_t _nitems_max;
71
72 /* Ensures that _nitems_max >= n.
73 To be called only when _nitems_max < n. */
74 void ensure_capacity (size_t n, size_t size_of_T);
75 };
76
77 template <class T>
78 class ArrayList
79 {
80 public:
81 // ----------------------------- Constructors -----------------------------
82
83 /* Creates a new ArrayList<T> with 0 elements,
84 with no storage allocated initially. */
85 ArrayList ()
86 : _rep () {}
87
88 // ------------------------------ Destructor ------------------------------
89
90 ~ArrayList ()
91 {
92 if (_rep._item != NULL)
93 {
94 /* Destruct the elements (in the opposite order of their
95 initialization). */
96 #if __cplusplus >= 201103L
97 /* See <https://en.cppreference.com/w/cpp/types/is_destructible>. */
98 if (! std::is_trivially_destructible<T>::value)
99 #endif
100 {
101 size_t index = _rep._nitems;
102 while (index > 0)
103 {
104 --index;
105 (static_cast<T *>(_rep._item))[index].~T ();
106 }
107 }
108 /* Free the storage. */
109 free (_rep._item);
110 }
111 }
112
113 // ---------------------- Read-only member functions ----------------------
114
115 /* Returns the current number of elements in the list. */
116 size_t size () const
117 {
118 return _rep._nitems;
119 }
120
121 T& get_at (size_t index) const
122 {
123 #if !__OPTIMIZE__
124 if (index >= _rep._nitems)
125 /* index out of range. */
126 abort ();
127 #endif
128 return (static_cast<T *>(_rep._item))[index];
129 }
130
131 // ---------------------- Modifying member functions ----------------------
132
133 void set_at (size_t index, const T& value)
134 {
135 #if !__OPTIMIZE__
136 if (index >= _rep._nitems)
137 /* index out of range. */
138 abort ();
139 #endif
140 (static_cast<T *>(_rep._item))[index] = value;
141 }
142
143 size_t add_last (const T& value)
144 {
145 if (_rep._nitems == _rep._nitems_max)
146 ensure_capacity (_rep._nitems + 1);
147 size_t index = _rep._nitems;
148 new (&(static_cast<T *>(_rep._item))[index]) T (value);
149 _rep._nitems++;
150 return index;
151 }
152
153 // ----------------------------- Private stuff -----------------------------
154
155 private:
156 /* Here, the vector of entries is a 'T *', but only the first _nitems
157 elements are initialized. The remaining memory is uninitialized. */
158 ArrayListRepresentation _rep;
159
160 /* Ensures that _nitems_max >= n.
161 To be called only when _nitems_max < n. */
162 void ensure_capacity (size_t n)
163 {
164 _rep.ensure_capacity (n, sizeof (T));
165 }
166 };
167
168 #endif