fm-index
This crate provides implementations of FM-Index and its variants.
FM-Index, originally proposed by Paolo Ferragina and Giovanni Manzini [^1], is a compressed full-text index which supports the following queries:
count: Given a pattern string, counts the number of its occurrences.locate: Given a pattern string, lists the all positions of its occurrences.extract: Given an integer, gets the character of the text at that position.
The fm-index crate does not support the third query (extracting a
character from arbitrary position). Instead, it provides backward/forward
iterators that return the text characters starting from a search result.
Usage
Add this to your Cargo.toml.
[]
= "0.3.1"
Example
use ;
// Prepare a text string to search for patterns.
let text = concat!.as_bytes;
let text = new;
// The sampling level determines how much is retained in order to support `locate`
// queries. `0` retains the full information, but we don't need the whole array
// since we can interpolate missing elements in a suffix array from others. A sampler
// will _sieve_ a suffix array for this purpose. If you don't need `locate` queries
// you can save the memory by not setting a sampling level.
let index = new.unwrap;
// Search for a pattern string.
let pattern = "dolor";
let search = index.search;
// Count the number of occurrences.
let n = search.count;
assert_eq!;
// List the position of all occurrences.
let positions = search
.iter_matches
.map
.;
assert_eq!;
// Extract preceding characters from a search position.
let mut prefix = search
.iter_matches
.next
.unwrap
.iter_chars_backward
.take
.;
prefix.reverse;
assert_eq!;
// Extract succeeding characters from a search position.
let postfix = search
.iter_matches
.nth
.unwrap
.iter_chars_forward
.take
.;
assert_eq!;
See examples/ directory for more examples.
Implementations
See the crate document for details about the implementation.