Navigation Menu

Skip to content

Commit

Permalink
grn_ts: use macros to remove redundant code
Browse files Browse the repository at this point in the history
GitHub: #416
  • Loading branch information
s-yata committed Oct 9, 2015
1 parent 29b96a0 commit be4306c
Showing 1 changed file with 42 additions and 70 deletions.
112 changes: 42 additions & 70 deletions lib/ts.c
Expand Up @@ -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
Expand Down Expand Up @@ -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;\
Expand Down

0 comments on commit be4306c

Please sign in to comment.