-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearSuffixSort.cpp
More file actions
3290 lines (2581 loc) · 90.5 KB
/
LinearSuffixSort.cpp
File metadata and controls
3290 lines (2581 loc) · 90.5 KB
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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <ctype.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include "BooleanString.h"
#include "Timing.h"
#define INT_SIZE 32
#define MAX_INT_INDEX 31
#define MAX_NUM_CHAR 256
#define MAX_CHAR_VAL 255
/*********************************
Reads input from the file associated with inFile.
Pre: inFile is associated with a file, i.e. open is called.
Post: inputString holds the characters from the file,
inputLength is the length of the string from the file.
Modify: inputString, inputLength.
*********************************/
void read_input(ifstream& inFile, char*& inputString, int& inputLength);
/*********************************
Similar to read_input, but this function reads the first sequence
in a fasta format file.
*********************************/
void read_fasta(ifstream& inFile, char*& inputString, int& inputLength);
/*********************************
Converts a char string to a int string.
Pre: inputString is of length stringLength, and stringLength
is non-zero and non-negative.
Post: returns a pointer to a string who's length is stringLength
the character A in the original string is given the value
0. The value of other characters are calculated by subtracting
the value of A, e.g. C has a value of 2. A value of 0 is
added to the end of the string this acts like the character
'$', and the length of string is incremented by 1.
Modify: stringLength++.
*********************************/
int* charToInt(const char* inputString, int& stringLength);
/*********************************
Same as the other charToInt function expect let user specify, the
smallest character by using baseChar.
*********************************/
int* charToInt(const char* inputString, int& stringLength,
const char baseChar);
/*********************************
Preforms the linear time suffix sort for string of integer alphabet.
Pre: inputString is of length stringLength, and stringLength
is non-zero and non-negative.
inputString[stringLength - 1], i.e. the last integer of
the string must be the smallest integer in the entire
inputString, and it must be unique.
Post: Returns an array A, where A[i] give the index of the
i-th smallest suffix in the string.
Modify: None
*********************************/
int* LinearSuffixSort(const int* inputString, const int stringLength);
/*********************************
Preforms the linear time suffix sort knowing minChar and maxChar
*********************************/
int* LinearSuffixSort(const int* inputString, const int stringLength,
const int minChar, const int maxChar);
/*********************************
Same as int* LinearSuffixSort(const int*, const int); but for ASCII
characters.
Pre: inputString is of length stringLength, and stringLength
is non-zero and non-negative.
ASCII character '0' is not a part of inputString.
Post: Returns an array A, where A[i] give the index of the
i-th smallest suffix in the string.
ASCII character '0' is appended to the end of the inputString.
stringLength increases by 1.
Modify: inputString, stringLength.
*********************************/
int* LinearSuffixSort(char*& inputString, int& stringLength);
/*******************************************************************/
/* The functions in this section are functions called by */
/* LinearSuffixSort, or helper functions during debugging, users */
/* Should not use those functions in their program. */
/*******************************************************************/
/*********************************
These functions are for debugging purpose.
These functions output the content of a string
*********************************/
void outstring(const int* inputString, const int stringLength);
void outstring3(const int* inputString, const int stringLength);
void outstringS(const int* inputString, const int stringLength);
void outstring(const char* inputString, const int stringLength,
const char* strSpace);
void outsuffix(const int* intString, const int intStringLen,
const char* charString, const int charStringLen,
const int suffixLength);
void outsubstring(const int* intString, const int intStringLen,
const char* charString, const int charStringLen,
const BooleanString& suffixType);
/*********************************
Determines the type of each suffix.
If suffixType[i] is true(1) then T[i] is a type S suffix.
Else if suffixType[i] is false(0) then T[i] is a type L suffix.
Pre: inputString is a integer array of length inputLength;
numStype, numLtype are integers.
suffixType is a BooleanString of length inputLength;
Post: Calculate the type of each suffix in string inputString.
The type of the last suffix depends on which type of
suffixes is less in number excluding the last suffix.
Therefore, user should not compare numStype and numLtype,
instead it should look at the type of the last suffix.
numStype holds the number of type S suffixes;
numLtype holds the number of type L suffixes;
suffixType[i] gives the type of each suffixes.
Modify: numLtype, numStype, suffixType.
*********************************/
void suffix_type(const int* inputString, const int inputLength,
int& numStype, int& numLtype,
BooleanString& suffixType);
/*********************************
Determines the type of each suffix. Except the inputString is
a character string of length inputLength
*********************************/
void suffix_type(const char* inputString, const int inputLength,
int& numStype, int& numLtype,
BooleanString& suffixType);
/*********************************
Find the maximum and minimum value of an integer array.
Pre: inputString is of length inputLength, Max and Min are
integers.
Post: Max will contain the value of the maximum element.
Min will contain the value of the minimum element.
Modify: Max, Min.
*********************************/
inline void findMaxMin(const int* inputString, const int inputLength,
int& Max, int& Min)
{
int i, temp;
if (inputLength <= 0)
{
cout << "In function findMaxMin(int*, int, int&, int&):" << endl;
cout << "Length of input string cannot be less than 0."
<< endl;
exit(0);
}
Max = inputString[0];
Min = inputString[0];
for (i = 1; i < inputLength; i++)
{
temp = Max - inputString[i];
temp = temp >> MAX_INT_INDEX;
Max = Max + ((inputString[i] - Max) & temp);
temp = inputString[i] - Min;
temp = temp >> MAX_INT_INDEX;
Min = Min - ((Min - inputString[i]) & temp);
}
}
/*********************************
Same as findMaxMin, but for character strings.
*********************************/
inline void findMaxMin(const char* inputString, const int inputLength,
char& Max, char& Min)
{
int i;
char temp;
if (inputLength <= 0)
{
cout << "In function findMaxMin(int*, int, int&, int&):" << endl;
cout << "Length of input string cannot be less than 0."
<< endl;
exit(0);
}
Max = inputString[0];
Min = inputString[0];
for (i = 1; i < inputLength; i++)
{
temp = Max - inputString[i];
temp = temp >> MAX_INT_INDEX;
Max = Max + ((inputString[i] - Max) & temp);
temp = inputString[i] - Min;
temp = temp >> MAX_INT_INDEX;
Min = Min - ((Min - inputString[i]) & temp);
}
}
/*********************************
Do counting sort on inputString. This is the same as sorting
each suffix according to their first character.
Pre: inputString is of length inputLength;
A is a pointer to an array of length inputLength;
A must be initalized with "new".
BuckA is a BooleanString of length inputLength.
buffer is a int array of length inputLength.
Post: A contains the index of the suffixes sorted
according to the first character.
BuckA[i] is set to true, if there is an bucket bundary
between A[i] and A[i+1]. The last element of BuckA
will always be 1, because it's the end of the array.
Modify: A, BuckA.
*********************************/
void counting_sort(const int* inputString, const int inputLength,
int* A, BooleanString& BuckA, int* buffer);
/*********************************
Same as counting_sort, but min & max char is provided.
*********************************/
void counting_sort(const int* inputString, const int inputLength,
int* A, BooleanString& BuckA, const int minChar,
const int maxChar, int* buffer);
/*********************************
Sort a bucket of type S substrings using counting sort.
Pre: inputString is a character string of inputLength,
A is an array containing the begining position of
all type S substrings.
A is of length ALength - the number of type S substrings.
BuckA is of length ALength.
suffixType contains the type of all suffixes of inputString.
maxDist is the maximum reverse S-distance of inputString.
Post: A contains the sorted order of all type S substrings.
BuckA contains the bucket boundary of A.
Modify: A, BuckA.
*********************************/
void sort_s_substringC(const char* inputString, const int inputLength,
int* A, BooleanString& BuckA, const int ALength,
const BooleanString& suffixType, const int maxDist);
/*********************************
Same as sort_s_substringC but for type L substrings.
*********************************/
void sort_l_substringC(const char* inputString, const int inputLength,
int* A, BooleanString& BuckA, const int ALength,
const BooleanString& suffixType, const int maxDist);
/*********************************
Compute the S-Distance of all suffixes according to algorithm.
Pre: suffixType contains the type of each suffix. It is of
length inputLength.
Dist is initialized with "new", and have length
inputLength;
DistCount is not initalized.
maxDist is a integer.
Post: Dist will contain the S-Distance of all suffixes.
DistCount[i] give the number of suffixes that have
S-Distance less than or equal to i, for 0 <= i < maxDist.
DistCount[maxDist] give the total number of non-zero
S-Distance suffixes. DistCount have length maxDist+1.
maxDist gives the maximum S-Distance length.
Note: DistCount[maxDist] = 0 if only 1 type S suffix exists
in the string, maxDist is also 0 in this case. Thus
in the case only 1 type S suffix exists this function
should not be called.
Modify: Dist, DistCount, maxDist.
*********************************/
void s_distance(const BooleanString& suffixType,
const int inputLength, int* Dist, int*& DistCount,
int& maxDist);
/*********************************
Similar to function s_distance
but this calculates the l-distance of each suffix.
*********************************/
void l_distance(const BooleanString& suffixType,
const int inputLength, int* Dist, int*& DistCount,
int& maxDist);
/*********************************
Compute the reverse S-Distance for all type S suffix.
i.e. the distance of a type S
suffix from the nearest type S suffix on the right instead of
left. The reverse S-Distance of the last suffix '$' is defined
to be 0. This function only returns the maximum S-Distance.
Pre: suffixType contains the type of each suffix. It is of
length inputLength.
inputLength is the length of the suffixType.
maxDist is a integer.
Post: maxDist gives the maximum S-Distance length.
Modify: Dist, DistCount, maxDist.
*********************************/
void s_distanceR(const BooleanString& suffixType,
const int inputLength, int& maxDist);
/*********************************
Similar to function s_distance(BooleanString,Char,int,int,int)
but this calculates the reverse l-distance of each type L suffix.
*********************************/
void l_distanceR(const BooleanString& suffixType,
const int inputLength, int& maxDist);
/*********************************
Construct the ArrayB of type S from ArrayA.
Pre: ArrayA is an array with all the suffixes sorted according to
their first character.
inputLength determines the length of ArrayA.
BuckA is of length inputLength and BuckA[i] is true if
there is a bucket bundary between ArrayA[i] and ArrayA[i+1]
The result will be stored in ArrayB, and BuckB marks the
bucket bundary of ArrayB.
suffixType[i] give the value of the i-th suffix.
Post: BuckB have all the suffixes sorted according to their
first character. BuckB marks the bucket bundary of ArrayB.
BuckB[i] = true if there is a boundary between ArrayB[i]
and ArrayB[i+1].
Modify: ArrayB, BuckB.
*********************************/
void construct_ArrayB_typeS(const int* ArrayA, const int inputLength,
const BooleanString& BuckA,
int* ArrayB, BooleanString& BuckB,
const BooleanString& suffixType);
/*********************************
Similar to construct_ArrayB_typeS, but this function calculates the
ArrayB of type L from ArrayA.
*********************************/
void construct_ArrayB_typeL(const int* ArrayA, const int inputLength,
const BooleanString& BuckA,
int* ArrayB, BooleanString& BuckB,
const BooleanString& suffixType);
/*********************************
Construct the m Lists.
Pre: ArrayA is an integer array of size inputLength.
ArrayA is all the suffixes bucketed according to their
first character.
Dist is an integer array of size inputLength.
Dist contains all the S-distance, or L-distance of
all the suffixes.
DistCount is of size maxDist+1.
DistCount[i] give the number of suffixes having
S-distance of i or less (not counting 0).
BuckA is of length inputLength.
BuckA give the bucket boundaries of ArrayA. i.e.
BuckA[i] = 1 if there is a boundary between
ArrayA[i] and ArrayA[i+1].
BuckA[inputLength - 1] is always 1.
BuckList is of length listLength, listLength must
not be 0.
listLength is equal to DistCount[maxDist], which is
potentially 0 in some case, refer to s_distance().
Post: ArrayA is not pointing to the List.
Dist is no longer vaid, and is the reverse mapping
array of the List.
DistCount[i] now give the number of suffixes of
S-distance of i+1 or less.
BuckList give the bucket boundary of all buckets.
Modify: ArrayA, Dist, BuckList, DistCount
*********************************/
int* construct_list_typeS(int*& ArrayA, const int inputLength, int* Dist,
int* DistCount, const int maxDist,
const BooleanString& BuckA,
BooleanString& BuckList, const int listLength);
/*********************************
Similar to function construct_list_typeS
but this is for constructing the list for type L substrings.
*********************************/
int* construct_list_typeL(int*& ArrayA, const int inputLength, int* Dist,
int* DistCount, const int maxDist,
const BooleanString& BuckA,
BooleanString& BuckList, const int listLength);
/*********************************
Bucket the substrings by refering to the m Lists. This is for
sorting type S substrings.
Pre: ArrayB is an integer array of size ArrayBLength.
ArrayB is all the type S or type L substrings bucketed
according to their first character.
BuckB is the bucket boundaries of ArrayB.
BuckB[i] = true if there is a boundary between ArrayB[i]
and ArrayB[i+1].
List is the m Lists, it is of size listLength.
Each element List[i] refer to the beginning index of
the type S or type L substring it is in.
BuckList gives the boundaries of List.
BuckList[i] = true if there is a boundary between List[i]
and List[i+1].
inputLength is the length of the string.
listLength is the length of List.
ArrayBLength is the length of ArrayB.
Post: ArrayB is all the type S or type L substrings bucketed.
BuckB is the bucket boundaries of ArrayB.
BuckB[i] = true if there is a boundary between ArrayB[i]
and ArrayB[i+1].
Modify: ArrayB, BuckB.
*********************************/
void sort_by_list_typeS(int* ArrayB, BooleanString& BuckB,
int* List, const BooleanString& BuckList,
const int inputLength, const int listLength,
const int ArrayBLength);
/*********************************
Simular to function sort_by_list_typeS
but this is for sorting the type L substrings.
*********************************/
void sort_by_list_typeL(int* ArrayB, BooleanString& BuckB,
int* List, const BooleanString& BuckList,
const int inputLength, const int listLength,
const int ArrayBLength);
/*********************************
Construct the suffix array, by using the sorted order
of type S suffixes.
Pre: ArrayB is the array with the sorted order of
type S suffixes, and it's of length
ArrayBLength.
stringT is the orginal string, and its length
is inputLength.
The smallest element of stringT is assumed to
be 0.
And the largest element of stringT is assumed to
be < inputLength.
suffixType is the BooleanString with the type
of each suffix. suffixType[i] = true iff
stringT[i] is a type S suffix, otherwise
suffixType[i] = false.
suffixArray is an empty array of length
inputLength, suffixArray must be initalized
with operator "new".
Post: suffixArray will contain the sorted order of
all suffixes of stringT.
Modify: suffixArray.
*********************************/
void construct_SA_typeS(int* ArrayB, const int ArrayBLength,
const int* stringT, const int inputLength,
const BooleanString& suffixType,
int* suffixArray);
/*********************************
Similar to construct_SA_typeS, except this function
is for using sorted order of type L suffixes to obtain
the order of the suffix array.
*********************************/
void construct_SA_typeL(int* ArrayB, const int ArrayBLength,
const int* stringT, const int inputLength,
const BooleanString& suffixType,
int* suffixArray);
/*********************************
counterpart of construct_SA_typeS for int stringT
*********************************/
void construct_SA_typeS(int* ArrayB, const int ArrayBLength,
const char* stringT, const int inputLength,
const BooleanString& suffixType,
int* suffixArray);
/*********************************
counterpart of construct_SA_typeS for int stringT
*********************************/
void construct_SA_typeL(int* ArrayB, const int ArrayBLength,
const char* stringT, const int inputLength,
const BooleanString& suffixType,
int* suffixArray);
/*********************************
Construct the new string T' from array B
Pre: ArrayB is all the type S substrings bucketed
lexicographically.
ArrayB is of length ArrayBLength.
BuckB marks the bucket boundaries of ArrayB.
tPrime an integer array of length ArrayBLength.
Memory space must be allocated for tPrime before
the function call.
inputLength is the length of the original string.
suffixType have all the types of all the suffixes.
Post: tPrime is the new string, and tPrime is of length
ArrayBLength.
Modify: tPrime.
*********************************/
void construct_TPrime_typeS(int* ArrayB, const int ArrayBLength,
const BooleanString& BuckB,
int* tPrime, const int inputLength,
const BooleanString& suffixType);
/*********************************
Similar to construct_TPrime_typeS, instead this function
construct the new string for all the type L suffixes.
*********************************/
void construct_TPrime_typeL(int* ArrayB, const int ArrayBLength,
const BooleanString& BuckB,
int* tPrime, const int inputLength,
const BooleanString& suffixType);
/*********************************
Construct the new string T' from array B
Pre: ArrayB is all the type S substrings bucketed
lexicographically.
ArrayB is of length ArrayBLength.
BuckB marks the bucket boundaries of ArrayB.
tPrime an integer array of length ArrayBLength.
Memory space must be allocated for tPrime before
the function call.
inputLength is the length of the original string.
suffixType have all the types of all the suffixes.
Post: tPrime is the new string, and tPrime is of length
ArrayBLength.
Returns the length of tPrime, because some of the
type S suffix may not be needed.
Modify: tPrime.
*********************************/
int construct_TPrime_NU_S(int* ArrayB, const int ArrayBLength,
const BooleanString& BuckB,
int* tPrime, const int inputLength,
const BooleanString& suffixType);
/*********************************
Given the sorted order of all type S suffixes as a suffix in
T', the sorted order is indexed as suffixes of T' not as
type S suffixes of T. Convert the index back to reflect the
position in T.
Pre: ArrayB is of length ArrayBLength.
ArrayB contains the sorted order of all suffixes of
T'.
suffixType gives the type of all suffixes of T.
inputLength is the length of T or suffixType.
Post: ArrayB now contains the sorted order of all
type S suffixes of T. i.e. the index is converted
from T' to T.
Modify: ArrayB.
*********************************/
void reconstruct_B_typeS(int* ArrayB, const int ArrayBLength,
const BooleanString& suffixType,
const int inputLength);
/*********************************
Same as reconstruct_B_typeS, instead this function operates
on type L suffixes.
*********************************/
void reconstruct_B_typeL(int* ArrayB, const int ArrayBLength,
const BooleanString& suffixType,
const int inputLength);
void read_input(ifstream& inFile, char*& inputString, int& inputLength)
{
int i, count;
char temp;
inputLength = 2048;
inputString = (char*) malloc(inputLength * sizeof(char));
i = 0;
count = 0;
inFile.get(temp);
while (!inFile.eof())
{
if (isalpha(temp) || isdigit(temp))
{
inputString[i] = toupper(temp);
i++;
}
if (i == inputLength - 1)
{
inputLength += 256;
inputString = (char*) realloc((void*)inputString,
inputLength * sizeof(char));
}
inFile.get(temp);
}
inputLength = i;
}
void read_fasta(ifstream& inFile, char*& inputString, int& inputLength)
{
char temp;
// skip the description of the sequence, if there is any.
inFile.get(temp);
while (isalpha(temp) == 0 && temp != '>' && !inFile.eof())
{
inFile.get(temp);
}
if (temp == '>')
{
inFile.get(temp);
while (temp != '\n')
{
inFile.get(temp);
}
read_input(inFile, inputString, inputLength);
}
else if (isalpha(temp) != 0)
{
read_input(inFile, inputString, inputLength);
}
else if (inFile.eof())
{
exit(0);
}
}
void suffix_type(const int* inputString, const int inputLength,
int& numStype, int& numLtype,
BooleanString& suffixType)
{
int i, j, k;
j = 0;
numStype = 0;
numLtype = 0;
suffixType.setAll(true);
for (i = 0; i < inputLength - 1; i++)
{
/****************************************/
//If character T[i] does not equal to T[i+1] make a
//decision and go back to mark all the previously
//undecided suffix a certain type. Also increment the
//counter for that type. And set undecided suffix
//counter (j) to 0.
/****************************************/
if (inputString[i] < inputString[i + 1])
{
for (k = i - j; k <= i; k++)
{
numStype++;
suffixType.setValN(k, 1);
}
j = 0;
}
else if (inputString[i] > inputString[i + 1])
{
for (k = i - j; k <= i; k++)
{
numLtype++;
suffixType.setValN(k, 0);
}
j = 0;
}
else
{
/**************************************/
//If two adjacent suffixes have the same first
//character, move on, to the next, but remember the
//number of undecided suffixes by increment j.
/**************************************/
j++;
}
}
/******************************************/
//The last suffix $ must be selected no matter we choose
//to sort L or to sort S. So the type of the last suffix
//is set to which ever type that is smaller in number.
/******************************************/
if (numStype < numLtype)
{
suffixType.setValN(inputLength - 1, 1);
numStype++;
}
else
{
suffixType.setValN(inputLength - 1, 0);
numLtype++;
}
}
void suffix_type(const char* inputString, const int inputLength,
int& numStype, int& numLtype,
BooleanString& suffixType)
{
int i, j, k;
j = 0;
numStype = 0;
numLtype = 0;
suffixType.setAll(true);
for (i = 0; i < inputLength - 1; i++)
{
/****************************************/
//If character T[i] does not equal to T[i+1] make a
//decision and go back to mark all the previously
//undecided suffix a certain type. Also increment the
//counter for that type. And set undecided suffix
//counter (j) to 0.
/****************************************/
if (inputString[i] < inputString[i + 1])
{
for (k = i - j; k <= i; k++)
{
numStype++;
suffixType.setValN(k, 1);
}
j = 0;
}
else if (inputString[i] > inputString[i + 1])
{
for (k = i - j; k <= i; k++)
{
numLtype++;
suffixType.setValN(k, 0);
}
j = 0;
}
else
{
/**************************************/
//If two adjacent suffixes have the same first
//character, move on, to the next, but remember the
//number of undecided suffixes by increment j.
/**************************************/
j++;
}
}
/******************************************/
//The types of all characters, except the last one is set.
//Because we are comparing inputString[i] and
//inputString[i+1] so even if the last few characters
//are the same, the very last character will determine
//their type.
/******************************************/
/******************************************/
//The last suffix $ must be selected no matter we choose
//to sort L or to sort S. So the type of the last suffix
//is set to which ever type that is smaller in number.
/******************************************/
if (numStype < numLtype)
{
suffixType.setValN(inputLength - 1, 1);
numStype++;
}
else
{
suffixType.setValN(inputLength - 1, 0);
numLtype++;
}
}
int* LinearSuffixSort(char*& inputString, int& stringLength)
{
int numS, numL, maxDistance, tPrimeLen;
int* arrayA;
int* arrayB;
int* tPrime;
BooleanString suffixType((stringLength + 1));
BooleanString BuckB;
//Append char 0 to the end of inputString.
//And some initialization;
// commented by Zhao XU, strings in C/C++ always has '\0' at end
// make sure the caller has alloc one more byte for inputString
// inputString = (char*) realloc((void*)inputString,
// sizeof(char) * (stringLength + 1));
//
stringLength++;
inputString[stringLength - 1] = (char) 0;
// outstring(inputString, stringLength, " ");
//Start of real program;
suffix_type(inputString, stringLength, numS, numL, suffixType);
if (suffixType.getValN(stringLength - 1) == 1 &&
numS == 1)
{
arrayB = (int*) malloc(sizeof(int));
arrayB[0] = stringLength - 1;
arrayA = (int*) malloc(stringLength * sizeof(int));
construct_SA_typeS(arrayB, numS, inputString, stringLength,
suffixType, arrayA);
free(arrayB);
return (arrayA);
}
if (suffixType.getValN(stringLength - 1) == 1)
{
//Less type S suffixes
/*
//This if is for debug.
if (suffixType.getVal(stringLength - 1) == false)
{
suffixType.setVal(stringLength - 1, true);
numS++;
}
*/
// cout << "Size of arrayB: " << sizeof(int)*numS << endl;
arrayB = (int *) malloc (sizeof(int) * numS);
BuckB.initialize(numS);
s_distanceR(suffixType, stringLength, maxDistance);
// suffixType.printAll(" ");
// cout << maxDistance << endl;
//assign arrayB[0] to the index of '$'.
//and for array[j], j >= 1, if inputString[i] is type S,
//set array[j] = i, and j++.
int j = 0;
for (int i = 0; i < stringLength; i++)
{
assert(j <= numS);
arrayB[j] = i;
j = j + suffixType.getValN(i);
}
sort_s_substringC(inputString, stringLength, arrayB, BuckB, numS,
suffixType, maxDistance);
/*
//This part prints debug information
outstring3(arrayB, numS);
BuckB.printAll(" ");
cout << endl;
suffixType.printAll(" ");
outsubstring(arrayB, numS, inputString, stringLength, suffixType);
*/
if (BuckB.isAllTrue())
{
arrayA = (int*) malloc(stringLength * sizeof(int));
construct_SA_typeS(arrayB, numS, inputString, stringLength,
suffixType, arrayA);
free((void*)arrayB);
return (arrayA);
}
tPrime = (int*) malloc(sizeof(int) * numS);
construct_TPrime_typeS(arrayB, numS, BuckB, tPrime,
stringLength, suffixType);
// outstring3(tPrime, numS);
free((void*)arrayB);
arrayB = LinearSuffixSort(tPrime, numS);
free((void*)tPrime);
reconstruct_B_typeS(arrayB, numS, suffixType, stringLength);
arrayA = (int*) malloc(stringLength * sizeof(int));
construct_SA_typeS(arrayB, numS, inputString, stringLength,
suffixType, arrayA);
free((void*)arrayB);
return (arrayA);
}
else
{
//Less type L suffixes
arrayB = (int *) malloc (sizeof(int) * numL);
BuckB.initialize(numL);
l_distanceR(suffixType, stringLength, maxDistance);
// suffixType.printAll(" ");
// cout << maxDistance << endl;
//assign arrayB[0] to the index of '$'.
//and for array[j], j >= 1, if inputString[i] is type S,
//set array[j] = i, and j++.
int j = 0;
for (int i = 0; i < stringLength; i++)
{
assert(j <= numL);
arrayB[j] = i;
j = j - (suffixType.getValN(i) - 1);
}
sort_l_substringC(inputString, stringLength, arrayB, BuckB, numL,
suffixType, maxDistance);
/*
//This part prints debug information
outstring3(arrayB, numL);
BuckB.printAll(" ");
cout << endl;
suffixType.printAll(" ");
outsubstring(arrayB, numL, inputString, stringLength, suffixType);
*/
if (BuckB.isAllTrue())
{
arrayA = (int*) malloc(stringLength * sizeof(int));
construct_SA_typeL(arrayB, numL, inputString, stringLength,
suffixType, arrayA);
free((void*)arrayB);
return (arrayA);
}
tPrime = (int*) malloc(sizeof(int) * numL);
construct_TPrime_typeL(arrayB, numL, BuckB, tPrime,
stringLength, suffixType);
// outstring3(tPrime, numL);
free((void*)arrayB);
arrayB = LinearSuffixSort(tPrime, numL);
free((void*)tPrime);
reconstruct_B_typeL(arrayB, numL, suffixType, stringLength);
arrayA = (int*) malloc(stringLength * sizeof(int));
construct_SA_typeL(arrayB, numL, inputString, stringLength,
suffixType, arrayA);
free((void*)arrayB);
return (arrayA);
}
}
void outstring(const char* inputString, const int stringLength,
const char* strSpace)
{
for (int i = 0; i < stringLength; i++)
{
cout << inputString[i] << strSpace;
}
cout << endl;
}
void outstring(const int* inputString, const int stringLength)
{
cout << inputString[0];
for (int i = 1; i < stringLength; i++)
{
cout << setw(3);