File Coverage

c-lib/libdivsufsort/trsort.c
Criterion Covered Total %
statement 111 286 38.8
branch 71 300 23.6
condition n/a
subroutine n/a
pod n/a
total 182 586 31.0


line stmt bran cond sub pod time code
1             /*
2             * trsort.c for libdivsufsort
3             * Copyright (c) 2003-2008 Yuta Mori All Rights Reserved.
4             *
5             * Permission is hereby granted, free of charge, to any person
6             * obtaining a copy of this software and associated documentation
7             * files (the "Software"), to deal in the Software without
8             * restriction, including without limitation the rights to use,
9             * copy, modify, merge, publish, distribute, sublicense, and/or sell
10             * copies of the Software, and to permit persons to whom the
11             * Software is furnished to do so, subject to the following
12             * conditions:
13             *
14             * The above copyright notice and this permission notice shall be
15             * included in all copies or substantial portions of the Software.
16             *
17             * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18             * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19             * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20             * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21             * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22             * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23             * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24             * OTHER DEALINGS IN THE SOFTWARE.
25             */
26              
27             #include "divsufsort_private.h"
28              
29              
30             /*- Private Functions -*/
31              
32             static const saint_t lg_table[256]= {
33             -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,
34             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,5,
35             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,6,
36             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,6,
37             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,7,
38             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,7,
39             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,7,
40             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,7
41             };
42              
43             static INLINE
44             saint_t
45 96           tr_ilg(saidx_t n) {
46             #if defined(BUILD_DIVSUFSORT64)
47             return (n >> 32) ?
48             ((n >> 48) ?
49             ((n >> 56) ?
50             56 + lg_table[(n >> 56) & 0xff] :
51             48 + lg_table[(n >> 48) & 0xff]) :
52             ((n >> 40) ?
53             40 + lg_table[(n >> 40) & 0xff] :
54             32 + lg_table[(n >> 32) & 0xff])) :
55             ((n & 0xffff0000) ?
56             ((n & 0xff000000) ?
57             24 + lg_table[(n >> 24) & 0xff] :
58             16 + lg_table[(n >> 16) & 0xff]) :
59             ((n & 0x0000ff00) ?
60             8 + lg_table[(n >> 8) & 0xff] :
61             0 + lg_table[(n >> 0) & 0xff]));
62             #else
63 96           return (n & 0xffff0000) ?
64 96           ((n & 0xff000000) ?
65 96 50         24 + lg_table[(n >> 24) & 0xff] :
66 192 50         16 + lg_table[(n >> 16) & 0xff]) :
67 0           ((n & 0x0000ff00) ?
68 0 0         8 + lg_table[(n >> 8) & 0xff] :
69 0           0 + lg_table[(n >> 0) & 0xff]);
70             #endif
71             }
72              
73              
74             /*---------------------------------------------------------------------------*/
75              
76             /* Simple insertionsort for small size groups. */
77             static
78             void
79 0           tr_insertionsort(const saidx_t *ISAd, saidx_t *first, saidx_t *last) {
80             saidx_t *a, *b;
81             saidx_t t, r;
82              
83 0 0         for(a = first + 1; a < last; ++a) {
84 0 0         for(t = *a, b = a - 1; 0 > (r = ISAd[t] - ISAd[*b]);) {
85 0 0         do { *(b + 1) = *b; } while((first <= --b) && (*b < 0));
    0          
86 0 0         if(b < first) { break; }
87             }
88 0 0         if(r == 0) { *b = ~*b; }
89 0           *(b + 1) = t;
90             }
91 0           }
92              
93              
94             /*---------------------------------------------------------------------------*/
95              
96             static INLINE
97             void
98 0           tr_fixdown(const saidx_t *ISAd, saidx_t *SA, saidx_t i, saidx_t size) {
99             saidx_t j, k;
100             saidx_t v;
101             saidx_t c, d, e;
102              
103 0 0         for(v = SA[i], c = ISAd[v]; (j = 2 * i + 1) < size; SA[i] = SA[k], i = k) {
104 0           d = ISAd[SA[k = j++]];
105 0 0         if(d < (e = ISAd[SA[j]])) { k = j; d = e; }
106 0 0         if(d <= c) { break; }
107             }
108 0           SA[i] = v;
109 0           }
110              
111             /* Simple top-down heapsort. */
112             static
113             void
114 0           tr_heapsort(const saidx_t *ISAd, saidx_t *SA, saidx_t size) {
115             saidx_t i, m;
116             saidx_t t;
117              
118 0           m = size;
119 0 0         if((size % 2) == 0) {
120 0           m--;
121 0 0         if(ISAd[SA[m / 2]] < ISAd[SA[m]]) { SWAP(SA[m], SA[m / 2]); }
122             }
123              
124 0 0         for(i = m / 2 - 1; 0 <= i; --i) { tr_fixdown(ISAd, SA, i, m); }
125 0 0         if((size % 2) == 0) { SWAP(SA[0], SA[m]); tr_fixdown(ISAd, SA, 0, m); }
126 0 0         for(i = m - 1; 0 < i; --i) {
127 0           t = SA[0], SA[0] = SA[i];
128 0           tr_fixdown(ISAd, SA, 0, i);
129 0           SA[i] = t;
130             }
131 0           }
132              
133              
134             /*---------------------------------------------------------------------------*/
135              
136             /* Returns the median of three elements. */
137             static INLINE
138             saidx_t *
139 192           tr_median3(const saidx_t *ISAd, saidx_t *v1, saidx_t *v2, saidx_t *v3) {
140             saidx_t *t;
141 192 50         if(ISAd[*v1] > ISAd[*v2]) { SWAP(v1, v2); }
142 192 100         if(ISAd[*v2] > ISAd[*v3]) {
143 48 50         if(ISAd[*v1] > ISAd[*v3]) { return v1; }
144 0           else { return v3; }
145             }
146 144           return v2;
147             }
148              
149             /* Returns the median of five elements. */
150             static INLINE
151             saidx_t *
152 0           tr_median5(const saidx_t *ISAd,
153             saidx_t *v1, saidx_t *v2, saidx_t *v3, saidx_t *v4, saidx_t *v5) {
154             saidx_t *t;
155 0 0         if(ISAd[*v2] > ISAd[*v3]) { SWAP(v2, v3); }
156 0 0         if(ISAd[*v4] > ISAd[*v5]) { SWAP(v4, v5); }
157 0 0         if(ISAd[*v2] > ISAd[*v4]) { SWAP(v2, v4); SWAP(v3, v5); }
158 0 0         if(ISAd[*v1] > ISAd[*v3]) { SWAP(v1, v3); }
159 0 0         if(ISAd[*v1] > ISAd[*v4]) { SWAP(v1, v4); SWAP(v3, v5); }
160 0 0         if(ISAd[*v3] > ISAd[*v4]) { return v4; }
161 0           return v3;
162             }
163              
164             /* Returns the pivot element. */
165             static INLINE
166             saidx_t *
167 48           tr_pivot(const saidx_t *ISAd, saidx_t *first, saidx_t *last) {
168             saidx_t *middle;
169             saidx_t t;
170              
171 48           t = last - first;
172 48           middle = first + t / 2;
173              
174 48 50         if(t <= 512) {
175 0 0         if(t <= 32) {
176 0           return tr_median3(ISAd, first, middle, last - 1);
177             } else {
178 0           t >>= 2;
179 0           return tr_median5(ISAd, first, first + t, middle, last - 1 - t, last - 1);
180             }
181             }
182 48           t >>= 3;
183 48           first = tr_median3(ISAd, first, first + t, first + (t << 1));
184 48           middle = tr_median3(ISAd, middle - t, middle, middle + t);
185 48           last = tr_median3(ISAd, last - 1 - (t << 1), last - 1 - t, last - 1);
186 48           return tr_median3(ISAd, first, middle, last);
187             }
188              
189              
190             /*---------------------------------------------------------------------------*/
191              
192             typedef struct _trbudget_t trbudget_t;
193             struct _trbudget_t {
194             saidx_t chance;
195             saidx_t remain;
196             saidx_t incval;
197             saidx_t count;
198             };
199              
200             static INLINE
201             void
202 48           trbudget_init(trbudget_t *budget, saidx_t chance, saidx_t incval) {
203 48           budget->chance = chance;
204 48           budget->remain = budget->incval = incval;
205 48           }
206              
207             static INLINE
208             saint_t
209 48           trbudget_check(trbudget_t *budget, saidx_t size) {
210 48 50         if(size <= budget->remain) { budget->remain -= size; return 1; }
211 0 0         if(budget->chance == 0) { budget->count += size; return 0; }
212 0           budget->remain += budget->incval - size;
213 0           budget->chance -= 1;
214 0           return 1;
215             }
216              
217              
218             /*---------------------------------------------------------------------------*/
219              
220             static INLINE
221             void
222 96           tr_partition(const saidx_t *ISAd,
223             saidx_t *first, saidx_t *middle, saidx_t *last,
224             saidx_t **pa, saidx_t **pb, saidx_t v) {
225             saidx_t *a, *b, *c, *d, *e, *f;
226             saidx_t t, s;
227 96           saidx_t x = 0;
228              
229 9119760 50         for(b = middle - 1; (++b < last) && ((x = ISAd[*b]) == v);) { }
    100          
230 96 50         if(((a = b) < last) && (x < v)) {
    50          
231 144 100         for(; (++b < last) && ((x = ISAd[*b]) <= v);) {
    50          
232 48 50         if(x == v) { SWAP(*b, *a); ++a; }
233             }
234             }
235 96 50         for(c = last; (b < --c) && ((x = ISAd[*c]) == v);) { }
    0          
236 96 50         if((b < (d = c)) && (x > v)) {
    0          
237 0 0         for(; (b < --c) && ((x = ISAd[*c]) >= v);) {
    0          
238 0 0         if(x == v) { SWAP(*c, *d); --d; }
239             }
240             }
241 96 50         for(; b < c;) {
242 0           SWAP(*b, *c);
243 0 0         for(; (++b < c) && ((x = ISAd[*b]) <= v);) {
    0          
244 0 0         if(x == v) { SWAP(*b, *a); ++a; }
245             }
246 0 0         for(; (b < --c) && ((x = ISAd[*c]) >= v);) {
    0          
247 0 0         if(x == v) { SWAP(*c, *d); --d; }
248             }
249             }
250              
251 96 50         if(a <= d) {
252 96           c = b - 1;
253 96 50         if((s = a - first) > (t = b - a)) { s = t; }
254 192 100         for(e = first, f = b - s; 0 < s; --s, ++e, ++f) { SWAP(*e, *f); }
255 96 50         if((s = d - c) > (t = last - d - 1)) { s = t; }
256 96 50         for(e = b, f = last - s; 0 < s; --s, ++e, ++f) { SWAP(*e, *f); }
257 96           first += (b - a), last -= (d - c);
258             }
259 96           *pa = first, *pb = last;
260 96           }
261              
262             static
263             void
264 48           tr_copy(saidx_t *ISA, const saidx_t *SA,
265             saidx_t *first, saidx_t *a, saidx_t *b, saidx_t *last,
266             saidx_t depth) {
267             /* sort suffixes of middle partition
268             by using sorted order of suffixes of left and right partition. */
269             saidx_t *c, *d, *e;
270             saidx_t s, v;
271              
272 48           v = b - SA - 1;
273 4559952 100         for(c = first, d = a - 1; c <= d; ++c) {
274 4559904 100         if((0 <= (s = *c - depth)) && (ISA[s] == v)) {
    50          
275 4559856           *++d = s;
276 4559856           ISA[s] = d - SA;
277             }
278             }
279 48 50         for(c = last - 1, e = d + 1, d = b; e < d; --c) {
280 0 0         if((0 <= (s = *c - depth)) && (ISA[s] == v)) {
    0          
281 0           *--d = s;
282 0           ISA[s] = d - SA;
283             }
284             }
285 48           }
286              
287             static
288             void
289 0           tr_partialcopy(saidx_t *ISA, const saidx_t *SA,
290             saidx_t *first, saidx_t *a, saidx_t *b, saidx_t *last,
291             saidx_t depth) {
292             saidx_t *c, *d, *e;
293             saidx_t s, v;
294 0           saidx_t rank, lastrank, newrank = -1;
295              
296 0           v = b - SA - 1;
297 0           lastrank = -1;
298 0 0         for(c = first, d = a - 1; c <= d; ++c) {
299 0 0         if((0 <= (s = *c - depth)) && (ISA[s] == v)) {
    0          
300 0           *++d = s;
301 0           rank = ISA[s + depth];
302 0 0         if(lastrank != rank) { lastrank = rank; newrank = d - SA; }
303 0           ISA[s] = newrank;
304             }
305             }
306              
307 0           lastrank = -1;
308 0 0         for(e = d; first <= e; --e) {
309 0           rank = ISA[*e];
310 0 0         if(lastrank != rank) { lastrank = rank; newrank = e - SA; }
311 0 0         if(newrank != rank) { ISA[*e] = newrank; }
312             }
313              
314 0           lastrank = -1;
315 0 0         for(c = last - 1, e = d + 1, d = b; e < d; --c) {
316 0 0         if((0 <= (s = *c - depth)) && (ISA[s] == v)) {
    0          
317 0           *--d = s;
318 0           rank = ISA[s + depth];
319 0 0         if(lastrank != rank) { lastrank = rank; newrank = d - SA; }
320 0           ISA[s] = newrank;
321             }
322             }
323 0           }
324              
325             static
326             void
327 48           tr_introsort(saidx_t *ISA, const saidx_t *ISAd,
328             saidx_t *SA, saidx_t *first, saidx_t *last,
329             trbudget_t *budget) {
330             #define STACK_SIZE TR_STACKSIZE
331             struct { const saidx_t *a; saidx_t *b, *c; saint_t d, e; }stack[STACK_SIZE];
332             saidx_t *a, *b, *c;
333             saidx_t t;
334 48           saidx_t v, x = 0;
335 48           saidx_t incr = ISAd - ISA;
336             saint_t limit, next;
337 48           saint_t ssize, trlink = -1;
338              
339 48           for(ssize = 0, limit = tr_ilg(last - first);;) {
340              
341 144 100         if(limit < 0) {
342 96 100         if(limit == -1) {
343             /* tandem repeat partition */
344 48           tr_partition(ISAd - incr, first, first, last, &a, &b, last - SA - 1);
345              
346             /* update ranks */
347 48 50         if(a < last) {
348 96 100         for(c = first, v = a - SA - 1; c < a; ++c) { ISA[*c] = v; }
349             }
350 48 50         if(b < last) {
351 0 0         for(c = a, v = b - SA - 1; c < b; ++c) { ISA[*c] = v; }
352             }
353              
354             /* push */
355 48 50         if(1 < (b - a)) {
356 48           STACK_PUSH5(NULL, a, b, 0, 0);
357 48           STACK_PUSH5(ISAd - incr, first, last, -2, trlink);
358 48           trlink = ssize - 2;
359             }
360 48 50         if((a - first) <= (last - b)) {
361 0 0         if(1 < (a - first)) {
362 0           STACK_PUSH5(ISAd, b, last, tr_ilg(last - b), trlink);
363 0           last = a, limit = tr_ilg(a - first);
364 0 0         } else if(1 < (last - b)) {
365 0           first = b, limit = tr_ilg(last - b);
366             } else {
367 0 0         STACK_POP5(ISAd, first, last, limit, trlink);
368             }
369             } else {
370 48 50         if(1 < (last - b)) {
371 0           STACK_PUSH5(ISAd, first, a, tr_ilg(a - first), trlink);
372 0           first = b, limit = tr_ilg(last - b);
373 48 50         } else if(1 < (a - first)) {
374 0           last = a, limit = tr_ilg(a - first);
375             } else {
376 48 50         STACK_POP5(ISAd, first, last, limit, trlink);
377             }
378             }
379 48 50         } else if(limit == -2) {
380             /* tandem repeat copy */
381 48           a = stack[--ssize].b, b = stack[ssize].c;
382 48 50         if(stack[ssize].d == 0) {
383 48           tr_copy(ISA, SA, first, a, b, last, ISAd - ISA);
384             } else {
385 0 0         if(0 <= trlink) { stack[trlink].d = -1; }
386 0           tr_partialcopy(ISA, SA, first, a, b, last, ISAd - ISA);
387             }
388 48 50         STACK_POP5(ISAd, first, last, limit, trlink);
389             } else {
390             /* sorted partition */
391 0 0         if(0 <= *first) {
392 0           a = first;
393 0 0         do { ISA[*a] = a - SA; } while((++a < last) && (0 <= *a));
    0          
394 0           first = a;
395             }
396 0 0         if(first < last) {
397 0 0         a = first; do { *a = ~*a; } while(*++a < 0);
398 0 0         next = (ISA[*a] != ISAd[*a]) ? tr_ilg(a - first + 1) : -1;
399 0 0         if(++a < last) { for(b = first, v = a - SA - 1; b < a; ++b) { ISA[*b] = v; } }
    0          
400              
401             /* push */
402 0 0         if(trbudget_check(budget, a - first)) {
403 0 0         if((a - first) <= (last - a)) {
404 0           STACK_PUSH5(ISAd, a, last, -3, trlink);
405 0           ISAd += incr, last = a, limit = next;
406             } else {
407 0 0         if(1 < (last - a)) {
408 0           STACK_PUSH5(ISAd + incr, first, a, next, trlink);
409 0           first = a, limit = -3;
410             } else {
411 0           ISAd += incr, last = a, limit = next;
412             }
413             }
414             } else {
415 0 0         if(0 <= trlink) { stack[trlink].d = -1; }
416 0 0         if(1 < (last - a)) {
417 0           first = a, limit = -3;
418             } else {
419 0 0         STACK_POP5(ISAd, first, last, limit, trlink);
420             }
421             }
422             } else {
423 0 0         STACK_POP5(ISAd, first, last, limit, trlink);
424             }
425             }
426 48           continue;
427             }
428              
429 48 50         if((last - first) <= TR_INSERTIONSORT_THRESHOLD) {
430 0           tr_insertionsort(ISAd, first, last);
431 0           limit = -3;
432 0           continue;
433             }
434              
435 48 50         if(limit-- == 0) {
436 0           tr_heapsort(ISAd, first, last - first);
437 0 0         for(a = last - 1; first < a; a = b) {
438 0 0         for(x = ISAd[*a], b = a - 1; (first <= b) && (ISAd[*b] == x); --b) { *b = ~*b; }
    0          
439             }
440 0           limit = -3;
441 0           continue;
442             }
443              
444             /* choose pivot */
445 48           a = tr_pivot(ISAd, first, last);
446 48           SWAP(*first, *a);
447 48           v = ISAd[*first];
448              
449             /* partition */
450 48           tr_partition(ISAd, first, first + 1, last, &a, &b, v);
451 48 50         if((last - first) != (b - a)) {
452 48 50         next = (ISA[*a] != v) ? tr_ilg(b - a) : -1;
453              
454             /* update ranks */
455 96 100         for(c = first, v = a - SA - 1; c < a; ++c) { ISA[*c] = v; }
456 48 50         if(b < last) { for(c = a, v = b - SA - 1; c < b; ++c) { ISA[*c] = v; } }
    0          
457              
458             /* push */
459 48 50         if((1 < (b - a)) && (trbudget_check(budget, b - a))) {
    50          
460 48 50         if((a - first) <= (last - b)) {
461 0 0         if((last - b) <= (b - a)) {
462 0 0         if(1 < (a - first)) {
463 0           STACK_PUSH5(ISAd + incr, a, b, next, trlink);
464 0           STACK_PUSH5(ISAd, b, last, limit, trlink);
465 0           last = a;
466 0 0         } else if(1 < (last - b)) {
467 0           STACK_PUSH5(ISAd + incr, a, b, next, trlink);
468 0           first = b;
469             } else {
470 0           ISAd += incr, first = a, last = b, limit = next;
471             }
472 0 0         } else if((a - first) <= (b - a)) {
473 0 0         if(1 < (a - first)) {
474 0           STACK_PUSH5(ISAd, b, last, limit, trlink);
475 0           STACK_PUSH5(ISAd + incr, a, b, next, trlink);
476 0           last = a;
477             } else {
478 0           STACK_PUSH5(ISAd, b, last, limit, trlink);
479 0           ISAd += incr, first = a, last = b, limit = next;
480             }
481             } else {
482 0           STACK_PUSH5(ISAd, b, last, limit, trlink);
483 0           STACK_PUSH5(ISAd, first, a, limit, trlink);
484 0           ISAd += incr, first = a, last = b, limit = next;
485             }
486             } else {
487 48 50         if((a - first) <= (b - a)) {
488 48 50         if(1 < (last - b)) {
489 0           STACK_PUSH5(ISAd + incr, a, b, next, trlink);
490 0           STACK_PUSH5(ISAd, first, a, limit, trlink);
491 0           first = b;
492 48 50         } else if(1 < (a - first)) {
493 0           STACK_PUSH5(ISAd + incr, a, b, next, trlink);
494 0           last = a;
495             } else {
496 48           ISAd += incr, first = a, last = b, limit = next;
497             }
498 0 0         } else if((last - b) <= (b - a)) {
499 0 0         if(1 < (last - b)) {
500 0           STACK_PUSH5(ISAd, first, a, limit, trlink);
501 0           STACK_PUSH5(ISAd + incr, a, b, next, trlink);
502 0           first = b;
503             } else {
504 0           STACK_PUSH5(ISAd, first, a, limit, trlink);
505 0           ISAd += incr, first = a, last = b, limit = next;
506             }
507             } else {
508 0           STACK_PUSH5(ISAd, first, a, limit, trlink);
509 0           STACK_PUSH5(ISAd, b, last, limit, trlink);
510 0           ISAd += incr, first = a, last = b, limit = next;
511             }
512             }
513             } else {
514 0 0         if((1 < (b - a)) && (0 <= trlink)) { stack[trlink].d = -1; }
    0          
515 0 0         if((a - first) <= (last - b)) {
516 0 0         if(1 < (a - first)) {
517 0           STACK_PUSH5(ISAd, b, last, limit, trlink);
518 0           last = a;
519 0 0         } else if(1 < (last - b)) {
520 0           first = b;
521             } else {
522 0 0         STACK_POP5(ISAd, first, last, limit, trlink);
523             }
524             } else {
525 0 0         if(1 < (last - b)) {
526 0           STACK_PUSH5(ISAd, first, a, limit, trlink);
527 0           first = b;
528 0 0         } else if(1 < (a - first)) {
529 0           last = a;
530             } else {
531 0 0         STACK_POP5(ISAd, first, last, limit, trlink);
532             }
533             }
534             }
535             } else {
536 0 0         if(trbudget_check(budget, last - first)) {
537 0           limit = tr_ilg(last - first), ISAd += incr;
538             } else {
539 0 0         if(0 <= trlink) { stack[trlink].d = -1; }
540 0 0         STACK_POP5(ISAd, first, last, limit, trlink);
541             }
542             }
543             }
544             #undef STACK_SIZE
545             }
546              
547              
548              
549             /*---------------------------------------------------------------------------*/
550              
551             /*- Function -*/
552              
553             /* Tandem repeat sort */
554             void
555 48           trsort(saidx_t *ISA, saidx_t *SA, saidx_t n, saidx_t depth) {
556             saidx_t *ISAd;
557             saidx_t *first, *last;
558             trbudget_t budget;
559             saidx_t t, skip, unsorted;
560              
561 48           trbudget_init(&budget, tr_ilg(n) * 2 / 3, n);
562             /* trbudget_init(&budget, tr_ilg(n) * 3 / 4, n); */
563 48 50         for(ISAd = ISA + depth; -n < *SA; ISAd += ISAd - ISA) {
564 48           first = SA;
565 48           skip = 0;
566 48           unsorted = 0;
567             do {
568 96 100         if((t = *first) < 0) { first -= t; skip += t; }
569             else {
570 48 50         if(skip != 0) { *(first + skip) = skip; skip = 0; }
571 48           last = SA + ISA[t] + 1;
572 48 50         if(1 < (last - first)) {
573 48           budget.count = 0;
574 48           tr_introsort(ISA, ISAd, SA, first, last, &budget);
575 48 50         if(budget.count != 0) { unsorted += budget.count; }
576 48           else { skip = first - last; }
577 0 0         } else if((last - first) == 1) {
578 0           skip = -1;
579             }
580 48           first = last;
581             }
582 96 100         } while(first < (SA + n));
583 48 50         if(skip != 0) { *(first + skip) = skip; }
584 48 50         if(unsorted == 0) { break; }
585             }
586 48           }