41 if (end_buf_ != NULL) {
53 (min_str.
ptr() == NULL) && (min_str.
length() != 0));
55 (max_str.
ptr() == NULL) && (max_str.
length() != 0));
57 flags = fix_flags(flags);
58 KeyCursor new_cursor(trie, offset, limit, flags);
59 new_cursor.init(min_str, max_str);
60 new_cursor.swap(
this);
65 new_cursor.swap(
this);
69 if (finished_ || (count_ >= max_count_)) {
74 return ascending_next();
76 return descending_next();
103 if (cursor_order == 0) {
114 void KeyCursor::init(
const String &min_str,
const String &max_str) {
118 max_count_ = offset_ + limit_;
126 ascending_init(min_str, max_str);
128 descending_init(min_str, max_str);
132 void KeyCursor::ascending_init(
const String &min_str,
const String &max_str) {
133 if (max_str.ptr() != NULL) {
134 if (max_str.length() != 0) {
135 end_buf_ =
new UInt8[max_str.length()];
136 std::memcpy(end_buf_, max_str.ptr(), max_str.length());
137 end_str_.
assign(end_buf_, max_str.length());
141 if ((min_str.ptr() == NULL) || (min_str.length() == 0)) {
148 for (
UInt32 i = 0;
i < min_str.length(); ++
i) {
150 if (node.is_linker()) {
151 const Key &key = trie_->
get_key(node.key_pos());
153 if ((result > 0) || ((result == 0) &&
157 buf_.
push_back(node_id ^ node.label() ^ node.sibling());
161 buf_.
push_back(node_id ^ node.label() ^ node.sibling());
164 node_id = node.offset() ^ min_str[
i];
166 UInt16 label = node.child();
168 label = trie_->
ith_node(node.offset() ^ label).sibling();
171 if (label > min_str[
i]) {
175 label = trie_->
ith_node(node.offset() ^ label).sibling();
182 if (node.is_linker()) {
183 const Key &key = trie_->
get_key(node.key_pos());
184 if ((key.length() != min_str.length()) ||
188 buf_.
push_back(node_id ^ node.label() ^ node.sibling());
192 buf_.
push_back(node_id ^ node.label() ^ node.sibling());
195 UInt16 label = node.child();
197 ((flags_ & EXCEPT_LOWER_BOUND) == EXCEPT_LOWER_BOUND)) {
198 label = trie_->
ith_node(node.offset() ^ label).sibling();
205 void KeyCursor::descending_init(
const String &min_str,
const String &max_str) {
206 if (min_str.ptr() != NULL) {
207 if (min_str.length() != 0) {
208 end_buf_ =
new UInt8[min_str.length()];
209 std::memcpy(end_buf_, min_str.ptr(), min_str.length());
210 end_str_.
assign(end_buf_, min_str.length());
214 if ((max_str.ptr() == NULL) || (max_str.length() == 0)) {
220 for (
UInt32 i = 0; i < max_str.length(); ++
i) {
222 if (base.is_linker()) {
223 const Key &key = trie_->
get_key(base.key_pos());
224 const int result = key.
str().
compare(max_str, i);
225 if ((result < 0) || ((result == 0) &&
227 buf_.
push_back(node_id | POST_ORDER_FLAG);
234 node_id = base.offset() ^ label;
235 buf_.
push_back(node_id | POST_ORDER_FLAG);
239 node_id = base.offset() ^ label;
240 if (label < max_str[i]) {
242 }
else if (label > max_str[i]) {
255 if (base.is_linker()) {
256 const Key &key = trie_->
get_key(base.key_pos());
257 if ((key.length() == max_str.length()) &&
259 buf_.
push_back(node_id | POST_ORDER_FLAG);
266 ((flags_ & EXCEPT_UPPER_BOUND) != EXCEPT_UPPER_BOUND)) {
267 buf_.
push_back((base.offset() ^ label) | POST_ORDER_FLAG);
271 void KeyCursor::swap(KeyCursor *cursor) {
272 std::swap(trie_, cursor->trie_);
273 std::swap(offset_, cursor->offset_);
274 std::swap(limit_, cursor->limit_);
275 std::swap(flags_, cursor->flags_);
276 buf_.
swap(&cursor->buf_);
277 std::swap(count_, cursor->count_);
278 std::swap(max_count_, cursor->max_count_);
279 std::swap(finished_, cursor->finished_);
280 std::swap(end_buf_, cursor->end_buf_);
281 end_str_.
swap(&cursor->end_str_);
284 const Key &KeyCursor::ascending_next() {
285 while (!buf_.
empty()) {
289 const Node node = trie_->
ith_node(node_id);
291 buf_.
push_back(node_id ^ node.label() ^ node.sibling());
294 if (node.is_linker()) {
295 const Key &key = trie_->
get_key(node.key_pos());
296 if (end_buf_ != NULL) {
297 const int result = key.
str().
compare(end_str_);
298 if ((result > 0) || ((result == 0) &&
299 ((flags_ & EXCEPT_UPPER_BOUND) == EXCEPT_UPPER_BOUND))) {
304 if (count_++ >= offset_) {
308 buf_.
push_back(node.offset() ^ node.child());
314 const Key &KeyCursor::descending_next() {
315 while (!buf_.
empty()) {
316 const bool post_order = (buf_.
back() & POST_ORDER_FLAG) == POST_ORDER_FLAG;
317 const UInt32 node_id = buf_.
back() & ~POST_ORDER_FLAG;
322 if (base.is_linker()) {
323 const Key &key = trie_->
get_key(base.key_pos());
324 if (end_buf_ != NULL) {
325 const int result = key.
str().
compare(end_str_);
326 if ((result < 0) || ((result == 0) &&
327 ((flags_ & EXCEPT_LOWER_BOUND) == EXCEPT_LOWER_BOUND))) {
332 if (count_++ >= offset_) {
337 buf_.
back() |= POST_ORDER_FLAG;
341 label = trie_->
ith_node(base.offset() ^ label).sibling();