[Groonga-commit] groonga/grnxx at f635416 [master] Enable arithmetic operators. (#106)

Zurück zum Archiv-Index

susumu.yata null+****@clear*****
Tue Dec 16 11:03:03 JST 2014


susumu.yata	2014-11-14 12:23:13 +0900 (Fri, 14 Nov 2014)

  New Revision: f6354164d95afde8f48974e74976005017b2dc26
  https://github.com/groonga/grnxx/commit/f6354164d95afde8f48974e74976005017b2dc26

  Message:
    Enable arithmetic operators. (#106)

  Modified files:
    lib/grnxx/impl/expression.cpp
    lib/grnxx/impl/expression.hpp

  Modified: lib/grnxx/impl/expression.cpp (+125 -28)
===================================================================
--- lib/grnxx/impl/expression.cpp    2014-11-14 12:17:25 +0900 (63cb553)
+++ lib/grnxx/impl/expression.cpp    2014-11-14 12:23:13 +0900 (d6b9fd5)
@@ -1006,7 +1006,7 @@ void GenericBinaryNode<T, Float, V, W>::adjust(ArrayRef<Record> records) {
   this->fill_arg1_values(records);
   this->fill_arg2_values(records);
   for (size_t i = 0; i < records.size(); ++i) {
-    records[i].score = operator_(this->arg_values_[i], this->arg2_values_[i]);
+    records[i].score = operator_(this->arg1_values_[i], this->arg2_values_[i]);
   }
 }
 
@@ -1145,6 +1145,81 @@ struct BitwiseXorOperator {
 template <typename T>
 using BitwiseXorNode = GenericBinaryNode<BitwiseXorOperator<T>>;
 
+// ----- PlusNode -----
+
+template <typename T>
+struct PlusOperator {
+  using Value = T;
+  using Arg1 = T;
+  using Arg2 = T;
+  Value operator()(Arg1 arg1, Arg2 arg2) const {
+    return arg1 + arg2;
+  }
+};
+
+template <typename T>
+using PlusNode = GenericBinaryNode<PlusOperator<T>>;
+
+// ----- MinusNode -----
+
+template <typename T>
+struct MinusOperator {
+  using Value = T;
+  using Arg1 = T;
+  using Arg2 = T;
+  Value operator()(Arg1 arg1, Arg2 arg2) const {
+    return arg1 - arg2;
+  }
+};
+
+template <typename T>
+using MinusNode = GenericBinaryNode<MinusOperator<T>>;
+
+// ----- MultiplicationNode -----
+
+template <typename T>
+struct MultiplicationOperator {
+  using Value = T;
+  using Arg1 = T;
+  using Arg2 = T;
+  Value operator()(Arg1 arg1, Arg2 arg2) const {
+    return arg1 * arg2;
+  }
+};
+
+template <typename T>
+using MultiplicationNode = GenericBinaryNode<MultiplicationOperator<T>>;
+
+// ----- DivisionNode -----
+
+template <typename T>
+struct DivisionOperator {
+  using Value = T;
+  using Arg1 = T;
+  using Arg2 = T;
+  Value operator()(Arg1 arg1, Arg2 arg2) const {
+    return arg1 / arg2;
+  }
+};
+
+template <typename T>
+using DivisionNode = GenericBinaryNode<DivisionOperator<T>>;
+
+// ----- ModulusNode -----
+
+template <typename T>
+struct ModulusOperator {
+  using Value = T;
+  using Arg1 = T;
+  using Arg2 = T;
+  Value operator()(Arg1 arg1, Arg2 arg2) const {
+    return arg1 % arg2;
+  }
+};
+
+template <typename T>
+using ModulusNode = GenericBinaryNode<ModulusOperator<T>>;
+
 }  // namespace expression
 
 using namespace expression;
@@ -1744,33 +1819,25 @@ Node *ExpressionBuilder::create_binary_node(
         }
       }
     }
-//    case PLUS_OPERATOR:
-//    case MINUS_OPERATOR:
-//    case MULTIPLICATION_OPERATOR:
-//    case DIVISION_OPERATOR: {
-//      switch (arg1->data_type()) {
-//        case INT_DATA: {
-//          return create_arithmetic_node<Int>(
-//              error, operator_type, std::move(arg1), std::move(arg2));
-//        }
-//        case FLOAT_DATA: {
-//          return create_arithmetic_node<Float>(
-//              error, operator_type, std::move(arg1), std::move(arg2));
-//        }
-//        default: {
-//          GRNXX_ERROR_SET(error, INVALID_OPERAND, "Invalid data type");
-//          return nullptr;
-//        }
-//      }
-//    }
-//    case MODULUS_OPERATOR: {
-//      if ((arg1->data_type() != INT_DATA) ||
-//          (arg2->data_type() != INT_DATA)) {
-//        GRNXX_ERROR_SET(error, INVALID_OPERAND, "Invalid data type");
-//        return nullptr;
-//      }
-//      return ModulusNode<Int>::create(error, std::move(arg1), std::move(arg2));
-//    }
+    case PLUS_OPERATOR:
+    case MINUS_OPERATOR:
+    case MULTIPLICATION_OPERATOR:
+    case DIVISION_OPERATOR:
+    case MODULUS_OPERATOR: {
+      switch (arg1->data_type()) {
+        case INT_DATA: {
+          return create_arithmetic_node<Int>(
+              operator_type, std::move(arg1), std::move(arg2));
+        }
+        case FLOAT_DATA: {
+          return create_arithmetic_node<Float>(
+              operator_type, std::move(arg1), std::move(arg2));
+        }
+        default: {
+          throw "Invalid data type";  // TODO
+        }
+      }
+    }
 //    case SUBSCRIPT_OPERATOR: {
 //      return create_subscript_node(error, std::move(arg1), std::move(arg2));
 //    }
@@ -1855,5 +1922,35 @@ Node *ExpressionBuilder::create_bitwise_binary_node(
   }
 }
 
+template <typename T>
+Node *ExpressionBuilder::create_arithmetic_node(
+    OperatorType operator_type,
+    std::unique_ptr<Node> &&arg1,
+    std::unique_ptr<Node> &&arg2) {
+  if (arg1->data_type() != arg2->data_type()) {
+    throw "Data type conflict";  // TODO
+  }
+  switch (operator_type) {
+    case PLUS_OPERATOR: {
+      return new PlusNode<T>(std::move(arg1), std::move(arg2));
+    }
+    case MINUS_OPERATOR: {
+      return new MinusNode<T>(std::move(arg1), std::move(arg2));
+    }
+    case MULTIPLICATION_OPERATOR: {
+      return new MultiplicationNode<T>(std::move(arg1), std::move(arg2));
+    }
+    case DIVISION_OPERATOR: {
+      return new DivisionNode<T>(std::move(arg1), std::move(arg2));
+    }
+    case MODULUS_OPERATOR: {
+      return new ModulusNode<T>(std::move(arg1), std::move(arg2));
+    }
+    default: {
+      throw "Invalid operator";  // TODO
+    }
+  }
+}
+
 }  // namespace impl
 }  // namespace grnxx

  Modified: lib/grnxx/impl/expression.hpp (+8 -0)
===================================================================
--- lib/grnxx/impl/expression.hpp    2014-11-14 12:17:25 +0900 (d0d9e17)
+++ lib/grnxx/impl/expression.hpp    2014-11-14 12:23:13 +0900 (bb05995)
@@ -161,6 +161,14 @@ class ExpressionBuilder : public ExpressionBuilderInterface {
   Node *create_bitwise_binary_node(OperatorType operator_type,
                                    std::unique_ptr<Node> &&arg1,
                                    std::unique_ptr<Node> &&arg2);
+
+  // Create a node associated with an arithmetic operator.
+  //
+  // On failure, throws an exception.
+  template <typename T>
+  Node *create_arithmetic_node(OperatorType operator_type,
+                               std::unique_ptr<Node> &&arg1,
+                               std::unique_ptr<Node> &&arg2);
 };
 
 }  // namespace impl
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Zurück zum Archiv-Index