annotate gcc/vec.h @ 120:f93fa5091070

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