[Groonga-commit] groonga/groonga at be4306c [master] grn_ts: use macros to remove redundant code

Back to archive index

susumu.yata null+****@clear*****
Fri Oct 9 17:38:01 JST 2015


susumu.yata	2015-10-09 17:38:01 +0900 (Fri, 09 Oct 2015)

  New Revision: be4306c576405328f81334c20b8893f777504e8a
  https://github.com/groonga/groonga/commit/be4306c576405328f81334c20b8893f777504e8a

  Message:
    grn_ts: use macros to remove redundant code
    
    GitHub: #416

  Modified files:
    lib/ts.c

  Modified: lib/ts.c (+42 -70)
===================================================================
--- lib/ts.c    2015-10-09 17:27:51 +0900 (c15c9ff)
+++ lib/ts.c    2015-10-09 17:38:01 +0900 (2989609)
@@ -3464,67 +3464,43 @@ grn_ts_op_bitwise_not_evaluate(grn_ctx *ctx, grn_ts_expr_op_node *node,
   }
 }
 
+#define GRN_TS_OP_SIGN_EVALUATE_CASE(type, KIND, kind) \
+  case GRN_TS_ ## KIND: {\
+    grn_ts_ ## kind *out_ptr = (grn_ts_ ## kind *)out;\
+    for (i = 0; i < n_in; i++) {\
+      out_ptr[i] = grn_ts_op_ ## type ## _ ## kind(out_ptr[i]);\
+    }\
+    return GRN_SUCCESS;\
+  }
+#define GRN_TS_OP_SIGN_EVALUATE(type) \
+  size_t i;\
+  grn_rc rc = grn_ts_expr_node_evaluate(ctx, node->args[0], in, n_in, out);\
+  if (rc != GRN_SUCCESS) {\
+    return rc;\
+  }\
+  switch (node->data_kind) {\
+    GRN_TS_OP_SIGN_EVALUATE_CASE(type, INT, int)\
+    GRN_TS_OP_SIGN_EVALUATE_CASE(type, FLOAT, float)\
+    default: {\
+      GRN_TS_ERR_RETURN(GRN_OBJECT_CORRUPT, "invalid data kind: %d",\
+                        node->data_kind);\
+    }\
+  }
 /* grn_ts_op_positive_evaluate() evaluates an operator. */
 static grn_rc
 grn_ts_op_positive_evaluate(grn_ctx *ctx, grn_ts_expr_op_node *node,
                             const grn_ts_record *in, size_t n_in, void *out) {
-  size_t i;
-  grn_rc rc = grn_ts_expr_node_evaluate(ctx, node->args[0], in, n_in, out);
-  if (rc != GRN_SUCCESS) {
-    return rc;
-  }
-  switch (node->data_kind) {
-    case GRN_TS_INT: {
-      grn_ts_int *out_ptr = (grn_ts_int *)out;
-      for (i = 0; i < n_in; i++) {
-        out_ptr[i] = grn_ts_op_positive_int(out_ptr[i]);
-      }
-      return GRN_SUCCESS;
-    }
-    case GRN_TS_FLOAT: {
-      grn_ts_float *out_ptr = (grn_ts_float *)out;
-      for (i = 0; i < n_in; i++) {
-        out_ptr[i] = grn_ts_op_positive_float(out_ptr[i]);
-      }
-      return GRN_SUCCESS;
-    }
-    default: {
-      GRN_TS_ERR_RETURN(GRN_OBJECT_CORRUPT, "invalid data kind: %d",
-                        node->data_kind);
-    }
-  }
+  GRN_TS_OP_SIGN_EVALUATE(positive)
 }
 
 /* grn_ts_op_negative_evaluate() evaluates an operator. */
 static grn_rc
 grn_ts_op_negative_evaluate(grn_ctx *ctx, grn_ts_expr_op_node *node,
                             const grn_ts_record *in, size_t n_in, void *out) {
-  size_t i;
-  grn_rc rc = grn_ts_expr_node_evaluate(ctx, node->args[0], in, n_in, out);
-  if (rc != GRN_SUCCESS) {
-    return rc;
-  }
-  switch (node->data_kind) {
-    case GRN_TS_INT: {
-      grn_ts_int *out_ptr = (grn_ts_int *)out;
-      for (i = 0; i < n_in; i++) {
-        out_ptr[i] = grn_ts_op_negative_int(out_ptr[i]);
-      }
-      return GRN_SUCCESS;
-    }
-    case GRN_TS_FLOAT: {
-      grn_ts_float *out_ptr = (grn_ts_float *)out;
-      for (i = 0; i < n_in; i++) {
-        out_ptr[i] = grn_ts_op_negative_float(out_ptr[i]);
-      }
-      return GRN_SUCCESS;
-    }
-    default: {
-      GRN_TS_ERR_RETURN(GRN_OBJECT_CORRUPT, "invalid data kind: %d",
-                        node->data_kind);
-    }
-  }
+  GRN_TS_OP_SIGN_EVALUATE(negative)
 }
