#ifndef GCC_VEC_H
#define GCC_VEC_H
+#include "statistics.h" /* For MEM_STAT_DECL. */
+
/* The macros here implement a set of templated vector types and
associated interfaces. These templates are implemented with
macros, as we're not in C++ land. The interface functions are
#define VEC_iterate(T,V,I,P) (VEC_OP(T,base,iterate)(VEC_BASE(V),I,&(P)))
+/* Convenience macro for forward iteration. */
+
+#define FOR_EACH_VEC_ELT(T, V, I, P) \
+ for (I = 0; VEC_iterate (T, (V), (I), (P)); ++(I))
+
+/* Convenience macro for reverse iteration. */
+
+#define FOR_EACH_VEC_ELT_REVERSE(T,V,I,P) \
+ for (I = VEC_length (T, (V)) - 1; \
+ VEC_iterate (T, (V), (I), (P)); \
+ (I)--)
+
/* Allocate new vector.
VEC(T,A) *VEC_T_A_alloc(int reserve);
#define VEC_reserve_exact(T,A,V,R) \
(VEC_OP(T,A,reserve_exact)(&(V),R VEC_CHECK_INFO MEM_STAT_INFO))
+/* Copy elements with no reallocation
+ void VEC_T_splice (VEC(T) *dst, VEC(T) *src); // Integer
+ void VEC_T_splice (VEC(T) *dst, VEC(T) *src); // Pointer
+ void VEC_T_splice (VEC(T) *dst, VEC(T) *src); // Object
+
+ Copy the elements in SRC to the end of DST as if by memcpy. DST and
+ SRC need not be allocated with the same mechanism, although they most
+ often will be. DST is assumed to have sufficient headroom
+ available. */
+
+#define VEC_splice(T,DST,SRC) \
+ (VEC_OP(T,base,splice)(VEC_BASE(DST), VEC_BASE(SRC) VEC_CHECK_INFO))
+
+/* Copy elements with reallocation
+ void VEC_T_safe_splice (VEC(T,A) *&dst, VEC(T) *src); // Integer
+ void VEC_T_safe_splice (VEC(T,A) *&dst, VEC(T) *src); // Pointer
+ void VEC_T_safe_splice (VEC(T,A) *&dst, VEC(T) *src); // Object
+
+ Copy the elements in SRC to the end of DST as if by memcpy. DST and
+ SRC need not be allocated with the same mechanism, although they most
+ often will be. DST need not have sufficient headroom and will be
+ reallocated if needed. */
+
+#define VEC_safe_splice(T,A,DST,SRC) \
+ (VEC_OP(T,A,safe_splice)(&(DST), VEC_BASE(SRC) VEC_CHECK_INFO MEM_STAT_INFO))
+
/* Push object with no reallocation
T *VEC_T_quick_push (VEC(T) *v, T obj); // Integer
T *VEC_T_quick_push (VEC(T) *v, T obj); // Pointer
void VEC_T_block_remove (VEC(T) *v, unsigned ix, unsigned len);
Remove LEN elements starting at the IXth. Ordering is retained.
- This is an O(1) operation. */
+ This is an O(N) operation due to memmove. */
#define VEC_block_remove(T,V,I,L) \
(VEC_OP(T,base,block_remove)(VEC_BASE(V),I,L VEC_CHECK_INFO))
#define VEC_address(T,V) (VEC_OP(T,base,address)(VEC_BASE(V)))
+/* Conveniently sort the contents of the vector with qsort.
+ void VEC_qsort (VEC(T) *v, int (*cmp_func)(const void *, const void *)) */
+
+#define VEC_qsort(T,V,CMP) qsort(VEC_address (T,V), VEC_length(T,V), \
+ sizeof (T), CMP)
+
/* Find the first index in the vector not less than the object.
unsigned VEC_T_lower_bound (VEC(T) *v, const T val,
bool (*lessthan) (const T, const T)); // Integer
#ifdef GATHER_STATISTICS
void vec_heap_free (void *);
#else
-#define vec_heap_free(V) free (V)
+/* Avoid problems with frontends that #define free(x). */
+#define vec_heap_free(V) (free) (V)
#endif
#if ENABLE_CHECKING
return vec_ ? vec_->alloc - vec_->num >= (unsigned)alloc_ : !alloc_; \
} \
\
+static inline void VEC_OP(T,base,splice) \
+ (VEC(T,base) *dst_, VEC(T,base) *src_ VEC_CHECK_DECL) \
+{ \
+ if (src_) \
+ { \
+ unsigned len_ = src_->num; \
+ VEC_ASSERT (dst_->num + len_ <= dst_->alloc, "splice", T, base); \
+ \
+ memcpy (&dst_->vec[dst_->num], &src_->vec[0], len_ * sizeof (T)); \
+ dst_->num += len_; \
+ } \
+} \
+ \
static inline T *VEC_OP (T,base,quick_push) \
(VEC(T,base) *vec_, T obj_ VEC_CHECK_DECL) \
{ \
sizeof (T) * (size_ - oldsize)); \
} \
\
+static inline void VEC_OP(T,A,safe_splice) \
+ (VEC(T,A) **dst_, VEC(T,base) *src_ VEC_CHECK_DECL MEM_STAT_DECL) \
+{ \
+ if (src_) \
+ { \
+ VEC_OP (T,A,reserve_exact) (dst_, src_->num \
+ VEC_CHECK_PASS MEM_STAT_INFO); \
+ \
+ VEC_OP (T,base,splice) (VEC_BASE (*dst_), src_ \
+ VEC_CHECK_PASS); \
+ } \
+} \
+ \
static inline T *VEC_OP (T,A,safe_push) \
(VEC(T,A) **vec_, T obj_ VEC_CHECK_DECL MEM_STAT_DECL) \
{ \
return vec_ ? vec_->alloc - vec_->num >= (unsigned)alloc_ : !alloc_; \
} \
\
+static inline void VEC_OP(T,base,splice) \
+ (VEC(T,base) *dst_, VEC(T,base) *src_ VEC_CHECK_DECL) \
+{ \
+ if (src_) \
+ { \
+ unsigned len_ = src_->num; \
+ VEC_ASSERT (dst_->num + len_ <= dst_->alloc, "splice", T, base); \
+ \
+ memcpy (&dst_->vec[dst_->num], &src_->vec[0], len_ * sizeof (T)); \
+ dst_->num += len_; \
+ } \
+} \
+ \
static inline T *VEC_OP (T,base,quick_push) \
(VEC(T,base) *vec_, const T *obj_ VEC_CHECK_DECL) \
{ \
sizeof (T) * (size_ - oldsize)); \
} \
\
+static inline void VEC_OP(T,A,safe_splice) \
+ (VEC(T,A) **dst_, VEC(T,base) *src_ VEC_CHECK_DECL MEM_STAT_DECL) \
+{ \
+ if (src_) \
+ { \
+ VEC_OP (T,A,reserve_exact) (dst_, src_->num \
+ VEC_CHECK_PASS MEM_STAT_INFO); \
+ \
+ VEC_OP (T,base,splice) (VEC_BASE (*dst_), src_ \
+ VEC_CHECK_PASS); \
+ } \
+} \
+ \
static inline T *VEC_OP (T,A,safe_push) \
(VEC(T,A) **vec_, const T *obj_ VEC_CHECK_DECL MEM_STAT_DECL) \
{ \
sizeof (T) * (size_ - oldsize)); \
} \
\
+static inline void VEC_OP(T,A,safe_splice) \
+ (VEC(T,A) **dst_, VEC(T,base) *src_ VEC_CHECK_DECL MEM_STAT_DECL) \
+{ \
+ if (src_) \
+ { \
+ VEC_OP (T,A,reserve_exact) (dst_, src_->num \
+ VEC_CHECK_PASS MEM_STAT_INFO); \
+ \
+ VEC_OP (T,base,splice) (VEC_BASE (*dst_), src_ \
+ VEC_CHECK_PASS); \
+ } \
+} \
+ \
static inline T *VEC_OP (T,A,safe_push) \
(VEC(T,A) **vec_, const T obj_ VEC_CHECK_DECL MEM_STAT_DECL) \
{ \