DFA *dfaProduct(DFA* a1, DFA* a2, dfaProductType ff) { DFA *b; int i; unsigned *root_ptr; char binfun[4]; int make_a_loop; unsigned size_estimate = 4 + 4 * (bdd_size(a1->bddm) > bdd_size(a2->bddm) ? bdd_size(a1->bddm) : bdd_size(a2->bddm)); bdd_manager *bddm; /* #define _AUTOMATON_HASHED_IN_PRODUCT_ */ #ifdef _AUTOMATON_HASHED_IN_PRODUCT_ /*prepare hashed access */ bddm = bdd_new_manager(size_estimate, size_estimate/8 + 2); bdd_make_cache(bddm, size_estimate, size_estimate/8 + 2); bddm->cache_erase_on_doubling = TRUE ; #else /*prepare sequential access*/ bddm = bdd_new_manager(size_estimate, 0); bdd_make_cache(bddm, size_estimate, size_estimate/8 + 2); #endif binfun[0] = ff&1; binfun[1] = (ff&2)>>1; /* The binary function */ binfun[2] = (ff&4)>>2; binfun[3] = (ff&8)>>3; qst = qh = qt = new_list(a1->s, a2->s, (list) 0); htbl = new_hash_tab(&hash2, &eq2); insert_in_hash_tab(htbl, a1->s, a2->s, (void *) 1); last_state = 1; /* Careful here! Bdd's start at 0, hashtbl at 1 */ while(qh) { /* Our main loop, nice and tight */ make_a_loop = make_a_loop_status(is_loop(a1->bddm, qh->li1, a1->q[qh->li1]), a1->f[qh->li1], is_loop(a2->bddm, qh->li2, a2->q[qh->li2]), a2->f[qh->li2], binfun); if (make_a_loop != 2) make_loop(bddm, qh->li1, qh->li2); else { #ifdef _AUTOMATON_HASHED_IN_PRODUCT_ (void) bdd_apply2_hashed (a1->bddm, a1->q[qh->li1], a2->bddm, a2->q[qh->li2], bddm, &prod_term_fn); #else (void) bdd_apply2_sequential (a1->bddm, a1->q[qh->li1], a2->bddm, a2->q[qh->li2], bddm, &prod_term_fn); #endif } qh = qh->next; } b = dfaMakeNoBddm(last_state); /* Return the result */ b->s = 0; /* Always first on list */ b->bddm = bddm; for (i=0, root_ptr = bdd_roots(bddm); i < last_state; root_ptr++, i++) { list qnxt; b->q[i] = *root_ptr; b->f[i] = ((a1->f[qst->li1] != 0) && (a2->f[qst->li2] != 0)) ? /* both states are non-bottom, use "binfun" */ BOOL_TO_STATUS(binfun[STATUS_TO_BOOL(a1->f[qst->li1])*2 + STATUS_TO_BOOL(a2->f[qst->li2])]) : /* at least one is bottom */ 0; qnxt = qst->next; mem_free(qst); /* Free the list */ qst = qnxt; } free_hash_tab(htbl); bdd_update_statistics(bddm, (unsigned) PRODUCT); bdd_kill_cache(b->bddm); return(b); }
int main() { bdd_manager *bddm, *bddm1; bdd_ptr zero, one; bdd_handle var2, var7; bdd_ptr and_2_7, nand_2_7; /*bdd_handle handle;*/ bdd_init(); /* needed since we're using statistics */ bddm = bdd_new_manager(100,50); /* get a BDD pointer to a node that is the leaf with value 0 */ zero = bdd_find_leaf_hashed_add_root(bddm, 0); /* and a leaf with value 1 */ one = bdd_find_leaf_hashed_add_root(bddm, 1); /* note already at this point "zero" could have been invalidated if the table doubled, but we know that there is room for a 100 nodes---anyway, this is really very bad style, so we go on in a more appropriate manner */ /* "then" part is one, "else" part is zero */ var2 = bdd_handle_find_node_hashed_add_root(bddm, zero, one, 2); var7 = bdd_handle_find_node_hashed_add_root(bddm, zero, one, 7); /* check node pointers and handles */ assert(zero == BDD_ROOT(bddm, 0)); /* since table was not doubled */ assert(one == BDD_ROOT(bddm, 1)); assert(var2 == 2); assert(var7 == 3); bddm1 = bdd_new_manager(100,50); /* make room for at least 100 nodes, overflow increment is 50 */ bdd_make_cache(bddm1, 100, 50); /* apply2 needs a result cache, here the size is a hundred with increment 50 */ /* apply operation on var2 and var7 in bddm; the result is a completely fresh bdd in bddm1 and a BDD pointer, named "and_2_7" */ and_2_7 = bdd_apply2_hashed(bddm, BDD_ROOT(bddm, var2), /* BDD #1 */ bddm, BDD_ROOT(bddm, var7), /* BDD #2 */ bddm1, /* result BDD */ &and); /* leaf operation */ bdd_update_statistics(bddm, 0); /* update statics group "0" with data from bddm before killing the manager */ printf("Size of bddm: %d\n\n", bdd_size(bddm)); /* let's see the number of nodes created */ bdd_kill_manager(bddm); printf("Size of bddm1: %d\n\n", bdd_size(bddm1)); /*handle = BDD_LAST_HANDLE(bddm1);*/ /* assert(handle == 0); assert(BDD_ROOT(bddm1, handle) == and_2_7); */ /* reset all mark fields in bddm1 before an apply1 operation */ bdd_prepare_apply1(bddm1); /* a new bdd (which as an unlabelled graph is isomorphic to old one) in bddm1 is the result of the following apply operation */ /* it's safe here to use and_2_7 since no operations were performed after it was calculated that could have entailed doubling of table */ nand_2_7 = bdd_apply1(bddm1, and_2_7, bddm1, ¬); bdd_update_statistics(bddm1, 1); printf("Size of bddm1: %d\n\n", bdd_size(bddm1)); print_bdd(bddm1, and_2_7); printf("\n\n"); print_bdd(bddm1, nand_2_7); printf("\n\n"); bdd_kill_manager(bddm1); bdd_print_statistics(0, "bddm"); /* print group 0 statistics with heading "bddm" */ bdd_print_statistics(1, "bddm1"); /* print group 1 statistics with heading "bddm1" */ return 0; }