(C) Relatively high density file backups on paper. Cross-platform CLI port of Ollydbg's Paperback from Windows and Borland C.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bz_blocksort.cpp 30KB

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