-
Notifications
You must be signed in to change notification settings - Fork 0
/
suffixarray.c
690 lines (542 loc) · 22.5 KB
/
suffixarray.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/* suffixarray implementation
*
* Copyright (C) 2015 Josh Marshall
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <assert.h>
#include <unistd.h>
#include "suffixarray.h"
//#ifdef DEBUG
#include <stdio.h>
//#endif
////////////////////////////////////////////////////////////////////////
// DEFINES ///////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
#define _L_ (1)
#define _S_ (2)
#define _LMS_ (3)
#define u8 unsigned char
#define s16 signed short
//1 for always SAIS, 0 for always working recursive bucket sort
#define DEREFERENCE_BREAK_EVEN (0.0)
////////////////////////////////////////////////////////////////////////
// MACROS ////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
#define max(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })
////////////////////////////////////////////////////////////////////////
// PRIVATE STRUCTURES ////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
/***********************************************************************
This is to help make sequence operations more general and safer.
***********************************************************************/
typedef struct{
size_t startIndex, endIndex;
}lSeqAbst, sSeqAbst, seqAbst; //Sequence Abstraction
typedef struct{
size_t size;
lSeqAbst *bucket;
}lBucket;
typedef struct{
size_t size;
sSeqAbst *bucket;
}sBucket;
typedef struct{
size_t size;
lBucket *buckets;
}lBuckets;
typedef struct{
size_t size;
sBucket *buckets;
}sBuckets;
//This is to aid in passing around sequence abstractions.
typedef struct{
size_t size;
size_t *S;
}sequence;
////////////////////////////////////////////////////////////////////////
// PRIVATE INTERFACE FUNCTIONS ///////////////////////////////////////
////////////////////////////////////////////////////////////////////////
#ifdef DEBUG
void printBucket(size_t **bucket, size_t oneD, size_t *twoD){
for(int i = 0; i < oneD; i++){
if(twoD[i] == 0) continue;
fprintf(stderr, "(");
for(size_t j = 0; j < twoD[i] - 1; j++){
fprintf(stderr, "%lu, ", bucket[i][j]);
}
fprintf(stderr, "%lu), ", bucket[i][twoD[i]-1]);
}
fprintf(stderr, "\n");
fflush(stdout);
}
void printArray(size_t *array, size_t size){
for(size_t i = 0; i < size; i++)
printf("%lu, \t", array[i]);
printf("\n");
fflush(stdout);
}
void printLMSandLS(unsigned char *LMSandLS, size_t length){
fprintf(stderr, "{");
for(size_t i = 0; i < length - 2; i++){
fprintf(stderr, "%c, ", LMSandLS[i] == _L_ ? 'L' : (LMSandLS[i] == _S_ ? 'S' : 'M'));
}
fprintf(stderr, "%c}\n", (LMSandLS[length-1] == _L_ ? 'L' : 'S'));
fflush(stdout);
}
#endif
sequence initSequence(size_t length){
sequence toReturn;
toReturn.size = length;
toReturn.S = malloc(sizeof(*toReturn.S) * toReturn.size);
#ifdef DEBUG
if(length == 0){
fprintf(stderr, "initSequnce: invalid length argument\n");
fflush(stderr);
exit(errno);
}
if(toReturn.S == NULL){
fprintf(stderr, "initSequnce: failed to allocate space to sequence\n");
fflush(stderr);
exit(errno);
}
#endif
return toReturn;
}
/***********************************************************************
* Create array of indexes from S which omit all except the last
* repeated value.
***********************************************************************/
sequence removeRuns(const u8 *S, const size_t size){
sequence toReturn = initSequence(size);
toReturn.size = 0;
for(size_t i = 0; i < size; i++) //if(S[i] != S[i+1])
toReturn.S[toReturn.size++] = i;
toReturn.S = realloc(toReturn.S, toReturn.size * sizeof(*toReturn.S));
return toReturn;
}
/***********************************************************************
* Re-add runs of elements into SSA
*
* proxy: the SSA arranged repeats removed indexes
*
***********************************************************************/
sequence addRuns(const u8 *input, const size_t length, sequence proxy){
//DECLARATIONS//////////////////////////////////////////////////////////
sequence toReturn;
//size_t numToExpand;
//INITIALIZATIONS///////////////////////////////////////////////////////
toReturn = initSequence(length);
memcpy(toReturn.S, proxy.S, sizeof(*toReturn.S) * toReturn.size);
//toReturn.size = 0;
//OPERATIONS////////////////////////////////////////////////////////////
/*for(size_t i = 1; i < proxy.size; i++){
const char isARepeat = input[proxy.S[i]] == input[proxy.S[i]-1];
if(!isARepeat){
toReturn.S[toReturn.size++] = proxy.S[i];
}else{
//size_t j = i+1;
while(proxy.S[i] - numToExpand > 0 &&
input[proxy.S[i] - (numToExpand+1)] ==
input[proxy.S[i] - numToExpand])
numToExpand++;
size_t *foundToRepeat;
//Here we flatten the 2D array and will just use a more complex
//accessing portion in order to avoid more calls to malloc.
foundToRepeat = malloc(sizeof(*foundToRepeat) * numToExpand * 3);
//NOTE: in parsing as entries become exausted, the pointers for
//reading and writing should be seperate so that in the course of
//each expantion iteration old entries are removed in an efficient
//manner.
//populate the entries to expand
if(proxy.S[i]+1 < length || input[proxy.S[i]] < input[proxy.S[i]+1]){//left expansion
for(size_t k = numToExpand-1; k != ((size_t)0)-1; k--){
toReturn.S[toReturn.size++] = proxy.S[i] - k;
}
}else{//right expansion
for(size_t k = 0; k < numToExpand; k++){
toReturn.S[toReturn.size++] = proxy.S[i] - k;
}
}
}
}*/
//CLEAN UP//////////////////////////////////////////////////////////////
return toReturn;
}
u8 getAlphabetSize(const u8 *input, const size_t size){
u8 toReturn = 0;
for(size_t i = 0; i < size; i++)
toReturn = input[i] > toReturn ? input[i] : toReturn;
return toReturn+1;
}
////////////////////////////////////////////////////////////////////////
// PRIVATE SUFFIX ARRAY IMPLEMENTATIONS //////////////////////////////
////////////////////////////////////////////////////////////////////////
/*Resursive Bucket Sort************************************************/
/***********************************************************************
* Recursive bucket sort is a simple algorithm with O(n^2) time and space
* requirements. It sorts each layer progresively from each first value
* of each suffix to the point the suffix is in it's own bucket. This is
* useful for small scale verification of correctness, but is too costly
* to use in practice.
*
* @bucket :contains a number of indicies from source to be sorted
* @source :contains sequence from which the suffix array is being
* constructed
* @depth :used to track the depth of the algorithm so suffixes can be
* compared appropriately. First call should always be 0.
***********************************************************************/
void recursiveBucketSort(size_t *bucket, const size_t bucketSize,
const u8 *source, const size_t sourceLength, size_t depth){
//DECLARATIONS//////////////////////////////////////////////////////////
size_t *count, *ptrTrackerUnmod, *ptrTrackerMod;
size_t i, *tmpBucket;
const size_t nextDepth = depth+1;
size_t overflowIndex;
//MEMORY ALLOCATION BUG HACK////////////////////////////////////////////
do{
for(i = 1; i < bucketSize; i++){
if(bucket[i] + depth >= sourceLength) break;
if(source[bucket[0] + depth] != source[bucket[i] + depth]) break;
}
if(i == bucketSize) depth++;
}while(i == bucketSize);
//INITIALIZATIONS///////////////////////////////////////////////////////
overflowIndex = 0;
ptrTrackerUnmod = malloc(sizeof(size_t) * 256);
ptrTrackerMod = malloc(sizeof(size_t) * 256);
count = malloc(sizeof(size_t) * 256);
tmpBucket = malloc(sizeof(size_t) * bucketSize);
memset(count, 0, sizeof(size_t) * 256);
//OPERATIONS////////////////////////////////////////////////////////////
ptrTrackerUnmod[0] = 0;
for(i = 0; i < bucketSize; i++){
if(bucket[i] + depth >= sourceLength){
ptrTrackerUnmod[0] = 1;
overflowIndex = i;
tmpBucket[0] = i;
break;
}
}
for(i = 0; i < bucketSize; i++)
if(ptrTrackerUnmod[0] == 0 || i != overflowIndex)
count[source[bucket[i] + depth]]++;
for(i = 1; i < 256; i++) ptrTrackerUnmod[i] = ptrTrackerUnmod[i-1] +
count[i-1];
memcpy(ptrTrackerMod, ptrTrackerUnmod, sizeof(size_t) * 256);
for(i = 0; i < bucketSize; i++)
if(ptrTrackerUnmod[0] == 0 || i != overflowIndex)
tmpBucket[ptrTrackerMod[source[bucket[i]+depth]]++] = bucket[i];
free(ptrTrackerMod); ptrTrackerMod = NULL;
memcpy(bucket, tmpBucket, sizeof(size_t) * bucketSize);
free(tmpBucket); tmpBucket = NULL;
for(i = 0; i < 256; i++)
if(count[i] > 1)
recursiveBucketSort(&bucket[ptrTrackerUnmod[i]], count[i], source,
sourceLength, nextDepth);
free(count);
free(ptrTrackerUnmod);
}
/*Suffix Array Induced Sorting*****************************************/
/***********************************************************************
* Suffix Array Induced Sorting (SAIS) is a linear time/space suffix
* array construction algorithm which competes with BPR2 for top
* time/space requirements. TODO: reference paper here.
*
* Currently this code is being changes to enable testing of run-removal
* on the code. Run removal enforces stricter conditions on the input
* sequence which allows for some of the logic to be simplified. This
* experimental approach is particularly useful for inputs with small
* alphabets in the general case, but definitionally saves on sequences
* which are known to have many runs of identical values in the sequence.
*
* @source :The sequence to construct the suffix array on.
* @runsRem :
*
* @alphabetSize : not actually alphabet size, but highest value seen in
* alphabet. Might change in future.
*
*
* Notes
* 0 = undefined, 1 = L, 2 = S, 3 = {LMS, M}
***********************************************************************/
sequence SAIS(const u8 *source, const size_t sourceLength,
const sequence runsRem, const u8 alphabetSize){
//DECLARATIONS//////////////////////////////////////////////////////////
sequence toReturn, sanityCheck;
size_t **bucket;
size_t **oldBucket;
size_t i;
size_t *bucketSize;
size_t *bucketFrontCounter;
size_t *bucketEndCounter;
unsigned char *LMSandLS;
////INITIALIZATION//////////////////////////////////////////////////////
LMSandLS = malloc(sizeof(unsigned char) * runsRem.size);
bucket = malloc(sizeof(*bucket) * alphabetSize);
oldBucket = malloc(sizeof(*oldBucket) * alphabetSize);
bucketSize = calloc(sizeof(size_t)* alphabetSize, 1);
bucketFrontCounter = calloc(sizeof(size_t)* alphabetSize, 1);
bucketEndCounter = calloc(sizeof(size_t)* alphabetSize, 1);
sanityCheck = initSequence(runsRem.size);
memset(sanityCheck.S, 0, sizeof(*sanityCheck.S) * sanityCheck.size);
sanityCheck.size = 0;
/*prescan for buckets************************************************/
//calculate bucket sizes
for(i = 0; i < runsRem.size; i++) bucketSize[source[runsRem.S[i]]]++;
//calculate bucket start and stops
for(short i = 0; i < alphabetSize; i++){
bucket[i] = calloc(sizeof(size_t), bucketSize[i]);
oldBucket[i] = calloc(sizeof(size_t), bucketSize[i]);
}
#ifdef DEBUG
//first place where bucket data can be printed
fprintf(stderr, "%lu\n", runsRem.size);
printBucket(bucket, alphabetSize, bucketSize);
#endif
//OPERATION/////////////////////////////////////////////////////////////
/*set up L, S, and LMS metadata**************************************/
/*The paper stipulates an additional universally minimal character
* which is definitionally LMS, but here it is simulated.*/
//Assign characters' values right to left (end to beginning) for L, S,
//and LMS
size_t loopUntil = runsRem.size - 2;
LMSandLS[runsRem.size-1] = _L_;
for(i = loopUntil; i != ((size_t)0)-1; i--)
LMSandLS[i] = source[runsRem.S[i]] > source[runsRem.S[i+1]] ? _L_ : _S_;
i=0;
while(1){
while(i < loopUntil && LMSandLS[i] == _L_) i++;
if(i >= loopUntil) break;
LMSandLS[i++] = _LMS_;
while(i < loopUntil && LMSandLS[i] == _S_) i++;
if(i >= loopUntil) break;
}
#ifdef DEBUG
printLMSandLS(LMSandLS, runsRem.size);
fprintf(stderr, "\n\nAdding to buckets\n\n");
#endif
/*PRIMEER***************************************Add entries to buckets*/
//This is supposed to prepare the data to be induce sorted.
memcpy(bucketEndCounter, bucketSize, sizeof(*bucketEndCounter) * alphabetSize);
bucket[source[runsRem.S[runsRem.size-1]]][0] = runsRem.size-1;
//bucketFrontCounter[bucketLocation]++;
//LMS type right-to-left scan -- Add LMS entries to the ends of
//various buckets going from right to left. The result is partially
//full buckets with LMS entries in acending order.
for(size_t i = runsRem.size-1; i != ((size_t)0)-1; i--){
if(LMSandLS[i] == _LMS_){
const unsigned char target = source[runsRem.S[i]];
bucket[target][--bucketEndCounter[target]] = i;
}
}
/*LOOP OVER UNTIL COMPLETE*********************************************/
//TODO: there's some really ugly ways to make this run faster.
//L type left-to-right scan, not exactly a direct reasoning for this,
//please refer to the paper. Bounds checking was used in place of
//checking for negative values so that -1 didn't have to be used,
//allowing architentually maximal string length.
char goOn;
do{
goOn = 0;
memset(sanityCheck.S, 0, sizeof(*sanityCheck.S) * runsRem.size);
sanityCheck.size = 0;
//step 3 of setting up SA
//L type right to left scan.
memcpy(bucketEndCounter, bucketSize, sizeof(*bucketEndCounter) * alphabetSize);
for(i = alphabetSize-1; i != ((size_t)0)-1 ; i--){
for(size_t j = bucketSize[i]-1; j != ((size_t)0)-1; j--){
if(!bucket[i][j]) continue;
const size_t target = bucket[i][j]-1;
if(LMSandLS[target] == _L_ || LMSandLS[target] == _LMS_){
char KILLYOSELF = 0;
if(source[runsRem.S[target]] == source[runsRem.S[runsRem.size-1]] && bucketEndCounter[source[runsRem.S[target]]]-1 == 0){
KILLYOSELF = 1;
printf("Trying to write over something you're blatently not supposed to write over.\n");
}
for(size_t k = 0; k < sanityCheck.size; k++)
if(sanityCheck.S[k] == target){
KILLYOSELF=1;
printf("trying to write a %lu a second time.\n", target);
}
if(KILLYOSELF){
printf("KILL YO SELF in r to l\n");
exit(1);
}
const unsigned char target2 = source[runsRem.S[target]];
bucket[target2][--bucketEndCounter[target2]] = target;
sanityCheck.S[sanityCheck.size++] = target;
}
}
}
#ifdef DEBUG
printBucket(bucket, alphabetSize, bucketSize);
#endif
//S type left to right scan.
memset(bucketFrontCounter, 0, sizeof(*bucketFrontCounter) * alphabetSize);
//bucket[source[runsRem.S[runsRem.size-1]]][0] = runsRem.size-1;
bucketFrontCounter[source[runsRem.S[runsRem.size-1]]] = 1;//protect last index
for(int i = 0; i < alphabetSize; i++){
for(size_t j = 0; j < bucketSize[i]; j++){
if(!bucket[i][j]) continue;
const size_t target = bucket[i][j]-1;
if(LMSandLS[target] == _S_){
char KILLYOSELF = 0;
if(source[runsRem.S[target]] == source[runsRem.S[runsRem.size-1]] && bucketEndCounter[source[runsRem.S[target]]]-1 == 0){
KILLYOSELF = 1;
printf("Trying to write over something you're blatently not supposed to write over.\n");
}
for(size_t k = 0; k < sanityCheck.size; k++)
if(sanityCheck.S[k] == target){
KILLYOSELF=1;
printf("trying to write a %lu a second time.\n", target);
}
if(KILLYOSELF){
printf("KILL YO SELF in l to r\n");
exit(1);
}
const unsigned char target2 = source[runsRem.S[target]];
bucket[target2][bucketFrontCounter[target2]++] = target;
sanityCheck.S[sanityCheck.size++] = target;
}
}
}
for(i = 0; i < alphabetSize; i ++)
if(memcmp(bucket[i], oldBucket[i], bucketSize[i] * sizeof(size_t))){
for(size_t j = i; j < alphabetSize; j++)
if(bucketSize[j])
memcpy(oldBucket[j], bucket[j], sizeof(*bucket) * bucketSize[j]);
goOn = 1;
break;
}
}while(goOn);
#ifdef DEBUG
printBucket(bucket, alphabetSize, bucketSize);
#endif
//CLEAN UP//////////////////////////////////////////////////////////////
free(LMSandLS);
toReturn = initSequence(runsRem.size);
toReturn.size = 0;
for(i = 0; i < alphabetSize; i++)
for(size_t j = 0; j < bucketSize[i]; j++)
toReturn.S[toReturn.size++] = bucket[i][j];
return toReturn;
}
/***********************************************************************
* tweaked BPR2 to handle some changes nessicary for run removal
***********************************************************************/
sequence bpr2dereferenced(const u8 *source, const size_t length, const sequence input){
sequence toReturn = initSequence(input.size);
memcpy(toReturn.S, input.S, toReturn.size * sizeof(*toReturn.S));
recursiveBucketSort(toReturn.S, toReturn.size, source, length, 0);
return toReturn;
}
/***********************************************************************
* original BPR2
***********************************************************************/
sequence bpr2direct(const u8 *source, const size_t length){
sequence toReturn = initSequence(length);
for(size_t i = 0; i < length; i++) toReturn.S[i] = i;
recursiveBucketSort(toReturn.S, toReturn.size, source, length, 0);
return toReturn;
}
/***********************************************************************
* Switching to BPR2 over SAIS because SAIS became unruley and BPR2
* seems to have better performance characteristics. Still
* investigating.
***********************************************************************/
size_t* getSortedSuffixArray(const u8 *input, const size_t length){
//DECLARATIONS//////////////////////////////////////////////////////////
sequence toReturn, intermediate, runsRem;
//INITIALIZATIONS///////////////////////////////////////////////////////
runsRem = removeRuns(input, length);
//OPERATIONS////////////////////////////////////////////////////////////
if((runsRem.size * 1.0) / length > DEREFERENCE_BREAK_EVEN){
free(runsRem.S);
toReturn = bpr2direct(input, length);
}else{
u8 alphabetSize = getAlphabetSize(input, length);
intermediate = SAIS(input, length, runsRem, alphabetSize);
toReturn = addRuns(input, length, intermediate);
free(runsRem.S);
free(intermediate.S);
}
//CLEAN UP//////////////////////////////////////////////////////////////
//free(intermediate.S);
return toReturn.S;
}
////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS //////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
SuffixArray makeSuffixArray(const u8* inputSequence,
const size_t inputLength){
#ifdef DEBUG
assert(inputLength > 0);
assert(inputSequence != NULL);
fprintf(stderr, "Initializing Suffix Array\n"); fflush(stdout);
#endif
SuffixArray toReturn = {inputSequence, false, inputLength,
getSortedSuffixArray(inputSequence, inputLength)};
#ifdef DEBUG
fprintf(stderr, "Finished initializing Suffix Array\n"); fflush(stdout);
#endif
return toReturn;
}
EnhancedSuffixArray makeEnhancedSuffixArray(const SuffixArray toProcess){
#ifdef DEBUG
fprintf(stderr, "Initializing EnhancedSuffixArray\n"); fflush(stdout);
#endif
EnhancedSuffixArray toReturn;// = {toProcess,
// AppendIdentInit(toProcess.sequence, toProcess.length,
// toProcess.sa_data)};
#ifdef DEBUG
fprintf(stderr, "Finished initializing EnhancedSuffixArray\n"); fflush(stdout);
#endif
return toReturn;
}
SuffixArray copySequenceToLocal(const SuffixArray toMod){
assert(false == toMod.doIOwnSequence);
u8 *tmp = malloc(sizeof(size_t) * toMod.length);
memcpy(tmp, toMod.sequence, sizeof(size_t) * toMod.length);
SuffixArray toReturn = {tmp, true, toMod.length, toMod.sa_data};
return toReturn;
}
void freeSuffixArray(SuffixArray *toFree){
SuffixArrayCaster *force = (SuffixArrayCaster*) toFree;
if(force->doIOwnSequence) free(force->sequence);
free(force->sa_data);
}
void freeEnhancedSuffixArray(EnhancedSuffixArray *toFree){
freeSuffixArray(&toFree->sa_struct);
free((size_t*)toFree->LCPArray);
}
#ifdef DEBUG
void printSuffixArrayContainer(EnhancedSuffixArray toDump){
fprintf(stderr, "i\tsuftab\tlcptab\tbwttab\tSsuftab[i]\n"); fflush(stdout);
for(size_t i = 0; i < toDump.sa_struct.length; i++){
fprintf(stderr, "%lu\t", i); fflush(stdout);
fprintf(stderr, "%lu\t", toDump.sa_struct.sa_data[i]); fflush(stdout);
fprintf(stderr, "%lu\t", toDump.LCPArray[i]); fflush(stdout);
fprintf(stderr, "%c\t", toDump.sa_struct.sequence[(toDump.sa_struct.sa_data[i] - 1 + toDump.sa_struct.length)%toDump.sa_struct.length]); fflush(stdout);
for(size_t j = toDump.sa_struct.sa_data[i]; j < toDump.sa_struct.length; j++){
fprintf(stderr, "%c", toDump.sa_struct.sequence[j]); fflush(stdout);
}
fprintf(stderr, "\n"); fflush(stdout);
}
}
#endif