[Groonga-commit] groonga/grnxx at 6e5ac9f [master] Improve the new Bool.

Back to archive index

susumu.yata null+****@clear*****
Thu Oct 23 17:12:59 JST 2014


susumu.yata	2014-10-23 17:12:59 +0900 (Thu, 23 Oct 2014)

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

  Message:
    Improve the new Bool.

  Modified files:
    include/grnxx/new_types/bool.hpp

  Modified: include/grnxx/new_types/bool.hpp (+16 -20)
===================================================================
--- include/grnxx/new_types/bool.hpp    2014-10-22 18:16:13 +0900 (141a92b)
+++ include/grnxx/new_types/bool.hpp    2014-10-23 17:12:59 +0900 (f1583db)
@@ -15,7 +15,8 @@ class Bool {
   constexpr Bool(const Bool &) = default;
   Bool &operator=(const Bool &) & = default;
 
-  explicit constexpr Bool(bool value) : value_(value) {}
+  explicit constexpr Bool(bool value)
+      : value_(value ? true_value() : false_value()) {}
   explicit constexpr Bool(NA) : value_(na_value()) {}
 
   constexpr uint8_t value() const {
@@ -39,7 +40,7 @@ class Bool {
   // -- Unary operators --
 
   constexpr Bool operator!() const {
-    return is_na() ? na() : Bool(!value_);
+    return is_na() ? na() : Bool(static_cast<uint8_t>(value_ ^ true_value()));
   }
   constexpr Bool operator~() const {
     return operator!();
@@ -47,24 +48,15 @@ class Bool {
 
   // -- Binary operators --
 
-  // TODO: Difficult branch predictions (for is_false()) should be removed.
-  constexpr Bool operator&&(Bool rhs) const {
-    return (is_false() || rhs.is_false()) ? Bool(false) :
-           ((is_na() || rhs.is_na()) ? na() : Bool(true));
-  }
-  constexpr Bool operator||(Bool rhs) const {
-    return (is_true() || rhs.is_true()) ? Bool(true) :
-           ((is_na() || rhs.is_na()) ? na() : Bool(false));
-  }
-
   constexpr Bool operator&(Bool rhs) const {
-    return operator&&(rhs);
+    return Bool(static_cast<uint8_t>(value_ & rhs.value_));
   }
   constexpr Bool operator|(Bool rhs) const {
-    return operator||(rhs);
+    return Bool(static_cast<uint8_t>(value_ | rhs.value_));
   }
   constexpr Bool operator^(Bool rhs) const {
-    return (is_na() || rhs.is_na()) ? na() : Bool(value_ ^ rhs.value_);
+    return (is_na() || rhs.is_na()) ? na() :
+           Bool(static_cast<uint8_t>(value_ ^ rhs.value_));
   }
 
   Bool &operator&=(Bool rhs) & {
@@ -80,10 +72,12 @@ class Bool {
   // -- Comparison operators --
 
   constexpr Bool operator==(Bool rhs) const {
-    return (is_na() || rhs.is_na()) ? na() : Bool(value_ == rhs.value_);
+    return (is_na() || rhs.is_na()) ? na() :
+           Bool(static_cast<uint8_t>(value_ ^ rhs.value_ ^ true_value()));
   }
   constexpr Bool operator!=(Bool rhs) const {
-    return (is_na() || rhs.is_na()) ? na() : Bool(value_ != rhs.value_);
+    return (is_na() || rhs.is_na()) ? na() :
+           Bool(static_cast<uint8_t>(value_ ^ rhs.value_));
   }
 
   static constexpr Bool na() {
@@ -91,17 +85,19 @@ class Bool {
   }
 
   static constexpr uint8_t true_value() {
-    return 1;
+    return 0b11;
   }
   static constexpr uint8_t false_value() {
-    return 0;
+    return 0b00;
   }
   static constexpr uint8_t na_value() {
-    return 2;
+    return 0b01;
   }
 
  private:
   uint8_t value_;
+
+  explicit constexpr Bool(uint8_t value) : value_(value) {}
 };
 
 }  // namespace grnxx
-------------- next part --------------
HTML����������������������������...
Download 



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