[Groonga-commit] groonga/grnxx at 6d935d7 [new_data_types] Update Text to use Int for size. (#92)

Back to archive index

susumu.yata null+****@clear*****
Mon Nov 10 11:32:29 JST 2014


susumu.yata	2014-11-10 11:32:29 +0900 (Mon, 10 Nov 2014)

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

  Message:
    Update Text to use Int for size. (#92)

  Modified files:
    include/grnxx/data_types/scalar/text.hpp
    test/test_data_types.cpp

  Modified: include/grnxx/data_types/scalar/text.hpp (+191 -49)
===================================================================
--- include/grnxx/data_types/scalar/text.hpp    2014-11-07 19:11:42 +0900 (7e0edb2)
+++ include/grnxx/data_types/scalar/text.hpp    2014-11-10 11:32:29 +0900 (59e919d)
@@ -6,6 +6,8 @@
 
 #include "grnxx/data_types/data_type.hpp"
 #include "grnxx/data_types/na.hpp"
+#include "grnxx/data_types/scalar/int.hpp"
+#include "grnxx/string.hpp"
 
 namespace grnxx {
 
@@ -18,11 +20,14 @@ class Text {
   constexpr Text(const Text &) = default;
   Text &operator=(const Text &) = default;
 
-  explicit Text(const char *str)
-      : data_(str),
-        size_(str ? std::strlen(str) : na_size()) {}
+  explicit Text(const char *string)
+      : data_(string),
+        size_(std::strlen(string)) {}
   constexpr Text(const char *data, size_t size) : data_(data), size_(size) {}
-  explicit constexpr Text(NA) : data_(na_data()), size_(na_size()) {}
+  explicit constexpr Text(const String &string)
+      : data_(string.data()),
+        size_(string.size()) {}
+  explicit constexpr Text(NA) : data_(nullptr), size_(NA()) {}
 
   const char &operator[](size_t i) const {
     return data_[i];
@@ -30,76 +35,104 @@ class Text {
   constexpr const char *data() const {
     return data_;
   }
-  constexpr size_t size() const {
+  constexpr Int size() const {
     return size_;
   }
 
   constexpr bool is_empty() const {
-    return !is_na() && (size_ == 0);
+    return size_.value() == 0;
   }
   constexpr bool is_na() const {
-    return data_ == na_data();
+    return size_.is_na();
   }
 
   Bool operator==(const Text &rhs) const {
-    if (is_na() || rhs.is_na()) {
-      return Bool::na();
-    }
-    if (size_ != rhs.size_) {
-      return Bool(false);
+    Bool has_equal_size = (size_ == rhs.size_);
+    if (has_equal_size) {
+      return Bool(std::memcmp(data_, rhs.data_, size_.value()) == 0);
     }
-    return Bool(std::memcmp(data_, rhs.data_, size_) == 0);
+    return has_equal_size;
   }
   Bool operator!=(const Text &rhs) const {
-    if (is_na() || rhs.is_na()) {
-      return Bool::na();
-    }
-    if (size_ != rhs.size_) {
-      return Bool(true);
+    Bool has_not_equal_size = (size_ != rhs.size_);
+    if (has_not_equal_size.is_false()) {
+      return Bool(std::memcmp(data_, rhs.data_, size_.value()) != 0);
     }
-    return Bool(std::memcmp(data_, rhs.data_, size_) != 0);
+    return has_not_equal_size;
   }
   Bool operator<(const Text &rhs) const {
-    if (is_na() || rhs.is_na()) {
+    Bool has_less_size = (size_ < rhs.size_);
+    if (has_less_size.is_na()) {
       return Bool::na();
     }
-    size_t min_size = size_ < rhs.size_ ? size_ : rhs.size_;
-    int result = std::memcmp(data_, rhs.data_, min_size);
-    return Bool((result < 0) || ((result == 0) && (size_ < rhs.size_)));
+    size_t min_size = has_less_size ? size_.value() : rhs.size_.value();
+    int data_result = std::memcmp(data_, rhs.data_, min_size);
+    return (data_result < 0) ? Bool(true) :
+           ((data_result == 0) ? has_less_size : Bool(false));
+
+//    if (is_na() || rhs.is_na()) {
+//      return Bool::na();
+//    }
+//    size_t min_size = size_ < rhs.size_ ? size_ : rhs.size_;
+//    int result = std::memcmp(data_, rhs.data_, min_size);
+//    return Bool((result < 0) || ((result == 0) && (size_ < rhs.size_)));
   }
   Bool operator>(const Text &rhs) const {
     return rhs < *this;
   }
   Bool operator<=(const Text &rhs) const {
-    if (is_na() || rhs.is_na()) {
+    Bool has_less_or_equal_size = (size_ <= rhs.size_);
+    if (has_less_or_equal_size.is_na()) {
       return Bool::na();
     }
-    size_t min_size = size_ < rhs.size_ ? size_ : rhs.size_;
-    int result = std::memcmp(data_, rhs.data_, min_size);
-    return Bool((result < 0) || ((result == 0) && (size_ <= rhs.size_)));
+    size_t min_size = has_less_or_equal_size ?
+                      size_.value() : rhs.size_.value();
+    int data_result = std::memcmp(data_, rhs.data_, min_size);
+    return (data_result < 0) ? Bool(true) :
+           ((data_result == 0) ? has_less_or_equal_size : Bool(false));
+
+//    if (is_na() || rhs.is_na()) {
+//      return Bool::na();
+//    }
+//    size_t min_size = size_ < rhs.size_ ? size_ : rhs.size_;
+//    int result = std::memcmp(data_, rhs.data_, min_size);
+//    return Bool((result < 0) || ((result == 0) && (size_ <= rhs.size_)));
   }
   Bool operator>=(const Text &rhs) const {
     return rhs <= *this;
   }
 
   Bool starts_with(const Text &rhs) const {
-    if (is_na() || rhs.is_na()) {
-      return Bool::na();
+    Bool has_greater_or_equal_size = (size_ >= rhs.size_);
+    if (has_greater_or_equal_size) {
+      return Bool(std::memcmp(data_, rhs.data_, rhs.size_.value()) == 0);
     }
-    if (size_ < rhs.size_) {
-      return Bool(false);
-    }
-    return Bool(std::memcmp(data_, rhs.data_, rhs.size_) == 0);
+    return has_greater_or_equal_size;
+
+//    if (is_na() || rhs.is_na()) {
+//      return Bool::na();
+//    }
+//    if (size_ < rhs.size_) {
+//      return Bool(false);
+//    }
+//    return Bool(std::memcmp(data_, rhs.data_, rhs.size_) == 0);
   }
   Bool ends_with(const Text &rhs) const {
-    if (is_na() || rhs.is_na()) {
-      return Bool::na();
+    Bool has_greater_or_equal_size = (size_ >= rhs.size_);
+    if (has_greater_or_equal_size) {
+      return Bool(std::memcmp(data_ + size_.value() - rhs.size_.value(),
+                              rhs.data_, rhs.size_.value()) == 0);
     }
-    if (size_ < rhs.size_) {
-      return Bool(false);
-    }
-    return Bool(std::memcmp(data_ + size_ - rhs.size_,
-                            rhs.data_, rhs.size_) == 0);
+    return has_greater_or_equal_size;
+
+//    if (is_na() || rhs.is_na()) {
+//      return Bool::na();
+//    }
+//    if (size_ < rhs.size_) {
+//      return Bool(false);
+//    }
+//    return Bool(std::memcmp(data_ + size_ - rhs.size_,
+//                            rhs.data_, rhs.size_) == 0);
   }
 
   static constexpr DataType type() {
@@ -107,24 +140,133 @@ class Text {
   }
 
   static constexpr Text empty() {
-    return Text("", 0);
+    return Text(nullptr, 0);
   }
   static constexpr Text na() {
     return Text(NA());
   }
 
-  static constexpr const char *na_data() {
-    return nullptr;
-  }
-  static constexpr size_t na_size() {
-    return 0;
-  }
-
  private:
   const char *data_;
-  size_t size_;
+  Int size_;
 };
 
+//// Reference to a byte string.
+//class Text {
+// public:
+//  Text() = default;
+//  ~Text() = default;
+
+//  constexpr Text(const Text &) = default;
+//  Text &operator=(const Text &) = default;
+
+//  explicit Text(const char *str)
+//      : data_(str),
+//        size_(str ? std::strlen(str) : na_size()) {}
+//  constexpr Text(const char *data, size_t size) : data_(data), size_(size) {}
+//  explicit constexpr Text(NA) : data_(na_data()), size_(na_size()) {}
+
+//  const char &operator[](size_t i) const {
+//    return data_[i];
+//  }
+//  constexpr const char *data() const {
+//    return data_;
+//  }
+//  constexpr size_t size() const {
+//    return size_;
+//  }
+
+//  constexpr bool is_empty() const {
+//    return !is_na() && (size_ == 0);
+//  }
+//  constexpr bool is_na() const {
+//    return data_ == na_data();
+//  }
+
+//  Bool operator==(const Text &rhs) const {
+//    if (is_na() || rhs.is_na()) {
+//      return Bool::na();
+//    }
+//    if (size_ != rhs.size_) {
+//      return Bool(false);
+//    }
+//    return Bool(std::memcmp(data_, rhs.data_, size_) == 0);
+//  }
+//  Bool operator!=(const Text &rhs) const {
+//    if (is_na() || rhs.is_na()) {
+//      return Bool::na();
+//    }
+//    if (size_ != rhs.size_) {
+//      return Bool(true);
+//    }
+//    return Bool(std::memcmp(data_, rhs.data_, size_) != 0);
+//  }
+//  Bool operator<(const Text &rhs) const {
+//    if (is_na() || rhs.is_na()) {
+//      return Bool::na();
+//    }
+//    size_t min_size = size_ < rhs.size_ ? size_ : rhs.size_;
+//    int result = std::memcmp(data_, rhs.data_, min_size);
+//    return Bool((result < 0) || ((result == 0) && (size_ < rhs.size_)));
+//  }
+//  Bool operator>(const Text &rhs) const {
+//    return rhs < *this;
+//  }
+//  Bool operator<=(const Text &rhs) const {
+//    if (is_na() || rhs.is_na()) {
+//      return Bool::na();
+//    }
+//    size_t min_size = size_ < rhs.size_ ? size_ : rhs.size_;
+//    int result = std::memcmp(data_, rhs.data_, min_size);
+//    return Bool((result < 0) || ((result == 0) && (size_ <= rhs.size_)));
+//  }
+//  Bool operator>=(const Text &rhs) const {
+//    return rhs <= *this;
+//  }
+
+//  Bool starts_with(const Text &rhs) const {
+//    if (is_na() || rhs.is_na()) {
+//      return Bool::na();
+//    }
+//    if (size_ < rhs.size_) {
+//      return Bool(false);
+//    }
+//    return Bool(std::memcmp(data_, rhs.data_, rhs.size_) == 0);
+//  }
+//  Bool ends_with(const Text &rhs) const {
+//    if (is_na() || rhs.is_na()) {
+//      return Bool::na();
+//    }
+//    if (size_ < rhs.size_) {
+//      return Bool(false);
+//    }
+//    return Bool(std::memcmp(data_ + size_ - rhs.size_,
+//                            rhs.data_, rhs.size_) == 0);
+//  }
+
+//  static constexpr DataType type() {
+//    return TEXT_DATA;
+//  }
+
+//  static constexpr Text empty() {
+//    return Text("", 0);
+//  }
+//  static constexpr Text na() {
+//    return Text(NA());
+//  }
+
+//  static constexpr const char *na_data() {
+//    return nullptr;
+//  }
+//  static constexpr size_t na_size() {
+//    return 0;
+//  }
+
+// private:
+//  const char *data_;
+//  size_t size_;
+//};
+
 }  // namespace grnxx
 
 #endif   // GRNXX_DATA_TYPES_SCALAR_TEXT_HPP

  Modified: test/test_data_types.cpp (+186 -0)
===================================================================
--- test/test_data_types.cpp    2014-11-07 19:11:42 +0900 (f17362f)
+++ test/test_data_types.cpp    2014-11-10 11:32:29 +0900 (1ab4a39)
@@ -17,6 +17,7 @@
 */
 #include <cassert>
 #include <cmath>
+#include <cstring>
 #include <iostream>
 
 #include "grnxx/data_types.hpp"
@@ -814,10 +815,195 @@ void test_geo_point() {
   assert((na != na).is_na());
 }
 
+void test_text() {
+  grnxx::Text ab("ab");
+  grnxx::Text abc("abc", 3);
+  grnxx::Text bc(grnxx::String("bc"));
+  grnxx::Text empty = grnxx::Text::empty();
+  grnxx::Text na = grnxx::Text::na();
+
+  assert(ab.type() == grnxx::TEXT_DATA);
+  assert(abc.type() == grnxx::TEXT_DATA);
+  assert(bc.type() == grnxx::TEXT_DATA);
+  assert(empty.type() == grnxx::TEXT_DATA);
+  assert(na.type() == grnxx::TEXT_DATA);
+
+  assert(std::strcmp(ab.data(), "ab") == 0);
+  assert(std::strcmp(abc.data(), "abc") == 0);
+  assert(std::strcmp(bc.data(), "bc") == 0);
+
+  assert(ab.size() == grnxx::Int(2));
+  assert(abc.size() == grnxx::Int(3));
+  assert(bc.size() == grnxx::Int(2));
+  assert(empty.size() == grnxx::Int(0));
+  assert(na.size().is_na());
+
+  assert(!ab.is_empty());
+  assert(!abc.is_empty());
+  assert(!bc.is_empty());
+  assert(empty.is_empty());
+  assert(!na.is_empty());
+
+  assert(!ab.is_na());
+  assert(!abc.is_na());
+  assert(!bc.is_na());
+  assert(!empty.is_na());
+  assert(na.is_na());
+
+  assert((ab == ab).is_true());
+  assert((ab == abc).is_false());
+  assert((ab == bc).is_false());
+  assert((ab == empty).is_false());
+  assert((ab == na).is_na());
+  assert((abc == abc).is_true());
+  assert((abc == bc).is_false());
+  assert((abc == empty).is_false());
+  assert((abc == na).is_na());
+  assert((bc == bc).is_true());
+  assert((bc == empty).is_false());
+  assert((bc == na).is_na());
+  assert((empty == empty).is_true());
+  assert((empty == na).is_na());
+  assert((na == na).is_na());
+
+  assert((ab != ab).is_false());
+  assert((ab != abc).is_true());
+  assert((ab != bc).is_true());
+  assert((ab != empty).is_true());
+  assert((ab != na).is_na());
+  assert((abc != abc).is_false());
+  assert((abc != bc).is_true());
+  assert((abc != empty).is_true());
+  assert((abc != na).is_na());
+  assert((bc != bc).is_false());
+  assert((bc != empty).is_true());
+  assert((bc != na).is_na());
+  assert((empty != empty).is_false());
+  assert((empty != na).is_na());
+  assert((na != na).is_na());
+
+  assert((ab < ab).is_false());
+  assert((ab < abc).is_true());
+  assert((ab < bc).is_true());
+  assert((ab < empty).is_false());
+  assert((ab < na).is_na());
+  assert((abc < abc).is_false());
+  assert((abc < bc).is_true());
+  assert((abc < empty).is_false());
+  assert((abc < na).is_na());
+  assert((bc < bc).is_false());
+  assert((bc < empty).is_false());
+  assert((bc < na).is_na());
+  assert((empty < empty).is_false());
+  assert((empty < na).is_na());
+  assert((na < na).is_na());
+
+  assert((ab > ab).is_false());
+  assert((ab > abc).is_false());
+  assert((ab > bc).is_false());
+  assert((ab > empty).is_true());
+  assert((ab > na).is_na());
+  assert((abc > abc).is_false());
+  assert((abc > bc).is_false());
+  assert((abc > empty).is_true());
+  assert((abc > na).is_na());
+  assert((bc > bc).is_false());
+  assert((bc > empty).is_true());
+  assert((bc > na).is_na());
+  assert((empty > empty).is_false());
+  assert((empty > na).is_na());
+  assert((na > na).is_na());
+
+  assert((ab <= ab).is_true());
+  assert((ab <= abc).is_true());
+  assert((ab <= bc).is_true());
+  assert((ab <= empty).is_false());
+  assert((ab <= na).is_na());
+  assert((abc <= abc).is_true());
+  assert((abc <= bc).is_true());
+  assert((abc <= empty).is_false());
+  assert((abc <= na).is_na());
+  assert((bc <= bc).is_true());
+  assert((bc <= empty).is_false());
+  assert((bc <= na).is_na());
+  assert((empty <= empty).is_true());
+  assert((empty <= na).is_na());
+  assert((na <= na).is_na());
+
+  assert((ab >= ab).is_true());
+  assert((ab >= abc).is_false());
+  assert((ab >= bc).is_false());
+  assert((ab >= empty).is_true());
+  assert((ab >= na).is_na());
+  assert((abc >= abc).is_true());
+  assert((abc >= bc).is_false());
+  assert((abc >= empty).is_true());
+  assert((abc >= na).is_na());
+  assert((bc >= bc).is_true());
+  assert((bc >= empty).is_true());
+  assert((bc >= na).is_na());
+  assert((empty >= empty).is_true());
+  assert((empty >= na).is_na());
+  assert((na >= na).is_na());
+
+  assert((ab.starts_with(ab)).is_true());
+  assert((ab.starts_with(abc)).is_false());
+  assert((ab.starts_with(bc)).is_false());
+  assert((ab.starts_with(empty)).is_true());
+  assert((ab.starts_with(na)).is_na());
+  assert((abc.starts_with(ab)).is_true());
+  assert((abc.starts_with(abc)).is_true());
+  assert((abc.starts_with(bc)).is_false());
+  assert((abc.starts_with(empty)).is_true());
+  assert((abc.starts_with(na)).is_na());
+  assert((bc.starts_with(ab)).is_false());
+  assert((bc.starts_with(abc)).is_false());
+  assert((bc.starts_with(bc)).is_true());
+  assert((bc.starts_with(empty)).is_true());
+  assert((bc.starts_with(na)).is_na());
+  assert((empty.starts_with(ab)).is_false());
+  assert((empty.starts_with(abc)).is_false());
+  assert((empty.starts_with(bc)).is_false());
+  assert((empty.starts_with(empty)).is_true());
+  assert((empty.starts_with(na)).is_na());
+  assert((na.starts_with(ab)).is_na());
+  assert((na.starts_with(abc)).is_na());
+  assert((na.starts_with(bc)).is_na());
+  assert((na.starts_with(empty)).is_na());
+  assert((na.starts_with(na)).is_na());
+
+  assert((ab.ends_with(ab)).is_true());
+  assert((ab.ends_with(abc)).is_false());
+  assert((ab.ends_with(bc)).is_false());
+  assert((ab.ends_with(empty)).is_true());
+  assert((ab.ends_with(na)).is_na());
+  assert((abc.ends_with(ab)).is_false());
+  assert((abc.ends_with(abc)).is_true());
+  assert((abc.ends_with(bc)).is_true());
+  assert((abc.ends_with(empty)).is_true());
+  assert((abc.ends_with(na)).is_na());
+  assert((bc.ends_with(ab)).is_false());
+  assert((bc.ends_with(abc)).is_false());
+  assert((bc.ends_with(bc)).is_true());
+  assert((bc.ends_with(empty)).is_true());
+  assert((bc.ends_with(na)).is_na());
+  assert((empty.ends_with(ab)).is_false());
+  assert((empty.ends_with(abc)).is_false());
+  assert((empty.ends_with(bc)).is_false());
+  assert((empty.ends_with(empty)).is_true());
+  assert((empty.ends_with(na)).is_na());
+  assert((na.ends_with(ab)).is_na());
+  assert((na.ends_with(abc)).is_na());
+  assert((na.ends_with(bc)).is_na());
+  assert((na.ends_with(empty)).is_na());
+  assert((na.ends_with(na)).is_na());
+}
+
 int main() {
   test_bool();
   test_int();
   test_float();
   test_geo_point();
+  test_text();
   return 0;
 }
-------------- next part --------------
HTML����������������������������...
下載 



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