[Groonga-commit] groonga/grnxx [master] Add grnxx::alpha::map::BasicCursor.

Back to archive index

susumu.yata null+****@clear*****
Tue Apr 9 13:33:20 JST 2013


susumu.yata	2013-04-09 13:33:20 +0900 (Tue, 09 Apr 2013)

  New Revision: 78b75e084cf1940a8775084098263aaceb44d697
  https://github.com/groonga/grnxx/commit/78b75e084cf1940a8775084098263aaceb44d697

  Message:
    Add grnxx::alpha::map::BasicCursor.

  Modified files:
    lib/grnxx/alpha/map/array.cpp
    lib/grnxx/alpha/map/cursor.cpp
    lib/grnxx/alpha/map/cursor.hpp

  Modified: lib/grnxx/alpha/map/array.cpp (+2 -8)
===================================================================
--- lib/grnxx/alpha/map/array.cpp    2013-04-09 11:50:54 +0900 (87508a4)
+++ lib/grnxx/alpha/map/array.cpp    2013-04-09 13:33:20 +0900 (4ba6e4d)
@@ -241,10 +241,7 @@ void Array<T>::truncate() {
 
 template <typename T>
 MapCursor<T> *Array<T>::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<T>(
-      this, 0, header_->max_key_id, dummy_options);
+  return new (std::nothrow) BasicCursor<T>(this, options);
 }
 
 template <typename T>
@@ -445,10 +442,7 @@ 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, dummy_options);
+  return new (std::nothrow) BasicCursor<Slice>(this, options);
 }
 
 MapCursor<Slice> *Array<Slice>::open_id_cursor(

  Modified: lib/grnxx/alpha/map/cursor.cpp (+62 -0)
===================================================================
--- lib/grnxx/alpha/map/cursor.cpp    2013-04-09 11:50:54 +0900 (3fc6b8e)
+++ lib/grnxx/alpha/map/cursor.cpp    2013-04-09 13:33:20 +0900 (adbf817)
@@ -22,6 +22,68 @@ namespace alpha {
 namespace map {
 
 template <typename T>
+BasicCursor<T>::BasicCursor(Map<T> *map, const MapCursorOptions &options)
+  : MapCursor<T>(), map_(map), end_(), step_(), left_(options.limit) {
+  // TODO?
+//  if (options.flags & MAP_CURSOR_ORDER_BY_ID) {
+//  } else if (options.flags & MAP_CURSOR_ORDER_BY_KEY) {
+//  }
+
+  if (~options.flags & MAP_CURSOR_REVERSE_ORDER) {
+    this->key_id_ = -1;
+    end_ = map_->max_key_id();
+    step_ = 1;
+  } else {
+    this->key_id_ = map_->max_key_id() + 1;
+    end_ = 0;
+    step_ = -1;
+  }
+
+  uint64_t count = 0;
+  while ((count < options.offset) && (this->key_id_ != end_)) {
+    this->key_id_ += step_;
+    if (map_->get(this->key_id_)) {
+      ++count;
+    }
+  }
+}
+
+template <typename T>
+BasicCursor<T>::~BasicCursor() {}
+
+template <typename T>
+bool BasicCursor<T>::next() {
+  if (left_ == 0) {
+    return false;
+  }
+  while (this->key_id_ != end_) {
+    this->key_id_ += step_;
+    if (map_->get(this->key_id_, &this->key_)) {
+      --left_;
+      return true;
+    }
+  }
+  return false;
+}
+
+template <typename T>
+bool BasicCursor<T>::remove() {
+  return map_->unset(this->key_id_);
+}
+
+template class BasicCursor<int8_t>;
+template class BasicCursor<int16_t>;
+template class BasicCursor<int32_t>;
+template class BasicCursor<int64_t>;
+template class BasicCursor<uint8_t>;
+template class BasicCursor<uint16_t>;
+template class BasicCursor<uint32_t>;
+template class BasicCursor<uint64_t>;
+template class BasicCursor<double>;
+template class BasicCursor<GeoPoint>;
+template class BasicCursor<Slice>;
+
+template <typename T>
 IDCursor<T>::IDCursor(Map<T> *map, int64_t min, int64_t max,
                       const MapCursorOptions &options)
   : MapCursor<T>(), map_(map), end_(), step_(), left_(options.limit) {

  Modified: lib/grnxx/alpha/map/cursor.hpp (+16 -0)
===================================================================
--- lib/grnxx/alpha/map/cursor.hpp    2013-04-09 11:50:54 +0900 (4d41e2c)
+++ lib/grnxx/alpha/map/cursor.hpp    2013-04-09 13:33:20 +0900 (ee37a72)
@@ -25,6 +25,22 @@ namespace alpha {
 namespace map {
 
 template <typename T>
+class BasicCursor : public MapCursor<T> {
+ public:
+  explicit BasicCursor(Map<T> *map, const MapCursorOptions &options);
+  ~BasicCursor();
+
+  bool next();
+  bool remove();
+
+ private:
+  Map<T> *map_;
+  int64_t end_;
+  int64_t step_;
+  uint64_t left_;
+};
+
+template <typename T>
 class IDCursor : public MapCursor<T> {
  public:
   explicit IDCursor(Map<T> *map, int64_t min, int64_t max,
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index