Revert "Untested iterative implementation of evaluate"

This reverts commit f2ba9d7621.
This commit is contained in:
David Adrian 2013-10-11 17:10:20 -04:00
parent 8f19fb2cac
commit 84318c2cf0
4 changed files with 8 additions and 111 deletions

View File

@ -24,7 +24,7 @@ void free_stack(stack_t* stack)
xfree(stack);
}
void stack_push(stack_t* stack, void* elt)
void push(stack_t* stack, void* elt)
{
if (stack->cur_size == stack->max_size) {
stack->max_size *= 2;
@ -33,17 +33,8 @@ void stack_push(stack_t* stack, void* elt)
stack->arr[stack->cur_size++] = elt;
}
void stack_pop(stack_t* stack)
void* pop(stack_t* stack)
{
--stack->cur_size;
}
void* stack_peek(stack_t* stack)
{
return stack->arr[stack->cur_size - 1];
}
int stack_is_empty(stack_t* stack)
{
return (stack->cur_size == 0);
void* res = stack->arr[--stack->cur_size];
return res;
}

View File

@ -9,10 +9,7 @@ typedef struct stack stack_t;
stack_t* alloc_stack(size_t size);
void free_stack(stack_t* stack);
void stack_push(stack_t* stack, void* elt);
void stack_pop(stack_t* stack);
void* stack_peek(stack_t* stack);
int stack_is_empty(stack_t* stack);
void push(stack_t* stack, void* elt);
void* pop(stack_t* stack);
#endif /* ZMAP_STACK_H */

View File

@ -1,8 +1,6 @@
#include "expression.h"
#include "fieldset.h"
#include "fieldset.H"
#include "../lib/stack.h"
#include "../lib/xalloc.h"
/* Static helper functions */
@ -85,7 +83,6 @@ node_t* make_field_node(char *fieldname)
node_t *node = alloc_node();
node->type = FIELD;
node->value.field.fieldname = fieldname;
node->result = 1;
return node;
}
@ -94,7 +91,6 @@ node_t* make_string_node(char *literal)
node_t *node = alloc_node();
node->type = STRING;
node->value.string_literal = literal;
node->result = 1;
return node;
}
@ -103,7 +99,6 @@ node_t* make_int_node(int literal)
node_t *node = alloc_node();
node->type = INT;
node->value.int_literal = literal;
node->result = 1;
return node;
}
@ -140,88 +135,6 @@ int evaluate_expression(node_t *root, fieldset_t *fields) {
return 0;
}
int evaluate_expression_fast(node_t *root, fieldset_t *fields) {
stack_t* s;
node_t* node;
if (!root) return 1;
s = alloc_stack(16); /* XXX Find a way to guess/know this better */
root->evaluated = 0;
stack_push(s, root);
int result = 1;
while (!stack_is_empty(s) && result) {
node = (node_t *) stack_peek(s);
if (node->type != OP) {
continue;
}
if (node->value.op == AND) {
if (!node->right_child->evaluated) {
stack_push(s, node->right_child);
}
if (!node->left_child->evaluated) {
stack_push(s, node->left_child);
continue;
}
stack_pop(s);
if (!(node->left_child->result && node->right_child->result)) {
node->evaluated = 1;
result = 0;
}
node->left_child->evaluated = 0;
node->right_child->evaluated = 0;
} else if (node->value.op == OR) {
if (!node->right_child->evaluated) {
stack_push(s, node->right_child);
}
if (!node->left_child->evaluated) {
stack_push(s, node->left_child);
continue;
}
stack_pop(s);
if (!(node->left_child->result || node->right_child->result)) {
node->evaluated = 1;
result = 0;
}
node->left_child->evaluated = 0;
node->right_child->evaluated = 0;
} else {
stack_pop(s);
switch (node->value.op) {
case GT:
node->result = eval_gt_node(node, fields);
break;
case LT:
node->result = eval_lt_node(node, fields);
break;
case EQ:
node->result = eval_eq_node(node, fields);
break;
case NEQ:
node->result = !(eval_eq_node(node, fields));
break;
case LT_EQ:
node->result = eval_lt_eq_node(node, fields);
break;
case GT_EQ:
node->result = eval_gt_eq_node(node, fields);
break;
default:
exit(-1);
break;
}
node->evaluated = 1;
if (stack_is_empty(s)) {
result = node->result;
node->evaluated = 0;
}
}
}
free_stack(s);
return result;
}
void print_expression(node_t *root) {
if (!root) return;
printf("%s", "( ");

View File

@ -23,8 +23,8 @@ struct field_id {
union node_value {
struct field_id field;
uint64_t int_literal;
char *string_literal;
uint64_t int_literal;
enum operation op;
};
@ -33,8 +33,6 @@ typedef struct node {
struct node *right_child;
enum node_type type;
union node_value value;
int evaluated;
int result;
} node_t;
node_t* make_op_node(enum operation op);
@ -47,8 +45,6 @@ node_t* make_int_node(int literal);
int evaluate_expression(node_t *root, fieldset_t *fields);
int evaluate_expression_fast(node_t *root, fieldset_t *fields);
void print_expression(node_t *root);
#endif /* ZMAP_TREE_H */