Path: blob/main/sys/contrib/zstd/lib/dictBuilder/divsufsort.c
48375 views
/*1* divsufsort.c for libdivsufsort-lite2* Copyright (c) 2003-2008 Yuta Mori All Rights Reserved.3*4* Permission is hereby granted, free of charge, to any person5* obtaining a copy of this software and associated documentation6* files (the "Software"), to deal in the Software without7* restriction, including without limitation the rights to use,8* copy, modify, merge, publish, distribute, sublicense, and/or sell9* copies of the Software, and to permit persons to whom the10* Software is furnished to do so, subject to the following11* conditions:12*13* The above copyright notice and this permission notice shall be14* included in all copies or substantial portions of the Software.15*16* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,17* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES18* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND19* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT20* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,21* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING22* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR23* OTHER DEALINGS IN THE SOFTWARE.24*/2526/*- Compiler specifics -*/27#ifdef __clang__28#pragma clang diagnostic ignored "-Wshorten-64-to-32"29#endif3031#if defined(_MSC_VER)32# pragma warning(disable : 4244)33# pragma warning(disable : 4127) /* C4127 : Condition expression is constant */34#endif353637/*- Dependencies -*/38#include <assert.h>39#include <stdio.h>40#include <stdlib.h>4142#include "divsufsort.h"4344/*- Constants -*/45#if defined(INLINE)46# undef INLINE47#endif48#if !defined(INLINE)49# define INLINE __inline50#endif51#if defined(ALPHABET_SIZE) && (ALPHABET_SIZE < 1)52# undef ALPHABET_SIZE53#endif54#if !defined(ALPHABET_SIZE)55# define ALPHABET_SIZE (256)56#endif57#define BUCKET_A_SIZE (ALPHABET_SIZE)58#define BUCKET_B_SIZE (ALPHABET_SIZE * ALPHABET_SIZE)59#if defined(SS_INSERTIONSORT_THRESHOLD)60# if SS_INSERTIONSORT_THRESHOLD < 161# undef SS_INSERTIONSORT_THRESHOLD62# define SS_INSERTIONSORT_THRESHOLD (1)63# endif64#else65# define SS_INSERTIONSORT_THRESHOLD (8)66#endif67#if defined(SS_BLOCKSIZE)68# if SS_BLOCKSIZE < 069# undef SS_BLOCKSIZE70# define SS_BLOCKSIZE (0)71# elif 32768 <= SS_BLOCKSIZE72# undef SS_BLOCKSIZE73# define SS_BLOCKSIZE (32767)74# endif75#else76# define SS_BLOCKSIZE (1024)77#endif78/* minstacksize = log(SS_BLOCKSIZE) / log(3) * 2 */79#if SS_BLOCKSIZE == 080# define SS_MISORT_STACKSIZE (96)81#elif SS_BLOCKSIZE <= 409682# define SS_MISORT_STACKSIZE (16)83#else84# define SS_MISORT_STACKSIZE (24)85#endif86#define SS_SMERGE_STACKSIZE (32)87#define TR_INSERTIONSORT_THRESHOLD (8)88#define TR_STACKSIZE (64)899091/*- Macros -*/92#ifndef SWAP93# define SWAP(_a, _b) do { t = (_a); (_a) = (_b); (_b) = t; } while(0)94#endif /* SWAP */95#ifndef MIN96# define MIN(_a, _b) (((_a) < (_b)) ? (_a) : (_b))97#endif /* MIN */98#ifndef MAX99# define MAX(_a, _b) (((_a) > (_b)) ? (_a) : (_b))100#endif /* MAX */101#define STACK_PUSH(_a, _b, _c, _d)\102do {\103assert(ssize < STACK_SIZE);\104stack[ssize].a = (_a), stack[ssize].b = (_b),\105stack[ssize].c = (_c), stack[ssize++].d = (_d);\106} while(0)107#define STACK_PUSH5(_a, _b, _c, _d, _e)\108do {\109assert(ssize < STACK_SIZE);\110stack[ssize].a = (_a), stack[ssize].b = (_b),\111stack[ssize].c = (_c), stack[ssize].d = (_d), stack[ssize++].e = (_e);\112} while(0)113#define STACK_POP(_a, _b, _c, _d)\114do {\115assert(0 <= ssize);\116if(ssize == 0) { return; }\117(_a) = stack[--ssize].a, (_b) = stack[ssize].b,\118(_c) = stack[ssize].c, (_d) = stack[ssize].d;\119} while(0)120#define STACK_POP5(_a, _b, _c, _d, _e)\121do {\122assert(0 <= ssize);\123if(ssize == 0) { return; }\124(_a) = stack[--ssize].a, (_b) = stack[ssize].b,\125(_c) = stack[ssize].c, (_d) = stack[ssize].d, (_e) = stack[ssize].e;\126} while(0)127#define BUCKET_A(_c0) bucket_A[(_c0)]128#if ALPHABET_SIZE == 256129#define BUCKET_B(_c0, _c1) (bucket_B[((_c1) << 8) | (_c0)])130#define BUCKET_BSTAR(_c0, _c1) (bucket_B[((_c0) << 8) | (_c1)])131#else132#define BUCKET_B(_c0, _c1) (bucket_B[(_c1) * ALPHABET_SIZE + (_c0)])133#define BUCKET_BSTAR(_c0, _c1) (bucket_B[(_c0) * ALPHABET_SIZE + (_c1)])134#endif135136137/*- Private Functions -*/138139static const int lg_table[256]= {140-1,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,1415,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,1426,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,1436,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,1447,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,1457,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,1467,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,1477,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7148};149150#if (SS_BLOCKSIZE == 0) || (SS_INSERTIONSORT_THRESHOLD < SS_BLOCKSIZE)151152static INLINE153int154ss_ilg(int n) {155#if SS_BLOCKSIZE == 0156return (n & 0xffff0000) ?157((n & 0xff000000) ?15824 + lg_table[(n >> 24) & 0xff] :15916 + lg_table[(n >> 16) & 0xff]) :160((n & 0x0000ff00) ?1618 + lg_table[(n >> 8) & 0xff] :1620 + lg_table[(n >> 0) & 0xff]);163#elif SS_BLOCKSIZE < 256164return lg_table[n];165#else166return (n & 0xff00) ?1678 + lg_table[(n >> 8) & 0xff] :1680 + lg_table[(n >> 0) & 0xff];169#endif170}171172#endif /* (SS_BLOCKSIZE == 0) || (SS_INSERTIONSORT_THRESHOLD < SS_BLOCKSIZE) */173174#if SS_BLOCKSIZE != 0175176static const int sqq_table[256] = {1770, 16, 22, 27, 32, 35, 39, 42, 45, 48, 50, 53, 55, 57, 59, 61,17864, 65, 67, 69, 71, 73, 75, 76, 78, 80, 81, 83, 84, 86, 87, 89,17990, 91, 93, 94, 96, 97, 98, 99, 101, 102, 103, 104, 106, 107, 108, 109,180110, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126,181128, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142,182143, 144, 144, 145, 146, 147, 148, 149, 150, 150, 151, 152, 153, 154, 155, 155,183156, 157, 158, 159, 160, 160, 161, 162, 163, 163, 164, 165, 166, 167, 167, 168,184169, 170, 170, 171, 172, 173, 173, 174, 175, 176, 176, 177, 178, 178, 179, 180,185181, 181, 182, 183, 183, 184, 185, 185, 186, 187, 187, 188, 189, 189, 190, 191,186192, 192, 193, 193, 194, 195, 195, 196, 197, 197, 198, 199, 199, 200, 201, 201,187202, 203, 203, 204, 204, 205, 206, 206, 207, 208, 208, 209, 209, 210, 211, 211,188212, 212, 213, 214, 214, 215, 215, 216, 217, 217, 218, 218, 219, 219, 220, 221,189221, 222, 222, 223, 224, 224, 225, 225, 226, 226, 227, 227, 228, 229, 229, 230,190230, 231, 231, 232, 232, 233, 234, 234, 235, 235, 236, 236, 237, 237, 238, 238,191239, 240, 240, 241, 241, 242, 242, 243, 243, 244, 244, 245, 245, 246, 246, 247,192247, 248, 248, 249, 249, 250, 250, 251, 251, 252, 252, 253, 253, 254, 254, 255193};194195static INLINE196int197ss_isqrt(int x) {198int y, e;199200if(x >= (SS_BLOCKSIZE * SS_BLOCKSIZE)) { return SS_BLOCKSIZE; }201e = (x & 0xffff0000) ?202((x & 0xff000000) ?20324 + lg_table[(x >> 24) & 0xff] :20416 + lg_table[(x >> 16) & 0xff]) :205((x & 0x0000ff00) ?2068 + lg_table[(x >> 8) & 0xff] :2070 + lg_table[(x >> 0) & 0xff]);208209if(e >= 16) {210y = sqq_table[x >> ((e - 6) - (e & 1))] << ((e >> 1) - 7);211if(e >= 24) { y = (y + 1 + x / y) >> 1; }212y = (y + 1 + x / y) >> 1;213} else if(e >= 8) {214y = (sqq_table[x >> ((e - 6) - (e & 1))] >> (7 - (e >> 1))) + 1;215} else {216return sqq_table[x] >> 4;217}218219return (x < (y * y)) ? y - 1 : y;220}221222#endif /* SS_BLOCKSIZE != 0 */223224225/*---------------------------------------------------------------------------*/226227/* Compares two suffixes. */228static INLINE229int230ss_compare(const unsigned char *T,231const int *p1, const int *p2,232int depth) {233const unsigned char *U1, *U2, *U1n, *U2n;234235for(U1 = T + depth + *p1,236U2 = T + depth + *p2,237U1n = T + *(p1 + 1) + 2,238U2n = T + *(p2 + 1) + 2;239(U1 < U1n) && (U2 < U2n) && (*U1 == *U2);240++U1, ++U2) {241}242243return U1 < U1n ?244(U2 < U2n ? *U1 - *U2 : 1) :245(U2 < U2n ? -1 : 0);246}247248249/*---------------------------------------------------------------------------*/250251#if (SS_BLOCKSIZE != 1) && (SS_INSERTIONSORT_THRESHOLD != 1)252253/* Insertionsort for small size groups */254static255void256ss_insertionsort(const unsigned char *T, const int *PA,257int *first, int *last, int depth) {258int *i, *j;259int t;260int r;261262for(i = last - 2; first <= i; --i) {263for(t = *i, j = i + 1; 0 < (r = ss_compare(T, PA + t, PA + *j, depth));) {264do { *(j - 1) = *j; } while((++j < last) && (*j < 0));265if(last <= j) { break; }266}267if(r == 0) { *j = ~*j; }268*(j - 1) = t;269}270}271272#endif /* (SS_BLOCKSIZE != 1) && (SS_INSERTIONSORT_THRESHOLD != 1) */273274275/*---------------------------------------------------------------------------*/276277#if (SS_BLOCKSIZE == 0) || (SS_INSERTIONSORT_THRESHOLD < SS_BLOCKSIZE)278279static INLINE280void281ss_fixdown(const unsigned char *Td, const int *PA,282int *SA, int i, int size) {283int j, k;284int v;285int c, d, e;286287for(v = SA[i], c = Td[PA[v]]; (j = 2 * i + 1) < size; SA[i] = SA[k], i = k) {288d = Td[PA[SA[k = j++]]];289if(d < (e = Td[PA[SA[j]]])) { k = j; d = e; }290if(d <= c) { break; }291}292SA[i] = v;293}294295/* Simple top-down heapsort. */296static297void298ss_heapsort(const unsigned char *Td, const int *PA, int *SA, int size) {299int i, m;300int t;301302m = size;303if((size % 2) == 0) {304m--;305if(Td[PA[SA[m / 2]]] < Td[PA[SA[m]]]) { SWAP(SA[m], SA[m / 2]); }306}307308for(i = m / 2 - 1; 0 <= i; --i) { ss_fixdown(Td, PA, SA, i, m); }309if((size % 2) == 0) { SWAP(SA[0], SA[m]); ss_fixdown(Td, PA, SA, 0, m); }310for(i = m - 1; 0 < i; --i) {311t = SA[0], SA[0] = SA[i];312ss_fixdown(Td, PA, SA, 0, i);313SA[i] = t;314}315}316317318/*---------------------------------------------------------------------------*/319320/* Returns the median of three elements. */321static INLINE322int *323ss_median3(const unsigned char *Td, const int *PA,324int *v1, int *v2, int *v3) {325int *t;326if(Td[PA[*v1]] > Td[PA[*v2]]) { SWAP(v1, v2); }327if(Td[PA[*v2]] > Td[PA[*v3]]) {328if(Td[PA[*v1]] > Td[PA[*v3]]) { return v1; }329else { return v3; }330}331return v2;332}333334/* Returns the median of five elements. */335static INLINE336int *337ss_median5(const unsigned char *Td, const int *PA,338int *v1, int *v2, int *v3, int *v4, int *v5) {339int *t;340if(Td[PA[*v2]] > Td[PA[*v3]]) { SWAP(v2, v3); }341if(Td[PA[*v4]] > Td[PA[*v5]]) { SWAP(v4, v5); }342if(Td[PA[*v2]] > Td[PA[*v4]]) { SWAP(v2, v4); SWAP(v3, v5); }343if(Td[PA[*v1]] > Td[PA[*v3]]) { SWAP(v1, v3); }344if(Td[PA[*v1]] > Td[PA[*v4]]) { SWAP(v1, v4); SWAP(v3, v5); }345if(Td[PA[*v3]] > Td[PA[*v4]]) { return v4; }346return v3;347}348349/* Returns the pivot element. */350static INLINE351int *352ss_pivot(const unsigned char *Td, const int *PA, int *first, int *last) {353int *middle;354int t;355356t = last - first;357middle = first + t / 2;358359if(t <= 512) {360if(t <= 32) {361return ss_median3(Td, PA, first, middle, last - 1);362} else {363t >>= 2;364return ss_median5(Td, PA, first, first + t, middle, last - 1 - t, last - 1);365}366}367t >>= 3;368first = ss_median3(Td, PA, first, first + t, first + (t << 1));369middle = ss_median3(Td, PA, middle - t, middle, middle + t);370last = ss_median3(Td, PA, last - 1 - (t << 1), last - 1 - t, last - 1);371return ss_median3(Td, PA, first, middle, last);372}373374375/*---------------------------------------------------------------------------*/376377/* Binary partition for substrings. */378static INLINE379int *380ss_partition(const int *PA,381int *first, int *last, int depth) {382int *a, *b;383int t;384for(a = first - 1, b = last;;) {385for(; (++a < b) && ((PA[*a] + depth) >= (PA[*a + 1] + 1));) { *a = ~*a; }386for(; (a < --b) && ((PA[*b] + depth) < (PA[*b + 1] + 1));) { }387if(b <= a) { break; }388t = ~*b;389*b = *a;390*a = t;391}392if(first < a) { *first = ~*first; }393return a;394}395396/* Multikey introsort for medium size groups. */397static398void399ss_mintrosort(const unsigned char *T, const int *PA,400int *first, int *last,401int depth) {402#define STACK_SIZE SS_MISORT_STACKSIZE403struct { int *a, *b, c; int d; } stack[STACK_SIZE];404const unsigned char *Td;405int *a, *b, *c, *d, *e, *f;406int s, t;407int ssize;408int limit;409int v, x = 0;410411for(ssize = 0, limit = ss_ilg(last - first);;) {412413if((last - first) <= SS_INSERTIONSORT_THRESHOLD) {414#if 1 < SS_INSERTIONSORT_THRESHOLD415if(1 < (last - first)) { ss_insertionsort(T, PA, first, last, depth); }416#endif417STACK_POP(first, last, depth, limit);418continue;419}420421Td = T + depth;422if(limit-- == 0) { ss_heapsort(Td, PA, first, last - first); }423if(limit < 0) {424for(a = first + 1, v = Td[PA[*first]]; a < last; ++a) {425if((x = Td[PA[*a]]) != v) {426if(1 < (a - first)) { break; }427v = x;428first = a;429}430}431if(Td[PA[*first] - 1] < v) {432first = ss_partition(PA, first, a, depth);433}434if((a - first) <= (last - a)) {435if(1 < (a - first)) {436STACK_PUSH(a, last, depth, -1);437last = a, depth += 1, limit = ss_ilg(a - first);438} else {439first = a, limit = -1;440}441} else {442if(1 < (last - a)) {443STACK_PUSH(first, a, depth + 1, ss_ilg(a - first));444first = a, limit = -1;445} else {446last = a, depth += 1, limit = ss_ilg(a - first);447}448}449continue;450}451452/* choose pivot */453a = ss_pivot(Td, PA, first, last);454v = Td[PA[*a]];455SWAP(*first, *a);456457/* partition */458for(b = first; (++b < last) && ((x = Td[PA[*b]]) == v);) { }459if(((a = b) < last) && (x < v)) {460for(; (++b < last) && ((x = Td[PA[*b]]) <= v);) {461if(x == v) { SWAP(*b, *a); ++a; }462}463}464for(c = last; (b < --c) && ((x = Td[PA[*c]]) == v);) { }465if((b < (d = c)) && (x > v)) {466for(; (b < --c) && ((x = Td[PA[*c]]) >= v);) {467if(x == v) { SWAP(*c, *d); --d; }468}469}470for(; b < c;) {471SWAP(*b, *c);472for(; (++b < c) && ((x = Td[PA[*b]]) <= v);) {473if(x == v) { SWAP(*b, *a); ++a; }474}475for(; (b < --c) && ((x = Td[PA[*c]]) >= v);) {476if(x == v) { SWAP(*c, *d); --d; }477}478}479480if(a <= d) {481c = b - 1;482483if((s = a - first) > (t = b - a)) { s = t; }484for(e = first, f = b - s; 0 < s; --s, ++e, ++f) { SWAP(*e, *f); }485if((s = d - c) > (t = last - d - 1)) { s = t; }486for(e = b, f = last - s; 0 < s; --s, ++e, ++f) { SWAP(*e, *f); }487488a = first + (b - a), c = last - (d - c);489b = (v <= Td[PA[*a] - 1]) ? a : ss_partition(PA, a, c, depth);490491if((a - first) <= (last - c)) {492if((last - c) <= (c - b)) {493STACK_PUSH(b, c, depth + 1, ss_ilg(c - b));494STACK_PUSH(c, last, depth, limit);495last = a;496} else if((a - first) <= (c - b)) {497STACK_PUSH(c, last, depth, limit);498STACK_PUSH(b, c, depth + 1, ss_ilg(c - b));499last = a;500} else {501STACK_PUSH(c, last, depth, limit);502STACK_PUSH(first, a, depth, limit);503first = b, last = c, depth += 1, limit = ss_ilg(c - b);504}505} else {506if((a - first) <= (c - b)) {507STACK_PUSH(b, c, depth + 1, ss_ilg(c - b));508STACK_PUSH(first, a, depth, limit);509first = c;510} else if((last - c) <= (c - b)) {511STACK_PUSH(first, a, depth, limit);512STACK_PUSH(b, c, depth + 1, ss_ilg(c - b));513first = c;514} else {515STACK_PUSH(first, a, depth, limit);516STACK_PUSH(c, last, depth, limit);517first = b, last = c, depth += 1, limit = ss_ilg(c - b);518}519}520} else {521limit += 1;522if(Td[PA[*first] - 1] < v) {523first = ss_partition(PA, first, last, depth);524limit = ss_ilg(last - first);525}526depth += 1;527}528}529#undef STACK_SIZE530}531532#endif /* (SS_BLOCKSIZE == 0) || (SS_INSERTIONSORT_THRESHOLD < SS_BLOCKSIZE) */533534535/*---------------------------------------------------------------------------*/536537#if SS_BLOCKSIZE != 0538539static INLINE540void541ss_blockswap(int *a, int *b, int n) {542int t;543for(; 0 < n; --n, ++a, ++b) {544t = *a, *a = *b, *b = t;545}546}547548static INLINE549void550ss_rotate(int *first, int *middle, int *last) {551int *a, *b, t;552int l, r;553l = middle - first, r = last - middle;554for(; (0 < l) && (0 < r);) {555if(l == r) { ss_blockswap(first, middle, l); break; }556if(l < r) {557a = last - 1, b = middle - 1;558t = *a;559do {560*a-- = *b, *b-- = *a;561if(b < first) {562*a = t;563last = a;564if((r -= l + 1) <= l) { break; }565a -= 1, b = middle - 1;566t = *a;567}568} while(1);569} else {570a = first, b = middle;571t = *a;572do {573*a++ = *b, *b++ = *a;574if(last <= b) {575*a = t;576first = a + 1;577if((l -= r + 1) <= r) { break; }578a += 1, b = middle;579t = *a;580}581} while(1);582}583}584}585586587/*---------------------------------------------------------------------------*/588589static590void591ss_inplacemerge(const unsigned char *T, const int *PA,592int *first, int *middle, int *last,593int depth) {594const int *p;595int *a, *b;596int len, half;597int q, r;598int x;599600for(;;) {601if(*(last - 1) < 0) { x = 1; p = PA + ~*(last - 1); }602else { x = 0; p = PA + *(last - 1); }603for(a = first, len = middle - first, half = len >> 1, r = -1;6040 < len;605len = half, half >>= 1) {606b = a + half;607q = ss_compare(T, PA + ((0 <= *b) ? *b : ~*b), p, depth);608if(q < 0) {609a = b + 1;610half -= (len & 1) ^ 1;611} else {612r = q;613}614}615if(a < middle) {616if(r == 0) { *a = ~*a; }617ss_rotate(a, middle, last);618last -= middle - a;619middle = a;620if(first == middle) { break; }621}622--last;623if(x != 0) { while(*--last < 0) { } }624if(middle == last) { break; }625}626}627628629/*---------------------------------------------------------------------------*/630631/* Merge-forward with internal buffer. */632static633void634ss_mergeforward(const unsigned char *T, const int *PA,635int *first, int *middle, int *last,636int *buf, int depth) {637int *a, *b, *c, *bufend;638int t;639int r;640641bufend = buf + (middle - first) - 1;642ss_blockswap(buf, first, middle - first);643644for(t = *(a = first), b = buf, c = middle;;) {645r = ss_compare(T, PA + *b, PA + *c, depth);646if(r < 0) {647do {648*a++ = *b;649if(bufend <= b) { *bufend = t; return; }650*b++ = *a;651} while(*b < 0);652} else if(r > 0) {653do {654*a++ = *c, *c++ = *a;655if(last <= c) {656while(b < bufend) { *a++ = *b, *b++ = *a; }657*a = *b, *b = t;658return;659}660} while(*c < 0);661} else {662*c = ~*c;663do {664*a++ = *b;665if(bufend <= b) { *bufend = t; return; }666*b++ = *a;667} while(*b < 0);668669do {670*a++ = *c, *c++ = *a;671if(last <= c) {672while(b < bufend) { *a++ = *b, *b++ = *a; }673*a = *b, *b = t;674return;675}676} while(*c < 0);677}678}679}680681/* Merge-backward with internal buffer. */682static683void684ss_mergebackward(const unsigned char *T, const int *PA,685int *first, int *middle, int *last,686int *buf, int depth) {687const int *p1, *p2;688int *a, *b, *c, *bufend;689int t;690int r;691int x;692693bufend = buf + (last - middle) - 1;694ss_blockswap(buf, middle, last - middle);695696x = 0;697if(*bufend < 0) { p1 = PA + ~*bufend; x |= 1; }698else { p1 = PA + *bufend; }699if(*(middle - 1) < 0) { p2 = PA + ~*(middle - 1); x |= 2; }700else { p2 = PA + *(middle - 1); }701for(t = *(a = last - 1), b = bufend, c = middle - 1;;) {702r = ss_compare(T, p1, p2, depth);703if(0 < r) {704if(x & 1) { do { *a-- = *b, *b-- = *a; } while(*b < 0); x ^= 1; }705*a-- = *b;706if(b <= buf) { *buf = t; break; }707*b-- = *a;708if(*b < 0) { p1 = PA + ~*b; x |= 1; }709else { p1 = PA + *b; }710} else if(r < 0) {711if(x & 2) { do { *a-- = *c, *c-- = *a; } while(*c < 0); x ^= 2; }712*a-- = *c, *c-- = *a;713if(c < first) {714while(buf < b) { *a-- = *b, *b-- = *a; }715*a = *b, *b = t;716break;717}718if(*c < 0) { p2 = PA + ~*c; x |= 2; }719else { p2 = PA + *c; }720} else {721if(x & 1) { do { *a-- = *b, *b-- = *a; } while(*b < 0); x ^= 1; }722*a-- = ~*b;723if(b <= buf) { *buf = t; break; }724*b-- = *a;725if(x & 2) { do { *a-- = *c, *c-- = *a; } while(*c < 0); x ^= 2; }726*a-- = *c, *c-- = *a;727if(c < first) {728while(buf < b) { *a-- = *b, *b-- = *a; }729*a = *b, *b = t;730break;731}732if(*b < 0) { p1 = PA + ~*b; x |= 1; }733else { p1 = PA + *b; }734if(*c < 0) { p2 = PA + ~*c; x |= 2; }735else { p2 = PA + *c; }736}737}738}739740/* D&C based merge. */741static742void743ss_swapmerge(const unsigned char *T, const int *PA,744int *first, int *middle, int *last,745int *buf, int bufsize, int depth) {746#define STACK_SIZE SS_SMERGE_STACKSIZE747#define GETIDX(a) ((0 <= (a)) ? (a) : (~(a)))748#define MERGE_CHECK(a, b, c)\749do {\750if(((c) & 1) ||\751(((c) & 2) && (ss_compare(T, PA + GETIDX(*((a) - 1)), PA + *(a), depth) == 0))) {\752*(a) = ~*(a);\753}\754if(((c) & 4) && ((ss_compare(T, PA + GETIDX(*((b) - 1)), PA + *(b), depth) == 0))) {\755*(b) = ~*(b);\756}\757} while(0)758struct { int *a, *b, *c; int d; } stack[STACK_SIZE];759int *l, *r, *lm, *rm;760int m, len, half;761int ssize;762int check, next;763764for(check = 0, ssize = 0;;) {765if((last - middle) <= bufsize) {766if((first < middle) && (middle < last)) {767ss_mergebackward(T, PA, first, middle, last, buf, depth);768}769MERGE_CHECK(first, last, check);770STACK_POP(first, middle, last, check);771continue;772}773774if((middle - first) <= bufsize) {775if(first < middle) {776ss_mergeforward(T, PA, first, middle, last, buf, depth);777}778MERGE_CHECK(first, last, check);779STACK_POP(first, middle, last, check);780continue;781}782783for(m = 0, len = MIN(middle - first, last - middle), half = len >> 1;7840 < len;785len = half, half >>= 1) {786if(ss_compare(T, PA + GETIDX(*(middle + m + half)),787PA + GETIDX(*(middle - m - half - 1)), depth) < 0) {788m += half + 1;789half -= (len & 1) ^ 1;790}791}792793if(0 < m) {794lm = middle - m, rm = middle + m;795ss_blockswap(lm, middle, m);796l = r = middle, next = 0;797if(rm < last) {798if(*rm < 0) {799*rm = ~*rm;800if(first < lm) { for(; *--l < 0;) { } next |= 4; }801next |= 1;802} else if(first < lm) {803for(; *r < 0; ++r) { }804next |= 2;805}806}807808if((l - first) <= (last - r)) {809STACK_PUSH(r, rm, last, (next & 3) | (check & 4));810middle = lm, last = l, check = (check & 3) | (next & 4);811} else {812if((next & 2) && (r == middle)) { next ^= 6; }813STACK_PUSH(first, lm, l, (check & 3) | (next & 4));814first = r, middle = rm, check = (next & 3) | (check & 4);815}816} else {817if(ss_compare(T, PA + GETIDX(*(middle - 1)), PA + *middle, depth) == 0) {818*middle = ~*middle;819}820MERGE_CHECK(first, last, check);821STACK_POP(first, middle, last, check);822}823}824#undef STACK_SIZE825}826827#endif /* SS_BLOCKSIZE != 0 */828829830/*---------------------------------------------------------------------------*/831832/* Substring sort */833static834void835sssort(const unsigned char *T, const int *PA,836int *first, int *last,837int *buf, int bufsize,838int depth, int n, int lastsuffix) {839int *a;840#if SS_BLOCKSIZE != 0841int *b, *middle, *curbuf;842int j, k, curbufsize, limit;843#endif844int i;845846if(lastsuffix != 0) { ++first; }847848#if SS_BLOCKSIZE == 0849ss_mintrosort(T, PA, first, last, depth);850#else851if((bufsize < SS_BLOCKSIZE) &&852(bufsize < (last - first)) &&853(bufsize < (limit = ss_isqrt(last - first)))) {854if(SS_BLOCKSIZE < limit) { limit = SS_BLOCKSIZE; }855buf = middle = last - limit, bufsize = limit;856} else {857middle = last, limit = 0;858}859for(a = first, i = 0; SS_BLOCKSIZE < (middle - a); a += SS_BLOCKSIZE, ++i) {860#if SS_INSERTIONSORT_THRESHOLD < SS_BLOCKSIZE861ss_mintrosort(T, PA, a, a + SS_BLOCKSIZE, depth);862#elif 1 < SS_BLOCKSIZE863ss_insertionsort(T, PA, a, a + SS_BLOCKSIZE, depth);864#endif865curbufsize = last - (a + SS_BLOCKSIZE);866curbuf = a + SS_BLOCKSIZE;867if(curbufsize <= bufsize) { curbufsize = bufsize, curbuf = buf; }868for(b = a, k = SS_BLOCKSIZE, j = i; j & 1; b -= k, k <<= 1, j >>= 1) {869ss_swapmerge(T, PA, b - k, b, b + k, curbuf, curbufsize, depth);870}871}872#if SS_INSERTIONSORT_THRESHOLD < SS_BLOCKSIZE873ss_mintrosort(T, PA, a, middle, depth);874#elif 1 < SS_BLOCKSIZE875ss_insertionsort(T, PA, a, middle, depth);876#endif877for(k = SS_BLOCKSIZE; i != 0; k <<= 1, i >>= 1) {878if(i & 1) {879ss_swapmerge(T, PA, a - k, a, middle, buf, bufsize, depth);880a -= k;881}882}883if(limit != 0) {884#if SS_INSERTIONSORT_THRESHOLD < SS_BLOCKSIZE885ss_mintrosort(T, PA, middle, last, depth);886#elif 1 < SS_BLOCKSIZE887ss_insertionsort(T, PA, middle, last, depth);888#endif889ss_inplacemerge(T, PA, first, middle, last, depth);890}891#endif892893if(lastsuffix != 0) {894/* Insert last type B* suffix. */895int PAi[2]; PAi[0] = PA[*(first - 1)], PAi[1] = n - 2;896for(a = first, i = *(first - 1);897(a < last) && ((*a < 0) || (0 < ss_compare(T, &(PAi[0]), PA + *a, depth)));898++a) {899*(a - 1) = *a;900}901*(a - 1) = i;902}903}904905906/*---------------------------------------------------------------------------*/907908static INLINE909int910tr_ilg(int n) {911return (n & 0xffff0000) ?912((n & 0xff000000) ?91324 + lg_table[(n >> 24) & 0xff] :91416 + lg_table[(n >> 16) & 0xff]) :915((n & 0x0000ff00) ?9168 + lg_table[(n >> 8) & 0xff] :9170 + lg_table[(n >> 0) & 0xff]);918}919920921/*---------------------------------------------------------------------------*/922923/* Simple insertionsort for small size groups. */924static925void926tr_insertionsort(const int *ISAd, int *first, int *last) {927int *a, *b;928int t, r;929930for(a = first + 1; a < last; ++a) {931for(t = *a, b = a - 1; 0 > (r = ISAd[t] - ISAd[*b]);) {932do { *(b + 1) = *b; } while((first <= --b) && (*b < 0));933if(b < first) { break; }934}935if(r == 0) { *b = ~*b; }936*(b + 1) = t;937}938}939940941/*---------------------------------------------------------------------------*/942943static INLINE944void945tr_fixdown(const int *ISAd, int *SA, int i, int size) {946int j, k;947int v;948int c, d, e;949950for(v = SA[i], c = ISAd[v]; (j = 2 * i + 1) < size; SA[i] = SA[k], i = k) {951d = ISAd[SA[k = j++]];952if(d < (e = ISAd[SA[j]])) { k = j; d = e; }953if(d <= c) { break; }954}955SA[i] = v;956}957958/* Simple top-down heapsort. */959static960void961tr_heapsort(const int *ISAd, int *SA, int size) {962int i, m;963int t;964965m = size;966if((size % 2) == 0) {967m--;968if(ISAd[SA[m / 2]] < ISAd[SA[m]]) { SWAP(SA[m], SA[m / 2]); }969}970971for(i = m / 2 - 1; 0 <= i; --i) { tr_fixdown(ISAd, SA, i, m); }972if((size % 2) == 0) { SWAP(SA[0], SA[m]); tr_fixdown(ISAd, SA, 0, m); }973for(i = m - 1; 0 < i; --i) {974t = SA[0], SA[0] = SA[i];975tr_fixdown(ISAd, SA, 0, i);976SA[i] = t;977}978}979980981/*---------------------------------------------------------------------------*/982983/* Returns the median of three elements. */984static INLINE985int *986tr_median3(const int *ISAd, int *v1, int *v2, int *v3) {987int *t;988if(ISAd[*v1] > ISAd[*v2]) { SWAP(v1, v2); }989if(ISAd[*v2] > ISAd[*v3]) {990if(ISAd[*v1] > ISAd[*v3]) { return v1; }991else { return v3; }992}993return v2;994}995996/* Returns the median of five elements. */997static INLINE998int *999tr_median5(const int *ISAd,1000int *v1, int *v2, int *v3, int *v4, int *v5) {1001int *t;1002if(ISAd[*v2] > ISAd[*v3]) { SWAP(v2, v3); }1003if(ISAd[*v4] > ISAd[*v5]) { SWAP(v4, v5); }1004if(ISAd[*v2] > ISAd[*v4]) { SWAP(v2, v4); SWAP(v3, v5); }1005if(ISAd[*v1] > ISAd[*v3]) { SWAP(v1, v3); }1006if(ISAd[*v1] > ISAd[*v4]) { SWAP(v1, v4); SWAP(v3, v5); }1007if(ISAd[*v3] > ISAd[*v4]) { return v4; }1008return v3;1009}10101011/* Returns the pivot element. */1012static INLINE1013int *1014tr_pivot(const int *ISAd, int *first, int *last) {1015int *middle;1016int t;10171018t = last - first;1019middle = first + t / 2;10201021if(t <= 512) {1022if(t <= 32) {1023return tr_median3(ISAd, first, middle, last - 1);1024} else {1025t >>= 2;1026return tr_median5(ISAd, first, first + t, middle, last - 1 - t, last - 1);1027}1028}1029t >>= 3;1030first = tr_median3(ISAd, first, first + t, first + (t << 1));1031middle = tr_median3(ISAd, middle - t, middle, middle + t);1032last = tr_median3(ISAd, last - 1 - (t << 1), last - 1 - t, last - 1);1033return tr_median3(ISAd, first, middle, last);1034}103510361037/*---------------------------------------------------------------------------*/10381039typedef struct _trbudget_t trbudget_t;1040struct _trbudget_t {1041int chance;1042int remain;1043int incval;1044int count;1045};10461047static INLINE1048void1049trbudget_init(trbudget_t *budget, int chance, int incval) {1050budget->chance = chance;1051budget->remain = budget->incval = incval;1052}10531054static INLINE1055int1056trbudget_check(trbudget_t *budget, int size) {1057if(size <= budget->remain) { budget->remain -= size; return 1; }1058if(budget->chance == 0) { budget->count += size; return 0; }1059budget->remain += budget->incval - size;1060budget->chance -= 1;1061return 1;1062}106310641065/*---------------------------------------------------------------------------*/10661067static INLINE1068void1069tr_partition(const int *ISAd,1070int *first, int *middle, int *last,1071int **pa, int **pb, int v) {1072int *a, *b, *c, *d, *e, *f;1073int t, s;1074int x = 0;10751076for(b = middle - 1; (++b < last) && ((x = ISAd[*b]) == v);) { }1077if(((a = b) < last) && (x < v)) {1078for(; (++b < last) && ((x = ISAd[*b]) <= v);) {1079if(x == v) { SWAP(*b, *a); ++a; }1080}1081}1082for(c = last; (b < --c) && ((x = ISAd[*c]) == v);) { }1083if((b < (d = c)) && (x > v)) {1084for(; (b < --c) && ((x = ISAd[*c]) >= v);) {1085if(x == v) { SWAP(*c, *d); --d; }1086}1087}1088for(; b < c;) {1089SWAP(*b, *c);1090for(; (++b < c) && ((x = ISAd[*b]) <= v);) {1091if(x == v) { SWAP(*b, *a); ++a; }1092}1093for(; (b < --c) && ((x = ISAd[*c]) >= v);) {1094if(x == v) { SWAP(*c, *d); --d; }1095}1096}10971098if(a <= d) {1099c = b - 1;1100if((s = a - first) > (t = b - a)) { s = t; }1101for(e = first, f = b - s; 0 < s; --s, ++e, ++f) { SWAP(*e, *f); }1102if((s = d - c) > (t = last - d - 1)) { s = t; }1103for(e = b, f = last - s; 0 < s; --s, ++e, ++f) { SWAP(*e, *f); }1104first += (b - a), last -= (d - c);1105}1106*pa = first, *pb = last;1107}11081109static1110void1111tr_copy(int *ISA, const int *SA,1112int *first, int *a, int *b, int *last,1113int depth) {1114/* sort suffixes of middle partition1115by using sorted order of suffixes of left and right partition. */1116int *c, *d, *e;1117int s, v;11181119v = b - SA - 1;1120for(c = first, d = a - 1; c <= d; ++c) {1121if((0 <= (s = *c - depth)) && (ISA[s] == v)) {1122*++d = s;1123ISA[s] = d - SA;1124}1125}1126for(c = last - 1, e = d + 1, d = b; e < d; --c) {1127if((0 <= (s = *c - depth)) && (ISA[s] == v)) {1128*--d = s;1129ISA[s] = d - SA;1130}1131}1132}11331134static1135void1136tr_partialcopy(int *ISA, const int *SA,1137int *first, int *a, int *b, int *last,1138int depth) {1139int *c, *d, *e;1140int s, v;1141int rank, lastrank, newrank = -1;11421143v = b - SA - 1;1144lastrank = -1;1145for(c = first, d = a - 1; c <= d; ++c) {1146if((0 <= (s = *c - depth)) && (ISA[s] == v)) {1147*++d = s;1148rank = ISA[s + depth];1149if(lastrank != rank) { lastrank = rank; newrank = d - SA; }1150ISA[s] = newrank;1151}1152}11531154lastrank = -1;1155for(e = d; first <= e; --e) {1156rank = ISA[*e];1157if(lastrank != rank) { lastrank = rank; newrank = e - SA; }1158if(newrank != rank) { ISA[*e] = newrank; }1159}11601161lastrank = -1;1162for(c = last - 1, e = d + 1, d = b; e < d; --c) {1163if((0 <= (s = *c - depth)) && (ISA[s] == v)) {1164*--d = s;1165rank = ISA[s + depth];1166if(lastrank != rank) { lastrank = rank; newrank = d - SA; }1167ISA[s] = newrank;1168}1169}1170}11711172static1173void1174tr_introsort(int *ISA, const int *ISAd,1175int *SA, int *first, int *last,1176trbudget_t *budget) {1177#define STACK_SIZE TR_STACKSIZE1178struct { const int *a; int *b, *c; int d, e; }stack[STACK_SIZE];1179int *a, *b, *c;1180int t;1181int v, x = 0;1182int incr = ISAd - ISA;1183int limit, next;1184int ssize, trlink = -1;11851186for(ssize = 0, limit = tr_ilg(last - first);;) {11871188if(limit < 0) {1189if(limit == -1) {1190/* tandem repeat partition */1191tr_partition(ISAd - incr, first, first, last, &a, &b, last - SA - 1);11921193/* update ranks */1194if(a < last) {1195for(c = first, v = a - SA - 1; c < a; ++c) { ISA[*c] = v; }1196}1197if(b < last) {1198for(c = a, v = b - SA - 1; c < b; ++c) { ISA[*c] = v; }1199}12001201/* push */1202if(1 < (b - a)) {1203STACK_PUSH5(NULL, a, b, 0, 0);1204STACK_PUSH5(ISAd - incr, first, last, -2, trlink);1205trlink = ssize - 2;1206}1207if((a - first) <= (last - b)) {1208if(1 < (a - first)) {1209STACK_PUSH5(ISAd, b, last, tr_ilg(last - b), trlink);1210last = a, limit = tr_ilg(a - first);1211} else if(1 < (last - b)) {1212first = b, limit = tr_ilg(last - b);1213} else {1214STACK_POP5(ISAd, first, last, limit, trlink);1215}1216} else {1217if(1 < (last - b)) {1218STACK_PUSH5(ISAd, first, a, tr_ilg(a - first), trlink);1219first = b, limit = tr_ilg(last - b);1220} else if(1 < (a - first)) {1221last = a, limit = tr_ilg(a - first);1222} else {1223STACK_POP5(ISAd, first, last, limit, trlink);1224}1225}1226} else if(limit == -2) {1227/* tandem repeat copy */1228a = stack[--ssize].b, b = stack[ssize].c;1229if(stack[ssize].d == 0) {1230tr_copy(ISA, SA, first, a, b, last, ISAd - ISA);1231} else {1232if(0 <= trlink) { stack[trlink].d = -1; }1233tr_partialcopy(ISA, SA, first, a, b, last, ISAd - ISA);1234}1235STACK_POP5(ISAd, first, last, limit, trlink);1236} else {1237/* sorted partition */1238if(0 <= *first) {1239a = first;1240do { ISA[*a] = a - SA; } while((++a < last) && (0 <= *a));1241first = a;1242}1243if(first < last) {1244a = first; do { *a = ~*a; } while(*++a < 0);1245next = (ISA[*a] != ISAd[*a]) ? tr_ilg(a - first + 1) : -1;1246if(++a < last) { for(b = first, v = a - SA - 1; b < a; ++b) { ISA[*b] = v; } }12471248/* push */1249if(trbudget_check(budget, a - first)) {1250if((a - first) <= (last - a)) {1251STACK_PUSH5(ISAd, a, last, -3, trlink);1252ISAd += incr, last = a, limit = next;1253} else {1254if(1 < (last - a)) {1255STACK_PUSH5(ISAd + incr, first, a, next, trlink);1256first = a, limit = -3;1257} else {1258ISAd += incr, last = a, limit = next;1259}1260}1261} else {1262if(0 <= trlink) { stack[trlink].d = -1; }1263if(1 < (last - a)) {1264first = a, limit = -3;1265} else {1266STACK_POP5(ISAd, first, last, limit, trlink);1267}1268}1269} else {1270STACK_POP5(ISAd, first, last, limit, trlink);1271}1272}1273continue;1274}12751276if((last - first) <= TR_INSERTIONSORT_THRESHOLD) {1277tr_insertionsort(ISAd, first, last);1278limit = -3;1279continue;1280}12811282if(limit-- == 0) {1283tr_heapsort(ISAd, first, last - first);1284for(a = last - 1; first < a; a = b) {1285for(x = ISAd[*a], b = a - 1; (first <= b) && (ISAd[*b] == x); --b) { *b = ~*b; }1286}1287limit = -3;1288continue;1289}12901291/* choose pivot */1292a = tr_pivot(ISAd, first, last);1293SWAP(*first, *a);1294v = ISAd[*first];12951296/* partition */1297tr_partition(ISAd, first, first + 1, last, &a, &b, v);1298if((last - first) != (b - a)) {1299next = (ISA[*a] != v) ? tr_ilg(b - a) : -1;13001301/* update ranks */1302for(c = first, v = a - SA - 1; c < a; ++c) { ISA[*c] = v; }1303if(b < last) { for(c = a, v = b - SA - 1; c < b; ++c) { ISA[*c] = v; } }13041305/* push */1306if((1 < (b - a)) && (trbudget_check(budget, b - a))) {1307if((a - first) <= (last - b)) {1308if((last - b) <= (b - a)) {1309if(1 < (a - first)) {1310STACK_PUSH5(ISAd + incr, a, b, next, trlink);1311STACK_PUSH5(ISAd, b, last, limit, trlink);1312last = a;1313} else if(1 < (last - b)) {1314STACK_PUSH5(ISAd + incr, a, b, next, trlink);1315first = b;1316} else {1317ISAd += incr, first = a, last = b, limit = next;1318}1319} else if((a - first) <= (b - a)) {1320if(1 < (a - first)) {1321STACK_PUSH5(ISAd, b, last, limit, trlink);1322STACK_PUSH5(ISAd + incr, a, b, next, trlink);1323last = a;1324} else {1325STACK_PUSH5(ISAd, b, last, limit, trlink);1326ISAd += incr, first = a, last = b, limit = next;1327}1328} else {1329STACK_PUSH5(ISAd, b, last, limit, trlink);1330STACK_PUSH5(ISAd, first, a, limit, trlink);1331ISAd += incr, first = a, last = b, limit = next;1332}1333} else {1334if((a - first) <= (b - a)) {1335if(1 < (last - b)) {1336STACK_PUSH5(ISAd + incr, a, b, next, trlink);1337STACK_PUSH5(ISAd, first, a, limit, trlink);1338first = b;1339} else if(1 < (a - first)) {1340STACK_PUSH5(ISAd + incr, a, b, next, trlink);1341last = a;1342} else {1343ISAd += incr, first = a, last = b, limit = next;1344}1345} else if((last - b) <= (b - a)) {1346if(1 < (last - b)) {1347STACK_PUSH5(ISAd, first, a, limit, trlink);1348STACK_PUSH5(ISAd + incr, a, b, next, trlink);1349first = b;1350} else {1351STACK_PUSH5(ISAd, first, a, limit, trlink);1352ISAd += incr, first = a, last = b, limit = next;1353}1354} else {1355STACK_PUSH5(ISAd, first, a, limit, trlink);1356STACK_PUSH5(ISAd, b, last, limit, trlink);1357ISAd += incr, first = a, last = b, limit = next;1358}1359}1360} else {1361if((1 < (b - a)) && (0 <= trlink)) { stack[trlink].d = -1; }1362if((a - first) <= (last - b)) {1363if(1 < (a - first)) {1364STACK_PUSH5(ISAd, b, last, limit, trlink);1365last = a;1366} else if(1 < (last - b)) {1367first = b;1368} else {1369STACK_POP5(ISAd, first, last, limit, trlink);1370}1371} else {1372if(1 < (last - b)) {1373STACK_PUSH5(ISAd, first, a, limit, trlink);1374first = b;1375} else if(1 < (a - first)) {1376last = a;1377} else {1378STACK_POP5(ISAd, first, last, limit, trlink);1379}1380}1381}1382} else {1383if(trbudget_check(budget, last - first)) {1384limit = tr_ilg(last - first), ISAd += incr;1385} else {1386if(0 <= trlink) { stack[trlink].d = -1; }1387STACK_POP5(ISAd, first, last, limit, trlink);1388}1389}1390}1391#undef STACK_SIZE1392}1393139413951396/*---------------------------------------------------------------------------*/13971398/* Tandem repeat sort */1399static1400void1401trsort(int *ISA, int *SA, int n, int depth) {1402int *ISAd;1403int *first, *last;1404trbudget_t budget;1405int t, skip, unsorted;14061407trbudget_init(&budget, tr_ilg(n) * 2 / 3, n);1408/* trbudget_init(&budget, tr_ilg(n) * 3 / 4, n); */1409for(ISAd = ISA + depth; -n < *SA; ISAd += ISAd - ISA) {1410first = SA;1411skip = 0;1412unsorted = 0;1413do {1414if((t = *first) < 0) { first -= t; skip += t; }1415else {1416if(skip != 0) { *(first + skip) = skip; skip = 0; }1417last = SA + ISA[t] + 1;1418if(1 < (last - first)) {1419budget.count = 0;1420tr_introsort(ISA, ISAd, SA, first, last, &budget);1421if(budget.count != 0) { unsorted += budget.count; }1422else { skip = first - last; }1423} else if((last - first) == 1) {1424skip = -1;1425}1426first = last;1427}1428} while(first < (SA + n));1429if(skip != 0) { *(first + skip) = skip; }1430if(unsorted == 0) { break; }1431}1432}143314341435/*---------------------------------------------------------------------------*/14361437/* Sorts suffixes of type B*. */1438static1439int1440sort_typeBstar(const unsigned char *T, int *SA,1441int *bucket_A, int *bucket_B,1442int n, int openMP) {1443int *PAb, *ISAb, *buf;1444#ifdef LIBBSC_OPENMP1445int *curbuf;1446int l;1447#endif1448int i, j, k, t, m, bufsize;1449int c0, c1;1450#ifdef LIBBSC_OPENMP1451int d0, d1;1452#endif1453(void)openMP;14541455/* Initialize bucket arrays. */1456for(i = 0; i < BUCKET_A_SIZE; ++i) { bucket_A[i] = 0; }1457for(i = 0; i < BUCKET_B_SIZE; ++i) { bucket_B[i] = 0; }14581459/* Count the number of occurrences of the first one or two characters of each1460type A, B and B* suffix. Moreover, store the beginning position of all1461type B* suffixes into the array SA. */1462for(i = n - 1, m = n, c0 = T[n - 1]; 0 <= i;) {1463/* type A suffix. */1464do { ++BUCKET_A(c1 = c0); } while((0 <= --i) && ((c0 = T[i]) >= c1));1465if(0 <= i) {1466/* type B* suffix. */1467++BUCKET_BSTAR(c0, c1);1468SA[--m] = i;1469/* type B suffix. */1470for(--i, c1 = c0; (0 <= i) && ((c0 = T[i]) <= c1); --i, c1 = c0) {1471++BUCKET_B(c0, c1);1472}1473}1474}1475m = n - m;1476/*1477note:1478A type B* suffix is lexicographically smaller than a type B suffix that1479begins with the same first two characters.1480*/14811482/* Calculate the index of start/end point of each bucket. */1483for(c0 = 0, i = 0, j = 0; c0 < ALPHABET_SIZE; ++c0) {1484t = i + BUCKET_A(c0);1485BUCKET_A(c0) = i + j; /* start point */1486i = t + BUCKET_B(c0, c0);1487for(c1 = c0 + 1; c1 < ALPHABET_SIZE; ++c1) {1488j += BUCKET_BSTAR(c0, c1);1489BUCKET_BSTAR(c0, c1) = j; /* end point */1490i += BUCKET_B(c0, c1);1491}1492}14931494if(0 < m) {1495/* Sort the type B* suffixes by their first two characters. */1496PAb = SA + n - m; ISAb = SA + m;1497for(i = m - 2; 0 <= i; --i) {1498t = PAb[i], c0 = T[t], c1 = T[t + 1];1499SA[--BUCKET_BSTAR(c0, c1)] = i;1500}1501t = PAb[m - 1], c0 = T[t], c1 = T[t + 1];1502SA[--BUCKET_BSTAR(c0, c1)] = m - 1;15031504/* Sort the type B* substrings using sssort. */1505#ifdef LIBBSC_OPENMP1506if (openMP)1507{1508buf = SA + m;1509c0 = ALPHABET_SIZE - 2, c1 = ALPHABET_SIZE - 1, j = m;1510#pragma omp parallel default(shared) private(bufsize, curbuf, k, l, d0, d1)1511{1512bufsize = (n - (2 * m)) / omp_get_num_threads();1513curbuf = buf + omp_get_thread_num() * bufsize;1514k = 0;1515for(;;) {1516#pragma omp critical(sssort_lock)1517{1518if(0 < (l = j)) {1519d0 = c0, d1 = c1;1520do {1521k = BUCKET_BSTAR(d0, d1);1522if(--d1 <= d0) {1523d1 = ALPHABET_SIZE - 1;1524if(--d0 < 0) { break; }1525}1526} while(((l - k) <= 1) && (0 < (l = k)));1527c0 = d0, c1 = d1, j = k;1528}1529}1530if(l == 0) { break; }1531sssort(T, PAb, SA + k, SA + l,1532curbuf, bufsize, 2, n, *(SA + k) == (m - 1));1533}1534}1535}1536else1537{1538buf = SA + m, bufsize = n - (2 * m);1539for(c0 = ALPHABET_SIZE - 2, j = m; 0 < j; --c0) {1540for(c1 = ALPHABET_SIZE - 1; c0 < c1; j = i, --c1) {1541i = BUCKET_BSTAR(c0, c1);1542if(1 < (j - i)) {1543sssort(T, PAb, SA + i, SA + j,1544buf, bufsize, 2, n, *(SA + i) == (m - 1));1545}1546}1547}1548}1549#else1550buf = SA + m, bufsize = n - (2 * m);1551for(c0 = ALPHABET_SIZE - 2, j = m; 0 < j; --c0) {1552for(c1 = ALPHABET_SIZE - 1; c0 < c1; j = i, --c1) {1553i = BUCKET_BSTAR(c0, c1);1554if(1 < (j - i)) {1555sssort(T, PAb, SA + i, SA + j,1556buf, bufsize, 2, n, *(SA + i) == (m - 1));1557}1558}1559}1560#endif15611562/* Compute ranks of type B* substrings. */1563for(i = m - 1; 0 <= i; --i) {1564if(0 <= SA[i]) {1565j = i;1566do { ISAb[SA[i]] = i; } while((0 <= --i) && (0 <= SA[i]));1567SA[i + 1] = i - j;1568if(i <= 0) { break; }1569}1570j = i;1571do { ISAb[SA[i] = ~SA[i]] = j; } while(SA[--i] < 0);1572ISAb[SA[i]] = j;1573}15741575/* Construct the inverse suffix array of type B* suffixes using trsort. */1576trsort(ISAb, SA, m, 1);15771578/* Set the sorted order of type B* suffixes. */1579for(i = n - 1, j = m, c0 = T[n - 1]; 0 <= i;) {1580for(--i, c1 = c0; (0 <= i) && ((c0 = T[i]) >= c1); --i, c1 = c0) { }1581if(0 <= i) {1582t = i;1583for(--i, c1 = c0; (0 <= i) && ((c0 = T[i]) <= c1); --i, c1 = c0) { }1584SA[ISAb[--j]] = ((t == 0) || (1 < (t - i))) ? t : ~t;1585}1586}15871588/* Calculate the index of start/end point of each bucket. */1589BUCKET_B(ALPHABET_SIZE - 1, ALPHABET_SIZE - 1) = n; /* end point */1590for(c0 = ALPHABET_SIZE - 2, k = m - 1; 0 <= c0; --c0) {1591i = BUCKET_A(c0 + 1) - 1;1592for(c1 = ALPHABET_SIZE - 1; c0 < c1; --c1) {1593t = i - BUCKET_B(c0, c1);1594BUCKET_B(c0, c1) = i; /* end point */15951596/* Move all type B* suffixes to the correct position. */1597for(i = t, j = BUCKET_BSTAR(c0, c1);1598j <= k;1599--i, --k) { SA[i] = SA[k]; }1600}1601BUCKET_BSTAR(c0, c0 + 1) = i - BUCKET_B(c0, c0) + 1; /* start point */1602BUCKET_B(c0, c0) = i; /* end point */1603}1604}16051606return m;1607}16081609/* Constructs the suffix array by using the sorted order of type B* suffixes. */1610static1611void1612construct_SA(const unsigned char *T, int *SA,1613int *bucket_A, int *bucket_B,1614int n, int m) {1615int *i, *j, *k;1616int s;1617int c0, c1, c2;16181619if(0 < m) {1620/* Construct the sorted order of type B suffixes by using1621the sorted order of type B* suffixes. */1622for(c1 = ALPHABET_SIZE - 2; 0 <= c1; --c1) {1623/* Scan the suffix array from right to left. */1624for(i = SA + BUCKET_BSTAR(c1, c1 + 1),1625j = SA + BUCKET_A(c1 + 1) - 1, k = NULL, c2 = -1;1626i <= j;1627--j) {1628if(0 < (s = *j)) {1629assert(T[s] == c1);1630assert(((s + 1) < n) && (T[s] <= T[s + 1]));1631assert(T[s - 1] <= T[s]);1632*j = ~s;1633c0 = T[--s];1634if((0 < s) && (T[s - 1] > c0)) { s = ~s; }1635if(c0 != c2) {1636if(0 <= c2) { BUCKET_B(c2, c1) = k - SA; }1637k = SA + BUCKET_B(c2 = c0, c1);1638}1639assert(k < j); assert(k != NULL);1640*k-- = s;1641} else {1642assert(((s == 0) && (T[s] == c1)) || (s < 0));1643*j = ~s;1644}1645}1646}1647}16481649/* Construct the suffix array by using1650the sorted order of type B suffixes. */1651k = SA + BUCKET_A(c2 = T[n - 1]);1652*k++ = (T[n - 2] < c2) ? ~(n - 1) : (n - 1);1653/* Scan the suffix array from left to right. */1654for(i = SA, j = SA + n; i < j; ++i) {1655if(0 < (s = *i)) {1656assert(T[s - 1] >= T[s]);1657c0 = T[--s];1658if((s == 0) || (T[s - 1] < c0)) { s = ~s; }1659if(c0 != c2) {1660BUCKET_A(c2) = k - SA;1661k = SA + BUCKET_A(c2 = c0);1662}1663assert(i < k);1664*k++ = s;1665} else {1666assert(s < 0);1667*i = ~s;1668}1669}1670}16711672/* Constructs the burrows-wheeler transformed string directly1673by using the sorted order of type B* suffixes. */1674static1675int1676construct_BWT(const unsigned char *T, int *SA,1677int *bucket_A, int *bucket_B,1678int n, int m) {1679int *i, *j, *k, *orig;1680int s;1681int c0, c1, c2;16821683if(0 < m) {1684/* Construct the sorted order of type B suffixes by using1685the sorted order of type B* suffixes. */1686for(c1 = ALPHABET_SIZE - 2; 0 <= c1; --c1) {1687/* Scan the suffix array from right to left. */1688for(i = SA + BUCKET_BSTAR(c1, c1 + 1),1689j = SA + BUCKET_A(c1 + 1) - 1, k = NULL, c2 = -1;1690i <= j;1691--j) {1692if(0 < (s = *j)) {1693assert(T[s] == c1);1694assert(((s + 1) < n) && (T[s] <= T[s + 1]));1695assert(T[s - 1] <= T[s]);1696c0 = T[--s];1697*j = ~((int)c0);1698if((0 < s) && (T[s - 1] > c0)) { s = ~s; }1699if(c0 != c2) {1700if(0 <= c2) { BUCKET_B(c2, c1) = k - SA; }1701k = SA + BUCKET_B(c2 = c0, c1);1702}1703assert(k < j); assert(k != NULL);1704*k-- = s;1705} else if(s != 0) {1706*j = ~s;1707#ifndef NDEBUG1708} else {1709assert(T[s] == c1);1710#endif1711}1712}1713}1714}17151716/* Construct the BWTed string by using1717the sorted order of type B suffixes. */1718k = SA + BUCKET_A(c2 = T[n - 1]);1719*k++ = (T[n - 2] < c2) ? ~((int)T[n - 2]) : (n - 1);1720/* Scan the suffix array from left to right. */1721for(i = SA, j = SA + n, orig = SA; i < j; ++i) {1722if(0 < (s = *i)) {1723assert(T[s - 1] >= T[s]);1724c0 = T[--s];1725*i = c0;1726if((0 < s) && (T[s - 1] < c0)) { s = ~((int)T[s - 1]); }1727if(c0 != c2) {1728BUCKET_A(c2) = k - SA;1729k = SA + BUCKET_A(c2 = c0);1730}1731assert(i < k);1732*k++ = s;1733} else if(s != 0) {1734*i = ~s;1735} else {1736orig = i;1737}1738}17391740return orig - SA;1741}17421743/* Constructs the burrows-wheeler transformed string directly1744by using the sorted order of type B* suffixes. */1745static1746int1747construct_BWT_indexes(const unsigned char *T, int *SA,1748int *bucket_A, int *bucket_B,1749int n, int m,1750unsigned char * num_indexes, int * indexes) {1751int *i, *j, *k, *orig;1752int s;1753int c0, c1, c2;17541755int mod = n / 8;1756{1757mod |= mod >> 1; mod |= mod >> 2;1758mod |= mod >> 4; mod |= mod >> 8;1759mod |= mod >> 16; mod >>= 1;17601761*num_indexes = (unsigned char)((n - 1) / (mod + 1));1762}17631764if(0 < m) {1765/* Construct the sorted order of type B suffixes by using1766the sorted order of type B* suffixes. */1767for(c1 = ALPHABET_SIZE - 2; 0 <= c1; --c1) {1768/* Scan the suffix array from right to left. */1769for(i = SA + BUCKET_BSTAR(c1, c1 + 1),1770j = SA + BUCKET_A(c1 + 1) - 1, k = NULL, c2 = -1;1771i <= j;1772--j) {1773if(0 < (s = *j)) {1774assert(T[s] == c1);1775assert(((s + 1) < n) && (T[s] <= T[s + 1]));1776assert(T[s - 1] <= T[s]);17771778if ((s & mod) == 0) indexes[s / (mod + 1) - 1] = j - SA;17791780c0 = T[--s];1781*j = ~((int)c0);1782if((0 < s) && (T[s - 1] > c0)) { s = ~s; }1783if(c0 != c2) {1784if(0 <= c2) { BUCKET_B(c2, c1) = k - SA; }1785k = SA + BUCKET_B(c2 = c0, c1);1786}1787assert(k < j); assert(k != NULL);1788*k-- = s;1789} else if(s != 0) {1790*j = ~s;1791#ifndef NDEBUG1792} else {1793assert(T[s] == c1);1794#endif1795}1796}1797}1798}17991800/* Construct the BWTed string by using1801the sorted order of type B suffixes. */1802k = SA + BUCKET_A(c2 = T[n - 1]);1803if (T[n - 2] < c2) {1804if (((n - 1) & mod) == 0) indexes[(n - 1) / (mod + 1) - 1] = k - SA;1805*k++ = ~((int)T[n - 2]);1806}1807else {1808*k++ = n - 1;1809}18101811/* Scan the suffix array from left to right. */1812for(i = SA, j = SA + n, orig = SA; i < j; ++i) {1813if(0 < (s = *i)) {1814assert(T[s - 1] >= T[s]);18151816if ((s & mod) == 0) indexes[s / (mod + 1) - 1] = i - SA;18171818c0 = T[--s];1819*i = c0;1820if(c0 != c2) {1821BUCKET_A(c2) = k - SA;1822k = SA + BUCKET_A(c2 = c0);1823}1824assert(i < k);1825if((0 < s) && (T[s - 1] < c0)) {1826if ((s & mod) == 0) indexes[s / (mod + 1) - 1] = k - SA;1827*k++ = ~((int)T[s - 1]);1828} else1829*k++ = s;1830} else if(s != 0) {1831*i = ~s;1832} else {1833orig = i;1834}1835}18361837return orig - SA;1838}183918401841/*---------------------------------------------------------------------------*/18421843/*- Function -*/18441845int1846divsufsort(const unsigned char *T, int *SA, int n, int openMP) {1847int *bucket_A, *bucket_B;1848int m;1849int err = 0;18501851/* Check arguments. */1852if((T == NULL) || (SA == NULL) || (n < 0)) { return -1; }1853else if(n == 0) { return 0; }1854else if(n == 1) { SA[0] = 0; return 0; }1855else if(n == 2) { m = (T[0] < T[1]); SA[m ^ 1] = 0, SA[m] = 1; return 0; }18561857bucket_A = (int *)malloc(BUCKET_A_SIZE * sizeof(int));1858bucket_B = (int *)malloc(BUCKET_B_SIZE * sizeof(int));18591860/* Suffixsort. */1861if((bucket_A != NULL) && (bucket_B != NULL)) {1862m = sort_typeBstar(T, SA, bucket_A, bucket_B, n, openMP);1863construct_SA(T, SA, bucket_A, bucket_B, n, m);1864} else {1865err = -2;1866}18671868free(bucket_B);1869free(bucket_A);18701871return err;1872}18731874int1875divbwt(const unsigned char *T, unsigned char *U, int *A, int n, unsigned char * num_indexes, int * indexes, int openMP) {1876int *B;1877int *bucket_A, *bucket_B;1878int m, pidx, i;18791880/* Check arguments. */1881if((T == NULL) || (U == NULL) || (n < 0)) { return -1; }1882else if(n <= 1) { if(n == 1) { U[0] = T[0]; } return n; }18831884if((B = A) == NULL) { B = (int *)malloc((size_t)(n + 1) * sizeof(int)); }1885bucket_A = (int *)malloc(BUCKET_A_SIZE * sizeof(int));1886bucket_B = (int *)malloc(BUCKET_B_SIZE * sizeof(int));18871888/* Burrows-Wheeler Transform. */1889if((B != NULL) && (bucket_A != NULL) && (bucket_B != NULL)) {1890m = sort_typeBstar(T, B, bucket_A, bucket_B, n, openMP);18911892if (num_indexes == NULL || indexes == NULL) {1893pidx = construct_BWT(T, B, bucket_A, bucket_B, n, m);1894} else {1895pidx = construct_BWT_indexes(T, B, bucket_A, bucket_B, n, m, num_indexes, indexes);1896}18971898/* Copy to output string. */1899U[0] = T[n - 1];1900for(i = 0; i < pidx; ++i) { U[i + 1] = (unsigned char)B[i]; }1901for(i += 1; i < n; ++i) { U[i] = (unsigned char)B[i]; }1902pidx += 1;1903} else {1904pidx = -2;1905}19061907free(bucket_B);1908free(bucket_A);1909if(A == NULL) { free(B); }19101911return pidx;1912}191319141915