Added RLCSA index option
[SXSI/TextCollection.git] / incbwt / sasamples.h
1 #ifndef SASAMPLES_H
2 #define SASAMPLES_H
3
4 #include <fstream>
5
6 #include "misc/definitions.h"
7 #include "bits/bitbuffer.h"
8 #include "bits/deltavector.h"
9
10
11 namespace CSA
12 {
13
14
15 class SASamples
16 {
17   public:
18     const static usint INDEX_BLOCK_SIZE = 16;
19
20     SASamples(std::ifstream& sample_file, usint sample_rate);
21
22     // These assume < 2 GB data. Use the second one when there are multiple sequences.
23     SASamples(uint* array, usint data_size, usint sample_rate);
24     SASamples(uint* inverse, DeltaVector* end_points, usint data_size, usint sample_rate);
25
26     ~SASamples();
27
28     // Destroys contents of index and increment.
29     // We assume index and increment have same sample rate.
30     // positions must not containt the positions of end of sequence markers.
31     // number_of_sequences is subtracted from each position before the value is used.
32     SASamples(SASamples& index, SASamples& increment, usint* positions, usint number_of_positions, usint number_of_sequences);
33
34     void writeTo(std::ofstream& sample_file) const;
35
36     // Returns i such that SA[i] = value.
37     // If SA[i] is not sampled, returns the next sampled value. (Don't try!)
38     // Value is actual 0-based suffix array value.
39     // Returns size if value is too large.
40     inline usint inverseSA(usint value) const
41     {
42       if(value >= this->size) { return this->size; }
43       DeltaVector::Iterator iter(*(this->indexes));
44       return iter.select(this->inverse_samples->readItemConst(value / this->rate));
45     }
46
47     // Returns the value of ith sample in suffix array order.
48     inline usint getSample(usint i) const
49     {
50       return std::min(this->samples->readItemConst(i) * this->rate, this->size - 1);
51     }
52
53     // Returns (ind, sample number) where ind >= index or (size, ???).
54     inline pair_type getFirstSampleAfter(usint index) const
55     {
56       DeltaVector::Iterator iter(*(this->indexes));
57       return iter.valueAfter(index);
58     }
59
60     inline usint getSampleRate() const { return this->rate; }
61     inline usint getNumberOfSamples() const { return this->items; }
62
63     usint reportSize() const;
64
65   private:
66     usint integer_bits;
67     usint rate, size, items;
68
69     DeltaVector* indexes;
70
71     ReadBuffer* samples;
72     ReadBuffer* inverse_samples;
73
74     void buildInverseSamples();
75
76     // Note: contents of original samples are deleted.
77     void mergeSamples(SASamples& index, SASamples& increment, usint* positions, usint n, usint skip);
78
79     // These are not allowed.
80     SASamples();
81     SASamples(const SASamples&);
82     SASamples& operator = (const SASamples&);
83 };
84
85
86 } // namespace CSA
87
88
89 #endif // SASAMPLES_H