Debian Codegraph @

lib/dat/predictive-cursor.cpp

raw symbols SCIP
1 /*
2 Copyright (C) 2011 Brazil
3 Copyright (C) 2022 Sutou Kouhei <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "predictive-cursor.hpp"
21
22 #include <algorithm>
23 #include <cstring>
24
25 #include "trie.hpp"
26
27 namespace grn {
28 namespace dat {
29
30 PredictiveCursor::PredictiveCursor()
31 : trie_(NULL),
32 offset_(0),
33 limit_(MAX_UINT32),
34 flags_(PREDICTIVE_CURSOR),
35 buf_(),
36 cur_(0),
37 end_(0),
38 min_length_(0) {}
39
40 PredictiveCursor::~PredictiveCursor() {}
41
42 void PredictiveCursor::open(const Trie &trie,
43 const String &str,
44 UInt32 offset,
45 UInt32 limit,
46 UInt32 flags) {
47 GRN_DAT_THROW_IF(PARAM_ERROR, (str.ptr() == NULL) && (str.length() != 0));
48
49 flags = fix_flags(flags);
50 PredictiveCursor new_cursor(trie, offset, limit, flags);
51 new_cursor.init(str);
52 new_cursor.swap(this);
53 }
54
55 void PredictiveCursor::close() {
56 PredictiveCursor new_cursor;
57 new_cursor.swap(this);
58 }
59
60 const Key &PredictiveCursor::next() {
61 if (cur_ == end_) {
62 return Key::invalid_key();
63 }
64
65 if ((flags_ & ASCENDING_CURSOR) == ASCENDING_CURSOR) {
66 return ascending_next();
67 } else {
68 return descending_next();
69 }
70 }
71
72 PredictiveCursor::PredictiveCursor(const Trie &trie,
73 UInt32 offset, UInt32 limit, UInt32 flags)
74 : trie_(&trie),
75 offset_(offset),
76 limit_(limit),
77 flags_(flags),
78 buf_(),
79 cur_(0),
80 end_(0),
81 min_length_(0) {}
82
83 UInt32 PredictiveCursor::fix_flags(UInt32 flags) const {
84 const UInt32 cursor_type = flags & CURSOR_TYPE_MASK;
85 GRN_DAT_THROW_IF(PARAM_ERROR, (cursor_type != 0) &&
86 (cursor_type != PREDICTIVE_CURSOR));
87 flags |= PREDICTIVE_CURSOR;
88
89 const UInt32 cursor_order = flags & CURSOR_ORDER_MASK;
90 GRN_DAT_THROW_IF(PARAM_ERROR, (cursor_order != 0) &&
91 (cursor_order != ASCENDING_CURSOR) &&
92 (cursor_order != DESCENDING_CURSOR));
93 if (cursor_order == 0) {
94 flags |= ASCENDING_CURSOR;
95 }
96
97 const UInt32 cursor_options = flags & CURSOR_OPTIONS_MASK;
98 GRN_DAT_THROW_IF(PARAM_ERROR, cursor_options & ~(EXCEPT_EXACT_MATCH));
99
100 return flags;
101 }
102
103 void PredictiveCursor::init(const String &str) {
104 if (limit_ == 0) {
105 return;
106 }
107
108 min_length_ = str.length();
109 if ((flags_ & EXCEPT_EXACT_MATCH) == EXCEPT_EXACT_MATCH) {
110 ++min_length_;
111 }
112 end_ = (offset_ > (MAX_UINT32 - limit_)) ? MAX_UINT32 : (offset_ + limit_);
113
114 UInt32 node_id = ROOT_NODE_ID;
115 for (UInt32 i = 0; i < str.length(); ++i) {
116 const Base base = trie_->ith_node(node_id).base();
117 if (base.is_linker()) {
118 if (offset_ == 0) {
119 const Key &key = trie_->get_key(base.key_pos());
120 if ((key.length() >= str.length()) &&
121 (key.str().substr(0, str.length()).compare(str, i) == 0)) {
122 if ((flags_ & ASCENDING_CURSOR) == ASCENDING_CURSOR) {
123 node_id |= IS_ROOT_FLAG;
124 }
125 buf_.push_back(node_id);
126 }
127 }
128 return;
129 }
130
131 node_id = base.offset() ^ str[i];
132 if (trie_->ith_node(node_id).label() != str[i]) {
133 return;
134 }
135 }
136
137 if ((flags_ & ASCENDING_CURSOR) == ASCENDING_CURSOR) {
138 node_id |= IS_ROOT_FLAG;
139 }
140 buf_.push_back(node_id);
141 }
142
143 void PredictiveCursor::swap(PredictiveCursor *cursor) {
144 std::swap(trie_, cursor->trie_);
145 std::swap(offset_, cursor->offset_);
146 std::swap(limit_, cursor->limit_);
147 std::swap(flags_, cursor->flags_);
148 buf_.swap(&cursor->buf_);
149 std::swap(cur_, cursor->cur_);
150 std::swap(end_, cursor->end_);
151 std::swap(min_length_, cursor->min_length_);
152 }
153
154 const Key &PredictiveCursor::ascending_next() {
155 while (!buf_.empty()) {
156 const bool is_root = (buf_.back() & IS_ROOT_FLAG) == IS_ROOT_FLAG;
157 const UInt32 node_id = buf_.back() & ~IS_ROOT_FLAG;
158 buf_.pop_back();
159
160 const Node node = trie_->ith_node(node_id);
161 if (!is_root && (node.sibling() != INVALID_LABEL)) {
162 buf_.push_back(node_id ^ node.label() ^ node.sibling());
163 }
164
165 if (node.is_linker()) {
166 const Key &key = trie_->get_key(node.key_pos());
167 if (key.length() >= min_length_) {
168 if (cur_++ >= offset_) {
169 return key;
170 }
171 }
172 } else if (node.child() != INVALID_LABEL) {
173 buf_.push_back(node.offset() ^ node.child());
174 }
175 }
176 return Key::invalid_key();
177 }
178
179 const Key &PredictiveCursor::descending_next() {
180 while (!buf_.empty()) {
181 const bool post_order = (buf_.back() & POST_ORDER_FLAG) == POST_ORDER_FLAG;
182 const UInt32 node_id = buf_.back() & ~POST_ORDER_FLAG;
183
184 const Base base = trie_->ith_node(node_id).base();
185 if (post_order) {
186 buf_.pop_back();
187 if (base.is_linker()) {
188 const Key &key = trie_->get_key(base.key_pos());
189 if (key.length() >= min_length_) {
190 if (cur_++ >= offset_) {
191 return key;
192 }
193 }
194 }
195 } else {
196 buf_.back() |= POST_ORDER_FLAG;
197 UInt32 label = trie_->ith_node(node_id).child();
198 while (label != INVALID_LABEL) {
199 buf_.push_back(base.offset() ^ label);
200 label = trie_->ith_node(base.offset() ^ label).sibling();
201 }
202 }
203 }
204 return Key::invalid_key();
205 }
206
207 } // namespace dat
208 } // namespace grn