From: Trevor Saunders Date: Sun, 24 Apr 2016 07:47:38 +0000 (+0000) Subject: add some utility methods to vec X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=12e109d14b37685b6c6067fb94f1887411b130d0;p=gcc.git add some utility methods to vec gcc/ChangeLog: 2016-04-24 Trevor Saunders * vec.h (vec_safe_contains): New function. (vec::contains): Likewise. (vec::begin): Likewise. (vec::end): Likewise. From-SVN: r235392 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index d2e15556206..a820a0f9f7b 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,10 @@ +2016-04-24 Trevor Saunders + + * vec.h (vec_safe_contains): New function. + (vec::contains): Likewise. + (vec::begin): Likewise. + (vec::end): Likewise. + 2016-04-23 Jakub Jelinek PR sanitizer/70712 diff --git a/gcc/vec.h b/gcc/vec.h index ff575287894..eb8c27024f4 100644 --- a/gcc/vec.h +++ b/gcc/vec.h @@ -454,6 +454,10 @@ public: bool is_empty (void) const { return m_vecpfx.m_num == 0; } T *address (void) { return m_vecdata; } const T *address (void) const { return m_vecdata; } + T *begin () { return address (); } + const T *begin () const { return address (); } + T *end () { return address () + length (); } + const T *end () const { return address () + length (); } const T &operator[] (unsigned) const; T &operator[] (unsigned); T &last (void); @@ -473,6 +477,7 @@ public: void qsort (int (*) (const void *, const void *)); T *bsearch (const void *key, int (*compar)(const void *, const void *)); unsigned lower_bound (T, bool (*)(const T &, const T &)) const; + bool contains (const T &search) const; static size_t embedded_size (unsigned); void embedded_init (unsigned, unsigned = 0, unsigned = 0); void quick_grow (unsigned len); @@ -542,7 +547,6 @@ vec_safe_is_empty (vec *v) return v ? v->is_empty () : true; } - /* If V does not have space for NELEMS elements, call V->reserve(NELEMS, EXACT). */ template @@ -695,6 +699,15 @@ vec_safe_splice (vec *&dst, const vec *src } } +/* Return true if SEARCH is an element of V. Note that this is O(N) in the + size of the vector and so should be used with care. */ + +template +inline bool +vec_safe_contains (vec *v, const T &search) +{ + return v ? v->contains (search) : false; +} /* Index into vector. Return the IX'th element. IX must be in the domain of the vector. */ @@ -973,6 +986,20 @@ vec::bsearch (const void *key, return NULL; } +/* Return true if SEARCH is an element of V. Note that this is O(N) in the + size of the vector and so should be used with care. */ + +template +inline bool +vec::contains (const T &search) const +{ + unsigned int len = length (); + for (unsigned int i = 0; i < len; i++) + if ((*this)[i] == search) + return true; + + return false; +} /* Find and return the first position in which OBJ could be inserted without changing the ordering of this vector. LESSTHAN is a @@ -1167,6 +1194,10 @@ public: const T *address (void) const { return m_vec ? m_vec->m_vecdata : NULL; } + T *begin () { return address (); } + const T *begin () const { return address (); } + T *end () { return begin () + length (); } + const T *end () const { return begin () + length (); } const T &operator[] (unsigned ix) const { return (*m_vec)[ix]; } @@ -1208,6 +1239,7 @@ public: void qsort (int (*) (const void *, const void *)); T *bsearch (const void *key, int (*compar)(const void *, const void *)); unsigned lower_bound (T, bool (*)(const T &, const T &)) const; + bool contains (const T &search) const; bool using_auto_storage () const; @@ -1695,6 +1727,16 @@ vec::lower_bound (T obj, return m_vec ? m_vec->lower_bound (obj, lessthan) : 0; } +/* Return true if SEARCH is an element of V. Note that this is O(N) in the + size of the vector and so should be used with care. */ + +template +inline bool +vec::contains (const T &search) const +{ + return m_vec ? m_vec->contains (search) : false; +} + template inline bool vec::using_auto_storage () const