[Groonga-commit] groonga/grnxx at 7838a50 [master] Add RecordSubset.

Back to archive index

susumu.yata null+****@clear*****
Tue Jul 29 14:53:16 JST 2014


susumu.yata	2014-07-29 14:53:16 +0900 (Tue, 29 Jul 2014)

  New Revision: 7838a50450633486fadf0fe393cd15f73ee11766
  https://github.com/groonga/grnxx/commit/7838a50450633486fadf0fe393cd15f73ee11766

  Message:
    Add RecordSubset.

  Modified files:
    include/grnxx/record.hpp
    include/grnxx/types.hpp

  Modified: include/grnxx/record.hpp (+150 -0)
===================================================================
--- include/grnxx/record.hpp    2014-07-28 16:14:35 +0900 (99af53f)
+++ include/grnxx/record.hpp    2014-07-29 14:53:16 +0900 (0480c36)
@@ -14,6 +14,69 @@ struct Record {
   Record(Int row_id, Float score) : row_id(row_id), score(score) {}
 };
 
+class RecordSubset {
+ public:
+  RecordSubset() = default;
+  RecordSubset(const RecordSubset &) = default;
+
+  RecordSubset &operator=(const RecordSubset &) = default;
+
+  // Return the number of records.
+  Int size() const {
+    return size_;
+  }
+
+  // Return the subset.
+  RecordSubset subset(Int offset = 0) const {
+    return RecordSubset(records_ + offset, size_ - offset);
+  }
+  // Return the subset.
+  RecordSubset subset(Int offset, Int size) const {
+    return RecordSubset(records_ + offset, size);
+  }
+
+  // Return the "i"-th record.
+  Record get(Int i) const {
+    return records_[i];
+  }
+  // Return the row ID of the "i"-th record.
+  Int get_row_id(Int i) const {
+    return records_[i].row_id;
+  }
+  // Return the score of the "i"-th record.
+  Float get_score(Int i) const {
+    return records_[i].score;
+  }
+
+  // Set the "i"-th record.
+  void set(Int i, Record record) {
+    records_[i] = record;
+  }
+  // Set the row ID of the "i"-th record.
+  void set_row_id(Int i, Int row_id) {
+    records_[i].row_id = row_id;
+  }
+  // Set the score of the "i"-th record.
+  void set_score(Int i, Float score) {
+    records_[i].score = score;
+  }
+
+  // Swap the "i"-th record and the "j"-th record.
+  void swap(Int i, Int j) {
+    Record temporary_record = get(i);
+    set(i, get(j));
+    set(j, temporary_record);
+  }
+
+ private:
+  Record *records_;
+  Int size_;
+
+  RecordSubset(Record *records, Int size) : records_(records), size_(size) {}
+
+  friend class RecordSet;
+};
+
 class RecordSet {
  public:
   RecordSet() : records_() {}
@@ -33,6 +96,25 @@ class RecordSet {
     return records_.push_back(error, record);
   }
 
+  // TODO: The following const_cast can be removed if RecordSet does not
+  //       use Array<Record>.
+
+  // Return a subset.
+  RecordSubset subset(Int offset = 0) const {
+    return RecordSubset(const_cast<Record *>(&records_[offset]),
+                        records_.size() - offset);
+  }
+  // Return a subset.
+  RecordSubset subset(Int offset, Int size) const {
+    return RecordSubset(const_cast<Record *>(&records_[offset]),
+                        records_.size());
+  }
+
+  // Return a subset.
+  operator RecordSubset() const {
+    return subset();
+  }
+
   // If "i" is invalid, the result of the following functions is undefined.
 
   // Return the "i"-th record.
@@ -79,6 +161,74 @@ class RecordSet {
   Array<Record> records_;
 };
 
+//class RecordSubset {
+// public:
+//  explicit RecordSubset(RecordSet *record_set)
+//      : record_set_(record_set),
+//        offset_(0),
+//        size_(record_set_->size()) {}
+//  RecordSubset(RecordSet *record_set, Int offset)
+//      : record_set_(record_set),
+//        offset_(offset),
+//        size_(record_set_->size() - offset) {}
+//  RecordSubset(RecordSet *record_set, Int offset, Int size)
+//      : record_set_(record_set),
+//        offset_(offset),
+//        size_(size) {}
+//  RecordSubset(const RecordSubset &subset) = default;
+//  RecordSubset(const RecordSubset &subset, Int offset)
+//      : record_set_(subset.record_set_),
+//        offset_(subset.offset_ + offset),
+//        size_(subset.size() - offset) {}
+//  RecordSubset(const RecordSubset &subset, Int offset, Int size)
+//      : record_set_(subset.record_set_),
+//        offset_(subset.offset_ + offset),
+//        size_(size) {}
+
+//  // Return the number of records.
+//  Int size() const {
+//    return size_;
+//  }
+
+//  // Return the "i"-th record.
+//  Record get(Int i) const {
+//    return record_set_->get(offset_ + i);
+//  }
+//  // Return the row ID of the "i"-th record.
+//  Int get_row_id(Int i) const {
+//    return record_set_->get_row_id(offset_ + i);
+//  }
+//  // Return the score of the "i"-th record.
+//  Float get_score(Int i) const {
+//    return record_set_->get_score(offset_ + i);
+//  }
+
+//  // Set the "i"-th record.
+//  void set(Int i, Record record) {
+//    record_set_->set(offset_ + i, record);
+//  }
+//  // Set the row ID of the "i"-th record.
+//  void set_row_id(Int i, Int row_id) {
+//    record_set_->set_row_id(offset_ + i, row_id);
+//  }
+//  // Set the score of the "i"-th record.
+//  void set_score(Int i, Float score) {
+//    record_set_->set_score(offset_ + i, score);
+//  }
+
+//  // Swap the "i"-th record and the "j"-th record.
+//  void swap(Int i, Int j) {
+//    Record temporary_record = get(i);
+//    set(i, get(j));
+//    set(j, temporary_record);
+//  }
+
+// private:
+//  RecordSet *record_set_;
+//  Int offset_;
+//  Int size_;
+//};
+
 }  // namespace grnxx
 
 #endif  // GRNXX_RECORD_HPP

  Modified: include/grnxx/types.hpp (+1 -0)
===================================================================
--- include/grnxx/types.hpp    2014-07-28 16:14:35 +0900 (0ffab64)
+++ include/grnxx/types.hpp    2014-07-29 14:53:16 +0900 (5eddad8)
@@ -403,6 +403,7 @@ struct SorterOptions {
 // Database temporary object types.
 class Datum;
 class Cursor;
+class RecordSubset;
 class RecordSet;
 class Expression;
 class ExpressionNode;
-------------- next part --------------
HTML����������������������������...
Download 



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