| 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 "id-cursor.hpp"cxx . . $ `<file>/lib/dat/id-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 | IdCursorcxx . . $ grn/dat/IdCursor#::IdCursorcxx . . $ grn/dat/IdCursor#IdCursor(49f6e7a06ebc5aa8).() |
| 28 | : trie_cxx . . $ grn/dat/IdCursor#trie_.(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:416:9`!), |
| 29 | offset_cxx . . $ grn/dat/IdCursor#offset_.(0), |
| 30 | limit_cxx . . $ grn/dat/IdCursor#limit_.(MAX_UINT32cxx . . $ grn/dat/MAX_UINT32.), |
| 31 | flags_cxx . . $ grn/dat/IdCursor#flags_.(ID_RANGE_CURSORcxx . . $ grn/dat/ID_RANGE_CURSOR.), |
| 32 | cur_cxx . . $ grn/dat/IdCursor#cur_.(INVALID_KEY_IDcxx . . $ grn/dat/INVALID_KEY_ID.), |
| 33 | end_cxx . . $ grn/dat/IdCursor#end_.(INVALID_KEY_IDcxx . . $ grn/dat/INVALID_KEY_ID.), |
| 34 | count_cxx . . $ grn/dat/IdCursor#count_.(0) {} |
| 35 | |
| 36 | IdCursorcxx . . $ grn/dat/IdCursor#::~cxx . . $ grn/dat/IdCursor#`~IdCursor`(ced63f7c635d850d).IdCursorcxx . . $ grn/dat/IdCursor#() {} |
| 37 | |
| 38 | void IdCursorcxx . . $ grn/dat/IdCursor#::opencxx . . $ grn/dat/IdCursor#open(ff67b50667d7ad53).(const Triecxx . . $ grn/dat/Trie# &trie, |
| 39 | const Stringcxx . . $ grn/dat/String# &min_str, |
| 40 | const Stringcxx . . $ grn/dat/String# &max_str, |
| 41 | UInt32cxx . . $ grn/dat/UInt32# offset, |
| 42 | UInt32cxx . . $ grn/dat/UInt32# limit, |
| 43 | UInt32cxx . . $ grn/dat/UInt32# flags) { |
| 44 | UInt32cxx . . $ grn/dat/UInt32# min_id = INVALID_KEY_IDcxx . . $ grn/dat/INVALID_KEY_ID.; |
| 45 | if (min_str.ptrcxx . . $ grn/dat/String#ptr(3a8ef3fdc6e817ef).() != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:416:9`!) { |
| 46 | UInt32cxx . . $ grn/dat/UInt32# key_pos; |
| 47 | GRN_DAT_THROW_IFcxx . . $ `lib/dat/dat.hpp:232:9`!(PARAM_ERROR, |
| 48 | !trie.search(min_str.ptr(), min_str.length(), &key_pos)); |
| 49 | min_id = trie.get_keycxx . . $ grn/dat/Trie#get_key(ac1ad672cda84f14).(key_pos).idcxx . . $ grn/dat/Key#id(ddd8539a2fcdd4ce).(); |
| 50 | } |
| 51 | |
| 52 | UInt32cxx . . $ grn/dat/UInt32# max_id = INVALID_KEY_IDcxx . . $ grn/dat/INVALID_KEY_ID.; |
| 53 | if (max_str.ptrcxx . . $ grn/dat/String#ptr(3a8ef3fdc6e817ef).() != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:416:9`!) { |
| 54 | UInt32cxx . . $ grn/dat/UInt32# key_pos; |
| 55 | GRN_DAT_THROW_IFcxx . . $ `lib/dat/dat.hpp:232:9`!(PARAM_ERROR, |
| 56 | !trie.search(max_str.ptr(), max_str.length(), &key_pos)); |
| 57 | max_id = trie.get_keycxx . . $ grn/dat/Trie#get_key(ac1ad672cda84f14).(key_pos).idcxx . . $ grn/dat/Key#id(ddd8539a2fcdd4ce).(); |
| 58 | } |
| 59 | |
| 60 | opencxx . . $ grn/dat/IdCursor#open(55ee5e0940af299).(trie, min_id, max_id, offset, limit, flags); |
| 61 | } |
| 62 | |
| 63 | void IdCursorcxx . . $ grn/dat/IdCursor#::opencxx . . $ grn/dat/IdCursor#open(55ee5e0940af299).(const Triecxx . . $ grn/dat/Trie# &trie, |
| 64 | UInt32cxx . . $ grn/dat/UInt32# min_id, |
| 65 | UInt32cxx . . $ grn/dat/UInt32# max_id, |
| 66 | UInt32cxx . . $ grn/dat/UInt32# offset, |
| 67 | UInt32cxx . . $ grn/dat/UInt32# limit, |
| 68 | UInt32cxx . . $ grn/dat/UInt32# flags) { |
| 69 | flags = fix_flagscxx . . $ grn/dat/IdCursor#fix_flags(560347b7e9428769).(flags); |
| 70 | |
| 71 | IdCursorcxx . . $ grn/dat/IdCursor# new_cursorcxx . . $ grn/dat/IdCursor#IdCursor(86ae9facf0bf692d).(trie, offset, limit, flags); |
| 72 | new_cursor.initcxx . . $ grn/dat/IdCursor#init(b4d55b1286ff73e8).(min_id, max_id); |
| 73 | new_cursor.swapcxx . . $ grn/dat/IdCursor#swap(b05bc280ba22b3f5).(this); |
| 74 | } |
| 75 | |
| 76 | void IdCursorcxx . . $ grn/dat/IdCursor#::closecxx . . $ grn/dat/IdCursor#close(49f6e7a06ebc5aa8).() { |
| 77 | IdCursorcxx . . $ grn/dat/IdCursor# new_cursorcxx . . $ grn/dat/IdCursor#IdCursor(49f6e7a06ebc5aa8).; |
| 78 | new_cursor.swapcxx . . $ grn/dat/IdCursor#swap(b05bc280ba22b3f5).(this); |
| 79 | } |
| 80 | |
| 81 | const Keycxx . . $ grn/dat/Key# &IdCursorcxx . . $ grn/dat/IdCursor#::nextcxx . . $ grn/dat/IdCursor#next(741ea5814372790).() { |
| 82 | if (count_cxx . . $ grn/dat/IdCursor#count_. >= limit_cxx . . $ grn/dat/IdCursor#limit_.) { |
| 83 | return Keycxx . . $ grn/dat/Key#::invalid_keycxx . . $ grn/dat/Key#invalid_key(741ea5814372790).(); |
| 84 | } |
| 85 | while (cur_cxx . . $ grn/dat/IdCursor#cur_. != end_cxx . . $ grn/dat/IdCursor#end_.) { |
| 86 | const Keycxx . . $ grn/dat/Key# &key = trie_cxx . . $ grn/dat/IdCursor#trie_.->ith_keycxx . . $ grn/dat/Trie#ith_key(ac1ad672cda84f14).(cur_cxx . . $ grn/dat/IdCursor#cur_.); |
| 87 | if ((flags_cxx . . $ grn/dat/IdCursor#flags_. & ASCENDING_CURSORcxx . . $ grn/dat/ASCENDING_CURSOR.) == ASCENDING_CURSORcxx . . $ grn/dat/ASCENDING_CURSOR.) { |
| 88 | ++cur_cxx . . $ grn/dat/IdCursor#cur_.; |
| 89 | } else { |
| 90 | --cur_cxx . . $ grn/dat/IdCursor#cur_.; |
| 91 | } |
| 92 | if (key.is_validcxx . . $ grn/dat/Key#is_valid(50ce9a9e25b4a850).()) { |
| 93 | ++count_cxx . . $ grn/dat/IdCursor#count_.; |
| 94 | return key; |
| 95 | } |
| 96 | } |
| 97 | return Keycxx . . $ grn/dat/Key#::invalid_keycxx . . $ grn/dat/Key#invalid_key(741ea5814372790).(); |
| 98 | } |
| 99 | |
| 100 | IdCursorcxx . . $ grn/dat/IdCursor#::IdCursorcxx . . $ grn/dat/IdCursor#IdCursor(86ae9facf0bf692d).(const Triecxx . . $ grn/dat/Trie# &trie, |
| 101 | UInt32cxx . . $ grn/dat/UInt32# offset, |
| 102 | UInt32cxx . . $ grn/dat/UInt32# limit, |
| 103 | UInt32cxx . . $ grn/dat/UInt32# flags) |
| 104 | : trie_cxx . . $ grn/dat/IdCursor#trie_.(&trie), |
| 105 | offset_cxx . . $ grn/dat/IdCursor#offset_.(offset), |
| 106 | limit_cxx . . $ grn/dat/IdCursor#limit_.(limit), |
| 107 | flags_cxx . . $ grn/dat/IdCursor#flags_.(flags), |
| 108 | cur_cxx . . $ grn/dat/IdCursor#cur_.(INVALID_KEY_IDcxx . . $ grn/dat/INVALID_KEY_ID.), |
| 109 | end_cxx . . $ grn/dat/IdCursor#end_.(INVALID_KEY_IDcxx . . $ grn/dat/INVALID_KEY_ID.), |
| 110 | count_cxx . . $ grn/dat/IdCursor#count_.(0) {} |
| 111 | |
| 112 | UInt32cxx . . $ grn/dat/UInt32# IdCursorcxx . . $ grn/dat/IdCursor#::fix_flagscxx . . $ grn/dat/IdCursor#fix_flags(560347b7e9428769).(UInt32cxx . . $ grn/dat/UInt32# flags) const { |
| 113 | const UInt32cxx . . $ grn/dat/UInt32# cursor_type = flags & CURSOR_TYPE_MASKcxx . . $ grn/dat/CURSOR_TYPE_MASK.; |
| 114 | GRN_DAT_THROW_IFcxx . . $ `lib/dat/dat.hpp:232:9`!(PARAM_ERROR, (cursor_type != 0) && |
| 115 | (cursor_type != ID_RANGE_CURSOR)); |
| 116 | flags |= ID_RANGE_CURSORcxx . . $ grn/dat/ID_RANGE_CURSOR.; |
| 117 | |
| 118 | const UInt32cxx . . $ grn/dat/UInt32# cursor_order = flags & CURSOR_ORDER_MASKcxx . . $ grn/dat/CURSOR_ORDER_MASK.; |
| 119 | GRN_DAT_THROW_IFcxx . . $ `lib/dat/dat.hpp:232:9`!(PARAM_ERROR, (cursor_order != 0) && |
| 120 | (cursor_order != ASCENDING_CURSOR) && |
| 121 | (cursor_order != DESCENDING_CURSOR)); |
| 122 | if (cursor_order == 0) { |
| 123 | flags |= ASCENDING_CURSORcxx . . $ grn/dat/ASCENDING_CURSOR.; |
| 124 | } |
| 125 | |
| 126 | const UInt32cxx . . $ grn/dat/UInt32# cursor_options = flags & CURSOR_OPTIONS_MASKcxx . . $ grn/dat/CURSOR_OPTIONS_MASK.; |
| 127 | GRN_DAT_THROW_IFcxx . . $ `lib/dat/dat.hpp:232:9`!(PARAM_ERROR, |
| 128 | cursor_options & ~(EXCEPT_LOWER_BOUND | EXCEPT_UPPER_BOUND)); |
| 129 | |
| 130 | return flags; |
| 131 | } |
| 132 | |
| 133 | void IdCursorcxx . . $ grn/dat/IdCursor#::initcxx . . $ grn/dat/IdCursor#init(b4d55b1286ff73e8).(UInt32cxx . . $ grn/dat/UInt32# min_id, UInt32cxx . . $ grn/dat/UInt32# max_id) { |
| 134 | if (min_id == INVALID_KEY_IDcxx . . $ grn/dat/INVALID_KEY_ID.) { |
| 135 | min_id = trie_cxx . . $ grn/dat/IdCursor#trie_.->min_key_idcxx . . $ grn/dat/Trie#min_key_id(ddd8539a2fcdd4ce).(); |
| 136 | } else if ((flags_cxx . . $ grn/dat/IdCursor#flags_. & EXCEPT_LOWER_BOUNDcxx . . $ grn/dat/EXCEPT_LOWER_BOUND.) == EXCEPT_LOWER_BOUNDcxx . . $ grn/dat/EXCEPT_LOWER_BOUND.) { |
| 137 | ++min_id; |
| 138 | } |
| 139 | |
| 140 | if (max_id == INVALID_KEY_IDcxx . . $ grn/dat/INVALID_KEY_ID.) { |
| 141 | max_id = trie_cxx . . $ grn/dat/IdCursor#trie_.->max_key_idcxx . . $ grn/dat/Trie#max_key_id(ddd8539a2fcdd4ce).(); |
| 142 | } else if ((flags_cxx . . $ grn/dat/IdCursor#flags_. & EXCEPT_UPPER_BOUNDcxx . . $ grn/dat/EXCEPT_UPPER_BOUND.) == EXCEPT_UPPER_BOUNDcxx . . $ grn/dat/EXCEPT_UPPER_BOUND.) { |
| 143 | --max_id; |
| 144 | } |
| 145 | |
| 146 | if ((max_id < min_id) || ((max_id - min_id) < offset_cxx . . $ grn/dat/IdCursor#offset_.)) { |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | if ((flags_cxx . . $ grn/dat/IdCursor#flags_. & ASCENDING_CURSORcxx . . $ grn/dat/ASCENDING_CURSOR.) == ASCENDING_CURSORcxx . . $ grn/dat/ASCENDING_CURSOR.) { |
| 151 | cur_cxx . . $ grn/dat/IdCursor#cur_. = min_id; |
| 152 | end_cxx . . $ grn/dat/IdCursor#end_. = max_id + 1; |
| 153 | for (UInt32cxx . . $ grn/dat/UInt32# i = 0; (i < offset_cxx . . $ grn/dat/IdCursor#offset_.) && (cur_cxx . . $ grn/dat/IdCursor#cur_. != end_cxx . . $ grn/dat/IdCursor#end_.); ++i) { |
| 154 | while (cur_cxx . . $ grn/dat/IdCursor#cur_. != end_cxx . . $ grn/dat/IdCursor#end_.) { |
| 155 | if (trie_cxx . . $ grn/dat/IdCursor#trie_.->ith_keycxx . . $ grn/dat/Trie#ith_key(ac1ad672cda84f14).(cur_cxx . . $ grn/dat/IdCursor#cur_.++).is_validcxx . . $ grn/dat/Key#is_valid(50ce9a9e25b4a850).()) { |
| 156 | break; |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | } else { |
| 161 | cur_cxx . . $ grn/dat/IdCursor#cur_. = max_id; |
| 162 | end_cxx . . $ grn/dat/IdCursor#end_. = min_id - 1; |
| 163 | for (UInt32cxx . . $ grn/dat/UInt32# i = 0; (i < offset_cxx . . $ grn/dat/IdCursor#offset_.) && (cur_cxx . . $ grn/dat/IdCursor#cur_. != end_cxx . . $ grn/dat/IdCursor#end_.); ++i) { |
| 164 | while (cur_cxx . . $ grn/dat/IdCursor#cur_. != end_cxx . . $ grn/dat/IdCursor#end_.) { |
| 165 | if (trie_cxx . . $ grn/dat/IdCursor#trie_.->ith_keycxx . . $ grn/dat/Trie#ith_key(ac1ad672cda84f14).(cur_cxx . . $ grn/dat/IdCursor#cur_.--).is_validcxx . . $ grn/dat/Key#is_valid(50ce9a9e25b4a850).()) { |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | void IdCursorcxx . . $ grn/dat/IdCursor#::swapcxx . . $ grn/dat/IdCursor#swap(b05bc280ba22b3f5).(IdCursorcxx . . $ grn/dat/IdCursor# *cursor) { |
| 174 | stdcxx . . $ std/::swapcxx . . $ std/swap(c75b8fd57d7c2e06).(trie_cxx . . $ grn/dat/IdCursor#trie_., cursor->trie_cxx . . $ grn/dat/IdCursor#trie_.); |
| 175 | stdcxx . . $ std/::swapcxx . . $ std/swap(c75b8fd57d7c2e06).(offset_cxx . . $ grn/dat/IdCursor#offset_., cursor->offset_cxx . . $ grn/dat/IdCursor#offset_.); |
| 176 | stdcxx . . $ std/::swapcxx . . $ std/swap(c75b8fd57d7c2e06).(limit_cxx . . $ grn/dat/IdCursor#limit_., cursor->limit_cxx . . $ grn/dat/IdCursor#limit_.); |
| 177 | stdcxx . . $ std/::swapcxx . . $ std/swap(c75b8fd57d7c2e06).(flags_cxx . . $ grn/dat/IdCursor#flags_., cursor->flags_cxx . . $ grn/dat/IdCursor#flags_.); |
| 178 | stdcxx . . $ std/::swapcxx . . $ std/swap(c75b8fd57d7c2e06).(cur_cxx . . $ grn/dat/IdCursor#cur_., cursor->cur_cxx . . $ grn/dat/IdCursor#cur_.); |
| 179 | stdcxx . . $ std/::swapcxx . . $ std/swap(c75b8fd57d7c2e06).(end_cxx . . $ grn/dat/IdCursor#end_., cursor->end_cxx . . $ grn/dat/IdCursor#end_.); |
| 180 | stdcxx . . $ std/::swapcxx . . $ std/swap(c75b8fd57d7c2e06).(count_cxx . . $ grn/dat/IdCursor#count_., cursor->count_cxx . . $ grn/dat/IdCursor#count_.); |
| 181 | } |
| 182 | |
| 183 | } // namespace dat |
| 184 | } // namespace grn |