diff options
author | Richard Biener <rguenther@suse.de> | 2019-08-02 09:31:34 +0000 |
---|---|---|
committer | Richard Biener <rguenth@gcc.gnu.org> | 2019-08-02 09:31:34 +0000 |
commit | 8c2289931106421819cb14ee25976ab8b1c06ff1 (patch) | |
tree | 22adc8d5c7ec0ac2286218d43def72455da6055d /gcc/vec.h | |
parent | e006ead5230560030c44856952967ca0cfea4db2 (diff) | |
download | gcc-8c2289931106421819cb14ee25976ab8b1c06ff1.zip gcc-8c2289931106421819cb14ee25976ab8b1c06ff1.tar.gz gcc-8c2289931106421819cb14ee25976ab8b1c06ff1.tar.bz2 |
vec.h (vec::sort): Add gcc_qsort_r support.
2019-08-02 Richard Biener <rguenther@suse.de>
* vec.h (vec::sort): Add gcc_qsort_r support.
(vec::bsearch): Add an overload with gcc_qsort_r style callbacks.
* tree-ssa-loop-im.c (sort_bbs_in_loop_postorder_cmp): Adjust
to gcc_qsort_r style callback.
(sort_locs_in_loop_postorder_cmp): Likewise.
(analyze_memory_references): Use gcc_sort_r interfaces.
(find_ref_loc_in_loop_cmp): Use new bsearch overload.
From-SVN: r274004
Diffstat (limited to 'gcc/vec.h')
-rw-r--r-- | gcc/vec.h | 81 |
1 files changed, 80 insertions, 1 deletions
@@ -593,7 +593,10 @@ public: void unordered_remove (unsigned); void block_remove (unsigned, unsigned); void qsort (int (*) (const void *, const void *)); + void sort (int (*) (const void *, const void *, void *), void *); T *bsearch (const void *key, int (*compar)(const void *, const void *)); + T *bsearch (const void *key, + int (*compar)(const void *, const void *, void *), void *); unsigned lower_bound (T, bool (*)(const T &, const T &)) const; bool contains (const T &search) const; static size_t embedded_size (unsigned); @@ -1111,7 +1114,19 @@ inline void vec<T, A, vl_embed>::qsort (int (*cmp) (const void *, const void *)) { if (length () > 1) - ::qsort (address (), length (), sizeof (T), cmp); + gcc_qsort (address (), length (), sizeof (T), cmp); +} + +/* Sort the contents of this vector with qsort. CMP is the comparison + function to pass to qsort. */ + +template<typename T, typename A> +inline void +vec<T, A, vl_embed>::sort (int (*cmp) (const void *, const void *, void *), + void *data) +{ + if (length () > 1) + gcc_sort_r (address (), length (), sizeof (T), cmp, data); } @@ -1149,6 +1164,41 @@ vec<T, A, vl_embed>::bsearch (const void *key, return NULL; } +/* Search the contents of the sorted vector with a binary search. + CMP is the comparison function to pass to bsearch. */ + +template<typename T, typename A> +inline T * +vec<T, A, vl_embed>::bsearch (const void *key, + int (*compar) (const void *, const void *, + void *), void *data) +{ + const void *base = this->address (); + size_t nmemb = this->length (); + size_t size = sizeof (T); + /* The following is a copy of glibc stdlib-bsearch.h. */ + size_t l, u, idx; + const void *p; + int comparison; + + l = 0; + u = nmemb; + while (l < u) + { + idx = (l + u) / 2; + p = (const void *) (((const char *) base) + (idx * size)); + comparison = (*compar) (key, p, data); + if (comparison < 0) + u = idx; + else if (comparison > 0) + l = idx + 1; + else + return (T *)const_cast<void *>(p); + } + + 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. */ @@ -1401,7 +1451,10 @@ public: void unordered_remove (unsigned); void block_remove (unsigned, unsigned); void qsort (int (*) (const void *, const void *)); + void sort (int (*) (const void *, const void *, void *), void *); T *bsearch (const void *key, int (*compar)(const void *, const void *)); + T *bsearch (const void *key, + int (*compar)(const void *, const void *, void *), void *); unsigned lower_bound (T, bool (*)(const T &, const T &)) const; bool contains (const T &search) const; void reverse (void); @@ -1898,6 +1951,18 @@ vec<T, va_heap, vl_ptr>::qsort (int (*cmp) (const void *, const void *)) m_vec->qsort (cmp); } +/* Sort the contents of this vector with qsort. CMP is the comparison + function to pass to qsort. */ + +template<typename T> +inline void +vec<T, va_heap, vl_ptr>::sort (int (*cmp) (const void *, const void *, + void *), void *data) +{ + if (m_vec) + m_vec->sort (cmp, data); +} + /* Search the contents of the sorted vector with a binary search. CMP is the comparison function to pass to bsearch. */ @@ -1912,6 +1977,20 @@ vec<T, va_heap, vl_ptr>::bsearch (const void *key, return NULL; } +/* Search the contents of the sorted vector with a binary search. + CMP is the comparison function to pass to bsearch. */ + +template<typename T> +inline T * +vec<T, va_heap, vl_ptr>::bsearch (const void *key, + int (*cmp) (const void *, const void *, + void *), void *data) +{ + if (m_vec) + return m_vec->bsearch (key, cmp, data); + return NULL; +} + /* Find and return the first position in which OBJ could be inserted without changing the ordering of this vector. LESSTHAN is a |