1 /* Vector API for GNU compiler.
2 Copyright (C) 2004-2016 Free Software Foundation, Inc.
3 Contributed by Nathan Sidwell <nathan@codesourcery.com>
4 Re-implemented in C++ by Diego Novillo <dnovillo@google.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
25 /* Some gen* file have no ggc support as the header file gtype-desc.h is
26 missing. Provide these definitions in case ggc.h has not been included.
27 This is not a problem because any code that runs before gengtype is built
28 will never need to use GC vectors.*/
30 extern void ggc_free (void *);
31 extern size_t ggc_round_alloc_size (size_t requested_size
);
32 extern void *ggc_realloc (void *, size_t MEM_STAT_DECL
);
34 /* Templated vector type and associated interfaces.
36 The interface functions are typesafe and use inline functions,
37 sometimes backed by out-of-line generic functions. The vectors are
38 designed to interoperate with the GTY machinery.
40 There are both 'index' and 'iterate' accessors. The index accessor
41 is implemented by operator[]. The iterator returns a boolean
42 iteration condition and updates the iteration variable passed by
43 reference. Because the iterator will be inlined, the address-of
44 can be optimized away.
46 Each operation that increases the number of active elements is
47 available in 'quick' and 'safe' variants. The former presumes that
48 there is sufficient allocated space for the operation to succeed
49 (it dies if there is not). The latter will reallocate the
50 vector, if needed. Reallocation causes an exponential increase in
51 vector size. If you know you will be adding N elements, it would
52 be more efficient to use the reserve operation before adding the
53 elements with the 'quick' operation. This will ensure there are at
54 least as many elements as you ask for, it will exponentially
55 increase if there are too few spare slots. If you want reserve a
56 specific number of slots, but do not want the exponential increase
57 (for instance, you know this is the last allocation), use the
58 reserve_exact operation. You can also create a vector of a
59 specific size from the get go.
61 You should prefer the push and pop operations, as they append and
62 remove from the end of the vector. If you need to remove several
63 items in one go, use the truncate operation. The insert and remove
64 operations allow you to change elements in the middle of the
65 vector. There are two remove operations, one which preserves the
66 element ordering 'ordered_remove', and one which does not
67 'unordered_remove'. The latter function copies the end element
68 into the removed slot, rather than invoke a memmove operation. The
69 'lower_bound' function will determine where to place an item in the
70 array using insert that will maintain sorted order.
72 Vectors are template types with three arguments: the type of the
73 elements in the vector, the allocation strategy, and the physical
76 Four allocation strategies are supported:
78 - Heap: allocation is done using malloc/free. This is the
79 default allocation strategy.
81 - GC: allocation is done using ggc_alloc/ggc_free.
83 - GC atomic: same as GC with the exception that the elements
84 themselves are assumed to be of an atomic type that does
85 not need to be garbage collected. This means that marking
86 routines do not need to traverse the array marking the
87 individual elements. This increases the performance of
90 Two physical layouts are supported:
92 - Embedded: The vector is structured using the trailing array
93 idiom. The last member of the structure is an array of size
94 1. When the vector is initially allocated, a single memory
95 block is created to hold the vector's control data and the
96 array of elements. These vectors cannot grow without
97 reallocation (see discussion on embeddable vectors below).
99 - Space efficient: The vector is structured as a pointer to an
100 embedded vector. This is the default layout. It means that
101 vectors occupy a single word of storage before initial
102 allocation. Vectors are allowed to grow (the internal
103 pointer is reallocated but the main vector instance does not
106 The type, allocation and layout are specified when the vector is
109 If you need to directly manipulate a vector, then the 'address'
110 accessor will return the address of the start of the vector. Also
111 the 'space' predicate will tell you whether there is spare capacity
112 in the vector. You will not normally need to use these two functions.
114 Notes on the different layout strategies
116 * Embeddable vectors (vec<T, A, vl_embed>)
118 These vectors are suitable to be embedded in other data
119 structures so that they can be pre-allocated in a contiguous
122 Embeddable vectors are implemented using the trailing array
123 idiom, thus they are not resizeable without changing the address
124 of the vector object itself. This means you cannot have
125 variables or fields of embeddable vector type -- always use a
126 pointer to a vector. The one exception is the final field of a
127 structure, which could be a vector type.
129 You will have to use the embedded_size & embedded_init calls to
130 create such objects, and they will not be resizeable (so the
131 'safe' allocation variants are not available).
133 Properties of embeddable vectors:
135 - The whole vector and control data are allocated in a single
136 contiguous block. It uses the trailing-vector idiom, so
137 allocation must reserve enough space for all the elements
138 in the vector plus its control data.
139 - The vector cannot be re-allocated.
140 - The vector cannot grow nor shrink.
141 - No indirections needed for access/manipulation.
142 - It requires 2 words of storage (prior to vector allocation).
145 * Space efficient vector (vec<T, A, vl_ptr>)
147 These vectors can grow dynamically and are allocated together
148 with their control data. They are suited to be included in data
149 structures. Prior to initial allocation, they only take a single
152 These vectors are implemented as a pointer to embeddable vectors.
153 The semantics allow for this pointer to be NULL to represent
154 empty vectors. This way, empty vectors occupy minimal space in
155 the structure containing them.
159 - The whole vector and control data are allocated in a single
161 - The whole vector may be re-allocated.
162 - Vector data may grow and shrink.
163 - Access and manipulation requires a pointer test and
165 - It requires 1 word of storage (prior to vector allocation).
167 An example of their use would be,
170 // A space-efficient vector of tree pointers in GC memory.
171 vec<tree, va_gc, vl_ptr> v;
176 if (s->v.length ()) { we have some contents }
177 s->v.safe_push (decl); // append some decl onto the end
178 for (ix = 0; s->v.iterate (ix, &elt); ix++)
179 { do something with elt }
182 /* Support function for statistics. */
183 extern void dump_vec_loc_statistics (void);
185 /* Hashtable mapping vec addresses to descriptors. */
186 extern htab_t vec_mem_usage_hash
;
188 /* Control data for vectors. This contains the number of allocated
189 and used slots inside a vector. */
193 /* FIXME - These fields should be private, but we need to cater to
194 compilers that have stricter notions of PODness for types. */
196 /* Memory allocation support routines in vec.c. */
197 void register_overhead (void *, size_t, size_t CXX_MEM_STAT_INFO
);
198 void release_overhead (void *, size_t, bool CXX_MEM_STAT_INFO
);
199 static unsigned calculate_allocation (vec_prefix
*, unsigned, bool);
200 static unsigned calculate_allocation_1 (unsigned, unsigned);
202 /* Note that vec_prefix should be a base class for vec, but we use
203 offsetof() on vector fields of tree structures (e.g.,
204 tree_binfo::base_binfos), and offsetof only supports base types.
206 To compensate, we make vec_prefix a field inside vec and make
207 vec a friend class of vec_prefix so it can access its fields. */
208 template <typename
, typename
, typename
> friend struct vec
;
210 /* The allocator types also need access to our internals. */
212 friend struct va_gc_atomic
;
213 friend struct va_heap
;
215 unsigned m_alloc
: 31;
216 unsigned m_using_auto_storage
: 1;
220 /* Calculate the number of slots to reserve a vector, making sure that
221 RESERVE slots are free. If EXACT grow exactly, otherwise grow
222 exponentially. PFX is the control data for the vector. */
225 vec_prefix::calculate_allocation (vec_prefix
*pfx
, unsigned reserve
,
229 return (pfx
? pfx
->m_num
: 0) + reserve
;
231 return MAX (4, reserve
);
232 return calculate_allocation_1 (pfx
->m_alloc
, pfx
->m_num
+ reserve
);
235 template<typename
, typename
, typename
> struct vec
;
237 /* Valid vector layouts
239 vl_embed - Embeddable vector that uses the trailing array idiom.
240 vl_ptr - Space efficient vector that uses a pointer to an
241 embeddable vector. */
246 /* Types of supported allocations
248 va_heap - Allocation uses malloc/free.
249 va_gc - Allocation uses ggc_alloc.
250 va_gc_atomic - Same as GC, but individual elements of the array
251 do not need to be marked during collection. */
253 /* Allocator type for heap vectors. */
256 /* Heap vectors are frequently regular instances, so use the vl_ptr
258 typedef vl_ptr default_layout
;
261 static void reserve (vec
<T
, va_heap
, vl_embed
> *&, unsigned, bool
265 static void release (vec
<T
, va_heap
, vl_embed
> *&);
269 /* Allocator for heap memory. Ensure there are at least RESERVE free
270 slots in V. If EXACT is true, grow exactly, else grow
271 exponentially. As a special case, if the vector had not been
272 allocated and RESERVE is 0, no vector will be created. */
276 va_heap::reserve (vec
<T
, va_heap
, vl_embed
> *&v
, unsigned reserve
, bool exact
280 = vec_prefix::calculate_allocation (v
? &v
->m_vecpfx
: 0, reserve
, exact
);
281 gcc_checking_assert (alloc
);
283 if (GATHER_STATISTICS
&& v
)
284 v
->m_vecpfx
.release_overhead (v
, v
->allocated (), false);
286 size_t size
= vec
<T
, va_heap
, vl_embed
>::embedded_size (alloc
);
287 unsigned nelem
= v
? v
->length () : 0;
288 v
= static_cast <vec
<T
, va_heap
, vl_embed
> *> (xrealloc (v
, size
));
289 v
->embedded_init (alloc
, nelem
);
291 if (GATHER_STATISTICS
)
292 v
->m_vecpfx
.register_overhead (v
, alloc
, nelem PASS_MEM_STAT
);
296 /* Free the heap space allocated for vector V. */
300 va_heap::release (vec
<T
, va_heap
, vl_embed
> *&v
)
305 if (GATHER_STATISTICS
)
306 v
->m_vecpfx
.release_overhead (v
, v
->allocated (), true);
312 /* Allocator type for GC vectors. Notice that we need the structure
313 declaration even if GC is not enabled. */
317 /* Use vl_embed as the default layout for GC vectors. Due to GTY
318 limitations, GC vectors must always be pointers, so it is more
319 efficient to use a pointer to the vl_embed layout, rather than
320 using a pointer to a pointer as would be the case with vl_ptr. */
321 typedef vl_embed default_layout
;
323 template<typename T
, typename A
>
324 static void reserve (vec
<T
, A
, vl_embed
> *&, unsigned, bool
327 template<typename T
, typename A
>
328 static void release (vec
<T
, A
, vl_embed
> *&v
);
332 /* Free GC memory used by V and reset V to NULL. */
334 template<typename T
, typename A
>
336 va_gc::release (vec
<T
, A
, vl_embed
> *&v
)
344 /* Allocator for GC memory. Ensure there are at least RESERVE free
345 slots in V. If EXACT is true, grow exactly, else grow
346 exponentially. As a special case, if the vector had not been
347 allocated and RESERVE is 0, no vector will be created. */
349 template<typename T
, typename A
>
351 va_gc::reserve (vec
<T
, A
, vl_embed
> *&v
, unsigned reserve
, bool exact
355 = vec_prefix::calculate_allocation (v
? &v
->m_vecpfx
: 0, reserve
, exact
);
363 /* Calculate the amount of space we want. */
364 size_t size
= vec
<T
, A
, vl_embed
>::embedded_size (alloc
);
366 /* Ask the allocator how much space it will really give us. */
367 size
= ::ggc_round_alloc_size (size
);
369 /* Adjust the number of slots accordingly. */
370 size_t vec_offset
= sizeof (vec_prefix
);
371 size_t elt_size
= sizeof (T
);
372 alloc
= (size
- vec_offset
) / elt_size
;
374 /* And finally, recalculate the amount of space we ask for. */
375 size
= vec_offset
+ alloc
* elt_size
;
377 unsigned nelem
= v
? v
->length () : 0;
378 v
= static_cast <vec
<T
, A
, vl_embed
> *> (::ggc_realloc (v
, size
380 v
->embedded_init (alloc
, nelem
);
384 /* Allocator type for GC vectors. This is for vectors of types
385 atomics w.r.t. collection, so allocation and deallocation is
386 completely inherited from va_gc. */
387 struct va_gc_atomic
: va_gc
392 /* Generic vector template. Default values for A and L indicate the
393 most commonly used strategies.
395 FIXME - Ideally, they would all be vl_ptr to encourage using regular
396 instances for vectors, but the existing GTY machinery is limited
397 in that it can only deal with GC objects that are pointers
400 This means that vector operations that need to deal with
401 potentially NULL pointers, must be provided as free
402 functions (see the vec_safe_* functions above). */
404 typename A
= va_heap
,
405 typename L
= typename
A::default_layout
>
406 struct GTY((user
)) vec
410 /* Type to provide NULL values for vec<T, A, L>. This is used to
411 provide nil initializers for vec instances. Since vec must be
412 a POD, we cannot have proper ctor/dtor for it. To initialize
413 a vec instance, you can assign it the value vNULL. This isn't
414 needed for file-scope and function-local static vectors, which
415 are zero-initialized by default. */
418 template <typename T
, typename A
, typename L
>
419 #if __cpp_constexpr >= 200704
422 operator vec
<T
, A
, L
> () { return vec
<T
, A
, L
>(); }
427 /* Embeddable vector. These vectors are suitable to be embedded
428 in other data structures so that they can be pre-allocated in a
429 contiguous memory block.
431 Embeddable vectors are implemented using the trailing array idiom,
432 thus they are not resizeable without changing the address of the
433 vector object itself. This means you cannot have variables or
434 fields of embeddable vector type -- always use a pointer to a
435 vector. The one exception is the final field of a structure, which
436 could be a vector type.
438 You will have to use the embedded_size & embedded_init calls to
439 create such objects, and they will not be resizeable (so the 'safe'
440 allocation variants are not available).
444 - The whole vector and control data are allocated in a single
445 contiguous block. It uses the trailing-vector idiom, so
446 allocation must reserve enough space for all the elements
447 in the vector plus its control data.
448 - The vector cannot be re-allocated.
449 - The vector cannot grow nor shrink.
450 - No indirections needed for access/manipulation.
451 - It requires 2 words of storage (prior to vector allocation). */
453 template<typename T
, typename A
>
454 struct GTY((user
)) vec
<T
, A
, vl_embed
>
457 unsigned allocated (void) const { return m_vecpfx
.m_alloc
; }
458 unsigned length (void) const { return m_vecpfx
.m_num
; }
459 bool is_empty (void) const { return m_vecpfx
.m_num
== 0; }
460 T
*address (void) { return m_vecdata
; }
461 const T
*address (void) const { return m_vecdata
; }
462 T
*begin () { return address (); }
463 const T
*begin () const { return address (); }
464 T
*end () { return address () + length (); }
465 const T
*end () const { return address () + length (); }
466 const T
&operator[] (unsigned) const;
467 T
&operator[] (unsigned);
469 bool space (unsigned) const;
470 bool iterate (unsigned, T
*) const;
471 bool iterate (unsigned, T
**) const;
472 vec
*copy (ALONE_CXX_MEM_STAT_INFO
) const;
473 void splice (const vec
&);
474 void splice (const vec
*src
);
475 T
*quick_push (const T
&);
477 void truncate (unsigned);
478 void quick_insert (unsigned, const T
&);
479 void ordered_remove (unsigned);
480 void unordered_remove (unsigned);
481 void block_remove (unsigned, unsigned);
482 void qsort (int (*) (const void *, const void *));
483 T
*bsearch (const void *key
, int (*compar
)(const void *, const void *));
484 unsigned lower_bound (T
, bool (*)(const T
&, const T
&)) const;
485 bool contains (const T
&search
) const;
486 static size_t embedded_size (unsigned);
487 void embedded_init (unsigned, unsigned = 0, unsigned = 0);
488 void quick_grow (unsigned len
);
489 void quick_grow_cleared (unsigned len
);
491 /* vec class can access our internal data and functions. */
492 template <typename
, typename
, typename
> friend struct vec
;
494 /* The allocator types also need access to our internals. */
496 friend struct va_gc_atomic
;
497 friend struct va_heap
;
499 /* FIXME - These fields should be private, but we need to cater to
500 compilers that have stricter notions of PODness for types. */
506 /* Convenience wrapper functions to use when dealing with pointers to
507 embedded vectors. Some functionality for these vectors must be
508 provided via free functions for these reasons:
510 1- The pointer may be NULL (e.g., before initial allocation).
512 2- When the vector needs to grow, it must be reallocated, so
513 the pointer will change its value.
515 Because of limitations with the current GC machinery, all vectors
516 in GC memory *must* be pointers. */
519 /* If V contains no room for NELEMS elements, return false. Otherwise,
521 template<typename T
, typename A
>
523 vec_safe_space (const vec
<T
, A
, vl_embed
> *v
, unsigned nelems
)
525 return v
? v
->space (nelems
) : nelems
== 0;
529 /* If V is NULL, return 0. Otherwise, return V->length(). */
530 template<typename T
, typename A
>
532 vec_safe_length (const vec
<T
, A
, vl_embed
> *v
)
534 return v
? v
->length () : 0;
538 /* If V is NULL, return NULL. Otherwise, return V->address(). */
539 template<typename T
, typename A
>
541 vec_safe_address (vec
<T
, A
, vl_embed
> *v
)
543 return v
? v
->address () : NULL
;
547 /* If V is NULL, return true. Otherwise, return V->is_empty(). */
548 template<typename T
, typename A
>
550 vec_safe_is_empty (vec
<T
, A
, vl_embed
> *v
)
552 return v
? v
->is_empty () : true;
555 /* If V does not have space for NELEMS elements, call
556 V->reserve(NELEMS, EXACT). */
557 template<typename T
, typename A
>
559 vec_safe_reserve (vec
<T
, A
, vl_embed
> *&v
, unsigned nelems
, bool exact
= false
562 bool extend
= nelems
? !vec_safe_space (v
, nelems
) : false;
564 A::reserve (v
, nelems
, exact PASS_MEM_STAT
);
568 template<typename T
, typename A
>
570 vec_safe_reserve_exact (vec
<T
, A
, vl_embed
> *&v
, unsigned nelems
573 return vec_safe_reserve (v
, nelems
, true PASS_MEM_STAT
);
577 /* Allocate GC memory for V with space for NELEMS slots. If NELEMS
578 is 0, V is initialized to NULL. */
580 template<typename T
, typename A
>
582 vec_alloc (vec
<T
, A
, vl_embed
> *&v
, unsigned nelems CXX_MEM_STAT_INFO
)
585 vec_safe_reserve (v
, nelems
, false PASS_MEM_STAT
);
589 /* Free the GC memory allocated by vector V and set it to NULL. */
591 template<typename T
, typename A
>
593 vec_free (vec
<T
, A
, vl_embed
> *&v
)
599 /* Grow V to length LEN. Allocate it, if necessary. */
600 template<typename T
, typename A
>
602 vec_safe_grow (vec
<T
, A
, vl_embed
> *&v
, unsigned len CXX_MEM_STAT_INFO
)
604 unsigned oldlen
= vec_safe_length (v
);
605 gcc_checking_assert (len
>= oldlen
);
606 vec_safe_reserve_exact (v
, len
- oldlen PASS_MEM_STAT
);
611 /* If V is NULL, allocate it. Call V->safe_grow_cleared(LEN). */
612 template<typename T
, typename A
>
614 vec_safe_grow_cleared (vec
<T
, A
, vl_embed
> *&v
, unsigned len CXX_MEM_STAT_INFO
)
616 unsigned oldlen
= vec_safe_length (v
);
617 vec_safe_grow (v
, len PASS_MEM_STAT
);
618 memset (&(v
->address ()[oldlen
]), 0, sizeof (T
) * (len
- oldlen
));
622 /* If V is NULL return false, otherwise return V->iterate(IX, PTR). */
623 template<typename T
, typename A
>
625 vec_safe_iterate (const vec
<T
, A
, vl_embed
> *v
, unsigned ix
, T
**ptr
)
628 return v
->iterate (ix
, ptr
);
636 template<typename T
, typename A
>
638 vec_safe_iterate (const vec
<T
, A
, vl_embed
> *v
, unsigned ix
, T
*ptr
)
641 return v
->iterate (ix
, ptr
);
650 /* If V has no room for one more element, reallocate it. Then call
651 V->quick_push(OBJ). */
652 template<typename T
, typename A
>
654 vec_safe_push (vec
<T
, A
, vl_embed
> *&v
, const T
&obj CXX_MEM_STAT_INFO
)
656 vec_safe_reserve (v
, 1, false PASS_MEM_STAT
);
657 return v
->quick_push (obj
);
661 /* if V has no room for one more element, reallocate it. Then call
662 V->quick_insert(IX, OBJ). */
663 template<typename T
, typename A
>
665 vec_safe_insert (vec
<T
, A
, vl_embed
> *&v
, unsigned ix
, const T
&obj
668 vec_safe_reserve (v
, 1, false PASS_MEM_STAT
);
669 v
->quick_insert (ix
, obj
);
673 /* If V is NULL, do nothing. Otherwise, call V->truncate(SIZE). */
674 template<typename T
, typename A
>
676 vec_safe_truncate (vec
<T
, A
, vl_embed
> *v
, unsigned size
)
683 /* If SRC is not NULL, return a pointer to a copy of it. */
684 template<typename T
, typename A
>
685 inline vec
<T
, A
, vl_embed
> *
686 vec_safe_copy (vec
<T
, A
, vl_embed
> *src CXX_MEM_STAT_INFO
)
688 return src
? src
->copy (ALONE_PASS_MEM_STAT
) : NULL
;
691 /* Copy the elements from SRC to the end of DST as if by memcpy.
692 Reallocate DST, if necessary. */
693 template<typename T
, typename A
>
695 vec_safe_splice (vec
<T
, A
, vl_embed
> *&dst
, const vec
<T
, A
, vl_embed
> *src
698 unsigned src_len
= vec_safe_length (src
);
701 vec_safe_reserve_exact (dst
, vec_safe_length (dst
) + src_len
707 /* Return true if SEARCH is an element of V. Note that this is O(N) in the
708 size of the vector and so should be used with care. */
710 template<typename T
, typename A
>
712 vec_safe_contains (vec
<T
, A
, vl_embed
> *v
, const T
&search
)
714 return v
? v
->contains (search
) : false;
717 /* Index into vector. Return the IX'th element. IX must be in the
718 domain of the vector. */
720 template<typename T
, typename A
>
722 vec
<T
, A
, vl_embed
>::operator[] (unsigned ix
) const
724 gcc_checking_assert (ix
< m_vecpfx
.m_num
);
725 return m_vecdata
[ix
];
728 template<typename T
, typename A
>
730 vec
<T
, A
, vl_embed
>::operator[] (unsigned ix
)
732 gcc_checking_assert (ix
< m_vecpfx
.m_num
);
733 return m_vecdata
[ix
];
737 /* Get the final element of the vector, which must not be empty. */
739 template<typename T
, typename A
>
741 vec
<T
, A
, vl_embed
>::last (void)
743 gcc_checking_assert (m_vecpfx
.m_num
> 0);
744 return (*this)[m_vecpfx
.m_num
- 1];
748 /* If this vector has space for NELEMS additional entries, return
749 true. You usually only need to use this if you are doing your
750 own vector reallocation, for instance on an embedded vector. This
751 returns true in exactly the same circumstances that vec::reserve
754 template<typename T
, typename A
>
756 vec
<T
, A
, vl_embed
>::space (unsigned nelems
) const
758 return m_vecpfx
.m_alloc
- m_vecpfx
.m_num
>= nelems
;
762 /* Return iteration condition and update PTR to point to the IX'th
763 element of this vector. Use this to iterate over the elements of a
766 for (ix = 0; vec<T, A>::iterate (v, ix, &ptr); ix++)
769 template<typename T
, typename A
>
771 vec
<T
, A
, vl_embed
>::iterate (unsigned ix
, T
*ptr
) const
773 if (ix
< m_vecpfx
.m_num
)
775 *ptr
= m_vecdata
[ix
];
786 /* Return iteration condition and update *PTR to point to the
787 IX'th element of this vector. Use this to iterate over the
788 elements of a vector as follows,
790 for (ix = 0; v->iterate (ix, &ptr); ix++)
793 This variant is for vectors of objects. */
795 template<typename T
, typename A
>
797 vec
<T
, A
, vl_embed
>::iterate (unsigned ix
, T
**ptr
) const
799 if (ix
< m_vecpfx
.m_num
)
801 *ptr
= CONST_CAST (T
*, &m_vecdata
[ix
]);
812 /* Return a pointer to a copy of this vector. */
814 template<typename T
, typename A
>
815 inline vec
<T
, A
, vl_embed
> *
816 vec
<T
, A
, vl_embed
>::copy (ALONE_MEM_STAT_DECL
) const
818 vec
<T
, A
, vl_embed
> *new_vec
= NULL
;
819 unsigned len
= length ();
822 vec_alloc (new_vec
, len PASS_MEM_STAT
);
823 new_vec
->embedded_init (len
, len
);
824 memcpy (new_vec
->address (), m_vecdata
, sizeof (T
) * len
);
830 /* Copy the elements from SRC to the end of this vector as if by memcpy.
831 The vector must have sufficient headroom available. */
833 template<typename T
, typename A
>
835 vec
<T
, A
, vl_embed
>::splice (const vec
<T
, A
, vl_embed
> &src
)
837 unsigned len
= src
.length ();
840 gcc_checking_assert (space (len
));
841 memcpy (address () + length (), src
.address (), len
* sizeof (T
));
842 m_vecpfx
.m_num
+= len
;
846 template<typename T
, typename A
>
848 vec
<T
, A
, vl_embed
>::splice (const vec
<T
, A
, vl_embed
> *src
)
855 /* Push OBJ (a new element) onto the end of the vector. There must be
856 sufficient space in the vector. Return a pointer to the slot
857 where OBJ was inserted. */
859 template<typename T
, typename A
>
861 vec
<T
, A
, vl_embed
>::quick_push (const T
&obj
)
863 gcc_checking_assert (space (1));
864 T
*slot
= &m_vecdata
[m_vecpfx
.m_num
++];
870 /* Pop and return the last element off the end of the vector. */
872 template<typename T
, typename A
>
874 vec
<T
, A
, vl_embed
>::pop (void)
876 gcc_checking_assert (length () > 0);
877 return m_vecdata
[--m_vecpfx
.m_num
];
881 /* Set the length of the vector to SIZE. The new length must be less
882 than or equal to the current length. This is an O(1) operation. */
884 template<typename T
, typename A
>
886 vec
<T
, A
, vl_embed
>::truncate (unsigned size
)
888 gcc_checking_assert (length () >= size
);
889 m_vecpfx
.m_num
= size
;
893 /* Insert an element, OBJ, at the IXth position of this vector. There
894 must be sufficient space. */
896 template<typename T
, typename A
>
898 vec
<T
, A
, vl_embed
>::quick_insert (unsigned ix
, const T
&obj
)
900 gcc_checking_assert (length () < allocated ());
901 gcc_checking_assert (ix
<= length ());
902 T
*slot
= &m_vecdata
[ix
];
903 memmove (slot
+ 1, slot
, (m_vecpfx
.m_num
++ - ix
) * sizeof (T
));
908 /* Remove an element from the IXth position of this vector. Ordering of
909 remaining elements is preserved. This is an O(N) operation due to
912 template<typename T
, typename A
>
914 vec
<T
, A
, vl_embed
>::ordered_remove (unsigned ix
)
916 gcc_checking_assert (ix
< length ());
917 T
*slot
= &m_vecdata
[ix
];
918 memmove (slot
, slot
+ 1, (--m_vecpfx
.m_num
- ix
) * sizeof (T
));
922 /* Remove an element from the IXth position of this vector. Ordering of
923 remaining elements is destroyed. This is an O(1) operation. */
925 template<typename T
, typename A
>
927 vec
<T
, A
, vl_embed
>::unordered_remove (unsigned ix
)
929 gcc_checking_assert (ix
< length ());
930 m_vecdata
[ix
] = m_vecdata
[--m_vecpfx
.m_num
];
934 /* Remove LEN elements starting at the IXth. Ordering is retained.
935 This is an O(N) operation due to memmove. */
937 template<typename T
, typename A
>
939 vec
<T
, A
, vl_embed
>::block_remove (unsigned ix
, unsigned len
)
941 gcc_checking_assert (ix
+ len
<= length ());
942 T
*slot
= &m_vecdata
[ix
];
943 m_vecpfx
.m_num
-= len
;
944 memmove (slot
, slot
+ len
, (m_vecpfx
.m_num
- ix
) * sizeof (T
));
948 /* Sort the contents of this vector with qsort. CMP is the comparison
949 function to pass to qsort. */
951 template<typename T
, typename A
>
953 vec
<T
, A
, vl_embed
>::qsort (int (*cmp
) (const void *, const void *))
956 ::qsort (address (), length (), sizeof (T
), cmp
);
960 /* Search the contents of the sorted vector with a binary search.
961 CMP is the comparison function to pass to bsearch. */
963 template<typename T
, typename A
>
965 vec
<T
, A
, vl_embed
>::bsearch (const void *key
,
966 int (*compar
) (const void *, const void *))
968 const void *base
= this->address ();
969 size_t nmemb
= this->length ();
970 size_t size
= sizeof (T
);
971 /* The following is a copy of glibc stdlib-bsearch.h. */
981 p
= (const void *) (((const char *) base
) + (idx
* size
));
982 comparison
= (*compar
) (key
, p
);
985 else if (comparison
> 0)
988 return (T
*)const_cast<void *>(p
);
994 /* Return true if SEARCH is an element of V. Note that this is O(N) in the
995 size of the vector and so should be used with care. */
997 template<typename T
, typename A
>
999 vec
<T
, A
, vl_embed
>::contains (const T
&search
) const
1001 unsigned int len
= length ();
1002 for (unsigned int i
= 0; i
< len
; i
++)
1003 if ((*this)[i
] == search
)
1009 /* Find and return the first position in which OBJ could be inserted
1010 without changing the ordering of this vector. LESSTHAN is a
1011 function that returns true if the first argument is strictly less
1014 template<typename T
, typename A
>
1016 vec
<T
, A
, vl_embed
>::lower_bound (T obj
, bool (*lessthan
)(const T
&, const T
&))
1019 unsigned int len
= length ();
1020 unsigned int half
, middle
;
1021 unsigned int first
= 0;
1027 T middle_elem
= (*this)[middle
];
1028 if (lessthan (middle_elem
, obj
))
1032 len
= len
- half
- 1;
1041 /* Return the number of bytes needed to embed an instance of an
1042 embeddable vec inside another data structure.
1044 Use these methods to determine the required size and initialization
1045 of a vector V of type T embedded within another structure (as the
1048 size_t vec<T, A, vl_embed>::embedded_size (unsigned alloc);
1049 void v->embedded_init (unsigned alloc, unsigned num);
1051 These allow the caller to perform the memory allocation. */
1053 template<typename T
, typename A
>
1055 vec
<T
, A
, vl_embed
>::embedded_size (unsigned alloc
)
1057 typedef vec
<T
, A
, vl_embed
> vec_embedded
;
1058 return offsetof (vec_embedded
, m_vecdata
) + alloc
* sizeof (T
);
1062 /* Initialize the vector to contain room for ALLOC elements and
1063 NUM active elements. */
1065 template<typename T
, typename A
>
1067 vec
<T
, A
, vl_embed
>::embedded_init (unsigned alloc
, unsigned num
, unsigned aut
)
1069 m_vecpfx
.m_alloc
= alloc
;
1070 m_vecpfx
.m_using_auto_storage
= aut
;
1071 m_vecpfx
.m_num
= num
;
1075 /* Grow the vector to a specific length. LEN must be as long or longer than
1076 the current length. The new elements are uninitialized. */
1078 template<typename T
, typename A
>
1080 vec
<T
, A
, vl_embed
>::quick_grow (unsigned len
)
1082 gcc_checking_assert (length () <= len
&& len
<= m_vecpfx
.m_alloc
);
1083 m_vecpfx
.m_num
= len
;
1087 /* Grow the vector to a specific length. LEN must be as long or longer than
1088 the current length. The new elements are initialized to zero. */
1090 template<typename T
, typename A
>
1092 vec
<T
, A
, vl_embed
>::quick_grow_cleared (unsigned len
)
1094 unsigned oldlen
= length ();
1095 size_t sz
= sizeof (T
) * (len
- oldlen
);
1098 memset (&(address ()[oldlen
]), 0, sz
);
1102 /* Garbage collection support for vec<T, A, vl_embed>. */
1104 template<typename T
>
1106 gt_ggc_mx (vec
<T
, va_gc
> *v
)
1108 extern void gt_ggc_mx (T
&);
1109 for (unsigned i
= 0; i
< v
->length (); i
++)
1110 gt_ggc_mx ((*v
)[i
]);
1113 template<typename T
>
1115 gt_ggc_mx (vec
<T
, va_gc_atomic
, vl_embed
> *v ATTRIBUTE_UNUSED
)
1117 /* Nothing to do. Vectors of atomic types wrt GC do not need to
1122 /* PCH support for vec<T, A, vl_embed>. */
1124 template<typename T
, typename A
>
1126 gt_pch_nx (vec
<T
, A
, vl_embed
> *v
)
1128 extern void gt_pch_nx (T
&);
1129 for (unsigned i
= 0; i
< v
->length (); i
++)
1130 gt_pch_nx ((*v
)[i
]);
1133 template<typename T
, typename A
>
1135 gt_pch_nx (vec
<T
*, A
, vl_embed
> *v
, gt_pointer_operator op
, void *cookie
)
1137 for (unsigned i
= 0; i
< v
->length (); i
++)
1138 op (&((*v
)[i
]), cookie
);
1141 template<typename T
, typename A
>
1143 gt_pch_nx (vec
<T
, A
, vl_embed
> *v
, gt_pointer_operator op
, void *cookie
)
1145 extern void gt_pch_nx (T
*, gt_pointer_operator
, void *);
1146 for (unsigned i
= 0; i
< v
->length (); i
++)
1147 gt_pch_nx (&((*v
)[i
]), op
, cookie
);
1151 /* Space efficient vector. These vectors can grow dynamically and are
1152 allocated together with their control data. They are suited to be
1153 included in data structures. Prior to initial allocation, they
1154 only take a single word of storage.
1156 These vectors are implemented as a pointer to an embeddable vector.
1157 The semantics allow for this pointer to be NULL to represent empty
1158 vectors. This way, empty vectors occupy minimal space in the
1159 structure containing them.
1163 - The whole vector and control data are allocated in a single
1165 - The whole vector may be re-allocated.
1166 - Vector data may grow and shrink.
1167 - Access and manipulation requires a pointer test and
1169 - It requires 1 word of storage (prior to vector allocation).
1174 These vectors must be PODs because they are stored in unions.
1175 (http://en.wikipedia.org/wiki/Plain_old_data_structures).
1176 As long as we use C++03, we cannot have constructors nor
1177 destructors in classes that are stored in unions. */
1179 template<typename T
>
1180 struct vec
<T
, va_heap
, vl_ptr
>
1183 /* Memory allocation and deallocation for the embedded vector.
1184 Needed because we cannot have proper ctors/dtors defined. */
1185 void create (unsigned nelems CXX_MEM_STAT_INFO
);
1186 void release (void);
1188 /* Vector operations. */
1189 bool exists (void) const
1190 { return m_vec
!= NULL
; }
1192 bool is_empty (void) const
1193 { return m_vec
? m_vec
->is_empty () : true; }
1195 unsigned length (void) const
1196 { return m_vec
? m_vec
->length () : 0; }
1199 { return m_vec
? m_vec
->m_vecdata
: NULL
; }
1201 const T
*address (void) const
1202 { return m_vec
? m_vec
->m_vecdata
: NULL
; }
1204 T
*begin () { return address (); }
1205 const T
*begin () const { return address (); }
1206 T
*end () { return begin () + length (); }
1207 const T
*end () const { return begin () + length (); }
1208 const T
&operator[] (unsigned ix
) const
1209 { return (*m_vec
)[ix
]; }
1211 bool operator!=(const vec
&other
) const
1212 { return !(*this == other
); }
1214 bool operator==(const vec
&other
) const
1215 { return address () == other
.address (); }
1217 T
&operator[] (unsigned ix
)
1218 { return (*m_vec
)[ix
]; }
1221 { return m_vec
->last (); }
1223 bool space (int nelems
) const
1224 { return m_vec
? m_vec
->space (nelems
) : nelems
== 0; }
1226 bool iterate (unsigned ix
, T
*p
) const;
1227 bool iterate (unsigned ix
, T
**p
) const;
1228 vec
copy (ALONE_CXX_MEM_STAT_INFO
) const;
1229 bool reserve (unsigned, bool = false CXX_MEM_STAT_INFO
);
1230 bool reserve_exact (unsigned CXX_MEM_STAT_INFO
);
1231 void splice (const vec
&);
1232 void safe_splice (const vec
& CXX_MEM_STAT_INFO
);
1233 T
*quick_push (const T
&);
1234 T
*safe_push (const T
&CXX_MEM_STAT_INFO
);
1236 void truncate (unsigned);
1237 void safe_grow (unsigned CXX_MEM_STAT_INFO
);
1238 void safe_grow_cleared (unsigned CXX_MEM_STAT_INFO
);
1239 void quick_grow (unsigned);
1240 void quick_grow_cleared (unsigned);
1241 void quick_insert (unsigned, const T
&);
1242 void safe_insert (unsigned, const T
& CXX_MEM_STAT_INFO
);
1243 void ordered_remove (unsigned);
1244 void unordered_remove (unsigned);
1245 void block_remove (unsigned, unsigned);
1246 void qsort (int (*) (const void *, const void *));
1247 T
*bsearch (const void *key
, int (*compar
)(const void *, const void *));
1248 unsigned lower_bound (T
, bool (*)(const T
&, const T
&)) const;
1249 bool contains (const T
&search
) const;
1251 bool using_auto_storage () const;
1253 /* FIXME - This field should be private, but we need to cater to
1254 compilers that have stricter notions of PODness for types. */
1255 vec
<T
, va_heap
, vl_embed
> *m_vec
;
1259 /* auto_vec is a subclass of vec that automatically manages creating and
1260 releasing the internal vector. If N is non zero then it has N elements of
1261 internal storage. The default is no internal storage, and you probably only
1262 want to ask for internal storage for vectors on the stack because if the
1263 size of the vector is larger than the internal storage that space is wasted.
1265 template<typename T
, size_t N
= 0>
1266 class auto_vec
: public vec
<T
, va_heap
>
1271 m_auto
.embedded_init (MAX (N
, 2), 0, 1);
1272 this->m_vec
= &m_auto
;
1281 vec
<T
, va_heap
, vl_embed
> m_auto
;
1282 T m_data
[MAX (N
- 1, 1)];
1285 /* auto_vec is a sub class of vec whose storage is released when it is
1287 template<typename T
>
1288 class auto_vec
<T
, 0> : public vec
<T
, va_heap
>
1291 auto_vec () { this->m_vec
= NULL
; }
1292 auto_vec (size_t n
) { this->create (n
); }
1293 ~auto_vec () { this->release (); }
1297 /* Allocate heap memory for pointer V and create the internal vector
1298 with space for NELEMS elements. If NELEMS is 0, the internal
1299 vector is initialized to empty. */
1301 template<typename T
>
1303 vec_alloc (vec
<T
> *&v
, unsigned nelems CXX_MEM_STAT_INFO
)
1306 v
->create (nelems PASS_MEM_STAT
);
1310 /* Conditionally allocate heap memory for VEC and its internal vector. */
1312 template<typename T
>
1314 vec_check_alloc (vec
<T
, va_heap
> *&vec
, unsigned nelems CXX_MEM_STAT_INFO
)
1317 vec_alloc (vec
, nelems PASS_MEM_STAT
);
1321 /* Free the heap memory allocated by vector V and set it to NULL. */
1323 template<typename T
>
1325 vec_free (vec
<T
> *&v
)
1336 /* Return iteration condition and update PTR to point to the IX'th
1337 element of this vector. Use this to iterate over the elements of a
1340 for (ix = 0; v.iterate (ix, &ptr); ix++)
1343 template<typename T
>
1345 vec
<T
, va_heap
, vl_ptr
>::iterate (unsigned ix
, T
*ptr
) const
1348 return m_vec
->iterate (ix
, ptr
);
1357 /* Return iteration condition and update *PTR to point to the
1358 IX'th element of this vector. Use this to iterate over the
1359 elements of a vector as follows,
1361 for (ix = 0; v->iterate (ix, &ptr); ix++)
1364 This variant is for vectors of objects. */
1366 template<typename T
>
1368 vec
<T
, va_heap
, vl_ptr
>::iterate (unsigned ix
, T
**ptr
) const
1371 return m_vec
->iterate (ix
, ptr
);
1380 /* Convenience macro for forward iteration. */
1381 #define FOR_EACH_VEC_ELT(V, I, P) \
1382 for (I = 0; (V).iterate ((I), &(P)); ++(I))
1384 #define FOR_EACH_VEC_SAFE_ELT(V, I, P) \
1385 for (I = 0; vec_safe_iterate ((V), (I), &(P)); ++(I))
1387 /* Likewise, but start from FROM rather than 0. */
1388 #define FOR_EACH_VEC_ELT_FROM(V, I, P, FROM) \
1389 for (I = (FROM); (V).iterate ((I), &(P)); ++(I))
1391 /* Convenience macro for reverse iteration. */
1392 #define FOR_EACH_VEC_ELT_REVERSE(V, I, P) \
1393 for (I = (V).length () - 1; \
1394 (V).iterate ((I), &(P)); \
1397 #define FOR_EACH_VEC_SAFE_ELT_REVERSE(V, I, P) \
1398 for (I = vec_safe_length (V) - 1; \
1399 vec_safe_iterate ((V), (I), &(P)); \
1403 /* Return a copy of this vector. */
1405 template<typename T
>
1406 inline vec
<T
, va_heap
, vl_ptr
>
1407 vec
<T
, va_heap
, vl_ptr
>::copy (ALONE_MEM_STAT_DECL
) const
1409 vec
<T
, va_heap
, vl_ptr
> new_vec
= vNULL
;
1411 new_vec
.m_vec
= m_vec
->copy ();
1416 /* Ensure that the vector has at least RESERVE slots available (if
1417 EXACT is false), or exactly RESERVE slots available (if EXACT is
1420 This may create additional headroom if EXACT is false.
1422 Note that this can cause the embedded vector to be reallocated.
1423 Returns true iff reallocation actually occurred. */
1425 template<typename T
>
1427 vec
<T
, va_heap
, vl_ptr
>::reserve (unsigned nelems
, bool exact MEM_STAT_DECL
)
1432 /* For now play a game with va_heap::reserve to hide our auto storage if any,
1433 this is necessary because it doesn't have enough information to know the
1434 embedded vector is in auto storage, and so should not be freed. */
1435 vec
<T
, va_heap
, vl_embed
> *oldvec
= m_vec
;
1436 unsigned int oldsize
= 0;
1437 bool handle_auto_vec
= m_vec
&& using_auto_storage ();
1438 if (handle_auto_vec
)
1441 oldsize
= oldvec
->length ();
1445 va_heap::reserve (m_vec
, nelems
, exact PASS_MEM_STAT
);
1446 if (handle_auto_vec
)
1448 memcpy (m_vec
->address (), oldvec
->address (), sizeof (T
) * oldsize
);
1449 m_vec
->m_vecpfx
.m_num
= oldsize
;
1456 /* Ensure that this vector has exactly NELEMS slots available. This
1457 will not create additional headroom. Note this can cause the
1458 embedded vector to be reallocated. Returns true iff reallocation
1459 actually occurred. */
1461 template<typename T
>
1463 vec
<T
, va_heap
, vl_ptr
>::reserve_exact (unsigned nelems MEM_STAT_DECL
)
1465 return reserve (nelems
, true PASS_MEM_STAT
);
1469 /* Create the internal vector and reserve NELEMS for it. This is
1470 exactly like vec::reserve, but the internal vector is
1471 unconditionally allocated from scratch. The old one, if it
1472 existed, is lost. */
1474 template<typename T
>
1476 vec
<T
, va_heap
, vl_ptr
>::create (unsigned nelems MEM_STAT_DECL
)
1480 reserve_exact (nelems PASS_MEM_STAT
);
1484 /* Free the memory occupied by the embedded vector. */
1486 template<typename T
>
1488 vec
<T
, va_heap
, vl_ptr
>::release (void)
1493 if (using_auto_storage ())
1495 m_vec
->m_vecpfx
.m_num
= 0;
1499 va_heap::release (m_vec
);
1502 /* Copy the elements from SRC to the end of this vector as if by memcpy.
1503 SRC and this vector must be allocated with the same memory
1504 allocation mechanism. This vector is assumed to have sufficient
1505 headroom available. */
1507 template<typename T
>
1509 vec
<T
, va_heap
, vl_ptr
>::splice (const vec
<T
, va_heap
, vl_ptr
> &src
)
1512 m_vec
->splice (*(src
.m_vec
));
1516 /* Copy the elements in SRC to the end of this vector as if by memcpy.
1517 SRC and this vector must be allocated with the same mechanism.
1518 If there is not enough headroom in this vector, it will be reallocated
1521 template<typename T
>
1523 vec
<T
, va_heap
, vl_ptr
>::safe_splice (const vec
<T
, va_heap
, vl_ptr
> &src
1528 reserve_exact (src
.length ());
1534 /* Push OBJ (a new element) onto the end of the vector. There must be
1535 sufficient space in the vector. Return a pointer to the slot
1536 where OBJ was inserted. */
1538 template<typename T
>
1540 vec
<T
, va_heap
, vl_ptr
>::quick_push (const T
&obj
)
1542 return m_vec
->quick_push (obj
);
1546 /* Push a new element OBJ onto the end of this vector. Reallocates
1547 the embedded vector, if needed. Return a pointer to the slot where
1548 OBJ was inserted. */
1550 template<typename T
>
1552 vec
<T
, va_heap
, vl_ptr
>::safe_push (const T
&obj MEM_STAT_DECL
)
1554 reserve (1, false PASS_MEM_STAT
);
1555 return quick_push (obj
);
1559 /* Pop and return the last element off the end of the vector. */
1561 template<typename T
>
1563 vec
<T
, va_heap
, vl_ptr
>::pop (void)
1565 return m_vec
->pop ();
1569 /* Set the length of the vector to LEN. The new length must be less
1570 than or equal to the current length. This is an O(1) operation. */
1572 template<typename T
>
1574 vec
<T
, va_heap
, vl_ptr
>::truncate (unsigned size
)
1577 m_vec
->truncate (size
);
1579 gcc_checking_assert (size
== 0);
1583 /* Grow the vector to a specific length. LEN must be as long or
1584 longer than the current length. The new elements are
1585 uninitialized. Reallocate the internal vector, if needed. */
1587 template<typename T
>
1589 vec
<T
, va_heap
, vl_ptr
>::safe_grow (unsigned len MEM_STAT_DECL
)
1591 unsigned oldlen
= length ();
1592 gcc_checking_assert (oldlen
<= len
);
1593 reserve_exact (len
- oldlen PASS_MEM_STAT
);
1595 m_vec
->quick_grow (len
);
1597 gcc_checking_assert (len
== 0);
1601 /* Grow the embedded vector to a specific length. LEN must be as
1602 long or longer than the current length. The new elements are
1603 initialized to zero. Reallocate the internal vector, if needed. */
1605 template<typename T
>
1607 vec
<T
, va_heap
, vl_ptr
>::safe_grow_cleared (unsigned len MEM_STAT_DECL
)
1609 unsigned oldlen
= length ();
1610 size_t sz
= sizeof (T
) * (len
- oldlen
);
1611 safe_grow (len PASS_MEM_STAT
);
1613 memset (&(address ()[oldlen
]), 0, sz
);
1617 /* Same as vec::safe_grow but without reallocation of the internal vector.
1618 If the vector cannot be extended, a runtime assertion will be triggered. */
1620 template<typename T
>
1622 vec
<T
, va_heap
, vl_ptr
>::quick_grow (unsigned len
)
1624 gcc_checking_assert (m_vec
);
1625 m_vec
->quick_grow (len
);
1629 /* Same as vec::quick_grow_cleared but without reallocation of the
1630 internal vector. If the vector cannot be extended, a runtime
1631 assertion will be triggered. */
1633 template<typename T
>
1635 vec
<T
, va_heap
, vl_ptr
>::quick_grow_cleared (unsigned len
)
1637 gcc_checking_assert (m_vec
);
1638 m_vec
->quick_grow_cleared (len
);
1642 /* Insert an element, OBJ, at the IXth position of this vector. There
1643 must be sufficient space. */
1645 template<typename T
>
1647 vec
<T
, va_heap
, vl_ptr
>::quick_insert (unsigned ix
, const T
&obj
)
1649 m_vec
->quick_insert (ix
, obj
);
1653 /* Insert an element, OBJ, at the IXth position of the vector.
1654 Reallocate the embedded vector, if necessary. */
1656 template<typename T
>
1658 vec
<T
, va_heap
, vl_ptr
>::safe_insert (unsigned ix
, const T
&obj MEM_STAT_DECL
)
1660 reserve (1, false PASS_MEM_STAT
);
1661 quick_insert (ix
, obj
);
1665 /* Remove an element from the IXth position of this vector. Ordering of
1666 remaining elements is preserved. This is an O(N) operation due to
1669 template<typename T
>
1671 vec
<T
, va_heap
, vl_ptr
>::ordered_remove (unsigned ix
)
1673 m_vec
->ordered_remove (ix
);
1677 /* Remove an element from the IXth position of this vector. Ordering
1678 of remaining elements is destroyed. This is an O(1) operation. */
1680 template<typename T
>
1682 vec
<T
, va_heap
, vl_ptr
>::unordered_remove (unsigned ix
)
1684 m_vec
->unordered_remove (ix
);
1688 /* Remove LEN elements starting at the IXth. Ordering is retained.
1689 This is an O(N) operation due to memmove. */
1691 template<typename T
>
1693 vec
<T
, va_heap
, vl_ptr
>::block_remove (unsigned ix
, unsigned len
)
1695 m_vec
->block_remove (ix
, len
);
1699 /* Sort the contents of this vector with qsort. CMP is the comparison
1700 function to pass to qsort. */
1702 template<typename T
>
1704 vec
<T
, va_heap
, vl_ptr
>::qsort (int (*cmp
) (const void *, const void *))
1711 /* Search the contents of the sorted vector with a binary search.
1712 CMP is the comparison function to pass to bsearch. */
1714 template<typename T
>
1716 vec
<T
, va_heap
, vl_ptr
>::bsearch (const void *key
,
1717 int (*cmp
) (const void *, const void *))
1720 return m_vec
->bsearch (key
, cmp
);
1725 /* Find and return the first position in which OBJ could be inserted
1726 without changing the ordering of this vector. LESSTHAN is a
1727 function that returns true if the first argument is strictly less
1730 template<typename T
>
1732 vec
<T
, va_heap
, vl_ptr
>::lower_bound (T obj
,
1733 bool (*lessthan
)(const T
&, const T
&))
1736 return m_vec
? m_vec
->lower_bound (obj
, lessthan
) : 0;
1739 /* Return true if SEARCH is an element of V. Note that this is O(N) in the
1740 size of the vector and so should be used with care. */
1742 template<typename T
>
1744 vec
<T
, va_heap
, vl_ptr
>::contains (const T
&search
) const
1746 return m_vec
? m_vec
->contains (search
) : false;
1749 template<typename T
>
1751 vec
<T
, va_heap
, vl_ptr
>::using_auto_storage () const
1753 return m_vec
->m_vecpfx
.m_using_auto_storage
;
1756 /* Release VEC and call release of all element vectors. */
1758 template<typename T
>
1760 release_vec_vec (vec
<vec
<T
> > &vec
)
1762 for (unsigned i
= 0; i
< vec
.length (); i
++)
1768 #if (GCC_VERSION >= 3000)
1769 # pragma GCC poison m_vec m_vecpfx m_vecdata