📄 blocksort.c
字号:
/*-------------------------------------------------------------*//*--- Block sorting machinery ---*//*--- blocksort.c ---*//*-------------------------------------------------------------*//*-- This file is a part of bzip2 and/or libbzip2, a program and 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 bzip2/libbzip2 version 0.9.0c of 18 October 1998 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 "bzlib_private.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].--*/static __inline__ Bool fullGtU ( UChar* block, UInt16* quadrant, UInt32 nblock, Int32* workDone, Int32 i1, Int32 i2 ){ Int32 k; UChar c1, c2; UInt16 s1, s2; AssertD ( i1 != i2, "fullGtU(1)" ); c1 = block[i1]; c2 = block[i2]; if (c1 != c2) return (c1 > c2); i1++; i2++; c1 = block[i1]; c2 = block[i2]; if (c1 != c2) return (c1 > c2); i1++; i2++; c1 = block[i1]; c2 = block[i2]; if (c1 != c2) return (c1 > c2); i1++; i2++; c1 = block[i1]; c2 = block[i2]; if (c1 != c2) return (c1 > c2); i1++; i2++; c1 = block[i1]; c2 = block[i2]; if (c1 != c2) return (c1 > c2); i1++; i2++; c1 = block[i1]; c2 = block[i2]; if (c1 != c2) return (c1 > c2); i1++; i2++; k = nblock; do { c1 = block[i1]; c2 = block[i2]; if (c1 != c2) return (c1 > c2); s1 = quadrant[i1]; s2 = quadrant[i2]; if (s1 != s2) return (s1 > s2); i1++; i2++; c1 = block[i1]; c2 = block[i2]; if (c1 != c2) return (c1 > c2); s1 = quadrant[i1]; s2 = quadrant[i2]; if (s1 != s2) return (s1 > s2); i1++; i2++; c1 = block[i1]; c2 = block[i2]; if (c1 != c2) return (c1 > c2); s1 = quadrant[i1]; s2 = quadrant[i2]; if (s1 != s2) return (s1 > s2); i1++; i2++; c1 = block[i1]; c2 = block[i2]; if (c1 != c2) return (c1 > c2); s1 = quadrant[i1]; s2 = quadrant[i2]; if (s1 != s2) return (s1 > s2); i1++; i2++; if (i1 >= nblock) i1 -= nblock; if (i2 >= nblock) i2 -= nblock; 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.--*/static Int32 incs[14] = { 1, 4, 13, 40, 121, 364, 1093, 3280, 9841, 29524, 88573, 265720, 797161, 2391484 };static void simpleSort ( EState* s, Int32 lo, Int32 hi, Int32 d ){ Int32 i, j, h, bigN, hp; Int32 v; UChar* block = s->block; UInt32* zptr = s->zptr; UInt16* quadrant = s->quadrant; Int32* workDone = &(s->workDone); Int32 nblock = s->nblock; Int32 workLimit = s->workLimit; Bool firstAttempt = s->firstAttempt; bigN = hi - lo + 1; if (bigN < 2) return; hp = 0; while (incs[hp] < bigN) hp++; hp--; for (; hp >= 0; hp--) { h = incs[hp]; i = lo + h; while (True) { /*-- copy 1 --*/ if (i > hi) break; v = zptr[i]; j = i; while ( fullGtU ( block, quadrant, nblock, workDone, zptr[j-h]+d, v+d ) ) { zptr[j] = zptr[j-h]; j = j - h; if (j <= (lo + h - 1)) break; } zptr[j] = v; i++; /*-- copy 2 --*/ if (i > hi) break; v = zptr[i]; j = i; while ( fullGtU ( block, quadrant, nblock, workDone, zptr[j-h]+d, v+d ) ) { zptr[j] = zptr[j-h]; j = j - h; if (j <= (lo + h - 1)) break; } zptr[j] = v; i++; /*-- copy 3 --*/ if (i > hi) break; v = zptr[i]; j = i; while ( fullGtU ( block, quadrant, nblock, workDone, zptr[j-h]+d, v+d ) ) { zptr[j] = zptr[j-h]; j = j - h; if (j <= (lo + h - 1)) break; } zptr[j] = v; i++; if (*workDone > workLimit && firstAttempt) return; } }}/*---------------------------------------------*//*-- 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.--*/#define swap(lv1, lv2) \ { Int32 tmp = lv1; lv1 = lv2; lv2 = tmp; }static void vswap ( UInt32* zptr, Int32 p1, Int32 p2, Int32 n ){ while (n > 0) { swap(zptr[p1], zptr[p2]); p1++; p2++; n--; }}static UChar med3 ( UChar a, UChar b, UChar c ){ 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 struct { Int32 ll; Int32 hh; Int32 dd; } StackElem;#define push(lz,hz,dz) { stack[sp].ll = lz; \ stack[sp].hh = hz; \ stack[sp].dd = dz; \ sp++; }#define pop(lz,hz,dz) { sp--; \ lz = stack[sp].ll; \ hz = stack[sp].hh; \ dz = stack[sp].dd; }#define SMALL_THRESH 20#define DEPTH_THRESH 10/*-- 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.--*/#define QSORT_STACK_SIZE 1000static void qSort3 ( EState* s, Int32 loSt, Int32 hiSt, Int32 dSt ){ Int32 unLo, unHi, ltLo, gtHi, med, n, m; Int32 sp, lo, hi, d; StackElem stack[QSORT_STACK_SIZE]; UChar* block = s->block; UInt32* zptr = s->zptr; Int32* workDone = &(s->workDone); Int32 workLimit = s->workLimit; Bool firstAttempt = s->firstAttempt; sp = 0; push ( loSt, hiSt, dSt ); while (sp > 0) { AssertH ( sp < QSORT_STACK_SIZE, 1001 ); pop ( lo, hi, d ); if (hi - lo < SMALL_THRESH || d > DEPTH_THRESH) { simpleSort ( s, lo, hi, d ); if (*workDone > workLimit && firstAttempt) return; continue; } med = med3 ( block[zptr[ lo ]+d], block[zptr[ hi ]+d], block[zptr[ (lo+hi)>>1 ]+d] ); unLo = ltLo = lo; unHi = gtHi = hi; while (True) { while (True) { if (unLo > unHi) break; n = ((Int32)block[zptr[unLo]+d]) - med; if (n == 0) { swap(zptr[unLo], zptr[ltLo]); ltLo++; unLo++; continue; }; if (n > 0) break; unLo++; } while (True) { if (unLo > unHi) break; n = ((Int32)block[zptr[unHi]+d]) - med; if (n == 0) { swap(zptr[unHi], zptr[gtHi]); gtHi--; unHi--; continue; }; if (n < 0) break; unHi--; } if (unLo > unHi) break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -