fm-index 0.3.1

FM index and its variant implementations for Rust.
Documentation
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
// This module contains the API exposed to the frontend.
//
// It consists of concrete implementations of the search index and search
// traits; one for count-only and the other supporting locate queries.
//
// This wrapper code is very verbose, so written using macros to avoid repetition.
//
// It's written around the wrapper module which provides the implementation of
// the behavior. This module only exists so we can avoid exposing implementation
// traits.

use crate::character::Character;
use crate::error::Error;
use crate::fm_index::FMIndexBackend;
use crate::multi_pieces::FMIndexMultiPiecesBackend;
use crate::piece::PieceId;
use crate::rlfmi::RLFMIndexBackend;
use crate::suffix_array::discard::DiscardedSuffixArray;
use crate::suffix_array::sample::SOSampledSuffixArray;
use crate::text::Text;
use crate::wrapper::{MatchWrapper, SearchIndexWrapper, SearchWrapper};

/// Trait for searching in an index.
///
/// You can use this to search in an index generically.
pub trait SearchIndex<C> {
    /// Search for a pattern in the text.
    ///
    /// Return a [`Search`] object with information about the search
    /// result.
    fn search<K>(&self, pattern: K) -> impl Search<'_, C>
    where
        K: AsRef<[C]>;

    /// The size of the text in the index
    ///
    /// Note that this includes an ending \0 (terminator) character
    /// so will be one more than the length of the text.
    fn len(&self) -> usize;

    /// The size of the data used by this structure on the heap, in bytes.
    ///
    /// This does not include non-used pre-allocated space.
    fn heap_size(&self) -> usize;
}

/// Trait for searching in an index that supports multiple texts.
pub trait SearchIndexWithMultiPieces<C>: SearchIndex<C> {
    /// Search for a pattern that is a prefix of a text.
    fn search_prefix<K>(&self, pattern: K) -> impl Search<'_, C>
    where
        K: AsRef<[C]>;

    /// Search for a pattern that is a suffix of a text.
    fn search_suffix<K>(&self, pattern: K) -> impl Search<'_, C>
    where
        K: AsRef<[C]>;

    /// Search for a pattern that is an exact match of a text.
    fn search_exact<K>(&self, pattern: K) -> impl Search<'_, C>
    where
        K: AsRef<[C]>;
}

/// The result of a search.
///
/// A search result can be refined by adding more characters to the
/// search pattern.
/// A search result contains matches, which can be iterated over.
pub trait Search<'a, C> {
    /// Associated type for matches.
    type Match: Match<'a, C>;

    /// Search in the current search result, refining it.
    ///
    /// This adds a prefix `pattern` to the existing pattern, and
    /// looks for those expanded patterns in the text.
    fn search<K: AsRef<[C]>>(&self, pattern: K) -> Self;
    /// Count the number of occurrences.
    fn count(&self) -> usize;
    /// Get an iterator over all matches.
    fn iter_matches(&'a self) -> impl Iterator<Item = Self::Match> + 'a;
}

/// A match in the text.
pub trait Match<'a, C> {
    /// Iterate over the characters of the match.
    fn iter_chars_forward(&self) -> impl Iterator<Item = C> + 'a;

    /// Iterate over the characters of the match in reverse.
    fn iter_chars_backward(&self) -> impl Iterator<Item = C> + 'a;
}

/// A match in the text that contains its location on the text.
pub trait MatchWithLocate<'a, C>: Match<'a, C> {
    /// Get the location of the match in the text.
    fn locate(&self) -> usize;
}

/// A match in the text that contains the ID of the piece where the pattern is found.
pub trait MatchWithPieceId<'a, C>: Match<'a, C> {
    /// Get the ID of the text that the character at the matched position belongs to.
    fn piece_id(&self) -> PieceId;
}

/// FMIndex, count only.
///
/// The FM-Index is both a search index as well as compact representation of
/// the text.
pub struct FMIndex<C: Character>(SearchIndexWrapper<FMIndexBackend<C, DiscardedSuffixArray>>);
/// Search result for FMIndex, count only.
pub struct FMIndexSearch<'a, C: Character>(
    SearchWrapper<'a, FMIndexBackend<C, DiscardedSuffixArray>>,
);
/// Match in the text for FMIndex.
pub struct FMIndexMatch<'a, C: Character>(
    MatchWrapper<'a, FMIndexBackend<C, DiscardedSuffixArray>>,
);

/// FMIndex with locate support.
///
/// This is an FM-Index which uses additional storage to support locate queries.
pub struct FMIndexWithLocate<C: Character>(
    SearchIndexWrapper<FMIndexBackend<C, SOSampledSuffixArray>>,
);
/// Search result for FMIndex with locate support.
pub struct FMIndexSearchWithLocate<'a, C: Character>(
    SearchWrapper<'a, FMIndexBackend<C, SOSampledSuffixArray>>,
);
/// Match in the text for FMIndex with locate support.
pub struct FMIndexMatchWithLocate<'a, C: Character>(
    MatchWrapper<'a, FMIndexBackend<C, SOSampledSuffixArray>>,
);

/// RLFMIndex, count only.
///
/// This is a version of the FM-Index that uses less space, but is also less efficient.
pub struct RLFMIndex<C: Character>(SearchIndexWrapper<RLFMIndexBackend<C, DiscardedSuffixArray>>);
/// Search result for RLFMIndex, count only.
pub struct RLFMIndexSearch<'a, C: Character>(
    SearchWrapper<'a, RLFMIndexBackend<C, DiscardedSuffixArray>>,
);
/// Match in the text for RLFMIndex.
pub struct RLFMIndexMatch<'a, C: Character>(
    MatchWrapper<'a, RLFMIndexBackend<C, DiscardedSuffixArray>>,
);

/// RLFMIndex with locate support.
///
/// This is a version of the FM-Index that uses less space, but is also less efficient.
/// It uses additional storage to support locate queries.
pub struct RLFMIndexWithLocate<C: Character>(
    SearchIndexWrapper<RLFMIndexBackend<C, SOSampledSuffixArray>>,
);
/// Search result for RLFMIndex with locate support.
pub struct RLFMIndexSearchWithLocate<'a, C: Character>(
    SearchWrapper<'a, RLFMIndexBackend<C, SOSampledSuffixArray>>,
);
/// Match in the text for RLFMIndex with locate support.
pub struct RLFMIndexMatchWithLocate<'a, C: Character>(
    MatchWrapper<'a, RLFMIndexBackend<C, SOSampledSuffixArray>>,
);

/// MultiText index, count only.
///
/// This is a multi-text version of the FM-Index. It allows \0 separated strings.
pub struct FMIndexMultiPieces<C: Character>(
    SearchIndexWrapper<FMIndexMultiPiecesBackend<C, DiscardedSuffixArray>>,
);
/// Search result for MultiText index, count only.
pub struct FMIndexMultiPiecesSearch<'a, C: Character>(
    SearchWrapper<'a, FMIndexMultiPiecesBackend<C, DiscardedSuffixArray>>,
);
/// Match in the text for MultiText index.
pub struct FMIndexMultiPiecesMatch<'a, C: Character>(
    MatchWrapper<'a, FMIndexMultiPiecesBackend<C, DiscardedSuffixArray>>,
);

/// MultiText index with locate support.
///
/// This is a multi-text version of the FM-Index. It allows \0 separated strings.
/// It uses additional storage to support locate queries.
pub struct FMIndexMultiPiecesWithLocate<C: Character>(
    SearchIndexWrapper<FMIndexMultiPiecesBackend<C, SOSampledSuffixArray>>,
);
/// Search result for MultiText index with locate support.
pub struct FMIndexMultiPiecesSearchWithLocate<'a, C: Character>(
    SearchWrapper<'a, FMIndexMultiPiecesBackend<C, SOSampledSuffixArray>>,
);
/// Match in the text for MultiText index with locate support.
pub struct FMIndexMultiPiecesMatchWithLocate<'a, C: Character>(
    MatchWrapper<'a, FMIndexMultiPiecesBackend<C, SOSampledSuffixArray>>,
);

impl<C: Character> FMIndex<C> {
    /// Create a new FMIndex without locate support.
    pub fn new<T: AsRef<[C]>>(text: &Text<C, T>) -> Result<Self, Error> {
        Ok(FMIndex(SearchIndexWrapper::new(FMIndexBackend::new(
            text,
            |_| DiscardedSuffixArray {},
        )?)))
    }
}

impl<C: Character> FMIndexWithLocate<C> {
    /// Create a new FMIndex with locate support.
    ///
    /// The level argument controls the sampling rate used. Higher levels use
    /// less storage, at the cost of performance of locate queries. A level of
    /// 0 means no sampling, and a level of 1 means half of the suffix array is
    /// sampled, a level of 2 means a quarter of the suffix array is sampled,
    /// and so on.
    pub fn new<T: AsRef<[C]>>(text: &Text<C, T>, level: usize) -> Result<Self, Error> {
        Ok(FMIndexWithLocate(SearchIndexWrapper::new(
            FMIndexBackend::new(text, |sa| SOSampledSuffixArray::sample(sa, level))?,
        )))
    }
}

impl<C: Character> RLFMIndex<C> {
    /// Create a new RLFMIndex without locate support.
    pub fn new<T: AsRef<[C]>>(text: &Text<C, T>) -> Result<Self, Error> {
        Ok(RLFMIndex(SearchIndexWrapper::new(RLFMIndexBackend::new(
            text,
            |_| DiscardedSuffixArray {},
        )?)))
    }
}

impl<C: Character> RLFMIndexWithLocate<C> {
    /// Create a new RLFMIndex with locate support.
    ///
    /// The level argument controls the sampling rate used. Higher levels use
    /// less storage, at the cost of performance of locate queries. A level of
    /// 0 means no sampling, and a level of 1 means half of the suffix array is
    /// sampled, a level of 2 means a quarter of the suffix array is sampled,
    /// and so on.
    pub fn new<T: AsRef<[C]>>(text: &Text<C, T>, level: usize) -> Result<Self, Error> {
        Ok(RLFMIndexWithLocate(SearchIndexWrapper::new(
            RLFMIndexBackend::new(text, |sa| SOSampledSuffixArray::sample(sa, level))?,
        )))
    }
}

impl<C: Character> FMIndexMultiPieces<C> {
    /// Create a new FMIndexMultiPieces without locate support.
    pub fn new<T: AsRef<[C]>>(text: &Text<C, T>) -> Result<Self, Error> {
        Ok(FMIndexMultiPieces(SearchIndexWrapper::new(
            FMIndexMultiPiecesBackend::new(text, |_| DiscardedSuffixArray {})?,
        )))
    }
}

impl<C: Character> FMIndexMultiPiecesWithLocate<C> {
    /// Create a new FMIndexMultiPieces with locate support.
    ///
    /// The level argument controls the sampling rate used. Higher levels use
    /// less storage, at the cost of performance of locate queries. A level of
    /// 0 means no sampling, and a level of 1 means half of the suffix array is
    /// sampled, a level of 2 means a quarter of the suffix array is sampled,
    /// and so on.
    pub fn new<T: AsRef<[C]>>(text: &Text<C, T>, level: usize) -> Result<Self, Error> {
        Ok(FMIndexMultiPiecesWithLocate(SearchIndexWrapper::new(
            FMIndexMultiPiecesBackend::new(text, |sa| SOSampledSuffixArray::sample(sa, level))?,
        )))
    }
}

