BlockSort.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  * http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied.  See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.
  18.  */
  19. package org.apache.commons.compress.compressors.bzip2;

  20. import java.util.Arrays;
  21. import java.util.BitSet;

  22. /**
  23.  * Encapsulates the Burrows-Wheeler sorting algorithm needed by {@link BZip2CompressorOutputStream}.
  24.  *
  25.  * <p>
  26.  * This class is based on a Java port of Julian Seward's blocksort.c in his libbzip2
  27.  * </p>
  28.  *
  29.  * <p>
  30.  * The Burrows-Wheeler transform is a reversible transform of the original data that is supposed to group similar bytes close to each other. The idea is to sort
  31.  * all permutations of the input and only keep the last byte of each permutation. E.g. for "Commons Compress" you'd get:
  32.  * </p>
  33.  *
  34.  * <pre>
  35.  *  CompressCommons
  36.  * Commons Compress
  37.  * CompressCommons
  38.  * essCommons Compr
  39.  * mmons CompressCo
  40.  * mons CompressCom
  41.  * mpressCommons Co
  42.  * ns CompressCommo
  43.  * ommons CompressC
  44.  * ompressCommons C
  45.  * ons CompressComm
  46.  * pressCommons Com
  47.  * ressCommons Comp
  48.  * s CompressCommon
  49.  * sCommons Compres
  50.  * ssCommons Compre
  51.  * </pre>
  52.  *
  53.  * <p>
  54.  * Which results in a new text "ss romooCCmmpnse", in adition the index of the first line that contained the original text is kept - in this case it is 1. The
  55.  * idea is that in a long English text all permutations that start with "he" are likely suffixes of a "the" and thus they end in "t" leading to a larger block
  56.  * of "t"s that can better be compressed by the subsequent Move-to-Front, run-length und Huffman encoding steps.
  57.  * </p>
  58.  *
  59.  * <p>
  60.  * For more information see for example:
  61.  * </p>
  62.  * <ul>
  63.  * <li><a href="http://www.hpl.hp.com/techreports/Compaq-DEC/SRC-RR-124.pdf">Burrows, M. and Wheeler, D.: A Block-sorting Lossless Data Compression
  64.  * Algorithm</a></li>
  65.  * <li><a href="http://webglimpse.net/pubs/suffix.pdf">Manber, U. and Myers, G.: Suffix arrays: A new method for on-line string searches</a></li>
  66.  * <li><a href="http://www.cs.tufts.edu/~nr/comp150fp/archive/bob-sedgewick/fast-strings.pdf">Bentley, J.L. and Sedgewick, R.: Fast Algorithms for Sorting and
  67.  * Searching Strings</a></li>
  68.  * </ul>
  69.  *
  70.  * @NotThreadSafe
  71.  */
  72. final class BlockSort {

  73.     /*
  74.      * Some of the constructs used in the C code cannot be ported literally to Java - for example macros, unsigned types. Some code has been hand-tuned to
  75.      * improve performance. In order to avoid memory pressure some structures are reused for several blocks and some memory is even shared between sorting and
  76.      * the MTF stage even though either algorithm uses it for its own purpose.
  77.      *
  78.      * Comments preserved from the actual C code are prefixed with "LBZ2:".
  79.      */

  80.     /*
  81.      * 2012-05-20 Stefan Bodewig:
  82.      *
  83.      * This class seems to mix several revisions of libbzip2's code. The mainSort function and those used by it look closer to the 0.9.5 version but show some
  84.      * variations introduced later. At the same time the logic of Compress 1.4 to randomize the block on bad input has been dropped after libbzip2 0.9.0 and
  85.      * replaced by a fallback sorting algorithm.
  86.      *
  87.      * I've added the fallbackSort function of 1.0.6 and tried to integrate it with the existing code without touching too much. I've also removed the now
  88.      * unused randomization code.
  89.      */

  90.     private static final int FTAB_LENGTH = 65537; // 262148 byte

  91.     /*
  92.      * LBZ2: If you are ever unlucky/improbable enough to get a stack overflow whilst sorting, increase the following constant and try again. In practice I have
  93.      * never seen the stack go above 27 elems, so the following limit seems very generous.
  94.      */
  95.     private static final int QSORT_STACK_SIZE = 1000;

  96.     private static final int FALLBACK_QSORT_STACK_SIZE = 100;

  97.     private static final int STACK_SIZE = Math.max(QSORT_STACK_SIZE, FALLBACK_QSORT_STACK_SIZE);

  98.     private static final int FALLBACK_QSORT_SMALL_THRESH = 10;
  99.     /*
  100.      * LBZ2: Knuth's increments seem to work better than Incerpi-Sedgewick here. Possibly because the number of elems to sort is usually small, typically &lt;=
  101.      * 20.
  102.      */
  103.     private static final int[] INCS = { 1, 4, 13, 40, 121, 364, 1093, 3280, 9841, 29524, 88573, 265720, 797161, 2391484 };
  104.     private static final int SMALL_THRESH = 20;

  105.     private static final int DEPTH_THRESH = 10;
  106.     private static final int WORK_FACTOR = 30;
  107.     private static final int SETMASK = 1 << 21;

  108.     private static final int CLEARMASK = ~SETMASK;

  109.     private static int med3(final int a, final int b, final int c) {
  110.         return a < b ? b < c ? b : a < c ? c : a : b > c ? b : a > c ? c : a;
  111.     }

  112.     private static void vswap(final int[] fmap, int p1, int p2, int n) {
  113.         n += p1;
  114.         while (p1 < n) {
  115.             final int t = fmap[p1];
  116.             fmap[p1++] = fmap[p2];
  117.             fmap[p2++] = t;
  118.         }
  119.     }

  120.     /*
  121.      * Used when sorting. If too many long comparisons happen, we stop sorting, and use fallbackSort instead.
  122.      */
  123.     private int workDone;

  124.     private int workLimit;

  125.     private boolean firstAttempt;

  126.     private final int[] stack_ll = new int[STACK_SIZE]; // 4000 byte

  127.     private final int[] stack_hh = new int[STACK_SIZE]; // 4000 byte

  128.     /*---------------------------------------------*/

  129.     /*---------------------------------------------*/
  130.     /*--- LBZ2: Fallback O(N log(N)^2) sorting        ---*/
  131.     /*--- algorithm, for repetitive blocks      ---*/
  132.     /*---------------------------------------------*/

  133.     /*
  134.      * This is the fallback sorting algorithm libbzip2 1.0.6 uses for repetitive or very short inputs.
  135.      *
  136.      * The idea is inspired by Manber-Myers string suffix sorting algorithm. First a bucket sort places each permutation of the block into a bucket based on its
  137.      * first byte. Permutations are represented by pointers to their first character kept in (partially) sorted order inside the array ftab.
  138.      *
  139.      * The next step visits all buckets in order and performs a quicksort on all permutations of the bucket based on the index of the bucket the second byte of
  140.      * the permutation belongs to, thereby forming new buckets. When arrived here the permutations are sorted up to the second character and we have buckets of
  141.      * permutations that are identical up to two characters.
  142.      *
  143.      * Repeat the step of quicksorting each bucket, now based on the bucket holding the sequence of the third and forth character leading to four byte buckets.
  144.      * Repeat this doubling of bucket sizes until all buckets only contain single permutations or the bucket size exceeds the block size.
  145.      *
  146.      * I.e.
  147.      *
  148.      * "abraba" form three buckets for the chars "a", "b", and "r" in the first step with
  149.      *
  150.      * fmap = { 'a:' 5, 3, 0, 'b:' 4, 1, 'r', 2 }
  151.      *
  152.      * when looking at the bucket of "a"s the second characters are in the buckets that start with fmap-index 0 (rolled over), 3 and 3 respectively, forming two
  153.      * new buckets "aa" and "ab", so we get
  154.      *
  155.      * fmap = { 'aa:' 5, 'ab:' 3, 0, 'ba:' 4, 'br': 1, 'ra:' 2 }
  156.      *
  157.      * since the last bucket only contained a single item it didn't have to be sorted at all.
  158.      *
  159.      * There now is just one bucket with more than one permutation that remains to be sorted. For the permutation that starts with index 3 the third and forth
  160.      * char are in bucket 'aa' at index 0 and for the one starting at block index 0 they are in bucket 'ra' with sort index 5. The fully sorted order then
  161.      * becomes.
  162.      *
  163.      * fmap = { 5, 3, 0, 4, 1, 2 }
  164.      */

  165.     private final int[] stack_dd = new int[QSORT_STACK_SIZE]; // 4000 byte

  166.     private final int[] mainSort_runningOrder = new int[256]; // 1024 byte

  167.     private final int[] mainSort_copy = new int[256]; // 1024 byte

  168.     private final boolean[] mainSort_bigDone = new boolean[256]; // 256 byte

  169.     private final int[] ftab = new int[FTAB_LENGTH]; // 262148 byte

  170.     /**
  171.      * Array instance identical to Data's sfmap, both are used only temporarily and indepently, so we do not need to allocate additional memory.
  172.      */
  173.     private final char[] quadrant;

  174.     private int[] eclass;

  175.     /*---------------------------------------------*/

  176.     BlockSort(final BZip2CompressorOutputStream.Data data) {
  177.         this.quadrant = data.sfmap;
  178.     }

  179.     void blockSort(final BZip2CompressorOutputStream.Data data, final int last) {
  180.         this.workLimit = WORK_FACTOR * last;
  181.         this.workDone = 0;
  182.         this.firstAttempt = true;

  183.         if (last + 1 < 10000) {
  184.             fallbackSort(data, last);
  185.         } else {
  186.             mainSort(data, last);

  187.             if (this.firstAttempt && this.workDone > this.workLimit) {
  188.                 fallbackSort(data, last);
  189.             }
  190.         }

  191.         final int[] fmap = data.fmap;
  192.         data.origPtr = -1;
  193.         for (int i = 0; i <= last; i++) {
  194.             if (fmap[i] == 0) {
  195.                 data.origPtr = i;
  196.                 break;
  197.             }
  198.         }

  199.         // assert (data.origPtr != -1) : data.origPtr;
  200.     }

  201.     /*
  202.      * The C code uses an array of ints (each int holding 32 flags) to represents the bucket-start flags (bhtab). It also contains optimizations to skip over 32
  203.      * consecutively set or consecutively unset bits on word boundaries at once. For now I've chosen to use the simpler but potentially slower code using BitSet
  204.      * - also in the hope that using the BitSet#nextXXX methods may be fast enough.
  205.      */

  206.     /**
  207.      * @param fmap   points to the index of the starting point of a permutation inside the block of data in the current partially sorted order
  208.      * @param eclass points from the index of a character inside the block to the first index in fmap that contains the bucket of its suffix that is sorted in
  209.      *               this step.
  210.      * @param loSt   lower boundary of the fmap-interval to be sorted
  211.      * @param hiSt   upper boundary of the fmap-interval to be sorted
  212.      */
  213.     private void fallbackQSort3(final int[] fmap, final int[] eclass, final int loSt, final int hiSt) {
  214.         int lo, unLo, ltLo, hi, unHi, gtHi, n;

  215.         long r = 0;
  216.         int sp = 0;
  217.         fpush(sp++, loSt, hiSt);

  218.         while (sp > 0) {
  219.             final int[] s = fpop(--sp);
  220.             lo = s[0];
  221.             hi = s[1];

  222.             if (hi - lo < FALLBACK_QSORT_SMALL_THRESH) {
  223.                 fallbackSimpleSort(fmap, eclass, lo, hi);
  224.                 continue;
  225.             }

  226.             /*
  227.              * LBZ2: Random partitioning. Median of 3 sometimes fails to avoid bad cases. Median of 9 seems to help but looks rather expensive. This too seems
  228.              * to work but is cheaper. Guidance for the magic constants 7621 and 32768 is taken from Sedgewick's algorithms book, chapter 35.
  229.              */
  230.             r = (r * 7621 + 1) % 32768;
  231.             final long r3 = r % 3;
  232.             final long med;
  233.             if (r3 == 0) {
  234.                 med = eclass[fmap[lo]];
  235.             } else if (r3 == 1) {
  236.                 med = eclass[fmap[lo + hi >>> 1]];
  237.             } else {
  238.                 med = eclass[fmap[hi]];
  239.             }

  240.             unLo = ltLo = lo;
  241.             unHi = gtHi = hi;

  242.             // looks like the ternary partition attributed to Wegner
  243.             // in the cited Sedgewick paper
  244.             while (true) {
  245.                 while (true) {
  246.                     if (unLo > unHi) {
  247.                         break;
  248.                     }
  249.                     n = eclass[fmap[unLo]] - (int) med;
  250.                     if (n == 0) {
  251.                         fswap(fmap, unLo, ltLo);
  252.                         ltLo++;
  253.                         unLo++;
  254.                         continue;
  255.                     }
  256.                     if (n > 0) {
  257.                         break;
  258.                     }
  259.                     unLo++;
  260.                 }
  261.                 while (true) {
  262.                     if (unLo > unHi) {
  263.                         break;
  264.                     }
  265.                     n = eclass[fmap[unHi]] - (int) med;
  266.                     if (n == 0) {
  267.                         fswap(fmap, unHi, gtHi);
  268.                         gtHi--;
  269.                         unHi--;
  270.                         continue;
  271.                     }
  272.                     if (n < 0) {
  273.                         break;
  274.                     }
  275.                     unHi--;
  276.                 }
  277.                 if (unLo > unHi) {
  278.                     break;
  279.                 }
  280.                 fswap(fmap, unLo, unHi);
  281.                 unLo++;
  282.                 unHi--;
  283.             }

  284.             if (gtHi < ltLo) {
  285.                 continue;
  286.             }

  287.             n = Math.min(ltLo - lo, unLo - ltLo);
  288.             fvswap(fmap, lo, unLo - n, n);
  289.             int m = Math.min(hi - gtHi, gtHi - unHi);
  290.             fvswap(fmap, unHi + 1, hi - m + 1, m);

  291.             n = lo + unLo - ltLo - 1;
  292.             m = hi - (gtHi - unHi) + 1;

  293.             if (n - lo > hi - m) {
  294.                 fpush(sp++, lo, n);
  295.                 fpush(sp++, m, hi);
  296.             } else {
  297.                 fpush(sp++, m, hi);
  298.                 fpush(sp++, lo, n);
  299.             }
  300.         }
  301.     }

  302.     /*---------------------------------------------*/

  303.     /**
  304.      * @param fmap   points to the index of the starting point of a permutation inside the block of data in the current partially sorted order
  305.      * @param eclass points from the index of a character inside the block to the first index in fmap that contains the bucket of its suffix that is sorted in
  306.      *               this step.
  307.      * @param lo     lower boundary of the fmap-interval to be sorted
  308.      * @param hi     upper boundary of the fmap-interval to be sorted
  309.      */
  310.     private void fallbackSimpleSort(final int[] fmap, final int[] eclass, final int lo, final int hi) {
  311.         if (lo == hi) {
  312.             return;
  313.         }

  314.         int j;
  315.         if (hi - lo > 3) {
  316.             for (int i = hi - 4; i >= lo; i--) {
  317.                 final int tmp = fmap[i];
  318.                 final int ec_tmp = eclass[tmp];
  319.                 for (j = i + 4; j <= hi && ec_tmp > eclass[fmap[j]]; j += 4) {
  320.                     fmap[j - 4] = fmap[j];
  321.                 }
  322.                 fmap[j - 4] = tmp;
  323.             }
  324.         }

  325.         for (int i = hi - 1; i >= lo; i--) {
  326.             final int tmp = fmap[i];
  327.             final int ec_tmp = eclass[tmp];
  328.             for (j = i + 1; j <= hi && ec_tmp > eclass[fmap[j]]; j++) {
  329.                 fmap[j - 1] = fmap[j];
  330.             }
  331.             fmap[j - 1] = tmp;
  332.         }
  333.     }

  334.     /**
  335.      * Adapt fallbackSort to the expected interface of the rest of the code, in particular deal with the fact that block starts at offset 1 (in libbzip2 1.0.6
  336.      * it starts at 0).
  337.      */
  338.     void fallbackSort(final BZip2CompressorOutputStream.Data data, final int last) {
  339.         data.block[0] = data.block[last + 1];
  340.         fallbackSort(data.fmap, data.block, last + 1);
  341.         for (int i = 0; i < last + 1; i++) {
  342.             --data.fmap[i];
  343.         }
  344.         for (int i = 0; i < last + 1; i++) {
  345.             if (data.fmap[i] == -1) {
  346.                 data.fmap[i] = last;
  347.                 break;
  348.             }
  349.         }
  350.     }

  351.     /*--
  352.        LBZ2: The following is an implementation of
  353.        an elegant 3-way quicksort for strings,
  354.        described in a paper "Fast Algorithms for
  355.        Sorting and Searching Strings", by Robert
  356.        Sedgewick and Jon L. Bentley.
  357.     --*/

  358.     /**
  359.      * @param fmap   points to the index of the starting point of a permutation inside the block of data in the current partially sorted order
  360.      * @param block  the original data
  361.      * @param nblock size of the block
  362.      */
  363.     void fallbackSort(final int[] fmap, final byte[] block, final int nblock) {
  364.         final int[] ftab = new int[257];
  365.         int H, i, j, k, l, r, cc, cc1;
  366.         int nNotDone;
  367.         final int nBhtab;
  368.         final int[] eclass = getEclass();

  369.         for (i = 0; i < nblock; i++) {
  370.             eclass[i] = 0;
  371.         }
  372.         /*--
  373.           LBZ2: Initial 1-char radix sort to generate
  374.           initial fmap and initial BH bits.
  375.           --*/
  376.         for (i = 0; i < nblock; i++) {
  377.             ftab[block[i] & 0xff]++;
  378.         }
  379.         for (i = 1; i < 257; i++) {
  380.             ftab[i] += ftab[i - 1];
  381.         }

  382.         for (i = 0; i < nblock; i++) {
  383.             j = block[i] & 0xff;
  384.             k = ftab[j] - 1;
  385.             ftab[j] = k;
  386.             fmap[k] = i;
  387.         }

  388.         nBhtab = 64 + nblock;
  389.         final BitSet bhtab = new BitSet(nBhtab);
  390.         for (i = 0; i < 256; i++) {
  391.             bhtab.set(ftab[i]);
  392.         }

  393.         /*--
  394.           LBZ2: Inductively refine the buckets.  Kind-of an
  395.           "exponential radix sort" (!), inspired by the
  396.           Manber-Myers suffix array construction algorithm.
  397.           --*/

  398.         /*-- LBZ2: set sentinel bits for block-end detection --*/
  399.         for (i = 0; i < 32; i++) {
  400.             bhtab.set(nblock + 2 * i);
  401.             bhtab.clear(nblock + 2 * i + 1);
  402.         }

  403.         /*-- LBZ2: the log(N) loop --*/
  404.         H = 1;
  405.         while (true) {

  406.             j = 0;
  407.             for (i = 0; i < nblock; i++) {
  408.                 if (bhtab.get(i)) {
  409.                     j = i;
  410.                 }
  411.                 k = fmap[i] - H;
  412.                 if (k < 0) {
  413.                     k += nblock;
  414.                 }
  415.                 eclass[k] = j;
  416.             }

  417.             nNotDone = 0;
  418.             r = -1;
  419.             while (true) {

  420.                 /*-- LBZ2: find the next non-singleton bucket --*/
  421.                 k = r + 1;
  422.                 k = bhtab.nextClearBit(k);
  423.                 l = k - 1;
  424.                 if (l >= nblock) {
  425.                     break;
  426.                 }
  427.                 k = bhtab.nextSetBit(k + 1);
  428.                 r = k - 1;
  429.                 if (r >= nblock) {
  430.                     break;
  431.                 }

  432.                 /*-- LBZ2: now [l, r] bracket current bucket --*/
  433.                 if (r > l) {
  434.                     nNotDone += r - l + 1;
  435.                     fallbackQSort3(fmap, eclass, l, r);

  436.                     /*-- LBZ2: scan bucket and generate header bits-- */
  437.                     cc = -1;
  438.                     for (i = l; i <= r; i++) {
  439.                         cc1 = eclass[fmap[i]];
  440.                         if (cc != cc1) {
  441.                             bhtab.set(i);
  442.                             cc = cc1;
  443.                         }
  444.                     }
  445.                 }
  446.             }

  447.             H *= 2;
  448.             if (H > nblock || nNotDone == 0) {
  449.                 break;
  450.             }
  451.         }
  452.     }

  453.     private int[] fpop(final int sp) {
  454.         return new int[] { stack_ll[sp], stack_hh[sp] };
  455.     }

  456.     private void fpush(final int sp, final int lz, final int hz) {
  457.         stack_ll[sp] = lz;
  458.         stack_hh[sp] = hz;
  459.     }

  460.     /**
  461.      * swaps two values in fmap
  462.      */
  463.     private void fswap(final int[] fmap, final int zz1, final int zz2) {
  464.         final int zztmp = fmap[zz1];
  465.         fmap[zz1] = fmap[zz2];
  466.         fmap[zz2] = zztmp;
  467.     }

  468.     /**
  469.      * swaps two intervals starting at yyp1 and yyp2 of length yyn inside fmap.
  470.      */
  471.     private void fvswap(final int[] fmap, int yyp1, int yyp2, int yyn) {
  472.         while (yyn > 0) {
  473.             fswap(fmap, yyp1, yyp2);
  474.             yyp1++;
  475.             yyp2++;
  476.             yyn--;
  477.         }
  478.     }

  479.     private int[] getEclass() {
  480.         if (eclass == null) {
  481.             eclass = new int[quadrant.length / 2];
  482.         }
  483.         return eclass;
  484.     }

  485.     /**
  486.      * Method "mainQSort3", file "blocksort.c", BZip2 1.0.2
  487.      */
  488.     private void mainQSort3(final BZip2CompressorOutputStream.Data dataShadow, final int loSt, final int hiSt, final int dSt, final int last) {
  489.         final int[] stack_ll = this.stack_ll;
  490.         final int[] stack_hh = this.stack_hh;
  491.         final int[] stack_dd = this.stack_dd;
  492.         final int[] fmap = dataShadow.fmap;
  493.         final byte[] block = dataShadow.block;

  494.         stack_ll[0] = loSt;
  495.         stack_hh[0] = hiSt;
  496.         stack_dd[0] = dSt;

  497.         for (int sp = 1; --sp >= 0;) {
  498.             final int lo = stack_ll[sp];
  499.             final int hi = stack_hh[sp];
  500.             final int d = stack_dd[sp];

  501.             if (hi - lo < SMALL_THRESH || d > DEPTH_THRESH) {
  502.                 if (mainSimpleSort(dataShadow, lo, hi, d, last)) {
  503.                     return;
  504.                 }
  505.             } else {
  506.                 final int d1 = d + 1;
  507.                 final int med = med3(block[fmap[lo] + d1] & 0xff, block[fmap[hi] + d1] & 0xff, block[fmap[lo + hi >>> 1] + d1] & 0xff);

  508.                 int unLo = lo;
  509.                 int unHi = hi;
  510.                 int ltLo = lo;
  511.                 int gtHi = hi;

  512.                 while (true) {
  513.                     while (unLo <= unHi) {
  514.                         final int n = (block[fmap[unLo] + d1] & 0xff) - med;
  515.                         if (n == 0) {
  516.                             final int temp = fmap[unLo];
  517.                             fmap[unLo++] = fmap[ltLo];
  518.                             fmap[ltLo++] = temp;
  519.                         } else if (n < 0) {
  520.                             unLo++;
  521.                         } else {
  522.                             break;
  523.                         }
  524.                     }

  525.                     while (unLo <= unHi) {
  526.                         final int n = (block[fmap[unHi] + d1] & 0xff) - med;
  527.                         if (n == 0) {
  528.                             final int temp = fmap[unHi];
  529.                             fmap[unHi--] = fmap[gtHi];
  530.                             fmap[gtHi--] = temp;
  531.                         } else if (n > 0) {
  532.                             unHi--;
  533.                         } else {
  534.                             break;
  535.                         }
  536.                     }

  537.                     if (unLo > unHi) {
  538.                         break;
  539.                     }
  540.                     final int temp = fmap[unLo];
  541.                     fmap[unLo++] = fmap[unHi];
  542.                     fmap[unHi--] = temp;
  543.                 }

  544.                 if (gtHi < ltLo) {
  545.                     stack_ll[sp] = lo;
  546.                     stack_hh[sp] = hi;
  547.                     stack_dd[sp] = d1;
  548.                 } else {
  549.                     int n = Math.min(ltLo - lo, unLo - ltLo);
  550.                     vswap(fmap, lo, unLo - n, n);
  551.                     int m = Math.min(hi - gtHi, gtHi - unHi);
  552.                     vswap(fmap, unLo, hi - m + 1, m);

  553.                     n = lo + unLo - ltLo - 1;
  554.                     m = hi - (gtHi - unHi) + 1;

  555.                     stack_ll[sp] = lo;
  556.                     stack_hh[sp] = n;
  557.                     stack_dd[sp] = d;
  558.                     sp++;

  559.                     stack_ll[sp] = n + 1;
  560.                     stack_hh[sp] = m - 1;
  561.                     stack_dd[sp] = d1;
  562.                     sp++;

  563.                     stack_ll[sp] = m;
  564.                     stack_hh[sp] = hi;
  565.                     stack_dd[sp] = d;
  566.                 }
  567.                 sp++;
  568.             }
  569.         }
  570.     }

  571.     /**
  572.      * This is the most hammered method of this class.
  573.      *
  574.      * <p>
  575.      * This is the version using unrolled loops. Normally I never use such ones in Java code. The unrolling has shown a noticeable performance improvement on
  576.      * JRE 1.4.2 (Linux i586 / HotSpot Client). Of course it depends on the JIT compiler of the vm.
  577.      * </p>
  578.      */
  579.     private boolean mainSimpleSort(final BZip2CompressorOutputStream.Data dataShadow, final int lo, final int hi, final int d, final int lastShadow) {
  580.         final int bigN = hi - lo + 1;
  581.         if (bigN < 2) {
  582.             return this.firstAttempt && this.workDone > this.workLimit;
  583.         }

  584.         int hp = 0;
  585.         while (INCS[hp] < bigN) {
  586.             hp++;
  587.         }

  588.         final int[] fmap = dataShadow.fmap;
  589.         final char[] quadrant = this.quadrant;
  590.         final byte[] block = dataShadow.block;
  591.         final int lastPlus1 = lastShadow + 1;
  592.         final boolean firstAttemptShadow = this.firstAttempt;
  593.         final int workLimitShadow = this.workLimit;
  594.         int workDoneShadow = this.workDone;

  595.         // Following block contains unrolled code which could be shortened by
  596.         // coding it in additional loops.

  597.         HP: while (--hp >= 0) {
  598.             final int h = INCS[hp];
  599.             final int mj = lo + h - 1;

  600.             for (int i = lo + h; i <= hi;) {
  601.                 // copy
  602.                 for (int k = 3; i <= hi && --k >= 0; i++) {
  603.                     final int v = fmap[i];
  604.                     final int vd = v + d;
  605.                     int j = i;

  606.                     // for (int a;
  607.                     // (j > mj) && mainGtU((a = fmap[j - h]) + d, vd,
  608.                     // block, quadrant, lastShadow);
  609.                     // j -= h) {
  610.                     // fmap[j] = a;
  611.                     // }
  612.                     //
  613.                     // unrolled version:

  614.                     // start inline mainGTU
  615.                     boolean onceRunned = false;
  616.                     int a = 0;

  617.                     HAMMER: while (true) {
  618.                         if (onceRunned) {
  619.                             fmap[j] = a;
  620.                             if ((j -= h) <= mj) { // NOSONAR
  621.                                 break HAMMER;
  622.                             }
  623.                         } else {
  624.                             onceRunned = true;
  625.                         }

  626.                         a = fmap[j - h];
  627.                         int i1 = a + d;
  628.                         int i2 = vd;

  629.                         // following could be done in a loop, but
  630.                         // unrolled it for performance:
  631.                         if (block[i1 + 1] == block[i2 + 1]) {
  632.                             if (block[i1 + 2] == block[i2 + 2]) {
  633.                                 if (block[i1 + 3] == block[i2 + 3]) {
  634.                                     if (block[i1 + 4] == block[i2 + 4]) {
  635.                                         if (block[i1 + 5] == block[i2 + 5]) {
  636.                                             if (block[i1 += 6] == block[i2 += 6]) { // NOSONAR
  637.                                                 int x = lastShadow;
  638.                                                 X: while (x > 0) {
  639.                                                     x -= 4;
  640.                                                     if (block[i1 + 1] == block[i2 + 1]) {
  641.                                                         if (quadrant[i1] == quadrant[i2]) {
  642.                                                             if (block[i1 + 2] == block[i2 + 2]) {
  643.                                                                 if (quadrant[i1 + 1] == quadrant[i2 + 1]) {
  644.                                                                     if (block[i1 + 3] == block[i2 + 3]) {
  645.                                                                         if (quadrant[i1 + 2] == quadrant[i2 + 2]) {
  646.                                                                             if (block[i1 + 4] == block[i2 + 4]) {
  647.                                                                                 if (quadrant[i1 + 3] == quadrant[i2 + 3]) {
  648.                                                                                     if ((i1 += 4) >= lastPlus1) { // NOSONAR
  649.                                                                                         i1 -= lastPlus1;
  650.                                                                                     }
  651.                                                                                     if ((i2 += 4) >= lastPlus1) { // NOSONAR
  652.                                                                                         i2 -= lastPlus1;
  653.                                                                                     }
  654.                                                                                     workDoneShadow++;
  655.                                                                                     continue X;
  656.                                                                                 }
  657.                                                                                 if (quadrant[i1 + 3] > quadrant[i2 + 3]) {
  658.                                                                                     continue HAMMER;
  659.                                                                                 }
  660.                                                                                 break HAMMER;
  661.                                                                             }
  662.                                                                             if ((block[i1 + 4] & 0xff) > (block[i2 + 4] & 0xff)) {
  663.                                                                                 continue HAMMER;
  664.                                                                             }
  665.                                                                             break HAMMER;
  666.                                                                         }
  667.                                                                         if (quadrant[i1 + 2] > quadrant[i2 + 2]) {
  668.                                                                             continue HAMMER;
  669.                                                                         }
  670.                                                                         break HAMMER;
  671.                                                                     }
  672.                                                                     if ((block[i1 + 3] & 0xff) > (block[i2 + 3] & 0xff)) {
  673.                                                                         continue HAMMER;
  674.                                                                     }
  675.                                                                     break HAMMER;
  676.                                                                 }
  677.                                                                 if (quadrant[i1 + 1] > quadrant[i2 + 1]) {
  678.                                                                     continue HAMMER;
  679.                                                                 }
  680.                                                                 break HAMMER;
  681.                                                             }
  682.                                                             if ((block[i1 + 2] & 0xff) > (block[i2 + 2] & 0xff)) {
  683.                                                                 continue HAMMER;
  684.                                                             }
  685.                                                             break HAMMER;
  686.                                                         }
  687.                                                         if (quadrant[i1] > quadrant[i2]) {
  688.                                                             continue HAMMER;
  689.                                                         }
  690.                                                         break HAMMER;
  691.                                                     }
  692.                                                     if ((block[i1 + 1] & 0xff) > (block[i2 + 1] & 0xff)) {
  693.                                                         continue HAMMER;
  694.                                                     }
  695.                                                     break HAMMER;

  696.                                                 }
  697.                                                 break HAMMER;
  698.                                             } // while x > 0
  699.                                             if ((block[i1] & 0xff) > (block[i2] & 0xff)) {
  700.                                                 continue HAMMER;
  701.                                             }
  702.                                             break HAMMER;
  703.                                         }
  704.                                         if ((block[i1 + 5] & 0xff) > (block[i2 + 5] & 0xff)) {
  705.                                             continue HAMMER;
  706.                                         }
  707.                                         break HAMMER;
  708.                                     }
  709.                                     if ((block[i1 + 4] & 0xff) > (block[i2 + 4] & 0xff)) {
  710.                                         continue HAMMER;
  711.                                     }
  712.                                     break HAMMER;
  713.                                 }
  714.                                 if ((block[i1 + 3] & 0xff) > (block[i2 + 3] & 0xff)) {
  715.                                     continue HAMMER;
  716.                                 }
  717.                                 break HAMMER;
  718.                             }
  719.                             if ((block[i1 + 2] & 0xff) > (block[i2 + 2] & 0xff)) {
  720.                                 continue HAMMER;
  721.                             }
  722.                             break HAMMER;
  723.                         }
  724.                         if ((block[i1 + 1] & 0xff) > (block[i2 + 1] & 0xff)) {
  725.                             continue HAMMER;
  726.                         }
  727.                         break HAMMER;

  728.                     } // HAMMER
  729.                       // end inline mainGTU

  730.                     fmap[j] = v;
  731.                 }

  732.                 if (firstAttemptShadow && i <= hi && workDoneShadow > workLimitShadow) {
  733.                     break HP;
  734.                 }
  735.             }
  736.         }

  737.         this.workDone = workDoneShadow;
  738.         return firstAttemptShadow && workDoneShadow > workLimitShadow;
  739.     }

  740.     void mainSort(final BZip2CompressorOutputStream.Data dataShadow, final int lastShadow) {
  741.         final int[] runningOrder = this.mainSort_runningOrder;
  742.         final int[] copy = this.mainSort_copy;
  743.         final boolean[] bigDone = this.mainSort_bigDone;
  744.         final int[] ftab = this.ftab;
  745.         final byte[] block = dataShadow.block;
  746.         final int[] fmap = dataShadow.fmap;
  747.         final char[] quadrant = this.quadrant;
  748.         final int workLimitShadow = this.workLimit;
  749.         final boolean firstAttemptShadow = this.firstAttempt;

  750.         // LBZ2: Set up the 2-byte frequency table
  751.         Arrays.fill(ftab, 0);

  752.         /*
  753.          * In the various block-sized structures, live data runs from 0 to last+NUM_OVERSHOOT_BYTES inclusive. First, set up the overshoot area for block.
  754.          */
  755.         for (int i = 0; i < BZip2Constants.NUM_OVERSHOOT_BYTES; i++) {
  756.             block[lastShadow + i + 2] = block[i % (lastShadow + 1) + 1];
  757.         }
  758.         for (int i = lastShadow + BZip2Constants.NUM_OVERSHOOT_BYTES + 1; --i >= 0;) {
  759.             quadrant[i] = 0;
  760.         }
  761.         block[0] = block[lastShadow + 1];

  762.         // LBZ2: Complete the initial radix sort:

  763.         int c1 = block[0] & 0xff;
  764.         for (int i = 0; i <= lastShadow; i++) {
  765.             final int c2 = block[i + 1] & 0xff;
  766.             ftab[(c1 << 8) + c2]++;
  767.             c1 = c2;
  768.         }

  769.         for (int i = 1; i <= 65536; i++) {
  770.             ftab[i] += ftab[i - 1];
  771.         }

  772.         c1 = block[1] & 0xff;
  773.         for (int i = 0; i < lastShadow; i++) {
  774.             final int c2 = block[i + 2] & 0xff;
  775.             fmap[--ftab[(c1 << 8) + c2]] = i;
  776.             c1 = c2;
  777.         }

  778.         fmap[--ftab[((block[lastShadow + 1] & 0xff) << 8) + (block[1] & 0xff)]] = lastShadow;

  779.         /*
  780.          * LBZ2: Now ftab contains the first loc of every small bucket. Calculate the running order, from smallest to largest big bucket.
  781.          */
  782.         for (int i = 256; --i >= 0;) {
  783.             bigDone[i] = false;
  784.             runningOrder[i] = i;
  785.         }

  786.         // h = 364, 121, 40, 13, 4, 1
  787.         for (int h = 364; h != 1;) { // NOSONAR
  788.             h /= 3;
  789.             for (int i = h; i <= 255; i++) {
  790.                 final int vv = runningOrder[i];
  791.                 final int a = ftab[vv + 1 << 8] - ftab[vv << 8];
  792.                 final int b = h - 1;
  793.                 int j = i;
  794.                 for (int ro = runningOrder[j - h]; ftab[ro + 1 << 8] - ftab[ro << 8] > a; ro = runningOrder[j - h]) {
  795.                     runningOrder[j] = ro;
  796.                     j -= h;
  797.                     if (j <= b) {
  798.                         break;
  799.                     }
  800.                 }
  801.                 runningOrder[j] = vv;
  802.             }
  803.         }

  804.         /*
  805.          * LBZ2: The main sorting loop.
  806.          */
  807.         for (int i = 0; i <= 255; i++) {
  808.             /*
  809.              * LBZ2: Process big buckets, starting with the least full.
  810.              */
  811.             final int ss = runningOrder[i];

  812.             // Step 1:
  813.             /*
  814.              * LBZ2: Complete the big bucket [ss] by quicksorting any unsorted small buckets [ss, j]. Hopefully previous pointer-scanning phases have already
  815.              * completed many of the small buckets [ss, j], so we don't have to sort them at all.
  816.              */
  817.             for (int j = 0; j <= 255; j++) {
  818.                 final int sb = (ss << 8) + j;
  819.                 final int ftab_sb = ftab[sb];
  820.                 if ((ftab_sb & SETMASK) != SETMASK) {
  821.                     final int lo = ftab_sb & CLEARMASK;
  822.                     final int hi = (ftab[sb + 1] & CLEARMASK) - 1;
  823.                     if (hi > lo) {
  824.                         mainQSort3(dataShadow, lo, hi, 2, lastShadow);
  825.                         if (firstAttemptShadow && this.workDone > workLimitShadow) {
  826.                             return;
  827.                         }
  828.                     }
  829.                     ftab[sb] = ftab_sb | SETMASK;
  830.                 }
  831.             }

  832.             // Step 2:
  833.             // LBZ2: Now scan this big bucket so as to synthesise the
  834.             // sorted order for small buckets [t, ss] for all t != ss.

  835.             for (int j = 0; j <= 255; j++) {
  836.                 copy[j] = ftab[(j << 8) + ss] & CLEARMASK;
  837.             }

  838.             for (int j = ftab[ss << 8] & CLEARMASK, hj = ftab[ss + 1 << 8] & CLEARMASK; j < hj; j++) {
  839.                 final int fmap_j = fmap[j];
  840.                 c1 = block[fmap_j] & 0xff;
  841.                 if (!bigDone[c1]) {
  842.                     fmap[copy[c1]] = fmap_j == 0 ? lastShadow : fmap_j - 1;
  843.                     copy[c1]++;
  844.                 }
  845.             }

  846.             for (int j = 256; --j >= 0;) {
  847.                 ftab[(j << 8) + ss] |= SETMASK;
  848.             }

  849.             // Step 3:
  850.             /*
  851.              * LBZ2: The ss big bucket is now done. Record this fact, and update the quadrant descriptors. Remember to update quadrants in the overshoot area
  852.              * too, if necessary. The "if (i < 255)" test merely skips this updating for the last bucket processed, since updating for the last bucket is
  853.              * pointless.
  854.              */
  855.             bigDone[ss] = true;

  856.             if (i < 255) {
  857.                 final int bbStart = ftab[ss << 8] & CLEARMASK;
  858.                 final int bbSize = (ftab[ss + 1 << 8] & CLEARMASK) - bbStart;
  859.                 int shifts = 0;

  860.                 while (bbSize >> shifts > 65534) {
  861.                     shifts++;
  862.                 }

  863.                 for (int j = 0; j < bbSize; j++) {
  864.                     final int a2update = fmap[bbStart + j];
  865.                     final char qVal = (char) (j >> shifts);
  866.                     quadrant[a2update] = qVal;
  867.                     if (a2update < BZip2Constants.NUM_OVERSHOOT_BYTES) {
  868.                         quadrant[a2update + lastShadow + 1] = qVal;
  869.                     }
  870.                 }
  871.             }

  872.         }
  873.     }

  874. }