Path: blob/master/Utilities/cmzstd/lib/dictBuilder/divsufsort.c
3156 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#ifdef __clang_analyzer__43#include <string.h>44#endif4546#include "divsufsort.h"4748/*- Constants -*/49#if defined(INLINE)50# undef INLINE51#endif52#if !defined(INLINE)53# define INLINE __inline54#endif55#if defined(ALPHABET_SIZE) && (ALPHABET_SIZE < 1)56# undef ALPHABET_SIZE57#endif58#if !defined(ALPHABET_SIZE)59# define ALPHABET_SIZE (256)60#endif61#define BUCKET_A_SIZE (ALPHABET_SIZE)62#define BUCKET_B_SIZE (ALPHABET_SIZE * ALPHABET_SIZE)63#if defined(SS_INSERTIONSORT_THRESHOLD)64# if SS_INSERTIONSORT_THRESHOLD < 165# undef SS_INSERTIONSORT_THRESHOLD66# define SS_INSERTIONSORT_THRESHOLD (1)67# endif68#else69# define SS_INSERTIONSORT_THRESHOLD (8)70#endif71#if defined(SS_BLOCKSIZE)72# if SS_BLOCKSIZE < 073# undef SS_BLOCKSIZE74# define SS_BLOCKSIZE (0)75# elif 32768 <= SS_BLOCKSIZE76# undef SS_BLOCKSIZE77# define SS_BLOCKSIZE (32767)78# endif79#else80# define SS_BLOCKSIZE (1024)81#endif82/* minstacksize = log(SS_BLOCKSIZE) / log(3) * 2 */83#if SS_BLOCKSIZE == 084# define SS_MISORT_STACKSIZE (96)85#elif SS_BLOCKSIZE <= 409686# define SS_MISORT_STACKSIZE (16)87#else88# define SS_MISORT_STACKSIZE (24)89#endif90#define SS_SMERGE_STACKSIZE (32)91#define TR_INSERTIONSORT_THRESHOLD (8)92#define TR_STACKSIZE (64)939495/*- Macros -*/96#ifndef SWAP97# define SWAP(_a, _b) do { t = (_a); (_a) = (_b); (_b) = t; } while(0)98#endif /* SWAP */99#ifndef MIN100# define MIN(_a, _b) (((_a) < (_b)) ? (_a) : (_b))101#endif /* MIN */102#ifndef MAX103# define MAX(_a, _b) (((_a) > (_b)) ? (_a) : (_b))104#endif /* MAX */105#define STACK_PUSH(_a, _b, _c, _d)\106do {\107assert(ssize < STACK_SIZE);\108stack[ssize].a = (_a), stack[ssize].b = (_b),\109stack[ssize].c = (_c), stack[ssize++].d = (_d);\110} while(0)111#define STACK_PUSH5(_a, _b, _c, _d, _e)\112do {\113assert(ssize < STACK_SIZE);\114stack[ssize].a = (_a), stack[ssize].b = (_b),\115stack[ssize].c = (_c), stack[ssize].d = (_d), stack[ssize++].e = (_e);\116} while(0)117#define STACK_POP(_a, _b, _c, _d)\118do {\119assert(0 <= ssize);\120if(ssize == 0) { return; }\121(_a) = stack[--ssize].a, (_b) = stack[ssize].b,\122(_c) = stack[ssize].c, (_d) = stack[ssize].d;\123} while(0)124#define STACK_POP5(_a, _b, _c, _d, _e)\125do {\126assert(0 <= ssize);\127if(ssize == 0) { return; }\128(_a) = stack[--ssize].a, (_b) = stack[ssize].b,\129(_c) = stack[ssize].c, (_d) = stack[ssize].d, (_e) = stack[ssize].e;\130} while(0)131#define BUCKET_A(_c0) bucket_A[(_c0)]132#if ALPHABET_SIZE == 256133#define BUCKET_B(_c0, _c1) (bucket_B[((_c1) << 8) | (_c0)])134#define BUCKET_BSTAR(_c0, _c1) (bucket_B[((_c0) << 8) | (_c1)])135#else136#define BUCKET_B(_c0, _c1) (bucket_B[(_c1) * ALPHABET_SIZE + (_c0)])137#define BUCKET_BSTAR(_c0, _c1) (bucket_B[(_c0) * ALPHABET_SIZE + (_c1)])138#endif139140141/*- Private Functions -*/142143static const int lg_table[256]= {144-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,1455,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,1466,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,1476,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,1487,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,1497,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,1507,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,1517,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,7152};153154#if (SS_BLOCKSIZE == 0) || (SS_INSERTIONSORT_THRESHOLD < SS_BLOCKSIZE)155156static INLINE157int158ss_ilg(int n) {159#if SS_BLOCKSIZE == 0160return (n & 0xffff0000) ?161((n & 0xff000000) ?16224 + lg_table[(n >> 24) & 0xff] :16316 + lg_table[(n >> 16) & 0xff]) :164((n & 0x0000ff00) ?1658 + lg_table[(n >> 8) & 0xff] :1660 + lg_table[(n >> 0) & 0xff]);167#elif SS_BLOCKSIZE < 256168return lg_table[n];169#else170return (n & 0xff00) ?1718 + lg_table[(n >> 8) & 0xff] :1720 + lg_table[(n >> 0) & 0xff];173#endif174}175176#endif /* (SS_BLOCKSIZE == 0) || (SS_INSERTIONSORT_THRESHOLD < SS_BLOCKSIZE) */177178#if SS_BLOCKSIZE != 0179180static const int sqq_table[256] = {1810, 16, 22, 27, 32, 35, 39, 42, 45, 48, 50, 53, 55, 57, 59, 61,18264, 65, 67, 69, 71, 73, 75, 76, 78, 80, 81, 83, 84, 86, 87, 89,18390, 91, 93, 94, 96, 97, 98, 99, 101, 102, 103, 104, 106, 107, 108, 109,184110, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126,185128, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142,186143, 144, 144, 145, 146, 147, 148, 149, 150, 150, 151, 152, 153, 154, 155, 155,187156, 157, 158, 159, 160, 160, 161, 162, 163, 163, 164, 165, 166, 167, 167, 168,188169, 170, 170, 171, 172, 173, 173, 174, 175, 176, 176, 177, 178, 178, 179, 180,189181, 181, 182, 183, 183, 184, 185, 185, 186, 187, 187, 188, 189, 189, 190, 191,190192, 192, 193, 193, 194, 195, 195, 196, 197, 197, 198, 199, 199, 200, 201, 201,191202, 203, 203, 204, 204, 205, 206, 206, 207, 208, 208, 209, 209, 210, 211, 211,192212, 212, 213, 214, 214, 215, 215, 216, 217, 217, 218, 218, 219, 219, 220, 221,193221, 222, 222, 223, 224, 224, 225, 225, 226, 226, 227, 227, 228, 229, 229, 230,194230, 231, 231, 232, 232, 233, 234, 234, 235, 235, 236, 236, 237, 237, 238, 238,195239, 240, 240, 241, 241, 242, 242, 243, 243, 244, 244, 245, 245, 246, 246, 247,196247, 248, 248, 249, 249, 250, 250, 251, 251, 252, 252, 253, 253, 254, 254, 255197};198199static INLINE200int201ss_isqrt(int x) {202int y, e;203204if(x >= (SS_BLOCKSIZE * SS_BLOCKSIZE)) { return SS_BLOCKSIZE; }205e = (x & 0xffff0000) ?206((x & 0xff000000) ?20724 + lg_table[(x >> 24) & 0xff] :20816 + lg_table[(x >> 16) & 0xff]) :209((x & 0x0000ff00) ?2108 + lg_table[(x >> 8) & 0xff] :2110 + lg_table[(x >> 0) & 0xff]);212213if(e >= 16) {214y = sqq_table[x >> ((e - 6) - (e & 1))] << ((e >> 1) - 7);215if(e >= 24) { y = (y + 1 + x / y) >> 1; }216y = (y + 1 + x / y) >> 1;217} else if(e >= 8) {218y = (sqq_table[x >> ((e - 6) - (e & 1))] >> (7 - (e >> 1))) + 1;219} else {220return sqq_table[x] >> 4;221}222223return (x < (y * y)) ? y - 1 : y;224}225226#endif /* SS_BLOCKSIZE != 0 */227228229/*---------------------------------------------------------------------------*/230231/* Compares two suffixes. */232static INLINE233int234ss_compare(const unsigned char *T,235const int *p1, const int *p2,236int depth) {237const unsigned char *U1, *U2, *U1n, *U2n;238239for(U1 = T + depth + *p1,240U2 = T + depth + *p2,241U1n = T + *(p1 + 1) + 2,242U2n = T + *(p2 + 1) + 2;243(U1 < U1n) && (U2 < U2n) && (*U1 == *U2);244++U1, ++U2) {245}246247return U1 < U1n ?248(U2 < U2n ? *U1 - *U2 : 1) :249(U2 < U2n ? -1 : 0);250}251252253/*---------------------------------------------------------------------------*/254255#if (SS_BLOCKSIZE != 1) && (SS_INSERTIONSORT_THRESHOLD != 1)256257/* Insertionsort for small size groups */258static259void260ss_insertionsort(const unsigned char *T, const int *PA,261int *first, int *last, int depth) {262int *i, *j;263int t;264int r;265266for(i = last - 2; first <= i; --i) {267for(t = *i, j = i + 1; 0 < (r = ss_compare(T, PA + t, PA + *j, depth));) {268do { *(j - 1) = *j; } while((++j < last) && (*j < 0));269if(last <= j) { break; }270}271if(r == 0) { *j = ~*j; }272*(j - 1) = t;273}274}275276#endif /* (SS_BLOCKSIZE != 1) && (SS_INSERTIONSORT_THRESHOLD != 1) */277278279/*---------------------------------------------------------------------------*/280281#if (SS_BLOCKSIZE == 0) || (SS_INSERTIONSORT_THRESHOLD < SS_BLOCKSIZE)282283static INLINE284void285ss_fixdown(const unsigned char *Td, const int *PA,286int *SA, int i, int size) {287int j, k;288int v;289int c, d, e;290291for(v = SA[i], c = Td[PA[v]]; (j = 2 * i + 1) < size; SA[i] = SA[k], i = k) {292d = Td[PA[SA[k = j++]]];293if(d < (e = Td[PA[SA[j]]])) { k = j; d = e; }294if(d <= c) { break; }295}296SA[i] = v;297}298299/* Simple top-down heapsort. */300static301void302ss_heapsort(const unsigned char *Td, const int *PA, int *SA, int size) {303int i, m;304int t;305306m = size;307if((size % 2) == 0) {308m--;309if(Td[PA[SA[m / 2]]] < Td[PA[SA[m]]]) { SWAP(SA[m], SA[m / 2]); }310}311312for(i = m / 2 - 1; 0 <= i; --i) { ss_fixdown(Td, PA, SA, i, m); }313if((size % 2) == 0) { SWAP(SA[0], SA[m]); ss_fixdown(Td, PA, SA, 0, m); }314for(i = m - 1; 0 < i; --i) {315t = SA[0], SA[0] = SA[i];316ss_fixdown(Td, PA, SA, 0, i);317SA[i] = t;318}319}320321322/*---------------------------------------------------------------------------*/323324/* Returns the median of three elements. */325static INLINE326int *327ss_median3(const unsigned char *Td, const int *PA,328int *v1, int *v2, int *v3) {329int *t;330if(Td[PA[*v1]] > Td[PA[*v2]]) { SWAP(v1, v2); }331if(Td[PA[*v2]] > Td[PA[*v3]]) {332if(Td[PA[*v1]] > Td[PA[*v3]]) { return v1; }333else { return v3; }334}335return v2;336}337338/* Returns the median of five elements. */339static INLINE340int *341ss_median5(const unsigned char *Td, const int *PA,342int *v1, int *v2, int *v3, int *v4, int *v5) {343int *t;344if(Td[PA[*v2]] > Td[PA[*v3]]) { SWAP(v2, v3); }345if(Td[PA[*v4]] > Td[PA[*v5]]) { SWAP(v4, v5); }346if(Td[PA[*v2]] > Td[PA[*v4]]) { SWAP(v2, v4); SWAP(v3, v5); }347if(Td[PA[*v1]] > Td[PA[*v3]]) { SWAP(v1, v3); }348if(Td[PA[*v1]] > Td[PA[*v4]]) { SWAP(v1, v4); SWAP(v3, v5); }349if(Td[PA[*v3]] > Td[PA[*v4]]) { return v4; }350return v3;351}352353/* Returns the pivot element. */354static INLINE355int *356ss_pivot(const unsigned char *Td, const int *PA, int *first, int *last) {357int *middle;358int t;359360t = last - first;361middle = first + t / 2;362363if(t <= 512) {364if(t <= 32) {365return ss_median3(Td, PA, first, middle, last - 1);366} else {367t >>= 2;368return ss_median5(Td, PA, first, first + t, middle, last - 1 - t, last - 1);369}370}371t >>= 3;372first = ss_median3(Td, PA, first, first + t, first + (t << 1));373middle = ss_median3(Td, PA, middle - t, middle, middle + t);374last = ss_median3(Td, PA, last - 1 - (t << 1), last - 1 - t, last - 1);375return ss_median3(Td, PA, first, middle, last);376}377378379/*---------------------------------------------------------------------------*/380381/* Binary partition for substrings. */382static INLINE383int *384ss_partition(const int *PA,385int *first, int *last, int depth) {386int *a, *b;387int t;388for(a = first - 1, b = last;;) {389for(; (++a < b) && ((PA[*a] + depth) >= (PA[*a + 1] + 1));) { *a = ~*a; }390for(; (a < --b) && ((PA[*b] + depth) < (PA[*b + 1] + 1));) { }391if(b <= a) { break; }392t = ~*b;393*b = *a;394*a = t;395}396if(first < a) { *first = ~*first; }397return a;398}399400/* Multikey introsort for medium size groups. */401static402void403ss_mintrosort(const unsigned char *T, const int *PA,404int *first, int *last,405int depth) {406#define STACK_SIZE SS_MISORT_STACKSIZE407struct { int *a, *b, c; int d; } stack[STACK_SIZE];408const unsigned char *Td;409int *a, *b, *c, *d, *e, *f;410int s, t;411int ssize;412int limit;413int v, x = 0;414415for(ssize = 0, limit = ss_ilg(last - first);;) {416417if((last - first) <= SS_INSERTIONSORT_THRESHOLD) {418#if 1 < SS_INSERTIONSORT_THRESHOLD419if(1 < (last - first)) { ss_insertionsort(T, PA, first, last, depth); }420#endif421STACK_POP(first, last, depth, limit);422continue;423}424425Td = T + depth;426if(limit-- == 0) { ss_heapsort(Td, PA, first, last - first); }427if(limit < 0) {428for(a = first + 1, v = Td[PA[*first]]; a < last; ++a) {429if((x = Td[PA[*a]]) != v) {430if(1 < (a - first)) { break; }431v = x;432first = a;433}434}435if(Td[PA[*first] - 1] < v) {436first = ss_partition(PA, first, a, depth);437}438if((a - first) <= (last - a)) {439if(1 < (a - first)) {440STACK_PUSH(a, last, depth, -1);441last = a, depth += 1, limit = ss_ilg(a - first);442} else {443first = a, limit = -1;444}445} else {446if(1 < (last - a)) {447STACK_PUSH(first, a, depth + 1, ss_ilg(a - first));448first = a, limit = -1;449} else {450last = a, depth += 1, limit = ss_ilg(a - first);451}452}453continue;454}455456/* choose pivot */457a = ss_pivot(Td, PA, first, last);458v = Td[PA[*a]];459SWAP(*first, *a);460461/* partition */462for(b = first; (++b < last) && ((x = Td[PA[*b]]) == v);) { }463if(((a = b) < last) && (x < v)) {464for(; (++b < last) && ((x = Td[PA[*b]]) <= v);) {465if(x == v) { SWAP(*b, *a); ++a; }466}467}468for(c = last; (b < --c) && ((x = Td[PA[*c]]) == v);) { }469if((b < (d = c)) && (x > v)) {470for(; (b < --c) && ((x = Td[PA[*c]]) >= v);) {471if(x == v) { SWAP(*c, *d); --d; }472}473}474for(; b < c;) {475SWAP(*b, *c);476for(; (++b < c) && ((x = Td[PA[*b]]) <= v);) {477if(x == v) { SWAP(*b, *a); ++a; }478}479for(; (b < --c) && ((x = Td[PA[*c]]) >= v);) {480if(x == v) { SWAP(*c, *d); --d; }481}482}483484if(a <= d) {485c = b - 1;486487if((s = a - first) > (t = b - a)) { s = t; }488for(e = first, f = b - s; 0 < s; --s, ++e, ++f) { SWAP(*e, *f); }489if((s = d - c) > (t = last - d - 1)) { s = t; }490for(e = b, f = last - s; 0 < s; --s, ++e, ++f) { SWAP(*e, *f); }491492a = first + (b - a), c = last - (d - c);493b = (v <= Td[PA[*a] - 1]) ? a : ss_partition(PA, a, c, depth);494495if((a - first) <= (last - c)) {496if((last - c) <= (c - b)) {497STACK_PUSH(b, c, depth + 1, ss_ilg(c - b));498STACK_PUSH(c, last, depth, limit);499last = a;500} else if((a - first) <= (c - b)) {501STACK_PUSH(c, last, depth, limit);502STACK_PUSH(b, c, depth + 1, ss_ilg(c - b));503last = a;504} else {505STACK_PUSH(c, last, depth, limit);506STACK_PUSH(first, a, depth, limit);507first = b, last = c, depth += 1, limit = ss_ilg(c - b);508}509} else {510if((a - first) <= (c - b)) {511STACK_PUSH(b, c, depth + 1, ss_ilg(c - b));512STACK_PUSH(first, a, depth, limit);513first = c;514} else if((last - c) <= (c - b)) {515STACK_PUSH(first, a, depth, limit);516STACK_PUSH(b, c, depth + 1, ss_ilg(c - b));517first = c;518} else {519STACK_PUSH(first, a, depth, limit);520STACK_PUSH(c, last, depth, limit);521first = b, last = c, depth += 1, limit = ss_ilg(c - b);522}523}524} else {525limit += 1;526if(Td[PA[*first] - 1] < v) {527first = ss_partition(PA, first, last, depth);528limit = ss_ilg(last - first);529}530depth += 1;531}532}533#undef STACK_SIZE534}535536#endif /* (SS_BLOCKSIZE == 0) || (SS_INSERTIONSORT_THRESHOLD < SS_BLOCKSIZE) */537538539/*---------------------------------------------------------------------------*/540541#if SS_BLOCKSIZE != 0542543static INLINE544void545ss_blockswap(int *a, int *b, int n) {546int t;547for(; 0 < n; --n, ++a, ++b) {548t = *a, *a = *b, *b = t;549}550}551552static INLINE553void554ss_rotate(int *first, int *middle, int *last) {555int *a, *b, t;556int l, r;557l = middle - first, r = last - middle;558for(; (0 < l) && (0 < r);) {559if(l == r) { ss_blockswap(first, middle, l); break; }560if(l < r) {561a = last - 1, b = middle - 1;562t = *a;563do {564*a-- = *b, *b-- = *a;565if(b < first) {566*a = t;567last = a;568if((r -= l + 1) <= l) { break; }569a -= 1, b = middle - 1;570t = *a;571}572} while(1);573} else {574a = first, b = middle;575t = *a;576do {577*a++ = *b, *b++ = *a;578if(last <= b) {579*a = t;580first = a + 1;581if((l -= r + 1) <= r) { break; }582a += 1, b = middle;583t = *a;584}585} while(1);586}587}588}589590591/*---------------------------------------------------------------------------*/592593static594void595ss_inplacemerge(const unsigned char *T, const int *PA,596int *first, int *middle, int *last,597int depth) {598const int *p;599int *a, *b;600int len, half;601int q, r;602int x;603604for(;;) {605if(*(last - 1) < 0) { x = 1; p = PA + ~*(last - 1); }606else { x = 0; p = PA + *(last - 1); }607for(a = first, len = middle - first, half = len >> 1, r = -1;6080 < len;609len = half, half >>= 1) {610b = a + half;611q = ss_compare(T, PA + ((0 <= *b) ? *b : ~*b), p, depth);612if(q < 0) {613a = b + 1;614half -= (len & 1) ^ 1;615} else {616r = q;617}618}619if(a < middle) {620if(r == 0) { *a = ~*a; }621ss_rotate(a, middle, last);622last -= middle - a;623middle = a;624if(first == middle) { break; }625}626--last;627if(x != 0) { while(*--last < 0) { } }628if(middle == last) { break; }629}630}631632633/*---------------------------------------------------------------------------*/634635/* Merge-forward with internal buffer. */636static637void638ss_mergeforward(const unsigned char *T, const int *PA,639int *first, int *middle, int *last,640int *buf, int depth) {641int *a, *b, *c, *bufend;642int t;643int r;644645bufend = buf + (middle - first) - 1;646ss_blockswap(buf, first, middle - first);647648for(t = *(a = first), b = buf, c = middle;;) {649r = ss_compare(T, PA + *b, PA + *c, depth);650if(r < 0) {651do {652*a++ = *b;653if(bufend <= b) { *bufend = t; return; }654*b++ = *a;655} while(*b < 0);656} else if(r > 0) {657do {658*a++ = *c, *c++ = *a;659if(last <= c) {660while(b < bufend) { *a++ = *b, *b++ = *a; }661*a = *b, *b = t;662return;663}664} while(*c < 0);665} else {666*c = ~*c;667do {668*a++ = *b;669if(bufend <= b) { *bufend = t; return; }670*b++ = *a;671} while(*b < 0);672673do {674*a++ = *c, *c++ = *a;675if(last <= c) {676while(b < bufend) { *a++ = *b, *b++ = *a; }677*a = *b, *b = t;678return;679}680} while(*c < 0);681}682}683}684685/* Merge-backward with internal buffer. */686static687void688ss_mergebackward(const unsigned char *T, const int *PA,689int *first, int *middle, int *last,690int *buf, int depth) {691const int *p1, *p2;692int *a, *b, *c, *bufend;693int t;694int r;695int x;696697bufend = buf + (last - middle) - 1;698ss_blockswap(buf, middle, last - middle);699700x = 0;701if(*bufend < 0) { p1 = PA + ~*bufend; x |= 1; }702else { p1 = PA + *bufend; }703if(*(middle - 1) < 0) { p2 = PA + ~*(middle - 1); x |= 2; }704else { p2 = PA + *(middle - 1); }705for(t = *(a = last - 1), b = bufend, c = middle - 1;;) {706r = ss_compare(T, p1, p2, depth);707if(0 < r) {708if(x & 1) { do { *a-- = *b, *b-- = *a; } while(*b < 0); x ^= 1; }709*a-- = *b;710if(b <= buf) { *buf = t; break; }711*b-- = *a;712if(*b < 0) { p1 = PA + ~*b; x |= 1; }713else { p1 = PA + *b; }714} else if(r < 0) {715if(x & 2) { do { *a-- = *c, *c-- = *a; } while(*c < 0); x ^= 2; }716*a-- = *c, *c-- = *a;717if(c < first) {718while(buf < b) { *a-- = *b, *b-- = *a; }719*a = *b, *b = t;720break;721}722if(*c < 0) { p2 = PA + ~*c; x |= 2; }723else { p2 = PA + *c; }724} else {725if(x & 1) { do { *a-- = *b, *b-- = *a; } while(*b < 0); x ^= 1; }726*a-- = ~*b;727if(b <= buf) { *buf = t; break; }728*b-- = *a;729if(x & 2) { do { *a-- = *c, *c-- = *a; } while(*c < 0); x ^= 2; }730*a-- = *c, *c-- = *a;731if(c < first) {732while(buf < b) { *a-- = *b, *b-- = *a; }733*a = *b, *b = t;734break;735}736if(*b < 0) { p1 = PA + ~*b; x |= 1; }737else { p1 = PA + *b; }738if(*c < 0) { p2 = PA + ~*c; x |= 2; }739else { p2 = PA + *c; }740}741}742}743744/* D&C based merge. */745static746void747ss_swapmerge(const unsigned char *T, const int *PA,748int *first, int *middle, int *last,749int *buf, int bufsize, int depth) {750#define STACK_SIZE SS_SMERGE_STACKSIZE751#define GETIDX(a) ((0 <= (a)) ? (a) : (~(a)))752#define MERGE_CHECK(a, b, c)\753do {\754if(((c) & 1) ||\755(((c) & 2) && (ss_compare(T, PA + GETIDX(*((a) - 1)), PA + *(a), depth) == 0))) {\756*(a) = ~*(a);\757}\758if(((c) & 4) && ((ss_compare(T, PA + GETIDX(*((b) - 1)), PA + *(b), depth) == 0))) {\759*(b) = ~*(b);\760}\761} while(0)762struct { int *a, *b, *c; int d; } stack[STACK_SIZE];763int *l, *r, *lm, *rm;764int m, len, half;765int ssize;766int check, next;767768for(check = 0, ssize = 0;;) {769if((last - middle) <= bufsize) {770if((first < middle) && (middle < last)) {771ss_mergebackward(T, PA, first, middle, last, buf, depth);772}773MERGE_CHECK(first, last, check);774STACK_POP(first, middle, last, check);775continue;776}777778if((middle - first) <= bufsize) {779if(first < middle) {780ss_mergeforward(T, PA, first, middle, last, buf, depth);781}782MERGE_CHECK(first, last, check);783STACK_POP(first, middle, last, check);784continue;785}786787for(m = 0, len = MIN(middle - first, last - middle), half = len >> 1;7880 < len;789len = half, half >>= 1) {790if(ss_compare(T, PA + GETIDX(*(middle + m + half)),791PA + GETIDX(*(middle - m - half - 1)), depth) < 0) {792m += half + 1;793half -= (len & 1) ^ 1;794}795}796797if(0 < m) {798lm = middle - m, rm = middle + m;799ss_blockswap(lm, middle, m);800l = r = middle, next = 0;801if(rm < last) {802if(*rm < 0) {803*rm = ~*rm;804if(first < lm) { for(; *--l < 0;) { } next |= 4; }805next |= 1;806} else if(first < lm) {807for(; *r < 0; ++r) { }808next |= 2;809}810}811812if((l - first) <= (last - r)) {813STACK_PUSH(r, rm, last, (next & 3) | (check & 4));814middle = lm, last = l, check = (check & 3) | (next & 4);815} else {816if((next & 2) && (r == middle)) { next ^= 6; }817STACK_PUSH(first, lm, l, (check & 3) | (next & 4));818first = r, middle = rm, check = (next & 3) | (check & 4);819}820} else {821if(ss_compare(T, PA + GETIDX(*(middle - 1)), PA + *middle, depth) == 0) {822*middle = ~*middle;823}824MERGE_CHECK(first, last, check);825STACK_POP(first, middle, last, check);826}827}828#undef STACK_SIZE829}830831#endif /* SS_BLOCKSIZE != 0 */832833834/*---------------------------------------------------------------------------*/835836/* Substring sort */837static838void839sssort(const unsigned char *T, const int *PA,840int *first, int *last,841int *buf, int bufsize,842int depth, int n, int lastsuffix) {843int *a;844#if SS_BLOCKSIZE != 0845int *b, *middle, *curbuf;846int j, k, curbufsize, limit;847#endif848int i;849850if(lastsuffix != 0) { ++first; }851852#if SS_BLOCKSIZE == 0853ss_mintrosort(T, PA, first, last, depth);854#else855if((bufsize < SS_BLOCKSIZE) &&856(bufsize < (last - first)) &&857(bufsize < (limit = ss_isqrt(last - first)))) {858if(SS_BLOCKSIZE < limit) { limit = SS_BLOCKSIZE; }859buf = middle = last - limit, bufsize = limit;860} else {861middle = last, limit = 0;862}863for(a = first, i = 0; SS_BLOCKSIZE < (middle - a); a += SS_BLOCKSIZE, ++i) {864#if SS_INSERTIONSORT_THRESHOLD < SS_BLOCKSIZE865ss_mintrosort(T, PA, a, a + SS_BLOCKSIZE, depth);866#elif 1 < SS_BLOCKSIZE867ss_insertionsort(T, PA, a, a + SS_BLOCKSIZE, depth);868#endif869curbufsize = last - (a + SS_BLOCKSIZE);870curbuf = a + SS_BLOCKSIZE;871if(curbufsize <= bufsize) { curbufsize = bufsize, curbuf = buf; }872for(b = a, k = SS_BLOCKSIZE, j = i; j & 1; b -= k, k <<= 1, j >>= 1) {873ss_swapmerge(T, PA, b - k, b, b + k, curbuf, curbufsize, depth);874}875}876#if SS_INSERTIONSORT_THRESHOLD < SS_BLOCKSIZE877ss_mintrosort(T, PA, a, middle, depth);878#elif 1 < SS_BLOCKSIZE879ss_insertionsort(T, PA, a, middle, depth);880#endif881for(k = SS_BLOCKSIZE; i != 0; k <<= 1, i >>= 1) {882if(i & 1) {883ss_swapmerge(T, PA, a - k, a, middle, buf, bufsize, depth);884a -= k;885}886}887if(limit != 0) {888#if SS_INSERTIONSORT_THRESHOLD < SS_BLOCKSIZE889ss_mintrosort(T, PA, middle, last, depth);890#elif 1 < SS_BLOCKSIZE891ss_insertionsort(T, PA, middle, last, depth);892#endif893ss_inplacemerge(T, PA, first, middle, last, depth);894}895#endif896897if(lastsuffix != 0) {898/* Insert last type B* suffix. */899int PAi[2]; PAi[0] = PA[*(first - 1)], PAi[1] = n - 2;900for(a = first, i = *(first - 1);901(a < last) && ((*a < 0) || (0 < ss_compare(T, &(PAi[0]), PA + *a, depth)));902++a) {903*(a - 1) = *a;904}905*(a - 1) = i;906}907}908909910/*---------------------------------------------------------------------------*/911912static INLINE913int914tr_ilg(int n) {915return (n & 0xffff0000) ?916((n & 0xff000000) ?91724 + lg_table[(n >> 24) & 0xff] :91816 + lg_table[(n >> 16) & 0xff]) :919((n & 0x0000ff00) ?9208 + lg_table[(n >> 8) & 0xff] :9210 + lg_table[(n >> 0) & 0xff]);922}923924925/*---------------------------------------------------------------------------*/926927/* Simple insertionsort for small size groups. */928static929void930tr_insertionsort(const int *ISAd, int *first, int *last) {931int *a, *b;932int t, r;933934for(a = first + 1; a < last; ++a) {935for(t = *a, b = a - 1; 0 > (r = ISAd[t] - ISAd[*b]);) {936do { *(b + 1) = *b; } while((first <= --b) && (*b < 0));937if(b < first) { break; }938}939if(r == 0) { *b = ~*b; }940*(b + 1) = t;941}942}943944945/*---------------------------------------------------------------------------*/946947static INLINE948void949tr_fixdown(const int *ISAd, int *SA, int i, int size) {950int j, k;951int v;952int c, d, e;953954for(v = SA[i], c = ISAd[v]; (j = 2 * i + 1) < size; SA[i] = SA[k], i = k) {955d = ISAd[SA[k = j++]];956if(d < (e = ISAd[SA[j]])) { k = j; d = e; }957if(d <= c) { break; }958}959SA[i] = v;960}961962/* Simple top-down heapsort. */963static964void965tr_heapsort(const int *ISAd, int *SA, int size) {966int i, m;967int t;968969m = size;970if((size % 2) == 0) {971m--;972if(ISAd[SA[m / 2]] < ISAd[SA[m]]) { SWAP(SA[m], SA[m / 2]); }973}974975for(i = m / 2 - 1; 0 <= i; --i) { tr_fixdown(ISAd, SA, i, m); }976if((size % 2) == 0) { SWAP(SA[0], SA[m]); tr_fixdown(ISAd, SA, 0, m); }977for(i = m - 1; 0 < i; --i) {978t = SA[0], SA[0] = SA[i];979tr_fixdown(ISAd, SA, 0, i);980SA[i] = t;981}982}983984985/*---------------------------------------------------------------------------*/986987/* Returns the median of three elements. */988static INLINE989int *990tr_median3(const int *ISAd, int *v1, int *v2, int *v3) {991int *t;992if(ISAd[*v1] > ISAd[*v2]) { SWAP(v1, v2); }993if(ISAd[*v2] > ISAd[*v3]) {994if(ISAd[*v1] > ISAd[*v3]) { return v1; }995else { return v3; }996}997return v2;998}9991000/* Returns the median of five elements. */1001static INLINE1002int *1003tr_median5(const int *ISAd,1004int *v1, int *v2, int *v3, int *v4, int *v5) {1005int *t;1006if(ISAd[*v2] > ISAd[*v3]) { SWAP(v2, v3); }1007if(ISAd[*v4] > ISAd[*v5]) { SWAP(v4, v5); }1008if(ISAd[*v2] > ISAd[*v4]) { SWAP(v2, v4); SWAP(v3, v5); }1009if(ISAd[*v1] > ISAd[*v3]) { SWAP(v1, v3); }1010if(ISAd[*v1] > ISAd[*v4]) { SWAP(v1, v4); SWAP(v3, v5); }1011if(ISAd[*v3] > ISAd[*v4]) { return v4; }1012return v3;1013}10141015/* Returns the pivot element. */1016static INLINE1017int *1018tr_pivot(const int *ISAd, int *first, int *last) {1019int *middle;1020int t;10211022t = last - first;1023middle = first + t / 2;10241025if(t <= 512) {1026if(t <= 32) {1027return tr_median3(ISAd, first, middle, last - 1);1028} else {1029t >>= 2;1030return tr_median5(ISAd, first, first + t, middle, last - 1 - t, last - 1);1031}1032}1033t >>= 3;1034first = tr_median3(ISAd, first, first + t, first + (t << 1));1035middle = tr_median3(ISAd, middle - t, middle, middle + t);1036last = tr_median3(ISAd, last - 1 - (t << 1), last - 1 - t, last - 1);1037return tr_median3(ISAd, first, middle, last);1038}103910401041/*---------------------------------------------------------------------------*/10421043typedef struct _trbudget_t trbudget_t;1044struct _trbudget_t {1045int chance;1046int remain;1047int incval;1048int count;1049};10501051static INLINE1052void1053trbudget_init(trbudget_t *budget, int chance, int incval) {1054budget->chance = chance;1055budget->remain = budget->incval = incval;1056}10571058static INLINE1059int1060trbudget_check(trbudget_t *budget, int size) {1061if(size <= budget->remain) { budget->remain -= size; return 1; }1062if(budget->chance == 0) { budget->count += size; return 0; }1063budget->remain += budget->incval - size;1064budget->chance -= 1;1065return 1;1066}106710681069/*---------------------------------------------------------------------------*/10701071static INLINE1072void1073tr_partition(const int *ISAd,1074int *first, int *middle, int *last,1075int **pa, int **pb, int v) {1076int *a, *b, *c, *d, *e, *f;1077int t, s;1078int x = 0;10791080for(b = middle - 1; (++b < last) && ((x = ISAd[*b]) == v);) { }1081if(((a = b) < last) && (x < v)) {1082for(; (++b < last) && ((x = ISAd[*b]) <= v);) {1083if(x == v) { SWAP(*b, *a); ++a; }1084}1085}1086for(c = last; (b < --c) && ((x = ISAd[*c]) == v);) { }1087if((b < (d = c)) && (x > v)) {1088for(; (b < --c) && ((x = ISAd[*c]) >= v);) {1089if(x == v) { SWAP(*c, *d); --d; }1090}1091}1092for(; b < c;) {1093SWAP(*b, *c);1094for(; (++b < c) && ((x = ISAd[*b]) <= v);) {1095if(x == v) { SWAP(*b, *a); ++a; }1096}1097for(; (b < --c) && ((x = ISAd[*c]) >= v);) {1098if(x == v) { SWAP(*c, *d); --d; }1099}1100}11011102if(a <= d) {1103c = b - 1;1104if((s = a - first) > (t = b - a)) { s = t; }1105for(e = first, f = b - s; 0 < s; --s, ++e, ++f) { SWAP(*e, *f); }1106if((s = d - c) > (t = last - d - 1)) { s = t; }1107for(e = b, f = last - s; 0 < s; --s, ++e, ++f) { SWAP(*e, *f); }1108first += (b - a), last -= (d - c);1109}1110*pa = first, *pb = last;1111}11121113static1114void1115tr_copy(int *ISA, const int *SA,1116int *first, int *a, int *b, int *last,1117int depth) {1118/* sort suffixes of middle partition1119by using sorted order of suffixes of left and right partition. */1120int *c, *d, *e;1121int s, v;11221123v = b - SA - 1;1124for(c = first, d = a - 1; c <= d; ++c) {1125#ifdef __clang_analyzer__1126assert(c);1127#endif1128if((0 <= (s = *c - depth)) && (ISA[s] == v)) {1129*++d = s;1130ISA[s] = d - SA;1131}1132}1133for(c = last - 1, e = d + 1, d = b; e < d; --c) {1134if((0 <= (s = *c - depth)) && (ISA[s] == v)) {1135*--d = s;1136ISA[s] = d - SA;1137}1138}1139}11401141static1142void1143tr_partialcopy(int *ISA, const int *SA,1144int *first, int *a, int *b, int *last,1145int depth) {1146int *c, *d, *e;1147int s, v;1148int rank, lastrank, newrank = -1;11491150v = b - SA - 1;1151lastrank = -1;1152for(c = first, d = a - 1; c <= d; ++c) {1153if((0 <= (s = *c - depth)) && (ISA[s] == v)) {1154*++d = s;1155rank = ISA[s + depth];1156if(lastrank != rank) { lastrank = rank; newrank = d - SA; }1157ISA[s] = newrank;1158}1159}11601161lastrank = -1;1162for(e = d; first <= e; --e) {1163rank = ISA[*e];1164if(lastrank != rank) { lastrank = rank; newrank = e - SA; }1165if(newrank != rank) { ISA[*e] = newrank; }1166}11671168lastrank = -1;1169for(c = last - 1, e = d + 1, d = b; e < d; --c) {1170if((0 <= (s = *c - depth)) && (ISA[s] == v)) {1171*--d = s;1172rank = ISA[s + depth];1173if(lastrank != rank) { lastrank = rank; newrank = d - SA; }1174ISA[s] = newrank;1175}1176}1177}11781179static1180void1181tr_introsort(int *ISA, const int *ISAd,1182int *SA, int *first, int *last,1183trbudget_t *budget) {1184#define STACK_SIZE TR_STACKSIZE1185struct { const int *a; int *b, *c; int d, e; }stack[STACK_SIZE];1186int *a, *b, *c;1187int t;1188int v, x = 0;1189int incr = ISAd - ISA;1190int limit, next;1191int ssize, trlink = -1;11921193#ifdef __clang_analyzer__1194memset(stack, 0, sizeof(stack));1195#endif11961197for(ssize = 0, limit = tr_ilg(last - first);;) {11981199if(limit < 0) {1200if(limit == -1) {1201/* tandem repeat partition */1202tr_partition(ISAd - incr, first, first, last, &a, &b, last - SA - 1);12031204/* update ranks */1205if(a < last) {1206for(c = first, v = a - SA - 1; c < a; ++c) { ISA[*c] = v; }1207}1208if(b < last) {1209for(c = a, v = b - SA - 1; c < b; ++c) { ISA[*c] = v; }1210}12111212/* push */1213if(1 < (b - a)) {1214STACK_PUSH5(NULL, a, b, 0, 0);1215STACK_PUSH5(ISAd - incr, first, last, -2, trlink);1216trlink = ssize - 2;1217}1218if((a - first) <= (last - b)) {1219if(1 < (a - first)) {1220STACK_PUSH5(ISAd, b, last, tr_ilg(last - b), trlink);1221last = a, limit = tr_ilg(a - first);1222} else if(1 < (last - b)) {1223first = b, limit = tr_ilg(last - b);1224} else {1225STACK_POP5(ISAd, first, last, limit, trlink);1226}1227} else {1228if(1 < (last - b)) {1229STACK_PUSH5(ISAd, first, a, tr_ilg(a - first), trlink);1230first = b, limit = tr_ilg(last - b);1231} else if(1 < (a - first)) {1232last = a, limit = tr_ilg(a - first);1233} else {1234STACK_POP5(ISAd, first, last, limit, trlink);1235}1236}1237} else if(limit == -2) {1238/* tandem repeat copy */1239a = stack[--ssize].b, b = stack[ssize].c;1240if(stack[ssize].d == 0) {1241tr_copy(ISA, SA, first, a, b, last, ISAd - ISA);1242} else {1243if(0 <= trlink) { stack[trlink].d = -1; }1244tr_partialcopy(ISA, SA, first, a, b, last, ISAd - ISA);1245}1246STACK_POP5(ISAd, first, last, limit, trlink);1247} else {1248/* sorted partition */1249if(0 <= *first) {1250a = first;1251do { ISA[*a] = a - SA; } while((++a < last) && (0 <= *a));1252first = a;1253}1254if(first < last) {1255a = first; do { *a = ~*a; } while(*++a < 0);1256next = (ISA[*a] != ISAd[*a]) ? tr_ilg(a - first + 1) : -1;1257if(++a < last) { for(b = first, v = a - SA - 1; b < a; ++b) { ISA[*b] = v; } }12581259/* push */1260if(trbudget_check(budget, a - first)) {1261if((a - first) <= (last - a)) {1262STACK_PUSH5(ISAd, a, last, -3, trlink);1263ISAd += incr, last = a, limit = next;1264} else {1265if(1 < (last - a)) {1266STACK_PUSH5(ISAd + incr, first, a, next, trlink);1267first = a, limit = -3;1268} else {1269ISAd += incr, last = a, limit = next;1270}1271}1272} else {1273if(0 <= trlink) { stack[trlink].d = -1; }1274if(1 < (last - a)) {1275first = a, limit = -3;1276} else {1277STACK_POP5(ISAd, first, last, limit, trlink);1278}1279}1280} else {1281STACK_POP5(ISAd, first, last, limit, trlink);1282}1283}1284continue;1285}12861287if((last - first) <= TR_INSERTIONSORT_THRESHOLD) {1288tr_insertionsort(ISAd, first, last);1289limit = -3;1290continue;1291}12921293if(limit-- == 0) {1294tr_heapsort(ISAd, first, last - first);1295for(a = last - 1; first < a; a = b) {1296for(x = ISAd[*a], b = a - 1; (first <= b) && (ISAd[*b] == x); --b) { *b = ~*b; }1297}1298limit = -3;1299continue;1300}13011302/* choose pivot */1303a = tr_pivot(ISAd, first, last);1304SWAP(*first, *a);1305v = ISAd[*first];13061307/* partition */1308tr_partition(ISAd, first, first + 1, last, &a, &b, v);1309if((last - first) != (b - a)) {1310next = (ISA[*a] != v) ? tr_ilg(b - a) : -1;13111312/* update ranks */1313for(c = first, v = a - SA - 1; c < a; ++c) { ISA[*c] = v; }1314if(b < last) { for(c = a, v = b - SA - 1; c < b; ++c) { ISA[*c] = v; } }13151316/* push */1317if((1 < (b - a)) && (trbudget_check(budget, b - a))) {1318if((a - first) <= (last - b)) {1319if((last - b) <= (b - a)) {1320if(1 < (a - first)) {1321STACK_PUSH5(ISAd + incr, a, b, next, trlink);1322STACK_PUSH5(ISAd, b, last, limit, trlink);1323last = a;1324} else if(1 < (last - b)) {1325STACK_PUSH5(ISAd + incr, a, b, next, trlink);1326first = b;1327} else {1328ISAd += incr, first = a, last = b, limit = next;1329}1330} else if((a - first) <= (b - a)) {1331if(1 < (a - first)) {1332STACK_PUSH5(ISAd, b, last, limit, trlink);1333STACK_PUSH5(ISAd + incr, a, b, next, trlink);1334last = a;1335} else {1336STACK_PUSH5(ISAd, b, last, limit, trlink);1337ISAd += incr, first = a, last = b, limit = next;1338}1339} else {1340STACK_PUSH5(ISAd, b, last, limit, trlink);1341STACK_PUSH5(ISAd, first, a, limit, trlink);1342ISAd += incr, first = a, last = b, limit = next;1343}1344} else {1345if((a - first) <= (b - a)) {1346if(1 < (last - b)) {1347STACK_PUSH5(ISAd + incr, a, b, next, trlink);1348STACK_PUSH5(ISAd, first, a, limit, trlink);1349first = b;1350} else if(1 < (a - first)) {1351STACK_PUSH5(ISAd + incr, a, b, next, trlink);1352last = a;1353} else {1354ISAd += incr, first = a, last = b, limit = next;1355}1356} else if((last - b) <= (b - a)) {1357if(1 < (last - b)) {1358STACK_PUSH5(ISAd, first, a, limit, trlink);1359STACK_PUSH5(ISAd + incr, a, b, next, trlink);1360first = b;1361} else {1362STACK_PUSH5(ISAd, first, a, limit, trlink);1363ISAd += incr, first = a, last = b, limit = next;1364}1365} else {1366STACK_PUSH5(ISAd, first, a, limit, trlink);1367STACK_PUSH5(ISAd, b, last, limit, trlink);1368ISAd += incr, first = a, last = b, limit = next;1369}1370}1371} else {1372if((1 < (b - a)) && (0 <= trlink)) { stack[trlink].d = -1; }1373if((a - first) <= (last - b)) {1374if(1 < (a - first)) {1375STACK_PUSH5(ISAd, b, last, limit, trlink);1376last = a;1377} else if(1 < (last - b)) {1378first = b;1379} else {1380STACK_POP5(ISAd, first, last, limit, trlink);1381}1382} else {1383if(1 < (last - b)) {1384STACK_PUSH5(ISAd, first, a, limit, trlink);1385first = b;1386} else if(1 < (a - first)) {1387last = a;1388} else {1389STACK_POP5(ISAd, first, last, limit, trlink);1390}1391}1392}1393} else {1394if(trbudget_check(budget, last - first)) {1395limit = tr_ilg(last - first), ISAd += incr;1396} else {1397if(0 <= trlink) { stack[trlink].d = -1; }1398STACK_POP5(ISAd, first, last, limit, trlink);1399}1400}1401}1402#undef STACK_SIZE1403}1404140514061407/*---------------------------------------------------------------------------*/14081409/* Tandem repeat sort */1410static1411void1412trsort(int *ISA, int *SA, int n, int depth) {1413int *ISAd;1414int *first, *last;1415trbudget_t budget;1416int t, skip, unsorted;14171418trbudget_init(&budget, tr_ilg(n) * 2 / 3, n);1419/* trbudget_init(&budget, tr_ilg(n) * 3 / 4, n); */1420for(ISAd = ISA + depth; -n < *SA; ISAd += ISAd - ISA) {1421first = SA;1422skip = 0;1423unsorted = 0;1424do {1425if((t = *first) < 0) { first -= t; skip += t; }1426else {1427if(skip != 0) { *(first + skip) = skip; skip = 0; }1428last = SA + ISA[t] + 1;1429if(1 < (last - first)) {1430budget.count = 0;1431tr_introsort(ISA, ISAd, SA, first, last, &budget);1432if(budget.count != 0) { unsorted += budget.count; }1433else { skip = first - last; }1434} else if((last - first) == 1) {1435skip = -1;1436}1437first = last;1438}1439} while(first < (SA + n));1440if(skip != 0) { *(first + skip) = skip; }1441if(unsorted == 0) { break; }1442}1443}144414451446/*---------------------------------------------------------------------------*/14471448/* Sorts suffixes of type B*. */1449static1450int1451sort_typeBstar(const unsigned char *T, int *SA,1452int *bucket_A, int *bucket_B,1453int n, int openMP) {1454int *PAb, *ISAb, *buf;1455#ifdef LIBBSC_OPENMP1456int *curbuf;1457int l;1458#endif1459int i, j, k, t, m, bufsize;1460int c0, c1;1461#ifdef LIBBSC_OPENMP1462int d0, d1;1463#endif1464(void)openMP;14651466/* Initialize bucket arrays. */1467for(i = 0; i < BUCKET_A_SIZE; ++i) { bucket_A[i] = 0; }1468for(i = 0; i < BUCKET_B_SIZE; ++i) { bucket_B[i] = 0; }14691470/* Count the number of occurrences of the first one or two characters of each1471type A, B and B* suffix. Moreover, store the beginning position of all1472type B* suffixes into the array SA. */1473for(i = n - 1, m = n, c0 = T[n - 1]; 0 <= i;) {1474/* type A suffix. */1475do { ++BUCKET_A(c1 = c0); } while((0 <= --i) && ((c0 = T[i]) >= c1));1476if(0 <= i) {1477/* type B* suffix. */1478++BUCKET_BSTAR(c0, c1);1479SA[--m] = i;1480/* type B suffix. */1481for(--i, c1 = c0; (0 <= i) && ((c0 = T[i]) <= c1); --i, c1 = c0) {1482++BUCKET_B(c0, c1);1483}1484}1485}1486m = n - m;1487/*1488note:1489A type B* suffix is lexicographically smaller than a type B suffix that1490begins with the same first two characters.1491*/14921493/* Calculate the index of start/end point of each bucket. */1494for(c0 = 0, i = 0, j = 0; c0 < ALPHABET_SIZE; ++c0) {1495t = i + BUCKET_A(c0);1496BUCKET_A(c0) = i + j; /* start point */1497i = t + BUCKET_B(c0, c0);1498for(c1 = c0 + 1; c1 < ALPHABET_SIZE; ++c1) {1499j += BUCKET_BSTAR(c0, c1);1500BUCKET_BSTAR(c0, c1) = j; /* end point */1501i += BUCKET_B(c0, c1);1502}1503}15041505if(0 < m) {1506/* Sort the type B* suffixes by their first two characters. */1507PAb = SA + n - m; ISAb = SA + m;1508for(i = m - 2; 0 <= i; --i) {1509t = PAb[i], c0 = T[t], c1 = T[t + 1];1510SA[--BUCKET_BSTAR(c0, c1)] = i;1511}1512t = PAb[m - 1], c0 = T[t], c1 = T[t + 1];1513SA[--BUCKET_BSTAR(c0, c1)] = m - 1;15141515/* Sort the type B* substrings using sssort. */1516#ifdef LIBBSC_OPENMP1517if (openMP)1518{1519buf = SA + m;1520c0 = ALPHABET_SIZE - 2, c1 = ALPHABET_SIZE - 1, j = m;1521#pragma omp parallel default(shared) private(bufsize, curbuf, k, l, d0, d1)1522{1523bufsize = (n - (2 * m)) / omp_get_num_threads();1524curbuf = buf + omp_get_thread_num() * bufsize;1525k = 0;1526for(;;) {1527#pragma omp critical(sssort_lock)1528{1529if(0 < (l = j)) {1530d0 = c0, d1 = c1;1531do {1532k = BUCKET_BSTAR(d0, d1);1533if(--d1 <= d0) {1534d1 = ALPHABET_SIZE - 1;1535if(--d0 < 0) { break; }1536}1537} while(((l - k) <= 1) && (0 < (l = k)));1538c0 = d0, c1 = d1, j = k;1539}1540}1541if(l == 0) { break; }1542sssort(T, PAb, SA + k, SA + l,1543curbuf, bufsize, 2, n, *(SA + k) == (m - 1));1544}1545}1546}1547else1548{1549buf = SA + m, bufsize = n - (2 * m);1550for(c0 = ALPHABET_SIZE - 2, j = m; 0 < j; --c0) {1551for(c1 = ALPHABET_SIZE - 1; c0 < c1; j = i, --c1) {1552i = BUCKET_BSTAR(c0, c1);1553if(1 < (j - i)) {1554sssort(T, PAb, SA + i, SA + j,1555buf, bufsize, 2, n, *(SA + i) == (m - 1));1556}1557}1558}1559}1560#else1561buf = SA + m, bufsize = n - (2 * m);1562for(c0 = ALPHABET_SIZE - 2, j = m; 0 < j; --c0) {1563for(c1 = ALPHABET_SIZE - 1; c0 < c1; j = i, --c1) {1564i = BUCKET_BSTAR(c0, c1);1565if(1 < (j - i)) {1566sssort(T, PAb, SA + i, SA + j,1567buf, bufsize, 2, n, *(SA + i) == (m - 1));1568}1569}1570}1571#endif15721573/* Compute ranks of type B* substrings. */1574for(i = m - 1; 0 <= i; --i) {1575if(0 <= SA[i]) {1576j = i;1577do { ISAb[SA[i]] = i; } while((0 <= --i) && (0 <= SA[i]));1578SA[i + 1] = i - j;1579if(i <= 0) { break; }1580}1581j = i;1582do { ISAb[SA[i] = ~SA[i]] = j; } while(SA[--i] < 0);1583ISAb[SA[i]] = j;1584}15851586/* Construct the inverse suffix array of type B* suffixes using trsort. */1587trsort(ISAb, SA, m, 1);15881589/* Set the sorted order of type B* suffixes. */1590for(i = n - 1, j = m, c0 = T[n - 1]; 0 <= i;) {1591for(--i, c1 = c0; (0 <= i) && ((c0 = T[i]) >= c1); --i, c1 = c0) { }1592if(0 <= i) {1593t = i;1594for(--i, c1 = c0; (0 <= i) && ((c0 = T[i]) <= c1); --i, c1 = c0) { }1595SA[ISAb[--j]] = ((t == 0) || (1 < (t - i))) ? t : ~t;1596}1597}15981599/* Calculate the index of start/end point of each bucket. */1600BUCKET_B(ALPHABET_SIZE - 1, ALPHABET_SIZE - 1) = n; /* end point */1601for(c0 = ALPHABET_SIZE - 2, k = m - 1; 0 <= c0; --c0) {1602i = BUCKET_A(c0 + 1) - 1;1603for(c1 = ALPHABET_SIZE - 1; c0 < c1; --c1) {1604t = i - BUCKET_B(c0, c1);1605BUCKET_B(c0, c1) = i; /* end point */16061607/* Move all type B* suffixes to the correct position. */1608for(i = t, j = BUCKET_BSTAR(c0, c1);1609j <= k;1610--i, --k) { SA[i] = SA[k]; }1611}1612BUCKET_BSTAR(c0, c0 + 1) = i - BUCKET_B(c0, c0) + 1; /* start point */1613BUCKET_B(c0, c0) = i; /* end point */1614}1615}16161617return m;1618}16191620/* Constructs the suffix array by using the sorted order of type B* suffixes. */1621static1622void1623construct_SA(const unsigned char *T, int *SA,1624int *bucket_A, int *bucket_B,1625int n, int m) {1626int *i, *j, *k;1627int s;1628int c0, c1, c2;16291630if(0 < m) {1631/* Construct the sorted order of type B suffixes by using1632the sorted order of type B* suffixes. */1633for(c1 = ALPHABET_SIZE - 2; 0 <= c1; --c1) {1634/* Scan the suffix array from right to left. */1635for(i = SA + BUCKET_BSTAR(c1, c1 + 1),1636j = SA + BUCKET_A(c1 + 1) - 1, k = NULL, c2 = -1;1637i <= j;1638--j) {1639if(0 < (s = *j)) {1640assert(T[s] == c1);1641assert(((s + 1) < n) && (T[s] <= T[s + 1]));1642assert(T[s - 1] <= T[s]);1643*j = ~s;1644c0 = T[--s];1645if((0 < s) && (T[s - 1] > c0)) { s = ~s; }1646if(c0 != c2) {1647if(0 <= c2) { BUCKET_B(c2, c1) = k - SA; }1648k = SA + BUCKET_B(c2 = c0, c1);1649}1650assert(k < j); assert(k != NULL);1651*k-- = s;1652} else {1653assert(((s == 0) && (T[s] == c1)) || (s < 0));1654*j = ~s;1655}1656}1657}1658}16591660/* Construct the suffix array by using1661the sorted order of type B suffixes. */1662k = SA + BUCKET_A(c2 = T[n - 1]);1663*k++ = (T[n - 2] < c2) ? ~(n - 1) : (n - 1);1664/* Scan the suffix array from left to right. */1665for(i = SA, j = SA + n; i < j; ++i) {1666if(0 < (s = *i)) {1667assert(T[s - 1] >= T[s]);1668c0 = T[--s];1669if((s == 0) || (T[s - 1] < c0)) { s = ~s; }1670if(c0 != c2) {1671BUCKET_A(c2) = k - SA;1672k = SA + BUCKET_A(c2 = c0);1673}1674assert(i < k);1675*k++ = s;1676} else {1677assert(s < 0);1678*i = ~s;1679}1680}1681}16821683/* Constructs the burrows-wheeler transformed string directly1684by using the sorted order of type B* suffixes. */1685static1686int1687construct_BWT(const unsigned char *T, int *SA,1688int *bucket_A, int *bucket_B,1689int n, int m) {1690int *i, *j, *k, *orig;1691int s;1692int c0, c1, c2;16931694if(0 < m) {1695/* Construct the sorted order of type B suffixes by using1696the sorted order of type B* suffixes. */1697for(c1 = ALPHABET_SIZE - 2; 0 <= c1; --c1) {1698/* Scan the suffix array from right to left. */1699for(i = SA + BUCKET_BSTAR(c1, c1 + 1),1700j = SA + BUCKET_A(c1 + 1) - 1, k = NULL, c2 = -1;1701i <= j;1702--j) {1703if(0 < (s = *j)) {1704assert(T[s] == c1);1705assert(((s + 1) < n) && (T[s] <= T[s + 1]));1706assert(T[s - 1] <= T[s]);1707c0 = T[--s];1708*j = ~((int)c0);1709if((0 < s) && (T[s - 1] > c0)) { s = ~s; }1710if(c0 != c2) {1711if(0 <= c2) { BUCKET_B(c2, c1) = k - SA; }1712k = SA + BUCKET_B(c2 = c0, c1);1713}1714assert(k < j); assert(k != NULL);1715*k-- = s;1716} else if(s != 0) {1717*j = ~s;1718#ifndef NDEBUG1719} else {1720assert(T[s] == c1);1721#endif1722}1723}1724}1725}17261727/* Construct the BWTed string by using1728the sorted order of type B suffixes. */1729k = SA + BUCKET_A(c2 = T[n - 1]);1730*k++ = (T[n - 2] < c2) ? ~((int)T[n - 2]) : (n - 1);1731/* Scan the suffix array from left to right. */1732for(i = SA, j = SA + n, orig = SA; i < j; ++i) {1733if(0 < (s = *i)) {1734assert(T[s - 1] >= T[s]);1735c0 = T[--s];1736*i = c0;1737if((0 < s) && (T[s - 1] < c0)) { s = ~((int)T[s - 1]); }1738if(c0 != c2) {1739BUCKET_A(c2) = k - SA;1740k = SA + BUCKET_A(c2 = c0);1741}1742assert(i < k);1743*k++ = s;1744} else if(s != 0) {1745*i = ~s;1746} else {1747orig = i;1748}1749}17501751return orig - SA;1752}17531754/* Constructs the burrows-wheeler transformed string directly1755by using the sorted order of type B* suffixes. */1756static1757int1758construct_BWT_indexes(const unsigned char *T, int *SA,1759int *bucket_A, int *bucket_B,1760int n, int m,1761unsigned char * num_indexes, int * indexes) {1762int *i, *j, *k, *orig;1763int s;1764int c0, c1, c2;17651766int mod = n / 8;1767{1768mod |= mod >> 1; mod |= mod >> 2;1769mod |= mod >> 4; mod |= mod >> 8;1770mod |= mod >> 16; mod >>= 1;17711772*num_indexes = (unsigned char)((n - 1) / (mod + 1));1773}17741775if(0 < m) {1776/* Construct the sorted order of type B suffixes by using1777the sorted order of type B* suffixes. */1778for(c1 = ALPHABET_SIZE - 2; 0 <= c1; --c1) {1779/* Scan the suffix array from right to left. */1780for(i = SA + BUCKET_BSTAR(c1, c1 + 1),1781j = SA + BUCKET_A(c1 + 1) - 1, k = NULL, c2 = -1;1782i <= j;1783--j) {1784if(0 < (s = *j)) {1785assert(T[s] == c1);1786assert(((s + 1) < n) && (T[s] <= T[s + 1]));1787assert(T[s - 1] <= T[s]);17881789if ((s & mod) == 0) indexes[s / (mod + 1) - 1] = j - SA;17901791c0 = T[--s];1792*j = ~((int)c0);1793if((0 < s) && (T[s - 1] > c0)) { s = ~s; }1794if(c0 != c2) {1795if(0 <= c2) { BUCKET_B(c2, c1) = k - SA; }1796k = SA + BUCKET_B(c2 = c0, c1);1797}1798assert(k < j); assert(k != NULL);1799*k-- = s;1800} else if(s != 0) {1801*j = ~s;1802#ifndef NDEBUG1803} else {1804assert(T[s] == c1);1805#endif1806}1807}1808}1809}18101811/* Construct the BWTed string by using1812the sorted order of type B suffixes. */1813k = SA + BUCKET_A(c2 = T[n - 1]);1814if (T[n - 2] < c2) {1815if (((n - 1) & mod) == 0) indexes[(n - 1) / (mod + 1) - 1] = k - SA;1816*k++ = ~((int)T[n - 2]);1817}1818else {1819*k++ = n - 1;1820}18211822/* Scan the suffix array from left to right. */1823for(i = SA, j = SA + n, orig = SA; i < j; ++i) {1824if(0 < (s = *i)) {1825assert(T[s - 1] >= T[s]);18261827if ((s & mod) == 0) indexes[s / (mod + 1) - 1] = i - SA;18281829c0 = T[--s];1830*i = c0;1831if(c0 != c2) {1832BUCKET_A(c2) = k - SA;1833k = SA + BUCKET_A(c2 = c0);1834}1835assert(i < k);1836if((0 < s) && (T[s - 1] < c0)) {1837if ((s & mod) == 0) indexes[s / (mod + 1) - 1] = k - SA;1838*k++ = ~((int)T[s - 1]);1839} else1840*k++ = s;1841} else if(s != 0) {1842*i = ~s;1843} else {1844orig = i;1845}1846}18471848return orig - SA;1849}185018511852/*---------------------------------------------------------------------------*/18531854/*- Function -*/18551856int1857divsufsort(const unsigned char *T, int *SA, int n, int openMP) {1858int *bucket_A, *bucket_B;1859int m;1860int err = 0;18611862/* Check arguments. */1863if((T == NULL) || (SA == NULL) || (n < 0)) { return -1; }1864else if(n == 0) { return 0; }1865else if(n == 1) { SA[0] = 0; return 0; }1866else if(n == 2) { m = (T[0] < T[1]); SA[m ^ 1] = 0, SA[m] = 1; return 0; }18671868bucket_A = (int *)malloc(BUCKET_A_SIZE * sizeof(int));1869bucket_B = (int *)malloc(BUCKET_B_SIZE * sizeof(int));18701871/* Suffixsort. */1872if((bucket_A != NULL) && (bucket_B != NULL)) {1873m = sort_typeBstar(T, SA, bucket_A, bucket_B, n, openMP);1874construct_SA(T, SA, bucket_A, bucket_B, n, m);1875} else {1876err = -2;1877}18781879free(bucket_B);1880free(bucket_A);18811882return err;1883}18841885int1886divbwt(const unsigned char *T, unsigned char *U, int *A, int n, unsigned char * num_indexes, int * indexes, int openMP) {1887int *B;1888int *bucket_A, *bucket_B;1889int m, pidx, i;18901891/* Check arguments. */1892if((T == NULL) || (U == NULL) || (n < 0)) { return -1; }1893else if(n <= 1) { if(n == 1) { U[0] = T[0]; } return n; }18941895if((B = A) == NULL) { B = (int *)malloc((size_t)(n + 1) * sizeof(int)); }1896bucket_A = (int *)malloc(BUCKET_A_SIZE * sizeof(int));1897bucket_B = (int *)malloc(BUCKET_B_SIZE * sizeof(int));18981899/* Burrows-Wheeler Transform. */1900if((B != NULL) && (bucket_A != NULL) && (bucket_B != NULL)) {1901m = sort_typeBstar(T, B, bucket_A, bucket_B, n, openMP);19021903if (num_indexes == NULL || indexes == NULL) {1904pidx = construct_BWT(T, B, bucket_A, bucket_B, n, m);1905} else {1906pidx = construct_BWT_indexes(T, B, bucket_A, bucket_B, n, m, num_indexes, indexes);1907}19081909/* Copy to output string. */1910U[0] = T[n - 1];1911for(i = 0; i < pidx; ++i) { U[i + 1] = (unsigned char)B[i]; }1912for(i += 1; i < n; ++i) { U[i] = (unsigned char)B[i]; }1913pidx += 1;1914} else {1915pidx = -2;1916}19171918free(bucket_B);1919free(bucket_A);1920if(A == NULL) { free(B); }19211922return pidx;1923}192419251926