Annotation of src/usr.bin/sort/radixsort.c, Revision 1.3
1.3 ! millert 1: /* $OpenBSD: radixsort.c,v 1.2 2015/03/17 17:49:27 millert Exp $ */
1.1 millert 2:
3: /*-
4: * Copyright (C) 2012 Oleg Moskalenko <mom040267@gmail.com>
5: * Copyright (C) 2012 Gabor Kovesdan <gabor@FreeBSD.org>
6: * All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: *
17: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27: * SUCH DAMAGE.
28: */
29:
30: #include <errno.h>
31: #include <err.h>
32: #include <langinfo.h>
33: #include <math.h>
34: #include <stdlib.h>
35: #include <string.h>
36: #include <wchar.h>
37: #include <wctype.h>
38: #include <unistd.h>
39:
40: #include "coll.h"
41: #include "radixsort.h"
42:
43: #define DEFAULT_SORT_FUNC_RADIXSORT mergesort
44:
45: #define TINY_NODE(sl) ((sl)->tosort_num < 65)
46: #define SMALL_NODE(sl) ((sl)->tosort_num < 5)
47:
48: /* are we sorting in reverse order ? */
49: static bool reverse_sort;
50:
51: /* sort sub-levels array size */
52: static const size_t slsz = 256 * sizeof(struct sort_level *);
53:
54: /* one sort level structure */
55: struct sort_level {
56: struct sort_level **sublevels;
57: struct sort_list_item **leaves;
58: struct sort_list_item **sorted;
59: struct sort_list_item **tosort;
60: size_t leaves_num;
61: size_t leaves_sz;
62: size_t level;
63: size_t real_sln;
64: size_t start_position;
65: size_t sln;
66: size_t tosort_num;
67: size_t tosort_sz;
68: };
69:
70: /* stack of sort levels ready to be sorted */
71: struct level_stack {
72: struct level_stack *next;
73: struct sort_level *sl;
74: };
75:
76: static struct level_stack *g_ls;
77:
78: /*
79: * Push sort level to the stack
80: */
81: static inline void
82: push_ls(struct sort_level* sl)
83: {
84: struct level_stack *new_ls;
85:
86: new_ls = sort_malloc(sizeof(struct level_stack));
87: new_ls->sl = sl;
88:
89: new_ls->next = g_ls;
90: g_ls = new_ls;
91: }
92:
93: /*
94: * Pop sort level from the stack (single-threaded style)
95: */
96: static inline struct sort_level *
97: pop_ls_st(void)
98: {
99: struct sort_level *sl;
100:
101: if (g_ls) {
102: struct level_stack *saved_ls;
103:
104: sl = g_ls->sl;
105: saved_ls = g_ls;
106: g_ls = g_ls->next;
107: sort_free(saved_ls);
108: } else
109: sl = NULL;
110:
111: return sl;
112: }
113:
114: static void
115: add_to_sublevel(struct sort_level *sl, struct sort_list_item *item, size_t indx)
116: {
117: struct sort_level *ssl;
118:
119: ssl = sl->sublevels[indx];
120:
121: if (ssl == NULL) {
1.2 millert 122: ssl = sort_calloc(1, sizeof(struct sort_level));
1.1 millert 123: ssl->level = sl->level + 1;
124: sl->sublevels[indx] = ssl;
125:
126: ++(sl->real_sln);
127: }
128:
129: if (++(ssl->tosort_num) > ssl->tosort_sz) {
130: ssl->tosort_sz = ssl->tosort_num + 128;
131: ssl->tosort = sort_reallocarray(ssl->tosort, ssl->tosort_sz,
132: sizeof(struct sort_list_item *));
133: }
134:
135: ssl->tosort[ssl->tosort_num - 1] = item;
136: }
137:
138: static inline void
139: add_leaf(struct sort_level *sl, struct sort_list_item *item)
140: {
141: if (++(sl->leaves_num) > sl->leaves_sz) {
142: sl->leaves_sz = sl->leaves_num + 128;
143: sl->leaves = sort_reallocarray(sl->leaves, sl->leaves_sz,
144: sizeof(struct sort_list_item *));
145: }
146: sl->leaves[sl->leaves_num - 1] = item;
147: }
148:
149: static inline int
150: get_wc_index(struct sort_list_item *sli, size_t level)
151: {
152: const struct bwstring *bws;
153:
154: bws = sli->ka.key[0].k;
155:
156: if ((BWSLEN(bws) > level))
157: return (unsigned char)BWS_GET(bws, level);
158: return -1;
159: }
160:
161: static void
162: place_item(struct sort_level *sl, size_t item)
163: {
164: struct sort_list_item *sli;
165: int c;
166:
167: sli = sl->tosort[item];
168: c = get_wc_index(sli, sl->level);
169:
170: if (c == -1)
171: add_leaf(sl, sli);
172: else
173: add_to_sublevel(sl, sli, c);
174: }
175:
176: static void
177: free_sort_level(struct sort_level *sl)
178: {
179: if (sl) {
180: if (sl->leaves)
181: sort_free(sl->leaves);
182:
183: if (sl->level > 0)
184: sort_free(sl->tosort);
185:
186: if (sl->sublevels) {
187: struct sort_level *slc;
188: size_t i, sln;
189:
190: sln = sl->sln;
191:
192: for (i = 0; i < sln; ++i) {
193: slc = sl->sublevels[i];
194: if (slc)
195: free_sort_level(slc);
196: }
197:
198: sort_free(sl->sublevels);
199: }
200:
201: sort_free(sl);
202: }
203: }
204:
205: static void
206: run_sort_level_next(struct sort_level *sl)
207: {
208: struct sort_level *slc;
209: size_t i, sln, tosort_num;
210:
211: if (sl->sublevels) {
212: sort_free(sl->sublevels);
213: sl->sublevels = NULL;
214: }
215:
216: switch (sl->tosort_num){
217: case 0:
218: goto end;
219: case 1:
220: sl->sorted[sl->start_position] = sl->tosort[0];
221: goto end;
222: case 2:
223: if (list_coll_offset(&(sl->tosort[0]), &(sl->tosort[1]),
224: sl->level) > 0) {
225: sl->sorted[sl->start_position++] = sl->tosort[1];
226: sl->sorted[sl->start_position] = sl->tosort[0];
227: } else {
228: sl->sorted[sl->start_position++] = sl->tosort[0];
229: sl->sorted[sl->start_position] = sl->tosort[1];
230: }
231:
232: goto end;
233: default:
234: if (TINY_NODE(sl) || (sl->level > 15)) {
235: listcoll_t func;
236:
237: func = get_list_call_func(sl->level);
238:
239: sl->leaves = sl->tosort;
240: sl->leaves_num = sl->tosort_num;
241: sl->leaves_sz = sl->leaves_num;
242: sl->leaves = sort_reallocarray(sl->leaves,
243: sl->leaves_sz, sizeof(struct sort_list_item *));
244: sl->tosort = NULL;
245: sl->tosort_num = 0;
246: sl->tosort_sz = 0;
247: sl->sln = 0;
248: sl->real_sln = 0;
249: if (sort_opts_vals.sflag) {
250: if (mergesort(sl->leaves, sl->leaves_num,
251: sizeof(struct sort_list_item *),
252: (int(*)(const void *, const void *)) func) == -1)
253: /* NOTREACHED */
254: err(2, "Radix sort error 3");
255: } else
256: DEFAULT_SORT_FUNC_RADIXSORT(sl->leaves, sl->leaves_num,
257: sizeof(struct sort_list_item *),
258: (int(*)(const void *, const void *)) func);
259:
260: memcpy(sl->sorted + sl->start_position,
261: sl->leaves, sl->leaves_num *
262: sizeof(struct sort_list_item *));
263:
264: goto end;
265: } else {
266: sl->tosort_sz = sl->tosort_num;
267: sl->tosort = sort_reallocarray(sl->tosort,
268: sl->tosort_sz, sizeof(struct sort_list_item *));
269: }
270: }
271:
272: sl->sln = 256;
1.2 millert 273: sl->sublevels = sort_calloc(1, slsz);
1.1 millert 274: sl->real_sln = 0;
275:
276: tosort_num = sl->tosort_num;
277: for (i = 0; i < tosort_num; ++i)
278: place_item(sl, i);
279:
280: sort_free(sl->tosort);
281: sl->tosort = NULL;
282: sl->tosort_num = 0;
283: sl->tosort_sz = 0;
284:
285: if (sl->leaves_num > 1) {
286: if (keys_num > 1) {
287: if (sort_opts_vals.sflag) {
288: mergesort(sl->leaves, sl->leaves_num,
289: sizeof(struct sort_list_item *), list_coll);
290: } else {
291: DEFAULT_SORT_FUNC_RADIXSORT(sl->leaves, sl->leaves_num,
292: sizeof(struct sort_list_item *), list_coll);
293: }
294: } else if (!sort_opts_vals.sflag && sort_opts_vals.complex_sort) {
295: DEFAULT_SORT_FUNC_RADIXSORT(sl->leaves, sl->leaves_num,
296: sizeof(struct sort_list_item *), list_coll);
297: }
298: }
299:
300: sl->leaves_sz = sl->leaves_num;
301: sl->leaves = sort_reallocarray(sl->leaves, sl->leaves_sz,
302: sizeof(struct sort_list_item *));
303:
304: if (!reverse_sort) {
305: memcpy(sl->sorted + sl->start_position, sl->leaves,
306: sl->leaves_num * sizeof(struct sort_list_item *));
307: sl->start_position += sl->leaves_num;
308:
309: sort_free(sl->leaves);
310: sl->leaves = NULL;
311: sl->leaves_num = 0;
312: sl->leaves_sz = 0;
313:
314: sln = sl->sln;
315:
316: for (i = 0; i < sln; ++i) {
317: slc = sl->sublevels[i];
318:
319: if (slc) {
320: slc->sorted = sl->sorted;
321: slc->start_position = sl->start_position;
322: sl->start_position += slc->tosort_num;
323: if (SMALL_NODE(slc))
324: run_sort_level_next(slc);
325: else
326: push_ls(slc);
327: sl->sublevels[i] = NULL;
328: }
329: }
330:
331: } else {
332: size_t n;
333:
334: sln = sl->sln;
335:
336: for (i = 0; i < sln; ++i) {
337: n = sln - i - 1;
338: slc = sl->sublevels[n];
339:
340: if (slc) {
341: slc->sorted = sl->sorted;
342: slc->start_position = sl->start_position;
343: sl->start_position += slc->tosort_num;
344: if (SMALL_NODE(slc))
345: run_sort_level_next(slc);
346: else
347: push_ls(slc);
348: sl->sublevels[n] = NULL;
349: }
350: }
351:
352: memcpy(sl->sorted + sl->start_position, sl->leaves,
353: sl->leaves_num * sizeof(struct sort_list_item *));
354: }
355:
356: end:
357: free_sort_level(sl);
358: }
359:
360: /*
361: * Single-threaded sort cycle
362: */
363: static void
364: run_sort_cycle_st(void)
365: {
366: struct sort_level *slc;
367:
368: for (;;) {
369: slc = pop_ls_st();
370: if (slc == NULL) {
371: break;
372: }
373: run_sort_level_next(slc);
374: }
375: }
376:
377: static void
378: run_top_sort_level(struct sort_level *sl)
379: {
380: struct sort_level *slc;
381: size_t i;
382:
383: reverse_sort = sort_opts_vals.kflag ? keys[0].sm.rflag :
384: default_sort_mods->rflag;
385:
386: sl->start_position = 0;
387: sl->sln = 256;
1.2 millert 388: sl->sublevels = sort_calloc(1, slsz);
1.1 millert 389:
390: for (i = 0; i < sl->tosort_num; ++i)
391: place_item(sl, i);
392:
393: if (sl->leaves_num > 1) {
394: if (keys_num > 1) {
395: if (sort_opts_vals.sflag) {
396: mergesort(sl->leaves, sl->leaves_num,
397: sizeof(struct sort_list_item *), list_coll);
398: } else {
399: DEFAULT_SORT_FUNC_RADIXSORT(sl->leaves, sl->leaves_num,
400: sizeof(struct sort_list_item *), list_coll);
401: }
402: } else if (!sort_opts_vals.sflag && sort_opts_vals.complex_sort) {
403: DEFAULT_SORT_FUNC_RADIXSORT(sl->leaves, sl->leaves_num,
404: sizeof(struct sort_list_item *), list_coll);
405: }
406: }
407:
408: if (!reverse_sort) {
409: size_t i;
410:
411: memcpy(sl->tosort + sl->start_position, sl->leaves,
412: sl->leaves_num * sizeof(struct sort_list_item *));
413: sl->start_position += sl->leaves_num;
414:
415: for (i = 0; i < sl->sln; ++i) {
416: slc = sl->sublevels[i];
417:
418: if (slc) {
419: slc->sorted = sl->tosort;
420: slc->start_position = sl->start_position;
421: sl->start_position += slc->tosort_num;
422: push_ls(slc);
423: sl->sublevels[i] = NULL;
424: }
425: }
426:
427: } else {
428: size_t i, n;
429:
430: for (i = 0; i < sl->sln; ++i) {
431:
432: n = sl->sln - i - 1;
433: slc = sl->sublevels[n];
434:
435: if (slc) {
436: slc->sorted = sl->tosort;
437: slc->start_position = sl->start_position;
438: sl->start_position += slc->tosort_num;
439: push_ls(slc);
440: sl->sublevels[n] = NULL;
441: }
442: }
443:
444: memcpy(sl->tosort + sl->start_position, sl->leaves,
445: sl->leaves_num * sizeof(struct sort_list_item *));
446: }
447: run_sort_cycle_st();
448: }
449:
450: void
451: rxsort(struct sort_list_item **base, size_t nmemb)
452: {
453: struct sort_level *sl;
454:
1.2 millert 455: sl = sort_calloc(1, sizeof(struct sort_level));
1.1 millert 456: sl->tosort = base;
457: sl->tosort_num = nmemb;
458: sl->tosort_sz = nmemb;
459:
460: run_top_sort_level(sl);
461:
462: free_sort_level(sl);
463: }