susumu.yata
null+****@clear*****
Tue Apr 9 11:50:54 JST 2013
susumu.yata 2013-04-09 11:50:54 +0900 (Tue, 09 Apr 2013) New Revision: 9b55b74e24a1b601af2afaecdc71845da82fbcdb https://github.com/groonga/grnxx/commit/9b55b74e24a1b601af2afaecdc71845da82fbcdb Message: Add new cursor flags MAP_CURSOR_EXCEPT_MIN and MAP_CURSOR_EXCEPT_MAX. Modified files: lib/grnxx/alpha/map.hpp lib/grnxx/alpha/map/array.cpp lib/grnxx/alpha/map/cursor.cpp lib/grnxx/alpha/map/cursor.hpp test/test_alpha_map.cpp Modified: lib/grnxx/alpha/map.hpp (+9 -3) =================================================================== --- lib/grnxx/alpha/map.hpp 2013-04-09 11:17:38 +0900 (211291c) +++ lib/grnxx/alpha/map.hpp 2013-04-09 11:50:54 +0900 (fc74938) @@ -83,13 +83,19 @@ typedef FlagsImpl<MapCursorFlagsIdentifier> MapCursorFlags; // Sort keys by ID. constexpr MapCursorFlags MAP_CURSOR_ORDER_BY_ID = - MapCursorFlags::define(0x01); + MapCursorFlags::define(0x001); // Sort keys by key. constexpr MapCursorFlags MAP_CURSOR_ORDER_BY_KEY = - MapCursorFlags::define(0x02); + MapCursorFlags::define(0x002); // Access keys in reverse order. constexpr MapCursorFlags MAP_CURSOR_REVERSE_ORDER = - MapCursorFlags::define(0x10); + MapCursorFlags::define(0x010); +// Return keys except min. +constexpr MapCursorFlags MAP_CURSOR_EXCEPT_MIN = + MapCursorFlags::define(0x100); +// Return keys except max. +constexpr MapCursorFlags MAP_CURSOR_EXCEPT_MAX = + MapCursorFlags::define(0x200); struct MapCursorOptions { MapCursorFlags flags; Modified: lib/grnxx/alpha/map/array.cpp (+7 -2) =================================================================== --- lib/grnxx/alpha/map/array.cpp 2013-04-09 11:17:38 +0900 (c577779) +++ lib/grnxx/alpha/map/array.cpp 2013-04-09 11:50:54 +0900 (87508a4) @@ -241,7 +241,10 @@ void Array<T>::truncate() { template <typename T> MapCursor<T> *Array<T>::open_basic_cursor(const MapCursorOptions &options) { - return new (std::nothrow) IDCursor<T>(this, 0, header_->max_key_id, options); + MapCursorOptions dummy_options = options; + dummy_options.flags &= ~(MAP_CURSOR_EXCEPT_MIN | MAP_CURSOR_EXCEPT_MAX); + return new (std::nothrow) IDCursor<T>( + this, 0, header_->max_key_id, dummy_options); } template <typename T> @@ -442,8 +445,10 @@ void Array<Slice>::truncate() { MapCursor<Slice> *Array<Slice>::open_basic_cursor( const MapCursorOptions &options) { + MapCursorOptions dummy_options = options; + dummy_options.flags &= ~(MAP_CURSOR_EXCEPT_MIN | MAP_CURSOR_EXCEPT_MAX); return new (std::nothrow) IDCursor<Slice>( - this, 0, header_->max_key_id, options); + this, 0, header_->max_key_id, dummy_options); } MapCursor<Slice> *Array<Slice>::open_id_cursor( Modified: lib/grnxx/alpha/map/cursor.cpp (+28 -5) =================================================================== --- lib/grnxx/alpha/map/cursor.cpp 2013-04-09 11:17:38 +0900 (dd1583b) +++ lib/grnxx/alpha/map/cursor.cpp 2013-04-09 11:50:54 +0900 (3fc6b8e) @@ -32,13 +32,15 @@ IDCursor<T>::IDCursor(Map<T> *map, int64_t min, int64_t max, if (min < 0) { min = 0; + } else if (options.flags & MAP_CURSOR_EXCEPT_MIN) { + ++min; } - // EXCEPT_MIN if (max > map_->max_key_id()) { max = map_->max_key_id(); + } else if (options.flags & MAP_CURSOR_EXCEPT_MAX) { + --max; } - // EXCEPT_MAX if (~options.flags & MAP_CURSOR_REVERSE_ORDER) { this->key_id_ = min - 1; @@ -98,7 +100,7 @@ template <typename T> KeyCursor<T>::KeyCursor(Map<T> *map, T min, T max, const MapCursorOptions &options) : MapCursor<T>(), map_(map), min_(min), max_(max), end_(), step_(), - left_(options.limit) { + left_(options.limit), flags_(options.flags) { // TODO? // if (options.flags & MAP_CURSOR_ORDER_BY_ID) { // } else if (options.flags & MAP_CURSOR_ORDER_BY_KEY) { @@ -118,7 +120,7 @@ KeyCursor<T>::KeyCursor(Map<T> *map, T min, T max, while ((count < options.offset) && (this->key_id_ != end_)) { this->key_id_ += step_; if (map_->get(this->key_id_, &this->key_)) { - if ((this->key_ >= min_) && (this->key_ <= max_)) { + if (in_range(this->key_)) { ++count; } } @@ -136,7 +138,7 @@ bool KeyCursor<T>::next() { while (this->key_id_ != end_) { this->key_id_ += step_; if (map_->get(this->key_id_, &this->key_)) { - if ((this->key_ >= min_) && (this->key_ <= max_)) { + if (in_range(this->key_)) { --left_; return true; } @@ -150,6 +152,27 @@ bool KeyCursor<T>::remove() { return map_->unset(this->key_id_); } +template <typename T> +bool KeyCursor<T>::in_range(T key) const { + if (flags_ & MAP_CURSOR_EXCEPT_MIN) { + if (key <= min_) { + return false; + } + } else if (key < min_) { + return false; + } + + if (flags_ & MAP_CURSOR_EXCEPT_MAX) { + if (key >= max_) { + return false; + } + } else if (key > max_) { + return false; + } + + return true; +} + template class KeyCursor<int8_t>; template class KeyCursor<int16_t>; template class KeyCursor<int32_t>; Modified: lib/grnxx/alpha/map/cursor.hpp (+2 -0) =================================================================== --- lib/grnxx/alpha/map/cursor.hpp 2013-04-09 11:17:38 +0900 (c2b3748) +++ lib/grnxx/alpha/map/cursor.hpp 2013-04-09 11:50:54 +0900 (4d41e2c) @@ -59,6 +59,8 @@ class KeyCursor : public MapCursor<T> { int64_t step_; uint64_t left_; MapCursorFlags flags_; + + bool in_range(T key) const; }; } // namespace map Modified: test/test_alpha_map.cpp (+31 -0) =================================================================== --- test/test_alpha_map.cpp 2013-04-09 11:17:38 +0900 (042bea5) +++ test/test_alpha_map.cpp 2013-04-09 11:50:54 +0900 (c1e76ca) @@ -119,6 +119,15 @@ void test_basic_cursor(const std::unique_ptr<grnxx::alpha::Map<T>> &map, assert(cursor->next()); } assert(!cursor->next()); + + grnxx::alpha::MapCursorOptions options; + options.flags |= grnxx::alpha::MAP_CURSOR_EXCEPT_MIN | + grnxx::alpha::MAP_CURSOR_EXCEPT_MAX; + cursor.reset(map->open_basic_cursor(options)); + for (std::size_t i = 0; i < MAP_SIZE; ++i) { + assert(cursor->next()); + } + assert(!cursor->next()); } template <typename T> @@ -139,6 +148,18 @@ void test_id_cursor(const std::unique_ptr<grnxx::alpha::Map<T>> &map, assert(cursor->key() == key); } assert(!cursor->next()); + + options.flags |= grnxx::alpha::MAP_CURSOR_EXCEPT_MIN | + grnxx::alpha::MAP_CURSOR_EXCEPT_MAX; + cursor.reset(map->open_id_cursor(MIN_ID, MAX_ID, options)); + for (std::int64_t i = MIN_ID + 1; i <= (MAX_ID - 1); ++i) { + assert(cursor->next()); + assert(cursor->key_id() == i); + T key; + assert(map->get(i, &key)); + assert(cursor->key() == key); + } + assert(!cursor->next()); } template <typename T> @@ -157,6 +178,16 @@ void test_key_cursor(const std::unique_ptr<grnxx::alpha::Map<T>> &map) { assert(cursor->key() <= max_key); } assert(!cursor->next()); + + grnxx::alpha::MapCursorOptions options; + options.flags |= grnxx::alpha::MAP_CURSOR_EXCEPT_MIN | + grnxx::alpha::MAP_CURSOR_EXCEPT_MAX; + cursor.reset(map->open_key_cursor(min_key, max_key, options)); + while (cursor->next()) { + assert(cursor->key() > min_key); + assert(cursor->key() < max_key); + } + assert(!cursor->next()); } void test_key_cursor( -------------- next part -------------- HTML����������������������������... Télécharger