fm_index/
text.rs

1use crate::util;
2use crate::Character;
3use num_traits::Bounded;
4
5/// A text structure used as the target for pattern searching in the index.
6///
7/// Not only does it contain the text, but also the maximum character value in the
8/// text. This is used to determine the number of bits needed to store the
9/// characters in the text.
10pub struct Text<C, T>
11where
12    C: Character,
13    T: AsRef<[C]>,
14{
15    text: T,
16    max_character: C,
17}
18
19impl<C, T> Text<C, T>
20where
21    C: Character + Bounded,
22    T: AsRef<[C]>,
23{
24    /// Create a new text structure with the given text.
25    ///
26    /// The maximum character value is set to the maximum value of the
27    /// character type.
28    pub fn new(text: T) -> Self {
29        Text {
30            text,
31            max_character: C::max_value(),
32        }
33    }
34}
35
36impl<C, T> Text<C, T>
37where
38    C: Character,
39    T: AsRef<[C]>,
40{
41    /// Create a new text structure with the given text and the largest character value.
42    ///
43    /// This is used when the maximum character value is known in advance.
44    pub fn with_max_character(text: T, max_character: C) -> Self {
45        Text {
46            text,
47            max_character,
48        }
49    }
50
51    /// Get the text as a slice.
52    pub fn text(&self) -> &[C] {
53        self.text.as_ref()
54    }
55
56    /// Return the maximum character value in the text.
57    pub fn max_character(&self) -> C {
58        self.max_character
59    }
60
61    pub(crate) fn max_bits(&self) -> usize {
62        util::log2_usize(self.max_character().into_usize()) + 1
63    }
64}