annotate gcc/vec.h @ 132:d34655255c78

update gcc-8.2
author mir3636
date Thu, 25 Oct 2018 10:21:07 +0900
parents 84e7813d76e9
children 1830386684a0
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.
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2 Copyright (C) 2004-2018 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);
kono
parents: 67
diff changeset
198 void release_overhead (void *, size_t, bool CXX_MEM_STAT_INFO);
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 {
kono
parents: 67
diff changeset
279 unsigned alloc
kono
parents: 67
diff changeset
280 = vec_prefix::calculate_allocation (v ? &v->m_vecpfx : 0, reserve, exact);
kono
parents: 67
diff changeset
281 gcc_checking_assert (alloc);
kono
parents: 67
diff changeset
282
kono
parents: 67
diff changeset
283 if (GATHER_STATISTICS && v)
kono
parents: 67
diff changeset
284 v->m_vecpfx.release_overhead (v, v->allocated (), false);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
285
111
kono
parents: 67
diff changeset
286 size_t size = vec<T, va_heap, vl_embed>::embedded_size (alloc);
kono
parents: 67
diff changeset
287 unsigned nelem = v ? v->length () : 0;
kono
parents: 67
diff changeset
288 v = static_cast <vec<T, va_heap, vl_embed> *> (xrealloc (v, size));
kono
parents: 67
diff changeset
289 v->embedded_init (alloc, nelem);
kono
parents: 67
diff changeset
290
kono
parents: 67
diff changeset
291 if (GATHER_STATISTICS)
kono
parents: 67
diff changeset
292 v->m_vecpfx.register_overhead (v, alloc, nelem PASS_MEM_STAT);
kono
parents: 67
diff changeset
293 }
kono
parents: 67
diff changeset
294
kono
parents: 67
diff changeset
295
kono
parents: 67
diff changeset
296 /* Free the heap space allocated for vector V. */
kono
parents: 67
diff changeset
297
kono
parents: 67
diff changeset
298 template<typename T>
kono
parents: 67
diff changeset
299 void
kono
parents: 67
diff changeset
300 va_heap::release (vec<T, va_heap, vl_embed> *&v)
kono
parents: 67
diff changeset
301 {
kono
parents: 67
diff changeset
302 if (v == NULL)
kono
parents: 67
diff changeset
303 return;
kono
parents: 67
diff changeset
304
kono
parents: 67
diff changeset
305 if (GATHER_STATISTICS)
kono
parents: 67
diff changeset
306 v->m_vecpfx.release_overhead (v, v->allocated (), true);
kono
parents: 67
diff changeset
307 ::free (v);
kono
parents: 67
diff changeset
308 v = NULL;
kono
parents: 67
diff changeset
309 }
kono
parents: 67
diff changeset
310
kono
parents: 67
diff changeset
311
kono
parents: 67
diff changeset
312 /* Allocator type for GC vectors. Notice that we need the structure
kono
parents: 67
diff changeset
313 declaration even if GC is not enabled. */
kono
parents: 67
diff changeset
314
kono
parents: 67
diff changeset
315 struct va_gc
kono
parents: 67
diff changeset
316 {
kono
parents: 67
diff changeset
317 /* Use vl_embed as the default layout for GC vectors. Due to GTY
kono
parents: 67
diff changeset
318 limitations, GC vectors must always be pointers, so it is more
kono
parents: 67
diff changeset
319 efficient to use a pointer to the vl_embed layout, rather than
kono
parents: 67
diff changeset
320 using a pointer to a pointer as would be the case with vl_ptr. */
kono
parents: 67
diff changeset
321 typedef vl_embed default_layout;
kono
parents: 67
diff changeset
322
kono
parents: 67
diff changeset
323 template<typename T, typename A>
kono
parents: 67
diff changeset
324 static void reserve (vec<T, A, vl_embed> *&, unsigned, bool
kono
parents: 67
diff changeset
325 CXX_MEM_STAT_INFO);
kono
parents: 67
diff changeset
326
kono
parents: 67
diff changeset
327 template<typename T, typename A>
kono
parents: 67
diff changeset
328 static void release (vec<T, A, vl_embed> *&v);
kono
parents: 67
diff changeset
329 };
kono
parents: 67
diff changeset
330
kono
parents: 67
diff changeset
331
kono
parents: 67
diff changeset
332 /* 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
333
111
kono
parents: 67
diff changeset
334 template<typename T, typename A>
kono
parents: 67
diff changeset
335 inline void
kono
parents: 67
diff changeset
336 va_gc::release (vec<T, A, vl_embed> *&v)
kono
parents: 67
diff changeset
337 {
kono
parents: 67
diff changeset
338 if (v)
kono
parents: 67
diff changeset
339 ::ggc_free (v);
kono
parents: 67
diff changeset
340 v = NULL;
kono
parents: 67
diff changeset
341 }
kono
parents: 67
diff changeset
342
kono
parents: 67
diff changeset
343
kono
parents: 67
diff changeset
344 /* Allocator for GC memory. Ensure there are at least RESERVE free
kono
parents: 67
diff changeset
345 slots in V. If EXACT is true, grow exactly, else grow
kono
parents: 67
diff changeset
346 exponentially. As a special case, if the vector had not been
kono
parents: 67
diff changeset
347 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
348
111
kono
parents: 67
diff changeset
349 template<typename T, typename A>
kono
parents: 67
diff changeset
350 void
kono
parents: 67
diff changeset
351 va_gc::reserve (vec<T, A, vl_embed> *&v, unsigned reserve, bool exact
kono
parents: 67
diff changeset
352 MEM_STAT_DECL)
kono
parents: 67
diff changeset
353 {
kono
parents: 67
diff changeset
354 unsigned alloc
kono
parents: 67
diff changeset
355 = vec_prefix::calculate_allocation (v ? &v->m_vecpfx : 0, reserve, exact);
kono
parents: 67
diff changeset
356 if (!alloc)
kono
parents: 67
diff changeset
357 {
kono
parents: 67
diff changeset
358 ::ggc_free (v);
kono
parents: 67
diff changeset
359 v = NULL;
kono
parents: 67
diff changeset
360 return;
kono
parents: 67
diff changeset
361 }
kono
parents: 67
diff changeset
362
kono
parents: 67
diff changeset
363 /* Calculate the amount of space we want. */
kono
parents: 67
diff changeset
364 size_t size = vec<T, A, vl_embed>::embedded_size (alloc);
kono
parents: 67
diff changeset
365
kono
parents: 67
diff changeset
366 /* Ask the allocator how much space it will really give us. */
kono
parents: 67
diff changeset
367 size = ::ggc_round_alloc_size (size);
kono
parents: 67
diff changeset
368
kono
parents: 67
diff changeset
369 /* Adjust the number of slots accordingly. */
kono
parents: 67
diff changeset
370 size_t vec_offset = sizeof (vec_prefix);
kono
parents: 67
diff changeset
371 size_t elt_size = sizeof (T);
kono
parents: 67
diff changeset
372 alloc = (size - vec_offset) / elt_size;
kono
parents: 67
diff changeset
373
kono
parents: 67
diff changeset
374 /* And finally, recalculate the amount of space we ask for. */
kono
parents: 67
diff changeset
375 size = vec_offset + alloc * elt_size;
kono
parents: 67
diff changeset
376
kono
parents: 67
diff changeset
377 unsigned nelem = v ? v->length () : 0;
kono
parents: 67
diff changeset
378 v = static_cast <vec<T, A, vl_embed> *> (::ggc_realloc (v, size
kono
parents: 67
diff changeset
379 PASS_MEM_STAT));
kono
parents: 67
diff changeset
380 v->embedded_init (alloc, nelem);
kono
parents: 67
diff changeset
381 }
kono
parents: 67
diff changeset
382
kono
parents: 67
diff changeset
383
kono
parents: 67
diff changeset
384 /* Allocator type for GC vectors. This is for vectors of types
kono
parents: 67
diff changeset
385 atomics w.r.t. collection, so allocation and deallocation is
kono
parents: 67
diff changeset
386 completely inherited from va_gc. */
kono
parents: 67
diff changeset
387 struct va_gc_atomic : va_gc
kono
parents: 67
diff changeset
388 {
kono
parents: 67
diff changeset
389 };
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
390
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
391
111
kono
parents: 67
diff changeset
392 /* Generic vector template. Default values for A and L indicate the
kono
parents: 67
diff changeset
393 most commonly used strategies.
kono
parents: 67
diff changeset
394
kono
parents: 67
diff changeset
395 FIXME - Ideally, they would all be vl_ptr to encourage using regular
kono
parents: 67
diff changeset
396 instances for vectors, but the existing GTY machinery is limited
kono
parents: 67
diff changeset
397 in that it can only deal with GC objects that are pointers
kono
parents: 67
diff changeset
398 themselves.
kono
parents: 67
diff changeset
399
kono
parents: 67
diff changeset
400 This means that vector operations that need to deal with
kono
parents: 67
diff changeset
401 potentially NULL pointers, must be provided as free
kono
parents: 67
diff changeset
402 functions (see the vec_safe_* functions above). */
kono
parents: 67
diff changeset
403 template<typename T,
kono
parents: 67
diff changeset
404 typename A = va_heap,
kono
parents: 67
diff changeset
405 typename L = typename A::default_layout>
kono
parents: 67
diff changeset
406 struct GTY((user)) vec
kono
parents: 67
diff changeset
407 {
kono
parents: 67
diff changeset
408 };
kono
parents: 67
diff changeset
409
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
410 /* Generic vec<> debug helpers.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
411
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
412 These need to be instantiated for each vec<TYPE> used throughout
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
413 the compiler like this:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
414
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
415 DEFINE_DEBUG_VEC (TYPE)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
416
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
417 The reason we have a debug_helper() is because GDB can't
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
418 disambiguate a plain call to debug(some_vec), and it must be called
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
419 like debug<TYPE>(some_vec). */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
420
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
421 template<typename T>
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
422 void
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
423 debug_helper (vec<T> &ref)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
424 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
425 unsigned i;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
426 for (i = 0; i < ref.length (); ++i)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
427 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
428 fprintf (stderr, "[%d] = ", i);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
429 debug_slim (ref[i]);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
430 fputc ('\n', stderr);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
431 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
432 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
433
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
434 /* We need a separate va_gc variant here because default template
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
435 argument for functions cannot be used in c++-98. Once this
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
436 restriction is removed, those variant should be folded with the
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
437 above debug_helper. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
438
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
439 template<typename T>
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
440 void
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
441 debug_helper (vec<T, va_gc> &ref)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
442 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
443 unsigned i;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
444 for (i = 0; i < ref.length (); ++i)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
445 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
446 fprintf (stderr, "[%d] = ", i);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
447 debug_slim (ref[i]);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
448 fputc ('\n', stderr);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
449 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
450 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
451
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
452 /* Macro to define debug(vec<T>) and debug(vec<T, va_gc>) helper
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
453 functions for a type T. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
454
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
455 #define DEFINE_DEBUG_VEC(T) \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
456 template void debug_helper (vec<T> &); \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
457 template void debug_helper (vec<T, va_gc> &); \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
458 /* Define the vec<T> debug functions. */ \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
459 DEBUG_FUNCTION void \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
460 debug (vec<T> &ref) \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
461 { \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
462 debug_helper <T> (ref); \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
463 } \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
464 DEBUG_FUNCTION void \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
465 debug (vec<T> *ptr) \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
466 { \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
467 if (ptr) \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
468 debug (*ptr); \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
469 else \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
470 fprintf (stderr, "<nil>\n"); \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
471 } \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
472 /* Define the vec<T, va_gc> debug functions. */ \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
473 DEBUG_FUNCTION void \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
474 debug (vec<T, va_gc> &ref) \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
475 { \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
476 debug_helper <T> (ref); \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
477 } \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
478 DEBUG_FUNCTION void \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
479 debug (vec<T, va_gc> *ptr) \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
480 { \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
481 if (ptr) \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
482 debug (*ptr); \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
483 else \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
484 fprintf (stderr, "<nil>\n"); \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
485 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
486
111
kono
parents: 67
diff changeset
487 /* Default-construct N elements in DST. */
kono
parents: 67
diff changeset
488
kono
parents: 67
diff changeset
489 template <typename T>
kono
parents: 67
diff changeset
490 inline void
kono
parents: 67
diff changeset
491 vec_default_construct (T *dst, unsigned n)
kono
parents: 67
diff changeset
492 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
493 #ifdef BROKEN_VALUE_INITIALIZATION
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
494 /* Versions of GCC before 4.4 sometimes leave certain objects
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
495 uninitialized when value initialized, though if the type has
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
496 user defined default ctor, that ctor is invoked. As a workaround
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
497 perform clearing first and then the value initialization, which
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
498 fixes the case when value initialization doesn't initialize due to
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
499 the bugs and should initialize to all zeros, but still allows
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
500 vectors for types with user defined default ctor that initializes
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
501 some or all elements to non-zero. If T has no user defined
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
502 default ctor and some non-static data members have user defined
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
503 default ctors that initialize to non-zero the workaround will
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
504 still not work properly; in that case we just need to provide
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
505 user defined default ctor. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
506 memset (dst, '\0', sizeof (T) * n);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
507 #endif
111
kono
parents: 67
diff changeset
508 for ( ; n; ++dst, --n)
kono
parents: 67
diff changeset
509 ::new (static_cast<void*>(dst)) T ();
kono
parents: 67
diff changeset
510 }
kono
parents: 67
diff changeset
511
kono
parents: 67
diff changeset
512 /* Copy-construct N elements in DST from *SRC. */
kono
parents: 67
diff changeset
513
kono
parents: 67
diff changeset
514 template <typename T>
kono
parents: 67
diff changeset
515 inline void
kono
parents: 67
diff changeset
516 vec_copy_construct (T *dst, const T *src, unsigned n)
kono
parents: 67
diff changeset
517 {
kono
parents: 67
diff changeset
518 for ( ; n; ++dst, ++src, --n)
kono
parents: 67
diff changeset
519 ::new (static_cast<void*>(dst)) T (*src);
kono
parents: 67
diff changeset
520 }
kono
parents: 67
diff changeset
521
kono
parents: 67
diff changeset
522 /* Type to provide NULL values for vec<T, A, L>. This is used to
kono
parents: 67
diff changeset
523 provide nil initializers for vec instances. Since vec must be
kono
parents: 67
diff changeset
524 a POD, we cannot have proper ctor/dtor for it. To initialize
kono
parents: 67
diff changeset
525 a vec instance, you can assign it the value vNULL. This isn't
kono
parents: 67
diff changeset
526 needed for file-scope and function-local static vectors, which
kono
parents: 67
diff changeset
527 are zero-initialized by default. */
kono
parents: 67
diff changeset
528 struct vnull
kono
parents: 67
diff changeset
529 {
kono
parents: 67
diff changeset
530 template <typename T, typename A, typename L>
kono
parents: 67
diff changeset
531 CONSTEXPR operator vec<T, A, L> () { return vec<T, A, L>(); }
kono
parents: 67
diff changeset
532 };
kono
parents: 67
diff changeset
533 extern vnull vNULL;
kono
parents: 67
diff changeset
534
kono
parents: 67
diff changeset
535
kono
parents: 67
diff changeset
536 /* Embeddable vector. These vectors are suitable to be embedded
kono
parents: 67
diff changeset
537 in other data structures so that they can be pre-allocated in a
kono
parents: 67
diff changeset
538 contiguous memory block.
kono
parents: 67
diff changeset
539
kono
parents: 67
diff changeset
540 Embeddable vectors are implemented using the trailing array idiom,
kono
parents: 67
diff changeset
541 thus they are not resizeable without changing the address of the
kono
parents: 67
diff changeset
542 vector object itself. This means you cannot have variables or
kono
parents: 67
diff changeset
543 fields of embeddable vector type -- always use a pointer to a
kono
parents: 67
diff changeset
544 vector. The one exception is the final field of a structure, which
kono
parents: 67
diff changeset
545 could be a vector type.
kono
parents: 67
diff changeset
546
kono
parents: 67
diff changeset
547 You will have to use the embedded_size & embedded_init calls to
kono
parents: 67
diff changeset
548 create such objects, and they will not be resizeable (so the 'safe'
kono
parents: 67
diff changeset
549 allocation variants are not available).
kono
parents: 67
diff changeset
550
kono
parents: 67
diff changeset
551 Properties:
kono
parents: 67
diff changeset
552
kono
parents: 67
diff changeset
553 - The whole vector and control data are allocated in a single
kono
parents: 67
diff changeset
554 contiguous block. It uses the trailing-vector idiom, so
kono
parents: 67
diff changeset
555 allocation must reserve enough space for all the elements
kono
parents: 67
diff changeset
556 in the vector plus its control data.
kono
parents: 67
diff changeset
557 - The vector cannot be re-allocated.
kono
parents: 67
diff changeset
558 - The vector cannot grow nor shrink.
kono
parents: 67
diff changeset
559 - No indirections needed for access/manipulation.
kono
parents: 67
diff changeset
560 - 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
561
111
kono
parents: 67
diff changeset
562 template<typename T, typename A>
kono
parents: 67
diff changeset
563 struct GTY((user)) vec<T, A, vl_embed>
kono
parents: 67
diff changeset
564 {
kono
parents: 67
diff changeset
565 public:
kono
parents: 67
diff changeset
566 unsigned allocated (void) const { return m_vecpfx.m_alloc; }
kono
parents: 67
diff changeset
567 unsigned length (void) const { return m_vecpfx.m_num; }
kono
parents: 67
diff changeset
568 bool is_empty (void) const { return m_vecpfx.m_num == 0; }
kono
parents: 67
diff changeset
569 T *address (void) { return m_vecdata; }
kono
parents: 67
diff changeset
570 const T *address (void) const { return m_vecdata; }
kono
parents: 67
diff changeset
571 T *begin () { return address (); }
kono
parents: 67
diff changeset
572 const T *begin () const { return address (); }
kono
parents: 67
diff changeset
573 T *end () { return address () + length (); }
kono
parents: 67
diff changeset
574 const T *end () const { return address () + length (); }
kono
parents: 67
diff changeset
575 const T &operator[] (unsigned) const;
kono
parents: 67
diff changeset
576 T &operator[] (unsigned);
kono
parents: 67
diff changeset
577 T &last (void);
kono
parents: 67
diff changeset
578 bool space (unsigned) const;
kono
parents: 67
diff changeset
579 bool iterate (unsigned, T *) const;
kono
parents: 67
diff changeset
580 bool iterate (unsigned, T **) const;
kono
parents: 67
diff changeset
581 vec *copy (ALONE_CXX_MEM_STAT_INFO) const;
kono
parents: 67
diff changeset
582 void splice (const vec &);
kono
parents: 67
diff changeset
583 void splice (const vec *src);
kono
parents: 67
diff changeset
584 T *quick_push (const T &);
kono
parents: 67
diff changeset
585 T &pop (void);
kono
parents: 67
diff changeset
586 void truncate (unsigned);
kono
parents: 67
diff changeset
587 void quick_insert (unsigned, const T &);
kono
parents: 67
diff changeset
588 void ordered_remove (unsigned);
kono
parents: 67
diff changeset
589 void unordered_remove (unsigned);
kono
parents: 67
diff changeset
590 void block_remove (unsigned, unsigned);
kono
parents: 67
diff changeset
591 void qsort (int (*) (const void *, const void *));
kono
parents: 67
diff changeset
592 T *bsearch (const void *key, int (*compar)(const void *, const void *));
kono
parents: 67
diff changeset
593 unsigned lower_bound (T, bool (*)(const T &, const T &)) const;
kono
parents: 67
diff changeset
594 bool contains (const T &search) const;
kono
parents: 67
diff changeset
595 static size_t embedded_size (unsigned);
kono
parents: 67
diff changeset
596 void embedded_init (unsigned, unsigned = 0, unsigned = 0);
kono
parents: 67
diff changeset
597 void quick_grow (unsigned len);
kono
parents: 67
diff changeset
598 void quick_grow_cleared (unsigned len);
kono
parents: 67
diff changeset
599
kono
parents: 67
diff changeset
600 /* vec class can access our internal data and functions. */
kono
parents: 67
diff changeset
601 template <typename, typename, typename> friend struct vec;
kono
parents: 67
diff changeset
602
kono
parents: 67
diff changeset
603 /* The allocator types also need access to our internals. */
kono
parents: 67
diff changeset
604 friend struct va_gc;
kono
parents: 67
diff changeset
605 friend struct va_gc_atomic;
kono
parents: 67
diff changeset
606 friend struct va_heap;
kono
parents: 67
diff changeset
607
kono
parents: 67
diff changeset
608 /* FIXME - These fields should be private, but we need to cater to
kono
parents: 67
diff changeset
609 compilers that have stricter notions of PODness for types. */
kono
parents: 67
diff changeset
610 vec_prefix m_vecpfx;
kono
parents: 67
diff changeset
611 T m_vecdata[1];
kono
parents: 67
diff changeset
612 };
kono
parents: 67
diff changeset
613
kono
parents: 67
diff changeset
614
kono
parents: 67
diff changeset
615 /* Convenience wrapper functions to use when dealing with pointers to
kono
parents: 67
diff changeset
616 embedded vectors. Some functionality for these vectors must be
kono
parents: 67
diff changeset
617 provided via free functions for these reasons:
kono
parents: 67
diff changeset
618
kono
parents: 67
diff changeset
619 1- The pointer may be NULL (e.g., before initial allocation).
kono
parents: 67
diff changeset
620
kono
parents: 67
diff changeset
621 2- When the vector needs to grow, it must be reallocated, so
kono
parents: 67
diff changeset
622 the pointer will change its value.
kono
parents: 67
diff changeset
623
kono
parents: 67
diff changeset
624 Because of limitations with the current GC machinery, all vectors
kono
parents: 67
diff changeset
625 in GC memory *must* be pointers. */
kono
parents: 67
diff changeset
626
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
627
111
kono
parents: 67
diff changeset
628 /* If V contains no room for NELEMS elements, return false. Otherwise,
kono
parents: 67
diff changeset
629 return true. */
kono
parents: 67
diff changeset
630 template<typename T, typename A>
kono
parents: 67
diff changeset
631 inline bool
kono
parents: 67
diff changeset
632 vec_safe_space (const vec<T, A, vl_embed> *v, unsigned nelems)
kono
parents: 67
diff changeset
633 {
kono
parents: 67
diff changeset
634 return v ? v->space (nelems) : nelems == 0;
kono
parents: 67
diff changeset
635 }
kono
parents: 67
diff changeset
636
kono
parents: 67
diff changeset
637
kono
parents: 67
diff changeset
638 /* If V is NULL, return 0. Otherwise, return V->length(). */
kono
parents: 67
diff changeset
639 template<typename T, typename A>
kono
parents: 67
diff changeset
640 inline unsigned
kono
parents: 67
diff changeset
641 vec_safe_length (const vec<T, A, vl_embed> *v)
kono
parents: 67
diff changeset
642 {
kono
parents: 67
diff changeset
643 return v ? v->length () : 0;
kono
parents: 67
diff changeset
644 }
kono
parents: 67
diff changeset
645
kono
parents: 67
diff changeset
646
kono
parents: 67
diff changeset
647 /* If V is NULL, return NULL. Otherwise, return V->address(). */
kono
parents: 67
diff changeset
648 template<typename T, typename A>
kono
parents: 67
diff changeset
649 inline T *
kono
parents: 67
diff changeset
650 vec_safe_address (vec<T, A, vl_embed> *v)
kono
parents: 67
diff changeset
651 {
kono
parents: 67
diff changeset
652 return v ? v->address () : NULL;
kono
parents: 67
diff changeset
653 }
kono
parents: 67
diff changeset
654
kono
parents: 67
diff changeset
655
kono
parents: 67
diff changeset
656 /* If V is NULL, return true. Otherwise, return V->is_empty(). */
kono
parents: 67
diff changeset
657 template<typename T, typename A>
kono
parents: 67
diff changeset
658 inline bool
kono
parents: 67
diff changeset
659 vec_safe_is_empty (vec<T, A, vl_embed> *v)
kono
parents: 67
diff changeset
660 {
kono
parents: 67
diff changeset
661 return v ? v->is_empty () : true;
kono
parents: 67
diff changeset
662 }
kono
parents: 67
diff changeset
663
kono
parents: 67
diff changeset
664 /* If V does not have space for NELEMS elements, call
kono
parents: 67
diff changeset
665 V->reserve(NELEMS, EXACT). */
kono
parents: 67
diff changeset
666 template<typename T, typename A>
kono
parents: 67
diff changeset
667 inline bool
kono
parents: 67
diff changeset
668 vec_safe_reserve (vec<T, A, vl_embed> *&v, unsigned nelems, bool exact = false
kono
parents: 67
diff changeset
669 CXX_MEM_STAT_INFO)
kono
parents: 67
diff changeset
670 {
kono
parents: 67
diff changeset
671 bool extend = nelems ? !vec_safe_space (v, nelems) : false;
kono
parents: 67
diff changeset
672 if (extend)
kono
parents: 67
diff changeset
673 A::reserve (v, nelems, exact PASS_MEM_STAT);
kono
parents: 67
diff changeset
674 return extend;
kono
parents: 67
diff changeset
675 }
kono
parents: 67
diff changeset
676
kono
parents: 67
diff changeset
677 template<typename T, typename A>
kono
parents: 67
diff changeset
678 inline bool
kono
parents: 67
diff changeset
679 vec_safe_reserve_exact (vec<T, A, vl_embed> *&v, unsigned nelems
kono
parents: 67
diff changeset
680 CXX_MEM_STAT_INFO)
kono
parents: 67
diff changeset
681 {
kono
parents: 67
diff changeset
682 return vec_safe_reserve (v, nelems, true PASS_MEM_STAT);
kono
parents: 67
diff changeset
683 }
kono
parents: 67
diff changeset
684
kono
parents: 67
diff changeset
685
kono
parents: 67
diff changeset
686 /* Allocate GC memory for V with space for NELEMS slots. If NELEMS
kono
parents: 67
diff changeset
687 is 0, V is initialized to NULL. */
kono
parents: 67
diff changeset
688
kono
parents: 67
diff changeset
689 template<typename T, typename A>
kono
parents: 67
diff changeset
690 inline void
kono
parents: 67
diff changeset
691 vec_alloc (vec<T, A, vl_embed> *&v, unsigned nelems CXX_MEM_STAT_INFO)
kono
parents: 67
diff changeset
692 {
kono
parents: 67
diff changeset
693 v = NULL;
kono
parents: 67
diff changeset
694 vec_safe_reserve (v, nelems, false PASS_MEM_STAT);
kono
parents: 67
diff changeset
695 }
kono
parents: 67
diff changeset
696
kono
parents: 67
diff changeset
697
kono
parents: 67
diff changeset
698 /* Free the GC memory allocated by vector V and set it to NULL. */
kono
parents: 67
diff changeset
699
kono
parents: 67
diff changeset
700 template<typename T, typename A>
kono
parents: 67
diff changeset
701 inline void
kono
parents: 67
diff changeset
702 vec_free (vec<T, A, vl_embed> *&v)
kono
parents: 67
diff changeset
703 {
kono
parents: 67
diff changeset
704 A::release (v);
kono
parents: 67
diff changeset
705 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
706
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
707
111
kono
parents: 67
diff changeset
708 /* Grow V to length LEN. Allocate it, if necessary. */
kono
parents: 67
diff changeset
709 template<typename T, typename A>
kono
parents: 67
diff changeset
710 inline void
kono
parents: 67
diff changeset
711 vec_safe_grow (vec<T, A, vl_embed> *&v, unsigned len CXX_MEM_STAT_INFO)
kono
parents: 67
diff changeset
712 {
kono
parents: 67
diff changeset
713 unsigned oldlen = vec_safe_length (v);
kono
parents: 67
diff changeset
714 gcc_checking_assert (len >= oldlen);
kono
parents: 67
diff changeset
715 vec_safe_reserve_exact (v, len - oldlen PASS_MEM_STAT);
kono
parents: 67
diff changeset
716 v->quick_grow (len);
kono
parents: 67
diff changeset
717 }
kono
parents: 67
diff changeset
718
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
719
111
kono
parents: 67
diff changeset
720 /* If V is NULL, allocate it. Call V->safe_grow_cleared(LEN). */
kono
parents: 67
diff changeset
721 template<typename T, typename A>
kono
parents: 67
diff changeset
722 inline void
kono
parents: 67
diff changeset
723 vec_safe_grow_cleared (vec<T, A, vl_embed> *&v, unsigned len CXX_MEM_STAT_INFO)
kono
parents: 67
diff changeset
724 {
kono
parents: 67
diff changeset
725 unsigned oldlen = vec_safe_length (v);
kono
parents: 67
diff changeset
726 vec_safe_grow (v, len PASS_MEM_STAT);
kono
parents: 67
diff changeset
727 vec_default_construct (v->address () + oldlen, len - oldlen);
kono
parents: 67
diff changeset
728 }
kono
parents: 67
diff changeset
729
kono
parents: 67
diff changeset
730
kono
parents: 67
diff changeset
731 /* If V is NULL return false, otherwise return V->iterate(IX, PTR). */
kono
parents: 67
diff changeset
732 template<typename T, typename A>
kono
parents: 67
diff changeset
733 inline bool
kono
parents: 67
diff changeset
734 vec_safe_iterate (const vec<T, A, vl_embed> *v, unsigned ix, T **ptr)
kono
parents: 67
diff changeset
735 {
kono
parents: 67
diff changeset
736 if (v)
kono
parents: 67
diff changeset
737 return v->iterate (ix, ptr);
kono
parents: 67
diff changeset
738 else
kono
parents: 67
diff changeset
739 {
kono
parents: 67
diff changeset
740 *ptr = 0;
kono
parents: 67
diff changeset
741 return false;
kono
parents: 67
diff changeset
742 }
kono
parents: 67
diff changeset
743 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
744
111
kono
parents: 67
diff changeset
745 template<typename T, typename A>
kono
parents: 67
diff changeset
746 inline bool
kono
parents: 67
diff changeset
747 vec_safe_iterate (const vec<T, A, vl_embed> *v, unsigned ix, T *ptr)
kono
parents: 67
diff changeset
748 {
kono
parents: 67
diff changeset
749 if (v)
kono
parents: 67
diff changeset
750 return v->iterate (ix, ptr);
kono
parents: 67
diff changeset
751 else
kono
parents: 67
diff changeset
752 {
kono
parents: 67
diff changeset
753 *ptr = 0;
kono
parents: 67
diff changeset
754 return false;
kono
parents: 67
diff changeset
755 }
kono
parents: 67
diff changeset
756 }
kono
parents: 67
diff changeset
757
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
758
111
kono
parents: 67
diff changeset
759 /* If V has no room for one more element, reallocate it. Then call
kono
parents: 67
diff changeset
760 V->quick_push(OBJ). */
kono
parents: 67
diff changeset
761 template<typename T, typename A>
kono
parents: 67
diff changeset
762 inline T *
kono
parents: 67
diff changeset
763 vec_safe_push (vec<T, A, vl_embed> *&v, const T &obj CXX_MEM_STAT_INFO)
kono
parents: 67
diff changeset
764 {
kono
parents: 67
diff changeset
765 vec_safe_reserve (v, 1, false PASS_MEM_STAT);
kono
parents: 67
diff changeset
766 return v->quick_push (obj);
kono
parents: 67
diff changeset
767 }
kono
parents: 67
diff changeset
768
kono
parents: 67
diff changeset
769
kono
parents: 67
diff changeset
770 /* if V has no room for one more element, reallocate it. Then call
kono
parents: 67
diff changeset
771 V->quick_insert(IX, OBJ). */
kono
parents: 67
diff changeset
772 template<typename T, typename A>
kono
parents: 67
diff changeset
773 inline void
kono
parents: 67
diff changeset
774 vec_safe_insert (vec<T, A, vl_embed> *&v, unsigned ix, const T &obj
kono
parents: 67
diff changeset
775 CXX_MEM_STAT_INFO)
kono
parents: 67
diff changeset
776 {
kono
parents: 67
diff changeset
777 vec_safe_reserve (v, 1, false PASS_MEM_STAT);
kono
parents: 67
diff changeset
778 v->quick_insert (ix, obj);
kono
parents: 67
diff changeset
779 }
kono
parents: 67
diff changeset
780
kono
parents: 67
diff changeset
781
kono
parents: 67
diff changeset
782 /* If V is NULL, do nothing. Otherwise, call V->truncate(SIZE). */
kono
parents: 67
diff changeset
783 template<typename T, typename A>
kono
parents: 67
diff changeset
784 inline void
kono
parents: 67
diff changeset
785 vec_safe_truncate (vec<T, A, vl_embed> *v, unsigned size)
kono
parents: 67
diff changeset
786 {
kono
parents: 67
diff changeset
787 if (v)
kono
parents: 67
diff changeset
788 v->truncate (size);
kono
parents: 67
diff changeset
789 }
kono
parents: 67
diff changeset
790
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
791
111
kono
parents: 67
diff changeset
792 /* If SRC is not NULL, return a pointer to a copy of it. */
kono
parents: 67
diff changeset
793 template<typename T, typename A>
kono
parents: 67
diff changeset
794 inline vec<T, A, vl_embed> *
kono
parents: 67
diff changeset
795 vec_safe_copy (vec<T, A, vl_embed> *src CXX_MEM_STAT_INFO)
kono
parents: 67
diff changeset
796 {
kono
parents: 67
diff changeset
797 return src ? src->copy (ALONE_PASS_MEM_STAT) : NULL;
kono
parents: 67
diff changeset
798 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
799
111
kono
parents: 67
diff changeset
800 /* Copy the elements from SRC to the end of DST as if by memcpy.
kono
parents: 67
diff changeset
801 Reallocate DST, if necessary. */
kono
parents: 67
diff changeset
802 template<typename T, typename A>
kono
parents: 67
diff changeset
803 inline void
kono
parents: 67
diff changeset
804 vec_safe_splice (vec<T, A, vl_embed> *&dst, const vec<T, A, vl_embed> *src
kono
parents: 67
diff changeset
805 CXX_MEM_STAT_INFO)
kono
parents: 67
diff changeset
806 {
kono
parents: 67
diff changeset
807 unsigned src_len = vec_safe_length (src);
kono
parents: 67
diff changeset
808 if (src_len)
kono
parents: 67
diff changeset
809 {
kono
parents: 67
diff changeset
810 vec_safe_reserve_exact (dst, vec_safe_length (dst) + src_len
kono
parents: 67
diff changeset
811 PASS_MEM_STAT);
kono
parents: 67
diff changeset
812 dst->splice (*src);
kono
parents: 67
diff changeset
813 }
kono
parents: 67
diff changeset
814 }
kono
parents: 67
diff changeset
815
kono
parents: 67
diff changeset
816 /* Return true if SEARCH is an element of V. Note that this is O(N) in the
kono
parents: 67
diff changeset
817 size of the vector and so should be used with care. */
kono
parents: 67
diff changeset
818
kono
parents: 67
diff changeset
819 template<typename T, typename A>
kono
parents: 67
diff changeset
820 inline bool
kono
parents: 67
diff changeset
821 vec_safe_contains (vec<T, A, vl_embed> *v, const T &search)
kono
parents: 67
diff changeset
822 {
kono
parents: 67
diff changeset
823 return v ? v->contains (search) : false;
kono
parents: 67
diff changeset
824 }
kono
parents: 67
diff changeset
825
kono
parents: 67
diff changeset
826 /* Index into vector. Return the IX'th element. IX must be in the
kono
parents: 67
diff changeset
827 domain of the vector. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
828
111
kono
parents: 67
diff changeset
829 template<typename T, typename A>
kono
parents: 67
diff changeset
830 inline const T &
kono
parents: 67
diff changeset
831 vec<T, A, vl_embed>::operator[] (unsigned ix) const
kono
parents: 67
diff changeset
832 {
kono
parents: 67
diff changeset
833 gcc_checking_assert (ix < m_vecpfx.m_num);
kono
parents: 67
diff changeset
834 return m_vecdata[ix];
kono
parents: 67
diff changeset
835 }
kono
parents: 67
diff changeset
836
kono
parents: 67
diff changeset
837 template<typename T, typename A>
kono
parents: 67
diff changeset
838 inline T &
kono
parents: 67
diff changeset
839 vec<T, A, vl_embed>::operator[] (unsigned ix)
kono
parents: 67
diff changeset
840 {
kono
parents: 67
diff changeset
841 gcc_checking_assert (ix < m_vecpfx.m_num);
kono
parents: 67
diff changeset
842 return m_vecdata[ix];
kono
parents: 67
diff changeset
843 }
kono
parents: 67
diff changeset
844
kono
parents: 67
diff changeset
845
kono
parents: 67
diff changeset
846 /* 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
847
111
kono
parents: 67
diff changeset
848 template<typename T, typename A>
kono
parents: 67
diff changeset
849 inline T &
kono
parents: 67
diff changeset
850 vec<T, A, vl_embed>::last (void)
kono
parents: 67
diff changeset
851 {
kono
parents: 67
diff changeset
852 gcc_checking_assert (m_vecpfx.m_num > 0);
kono
parents: 67
diff changeset
853 return (*this)[m_vecpfx.m_num - 1];
kono
parents: 67
diff changeset
854 }
kono
parents: 67
diff changeset
855
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
856
111
kono
parents: 67
diff changeset
857 /* If this vector has space for NELEMS additional entries, return
kono
parents: 67
diff changeset
858 true. You usually only need to use this if you are doing your
kono
parents: 67
diff changeset
859 own vector reallocation, for instance on an embedded vector. This
kono
parents: 67
diff changeset
860 returns true in exactly the same circumstances that vec::reserve
kono
parents: 67
diff changeset
861 will. */
kono
parents: 67
diff changeset
862
kono
parents: 67
diff changeset
863 template<typename T, typename A>
kono
parents: 67
diff changeset
864 inline bool
kono
parents: 67
diff changeset
865 vec<T, A, vl_embed>::space (unsigned nelems) const
kono
parents: 67
diff changeset
866 {
kono
parents: 67
diff changeset
867 return m_vecpfx.m_alloc - m_vecpfx.m_num >= nelems;
kono
parents: 67
diff changeset
868 }
kono
parents: 67
diff changeset
869
kono
parents: 67
diff changeset
870
kono
parents: 67
diff changeset
871 /* Return iteration condition and update PTR to point to the IX'th
kono
parents: 67
diff changeset
872 element of this vector. Use this to iterate over the elements of a
kono
parents: 67
diff changeset
873 vector as follows,
kono
parents: 67
diff changeset
874
kono
parents: 67
diff changeset
875 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
876 continue; */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
877
111
kono
parents: 67
diff changeset
878 template<typename T, typename A>
kono
parents: 67
diff changeset
879 inline bool
kono
parents: 67
diff changeset
880 vec<T, A, vl_embed>::iterate (unsigned ix, T *ptr) const
kono
parents: 67
diff changeset
881 {
kono
parents: 67
diff changeset
882 if (ix < m_vecpfx.m_num)
kono
parents: 67
diff changeset
883 {
kono
parents: 67
diff changeset
884 *ptr = m_vecdata[ix];
kono
parents: 67
diff changeset
885 return true;
kono
parents: 67
diff changeset
886 }
kono
parents: 67
diff changeset
887 else
kono
parents: 67
diff changeset
888 {
kono
parents: 67
diff changeset
889 *ptr = 0;
kono
parents: 67
diff changeset
890 return false;
kono
parents: 67
diff changeset
891 }
kono
parents: 67
diff changeset
892 }
kono
parents: 67
diff changeset
893
kono
parents: 67
diff changeset
894
kono
parents: 67
diff changeset
895 /* Return iteration condition and update *PTR to point to the
kono
parents: 67
diff changeset
896 IX'th element of this vector. Use this to iterate over the
kono
parents: 67
diff changeset
897 elements of a vector as follows,
kono
parents: 67
diff changeset
898
kono
parents: 67
diff changeset
899 for (ix = 0; v->iterate (ix, &ptr); ix++)
kono
parents: 67
diff changeset
900 continue;
kono
parents: 67
diff changeset
901
kono
parents: 67
diff changeset
902 This variant is for vectors of objects. */
0
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 = CONST_CAST (T *, &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 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
922
111
kono
parents: 67
diff changeset
923 template<typename T, typename A>
kono
parents: 67
diff changeset
924 inline vec<T, A, vl_embed> *
kono
parents: 67
diff changeset
925 vec<T, A, vl_embed>::copy (ALONE_MEM_STAT_DECL) const
kono
parents: 67
diff changeset
926 {
kono
parents: 67
diff changeset
927 vec<T, A, vl_embed> *new_vec = NULL;
kono
parents: 67
diff changeset
928 unsigned len = length ();
kono
parents: 67
diff changeset
929 if (len)
kono
parents: 67
diff changeset
930 {
kono
parents: 67
diff changeset
931 vec_alloc (new_vec, len PASS_MEM_STAT);
kono
parents: 67
diff changeset
932 new_vec->embedded_init (len, len);
kono
parents: 67
diff changeset
933 vec_copy_construct (new_vec->address (), m_vecdata, len);
kono
parents: 67
diff changeset
934 }
kono
parents: 67
diff changeset
935 return new_vec;
kono
parents: 67
diff changeset
936 }
kono
parents: 67
diff changeset
937
kono
parents: 67
diff changeset
938
kono
parents: 67
diff changeset
939 /* Copy the elements from SRC to the end of this vector as if by memcpy.
kono
parents: 67
diff changeset
940 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
941
111
kono
parents: 67
diff changeset
942 template<typename T, typename A>
kono
parents: 67
diff changeset
943 inline void
kono
parents: 67
diff changeset
944 vec<T, A, vl_embed>::splice (const vec<T, A, vl_embed> &src)
kono
parents: 67
diff changeset
945 {
kono
parents: 67
diff changeset
946 unsigned len = src.length ();
kono
parents: 67
diff changeset
947 if (len)
kono
parents: 67
diff changeset
948 {
kono
parents: 67
diff changeset
949 gcc_checking_assert (space (len));
kono
parents: 67
diff changeset
950 vec_copy_construct (end (), src.address (), len);
kono
parents: 67
diff changeset
951 m_vecpfx.m_num += len;
kono
parents: 67
diff changeset
952 }
kono
parents: 67
diff changeset
953 }
kono
parents: 67
diff changeset
954
kono
parents: 67
diff changeset
955 template<typename T, typename A>
kono
parents: 67
diff changeset
956 inline void
kono
parents: 67
diff changeset
957 vec<T, A, vl_embed>::splice (const vec<T, A, vl_embed> *src)
kono
parents: 67
diff changeset
958 {
kono
parents: 67
diff changeset
959 if (src)
kono
parents: 67
diff changeset
960 splice (*src);
kono
parents: 67
diff changeset
961 }
kono
parents: 67
diff changeset
962
kono
parents: 67
diff changeset
963
kono
parents: 67
diff changeset
964 /* Push OBJ (a new element) onto the end of the vector. There must be
kono
parents: 67
diff changeset
965 sufficient space in the vector. Return a pointer to the slot
kono
parents: 67
diff changeset
966 where OBJ was inserted. */
kono
parents: 67
diff changeset
967
kono
parents: 67
diff changeset
968 template<typename T, typename A>
kono
parents: 67
diff changeset
969 inline T *
kono
parents: 67
diff changeset
970 vec<T, A, vl_embed>::quick_push (const T &obj)
kono
parents: 67
diff changeset
971 {
kono
parents: 67
diff changeset
972 gcc_checking_assert (space (1));
kono
parents: 67
diff changeset
973 T *slot = &m_vecdata[m_vecpfx.m_num++];
kono
parents: 67
diff changeset
974 *slot = obj;
kono
parents: 67
diff changeset
975 return slot;
kono
parents: 67
diff changeset
976 }
kono
parents: 67
diff changeset
977
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
978
111
kono
parents: 67
diff changeset
979 /* Pop and return the last element off the end of the vector. */
kono
parents: 67
diff changeset
980
kono
parents: 67
diff changeset
981 template<typename T, typename A>
kono
parents: 67
diff changeset
982 inline T &
kono
parents: 67
diff changeset
983 vec<T, A, vl_embed>::pop (void)
kono
parents: 67
diff changeset
984 {
kono
parents: 67
diff changeset
985 gcc_checking_assert (length () > 0);
kono
parents: 67
diff changeset
986 return m_vecdata[--m_vecpfx.m_num];
kono
parents: 67
diff changeset
987 }
kono
parents: 67
diff changeset
988
kono
parents: 67
diff changeset
989
kono
parents: 67
diff changeset
990 /* Set the length of the vector to SIZE. The new length must be less
kono
parents: 67
diff changeset
991 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
992
111
kono
parents: 67
diff changeset
993 template<typename T, typename A>
kono
parents: 67
diff changeset
994 inline void
kono
parents: 67
diff changeset
995 vec<T, A, vl_embed>::truncate (unsigned size)
kono
parents: 67
diff changeset
996 {
kono
parents: 67
diff changeset
997 gcc_checking_assert (length () >= size);
kono
parents: 67
diff changeset
998 m_vecpfx.m_num = size;
kono
parents: 67
diff changeset
999 }
kono
parents: 67
diff changeset
1000
kono
parents: 67
diff changeset
1001
kono
parents: 67
diff changeset
1002 /* Insert an element, OBJ, at the IXth position of this vector. There
kono
parents: 67
diff changeset
1003 must be sufficient space. */
kono
parents: 67
diff changeset
1004
kono
parents: 67
diff changeset
1005 template<typename T, typename A>
kono
parents: 67
diff changeset
1006 inline void
kono
parents: 67
diff changeset
1007 vec<T, A, vl_embed>::quick_insert (unsigned ix, const T &obj)
kono
parents: 67
diff changeset
1008 {
kono
parents: 67
diff changeset
1009 gcc_checking_assert (length () < allocated ());
kono
parents: 67
diff changeset
1010 gcc_checking_assert (ix <= length ());
kono
parents: 67
diff changeset
1011 T *slot = &m_vecdata[ix];
kono
parents: 67
diff changeset
1012 memmove (slot + 1, slot, (m_vecpfx.m_num++ - ix) * sizeof (T));
kono
parents: 67
diff changeset
1013 *slot = obj;
kono
parents: 67
diff changeset
1014 }
kono
parents: 67
diff changeset
1015
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1016
111
kono
parents: 67
diff changeset
1017 /* Remove an element from the IXth position of this vector. Ordering of
kono
parents: 67
diff changeset
1018 remaining elements is preserved. This is an O(N) operation due to
kono
parents: 67
diff changeset
1019 memmove. */
kono
parents: 67
diff changeset
1020
kono
parents: 67
diff changeset
1021 template<typename T, typename A>
kono
parents: 67
diff changeset
1022 inline void
kono
parents: 67
diff changeset
1023 vec<T, A, vl_embed>::ordered_remove (unsigned ix)
kono
parents: 67
diff changeset
1024 {
kono
parents: 67
diff changeset
1025 gcc_checking_assert (ix < length ());
kono
parents: 67
diff changeset
1026 T *slot = &m_vecdata[ix];
kono
parents: 67
diff changeset
1027 memmove (slot, slot + 1, (--m_vecpfx.m_num - ix) * sizeof (T));
kono
parents: 67
diff changeset
1028 }
kono
parents: 67
diff changeset
1029
kono
parents: 67
diff changeset
1030
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1031 /* Remove elements in [START, END) from VEC for which COND holds. Ordering of
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1032 remaining elements is preserved. This is an O(N) operation. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1033
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1034 #define VEC_ORDERED_REMOVE_IF_FROM_TO(vec, read_index, write_index, \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1035 elem_ptr, start, end, cond) \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1036 { \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1037 gcc_assert ((end) <= (vec).length ()); \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1038 for (read_index = write_index = (start); read_index < (end); \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1039 ++read_index) \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1040 { \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1041 elem_ptr = &(vec)[read_index]; \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1042 bool remove_p = (cond); \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1043 if (remove_p) \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1044 continue; \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1045 \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1046 if (read_index != write_index) \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1047 (vec)[write_index] = (vec)[read_index]; \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1048 \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1049 write_index++; \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1050 } \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1051 \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1052 if (read_index - write_index > 0) \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1053 (vec).block_remove (write_index, read_index - write_index); \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1054 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1055
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1056
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1057 /* Remove elements from VEC for which COND holds. Ordering of remaining
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1058 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(vec, read_index, write_index, elem_ptr, \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1061 cond) \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1062 VEC_ORDERED_REMOVE_IF_FROM_TO ((vec), read_index, write_index, \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1063 elem_ptr, 0, (vec).length (), (cond))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1064
111
kono
parents: 67
diff changeset
1065 /* Remove an element from the IXth position of this vector. Ordering of
kono
parents: 67
diff changeset
1066 remaining elements is destroyed. This is an O(1) operation. */
kono
parents: 67
diff changeset
1067
kono
parents: 67
diff changeset
1068 template<typename T, typename A>
kono
parents: 67
diff changeset
1069 inline void
kono
parents: 67
diff changeset
1070 vec<T, A, vl_embed>::unordered_remove (unsigned ix)
kono
parents: 67
diff changeset
1071 {
kono
parents: 67
diff changeset
1072 gcc_checking_assert (ix < length ());
kono
parents: 67
diff changeset
1073 m_vecdata[ix] = m_vecdata[--m_vecpfx.m_num];
kono
parents: 67
diff changeset
1074 }
kono
parents: 67
diff changeset
1075
kono
parents: 67
diff changeset
1076
kono
parents: 67
diff changeset
1077 /* Remove LEN elements starting at the IXth. Ordering is retained.
kono
parents: 67
diff changeset
1078 This is an O(N) operation due to memmove. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1079
111
kono
parents: 67
diff changeset
1080 template<typename T, typename A>
kono
parents: 67
diff changeset
1081 inline void
kono
parents: 67
diff changeset
1082 vec<T, A, vl_embed>::block_remove (unsigned ix, unsigned len)
kono
parents: 67
diff changeset
1083 {
kono
parents: 67
diff changeset
1084 gcc_checking_assert (ix + len <= length ());
kono
parents: 67
diff changeset
1085 T *slot = &m_vecdata[ix];
kono
parents: 67
diff changeset
1086 m_vecpfx.m_num -= len;
kono
parents: 67
diff changeset
1087 memmove (slot, slot + len, (m_vecpfx.m_num - ix) * sizeof (T));
kono
parents: 67
diff changeset
1088 }
kono
parents: 67
diff changeset
1089
kono
parents: 67
diff changeset
1090
kono
parents: 67
diff changeset
1091 /* Sort the contents of this vector with qsort. CMP is the comparison
kono
parents: 67
diff changeset
1092 function to pass to qsort. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1093
111
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>::qsort (int (*cmp) (const void *, const void *))
kono
parents: 67
diff changeset
1097 {
kono
parents: 67
diff changeset
1098 if (length () > 1)
kono
parents: 67
diff changeset
1099 ::qsort (address (), length (), sizeof (T), cmp);
kono
parents: 67
diff changeset
1100 }
kono
parents: 67
diff changeset
1101
kono
parents: 67
diff changeset
1102
kono
parents: 67
diff changeset
1103 /* Search the contents of the sorted vector with a binary search.
kono
parents: 67
diff changeset
1104 CMP is the comparison function to pass to bsearch. */
kono
parents: 67
diff changeset
1105
kono
parents: 67
diff changeset
1106 template<typename T, typename A>
kono
parents: 67
diff changeset
1107 inline T *
kono
parents: 67
diff changeset
1108 vec<T, A, vl_embed>::bsearch (const void *key,
kono
parents: 67
diff changeset
1109 int (*compar) (const void *, const void *))
kono
parents: 67
diff changeset
1110 {
kono
parents: 67
diff changeset
1111 const void *base = this->address ();
kono
parents: 67
diff changeset
1112 size_t nmemb = this->length ();
kono
parents: 67
diff changeset
1113 size_t size = sizeof (T);
kono
parents: 67
diff changeset
1114 /* The following is a copy of glibc stdlib-bsearch.h. */
kono
parents: 67
diff changeset
1115 size_t l, u, idx;
kono
parents: 67
diff changeset
1116 const void *p;
kono
parents: 67
diff changeset
1117 int comparison;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1118
111
kono
parents: 67
diff changeset
1119 l = 0;
kono
parents: 67
diff changeset
1120 u = nmemb;
kono
parents: 67
diff changeset
1121 while (l < u)
kono
parents: 67
diff changeset
1122 {
kono
parents: 67
diff changeset
1123 idx = (l + u) / 2;
kono
parents: 67
diff changeset
1124 p = (const void *) (((const char *) base) + (idx * size));
kono
parents: 67
diff changeset
1125 comparison = (*compar) (key, p);
kono
parents: 67
diff changeset
1126 if (comparison < 0)
kono
parents: 67
diff changeset
1127 u = idx;
kono
parents: 67
diff changeset
1128 else if (comparison > 0)
kono
parents: 67
diff changeset
1129 l = idx + 1;
kono
parents: 67
diff changeset
1130 else
kono
parents: 67
diff changeset
1131 return (T *)const_cast<void *>(p);
kono
parents: 67
diff changeset
1132 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1133
111
kono
parents: 67
diff changeset
1134 return NULL;
kono
parents: 67
diff changeset
1135 }
kono
parents: 67
diff changeset
1136
kono
parents: 67
diff changeset
1137 /* Return true if SEARCH is an element of V. Note that this is O(N) in the
kono
parents: 67
diff changeset
1138 size of the vector and so should be used with care. */
kono
parents: 67
diff changeset
1139
kono
parents: 67
diff changeset
1140 template<typename T, typename A>
kono
parents: 67
diff changeset
1141 inline bool
kono
parents: 67
diff changeset
1142 vec<T, A, vl_embed>::contains (const T &search) const
kono
parents: 67
diff changeset
1143 {
kono
parents: 67
diff changeset
1144 unsigned int len = length ();
kono
parents: 67
diff changeset
1145 for (unsigned int i = 0; i < len; i++)
kono
parents: 67
diff changeset
1146 if ((*this)[i] == search)
kono
parents: 67
diff changeset
1147 return true;
kono
parents: 67
diff changeset
1148
kono
parents: 67
diff changeset
1149 return false;
kono
parents: 67
diff changeset
1150 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1151
111
kono
parents: 67
diff changeset
1152 /* Find and return the first position in which OBJ could be inserted
kono
parents: 67
diff changeset
1153 without changing the ordering of this vector. LESSTHAN is a
kono
parents: 67
diff changeset
1154 function that returns true if the first argument is strictly less
kono
parents: 67
diff changeset
1155 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
1156
111
kono
parents: 67
diff changeset
1157 template<typename T, typename A>
kono
parents: 67
diff changeset
1158 unsigned
kono
parents: 67
diff changeset
1159 vec<T, A, vl_embed>::lower_bound (T obj, bool (*lessthan)(const T &, const T &))
kono
parents: 67
diff changeset
1160 const
kono
parents: 67
diff changeset
1161 {
kono
parents: 67
diff changeset
1162 unsigned int len = length ();
kono
parents: 67
diff changeset
1163 unsigned int half, middle;
kono
parents: 67
diff changeset
1164 unsigned int first = 0;
kono
parents: 67
diff changeset
1165 while (len > 0)
kono
parents: 67
diff changeset
1166 {
kono
parents: 67
diff changeset
1167 half = len / 2;
kono
parents: 67
diff changeset
1168 middle = first;
kono
parents: 67
diff changeset
1169 middle += half;
kono
parents: 67
diff changeset
1170 T middle_elem = (*this)[middle];
kono
parents: 67
diff changeset
1171 if (lessthan (middle_elem, obj))
kono
parents: 67
diff changeset
1172 {
kono
parents: 67
diff changeset
1173 first = middle;
kono
parents: 67
diff changeset
1174 ++first;
kono
parents: 67
diff changeset
1175 len = len - half - 1;
kono
parents: 67
diff changeset
1176 }
kono
parents: 67
diff changeset
1177 else
kono
parents: 67
diff changeset
1178 len = half;
kono
parents: 67
diff changeset
1179 }
kono
parents: 67
diff changeset
1180 return first;
kono
parents: 67
diff changeset
1181 }
kono
parents: 67
diff changeset
1182
kono
parents: 67
diff changeset
1183
kono
parents: 67
diff changeset
1184 /* Return the number of bytes needed to embed an instance of an
kono
parents: 67
diff changeset
1185 embeddable vec inside another data structure.
kono
parents: 67
diff changeset
1186
kono
parents: 67
diff changeset
1187 Use these methods to determine the required size and initialization
kono
parents: 67
diff changeset
1188 of a vector V of type T embedded within another structure (as the
kono
parents: 67
diff changeset
1189 final member):
kono
parents: 67
diff changeset
1190
kono
parents: 67
diff changeset
1191 size_t vec<T, A, vl_embed>::embedded_size (unsigned alloc);
kono
parents: 67
diff changeset
1192 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
1193
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1194 These allow the caller to perform the memory allocation. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1195
111
kono
parents: 67
diff changeset
1196 template<typename T, typename A>
kono
parents: 67
diff changeset
1197 inline size_t
kono
parents: 67
diff changeset
1198 vec<T, A, vl_embed>::embedded_size (unsigned alloc)
kono
parents: 67
diff changeset
1199 {
kono
parents: 67
diff changeset
1200 typedef vec<T, A, vl_embed> vec_embedded;
kono
parents: 67
diff changeset
1201 return offsetof (vec_embedded, m_vecdata) + alloc * sizeof (T);
kono
parents: 67
diff changeset
1202 }
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
1203
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
1204
111
kono
parents: 67
diff changeset
1205 /* Initialize the vector to contain room for ALLOC elements and
kono
parents: 67
diff changeset
1206 NUM active elements. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1207
111
kono
parents: 67
diff changeset
1208 template<typename T, typename A>
kono
parents: 67
diff changeset
1209 inline void
kono
parents: 67
diff changeset
1210 vec<T, A, vl_embed>::embedded_init (unsigned alloc, unsigned num, unsigned aut)
kono
parents: 67
diff changeset
1211 {
kono
parents: 67
diff changeset
1212 m_vecpfx.m_alloc = alloc;
kono
parents: 67
diff changeset
1213 m_vecpfx.m_using_auto_storage = aut;
kono
parents: 67
diff changeset
1214 m_vecpfx.m_num = num;
kono
parents: 67
diff changeset
1215 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1216
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1217
111
kono
parents: 67
diff changeset
1218 /* Grow the vector to a specific length. LEN must be as long or longer than
kono
parents: 67
diff changeset
1219 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
1220
111
kono
parents: 67
diff changeset
1221 template<typename T, typename A>
kono
parents: 67
diff changeset
1222 inline void
kono
parents: 67
diff changeset
1223 vec<T, A, vl_embed>::quick_grow (unsigned len)
kono
parents: 67
diff changeset
1224 {
kono
parents: 67
diff changeset
1225 gcc_checking_assert (length () <= len && len <= m_vecpfx.m_alloc);
kono
parents: 67
diff changeset
1226 m_vecpfx.m_num = len;
kono
parents: 67
diff changeset
1227 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1228
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1229
111
kono
parents: 67
diff changeset
1230 /* Grow the vector to a specific length. LEN must be as long or longer than
kono
parents: 67
diff changeset
1231 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
1232
111
kono
parents: 67
diff changeset
1233 template<typename T, typename A>
kono
parents: 67
diff changeset
1234 inline void
kono
parents: 67
diff changeset
1235 vec<T, A, vl_embed>::quick_grow_cleared (unsigned len)
kono
parents: 67
diff changeset
1236 {
kono
parents: 67
diff changeset
1237 unsigned oldlen = length ();
kono
parents: 67
diff changeset
1238 size_t growby = len - oldlen;
kono
parents: 67
diff changeset
1239 quick_grow (len);
kono
parents: 67
diff changeset
1240 if (growby != 0)
kono
parents: 67
diff changeset
1241 vec_default_construct (address () + oldlen, growby);
kono
parents: 67
diff changeset
1242 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1243
111
kono
parents: 67
diff changeset
1244 /* Garbage collection support for vec<T, A, vl_embed>. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1245
111
kono
parents: 67
diff changeset
1246 template<typename T>
kono
parents: 67
diff changeset
1247 void
kono
parents: 67
diff changeset
1248 gt_ggc_mx (vec<T, va_gc> *v)
kono
parents: 67
diff changeset
1249 {
kono
parents: 67
diff changeset
1250 extern void gt_ggc_mx (T &);
kono
parents: 67
diff changeset
1251 for (unsigned i = 0; i < v->length (); i++)
kono
parents: 67
diff changeset
1252 gt_ggc_mx ((*v)[i]);
kono
parents: 67
diff changeset
1253 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1254
111
kono
parents: 67
diff changeset
1255 template<typename T>
kono
parents: 67
diff changeset
1256 void
kono
parents: 67
diff changeset
1257 gt_ggc_mx (vec<T, va_gc_atomic, vl_embed> *v ATTRIBUTE_UNUSED)
kono
parents: 67
diff changeset
1258 {
kono
parents: 67
diff changeset
1259 /* Nothing to do. Vectors of atomic types wrt GC do not need to
kono
parents: 67
diff changeset
1260 be traversed. */
kono
parents: 67
diff changeset
1261 }
kono
parents: 67
diff changeset
1262
kono
parents: 67
diff changeset
1263
kono
parents: 67
diff changeset
1264 /* PCH support for vec<T, A, vl_embed>. */
kono
parents: 67
diff changeset
1265
kono
parents: 67
diff changeset
1266 template<typename T, typename A>
kono
parents: 67
diff changeset
1267 void
kono
parents: 67
diff changeset
1268 gt_pch_nx (vec<T, A, vl_embed> *v)
kono
parents: 67
diff changeset
1269 {
kono
parents: 67
diff changeset
1270 extern void gt_pch_nx (T &);
kono
parents: 67
diff changeset
1271 for (unsigned i = 0; i < v->length (); i++)
kono
parents: 67
diff changeset
1272 gt_pch_nx ((*v)[i]);
kono
parents: 67
diff changeset
1273 }
kono
parents: 67
diff changeset
1274
kono
parents: 67
diff changeset
1275 template<typename T, typename A>
kono
parents: 67
diff changeset
1276 void
kono
parents: 67
diff changeset
1277 gt_pch_nx (vec<T *, A, vl_embed> *v, gt_pointer_operator op, void *cookie)
kono
parents: 67
diff changeset
1278 {
kono
parents: 67
diff changeset
1279 for (unsigned i = 0; i < v->length (); i++)
kono
parents: 67
diff changeset
1280 op (&((*v)[i]), cookie);
kono
parents: 67
diff changeset
1281 }
kono
parents: 67
diff changeset
1282
kono
parents: 67
diff changeset
1283 template<typename T, typename A>
kono
parents: 67
diff changeset
1284 void
kono
parents: 67
diff changeset
1285 gt_pch_nx (vec<T, A, vl_embed> *v, gt_pointer_operator op, void *cookie)
kono
parents: 67
diff changeset
1286 {
kono
parents: 67
diff changeset
1287 extern void gt_pch_nx (T *, gt_pointer_operator, void *);
kono
parents: 67
diff changeset
1288 for (unsigned i = 0; i < v->length (); i++)
kono
parents: 67
diff changeset
1289 gt_pch_nx (&((*v)[i]), op, cookie);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1290 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1291
111
kono
parents: 67
diff changeset
1292
kono
parents: 67
diff changeset
1293 /* Space efficient vector. These vectors can grow dynamically and are
kono
parents: 67
diff changeset
1294 allocated together with their control data. They are suited to be
kono
parents: 67
diff changeset
1295 included in data structures. Prior to initial allocation, they
kono
parents: 67
diff changeset
1296 only take a single word of storage.
kono
parents: 67
diff changeset
1297
kono
parents: 67
diff changeset
1298 These vectors are implemented as a pointer to an embeddable vector.
kono
parents: 67
diff changeset
1299 The semantics allow for this pointer to be NULL to represent empty
kono
parents: 67
diff changeset
1300 vectors. This way, empty vectors occupy minimal space in the
kono
parents: 67
diff changeset
1301 structure containing them.
kono
parents: 67
diff changeset
1302
kono
parents: 67
diff changeset
1303 Properties:
kono
parents: 67
diff changeset
1304
kono
parents: 67
diff changeset
1305 - The whole vector and control data are allocated in a single
kono
parents: 67
diff changeset
1306 contiguous block.
kono
parents: 67
diff changeset
1307 - The whole vector may be re-allocated.
kono
parents: 67
diff changeset
1308 - Vector data may grow and shrink.
kono
parents: 67
diff changeset
1309 - Access and manipulation requires a pointer test and
kono
parents: 67
diff changeset
1310 indirection.
kono
parents: 67
diff changeset
1311 - It requires 1 word of storage (prior to vector allocation).
kono
parents: 67
diff changeset
1312
kono
parents: 67
diff changeset
1313
kono
parents: 67
diff changeset
1314 Limitations:
kono
parents: 67
diff changeset
1315
kono
parents: 67
diff changeset
1316 These vectors must be PODs because they are stored in unions.
kono
parents: 67
diff changeset
1317 (http://en.wikipedia.org/wiki/Plain_old_data_structures).
kono
parents: 67
diff changeset
1318 As long as we use C++03, we cannot have constructors nor
kono
parents: 67
diff changeset
1319 destructors in classes that are stored in unions. */
kono
parents: 67
diff changeset
1320
kono
parents: 67
diff changeset
1321 template<typename T>
kono
parents: 67
diff changeset
1322 struct vec<T, va_heap, vl_ptr>
kono
parents: 67
diff changeset
1323 {
kono
parents: 67
diff changeset
1324 public:
kono
parents: 67
diff changeset
1325 /* Memory allocation and deallocation for the embedded vector.
kono
parents: 67
diff changeset
1326 Needed because we cannot have proper ctors/dtors defined. */
kono
parents: 67
diff changeset
1327 void create (unsigned nelems CXX_MEM_STAT_INFO);
kono
parents: 67
diff changeset
1328 void release (void);
kono
parents: 67
diff changeset
1329
kono
parents: 67
diff changeset
1330 /* Vector operations. */
kono
parents: 67
diff changeset
1331 bool exists (void) const
kono
parents: 67
diff changeset
1332 { return m_vec != NULL; }
kono
parents: 67
diff changeset
1333
kono
parents: 67
diff changeset
1334 bool is_empty (void) const
kono
parents: 67
diff changeset
1335 { return m_vec ? m_vec->is_empty () : true; }
kono
parents: 67
diff changeset
1336
kono
parents: 67
diff changeset
1337 unsigned length (void) const
kono
parents: 67
diff changeset
1338 { return m_vec ? m_vec->length () : 0; }
kono
parents: 67
diff changeset
1339
kono
parents: 67
diff changeset
1340 T *address (void)
kono
parents: 67
diff changeset
1341 { return m_vec ? m_vec->m_vecdata : NULL; }
kono
parents: 67
diff changeset
1342
kono
parents: 67
diff changeset
1343 const T *address (void) const
kono
parents: 67
diff changeset
1344 { return m_vec ? m_vec->m_vecdata : NULL; }
kono
parents: 67
diff changeset
1345
kono
parents: 67
diff changeset
1346 T *begin () { return address (); }
kono
parents: 67
diff changeset
1347 const T *begin () const { return address (); }
kono
parents: 67
diff changeset
1348 T *end () { return begin () + length (); }
kono
parents: 67
diff changeset
1349 const T *end () const { return begin () + length (); }
kono
parents: 67
diff changeset
1350 const T &operator[] (unsigned ix) const
kono
parents: 67
diff changeset
1351 { return (*m_vec)[ix]; }
kono
parents: 67
diff changeset
1352
kono
parents: 67
diff changeset
1353 bool operator!=(const vec &other) const
kono
parents: 67
diff changeset
1354 { return !(*this == other); }
kono
parents: 67
diff changeset
1355
kono
parents: 67
diff changeset
1356 bool operator==(const vec &other) const
kono
parents: 67
diff changeset
1357 { return address () == other.address (); }
kono
parents: 67
diff changeset
1358
kono
parents: 67
diff changeset
1359 T &operator[] (unsigned ix)
kono
parents: 67
diff changeset
1360 { return (*m_vec)[ix]; }
kono
parents: 67
diff changeset
1361
kono
parents: 67
diff changeset
1362 T &last (void)
kono
parents: 67
diff changeset
1363 { return m_vec->last (); }
kono
parents: 67
diff changeset
1364
kono
parents: 67
diff changeset
1365 bool space (int nelems) const
kono
parents: 67
diff changeset
1366 { return m_vec ? m_vec->space (nelems) : nelems == 0; }
kono
parents: 67
diff changeset
1367
kono
parents: 67
diff changeset
1368 bool iterate (unsigned ix, T *p) const;
kono
parents: 67
diff changeset
1369 bool iterate (unsigned ix, T **p) const;
kono
parents: 67
diff changeset
1370 vec copy (ALONE_CXX_MEM_STAT_INFO) const;
kono
parents: 67
diff changeset
1371 bool reserve (unsigned, bool = false CXX_MEM_STAT_INFO);
kono
parents: 67
diff changeset
1372 bool reserve_exact (unsigned CXX_MEM_STAT_INFO);
kono
parents: 67
diff changeset
1373 void splice (const vec &);
kono
parents: 67
diff changeset
1374 void safe_splice (const vec & CXX_MEM_STAT_INFO);
kono
parents: 67
diff changeset
1375 T *quick_push (const T &);
kono
parents: 67
diff changeset
1376 T *safe_push (const T &CXX_MEM_STAT_INFO);
kono
parents: 67
diff changeset
1377 T &pop (void);
kono
parents: 67
diff changeset
1378 void truncate (unsigned);
kono
parents: 67
diff changeset
1379 void safe_grow (unsigned CXX_MEM_STAT_INFO);
kono
parents: 67
diff changeset
1380 void safe_grow_cleared (unsigned CXX_MEM_STAT_INFO);
kono
parents: 67
diff changeset
1381 void quick_grow (unsigned);
kono
parents: 67
diff changeset
1382 void quick_grow_cleared (unsigned);
kono
parents: 67
diff changeset
1383 void quick_insert (unsigned, const T &);
kono
parents: 67
diff changeset
1384 void safe_insert (unsigned, const T & CXX_MEM_STAT_INFO);
kono
parents: 67
diff changeset
1385 void ordered_remove (unsigned);
kono
parents: 67
diff changeset
1386 void unordered_remove (unsigned);
kono
parents: 67
diff changeset
1387 void block_remove (unsigned, unsigned);
kono
parents: 67
diff changeset
1388 void qsort (int (*) (const void *, const void *));
kono
parents: 67
diff changeset
1389 T *bsearch (const void *key, int (*compar)(const void *, const void *));
kono
parents: 67
diff changeset
1390 unsigned lower_bound (T, bool (*)(const T &, const T &)) const;
kono
parents: 67
diff changeset
1391 bool contains (const T &search) const;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1392 void reverse (void);
111
kono
parents: 67
diff changeset
1393
kono
parents: 67
diff changeset
1394 bool using_auto_storage () const;
kono
parents: 67
diff changeset
1395
kono
parents: 67
diff changeset
1396 /* FIXME - This field should be private, but we need to cater to
kono
parents: 67
diff changeset
1397 compilers that have stricter notions of PODness for types. */
kono
parents: 67
diff changeset
1398 vec<T, va_heap, vl_embed> *m_vec;
kono
parents: 67
diff changeset
1399 };
kono
parents: 67
diff changeset
1400
kono
parents: 67
diff changeset
1401
kono
parents: 67
diff changeset
1402 /* auto_vec is a subclass of vec that automatically manages creating and
kono
parents: 67
diff changeset
1403 releasing the internal vector. If N is non zero then it has N elements of
kono
parents: 67
diff changeset
1404 internal storage. The default is no internal storage, and you probably only
kono
parents: 67
diff changeset
1405 want to ask for internal storage for vectors on the stack because if the
kono
parents: 67
diff changeset
1406 size of the vector is larger than the internal storage that space is wasted.
kono
parents: 67
diff changeset
1407 */
kono
parents: 67
diff changeset
1408 template<typename T, size_t N = 0>
kono
parents: 67
diff changeset
1409 class auto_vec : public vec<T, va_heap>
kono
parents: 67
diff changeset
1410 {
kono
parents: 67
diff changeset
1411 public:
kono
parents: 67
diff changeset
1412 auto_vec ()
kono
parents: 67
diff changeset
1413 {
kono
parents: 67
diff changeset
1414 m_auto.embedded_init (MAX (N, 2), 0, 1);
kono
parents: 67
diff changeset
1415 this->m_vec = &m_auto;
kono
parents: 67
diff changeset
1416 }
kono
parents: 67
diff changeset
1417
kono
parents: 67
diff changeset
1418 auto_vec (size_t s)
kono
parents: 67
diff changeset
1419 {
kono
parents: 67
diff changeset
1420 if (s > N)
kono
parents: 67
diff changeset
1421 {
kono
parents: 67
diff changeset
1422 this->create (s);
kono
parents: 67
diff changeset
1423 return;
kono
parents: 67
diff changeset
1424 }
kono
parents: 67
diff changeset
1425
kono
parents: 67
diff changeset
1426 m_auto.embedded_init (MAX (N, 2), 0, 1);
kono
parents: 67
diff changeset
1427 this->m_vec = &m_auto;
kono
parents: 67
diff changeset
1428 }
kono
parents: 67
diff changeset
1429
kono
parents: 67
diff changeset
1430 ~auto_vec ()
kono
parents: 67
diff changeset
1431 {
kono
parents: 67
diff changeset
1432 this->release ();
kono
parents: 67
diff changeset
1433 }
kono
parents: 67
diff changeset
1434
kono
parents: 67
diff changeset
1435 private:
kono
parents: 67
diff changeset
1436 vec<T, va_heap, vl_embed> m_auto;
kono
parents: 67
diff changeset
1437 T m_data[MAX (N - 1, 1)];
kono
parents: 67
diff changeset
1438 };
kono
parents: 67
diff changeset
1439
kono
parents: 67
diff changeset
1440 /* auto_vec is a sub class of vec whose storage is released when it is
kono
parents: 67
diff changeset
1441 destroyed. */
kono
parents: 67
diff changeset
1442 template<typename T>
kono
parents: 67
diff changeset
1443 class auto_vec<T, 0> : public vec<T, va_heap>
kono
parents: 67
diff changeset
1444 {
kono
parents: 67
diff changeset
1445 public:
kono
parents: 67
diff changeset
1446 auto_vec () { this->m_vec = NULL; }
kono
parents: 67
diff changeset
1447 auto_vec (size_t n) { this->create (n); }
kono
parents: 67
diff changeset
1448 ~auto_vec () { this->release (); }
kono
parents: 67
diff changeset
1449 };
kono
parents: 67
diff changeset
1450
kono
parents: 67
diff changeset
1451
kono
parents: 67
diff changeset
1452 /* Allocate heap memory for pointer V and create the internal vector
kono
parents: 67
diff changeset
1453 with space for NELEMS elements. If NELEMS is 0, the internal
kono
parents: 67
diff changeset
1454 vector is initialized to empty. */
kono
parents: 67
diff changeset
1455
kono
parents: 67
diff changeset
1456 template<typename T>
kono
parents: 67
diff changeset
1457 inline void
kono
parents: 67
diff changeset
1458 vec_alloc (vec<T> *&v, unsigned nelems CXX_MEM_STAT_INFO)
kono
parents: 67
diff changeset
1459 {
kono
parents: 67
diff changeset
1460 v = new vec<T>;
kono
parents: 67
diff changeset
1461 v->create (nelems PASS_MEM_STAT);
kono
parents: 67
diff changeset
1462 }
kono
parents: 67
diff changeset
1463
kono
parents: 67
diff changeset
1464
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1465 /* A subclass of auto_vec <char *> that frees all of its elements on
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1466 deletion. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1467
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1468 class auto_string_vec : public auto_vec <char *>
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1469 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1470 public:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1471 ~auto_string_vec ();
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1472 };
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1473
111
kono
parents: 67
diff changeset
1474 /* Conditionally allocate heap memory for VEC and its internal vector. */
kono
parents: 67
diff changeset
1475
kono
parents: 67
diff changeset
1476 template<typename T>
kono
parents: 67
diff changeset
1477 inline void
kono
parents: 67
diff changeset
1478 vec_check_alloc (vec<T, va_heap> *&vec, unsigned nelems CXX_MEM_STAT_INFO)
kono
parents: 67
diff changeset
1479 {
kono
parents: 67
diff changeset
1480 if (!vec)
kono
parents: 67
diff changeset
1481 vec_alloc (vec, nelems PASS_MEM_STAT);
kono
parents: 67
diff changeset
1482 }
kono
parents: 67
diff changeset
1483
kono
parents: 67
diff changeset
1484
kono
parents: 67
diff changeset
1485 /* Free the heap memory allocated by vector V and set it to NULL. */
kono
parents: 67
diff changeset
1486
kono
parents: 67
diff changeset
1487 template<typename T>
kono
parents: 67
diff changeset
1488 inline void
kono
parents: 67
diff changeset
1489 vec_free (vec<T> *&v)
kono
parents: 67
diff changeset
1490 {
kono
parents: 67
diff changeset
1491 if (v == NULL)
kono
parents: 67
diff changeset
1492 return;
kono
parents: 67
diff changeset
1493
kono
parents: 67
diff changeset
1494 v->release ();
kono
parents: 67
diff changeset
1495 delete v;
kono
parents: 67
diff changeset
1496 v = NULL;
kono
parents: 67
diff changeset
1497 }
kono
parents: 67
diff changeset
1498
kono
parents: 67
diff changeset
1499
kono
parents: 67
diff changeset
1500 /* Return iteration condition and update PTR to point to the IX'th
kono
parents: 67
diff changeset
1501 element of this vector. Use this to iterate over the elements of a
kono
parents: 67
diff changeset
1502 vector as follows,
kono
parents: 67
diff changeset
1503
kono
parents: 67
diff changeset
1504 for (ix = 0; v.iterate (ix, &ptr); ix++)
kono
parents: 67
diff changeset
1505 continue; */
kono
parents: 67
diff changeset
1506
kono
parents: 67
diff changeset
1507 template<typename T>
kono
parents: 67
diff changeset
1508 inline bool
kono
parents: 67
diff changeset
1509 vec<T, va_heap, vl_ptr>::iterate (unsigned ix, T *ptr) const
kono
parents: 67
diff changeset
1510 {
kono
parents: 67
diff changeset
1511 if (m_vec)
kono
parents: 67
diff changeset
1512 return m_vec->iterate (ix, ptr);
kono
parents: 67
diff changeset
1513 else
kono
parents: 67
diff changeset
1514 {
kono
parents: 67
diff changeset
1515 *ptr = 0;
kono
parents: 67
diff changeset
1516 return false;
kono
parents: 67
diff changeset
1517 }
kono
parents: 67
diff changeset
1518 }
kono
parents: 67
diff changeset
1519
kono
parents: 67
diff changeset
1520
kono
parents: 67
diff changeset
1521 /* Return iteration condition and update *PTR to point to the
kono
parents: 67
diff changeset
1522 IX'th element of this vector. Use this to iterate over the
kono
parents: 67
diff changeset
1523 elements of a vector as follows,
kono
parents: 67
diff changeset
1524
kono
parents: 67
diff changeset
1525 for (ix = 0; v->iterate (ix, &ptr); ix++)
kono
parents: 67
diff changeset
1526 continue;
kono
parents: 67
diff changeset
1527
kono
parents: 67
diff changeset
1528 This variant is for vectors of objects. */
kono
parents: 67
diff changeset
1529
kono
parents: 67
diff changeset
1530 template<typename T>
kono
parents: 67
diff changeset
1531 inline bool
kono
parents: 67
diff changeset
1532 vec<T, va_heap, vl_ptr>::iterate (unsigned ix, T **ptr) const
kono
parents: 67
diff changeset
1533 {
kono
parents: 67
diff changeset
1534 if (m_vec)
kono
parents: 67
diff changeset
1535 return m_vec->iterate (ix, ptr);
kono
parents: 67
diff changeset
1536 else
kono
parents: 67
diff changeset
1537 {
kono
parents: 67
diff changeset
1538 *ptr = 0;
kono
parents: 67
diff changeset
1539 return false;
kono
parents: 67
diff changeset
1540 }
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1541 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1542
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1543
111
kono
parents: 67
diff changeset
1544 /* Convenience macro for forward iteration. */
kono
parents: 67
diff changeset
1545 #define FOR_EACH_VEC_ELT(V, I, P) \
kono
parents: 67
diff changeset
1546 for (I = 0; (V).iterate ((I), &(P)); ++(I))
kono
parents: 67
diff changeset
1547
kono
parents: 67
diff changeset
1548 #define FOR_EACH_VEC_SAFE_ELT(V, I, P) \
kono
parents: 67
diff changeset
1549 for (I = 0; vec_safe_iterate ((V), (I), &(P)); ++(I))
kono
parents: 67
diff changeset
1550
kono
parents: 67
diff changeset
1551 /* Likewise, but start from FROM rather than 0. */
kono
parents: 67
diff changeset
1552 #define FOR_EACH_VEC_ELT_FROM(V, I, P, FROM) \
kono
parents: 67
diff changeset
1553 for (I = (FROM); (V).iterate ((I), &(P)); ++(I))
kono
parents: 67
diff changeset
1554
kono
parents: 67
diff changeset
1555 /* Convenience macro for reverse iteration. */
kono
parents: 67
diff changeset
1556 #define FOR_EACH_VEC_ELT_REVERSE(V, I, P) \
kono
parents: 67
diff changeset
1557 for (I = (V).length () - 1; \
kono
parents: 67
diff changeset
1558 (V).iterate ((I), &(P)); \
kono
parents: 67
diff changeset
1559 (I)--)
kono
parents: 67
diff changeset
1560
kono
parents: 67
diff changeset
1561 #define FOR_EACH_VEC_SAFE_ELT_REVERSE(V, I, P) \
kono
parents: 67
diff changeset
1562 for (I = vec_safe_length (V) - 1; \
kono
parents: 67
diff changeset
1563 vec_safe_iterate ((V), (I), &(P)); \
kono
parents: 67
diff changeset
1564 (I)--)
kono
parents: 67
diff changeset
1565
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1566 /* auto_string_vec's dtor, freeing all contained strings, automatically
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1567 chaining up to ~auto_vec <char *>, which frees the internal buffer. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1568
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1569 inline
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1570 auto_string_vec::~auto_string_vec ()
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1571 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1572 int i;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1573 char *str;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1574 FOR_EACH_VEC_ELT (*this, i, str)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1575 free (str);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1576 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1577
111
kono
parents: 67
diff changeset
1578
kono
parents: 67
diff changeset
1579 /* Return a copy of this vector. */
kono
parents: 67
diff changeset
1580
kono
parents: 67
diff changeset
1581 template<typename T>
kono
parents: 67
diff changeset
1582 inline vec<T, va_heap, vl_ptr>
kono
parents: 67
diff changeset
1583 vec<T, va_heap, vl_ptr>::copy (ALONE_MEM_STAT_DECL) const
kono
parents: 67
diff changeset
1584 {
kono
parents: 67
diff changeset
1585 vec<T, va_heap, vl_ptr> new_vec = vNULL;
kono
parents: 67
diff changeset
1586 if (length ())
kono
parents: 67
diff changeset
1587 new_vec.m_vec = m_vec->copy ();
kono
parents: 67
diff changeset
1588 return new_vec;
kono
parents: 67
diff changeset
1589 }
kono
parents: 67
diff changeset
1590
kono
parents: 67
diff changeset
1591
kono
parents: 67
diff changeset
1592 /* Ensure that the vector has at least RESERVE slots available (if
kono
parents: 67
diff changeset
1593 EXACT is false), or exactly RESERVE slots available (if EXACT is
kono
parents: 67
diff changeset
1594 true).
kono
parents: 67
diff changeset
1595
kono
parents: 67
diff changeset
1596 This may create additional headroom if EXACT is false.
kono
parents: 67
diff changeset
1597
kono
parents: 67
diff changeset
1598 Note that this can cause the embedded vector to be reallocated.
kono
parents: 67
diff changeset
1599 Returns true iff reallocation actually occurred. */
kono
parents: 67
diff changeset
1600
kono
parents: 67
diff changeset
1601 template<typename T>
kono
parents: 67
diff changeset
1602 inline bool
kono
parents: 67
diff changeset
1603 vec<T, va_heap, vl_ptr>::reserve (unsigned nelems, bool exact MEM_STAT_DECL)
kono
parents: 67
diff changeset
1604 {
kono
parents: 67
diff changeset
1605 if (space (nelems))
kono
parents: 67
diff changeset
1606 return false;
kono
parents: 67
diff changeset
1607
kono
parents: 67
diff changeset
1608 /* For now play a game with va_heap::reserve to hide our auto storage if any,
kono
parents: 67
diff changeset
1609 this is necessary because it doesn't have enough information to know the
kono
parents: 67
diff changeset
1610 embedded vector is in auto storage, and so should not be freed. */
kono
parents: 67
diff changeset
1611 vec<T, va_heap, vl_embed> *oldvec = m_vec;
kono
parents: 67
diff changeset
1612 unsigned int oldsize = 0;
kono
parents: 67
diff changeset
1613 bool handle_auto_vec = m_vec && using_auto_storage ();
kono
parents: 67
diff changeset
1614 if (handle_auto_vec)
kono
parents: 67
diff changeset
1615 {
kono
parents: 67
diff changeset
1616 m_vec = NULL;
kono
parents: 67
diff changeset
1617 oldsize = oldvec->length ();
kono
parents: 67
diff changeset
1618 nelems += oldsize;
kono
parents: 67
diff changeset
1619 }
kono
parents: 67
diff changeset
1620
kono
parents: 67
diff changeset
1621 va_heap::reserve (m_vec, nelems, exact PASS_MEM_STAT);
kono
parents: 67
diff changeset
1622 if (handle_auto_vec)
kono
parents: 67
diff changeset
1623 {
kono
parents: 67
diff changeset
1624 vec_copy_construct (m_vec->address (), oldvec->address (), oldsize);
kono
parents: 67
diff changeset
1625 m_vec->m_vecpfx.m_num = oldsize;
kono
parents: 67
diff changeset
1626 }
kono
parents: 67
diff changeset
1627
kono
parents: 67
diff changeset
1628 return true;
kono
parents: 67
diff changeset
1629 }
kono
parents: 67
diff changeset
1630
kono
parents: 67
diff changeset
1631
kono
parents: 67
diff changeset
1632 /* Ensure that this vector has exactly NELEMS slots available. This
kono
parents: 67
diff changeset
1633 will not create additional headroom. Note this can cause the
kono
parents: 67
diff changeset
1634 embedded vector to be reallocated. Returns true iff reallocation
kono
parents: 67
diff changeset
1635 actually occurred. */
kono
parents: 67
diff changeset
1636
kono
parents: 67
diff changeset
1637 template<typename T>
kono
parents: 67
diff changeset
1638 inline bool
kono
parents: 67
diff changeset
1639 vec<T, va_heap, vl_ptr>::reserve_exact (unsigned nelems MEM_STAT_DECL)
kono
parents: 67
diff changeset
1640 {
kono
parents: 67
diff changeset
1641 return reserve (nelems, true PASS_MEM_STAT);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1642 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1643
111
kono
parents: 67
diff changeset
1644
kono
parents: 67
diff changeset
1645 /* Create the internal vector and reserve NELEMS for it. This is
kono
parents: 67
diff changeset
1646 exactly like vec::reserve, but the internal vector is
kono
parents: 67
diff changeset
1647 unconditionally allocated from scratch. The old one, if it
kono
parents: 67
diff changeset
1648 existed, is lost. */
kono
parents: 67
diff changeset
1649
kono
parents: 67
diff changeset
1650 template<typename T>
kono
parents: 67
diff changeset
1651 inline void
kono
parents: 67
diff changeset
1652 vec<T, va_heap, vl_ptr>::create (unsigned nelems MEM_STAT_DECL)
kono
parents: 67
diff changeset
1653 {
kono
parents: 67
diff changeset
1654 m_vec = NULL;
kono
parents: 67
diff changeset
1655 if (nelems > 0)
kono
parents: 67
diff changeset
1656 reserve_exact (nelems PASS_MEM_STAT);
kono
parents: 67
diff changeset
1657 }
kono
parents: 67
diff changeset
1658
kono
parents: 67
diff changeset
1659
kono
parents: 67
diff changeset
1660 /* Free the memory occupied by the embedded vector. */
kono
parents: 67
diff changeset
1661
kono
parents: 67
diff changeset
1662 template<typename T>
kono
parents: 67
diff changeset
1663 inline void
kono
parents: 67
diff changeset
1664 vec<T, va_heap, vl_ptr>::release (void)
kono
parents: 67
diff changeset
1665 {
kono
parents: 67
diff changeset
1666 if (!m_vec)
kono
parents: 67
diff changeset
1667 return;
kono
parents: 67
diff changeset
1668
kono
parents: 67
diff changeset
1669 if (using_auto_storage ())
kono
parents: 67
diff changeset
1670 {
kono
parents: 67
diff changeset
1671 m_vec->m_vecpfx.m_num = 0;
kono
parents: 67
diff changeset
1672 return;
kono
parents: 67
diff changeset
1673 }
kono
parents: 67
diff changeset
1674
kono
parents: 67
diff changeset
1675 va_heap::release (m_vec);
kono
parents: 67
diff changeset
1676 }
kono
parents: 67
diff changeset
1677
kono
parents: 67
diff changeset
1678 /* Copy the elements from SRC to the end of this vector as if by memcpy.
kono
parents: 67
diff changeset
1679 SRC and this vector must be allocated with the same memory
kono
parents: 67
diff changeset
1680 allocation mechanism. This vector is assumed to have sufficient
kono
parents: 67
diff changeset
1681 headroom available. */
kono
parents: 67
diff changeset
1682
kono
parents: 67
diff changeset
1683 template<typename T>
kono
parents: 67
diff changeset
1684 inline void
kono
parents: 67
diff changeset
1685 vec<T, va_heap, vl_ptr>::splice (const vec<T, va_heap, vl_ptr> &src)
kono
parents: 67
diff changeset
1686 {
kono
parents: 67
diff changeset
1687 if (src.m_vec)
kono
parents: 67
diff changeset
1688 m_vec->splice (*(src.m_vec));
kono
parents: 67
diff changeset
1689 }
kono
parents: 67
diff changeset
1690
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1691
111
kono
parents: 67
diff changeset
1692 /* Copy the elements in SRC to the end of this vector as if by memcpy.
kono
parents: 67
diff changeset
1693 SRC and this vector must be allocated with the same mechanism.
kono
parents: 67
diff changeset
1694 If there is not enough headroom in this vector, it will be reallocated
kono
parents: 67
diff changeset
1695 as needed. */
kono
parents: 67
diff changeset
1696
kono
parents: 67
diff changeset
1697 template<typename T>
kono
parents: 67
diff changeset
1698 inline void
kono
parents: 67
diff changeset
1699 vec<T, va_heap, vl_ptr>::safe_splice (const vec<T, va_heap, vl_ptr> &src
kono
parents: 67
diff changeset
1700 MEM_STAT_DECL)
kono
parents: 67
diff changeset
1701 {
kono
parents: 67
diff changeset
1702 if (src.length ())
kono
parents: 67
diff changeset
1703 {
kono
parents: 67
diff changeset
1704 reserve_exact (src.length ());
kono
parents: 67
diff changeset
1705 splice (src);
kono
parents: 67
diff changeset
1706 }
kono
parents: 67
diff changeset
1707 }
kono
parents: 67
diff changeset
1708
kono
parents: 67
diff changeset
1709
kono
parents: 67
diff changeset
1710 /* Push OBJ (a new element) onto the end of the vector. There must be
kono
parents: 67
diff changeset
1711 sufficient space in the vector. Return a pointer to the slot
kono
parents: 67
diff changeset
1712 where OBJ was inserted. */
kono
parents: 67
diff changeset
1713
kono
parents: 67
diff changeset
1714 template<typename T>
kono
parents: 67
diff changeset
1715 inline T *
kono
parents: 67
diff changeset
1716 vec<T, va_heap, vl_ptr>::quick_push (const T &obj)
kono
parents: 67
diff changeset
1717 {
kono
parents: 67
diff changeset
1718 return m_vec->quick_push (obj);
kono
parents: 67
diff changeset
1719 }
kono
parents: 67
diff changeset
1720
kono
parents: 67
diff changeset
1721
kono
parents: 67
diff changeset
1722 /* Push a new element OBJ onto the end of this vector. Reallocates
kono
parents: 67
diff changeset
1723 the embedded vector, if needed. Return a pointer to the slot where
kono
parents: 67
diff changeset
1724 OBJ was inserted. */
kono
parents: 67
diff changeset
1725
kono
parents: 67
diff changeset
1726 template<typename T>
kono
parents: 67
diff changeset
1727 inline T *
kono
parents: 67
diff changeset
1728 vec<T, va_heap, vl_ptr>::safe_push (const T &obj MEM_STAT_DECL)
kono
parents: 67
diff changeset
1729 {
kono
parents: 67
diff changeset
1730 reserve (1, false PASS_MEM_STAT);
kono
parents: 67
diff changeset
1731 return quick_push (obj);
kono
parents: 67
diff changeset
1732 }
kono
parents: 67
diff changeset
1733
kono
parents: 67
diff changeset
1734
kono
parents: 67
diff changeset
1735 /* Pop and return the last element off the end of the vector. */
kono
parents: 67
diff changeset
1736
kono
parents: 67
diff changeset
1737 template<typename T>
kono
parents: 67
diff changeset
1738 inline T &
kono
parents: 67
diff changeset
1739 vec<T, va_heap, vl_ptr>::pop (void)
kono
parents: 67
diff changeset
1740 {
kono
parents: 67
diff changeset
1741 return m_vec->pop ();
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1742 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1743
111
kono
parents: 67
diff changeset
1744
kono
parents: 67
diff changeset
1745 /* Set the length of the vector to LEN. The new length must be less
kono
parents: 67
diff changeset
1746 than or equal to the current length. This is an O(1) operation. */
kono
parents: 67
diff changeset
1747
kono
parents: 67
diff changeset
1748 template<typename T>
kono
parents: 67
diff changeset
1749 inline void
kono
parents: 67
diff changeset
1750 vec<T, va_heap, vl_ptr>::truncate (unsigned size)
kono
parents: 67
diff changeset
1751 {
kono
parents: 67
diff changeset
1752 if (m_vec)
kono
parents: 67
diff changeset
1753 m_vec->truncate (size);
kono
parents: 67
diff changeset
1754 else
kono
parents: 67
diff changeset
1755 gcc_checking_assert (size == 0);
kono
parents: 67
diff changeset
1756 }
kono
parents: 67
diff changeset
1757
kono
parents: 67
diff changeset
1758
kono
parents: 67
diff changeset
1759 /* Grow the vector to a specific length. LEN must be as long or
kono
parents: 67
diff changeset
1760 longer than the current length. The new elements are
kono
parents: 67
diff changeset
1761 uninitialized. Reallocate the internal vector, if needed. */
kono
parents: 67
diff changeset
1762
kono
parents: 67
diff changeset
1763 template<typename T>
kono
parents: 67
diff changeset
1764 inline void
kono
parents: 67
diff changeset
1765 vec<T, va_heap, vl_ptr>::safe_grow (unsigned len MEM_STAT_DECL)
kono
parents: 67
diff changeset
1766 {
kono
parents: 67
diff changeset
1767 unsigned oldlen = length ();
kono
parents: 67
diff changeset
1768 gcc_checking_assert (oldlen <= len);
kono
parents: 67
diff changeset
1769 reserve_exact (len - oldlen PASS_MEM_STAT);
kono
parents: 67
diff changeset
1770 if (m_vec)
kono
parents: 67
diff changeset
1771 m_vec->quick_grow (len);
kono
parents: 67
diff changeset
1772 else
kono
parents: 67
diff changeset
1773 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
1774 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1775
111
kono
parents: 67
diff changeset
1776
kono
parents: 67
diff changeset
1777 /* Grow the embedded vector to a specific length. LEN must be as
kono
parents: 67
diff changeset
1778 long or longer than the current length. The new elements are
kono
parents: 67
diff changeset
1779 initialized to zero. Reallocate the internal vector, if needed. */
kono
parents: 67
diff changeset
1780
kono
parents: 67
diff changeset
1781 template<typename T>
kono
parents: 67
diff changeset
1782 inline void
kono
parents: 67
diff changeset
1783 vec<T, va_heap, vl_ptr>::safe_grow_cleared (unsigned len MEM_STAT_DECL)
kono
parents: 67
diff changeset
1784 {
kono
parents: 67
diff changeset
1785 unsigned oldlen = length ();
kono
parents: 67
diff changeset
1786 size_t growby = len - oldlen;
kono
parents: 67
diff changeset
1787 safe_grow (len PASS_MEM_STAT);
kono
parents: 67
diff changeset
1788 if (growby != 0)
kono
parents: 67
diff changeset
1789 vec_default_construct (address () + oldlen, growby);
kono
parents: 67
diff changeset
1790 }
kono
parents: 67
diff changeset
1791
kono
parents: 67
diff changeset
1792
kono
parents: 67
diff changeset
1793 /* Same as vec::safe_grow but without reallocation of the internal vector.
kono
parents: 67
diff changeset
1794 If the vector cannot be extended, a runtime assertion will be triggered. */
kono
parents: 67
diff changeset
1795
kono
parents: 67
diff changeset
1796 template<typename T>
kono
parents: 67
diff changeset
1797 inline void
kono
parents: 67
diff changeset
1798 vec<T, va_heap, vl_ptr>::quick_grow (unsigned len)
kono
parents: 67
diff changeset
1799 {
kono
parents: 67
diff changeset
1800 gcc_checking_assert (m_vec);
kono
parents: 67
diff changeset
1801 m_vec->quick_grow (len);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1802 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1803
111
kono
parents: 67
diff changeset
1804
kono
parents: 67
diff changeset
1805 /* Same as vec::quick_grow_cleared but without reallocation of the
kono
parents: 67
diff changeset
1806 internal vector. If the vector cannot be extended, a runtime
kono
parents: 67
diff changeset
1807 assertion will be triggered. */
kono
parents: 67
diff changeset
1808
kono
parents: 67
diff changeset
1809 template<typename T>
kono
parents: 67
diff changeset
1810 inline void
kono
parents: 67
diff changeset
1811 vec<T, va_heap, vl_ptr>::quick_grow_cleared (unsigned len)
kono
parents: 67
diff changeset
1812 {
kono
parents: 67
diff changeset
1813 gcc_checking_assert (m_vec);
kono
parents: 67
diff changeset
1814 m_vec->quick_grow_cleared (len);
kono
parents: 67
diff changeset
1815 }
kono
parents: 67
diff changeset
1816
kono
parents: 67
diff changeset
1817
kono
parents: 67
diff changeset
1818 /* Insert an element, OBJ, at the IXth position of this vector. There
kono
parents: 67
diff changeset
1819 must be sufficient space. */
kono
parents: 67
diff changeset
1820
kono
parents: 67
diff changeset
1821 template<typename T>
kono
parents: 67
diff changeset
1822 inline void
kono
parents: 67
diff changeset
1823 vec<T, va_heap, vl_ptr>::quick_insert (unsigned ix, const T &obj)
kono
parents: 67
diff changeset
1824 {
kono
parents: 67
diff changeset
1825 m_vec->quick_insert (ix, obj);
kono
parents: 67
diff changeset
1826 }
kono
parents: 67
diff changeset
1827
kono
parents: 67
diff changeset
1828
kono
parents: 67
diff changeset
1829 /* Insert an element, OBJ, at the IXth position of the vector.
kono
parents: 67
diff changeset
1830 Reallocate the embedded vector, if necessary. */
kono
parents: 67
diff changeset
1831
kono
parents: 67
diff changeset
1832 template<typename T>
kono
parents: 67
diff changeset
1833 inline void
kono
parents: 67
diff changeset
1834 vec<T, va_heap, vl_ptr>::safe_insert (unsigned ix, const T &obj MEM_STAT_DECL)
kono
parents: 67
diff changeset
1835 {
kono
parents: 67
diff changeset
1836 reserve (1, false PASS_MEM_STAT);
kono
parents: 67
diff changeset
1837 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
1838 }
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1839
111
kono
parents: 67
diff changeset
1840
kono
parents: 67
diff changeset
1841 /* Remove an element from the IXth position of this vector. Ordering of
kono
parents: 67
diff changeset
1842 remaining elements is preserved. This is an O(N) operation due to
kono
parents: 67
diff changeset
1843 a memmove. */
kono
parents: 67
diff changeset
1844
kono
parents: 67
diff changeset
1845 template<typename T>
kono
parents: 67
diff changeset
1846 inline void
kono
parents: 67
diff changeset
1847 vec<T, va_heap, vl_ptr>::ordered_remove (unsigned ix)
kono
parents: 67
diff changeset
1848 {
kono
parents: 67
diff changeset
1849 m_vec->ordered_remove (ix);
kono
parents: 67
diff changeset
1850 }
kono
parents: 67
diff changeset
1851
kono
parents: 67
diff changeset
1852
kono
parents: 67
diff changeset
1853 /* Remove an element from the IXth position of this vector. Ordering
kono
parents: 67
diff changeset
1854 of remaining elements is destroyed. This is an O(1) operation. */
kono
parents: 67
diff changeset
1855
kono
parents: 67
diff changeset
1856 template<typename T>
kono
parents: 67
diff changeset
1857 inline void
kono
parents: 67
diff changeset
1858 vec<T, va_heap, vl_ptr>::unordered_remove (unsigned ix)
kono
parents: 67
diff changeset
1859 {
kono
parents: 67
diff changeset
1860 m_vec->unordered_remove (ix);
kono
parents: 67
diff changeset
1861 }
kono
parents: 67
diff changeset
1862
kono
parents: 67
diff changeset
1863
kono
parents: 67
diff changeset
1864 /* Remove LEN elements starting at the IXth. Ordering is retained.
kono
parents: 67
diff changeset
1865 This is an O(N) operation due to memmove. */
kono
parents: 67
diff changeset
1866
kono
parents: 67
diff changeset
1867 template<typename T>
kono
parents: 67
diff changeset
1868 inline void
kono
parents: 67
diff changeset
1869 vec<T, va_heap, vl_ptr>::block_remove (unsigned ix, unsigned len)
kono
parents: 67
diff changeset
1870 {
kono
parents: 67
diff changeset
1871 m_vec->block_remove (ix, len);
kono
parents: 67
diff changeset
1872 }
kono
parents: 67
diff changeset
1873
kono
parents: 67
diff changeset
1874
kono
parents: 67
diff changeset
1875 /* Sort the contents of this vector with qsort. CMP is the comparison
kono
parents: 67
diff changeset
1876 function to pass to qsort. */
kono
parents: 67
diff changeset
1877
kono
parents: 67
diff changeset
1878 template<typename T>
kono
parents: 67
diff changeset
1879 inline void
kono
parents: 67
diff changeset
1880 vec<T, va_heap, vl_ptr>::qsort (int (*cmp) (const void *, const void *))
kono
parents: 67
diff changeset
1881 {
kono
parents: 67
diff changeset
1882 if (m_vec)
kono
parents: 67
diff changeset
1883 m_vec->qsort (cmp);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1884 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1885
111
kono
parents: 67
diff changeset
1886
kono
parents: 67
diff changeset
1887 /* Search the contents of the sorted vector with a binary search.
kono
parents: 67
diff changeset
1888 CMP is the comparison function to pass to bsearch. */
kono
parents: 67
diff changeset
1889
kono
parents: 67
diff changeset
1890 template<typename T>
kono
parents: 67
diff changeset
1891 inline T *
kono
parents: 67
diff changeset
1892 vec<T, va_heap, vl_ptr>::bsearch (const void *key,
kono
parents: 67
diff changeset
1893 int (*cmp) (const void *, const void *))
kono
parents: 67
diff changeset
1894 {
kono
parents: 67
diff changeset
1895 if (m_vec)
kono
parents: 67
diff changeset
1896 return m_vec->bsearch (key, cmp);
kono
parents: 67
diff changeset
1897 return NULL;
kono
parents: 67
diff changeset
1898 }
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1899
111
kono
parents: 67
diff changeset
1900
kono
parents: 67
diff changeset
1901 /* Find and return the first position in which OBJ could be inserted
kono
parents: 67
diff changeset
1902 without changing the ordering of this vector. LESSTHAN is a
kono
parents: 67
diff changeset
1903 function that returns true if the first argument is strictly less
kono
parents: 67
diff changeset
1904 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
1905
111
kono
parents: 67
diff changeset
1906 template<typename T>
kono
parents: 67
diff changeset
1907 inline unsigned
kono
parents: 67
diff changeset
1908 vec<T, va_heap, vl_ptr>::lower_bound (T obj,
kono
parents: 67
diff changeset
1909 bool (*lessthan)(const T &, const T &))
kono
parents: 67
diff changeset
1910 const
kono
parents: 67
diff changeset
1911 {
kono
parents: 67
diff changeset
1912 return m_vec ? m_vec->lower_bound (obj, lessthan) : 0;
kono
parents: 67
diff changeset
1913 }
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1914
111
kono
parents: 67
diff changeset
1915 /* Return true if SEARCH is an element of V. Note that this is O(N) in the
kono
parents: 67
diff changeset
1916 size of the vector and so should be used with care. */
kono
parents: 67
diff changeset
1917
kono
parents: 67
diff changeset
1918 template<typename T>
kono
parents: 67
diff changeset
1919 inline bool
kono
parents: 67
diff changeset
1920 vec<T, va_heap, vl_ptr>::contains (const T &search) const
kono
parents: 67
diff changeset
1921 {
kono
parents: 67
diff changeset
1922 return m_vec ? m_vec->contains (search) : false;
kono
parents: 67
diff changeset
1923 }
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1924
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1925 /* Reverse content of the vector. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1926
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1927 template<typename T>
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1928 inline void
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1929 vec<T, va_heap, vl_ptr>::reverse (void)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1930 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1931 unsigned l = length ();
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1932 T *ptr = address ();
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1933
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1934 for (unsigned i = 0; i < l / 2; i++)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1935 std::swap (ptr[i], ptr[l - i - 1]);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1936 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1937
111
kono
parents: 67
diff changeset
1938 template<typename T>
kono
parents: 67
diff changeset
1939 inline bool
kono
parents: 67
diff changeset
1940 vec<T, va_heap, vl_ptr>::using_auto_storage () const
kono
parents: 67
diff changeset
1941 {
kono
parents: 67
diff changeset
1942 return m_vec->m_vecpfx.m_using_auto_storage;
kono
parents: 67
diff changeset
1943 }
kono
parents: 67
diff changeset
1944
kono
parents: 67
diff changeset
1945 /* 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
1946
111
kono
parents: 67
diff changeset
1947 template<typename T>
kono
parents: 67
diff changeset
1948 inline void
kono
parents: 67
diff changeset
1949 release_vec_vec (vec<vec<T> > &vec)
kono
parents: 67
diff changeset
1950 {
kono
parents: 67
diff changeset
1951 for (unsigned i = 0; i < vec.length (); i++)
kono
parents: 67
diff changeset
1952 vec[i].release ();
kono
parents: 67
diff changeset
1953
kono
parents: 67
diff changeset
1954 vec.release ();
kono
parents: 67
diff changeset
1955 }
kono
parents: 67
diff changeset
1956
kono
parents: 67
diff changeset
1957 #if (GCC_VERSION >= 3000)
kono
parents: 67
diff changeset
1958 # 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
1959 #endif
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1960
111
kono
parents: 67
diff changeset
1961 #endif // GCC_VEC_H