Debian Codegraph @

zgz/old-bzip2/blocksort.c

raw
1
2 /*-------------------------------------------------------------*/
3 /*--- Block sorting machinery ---*/
4 /*--- blocksort.c ---*/
5 /*-------------------------------------------------------------*/
6
7 /*--
8 This file is a part of bzip2 and/or libbzip2, a program and
9 library for lossless, block-sorting data compression.
10
11 Copyright (C) 1996-1999 Julian R Seward. All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions
15 are met:
16
17 1. Redistributions of source code must retain the above copyright
18 notice, this list of conditions and the following disclaimer.
19
20 2. The origin of this software must not be misrepresented; you must
21 not claim that you wrote the original software. If you use this
22 software in a product, an acknowledgment in the product
23 documentation would be appreciated but is not required.
24
25 3. Altered source versions must be plainly marked as such, and must
26 not be misrepresented as being the original software.
27
28 4. The name of the author may not be used to endorse or promote
29 products derived from this software without specific prior written
30 permission.
31
32 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
33 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
34 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
36 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
38 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
40 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
41 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43
44 Julian Seward, Cambridge, UK.
45 [email protected]
46 bzip2/libbzip2 version 0.9.5 of 24 May 1999
47
48 This program is based on (at least) the work of:
49 Mike Burrows
50 David Wheeler
51 Peter Fenwick
52 Alistair Moffat
53 Radford Neal
54 Ian H. Witten
55 Robert Sedgewick
56 Jon L. Bentley
57
58 For more information on these sources, see the manual.
59 --*/
60
61
62 #include "bzlib_private.h"
63
64 /*---------------------------------------------*/
65 /*--- Fallback O(N log(N)^2) sorting ---*/
66 /*--- algorithm, for repetitive blocks ---*/
67 /*---------------------------------------------*/
68
69 /*---------------------------------------------*/
70 static
71 __inline__
72 void fallbackSimpleSort ( UInt32* fmap,
73 UInt32* eclass,
74 Int32 lo,
75 Int32 hi )
76 {
77 Int32 i, j, tmp;
78 UInt32 ec_tmp;
79
80 if (lo == hi) return;
81
82 if (hi - lo > 3) {
83 for ( i = hi-4; i >= lo; i-- ) {
84 tmp = fmap[i];
85 ec_tmp = eclass[tmp];
86 for ( j = i+4; j <= hi && ec_tmp > eclass[fmap[j]]; j += 4 )
87 fmap[j-4] = fmap[j];
88 fmap[j-4] = tmp;
89 }
90 }
91
92 for ( i = hi-1; i >= lo; i-- ) {
93 tmp = fmap[i];
94 ec_tmp = eclass[tmp];
95 for ( j = i+1; j <= hi && ec_tmp > eclass[fmap[j]]; j++ )
96 fmap[j-1] = fmap[j];
97 fmap[j-1] = tmp;
98 }
99 }
100
101
102 /*---------------------------------------------*/
103 #define fswap(zz1, zz2) \
104 { Int32 zztmp = zz1; zz1 = zz2; zz2 = zztmp; }
105
106 #define fvswap(zzp1, zzp2, zzn) \
107 { \
108 Int32 yyp1 = (zzp1); \
109 Int32 yyp2 = (zzp2); \
110 Int32 yyn = (zzn); \
111 while (yyn > 0) { \
112 fswap(fmap[yyp1], fmap[yyp2]); \
113 yyp1++; yyp2++; yyn--; \
114 } \
115 }
116
117
118 #define fmin(a,b) ((a) < (b)) ? (a) : (b)
119
120 #define fpush(lz,hz) { stackLo[sp] = lz; \
121 stackHi[sp] = hz; \
122 sp++; }
123
124 #define fpop(lz,hz) { sp--; \
125 lz = stackLo[sp]; \
126 hz = stackHi[sp]; }
127
128 #define FALLBACK_QSORT_SMALL_THRESH 10
129 #define FALLBACK_QSORT_STACK_SIZE 100
130
131
132 static
133 void fallbackQSort3 ( UInt32* fmap,
134 UInt32* eclass,
135 Int32 loSt,
136 Int32 hiSt )
137 {
138 Int32 unLo, unHi, ltLo, gtHi, n, m;
139 Int32 sp, lo, hi;
140 UInt32 med, r, r3;
141 Int32 stackLo[FALLBACK_QSORT_STACK_SIZE];
142 Int32 stackHi[FALLBACK_QSORT_STACK_SIZE];
143
144 r = 0;
145
146 sp = 0;
147 fpush ( loSt, hiSt );
148
149 while (sp > 0) {
150
151 AssertH ( sp < FALLBACK_QSORT_STACK_SIZE, 1004 );
152
153 fpop ( lo, hi );
154 if (hi - lo < FALLBACK_QSORT_SMALL_THRESH) {
155 fallbackSimpleSort ( fmap, eclass, lo, hi );
156 continue;
157 }
158
159 /* Random partitioning. Median of 3 sometimes fails to
160 avoid bad cases. Median of 9 seems to help but
161 looks rather expensive. This too seems to work but
162 is cheaper. Guidance for the magic constants
163 7621 and 32768 is taken from Sedgewick's algorithms
164 book, chapter 35.
165 */
166 r = ((r * 7621) + 1) % 32768;
167 r3 = r % 3;
168 if (r3 == 0) med = eclass[fmap[lo]]; else
169 if (r3 == 1) med = eclass[fmap[(lo+hi)>>1]]; else
170 med = eclass[fmap[hi]];
171
172 unLo = ltLo = lo;
173 unHi = gtHi = hi;
174
175 while (1) {
176 while (1) {
177 if (unLo > unHi) break;
178 n = (Int32)eclass[fmap[unLo]] - (Int32)med;
179 if (n == 0) {
180 fswap(fmap[unLo], fmap[ltLo]);
181 ltLo++; unLo++;
182 continue;
183 };
184 if (n > 0) break;
185 unLo++;
186 }
187 while (1) {
188 if (unLo > unHi) break;
189 n = (Int32)eclass[fmap[unHi]] - (Int32)med;
190 if (n == 0) {
191 fswap(fmap[unHi], fmap[gtHi]);
192 gtHi--; unHi--;
193 continue;
194 };
195 if (n < 0) break;
196 unHi--;
197 }
198 if (unLo > unHi) break;
199 fswap(fmap[unLo], fmap[unHi]); unLo++; unHi--;
200 }
201
202 AssertD ( unHi == unLo-1, "fallbackQSort3(2)" );
203
204 if (gtHi < ltLo) continue;
205
206 n = fmin(ltLo-lo, unLo-ltLo); fvswap(lo, unLo-n, n);
207 m = fmin(hi-gtHi, gtHi-unHi); fvswap(unLo, hi-m+1, m);
208
209 n = lo + unLo - ltLo - 1;
210 m = hi - (gtHi - unHi) + 1;
211
212 if (n - lo > hi - m) {
213 fpush ( lo, n );
214 fpush ( m, hi );
215 } else {
216 fpush ( m, hi );
217 fpush ( lo, n );
218 }
219 }
220 }
221
222 #undef fmin
223 #undef fpush
224 #undef fpop
225 #undef fswap
226 #undef fvswap
227 #undef FALLBACK_QSORT_SMALL_THRESH
228 #undef FALLBACK_QSORT_STACK_SIZE
229
230
231 /*---------------------------------------------*/
232 /* Pre:
233 nblock > 0
234 eclass exists for [0 .. nblock-1]
235 ((UInt16*)eclass) [0 .. nblock-1] [15:8] holds block
236 ptr exists for [0 .. nblock-1]
237
238 Post:
239 ((UInt16*)eclass) [0 .. nblock-1] [15:8] holds block
240 All other areas of eclass destroyed
241 fmap [0 .. nblock-1] holds sorted order
242 bhtab [ 0 .. 2+(nblock/32) ] destroyed
243 */
244
245 #define SET_BH(zz) bhtab[(zz) >> 5] |= (1 << ((zz) & 31))
246 #define CLEAR_BH(zz) bhtab[(zz) >> 5] &= ~(1 << ((zz) & 31))
247 #define ISSET_BH(zz) (bhtab[(zz) >> 5] & (1 << ((zz) & 31)))
248 #define WORD_BH(zz) bhtab[(zz) >> 5]
249 #define UNALIGNED_BH(zz) ((zz) & 0x01f)
250
251 static
252 void fallbackSort ( UInt32* fmap,
253 UInt32* eclass,
254 UInt32* bhtab,
255 Int32 nblock,
256 Int32 verb )
257 {
258 Int32 ftab[257];
259 Int32 ftabCopy[256];
260 Int32 H, i, j, k, l, r, cc, cc1;
261 Int32 nNotDone;
262 Int32 nBhtab;
263 UInt16* eclass16 = (UInt16*)eclass;
264
265 /*--
266 Initial 1-char radix sort to generate
267 initial fmap and initial BH bits.
268 --*/
269 if (verb >= 4)
270 VPrintf0 ( " bucket sorting ...\n" );
271 for (i = 0; i < 257; i++) ftab[i] = 0;
272 for (i = 0; i < nblock; i++) ftab[eclass16[i] >> 8]++;
273 for (i = 0; i < 256; i++) ftabCopy[i] = ftab[i];
274 for (i = 1; i < 257; i++) ftab[i] += ftab[i-1];
275
276 for (i = 0; i < nblock; i++) {
277 j = eclass16[i] >> 8;
278 k = ftab[j] - 1;
279 ftab[j] = k;
280 fmap[k] = i;
281 }
282
283 nBhtab = 2 + (nblock / 32);
284 for (i = 0; i < nBhtab; i++) bhtab[i] = 0;
285 for (i = 0; i < 256; i++) SET_BH(ftab[i]);
286
287 /*--
288 Inductively refine the buckets. Kind-of an
289 "exponential radix sort" (!), inspired by the
290 Manber-Myers suffix array construction algorithm.
291 --*/
292
293 /*-- set sentinel bits for block-end detection --*/
294 for (i = 0; i < 32; i++) {
295 SET_BH(nblock + 2*i);
296 CLEAR_BH(nblock + 2*i + 1);
297 }
298
299 /*-- the log(N) loop --*/
300 H = 1;
301 while (1) {
302
303 if (verb >= 4)
304 VPrintf1 ( " depth %6d has ", H );
305
306 j = 0;
307 for (i = 0; i < nblock; i++) {
308 if (ISSET_BH(i)) j = i;
309 k = fmap[i] - H; if (k < 0) k += nblock;
310 eclass[k] = j;
311 }
312
313 nNotDone = 0;
314 r = -1;
315 while (1) {
316
317 /*-- find the next non-singleton bucket --*/
318 k = r + 1;
319 while (ISSET_BH(k) && UNALIGNED_BH(k)) k++;
320 if (ISSET_BH(k)) {
321 while (WORD_BH(k) == 0xffffffff) k += 32;
322 while (ISSET_BH(k)) k++;
323 }
324 l = k - 1;
325 if (l >= nblock) break;
326 while (!ISSET_BH(k) && UNALIGNED_BH(k)) k++;
327 if (!ISSET_BH(k)) {
328 while (WORD_BH(k) == 0x00000000) k += 32;
329 while (!ISSET_BH(k)) k++;
330 }
331 r = k - 1;
332 if (r >= nblock) break;
333
334 /*-- now [l, r] bracket current bucket --*/
335 if (r > l) {
336 nNotDone += (r - l + 1);
337 fallbackQSort3 ( fmap, eclass, l, r );
338
339 /*-- scan bucket and generate header bits-- */
340 cc = -1;
341 for (i = l; i <= r; i++) {
342 cc1 = eclass[fmap[i]];
343 if (cc != cc1) { SET_BH(i); cc = cc1; };
344 }
345 }
346 }
347
348 if (verb >= 4)
349 VPrintf1 ( "%6d unresolved strings\n", nNotDone );
350
351 H *= 2;
352 if (H > nblock || nNotDone == 0) break;
353 }
354
355 /*--
356 Reconstruct the original block in
357 eclass16 [0 .. nblock-1] [15:8], since the
358 previous phase destroyed it.
359 --*/
360 if (verb >= 4)
361 VPrintf0 ( " reconstructing block ...\n" );
362 j = 0;
363 for (i = 0; i < nblock; i++) {
364 while (ftabCopy[j] == 0) j++;
365 ftabCopy[j]--;
366 eclass16[fmap[i]] = j << 8;
367 }
368 AssertH ( j < 256, 1005 );
369 }
370
371 #undef SET_BH
372 #undef CLEAR_BH
373 #undef ISSET_BH
374 #undef WORD_BH
375 #undef UNALIGNED_BH
376
377
378 /*---------------------------------------------*/
379 /*--- The main, O(N^2 log(N)) sorting ---*/
380 /*--- algorithm. Faster for "normal" ---*/
381 /*--- non-repetitive blocks. ---*/
382 /*---------------------------------------------*/
383
384 /*---------------------------------------------*/
385 static
386 __inline__
387 Bool mainGtU ( UInt32 i1,
388 UInt32 i2,
389 UInt16* block,
390 UInt16* quadrant,
391 UInt32 nblock,
392 Int32* budget )
393 {
394 Int32 k;
395 UInt16 s1, s2;
396
397 AssertD ( i1 != i2, "mainGtU" );
398
399 s1 = block[i1]; s2 = block[i2];
400 if (s1 != s2) return (s1 > s2);
401 i1 += 2; i2 += 2;
402
403 s1 = block[i1]; s2 = block[i2];
404 if (s1 != s2) return (s1 > s2);
405 i1 += 2; i2 += 2;
406
407 s1 = block[i1]; s2 = block[i2];
408 if (s1 != s2) return (s1 > s2);
409 i1 += 2; i2 += 2;
410
411 s1 = block[i1]; s2 = block[i2];
412 if (s1 != s2) return (s1 > s2);
413 i1 += 2; i2 += 2;
414
415 s1 = block[i1]; s2 = block[i2];
416 if (s1 != s2) return (s1 > s2);
417 i1 += 2; i2 += 2;
418
419 s1 = block[i1]; s2 = block[i2];
420 if (s1 != s2) return (s1 > s2);
421 i1 += 2; i2 += 2;
422
423 k = nblock + 8;
424
425 do {
426
427 s1 = block[i1]; s2 = block[i2];
428 if (s1 != s2) return (s1 > s2);
429 s1 = quadrant[i1]; s2 = quadrant[i2];
430 if (s1 != s2) return (s1 > s2);
431 i1 += 2; i2 += 2;
432
433 s1 = block[i1]; s2 = block[i2];
434 if (s1 != s2) return (s1 > s2);
435 s1 = quadrant[i1]; s2 = quadrant[i2];
436 if (s1 != s2) return (s1 > s2);
437 i1 += 2; i2 += 2;
438
439 s1 = block[i1]; s2 = block[i2];
440 if (s1 != s2) return (s1 > s2);
441 s1 = quadrant[i1]; s2 = quadrant[i2];
442 if (s1 != s2) return (s1 > s2);
443 i1 += 2; i2 += 2;
444
445 s1 = block[i1]; s2 = block[i2];
446 if (s1 != s2) return (s1 > s2);
447 s1 = quadrant[i1]; s2 = quadrant[i2];
448 if (s1 != s2) return (s1 > s2);
449 i1 += 2; i2 += 2;
450
451 if (i1 >= nblock) i1 -= nblock;
452 if (i2 >= nblock) i2 -= nblock;
453
454 k -= 8;
455 (*budget)--;
456 }
457 while (k >= 0);
458
459 return False;
460 }
461
462
463 /*---------------------------------------------*/
464 /*--
465 Knuth's increments seem to work better
466 than Incerpi-Sedgewick here. Possibly
467 because the number of elems to sort is
468 usually small, typically <= 20.
469 --*/
470 Int32 incs[14] = { 1, 4, 13, 40, 121, 364, 1093, 3280,
471 9841, 29524, 88573, 265720,
472 797161, 2391484 };
473
474 static
475 void mainSimpleSort ( UInt32* ptr,
476 UInt16* block,
477 UInt16* quadrant,
478 Int32 nblock,
479 Int32 lo,
480 Int32 hi,
481 Int32 d,
482 Int32* budget )
483 {
484 Int32 i, j, h, bigN, hp;
485 UInt32 v;
486
487 bigN = hi - lo + 1;
488 if (bigN < 2) return;
489
490 hp = 0;
491 while (incs[hp] < bigN) hp++;
492 hp--;
493
494 for (; hp >= 0; hp--) {
495 h = incs[hp];
496
497 i = lo + h;
498 while (True) {
499
500 /*-- copy 1 --*/
501 if (i > hi) break;
502 v = ptr[i];
503 j = i;
504 while ( mainGtU (
505 ptr[j-h]+d, v+d, block, quadrant, nblock, budget
506 ) ) {
507 ptr[j] = ptr[j-h];
508 j = j - h;
509 if (j <= (lo + h - 1)) break;
510 }
511 ptr[j] = v;
512 i++;
513
514 /*-- copy 2 --*/
515 if (i > hi) break;
516 v = ptr[i];
517 j = i;
518 while ( mainGtU (
519 ptr[j-h]+d, v+d, block, quadrant, nblock, budget
520 ) ) {
521 ptr[j] = ptr[j-h];
522 j = j - h;
523 if (j <= (lo + h - 1)) break;
524 }
525 ptr[j] = v;
526 i++;
527
528 /*-- copy 3 --*/
529 if (i > hi) break;
530 v = ptr[i];
531 j = i;
532 while ( mainGtU (
533 ptr[j-h]+d, v+d, block, quadrant, nblock, budget
534 ) ) {
535 ptr[j] = ptr[j-h];
536 j = j - h;
537 if (j <= (lo + h - 1)) break;
538 }
539 ptr[j] = v;
540 i++;
541
542 if (*budget < 0) return;
543 }
544 }
545 }
546
547
548 /*---------------------------------------------*/
549 /*--
550 The following is an implementation of
551 an elegant 3-way quicksort for strings,
552 described in a paper "Fast Algorithms for
553 Sorting and Searching Strings", by Robert
554 Sedgewick and Jon L. Bentley.
555 --*/
556
557 #define mswap(zz1, zz2) \
558 { Int32 zztmp = zz1; zz1 = zz2; zz2 = zztmp; }
559
560 #define mvswap(zzp1, zzp2, zzn) \
561 { \
562 Int32 yyp1 = (zzp1); \
563 Int32 yyp2 = (zzp2); \
564 Int32 yyn = (zzn); \
565 while (yyn > 0) { \
566 mswap(ptr[yyp1], ptr[yyp2]); \
567 yyp1++; yyp2++; yyn--; \
568 } \
569 }
570
571
572 static
573 __inline__
574 UInt16 mmed3 ( UInt16 a, UInt16 b, UInt16 c )
575 {
576 UInt16 t;
577 if (a > b) { t = a; a = b; b = t; };
578 if (b > c) { t = b; b = c; c = t; };
579 if (a > b) b = a;
580 return b;
581 }
582
583
584 #define mmin(a,b) ((a) < (b)) ? (a) : (b)
585
586 #define mpush(lz,hz,dz) { stackLo[sp] = lz; \
587 stackHi[sp] = hz; \
588 stackD [sp] = dz; \
589 sp++; }
590
591 #define mpop(lz,hz,dz) { sp--; \
592 lz = stackLo[sp]; \
593 hz = stackHi[sp]; \
594 dz = stackD [sp]; }
595
596
597 #define mnextsize(az) (nextHi[az]-nextLo[az])
598
599 #define mnextswap(az,bz) \
600 { Int32 tz; \
601 tz = nextLo[az]; nextLo[az] = nextLo[bz]; nextLo[bz] = tz; \
602 tz = nextHi[az]; nextHi[az] = nextHi[bz]; nextHi[bz] = tz; \
603 tz = nextD [az]; nextD [az] = nextD [bz]; nextD [bz] = tz; }
604
605
606 #define MAIN_QSORT_SMALL_THRESH 20
607 #define MAIN_QSORT_DEPTH_THRESH (BZ_N_RADIX + BZ_N_QSORT)
608 #define MAIN_QSORT_STACK_SIZE 100
609
610 static
611 void mainQSort3 ( UInt32* ptr,
612 UInt16* block,
613 UInt16* quadrant,
614 Int32 nblock,
615 Int32 loSt,
616 Int32 hiSt,
617 Int32 dSt,
618 Int32* budget )
619 {
620 Int32 unLo, unHi, ltLo, gtHi, n, m, med;
621 Int32 sp, lo, hi, d;
622
623 Int32 stackLo[MAIN_QSORT_STACK_SIZE];
624 Int32 stackHi[MAIN_QSORT_STACK_SIZE];
625 Int32 stackD [MAIN_QSORT_STACK_SIZE];
626
627 Int32 nextLo[3];
628 Int32 nextHi[3];
629 Int32 nextD [3];
630
631 sp = 0;
632 mpush ( loSt, hiSt, dSt );
633
634 while (sp > 0) {
635
636 AssertH ( sp < MAIN_QSORT_STACK_SIZE, 1001 );
637
638 mpop ( lo, hi, d );
639 if (hi - lo < MAIN_QSORT_SMALL_THRESH ||
640 d > MAIN_QSORT_DEPTH_THRESH) {
641 mainSimpleSort ( ptr, block, quadrant, nblock, lo, hi, d, budget );
642 if (*budget < 0) return;
643 continue;
644 }
645
646 med = (Int32)
647 mmed3 ( block[ptr[ lo ]+d],
648 block[ptr[ hi ]+d],
649 block[ptr[ (lo+hi)>>1 ]+d] );
650
651 unLo = ltLo = lo;
652 unHi = gtHi = hi;
653
654 while (True) {
655 while (True) {
656 if (unLo > unHi) break;
657 n = ((Int32)block[ptr[unLo]+d]) - med;
658 if (n == 0) {
659 mswap(ptr[unLo], ptr[ltLo]);
660 ltLo++; unLo++; continue;
661 };
662 if (n > 0) break;
663 unLo++;
664 }
665 while (True) {
666 if (unLo > unHi) break;
667 n = ((Int32)block[ptr[unHi]+d]) - med;
668 if (n == 0) {
669 mswap(ptr[unHi], ptr[gtHi]);
670 gtHi--; unHi--; continue;
671 };
672 if (n < 0) break;
673 unHi--;
674 }
675 if (unLo > unHi) break;
676 mswap(ptr[unLo], ptr[unHi]); unLo++; unHi--;
677 }
678
679 AssertD ( unHi == unLo-1, "mainQSort3(2)" );
680
681 if (gtHi < ltLo) {
682 mpush(lo, hi, d+2 );
683 continue;
684 }
685
686 n = mmin(ltLo-lo, unLo-ltLo); mvswap(lo, unLo-n, n);
687 m = mmin(hi-gtHi, gtHi-unHi); mvswap(unLo, hi-m+1, m);
688
689 n = lo + unLo - ltLo - 1;
690 m = hi - (gtHi - unHi) + 1;
691
692 nextLo[0] = lo; nextHi[0] = n; nextD[0] = d;
693 nextLo[1] = m; nextHi[1] = hi; nextD[1] = d;
694 nextLo[2] = n+1; nextHi[2] = m-1; nextD[2] = d+2;
695
696 if (mnextsize(0) < mnextsize(1)) mnextswap(0,1);
697 if (mnextsize(1) < mnextsize(2)) mnextswap(1,2);
698 if (mnextsize(0) < mnextsize(1)) mnextswap(0,1);
699
700 AssertD (mnextsize(0) >= mnextsize(1), "mainQSort3(8)" );
701 AssertD (mnextsize(1) >= mnextsize(2), "mainQSort3(9)" );
702
703 mpush (nextLo[0], nextHi[0], nextD[0]);
704 mpush (nextLo[1], nextHi[1], nextD[1]);
705 mpush (nextLo[2], nextHi[2], nextD[2]);
706 }
707 }
708
709 #undef mswap
710 #undef mvswap
711 #undef mpush
712 #undef mpop
713 #undef mmin
714 #undef mnextsize
715 #undef mnextswap
716 #undef MAIN_QSORT_SMALL_THRESH
717 #undef MAIN_QSORT_DEPTH_THRESH
718 #undef MAIN_QSORT_STACK_SIZE
719
720
721 /*---------------------------------------------*/
722 /* Pre:
723 nblock > N_OVERSHOOT
724 block32 exists for [0 .. nblock-1 +N_OVERSHOOT]
725 ((UInt16*)block32) [0 .. nblock-1] [15:8] holds block
726 ptr exists for [0 .. nblock-1]
727
728 Post:
729 ((UInt16*)block32) [0 .. nblock-1] [15:8] holds block
730 All other areas of block32 destroyed
731 ftab [0 .. 65536 ] destroyed
732 ptr [0 .. nblock-1] holds sorted order
733 if (*budget < 0), sorting was abandoned
734 */
735
736 #define BIGFREQ(b) (ftab[((b)+1) << 8] - ftab[(b) << 8])
737 #define SETMASK (1 << 21)
738 #define CLEARMASK (~(SETMASK))
739
740 static
741 void mainSort ( UInt32* ptr,
742 UInt16* block,
743 UInt16* quadrant,
744 UInt32* ftab,
745 Int32 nblock,
746 Int32 verb,
747 Int32* budget )
748 {
749 Int32 i, j, k, m, ss, sb;
750 Int32 runningOrder[256];
751 Int32 copy[256];
752 Bool bigDone[256];
753 UChar c1;
754 Int32 numQSorted = 0;
755 UInt16 s;
756
757 if (verb >= 4) VPrintf0 ( " main sort initialise ...\n" );
758
759 /*-- Stripe the block data into 16 bits, and at the
760 same time set up the 2-byte frequency table
761 --*/
762 for (i = 65536; i >= 0; i--) ftab[i] = 0;
763
764 s = block[0];
765 for (i = 1; i < nblock; i++) {
766 quadrant[i] = 0;
767 s = (s << 8) | block[i];
768 block[i-1] = s;
769 ftab[s]++;
770 }
771 quadrant[0] = 0;
772 s = (s << 8) | (block[0] >> 8);
773 block[nblock-1] = s;
774 ftab[s]++;
775
776 /*-- (emphasises close relationship of block & quadrant) --*/
777 for (i = 0; i < BZ_N_OVERSHOOT; i++) {
778 block [nblock+i] = block[i];
779 quadrant[nblock+i] = 0;
780 }
781
782 if (verb >= 4) VPrintf0 ( " bucket sorting ...\n" );
783
784 /*-- Complete the initial radix sort --*/
785 for (i = 1; i <= 65536; i++) ftab[i] += ftab[i-1];
786
787 for (i = 0; i < nblock; i++) {
788 s = block[i];
789 j = ftab[s] - 1;
790 ftab[s] = j;
791 ptr[j] = i;
792 }
793
794 /*--
795 Now ftab contains the first loc of every small bucket.
796 Calculate the running order, from smallest to largest
797 big bucket.
798 --*/
799 for (i = 0; i <= 255; i++) {
800 bigDone [i] = False;
801 runningOrder[i] = i;
802 }
803
804 {
805 Int32 vv;
806 Int32 h = 1;
807 do h = 3 * h + 1; while (h <= 256);
808 do {
809 h = h / 3;
810 for (i = h; i <= 255; i++) {
811 vv = runningOrder[i];
812 j = i;
813 while ( BIGFREQ(runningOrder[j-h]) > BIGFREQ(vv) ) {
814 runningOrder[j] = runningOrder[j-h];
815 j = j - h;
816 if (j <= (h - 1)) goto zero;
817 }
818 zero:
819 runningOrder[j] = vv;
820 }
821 } while (h != 1);
822 }
823
824 /*--
825 The main sorting loop.
826 --*/
827
828 for (i = 0; i <= 255; i++) {
829
830 /*--
831 Process big buckets, starting with the least full.
832 Basically this is a 4-step process in which we call
833 mainQSort3 to sort the small buckets [ss, j], but
834 also make a big effort to avoid the calls if we can.
835 --*/
836 ss = runningOrder[i];
837
838 /*--
839 Step 1:
840 Complete the big bucket [ss] by quicksorting
841 any unsorted small buckets [ss, j], for j != ss.
842 Hopefully previous pointer-scanning phases have already
843 completed many of the small buckets [ss, j], so
844 we don't have to sort them at all.
845 --*/
846 for (j = 0; j <= 255; j++) {
847 if (j != ss) {
848 sb = (ss << 8) + j;
849 if ( ! (ftab[sb] & SETMASK) ) {
850 Int32 lo = ftab[sb] & CLEARMASK;
851 Int32 hi = (ftab[sb+1] & CLEARMASK) - 1;
852 if (hi > lo) {
853 if (verb >= 4)
854 VPrintf4 ( " qsort [0x%x, 0x%x] "
855 "done %d this %d\n",
856 ss, j, numQSorted, hi - lo + 1 );
857 mainQSort3 (
858 ptr, block, quadrant, nblock,
859 lo, hi, BZ_N_RADIX, budget
860 );
861 numQSorted += (hi - lo + 1);
862 if (*budget < 0) return;
863 }
864 }
865 ftab[sb] |= SETMASK;
866 }
867 }
868
869 /*--
870 Step 2:
871 Deal specially with case [ss, ss]. This establishes the
872 sorted order for [ss, ss] without any comparisons.
873 A clever trick, cryptically described as steps Q6b and Q6c
874 in SRC-124 (aka BW94). Compared to bzip2, this makes it
875 practical not to use a preliminary run-length coder.
876 --*/
877 {
878 Int32 put0, get0, put1, get1;
879 Int32 sbn = (ss << 8) + ss;
880 Int32 lo = ftab[sbn] & CLEARMASK;
881 Int32 hi = (ftab[sbn+1] & CLEARMASK) - 1;
882 UChar ssc = (UChar)ss;
883 put0 = lo;
884 get0 = ftab[ss << 8] & CLEARMASK;
885 put1 = hi;
886 get1 = (ftab[(ss+1) << 8] & CLEARMASK) - 1;
887 while (get0 < put0) {
888 j = ptr[get0]-1; if (j < 0) j += nblock;
889 c1 = (UChar)(block[j] >> 8);
890 if (c1 == ssc) { ptr[put0] = j; put0++; };
891 get0++;
892 }
893 while (get1 > put1) {
894 j = ptr[get1]-1; if (j < 0) j += nblock;
895 c1 = (UChar)(block[j] >> 8);
896 if (c1 == ssc) { ptr[put1] = j; put1--; };
897 get1--;
898 }
899 ftab[sbn] |= SETMASK;
900 }
901
902 /*--
903 Step 3:
904 The [ss] big bucket is now done. Record this fact,
905 and update the quadrant descriptors. Remember to
906 update quadrants in the overshoot area too, if
907 necessary. The "if (i < 255)" test merely skips
908 this updating for the last bucket processed, since
909 updating for the last bucket is pointless.
910
911 The quadrant array provides a way to incrementally
912 cache sort orderings, as they appear, so as to
913 make subsequent comparisons in fullGtU() complete
914 faster. For repetitive blocks this makes a big
915 difference (but not big enough to be able to avoid
916 the fallback sorting mechanism, exponential radix sort).
917
918 The precise meaning is: at all times:
919
920 for 0 <= i < nblock and 0 <= j <= nblock
921
922 if block[i] != block[j],
923
924 then the relative values of quadrant[i] and
925 quadrant[j] are meaningless.
926
927 else {
928 if quadrant[i] < quadrant[j]
929 then the string starting at i lexicographically
930 precedes the string starting at j
931
932 else if quadrant[i] > quadrant[j]
933 then the string starting at j lexicographically
934 precedes the string starting at i
935
936 else
937 the relative ordering of the strings starting
938 at i and j has not yet been determined.
939 }
940 --*/
941 bigDone[ss] = True;
942
943 if (i < 255) {
944 Int32 bbStart = ftab[ss << 8] & CLEARMASK;
945 Int32 bbSize = (ftab[(ss+1) << 8] & CLEARMASK) - bbStart;
946 Int32 shifts = 0;
947
948 while ((bbSize >> shifts) > 65534) shifts++;
949
950 for (j = 0; j < bbSize; j++) {
951 Int32 a2update = ptr[bbStart + j];
952 UInt16 qVal = (UInt16)(j >> shifts);
953 quadrant[a2update] = qVal;
954 if (a2update < BZ_N_OVERSHOOT)
955 quadrant[a2update + nblock] = qVal;
956 }
957 AssertH ( ((bbSize-1) >> shifts) <= 65535, 1002 );
958 }
959
960 /*--
961 Step 4:
962 Now scan this big bucket [ss] so as to synthesise the
963 sorted order for small buckets [t, ss] for all t != ss.
964 This will avoid doing Real Work in subsequent Step 1's.
965 --*/
966 for (j = 0; j <= 255; j++)
967 copy[j] = ftab[(j << 8) + ss] & CLEARMASK;
968
969 m = ftab[(ss+1) << 8] & CLEARMASK;
970 for (j = ftab[ss << 8] & CLEARMASK; j < m; j++) {
971 k = ptr[j] - 1; if (k < 0) k += nblock;
972 c1 = (UChar)(block[k] >> 8);
973 if ( ! bigDone[c1] ) {
974 ptr[copy[c1]] = k;
975 copy[c1] ++;
976 }
977 }
978
979 for (j = 0; j <= 255; j++) ftab[(j << 8) + ss] |= SETMASK;
980 }
981
982 if (verb >= 4)
983 VPrintf3 ( " %d pointers, %d sorted, %d scanned\n",
984 nblock, numQSorted, nblock - numQSorted );
985 }
986
987 #undef BIGFREQ
988 #undef SETMASK
989 #undef CLEARMASK
990
991
992 /*---------------------------------------------*/
993 /* Pre:
994 nblock > 0
995 arr2 exists for [0 .. nblock-1 +N_OVERSHOOT]
996 ((UInt16*)arr2) [0 .. nblock-1] [15:8] holds block
997 arr1 exists for [0 .. nblock-1]
998
999 Post:
1000 ((UInt16*)arr2) [0 .. nblock-1] [15:8] holds block
1001 All other areas of block destroyed
1002 ftab [ 0 .. 65536 ] destroyed
1003 arr1 [0 .. nblock-1] holds sorted order
1004 */
1005 void blockSort ( EState* s )
1006 {
1007 UInt32* ptr = s->ptr;
1008 UInt16* block = s->block;
1009 UInt32* ftab = s->ftab;
1010 Int32 nblock = s->nblock;
1011 Int32 verb = s->verbosity;
1012 Int32 wfact = s->workFactor;
1013 UInt16* quadrant;
1014 Int32 budget;
1015 Int32 budgetInit;
1016 Int32 i;
1017
1018 if (nblock < 10000) {
1019 for (i = 0; i < nblock; i++) block[i] <<= 8;
1020 fallbackSort ( s->arr1, s->arr2, ftab, nblock, verb );
1021 } else {
1022 quadrant = &(block[nblock+BZ_N_OVERSHOOT]);
1023
1024 /* (wfact-1) / 3 puts the default-factor-30
1025 transition point at very roughly the same place as
1026 with v0.1 and v0.9.0.
1027 Not that it particularly matters any more, since the
1028 resulting compressed stream is now the same regardless
1029 of whether or not we use the main sort or fallback sort.
1030 */
1031 if (wfact < 1 ) wfact = 1;
1032 if (wfact > 100) wfact = 100;
1033 budgetInit = nblock * ((wfact-1) / 3);
1034 budget = budgetInit;
1035
1036 mainSort ( ptr, block, quadrant, ftab, nblock, verb, &budget );
1037 if (verb >= 3)
1038 VPrintf3 ( " %d work, %d block, ratio %5.2f\n",
1039 budgetInit - budget,
1040 nblock,
1041 (float)(budgetInit - budget) /
1042 (float)(nblock==0 ? 1 : nblock) );
1043 if (budget < 0) {
1044 if (verb >= 2)
1045 VPrintf0 ( " too repetitive; using fallback"
1046 " sorting algorithm\n" );
1047 fallbackSort ( s->arr1, s->arr2, ftab, nblock, verb );
1048 }
1049 }
1050
1051 s->origPtr = -1;
1052 for (i = 0; i < s->nblock; i++)
1053 if (ptr[i] == 0)
1054 { s->origPtr = i; break; };
1055
1056 AssertH( s->origPtr != -1, 1003 );
1057 }
1058
1059
1060 /*-------------------------------------------------------------*/
1061 /*--- end blocksort.c ---*/
1062 /*-------------------------------------------------------------*/