-
Notifications
You must be signed in to change notification settings - Fork 0
/
FmIndex.java
1047 lines (946 loc) · 45.3 KB
/
FmIndex.java
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
/*
* Copyright 2024 Dynatrace LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.dynatrace.fm;
import static com.dynatrace.intsequence.Common.minimumNumberOfBits;
import static com.dynatrace.serialization.Serialization.checkSerialVersion;
import com.dynatrace.bitsequence.RrrVector;
import com.dynatrace.intsequence.IntVector;
import com.dynatrace.wavelet.WaveletFixedBlockBoosting;
import it.unimi.dsi.bits.BitVector;
import it.unimi.dsi.bits.LongArrayBitVector;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.annotation.concurrent.ThreadSafe;
/**
* An FM-Index is a compressed full-text substring index based on suffix arrays, bit vectors,
* wavelet trees and the Burrows–Wheeler transform. It can be used to find the number of occurrences
* of a pattern within compressed text, locate the position of each occurrence and retrieve the
* original string. The query time and the required storage space has a sublinear complexity with
* respect to the size of the input data. Its memory requirements are sensitive to the alphabet
* size, meaning that the less different symbols in the input text, the less memory the index will
* require. Storage will increase linearly with the size of the input text.
*
* <p>For easier construction, take a look at the {@link FmIndexBuilder}. Note that this
* implementation only supports UNICODE values up to the Java default for {@code
* Character.MAX_VALUE} (which is {@code u"\U0000FFFF"}). If more is required, then consider mapping
* your alphabet to a sequence of monotonic integers first, where the maximum value is {@code
* SIZE_WCHAR}.
*
* <p>This implementation of the FM-Index makes use of Fixed-Block boosting wavelet trees (see
* {@link WaveletFixedBlockBoosting}). The currently supported operations are:
*
* <ul>
* <li>{@link FmIndex#count(char[])}: enables to count the number of occurrences of a given
* pattern.
* <li>{@link FmIndex#locate(char[], int, int, int[], int)}: enables to find all the leftmost
* starting positions of a given pattern. Its speed will depend on the parameter {@code
* sampleRate} which trades off space for speed. A {@code sampleRate} of {@code 4} means to
* store an additional integer every four input symbols but also reduces the number of
* iterations per query to a maximum of {@code 4}. Therefore, more position integers require
* more space but reduce the number of searches.
* <li>{@link FmIndex#extract(int, int, char[], int)}: enables retrieving the original input text
* from the compressed index between given positions. The {@code sampleRate} parameter has the
* same impact as with the locate query.
* <li>{@link FmIndex#extractUntilBoundary(int, char[], int, char)}: enables retrieving arbitrary
* text from a start position until a given character is found. Useful when looking for
* example for record boundaries with the newline character as a record separator starting
* from a starting match previously found with the locate query.
* <li>{@link FmIndex#extractUntilBoundaryLeft(int, char[], int, char)}: Same as {@code
* extractUntilBoundary} but only recovers the original input to the left of the starting
* point until matching the delimiter. Useful for extracting key-value pairs.
* <li>{@link FmIndex#extractUntilBoundaryRight(int, char[], int, char)}: Same as {@code
* extractUntilBoundary} but only recovers the original input to the right of the starting
* point until matching the delimiter. Useful for extracting key-value pairs.
* </ul>
*
* <p>The code here present is based on the original FM-Index article by Ferragina, Paolo, and
* Giovanni Manzini. "Opportunistic data structures with applications." In Proceedings 41st annual
* symposium on foundations of computer science, pp. 390-398. IEEE, 2000.
*/
@ThreadSafe
public final class FmIndex {
private static final byte SERIAL_VERSION_V0 = 0;
// the maximum alphabet size (256 * 256) as defined by the Char.MAX_VALUE constant
private static final int MAX_ALPHABET = Character.MAX_VALUE;
// the trade-off parameter for space and runtime. Higher sample rate means less integer
// positions
// corresponding to the suffixes are stored, therefore less memory is used. However, both
// locating
// and counting will require iterating until the next stored position
private final int sampleRate;
// monotonicMap holds the mapping for each symbol to the number in order of appearance
// we can look up the numeric value of every symbol as follows:
// short c = monotonicMap.getOrDefault((int) 'x', (short) 0) where c is the numeric value
private final Map<Integer, Short> monotonicMap;
// boolean to indicate whether we want to enable input extraction - if the application does not
// need it,
// we can save some memory
private final boolean enableExtract;
// cumulative counts of each letter (mapped)
private int[] cumulativeCounts;
// monotonicLookUp holds the symbol that corresponds to each mapping in order of appearance
private int[] monotonicLookUp;
// the positions corresponding to the entries in the suffix array - the smaller the sample rate,
// the more suffixes we need to store.
private IntVector suffixes;
// same as before but for the entries / order of the input text. This is only required if
// displaying
// or extracting (decompressing) text
private IntVector positions;
// the bit width that we are using to store the positions. For example if the maximum value that
// we
// need to store is, e.g. 7, then 3 bits are enough to represent all values from 0 to 7
// inclusive.
private int bitWidthSuffixes;
// same as before but for the order of the text
private int bitWidthPositions;
// bit vector representing which positions are sampled in the suffix array and which are not -
// needed
// as stopping condition when locating or reconstructing
private RrrVector sampledSuffixes;
// the wavelet tree (at this point, it is an array, rather) that represents the burrows-wheeler
// transform
// of the input sequence. It uses huffman encoding and fixed block boosting to reduce memory
// consumption
// compared to wavelet trees or wavelet matrices
private final WaveletFixedBlockBoosting waveletFixedBlockBoosting;
// the length of the input corpus text
private final int length;
/**
* Builds an FM-Index over the text {@code input} using the given {@code sampleRate}. The {@code
* sampleRate} controls the trade-off between space consumption and speed of locate and extract
* queries. A sample rate of {@code 8} means that an additional integer will be stored for every
* 8-th symbol in the input text, and that in order to retrieve the position of a symbol, we
* will need to iterate a maximum of {@code 8} times per symbol. Therefore, a larger {@code
* sampleRate} results in less space, but more iterations. Using a sample rate of one
* essentially results in the whole structure becoming a map from all symbols to all locations
* and will require an additional space of {@code 4} bytes times the length of the input text.
* Also note that if the recovery of the original text is enabled, then each location will also
* require an additional integer.
*
* <p>It is recommended to use the builder to create an instance of the FM-Index. See {@link
* FmIndexBuilder}.
*
* @param input A char array containing the input text
* @param sampleRate The sample rate to control the trade-off between space and locate speed.
* Typically, a value of {@code 64} is reasonable.
* @param enableExtract If the index should enable retrieving the original text, rather than
* just the count or locations of substrings. If that is the case, setting this parameter to
* {@code false} will result in saving some space.
*/
public FmIndex(char[] input, int sampleRate, boolean enableExtract) {
this.sampleRate = sampleRate;
this.enableExtract = enableExtract;
this.cumulativeCounts = new int[MAX_ALPHABET + 1]; // this is later resized to alphabet size
this.monotonicMap = new HashMap<Integer, Short>();
// Add the terminating $ character that is lexicographically smaller than any other
char[] sentinelTerminatedInput = addSentinelTerminatingCharacter(input);
this.length = sentinelTerminatedInput.length;
// Map the alphabet to a continuous, monotonically-increasing integer sequence
// example: if input is "bab\0" then X will be "2120" (a -> 1, b -> 2, \0 -> 0) (symbol
// ordered)
short[] mappedSequence = mapToMonotonicSequence(sentinelTerminatedInput);
// Create cumulative counts
fillCumulativeCounts(mappedSequence);
// Build suffix array and sample positions
int[] suffixArray = buildSuffixArrayAndSample(mappedSequence);
// Create Burrows-Wheeler Transform from the suffix array
short[] bwt = burrowsWheelerTransform(mappedSequence, suffixArray);
waveletFixedBlockBoosting = new WaveletFixedBlockBoosting(bwt, sampleRate);
}
/**
* Builds an FM-Index over the text {@code input} using the given {@code sampleRate}. The {@code
* sampleRate} controls the trade-off between space consumption and speed of locate and extract
* queries. A sample rate of {@code 8} means that an additional integer will be stored for every
* 8-th symbol in the input text, and that in order to retrieve the position of a symbol, we
* will need to iterate a maximum of {@code 8} times per symbol. Therefore, a larger {@code
* sampleRate} results in less space, but more iterations. Using a sample rate of one
* essentially results in the whole structure becoming a map from all symbols to all locations
* and will require an additional space of {@code 4} bytes times the length of the input text.
*
* <p>It is recommended to use the builder to create an instance of the FM-Index. See {@link
* FmIndexBuilder}.
*
* @param input A char array containing the input text
* @param sampleRate The sample rate to control the trade-off between space and locate speed.
* Typically, a value of {@code 64} is reasonable.
*/
public FmIndex(char[] input, int sampleRate) {
this(input, sampleRate, true);
}
private FmIndex(
int sampleRate,
boolean enableExtract,
int bitWidthSuffixes,
int bitWidthPositions,
int length,
Map<Integer, Short> monotonicMap,
int[] cumulativeCounts,
int[] monotonicLookup,
IntVector suffixes,
IntVector positions,
RrrVector sampledSuffixes,
WaveletFixedBlockBoosting wavelet) {
this.sampleRate = sampleRate;
this.enableExtract = enableExtract;
this.bitWidthSuffixes = bitWidthSuffixes;
this.bitWidthPositions = bitWidthPositions;
this.length = length;
this.monotonicMap = monotonicMap;
this.cumulativeCounts = cumulativeCounts;
this.monotonicLookUp = monotonicLookup;
this.suffixes = suffixes;
this.positions = positions;
this.sampledSuffixes = sampledSuffixes;
this.waveletFixedBlockBoosting = wavelet;
}
/**
* Converts a UTF-8 encoded byte pattern into a char pattern and enables to use any of the
* queries that employ the {@code char} datatype. Note that this implementation only supports
* UNICODE values up to the Java default for {@code Character.MAX_VALUE} (which is {@code
* u"\U0000FFFF"}). If more is required, then consider mapping your alphabet to a sequence of
* monotonic integers first (see function {@link FmIndex#mapToMonotonicSequence(char[])} to see
* an example).
*
* @param pattern The byte pattern to convert to char array
* @param offset Byte position from which to start converting
* @param length How many bytes to convert, counting from offset
* @param destination The char array to which the byte pattern will be written to
* @return The number of resulting chars
*/
protected static int convertBytePatternToCharPattern(
byte[] pattern, int offset, int length, char[] destination) {
int pos = offset;
int i = 0;
char nextChar;
while (pos < length + offset) {
byte firstByte = pattern[pos];
if (firstByte < 0b0000_0000) {
if (((firstByte & 0b1111_0000) >>> 3) == 30) {
// 4 byte char
byte secondByte = pattern[pos + 1];
byte thirdByte = pattern[pos + 2];
byte fourthByte = pattern[pos + 3];
pos += 4;
int beforeConversion =
((((firstByte & 0b0000_0111) << 18)
| ((secondByte & 0b0011_1111) << 12)
| ((thirdByte & 0b0011_1111) << 6)
| (fourthByte & 0b0011_1111))
& 0b111_111111_111111_111111);
if (beforeConversion > Short.MAX_VALUE) {
throw new RuntimeException(
"Found a character that exceeds ("
+ (int) Short.MAX_VALUE
+ "): it was "
+ beforeConversion);
}
nextChar = (char) beforeConversion;
} else if (((firstByte & 0b1110_0000) >>> 4) == 14) {
// 3 byte char
byte secondByte = pattern[pos + 1];
byte thirdByte = pattern[pos + 2];
pos += 3;
nextChar =
(char)
((((firstByte & 0b00001111) << 12)
| ((secondByte & 0b00111111) << 6)
| (thirdByte & 0b00111111))
& 0b1111_111111_111111);
} else {
// 2 byte char
byte secondByte = pattern[pos + 1];
pos += 2;
nextChar =
(char)
((((firstByte & 0b00011111) << 6) | (secondByte & 0b00111111))
& 0b11111111111);
}
} else {
// single byte char
++pos;
nextChar = (char) firstByte;
}
destination[i++] = nextChar;
}
return i;
}
private char[] addSentinelTerminatingCharacter(char[] input) {
char[] sentinel = new char[input.length + 1];
System.arraycopy(input, 0, sentinel, 0, input.length);
sentinel[input.length] = 0;
return sentinel;
}
private void fillCumulativeCounts(short[] mappedSequence) {
// example: if input is "bab\0" then the cumulative counts will be {0, 1, 2, 4, repeat 4
// until
// end of alphabet}
// which is the cumulative sum of alphabet counts, '\0' increases from 0 to 1, 'a' increases
// from
// 1 to 2, 'b' increases by two from 2 to 4 and so it stays
for (short value : mappedSequence) {
cumulativeCounts[value]++;
}
int offset = cumulativeCounts[0];
cumulativeCounts[0] = 0;
for (int i = 1; i < monotonicLookUp.length; i++) {
int previousSum = cumulativeCounts[i];
cumulativeCounts[i] = cumulativeCounts[i - 1] + offset;
offset = previousSum;
}
cumulativeCounts = Arrays.copyOfRange(cumulativeCounts, 0, monotonicLookUp.length + 1);
cumulativeCounts[monotonicLookUp.length] = this.length;
}
private int[] buildSuffixArrayAndSample(short[] mappedSequence) {
// Build the suffix array using Yuta Mori's div suf sort algorithm
// DivSufSort dss = new DivSufSort(monotonicLookUp.length); // this is the alphabet length
org.jsuffixarrays.DivSufSort dss =
new org.jsuffixarrays.DivSufSort(
monotonicLookUp.length); // this is the alphabet length
int[] mappedSequenceAsInts = new int[mappedSequence.length];
for (int i = 0; i < mappedSequence.length; i++) {
mappedSequenceAsInts[i] = mappedSequence[i];
}
// int[] suffixArray = dss.buildSuffixArray(mappedSequence, 0, mappedSequence.length);
int[] suffixArray =
dss.buildSuffixArray(mappedSequenceAsInts, 0, mappedSequenceAsInts.length);
// Sample positions
bitWidthSuffixes = minimumNumberOfBits(mappedSequence.length);
suffixes = new IntVector(mappedSequence.length / sampleRate + 1, bitWidthSuffixes);
BitVector whichPositionIsSampled =
LongArrayBitVector.getInstance().length(mappedSequence.length);
int samplingIndex = 0;
for (int i = 0; i < mappedSequence.length; i++) {
if (suffixArray[i] % sampleRate == 0) {
suffixes.setValue(samplingIndex, suffixArray[i]);
whichPositionIsSampled.set(i, true);
samplingIndex++;
} else {
whichPositionIsSampled.set(i, false);
}
}
this.sampledSuffixes = new RrrVector(whichPositionIsSampled, sampleRate);
// Sample also positions in case we need to extract / decompress
if (enableExtract) {
this.bitWidthPositions = bitWidthSuffixes;
positions = new IntVector(mappedSequence.length / sampleRate + 2, bitWidthPositions);
for (int i = 0; i < mappedSequence.length; i++) {
if (suffixArray[i] % sampleRate == 0) {
positions.setValue(suffixArray[i] / sampleRate, i);
}
}
positions.setValue(
(mappedSequence.length - 1) / sampleRate + 1,
positions.getValue(0, bitWidthPositions));
}
return suffixArray;
}
private short[] burrowsWheelerTransform(short[] mappedSequence, int[] suffixArray) {
// This basically takes all rotations, sorts them into lexical order and then
// takes the last column. E.g., for "baba$" the output would be "abba$":
// rotations sort last output
// baba$ $baba a abba$
// $baba a$bab b
// a$baba aba$b b
// ba$ba ba$ba a
// aba$b baba$ $
// note that the output "abba" contains the sequence of "b" together, making it
// easier to compress in the wavelet tree
short[] bwt = new short[mappedSequence.length];
for (int i = 0; i < mappedSequence.length; i++) {
if (suffixArray[i] == 0) {
bwt[i] = mappedSequence[mappedSequence.length - 1];
} else {
bwt[i] = mappedSequence[suffixArray[i] - 1];
}
}
return bwt;
}
private short[] mapToMonotonicSequence(char[] inputSentinel) {
Set<Character> alphabet = new HashSet<>();
int otherTerminating = 0;
for (char c : inputSentinel) {
alphabet.add(c);
if (c == '\0') {
++otherTerminating;
}
}
int mappedValue = 0;
if (otherTerminating != 1) {
mappedValue = 1;
}
monotonicLookUp = new int[alphabet.size() + 1];
// Add the sentinel value manually
monotonicMap.put((int) '\0', (short) mappedValue);
monotonicLookUp[mappedValue] = 0;
mappedValue++;
// And add the rest
for (char symbol : inputSentinel) {
if (null == monotonicMap.putIfAbsent((int) symbol, (short) mappedValue)) {
monotonicLookUp[mappedValue++] = symbol;
}
}
if (monotonicMap.size() > Short.MAX_VALUE) {
throw new IllegalArgumentException(
"Input has more than " + Short.MAX_VALUE + " different symbols");
}
// Do the actual mapping
short[] mappedSequence = new short[inputSentinel.length];
for (int i = 0; i < inputSentinel.length - 1; i++) {
mappedSequence[i] = monotonicMap.get((int) inputSentinel[i]);
}
mappedSequence[inputSentinel.length - 1] = 0;
return mappedSequence;
}
/**
* Counts the number of times a given pattern is found in the indexed input.
*
* @param pattern The substring to search in the index
* @return The number of overlapping matches
*/
public int count(char[] pattern) {
return count(pattern, 0, pattern.length);
}
/**
* Counts the number of times a given pattern is found in the indexed input.
*
* @param pattern The substring to search in the index
* @param offset The offset in the substring from which to start using the pattern
* @param length The number of chars from the offset used for the pattern
* @return The number of overlapping matches
*/
public int count(char[] pattern, int offset, int length) {
int i = (offset + length) - 1;
short c = monotonicMap.getOrDefault((int) pattern[i], (short) 0);
if (c == 0) {
return 0;
}
int start = cumulativeCounts[c];
int end = cumulativeCounts[c + 1];
while (start < end && i >= offset + 1) {
c = monotonicMap.getOrDefault((int) pattern[--i], (short) 0);
if (c == 0) {
return 0;
}
start = (int) (cumulativeCounts[c] + waveletFixedBlockBoosting.rank(start, c));
end = (int) (cumulativeCounts[c] + waveletFixedBlockBoosting.rank(end, c));
}
return Math.max(0, end - start);
}
/**
* Locates all occurrences of the given pattern and stores the positions in the array locations.
* Note that the user is responsible for allocating a large enough array, or to otherwise,
* indicate the maximum number of matches to find using the alternative function call {@link
* FmIndex#locate(char[], int, int, int[], int)}.
*
* @param pattern The char pattern to search in the index
* @param locations An array where the found locations will be written to
* @return The number of occurrences of the pattern, similar as with {@link
* FmIndex#count(char[], int, int)}
*/
public int locate(char[] pattern, int[] locations) {
return locate(pattern, 0, pattern.length, locations, -1);
}
/**
* Locates all occurrences of the given pattern and stores the positions in the array locations.
* Note that the user is responsible for allocating a large enough array, or to otherwise,
* indicate the maximum number of matches to find.
*
* @param pattern The char pattern to search in the index
* @param offset Where to start matching within the pattern
* @param length How many chars, from the offset, to use for the pattern
* @param locations An array where the found locations will be written to
* @param maxMatches The maximum number of occurrences. Once reached, the search will stop
* @return The number of occurrences of the pattern, similar as with {@link
* FmIndex#count(char[], int, int)}
*/
public int locate(char[] pattern, int offset, int length, int[] locations, int maxMatches) {
int i = (offset + length) - 1;
short c = monotonicMap.getOrDefault((int) pattern[i], (short) 0);
if (c == 0) {
return 0;
}
int start = cumulativeCounts[c];
int end = cumulativeCounts[c + 1];
int matchesPosition = 0;
// first determine range as in count
while (start < end && i >= (offset + 1)) {
c = monotonicMap.getOrDefault((int) pattern[--i], (short) 0);
if (c == 0) {
return 0;
}
start = (int) (cumulativeCounts[c] + waveletFixedBlockBoosting.rank(start, c));
end = (int) (cumulativeCounts[c] + waveletFixedBlockBoosting.rank(end, c));
}
// now extract locations with the sampled suffixes
if (start < end) {
i = start + 1;
while (i <= end) {
int j = i;
int distance = 0;
while (!(sampledSuffixes.access(j - 1))) {
long tuple = (waveletFixedBlockBoosting.inverseSelect(j - 1));
c = (short) tuple;
int rank = (int) waveletFixedBlockBoosting.rank(j, c);
j = cumulativeCounts[c] + rank;
++distance;
}
locations[matchesPosition] =
(int)
(suffixes.getValue(
(sampledSuffixes.rankOnes(j) - 1), bitWidthSuffixes)
+ distance);
++matchesPosition;
if (matchesPosition == maxMatches) {
break;
}
++i;
}
}
return matchesPosition;
}
/**
* Extracts the original string from the index in the range {@code [start, stop)} and stores it
* in the given array, starting from the given offset.
*
* @param start The inclusive starting position of the original slice of the string
* @param stop The exclusive ending position of the original slice of the string
* @param destination The array where to store the extracted slice
* @param offset The offset to shift the extracted string in the destination array
* @return The number of symbols extracted, which should be {@code stop - start}
*/
public int extract(int start, int stop, char[] destination, int offset) {
if (!enableExtract) {
throw new RuntimeException("Text recovery not enabled at build time");
}
if (start < 0) {
throw new RuntimeException("Requested position less than 0");
}
if (stop >= length) {
throw new RuntimeException("Stop position longer than index string");
}
// calculate position of backward search
int samplePosition =
(int) (positions.getValue((stop / sampleRate) + 1, bitWidthPositions) + 1);
// number of letters to skip
int skipUntilNextSampled = sampleRate - (stop) % sampleRate;
// special case: we are at the last position
if ((stop / sampleRate) == positions.getLength() - 2) {
skipUntilNextSampled = length - stop;
}
// backwards search
int range = stop - start;
if (destination.length - offset < range) {
throw new RuntimeException("Supplied destination is not large enough");
}
int remaining = range;
int distance = 0;
while (remaining > 0) {
short c = (short) waveletFixedBlockBoosting.inverseSelect(samplePosition - 1);
samplePosition =
(int) (cumulativeCounts[c] + waveletFixedBlockBoosting.rank(samplePosition, c));
if (distance >= skipUntilNextSampled) {
destination[remaining - 1 + offset] = (char) monotonicLookUp[c];
remaining--;
}
distance++;
}
return range;
}
private void checkBoundsForExtraction(int from, char[] destination) {
if (!enableExtract) {
throw new RuntimeException("Text recovery not enabled at build time");
}
if (from < 0) {
throw new RuntimeException("Requested position less than 0");
}
if (from >= length) {
throw new RuntimeException("Requested position longer than index string");
}
if (destination.length == 0) {
throw new IllegalArgumentException("Supplied destination for extraction has size zero");
}
}
/**
* Extracts the original string starting from the given position until the boundary characters
* are found, and then stores it in the given array, starting from the given offset. If the
* supplied destination is not large enough, an exception will be thrown.
*
* @param from The starting position of the original string from which to extract left and right
* @param destination The array where to store the extracted slice
* @param offset The offset to shift the extracted string in the destination array
* @param boundary The symbol at which to stop extracting, both left and right from the starting
* position
* @return The number of symbols extracted
*/
public int extractUntilBoundary(int from, char[] destination, int offset, char boundary) {
checkBoundsForExtraction(from, destination);
// calculate position of backward search
int samplePosition =
(int) (positions.getValue((from / sampleRate) + 1, bitWidthPositions) + 1);
// number of letters to skip at the start
int skipUntilNextSampled = sampleRate - (from) % sampleRate;
// special case: we are at the last position
if ((from / sampleRate) == positions.getLength() - 2) {
skipUntilNextSampled = length - from;
}
int downStreamPos = destination.length - 1;
// start the backwards search
short mappedBoundary = monotonicMap.getOrDefault((int) boundary, (short) 0);
if (mappedBoundary == 0) {
throw new IllegalArgumentException("Boundary does not exist");
}
int remaining = destination.length;
int distance = 0;
while (remaining > 0) {
short c = (short) waveletFixedBlockBoosting.inverseSelect(samplePosition - 1);
samplePosition =
(int) (cumulativeCounts[c] + waveletFixedBlockBoosting.rank(samplePosition, c));
if (distance >= skipUntilNextSampled) {
// this will exit if we find the left boundary
if (c == mappedBoundary) {
break;
}
// special break: we are the very beginning
if (c == 0) {
break;
}
destination[downStreamPos--] = (char) monotonicLookUp[c];
remaining--;
}
distance++;
}
// Shift the downstream to begin at 0 so that we can copy the upstream directly afterwards
int downStreamLength = destination.length - (downStreamPos + 1);
System.arraycopy(destination, downStreamPos + 1, destination, offset, downStreamLength);
// Do incremental (+4) searches
int step = 4;
int upStreamPos;
int finalPos = -1;
int timesUpStream = 1;
while (finalPos == -1) {
int prevFrom = from;
from += step;
from = Math.min(from, this.length - 1);
remaining = from - prevFrom;
upStreamPos = (timesUpStream - 1) * step + remaining - 1;
samplePosition =
(int) (positions.getValue((from / sampleRate) + 1, bitWidthPositions) + 1);
skipUntilNextSampled = sampleRate - (from) % sampleRate;
if ((from / sampleRate) == positions.getLength() - 2) {
skipUntilNextSampled = length - from;
}
distance = 0;
while (remaining > 0) {
short c = (short) waveletFixedBlockBoosting.inverseSelect(samplePosition - 1);
samplePosition =
(int)
(cumulativeCounts[c]
+ waveletFixedBlockBoosting.rank(samplePosition, c));
// check if we are at the snippet
if (distance >= skipUntilNextSampled) {
// this will exit if we find the left boundary
if (c == mappedBoundary) {
if (upStreamPos == 0) {
// this actually means the first char was a boundary
return 0;
}
finalPos = upStreamPos;
}
if (offset + downStreamLength + (upStreamPos) >= destination.length) {
throw new RuntimeException(
"Extraction does not fit in the supplied destination. "
+ "Currently extracted: "
+ (offset + downStreamLength + (upStreamPos)));
}
destination[offset + downStreamLength + (upStreamPos--)] =
(char) monotonicLookUp[c];
remaining--;
}
distance++;
}
// exit if we reached the end
if (from == this.length - 1) {
// If we found the EOF of the string in the first upStream segment, then when
// we reach here it will be -1. But if we reached here, in the worst case we
// only add a single char - thats where the 1 comes from. Otherwise, we add
// whatever upStreamPos was incremented to.
finalPos = (upStreamPos < 0) ? 1 : upStreamPos + from - prevFrom;
break;
}
// update offset
++timesUpStream;
}
return downStreamLength + finalPos;
}
/**
* Extracts the original string starting from the given position only to the left until the
* boundary character is found, and then stores it in the given array, starting from the given
* offset. If the supplied destination is not large enough, an exception will be thrown.
*
* @param from The starting position of the original string from which to extract left
* @param destination The array where to store the extracted slice
* @param offset The offset to shift the extracted string in the destination array
* @param boundary The symbol at which to stop extracting, left from the starting position
* @return The number of symbols extracted
*/
public int extractUntilBoundaryLeft(int from, char[] destination, int offset, char boundary) {
++from; // include the "from" character
checkBoundsForExtraction(from, destination);
// calculate position of backward search
int samplePosition =
(int) (positions.getValue((from / sampleRate) + 1, bitWidthPositions) + 1);
// number of letters to skip at the start
int skipUntilNextSampled = sampleRate - (from) % sampleRate;
// special case: we are at the last position
if ((from / sampleRate) == positions.getLength() - 2) {
skipUntilNextSampled = length - from;
}
int downStreamPos = destination.length - 1;
// start the backwards search
short mappedBoundary = monotonicMap.getOrDefault((int) boundary, (short) 0);
if (mappedBoundary == 0) {
throw new IllegalArgumentException("Boundary does not exist");
}
int distance = 0;
while (true) {
short c = (short) waveletFixedBlockBoosting.inverseSelect(samplePosition - 1);
samplePosition =
(int) (cumulativeCounts[c] + waveletFixedBlockBoosting.rank(samplePosition, c));
if (distance >= skipUntilNextSampled) {
// this will exit if we find the left boundary
if (c == mappedBoundary) {
break;
}
// special break: we are the very beginning
if (c == 0) {
break;
}
destination[downStreamPos--] = (char) monotonicLookUp[c];
if (downStreamPos == offset) {
throw new RuntimeException(
"Extraction does not fit in the supplied destination. "
+ "Currently extracted: "
+ (destination.length - offset));
}
}
distance++;
}
// Shift the downstream to begin at 0 so that we can copy the upstream directly afterwards
int downStreamLength = destination.length - (downStreamPos + 1);
System.arraycopy(destination, downStreamPos + 1, destination, offset, downStreamLength);
return downStreamLength;
}
/**
* Extracts the original string starting from the given position only to the right until the
* boundary character is found, and then stores it in the given array, starting from the given
* offset. If the supplied destination is not large enough, an exception will be thrown.
*
* @param from The starting position of the original string from which to extract right
* @param destination The array where to store the extracted slice
* @param offset The offset to shift the extracted string in the destination array
* @param boundary The symbol at which to stop extracting, right from the starting position
* @return The number of symbols extracted
*/
public int extractUntilBoundaryRight(int from, char[] destination, int offset, char boundary) {
checkBoundsForExtraction(from, destination);
short mappedBoundary = monotonicMap.getOrDefault((int) boundary, (short) 0);
if (mappedBoundary == 0) {
throw new IllegalArgumentException("Boundary does not exist");
}
// Do incremental (+4) searches
int step = 4;
int upStreamPos;
int finalPos = -1;
int timesUpStream = 1;
while (finalPos == -1) {
int prevFrom = from;
from += step;
from = Math.min(from, this.length - 1);
int remaining = from - prevFrom;
upStreamPos = (timesUpStream - 1) * step + remaining - 1;
int samplePosition =
(int) (positions.getValue((from / sampleRate) + 1, bitWidthPositions) + 1);
int skipUntilNextSampled = sampleRate - (from) % sampleRate;
if ((from / sampleRate) == positions.getLength() - 2) {
skipUntilNextSampled = length - from;
}
int distance = 0;
while (remaining > 0) {
short c = (short) waveletFixedBlockBoosting.inverseSelect(samplePosition - 1);
samplePosition =
(int)
(cumulativeCounts[c]
+ waveletFixedBlockBoosting.rank(samplePosition, c));
// check if we are at the snippet
if (distance >= skipUntilNextSampled) {
// this will exit if we find the left boundary
if (c == mappedBoundary) {
if (upStreamPos == 0) {
// this actually means the first char was a boundary
return 0;
}
finalPos = upStreamPos;
}
if (offset + (upStreamPos) >= destination.length) {
throw new RuntimeException(
"Extraction does not fit in the supplied destination. "
+ "Currently extracted: "
+ (offset + (upStreamPos)));
}
// This is because our range is (from, boundary]
if (upStreamPos > 0) {
destination[offset + (upStreamPos--) - 1] = (char) monotonicLookUp[c];
}
remaining--;
}
distance++;
}
// exit if we reached the end
if (from == this.length - 1) {
// If we found the EOF of the string in the first upStream segment, then when
// we reach here it will be -1. But if we reached here, in the worst case we
// only add a single char - thats where the 1 comes from. Otherwise, we add
// whatever upStreamPos was incremented to.
finalPos = upStreamPos + from - prevFrom;
break;
}
// update offset
++timesUpStream;
}
return finalPos - 1;
}
/**
* Returns the size of the original string indexed.
*
* @return The size of the original string indexed
*/
public int getInputLength() {
return this.length;
}
/**
* Gets sigma, the size of the alphabet of the original string, i.e., the number of different
* symbols.
*
* @return The size of the alphabet
*/
public int getAlphabetLength() {
return this.monotonicMap.size();
}
/**
* Serializes this object to an {@code ObjectOutput} stream.
*
* @param objectOutput The stream to which the object will be written
*/
public void write(ObjectOutput objectOutput) throws IOException {
objectOutput.writeByte(SERIAL_VERSION_V0);
objectOutput.writeInt(sampleRate);
objectOutput.writeBoolean(enableExtract);
objectOutput.writeInt(bitWidthSuffixes);
objectOutput.writeInt(bitWidthPositions);
objectOutput.writeInt(length);
objectOutput.writeInt(monotonicMap.keySet().size());
for (int v : monotonicMap.keySet()) {
objectOutput.writeInt(v);
objectOutput.writeShort(monotonicMap.get(v));
}
objectOutput.writeInt(cumulativeCounts.length);
for (int v : cumulativeCounts) {
objectOutput.writeInt(v);
}
objectOutput.writeInt(monotonicLookUp.length);
for (int v : monotonicLookUp) {
objectOutput.writeInt(v);
}
suffixes.write(objectOutput);
if (enableExtract) {
positions.write(objectOutput);
}
sampledSuffixes.write(objectOutput);
waveletFixedBlockBoosting.write(objectOutput);
}
/**
* Deserializes an {@code FM-index} from an {@code ObjectInput} stream.
*
* @param objectInput The stream from which to read from
* @return The deserialized instance of this object
*/
public static FmIndex read(ObjectInput objectInput) throws IOException {
checkSerialVersion(SERIAL_VERSION_V0, objectInput.readByte());
int sampleRate = objectInput.readInt();
boolean enableExtract = objectInput.readBoolean();
int bitWidthSuffixes = objectInput.readInt();
int bitWidthPositions = objectInput.readInt();
int length = objectInput.readInt();
int numKeys = objectInput.readInt();
Map<Integer, Short> monotonicMap = new HashMap<>();
for (int i = 0; i < numKeys; i++) {
int key = objectInput.readInt();
short value = objectInput.readShort();
monotonicMap.put(key, value);
}
int[] cumulativeCounts = new int[objectInput.readInt()];
for (int i = 0; i < cumulativeCounts.length; i++) {