annotate gcc/vec.h @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 /* Vector API for GNU compiler.
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2 Copyright (C) 2004-2020 Free Software Foundation, Inc.
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 Contributed by Nathan Sidwell <nathan@codesourcery.com>
111
kono
parents: 67
diff changeset
4 Re-implemented in C++ by Diego Novillo <dnovillo@google.com>
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
5
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 This file is part of GCC.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
7
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 GCC is free software; you can redistribute it and/or modify it under
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 the terms of the GNU General Public License as published by the Free
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 Software Foundation; either version 3, or (at your option) any later
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 version.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
12
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 for more details.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
17
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 along with GCC; see the file COPYING3. If not see
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 <http://www.gnu.org/licenses/>. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
21
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 #ifndef GCC_VEC_H
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 #define GCC_VEC_H
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
24
111
kono
parents: 67
diff changeset
25 /* Some gen* file have no ggc support as the header file gtype-desc.h is
kono
parents: 67
diff changeset
26 missing. Provide these definitions in case ggc.h has not been included.
kono
parents: 67
diff changeset
27 This is not a problem because any code that runs before gengtype is built
kono
parents: 67
diff changeset
28 will never need to use GC vectors.*/
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
29
111
kono
parents: 67
diff changeset
30 extern void ggc_free (void *);
kono
parents: 67
diff changeset
31 extern size_t ggc_round_alloc_size (size_t requested_size);
kono
parents: 67
diff changeset
32 extern void *ggc_realloc (void *, size_t MEM_STAT_DECL);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
33
111
kono
parents: 67
diff changeset
34 /* Templated vector type and associated interfaces.
kono
parents: 67
diff changeset
35
kono
parents: 67
diff changeset
36 The interface functions are typesafe and use inline functions,
kono
parents: 67
diff changeset
37 sometimes backed by out-of-line generic functions. The vectors are
kono
parents: 67
diff changeset
38 designed to interoperate with the GTY machinery.
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
39
111
kono
parents: 67
diff changeset
40 There are both 'index' and 'iterate' accessors. The index accessor
kono
parents: 67
diff changeset
41 is implemented by operator[]. The iterator returns a boolean
kono
parents: 67
diff changeset
42 iteration condition and updates the iteration variable passed by
kono
parents: 67
diff changeset
43 reference. Because the iterator will be inlined, the address-of
kono
parents: 67
diff changeset
44 can be optimized away.
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
45
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 Each operation that increases the number of active elements is
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 available in 'quick' and 'safe' variants. The former presumes that
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 there is sufficient allocated space for the operation to succeed
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 (it dies if there is not). The latter will reallocate the
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 vector, if needed. Reallocation causes an exponential increase in
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 vector size. If you know you will be adding N elements, it would
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 be more efficient to use the reserve operation before adding the
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 elements with the 'quick' operation. This will ensure there are at
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 least as many elements as you ask for, it will exponentially
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 increase if there are too few spare slots. If you want reserve a
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 specific number of slots, but do not want the exponential increase
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 (for instance, you know this is the last allocation), use the
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 reserve_exact operation. You can also create a vector of a
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 specific size from the get go.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
60
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 You should prefer the push and pop operations, as they append and
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 remove from the end of the vector. If you need to remove several
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 items in one go, use the truncate operation. The insert and remove
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 operations allow you to change elements in the middle of the
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
65 vector. There are two remove operations, one which preserves the
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 element ordering 'ordered_remove', and one which does not
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
67 'unordered_remove'. The latter function copies the end element
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 into the removed slot, rather than invoke a memmove operation. The
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
69 'lower_bound' function will determine where to place an item in the
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
70 array using insert that will maintain sorted order.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
71
111
kono
parents: 67
diff changeset
72 Vectors are template types with three arguments: the type of the
kono
parents: 67
diff changeset
73 elements in the vector, the allocation strategy, and the physical
kono
parents: 67
diff changeset
74 layout to use
kono
parents: 67
diff changeset
75
kono
parents: 67
diff changeset
76 Four allocation strategies are supported:
kono
parents: 67
diff changeset
77
kono
parents: 67
diff changeset
78 - Heap: allocation is done using malloc/free. This is the
kono
parents: 67
diff changeset
79 default allocation strategy.
kono
parents: 67
diff changeset
80
kono
parents: 67
diff changeset
81 - GC: allocation is done using ggc_alloc/ggc_free.
kono
parents: 67
diff changeset
82
kono
parents: 67
diff changeset
83 - GC atomic: same as GC with the exception that the elements
kono
parents: 67
diff changeset
84 themselves are assumed to be of an atomic type that does
kono
parents: 67
diff changeset
85 not need to be garbage collected. This means that marking
kono
parents: 67
diff changeset
86 routines do not need to traverse the array marking the
kono
parents: 67
diff changeset
87 individual elements. This increases the performance of
kono
parents: 67
diff changeset
88 GC activities.
kono
parents: 67
diff changeset
89
kono
parents: 67
diff changeset
90 Two physical layouts are supported:
kono
parents: 67
diff changeset
91
kono
parents: 67
diff changeset
92 - Embedded: The vector is structured using the trailing array
kono
parents: 67
diff changeset
93 idiom. The last member of the structure is an array of size
kono
parents: 67
diff changeset
94 1. When the vector is initially allocated, a single memory
kono
parents: 67
diff changeset
95 block is created to hold the vector's control data and the
kono
parents: 67
diff changeset
96 array of elements. These vectors cannot grow without
kono
parents: 67
diff changeset
97 reallocation (see discussion on embeddable vectors below).
kono
parents: 67
diff changeset
98
kono
parents: 67
diff changeset
99 - Space efficient: The vector is structured as a pointer to an
kono
parents: 67
diff changeset
100 embedded vector. This is the default layout. It means that
kono
parents: 67
diff changeset
101 vectors occupy a single word of storage before initial
kono
parents: 67
diff changeset
102 allocation. Vectors are allowed to grow (the internal
kono
parents: 67
diff changeset
103 pointer is reallocated but the main vector instance does not
kono
parents: 67
diff changeset
104 need to relocate).
kono
parents: 67
diff changeset
105
kono
parents: 67
diff changeset
106 The type, allocation and layout are specified when the vector is
kono
parents: 67
diff changeset
107 declared.
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
108
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
109 If you need to directly manipulate a vector, then the 'address'
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
110 accessor will return the address of the start of the vector. Also
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
111 the 'space' predicate will tell you whether there is spare capacity
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
112 in the vector. You will not normally need to use these two functions.
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
113
111
kono
parents: 67
diff changeset
114 Notes on the different layout strategies
kono
parents: 67
diff changeset
115
kono
parents: 67
diff changeset
116 * Embeddable vectors (vec<T, A, vl_embed>)
kono
parents: 67
diff changeset
117
kono
parents: 67
diff changeset
118 These vectors are suitable to be embedded in other data
kono
parents: 67
diff changeset
119 structures so that they can be pre-allocated in a contiguous
kono
parents: 67
diff changeset
120 memory block.
kono
parents: 67
diff changeset
121
kono
parents: 67
diff changeset
122 Embeddable vectors are implemented using the trailing array
kono
parents: 67
diff changeset
123 idiom, thus they are not resizeable without changing the address
kono
parents: 67
diff changeset
124 of the vector object itself. This means you cannot have
kono
parents: 67
diff changeset
125 variables or fields of embeddable vector type -- always use a
kono
parents: 67
diff changeset
126 pointer to a vector. The one exception is the final field of a
kono
parents: 67
diff changeset
127 structure, which could be a vector type.
kono
parents: 67
diff changeset
128
kono
parents: 67
diff changeset
129 You will have to use the embedded_size & embedded_init calls to
kono
parents: 67
diff changeset
130 create such objects, and they will not be resizeable (so the
kono
parents: 67
diff changeset
131 'safe' allocation variants are not available).
kono
parents: 67
diff changeset
132
kono
parents: 67
diff changeset
133 Properties of embeddable vectors:
kono
parents: 67
diff changeset
134
kono
parents: 67
diff changeset
135 - The whole vector and control data are allocated in a single
kono
parents: 67
diff changeset
136 contiguous block. It uses the trailing-vector idiom, so
kono
parents: 67
diff changeset
137 allocation must reserve enough space for all the elements
kono
parents: 67
diff changeset
138 in the vector plus its control data.
kono
parents: 67
diff changeset
139 - The vector cannot be re-allocated.
kono
parents: 67
diff changeset
140 - The vector cannot grow nor shrink.
kono
parents: 67
diff changeset
141 - No indirections needed for access/manipulation.
kono
parents: 67
diff changeset
142 - It requires 2 words of storage (prior to vector allocation).
kono
parents: 67
diff changeset
143
kono
parents: 67
diff changeset
144
kono
parents: 67
diff changeset
145 * Space efficient vector (vec<T, A, vl_ptr>)
kono
parents: 67
diff changeset
146
kono
parents: 67
diff changeset
147 These vectors can grow dynamically and are allocated together
kono
parents: 67
diff changeset
148 with their control data. They are suited to be included in data
kono
parents: 67
diff changeset
149 structures. Prior to initial allocation, they only take a single
kono
parents: 67
diff changeset
150 word of storage.
kono
parents: 67
diff changeset
151
kono
parents: 67
diff changeset
152 These vectors are implemented as a pointer to embeddable vectors.
kono
parents: 67
diff changeset
153 The semantics allow for this pointer to be NULL to represent
kono
parents: 67
diff changeset
154 empty vectors. This way, empty vectors occupy minimal space in
kono
parents: 67
diff changeset
155 the structure containing them.
kono
parents: 67
diff changeset
156
kono
parents: 67
diff changeset
157 Properties:
kono
parents: 67
diff changeset
158
kono
parents: 67
diff changeset
159 - The whole vector and control data are allocated in a single
kono
parents: 67
diff changeset
160 contiguous block.
kono
parents: 67
diff changeset
161 - The whole vector may be re-allocated.
kono
parents: 67
diff changeset
162 - Vector data may grow and shrink.
kono
parents: 67
diff changeset
163 - Access and manipulation requires a pointer test and
kono
parents: 67
diff changeset
164 indirection.
kono
parents: 67
diff changeset
165 - It requires 1 word of storage (prior to vector allocation).
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
166
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
167 An example of their use would be,
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
168
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
169 struct my_struct {
111
kono
parents: 67
diff changeset
170 // A space-efficient vector of tree pointers in GC memory.
kono
parents: 67
diff changeset
171 vec<tree, va_gc, vl_ptr> v;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
172 };
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
173
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
174 struct my_struct *s;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
175
111
kono
parents: 67
diff changeset
176 if (s->v.length ()) { we have some contents }
kono
parents: 67
diff changeset
177 s->v.safe_push (decl); // append some decl onto the end
kono
parents: 67
diff changeset
178 for (ix = 0; s->v.iterate (ix, &elt); ix++)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
179 { do something with elt }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
180 */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
181
111
kono
parents: 67
diff changeset
182 /* Support function for statistics. */
kono
parents: 67
diff changeset
183 extern void dump_vec_loc_statistics (void);
kono
parents: 67
diff changeset
184
kono
parents: 67
diff changeset
185 /* Hashtable mapping vec addresses to descriptors. */
kono
parents: 67
diff changeset
186 extern htab_t vec_mem_usage_hash;
kono
parents: 67
diff changeset
187
kono
parents: 67
diff changeset
188 /* Control data for vectors. This contains the number of allocated
kono
parents: 67
diff changeset
189 and used slots inside a vector. */
kono
parents: 67
diff changeset
190
kono
parents: 67
diff changeset
191 struct vec_prefix
kono
parents: 67
diff changeset
192 {
kono
parents: 67
diff changeset
193 /* FIXME - These fields should be private, but we need to cater to
kono
parents: 67
diff changeset
194 compilers that have stricter notions of PODness for types. */
kono
parents: 67
diff changeset
195
kono
parents: 67
diff changeset
196 /* Memory allocation support routines in vec.c. */
kono
parents: 67
diff changeset
197 void register_overhead (void *, size_t, size_t CXX_MEM_STAT_INFO);
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
198 void release_overhead (void *, size_t, size_t, bool CXX_MEM_STAT_INFO);
111
kono
parents: 67
diff changeset
199 static unsigned calculate_allocation (vec_prefix *, unsigned, bool);
kono
parents: 67
diff changeset
200 static unsigned calculate_allocation_1 (unsigned, unsigned);
kono
parents: 67
diff changeset
201
kono
parents: 67
diff changeset
202 /* Note that vec_prefix should be a base class for vec, but we use
kono
parents: 67
diff changeset
203 offsetof() on vector fields of tree structures (e.g.,
kono
parents: 67
diff changeset
204 tree_binfo::base_binfos), and offsetof only supports base types.
kono
parents: 67
diff changeset
205
kono
parents: 67
diff changeset
206 To compensate, we make vec_prefix a field inside vec and make
kono
parents: 67
diff changeset
207 vec a friend class of vec_prefix so it can access its fields. */
kono
parents: 67
diff changeset
208 template <typename, typename, typename> friend struct vec;
kono
parents: 67
diff changeset
209
kono
parents: 67
diff changeset
210 /* The allocator types also need access to our internals. */
kono
parents: 67
diff changeset
211 friend struct va_gc;
kono
parents: 67
diff changeset
212 friend struct va_gc_atomic;
kono
parents: 67
diff changeset
213 friend struct va_heap;
kono
parents: 67
diff changeset
214
kono
parents: 67
diff changeset
215 unsigned m_alloc : 31;
kono
parents: 67
diff changeset
216 unsigned m_using_auto_storage : 1;
kono
parents: 67
diff changeset
217 unsigned m_num;
kono
parents: 67
diff changeset
218 };
kono
parents: 67
diff changeset
219
kono
parents: 67
diff changeset
220 /* Calculate the number of slots to reserve a vector, making sure that
kono
parents: 67
diff changeset
221 RESERVE slots are free. If EXACT grow exactly, otherwise grow
kono
parents: 67
diff changeset
222 exponentially. PFX is the control data for the vector. */
kono
parents: 67
diff changeset
223
kono
parents: 67
diff changeset
224 inline unsigned
kono
parents: 67
diff changeset
225 vec_prefix::calculate_allocation (vec_prefix *pfx, unsigned reserve,
kono
parents: 67
diff changeset
226 bool exact)
kono
parents: 67
diff changeset
227 {
kono
parents: 67
diff changeset
228 if (exact)
kono
parents: 67
diff changeset
229 return (pfx ? pfx->m_num : 0) + reserve;
kono
parents: 67
diff changeset
230 else if (!pfx)
kono
parents: 67
diff changeset
231 return MAX (4, reserve);
kono
parents: 67
diff changeset
232 return calculate_allocation_1 (pfx->m_alloc, pfx->m_num + reserve);
kono
parents: 67
diff changeset
233 }
kono
parents: 67
diff changeset
234
kono
parents: 67
diff changeset
235 template<typename, typename, typename> struct vec;
kono
parents: 67
diff changeset
236
kono
parents: 67
diff changeset
237 /* Valid vector layouts
kono
parents: 67
diff changeset
238
kono
parents: 67
diff changeset
239 vl_embed - Embeddable vector that uses the trailing array idiom.
kono
parents: 67
diff changeset
240 vl_ptr - Space efficient vector that uses a pointer to an
kono
parents: 67
diff changeset
241 embeddable vector. */
kono
parents: 67
diff changeset
242 struct vl_embed { };
kono
parents: 67
diff changeset
243 struct vl_ptr { };
kono
parents: 67
diff changeset
244
kono
parents: 67
diff changeset
245
kono
parents: 67
diff changeset
246 /* Types of supported allocations
kono
parents: 67
diff changeset
247
kono
parents: 67
diff changeset
248 va_heap - Allocation uses malloc/free.
kono
parents: 67
diff changeset
249 va_gc - Allocation uses ggc_alloc.
kono
parents: 67
diff changeset
250 va_gc_atomic - Same as GC, but individual elements of the array
kono
parents: 67
diff changeset
251 do not need to be marked during collection. */
kono
parents: 67
diff changeset
252
kono
parents: 67
diff changeset
253 /* Allocator type for heap vectors. */
kono
parents: 67
diff changeset
254 struct va_heap
kono
parents: 67
diff changeset
255 {
kono
parents: 67
diff changeset
256 /* Heap vectors are frequently regular instances, so use the vl_ptr
kono
parents: 67
diff changeset
257 layout for them. */
kono
parents: 67
diff changeset
258 typedef vl_ptr default_layout;
kono
parents: 67
diff changeset
259
kono
parents: 67
diff changeset
260 template<typename T>
kono
parents: 67
diff changeset
261 static void reserve (vec<T, va_heap, vl_embed> *&, unsigned, bool
kono
parents: 67
diff changeset
262 CXX_MEM_STAT_INFO);
kono
parents: 67
diff changeset
263
kono
parents: 67
diff changeset
264 template<typename T>
kono
parents: 67
diff changeset
265 static void release (vec<T, va_heap, vl_embed> *&);
kono
parents: 67
diff changeset
266 };
kono
parents: 67
diff changeset
267
kono
parents: 67
diff changeset
268
kono
parents: 67
diff changeset
269 /* Allocator for heap memory. Ensure there are at least RESERVE free
kono
parents: 67
diff changeset
270 slots in V. If EXACT is true, grow exactly, else grow
kono
parents: 67
diff changeset
271 exponentially. As a special case, if the vector had not been
kono
parents: 67
diff changeset
272 allocated and RESERVE is 0, no vector will be created. */
kono
parents: 67
diff changeset
273
kono
parents: 67
diff changeset
274 template<typename T>
kono
parents: 67
diff changeset
275 inline void
kono
parents: 67
diff changeset
276 va_heap::reserve (vec<T, va_heap, vl_embed> *&v, unsigned reserve, bool exact
kono
parents: 67
diff changeset
277 MEM_STAT_DECL)
kono
parents: 67
diff changeset
278 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
279 size_t elt_size = sizeof (T);
111
kono
parents: 67
diff changeset
280 unsigned alloc
kono
parents: 67
diff changeset
281 = vec_prefix::calculate_allocation (v ? &v->m_vecpfx : 0, reserve, exact);
kono
parents: 67
diff changeset
282 gcc_checking_assert (alloc);
kono
parents: 67
diff changeset
283
kono
parents: 67
diff changeset
284 if (GATHER_STATISTICS && v)
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
285 v->m_vecpfx.release_overhead (v, elt_size * v->allocated (),
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
286 v->allocated (), false);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
287
111
kono
parents: 67
diff changeset
288 size_t size = vec<T, va_heap, vl_embed>::embedded_size (alloc);
kono
parents: 67
diff changeset
289 unsigned nelem = v ? v->length () : 0;
kono
parents: 67
diff changeset
290 v = static_cast <vec<T, va_heap, vl_embed> *> (xrealloc (v, size));
kono
parents: 67
diff changeset
291 v->embedded_init (alloc, nelem);
kono
parents: 67
diff changeset
292
kono
parents: 67
diff changeset
293 if (GATHER_STATISTICS)
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
294 v->m_vecpfx.register_overhead (v, alloc, elt_size PASS_MEM_STAT);
111
kono
parents: 67
diff changeset
295 }
kono
parents: 67
diff changeset
296
kono
parents: 67
diff changeset
297
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
298 #if GCC_VERSION >= 4007
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
299 #pragma GCC diagnostic push
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
300 #pragma GCC diagnostic ignored "-Wfree-nonheap-object"
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
301 #endif
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
302
111
kono
parents: 67
diff changeset
303 /* Free the heap space allocated for vector V. */
kono
parents: 67
diff changeset
304
kono
parents: 67
diff changeset
305 template<typename T>
kono
parents: 67
diff changeset
306 void
kono
parents: 67
diff changeset
307 va_heap::release (vec<T, va_heap, vl_embed> *&v)
kono
parents: 67
diff changeset
308 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
309 size_t elt_size = sizeof (T);
111
kono
parents: 67
diff changeset
310 if (v == NULL)
kono
parents: 67
diff changeset
311 return;
kono
parents: 67
diff changeset
312
kono
parents: 67
diff changeset
313 if (GATHER_STATISTICS)
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
314 v->m_vecpfx.release_overhead (v, elt_size * v->allocated (),
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
315 v->allocated (), true);
111
kono
parents: 67
diff changeset
316 ::free (v);
kono
parents: 67
diff changeset
317 v = NULL;
kono
parents: 67
diff changeset
318 }
kono
parents: 67
diff changeset
319
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
320 #if GCC_VERSION >= 4007
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
321 #pragma GCC diagnostic pop
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
322 #endif
111
kono
parents: 67
diff changeset
323
kono
parents: 67
diff changeset
324 /* Allocator type for GC vectors. Notice that we need the structure
kono
parents: 67
diff changeset
325 declaration even if GC is not enabled. */
kono
parents: 67
diff changeset
326
kono
parents: 67
diff changeset
327 struct va_gc
kono
parents: 67
diff changeset
328 {
kono
parents: 67
diff changeset
329 /* Use vl_embed as the default layout for GC vectors. Due to GTY
kono
parents: 67
diff changeset
330 limitations, GC vectors must always be pointers, so it is more
kono
parents: 67
diff changeset
331 efficient to use a pointer to the vl_embed layout, rather than
kono
parents: 67
diff changeset
332 using a pointer to a pointer as would be the case with vl_ptr. */
kono
parents: 67
diff changeset
333 typedef vl_embed default_layout;
kono
parents: 67
diff changeset
334
kono
parents: 67
diff changeset
335 template<typename T, typename A>
kono
parents: 67
diff changeset
336 static void reserve (vec<T, A, vl_embed> *&, unsigned, bool
kono
parents: 67
diff changeset
337 CXX_MEM_STAT_INFO);
kono
parents: 67
diff changeset
338
kono
parents: 67
diff changeset
339 template<typename T, typename A>
kono
parents: 67
diff changeset
340 static void release (vec<T, A, vl_embed> *&v);
kono
parents: 67
diff changeset
341 };
kono
parents: 67
diff changeset
342
kono
parents: 67
diff changeset
343
kono
parents: 67
diff changeset
344 /* Free GC memory used by V and reset V to NULL. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
345
111
kono
parents: 67
diff changeset
346 template<typename T, typename A>
kono
parents: 67
diff changeset
347 inline void
kono
parents: 67
diff changeset
348 va_gc::release (vec<T, A, vl_embed> *&v)
kono
parents: 67
diff changeset
349 {
kono
parents: 67
diff changeset
350 if (v)
kono
parents: 67
diff changeset
351 ::ggc_free (v);
kono
parents: 67
diff changeset
352 v = NULL;
kono
parents: 67
diff changeset
353 }
kono
parents: 67
diff changeset
354
kono
parents: 67
diff changeset
355
kono
parents: 67
diff changeset
356 /* Allocator for GC memory. Ensure there are at least RESERVE free
kono
parents: 67
diff changeset
357 slots in V. If EXACT is true, grow exactly, else grow
kono
parents: 67
diff changeset
358 exponentially. As a special case, if the vector had not been
kono
parents: 67
diff changeset
359 allocated and RESERVE is 0, no vector will be created. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
360
111
kono
parents: 67
diff changeset
361 template<typename T, typename A>
kono
parents: 67
diff changeset
362 void
kono
parents: 67
diff changeset
363 va_gc::reserve (vec<T, A, vl_embed> *&v, unsigned reserve, bool exact
kono
parents: 67
diff changeset
364 MEM_STAT_DECL)
kono
parents: 67
diff changeset
365 {
kono
parents: 67
diff changeset
366 unsigned alloc
kono
parents: 67
diff changeset
367 = vec_prefix::calculate_allocation (v ? &v->m_vecpfx : 0, reserve, exact);
kono
parents: 67
diff changeset
368 if (!alloc)
kono
parents: 67
diff changeset
369 {
kono
parents: 67
diff changeset
370 ::ggc_free (v);
kono
parents: 67
diff changeset
371 v = NULL;
kono
parents: 67
diff changeset
372 return;
kono
parents: 67
diff changeset
373 }
kono
parents: 67
diff changeset
374
kono
parents: 67
diff changeset
375 /* Calculate the amount of space we want. */
kono
parents: 67
diff changeset
376 size_t size = vec<T, A, vl_embed>::embedded_size (alloc);
kono
parents: 67
diff changeset
377
kono
parents: 67
diff changeset
378 /* Ask the allocator how much space it will really give us. */
kono
parents: 67
diff changeset
379 size = ::ggc_round_alloc_size (size);
kono
parents: 67
diff changeset
380
kono
parents: 67
diff changeset
381 /* Adjust the number of slots accordingly. */
kono
parents: 67
diff changeset
382 size_t vec_offset = sizeof (vec_prefix);
kono
parents: 67
diff changeset
383 size_t elt_size = sizeof (T);
kono
parents: 67
diff changeset
384 alloc = (size - vec_offset) / elt_size;
kono
parents: 67
diff changeset
385
kono
parents: 67
diff changeset
386 /* And finally, recalculate the amount of space we ask for. */
kono
parents: 67
diff changeset
387 size = vec_offset + alloc * elt_size;
kono
parents: 67
diff changeset
388
kono
parents: 67
diff changeset
389 unsigned nelem = v ? v->length () : 0;
kono
parents: 67
diff changeset
390 v = static_cast <vec<T, A, vl_embed> *> (::ggc_realloc (v, size
kono
parents: 67
diff changeset
391 PASS_MEM_STAT));
kono
parents: 67
diff changeset
392 v->embedded_init (alloc, nelem);
kono
parents: 67
diff changeset
393 }
kono
parents: 67
diff changeset
394
kono
parents: 67
diff changeset
395
kono
parents: 67
diff changeset
396 /* Allocator type for GC vectors. This is for vectors of types
kono
parents: 67
diff changeset
397 atomics w.r.t. collection, so allocation and deallocation is
kono
parents: 67
diff changeset
398 completely inherited from va_gc. */
kono
parents: 67
diff changeset
399 struct va_gc_atomic : va_gc
kono
parents: 67
diff changeset
400 {
kono
parents: 67
diff changeset
401 };
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
402
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
403
111
kono
parents: 67
diff changeset
404 /* Generic vector template. Default values for A and L indicate the
kono
parents: 67
diff changeset
405 most commonly used strategies.
kono
parents: 67
diff changeset
406
kono
parents: 67
diff changeset
407 FIXME - Ideally, they would all be vl_ptr to encourage using regular
kono
parents: 67
diff changeset
408 instances for vectors, but the existing GTY machinery is limited
kono
parents: 67
diff changeset
409 in that it can only deal with GC objects that are pointers
kono
parents: 67
diff changeset
410 themselves.
kono
parents: 67
diff changeset
411
kono
parents: 67
diff changeset
412 This means that vector operations that need to deal with
kono
parents: 67
diff changeset
413 potentially NULL pointers, must be provided as free
kono
parents: 67
diff changeset
414 functions (see the vec_safe_* functions above). */
kono
parents: 67
diff changeset
415 template<typename T,
kono
parents: 67
diff changeset
416 typename A = va_heap,
kono
parents: 67
diff changeset
417 typename L = typename A::default_layout>
kono
parents: 67
diff changeset
418 struct GTY((user)) vec
kono
parents: 67
diff changeset
419 {
kono
parents: 67
diff changeset
420 };
kono
parents: 67
diff changeset
421
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
422 /* Generic vec<> debug helpers.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
423
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
424 These need to be instantiated for each vec<TYPE> used throughout
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
425 the compiler like this:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
426
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
427 DEFINE_DEBUG_VEC (TYPE)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
428
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
429 The reason we have a debug_helper() is because GDB can't
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
430 disambiguate a plain call to debug(some_vec), and it must be called
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
431 like debug<TYPE>(some_vec). */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
432
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
433 template<typename T>
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
434 void
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
435 debug_helper (vec<T> &ref)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
436 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
437 unsigned i;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
438 for (i = 0; i < ref.length (); ++i)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
439 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
440 fprintf (stderr, "[%d] = ", i);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
441 debug_slim (ref[i]);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
442 fputc ('\n', stderr);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
443 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
444 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
445
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
446 /* We need a separate va_gc variant here because default template
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
447 argument for functions cannot be used in c++-98. Once this
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
448 restriction is removed, those variant should be folded with the
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
449 above debug_helper. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
450
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
451 template<typename T>
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
452 void
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
453 debug_helper (vec<T, va_gc> &ref)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
454 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
455 unsigned i;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
456 for (i = 0; i < ref.length (); ++i)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
457 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
458 fprintf (stderr, "[%d] = ", i);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
459 debug_slim (ref[i]);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
460 fputc ('\n', stderr);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
461 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
462 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
463
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
464 /* Macro to define debug(vec<T>) and debug(vec<T, va_gc>) helper
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
465 functions for a type T. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
466
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
467 #define DEFINE_DEBUG_VEC(T) \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
468 template void debug_helper (vec<T> &); \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
469 template void debug_helper (vec<T, va_gc> &); \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
470 /* Define the vec<T> debug functions. */ \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
471 DEBUG_FUNCTION void \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
472 debug (vec<T> &ref) \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
473 { \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
474 debug_helper <T> (ref); \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
475 } \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
476 DEBUG_FUNCTION void \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
477 debug (vec<T> *ptr) \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
478 { \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
479 if (ptr) \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
480 debug (*ptr); \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
481 else \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
482 fprintf (stderr, "<nil>\n"); \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
483 } \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
484 /* Define the vec<T, va_gc> debug functions. */ \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
485 DEBUG_FUNCTION void \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
486 debug (vec<T, va_gc> &ref) \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
487 { \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
488 debug_helper <T> (ref); \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
489 } \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
490 DEBUG_FUNCTION void \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
491 debug (vec<T, va_gc> *ptr) \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
492 { \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
493 if (ptr) \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
494 debug (*ptr); \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
495 else \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
496 fprintf (stderr, "<nil>\n"); \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
497 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
498
111
kono
parents: 67
diff changeset
499 /* Default-construct N elements in DST. */
kono
parents: 67
diff changeset
500
kono
parents: 67
diff changeset
501 template <typename T>
kono
parents: 67
diff changeset
502 inline void
kono
parents: 67
diff changeset
503 vec_default_construct (T *dst, unsigned n)
kono
parents: 67
diff changeset
504 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
505 #ifdef BROKEN_VALUE_INITIALIZATION
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
506 /* Versions of GCC before 4.4 sometimes leave certain objects
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
507 uninitialized when value initialized, though if the type has
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
508 user defined default ctor, that ctor is invoked. As a workaround
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
509 perform clearing first and then the value initialization, which
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
510 fixes the case when value initialization doesn't initialize due to
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
511 the bugs and should initialize to all zeros, but still allows
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
512 vectors for types with user defined default ctor that initializes
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
513 some or all elements to non-zero. If T has no user defined
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
514 default ctor and some non-static data members have user defined
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
515 default ctors that initialize to non-zero the workaround will
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
516 still not work properly; in that case we just need to provide
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
517 user defined default ctor. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
518 memset (dst, '\0', sizeof (T) * n);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
519 #endif
111
kono
parents: 67
diff changeset
520 for ( ; n; ++dst, --n)
kono
parents: 67
diff changeset
521 ::new (static_cast<void*>(dst)) T ();
kono
parents: 67
diff changeset
522 }
kono
parents: 67
diff changeset
523
kono
parents: 67
diff changeset
524 /* Copy-construct N elements in DST from *SRC. */
kono
parents: 67
diff changeset
525
kono
parents: 67
diff changeset
526 template <typename T>
kono
parents: 67
diff changeset
527 inline void
kono
parents: 67
diff changeset
528 vec_copy_construct (T *dst, const T *src, unsigned n)
kono
parents: 67
diff changeset
529 {
kono
parents: 67
diff changeset
530 for ( ; n; ++dst, ++src, --n)
kono
parents: 67
diff changeset
531 ::new (static_cast<void*>(dst)) T (*src);
kono
parents: 67
diff changeset
532 }
kono
parents: 67
diff changeset
533
kono
parents: 67
diff changeset
534 /* Type to provide NULL values for vec<T, A, L>. This is used to
kono
parents: 67
diff changeset
535 provide nil initializers for vec instances. Since vec must be
kono
parents: 67
diff changeset
536 a POD, we cannot have proper ctor/dtor for it. To initialize
kono
parents: 67
diff changeset
537 a vec instance, you can assign it the value vNULL. This isn't
kono
parents: 67
diff changeset
538 needed for file-scope and function-local static vectors, which
kono
parents: 67
diff changeset
539 are zero-initialized by default. */
kono
parents: 67
diff changeset
540 struct vnull
kono
parents: 67
diff changeset
541 {
kono
parents: 67
diff changeset
542 template <typename T, typename A, typename L>
kono
parents: 67
diff changeset
543 CONSTEXPR operator vec<T, A, L> () { return vec<T, A, L>(); }
kono
parents: 67
diff changeset
544 };
kono
parents: 67
diff changeset
545 extern vnull vNULL;
kono
parents: 67
diff changeset
546
kono
parents: 67
diff changeset
547
kono
parents: 67
diff changeset
548 /* Embeddable vector. These vectors are suitable to be embedded
kono
parents: 67
diff changeset
549 in other data structures so that they can be pre-allocated in a
kono
parents: 67
diff changeset
550 contiguous memory block.
kono
parents: 67
diff changeset
551
kono
parents: 67
diff changeset
552 Embeddable vectors are implemented using the trailing array idiom,
kono
parents: 67
diff changeset
553 thus they are not resizeable without changing the address of the
kono
parents: 67
diff changeset
554 vector object itself. This means you cannot have variables or
kono
parents: 67
diff changeset
555 fields of embeddable vector type -- always use a pointer to a
kono
parents: 67
diff changeset
556 vector. The one exception is the final field of a structure, which
kono
parents: 67
diff changeset
557 could be a vector type.
kono
parents: 67
diff changeset
558
kono
parents: 67
diff changeset
559 You will have to use the embedded_size & embedded_init calls to
kono
parents: 67
diff changeset
560 create such objects, and they will not be resizeable (so the 'safe'
kono
parents: 67
diff changeset
561 allocation variants are not available).
kono
parents: 67
diff changeset
562
kono
parents: 67
diff changeset
563 Properties:
kono
parents: 67
diff changeset
564
kono
parents: 67
diff changeset
565 - The whole vector and control data are allocated in a single
kono
parents: 67
diff changeset
566 contiguous block. It uses the trailing-vector idiom, so
kono
parents: 67
diff changeset
567 allocation must reserve enough space for all the elements
kono
parents: 67
diff changeset
568 in the vector plus its control data.
kono
parents: 67
diff changeset
569 - The vector cannot be re-allocated.
kono
parents: 67
diff changeset
570 - The vector cannot grow nor shrink.
kono
parents: 67
diff changeset
571 - No indirections needed for access/manipulation.
kono
parents: 67
diff changeset
572 - It requires 2 words of storage (prior to vector allocation). */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
573
111
kono
parents: 67
diff changeset
574 template<typename T, typename A>
kono
parents: 67
diff changeset
575 struct GTY((user)) vec<T, A, vl_embed>
kono
parents: 67
diff changeset
576 {
kono
parents: 67
diff changeset
577 public:
kono
parents: 67
diff changeset
578 unsigned allocated (void) const { return m_vecpfx.m_alloc; }
kono
parents: 67
diff changeset
579 unsigned length (void) const { return m_vecpfx.m_num; }
kono
parents: 67
diff changeset
580 bool is_empty (void) const { return m_vecpfx.m_num == 0; }
kono
parents: 67
diff changeset
581 T *address (void) { return m_vecdata; }
kono
parents: 67
diff changeset
582 const T *address (void) const { return m_vecdata; }
kono
parents: 67
diff changeset
583 T *begin () { return address (); }
kono
parents: 67
diff changeset
584 const T *begin () const { return address (); }
kono
parents: 67
diff changeset
585 T *end () { return address () + length (); }
kono
parents: 67
diff changeset
586 const T *end () const { return address () + length (); }
kono
parents: 67
diff changeset
587 const T &operator[] (unsigned) const;
kono
parents: 67
diff changeset
588 T &operator[] (unsigned);
kono
parents: 67
diff changeset
589 T &last (void);
kono
parents: 67
diff changeset
590 bool space (unsigned) const;
kono
parents: 67
diff changeset
591 bool iterate (unsigned, T *) const;
kono
parents: 67
diff changeset
592 bool iterate (unsigned, T **) const;
kono
parents: 67
diff changeset
593 vec *copy (ALONE_CXX_MEM_STAT_INFO) const;
kono
parents: 67
diff changeset
594 void splice (const vec &);
kono
parents: 67
diff changeset
595 void splice (const vec *src);
kono
parents: 67
diff changeset
596 T *quick_push (const T &);
kono
parents: 67
diff changeset
597 T &pop (void);
kono
parents: 67
diff changeset
598 void truncate (unsigned);
kono
parents: 67
diff changeset
599 void quick_insert (unsigned, const T &);
kono
parents: 67
diff changeset
600 void ordered_remove (unsigned);
kono
parents: 67
diff changeset
601 void unordered_remove (unsigned);
kono
parents: 67
diff changeset
602 void block_remove (unsigned, unsigned);
kono
parents: 67
diff changeset
603 void qsort (int (*) (const void *, const void *));
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
604 void sort (int (*) (const void *, const void *, void *), void *);
111
kono
parents: 67
diff changeset
605 T *bsearch (const void *key, int (*compar)(const void *, const void *));
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
606 T *bsearch (const void *key,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
607 int (*compar)(const void *, const void *, void *), void *);
111
kono
parents: 67
diff changeset
608 unsigned lower_bound (T, bool (*)(const T &, const T &)) const;
kono
parents: 67
diff changeset
609 bool contains (const T &search) const;
kono
parents: 67
diff changeset
610 static size_t embedded_size (unsigned);
kono
parents: 67
diff changeset
611 void embedded_init (unsigned, unsigned = 0, unsigned = 0);
kono
parents: 67
diff changeset
612 void quick_grow (unsigned len);
kono
parents: 67
diff changeset
613 void quick_grow_cleared (unsigned len);
kono
parents: 67
diff changeset
614
kono
parents: 67
diff changeset
615 /* vec class can access our internal data and functions. */
kono
parents: 67
diff changeset
616 template <typename, typename, typename> friend struct vec;
kono
parents: 67
diff changeset
617
kono
parents: 67
diff changeset
618 /* The allocator types also need access to our internals. */
kono
parents: 67
diff changeset
619 friend struct va_gc;
kono
parents: 67
diff changeset
620 friend struct va_gc_atomic;
kono
parents: 67
diff changeset
621 friend struct va_heap;
kono
parents: 67
diff changeset
622
kono
parents: 67
diff changeset
623 /* FIXME - These fields should be private, but we need to cater to
kono
parents: 67
diff changeset
624 compilers that have stricter notions of PODness for types. */
kono
parents: 67
diff changeset
625 vec_prefix m_vecpfx;
kono
parents: 67
diff changeset
626 T m_vecdata[1];
kono
parents: 67
diff changeset
627 };
kono
parents: 67
diff changeset
628
kono
parents: 67
diff changeset
629
kono
parents: 67
diff changeset
630 /* Convenience wrapper functions to use when dealing with pointers to
kono
parents: 67
diff changeset
631 embedded vectors. Some functionality for these vectors must be
kono
parents: 67
diff changeset
632 provided via free functions for these reasons:
kono
parents: 67
diff changeset
633
kono
parents: 67
diff changeset
634 1- The pointer may be NULL (e.g., before initial allocation).
kono
parents: 67
diff changeset
635
kono
parents: 67
diff changeset
636 2- When the vector needs to grow, it must be reallocated, so
kono
parents: 67
diff changeset
637 the pointer will change its value.
kono
parents: 67
diff changeset
638
kono
parents: 67
diff changeset
639 Because of limitations with the current GC machinery, all vectors
kono
parents: 67
diff changeset
640 in GC memory *must* be pointers. */
kono
parents: 67
diff changeset
641
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
642
111
kono
parents: 67
diff changeset
643 /* If V contains no room for NELEMS elements, return false. Otherwise,
kono
parents: 67
diff changeset
644 return true. */
kono
parents: 67
diff changeset
645 template<typename T, typename A>
kono
parents: 67
diff changeset
646 inline bool
kono
parents: 67
diff changeset
647 vec_safe_space (const vec<T, A, vl_embed> *v, unsigned nelems)
kono
parents: 67
diff changeset
648 {
kono
parents: 67
diff changeset
649 return v ? v->space (nelems) : nelems == 0;
kono
parents: 67
diff changeset
650 }
kono
parents: 67
diff changeset
651
kono
parents: 67
diff changeset
652
kono
parents: 67
diff changeset
653 /* If V is NULL, return 0. Otherwise, return V->length(). */
kono
parents: 67
diff changeset
654 template<typename T, typename A>
kono
parents: 67
diff changeset
655 inline unsigned
kono
parents: 67
diff changeset
656 vec_safe_length (const vec<T, A, vl_embed> *v)
kono
parents: 67
diff changeset
657 {
kono
parents: 67
diff changeset
658 return v ? v->length () : 0;
kono
parents: 67
diff changeset
659 }
kono
parents: 67
diff changeset
660
kono
parents: 67
diff changeset
661
kono
parents: 67
diff changeset
662 /* If V is NULL, return NULL. Otherwise, return V->address(). */
kono
parents: 67
diff changeset
663 template<typename T, typename A>
kono
parents: 67
diff changeset
664 inline T *
kono
parents: 67
diff changeset
665 vec_safe_address (vec<T, A, vl_embed> *v)
kono
parents: 67
diff changeset
666 {
kono
parents: 67
diff changeset
667 return v ? v->address () : NULL;
kono
parents: 67
diff changeset
668 }
kono
parents: 67
diff changeset
669
kono
parents: 67
diff changeset
670
kono
parents: 67
diff changeset
671 /* If V is NULL, return true. Otherwise, return V->is_empty(). */
kono
parents: 67
diff changeset
672 template<typename T, typename A>
kono
parents: 67
diff changeset
673 inline bool
kono
parents: 67
diff changeset
674 vec_safe_is_empty (vec<T, A, vl_embed> *v)
kono
parents: 67
diff changeset
675 {
kono
parents: 67
diff changeset
676 return v ? v->is_empty () : true;
kono
parents: 67
diff changeset
677 }
kono
parents: 67
diff changeset
678
kono
parents: 67
diff changeset
679 /* If V does not have space for NELEMS elements, call
kono
parents: 67
diff changeset
680 V->reserve(NELEMS, EXACT). */
kono
parents: 67
diff changeset
681 template<typename T, typename A>
kono
parents: 67
diff changeset
682 inline bool
kono
parents: 67
diff changeset
683 vec_safe_reserve (vec<T, A, vl_embed> *&v, unsigned nelems, bool exact = false
kono
parents: 67
diff changeset
684 CXX_MEM_STAT_INFO)
kono
parents: 67
diff changeset
685 {
kono
parents: 67
diff changeset
686 bool extend = nelems ? !vec_safe_space (v, nelems) : false;
kono
parents: 67
diff changeset
687 if (extend)
kono
parents: 67
diff changeset
688 A::reserve (v, nelems, exact PASS_MEM_STAT);
kono
parents: 67
diff changeset
689 return extend;
kono
parents: 67
diff changeset
690 }
kono
parents: 67
diff changeset
691
kono
parents: 67
diff changeset
692 template<typename T, typename A>
kono
parents: 67
diff changeset
693 inline bool
kono
parents: 67
diff changeset
694 vec_safe_reserve_exact (vec<T, A, vl_embed> *&v, unsigned nelems
kono
parents: 67
diff changeset
695 CXX_MEM_STAT_INFO)
kono
parents: 67
diff changeset
696 {
kono
parents: 67
diff changeset
697 return vec_safe_reserve (v, nelems, true PASS_MEM_STAT);
kono
parents: 67
diff changeset
698 }
kono
parents: 67
diff changeset
699
kono
parents: 67
diff changeset
700
kono
parents: 67
diff changeset
701 /* Allocate GC memory for V with space for NELEMS slots. If NELEMS
kono
parents: 67
diff changeset
702 is 0, V is initialized to NULL. */
kono
parents: 67
diff changeset
703
kono
parents: 67
diff changeset
704 template<typename T, typename A>
kono
parents: 67
diff changeset
705 inline void
kono
parents: 67
diff changeset
706 vec_alloc (vec<T, A, vl_embed> *&v, unsigned nelems CXX_MEM_STAT_INFO)
kono
parents: 67
diff changeset
707 {
kono
parents: 67
diff changeset
708 v = NULL;
kono
parents: 67
diff changeset
709 vec_safe_reserve (v, nelems, false PASS_MEM_STAT);
kono
parents: 67
diff changeset
710 }
kono
parents: 67
diff changeset
711
kono
parents: 67
diff changeset
712
kono
parents: 67
diff changeset
713 /* Free the GC memory allocated by vector V and set it to NULL. */
kono
parents: 67
diff changeset
714
kono
parents: 67
diff changeset
715 template<typename T, typename A>
kono
parents: 67
diff changeset
716 inline void
kono
parents: 67
diff changeset
717 vec_free (vec<T, A, vl_embed> *&v)
kono
parents: 67
diff changeset
718 {
kono
parents: 67
diff changeset
719 A::release (v);
kono
parents: 67
diff changeset
720 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
721
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
722
111
kono
parents: 67
diff changeset
723 /* Grow V to length LEN. Allocate it, if necessary. */
kono
parents: 67
diff changeset
724 template<typename T, typename A>
kono
parents: 67
diff changeset
725 inline void
kono
parents: 67
diff changeset
726 vec_safe_grow (vec<T, A, vl_embed> *&v, unsigned len CXX_MEM_STAT_INFO)
kono
parents: 67
diff changeset
727 {
kono
parents: 67
diff changeset
728 unsigned oldlen = vec_safe_length (v);
kono
parents: 67
diff changeset
729 gcc_checking_assert (len >= oldlen);
kono
parents: 67
diff changeset
730 vec_safe_reserve_exact (v, len - oldlen PASS_MEM_STAT);
kono
parents: 67
diff changeset
731 v->quick_grow (len);
kono
parents: 67
diff changeset
732 }
kono
parents: 67
diff changeset
733
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
734
111
kono
parents: 67
diff changeset
735 /* If V is NULL, allocate it. Call V->safe_grow_cleared(LEN). */
kono
parents: 67
diff changeset
736 template<typename T, typename A>
kono
parents: 67
diff changeset
737 inline void
kono
parents: 67
diff changeset
738 vec_safe_grow_cleared (vec<T, A, vl_embed> *&v, unsigned len CXX_MEM_STAT_INFO)
kono
parents: 67
diff changeset
739 {
kono
parents: 67
diff changeset
740 unsigned oldlen = vec_safe_length (v);
kono
parents: 67
diff changeset
741 vec_safe_grow (v, len PASS_MEM_STAT);
kono
parents: 67
diff changeset
742 vec_default_construct (v->address () + oldlen, len - oldlen);
kono
parents: 67
diff changeset
743 }
kono
parents: 67
diff changeset
744
kono
parents: 67
diff changeset
745
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
746 /* Assume V is not NULL. */
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
747
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
748 template<typename T>
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
749 inline void
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
750 vec_safe_grow_cleared (vec<T, va_heap, vl_ptr> *&v,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
751 unsigned len CXX_MEM_STAT_INFO)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
752 {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
753 v->safe_grow_cleared (len PASS_MEM_STAT);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
754 }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
755
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
756
111
kono
parents: 67
diff changeset
757 /* If V is NULL return false, otherwise return V->iterate(IX, PTR). */
kono
parents: 67
diff changeset
758 template<typename T, typename A>
kono
parents: 67
diff changeset
759 inline bool
kono
parents: 67
diff changeset
760 vec_safe_iterate (const vec<T, A, vl_embed> *v, unsigned ix, T **ptr)
kono
parents: 67
diff changeset
761 {
kono
parents: 67
diff changeset
762 if (v)
kono
parents: 67
diff changeset
763 return v->iterate (ix, ptr);
kono
parents: 67
diff changeset
764 else
kono
parents: 67
diff changeset
765 {
kono
parents: 67
diff changeset
766 *ptr = 0;
kono
parents: 67
diff changeset
767 return false;
kono
parents: 67
diff changeset
768 }
kono
parents: 67
diff changeset
769 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
770
111
kono
parents: 67
diff changeset
771 template<typename T, typename A>
kono
parents: 67
diff changeset
772 inline bool
kono
parents: 67
diff changeset
773 vec_safe_iterate (const vec<T, A, vl_embed> *v, unsigned ix, T *ptr)
kono
parents: 67
diff changeset
774 {
kono
parents: 67
diff changeset
775 if (v)
kono
parents: 67
diff changeset
776 return v->iterate (ix, ptr);
kono
parents: 67
diff changeset
777 else
kono
parents: 67
diff changeset
778 {
kono
parents: 67
diff changeset
779 *ptr = 0;
kono
parents: 67
diff changeset
780 return false;
kono
parents: 67
diff changeset
781 }
kono
parents: 67
diff changeset
782 }
kono
parents: 67
diff changeset
783
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
784
111
kono
parents: 67
diff changeset
785 /* If V has no room for one more element, reallocate it. Then call
kono
parents: 67
diff changeset
786 V->quick_push(OBJ). */
kono
parents: 67
diff changeset
787 template<typename T, typename A>
kono
parents: 67
diff changeset
788 inline T *
kono
parents: 67
diff changeset
789 vec_safe_push (vec<T, A, vl_embed> *&v, const T &obj CXX_MEM_STAT_INFO)
kono
parents: 67
diff changeset
790 {
kono
parents: 67
diff changeset
791 vec_safe_reserve (v, 1, false PASS_MEM_STAT);
kono
parents: 67
diff changeset
792 return v->quick_push (obj);
kono
parents: 67
diff changeset
793 }
kono
parents: 67
diff changeset
794
kono
parents: 67
diff changeset
795
kono
parents: 67
diff changeset
796 /* if V has no room for one more element, reallocate it. Then call
kono
parents: 67
diff changeset
797 V->quick_insert(IX, OBJ). */
kono
parents: 67
diff changeset
798 template<typename T, typename A>
kono
parents: 67
diff changeset
799 inline void
kono
parents: 67
diff changeset
800 vec_safe_insert (vec<T, A, vl_embed> *&v, unsigned ix, const T &obj
kono
parents: 67
diff changeset
801 CXX_MEM_STAT_INFO)
kono
parents: 67
diff changeset
802 {
kono
parents: 67
diff changeset
803 vec_safe_reserve (v, 1, false PASS_MEM_STAT);
kono
parents: 67
diff changeset
804 v->quick_insert (ix, obj);
kono
parents: 67
diff changeset
805 }
kono
parents: 67
diff changeset
806
kono
parents: 67
diff changeset
807
kono
parents: 67
diff changeset
808 /* If V is NULL, do nothing. Otherwise, call V->truncate(SIZE). */
kono
parents: 67
diff changeset
809 template<typename T, typename A>
kono
parents: 67
diff changeset
810 inline void
kono
parents: 67
diff changeset
811 vec_safe_truncate (vec<T, A, vl_embed> *v, unsigned size)
kono
parents: 67
diff changeset
812 {
kono
parents: 67
diff changeset
813 if (v)
kono
parents: 67
diff changeset
814 v->truncate (size);
kono
parents: 67
diff changeset
815 }
kono
parents: 67
diff changeset
816
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
817
111
kono
parents: 67
diff changeset
818 /* If SRC is not NULL, return a pointer to a copy of it. */
kono
parents: 67
diff changeset
819 template<typename T, typename A>
kono
parents: 67
diff changeset
820 inline vec<T, A, vl_embed> *
kono
parents: 67
diff changeset
821 vec_safe_copy (vec<T, A, vl_embed> *src CXX_MEM_STAT_INFO)
kono
parents: 67
diff changeset
822 {
kono
parents: 67
diff changeset
823 return src ? src->copy (ALONE_PASS_MEM_STAT) : NULL;
kono
parents: 67
diff changeset
824 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
825
111
kono
parents: 67
diff changeset
826 /* Copy the elements from SRC to the end of DST as if by memcpy.
kono
parents: 67
diff changeset
827 Reallocate DST, if necessary. */
kono
parents: 67
diff changeset
828 template<typename T, typename A>
kono
parents: 67
diff changeset
829 inline void
kono
parents: 67
diff changeset
830 vec_safe_splice (vec<T, A, vl_embed> *&dst, const vec<T, A, vl_embed> *src
kono
parents: 67
diff changeset
831 CXX_MEM_STAT_INFO)
kono
parents: 67
diff changeset
832 {
kono
parents: 67
diff changeset
833 unsigned src_len = vec_safe_length (src);
kono
parents: 67
diff changeset
834 if (src_len)
kono
parents: 67
diff changeset
835 {
kono
parents: 67
diff changeset
836 vec_safe_reserve_exact (dst, vec_safe_length (dst) + src_len
kono
parents: 67
diff changeset
837 PASS_MEM_STAT);
kono
parents: 67
diff changeset
838 dst->splice (*src);
kono
parents: 67
diff changeset
839 }
kono
parents: 67
diff changeset
840 }
kono
parents: 67
diff changeset
841
kono
parents: 67
diff changeset
842 /* Return true if SEARCH is an element of V. Note that this is O(N) in the
kono
parents: 67
diff changeset
843 size of the vector and so should be used with care. */
kono
parents: 67
diff changeset
844
kono
parents: 67
diff changeset
845 template<typename T, typename A>
kono
parents: 67
diff changeset
846 inline bool
kono
parents: 67
diff changeset
847 vec_safe_contains (vec<T, A, vl_embed> *v, const T &search)
kono
parents: 67
diff changeset
848 {
kono
parents: 67
diff changeset
849 return v ? v->contains (search) : false;
kono
parents: 67
diff changeset
850 }
kono
parents: 67
diff changeset
851
kono
parents: 67
diff changeset
852 /* Index into vector. Return the IX'th element. IX must be in the
kono
parents: 67
diff changeset
853 domain of the vector. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
854
111
kono
parents: 67
diff changeset
855 template<typename T, typename A>
kono
parents: 67
diff changeset
856 inline const T &
kono
parents: 67
diff changeset
857 vec<T, A, vl_embed>::operator[] (unsigned ix) const
kono
parents: 67
diff changeset
858 {
kono
parents: 67
diff changeset
859 gcc_checking_assert (ix < m_vecpfx.m_num);
kono
parents: 67
diff changeset
860 return m_vecdata[ix];
kono
parents: 67
diff changeset
861 }
kono
parents: 67
diff changeset
862
kono
parents: 67
diff changeset
863 template<typename T, typename A>
kono
parents: 67
diff changeset
864 inline T &
kono
parents: 67
diff changeset
865 vec<T, A, vl_embed>::operator[] (unsigned ix)
kono
parents: 67
diff changeset
866 {
kono
parents: 67
diff changeset
867 gcc_checking_assert (ix < m_vecpfx.m_num);
kono
parents: 67
diff changeset
868 return m_vecdata[ix];
kono
parents: 67
diff changeset
869 }
kono
parents: 67
diff changeset
870
kono
parents: 67
diff changeset
871
kono
parents: 67
diff changeset
872 /* Get the final element of the vector, which must not be empty. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
873
111
kono
parents: 67
diff changeset
874 template<typename T, typename A>
kono
parents: 67
diff changeset
875 inline T &
kono
parents: 67
diff changeset
876 vec<T, A, vl_embed>::last (void)
kono
parents: 67
diff changeset
877 {
kono
parents: 67
diff changeset
878 gcc_checking_assert (m_vecpfx.m_num > 0);
kono
parents: 67
diff changeset
879 return (*this)[m_vecpfx.m_num - 1];
kono
parents: 67
diff changeset
880 }
kono
parents: 67
diff changeset
881
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
882
111
kono
parents: 67
diff changeset
883 /* If this vector has space for NELEMS additional entries, return
kono
parents: 67
diff changeset
884 true. You usually only need to use this if you are doing your
kono
parents: 67
diff changeset
885 own vector reallocation, for instance on an embedded vector. This
kono
parents: 67
diff changeset
886 returns true in exactly the same circumstances that vec::reserve
kono
parents: 67
diff changeset
887 will. */
kono
parents: 67
diff changeset
888
kono
parents: 67
diff changeset
889 template<typename T, typename A>
kono
parents: 67
diff changeset
890 inline bool
kono
parents: 67
diff changeset
891 vec<T, A, vl_embed>::space (unsigned nelems) const
kono
parents: 67
diff changeset
892 {
kono
parents: 67
diff changeset
893 return m_vecpfx.m_alloc - m_vecpfx.m_num >= nelems;
kono
parents: 67
diff changeset
894 }
kono
parents: 67
diff changeset
895
kono
parents: 67
diff changeset
896
kono
parents: 67
diff changeset
897 /* Return iteration condition and update PTR to point to the IX'th
kono
parents: 67
diff changeset
898 element of this vector. Use this to iterate over the elements of a
kono
parents: 67
diff changeset
899 vector as follows,
kono
parents: 67
diff changeset
900
kono
parents: 67
diff changeset
901 for (ix = 0; vec<T, A>::iterate (v, ix, &ptr); ix++)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
902 continue; */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
903
111
kono
parents: 67
diff changeset
904 template<typename T, typename A>
kono
parents: 67
diff changeset
905 inline bool
kono
parents: 67
diff changeset
906 vec<T, A, vl_embed>::iterate (unsigned ix, T *ptr) const
kono
parents: 67
diff changeset
907 {
kono
parents: 67
diff changeset
908 if (ix < m_vecpfx.m_num)
kono
parents: 67
diff changeset
909 {
kono
parents: 67
diff changeset
910 *ptr = m_vecdata[ix];
kono
parents: 67
diff changeset
911 return true;
kono
parents: 67
diff changeset
912 }
kono
parents: 67
diff changeset
913 else
kono
parents: 67
diff changeset
914 {
kono
parents: 67
diff changeset
915 *ptr = 0;
kono
parents: 67
diff changeset
916 return false;
kono
parents: 67
diff changeset
917 }
kono
parents: 67
diff changeset
918 }
kono
parents: 67
diff changeset
919
kono
parents: 67
diff changeset
920
kono
parents: 67
diff changeset
921 /* Return iteration condition and update *PTR to point to the
kono
parents: 67
diff changeset
922 IX'th element of this vector. Use this to iterate over the
kono
parents: 67
diff changeset
923 elements of a vector as follows,
kono
parents: 67
diff changeset
924
kono
parents: 67
diff changeset
925 for (ix = 0; v->iterate (ix, &ptr); ix++)
kono
parents: 67
diff changeset
926 continue;
kono
parents: 67
diff changeset
927
kono
parents: 67
diff changeset
928 This variant is for vectors of objects. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
929
111
kono
parents: 67
diff changeset
930 template<typename T, typename A>
kono
parents: 67
diff changeset
931 inline bool
kono
parents: 67
diff changeset
932 vec<T, A, vl_embed>::iterate (unsigned ix, T **ptr) const
kono
parents: 67
diff changeset
933 {
kono
parents: 67
diff changeset
934 if (ix < m_vecpfx.m_num)
kono
parents: 67
diff changeset
935 {
kono
parents: 67
diff changeset
936 *ptr = CONST_CAST (T *, &m_vecdata[ix]);
kono
parents: 67
diff changeset
937 return true;
kono
parents: 67
diff changeset
938 }
kono
parents: 67
diff changeset
939 else
kono
parents: 67
diff changeset
940 {
kono
parents: 67
diff changeset
941 *ptr = 0;
kono
parents: 67
diff changeset
942 return false;
kono
parents: 67
diff changeset
943 }
kono
parents: 67
diff changeset
944 }
kono
parents: 67
diff changeset
945
kono
parents: 67
diff changeset
946
kono
parents: 67
diff changeset
947 /* Return a pointer to a copy of this vector. */
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
948
111
kono
parents: 67
diff changeset
949 template<typename T, typename A>
kono
parents: 67
diff changeset
950 inline vec<T, A, vl_embed> *
kono
parents: 67
diff changeset
951 vec<T, A, vl_embed>::copy (ALONE_MEM_STAT_DECL) const
kono
parents: 67
diff changeset
952 {
kono
parents: 67
diff changeset
953 vec<T, A, vl_embed> *new_vec = NULL;
kono
parents: 67
diff changeset
954 unsigned len = length ();
kono
parents: 67
diff changeset
955 if (len)
kono
parents: 67
diff changeset
956 {
kono
parents: 67
diff changeset
957 vec_alloc (new_vec, len PASS_MEM_STAT);
kono
parents: 67
diff changeset
958 new_vec->embedded_init (len, len);
kono
parents: 67
diff changeset
959 vec_copy_construct (new_vec->address (), m_vecdata, len);
kono
parents: 67
diff changeset
960 }
kono
parents: 67
diff changeset
961 return new_vec;
kono
parents: 67
diff changeset
962 }
kono
parents: 67
diff changeset
963
kono
parents: 67
diff changeset
964
kono
parents: 67
diff changeset
965 /* Copy the elements from SRC to the end of this vector as if by memcpy.
kono
parents: 67
diff changeset
966 The vector must have sufficient headroom available. */
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
967
111
kono
parents: 67
diff changeset
968 template<typename T, typename A>
kono
parents: 67
diff changeset
969 inline void
kono
parents: 67
diff changeset
970 vec<T, A, vl_embed>::splice (const vec<T, A, vl_embed> &src)
kono
parents: 67
diff changeset
971 {
kono
parents: 67
diff changeset
972 unsigned len = src.length ();
kono
parents: 67
diff changeset
973 if (len)
kono
parents: 67
diff changeset
974 {
kono
parents: 67
diff changeset
975 gcc_checking_assert (space (len));
kono
parents: 67
diff changeset
976 vec_copy_construct (end (), src.address (), len);
kono
parents: 67
diff changeset
977 m_vecpfx.m_num += len;
kono
parents: 67
diff changeset
978 }
kono
parents: 67
diff changeset
979 }
kono
parents: 67
diff changeset
980
kono
parents: 67
diff changeset
981 template<typename T, typename A>
kono
parents: 67
diff changeset
982 inline void
kono
parents: 67
diff changeset
983 vec<T, A, vl_embed>::splice (const vec<T, A, vl_embed> *src)
kono
parents: 67
diff changeset
984 {
kono
parents: 67
diff changeset
985 if (src)
kono
parents: 67
diff changeset
986 splice (*src);
kono
parents: 67
diff changeset
987 }
kono
parents: 67
diff changeset
988
kono
parents: 67
diff changeset
989
kono
parents: 67
diff changeset
990 /* Push OBJ (a new element) onto the end of the vector. There must be
kono
parents: 67
diff changeset
991 sufficient space in the vector. Return a pointer to the slot
kono
parents: 67
diff changeset
992 where OBJ was inserted. */
kono
parents: 67
diff changeset
993
kono
parents: 67
diff changeset
994 template<typename T, typename A>
kono
parents: 67
diff changeset
995 inline T *
kono
parents: 67
diff changeset
996 vec<T, A, vl_embed>::quick_push (const T &obj)
kono
parents: 67
diff changeset
997 {
kono
parents: 67
diff changeset
998 gcc_checking_assert (space (1));
kono
parents: 67
diff changeset
999 T *slot = &m_vecdata[m_vecpfx.m_num++];
kono
parents: 67
diff changeset
1000 *slot = obj;
kono
parents: 67
diff changeset
1001 return slot;
kono
parents: 67
diff changeset
1002 }
kono
parents: 67
diff changeset
1003
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1004
111
kono
parents: 67
diff changeset
1005 /* Pop and return the last element off the end of the vector. */
kono
parents: 67
diff changeset
1006
kono
parents: 67
diff changeset
1007 template<typename T, typename A>
kono
parents: 67
diff changeset
1008 inline T &
kono
parents: 67
diff changeset
1009 vec<T, A, vl_embed>::pop (void)
kono
parents: 67
diff changeset
1010 {
kono
parents: 67
diff changeset
1011 gcc_checking_assert (length () > 0);
kono
parents: 67
diff changeset
1012 return m_vecdata[--m_vecpfx.m_num];
kono
parents: 67
diff changeset
1013 }
kono
parents: 67
diff changeset
1014
kono
parents: 67
diff changeset
1015
kono
parents: 67
diff changeset
1016 /* Set the length of the vector to SIZE. The new length must be less
kono
parents: 67
diff changeset
1017 than or equal to the current length. This is an O(1) operation. */
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1018
111
kono
parents: 67
diff changeset
1019 template<typename T, typename A>
kono
parents: 67
diff changeset
1020 inline void
kono
parents: 67
diff changeset
1021 vec<T, A, vl_embed>::truncate (unsigned size)
kono
parents: 67
diff changeset
1022 {
kono
parents: 67
diff changeset
1023 gcc_checking_assert (length () >= size);
kono
parents: 67
diff changeset
1024 m_vecpfx.m_num = size;
kono
parents: 67
diff changeset
1025 }
kono
parents: 67
diff changeset
1026
kono
parents: 67
diff changeset
1027
kono
parents: 67
diff changeset
1028 /* Insert an element, OBJ, at the IXth position of this vector. There
kono
parents: 67
diff changeset
1029 must be sufficient space. */
kono
parents: 67
diff changeset
1030
kono
parents: 67
diff changeset
1031 template<typename T, typename A>
kono
parents: 67
diff changeset
1032 inline void
kono
parents: 67
diff changeset
1033 vec<T, A, vl_embed>::quick_insert (unsigned ix, const T &obj)
kono
parents: 67
diff changeset
1034 {
kono
parents: 67
diff changeset
1035 gcc_checking_assert (length () < allocated ());
kono
parents: 67
diff changeset
1036 gcc_checking_assert (ix <= length ());
kono
parents: 67
diff changeset
1037 T *slot = &m_vecdata[ix];
kono
parents: 67
diff changeset
1038 memmove (slot + 1, slot, (m_vecpfx.m_num++ - ix) * sizeof (T));
kono
parents: 67
diff changeset
1039 *slot = obj;
kono
parents: 67
diff changeset
1040 }
kono
parents: 67
diff changeset
1041
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1042
111
kono
parents: 67
diff changeset
1043 /* Remove an element from the IXth position of this vector. Ordering of
kono
parents: 67
diff changeset
1044 remaining elements is preserved. This is an O(N) operation due to
kono
parents: 67
diff changeset
1045 memmove. */
kono
parents: 67
diff changeset
1046
kono
parents: 67
diff changeset
1047 template<typename T, typename A>
kono
parents: 67
diff changeset
1048 inline void
kono
parents: 67
diff changeset
1049 vec<T, A, vl_embed>::ordered_remove (unsigned ix)
kono
parents: 67
diff changeset
1050 {
kono
parents: 67
diff changeset
1051 gcc_checking_assert (ix < length ());
kono
parents: 67
diff changeset
1052 T *slot = &m_vecdata[ix];
kono
parents: 67
diff changeset
1053 memmove (slot, slot + 1, (--m_vecpfx.m_num - ix) * sizeof (T));
kono
parents: 67
diff changeset
1054 }
kono
parents: 67
diff changeset
1055
kono
parents: 67
diff changeset
1056
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1057 /* Remove elements in [START, END) from VEC for which COND holds. Ordering of
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1058 remaining elements is preserved. This is an O(N) operation. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1059
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1060 #define VEC_ORDERED_REMOVE_IF_FROM_TO(vec, read_index, write_index, \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1061 elem_ptr, start, end, cond) \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1062 { \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1063 gcc_assert ((end) <= (vec).length ()); \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1064 for (read_index = write_index = (start); read_index < (end); \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1065 ++read_index) \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1066 { \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1067 elem_ptr = &(vec)[read_index]; \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1068 bool remove_p = (cond); \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1069 if (remove_p) \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1070 continue; \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1071 \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1072 if (read_index != write_index) \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1073 (vec)[write_index] = (vec)[read_index]; \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1074 \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1075 write_index++; \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1076 } \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1077 \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1078 if (read_index - write_index > 0) \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1079 (vec).block_remove (write_index, read_index - write_index); \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1080 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1081
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1082
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1083 /* Remove elements from VEC for which COND holds. Ordering of remaining
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1084 elements is preserved. This is an O(N) operation. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1085
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1086 #define VEC_ORDERED_REMOVE_IF(vec, read_index, write_index, elem_ptr, \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1087 cond) \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1088 VEC_ORDERED_REMOVE_IF_FROM_TO ((vec), read_index, write_index, \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1089 elem_ptr, 0, (vec).length (), (cond))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1090
111
kono
parents: 67
diff changeset
1091 /* Remove an element from the IXth position of this vector. Ordering of
kono
parents: 67
diff changeset
1092 remaining elements is destroyed. This is an O(1) operation. */
kono
parents: 67
diff changeset
1093
kono
parents: 67
diff changeset
1094 template<typename T, typename A>
kono
parents: 67
diff changeset
1095 inline void
kono
parents: 67
diff changeset
1096 vec<T, A, vl_embed>::unordered_remove (unsigned ix)
kono
parents: 67
diff changeset
1097 {
kono
parents: 67
diff changeset
1098 gcc_checking_assert (ix < length ());
kono
parents: 67
diff changeset
1099 m_vecdata[ix] = m_vecdata[--m_vecpfx.m_num];
kono
parents: 67
diff changeset
1100 }
kono
parents: 67
diff changeset
1101
kono
parents: 67
diff changeset
1102
kono
parents: 67
diff changeset
1103 /* Remove LEN elements starting at the IXth. Ordering is retained.
kono
parents: 67
diff changeset
1104 This is an O(N) operation due to memmove. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1105
111
kono
parents: 67
diff changeset
1106 template<typename T, typename A>
kono
parents: 67
diff changeset
1107 inline void
kono
parents: 67
diff changeset
1108 vec<T, A, vl_embed>::block_remove (unsigned ix, unsigned len)
kono
parents: 67
diff changeset
1109 {
kono
parents: 67
diff changeset
1110 gcc_checking_assert (ix + len <= length ());
kono
parents: 67
diff changeset
1111 T *slot = &m_vecdata[ix];
kono
parents: 67
diff changeset
1112 m_vecpfx.m_num -= len;
kono
parents: 67
diff changeset
1113 memmove (slot, slot + len, (m_vecpfx.m_num - ix) * sizeof (T));
kono
parents: 67
diff changeset
1114 }
kono
parents: 67
diff changeset
1115
kono
parents: 67
diff changeset
1116
kono
parents: 67
diff changeset
1117 /* Sort the contents of this vector with qsort. CMP is the comparison
kono
parents: 67
diff changeset
1118 function to pass to qsort. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1119
111
kono
parents: 67
diff changeset
1120 template<typename T, typename A>
kono
parents: 67
diff changeset
1121 inline void
kono
parents: 67
diff changeset
1122 vec<T, A, vl_embed>::qsort (int (*cmp) (const void *, const void *))
kono
parents: 67
diff changeset
1123 {
kono
parents: 67
diff changeset
1124 if (length () > 1)
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1125 gcc_qsort (address (), length (), sizeof (T), cmp);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1126 }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1127
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1128 /* Sort the contents of this vector with qsort. CMP is the comparison
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1129 function to pass to qsort. */
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1130
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1131 template<typename T, typename A>
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1132 inline void
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1133 vec<T, A, vl_embed>::sort (int (*cmp) (const void *, const void *, void *),
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1134 void *data)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1135 {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1136 if (length () > 1)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1137 gcc_sort_r (address (), length (), sizeof (T), cmp, data);
111
kono
parents: 67
diff changeset
1138 }
kono
parents: 67
diff changeset
1139
kono
parents: 67
diff changeset
1140
kono
parents: 67
diff changeset
1141 /* Search the contents of the sorted vector with a binary search.
kono
parents: 67
diff changeset
1142 CMP is the comparison function to pass to bsearch. */
kono
parents: 67
diff changeset
1143
kono
parents: 67
diff changeset
1144 template<typename T, typename A>
kono
parents: 67
diff changeset
1145 inline T *
kono
parents: 67
diff changeset
1146 vec<T, A, vl_embed>::bsearch (const void *key,
kono
parents: 67
diff changeset
1147 int (*compar) (const void *, const void *))
kono
parents: 67
diff changeset
1148 {
kono
parents: 67
diff changeset
1149 const void *base = this->address ();
kono
parents: 67
diff changeset
1150 size_t nmemb = this->length ();
kono
parents: 67
diff changeset
1151 size_t size = sizeof (T);
kono
parents: 67
diff changeset
1152 /* The following is a copy of glibc stdlib-bsearch.h. */
kono
parents: 67
diff changeset
1153 size_t l, u, idx;
kono
parents: 67
diff changeset
1154 const void *p;
kono
parents: 67
diff changeset
1155 int comparison;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1156
111
kono
parents: 67
diff changeset
1157 l = 0;
kono
parents: 67
diff changeset
1158 u = nmemb;
kono
parents: 67
diff changeset
1159 while (l < u)
kono
parents: 67
diff changeset
1160 {
kono
parents: 67
diff changeset
1161 idx = (l + u) / 2;
kono
parents: 67
diff changeset
1162 p = (const void *) (((const char *) base) + (idx * size));
kono
parents: 67
diff changeset
1163 comparison = (*compar) (key, p);
kono
parents: 67
diff changeset
1164 if (comparison < 0)
kono
parents: 67
diff changeset
1165 u = idx;
kono
parents: 67
diff changeset
1166 else if (comparison > 0)
kono
parents: 67
diff changeset
1167 l = idx + 1;
kono
parents: 67
diff changeset
1168 else
kono
parents: 67
diff changeset
1169 return (T *)const_cast<void *>(p);
kono
parents: 67
diff changeset
1170 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1171
111
kono
parents: 67
diff changeset
1172 return NULL;
kono
parents: 67
diff changeset
1173 }
kono
parents: 67
diff changeset
1174
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1175 /* Search the contents of the sorted vector with a binary search.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1176 CMP is the comparison function to pass to bsearch. */
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1177
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1178 template<typename T, typename A>
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1179 inline T *
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1180 vec<T, A, vl_embed>::bsearch (const void *key,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1181 int (*compar) (const void *, const void *,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1182 void *), void *data)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1183 {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1184 const void *base = this->address ();
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1185 size_t nmemb = this->length ();
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1186 size_t size = sizeof (T);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1187 /* The following is a copy of glibc stdlib-bsearch.h. */
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1188 size_t l, u, idx;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1189 const void *p;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1190 int comparison;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1191
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1192 l = 0;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1193 u = nmemb;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1194 while (l < u)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1195 {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1196 idx = (l + u) / 2;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1197 p = (const void *) (((const char *) base) + (idx * size));
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1198 comparison = (*compar) (key, p, data);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1199 if (comparison < 0)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1200 u = idx;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1201 else if (comparison > 0)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1202 l = idx + 1;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1203 else
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1204 return (T *)const_cast<void *>(p);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1205 }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1206
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1207 return NULL;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1208 }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1209
111
kono
parents: 67
diff changeset
1210 /* Return true if SEARCH is an element of V. Note that this is O(N) in the
kono
parents: 67
diff changeset
1211 size of the vector and so should be used with care. */
kono
parents: 67
diff changeset
1212
kono
parents: 67
diff changeset
1213 template<typename T, typename A>
kono
parents: 67
diff changeset
1214 inline bool
kono
parents: 67
diff changeset
1215 vec<T, A, vl_embed>::contains (const T &search) const
kono
parents: 67
diff changeset
1216 {
kono
parents: 67
diff changeset
1217 unsigned int len = length ();
kono
parents: 67
diff changeset
1218 for (unsigned int i = 0; i < len; i++)
kono
parents: 67
diff changeset
1219 if ((*this)[i] == search)
kono
parents: 67
diff changeset
1220 return true;
kono
parents: 67
diff changeset
1221
kono
parents: 67
diff changeset
1222 return false;
kono
parents: 67
diff changeset
1223 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1224
111
kono
parents: 67
diff changeset
1225 /* Find and return the first position in which OBJ could be inserted
kono
parents: 67
diff changeset
1226 without changing the ordering of this vector. LESSTHAN is a
kono
parents: 67
diff changeset
1227 function that returns true if the first argument is strictly less
kono
parents: 67
diff changeset
1228 than the second. */
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1229
111
kono
parents: 67
diff changeset
1230 template<typename T, typename A>
kono
parents: 67
diff changeset
1231 unsigned
kono
parents: 67
diff changeset
1232 vec<T, A, vl_embed>::lower_bound (T obj, bool (*lessthan)(const T &, const T &))
kono
parents: 67
diff changeset
1233 const
kono
parents: 67
diff changeset
1234 {
kono
parents: 67
diff changeset
1235 unsigned int len = length ();
kono
parents: 67
diff changeset
1236 unsigned int half, middle;
kono
parents: 67
diff changeset
1237 unsigned int first = 0;
kono
parents: 67
diff changeset
1238 while (len > 0)
kono
parents: 67
diff changeset
1239 {
kono
parents: 67
diff changeset
1240 half = len / 2;
kono
parents: 67
diff changeset
1241 middle = first;
kono
parents: 67
diff changeset
1242 middle += half;
kono
parents: 67
diff changeset
1243 T middle_elem = (*this)[middle];
kono
parents: 67
diff changeset
1244 if (lessthan (middle_elem, obj))
kono
parents: 67
diff changeset
1245 {
kono
parents: 67
diff changeset
1246 first = middle;
kono
parents: 67
diff changeset
1247 ++first;
kono
parents: 67
diff changeset
1248 len = len - half - 1;
kono
parents: 67
diff changeset
1249 }
kono
parents: 67
diff changeset
1250 else
kono
parents: 67
diff changeset
1251 len = half;
kono
parents: 67
diff changeset
1252 }
kono
parents: 67
diff changeset
1253 return first;
kono
parents: 67
diff changeset
1254 }
kono
parents: 67
diff changeset
1255
kono
parents: 67
diff changeset
1256
kono
parents: 67
diff changeset
1257 /* Return the number of bytes needed to embed an instance of an
kono
parents: 67
diff changeset
1258 embeddable vec inside another data structure.
kono
parents: 67
diff changeset
1259
kono
parents: 67
diff changeset
1260 Use these methods to determine the required size and initialization
kono
parents: 67
diff changeset
1261 of a vector V of type T embedded within another structure (as the
kono
parents: 67
diff changeset
1262 final member):
kono
parents: 67
diff changeset
1263
kono
parents: 67
diff changeset
1264 size_t vec<T, A, vl_embed>::embedded_size (unsigned alloc);
kono
parents: 67
diff changeset
1265 void v->embedded_init (unsigned alloc, unsigned num);
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1266
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1267 These allow the caller to perform the memory allocation. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1268
111
kono
parents: 67
diff changeset
1269 template<typename T, typename A>
kono
parents: 67
diff changeset
1270 inline size_t
kono
parents: 67
diff changeset
1271 vec<T, A, vl_embed>::embedded_size (unsigned alloc)
kono
parents: 67
diff changeset
1272 {
kono
parents: 67
diff changeset
1273 typedef vec<T, A, vl_embed> vec_embedded;
kono
parents: 67
diff changeset
1274 return offsetof (vec_embedded, m_vecdata) + alloc * sizeof (T);
kono
parents: 67
diff changeset
1275 }
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1276
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 63
diff changeset
1277
111
kono
parents: 67
diff changeset
1278 /* Initialize the vector to contain room for ALLOC elements and
kono
parents: 67
diff changeset
1279 NUM active elements. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1280
111
kono
parents: 67
diff changeset
1281 template<typename T, typename A>
kono
parents: 67
diff changeset
1282 inline void
kono
parents: 67
diff changeset
1283 vec<T, A, vl_embed>::embedded_init (unsigned alloc, unsigned num, unsigned aut)
kono
parents: 67
diff changeset
1284 {
kono
parents: 67
diff changeset
1285 m_vecpfx.m_alloc = alloc;
kono
parents: 67
diff changeset
1286 m_vecpfx.m_using_auto_storage = aut;
kono
parents: 67
diff changeset
1287 m_vecpfx.m_num = num;
kono
parents: 67
diff changeset
1288 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1289
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1290
111
kono
parents: 67
diff changeset
1291 /* Grow the vector to a specific length. LEN must be as long or longer than
kono
parents: 67
diff changeset
1292 the current length. The new elements are uninitialized. */
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1293
111
kono
parents: 67
diff changeset
1294 template<typename T, typename A>
kono
parents: 67
diff changeset
1295 inline void
kono
parents: 67
diff changeset
1296 vec<T, A, vl_embed>::quick_grow (unsigned len)
kono
parents: 67
diff changeset
1297 {
kono
parents: 67
diff changeset
1298 gcc_checking_assert (length () <= len && len <= m_vecpfx.m_alloc);
kono
parents: 67
diff changeset
1299 m_vecpfx.m_num = len;
kono
parents: 67
diff changeset
1300 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1301
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1302
111
kono
parents: 67
diff changeset
1303 /* Grow the vector to a specific length. LEN must be as long or longer than
kono
parents: 67
diff changeset
1304 the current length. The new elements are initialized to zero. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1305
111
kono
parents: 67
diff changeset
1306 template<typename T, typename A>
kono
parents: 67
diff changeset
1307 inline void
kono
parents: 67
diff changeset
1308 vec<T, A, vl_embed>::quick_grow_cleared (unsigned len)
kono
parents: 67
diff changeset
1309 {
kono
parents: 67
diff changeset
1310 unsigned oldlen = length ();
kono
parents: 67
diff changeset
1311 size_t growby = len - oldlen;
kono
parents: 67
diff changeset
1312 quick_grow (len);
kono
parents: 67
diff changeset
1313 if (growby != 0)
kono
parents: 67
diff changeset
1314 vec_default_construct (address () + oldlen, growby);
kono
parents: 67
diff changeset
1315 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1316
111
kono
parents: 67
diff changeset
1317 /* Garbage collection support for vec<T, A, vl_embed>. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1318
111
kono
parents: 67
diff changeset
1319 template<typename T>
kono
parents: 67
diff changeset
1320 void
kono
parents: 67
diff changeset
1321 gt_ggc_mx (vec<T, va_gc> *v)
kono
parents: 67
diff changeset
1322 {
kono
parents: 67
diff changeset
1323 extern void gt_ggc_mx (T &);
kono
parents: 67
diff changeset
1324 for (unsigned i = 0; i < v->length (); i++)
kono
parents: 67
diff changeset
1325 gt_ggc_mx ((*v)[i]);
kono
parents: 67
diff changeset
1326 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1327
111
kono
parents: 67
diff changeset
1328 template<typename T>
kono
parents: 67
diff changeset
1329 void
kono
parents: 67
diff changeset
1330 gt_ggc_mx (vec<T, va_gc_atomic, vl_embed> *v ATTRIBUTE_UNUSED)
kono
parents: 67
diff changeset
1331 {
kono
parents: 67
diff changeset
1332 /* Nothing to do. Vectors of atomic types wrt GC do not need to
kono
parents: 67
diff changeset
1333 be traversed. */
kono
parents: 67
diff changeset
1334 }
kono
parents: 67
diff changeset
1335
kono
parents: 67
diff changeset
1336
kono
parents: 67
diff changeset
1337 /* PCH support for vec<T, A, vl_embed>. */
kono
parents: 67
diff changeset
1338
kono
parents: 67
diff changeset
1339 template<typename T, typename A>
kono
parents: 67
diff changeset
1340 void
kono
parents: 67
diff changeset
1341 gt_pch_nx (vec<T, A, vl_embed> *v)
kono
parents: 67
diff changeset
1342 {
kono
parents: 67
diff changeset
1343 extern void gt_pch_nx (T &);
kono
parents: 67
diff changeset
1344 for (unsigned i = 0; i < v->length (); i++)
kono
parents: 67
diff changeset
1345 gt_pch_nx ((*v)[i]);
kono
parents: 67
diff changeset
1346 }
kono
parents: 67
diff changeset
1347
kono
parents: 67
diff changeset
1348 template<typename T, typename A>
kono
parents: 67
diff changeset
1349 void
kono
parents: 67
diff changeset
1350 gt_pch_nx (vec<T *, A, vl_embed> *v, gt_pointer_operator op, void *cookie)
kono
parents: 67
diff changeset
1351 {
kono
parents: 67
diff changeset
1352 for (unsigned i = 0; i < v->length (); i++)
kono
parents: 67
diff changeset
1353 op (&((*v)[i]), cookie);
kono
parents: 67
diff changeset
1354 }
kono
parents: 67
diff changeset
1355
kono
parents: 67
diff changeset
1356 template<typename T, typename A>
kono
parents: 67
diff changeset
1357 void
kono
parents: 67
diff changeset
1358 gt_pch_nx (vec<T, A, vl_embed> *v, gt_pointer_operator op, void *cookie)
kono
parents: 67
diff changeset
1359 {
kono
parents: 67
diff changeset
1360 extern void gt_pch_nx (T *, gt_pointer_operator, void *);
kono
parents: 67
diff changeset
1361 for (unsigned i = 0; i < v->length (); i++)
kono
parents: 67
diff changeset
1362 gt_pch_nx (&((*v)[i]), op, cookie);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1363 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1364
111
kono
parents: 67
diff changeset
1365
kono
parents: 67
diff changeset
1366 /* Space efficient vector. These vectors can grow dynamically and are
kono
parents: 67
diff changeset
1367 allocated together with their control data. They are suited to be
kono
parents: 67
diff changeset
1368 included in data structures. Prior to initial allocation, they
kono
parents: 67
diff changeset
1369 only take a single word of storage.
kono
parents: 67
diff changeset
1370
kono
parents: 67
diff changeset
1371 These vectors are implemented as a pointer to an embeddable vector.
kono
parents: 67
diff changeset
1372 The semantics allow for this pointer to be NULL to represent empty
kono
parents: 67
diff changeset
1373 vectors. This way, empty vectors occupy minimal space in the
kono
parents: 67
diff changeset
1374 structure containing them.
kono
parents: 67
diff changeset
1375
kono
parents: 67
diff changeset
1376 Properties:
kono
parents: 67
diff changeset
1377
kono
parents: 67
diff changeset
1378 - The whole vector and control data are allocated in a single
kono
parents: 67
diff changeset
1379 contiguous block.
kono
parents: 67
diff changeset
1380 - The whole vector may be re-allocated.
kono
parents: 67
diff changeset
1381 - Vector data may grow and shrink.
kono
parents: 67
diff changeset
1382 - Access and manipulation requires a pointer test and
kono
parents: 67
diff changeset
1383 indirection.
kono
parents: 67
diff changeset
1384 - It requires 1 word of storage (prior to vector allocation).
kono
parents: 67
diff changeset
1385
kono
parents: 67
diff changeset
1386
kono
parents: 67
diff changeset
1387 Limitations:
kono
parents: 67
diff changeset
1388
kono
parents: 67
diff changeset
1389 These vectors must be PODs because they are stored in unions.
kono
parents: 67
diff changeset
1390 (http://en.wikipedia.org/wiki/Plain_old_data_structures).
kono
parents: 67
diff changeset
1391 As long as we use C++03, we cannot have constructors nor
kono
parents: 67
diff changeset
1392 destructors in classes that are stored in unions. */
kono
parents: 67
diff changeset
1393
kono
parents: 67
diff changeset
1394 template<typename T>
kono
parents: 67
diff changeset
1395 struct vec<T, va_heap, vl_ptr>
kono
parents: 67
diff changeset
1396 {
kono
parents: 67
diff changeset
1397 public:
kono
parents: 67
diff changeset
1398 /* Memory allocation and deallocation for the embedded vector.
kono
parents: 67
diff changeset
1399 Needed because we cannot have proper ctors/dtors defined. */
kono
parents: 67
diff changeset
1400 void create (unsigned nelems CXX_MEM_STAT_INFO);
kono
parents: 67
diff changeset
1401 void release (void);
kono
parents: 67
diff changeset
1402
kono
parents: 67
diff changeset
1403 /* Vector operations. */
kono
parents: 67
diff changeset
1404 bool exists (void) const
kono
parents: 67
diff changeset
1405 { return m_vec != NULL; }
kono
parents: 67
diff changeset
1406
kono
parents: 67
diff changeset
1407 bool is_empty (void) const
kono
parents: 67
diff changeset
1408 { return m_vec ? m_vec->is_empty () : true; }
kono
parents: 67
diff changeset
1409
kono
parents: 67
diff changeset
1410 unsigned length (void) const
kono
parents: 67
diff changeset
1411 { return m_vec ? m_vec->length () : 0; }
kono
parents: 67
diff changeset
1412
kono
parents: 67
diff changeset
1413 T *address (void)
kono
parents: 67
diff changeset
1414 { return m_vec ? m_vec->m_vecdata : NULL; }
kono
parents: 67
diff changeset
1415
kono
parents: 67
diff changeset
1416 const T *address (void) const
kono
parents: 67
diff changeset
1417 { return m_vec ? m_vec->m_vecdata : NULL; }
kono
parents: 67
diff changeset
1418
kono
parents: 67
diff changeset
1419 T *begin () { return address (); }
kono
parents: 67
diff changeset
1420 const T *begin () const { return address (); }
kono
parents: 67
diff changeset
1421 T *end () { return begin () + length (); }
kono
parents: 67
diff changeset
1422 const T *end () const { return begin () + length (); }
kono
parents: 67
diff changeset
1423 const T &operator[] (unsigned ix) const
kono
parents: 67
diff changeset
1424 { return (*m_vec)[ix]; }
kono
parents: 67
diff changeset
1425
kono
parents: 67
diff changeset
1426 bool operator!=(const vec &other) const
kono
parents: 67
diff changeset
1427 { return !(*this == other); }
kono
parents: 67
diff changeset
1428
kono
parents: 67
diff changeset
1429 bool operator==(const vec &other) const
kono
parents: 67
diff changeset
1430 { return address () == other.address (); }
kono
parents: 67
diff changeset
1431
kono
parents: 67
diff changeset
1432 T &operator[] (unsigned ix)
kono
parents: 67
diff changeset
1433 { return (*m_vec)[ix]; }
kono
parents: 67
diff changeset
1434
kono
parents: 67
diff changeset
1435 T &last (void)
kono
parents: 67
diff changeset
1436 { return m_vec->last (); }
kono
parents: 67
diff changeset
1437
kono
parents: 67
diff changeset
1438 bool space (int nelems) const
kono
parents: 67
diff changeset
1439 { return m_vec ? m_vec->space (nelems) : nelems == 0; }
kono
parents: 67
diff changeset
1440
kono
parents: 67
diff changeset
1441 bool iterate (unsigned ix, T *p) const;
kono
parents: 67
diff changeset
1442 bool iterate (unsigned ix, T **p) const;
kono
parents: 67
diff changeset
1443 vec copy (ALONE_CXX_MEM_STAT_INFO) const;
kono
parents: 67
diff changeset
1444 bool reserve (unsigned, bool = false CXX_MEM_STAT_INFO);
kono
parents: 67
diff changeset
1445 bool reserve_exact (unsigned CXX_MEM_STAT_INFO);
kono
parents: 67
diff changeset
1446 void splice (const vec &);
kono
parents: 67
diff changeset
1447 void safe_splice (const vec & CXX_MEM_STAT_INFO);
kono
parents: 67
diff changeset
1448 T *quick_push (const T &);
kono
parents: 67
diff changeset
1449 T *safe_push (const T &CXX_MEM_STAT_INFO);
kono
parents: 67
diff changeset
1450 T &pop (void);
kono
parents: 67
diff changeset
1451 void truncate (unsigned);
kono
parents: 67
diff changeset
1452 void safe_grow (unsigned CXX_MEM_STAT_INFO);
kono
parents: 67
diff changeset
1453 void safe_grow_cleared (unsigned CXX_MEM_STAT_INFO);
kono
parents: 67
diff changeset
1454 void quick_grow (unsigned);
kono
parents: 67
diff changeset
1455 void quick_grow_cleared (unsigned);
kono
parents: 67
diff changeset
1456 void quick_insert (unsigned, const T &);
kono
parents: 67
diff changeset
1457 void safe_insert (unsigned, const T & CXX_MEM_STAT_INFO);
kono
parents: 67
diff changeset
1458 void ordered_remove (unsigned);
kono
parents: 67
diff changeset
1459 void unordered_remove (unsigned);
kono
parents: 67
diff changeset
1460 void block_remove (unsigned, unsigned);
kono
parents: 67
diff changeset
1461 void qsort (int (*) (const void *, const void *));
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1462 void sort (int (*) (const void *, const void *, void *), void *);
111
kono
parents: 67
diff changeset
1463 T *bsearch (const void *key, int (*compar)(const void *, const void *));
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1464 T *bsearch (const void *key,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1465 int (*compar)(const void *, const void *, void *), void *);
111
kono
parents: 67
diff changeset
1466 unsigned lower_bound (T, bool (*)(const T &, const T &)) const;
kono
parents: 67
diff changeset
1467 bool contains (const T &search) const;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1468 void reverse (void);
111
kono
parents: 67
diff changeset
1469
kono
parents: 67
diff changeset
1470 bool using_auto_storage () const;
kono
parents: 67
diff changeset
1471
kono
parents: 67
diff changeset
1472 /* FIXME - This field should be private, but we need to cater to
kono
parents: 67
diff changeset
1473 compilers that have stricter notions of PODness for types. */
kono
parents: 67
diff changeset
1474 vec<T, va_heap, vl_embed> *m_vec;
kono
parents: 67
diff changeset
1475 };
kono
parents: 67
diff changeset
1476
kono
parents: 67
diff changeset
1477
kono
parents: 67
diff changeset
1478 /* auto_vec is a subclass of vec that automatically manages creating and
kono
parents: 67
diff changeset
1479 releasing the internal vector. If N is non zero then it has N elements of
kono
parents: 67
diff changeset
1480 internal storage. The default is no internal storage, and you probably only
kono
parents: 67
diff changeset
1481 want to ask for internal storage for vectors on the stack because if the
kono
parents: 67
diff changeset
1482 size of the vector is larger than the internal storage that space is wasted.
kono
parents: 67
diff changeset
1483 */
kono
parents: 67
diff changeset
1484 template<typename T, size_t N = 0>
kono
parents: 67
diff changeset
1485 class auto_vec : public vec<T, va_heap>
kono
parents: 67
diff changeset
1486 {
kono
parents: 67
diff changeset
1487 public:
kono
parents: 67
diff changeset
1488 auto_vec ()
kono
parents: 67
diff changeset
1489 {
kono
parents: 67
diff changeset
1490 m_auto.embedded_init (MAX (N, 2), 0, 1);
kono
parents: 67
diff changeset
1491 this->m_vec = &m_auto;
kono
parents: 67
diff changeset
1492 }
kono
parents: 67
diff changeset
1493
kono
parents: 67
diff changeset
1494 auto_vec (size_t s)
kono
parents: 67
diff changeset
1495 {
kono
parents: 67
diff changeset
1496 if (s > N)
kono
parents: 67
diff changeset
1497 {
kono
parents: 67
diff changeset
1498 this->create (s);
kono
parents: 67
diff changeset
1499 return;
kono
parents: 67
diff changeset
1500 }
kono
parents: 67
diff changeset
1501
kono
parents: 67
diff changeset
1502 m_auto.embedded_init (MAX (N, 2), 0, 1);
kono
parents: 67
diff changeset
1503 this->m_vec = &m_auto;
kono
parents: 67
diff changeset
1504 }
kono
parents: 67
diff changeset
1505
kono
parents: 67
diff changeset
1506 ~auto_vec ()
kono
parents: 67
diff changeset
1507 {
kono
parents: 67
diff changeset
1508 this->release ();
kono
parents: 67
diff changeset
1509 }
kono
parents: 67
diff changeset
1510
kono
parents: 67
diff changeset
1511 private:
kono
parents: 67
diff changeset
1512 vec<T, va_heap, vl_embed> m_auto;
kono
parents: 67
diff changeset
1513 T m_data[MAX (N - 1, 1)];
kono
parents: 67
diff changeset
1514 };
kono
parents: 67
diff changeset
1515
kono
parents: 67
diff changeset
1516 /* auto_vec is a sub class of vec whose storage is released when it is
kono
parents: 67
diff changeset
1517 destroyed. */
kono
parents: 67
diff changeset
1518 template<typename T>
kono
parents: 67
diff changeset
1519 class auto_vec<T, 0> : public vec<T, va_heap>
kono
parents: 67
diff changeset
1520 {
kono
parents: 67
diff changeset
1521 public:
kono
parents: 67
diff changeset
1522 auto_vec () { this->m_vec = NULL; }
kono
parents: 67
diff changeset
1523 auto_vec (size_t n) { this->create (n); }
kono
parents: 67
diff changeset
1524 ~auto_vec () { this->release (); }
kono
parents: 67
diff changeset
1525 };
kono
parents: 67
diff changeset
1526
kono
parents: 67
diff changeset
1527
kono
parents: 67
diff changeset
1528 /* Allocate heap memory for pointer V and create the internal vector
kono
parents: 67
diff changeset
1529 with space for NELEMS elements. If NELEMS is 0, the internal
kono
parents: 67
diff changeset
1530 vector is initialized to empty. */
kono
parents: 67
diff changeset
1531
kono
parents: 67
diff changeset
1532 template<typename T>
kono
parents: 67
diff changeset
1533 inline void
kono
parents: 67
diff changeset
1534 vec_alloc (vec<T> *&v, unsigned nelems CXX_MEM_STAT_INFO)
kono
parents: 67
diff changeset
1535 {
kono
parents: 67
diff changeset
1536 v = new vec<T>;
kono
parents: 67
diff changeset
1537 v->create (nelems PASS_MEM_STAT);
kono
parents: 67
diff changeset
1538 }
kono
parents: 67
diff changeset
1539
kono
parents: 67
diff changeset
1540
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1541 /* A subclass of auto_vec <char *> that frees all of its elements on
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1542 deletion. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1543
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1544 class auto_string_vec : public auto_vec <char *>
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1545 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1546 public:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1547 ~auto_string_vec ();
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1548 };
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1549
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1550 /* A subclass of auto_vec <T *> that deletes all of its elements on
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1551 destruction.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1552
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1553 This is a crude way for a vec to "own" the objects it points to
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1554 and clean up automatically.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1555
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1556 For example, no attempt is made to delete elements when an item
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1557 within the vec is overwritten.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1558
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1559 We can't rely on gnu::unique_ptr within a container,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1560 since we can't rely on move semantics in C++98. */
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1561
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1562 template <typename T>
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1563 class auto_delete_vec : public auto_vec <T *>
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1564 {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1565 public:
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1566 auto_delete_vec () {}
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1567 auto_delete_vec (size_t s) : auto_vec <T *> (s) {}
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1568
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1569 ~auto_delete_vec ();
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1570
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1571 private:
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1572 DISABLE_COPY_AND_ASSIGN(auto_delete_vec<T>);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1573 };
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1574
111
kono
parents: 67
diff changeset
1575 /* Conditionally allocate heap memory for VEC and its internal vector. */
kono
parents: 67
diff changeset
1576
kono
parents: 67
diff changeset
1577 template<typename T>
kono
parents: 67
diff changeset
1578 inline void
kono
parents: 67
diff changeset
1579 vec_check_alloc (vec<T, va_heap> *&vec, unsigned nelems CXX_MEM_STAT_INFO)
kono
parents: 67
diff changeset
1580 {
kono
parents: 67
diff changeset
1581 if (!vec)
kono
parents: 67
diff changeset
1582 vec_alloc (vec, nelems PASS_MEM_STAT);
kono
parents: 67
diff changeset
1583 }
kono
parents: 67
diff changeset
1584
kono
parents: 67
diff changeset
1585
kono
parents: 67
diff changeset
1586 /* Free the heap memory allocated by vector V and set it to NULL. */
kono
parents: 67
diff changeset
1587
kono
parents: 67
diff changeset
1588 template<typename T>
kono
parents: 67
diff changeset
1589 inline void
kono
parents: 67
diff changeset
1590 vec_free (vec<T> *&v)
kono
parents: 67
diff changeset
1591 {
kono
parents: 67
diff changeset
1592 if (v == NULL)
kono
parents: 67
diff changeset
1593 return;
kono
parents: 67
diff changeset
1594
kono
parents: 67
diff changeset
1595 v->release ();
kono
parents: 67
diff changeset
1596 delete v;
kono
parents: 67
diff changeset
1597 v = NULL;
kono
parents: 67
diff changeset
1598 }
kono
parents: 67
diff changeset
1599
kono
parents: 67
diff changeset
1600
kono
parents: 67
diff changeset
1601 /* Return iteration condition and update PTR to point to the IX'th
kono
parents: 67
diff changeset
1602 element of this vector. Use this to iterate over the elements of a
kono
parents: 67
diff changeset
1603 vector as follows,
kono
parents: 67
diff changeset
1604
kono
parents: 67
diff changeset
1605 for (ix = 0; v.iterate (ix, &ptr); ix++)
kono
parents: 67
diff changeset
1606 continue; */
kono
parents: 67
diff changeset
1607
kono
parents: 67
diff changeset
1608 template<typename T>
kono
parents: 67
diff changeset
1609 inline bool
kono
parents: 67
diff changeset
1610 vec<T, va_heap, vl_ptr>::iterate (unsigned ix, T *ptr) const
kono
parents: 67
diff changeset
1611 {
kono
parents: 67
diff changeset
1612 if (m_vec)
kono
parents: 67
diff changeset
1613 return m_vec->iterate (ix, ptr);
kono
parents: 67
diff changeset
1614 else
kono
parents: 67
diff changeset
1615 {
kono
parents: 67
diff changeset
1616 *ptr = 0;
kono
parents: 67
diff changeset
1617 return false;
kono
parents: 67
diff changeset
1618 }
kono
parents: 67
diff changeset
1619 }
kono
parents: 67
diff changeset
1620
kono
parents: 67
diff changeset
1621
kono
parents: 67
diff changeset
1622 /* Return iteration condition and update *PTR to point to the
kono
parents: 67
diff changeset
1623 IX'th element of this vector. Use this to iterate over the
kono
parents: 67
diff changeset
1624 elements of a vector as follows,
kono
parents: 67
diff changeset
1625
kono
parents: 67
diff changeset
1626 for (ix = 0; v->iterate (ix, &ptr); ix++)
kono
parents: 67
diff changeset
1627 continue;
kono
parents: 67
diff changeset
1628
kono
parents: 67
diff changeset
1629 This variant is for vectors of objects. */
kono
parents: 67
diff changeset
1630
kono
parents: 67
diff changeset
1631 template<typename T>
kono
parents: 67
diff changeset
1632 inline bool
kono
parents: 67
diff changeset
1633 vec<T, va_heap, vl_ptr>::iterate (unsigned ix, T **ptr) const
kono
parents: 67
diff changeset
1634 {
kono
parents: 67
diff changeset
1635 if (m_vec)
kono
parents: 67
diff changeset
1636 return m_vec->iterate (ix, ptr);
kono
parents: 67
diff changeset
1637 else
kono
parents: 67
diff changeset
1638 {
kono
parents: 67
diff changeset
1639 *ptr = 0;
kono
parents: 67
diff changeset
1640 return false;
kono
parents: 67
diff changeset
1641 }
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1642 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1643
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1644
111
kono
parents: 67
diff changeset
1645 /* Convenience macro for forward iteration. */
kono
parents: 67
diff changeset
1646 #define FOR_EACH_VEC_ELT(V, I, P) \
kono
parents: 67
diff changeset
1647 for (I = 0; (V).iterate ((I), &(P)); ++(I))
kono
parents: 67
diff changeset
1648
kono
parents: 67
diff changeset
1649 #define FOR_EACH_VEC_SAFE_ELT(V, I, P) \
kono
parents: 67
diff changeset
1650 for (I = 0; vec_safe_iterate ((V), (I), &(P)); ++(I))
kono
parents: 67
diff changeset
1651
kono
parents: 67
diff changeset
1652 /* Likewise, but start from FROM rather than 0. */
kono
parents: 67
diff changeset
1653 #define FOR_EACH_VEC_ELT_FROM(V, I, P, FROM) \
kono
parents: 67
diff changeset
1654 for (I = (FROM); (V).iterate ((I), &(P)); ++(I))
kono
parents: 67
diff changeset
1655
kono
parents: 67
diff changeset
1656 /* Convenience macro for reverse iteration. */
kono
parents: 67
diff changeset
1657 #define FOR_EACH_VEC_ELT_REVERSE(V, I, P) \
kono
parents: 67
diff changeset
1658 for (I = (V).length () - 1; \
kono
parents: 67
diff changeset
1659 (V).iterate ((I), &(P)); \
kono
parents: 67
diff changeset
1660 (I)--)
kono
parents: 67
diff changeset
1661
kono
parents: 67
diff changeset
1662 #define FOR_EACH_VEC_SAFE_ELT_REVERSE(V, I, P) \
kono
parents: 67
diff changeset
1663 for (I = vec_safe_length (V) - 1; \
kono
parents: 67
diff changeset
1664 vec_safe_iterate ((V), (I), &(P)); \
kono
parents: 67
diff changeset
1665 (I)--)
kono
parents: 67
diff changeset
1666
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1667 /* auto_string_vec's dtor, freeing all contained strings, automatically
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1668 chaining up to ~auto_vec <char *>, which frees the internal buffer. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1669
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1670 inline
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1671 auto_string_vec::~auto_string_vec ()
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1672 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1673 int i;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1674 char *str;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1675 FOR_EACH_VEC_ELT (*this, i, str)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1676 free (str);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1677 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1678
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1679 /* auto_delete_vec's dtor, deleting all contained items, automatically
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1680 chaining up to ~auto_vec <T*>, which frees the internal buffer. */
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1681
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1682 template <typename T>
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1683 inline
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1684 auto_delete_vec<T>::~auto_delete_vec ()
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1685 {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1686 int i;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1687 T *item;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1688 FOR_EACH_VEC_ELT (*this, i, item)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1689 delete item;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1690 }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1691
111
kono
parents: 67
diff changeset
1692
kono
parents: 67
diff changeset
1693 /* Return a copy of this vector. */
kono
parents: 67
diff changeset
1694
kono
parents: 67
diff changeset
1695 template<typename T>
kono
parents: 67
diff changeset
1696 inline vec<T, va_heap, vl_ptr>
kono
parents: 67
diff changeset
1697 vec<T, va_heap, vl_ptr>::copy (ALONE_MEM_STAT_DECL) const
kono
parents: 67
diff changeset
1698 {
kono
parents: 67
diff changeset
1699 vec<T, va_heap, vl_ptr> new_vec = vNULL;
kono
parents: 67
diff changeset
1700 if (length ())
kono
parents: 67
diff changeset
1701 new_vec.m_vec = m_vec->copy ();
kono
parents: 67
diff changeset
1702 return new_vec;
kono
parents: 67
diff changeset
1703 }
kono
parents: 67
diff changeset
1704
kono
parents: 67
diff changeset
1705
kono
parents: 67
diff changeset
1706 /* Ensure that the vector has at least RESERVE slots available (if
kono
parents: 67
diff changeset
1707 EXACT is false), or exactly RESERVE slots available (if EXACT is
kono
parents: 67
diff changeset
1708 true).
kono
parents: 67
diff changeset
1709
kono
parents: 67
diff changeset
1710 This may create additional headroom if EXACT is false.
kono
parents: 67
diff changeset
1711
kono
parents: 67
diff changeset
1712 Note that this can cause the embedded vector to be reallocated.
kono
parents: 67
diff changeset
1713 Returns true iff reallocation actually occurred. */
kono
parents: 67
diff changeset
1714
kono
parents: 67
diff changeset
1715 template<typename T>
kono
parents: 67
diff changeset
1716 inline bool
kono
parents: 67
diff changeset
1717 vec<T, va_heap, vl_ptr>::reserve (unsigned nelems, bool exact MEM_STAT_DECL)
kono
parents: 67
diff changeset
1718 {
kono
parents: 67
diff changeset
1719 if (space (nelems))
kono
parents: 67
diff changeset
1720 return false;
kono
parents: 67
diff changeset
1721
kono
parents: 67
diff changeset
1722 /* For now play a game with va_heap::reserve to hide our auto storage if any,
kono
parents: 67
diff changeset
1723 this is necessary because it doesn't have enough information to know the
kono
parents: 67
diff changeset
1724 embedded vector is in auto storage, and so should not be freed. */
kono
parents: 67
diff changeset
1725 vec<T, va_heap, vl_embed> *oldvec = m_vec;
kono
parents: 67
diff changeset
1726 unsigned int oldsize = 0;
kono
parents: 67
diff changeset
1727 bool handle_auto_vec = m_vec && using_auto_storage ();
kono
parents: 67
diff changeset
1728 if (handle_auto_vec)
kono
parents: 67
diff changeset
1729 {
kono
parents: 67
diff changeset
1730 m_vec = NULL;
kono
parents: 67
diff changeset
1731 oldsize = oldvec->length ();
kono
parents: 67
diff changeset
1732 nelems += oldsize;
kono
parents: 67
diff changeset
1733 }
kono
parents: 67
diff changeset
1734
kono
parents: 67
diff changeset
1735 va_heap::reserve (m_vec, nelems, exact PASS_MEM_STAT);
kono
parents: 67
diff changeset
1736 if (handle_auto_vec)
kono
parents: 67
diff changeset
1737 {
kono
parents: 67
diff changeset
1738 vec_copy_construct (m_vec->address (), oldvec->address (), oldsize);
kono
parents: 67
diff changeset
1739 m_vec->m_vecpfx.m_num = oldsize;
kono
parents: 67
diff changeset
1740 }
kono
parents: 67
diff changeset
1741
kono
parents: 67
diff changeset
1742 return true;
kono
parents: 67
diff changeset
1743 }
kono
parents: 67
diff changeset
1744
kono
parents: 67
diff changeset
1745
kono
parents: 67
diff changeset
1746 /* Ensure that this vector has exactly NELEMS slots available. This
kono
parents: 67
diff changeset
1747 will not create additional headroom. Note this can cause the
kono
parents: 67
diff changeset
1748 embedded vector to be reallocated. Returns true iff reallocation
kono
parents: 67
diff changeset
1749 actually occurred. */
kono
parents: 67
diff changeset
1750
kono
parents: 67
diff changeset
1751 template<typename T>
kono
parents: 67
diff changeset
1752 inline bool
kono
parents: 67
diff changeset
1753 vec<T, va_heap, vl_ptr>::reserve_exact (unsigned nelems MEM_STAT_DECL)
kono
parents: 67
diff changeset
1754 {
kono
parents: 67
diff changeset
1755 return reserve (nelems, true PASS_MEM_STAT);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1756 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1757
111
kono
parents: 67
diff changeset
1758
kono
parents: 67
diff changeset
1759 /* Create the internal vector and reserve NELEMS for it. This is
kono
parents: 67
diff changeset
1760 exactly like vec::reserve, but the internal vector is
kono
parents: 67
diff changeset
1761 unconditionally allocated from scratch. The old one, if it
kono
parents: 67
diff changeset
1762 existed, is lost. */
kono
parents: 67
diff changeset
1763
kono
parents: 67
diff changeset
1764 template<typename T>
kono
parents: 67
diff changeset
1765 inline void
kono
parents: 67
diff changeset
1766 vec<T, va_heap, vl_ptr>::create (unsigned nelems MEM_STAT_DECL)
kono
parents: 67
diff changeset
1767 {
kono
parents: 67
diff changeset
1768 m_vec = NULL;
kono
parents: 67
diff changeset
1769 if (nelems > 0)
kono
parents: 67
diff changeset
1770 reserve_exact (nelems PASS_MEM_STAT);
kono
parents: 67
diff changeset
1771 }
kono
parents: 67
diff changeset
1772
kono
parents: 67
diff changeset
1773
kono
parents: 67
diff changeset
1774 /* Free the memory occupied by the embedded vector. */
kono
parents: 67
diff changeset
1775
kono
parents: 67
diff changeset
1776 template<typename T>
kono
parents: 67
diff changeset
1777 inline void
kono
parents: 67
diff changeset
1778 vec<T, va_heap, vl_ptr>::release (void)
kono
parents: 67
diff changeset
1779 {
kono
parents: 67
diff changeset
1780 if (!m_vec)
kono
parents: 67
diff changeset
1781 return;
kono
parents: 67
diff changeset
1782
kono
parents: 67
diff changeset
1783 if (using_auto_storage ())
kono
parents: 67
diff changeset
1784 {
kono
parents: 67
diff changeset
1785 m_vec->m_vecpfx.m_num = 0;
kono
parents: 67
diff changeset
1786 return;
kono
parents: 67
diff changeset
1787 }
kono
parents: 67
diff changeset
1788
kono
parents: 67
diff changeset
1789 va_heap::release (m_vec);
kono
parents: 67
diff changeset
1790 }
kono
parents: 67
diff changeset
1791
kono
parents: 67
diff changeset
1792 /* Copy the elements from SRC to the end of this vector as if by memcpy.
kono
parents: 67
diff changeset
1793 SRC and this vector must be allocated with the same memory
kono
parents: 67
diff changeset
1794 allocation mechanism. This vector is assumed to have sufficient
kono
parents: 67
diff changeset
1795 headroom available. */
kono
parents: 67
diff changeset
1796
kono
parents: 67
diff changeset
1797 template<typename T>
kono
parents: 67
diff changeset
1798 inline void
kono
parents: 67
diff changeset
1799 vec<T, va_heap, vl_ptr>::splice (const vec<T, va_heap, vl_ptr> &src)
kono
parents: 67
diff changeset
1800 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1801 if (src.length ())
111
kono
parents: 67
diff changeset
1802 m_vec->splice (*(src.m_vec));
kono
parents: 67
diff changeset
1803 }
kono
parents: 67
diff changeset
1804
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1805
111
kono
parents: 67
diff changeset
1806 /* Copy the elements in SRC to the end of this vector as if by memcpy.
kono
parents: 67
diff changeset
1807 SRC and this vector must be allocated with the same mechanism.
kono
parents: 67
diff changeset
1808 If there is not enough headroom in this vector, it will be reallocated
kono
parents: 67
diff changeset
1809 as needed. */
kono
parents: 67
diff changeset
1810
kono
parents: 67
diff changeset
1811 template<typename T>
kono
parents: 67
diff changeset
1812 inline void
kono
parents: 67
diff changeset
1813 vec<T, va_heap, vl_ptr>::safe_splice (const vec<T, va_heap, vl_ptr> &src
kono
parents: 67
diff changeset
1814 MEM_STAT_DECL)
kono
parents: 67
diff changeset
1815 {
kono
parents: 67
diff changeset
1816 if (src.length ())
kono
parents: 67
diff changeset
1817 {
kono
parents: 67
diff changeset
1818 reserve_exact (src.length ());
kono
parents: 67
diff changeset
1819 splice (src);
kono
parents: 67
diff changeset
1820 }
kono
parents: 67
diff changeset
1821 }
kono
parents: 67
diff changeset
1822
kono
parents: 67
diff changeset
1823
kono
parents: 67
diff changeset
1824 /* Push OBJ (a new element) onto the end of the vector. There must be
kono
parents: 67
diff changeset
1825 sufficient space in the vector. Return a pointer to the slot
kono
parents: 67
diff changeset
1826 where OBJ was inserted. */
kono
parents: 67
diff changeset
1827
kono
parents: 67
diff changeset
1828 template<typename T>
kono
parents: 67
diff changeset
1829 inline T *
kono
parents: 67
diff changeset
1830 vec<T, va_heap, vl_ptr>::quick_push (const T &obj)
kono
parents: 67
diff changeset
1831 {
kono
parents: 67
diff changeset
1832 return m_vec->quick_push (obj);
kono
parents: 67
diff changeset
1833 }
kono
parents: 67
diff changeset
1834
kono
parents: 67
diff changeset
1835
kono
parents: 67
diff changeset
1836 /* Push a new element OBJ onto the end of this vector. Reallocates
kono
parents: 67
diff changeset
1837 the embedded vector, if needed. Return a pointer to the slot where
kono
parents: 67
diff changeset
1838 OBJ was inserted. */
kono
parents: 67
diff changeset
1839
kono
parents: 67
diff changeset
1840 template<typename T>
kono
parents: 67
diff changeset
1841 inline T *
kono
parents: 67
diff changeset
1842 vec<T, va_heap, vl_ptr>::safe_push (const T &obj MEM_STAT_DECL)
kono
parents: 67
diff changeset
1843 {
kono
parents: 67
diff changeset
1844 reserve (1, false PASS_MEM_STAT);
kono
parents: 67
diff changeset
1845 return quick_push (obj);
kono
parents: 67
diff changeset
1846 }
kono
parents: 67
diff changeset
1847
kono
parents: 67
diff changeset
1848
kono
parents: 67
diff changeset
1849 /* Pop and return the last element off the end of the vector. */
kono
parents: 67
diff changeset
1850
kono
parents: 67
diff changeset
1851 template<typename T>
kono
parents: 67
diff changeset
1852 inline T &
kono
parents: 67
diff changeset
1853 vec<T, va_heap, vl_ptr>::pop (void)
kono
parents: 67
diff changeset
1854 {
kono
parents: 67
diff changeset
1855 return m_vec->pop ();
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1856 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1857
111
kono
parents: 67
diff changeset
1858
kono
parents: 67
diff changeset
1859 /* Set the length of the vector to LEN. The new length must be less
kono
parents: 67
diff changeset
1860 than or equal to the current length. This is an O(1) operation. */
kono
parents: 67
diff changeset
1861
kono
parents: 67
diff changeset
1862 template<typename T>
kono
parents: 67
diff changeset
1863 inline void
kono
parents: 67
diff changeset
1864 vec<T, va_heap, vl_ptr>::truncate (unsigned size)
kono
parents: 67
diff changeset
1865 {
kono
parents: 67
diff changeset
1866 if (m_vec)
kono
parents: 67
diff changeset
1867 m_vec->truncate (size);
kono
parents: 67
diff changeset
1868 else
kono
parents: 67
diff changeset
1869 gcc_checking_assert (size == 0);
kono
parents: 67
diff changeset
1870 }
kono
parents: 67
diff changeset
1871
kono
parents: 67
diff changeset
1872
kono
parents: 67
diff changeset
1873 /* Grow the vector to a specific length. LEN must be as long or
kono
parents: 67
diff changeset
1874 longer than the current length. The new elements are
kono
parents: 67
diff changeset
1875 uninitialized. Reallocate the internal vector, if needed. */
kono
parents: 67
diff changeset
1876
kono
parents: 67
diff changeset
1877 template<typename T>
kono
parents: 67
diff changeset
1878 inline void
kono
parents: 67
diff changeset
1879 vec<T, va_heap, vl_ptr>::safe_grow (unsigned len MEM_STAT_DECL)
kono
parents: 67
diff changeset
1880 {
kono
parents: 67
diff changeset
1881 unsigned oldlen = length ();
kono
parents: 67
diff changeset
1882 gcc_checking_assert (oldlen <= len);
kono
parents: 67
diff changeset
1883 reserve_exact (len - oldlen PASS_MEM_STAT);
kono
parents: 67
diff changeset
1884 if (m_vec)
kono
parents: 67
diff changeset
1885 m_vec->quick_grow (len);
kono
parents: 67
diff changeset
1886 else
kono
parents: 67
diff changeset
1887 gcc_checking_assert (len == 0);
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1888 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1889
111
kono
parents: 67
diff changeset
1890
kono
parents: 67
diff changeset
1891 /* Grow the embedded vector to a specific length. LEN must be as
kono
parents: 67
diff changeset
1892 long or longer than the current length. The new elements are
kono
parents: 67
diff changeset
1893 initialized to zero. Reallocate the internal vector, if needed. */
kono
parents: 67
diff changeset
1894
kono
parents: 67
diff changeset
1895 template<typename T>
kono
parents: 67
diff changeset
1896 inline void
kono
parents: 67
diff changeset
1897 vec<T, va_heap, vl_ptr>::safe_grow_cleared (unsigned len MEM_STAT_DECL)
kono
parents: 67
diff changeset
1898 {
kono
parents: 67
diff changeset
1899 unsigned oldlen = length ();
kono
parents: 67
diff changeset
1900 size_t growby = len - oldlen;
kono
parents: 67
diff changeset
1901 safe_grow (len PASS_MEM_STAT);
kono
parents: 67
diff changeset
1902 if (growby != 0)
kono
parents: 67
diff changeset
1903 vec_default_construct (address () + oldlen, growby);
kono
parents: 67
diff changeset
1904 }
kono
parents: 67
diff changeset
1905
kono
parents: 67
diff changeset
1906
kono
parents: 67
diff changeset
1907 /* Same as vec::safe_grow but without reallocation of the internal vector.
kono
parents: 67
diff changeset
1908 If the vector cannot be extended, a runtime assertion will be triggered. */
kono
parents: 67
diff changeset
1909
kono
parents: 67
diff changeset
1910 template<typename T>
kono
parents: 67
diff changeset
1911 inline void
kono
parents: 67
diff changeset
1912 vec<T, va_heap, vl_ptr>::quick_grow (unsigned len)
kono
parents: 67
diff changeset
1913 {
kono
parents: 67
diff changeset
1914 gcc_checking_assert (m_vec);
kono
parents: 67
diff changeset
1915 m_vec->quick_grow (len);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1916 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1917
111
kono
parents: 67
diff changeset
1918
kono
parents: 67
diff changeset
1919 /* Same as vec::quick_grow_cleared but without reallocation of the
kono
parents: 67
diff changeset
1920 internal vector. If the vector cannot be extended, a runtime
kono
parents: 67
diff changeset
1921 assertion will be triggered. */
kono
parents: 67
diff changeset
1922
kono
parents: 67
diff changeset
1923 template<typename T>
kono
parents: 67
diff changeset
1924 inline void
kono
parents: 67
diff changeset
1925 vec<T, va_heap, vl_ptr>::quick_grow_cleared (unsigned len)
kono
parents: 67
diff changeset
1926 {
kono
parents: 67
diff changeset
1927 gcc_checking_assert (m_vec);
kono
parents: 67
diff changeset
1928 m_vec->quick_grow_cleared (len);
kono
parents: 67
diff changeset
1929 }
kono
parents: 67
diff changeset
1930
kono
parents: 67
diff changeset
1931
kono
parents: 67
diff changeset
1932 /* Insert an element, OBJ, at the IXth position of this vector. There
kono
parents: 67
diff changeset
1933 must be sufficient space. */
kono
parents: 67
diff changeset
1934
kono
parents: 67
diff changeset
1935 template<typename T>
kono
parents: 67
diff changeset
1936 inline void
kono
parents: 67
diff changeset
1937 vec<T, va_heap, vl_ptr>::quick_insert (unsigned ix, const T &obj)
kono
parents: 67
diff changeset
1938 {
kono
parents: 67
diff changeset
1939 m_vec->quick_insert (ix, obj);
kono
parents: 67
diff changeset
1940 }
kono
parents: 67
diff changeset
1941
kono
parents: 67
diff changeset
1942
kono
parents: 67
diff changeset
1943 /* Insert an element, OBJ, at the IXth position of the vector.
kono
parents: 67
diff changeset
1944 Reallocate the embedded vector, if necessary. */
kono
parents: 67
diff changeset
1945
kono
parents: 67
diff changeset
1946 template<typename T>
kono
parents: 67
diff changeset
1947 inline void
kono
parents: 67
diff changeset
1948 vec<T, va_heap, vl_ptr>::safe_insert (unsigned ix, const T &obj MEM_STAT_DECL)
kono
parents: 67
diff changeset
1949 {
kono
parents: 67
diff changeset
1950 reserve (1, false PASS_MEM_STAT);
kono
parents: 67
diff changeset
1951 quick_insert (ix, obj);
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1952 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1953
111
kono
parents: 67
diff changeset
1954
kono
parents: 67
diff changeset
1955 /* Remove an element from the IXth position of this vector. Ordering of
kono
parents: 67
diff changeset
1956 remaining elements is preserved. This is an O(N) operation due to
kono
parents: 67
diff changeset
1957 a memmove. */
kono
parents: 67
diff changeset
1958
kono
parents: 67
diff changeset
1959 template<typename T>
kono
parents: 67
diff changeset
1960 inline void
kono
parents: 67
diff changeset
1961 vec<T, va_heap, vl_ptr>::ordered_remove (unsigned ix)
kono
parents: 67
diff changeset
1962 {
kono
parents: 67
diff changeset
1963 m_vec->ordered_remove (ix);
kono
parents: 67
diff changeset
1964 }
kono
parents: 67
diff changeset
1965
kono
parents: 67
diff changeset
1966
kono
parents: 67
diff changeset
1967 /* Remove an element from the IXth position of this vector. Ordering
kono
parents: 67
diff changeset
1968 of remaining elements is destroyed. This is an O(1) operation. */
kono
parents: 67
diff changeset
1969
kono
parents: 67
diff changeset
1970 template<typename T>
kono
parents: 67
diff changeset
1971 inline void
kono
parents: 67
diff changeset
1972 vec<T, va_heap, vl_ptr>::unordered_remove (unsigned ix)
kono
parents: 67
diff changeset
1973 {
kono
parents: 67
diff changeset
1974 m_vec->unordered_remove (ix);
kono
parents: 67
diff changeset
1975 }
kono
parents: 67
diff changeset
1976
kono
parents: 67
diff changeset
1977
kono
parents: 67
diff changeset
1978 /* Remove LEN elements starting at the IXth. Ordering is retained.
kono
parents: 67
diff changeset
1979 This is an O(N) operation due to memmove. */
kono
parents: 67
diff changeset
1980
kono
parents: 67
diff changeset
1981 template<typename T>
kono
parents: 67
diff changeset
1982 inline void
kono
parents: 67
diff changeset
1983 vec<T, va_heap, vl_ptr>::block_remove (unsigned ix, unsigned len)
kono
parents: 67
diff changeset
1984 {
kono
parents: 67
diff changeset
1985 m_vec->block_remove (ix, len);
kono
parents: 67
diff changeset
1986 }
kono
parents: 67
diff changeset
1987
kono
parents: 67
diff changeset
1988
kono
parents: 67
diff changeset
1989 /* Sort the contents of this vector with qsort. CMP is the comparison
kono
parents: 67
diff changeset
1990 function to pass to qsort. */
kono
parents: 67
diff changeset
1991
kono
parents: 67
diff changeset
1992 template<typename T>
kono
parents: 67
diff changeset
1993 inline void
kono
parents: 67
diff changeset
1994 vec<T, va_heap, vl_ptr>::qsort (int (*cmp) (const void *, const void *))
kono
parents: 67
diff changeset
1995 {
kono
parents: 67
diff changeset
1996 if (m_vec)
kono
parents: 67
diff changeset
1997 m_vec->qsort (cmp);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1998 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1999
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2000 /* Sort the contents of this vector with qsort. CMP is the comparison
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2001 function to pass to qsort. */
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2002
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2003 template<typename T>
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2004 inline void
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2005 vec<T, va_heap, vl_ptr>::sort (int (*cmp) (const void *, const void *,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2006 void *), void *data)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2007 {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2008 if (m_vec)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2009 m_vec->sort (cmp, data);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2010 }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2011
111
kono
parents: 67
diff changeset
2012
kono
parents: 67
diff changeset
2013 /* Search the contents of the sorted vector with a binary search.
kono
parents: 67
diff changeset
2014 CMP is the comparison function to pass to bsearch. */
kono
parents: 67
diff changeset
2015
kono
parents: 67
diff changeset
2016 template<typename T>
kono
parents: 67
diff changeset
2017 inline T *
kono
parents: 67
diff changeset
2018 vec<T, va_heap, vl_ptr>::bsearch (const void *key,
kono
parents: 67
diff changeset
2019 int (*cmp) (const void *, const void *))
kono
parents: 67
diff changeset
2020 {
kono
parents: 67
diff changeset
2021 if (m_vec)
kono
parents: 67
diff changeset
2022 return m_vec->bsearch (key, cmp);
kono
parents: 67
diff changeset
2023 return NULL;
kono
parents: 67
diff changeset
2024 }
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
2025
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2026 /* Search the contents of the sorted vector with a binary search.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2027 CMP is the comparison function to pass to bsearch. */
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2028
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2029 template<typename T>
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2030 inline T *
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2031 vec<T, va_heap, vl_ptr>::bsearch (const void *key,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2032 int (*cmp) (const void *, const void *,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2033 void *), void *data)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2034 {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2035 if (m_vec)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2036 return m_vec->bsearch (key, cmp, data);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2037 return NULL;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2038 }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2039
111
kono
parents: 67
diff changeset
2040
kono
parents: 67
diff changeset
2041 /* Find and return the first position in which OBJ could be inserted
kono
parents: 67
diff changeset
2042 without changing the ordering of this vector. LESSTHAN is a
kono
parents: 67
diff changeset
2043 function that returns true if the first argument is strictly less
kono
parents: 67
diff changeset
2044 than the second. */
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
2045
111
kono
parents: 67
diff changeset
2046 template<typename T>
kono
parents: 67
diff changeset
2047 inline unsigned
kono
parents: 67
diff changeset
2048 vec<T, va_heap, vl_ptr>::lower_bound (T obj,
kono
parents: 67
diff changeset
2049 bool (*lessthan)(const T &, const T &))
kono
parents: 67
diff changeset
2050 const
kono
parents: 67
diff changeset
2051 {
kono
parents: 67
diff changeset
2052 return m_vec ? m_vec->lower_bound (obj, lessthan) : 0;
kono
parents: 67
diff changeset
2053 }
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
2054
111
kono
parents: 67
diff changeset
2055 /* Return true if SEARCH is an element of V. Note that this is O(N) in the
kono
parents: 67
diff changeset
2056 size of the vector and so should be used with care. */
kono
parents: 67
diff changeset
2057
kono
parents: 67
diff changeset
2058 template<typename T>
kono
parents: 67
diff changeset
2059 inline bool
kono
parents: 67
diff changeset
2060 vec<T, va_heap, vl_ptr>::contains (const T &search) const
kono
parents: 67
diff changeset
2061 {
kono
parents: 67
diff changeset
2062 return m_vec ? m_vec->contains (search) : false;
kono
parents: 67
diff changeset
2063 }
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
2064
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2065 /* Reverse content of the vector. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2066
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2067 template<typename T>
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2068 inline void
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2069 vec<T, va_heap, vl_ptr>::reverse (void)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2070 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2071 unsigned l = length ();
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2072 T *ptr = address ();
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2073
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2074 for (unsigned i = 0; i < l / 2; i++)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2075 std::swap (ptr[i], ptr[l - i - 1]);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2076 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2077
111
kono
parents: 67
diff changeset
2078 template<typename T>
kono
parents: 67
diff changeset
2079 inline bool
kono
parents: 67
diff changeset
2080 vec<T, va_heap, vl_ptr>::using_auto_storage () const
kono
parents: 67
diff changeset
2081 {
kono
parents: 67
diff changeset
2082 return m_vec->m_vecpfx.m_using_auto_storage;
kono
parents: 67
diff changeset
2083 }
kono
parents: 67
diff changeset
2084
kono
parents: 67
diff changeset
2085 /* Release VEC and call release of all element vectors. */
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
2086
111
kono
parents: 67
diff changeset
2087 template<typename T>
kono
parents: 67
diff changeset
2088 inline void
kono
parents: 67
diff changeset
2089 release_vec_vec (vec<vec<T> > &vec)
kono
parents: 67
diff changeset
2090 {
kono
parents: 67
diff changeset
2091 for (unsigned i = 0; i < vec.length (); i++)
kono
parents: 67
diff changeset
2092 vec[i].release ();
kono
parents: 67
diff changeset
2093
kono
parents: 67
diff changeset
2094 vec.release ();
kono
parents: 67
diff changeset
2095 }
kono
parents: 67
diff changeset
2096
kono
parents: 67
diff changeset
2097 #if (GCC_VERSION >= 3000)
kono
parents: 67
diff changeset
2098 # pragma GCC poison m_vec m_vecpfx m_vecdata
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
2099 #endif
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
2100
111
kono
parents: 67
diff changeset
2101 #endif // GCC_VEC_H