* arm.h (REVERSE_CONDITION): Define.
[official-gcc.git] / gcc / vec.h
bloba3eea311320e85c70f43e9c554ae37bcc83afa04
1 /* Vector API for GNU compiler.
2 Copyright (C) 2004 Free Software Foundation, Inc.
3 Contributed by Nathan Sidwell <nathan@codesourcery.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #ifndef GCC_VEC_H
23 #define GCC_VEC_H
25 /* The macros here implement a set of templated vector types and
26 associated interfaces. These templates are implemented with
27 macros, as we're not in C++ land. The interface functions are
28 typesafe and use static inline functions, sometimes backed by
29 out-of-line generic functions. The vectors are designed to
30 interoperate with the GTY machinery.
32 Because of the different behaviour of objects and of pointers to
33 objects, there are two flavours. One to deal with a vector of
34 pointers to objects, and one to deal with a vector of objects
35 themselves. Both of these pass pointers to objects around -- in
36 the former case the pointers are stored into the vector and in the
37 latter case the pointers are dereferenced and the objects copied
38 into the vector. Therefore, when using a vector of pointers, the
39 objects pointed to must be long lived, but when dealing with a
40 vector of objects, the source objects need not be.
42 There are both 'index' and 'iterate' accessors. The iterator
43 returns a boolean iteration condition and updates the iteration
44 variable passed by reference. Because the iterator will be
45 inlined, the address-of can be optimized away.
47 The vectors are implemented using the trailing array idiom, thus
48 they are not resizeable without changing the address of the vector
49 object itself. This means you cannot have variables or fields of
50 vector type -- always use a pointer to a vector. The one exception
51 is the final field of a structure, which could be a vector type.
52 You will have to use the embedded_size & embedded_init calls to
53 create such objects, and they will probably not be resizeable (so
54 don't use the 'safe' allocation variants). The trailing array
55 idiom is used (rather than a pointer to an array of data), because,
56 if we allow NULL to also represent an empty vector, empty vectors
57 occupy minimal space in the structure containing them.
59 Each operation that increases the number of active elements is
60 available in 'quick' and 'safe' variants. The former presumes that
61 there is sufficient allocated space for the operation to succeed
62 (it aborts if there is not). The latter will reallocate the
63 vector, if needed. Reallocation causes an exponential increase in
64 vector size. If you know you will be adding N elements, it would
65 be more efficient to use the reserve operation before adding the
66 elements with the 'quick' operation. You may also use the reserve
67 operation with a -1 operand, to gain control over exactly when
68 reallocation occurs.
70 You should prefer the push and pop operations, as they append and
71 remove from the end of the vector. If you need to remove several
72 items in one go, use the truncate operation. The insert and remove
73 operations allow you to change elements in the middle of the
74 vector. There are two remove operations, one which preserves the
75 element ordering 'ordered_remove', and one which does not
76 'unordered_remove'. The latter function copies the end element
77 into the removed slot, rather than invoke a memmove operation.
78 The 'lower_bound' function will determine where to place an item in the
79 array using insert that will maintain sorted order.
81 If you need to directly manipulate a vector, then the 'address'
82 accessor will return the address of the start of the vector. Also
83 the 'space' predicate will tell you whether there is spare capacity
84 in the vector. You will not normally need to use these two functions.
86 Vector types are defined using a DEF_VEC_{O,P}(TYPEDEF) macro, and
87 variables of vector type are declared using a VEC(TYPEDEF)
88 macro. The characters O and P indicate whether TYPEDEF is a pointer
89 (P) or object (O) type.
91 An example of their use would be,
93 DEF_VEC_P(tree); // define a vector of tree pointers. This must
94 // appear at file scope.
96 struct my_struct {
97 VEC(tree) *v; // A (pointer to) a vector of tree pointers.
100 struct my_struct *s;
102 if (VEC_length(tree,s->v)) { we have some contents }
103 VEC_safe_push(tree,s->v,decl); // append some decl onto the end
104 for (ix = 0; VEC_iterate(tree,s->v,ix,elt); ix++)
105 { do something with elt }
109 /* Macros to invoke API calls. A single macro works for both pointer
110 and object vectors, but the argument and return types might well be
111 different. In each macro, TDEF is the typedef of the vector
112 elements. Some of these macros pass the vector, V, by reference
113 (by taking its address), this is noted in the descriptions. */
115 /* Length of vector
116 unsigned VEC_T_length(const VEC(T) *v);
118 Return the number of active elements in V. V can be NULL, in which
119 case zero is returned. */
121 #define VEC_length(TDEF,V) (VEC_OP(TDEF,length)(V))
123 /* Get the final element of the vector.
124 T VEC_T_last(VEC(T) *v); // Pointer
125 T *VEC_T_last(VEC(T) *v); // Object
127 Return the final element. If V is empty, abort. */
129 #define VEC_last(TDEF,V) (VEC_OP(TDEF,last)(V VEC_CHECK_INFO))
131 /* Index into vector
132 T VEC_T_index(VEC(T) *v, unsigned ix); // Pointer
133 T *VEC_T_index(VEC(T) *v, unsigned ix); // Object
135 Return the IX'th element. If IX is outside the domain of V,
136 abort. */
138 #define VEC_index(TDEF,V,I) (VEC_OP(TDEF,index)(V,I VEC_CHECK_INFO))
140 /* Iterate over vector
141 int VEC_T_iterate(VEC(T) *v, unsigned ix, T &ptr); // Pointer
142 int VEC_T_iterate(VEC(T) *v, unsigned ix, T *&ptr); // Object
144 Return iteration condition and update PTR to point to the IX'th
145 element. At the end of iteration, sets PTR to NULL. Use this to
146 iterate over the elements of a vector as follows,
148 for (ix = 0; VEC_iterate(T,v,ix,ptr); ix++)
149 continue; */
151 #define VEC_iterate(TDEF,V,I,P) (VEC_OP(TDEF,iterate)(V,I,&(P)))
153 /* Allocate new vector.
154 VEC(T) *VEC_T_alloc(int reserve);
156 Allocate a new vector with space for RESERVE objects. If RESERVE
157 is <= 0, a default number of slots are created. */
159 #define VEC_alloc(TDEF,A) (VEC_OP(TDEF,alloc)(A MEM_STAT_INFO))
161 /* Use these to determine the required size and initialization of a
162 vector embedded within another structure (as the final member).
164 size_t VEC_T_embedded_size(int reserve);
165 void VEC_T_embedded_init(VEC(T) *v, int reserve);
167 These allow the caller to perform the memory allocation. */
169 #define VEC_embedded_size(TDEF,A) (VEC_OP(TDEF,embedded_size)(A))
170 #define VEC_embedded_init(TDEF,O,A) (VEC_OP(TDEF,embedded_init)(O,A))
172 /* Determine if a vector has additional capacity.
174 int VEC_T_space (VEC(T) *v,int reserve)
176 If V has space for RESERVE additional entries, return non-zero. If
177 RESERVE is < 0, ensure there is at least one space slot. You
178 usually only need to use this if you are doing your own vector
179 reallocation, for instance on an embedded vector. This returns
180 non-zero in exactly the same circumstances that VEC_T_reserve
181 will. */
183 #define VEC_space(TDEF,V,R) (VEC_OP(TDEF,space)(V,R))
185 /* Reserve space.
186 int VEC_T_reserve(VEC(T) *&v, int reserve);
188 Ensure that V has at least RESERVE slots available, if RESERVE is
189 >= 0. If RESERVE < 0, ensure that there is at least one spare
190 slot. These differ in their reallocation behaviour, the first will
191 not create additional headroom, but the second mechanism will
192 perform the usual exponential headroom increase. Note this can
193 cause V to be reallocated. Returns non-zero iff reallocation
194 actually occurred. */
196 #define VEC_reserve(TDEF,V,R) (VEC_OP(TDEF,reserve)(&(V),R MEM_STAT_INFO))
198 /* Push object with no reallocation
199 T *VEC_T_quick_push (VEC(T) *v, T obj); // Pointer
200 T *VEC_T_quick_push (VEC(T) *v, T *obj); // Object
202 Push a new element onto the end, returns a pointer to the slot
203 filled in. For object vectors, the new value can be NULL, in which
204 case NO initialization is performed. Aborts if there is
205 insufficient space in the vector. */
207 #define VEC_quick_push(TDEF,V,O) \
208 (VEC_OP(TDEF,quick_push)(V,O VEC_CHECK_INFO))
210 /* Push object with reallocation
211 T *VEC_T_safe_push (VEC(T) *&v, T obj); // Pointer
212 T *VEC_T_safe_push (VEC(T) *&v, T *obj); // Object
214 Push a new element onto the end, returns a pointer to the slot
215 filled in. For object vectors, the new value can be NULL, in which
216 case NO initialization is performed. Reallocates V, if needed. */
218 #define VEC_safe_push(TDEF,V,O) \
219 (VEC_OP(TDEF,safe_push)(&(V),O VEC_CHECK_INFO MEM_STAT_INFO))
221 /* Pop element off end
222 T VEC_T_pop (VEC(T) *v); // Pointer
223 void VEC_T_pop (VEC(T) *v); // Object
225 Pop the last element off the end. Returns the element popped, for
226 pointer vectors. */
228 #define VEC_pop(TDEF,V) (VEC_OP(TDEF,pop)(V VEC_CHECK_INFO))
230 /* Truncate to specific length
231 void VEC_T_truncate (VEC(T) *v, unsigned len);
233 Set the length as specified. This is an O(1) operation. */
235 #define VEC_truncate(TDEF,V,I) \
236 (VEC_OP(TDEF,truncate)(V,I VEC_CHECK_INFO))
238 /* Replace element
239 T VEC_T_replace (VEC(T) *v, unsigned ix, T val); // Pointer
240 T *VEC_T_replace (VEC(T) *v, unsigned ix, T *val); // Object
242 Replace the IXth element of V with a new value, VAL. For pointer
243 vectors returns the original value. For object vectors returns a
244 pointer to the new value. For object vectors the new value can be
245 NULL, in which case no overwriting of the slot is actually
246 performed. */
248 #define VEC_replace(TDEF,V,I,O) \
249 (VEC_OP(TDEF,replace)(V,I,O VEC_CHECK_INFO))
251 /* Insert object with no reallocation
252 T *VEC_T_quick_insert (VEC(T) *v, unsigned ix, T val); // Pointer
253 T *VEC_T_quick_insert (VEC(T) *v, unsigned ix, T *val); // Object
255 Insert an element, VAL, at the IXth position of V. Return a pointer
256 to the slot created. For vectors of object, the new value can be
257 NULL, in which case no initialization of the inserted slot takes
258 place. Aborts if there is insufficient space. */
260 #define VEC_quick_insert(TDEF,V,I,O) \
261 (VEC_OP(TDEF,quick_insert)(V,I,O VEC_CHECK_INFO))
263 /* Insert object with reallocation
264 T *VEC_T_safe_insert (VEC(T) *&v, unsigned ix, T val); // Pointer
265 T *VEC_T_safe_insert (VEC(T) *&v, unsigned ix, T *val); // Object
267 Insert an element, VAL, at the IXth position of V. Return a pointer
268 to the slot created. For vectors of object, the new value can be
269 NULL, in which case no initialization of the inserted slot takes
270 place. Reallocate V, if necessary. */
272 #define VEC_safe_insert(TDEF,V,I,O) \
273 (VEC_OP(TDEF,safe_insert)(&(V),I,O VEC_CHECK_INFO MEM_STAT_INFO))
275 /* Remove element retaining order
276 T VEC_T_ordered_remove (VEC(T) *v, unsigned ix); // Pointer
277 void VEC_T_ordered_remove (VEC(T) *v, unsigned ix); // Object
279 Remove an element from the IXth position of V. Ordering of
280 remaining elements is preserverd. For pointer vectors returns the
281 removed object. This is an O(N) operation due to a memmove. */
283 #define VEC_ordered_remove(TDEF,V,I) \
284 (VEC_OP(TDEF,ordered_remove)(V,I VEC_CHECK_INFO))
286 /* Remove element destroying order
287 T VEC_T_unordered_remove (VEC(T) *v, unsigned ix); // Pointer
288 void VEC_T_unordered_remove (VEC(T) *v, unsigned ix); // Object
290 Remove an element from the IXth position of V. Ordering of
291 remaining elements is destroyed. For pointer vectors returns the
292 removed object. This is an O(1) operation. */
294 #define VEC_unordered_remove(TDEF,V,I) \
295 (VEC_OP(TDEF,unordered_remove)(V,I VEC_CHECK_INFO))
297 /* Get the address of the array of elements
298 T *VEC_T_address (VEC(T) v)
300 If you need to directly manipulate the array (for instance, you
301 want to feed it to qsort), use this accessor. */
303 #define VEC_address(TDEF,V) (VEC_OP(TDEF,address)(V))
305 /* Find the first index in the vector not less than the object.
306 unsigned VEC_T_lower_bound (VEC(T) *v, const T val,
307 bool (*lessthan) (const T, const T)); // Pointer
308 unsigned VEC_T_lower_bound (VEC(T) *v, const T *val,
309 bool (*lessthan) (const T*, const T*)); // Object
311 Find the first position in which VAL could be inserted without
312 changing the ordering of V. LESSTHAN is a function that returns
313 true if the first argument is strictly less than the second. */
315 #define VEC_lower_bound(TDEF,V,O,LT) \
316 (VEC_OP(TDEF,lower_bound)(V,O,LT VEC_CHECK_INFO))
318 #if !IN_GENGTYPE
319 /* Reallocate an array of elements with prefix. */
320 extern void *vec_p_reserve (void *, int MEM_STAT_DECL);
321 extern void *vec_o_reserve (void *, int, size_t, size_t MEM_STAT_DECL);
323 #if ENABLE_CHECKING
324 #define VEC_CHECK_INFO ,__FILE__,__LINE__,__FUNCTION__
325 #define VEC_CHECK_DECL ,const char *file_,unsigned line_,const char *function_
326 #define VEC_CHECK_PASS ,file_,line_,function_
328 #define VEC_ASSERT(EXPR,OP,TDEF) \
329 (void)((EXPR) ? 0 : (VEC_ASSERT_FAIL(OP,VEC(TDEF)), 0))
331 extern void vec_assert_fail (const char *, const char * VEC_CHECK_DECL)
332 ATTRIBUTE_NORETURN;
333 #define VEC_ASSERT_FAIL(OP,VEC) vec_assert_fail (OP,#VEC VEC_CHECK_PASS)
334 #else
335 #define VEC_CHECK_INFO
336 #define VEC_CHECK_DECL
337 #define VEC_CHECK_PASS
338 #define VEC_ASSERT(EXPR,OP,TYPE) (void)(EXPR)
339 #endif
341 #define VEC(TDEF) VEC_##TDEF
342 #define VEC_OP(TDEF,OP) VEC_OP_(VEC(TDEF),OP)
343 #define VEC_OP_(VEC,OP) VEC_OP__(VEC,OP)
344 #define VEC_OP__(VEC,OP) VEC ## _ ## OP
345 #else /* IN_GENGTYPE */
346 #define VEC(TDEF) VEC_ TDEF
347 #define VEC_STRINGIFY(X) VEC_STRINGIFY_(X)
348 #define VEC_STRINGIFY_(X) #X
349 #undef GTY
350 #endif /* IN_GENGTYPE */
352 #define VEC_TDEF(TDEF) \
353 typedef struct VEC (TDEF) GTY(()) \
355 unsigned num; \
356 unsigned alloc; \
357 TDEF GTY ((length ("%h.num"))) vec[1]; \
358 } VEC (TDEF)
360 /* Vector of pointer to object. */
361 #if IN_GENGTYPE
362 {"DEF_VEC_P", VEC_STRINGIFY (VEC_TDEF (#)) ";", NULL},
363 #else
365 #define DEF_VEC_P(TDEF) \
366 VEC_TDEF (TDEF); \
368 static inline unsigned VEC_OP (TDEF,length) \
369 (const VEC (TDEF) *vec_) \
371 return vec_ ? vec_->num : 0; \
374 static inline TDEF VEC_OP (TDEF,last) \
375 (const VEC (TDEF) *vec_ VEC_CHECK_DECL) \
377 VEC_ASSERT (vec_ && vec_->num, "last", TDEF); \
379 return vec_->vec[vec_->num - 1]; \
382 static inline TDEF VEC_OP (TDEF,index) \
383 (const VEC (TDEF) *vec_, unsigned ix_ VEC_CHECK_DECL) \
385 VEC_ASSERT (vec_ && ix_ < vec_->num, "index", TDEF); \
387 return vec_->vec[ix_]; \
390 static inline int VEC_OP (TDEF,iterate) \
391 (const VEC (TDEF) *vec_, unsigned ix_, TDEF *ptr) \
393 if (vec_ && ix_ < vec_->num) \
395 *ptr = vec_->vec[ix_]; \
396 return 1; \
398 else \
400 *ptr = 0; \
401 return 0; \
405 static inline VEC (TDEF) *VEC_OP (TDEF,alloc) \
406 (int alloc_ MEM_STAT_DECL) \
408 return (VEC (TDEF) *) vec_p_reserve (NULL, alloc_ - !alloc_ PASS_MEM_STAT);\
411 static inline size_t VEC_OP (TDEF,embedded_size) \
412 (int alloc_) \
414 return offsetof (VEC(TDEF),vec) + alloc_ * sizeof(TDEF); \
417 static inline void VEC_OP (TDEF,embedded_init) \
418 (VEC (TDEF) *vec_, int alloc_) \
420 vec_->num = 0; \
421 vec_->alloc = alloc_; \
424 static inline int VEC_OP (TDEF,space) \
425 (VEC (TDEF) *vec_, int alloc_) \
427 return vec_ ? ((vec_)->alloc - (vec_)->num \
428 < (unsigned)(alloc_ < 0 ? 1 : alloc_)) : alloc_ != 0; \
431 static inline int VEC_OP (TDEF,reserve) \
432 (VEC (TDEF) **vec_, int alloc_ MEM_STAT_DECL) \
434 int extend = VEC_OP (TDEF,space) (*vec_, alloc_); \
436 if (extend) \
437 *vec_ = (VEC (TDEF) *) vec_p_reserve (*vec_, alloc_ PASS_MEM_STAT); \
439 return extend; \
442 static inline TDEF *VEC_OP (TDEF,quick_push) \
443 (VEC (TDEF) *vec_, TDEF obj_ VEC_CHECK_DECL) \
445 TDEF *slot_; \
447 VEC_ASSERT (vec_->num < vec_->alloc, "push", TDEF); \
448 slot_ = &vec_->vec[vec_->num++]; \
449 *slot_ = obj_; \
451 return slot_; \
454 static inline TDEF *VEC_OP (TDEF,safe_push) \
455 (VEC (TDEF) **vec_, TDEF obj_ VEC_CHECK_DECL MEM_STAT_DECL) \
457 VEC_OP (TDEF,reserve) (vec_, -1 PASS_MEM_STAT); \
459 return VEC_OP (TDEF,quick_push) (*vec_, obj_ VEC_CHECK_PASS); \
462 static inline TDEF VEC_OP (TDEF,pop) \
463 (VEC (TDEF) *vec_ VEC_CHECK_DECL) \
465 TDEF obj_; \
467 VEC_ASSERT (vec_->num, "pop", TDEF); \
468 obj_ = vec_->vec[--vec_->num]; \
470 return obj_; \
473 static inline void VEC_OP (TDEF,truncate) \
474 (VEC (TDEF) *vec_, unsigned size_ VEC_CHECK_DECL) \
476 VEC_ASSERT (vec_ ? vec_->num >= size_ : !size_, "truncate", TDEF); \
477 if (vec_) \
478 vec_->num = size_; \
481 static inline TDEF VEC_OP (TDEF,replace) \
482 (VEC (TDEF) *vec_, unsigned ix_, TDEF obj_ VEC_CHECK_DECL) \
484 TDEF old_obj_; \
486 VEC_ASSERT (ix_ < vec_->num, "replace", TDEF); \
487 old_obj_ = vec_->vec[ix_]; \
488 vec_->vec[ix_] = obj_; \
490 return old_obj_; \
493 static inline unsigned VEC_OP (TDEF,lower_bound) \
494 (VEC (TDEF) *vec_, const TDEF obj_, bool (*lessthan_)(const TDEF, const TDEF) VEC_CHECK_DECL) \
496 unsigned int len_ = VEC_OP (TDEF, length) (vec_); \
497 unsigned int half_, middle_; \
498 unsigned int first_ = 0; \
499 while (len_ > 0) \
501 TDEF middle_elem_; \
502 half_ = len_ >> 1; \
503 middle_ = first_; \
504 middle_ += half_; \
505 middle_elem_ = VEC_OP (TDEF, index) (vec_, middle_ VEC_CHECK_PASS); \
506 if (lessthan_ (middle_elem_, obj_)) \
508 first_ = middle_; \
509 ++first_; \
510 len_ = len_ - half_ - 1; \
512 else \
513 len_ = half_; \
515 return first_; \
518 static inline TDEF *VEC_OP (TDEF,quick_insert) \
519 (VEC (TDEF) *vec_, unsigned ix_, TDEF obj_ VEC_CHECK_DECL) \
521 TDEF *slot_; \
523 VEC_ASSERT (vec_->num < vec_->alloc, "insert", TDEF); \
524 VEC_ASSERT (ix_ <= vec_->num, "insert", TDEF); \
525 slot_ = &vec_->vec[ix_]; \
526 memmove (slot_ + 1, slot_, (vec_->num++ - ix_) * sizeof (TDEF)); \
527 *slot_ = obj_; \
529 return slot_; \
532 static inline TDEF *VEC_OP (TDEF,safe_insert) \
533 (VEC (TDEF) **vec_, unsigned ix_, TDEF obj_ \
534 VEC_CHECK_DECL MEM_STAT_DECL) \
536 VEC_OP (TDEF,reserve) (vec_, -1 PASS_MEM_STAT); \
538 return VEC_OP (TDEF,quick_insert) (*vec_, ix_, obj_ VEC_CHECK_PASS); \
541 static inline TDEF VEC_OP (TDEF,ordered_remove) \
542 (VEC (TDEF) *vec_, unsigned ix_ VEC_CHECK_DECL) \
544 TDEF *slot_; \
545 TDEF obj_; \
547 VEC_ASSERT (ix_ < vec_->num, "remove", TDEF); \
548 slot_ = &vec_->vec[ix_]; \
549 obj_ = *slot_; \
550 memmove (slot_, slot_ + 1, (--vec_->num - ix_) * sizeof (TDEF)); \
552 return obj_; \
555 static inline TDEF VEC_OP (TDEF,unordered_remove) \
556 (VEC (TDEF) *vec_, unsigned ix_ VEC_CHECK_DECL) \
558 TDEF *slot_; \
559 TDEF obj_; \
561 VEC_ASSERT (ix_ < vec_->num, "remove", TDEF); \
562 slot_ = &vec_->vec[ix_]; \
563 obj_ = *slot_; \
564 *slot_ = vec_->vec[--vec_->num]; \
566 return obj_; \
569 static inline TDEF *VEC_OP (TDEF,address) \
570 (VEC (TDEF) *vec_) \
572 return vec_ ? vec_->vec : 0; \
575 struct vec_swallow_trailing_semi
576 #endif
578 /* Vector of object. */
579 #if IN_GENGTYPE
580 {"DEF_VEC_O", VEC_STRINGIFY (VEC_TDEF (#)) ";", NULL},
581 #else
583 #define DEF_VEC_O(TDEF) \
584 VEC_TDEF (TDEF); \
586 static inline unsigned VEC_OP (TDEF,length) \
587 (const VEC (TDEF) *vec_) \
589 return vec_ ? vec_->num : 0; \
592 static inline TDEF *VEC_OP (TDEF,last) \
593 (VEC (TDEF) *vec_ VEC_CHECK_DECL) \
595 VEC_ASSERT (vec_ && vec_->num, "last", TDEF); \
597 return &vec_->vec[vec_->num - 1]; \
600 static inline TDEF *VEC_OP (TDEF,index) \
601 (VEC (TDEF) *vec_, unsigned ix_ VEC_CHECK_DECL) \
603 VEC_ASSERT (vec_ && ix_ < vec_->num, "index", TDEF); \
605 return &vec_->vec[ix_]; \
608 static inline int VEC_OP (TDEF,iterate) \
609 (VEC (TDEF) *vec_, unsigned ix_, TDEF **ptr) \
611 if (vec_ && ix_ < vec_->num) \
613 *ptr = &vec_->vec[ix_]; \
614 return 1; \
616 else \
618 *ptr = 0; \
619 return 0; \
623 static inline VEC (TDEF) *VEC_OP (TDEF,alloc) \
624 (int alloc_ MEM_STAT_DECL) \
626 return (VEC (TDEF) *) vec_o_reserve (NULL, alloc_ - !alloc_, \
627 offsetof (VEC(TDEF),vec), sizeof (TDEF)\
628 PASS_MEM_STAT); \
631 static inline size_t VEC_OP (TDEF,embedded_size) \
632 (int alloc_) \
634 return offsetof (VEC(TDEF),vec) + alloc_ * sizeof(TDEF); \
637 static inline void VEC_OP (TDEF,embedded_init) \
638 (VEC (TDEF) *vec_, int alloc_) \
640 vec_->num = 0; \
641 vec_->alloc = alloc_; \
644 static inline int VEC_OP (TDEF,space) \
645 (VEC (TDEF) *vec_, int alloc_) \
647 return vec_ ? ((vec_)->alloc - (vec_)->num \
648 < (unsigned)(alloc_ < 0 ? 1 : alloc_)) : alloc_ != 0; \
651 static inline int VEC_OP (TDEF,reserve) \
652 (VEC (TDEF) **vec_, int alloc_ MEM_STAT_DECL) \
654 int extend = VEC_OP (TDEF,space) (*vec_, alloc_); \
656 if (extend) \
657 *vec_ = (VEC (TDEF) *) vec_o_reserve (*vec_, alloc_, \
658 offsetof (VEC(TDEF),vec), sizeof (TDEF) \
659 PASS_MEM_STAT); \
661 return extend; \
664 static inline TDEF *VEC_OP (TDEF,quick_push) \
665 (VEC (TDEF) *vec_, const TDEF *obj_ VEC_CHECK_DECL) \
667 TDEF *slot_; \
669 VEC_ASSERT (vec_->num < vec_->alloc, "push", TDEF); \
670 slot_ = &vec_->vec[vec_->num++]; \
671 if (obj_) \
672 *slot_ = *obj_; \
674 return slot_; \
677 static inline TDEF *VEC_OP (TDEF,safe_push) \
678 (VEC (TDEF) **vec_, const TDEF *obj_ VEC_CHECK_DECL MEM_STAT_DECL) \
680 VEC_OP (TDEF,reserve) (vec_, -1 PASS_MEM_STAT); \
682 return VEC_OP (TDEF,quick_push) (*vec_, obj_ VEC_CHECK_PASS); \
685 static inline void VEC_OP (TDEF,pop) \
686 (VEC (TDEF) *vec_ VEC_CHECK_DECL) \
688 VEC_ASSERT (vec_->num, "pop", TDEF); \
689 --vec_->num; \
692 static inline void VEC_OP (TDEF,truncate) \
693 (VEC (TDEF) *vec_, unsigned size_ VEC_CHECK_DECL) \
695 VEC_ASSERT (vec_ ? vec_->num >= size_ : !size_, "truncate", TDEF); \
696 if (vec_) \
697 vec_->num = size_; \
700 static inline TDEF *VEC_OP (TDEF,replace) \
701 (VEC (TDEF) *vec_, unsigned ix_, const TDEF *obj_ VEC_CHECK_DECL) \
703 TDEF *slot_; \
705 VEC_ASSERT (ix_ < vec_->num, "replace", TDEF); \
706 slot_ = &vec_->vec[ix_]; \
707 if (obj_) \
708 *slot_ = *obj_; \
710 return slot_; \
713 static inline unsigned VEC_OP (TDEF,lower_bound) \
714 (VEC (TDEF) *vec_, const TDEF *obj_, bool (*lessthan_)(const TDEF *, const TDEF *) VEC_CHECK_DECL) \
716 unsigned int len_ = VEC_OP (TDEF, length) (vec_); \
717 unsigned int half_, middle_; \
718 unsigned int first_ = 0; \
719 while (len_ > 0) \
721 TDEF *middle_elem_; \
722 half_ = len_ >> 1; \
723 middle_ = first_; \
724 middle_ += half_; \
725 middle_elem_ = VEC_OP (TDEF, index) (vec_, middle_ VEC_CHECK_PASS); \
726 if (lessthan_ (middle_elem_, obj_)) \
728 first_ = middle_; \
729 ++first_; \
730 len_ = len_ - half_ - 1; \
732 else \
733 len_ = half_; \
735 return first_; \
738 static inline TDEF *VEC_OP (TDEF,quick_insert) \
739 (VEC (TDEF) *vec_, unsigned ix_, const TDEF *obj_ VEC_CHECK_DECL) \
741 TDEF *slot_; \
743 VEC_ASSERT (vec_->num < vec_->alloc, "insert", TDEF); \
744 VEC_ASSERT (ix_ <= vec_->num, "insert", TDEF); \
745 slot_ = &vec_->vec[ix_]; \
746 memmove (slot_ + 1, slot_, (vec_->num++ - ix_) * sizeof (TDEF)); \
747 if (obj_) \
748 *slot_ = *obj_; \
750 return slot_; \
753 static inline TDEF *VEC_OP (TDEF,safe_insert) \
754 (VEC (TDEF) **vec_, unsigned ix_, const TDEF *obj_ \
755 VEC_CHECK_DECL MEM_STAT_DECL) \
757 VEC_OP (TDEF,reserve) (vec_, -1 PASS_MEM_STAT); \
759 return VEC_OP (TDEF,quick_insert) (*vec_, ix_, obj_ VEC_CHECK_PASS); \
762 static inline void VEC_OP (TDEF,ordered_remove) \
763 (VEC (TDEF) *vec_, unsigned ix_ VEC_CHECK_DECL) \
765 TDEF *slot_; \
767 VEC_ASSERT (ix_ < vec_->num, "remove", TDEF); \
768 slot_ = &vec_->vec[ix_]; \
769 memmove (slot_, slot_ + 1, (--vec_->num - ix_) * sizeof (TDEF)); \
772 static inline void VEC_OP (TDEF,unordered_remove) \
773 (VEC (TDEF) *vec_, unsigned ix_ VEC_CHECK_DECL) \
775 VEC_ASSERT (ix_ < vec_->num, "remove", TDEF); \
776 vec_->vec[ix_] = vec_->vec[--vec_->num]; \
779 static inline TDEF *VEC_OP (TDEF,address) \
780 (VEC (TDEF) *vec_) \
782 return vec_ ? vec_->vec : 0; \
785 struct vec_swallow_trailing_semi
786 #endif
788 #endif /* GCC_VEC_H */