1 #ifndef MARISA_GRIMOIRE_TRIE_RANGE_H_
2 #define MARISA_GRIMOIRE_TRIE_RANGE_H_
3
4 #include "marisa/base.h"
5
6 namespace marisa {
7 namespace grimoire {
8 namespace trie {
9
10 class Range {
11 public:
Range()12 Range() : begin_(0), end_(0), key_pos_(0) {}
13
set_begin(std::size_t begin)14 void set_begin(std::size_t begin) {
15 MARISA_DEBUG_IF(begin > MARISA_UINT32_MAX, MARISA_SIZE_ERROR);
16 begin_ = static_cast<UInt32>(begin);
17 }
set_end(std::size_t end)18 void set_end(std::size_t end) {
19 MARISA_DEBUG_IF(end > MARISA_UINT32_MAX, MARISA_SIZE_ERROR);
20 end_ = static_cast<UInt32>(end);
21 }
set_key_pos(std::size_t key_pos)22 void set_key_pos(std::size_t key_pos) {
23 MARISA_DEBUG_IF(key_pos > MARISA_UINT32_MAX, MARISA_SIZE_ERROR);
24 key_pos_ = static_cast<UInt32>(key_pos);
25 }
26
begin()27 std::size_t begin() const {
28 return begin_;
29 }
end()30 std::size_t end() const {
31 return end_;
32 }
key_pos()33 std::size_t key_pos() const {
34 return key_pos_;
35 }
36
37 private:
38 UInt32 begin_;
39 UInt32 end_;
40 UInt32 key_pos_;
41 };
42
make_range(std::size_t begin,std::size_t end,std::size_t key_pos)43 inline Range make_range(std::size_t begin, std::size_t end,
44 std::size_t key_pos) {
45 Range range;
46 range.set_begin(begin);
47 range.set_end(end);
48 range.set_key_pos(key_pos);
49 return range;
50 }
51
52 class WeightedRange {
53 public:
WeightedRange()54 WeightedRange() : range_(), weight_(0.0F) {}
55
set_range(const Range & range)56 void set_range(const Range &range) {
57 range_ = range;
58 }
set_begin(std::size_t begin)59 void set_begin(std::size_t begin) {
60 range_.set_begin(begin);
61 }
set_end(std::size_t end)62 void set_end(std::size_t end) {
63 range_.set_end(end);
64 }
set_key_pos(std::size_t key_pos)65 void set_key_pos(std::size_t key_pos) {
66 range_.set_key_pos(key_pos);
67 }
set_weight(float weight)68 void set_weight(float weight) {
69 weight_ = weight;
70 }
71
range()72 const Range &range() const {
73 return range_;
74 }
begin()75 std::size_t begin() const {
76 return range_.begin();
77 }
end()78 std::size_t end() const {
79 return range_.end();
80 }
key_pos()81 std::size_t key_pos() const {
82 return range_.key_pos();
83 }
weight()84 float weight() const {
85 return weight_;
86 }
87
88 private:
89 Range range_;
90 float weight_;
91 };
92
93 inline bool operator<(const WeightedRange &lhs, const WeightedRange &rhs) {
94 return lhs.weight() < rhs.weight();
95 }
96
97 inline bool operator>(const WeightedRange &lhs, const WeightedRange &rhs) {
98 return lhs.weight() > rhs.weight();
99 }
100
make_weighted_range(std::size_t begin,std::size_t end,std::size_t key_pos,float weight)101 inline WeightedRange make_weighted_range(std::size_t begin, std::size_t end,
102 std::size_t key_pos, float weight) {
103 WeightedRange range;
104 range.set_begin(begin);
105 range.set_end(end);
106 range.set_key_pos(key_pos);
107 range.set_weight(weight);
108 return range;
109 }
110
111 } // namespace trie
112 } // namespace grimoire
113 } // namespace marisa
114
115 #endif // MARISA_GRIMOIRE_TRIE_RANGE_H_
116