macro_rules! impl_search_index {
    ($t:ty, $s:ident, $st:ty) => {
        impl<C: Character> SearchIndex<C> for $t {
            fn search<K>(&self, pattern: K) -> impl Search<'_, C>
            where
                K: AsRef<[C]>,
            {
                $s(self.0.search(pattern))
            }

            fn len(&self) -> usize {
                self.0.len()
            }

            fn heap_size(&self) -> usize {
                self.0.heap_size()
            }
        }

        // inherent
        impl<C: Character> $t {
            /// Search for a pattern in the text.
            pub fn search<K>(&self, pattern: K) -> $st
            where
                K: AsRef<[C]>,
            {
                $s(self.0.search(pattern))
            }
            /// The size of the text in the index
            pub fn len(&self) -> usize {
                SearchIndex::len(self)
            }
        }
    };
}

macro_rules! impl_search_index_with_locate {
    ($t:ty, $s:ident, $st:ty) => {
        impl<C: Character> SearchIndex<C> for $t {
            fn search<K>(&self, pattern: K) -> impl Search<'_, C>
            where
                K: AsRef<[C]>,
            {
                $s(self.0.search(pattern))
            }

            fn len(&self) -> usize {
                self.0.len()
            }

            fn heap_size(&self) -> usize {
                self.0.heap_size()
            }
        }

        // inherent
        impl<C: Character> $t {
            /// Search for a pattern in the text.
            pub fn search<K>(&self, pattern: K) -> $st
            where
                K: AsRef<[C]>,
            {
                $s(self.0.search(pattern))
            }
            /// The size of the text in the index
            pub fn len(&self) -> usize {
                SearchIndex::len(self)
            }
        }
    };
}

macro_rules! impl_search_index_with_multi_pieces {
    ($t:ty, $s:ident, $st:ty) => {
        impl<C: Character> SearchIndexWithMultiPieces<C> for $t {
            fn search_prefix<K>(&self, pattern: K) -> impl Search<'_, C>
            where
                K: AsRef<[C]>,
            {
                $s(self.0.search_prefix(pattern))
            }

            fn search_suffix<K>(&self, pattern: K) -> impl Search<'_, C>
            where
                K: AsRef<[C]>,
            {
                $s(self.0.search_suffix(pattern))
            }

            fn search_exact<K>(&self, pattern: K) -> impl Search<'_, C>
            where
                K: AsRef<[C]>,
            {
                $s(self.0.search_exact(pattern))
            }
        }

        // inherent
        impl<C: Character> $t {
            /// Search for a pattern that is a prefix of a text.
            pub fn search_prefix<K>(&self, pattern: K) -> $st
            where
                K: AsRef<[C]>,
            {
                $s(self.0.search_prefix(pattern))
            }

            /// Search for a pattern that is a suffix of a text.
            pub fn search_suffix<K>(&self, pattern: K) -> $st
            where
                K: AsRef<[C]>,
            {
                $s(self.0.search_suffix(pattern))
            }

            /// Search for a pattern that is an exact match of a text.
            pub fn search_exact<K>(&self, pattern: K) -> $st
            where
                K: AsRef<[C]>,
            {
                $s(self.0.search_exact(pattern))
            }
        }
    };
}

macro_rules! impl_search {
    ($t:ty, $m:ident, $mt:ty) => {
        impl<'a, C: Character> Search<'a, C> for $t {
            type Match = $mt;

            fn search<K>(&self, pattern: K) -> Self
            where
                K: AsRef<[C]>,
            {
                Self(self.0.search(pattern))
            }

            fn count(&self) -> usize {
                self.0.count()
            }

            fn iter_matches(&'a self) -> impl Iterator<Item = Self::Match> + 'a {
                self.0.iter_matches().map(|m| $m(m))
            }
        }
        // inherent
        impl<'a, C: Character> $t {
            /// Search in the current search result, refining it.
            ///
            /// This adds a prefix `pattern` to the existing pattern, and
            /// looks for those expanded patterns in the text.
            pub fn search<K>(&self, pattern: K) -> Self
            where
                K: AsRef<[C]>,
            {
                Search::search(self, pattern)
            }

            /// Count the number of occurrences.
            pub fn count(&self) -> usize {
                Search::count(self)
            }
        }
    };
}

