From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1666) id 5E71139A242B; Fri, 11 Jun 2021 07:29:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5E71139A242B MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Richard Biener To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-1376] Expose stable sort algorithm to gcc_sort_r and add vec::stablesort X-Act-Checkin: gcc X-Git-Author: Richard Biener X-Git-Refname: refs/heads/master X-Git-Oldrev: 26dbe85a3781af913639b17bc966f4a0b8209f3b X-Git-Newrev: 367f52dcc24045b072aeb26bc301a2980b39241f Message-Id: <20210611072955.5E71139A242B@sourceware.org> Date: Fri, 11 Jun 2021 07:29:55 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 11 Jun 2021 07:29:55 -0000 https://gcc.gnu.org/g:367f52dcc24045b072aeb26bc301a2980b39241f commit r12-1376-g367f52dcc24045b072aeb26bc301a2980b39241f Author: Richard Biener Date: Thu Jun 10 11:03:55 2021 +0200 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 * system.h (gcc_stablesort_r): Declare. * sort.cc (gcc_sort_r): Support stable sort. (gcc_stablesort_r): Define. * vec.h (vec<>::stablesort): Add. Diff: --- gcc/sort.cc | 14 +++++++++++++- gcc/system.h | 1 + gcc/vec.h | 24 ++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/gcc/sort.cc b/gcc/sort.cc index fe499b5ec73..1c83c62008d 100644 --- a/gcc/sort.cc +++ b/gcc/sort.cc @@ -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); +} diff --git a/gcc/system.h b/gcc/system.h index 3c856266cc2..adde3e264b6 100644 --- a/gcc/system.h +++ b/gcc/system.h @@ -1250,6 +1250,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 diff --git a/gcc/vec.h b/gcc/vec.h index 24df2db0eeb..193377cb69c 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::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 +inline void +vec::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::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 +inline void +vec::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. */