Path: blob/main/contrib/libdivsufsort/lib/trsort.c
39483 views
/*1* trsort.c for libdivsufsort2* 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#include "divsufsort_private.h"272829/*- Private Functions -*/3031static const saint_t lg_table[256]= {32-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,335,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,346,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,356,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,367,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,377,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,387,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,397,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,740};4142static INLINE43saint_t44tr_ilg(saidx_t n) {45#if defined(BUILD_DIVSUFSORT64)46return (n >> 32) ?47((n >> 48) ?48((n >> 56) ?4956 + lg_table[(n >> 56) & 0xff] :5048 + lg_table[(n >> 48) & 0xff]) :51((n >> 40) ?5240 + lg_table[(n >> 40) & 0xff] :5332 + lg_table[(n >> 32) & 0xff])) :54((n & 0xffff0000) ?55((n & 0xff000000) ?5624 + lg_table[(n >> 24) & 0xff] :5716 + lg_table[(n >> 16) & 0xff]) :58((n & 0x0000ff00) ?598 + lg_table[(n >> 8) & 0xff] :600 + lg_table[(n >> 0) & 0xff]));61#else62return (n & 0xffff0000) ?63((n & 0xff000000) ?6424 + lg_table[(n >> 24) & 0xff] :6516 + lg_table[(n >> 16) & 0xff]) :66((n & 0x0000ff00) ?678 + lg_table[(n >> 8) & 0xff] :680 + lg_table[(n >> 0) & 0xff]);69#endif70}717273/*---------------------------------------------------------------------------*/7475/* Simple insertionsort for small size groups. */76static77void78tr_insertionsort(const saidx_t *ISAd, saidx_t *first, saidx_t *last) {79saidx_t *a, *b;80saidx_t t, r;8182for(a = first + 1; a < last; ++a) {83for(t = *a, b = a - 1; 0 > (r = ISAd[t] - ISAd[*b]);) {84do { *(b + 1) = *b; } while((first <= --b) && (*b < 0));85if(b < first) { break; }86}87if(r == 0) { *b = ~*b; }88*(b + 1) = t;89}90}919293/*---------------------------------------------------------------------------*/9495static INLINE96void97tr_fixdown(const saidx_t *ISAd, saidx_t *SA, saidx_t i, saidx_t size) {98saidx_t j, k;99saidx_t v;100saidx_t c, d, e;101102for(v = SA[i], c = ISAd[v]; (j = 2 * i + 1) < size; SA[i] = SA[k], i = k) {103d = ISAd[SA[k = j++]];104if(d < (e = ISAd[SA[j]])) { k = j; d = e; }105if(d <= c) { break; }106}107SA[i] = v;108}109110/* Simple top-down heapsort. */111static112void113tr_heapsort(const saidx_t *ISAd, saidx_t *SA, saidx_t size) {114saidx_t i, m;115saidx_t t;116117m = size;118if((size % 2) == 0) {119m--;120if(ISAd[SA[m / 2]] < ISAd[SA[m]]) { SWAP(SA[m], SA[m / 2]); }121}122123for(i = m / 2 - 1; 0 <= i; --i) { tr_fixdown(ISAd, SA, i, m); }124if((size % 2) == 0) { SWAP(SA[0], SA[m]); tr_fixdown(ISAd, SA, 0, m); }125for(i = m - 1; 0 < i; --i) {126t = SA[0], SA[0] = SA[i];127tr_fixdown(ISAd, SA, 0, i);128SA[i] = t;129}130}131132133/*---------------------------------------------------------------------------*/134135/* Returns the median of three elements. */136static INLINE137saidx_t *138tr_median3(const saidx_t *ISAd, saidx_t *v1, saidx_t *v2, saidx_t *v3) {139saidx_t *t;140if(ISAd[*v1] > ISAd[*v2]) { SWAP(v1, v2); }141if(ISAd[*v2] > ISAd[*v3]) {142if(ISAd[*v1] > ISAd[*v3]) { return v1; }143else { return v3; }144}145return v2;146}147148/* Returns the median of five elements. */149static INLINE150saidx_t *151tr_median5(const saidx_t *ISAd,152saidx_t *v1, saidx_t *v2, saidx_t *v3, saidx_t *v4, saidx_t *v5) {153saidx_t *t;154if(ISAd[*v2] > ISAd[*v3]) { SWAP(v2, v3); }155if(ISAd[*v4] > ISAd[*v5]) { SWAP(v4, v5); }156if(ISAd[*v2] > ISAd[*v4]) { SWAP(v2, v4); SWAP(v3, v5); }157if(ISAd[*v1] > ISAd[*v3]) { SWAP(v1, v3); }158if(ISAd[*v1] > ISAd[*v4]) { SWAP(v1, v4); SWAP(v3, v5); }159if(ISAd[*v3] > ISAd[*v4]) { return v4; }160return v3;161}162163/* Returns the pivot element. */164static INLINE165saidx_t *166tr_pivot(const saidx_t *ISAd, saidx_t *first, saidx_t *last) {167saidx_t *middle;168saidx_t t;169170t = last - first;171middle = first + t / 2;172173if(t <= 512) {174if(t <= 32) {175return tr_median3(ISAd, first, middle, last - 1);176} else {177t >>= 2;178return tr_median5(ISAd, first, first + t, middle, last - 1 - t, last - 1);179}180}181t >>= 3;182first = tr_median3(ISAd, first, first + t, first + (t << 1));183middle = tr_median3(ISAd, middle - t, middle, middle + t);184last = tr_median3(ISAd, last - 1 - (t << 1), last - 1 - t, last - 1);185return tr_median3(ISAd, first, middle, last);186}187188189/*---------------------------------------------------------------------------*/190191typedef struct _trbudget_t trbudget_t;192struct _trbudget_t {193saidx_t chance;194saidx_t remain;195saidx_t incval;196saidx_t count;197};198199static INLINE200void201trbudget_init(trbudget_t *budget, saidx_t chance, saidx_t incval) {202budget->chance = chance;203budget->remain = budget->incval = incval;204}205206static INLINE207saint_t208trbudget_check(trbudget_t *budget, saidx_t size) {209if(size <= budget->remain) { budget->remain -= size; return 1; }210if(budget->chance == 0) { budget->count += size; return 0; }211budget->remain += budget->incval - size;212budget->chance -= 1;213return 1;214}215216217/*---------------------------------------------------------------------------*/218219static INLINE220void221tr_partition(const saidx_t *ISAd,222saidx_t *first, saidx_t *middle, saidx_t *last,223saidx_t **pa, saidx_t **pb, saidx_t v) {224saidx_t *a, *b, *c, *d, *e, *f;225saidx_t t, s;226saidx_t x = 0;227228for(b = middle - 1; (++b < last) && ((x = ISAd[*b]) == v);) { }229if(((a = b) < last) && (x < v)) {230for(; (++b < last) && ((x = ISAd[*b]) <= v);) {231if(x == v) { SWAP(*b, *a); ++a; }232}233}234for(c = last; (b < --c) && ((x = ISAd[*c]) == v);) { }235if((b < (d = c)) && (x > v)) {236for(; (b < --c) && ((x = ISAd[*c]) >= v);) {237if(x == v) { SWAP(*c, *d); --d; }238}239}240for(; b < c;) {241SWAP(*b, *c);242for(; (++b < c) && ((x = ISAd[*b]) <= v);) {243if(x == v) { SWAP(*b, *a); ++a; }244}245for(; (b < --c) && ((x = ISAd[*c]) >= v);) {246if(x == v) { SWAP(*c, *d); --d; }247}248}249250if(a <= d) {251c = b - 1;252if((s = a - first) > (t = b - a)) { s = t; }253for(e = first, f = b - s; 0 < s; --s, ++e, ++f) { SWAP(*e, *f); }254if((s = d - c) > (t = last - d - 1)) { s = t; }255for(e = b, f = last - s; 0 < s; --s, ++e, ++f) { SWAP(*e, *f); }256first += (b - a), last -= (d - c);257}258*pa = first, *pb = last;259}260261static262void263tr_copy(saidx_t *ISA, const saidx_t *SA,264saidx_t *first, saidx_t *a, saidx_t *b, saidx_t *last,265saidx_t depth) {266/* sort suffixes of middle partition267by using sorted order of suffixes of left and right partition. */268saidx_t *c, *d, *e;269saidx_t s, v;270271v = b - SA - 1;272for(c = first, d = a - 1; c <= d; ++c) {273if((0 <= (s = *c - depth)) && (ISA[s] == v)) {274*++d = s;275ISA[s] = d - SA;276}277}278for(c = last - 1, e = d + 1, d = b; e < d; --c) {279if((0 <= (s = *c - depth)) && (ISA[s] == v)) {280*--d = s;281ISA[s] = d - SA;282}283}284}285286static287void288tr_partialcopy(saidx_t *ISA, const saidx_t *SA,289saidx_t *first, saidx_t *a, saidx_t *b, saidx_t *last,290saidx_t depth) {291saidx_t *c, *d, *e;292saidx_t s, v;293saidx_t rank, lastrank, newrank = -1;294295v = b - SA - 1;296lastrank = -1;297for(c = first, d = a - 1; c <= d; ++c) {298if((0 <= (s = *c - depth)) && (ISA[s] == v)) {299*++d = s;300rank = ISA[s + depth];301if(lastrank != rank) { lastrank = rank; newrank = d - SA; }302ISA[s] = newrank;303}304}305306lastrank = -1;307for(e = d; first <= e; --e) {308rank = ISA[*e];309if(lastrank != rank) { lastrank = rank; newrank = e - SA; }310if(newrank != rank) { ISA[*e] = newrank; }311}312313lastrank = -1;314for(c = last - 1, e = d + 1, d = b; e < d; --c) {315if((0 <= (s = *c - depth)) && (ISA[s] == v)) {316*--d = s;317rank = ISA[s + depth];318if(lastrank != rank) { lastrank = rank; newrank = d - SA; }319ISA[s] = newrank;320}321}322}323324static325void326tr_introsort(saidx_t *ISA, const saidx_t *ISAd,327saidx_t *SA, saidx_t *first, saidx_t *last,328trbudget_t *budget) {329#define STACK_SIZE TR_STACKSIZE330struct { const saidx_t *a; saidx_t *b, *c; saint_t d, e; }stack[STACK_SIZE];331saidx_t *a, *b, *c;332saidx_t t;333saidx_t v, x = 0;334saidx_t incr = ISAd - ISA;335saint_t limit, next;336saint_t ssize, trlink = -1;337338for(ssize = 0, limit = tr_ilg(last - first);;) {339340if(limit < 0) {341if(limit == -1) {342/* tandem repeat partition */343tr_partition(ISAd - incr, first, first, last, &a, &b, last - SA - 1);344345/* update ranks */346if(a < last) {347for(c = first, v = a - SA - 1; c < a; ++c) { ISA[*c] = v; }348}349if(b < last) {350for(c = a, v = b - SA - 1; c < b; ++c) { ISA[*c] = v; }351}352353/* push */354if(1 < (b - a)) {355STACK_PUSH5(NULL, a, b, 0, 0);356STACK_PUSH5(ISAd - incr, first, last, -2, trlink);357trlink = ssize - 2;358}359if((a - first) <= (last - b)) {360if(1 < (a - first)) {361STACK_PUSH5(ISAd, b, last, tr_ilg(last - b), trlink);362last = a, limit = tr_ilg(a - first);363} else if(1 < (last - b)) {364first = b, limit = tr_ilg(last - b);365} else {366STACK_POP5(ISAd, first, last, limit, trlink);367}368} else {369if(1 < (last - b)) {370STACK_PUSH5(ISAd, first, a, tr_ilg(a - first), trlink);371first = b, limit = tr_ilg(last - b);372} else if(1 < (a - first)) {373last = a, limit = tr_ilg(a - first);374} else {375STACK_POP5(ISAd, first, last, limit, trlink);376}377}378} else if(limit == -2) {379/* tandem repeat copy */380a = stack[--ssize].b, b = stack[ssize].c;381if(stack[ssize].d == 0) {382tr_copy(ISA, SA, first, a, b, last, ISAd - ISA);383} else {384if(0 <= trlink) { stack[trlink].d = -1; }385tr_partialcopy(ISA, SA, first, a, b, last, ISAd - ISA);386}387STACK_POP5(ISAd, first, last, limit, trlink);388} else {389/* sorted partition */390if(0 <= *first) {391a = first;392do { ISA[*a] = a - SA; } while((++a < last) && (0 <= *a));393first = a;394}395if(first < last) {396a = first; do { *a = ~*a; } while(*++a < 0);397next = (ISA[*a] != ISAd[*a]) ? tr_ilg(a - first + 1) : -1;398if(++a < last) { for(b = first, v = a - SA - 1; b < a; ++b) { ISA[*b] = v; } }399400/* push */401if(trbudget_check(budget, a - first)) {402if((a - first) <= (last - a)) {403STACK_PUSH5(ISAd, a, last, -3, trlink);404ISAd += incr, last = a, limit = next;405} else {406if(1 < (last - a)) {407STACK_PUSH5(ISAd + incr, first, a, next, trlink);408first = a, limit = -3;409} else {410ISAd += incr, last = a, limit = next;411}412}413} else {414if(0 <= trlink) { stack[trlink].d = -1; }415if(1 < (last - a)) {416first = a, limit = -3;417} else {418STACK_POP5(ISAd, first, last, limit, trlink);419}420}421} else {422STACK_POP5(ISAd, first, last, limit, trlink);423}424}425continue;426}427428if((last - first) <= TR_INSERTIONSORT_THRESHOLD) {429tr_insertionsort(ISAd, first, last);430limit = -3;431continue;432}433434if(limit-- == 0) {435tr_heapsort(ISAd, first, last - first);436for(a = last - 1; first < a; a = b) {437for(x = ISAd[*a], b = a - 1; (first <= b) && (ISAd[*b] == x); --b) { *b = ~*b; }438}439limit = -3;440continue;441}442443/* choose pivot */444a = tr_pivot(ISAd, first, last);445SWAP(*first, *a);446v = ISAd[*first];447448/* partition */449tr_partition(ISAd, first, first + 1, last, &a, &b, v);450if((last - first) != (b - a)) {451next = (ISA[*a] != v) ? tr_ilg(b - a) : -1;452453/* update ranks */454for(c = first, v = a - SA - 1; c < a; ++c) { ISA[*c] = v; }455if(b < last) { for(c = a, v = b - SA - 1; c < b; ++c) { ISA[*c] = v; } }456457/* push */458if((1 < (b - a)) && (trbudget_check(budget, b - a))) {459if((a - first) <= (last - b)) {460if((last - b) <= (b - a)) {461if(1 < (a - first)) {462STACK_PUSH5(ISAd + incr, a, b, next, trlink);463STACK_PUSH5(ISAd, b, last, limit, trlink);464last = a;465} else if(1 < (last - b)) {466STACK_PUSH5(ISAd + incr, a, b, next, trlink);467first = b;468} else {469ISAd += incr, first = a, last = b, limit = next;470}471} else if((a - first) <= (b - a)) {472if(1 < (a - first)) {473STACK_PUSH5(ISAd, b, last, limit, trlink);474STACK_PUSH5(ISAd + incr, a, b, next, trlink);475last = a;476} else {477STACK_PUSH5(ISAd, b, last, limit, trlink);478ISAd += incr, first = a, last = b, limit = next;479}480} else {481STACK_PUSH5(ISAd, b, last, limit, trlink);482STACK_PUSH5(ISAd, first, a, limit, trlink);483ISAd += incr, first = a, last = b, limit = next;484}485} else {486if((a - first) <= (b - a)) {487if(1 < (last - b)) {488STACK_PUSH5(ISAd + incr, a, b, next, trlink);489STACK_PUSH5(ISAd, first, a, limit, trlink);490first = b;491} else if(1 < (a - first)) {492STACK_PUSH5(ISAd + incr, a, b, next, trlink);493last = a;494} else {495ISAd += incr, first = a, last = b, limit = next;496}497} else if((last - b) <= (b - a)) {498if(1 < (last - b)) {499STACK_PUSH5(ISAd, first, a, limit, trlink);500STACK_PUSH5(ISAd + incr, a, b, next, trlink);501first = b;502} else {503STACK_PUSH5(ISAd, first, a, limit, trlink);504ISAd += incr, first = a, last = b, limit = next;505}506} else {507STACK_PUSH5(ISAd, first, a, limit, trlink);508STACK_PUSH5(ISAd, b, last, limit, trlink);509ISAd += incr, first = a, last = b, limit = next;510}511}512} else {513if((1 < (b - a)) && (0 <= trlink)) { stack[trlink].d = -1; }514if((a - first) <= (last - b)) {515if(1 < (a - first)) {516STACK_PUSH5(ISAd, b, last, limit, trlink);517last = a;518} else if(1 < (last - b)) {519first = b;520} else {521STACK_POP5(ISAd, first, last, limit, trlink);522}523} else {524if(1 < (last - b)) {525STACK_PUSH5(ISAd, first, a, limit, trlink);526first = b;527} else if(1 < (a - first)) {528last = a;529} else {530STACK_POP5(ISAd, first, last, limit, trlink);531}532}533}534} else {535if(trbudget_check(budget, last - first)) {536limit = tr_ilg(last - first), ISAd += incr;537} else {538if(0 <= trlink) { stack[trlink].d = -1; }539STACK_POP5(ISAd, first, last, limit, trlink);540}541}542}543#undef STACK_SIZE544}545546547548/*---------------------------------------------------------------------------*/549550/*- Function -*/551552/* Tandem repeat sort */553void554trsort(saidx_t *ISA, saidx_t *SA, saidx_t n, saidx_t depth) {555saidx_t *ISAd;556saidx_t *first, *last;557trbudget_t budget;558saidx_t t, skip, unsorted;559560trbudget_init(&budget, tr_ilg(n) * 2 / 3, n);561/* trbudget_init(&budget, tr_ilg(n) * 3 / 4, n); */562for(ISAd = ISA + depth; -n < *SA; ISAd += ISAd - ISA) {563first = SA;564skip = 0;565unsorted = 0;566do {567if((t = *first) < 0) { first -= t; skip += t; }568else {569if(skip != 0) { *(first + skip) = skip; skip = 0; }570last = SA + ISA[t] + 1;571if(1 < (last - first)) {572budget.count = 0;573tr_introsort(ISA, ISAd, SA, first, last, &budget);574if(budget.count != 0) { unsorted += budget.count; }575else { skip = first - last; }576} else if((last - first) == 1) {577skip = -1;578}579first = last;580}581} while(first < (SA + n));582if(skip != 0) { *(first + skip) = skip; }583if(unsorted == 0) { break; }584}585}586587588