summaryrefslogtreecommitdiff
path: root/extractor/suffix_array.h
blob: 79a22694d4113d313854e376e75e0ffc00306d52 (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
#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;

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;
};

#endif