#pragma prototyped
/*-------------------------------------------------------------*/
/*--- Block sorting machinery ---*/
/*--- blocksort.c ---*/
/*-------------------------------------------------------------*/
/*--
library for lossless, block-sorting data compression.
Copyright (C) 1996-1998 Julian R Seward. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product
documentation would be appreciated but is not required.
3. Altered source versions must be plainly marked as such, and must
not be misrepresented as being the original software.
4. The name of the author may not be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Julian Seward, Guildford, Surrey, UK.
jseward@acm.org
This program is based on (at least) the work of:
Mike Burrows
David Wheeler
Peter Fenwick
Alistair Moffat
Radford Neal
Ian H. Witten
Robert Sedgewick
Jon L. Bentley
For more information on these sources, see the manual.
--*/
#include "bzhdr.h"
/*---------------------------------------------*/
/*--
Compare two strings in block. We assume (see
discussion above) that i1 and i2 have a max
offset of 10 on entry, and that the first
bytes of both block and quadrant have been
copied into the "overshoot area", ie
into the subscript range
[nblock .. nblock+NUM_OVERSHOOT_BYTES-1].
--*/
)
{
Int32 k;
k = nblock;
do {
k -= 4;
(*workDone)++;
}
while (k >= 0);
return False;
}
/*---------------------------------------------*/
/*--
Knuth's increments seem to work better
than Incerpi-Sedgewick here. Possibly
because the number of elems to sort is
usually small, typically <= 20.
--*/
9841, 29524, 88573, 265720,
797161, 2391484 };
{
Int32 v;
if (bigN < 2) return;
hp = 0;
hp--;
i = lo + h;
while (True) {
/*-- copy 1 --*/
if (i > hi) break;
v = zptr[i];
j = i;
zptr[j-h]+d, v+d ) ) {
j = j - h;
if (j <= (lo + h - 1)) break;
}
zptr[j] = v;
i++;
/*-- copy 2 --*/
if (i > hi) break;
v = zptr[i];
j = i;
zptr[j-h]+d, v+d ) ) {
j = j - h;
if (j <= (lo + h - 1)) break;
}
zptr[j] = v;
i++;
/*-- copy 3 --*/
if (i > hi) break;
v = zptr[i];
j = i;
zptr[j-h]+d, v+d ) ) {
j = j - h;
if (j <= (lo + h - 1)) break;
}
zptr[j] = v;
i++;
}
}
}
/*---------------------------------------------*/
/*--
The following is an implementation of
an elegant 3-way quicksort for strings,
described in a paper "Fast Algorithms for
Sorting and Searching Strings", by Robert
Sedgewick and Jon L. Bentley.
--*/
{
while (n > 0) {
}
}
{
UChar t;
if (a > b) { t = a; a = b; b = t; };
if (b > c) { t = b; b = c; c = t; };
if (a > b) b = a;
return b;
}
#define min(a,b) ((a) < (b)) ? (a) : (b)
typedef
sp++; }
/*--
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 never seen the
stack go above 27 elems, so the following
limit seems very generous.
--*/
{
sp = 0;
while (sp > 0) {
continue;
}
while (True) {
while (True) {
if (n > 0) break;
unLo++;
}
while (True) {
if (n < 0) break;
unHi--;
}
}
continue;
}
}
}
/*---------------------------------------------*/
{
/*--
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.
--*/
if (s->verbosity >= 4)
VPrintf0( " sort initialise ...\n" );
for (i = 0; i < BZ_NUM_OVERSHOOT_BYTES; i++)
for (i = 0; i < nblock+BZ_NUM_OVERSHOOT_BYTES; i++)
quadrant[i] = 0;
if (nblock <= 4000) {
/*--
Use simpleSort(), since the full sorting mechanism
has quite a large constant overhead.
--*/
} else {
numQSorted = 0;
for (i = 0; i <= 65536; i++) ftab[i] = 0;
for (i = 0; i < nblock; i++) {
}
for (i = 0; i < nblock-1; i++) {
ftab[j]--;
}
ftab[j]--;
/*--
Now ftab contains the first loc of every small bucket.
Calculate the running order, from smallest to largest
big bucket.
--*/
for (i = 0; i <= 255; i++) runningOrder[i] = i;
{
Int32 h = 1;
do h = 3 * h + 1; while (h <= 256);
do {
h = h / 3;
for (i = h; i <= 255; i++) {
vv = runningOrder[i];
j = i;
runningOrder[j] = runningOrder[j-h];
j = j - h;
if (j <= (h - 1)) goto zero;
}
zero:
runningOrder[j] = vv;
}
} while (h != 1);
}
/*--
The main sorting loop.
--*/
for (i = 0; i <= 255; i++) {
/*--
Process big buckets, starting with the least full.
Basically this is a 4-step process in which we call
qSort3 to sort the small buckets [ss, j], but
also make a big effort to avoid the calls if we can.
--*/
ss = runningOrder[i];
/*--
Step 1:
Complete the big bucket [ss] by quicksorting
any unsorted small buckets [ss, j], for j != ss.
Hopefully previous pointer-scanning phases have already
completed many of the small buckets [ss, j], so
we don't have to sort them at all.
--*/
for (j = 0; j <= 255; j++) {
if (j != ss) {
if (s->verbosity >= 4)
VPrintf4( " qsort [0x%x, 0x%x] done %d this %d\n",
}
}
}
}
/*--
Step 2:
Deal specially with case [ss, ss]. This establishes the
sorted order for [ss, ss] without any comparisons.
A clever trick, cryptically described as steps Q6b and Q6c
in SRC-124 (aka BW94). This makes it entirely practical to
not use a preliminary run-length coder, but unfortunately
we are now stuck with the .bz2 file format.
--*/
{
get0++;
}
get1--;
}
}
/*--
Step 3:
The [ss] big bucket is now done. Record this fact,
and update the quadrant descriptors. Remember to
update quadrants in the overshoot area too, if
necessary. The "if (i < 255)" test merely skips
this updating for the last bucket processed, since
updating for the last bucket is pointless.
The quadrant array provides a way to incrementally
cache sort orderings, as they appear, so as to
make subsequent comparisons in fullGtU() complete
faster. For repetitive blocks this makes a big
difference (but not big enough to be able to avoid
randomisation for very repetitive data.)
The precise meaning is: at all times:
for 0 <= i < nblock and 0 <= j <= nblock
if block[i] != block[j],
then the relative values of quadrant[i] and
quadrant[j] are meaningless.
else {
if quadrant[i] < quadrant[j]
then the string starting at i lexicographically
precedes the string starting at j
else if quadrant[i] > quadrant[j]
then the string starting at j lexicographically
precedes the string starting at i
else
the relative ordering of the strings starting
at i and j has not yet been determined.
}
--*/
if (i < 255) {
for (j = 0; j < bbSize; j++) {
if (a2update < BZ_NUM_OVERSHOOT_BYTES)
}
}
/*--
Step 4:
Now scan this big bucket [ss] so as to synthesise the
sorted order for small buckets [t, ss] for all t != ss.
This will avoid doing Real Work in subsequent Step 1's.
--*/
for (j = 0; j <= 255; j++)
j++) {
}
}
}
if (s->verbosity >= 4)
VPrintf3( " %d pointers, %d sorted, %d scanned\n",
}
}
/*---------------------------------------------*/
{
Int32 i;
for (i = 0; i < s->nblock; i++) {
s->block[i] ^= BZ_RAND_MASK;
}
}
/*---------------------------------------------*/
{
Int32 i;
s->workDone = 0;
s->blockRandomised = False;
s->firstAttempt = True;
sortMain ( s );
if (s->verbosity >= 3)
VPrintf3( " %d work, %d block, ratio %5.2f\n",
if (s->verbosity >= 2)
VPrintf0( " sorting aborted; randomising block\n" );
randomiseBlock ( s );
s->blockRandomised = True;
s->firstAttempt = False;
sortMain ( s );
if (s->verbosity >= 3)
VPrintf3( " %d work, %d block, ratio %f\n",
}
s->origPtr = -1;
for (i = 0; i < s->nblock; i++)
if (s->zptr[i] == 0)
{ s->origPtr = i; break; };
}
/*-------------------------------------------------------------*/
/*--- end blocksort.c ---*/
/*-------------------------------------------------------------*/