macro_rules! impl_match {
    ($t:ty) => {
        impl<'a, C: Character> Match<'a, C> for $t {
            fn iter_chars_forward(&self) -> impl Iterator<Item = C> + 'a {
                self.0.iter_chars_forward()
            }

            fn iter_chars_backward(&self) -> impl Iterator<Item = C> + 'a {
                self.0.iter_chars_backward()
            }
        }
    };
}

macro_rules! impl_match_locate {
    ($t:ty) => {
        impl<'a, C: Character> MatchWithLocate<'a, C> for $t {
            fn locate(&self) -> usize {
                self.0.locate()
            }
        }
    };
}

macro_rules! impl_match_piece_id {
    ($t:ty) => {
        impl<'a, C: Character> MatchWithPieceId<'a, C> for $t {
            fn piece_id(&self) -> PieceId {
                self.0.piece_id()
            }
        }
    };
}

impl_search_index!(FMIndex<C>, FMIndexSearch, FMIndexSearch<'_, C>);
impl_search!(FMIndexSearch<'a, C>, FMIndexMatch, FMIndexMatch<'a, C>);
impl_match!(FMIndexMatch<'a, C>);

impl_search_index_with_locate!(
    FMIndexWithLocate<C>,
    FMIndexSearchWithLocate,
    FMIndexSearchWithLocate<'_, C>
);
impl_search!(
    FMIndexSearchWithLocate<'a, C>,
    FMIndexMatchWithLocate,
    FMIndexMatchWithLocate<'a, C>
);
impl_match!(FMIndexMatchWithLocate<'a, C>);
impl_match_locate!(FMIndexMatchWithLocate<'a, C>);

impl_search_index!(RLFMIndex<C>, RLFMIndexSearch, RLFMIndexSearch<'_, C>);
impl_search!(
    RLFMIndexSearch<'a, C>,
    RLFMIndexMatch,
    RLFMIndexMatch<'a, C>
);
impl_match!(RLFMIndexMatch<'a, C>);

impl_search_index_with_locate!(
    RLFMIndexWithLocate<C>,
    RLFMIndexSearchWithLocate,
    RLFMIndexSearchWithLocate<'_, C>
);
impl_search!(
    RLFMIndexSearchWithLocate<'a, C>,
    RLFMIndexMatchWithLocate,
    RLFMIndexMatchWithLocate<'a, C>
);
impl_match!(RLFMIndexMatchWithLocate<'a, C>);
impl_match_locate!(RLFMIndexMatchWithLocate<'a, C>);

impl_search_index!(
    FMIndexMultiPieces<C>,
    FMIndexMultiPiecesSearch,
    FMIndexMultiPiecesSearch<'_, C>
);
impl_search_index_with_multi_pieces!(
    FMIndexMultiPieces<C>,
    FMIndexMultiPiecesSearch,
    FMIndexMultiPiecesSearch<'_, C>
);
impl_search!(
    FMIndexMultiPiecesSearch<'a, C>,
    FMIndexMultiPiecesMatch,
    FMIndexMultiPiecesMatch<'a, C>
);
impl_match!(FMIndexMultiPiecesMatch<'a, C>);

impl_search_index_with_locate!(
    FMIndexMultiPiecesWithLocate<C>,
    FMIndexMultiPiecesSearchWithLocate,
    FMIndexMultiPiecesSearchWithLocate<'_, C>
);
impl_search_index_with_multi_pieces!(
    FMIndexMultiPiecesWithLocate<C>,
    FMIndexMultiPiecesSearchWithLocate,
    FMIndexMultiPiecesSearchWithLocate<'_, C>
);
impl_search!(
    FMIndexMultiPiecesSearchWithLocate<'a, C>,
    FMIndexMultiPiecesMatchWithLocate,
    FMIndexMultiPiecesMatchWithLocate<'a, C>
);
impl_match!(FMIndexMultiPiecesMatchWithLocate<'a, C>);
impl_match_locate!(FMIndexMultiPiecesMatchWithLocate<'a, C>);
impl_match_piece_id!(FMIndexMultiPiecesMatchWithLocate<'a, C>);