]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Expose stable sort algorithm to gcc_sort_r and add vec::stablesort
authorRichard Biener <rguenther@suse.de>
Thu, 10 Jun 2021 09:03:55 +0000 (11:03 +0200)
committerKwok Cheung Yeung <kcy@codesourcery.com>
Tue, 25 Jan 2022 20:01:07 +0000 (12:01 -0800)
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.

(cherry-picked from commit 367f52dcc24045b072aeb26bc301a2980b39241f)

gcc/ChangeLog.omp
gcc/sort.cc
gcc/system.h
gcc/vec.h

index 568ffb839cfa19a362541409ac57bd2855860b67..07f31cffc35e6ed994c7bff2c3204e7486e3081d 100644 (file)
@@ -1,3 +1,13 @@
+2022-01-25  Kwok Cheung Yeung  <kcy@codesourcery.com>
+
+       Backport from master:
+       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.
+
 2022-01-19  Marcel Vollweiler  <marcel@codesourcery.com>
 
        Backport from master:
index fe499b5ec73c1616e2a7969cf63f4cae3cb1823a..1c83c62008dbf7a70d714b7b0df450f2953a9874 100644 (file)
@@ -277,8 +277,12 @@ gcc_sort_r (void *vbase, size_t n, size_t size, sort_r_cmp_fn *cmp, void *data)
 {
   if (n < 2)
     return;
+  size_t nlim = 5;
+  bool stable = (ssize_t) size < 0;
+  if (stable)
+    nlim = 3, size = ~size;
   char *base = (char *)vbase;
-  sort_r_ctx c = {data, cmp, base, n, size, 5};
+  sort_r_ctx c = {data, cmp, base, n, size, nlim};
   long long scratch[32];
   size_t bufsz = (n / 2) * size;
   void *buf = bufsz <= sizeof scratch ? scratch : xmalloc (bufsz);
@@ -296,3 +300,11 @@ gcc_stablesort (void *vbase, size_t n, size_t size, cmp_fn *cmp)
 {
   gcc_qsort (vbase, n, ~size, cmp);
 }
+
+/* Stable sort, signature-compatible to Glibc qsort_r.  */
+void
+gcc_stablesort_r (void *vbase, size_t n, size_t size, sort_r_cmp_fn *cmp,
+                 void *data)
+{
+  gcc_sort_r (vbase, n, ~size, cmp, data);
+}
index b13e9429577ac968f60579bb736d80c75a8f0b1c..ce3bcbba75373a1a1985436a695183ffa33dc836 100644 (file)
@@ -1245,6 +1245,7 @@ void gcc_sort_r (void *, size_t, size_t, sort_r_cmp_fn *, void *);
 void gcc_qsort (void *, size_t, size_t, int (*)(const void *, const void *));
 void gcc_stablesort (void *, size_t, size_t,
                     int (*)(const void *, const void *));
+void gcc_stablesort_r (void *, size_t, size_t, sort_r_cmp_fn *, void *data);
 /* Redirect four-argument qsort calls to gcc_qsort; one-argument invocations
    correspond to vec::qsort, and use C qsort internally.  */
 #define PP_5th(a1, a2, a3, a4, a5, ...) a5
index 24df2db0eebab139fa61de8af130b7669c93aafd..193377cb69c287c18bf8133c846c9a3bd8b31279 100644 (file)
--- 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.  */