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"cxx . . $ `<file>/lib/dat/predictive-cursor.hpp`/
21
22 #include <algorithm>
23 #include <cstring>
24
25 #include "trie.hpp"cxx . . $ `<file>/lib/dat/trie.hpp`/
26
27 namespace grncxx . . $ grn/ {
28 namespace datcxx . . $ grn/dat/ {
29
30 PredictiveCursorcxx . . $ grn/dat/PredictiveCursor#::PredictiveCursorcxx . . $ grn/dat/PredictiveCursor#PredictiveCursor(49f6e7a06ebc5aa8).()
31 : trie_cxx . . $ grn/dat/PredictiveCursor#trie_.(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:416:9`!),
32 offset_cxx . . $ grn/dat/PredictiveCursor#offset_.(0),
33 limit_cxx . . $ grn/dat/PredictiveCursor#limit_.(MAX_UINT32cxx . . $ grn/dat/MAX_UINT32.),
34 flags_cxx . . $ grn/dat/PredictiveCursor#flags_.(PREDICTIVE_CURSORcxx . . $ grn/dat/PREDICTIVE_CURSOR.),
35 buf_cxx . . $ grn/dat/PredictiveCursor#buf_.(),
36 cur_cxx . . $ grn/dat/PredictiveCursor#cur_.(0),
37 end_cxx . . $ grn/dat/PredictiveCursor#end_.(0),
38 min_length_cxx . . $ grn/dat/PredictiveCursor#min_length_.(0) {}
39
40 PredictiveCursorcxx . . $ grn/dat/PredictiveCursor#::~cxx . . $ grn/dat/PredictiveCursor#`~PredictiveCursor`(ced63f7c635d850d).PredictiveCursorcxx . . $ grn/dat/PredictiveCursor#() {}
41
42 void PredictiveCursorcxx . . $ grn/dat/PredictiveCursor#::opencxx . . $ grn/dat/PredictiveCursor#open(51969a13d721bec0).(const Triecxx . . $ grn/dat/Trie# &trie,
43 const Stringcxx . . $ grn/dat/String# &str,
44 UInt32cxx . . $ grn/dat/UInt32# offset,
45 UInt32cxx . . $ grn/dat/UInt32# limit,
46 UInt32cxx . . $ grn/dat/UInt32# flags) {
47 GRN_DAT_THROW_IFcxx . . $ `lib/dat/dat.hpp:232:9`!(PARAM_ERROR, (str.ptr() == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:416:9`!) && (str.length() != 0));
48
49 flags = fix_flagscxx . . $ grn/dat/PredictiveCursor#fix_flags(560347b7e9428769).(flags);
50 PredictiveCursorcxx . . $ grn/dat/PredictiveCursor# new_cursorcxx . . $ grn/dat/PredictiveCursor#PredictiveCursor(86ae9facf0bf692d).(trie, offset, limit, flags);
51 new_cursor.initcxx . . $ grn/dat/PredictiveCursor#init(134f24f25bda342e).(str);
52 new_cursor.swapcxx . . $ grn/dat/PredictiveCursor#swap(5346b7e23c5692d1).(this);
53 }
54
55 void PredictiveCursorcxx . . $ grn/dat/PredictiveCursor#::closecxx . . $ grn/dat/PredictiveCursor#close(49f6e7a06ebc5aa8).() {
56 PredictiveCursorcxx . . $ grn/dat/PredictiveCursor# new_cursorcxx . . $ grn/dat/PredictiveCursor#PredictiveCursor(49f6e7a06ebc5aa8).;
57 new_cursor.swapcxx . . $ grn/dat/PredictiveCursor#swap(5346b7e23c5692d1).(this);
58 }
59
60 const Keycxx . . $ grn/dat/Key# &PredictiveCursorcxx . . $ grn/dat/PredictiveCursor#::nextcxx . . $ grn/dat/PredictiveCursor#next(741ea5814372790).() {
61 if (cur_cxx . . $ grn/dat/PredictiveCursor#cur_. == end_cxx . . $ grn/dat/PredictiveCursor#end_.) {
62 return Keycxx . . $ grn/dat/Key#::invalid_keycxx . . $ grn/dat/Key#invalid_key(741ea5814372790).();
63 }
64
65 if ((flags_cxx . . $ grn/dat/PredictiveCursor#flags_. & ASCENDING_CURSORcxx . . $ grn/dat/ASCENDING_CURSOR.) == ASCENDING_CURSORcxx . . $ grn/dat/ASCENDING_CURSOR.) {
66 return ascending_nextcxx . . $ grn/dat/PredictiveCursor#ascending_next(741ea5814372790).();
67 } else {
68 return descending_nextcxx . . $ grn/dat/PredictiveCursor#descending_next(741ea5814372790).();
69 }
70 }
71
72 PredictiveCursorcxx . . $ grn/dat/PredictiveCursor#::PredictiveCursorcxx . . $ grn/dat/PredictiveCursor#PredictiveCursor(86ae9facf0bf692d).(const Triecxx . . $ grn/dat/Trie# &trie,
73 UInt32cxx . . $ grn/dat/UInt32# offset, UInt32cxx . . $ grn/dat/UInt32# limit, UInt32cxx . . $ grn/dat/UInt32# flags)
74 : trie_cxx . . $ grn/dat/PredictiveCursor#trie_.(&trie),
75 offset_cxx . . $ grn/dat/PredictiveCursor#offset_.(offset),
76 limit_cxx . . $ grn/dat/PredictiveCursor#limit_.(limit),
77 flags_cxx . . $ grn/dat/PredictiveCursor#flags_.(flags),
78 buf_cxx . . $ grn/dat/PredictiveCursor#buf_.(),
79 cur_cxx . . $ grn/dat/PredictiveCursor#cur_.(0),
80 end_cxx . . $ grn/dat/PredictiveCursor#end_.(0),
81 min_length_cxx . . $ grn/dat/PredictiveCursor#min_length_.(0) {}
82
83 UInt32cxx . . $ grn/dat/UInt32# PredictiveCursorcxx . . $ grn/dat/PredictiveCursor#::fix_flagscxx . . $ grn/dat/PredictiveCursor#fix_flags(560347b7e9428769).(UInt32cxx . . $ grn/dat/UInt32# flags) const {
84 const UInt32cxx . . $ grn/dat/UInt32# cursor_type = flags & CURSOR_TYPE_MASKcxx . . $ grn/dat/CURSOR_TYPE_MASK.;
85 GRN_DAT_THROW_IFcxx . . $ `lib/dat/dat.hpp:232:9`!(PARAM_ERROR, (cursor_type != 0) &&
86 (cursor_type != PREDICTIVE_CURSOR));
87 flags |= PREDICTIVE_CURSORcxx . . $ grn/dat/PREDICTIVE_CURSOR.;
88
89 const UInt32cxx . . $ grn/dat/UInt32# cursor_order = flags & CURSOR_ORDER_MASKcxx . . $ grn/dat/CURSOR_ORDER_MASK.;
90 GRN_DAT_THROW_IFcxx . . $ `lib/dat/dat.hpp:232:9`!(PARAM_ERROR, (cursor_order != 0) &&
91 (cursor_order != ASCENDING_CURSOR) &&
92 (cursor_order != DESCENDING_CURSOR));
93 if (cursor_order == 0) {
94 flags |= ASCENDING_CURSORcxx . . $ grn/dat/ASCENDING_CURSOR.;
95 }
96
97 const UInt32cxx . . $ grn/dat/UInt32# cursor_options = flags & CURSOR_OPTIONS_MASKcxx . . $ grn/dat/CURSOR_OPTIONS_MASK.;
98 GRN_DAT_THROW_IFcxx . . $ `lib/dat/dat.hpp:232:9`!(PARAM_ERROR, cursor_options & ~(EXCEPT_EXACT_MATCH));
99
100 return flags;
101 }
102
103 void PredictiveCursorcxx . . $ grn/dat/PredictiveCursor#::initcxx . . $ grn/dat/PredictiveCursor#init(134f24f25bda342e).(const Stringcxx . . $ grn/dat/String# &str) {
104 if (limit_cxx . . $ grn/dat/PredictiveCursor#limit_. == 0) {
105 return;
106 }
107
108 min_length_cxx . . $ grn/dat/PredictiveCursor#min_length_. = str.lengthcxx . . $ grn/dat/String#length(ddd8539a2fcdd4ce).();
109 if ((flags_cxx . . $ grn/dat/PredictiveCursor#flags_. & EXCEPT_EXACT_MATCHcxx . . $ grn/dat/EXCEPT_EXACT_MATCH.) == EXCEPT_EXACT_MATCHcxx . . $ grn/dat/EXCEPT_EXACT_MATCH.) {
110 ++min_length_cxx . . $ grn/dat/PredictiveCursor#min_length_.;
111 }
112 end_cxx . . $ grn/dat/PredictiveCursor#end_. = (offset_cxx . . $ grn/dat/PredictiveCursor#offset_. > (MAX_UINT32cxx . . $ grn/dat/MAX_UINT32. - limit_cxx . . $ grn/dat/PredictiveCursor#limit_.)) ? MAX_UINT32cxx . . $ grn/dat/MAX_UINT32. : (offset_cxx . . $ grn/dat/PredictiveCursor#offset_. + limit_cxx . . $ grn/dat/PredictiveCursor#limit_.);
113
114 UInt32cxx . . $ grn/dat/UInt32# node_id = ROOT_NODE_IDcxx . . $ grn/dat/ROOT_NODE_ID.;
115 for (UInt32cxx . . $ grn/dat/UInt32# i = 0; i < str.lengthcxx . . $ grn/dat/String#length(ddd8539a2fcdd4ce).(); ++i) {
116 const Basecxx . . $ grn/dat/Base# base = trie_cxx . . $ grn/dat/PredictiveCursor#trie_.->ith_nodecxx . . $ grn/dat/Trie#ith_node(cc2414b1dc013100).(node_id).basecxx . . $ grn/dat/Node#base(46688f87b1388fe2).();
117 if (base.is_linkercxx . . $ grn/dat/Base#is_linker(50ce9a9e25b4a850).()) {
118 if (offset_cxx . . $ grn/dat/PredictiveCursor#offset_. == 0) {
119 const Keycxx . . $ grn/dat/Key# &key = trie_cxx . . $ grn/dat/PredictiveCursor#trie_.->get_keycxx . . $ grn/dat/Trie#get_key(ac1ad672cda84f14).(base.key_poscxx . . $ grn/dat/Base#key_pos(ddd8539a2fcdd4ce).());
120 if ((key.lengthcxx . . $ grn/dat/Key#length(ddd8539a2fcdd4ce).() >= str.lengthcxx . . $ grn/dat/String#length(ddd8539a2fcdd4ce).()) &&
121 (key.strcxx . . $ grn/dat/Key#str(3f4ba625c018c01d).().substrcxx . . $ grn/dat/String#substr(31e796a9d948593a).(0, str.lengthcxx . . $ grn/dat/String#length(ddd8539a2fcdd4ce).()).comparecxx . . $ grn/dat/String#compare(6575af31c4a8b936).(str, i) == 0)) {
122 if ((flags_cxx . . $ grn/dat/PredictiveCursor#flags_. & ASCENDING_CURSORcxx . . $ grn/dat/ASCENDING_CURSOR.) == ASCENDING_CURSORcxx . . $ grn/dat/ASCENDING_CURSOR.) {
123 node_id |= IS_ROOT_FLAGcxx . . $ grn/dat/PredictiveCursor#IS_ROOT_FLAG.;
124 }
125 buf_cxx . . $ grn/dat/PredictiveCursor#buf_..push_backcxx . . $ grn/dat/Vector#push_back(77d9f17553126ecb).(node_id);
126 }
127 }
128 return;
129 }
130
131 node_id = base.offsetcxx . . $ grn/dat/Base#offset(ddd8539a2fcdd4ce).() ^ str[cxx . . $ grn/dat/String#`operator[]`(ffeb5165310d7d37).i];
132 if (trie_cxx . . $ grn/dat/PredictiveCursor#trie_.->ith_nodecxx . . $ grn/dat/Trie#ith_node(cc2414b1dc013100).(node_id).labelcxx . . $ grn/dat/Node#label(ddd8539a2fcdd4ce).() != str[cxx . . $ grn/dat/String#`operator[]`(ffeb5165310d7d37).i]) {
133 return;
134 }
135 }
136
137 if ((flags_cxx . . $ grn/dat/PredictiveCursor#flags_. & ASCENDING_CURSORcxx . . $ grn/dat/ASCENDING_CURSOR.) == ASCENDING_CURSORcxx . . $ grn/dat/ASCENDING_CURSOR.) {
138 node_id |= IS_ROOT_FLAGcxx . . $ grn/dat/PredictiveCursor#IS_ROOT_FLAG.;
139 }
140 buf_cxx . . $ grn/dat/PredictiveCursor#buf_..push_backcxx . . $ grn/dat/Vector#push_back(77d9f17553126ecb).(node_id);
141 }
142
143 void PredictiveCursorcxx . . $ grn/dat/PredictiveCursor#::swapcxx . . $ grn/dat/PredictiveCursor#swap(5346b7e23c5692d1).(PredictiveCursorcxx . . $ grn/dat/PredictiveCursor# *cursor) {
144 std::swapcxx . . $ std/swap(c75b8fd57d7c2e06).(trie_cxx . . $ grn/dat/PredictiveCursor#trie_., cursor->trie_cxx . . $ grn/dat/PredictiveCursor#trie_.);
145 std::swapcxx . . $ std/swap(c75b8fd57d7c2e06).(offset_cxx . . $ grn/dat/PredictiveCursor#offset_., cursor->offset_cxx . . $ grn/dat/PredictiveCursor#offset_.);
146 std::swapcxx . . $ std/swap(c75b8fd57d7c2e06).(limit_cxx . . $ grn/dat/PredictiveCursor#limit_., cursor->limit_cxx . . $ grn/dat/PredictiveCursor#limit_.);
147 std::swapcxx . . $ std/swap(c75b8fd57d7c2e06).(flags_cxx . . $ grn/dat/PredictiveCursor#flags_., cursor->flags_cxx . . $ grn/dat/PredictiveCursor#flags_.);
148 buf_cxx . . $ grn/dat/PredictiveCursor#buf_..swapcxx . . $ grn/dat/Vector#swap(31b4a4301f47d369).(&cursor->buf_cxx . . $ grn/dat/PredictiveCursor#buf_.);
149 std::swapcxx . . $ std/swap(c75b8fd57d7c2e06).(cur_cxx . . $ grn/dat/PredictiveCursor#cur_., cursor->cur_cxx . . $ grn/dat/PredictiveCursor#cur_.);
150 std::swapcxx . . $ std/swap(c75b8fd57d7c2e06).(end_cxx . . $ grn/dat/PredictiveCursor#end_., cursor->end_cxx . . $ grn/dat/PredictiveCursor#end_.);
151 std::swapcxx . . $ std/swap(c75b8fd57d7c2e06).(min_length_cxx . . $ grn/dat/PredictiveCursor#min_length_., cursor->min_length_cxx . . $ grn/dat/PredictiveCursor#min_length_.);
152 }
153
154 const Keycxx . . $ grn/dat/Key# &PredictiveCursorcxx . . $ grn/dat/PredictiveCursor#::ascending_nextcxx . . $ grn/dat/PredictiveCursor#ascending_next(741ea5814372790).() {
155 while (!buf_cxx . . $ grn/dat/PredictiveCursor#buf_..emptycxx . . $ grn/dat/Vector#empty(50ce9a9e25b4a850).()) {
156 const bool is_root = (buf_cxx . . $ grn/dat/PredictiveCursor#buf_..backcxx . . $ grn/dat/Vector#back(19b2b6f17af048b).() & IS_ROOT_FLAGcxx . . $ grn/dat/PredictiveCursor#IS_ROOT_FLAG.) == IS_ROOT_FLAGcxx . . $ grn/dat/PredictiveCursor#IS_ROOT_FLAG.;
157 const UInt32cxx . . $ grn/dat/UInt32# node_id = buf_cxx . . $ grn/dat/PredictiveCursor#buf_..backcxx . . $ grn/dat/Vector#back(19b2b6f17af048b).() & ~IS_ROOT_FLAGcxx . . $ grn/dat/PredictiveCursor#IS_ROOT_FLAG.;
158 buf_cxx . . $ grn/dat/PredictiveCursor#buf_..pop_backcxx . . $ grn/dat/Vector#pop_back(49f6e7a06ebc5aa8).();
159
160 const Nodecxx . . $ grn/dat/Node# node = trie_cxx . . $ grn/dat/PredictiveCursor#trie_.->ith_nodecxx . . $ grn/dat/Trie#ith_node(cc2414b1dc013100).(node_id);
161 if (!is_root && (node.siblingcxx . . $ grn/dat/Node#sibling(ddd8539a2fcdd4ce).() != INVALID_LABELcxx . . $ grn/dat/INVALID_LABEL.)) {
162 buf_cxx . . $ grn/dat/PredictiveCursor#buf_..push_backcxx . . $ grn/dat/Vector#push_back(77d9f17553126ecb).(node_id ^ node.labelcxx . . $ grn/dat/Node#label(ddd8539a2fcdd4ce).() ^ node.siblingcxx . . $ grn/dat/Node#sibling(ddd8539a2fcdd4ce).());
163 }
164
165 if (node.is_linkercxx . . $ grn/dat/Node#is_linker(50ce9a9e25b4a850).()) {
166 const Keycxx . . $ grn/dat/Key# &key = trie_cxx . . $ grn/dat/PredictiveCursor#trie_.->get_keycxx . . $ grn/dat/Trie#get_key(ac1ad672cda84f14).(node.key_poscxx . . $ grn/dat/Node#key_pos(ddd8539a2fcdd4ce).());
167 if (key.lengthcxx . . $ grn/dat/Key#length(ddd8539a2fcdd4ce).() >= min_length_cxx . . $ grn/dat/PredictiveCursor#min_length_.) {
168 if (cur_cxx . . $ grn/dat/PredictiveCursor#cur_.++ >= offset_cxx . . $ grn/dat/PredictiveCursor#offset_.) {
169 return key;
170 }
171 }
172 } else if (node.childcxx . . $ grn/dat/Node#child(ddd8539a2fcdd4ce).() != INVALID_LABELcxx . . $ grn/dat/INVALID_LABEL.) {
173 buf_cxx . . $ grn/dat/PredictiveCursor#buf_..push_backcxx . . $ grn/dat/Vector#push_back(77d9f17553126ecb).(node.offsetcxx . . $ grn/dat/Node#offset(ddd8539a2fcdd4ce).() ^ node.childcxx . . $ grn/dat/Node#child(ddd8539a2fcdd4ce).());
174 }
175 }
176 return Keycxx . . $ grn/dat/Key#::invalid_keycxx . . $ grn/dat/Key#invalid_key(741ea5814372790).();
177 }
178
179 const Keycxx . . $ grn/dat/Key# &PredictiveCursorcxx . . $ grn/dat/PredictiveCursor#::descending_nextcxx . . $ grn/dat/PredictiveCursor#descending_next(741ea5814372790).() {
180 while (!buf_cxx . . $ grn/dat/PredictiveCursor#buf_..emptycxx . . $ grn/dat/Vector#empty(50ce9a9e25b4a850).()) {
181 const bool post_order = (buf_cxx . . $ grn/dat/PredictiveCursor#buf_..backcxx . . $ grn/dat/Vector#back(19b2b6f17af048b).() & POST_ORDER_FLAGcxx . . $ grn/dat/PredictiveCursor#POST_ORDER_FLAG.) == POST_ORDER_FLAGcxx . . $ grn/dat/PredictiveCursor#POST_ORDER_FLAG.;
182 const UInt32cxx . . $ grn/dat/UInt32# node_id = buf_cxx . . $ grn/dat/PredictiveCursor#buf_..backcxx . . $ grn/dat/Vector#back(19b2b6f17af048b).() & ~POST_ORDER_FLAGcxx . . $ grn/dat/PredictiveCursor#POST_ORDER_FLAG.;
183
184 const Basecxx . . $ grn/dat/Base# base = trie_cxx . . $ grn/dat/PredictiveCursor#trie_.->ith_nodecxx . . $ grn/dat/Trie#ith_node(cc2414b1dc013100).(node_id).basecxx . . $ grn/dat/Node#base(46688f87b1388fe2).();
185 if (post_order) {
186 buf_cxx . . $ grn/dat/PredictiveCursor#buf_..pop_backcxx . . $ grn/dat/Vector#pop_back(49f6e7a06ebc5aa8).();
187 if (base.is_linkercxx . . $ grn/dat/Base#is_linker(50ce9a9e25b4a850).()) {
188 const Keycxx . . $ grn/dat/Key# &key = trie_cxx . . $ grn/dat/PredictiveCursor#trie_.->get_keycxx . . $ grn/dat/Trie#get_key(ac1ad672cda84f14).(base.key_poscxx . . $ grn/dat/Base#key_pos(ddd8539a2fcdd4ce).());
189 if (key.lengthcxx . . $ grn/dat/Key#length(ddd8539a2fcdd4ce).() >= min_length_cxx . . $ grn/dat/PredictiveCursor#min_length_.) {
190 if (cur_cxx . . $ grn/dat/PredictiveCursor#cur_.++ >= offset_cxx . . $ grn/dat/PredictiveCursor#offset_.) {
191 return key;
192 }
193 }
194 }
195 } else {
196 buf_cxx . . $ grn/dat/PredictiveCursor#buf_..backcxx . . $ grn/dat/Vector#back(19b2b6f17af048b).() |= POST_ORDER_FLAGcxx . . $ grn/dat/PredictiveCursor#POST_ORDER_FLAG.;
197 UInt32cxx . . $ grn/dat/UInt32# label = trie_cxx . . $ grn/dat/PredictiveCursor#trie_.->ith_nodecxx . . $ grn/dat/Trie#ith_node(cc2414b1dc013100).(node_id).childcxx . . $ grn/dat/Node#child(ddd8539a2fcdd4ce).();
198 while (label != INVALID_LABELcxx . . $ grn/dat/INVALID_LABEL.) {
199 buf_cxx . . $ grn/dat/PredictiveCursor#buf_..push_backcxx . . $ grn/dat/Vector#push_back(77d9f17553126ecb).(base.offsetcxx . . $ grn/dat/Base#offset(ddd8539a2fcdd4ce).() ^ label);
200 label = trie_cxx . . $ grn/dat/PredictiveCursor#trie_.->ith_nodecxx . . $ grn/dat/Trie#ith_node(cc2414b1dc013100).(base.offsetcxx . . $ grn/dat/Base#offset(ddd8539a2fcdd4ce).() ^ label).siblingcxx . . $ grn/dat/Node#sibling(ddd8539a2fcdd4ce).();
201 }
202 }
203 }
204 return Keycxx . . $ grn/dat/Key#::invalid_keycxx . . $ grn/dat/Key#invalid_key(741ea5814372790).();
205 }
206
207 } // namespace dat
208 } // namespace grn