sac.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include "sac.h"
  5. #include "ssort.h"
  6. typedef struct Chain Chain;
  7. typedef struct Chains Chains;
  8. typedef struct Huff Huff;
  9. enum
  10. {
  11. ZBase = 2, /* base of code to encode 0 runs */
  12. LitBase = ZBase-1, /* base of literal values */
  13. MaxLit = 256,
  14. MaxLeaf = MaxLit+LitBase,
  15. MaxHuffBits = 16, /* limit of bits in a huffman code */
  16. ChainMem = 2 * (MaxHuffBits - 1) * MaxHuffBits,
  17. };
  18. /*
  19. * huffman code table
  20. */
  21. struct Huff
  22. {
  23. short bits; /* length of the code */
  24. ulong encode; /* the code */
  25. };
  26. struct Chain
  27. {
  28. ulong count; /* occurances of everything in the chain */
  29. ushort leaf; /* leaves to the left of chain, or leaf value */
  30. char col; /* ref count for collecting unused chains */
  31. char gen; /* need to generate chains for next lower level */
  32. Chain *up; /* Chain up in the lists */
  33. };
  34. struct Chains
  35. {
  36. Chain *lists[(MaxHuffBits - 1) * 2];
  37. ulong leafcount[MaxLeaf]; /* sorted list of leaf counts */
  38. ushort leafmap[MaxLeaf]; /* map to actual leaf number */
  39. int nleaf; /* number of leaves */
  40. Chain chains[ChainMem];
  41. Chain *echains;
  42. Chain *free;
  43. char col;
  44. int nlists;
  45. };
  46. static jmp_buf errjmp;
  47. static uchar *bout;
  48. static int bmax;
  49. static int bpos;
  50. static ulong leafcount[MaxLeaf];
  51. static ulong nzero;
  52. static ulong *hbuf;
  53. static int hbpos;
  54. static ulong nzero;
  55. static ulong maxblocksym;
  56. static void henc(int);
  57. static void hbflush(void);
  58. static void mkprecode(Huff*, ulong *, int, int);
  59. static ulong sendtab(Huff *tab, int maxleaf, ushort *map);
  60. static int elimBits(int b, ulong *bused, char *tmtf, int maxbits);
  61. static void elimBit(int b, char *tmtf, int maxbits);
  62. static void nextchain(Chains*, int);
  63. static void hufftabinit(Huff*, int, ulong*, int);
  64. static void leafsort(ulong*, ushort*, int, int);
  65. static void bitput(int, int);
  66. static int mtf(uchar*, int);
  67. static void fatal(char*, ...);
  68. static ulong headerbits;
  69. static ulong charmapbits;
  70. static ulong hufftabbits;
  71. static ulong databits;
  72. static ulong nbits;
  73. static ulong bits;
  74. int
  75. sac(uchar *dst, uchar *src, int n)
  76. {
  77. uchar front[256];
  78. ulong *perm, cmap[256];
  79. long i, c, rev;
  80. rev = 0;
  81. perm = nil;
  82. if(setjmp(errjmp)){
  83. free(perm);
  84. if(rev){
  85. for(i = 0; i < n/2; i++){
  86. c = src[i];
  87. src[i] = src[n-i-1];
  88. src[n-i-1] = c;
  89. }
  90. }
  91. return -1;
  92. }
  93. /*
  94. * set up the output buffer
  95. */
  96. bout = dst;
  97. bmax = n;
  98. bpos = 0;
  99. perm = malloc((n+1)*sizeof *perm);
  100. if(perm == nil)
  101. return -1;
  102. hbuf = perm;
  103. hbpos = 0;
  104. nbits = 0;
  105. nzero = 0;
  106. for(i = 0; i < MaxLeaf; i++)
  107. leafcount[i] = 0;
  108. if(rev){
  109. for(i = 0; i < n/2; i++){
  110. c = src[i];
  111. src[i] = src[n-i-1];
  112. src[n-i-1] = c;
  113. }
  114. }
  115. i = ssortbyte(src, (int*)perm, nil, n);
  116. headerbits += 16;
  117. bitput(i, 16);
  118. /*
  119. * send the map of chars which occur in this block
  120. */
  121. for(i = 0; i < 256; i++)
  122. cmap[i] = 0;
  123. for(i = 0; i < n; i++)
  124. cmap[src[i]] = 1;
  125. charmapbits += 1;
  126. bitput(cmap[0] != 0, 1);
  127. for(i = 0; i < 256; i = c){
  128. for(c = i + 1; c < 256 && (cmap[c] != 0) == (cmap[i] != 0); c++)
  129. ;
  130. charmapbits += 8;
  131. bitput(c - i - 1, 8);
  132. }
  133. /*
  134. * initialize mtf state
  135. */
  136. c = 0;
  137. for(i = 0; i < 256; i++){
  138. if(cmap[i]){
  139. cmap[i] = c;
  140. front[c] = i;
  141. c++;
  142. }
  143. }
  144. maxblocksym = c + LitBase;
  145. for(i = 0; i <= n; i++){
  146. c = perm[i] - 1;
  147. if(c < 0)
  148. continue;
  149. henc(mtf(front, src[c]));
  150. }
  151. hbflush();
  152. bitput(0, 24);
  153. bitput(0, 8 - nbits);
  154. free(perm);
  155. return bpos;
  156. }
  157. static void
  158. bitput(int c, int n)
  159. {
  160. bits <<= n;
  161. bits |= c;
  162. for(nbits += n; nbits >= 8; nbits -= 8){
  163. c = bits << (32 - nbits);
  164. if(bpos >= bmax)
  165. longjmp(errjmp, 1);
  166. bout[bpos++] = c >> 24;
  167. }
  168. }
  169. static int
  170. mtf(uchar *front, int c)
  171. {
  172. int i, v;
  173. for(i = 0; front[i] != c; i++)
  174. ;
  175. for(v = i; i > 0; i--)
  176. front[i] = front[i - 1];
  177. front[0] = c;
  178. return v;
  179. }
  180. /*
  181. * encode a run of zeros
  182. * convert to funny run length coding:
  183. * add one, omit implicit top 1 bit.
  184. */
  185. static void
  186. zenc(void)
  187. {
  188. int m;
  189. nzero++;
  190. while(nzero != 1){
  191. m = nzero & 1;
  192. hbuf[hbpos++] = m;
  193. leafcount[m]++;
  194. nzero >>= 1;
  195. }
  196. nzero = 0;
  197. }
  198. /*
  199. * encode one symbol
  200. */
  201. static void
  202. henc(int c)
  203. {
  204. if(c == 0){
  205. nzero++;
  206. return;
  207. }
  208. if(nzero)
  209. zenc();
  210. c += LitBase;
  211. leafcount[c]++;
  212. hbuf[hbpos++] = c;
  213. }
  214. static void
  215. hbflush(void)
  216. {
  217. Huff tab[MaxLeaf];
  218. int i, b, s;
  219. if(nzero)
  220. zenc();
  221. if(hbpos == 0)
  222. return;
  223. mkprecode(tab, leafcount, maxblocksym, MaxHuffBits);
  224. hufftabbits += sendtab(tab, maxblocksym, nil);
  225. /*
  226. * send the data
  227. */
  228. for(i = 0; i < hbpos; i++){
  229. s = hbuf[i];
  230. b = tab[s].bits;
  231. if(b == 0)
  232. fatal("bad tables %d", i);
  233. databits += b;
  234. bitput(tab[s].encode, b);
  235. }
  236. }
  237. static ulong
  238. sendtab(Huff *tab, int maxleaf, ushort *map)
  239. {
  240. ulong ccount[MaxHuffBits+1], bused[MaxHuffBits+1];
  241. Huff codetab[MaxHuffBits+1];
  242. char tmtf[MaxHuffBits+1];
  243. ulong bits;
  244. int i, b, max, ttb, s, elim;
  245. /*
  246. * make up the table table
  247. * over cleverness: the data is known to be a huffman table
  248. * check for fullness at each bit level, and wipe it out from
  249. * the possibilities; when nothing exists except 0, stop.
  250. */
  251. for(i = 0; i <= MaxHuffBits; i++){
  252. tmtf[i] = i;
  253. ccount[i] = 0;
  254. bused[i] = 0;
  255. }
  256. tmtf[0] = -1;
  257. tmtf[MaxHuffBits] = 0;
  258. elim = 0;
  259. for(i = 0; i < maxleaf && elim != MaxHuffBits; i++){
  260. b = i;
  261. if(map)
  262. b = map[b];
  263. b = tab[b].bits;
  264. for(s = 0; tmtf[s] != b; s++)
  265. if(s >= MaxHuffBits)
  266. fatal("bitlength not found");
  267. ccount[s]++;
  268. for(; s > 0; s--)
  269. tmtf[s] = tmtf[s-1];
  270. tmtf[0] = b;
  271. elim += elimBits(b, bused, tmtf, MaxHuffBits);
  272. }
  273. if(elim != MaxHuffBits && elim != 0)
  274. fatal("incomplete huffman table");
  275. /*
  276. * make up and send the table table
  277. */
  278. max = 8;
  279. mkprecode(codetab, ccount, MaxHuffBits+1, max);
  280. for(i = 0; i <= max; i++){
  281. tmtf[i] = i;
  282. bused[i] = 0;
  283. }
  284. tmtf[0] = -1;
  285. tmtf[max] = 0;
  286. elim = 0;
  287. bits = 0;
  288. for(i = 0; i <= MaxHuffBits && elim != max; i++){
  289. b = codetab[i].bits;
  290. for(s = 0; tmtf[s] != b; s++)
  291. if(s > max)
  292. fatal("bitlength not found");
  293. ttb = 4;
  294. while(max - elim < (1 << (ttb-1)))
  295. ttb--;
  296. bits += ttb;
  297. bitput(s, ttb);
  298. elim += elimBits(b, bused, tmtf, max);
  299. }
  300. if(elim != max)
  301. fatal("incomplete huffman table table");
  302. /*
  303. * send the table
  304. */
  305. for(i = 0; i <= MaxHuffBits; i++){
  306. tmtf[i] = i;
  307. bused[i] = 0;
  308. }
  309. tmtf[0] = -1;
  310. tmtf[MaxHuffBits] = 0;
  311. elim = 0;
  312. for(i = 0; i < maxleaf && elim != MaxHuffBits; i++){
  313. b = i;
  314. if(map)
  315. b = map[b];
  316. b = tab[b].bits;
  317. for(s = 0; tmtf[s] != b; s++)
  318. if(s >= MaxHuffBits)
  319. fatal("bitlength not found");
  320. ttb = codetab[s].bits;
  321. if(ttb < 0)
  322. fatal("bad huffman table table");
  323. bits += ttb;
  324. bitput(codetab[s].encode, ttb);
  325. for(; s > 0; s--)
  326. tmtf[s] = tmtf[s-1];
  327. tmtf[0] = b;
  328. elim += elimBits(b, bused, tmtf, MaxHuffBits);
  329. }
  330. if(elim != MaxHuffBits && elim != 0)
  331. fatal("incomplete huffman table");
  332. return bits;
  333. }
  334. static int
  335. elimBits(int b, ulong *bused, char *tmtf, int maxbits)
  336. {
  337. int bb, elim;
  338. if(b < 0)
  339. return 0;
  340. elim = 0;
  341. /*
  342. * increase bits counts for all descendants
  343. */
  344. for(bb = b + 1; bb < maxbits; bb++){
  345. bused[bb] += 1 << (bb - b);
  346. if(bused[bb] == (1 << bb)){
  347. elim++;
  348. elimBit(bb, tmtf, maxbits);
  349. }
  350. }
  351. /*
  352. * steal bits from parent & check for fullness
  353. */
  354. for(; b >= 0; b--){
  355. bused[b]++;
  356. if(bused[b] == (1 << b)){
  357. elim++;
  358. elimBit(b, tmtf, maxbits);
  359. }
  360. if((bused[b] & 1) == 0)
  361. break;
  362. }
  363. return elim;
  364. }
  365. static void
  366. elimBit(int b, char *tmtf, int maxbits)
  367. {
  368. int bb;
  369. for(bb = 0; bb < maxbits; bb++)
  370. if(tmtf[bb] == b)
  371. break;
  372. if(tmtf[bb] != b)
  373. fatal("elim bit not found");
  374. while(++bb <= maxbits)
  375. tmtf[bb - 1] = tmtf[bb];
  376. }
  377. /*
  378. * fast?, in-place huffman codes
  379. *
  380. * A. Moffat, J. Katajainen, "In-Place Calculation of Minimum-Redundancy Codes",
  381. *
  382. * counts must be sorted, and may be aliased to bitlens
  383. */
  384. static int
  385. fmkprecode(ulong *bitcount, ulong *bitlen, ulong *counts, int n, int maxbits)
  386. {
  387. int leaf, tree, treet, depth, nodes, internal;
  388. /*
  389. * form the internal tree structure:
  390. * [0, tree) are parent pointers,
  391. * [tree, treet) are weights of internal nodes,
  392. * [leaf, n) are weights of remaining leaves.
  393. *
  394. * note that at the end, there are n-1 internal nodes
  395. */
  396. leaf = 0;
  397. tree = 0;
  398. for(treet = 0; treet != n - 1; treet++){
  399. if(leaf >= n || tree < treet && bitlen[tree] < counts[leaf]){
  400. bitlen[treet] = bitlen[tree];
  401. bitlen[tree++] = treet;
  402. }else
  403. bitlen[treet] = counts[leaf++];
  404. if(leaf >= n || tree < treet && bitlen[tree] < counts[leaf]){
  405. bitlen[treet] += bitlen[tree];
  406. bitlen[tree++] = treet;
  407. }else
  408. bitlen[treet] += counts[leaf++];
  409. }
  410. if(tree != treet - 1)
  411. fatal("bad fast mkprecode");
  412. /*
  413. * calculate depth of internal nodes
  414. */
  415. bitlen[n - 2] = 0;
  416. for(tree = n - 3; tree >= 0; tree--)
  417. bitlen[tree] = bitlen[bitlen[tree]] + 1;
  418. /*
  419. * calculate depth of leaves:
  420. * at each depth, leaves(depth) = nodes(depth) - internal(depth)
  421. * and nodes(depth+1) = 2 * internal(depth)
  422. */
  423. leaf = n;
  424. tree = n - 2;
  425. depth = 0;
  426. for(nodes = 1; nodes > 0; nodes = 2 * internal){
  427. internal = 0;
  428. while(tree >= 0 && bitlen[tree] == depth){
  429. tree--;
  430. internal++;
  431. }
  432. nodes -= internal;
  433. if(depth < maxbits)
  434. bitcount[depth] = nodes;
  435. while(nodes-- > 0)
  436. bitlen[--leaf] = depth;
  437. depth++;
  438. }
  439. if(leaf != 0 || tree != -1)
  440. fatal("bad leaves in fast mkprecode");
  441. return depth - 1;
  442. }
  443. /*
  444. * fast, low space overhead algorithm for max depth huffman type codes
  445. *
  446. * J. Katajainen, A. Moffat and A. Turpin, "A fast and space-economical
  447. * algorithm for length-limited coding," Proc. Intl. Symp. on Algorithms
  448. * and Computation, Cairns, Australia, Dec. 1995, Lecture Notes in Computer
  449. * Science, Vol 1004, J. Staples, P. Eades, N. Katoh, and A. Moffat, eds.,
  450. * pp 12-21, Springer Verlag, New York, 1995.
  451. */
  452. static void
  453. mkprecode(Huff *tab, ulong *count, int n, int maxbits)
  454. {
  455. Chains cs;
  456. Chain *c;
  457. ulong bitcount[MaxHuffBits];
  458. int i, m, em, bits;
  459. /*
  460. * set up the sorted list of leaves
  461. */
  462. m = 0;
  463. for(i = 0; i < n; i++){
  464. tab[i].bits = -1;
  465. tab[i].encode = 0;
  466. if(count[i] != 0){
  467. cs.leafcount[m] = count[i];
  468. cs.leafmap[m] = i;
  469. m++;
  470. }
  471. }
  472. if(m < 2){
  473. if(m != 0){
  474. m = cs.leafmap[0];
  475. tab[m].bits = 0;
  476. tab[m].encode = 0;
  477. }
  478. return;
  479. }
  480. cs.nleaf = m;
  481. leafsort(cs.leafcount, cs.leafmap, 0, m);
  482. /*
  483. * try fast code
  484. */
  485. bits = fmkprecode(bitcount, cs.leafcount, cs.leafcount, m, maxbits);
  486. if(bits < maxbits){
  487. for(i = 0; i < m; i++)
  488. tab[cs.leafmap[i]].bits = cs.leafcount[i];
  489. bitcount[0] = 0;
  490. }else{
  491. for(i = 0; i < m; i++)
  492. cs.leafcount[i] = count[cs.leafmap[i]];
  493. /*
  494. * set up free list
  495. */
  496. cs.free = &cs.chains[2];
  497. cs.echains = &cs.chains[ChainMem];
  498. cs.col = 1;
  499. /*
  500. * initialize chains for each list
  501. */
  502. c = &cs.chains[0];
  503. c->count = cs.leafcount[0];
  504. c->leaf = 1;
  505. c->col = cs.col;
  506. c->up = nil;
  507. c->gen = 0;
  508. cs.chains[1] = cs.chains[0];
  509. cs.chains[1].leaf = 2;
  510. cs.chains[1].count = cs.leafcount[1];
  511. for(i = 0; i < maxbits-1; i++){
  512. cs.lists[i * 2] = &cs.chains[0];
  513. cs.lists[i * 2 + 1] = &cs.chains[1];
  514. }
  515. cs.nlists = 2 * (maxbits - 1);
  516. m = 2 * m - 2;
  517. for(i = 2; i < m; i++)
  518. nextchain(&cs, cs.nlists - 2);
  519. bits = 0;
  520. bitcount[0] = cs.nleaf;
  521. for(c = cs.lists[cs.nlists - 1]; c != nil; c = c->up){
  522. m = c->leaf;
  523. bitcount[bits++] -= m;
  524. bitcount[bits] = m;
  525. }
  526. m = 0;
  527. for(i = bits; i >= 0; i--)
  528. for(em = m + bitcount[i]; m < em; m++)
  529. tab[cs.leafmap[m]].bits = i;
  530. }
  531. hufftabinit(tab, n, bitcount, bits);
  532. }
  533. /*
  534. * calculate the next chain on the list
  535. * we can always toss out the old chain
  536. */
  537. static void
  538. nextchain(Chains *cs, int list)
  539. {
  540. Chain *c, *oc;
  541. int i, nleaf, sumc;
  542. oc = cs->lists[list + 1];
  543. cs->lists[list] = oc;
  544. if(oc == nil)
  545. return;
  546. /*
  547. * make sure we have all chains needed to make sumc
  548. * note it is possible to generate only one of these,
  549. * use twice that value for sumc, and then generate
  550. * the second if that preliminary sumc would be chosen.
  551. * however, this appears to be slower on current tests
  552. */
  553. if(oc->gen){
  554. nextchain(cs, list - 2);
  555. nextchain(cs, list - 2);
  556. oc->gen = 0;
  557. }
  558. /*
  559. * pick up the chain we're going to add;
  560. * collect unused chains no free ones are left
  561. */
  562. for(c = cs->free; ; c++){
  563. if(c >= cs->echains){
  564. cs->col++;
  565. for(i = 0; i < cs->nlists; i++)
  566. for(c = cs->lists[i]; c != nil; c = c->up)
  567. c->col = cs->col;
  568. c = cs->chains;
  569. }
  570. if(c->col != cs->col)
  571. break;
  572. }
  573. /*
  574. * pick the cheapest of
  575. * 1) the next package from the previous list
  576. * 2) the next leaf
  577. */
  578. nleaf = oc->leaf;
  579. sumc = 0;
  580. if(list > 0 && cs->lists[list-1] != nil)
  581. sumc = cs->lists[list-2]->count + cs->lists[list-1]->count;
  582. if(sumc != 0 && (nleaf >= cs->nleaf || cs->leafcount[nleaf] > sumc)){
  583. c->count = sumc;
  584. c->leaf = oc->leaf;
  585. c->up = cs->lists[list-1];
  586. c->gen = 1;
  587. }else if(nleaf >= cs->nleaf){
  588. cs->lists[list + 1] = nil;
  589. return;
  590. }else{
  591. c->leaf = nleaf + 1;
  592. c->count = cs->leafcount[nleaf];
  593. c->up = oc->up;
  594. c->gen = 0;
  595. }
  596. cs->free = c + 1;
  597. cs->lists[list + 1] = c;
  598. c->col = cs->col;
  599. }
  600. static void
  601. hufftabinit(Huff *tab, int n, ulong *bitcount, int nbits)
  602. {
  603. ulong code, nc[MaxHuffBits];
  604. int i, bits;
  605. code = 0;
  606. for(bits = 1; bits <= nbits; bits++){
  607. code = (code + bitcount[bits-1]) << 1;
  608. nc[bits] = code;
  609. }
  610. for(i = 0; i < n; i++){
  611. bits = tab[i].bits;
  612. if(bits > 0)
  613. tab[i].encode = nc[bits]++;
  614. }
  615. }
  616. static int
  617. pivot(ulong *c, int a, int n)
  618. {
  619. int j, pi, pj, pk;
  620. j = n/6;
  621. pi = a + j; /* 1/6 */
  622. j += j;
  623. pj = pi + j; /* 1/2 */
  624. pk = pj + j; /* 5/6 */
  625. if(c[pi] < c[pj]){
  626. if(c[pi] < c[pk]){
  627. if(c[pj] < c[pk])
  628. return pj;
  629. return pk;
  630. }
  631. return pi;
  632. }
  633. if(c[pj] < c[pk]){
  634. if(c[pi] < c[pk])
  635. return pi;
  636. return pk;
  637. }
  638. return pj;
  639. }
  640. static void
  641. leafsort(ulong *leafcount, ushort *leafmap, int a, int n)
  642. {
  643. ulong t;
  644. int j, pi, pj, pn;
  645. while(n > 1){
  646. if(n > 10){
  647. pi = pivot(leafcount, a, n);
  648. }else
  649. pi = a + (n>>1);
  650. t = leafcount[pi];
  651. leafcount[pi] = leafcount[a];
  652. leafcount[a] = t;
  653. t = leafmap[pi];
  654. leafmap[pi] = leafmap[a];
  655. leafmap[a] = t;
  656. pi = a;
  657. pn = a + n;
  658. pj = pn;
  659. for(;;){
  660. do
  661. pi++;
  662. while(pi < pn && (leafcount[pi] < leafcount[a] || leafcount[pi] == leafcount[a] && leafmap[pi] > leafmap[a]));
  663. do
  664. pj--;
  665. while(pj > a && (leafcount[pj] > leafcount[a] || leafcount[pj] == leafcount[a] && leafmap[pj] < leafmap[a]));
  666. if(pj < pi)
  667. break;
  668. t = leafcount[pi];
  669. leafcount[pi] = leafcount[pj];
  670. leafcount[pj] = t;
  671. t = leafmap[pi];
  672. leafmap[pi] = leafmap[pj];
  673. leafmap[pj] = t;
  674. }
  675. t = leafcount[a];
  676. leafcount[a] = leafcount[pj];
  677. leafcount[pj] = t;
  678. t = leafmap[a];
  679. leafmap[a] = leafmap[pj];
  680. leafmap[pj] = t;
  681. j = pj - a;
  682. n = n-j-1;
  683. if(j >= n){
  684. leafsort(leafcount, leafmap, a, j);
  685. a += j+1;
  686. }else{
  687. leafsort(leafcount, leafmap, a + (j+1), n);
  688. n = j;
  689. }
  690. }
  691. }
  692. static void
  693. fatal(char *fmt, ...)
  694. {
  695. char buf[512];
  696. va_list arg;
  697. va_start(arg, fmt);
  698. doprint(buf, buf+sizeof(buf), fmt, arg);
  699. va_end(arg);
  700. longjmp(errjmp, 1);
  701. }