ext/figure_set/and.c in figure_set-1.0.0 vs ext/figure_set/and.c in figure_set-1.0.1

- old
+ new

@@ -6,113 +6,120 @@ #include <stdlib.h> #include <string.h> #include <ruby.h> #include "figure_set.h" -// -// intersection -// -void intersection(root_node result_set, root_node set0, root_node set1) +static void middel_intersection_branch_node(root_node, branch_node, branch_node, branch_node); +static void last_intersection_branch_node(root_node, branch_node, branch_node, branch_node); + +static branch_node +init_and_intersection_branch_node(root_node result_set, branch_node base, branch_node other) { - unsigned int i, count; - root_node base, other; - branch_node branch; + branch_node branch; - if (set0->size == 0 || set1->size == 0) { - return; - } else if (set0->size > set1->size) { - base = set1; - other = set0; - } else { - base = set0; - other = set1; - } + branch = (branch_node)init_branch_node(); - for (i = 0, count = 0; i < MAX_CHILDREN_SIZE_OF_ROOT_NODE || count < base->children_size; i++) { - if (base->index[i]) { - count++; - if (other->index[i]) { - branch = (branch_node)init_and_intersection_branch_node(result_set, (branch_node)base->index[i], (branch_node)other->index[i]); - if (branch) { - result_set->index[i] = (void*)branch; - result_set->children_size++; - } - } - } - } + if (base->children_type == CT_LEAF) { + last_intersection_branch_node(result_set, branch, base, other); + } else { + middel_intersection_branch_node(result_set, branch, base, other); + } + + if (branch->children_size) { + branch->children_type = base->children_type; + return branch; + } else { + destroy_branch(branch); + return (branch_node)NULL; + } } -void *init_and_intersection_branch_node(root_node result_set, branch_node base, branch_node other) +static leaf_node +init_and_intersection_leaf_node(root_node result_set, leaf_node base, leaf_node other) { - branch_node branch; + unsigned long data; + leaf_node leaf; - branch = (branch_node)init_branch_node(); + data = base->data & other->data; - if (base->children_type == CT_LEAF) { - last_intersection_branch_node(result_set, branch, base, other); - } else { - middel_intersection_branch_node(result_set, branch, base, other); - } + if (!data) return (leaf_node)NULL; - if (branch->children_size) { - branch->children_type = base->children_type; - return (void*)branch; - } else { - destroy_branch(branch); - return (void*)NULL; - } + leaf = (leaf_node)init_leaf_node(base->offset); + leaf->data = data; + result_set->size += BIT_COUNT(leaf->data); + + return leaf; } -void middel_intersection_branch_node(root_node result_set, branch_node branch, branch_node base, branch_node other) +static void +middel_intersection_branch_node(root_node result_set, branch_node branch, branch_node base, branch_node other) { - unsigned int i, count; - branch_node middle_branch; + unsigned int i, count; + branch_node middle_branch; - for (i = 0, count = 0; i < MAX_CHILDREN_SIZE_OF_BRANCH || count < base->children_size; i++) { - if (base->index[i]) { - count++; - if (other->index[i]) { - middle_branch = (branch_node)init_and_intersection_branch_node(result_set, (branch_node)base->index[i], (branch_node)other->index[i]); - if (middle_branch) { - branch->index[i] = (void*)middle_branch; - branch->children_size++; - } - } + for (i = 0, count = 0; i < MAX_CHILDREN_SIZE_OF_BRANCH || count < base->children_size; i++) { + if (base->index[i]) { + count++; + if (other->index[i]) { + middle_branch = (branch_node)init_and_intersection_branch_node(result_set, (branch_node)base->index[i], (branch_node)other->index[i]); + if (middle_branch) { + branch->index[i] = (void*)middle_branch; + branch->children_size++; } + } } + } } -void last_intersection_branch_node(root_node result_set, branch_node branch, branch_node base, branch_node other) +static void +last_intersection_branch_node(root_node result_set, branch_node branch, branch_node base, branch_node other) { - unsigned int i, count; - leaf_node leaf; + unsigned int i, count; + leaf_node leaf; - for (i = 0, count = 0; i < MAX_CHILDREN_SIZE_OF_BRANCH || count < base->children_size; i++) { - if (base->index[i]) { - count++; - if (other->index[i]) { - leaf = (leaf_node)init_and_intersection_leaf_node(result_set, (leaf_node)base->index[i], (leaf_node)other->index[i]); - if (leaf) { - branch->index[i] = (void*)leaf; - branch->children_size++; - } - } + for (i = 0, count = 0; i < MAX_CHILDREN_SIZE_OF_BRANCH || count < base->children_size; i++) { + if (base->index[i]) { + count++; + if (other->index[i]) { + leaf = (leaf_node)init_and_intersection_leaf_node(result_set, (leaf_node)base->index[i], (leaf_node)other->index[i]); + if (leaf) { + branch->index[i] = (void*)leaf; + branch->children_size++; } + } } + } } - -void *init_and_intersection_leaf_node(root_node result_set, leaf_node base, leaf_node other) +// +// intersection +// +void +intersection(root_node result_set, root_node set0, root_node set1) { - unsigned long data; - leaf_node leaf; - - data = base->data & other->data; + unsigned int i, count; + root_node base, other; + branch_node branch; - if (!data) return (void*)NULL; + if (set0->size == 0 || set1->size == 0) { + return; + } else if (set0->size > set1->size) { + base = set1; + other = set0; + } else { + base = set0; + other = set1; + } - leaf = (leaf_node)init_leaf_node(base->offset); - leaf->data = data; - result_set->size += BIT_COUNT(leaf->data); - - return (void*)leaf; + for (i = 0, count = 0; i < MAX_CHILDREN_SIZE_OF_ROOT_NODE || count < base->children_size; i++) { + if (base->index[i]) { + count++; + if (other->index[i]) { + branch = (branch_node)init_and_intersection_branch_node(result_set, (branch_node)base->index[i], (branch_node)other->index[i]); + if (branch) { + result_set->index[i] = (void*)branch; + result_set->children_size++; + } + } + } + } }