/* Compares binary trees rooted at |a| and |b|, making sure that they are identical. */ static int compare_trees (struct tavl_node *a, struct tavl_node *b) { int okay; if (a == NULL || b == NULL) { if (a != NULL || b != NULL) { printf (" a=%d b=%d\n", a ? *(int *) a->tavl_data : -1, b ? *(int *) b->tavl_data : -1); assert (0); } return 1; } assert (a != b); if (*(int *) a->tavl_data != *(int *) b->tavl_data || a->tavl_tag[0] != b->tavl_tag[0] || a->tavl_tag[1] != b->tavl_tag[1] || a->tavl_balance != b->tavl_balance) { printf (" Copied nodes differ: a=%d (bal=%d) b=%d (bal=%d) a:", *(int *) a->tavl_data, a->tavl_balance, *(int *) b->tavl_data, b->tavl_balance); if (a->tavl_tag[0] == TAVL_CHILD) printf ("l"); if (a->tavl_tag[1] == TAVL_CHILD) printf ("r"); printf (" b:"); if (b->tavl_tag[0] == TAVL_CHILD) printf ("l"); if (b->tavl_tag[1] == TAVL_CHILD) printf ("r"); printf ("\n"); return 0; } if (a->tavl_tag[0] == TAVL_THREAD) assert ((a->tavl_link[0] == NULL) != (a->tavl_link[0] != b->tavl_link[0])); if (a->tavl_tag[1] == TAVL_THREAD) assert ((a->tavl_link[1] == NULL) != (a->tavl_link[1] != b->tavl_link[1])); okay = 1; if (a->tavl_tag[0] == TAVL_CHILD) okay &= compare_trees (a->tavl_link[0], b->tavl_link[0]); if (a->tavl_tag[1] == TAVL_CHILD) okay &= compare_trees (a->tavl_link[1], b->tavl_link[1]); return okay; }
bool compare_equal( node *a, node *b ) { if( compare_trees( a, b, 0 ) ){ return 1; }else{ return 0; } }/* compare_equal */
/* Compares binary trees rooted at |a| and |b|, making sure that they are identical. */ static int compare_trees (struct rtrb_node *a, struct rtrb_node *b) { int okay; if (a == NULL || b == NULL) { if (a != NULL || b != NULL) { printf (" a=%d b=%d\n", a ? *(int *) a->rtrb_data : -1, b ? *(int *) b->rtrb_data : -1); assert (0); } return 1; } assert (a != b); if (*(int *) a->rtrb_data != *(int *) b->rtrb_data || a->rtrb_rtag != b->rtrb_rtag || a->rtrb_color != b->rtrb_color) { printf (" Copied nodes differ: a=%d%c b=%d%c a:", *(int *) a->rtrb_data, a->rtrb_color == RTRB_RED ? 'r' : 'b', *(int *) b->rtrb_data, b->rtrb_color == RTRB_RED ? 'r' : 'b'); if (a->rtrb_rtag == RTRB_CHILD) printf ("r"); printf (" b:"); if (b->rtrb_rtag == RTRB_CHILD) printf ("r"); printf ("\n"); return 0; } if (a->rtrb_rtag == RTRB_THREAD) assert ((a->rtrb_link[1] == NULL) != (a->rtrb_link[1] != b->rtrb_link[1])); okay = compare_trees (a->rtrb_link[0], b->rtrb_link[0]); if (a->rtrb_rtag == RTRB_CHILD) okay &= compare_trees (a->rtrb_link[1], b->rtrb_link[1]); return okay; }
/* Compares binary trees rooted at |a| and |b|, making sure that they are identical. */ static int compare_trees (struct prb_node *a, struct prb_node *b) { int okay; if (a == NULL || b == NULL) { assert (a == NULL && b == NULL); return 1; } if (*(int *) a->prb_data != *(int *) b->prb_data || ((a->prb_link[0] != NULL) != (b->prb_link[0] != NULL)) || ((a->prb_link[1] != NULL) != (b->prb_link[1] != NULL)) || a->prb_color != b->prb_color) { printf (" Copied nodes differ: a=%d%c b=%d%c a:", *(int *) a->prb_data, a->prb_color == PRB_RED ? 'r' : 'b', *(int *) b->prb_data, b->prb_color == PRB_RED ? 'r' : 'b'); if (a->prb_link[0] != NULL) printf ("l"); if (a->prb_link[1] != NULL) printf ("r"); printf (" b:"); if (b->prb_link[0] != NULL) printf ("l"); if (b->prb_link[1] != NULL) printf ("r"); printf ("\n"); return 0; } okay = 1; if (a->prb_link[0] != NULL) okay &= compare_trees (a->prb_link[0], b->prb_link[0]); if (a->prb_link[1] != NULL) okay &= compare_trees (a->prb_link[1], b->prb_link[1]); return okay; }
/* Compares binary trees rooted at |a| and |b|, making sure that they are identical. */ static int compare_trees (struct avl_node *a, struct avl_node *b) { int okay; if (a == NULL || b == NULL) { assert (a == NULL && b == NULL); return 1; } if (*(int *) a->avl_data != *(int *) b->avl_data || ((a->avl_link[0] != NULL) != (b->avl_link[0] != NULL)) || ((a->avl_link[1] != NULL) != (b->avl_link[1] != NULL)) || a->avl_balance != b->avl_balance) { printf (" Copied nodes differ: a=%d (bal=%d) b=%d (bal=%d) a:", *(int *) a->avl_data, a->avl_balance, *(int *) b->avl_data, b->avl_balance); if (a->avl_link[0] != NULL) printf ("l"); if (a->avl_link[1] != NULL) printf ("r"); printf (" b:"); if (b->avl_link[0] != NULL) printf ("l"); if (b->avl_link[1] != NULL) printf ("r"); printf ("\n"); return 0; } okay = 1; if (a->avl_link[0] != NULL) okay &= compare_trees (a->avl_link[0], b->avl_link[0]); if (a->avl_link[1] != NULL) okay &= compare_trees (a->avl_link[1], b->avl_link[1]); return okay; }
//Input: Two conditions x and y //Output: One condition x AND y (i.e., x && y) NC* createAndExpression( NC *cond1, NC *cond2 ) { NC *traverseOr; NC *t1, *t2, *node; boolean flag; if(cond1 == NULL && cond2 == NULL) return NULL; if(cond1 == NULL) return cond2; if(cond2 == NULL) return cond1; traverseOr = cond1->link; while(traverseOr->list != NULL) traverseOr = traverseOr->list; //Appending (ANDing) cond1 and cond2 //traverseOr->list = cond2->link; //The above-mentioned simplest strategy is neglected //to avoid duplicating the same conjuncts t2 = cond2->link; while( t2 != (NC*)NULL ) { flag = TRUE; t1 = cond1->link; while( t1 != (NC*)NULL ) { if(compare_trees(t1->link, t2->link) == 1) { flag = FALSE; break; } t1 = t1->list; } if(flag) { node = (NC*)malloc(sizeof(NC)); node->list = (NC*)NULL; node->type = 'O'; node->inc = 0; node->link = copylist( t2->link ); traverseOr->list = node; traverseOr = node; } t2 = t2->list; } return cond1; }
/* Compares binary trees rooted at |a| and |b|, making sure that they are identical. */ static int compare_trees (struct pavl_node *a, struct pavl_node *b) { int okay; if (a == NULL || b == NULL) { assert (a == NULL && b == NULL); return 1; } if (*(int *) a->pavl_data != *(int *) b->pavl_data || ((a->pavl_link[0] != NULL) != (b->pavl_link[0] != NULL)) || ((a->pavl_link[1] != NULL) != (b->pavl_link[1] != NULL)) || ((a->pavl_parent != NULL) != (b->pavl_parent != NULL)) || (a->pavl_parent != NULL && b->pavl_parent != NULL && a->pavl_parent->pavl_data != b->pavl_parent->pavl_data) || a->pavl_balance != b->pavl_balance) { printf (" Copied nodes differ:\n" " a: %d, bal %+d, parent %d, %s left child, %s right child\n" " b: %d, bal %+d, parent %d, %s left child, %s right child\n", *(int *) a->pavl_data, a->pavl_balance, a->pavl_parent != NULL ? *(int *) a->pavl_parent : -1, a->pavl_link[0] != NULL ? "has" : "no", a->pavl_link[1] != NULL ? "has" : "no", *(int *) b->pavl_data, b->pavl_balance, b->pavl_parent != NULL ? *(int *) b->pavl_parent : -1, b->pavl_link[0] != NULL ? "has" : "no", b->pavl_link[1] != NULL ? "has" : "no"); return 0; } okay = 1; if (a->pavl_link[0] != NULL) okay &= compare_trees (a->pavl_link[0], b->pavl_link[0]); if (a->pavl_link[1] != NULL) okay &= compare_trees (a->pavl_link[1], b->pavl_link[1]); return okay; }
bool compare_trees( node *a, node *b, int nxt ) { if( a == NULL && b == NULL ) return 1; if( a == NULL || b == NULL ){ return 0; } if( a->nodeop != b->nodeop ){ return 0; } switch( a->nodeop ){ /* compare 'value' field */ CASE_DO: case op_dolimit : case op_declare : case op_constant : case op_real : case op_index : CASE_MEMREF: case op_call: if( a->nodevalue != b->nodevalue ){ return 0; } default: break; } if( !compare_trees( a->nodechild, b->nodechild, 1 ) ){ return 0; } if( nxt && !compare_trees( a->nodenext, b->nodenext, 1 ) ){ return 0; } return 1; }/* compare_trees */
//Input: Two relations x = (a or b or c), and y = (b or d) //Output: One relation z = (a or b or c or d) -- devoid of identical clauses NC* compareRelations( NC *rel1, NC *rel2 ) { NC *copyRel1, *copyRel2; NC *r1, *r2, *tempR; boolean flag; if(rel1 == NULL && rel2 == NULL) return NULL; if(rel1 == NULL) return rel2; if(rel2 == NULL) return rel1; copyRel1 = copylist(rel1); copyRel2 = copylist(rel2); tempR = copyRel1; while(tempR->list != NULL) tempR = tempR->list; for(r2 = copyRel2; r2 != NULL; r2 = r2->list) { flag = TRUE; for(r1 = copyRel1; r1 != NULL; r1 = r1->list) { if(compare_trees(r1, r2) == 1) { flag = FALSE; break; } } if(flag) { tempR->list = copylist( r2->link ); tempR = tempR->list; } } return copyRel1; }
int main(int argc, char* argv[]){ int retcode = 0 ; int nTests = sizeof(stTestRef)/sizeof(stSTMT_REF) ; for(int c = 0 ; c < nTests ; c++) { if(stTestRef[c].nFlag){ stTestRef[c].pC = new SQL_compiler() ; stTestRef[c].pRefTree = new SQL_code_tree() ; } } /* Create reference code trees */ /* Statement: 0 "create table foo (pk integer primary key, a integer, b varchar(20), check (a is not null))" */ /* Statement: 1 */ /* Statement: 2 */ /* Statement: 3 "delete from foo" */ stTestRef[3].pRefTree->shift('N') ; stTestRef[3].pRefTree->shift('D') ; stTestRef[3].pRefTree->shift('B') ; stTestRef[3].pRefTree->reduce(0x2050400e, 3) ; stTestRef[3].pRefTree->shift('F') ; stTestRef[3].pRefTree->shift('O') ; stTestRef[3].pRefTree->shift('O') ; stTestRef[3].pRefTree->reduce(0x20502003, 3) ; stTestRef[3].pRefTree->reduce(0x2050400f, 1) ; stTestRef[3].pRefTree->reduce(0x20504007, 2) ; stTestRef[3].pRefTree->reduce(0x21407003, 1) ; stTestRef[3].pRefTree->shift(0x205021ca) ; stTestRef[3].pRefTree->reduce(0x20630001, 1) ; stTestRef[3].pRefTree->reduce(0x20815001, 1) ; stTestRef[3].pRefTree->shift(0x21407002) ; stTestRef[3].pRefTree->reduce(0x21407004, 3) ; stTestRef[3].pRefTree->shift(0x21407002) ; stTestRef[3].pRefTree->reduce(0x21407005, 1) ; stTestRef[3].pRefTree->shift(0x21414001) ; stTestRef[3].pRefTree->shift(0x21414002) ; stTestRef[3].pRefTree->reduce(0x21407001, 4) ; stTestRef[3].pRefTree->reduce(0x51506004, 1) ; stTestRef[3].pRefTree->reduce(0x51506003, 1) ; /* Statement: 4 */ /* Statement: 5 */ /* Statement: 6 */ /* Statement: 7 */ /* Statement: 8 */ /* Statement: 9 */ /* Statement: 10 */ /* Statement: 11 */ /* Statement: 12 */ for(int i = 0 ; i < nTests ; i++){ /* Check to see if the statement has an associated code tree and compiler */ if(stTestRef[i].nFlag){ stTestRef[i].pC->prepare( stTestRef[i].szTestStmt, strlen(stTestRef[i].szTestStmt)) ; if( 0 != compare_trees(&stTestRef[i].pC->m_code_tree, stTestRef[i].pRefTree) ){ printf("\nCompiler generated tree for statement #%d: \"%s\"\ndeviates from its reference\n", i, stTestRef[i].szTestStmt) ; retcode = -1 ; break ; }else{ printf("\nTrees for statement #%d: \"%s\" match nicely -- OK\n", i, stTestRef[i].szTestStmt) ; retcode = 0 ; } } } for(int d = 0 ; d < nTests ; d++) { if(stTestRef[d].nFlag){ delete stTestRef[d].pC ; delete stTestRef[d].pRefTree ; } } return retcode ; }
void run_tree_compare(void) { struct BTreeNode *roots[8ul][1000ul]; clock_t time_start; clock_t time_finish; clock_t time_elapsed; size_t i, j, h, n, nlgn; struct BTreeNode *root1; struct BTreeNode *root2; for (i = 0ul, h = 2ul; i < 8ul; ++i, h += 2ul) { for (j = 0ul; j < 1000ul; ++j) { roots[i][j] = init_tree(h); } time_start = clock(); do { --j; root1 = roots[i][j]; --j; root2 = roots[i][j]; similar_trees(root1, root2); } while (j > 0ul); time_finish = clock(); do { free_nodes(roots[i][j]); ++j; } while (j < 1000ul); time_elapsed = time_finish - time_start; n = 1ul << h; nlgn = h * n; printf("N: %zu\n" "lgN: %zu\n" "NlgN: %zu\n" "time: %zu\n" "N/T: %f\n", n, h, nlgn, time_elapsed, (500.0 * (double) n) / ((double) time_elapsed)); } struct BTree *tree1 = init_tree1(); struct BTree *tree2 = init_tree2(); struct BTree *tree3 = init_tree3(); struct BTree *tree4 = init_tree4(); struct BTree *tree5 = init_tree5(); struct BTree *tree6 = init_tree6(); struct BTree *tree7 = init_tree7(); compare_trees(tree1, tree2); compare_trees(tree1, tree3); compare_trees(tree1, tree4); compare_trees(tree1, tree5); compare_trees(tree1, tree6); compare_trees(tree1, tree7); inspect_compare_trees(tree1, tree2); inspect_compare_trees(tree1, tree3); inspect_compare_trees(tree1, tree4); inspect_compare_trees(tree1, tree5); inspect_compare_trees(tree1, tree6); inspect_compare_trees(tree1, tree7); free_tree(tree1); free_tree(tree2); free_tree(tree3); free_tree(tree4); free_tree(tree5); free_tree(tree6); free_tree(tree7); }