aboutsummaryrefslogtreecommitdiff
path: root/gcc/vec.h
diff options
context:
space:
mode:
authorRichard Biener <rguenther@suse.de>2021-06-10 11:03:55 +0200
committerRichard Biener <rguenther@suse.de>2021-06-11 09:29:35 +0200
commit367f52dcc24045b072aeb26bc301a2980b39241f (patch)
treee4af068ad4db4d7219b221bd0176c49776b9a4ff /gcc/vec.h
parent26dbe85a3781af913639b17bc966f4a0b8209f3b (diff)
downloadgcc-367f52dcc24045b072aeb26bc301a2980b39241f.zip
gcc-367f52dcc24045b072aeb26bc301a2980b39241f.tar.gz
gcc-367f52dcc24045b072aeb26bc301a2980b39241f.tar.bz2
Expose stable sort algorithm to gcc_sort_r and add vec::stablesort
This makes it possible to apply GCCs stable sort algorithm to vec<> and also use it with the qsort_r compatible interface. 2021-06-10 Richard Biener <rguenther@suse.de> * system.h (gcc_stablesort_r): Declare. * sort.cc (gcc_sort_r): Support stable sort. (gcc_stablesort_r): Define. * vec.h (vec<>::stablesort): Add.
Diffstat (limited to 'gcc/vec.h')
-rw-r--r--gcc/vec.h24
1 files changed, 24 insertions, 0 deletions
diff --git a/gcc/vec.h b/gcc/vec.h
index 24df2db..193377c 100644
--- a/gcc/vec.h
+++ b/gcc/vec.h
@@ -612,6 +612,7 @@ public:
void block_remove (unsigned, unsigned);
void qsort (int (*) (const void *, const void *));
void sort (int (*) (const void *, const void *, void *), void *);
+ void stablesort (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 *);
@@ -1160,6 +1161,17 @@ vec<T, A, vl_embed>::sort (int (*cmp) (const void *, const void *, void *),
gcc_sort_r (address (), length (), sizeof (T), cmp, data);
}
+/* Sort the contents of this vector with gcc_stablesort_r. CMP is the
+ comparison function to pass to qsort. */
+
+template<typename T, typename A>
+inline void
+vec<T, A, vl_embed>::stablesort (int (*cmp) (const void *, const void *,
+ void *), void *data)
+{
+ if (length () > 1)
+ gcc_stablesort_r (address (), length (), sizeof (T), cmp, data);
+}
/* Search the contents of the sorted vector with a binary search.
CMP is the comparison function to pass to bsearch. */
@@ -1488,6 +1500,7 @@ public:
void block_remove (unsigned, unsigned);
void qsort (int (*) (const void *, const void *));
void sort (int (*) (const void *, const void *, void *), void *);
+ void stablesort (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 *);
@@ -2053,6 +2066,17 @@ vec<T, va_heap, vl_ptr>::sort (int (*cmp) (const void *, const void *,
m_vec->sort (cmp, data);
}
+/* Sort the contents of this vector with gcc_stablesort_r. CMP is the
+ comparison function to pass to qsort. */
+
+template<typename T>
+inline void
+vec<T, va_heap, vl_ptr>::stablesort (int (*cmp) (const void *, const void *,
+ void *), void *data)
+{
+ if (m_vec)
+ m_vec->stablesort (cmp, data);
+}
/* Search the contents of the sorted vector with a binary search.
CMP is the comparison function to pass to bsearch. */