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