static bool ucl_schema_array_is_unique (const ucl_object_t *obj, struct ucl_schema_error *err) { ucl_compare_tree_t tree = TREE_INITIALIZER (ucl_schema_elt_compare); ucl_object_iter_t iter = NULL; const ucl_object_t *elt; struct ucl_compare_node *node, test, *nodes = NULL, *tmp; bool ret = true; while ((elt = ucl_iterate_object (obj, &iter, true)) != NULL) { test.obj = elt; node = TREE_FIND (&tree, ucl_compare_node, link, &test); if (node != NULL) { ucl_schema_create_error (err, UCL_SCHEMA_CONSTRAINT, elt, "duplicate values detected while uniqueItems is true"); ret = false; break; } node = calloc (1, sizeof (*node)); if (node == NULL) { ucl_schema_create_error (err, UCL_SCHEMA_UNKNOWN, elt, "cannot allocate tree node"); ret = false; break; } node->obj = elt; TREE_INSERT (&tree, ucl_compare_node, link, node); LL_PREPEND (nodes, node); } LL_FOREACH_SAFE (nodes, node, tmp) { free (node); }
int main(int argc, char **argv) { int i; Tree tree= TREE_INITIALIZER(Node_compare); mt_random_init(); for (i= 0; i < 100; ++i) { Node v= { mt_random() % 1000, i }; Node *vv= TREE_FIND(&tree, _Node, tree, &v); if (vv) { printf("already inserted "); Node_print(vv, stdout); printf("\n"); } else { printf("insert " ); Node_print(&v, stdout); printf("\n"); TREE_INSERT(&tree, _Node, tree, Node_new(v.key, v.value)); TREE_FORWARD_APPLY(&tree, _Node, tree, Node_printer, stdout); printf("\n"); } } TREE_FORWARD_APPLY(&tree, _Node, tree, Node_printer, stdout); printf("\n"); for (i= 0; i < 1000; ++i) { Node *v= Node_new(mt_random() % 1000, 0); Node *vv= TREE_FIND(&tree, _Node, tree, v); printf("looking for %d - ", v->key); if (vv) { printf("found "); Node_print(vv, stdout); printf("\n"); TREE_FORWARD_APPLY(&tree, _Node, tree, Node_printer, stdout); printf("\n"); } else { printf("not found\n"); } TREE_REMOVE(&tree, _Node, tree, v); } TREE_FORWARD_APPLY(&tree, _Node, tree, Node_printer, stdout); printf("\n"); return 0; }
Node *Node_new(char *ipaddr) { Node *self; if ((self = malloc(sizeof(Node))) != NULL) { memset(self, 0, sizeof(Node)); self->id = CURRENT_ID; strncpy(self->ipaddr, ipaddr, INET_ADDRSTRLEN); _dprintf("%s: new node ip=%s, version=%d, sizeof(Node)=%d (bytes)\n", __FUNCTION__, self->ipaddr, self->id, sizeof(Node)); } return self; } int Node_compare(Node *lhs, Node *rhs) { return strncmp(lhs->ipaddr, rhs->ipaddr, INET_ADDRSTRLEN); } Tree tree = TREE_INITIALIZER(Node_compare); #ifdef DEBUG_CSTATS void Node_print(Node *self, FILE *stream) { fprintf(stream, "%s", self->ipaddr); } void Node_printer(Node *self, void *stream) { Node_print(self, (FILE *)stream); fprintf((FILE *)stream, " "); } void Tree_info(void) { // _dprintf("Tree = "); // TREE_FORWARD_APPLY(&tree, _Node, linkage, Node_printer, stdout); // _dprintf("\n");
void Mainloop() { long closedset_sz = 0; long openset_sz = 0; //long start_time = clock(); time_t time_start,time_end; time(&time_start); Tree tree= TREE_INITIALIZER(Node_compare); _Node p0 = {0}; p0.uXYZ.lXYZ[0] = 0; p0.uXYZ.lXYZ[1] = 0; ComputeF(&p0); _Node* temp = Node_new(&p0); temp->uXYZ.lXYZ[0] = 0; temp->uXYZ.lXYZ[1] = 0; _Node* p = Node_new(temp); TREE_INSERT(&tree, _Node, linkage, p); HeapInsert(p); openset_sz++; while(true) { _Node* p = HeapRemove(); p->expended = true; closedset_sz++; openset_sz--; if (p->h == 0) { printf("\nThe MLCS is:\n"); int l = CommonSeq(p); printf("\n\n|MLCS|=%d\n", l); break; } for(int i=0; i<ABC_len; i++) { int j=0; if (NUMBYTE == 1) { while (j<MAXSTR) { temp->uXYZ.cXYZ[j] = cur_T[j][p->uXYZ.cXYZ[j]+1][i]; if (temp->uXYZ.cXYZ[j]==str_lngth[j]+1) break; j++; } } else { while (j<MAXSTR) { temp->uXYZ.sXYZ[j] = cur_T[j][p->uXYZ.sXYZ[j]+1][i]; if (temp->uXYZ.sXYZ[j]==str_lngth[j]+1) break; j++; } } if (j < MAXSTR) continue; _Node *q = TREE_FIND(&tree, _Node, linkage, temp); if (q) { //node is already in tree if (!q->expended && (q->fg & 0xffff) < (p->fg & 0xffff)+1) { q->parent = p; q->fg = ((q->h+(p->fg & 0xffff)+1) << 16) | ((p->fg & 0xffff)+1); } } else { q = Node_new(temp); q->parent = p; q->fg = p->fg+1; ComputeF(q); TREE_INSERT(&tree, _Node, linkage, q); HeapInsert(q); openset_sz++; } } } printf("|closedset|=%d\n|openset|=%d\n\n", closedset_sz, openset_sz+1); time(&time_end); double elapsedTime = difftime(time_end,time_start); printf("The time is %6.2f seconds\n",elapsedTime); }