diff --git a/library/tries/base_dbtries.c b/library/tries/base_dbtries.c index 083b4e5fc..72a38603e 100644 --- a/library/tries/base_dbtries.c +++ b/library/tries/base_dbtries.c @@ -238,9 +238,11 @@ YAP_Term trie_depth_breadth(TrEntry trie, TrEntry db_trie, YAP_Int opt_level, YA set_depth_breadth_reduction_current_data(NULL); /* We only need to simplify the trie once! */ /* This can be a 10% overhead for sld cases :-( */ +// printf("simplification\n"); trie_print(trie); if (TrNode_child(TrEntry_trie(trie))) simplification_reduction(trie); while (TrNode_child(TrEntry_trie(trie))) { +// printf("depth\n"); trie_print(trie); nested_trie = depth_reduction(trie, depth_node, opt_level); if (nested_trie) { set_depth_breadth_reduction_current_data(get_data_from_trie_node(nested_trie)); @@ -248,6 +250,7 @@ YAP_Term trie_depth_breadth(TrEntry trie, TrEntry db_trie, YAP_Int opt_level, YA *end_counter = core_get_label_counter(); return YAP_MkApplTerm((YAP_Functor)(~ApplTag & TrNode_entry(TrNode_parent(nested_trie))), 1, &TrNode_entry(nested_trie)); } +// printf("breadth\n"); trie_print(trie); nested_trie = breadth_reduction(trie, breadth_node, opt_level); if (nested_trie) { set_depth_breadth_reduction_current_data(get_data_from_trie_node(nested_trie)); @@ -276,7 +279,8 @@ TrData trie_get_depth_breadth_reduction_current_data(void) { void trie_replace_nested_trie(TrEntry trie, YAP_Int nested_trie_id, YAP_Term new_term) { - core_depth_breadth_trie_replace_nested_trie(TrNode_child(TrEntry_trie(trie)), nested_trie_id, new_term); + CURRENT_TRIE = trie; + core_depth_breadth_trie_replace_nested_trie(TrNode_child(TrEntry_trie(trie)), nested_trie_id, new_term, &trie_data_construct, &trie_data_destruct); return; } diff --git a/library/tries/base_tries.c b/library/tries/base_tries.c index 907334c7f..867d30c52 100644 --- a/library/tries/base_tries.c +++ b/library/tries/base_tries.c @@ -74,8 +74,23 @@ void trie_data_destruct(TrNode node) { data = (TrData) GET_DATA_FROM_LEAF_TRIE_NODE(node); trie = TrData_trie(data); - if (data == TrEntry_traverse_data(trie)) - TrEntry_traverse_data(trie) = TrData_previous(data); + if (data == TrEntry_traverse_data(trie)) { + if (CURRENT_TRAVERSE_MODE == TRAVERSE_MODE_FORWARD) { + TrEntry_traverse_data(trie) = TrData_previous(data); + } else { + if (TrData_next(data)) { + TrEntry_traverse_data(trie) = TrData_next(data); + } else { + TrData special; + new_struct(special, TYPE_TR_DATA, SIZEOF_TR_DATA); + TrData_next(special) = NULL; + TrData_previous(special) = TrData_previous(data); + TrData_trie(special) = NULL; + TrData_leaf(special) = NULL; + TrEntry_traverse_data(trie) = special; /* This special data is necessery to allow proper backwards traverse when the last entry is removed this is freed only if the trie is kept traversing */ + } + } + } if (TrData_next(data)) { TrData_previous(TrData_next(data)) = TrData_previous(data); TrData_next(TrData_previous(data)) = TrData_next(data); @@ -211,10 +226,19 @@ TrData trie_traverse_init(TrEntry trie, TrData init_data) { TrData trie_traverse_cont(TrEntry trie) { - TrData data; - + TrData data, temp = NULL; data = TrEntry_traverse_data(trie); if (data) { + if (!TrData_trie(data)) { + if (TrEntry_first_data(trie)) { + temp = data; + } else { + free_trie_data(data); + data = NULL; + TrEntry_traverse_data(trie) = NULL; + return NULL; + } + } if (CURRENT_TRAVERSE_MODE == TRAVERSE_MODE_FORWARD) data = TrData_next(data); else { @@ -223,6 +247,8 @@ TrData trie_traverse_cont(TrEntry trie) { data = NULL; } TrEntry_traverse_data(trie) = data; + if (temp) + free_trie_data(temp); } return data; } @@ -325,7 +351,6 @@ void trie_print(TrEntry trie) { void trie_data_construct(TrNode node) { TrData data; - new_trie_data(data, CURRENT_TRIE, node); PUT_DATA_IN_LEAF_TRIE_NODE(node, data); return; diff --git a/library/tries/core_dbtries.c b/library/tries/core_dbtries.c index 865ffa4de..f2a2a1c70 100644 --- a/library/tries/core_dbtries.c +++ b/library/tries/core_dbtries.c @@ -210,9 +210,9 @@ int traverse_get_counter(TrNode node); YAP_Term generate_label(YAP_Int Index); YAP_Term update_depth_breadth_trie(TrEngine engine, TrNode root, YAP_Int opt_level, void (*construct_function)(TrNode), void (*destruct_function)(TrNode), void (*copy_function)(TrNode, TrNode), void (*correct_order_function)(void)); YAP_Term get_return_node_term(TrNode node); -void traverse_and_replace_nested_trie(TrNode node, YAP_Int nested_trie_id, YAP_Term new_term); -TrNode replace_nested_trie(TrNode node, TrNode child, YAP_Term new_term); -void check_attach_childs(TrNode search_child, TrNode existing_child); +void traverse_and_replace_nested_trie(TrNode node, YAP_Int nested_trie_id, YAP_Term new_term, void (*construct_function)(TrNode), void (*destruct_function)(TrNode)); +TrNode replace_nested_trie(TrNode node, TrNode child, YAP_Term new_term, void (*construct_function)(TrNode), void (*destruct_function)(TrNode)); +void check_attach_childs(TrNode parent, TrNode search_child, TrNode existing_child, void (*construct_function)(TrNode), void (*destruct_function)(TrNode)); TrNode get_simplification_sibling(TrNode node); TrNode check_parent_first(TrNode node); TrNode TrNode_myparent(TrNode node); @@ -257,18 +257,18 @@ void core_set_trie_db_opt_min_prefix(YAP_Int min_prefix) { -void core_depth_breadth_trie_replace_nested_trie(TrNode node, YAP_Int nested_trie_id, YAP_Term new_term) { - traverse_and_replace_nested_trie(node, nested_trie_id, new_term); +void core_depth_breadth_trie_replace_nested_trie(TrNode node, YAP_Int nested_trie_id, YAP_Term new_term, void (*construct_function)(TrNode), void (*destruct_function)(TrNode)) { + traverse_and_replace_nested_trie(node, nested_trie_id, new_term, construct_function, destruct_function); return; } inline -void traverse_and_replace_nested_trie(TrNode node, YAP_Int nested_trie_id, YAP_Term new_term) { +void traverse_and_replace_nested_trie(TrNode node, YAP_Int nested_trie_id, YAP_Term new_term, void (*construct_function)(TrNode), void (*destruct_function)(TrNode)) { TrNode child, temp; if (TrNode_entry(node) == PairEndTag) { if (TrNode_next(node)) - traverse_and_replace_nested_trie(TrNode_next(node), nested_trie_id, new_term); + traverse_and_replace_nested_trie(TrNode_next(node), nested_trie_id, new_term, construct_function, destruct_function); return; } else if (IS_HASH_NODE(node)) { printf("HASH NODE ERROR: db_tries do not support hash nodes.\n"); @@ -280,7 +280,7 @@ void traverse_and_replace_nested_trie(TrNode node, YAP_Int nested_trie_id, YAP_T do { if ((node = *--bucket)) { do { - traverse_and_replace_nested_trie(node, nested_trie_id, new_term); + traverse_and_replace_nested_trie(node, nested_trie_id, new_term, construct_function, destruct_function); node = TrNode_next(node); } while(node); } @@ -303,13 +303,13 @@ void traverse_and_replace_nested_trie(TrNode node, YAP_Int nested_trie_id, YAP_T do { if (YAP_IntOfTerm(TrNode_entry(child)) == nested_trie_id) { temp = TrNode_previous(node); - node = replace_nested_trie(node, child, new_term); + node = replace_nested_trie(node, child, new_term, construct_function, destruct_function); if (temp) { temp = TrNode_next(node); if (temp) node = temp; } else { - traverse_and_replace_nested_trie(TrNode_child(node), nested_trie_id, new_term); + traverse_and_replace_nested_trie(TrNode_child(node), nested_trie_id, new_term, construct_function, destruct_function); return; } } @@ -322,10 +322,10 @@ void traverse_and_replace_nested_trie(TrNode node, YAP_Int nested_trie_id, YAP_T do { if (YAP_IntOfTerm(TrNode_entry(child)) == nested_trie_id) { temp = TrNode_next(node); - node = replace_nested_trie(node, child, new_term); - traverse_and_replace_nested_trie(TrNode_child(node), nested_trie_id, new_term); + node = replace_nested_trie(node, child, new_term, construct_function, destruct_function); + traverse_and_replace_nested_trie(TrNode_child(node), nested_trie_id, new_term, construct_function, destruct_function); if(temp) - traverse_and_replace_nested_trie(temp, nested_trie_id, new_term); + traverse_and_replace_nested_trie(temp, nested_trie_id, new_term, construct_function, destruct_function); return; } child = TrNode_next(child); @@ -333,17 +333,16 @@ void traverse_and_replace_nested_trie(TrNode node, YAP_Int nested_trie_id, YAP_T } } } - traverse_and_replace_nested_trie(TrNode_child(node), nested_trie_id, new_term); + traverse_and_replace_nested_trie(TrNode_child(node), nested_trie_id, new_term, construct_function, destruct_function); if (TrNode_next(node)) - traverse_and_replace_nested_trie(TrNode_next(node), nested_trie_id, new_term); + traverse_and_replace_nested_trie(TrNode_next(node), nested_trie_id, new_term, construct_function, destruct_function); } return; } /* fixmeeee */ -TrNode replace_nested_trie(TrNode node, TrNode child, YAP_Term new_term) { +TrNode replace_nested_trie(TrNode node, TrNode child, YAP_Term new_term, void (*construct_function)(TrNode), void (*destruct_function)(TrNode)) { TrNode newnode, temp, newnodef = NULL; - YAP_Term term_search = (YAP_Term) NULL; if (YAP_IsApplTerm(new_term)) { YAP_Term new_term_functor = ApplTag | ((YAP_Term) YAP_FunctorOfTerm(new_term)); YAP_Int arity = YAP_ArityOfFunctor(YAP_FunctorOfTerm(new_term)); @@ -369,27 +368,19 @@ TrNode replace_nested_trie(TrNode node, TrNode child, YAP_Term new_term) { TrNode_previous(TrNode_child(newnodef)) = newnode; TrNode_child(newnodef) = newnode; } else { - // Rewind to first uncle node - temp = TrNode_parent(node); - if (IS_FUNCTOR_NODE(temp)) - term_search = TrNode_entry(temp); + /* Check if one of the node siblings have new_term */ + temp = node; while (TrNode_previous(temp)) temp = TrNode_previous(temp); - // Handles cases like not(t(?)) but doesn't handle case like not(not(...) - if (term_search) { - while (temp && TrNode_entry(temp) != term_search) - temp = TrNode_next(temp); - if (temp) - temp = TrNode_child(temp); - } while (temp && TrNode_entry(temp) != new_term) temp = TrNode_next(temp); - if (temp) { // Found a node we can reuse + if (temp) { newnode = temp; // Check if the childs of node/child exist already otherwise attach them - check_attach_childs(TrNode_child(child), TrNode_child(newnode)); - //DATA_DESTRUCT_FUNCTION = destruct_function; + check_attach_childs(newnode, TrNode_child(child), TrNode_child(newnode), construct_function, destruct_function); + DATA_DESTRUCT_FUNCTION = destruct_function; remove_child_nodes(TrNode_child(child)); + TrNode_child(child) = NULL; remove_entry(child); return newnode; } else { // Make a new node @@ -426,20 +417,56 @@ TrNode replace_nested_trie(TrNode node, TrNode child, YAP_Term new_term) { } -void check_attach_childs(TrNode search_child, TrNode existing_child) { +void check_attach_childs(TrNode parent, TrNode search_child, TrNode existing_child, void (*construct_function)(TrNode), void (*destruct_function)(TrNode)) { + TrNode newnode; // Check if the childs of node/child exist already otherwise attach them do { - while(existing_child && (TrNode_entry(existing_child) != TrNode_entry(search_child))) + while(existing_child && (TrNode_entry(existing_child) != PairEndTag) && (TrNode_entry(existing_child) != TrNode_entry(search_child))) existing_child = TrNode_next(existing_child); + if (existing_child) { - if (TrNode_entry(existing_child) != PairEndTag) { - check_attach_childs(TrNode_child(search_child), TrNode_child(existing_child)); - } + if (TrNode_entry(existing_child) != PairEndTag) + check_attach_childs(existing_child, TrNode_child(search_child), TrNode_child(existing_child), construct_function, destruct_function); + existing_child = TrNode_child(parent); + search_child = TrNode_next(search_child); + } else if (TrNode_entry(search_child) == PairEndTag) { + newnode = parent; + DATA_DESTRUCT_FUNCTION = destruct_function; + remove_child_nodes(TrNode_child(newnode)); + TrNode_child(newnode) = NULL; + newnode = trie_node_check_insert(newnode, PairEndTag); + INCREMENT_ENTRIES(CURRENT_TRIE_ENGINE); + (*construct_function)(newnode); + return; } else { - printf("Need to attach child!\n"); - abort(); + existing_child = search_child; + search_child = TrNode_next(search_child); + if(TrNode_child(TrNode_parent(existing_child)) == existing_child) { + if(TrNode_next(existing_child)) { + TrNode_child(TrNode_parent(existing_child)) = TrNode_next(existing_child); + } else { + newnode = TrNode_parent(existing_child); +// DATA_DESTRUCT_FUNCTION = destruct_function; +// remove_child_nodes(TrNode_child(newnode)); + TrNode_child(newnode) = NULL; + newnode = trie_node_check_insert(newnode, PairEndTag); + INCREMENT_ENTRIES(CURRENT_TRIE_ENGINE); + (*construct_function)(newnode); + } + } + + if (TrNode_next(existing_child)) + TrNode_previous(TrNode_next(existing_child)) = TrNode_previous(existing_child); + if (TrNode_previous(existing_child)) + TrNode_next(TrNode_previous(existing_child)) = TrNode_next(existing_child); + + TrNode_parent(existing_child) = parent; + TrNode_previous(existing_child) = NULL; + TrNode_next(existing_child) = TrNode_child(parent); + TrNode_previous(TrNode_child(parent)) = existing_child; + TrNode_child(parent) = existing_child; + existing_child = TrNode_child(parent); } - search_child = TrNode_next(search_child); } while(search_child); } @@ -624,7 +651,7 @@ TrNode core_breadth_reduction(TrEngine engine, TrNode node, TrNode breadth_node, stack_args_base = stack_args = AUXILIARY_TERM_STACK; stack_top = AUXILIARY_TERM_STACK + CURRENT_AUXILIARY_TERM_STACK_SIZE - 1; node = TrNode_parent(TrNode_parent(node)); -// printf("start node: "); displaynode(node); + // printf("start node: "); displaynode(node); if (IS_FUNCTOR_NODE(node)) { while(IS_FUNCTOR_NODE(node)) node = TrNode_parent(node); @@ -633,7 +660,7 @@ TrNode core_breadth_reduction(TrEngine engine, TrNode node, TrNode breadth_node, child = TrNode_child(child); } else child = TrNode_child(node); -// printf("Chosen start node: "); displaynode(child); + // printf("Chosen start node: "); displaynode(child); if (IS_HASH_NODE(child)) { printf("HASH NODE ERROR: db_tries do not support hash nodes.\n"); abort(); @@ -729,7 +756,6 @@ TrNode core_breadth_reduction(TrEngine engine, TrNode node, TrNode breadth_node, /* nested trie: stop procedure and return nested trie node */ if (IS_FUNCTOR_NODE(TrNode_parent(child)) && (strcmp(YAP_AtomName(YAP_NameOfFunctor((YAP_Functor)(~ApplTag & TrNode_entry(TrNode_parent(child))))), NESTED_TRIE_TERM) == 0)) return child; - PUSH_DOWN(stack_args, TrNode_entry(child), stack_top); count++; if (IS_FUNCTOR_NODE(TrNode_parent(child))) { diff --git a/library/tries/core_dbtries.h b/library/tries/core_dbtries.h index 39b2b42f6..6a2e6f171 100644 --- a/library/tries/core_dbtries.h +++ b/library/tries/core_dbtries.h @@ -225,6 +225,6 @@ TrNode core_breadth_reduction(TrEngine engine, TrNode node, TrNode breadth_no YAP_Term core_get_trie_db_return_term(void); void core_set_trie_db_return_term(YAP_Term return_value); YAP_Int core_db_trie_get_optimization_level_count(YAP_Int opt_level); -void core_depth_breadth_trie_replace_nested_trie(TrNode node, YAP_Int nested_trie_id, YAP_Term new_term); +void core_depth_breadth_trie_replace_nested_trie(TrNode node, YAP_Int nested_trie_id, YAP_Term new_term, void (*construct_function)(TrNode), void (*destruct_function)(TrNode)); YAP_Int core_get_trie_db_opt_min_prefix(void); void core_set_trie_db_opt_min_prefix(YAP_Int min_prefix); diff --git a/library/tries/core_tries.c b/library/tries/core_tries.c index c3974a12b..f66380be2 100644 --- a/library/tries/core_tries.c +++ b/library/tries/core_tries.c @@ -807,7 +807,7 @@ YAP_Term get_entry(TrNode node, YAP_Term *stack_mark, TrNode *cur_node) { fprintf(stderr, "**************************************\n"); fprintf(stderr, " Tries core module: term stack full\n"); fprintf(stderr, "**************************************\n"); - fflush(stderr); + fflush(stderr); } for (i = index; i > CURRENT_INDEX; i--) stack_vars_base[i] = 0; @@ -875,10 +875,10 @@ YAP_Term get_entry(TrNode node, YAP_Term *stack_mark, TrNode *cur_node) { *cur_node = node; return t; } else if (t == FloatEndTag) { - volatile union { - double f; - YAP_Term p[SIZE_FLOAT_AS_TERM]; - } tf; /* to avoid gcc warning */ + volatile union { + double f; + YAP_Term p[SIZE_FLOAT_AS_TERM]; + } tf; /* to avoid gcc warning */ #ifdef TAG_LOW_BITS_32 node = TrNode_parent(node); tf.p[1] = TrNode_entry(node); @@ -915,29 +915,29 @@ void remove_entry(TrNode node) { while (parent) { if (TrNode_previous(node)) { if (IS_HASH_NODE(TrNode_child(parent))) { - TrHash hash = (TrHash) TrNode_child(parent); - TrHash_num_nodes(hash)--; - if (TrHash_num_nodes(hash)) { - if (TrNode_next(node)) { - TrNode_next(TrNode_previous(node)) = TrNode_next(node); - TrNode_previous(TrNode_next(node)) = TrNode_previous(node); - } else { - TrNode_next(TrNode_previous(node)) = NULL; - } - free_trie_node(node); - return; - } - free_hash_buckets(TrHash_buckets(hash), TrHash_num_buckets(hash)); - free_trie_hash(hash); + TrHash hash = (TrHash) TrNode_child(parent); + TrHash_num_nodes(hash)--; + if (TrHash_num_nodes(hash)) { + if (TrNode_next(node)) { + TrNode_next(TrNode_previous(node)) = TrNode_next(node); + TrNode_previous(TrNode_next(node)) = TrNode_previous(node); + } else { + TrNode_next(TrNode_previous(node)) = NULL; + } + free_trie_node(node); + return; + } + free_hash_buckets(TrHash_buckets(hash), TrHash_num_buckets(hash)); + free_trie_hash(hash); } else { - if (TrNode_next(node)) { - TrNode_next(TrNode_previous(node)) = TrNode_next(node); - TrNode_previous(TrNode_next(node)) = TrNode_previous(node); - } else { - TrNode_next(TrNode_previous(node)) = NULL; - } - free_trie_node(node); - return; + if (TrNode_next(node)) { + TrNode_next(TrNode_previous(node)) = TrNode_next(node); + TrNode_previous(TrNode_next(node)) = TrNode_previous(node); + } else { + TrNode_next(TrNode_previous(node)) = NULL; + } + free_trie_node(node); + return; } } else if (TrNode_next(node)) { TrNode_child(parent) = TrNode_next(node); @@ -963,7 +963,7 @@ void remove_child_nodes(TrNode node) { bucket = first_bucket + TrHash_num_buckets(hash); do { if (*--bucket) - remove_child_nodes(*bucket); + remove_child_nodes(*bucket); } while (bucket != first_bucket); free_hash_buckets(first_bucket, TrHash_num_buckets(hash)); free_trie_hash(hash); @@ -971,9 +971,9 @@ void remove_child_nodes(TrNode node) { } if (TrNode_next(node)) remove_child_nodes(TrNode_next(node)); - if (!IS_LEAF_TRIE_NODE(node)) + if (!IS_LEAF_TRIE_NODE(node)) { remove_child_nodes(TrNode_child(node)); - else { + } else { if (DATA_DESTRUCT_FUNCTION) (*DATA_DESTRUCT_FUNCTION)(node); DECREMENT_ENTRIES(CURRENT_TRIE_ENGINE); @@ -998,10 +998,10 @@ TrNode copy_child_nodes(TrNode parent_dest, TrNode child_source) { do { bucket_dest--; if (*--bucket_source) { - *bucket_dest = copy_child_nodes(parent_dest, *bucket_source); - TrNode_previous(*bucket_dest) = AS_TR_NODE_NEXT(bucket_dest); + *bucket_dest = copy_child_nodes(parent_dest, *bucket_source); + TrNode_previous(*bucket_dest) = AS_TR_NODE_NEXT(bucket_dest); } else - *bucket_dest = NULL; + *bucket_dest = NULL; } while (bucket_source != first_bucket_source); return (TrNode) hash_dest; } @@ -1039,18 +1039,18 @@ void traverse_and_add(TrNode parent_dest, TrNode parent_source) { do { child_source = *--bucket_source; while (child_source) { - /* parent_dest is not a leaf node */ - child_dest = trie_node_check(parent_dest, TrNode_entry(child_source)); - if (child_dest) { - if (IS_LEAF_TRIE_NODE(child_dest)) { - /* child_source is a leaf node */ - if (DATA_ADD_FUNCTION) - (*DATA_ADD_FUNCTION)(child_dest, child_source); - } else - /* child_dest and child_source are not leaf nodes */ - traverse_and_add(child_dest, child_source); - } - child_source = TrNode_next(child_source); + /* parent_dest is not a leaf node */ + child_dest = trie_node_check(parent_dest, TrNode_entry(child_source)); + if (child_dest) { + if (IS_LEAF_TRIE_NODE(child_dest)) { + /* child_source is a leaf node */ + if (DATA_ADD_FUNCTION) + (*DATA_ADD_FUNCTION)(child_dest, child_source); + } else + /* child_dest and child_source are not leaf nodes */ + traverse_and_add(child_dest, child_source); + } + child_source = TrNode_next(child_source); } } while (bucket_source != first_bucket_source); return; @@ -1060,12 +1060,12 @@ void traverse_and_add(TrNode parent_dest, TrNode parent_source) { child_dest = trie_node_check(parent_dest, TrNode_entry(child_source)); if (child_dest) { if (IS_LEAF_TRIE_NODE(child_dest)) { - /* child_source is a leaf node */ - if (DATA_ADD_FUNCTION) - (*DATA_ADD_FUNCTION)(child_dest, child_source); + /* child_source is a leaf node */ + if (DATA_ADD_FUNCTION) + (*DATA_ADD_FUNCTION)(child_dest, child_source); } else - /* child_dest and child_source are not leaf nodes */ - traverse_and_add(child_dest, child_source); + /* child_dest and child_source are not leaf nodes */ + traverse_and_add(child_dest, child_source); } child_source = TrNode_next(child_source); } @@ -1088,27 +1088,27 @@ void traverse_and_join(TrNode parent_dest, TrNode parent_source) { do { child_source = *--bucket_source; while (child_source) { - /* parent_dest is not a leaf node */ - child_dest = trie_node_check(parent_dest, TrNode_entry(child_source)); - if (child_dest) { - if (IS_LEAF_TRIE_NODE(child_dest)) { - /* child_source is a leaf node */ - if (DATA_ADD_FUNCTION) - (*DATA_ADD_FUNCTION)(child_dest, child_source); - } else - /* child_dest and child_source are not leaf nodes */ - traverse_and_join(child_dest, child_source); - } else { - child_dest = trie_node_check_insert(parent_dest, TrNode_entry(child_source)); - if (IS_LEAF_TRIE_NODE(child_source)) { - MARK_AS_LEAF_TRIE_NODE(child_dest); - INCREMENT_ENTRIES(CURRENT_TRIE_ENGINE); - if (DATA_COPY_FUNCTION) - (*DATA_COPY_FUNCTION)(child_dest, child_source); - } else + /* parent_dest is not a leaf node */ + child_dest = trie_node_check(parent_dest, TrNode_entry(child_source)); + if (child_dest) { + if (IS_LEAF_TRIE_NODE(child_dest)) { + /* child_source is a leaf node */ + if (DATA_ADD_FUNCTION) + (*DATA_ADD_FUNCTION)(child_dest, child_source); + } else + /* child_dest and child_source are not leaf nodes */ + traverse_and_join(child_dest, child_source); + } else { + child_dest = trie_node_check_insert(parent_dest, TrNode_entry(child_source)); + if (IS_LEAF_TRIE_NODE(child_source)) { + MARK_AS_LEAF_TRIE_NODE(child_dest); + INCREMENT_ENTRIES(CURRENT_TRIE_ENGINE); + if (DATA_COPY_FUNCTION) + (*DATA_COPY_FUNCTION)(child_dest, child_source); + } else TrNode_child(child_dest) = copy_child_nodes(child_dest, TrNode_child(child_source)); - } - child_source = TrNode_next(child_source); + } + child_source = TrNode_next(child_source); } } while (bucket_source != first_bucket_source); return; @@ -1118,19 +1118,19 @@ void traverse_and_join(TrNode parent_dest, TrNode parent_source) { child_dest = trie_node_check(parent_dest, TrNode_entry(child_source)); if (child_dest) { if (IS_LEAF_TRIE_NODE(child_dest)) { - /* child_source is a leaf node */ - if (DATA_ADD_FUNCTION) - (*DATA_ADD_FUNCTION)(child_dest, child_source); + /* child_source is a leaf node */ + if (DATA_ADD_FUNCTION) + (*DATA_ADD_FUNCTION)(child_dest, child_source); } else - /* child_dest and child_source are not leaf nodes */ - traverse_and_join(child_dest, child_source); + /* child_dest and child_source are not leaf nodes */ + traverse_and_join(child_dest, child_source); } else { child_dest = trie_node_check_insert(parent_dest, TrNode_entry(child_source)); if (IS_LEAF_TRIE_NODE(child_source)) { - MARK_AS_LEAF_TRIE_NODE(child_dest); - INCREMENT_ENTRIES(CURRENT_TRIE_ENGINE); - if (DATA_COPY_FUNCTION) - (*DATA_COPY_FUNCTION)(child_dest, child_source); + MARK_AS_LEAF_TRIE_NODE(child_dest); + INCREMENT_ENTRIES(CURRENT_TRIE_ENGINE); + if (DATA_COPY_FUNCTION) + (*DATA_COPY_FUNCTION)(child_dest, child_source); } else TrNode_child(child_dest) = copy_child_nodes(child_dest, TrNode_child(child_source)); } @@ -1155,27 +1155,27 @@ void traverse_and_intersect(TrNode parent_dest, TrNode parent_source) { do { child_dest = *--bucket_dest; while (child_dest) { - child_next = TrNode_next(child_dest); - /* parent_source is not a leaf node */ - child_source = trie_node_check(parent_source, TrNode_entry(child_dest)); - if (child_source) { - if (IS_LEAF_TRIE_NODE(child_dest)) { - /* child_source is a leaf node */ - if (DATA_ADD_FUNCTION) - (*DATA_ADD_FUNCTION)(child_dest, child_source); - } else - /* child_dest and child_source are not leaf nodes */ - traverse_and_intersect(child_dest, child_source); - } else { - if (IS_LEAF_TRIE_NODE(child_dest)) { - if (DATA_DESTRUCT_FUNCTION) - (*DATA_DESTRUCT_FUNCTION)(child_dest); - DECREMENT_ENTRIES(CURRENT_TRIE_ENGINE); - } else - remove_child_nodes(TrNode_child(child_dest)); - remove_entry(child_dest); - } - child_dest = child_next; + child_next = TrNode_next(child_dest); + /* parent_source is not a leaf node */ + child_source = trie_node_check(parent_source, TrNode_entry(child_dest)); + if (child_source) { + if (IS_LEAF_TRIE_NODE(child_dest)) { + /* child_source is a leaf node */ + if (DATA_ADD_FUNCTION) + (*DATA_ADD_FUNCTION)(child_dest, child_source); + } else + /* child_dest and child_source are not leaf nodes */ + traverse_and_intersect(child_dest, child_source); + } else { + if (IS_LEAF_TRIE_NODE(child_dest)) { + if (DATA_DESTRUCT_FUNCTION) + (*DATA_DESTRUCT_FUNCTION)(child_dest); + DECREMENT_ENTRIES(CURRENT_TRIE_ENGINE); + } else + remove_child_nodes(TrNode_child(child_dest)); + remove_entry(child_dest); + } + child_dest = child_next; } } while (bucket_dest != first_bucket_dest); return; @@ -1186,19 +1186,19 @@ void traverse_and_intersect(TrNode parent_dest, TrNode parent_source) { child_source = trie_node_check(parent_source, TrNode_entry(child_dest)); if (child_source) { if (IS_LEAF_TRIE_NODE(child_dest)) { - /* child_source is a leaf node */ - if (DATA_ADD_FUNCTION) - (*DATA_ADD_FUNCTION)(child_dest, child_source); + /* child_source is a leaf node */ + if (DATA_ADD_FUNCTION) + (*DATA_ADD_FUNCTION)(child_dest, child_source); } else - /* child_dest and child_source are not leaf nodes */ - traverse_and_intersect(child_dest, child_source); + /* child_dest and child_source are not leaf nodes */ + traverse_and_intersect(child_dest, child_source); } else { if (IS_LEAF_TRIE_NODE(child_dest)) { - if (DATA_DESTRUCT_FUNCTION) - (*DATA_DESTRUCT_FUNCTION)(child_dest); - DECREMENT_ENTRIES(CURRENT_TRIE_ENGINE); + if (DATA_DESTRUCT_FUNCTION) + (*DATA_DESTRUCT_FUNCTION)(child_dest); + DECREMENT_ENTRIES(CURRENT_TRIE_ENGINE); } else - remove_child_nodes(TrNode_child(child_dest)); + remove_child_nodes(TrNode_child(child_dest)); remove_entry(child_dest); } child_dest = child_next; @@ -1223,17 +1223,17 @@ YAP_Int traverse_and_count_common_entries(TrNode parent1, TrNode parent2) { do { child1 = *--bucket; while (child1) { - /* parent2 is not a leaf node */ - child2 = trie_node_check(parent2, TrNode_entry(child1)); - if (child2) { - if (IS_LEAF_TRIE_NODE(child1)) - /* child2 is a leaf node */ - count++; - else - /* child1 and child2 are not leaf nodes */ - count += traverse_and_count_common_entries(child1, child2); - } - child1 = TrNode_next(child1); + /* parent2 is not a leaf node */ + child2 = trie_node_check(parent2, TrNode_entry(child1)); + if (child2) { + if (IS_LEAF_TRIE_NODE(child1)) + /* child2 is a leaf node */ + count++; + else + /* child1 and child2 are not leaf nodes */ + count += traverse_and_count_common_entries(child1, child2); + } + child1 = TrNode_next(child1); } } while (bucket != first_bucket); return count; @@ -1243,11 +1243,11 @@ YAP_Int traverse_and_count_common_entries(TrNode parent1, TrNode parent2) { child2 = trie_node_check(parent2, TrNode_entry(child1)); if (child2) { if (IS_LEAF_TRIE_NODE(child1)) - /* child2 is a leaf node */ - count++; + /* child2 is a leaf node */ + count++; else - /* child1 and child2 are not leaf nodes */ - count += traverse_and_count_common_entries(child1, child2); + /* child1 and child2 are not leaf nodes */ + count += traverse_and_count_common_entries(child1, child2); } child1 = TrNode_next(child1); } @@ -1329,7 +1329,7 @@ void traverse_and_save(TrNode node, FILE *file, int float_block) { do { if (*--bucket) { node = *bucket; - traverse_and_save(node, file, float_block); + traverse_and_save(node, file, float_block); } } while (bucket != first_bucket); return; @@ -1356,23 +1356,23 @@ void traverse_and_save(TrNode node, FILE *file, int float_block) { int index; for (index = 0; index <= CURRENT_INDEX; index++) if (AUXILIARY_TERM_STACK[index] == t) - break; + break; if (index > CURRENT_INDEX) { CURRENT_INDEX = index; if (CURRENT_INDEX == CURRENT_AUXILIARY_TERM_STACK_SIZE) - expand_auxiliary_term_stack(); + expand_auxiliary_term_stack(); AUXILIARY_TERM_STACK[CURRENT_INDEX] = t; if (YAP_IsAtomTerm(t)) - fprintf(file, UInt_FORMAT " %d %s%c ", ATOM_SAVE_MARK, index, YAP_AtomName(YAP_AtomOfTerm(t)), '\0'); + fprintf(file, UInt_FORMAT " %d %s%c ", ATOM_SAVE_MARK, index, YAP_AtomName(YAP_AtomOfTerm(t)), '\0'); else /* (ApplTag & t) */ - fprintf(file, UInt_FORMAT " %d %s " UInt_FORMAT " ", FUNCTOR_SAVE_MARK, index, - YAP_AtomName(YAP_NameOfFunctor((YAP_Functor)(~ApplTag & t))), - YAP_ArityOfFunctor((YAP_Functor)(~ApplTag & t))); + fprintf(file, UInt_FORMAT " %d %s " UInt_FORMAT " ", FUNCTOR_SAVE_MARK, index, + YAP_AtomName(YAP_NameOfFunctor((YAP_Functor)(~ApplTag & t))), + YAP_ArityOfFunctor((YAP_Functor)(~ApplTag & t))); } else if (YAP_IsAtomTerm(t)) - fprintf(file, UInt_FORMAT " %d ", ATOM_SAVE_MARK, index); + fprintf(file, UInt_FORMAT " %d ", ATOM_SAVE_MARK, index); else - fprintf(file, UInt_FORMAT " %d ", FUNCTOR_SAVE_MARK, index); + fprintf(file, UInt_FORMAT " %d ", FUNCTOR_SAVE_MARK, index); } if (IS_LEAF_TRIE_NODE(node)) { fprintf(file, "- "); @@ -1415,34 +1415,34 @@ void traverse_and_load(TrNode parent, FILE *file) { int index; n = fscanf(file, "%d", &index); if (index > CURRENT_INDEX) { - char atom[1000]; - if (CURRENT_LOAD_VERSION == 2) { - char *ptr, ch; - ptr = atom; - fgetc(file); /* skip the first empty space */ - while ((ch = fgetc(file))) - *ptr++ = ch; - *ptr = '\0'; - } else if (CURRENT_LOAD_VERSION == 1) { - n = fscanf(file, "%s", atom); - } - CURRENT_INDEX = index; - if (CURRENT_INDEX == CURRENT_AUXILIARY_TERM_STACK_SIZE) - expand_auxiliary_term_stack(); - AUXILIARY_TERM_STACK[CURRENT_INDEX] = YAP_MkAtomTerm(YAP_LookupAtom(atom)); + char atom[1000]; + if (CURRENT_LOAD_VERSION == 2) { + char *ptr, ch; + ptr = atom; + fgetc(file); /* skip the first empty space */ + while ((ch = fgetc(file))) + *ptr++ = ch; + *ptr = '\0'; + } else if (CURRENT_LOAD_VERSION == 1) { + n = fscanf(file, "%s", atom); + } + CURRENT_INDEX = index; + if (CURRENT_INDEX == CURRENT_AUXILIARY_TERM_STACK_SIZE) + expand_auxiliary_term_stack(); + AUXILIARY_TERM_STACK[CURRENT_INDEX] = YAP_MkAtomTerm(YAP_LookupAtom(atom)); } t = AUXILIARY_TERM_STACK[index]; } else if (t == FUNCTOR_SAVE_MARK) { int index; n = fscanf(file, "%d", &index); if (index > CURRENT_INDEX) { - char atom[1000]; - int arity; - n = fscanf(file, "%s %d", atom, &arity); - CURRENT_INDEX = index; - if (CURRENT_INDEX == CURRENT_AUXILIARY_TERM_STACK_SIZE) - expand_auxiliary_term_stack(); - AUXILIARY_TERM_STACK[CURRENT_INDEX] = ApplTag | ((YAP_Term) YAP_MkFunctor(YAP_LookupAtom(atom), arity)); + char atom[1000]; + int arity; + n = fscanf(file, "%s %d", atom, &arity); + CURRENT_INDEX = index; + if (CURRENT_INDEX == CURRENT_AUXILIARY_TERM_STACK_SIZE) + expand_auxiliary_term_stack(); + AUXILIARY_TERM_STACK[CURRENT_INDEX] = ApplTag | ((YAP_Term) YAP_MkFunctor(YAP_LookupAtom(atom), arity)); } t = AUXILIARY_TERM_STACK[index]; } else if (t == FLOAT_SAVE_MARK) @@ -1473,15 +1473,15 @@ void traverse_and_print(TrNode node, int *arity, char *str, int str_index, int m if (*--bucket) { node = *bucket; traverse_and_print(node, arity, str, str_index, mode); - memcpy(arity, current_arity, sizeof(int) * (current_arity[0] + 1)); - if (mode != TRIE_PRINT_FLOAT2 && arity[arity[0]] < 0) { - /* restore possible PairEndEmptyTag/PairEndTermTag/CommaEndTag side-effect */ - if (str_index > 0 && str[str_index - 1] != '[') - str[str_index - 1] = ','; - /* restore possible PairEndTermTag side-effect */ - if (str[last_pair_mark] == '|') - str[last_pair_mark] = ','; - } + memcpy(arity, current_arity, sizeof(int) * (current_arity[0] + 1)); + if (mode != TRIE_PRINT_FLOAT2 && arity[arity[0]] < 0) { + /* restore possible PairEndEmptyTag/PairEndTermTag/CommaEndTag side-effect */ + if (str_index > 0 && str[str_index - 1] != '[') + str[str_index - 1] = ','; + /* restore possible PairEndTermTag side-effect */ + if (str[last_pair_mark] == '|') + str[last_pair_mark] = ','; + } } } while (bucket != first_bucket); free(current_arity); @@ -1496,10 +1496,10 @@ void traverse_and_print(TrNode node, int *arity, char *str, int str_index, int m if (mode != TRIE_PRINT_FLOAT2 && arity[arity[0]] < 0) { /* restore possible PairEndEmptyTag/PairEndTermTag/CommaEndTag side-effect */ if (str_index > 0 && str[str_index - 1] != '[') - str[str_index - 1] = ','; + str[str_index - 1] = ','; /* restore possible PairEndTermTag side-effect */ if (str[last_pair_mark] == '|') - str[last_pair_mark] = ','; + str[last_pair_mark] = ','; } free(current_arity); } @@ -1534,13 +1534,13 @@ void traverse_and_print(TrNode node, int *arity, char *str, int str_index, int m arity[0]--; while (arity[0]) { if (arity[arity[0]] == 1) { - str_index += sprintf(& str[str_index], ")"); - arity[0]--; + str_index += sprintf(& str[str_index], ")"); + arity[0]--; } else { - if (arity[arity[0]] > 1) - arity[arity[0]]--; - str_index += sprintf(& str[str_index], ","); - break; + if (arity[arity[0]] > 1) + arity[arity[0]]--; + str_index += sprintf(& str[str_index], ","); + break; } } mode = TRIE_PRINT_NORMAL; @@ -1548,39 +1548,39 @@ void traverse_and_print(TrNode node, int *arity, char *str, int str_index, int m str_index += sprintf(& str[str_index], "VAR" UInt_FORMAT, TrieVarIndex(t)); while (arity[0]) { if (arity[arity[0]] == 1) { - str_index += sprintf(& str[str_index], ")"); - arity[0]--; + str_index += sprintf(& str[str_index], ")"); + arity[0]--; } else { - if (arity[arity[0]] > 1) - arity[arity[0]]--; - str_index += sprintf(& str[str_index], ","); - break; + if (arity[arity[0]] > 1) + arity[arity[0]]--; + str_index += sprintf(& str[str_index], ","); + break; } } } else if (YAP_IsAtomTerm(t)) { str_index += sprintf(& str[str_index], "%s", YAP_AtomName(YAP_AtomOfTerm(t))); while (arity[0]) { if (arity[arity[0]] == 1) { - str_index += sprintf(& str[str_index], ")"); - arity[0]--; + str_index += sprintf(& str[str_index], ")"); + arity[0]--; } else { - if (arity[arity[0]] > 1) - arity[arity[0]]--; - str_index += sprintf(& str[str_index], ","); - break; + if (arity[arity[0]] > 1) + arity[arity[0]]--; + str_index += sprintf(& str[str_index], ","); + break; } } } else if (YAP_IsIntTerm(t)) { str_index += sprintf(& str[str_index], UInt_FORMAT , YAP_IntOfTerm(t)); while (arity[0]) { if (arity[arity[0]] == 1) { - str_index += sprintf(& str[str_index], ")"); - arity[0]--; + str_index += sprintf(& str[str_index], ")"); + arity[0]--; } else { - if (arity[arity[0]] > 1) - arity[arity[0]]--; - str_index += sprintf(& str[str_index], ","); - break; + if (arity[arity[0]] > 1) + arity[arity[0]]--; + str_index += sprintf(& str[str_index], ","); + break; } } } else if (YAP_IsPairTerm(t)) { @@ -1598,23 +1598,23 @@ void traverse_and_print(TrNode node, int *arity, char *str, int str_index, int m arity[arity[0]] = -1; } else { if (t == PairEndEmptyTag) - str[str_index - 1] = ']'; + str[str_index - 1] = ']'; else if (t == PairEndTermTag) { - str[last_pair_mark] = '|'; - str[str_index - 1] = ']'; + str[last_pair_mark] = '|'; + str[str_index - 1] = ']'; } else /* (t == CommaEndTag) */ - str[str_index - 1] = ')'; + str[str_index - 1] = ')'; arity[0]--; while (arity[0]) { - if (arity[arity[0]] == 1) { - str_index += sprintf(& str[str_index], ")"); - arity[0]--; - } else { - if (arity[arity[0]] > 1) - arity[arity[0]]--; - str_index += sprintf(& str[str_index], ","); - break; - } + if (arity[arity[0]] == 1) { + str_index += sprintf(& str[str_index], ")"); + arity[0]--; + } else { + if (arity[arity[0]] > 1) + arity[arity[0]]--; + str_index += sprintf(& str[str_index], ","); + break; + } } } } else if (ApplTag & t) {