| 1 | /* Copyright(C) 2011 Brazil |
| 2 | |
| 3 | This library is free software; you can redistribute it and/or |
| 4 | modify it under the terms of the GNU Lesser General Public |
| 5 | License as published by the Free Software Foundation; either |
| 6 | version 2.1 of the License, or (at your option) any later version. |
| 7 | |
| 8 | This library is distributed in the hope that it will be useful, |
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | Lesser General Public License for more details. |
| 12 | |
| 13 | You should have received a copy of the GNU Lesser General Public |
| 14 | License along with this library; if not, write to the Free Software |
| 15 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | */ |
| 17 | |
| 18 | #include "prefix-cursor.hpp"cxx . . $ `<file>/lib/dat/prefix-cursor.hpp`/ |
| 19 | |
| 20 | #include <algorithm> |
| 21 | |
| 22 | #include "trie.hpp"cxx . . $ `<file>/lib/dat/trie.hpp`/ |
| 23 | |
| 24 | namespace grncxx . . $ grn/ { |
| 25 | namespace datcxx . . $ grn/dat/ { |
| 26 | |
| 27 | PrefixCursorcxx . . $ grn/dat/PrefixCursor#::PrefixCursorcxx . . $ grn/dat/PrefixCursor#PrefixCursor(49f6e7a06ebc5aa8).() |
| 28 | : trie_cxx . . $ grn/dat/PrefixCursor#trie_.(NULLcxx . . $ `<external>/16391990256953433862/stddef.h:416:9`!), |
| 29 | offset_cxx . . $ grn/dat/PrefixCursor#offset_.(0), |
| 30 | limit_cxx . . $ grn/dat/PrefixCursor#limit_.(MAX_UINT32cxx . . $ grn/dat/MAX_UINT32.), |
| 31 | flags_cxx . . $ grn/dat/PrefixCursor#flags_.(PREFIX_CURSORcxx . . $ grn/dat/PREFIX_CURSOR.), |
| 32 | buf_cxx . . $ grn/dat/PrefixCursor#buf_.(), |
| 33 | cur_cxx . . $ grn/dat/PrefixCursor#cur_.(0), |
| 34 | end_cxx . . $ grn/dat/PrefixCursor#end_.(0) {} |
| 35 | |
| 36 | PrefixCursorcxx . . $ grn/dat/PrefixCursor#::~cxx . . $ grn/dat/PrefixCursor#`~PrefixCursor`(ced63f7c635d850d).PrefixCursorcxx . . $ grn/dat/PrefixCursor#() {} |
| 37 | |
| 38 | void PrefixCursorcxx . . $ grn/dat/PrefixCursor#::opencxx . . $ grn/dat/PrefixCursor#open(eb3ebad4d3cb99d).(const Triecxx . . $ grn/dat/Trie# &trie, |
| 39 | const Stringcxx . . $ grn/dat/String# &str, |
| 40 | UInt32cxx . . $ grn/dat/UInt32# min_length, |
| 41 | UInt32cxx . . $ grn/dat/UInt32# offset, |
| 42 | UInt32cxx . . $ grn/dat/UInt32# limit, |
| 43 | UInt32cxx . . $ grn/dat/UInt32# flags) { |
| 44 | GRN_DAT_THROW_IFcxx . . $ `lib/dat/dat.hpp:232:9`!(PARAM_ERROR, (str.ptr() == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:416:9`!) && (str.length() != 0)); |
| 45 | GRN_DAT_THROW_IFcxx . . $ `lib/dat/dat.hpp:232:9`!(PARAM_ERROR, min_length > str.length()); |
| 46 | |
| 47 | flags = fix_flagscxx . . $ grn/dat/PrefixCursor#fix_flags(560347b7e9428769).(flags); |
| 48 | PrefixCursorcxx . . $ grn/dat/PrefixCursor# new_cursorcxx . . $ grn/dat/PrefixCursor#PrefixCursor(86ae9facf0bf692d).(trie, offset, limit, flags); |
| 49 | new_cursor.initcxx . . $ grn/dat/PrefixCursor#init(336363187ac20b8c).(str, min_length); |
| 50 | new_cursor.swapcxx . . $ grn/dat/PrefixCursor#swap(f69801c098c83ffb).(this); |
| 51 | } |
| 52 | |
| 53 | void PrefixCursorcxx . . $ grn/dat/PrefixCursor#::closecxx . . $ grn/dat/PrefixCursor#close(49f6e7a06ebc5aa8).() { |
| 54 | PrefixCursorcxx . . $ grn/dat/PrefixCursor# new_cursorcxx . . $ grn/dat/PrefixCursor#PrefixCursor(49f6e7a06ebc5aa8).; |
| 55 | new_cursor.swapcxx . . $ grn/dat/PrefixCursor#swap(f69801c098c83ffb).(this); |
| 56 | } |
| 57 | |
| 58 | const Keycxx . . $ grn/dat/Key# &PrefixCursorcxx . . $ grn/dat/PrefixCursor#::nextcxx . . $ grn/dat/PrefixCursor#next(741ea5814372790).() { |
| 59 | if (cur_cxx . . $ grn/dat/PrefixCursor#cur_. == end_cxx . . $ grn/dat/PrefixCursor#end_.) { |
| 60 | return Keycxx . . $ grn/dat/Key#::invalid_keycxx . . $ grn/dat/Key#invalid_key(741ea5814372790).(); |
| 61 | } |
| 62 | if ((flags_cxx . . $ grn/dat/PrefixCursor#flags_. & ASCENDING_CURSORcxx . . $ grn/dat/ASCENDING_CURSOR.) == ASCENDING_CURSORcxx . . $ grn/dat/ASCENDING_CURSOR.) { |
| 63 | return trie_cxx . . $ grn/dat/PrefixCursor#trie_.->get_keycxx . . $ grn/dat/Trie#get_key(ac1ad672cda84f14).(buf_cxx . . $ grn/dat/PrefixCursor#buf_.[cxx . . $ grn/dat/Vector#`operator[]`(cbb7c2c208bf2972).cur_cxx . . $ grn/dat/PrefixCursor#cur_.++]); |
| 64 | } else { |
| 65 | return trie_cxx . . $ grn/dat/PrefixCursor#trie_.->get_keycxx . . $ grn/dat/Trie#get_key(ac1ad672cda84f14).(buf_cxx . . $ grn/dat/PrefixCursor#buf_.[cxx . . $ grn/dat/Vector#`operator[]`(cbb7c2c208bf2972).--cur_cxx . . $ grn/dat/PrefixCursor#cur_.]); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | PrefixCursorcxx . . $ grn/dat/PrefixCursor#::PrefixCursorcxx . . $ grn/dat/PrefixCursor#PrefixCursor(86ae9facf0bf692d).(const Triecxx . . $ grn/dat/Trie# &trie, |
| 70 | UInt32cxx . . $ grn/dat/UInt32# offset, UInt32cxx . . $ grn/dat/UInt32# limit, UInt32cxx . . $ grn/dat/UInt32# flags) |
| 71 | : trie_cxx . . $ grn/dat/PrefixCursor#trie_.(&trie), |
| 72 | offset_cxx . . $ grn/dat/PrefixCursor#offset_.(offset), |
| 73 | limit_cxx . . $ grn/dat/PrefixCursor#limit_.(limit), |
| 74 | flags_cxx . . $ grn/dat/PrefixCursor#flags_.(flags), |
| 75 | buf_cxx . . $ grn/dat/PrefixCursor#buf_.(), |
| 76 | cur_cxx . . $ grn/dat/PrefixCursor#cur_.(0), |
| 77 | end_cxx . . $ grn/dat/PrefixCursor#end_.(0) {} |
| 78 | |
| 79 | UInt32cxx . . $ grn/dat/UInt32# PrefixCursorcxx . . $ grn/dat/PrefixCursor#::fix_flagscxx . . $ grn/dat/PrefixCursor#fix_flags(560347b7e9428769).(UInt32cxx . . $ grn/dat/UInt32# flags) const { |
| 80 | const UInt32cxx . . $ grn/dat/UInt32# cursor_type = flags & CURSOR_TYPE_MASKcxx . . $ grn/dat/CURSOR_TYPE_MASK.; |
| 81 | GRN_DAT_THROW_IFcxx . . $ `lib/dat/dat.hpp:232:9`!(PARAM_ERROR, (cursor_type != 0) && |
| 82 | (cursor_type != PREFIX_CURSOR)); |
| 83 | flags |= PREFIX_CURSORcxx . . $ grn/dat/PREFIX_CURSOR.; |
| 84 | |
| 85 | const UInt32cxx . . $ grn/dat/UInt32# cursor_order = flags & CURSOR_ORDER_MASKcxx . . $ grn/dat/CURSOR_ORDER_MASK.; |
| 86 | GRN_DAT_THROW_IFcxx . . $ `lib/dat/dat.hpp:232:9`!(PARAM_ERROR, (cursor_order != 0) && |
| 87 | (cursor_order != ASCENDING_CURSOR) && |
| 88 | (cursor_order != DESCENDING_CURSOR)); |
| 89 | if (cursor_order == 0) { |
| 90 | flags |= ASCENDING_CURSORcxx . . $ grn/dat/ASCENDING_CURSOR.; |
| 91 | } |
| 92 | |
| 93 | const UInt32cxx . . $ grn/dat/UInt32# cursor_options = flags & CURSOR_OPTIONS_MASKcxx . . $ grn/dat/CURSOR_OPTIONS_MASK.; |
| 94 | GRN_DAT_THROW_IFcxx . . $ `lib/dat/dat.hpp:232:9`!(PARAM_ERROR, cursor_options & ~EXCEPT_EXACT_MATCH); |
| 95 | |
| 96 | return flags; |
| 97 | } |
| 98 | |
| 99 | void PrefixCursorcxx . . $ grn/dat/PrefixCursor#::initcxx . . $ grn/dat/PrefixCursor#init(336363187ac20b8c).(const Stringcxx . . $ grn/dat/String# &str, UInt32cxx . . $ grn/dat/UInt32# min_length) { |
| 100 | if ((limit_cxx . . $ grn/dat/PrefixCursor#limit_. == 0) || (offset_cxx . . $ grn/dat/PrefixCursor#offset_. > (str.lengthcxx . . $ grn/dat/String#length(ddd8539a2fcdd4ce).() - min_length))) { |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | UInt32cxx . . $ grn/dat/UInt32# node_id = ROOT_NODE_IDcxx . . $ grn/dat/ROOT_NODE_ID.; |
| 105 | UInt32cxx . . $ grn/dat/UInt32# i; |
| 106 | for (i = 0; i < str.lengthcxx . . $ grn/dat/String#length(ddd8539a2fcdd4ce).(); ++i) { |
| 107 | const Basecxx . . $ grn/dat/Base# base = trie_cxx . . $ grn/dat/PrefixCursor#trie_.->ith_nodecxx . . $ grn/dat/Trie#ith_node(cc2414b1dc013100).(node_id).basecxx . . $ grn/dat/Node#base(46688f87b1388fe2).(); |
| 108 | if (base.is_linkercxx . . $ grn/dat/Base#is_linker(50ce9a9e25b4a850).()) { |
| 109 | const Keycxx . . $ grn/dat/Key# &key = trie_cxx . . $ grn/dat/PrefixCursor#trie_.->get_keycxx . . $ grn/dat/Trie#get_key(ac1ad672cda84f14).(base.key_poscxx . . $ grn/dat/Base#key_pos(ddd8539a2fcdd4ce).()); |
| 110 | if ((key.lengthcxx . . $ grn/dat/Key#length(ddd8539a2fcdd4ce).() >= min_length) && (key.lengthcxx . . $ grn/dat/Key#length(ddd8539a2fcdd4ce).() <= str.lengthcxx . . $ grn/dat/String#length(ddd8539a2fcdd4ce).()) && |
| 111 | (str.substrcxx . . $ grn/dat/String#substr(31e796a9d948593a).(0, key.lengthcxx . . $ grn/dat/Key#length(ddd8539a2fcdd4ce).()).comparecxx . . $ grn/dat/String#compare(6575af31c4a8b936).(key.strcxx . . $ grn/dat/Key#str(3f4ba625c018c01d).(), i) == 0) && |
| 112 | ((key.lengthcxx . . $ grn/dat/Key#length(ddd8539a2fcdd4ce).() < str.lengthcxx . . $ grn/dat/String#length(ddd8539a2fcdd4ce).()) || |
| 113 | ((flags_cxx . . $ grn/dat/PrefixCursor#flags_. & EXCEPT_EXACT_MATCHcxx . . $ grn/dat/EXCEPT_EXACT_MATCH.) != EXCEPT_EXACT_MATCHcxx . . $ grn/dat/EXCEPT_EXACT_MATCH.))) { |
| 114 | buf_cxx . . $ grn/dat/PrefixCursor#buf_..push_backcxx . . $ grn/dat/Vector#push_back(77d9f17553126ecb).(base.key_poscxx . . $ grn/dat/Base#key_pos(ddd8539a2fcdd4ce).()); |
| 115 | } |
| 116 | break; |
| 117 | } |
| 118 | |
| 119 | if ((i >= min_length) && |
| 120 | (trie_cxx . . $ grn/dat/PrefixCursor#trie_.->ith_nodecxx . . $ grn/dat/Trie#ith_node(cc2414b1dc013100).(node_id).childcxx . . $ grn/dat/Node#child(ddd8539a2fcdd4ce).() == TERMINAL_LABELcxx . . $ grn/dat/TERMINAL_LABEL.)) { |
| 121 | const Basecxx . . $ grn/dat/Base# linker_base = |
| 122 | trie_cxx . . $ grn/dat/PrefixCursor#trie_.->ith_nodecxx . . $ grn/dat/Trie#ith_node(cc2414b1dc013100).(base.offsetcxx . . $ grn/dat/Base#offset(ddd8539a2fcdd4ce).() ^ TERMINAL_LABELcxx . . $ grn/dat/TERMINAL_LABEL.).basecxx . . $ grn/dat/Node#base(46688f87b1388fe2).(); |
| 123 | if (linker_base.is_linkercxx . . $ grn/dat/Base#is_linker(50ce9a9e25b4a850).()) { |
| 124 | buf_cxx . . $ grn/dat/PrefixCursor#buf_..push_backcxx . . $ grn/dat/Vector#push_back(77d9f17553126ecb).(linker_base.key_poscxx . . $ grn/dat/Base#key_pos(ddd8539a2fcdd4ce).()); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | node_id = base.offsetcxx . . $ grn/dat/Base#offset(ddd8539a2fcdd4ce).() ^ str[cxx . . $ grn/dat/String#`operator[]`(ffeb5165310d7d37).i]; |
| 129 | if (trie_cxx . . $ grn/dat/PrefixCursor#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]) { |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | if ((i == str.lengthcxx . . $ grn/dat/String#length(ddd8539a2fcdd4ce).()) && |
| 135 | ((flags_cxx . . $ grn/dat/PrefixCursor#flags_. & EXCEPT_EXACT_MATCHcxx . . $ grn/dat/EXCEPT_EXACT_MATCH.) != EXCEPT_EXACT_MATCHcxx . . $ grn/dat/EXCEPT_EXACT_MATCH.)) { |
| 136 | const Basecxx . . $ grn/dat/Base# base = trie_cxx . . $ grn/dat/PrefixCursor#trie_.->ith_nodecxx . . $ grn/dat/Trie#ith_node(cc2414b1dc013100).(node_id).basecxx . . $ grn/dat/Node#base(46688f87b1388fe2).(); |
| 137 | if (base.is_linkercxx . . $ grn/dat/Base#is_linker(50ce9a9e25b4a850).()) { |
| 138 | const Keycxx . . $ grn/dat/Key# &key = trie_cxx . . $ grn/dat/PrefixCursor#trie_.->get_keycxx . . $ grn/dat/Trie#get_key(ac1ad672cda84f14).(base.key_poscxx . . $ grn/dat/Base#key_pos(ddd8539a2fcdd4ce).()); |
| 139 | if ((key.lengthcxx . . $ grn/dat/Key#length(ddd8539a2fcdd4ce).() >= min_length) && (key.lengthcxx . . $ grn/dat/Key#length(ddd8539a2fcdd4ce).() <= str.lengthcxx . . $ grn/dat/String#length(ddd8539a2fcdd4ce).())) { |
| 140 | buf_cxx . . $ grn/dat/PrefixCursor#buf_..push_backcxx . . $ grn/dat/Vector#push_back(77d9f17553126ecb).(base.key_poscxx . . $ grn/dat/Base#key_pos(ddd8539a2fcdd4ce).()); |
| 141 | } |
| 142 | } else if (trie_cxx . . $ grn/dat/PrefixCursor#trie_.->ith_nodecxx . . $ grn/dat/Trie#ith_node(cc2414b1dc013100).(node_id).childcxx . . $ grn/dat/Node#child(ddd8539a2fcdd4ce).() == TERMINAL_LABELcxx . . $ grn/dat/TERMINAL_LABEL.) { |
| 143 | const Basecxx . . $ grn/dat/Base# linker_base = |
| 144 | trie_cxx . . $ grn/dat/PrefixCursor#trie_.->ith_nodecxx . . $ grn/dat/Trie#ith_node(cc2414b1dc013100).(base.offsetcxx . . $ grn/dat/Base#offset(ddd8539a2fcdd4ce).() ^ TERMINAL_LABELcxx . . $ grn/dat/TERMINAL_LABEL.).basecxx . . $ grn/dat/Node#base(46688f87b1388fe2).(); |
| 145 | if (linker_base.is_linkercxx . . $ grn/dat/Base#is_linker(50ce9a9e25b4a850).()) { |
| 146 | buf_cxx . . $ grn/dat/PrefixCursor#buf_..push_backcxx . . $ grn/dat/Vector#push_back(77d9f17553126ecb).(linker_base.key_poscxx . . $ grn/dat/Base#key_pos(ddd8539a2fcdd4ce).()); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | if (buf_cxx . . $ grn/dat/PrefixCursor#buf_..sizecxx . . $ grn/dat/Vector#size(ddd8539a2fcdd4ce).() <= offset_cxx . . $ grn/dat/PrefixCursor#offset_.) { |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | if ((flags_cxx . . $ grn/dat/PrefixCursor#flags_. & ASCENDING_CURSORcxx . . $ grn/dat/ASCENDING_CURSOR.) == ASCENDING_CURSORcxx . . $ grn/dat/ASCENDING_CURSOR.) { |
| 156 | cur_cxx . . $ grn/dat/PrefixCursor#cur_. = offset_cxx . . $ grn/dat/PrefixCursor#offset_.; |
| 157 | end_cxx . . $ grn/dat/PrefixCursor#end_. = (limit_cxx . . $ grn/dat/PrefixCursor#limit_. < (buf_cxx . . $ grn/dat/PrefixCursor#buf_..sizecxx . . $ grn/dat/Vector#size(ddd8539a2fcdd4ce).() - cur_cxx . . $ grn/dat/PrefixCursor#cur_.)) ? (cur_cxx . . $ grn/dat/PrefixCursor#cur_. + limit_cxx . . $ grn/dat/PrefixCursor#limit_.) : buf_cxx . . $ grn/dat/PrefixCursor#buf_..sizecxx . . $ grn/dat/Vector#size(ddd8539a2fcdd4ce).(); |
| 158 | } else { |
| 159 | cur_cxx . . $ grn/dat/PrefixCursor#cur_. = buf_cxx . . $ grn/dat/PrefixCursor#buf_..sizecxx . . $ grn/dat/Vector#size(ddd8539a2fcdd4ce).() - offset_cxx . . $ grn/dat/PrefixCursor#offset_.; |
| 160 | end_cxx . . $ grn/dat/PrefixCursor#end_. = (limit_cxx . . $ grn/dat/PrefixCursor#limit_. < cur_cxx . . $ grn/dat/PrefixCursor#cur_.) ? (cur_cxx . . $ grn/dat/PrefixCursor#cur_. - limit_cxx . . $ grn/dat/PrefixCursor#limit_.) : 0; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | void PrefixCursorcxx . . $ grn/dat/PrefixCursor#::swapcxx . . $ grn/dat/PrefixCursor#swap(f69801c098c83ffb).(PrefixCursorcxx . . $ grn/dat/PrefixCursor# *cursor) { |
| 165 | stdcxx . . $ std/::swapcxx . . $ std/swap(c75b8fd57d7c2e06).(trie_cxx . . $ grn/dat/PrefixCursor#trie_., cursor->trie_cxx . . $ grn/dat/PrefixCursor#trie_.); |
| 166 | stdcxx . . $ std/::swapcxx . . $ std/swap(c75b8fd57d7c2e06).(offset_cxx . . $ grn/dat/PrefixCursor#offset_., cursor->offset_cxx . . $ grn/dat/PrefixCursor#offset_.); |
| 167 | stdcxx . . $ std/::swapcxx . . $ std/swap(c75b8fd57d7c2e06).(limit_cxx . . $ grn/dat/PrefixCursor#limit_., cursor->limit_cxx . . $ grn/dat/PrefixCursor#limit_.); |
| 168 | stdcxx . . $ std/::swapcxx . . $ std/swap(c75b8fd57d7c2e06).(flags_cxx . . $ grn/dat/PrefixCursor#flags_., cursor->flags_cxx . . $ grn/dat/PrefixCursor#flags_.); |
| 169 | buf_cxx . . $ grn/dat/PrefixCursor#buf_..swapcxx . . $ grn/dat/Vector#swap(31b4a4301f47d369).(&cursor->buf_cxx . . $ grn/dat/PrefixCursor#buf_.); |
| 170 | stdcxx . . $ std/::swapcxx . . $ std/swap(c75b8fd57d7c2e06).(cur_cxx . . $ grn/dat/PrefixCursor#cur_., cursor->cur_cxx . . $ grn/dat/PrefixCursor#cur_.); |
| 171 | stdcxx . . $ std/::swapcxx . . $ std/swap(c75b8fd57d7c2e06).(end_cxx . . $ grn/dat/PrefixCursor#end_., cursor->end_cxx . . $ grn/dat/PrefixCursor#end_.); |
| 172 | } |
| 173 | |
| 174 | } // namespace dat |
| 175 | } // namespace grn |