2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 * Imported into CLIB by Eliot Dresselhaus from:
18 * This file is part of
19 * MakeIndex - A formatter and format independent index processor
21 * This file is public domain software donated by
22 * Nelson Beebe (beebe@science.utah.edu).
24 * modifications copyright (c) 2003 Cisco Systems, Inc.
27 #include <vppinfra/clib.h>
30 * qsort.c: Our own version of the system qsort routine which is faster by an
31 * average of 25%, with lows and highs of 10% and 50%. The THRESHold below is
32 * the insertion sort threshold, and has been adjusted for records of size 48
33 * bytes. The MTHREShold is where we stop finding a better median.
36 #define THRESH 4 /* threshold for insertion */
37 #define MTHRESH 6 /* threshold for median */
41 word qsz; /* size of each record */
42 word thresh; /* THRESHold in chars */
43 word mthresh; /* MTHRESHold in chars */
44 int (*qcmp) (const void *, const void *); /* the comparison routine */
47 static void qst (qst_t * q, char *base, char *max);
50 * qqsort: First, set up some global parameters for qst to share.
51 * Then, quicksort with qst(), and then a cleanup insertion sort ourselves.
52 * Sound simple? It's not...
56 qsort (void *base, uword n, uword size,
57 int (*compar) (const void *, const void *))
73 q->thresh = q->qsz * THRESH;
74 q->mthresh = q->qsz * MTHRESH;
75 max = base + n * q->qsz;
79 hi = base + q->thresh;
86 * First put smallest element, which must be in the first THRESH, in the
87 * first position as a sentinel. This is done just by searching the
88 * first THRESH elements (or the first n if n < THRESH), finding the min,
89 * and swapping it into the first position.
91 for (j = lo = base; (lo += q->qsz) < hi;)
93 if ((*compar) (j, lo) > 0)
97 { /* swap j into place */
98 for (i = base, hi = base + q->qsz; i < hi;)
106 * With our sentinel in place, we now run the following hyper-fast
107 * insertion sort. For each remaining element, min, from [1] to [n-1],
108 * set hi to the index of the element AFTER which this one goes. Then, do
109 * the standard insertion sort shift on a character at a time basis for
110 * each element in the frob.
112 for (min = base; (hi = min += q->qsz) < max;)
114 while ((*q->qcmp) (hi -= q->qsz, min) > 0);
115 if ((hi += q->qsz) != min)
117 for (lo = min + q->qsz; --lo >= min;)
120 for (i = j = lo; (j -= q->qsz) >= hi; i = j)
131 * qst: Do a quicksort. First, find the median element, and put that one in
132 * the first place as the discriminator. (This "median" is just the median
133 * of the first, last and middle elements). (Using this median instead of
134 * the first element is a big win). Then, the usual partitioning/swapping,
135 * followed by moving the discriminator into the right place. Then, figure
136 * out the sizes of the two partions, do the smaller one recursively and the
137 * larger one via a repeat of this code. Stopping when there are less than
138 * THRESH elements in a partition and cleaning up with an insertion sort (in
139 * our caller) is a huge win. All data swaps are done in-line, which is
140 * space-losing but time-saving. (And there are only three places where this
145 qst (qst_t * q, char *base, char *max)
158 lo = (int) (max - base); /* number of elements as chars */
162 * At the top here, lo is the number of characters of elements in the
163 * current partition. (Which should be max - base). Find the median
164 * of the first, last, and middle element and make that the middle
165 * element. Set j to largest of first and middle. If max is larger
166 * than that guy, then it's that guy, else compare max with loser of
167 * first and take larger. Things are set up to prefer the middle,
168 * then the first in case of ties.
170 mid = i = base + qsz * ((unsigned) (lo / qsz) >> 1);
171 if (lo >= q->mthresh)
173 j = ((*q->qcmp) ((jj = base), i) > 0 ? jj : i);
174 if ((*q->qcmp) (j, (tmp = max - qsz)) > 0)
176 /* switch to first loser */
177 j = (j == jj ? i : jj);
178 if ((*q->qcmp) (j, tmp) < 0)
193 /* Semi-standard quicksort partitioning/swapping */
194 for (i = base, j = max - qsz;;)
196 while (i < mid && (*q->qcmp) (i, mid) <= 0)
200 if ((*q->qcmp) (mid, j) <= 0)
205 tmp = i + qsz; /* value of i after swap */
207 { /* j <-> mid, new mid is j */
222 { /* i <-> mid, new mid is i */
224 tmp = mid = i; /* value of i after swap */
239 * Look at sizes of the two partitions, do the smaller one first by
240 * recursion, then do the larger one by making sure lo is its size,
241 * base and max are update correctly, and branching back. But only
242 * repeat (recursively or by branching) if the partition is of at
246 if ((lo = (int) (j - base)) <= (hi = (int) (max - i)))
260 while (lo >= q->thresh);
264 * fd.io coding-style-patch-verification: ON
267 * eval: (c-set-style "gnu")