+#undef GRN_TS_OP_SIGN_EVALUATE
+#undef GRN_TS_OP_SIGN_EVALUATE_CASE
 
 /* grn_ts_op_logical_and_evaluate() evaluates an operator. */
 static grn_rc
@@ -4344,37 +4320,33 @@ grn_ts_expr_op_node_filter(grn_ctx *ctx, grn_ts_expr_op_node *node,
   }
 }
 
+#define GRN_TS_OP_SIGN_ADJUST(type)\
+  size_t i, count = 0;\
+  grn_rc rc = grn_ts_expr_node_evaluate_to_buf(ctx, node->args[0], io, n_io,\
+                                               &node->bufs[0]);\
+  if (rc != GRN_SUCCESS) {\
+    return rc;\
+  }\
+  grn_ts_float *buf_ptr = (grn_ts_float *)node->bufs[0].ptr;\
+  for (i = 0; i < n_io; i++) {\
+    grn_ts_float result = grn_ts_op_ ## type ## _float(buf_ptr[i]);\
+    io[count++].score = (grn_ts_score)result;\
+  }\
+  return GRN_SUCCESS;
 /* grn_ts_op_positive_adjust() updates scores. */
 static grn_rc
 grn_ts_op_positive_adjust(grn_ctx *ctx, grn_ts_expr_op_node *node,
                           grn_ts_record *io, size_t n_io) {
-  size_t i, count = 0;
-  grn_rc rc = grn_ts_expr_node_evaluate_to_buf(ctx, node->args[0], io, n_io,
-                                               &node->bufs[0]);
-  if (rc != GRN_SUCCESS) {
-    return rc;
-  }
-  grn_ts_float *buf_ptr = (grn_ts_float *)node->bufs[0].ptr;
-  for (i = 0; i < n_io; i++) {
-    io[count++].score = (grn_ts_score)grn_ts_op_positive_float(buf_ptr[i]);
-  }
+  GRN_TS_OP_SIGN_ADJUST(positive)
 }
 
 /* grn_ts_op_negative_adjust() updates scores. */
 static grn_rc
 grn_ts_op_negative_adjust(grn_ctx *ctx, grn_ts_expr_op_node *node,
                           grn_ts_record *io, size_t n_io) {
-  size_t i, count = 0;
-  grn_rc rc = grn_ts_expr_node_evaluate_to_buf(ctx, node->args[0], io, n_io,
-                                               &node->bufs[0]);
-  if (rc != GRN_SUCCESS) {
-    return rc;
-  }
-  grn_ts_float *buf_ptr = (grn_ts_float *)node->bufs[0].ptr;
-  for (i = 0; i < n_io; i++) {
-    io[count++].score = (grn_ts_score)grn_ts_op_negative_float(buf_ptr[i]);
-  }
+  GRN_TS_OP_SIGN_ADJUST(negative)
 }
+#undef GRN_TS_OP_SIGN_ADJUST
 
 #define GRN_TS_OP_ARITH_ADJUST(type)\
   size_t i, count = 0;\
-------------- next part --------------
HTML����������������������������...
Download 



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