summaryrefslogtreecommitdiff
path: root/extractor/suffix_array.h
blob: 7a4f1110220835b8800e69e773db150260148364 (plain)
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
#ifndef _SUFFIX_ARRAY_H_
#define _SUFFIX_ARRAY_H_

#include <memory>
#include <string>
#include <vector>

#include <boost/filesystem.hpp>

namespace fs = boost::filesystem;
using namespace std;

namespace extractor {

class DataArray;
class PhraseLocation;

class SuffixArray {
 public:
  SuffixArray(shared_ptr<DataArray> data_array);

  virtual ~SuffixArray();

  virtual int GetSize() const;

  virtual shared_ptr<DataArray> GetData() const;

  virtual vector<int> BuildLCPArray() const;

  virtual int GetSuffix(int rank) const;

  virtual PhraseLocation Lookup(int low, int high, const string& word,
                                int offset) const;

  void WriteBinary(const fs::path& filepath) const;

 protected:
  SuffixArray();

 private:
  void BuildSuffixArray();

  void InitialBucketSort(vector<int>& groups);

  void TernaryQuicksort(int left, int right, int step, vector<int>& groups);

  void PrefixDoublingSort(vector<int>& groups);

  int LookupRangeStart(int low, int high, int word_id, int offset) const;

  shared_ptr<DataArray> data_array;
  vector<int> suffix_array;
  vector<int> word_start;
};

} // namespace extractor

#endif