[Groonga-commit] groonga/grnxx at 6a4cfbe [new_data_types] Add Vector<Text>. (#109)

アーカイブの一覧に戻る

susumu.yata null+****@clear*****
Tue Nov 18 16:38:22 JST 2014


susumu.yata	2014-11-18 16:38:22 +0900 (Tue, 18 Nov 2014)

  New Revision: 6a4cfbee7e81308a192af621181fa7397532494c
  https://github.com/groonga/grnxx/commit/6a4cfbee7e81308a192af621181fa7397532494c

  Message:
    Add Vector<Text>. (#109)

  Modified files:
    include/grnxx/data_types/vector/text.hpp

  Modified: include/grnxx/data_types/vector/text.hpp (+105 -1)
===================================================================
--- include/grnxx/data_types/vector/text.hpp    2014-11-18 11:54:17 +0900 (f0dd678)
+++ include/grnxx/data_types/vector/text.hpp    2014-11-18 16:38:22 +0900 (2971622)
@@ -12,10 +12,114 @@ template <typename T> class Vector;
 template <>
 class Vector<Text> {
  public:
-  // TODO
+  Vector() = default;
+  ~Vector() = default;
+
+  constexpr Vector(const Vector &) = default;
+  Vector &operator=(const Vector &) = default;
+
+  constexpr Vector(const Text *data, size_t size)
+      : is_direct_(true),
+        size_(size),
+        data_(data) {}
+  Vector(const void *headers, const char *bodies, Int size)
+      : is_direct_(false),
+        size_(size),
+        headers_(static_cast<const Header *>(headers)),
+        bodies_(bodies) {}
+  explicit constexpr Vector(NA)
+      : is_direct_(true),
+        size_(NA()),
+        data_(nullptr) {}
+
+  Text operator[](Int i) const {
+    if (is_na() || (static_cast<uint64_t>(i.value()) >=
+                    static_cast<uint64_t>(size_.value()))) {
+      return Text::na();
+    }
+    if (is_direct_) {
+      return data_[i.value()];
+    } else {
+      return Text(&bodies_[headers_[i.value()].offset],
+                  headers_[i.value()].size.value());
+    }
+  }
+  // TODO: To be removed.
+  const Text &operator[](size_t i) const {
+    return data_[i];
+  }
+  constexpr Int size() const {
+    return size_;
+  }
+
+  constexpr bool is_empty() const {
+    return size_.value() == 0;
+  }
+  constexpr bool is_na() const {
+    return size_.is_na();
+  }
+
+  // TODO: The behavior of N/A in vector is not fixed yet (#107).
+  Bool operator==(const Vector &rhs) const {
+    Bool has_equal_size = (size_ == rhs.size_);
+    if (has_equal_size.is_true()) {
+      size_t size = size_.value();
+      for (size_t i = 0; i < size; ++i) {
+        Text lhs_text = (*this)[grnxx::Int(i)];
+        Text rhs_text = rhs[grnxx::Int(i)];
+        // TODO: Binary not equal should be used.
+        if (!(lhs_text.is_na() && rhs_text.is_na()) &&
+            (!(lhs_text == rhs_text)).is_true()) {
+          return Bool(false);
+        }
+      }
+    }
+    return has_equal_size;
+  }
+  // TODO: The behavior of N/A in vector is not fixed yet (#107).
+  Bool operator!=(const Vector &rhs) const {
+    Bool has_not_equal_size = (size_ != rhs.size_);
+    if (has_not_equal_size.is_false()) {
+      size_t size = size_.value();
+      for (size_t i = 0; i < size; ++i) {
+        Text lhs_text = (*this)[grnxx::Int(i)];
+        Text rhs_text = rhs[grnxx::Int(i)];
+        // TODO: Binary not equal should be used.
+        if (!(lhs_text.is_na() && rhs_text.is_na()) &&
+            (!(lhs_text == rhs_text)).is_true()) {
+          return Bool(true);
+        }
+      }
+    }
+    return has_not_equal_size;
+  }
+
   static constexpr DataType type() {
     return TEXT_VECTOR_DATA;
   }
+
+  static constexpr Vector empty() {
+    return Vector(nullptr, 0);
+  }
+  static constexpr Vector na() {
+    return Vector(NA());
+  }
+
+ private:
+  struct Header {
+    size_t offset;
+    Int size;
+  };
+
+  bool is_direct_;
+  Int size_;
+  union {
+    const Text *data_;
+    struct {
+      const Header *headers_;
+      const char *bodies_;
+    };
+  };
 };
 
 using TextVector = Vector<Text>;
-------------- next part --------------
HTML����������������������������...
ダウンロード 



More information about the Groonga-commit mailing list
アーカイブの一覧に戻る