int set_union(Set *setu, const Set *set1, const Set *set2) { ListElmt *member; void *data; set_init(setu, set1->match, NULL); for (member = list_head(set1); member != NULL; member = list_next(member)) { data = list_data(member); if (list_ins_next(setu, list_tail(setu), data) != 0) { set_destroy(setu); return -1; } } for (member = list_head(set2); member != NULL; member = list_next(member)) { if (set_is_member(set1, list_data(member))) { continue; } else { data = list_data(member); if (list_ins_next(setu, list_tail(setu), data) != 0) { set_destroy(setu); return -1; } } } return 0; }
static void test_ops(void) { Set *s1 = set_create(); Set *s2 = set_create(); Set *s3 = set_create(); if (s1 == 0 || s2 == 0 || s3 == 0) err_syserr("Out of memory\n"); load_set(s1, 1, 3, 4, 6); dump_set("S1", s1); load_set(s2, 2, 5, 7, 9); dump_set("S2", s2); set_union(s1, s2, s3); dump_set("S1 union S2", s3); set_empty(s3); set_intersect(s1, s2, s3); dump_set("S1 intersect S2", s3); set_empty(s3); set_difference(s1, s2, s3); dump_set("S1 minus S2", s3); set_empty(s3); set_difference(s2, s1, s3); dump_set("S2 minus S1", s3); set_destroy(s1); set_destroy(s2); set_destroy(s3); }
int set_union(Set *st, const Set *st1, const Set *st2){ set_init(st, st1->destroy, st1->match); Node *pCurrentNode = list_head(st1); while(pCurrentNode){ /* trust use's st1 is a really set(no duplicate node) */ if(list_push(st, list_node_data(pCurrentNode)) != 0){ set_destroy(st); return -1; } pCurrentNode = list_node_next(pCurrentNode); } pCurrentNode = list_head(st2); while(pCurrentNode){ if(!set_is_member(st, list_node_data(pCurrentNode))){ if(list_push(st, list_node_data(pCurrentNode)) != 0){ set_destroy(st); return -1; } } pCurrentNode = list_node_next(pCurrentNode); } return 0; }
/* graph_destroy */ void graph_destroy(Graph *graph) { AdjList *adjlist; /* Remove each adjacency-list structure and destroy its adjacency list.*/ while (list_size(&graph->adjlists) > 0) { if (list_rem_next(&graph->adjlists, NULL, (void **)&adjlist) == 0) { set_destroy(&adjlist->adjacent); if (graph->destroy != NULL) { graph->destroy(adjlist->vertex); } free(adjlist); } } /* Destroy the list of adjacency-list structures, which is now empty. */ list_destroy(&graph->adjlists); /* No operation are allowed now, * but clear the structures as a precaution. */ memset(graph, 0, sizeof(Graph)); return; }
int set_intersection(Set *seti, const Set *set1, const Set *set2) { ListElmt *member; void *data; /* * Initialize set */ set_init(seti, set1->match, NULL); /* * Insert elements that are in both sets */ member = list_head(set1); for ( ; member != NULL; member = list_next(member)) { if (set_is_member(set2, list_data(member))) { data = list_data(member); if (list_ins_next(seti, list_tail(seti), data) != 0) { set_destroy(seti); return -1; } } } return 0; }
int main(int argc, char **argv) { struct set *set; const char *str1 = "test1"; const char *str2 = "test2"; const char *str3 = "test3"; set = set_create(badhash, string_key_equals); /* Unlike the hash table equivalent of this test, use an extra * string so that the rehash happens at the right time. */ set_add(set, str1); set_add(set, str2); set_add(set, str3); set_remove(set, str2); set_add(set, str3); set_remove(set, str3); assert(!set_contains(set, str3)); set_destroy(set, NULL); return 0; }
void bfs_data_structure_destroy(set_t** visited_ptr, queue_t** queue_ptr) { assert(queue_ptr != NULL); struct bfs_node_struct *bfs_node_ptr = NULL; /* Check if the set is already destroyed */ if(*visited_ptr != NULL) { /* Destroy the set */ set_destroy(visited_ptr); *visited_ptr = NULL; } /* Check if the queue is already destroyed */ if(*queue_ptr != NULL) { /* Free all queue nodes */ while(!queue_empty(*queue_ptr)) { if(queue_dequeue(*queue_ptr, (void*)&bfs_node_ptr)) free(bfs_node_ptr); else break; } /* Destroy the queue */ queue_destroy(queue_ptr); *queue_ptr = NULL; } }
/* set_differece */ int set_differece(Set *setd, const Set *set1, const Set *set2) { ListElmt *member; void *data; /* Initialize the set for the difference. */ set_init(setd, set1->match, NULL); /* Insert the member from set1 not in set2. */ for (member = list_head(set1); member != NULL; member = list_next(member)) { if (!set_is_member(set2, list_data(member))) { data = list_data(member); if (list_ins_next(setd, list_tail(setd), data) != 0) { set_destroy(setd); return -1; } } } return 0; }
int set_difference(Set *setd, const Set *set1, const Set *set2) { ListElmt *member; void *data; /* * Initialize set */ set_init(setd, set1->match, NULL); /* * Insert elements that are in set1 but not set2 */ member = list_head(set1); for ( ; member != NULL; member = list_next(member)) { if (!set_is_member(set2, list_data(member))) { data = list_data(member); if (list_ins_next(setd, list_tail(setd), data) != 0) { set_destroy(setd); return -1; } } } return 0; }
int main(int argc, char *argv[]) { int i, size = 100, *x; struct set *s, *t, *u; if (argc > 1) size = atoi(argv[1]); srand(time(NULL)); s = set_create(size, sizeof(int), cmpint); t = set_create(size, sizeof(int), cmpint); if (s == NULL || t == NULL){ printf("Unable to create the test set\n"); return 1; } printf("Inserting into first set...\n"); for (i = 0; i < size; i++){ int y = rand() % size; printf("%d ", y); set_insert(s, &y); } printf("\n\n"); printf("The set contains:\n"); for (set_reset(s), x = set_next(s); x != NULL; x = set_next(s)) printf("%d ", *x); printf("\n\n"); printf("Inserting into second set...\n"); for (i = 0; i < size; i++){ int y = rand() % size; printf("%d ", y); set_insert(t, &y); } printf("\n\n"); printf("The set contains:\n"); for (set_reset(t), x = set_next(t); x != NULL; x = set_next(t)) printf("%d ", *x); printf("\n\n"); u = set_union(s, t); printf("The union of the two sets is:\n"); for (set_reset(u), x = set_next(u); x != NULL; x = set_next(u)) printf("%d ", *x); printf("\n\n"); set_destroy(u); u = set_diff(s, t); printf("The difference of the two sets is:\n"); for (set_reset(u), x = set_next(u); x != NULL; x = set_next(u)) printf("%d ", *x); printf("\n\n"); return 0; }
void solver_bfs_destroy(solver *s) { bfs_data bfs = *((bfs_data *) solver_get_data(s)); set_destroy(bfs.visited); list_destroy(bfs.queue); map_destroy(bfs.parent); free(solver_get_data(s)); solver_destroy(s); }
static void test_set(void) { Set *set = set_create(); if (set == 0) err_syserr("Out of memory\n"); load_set(set, 1, 3, 4, 6); dump_set("S0", set); set_destroy(set); }
/* * NOTE: We expect setu to be uninitialized and empty. */ int set_union(Set *setu, const Set *set1, const Set *set2) { ListElmt *member; void *data; /* * Initialize set */ set_init(setu, set1->match, NULL); /* * Insert members of first set */ member = list_head(set1); for ( ; member != NULL; member = list_next(member)) { data = list_data(member); if (list_ins_next(setu, list_tail(setu), data) != 0) { set_destroy(setu); return -1; } } /* * Insert members of second set */ member = list_head(set2); for ( ; member != NULL; member = list_next(member)) { if (set_is_member(setu, list_data(member))) continue; data = list_data(member); if (list_ins_next(setu, list_tail(setu), data) != 0) { set_destroy(setu); return -1; } } return 0; }
int main(int argc, char *argv[]) { set *s = set_init(10); set *t = set_init(3); set *r; set_iter i; char *p; set_add(s, "calvin"); set_add(s, "hobbes"); set_add(s, "alfred"); set_add(s, "hobbes"); set_add(t, "batman"); set_add(t, "robin"); set_add(t, "calvin"); set_add(t, "kevin bacon"); set_add(t, "alfred"); /*printf("len: %d\n", set_size(s)); printf("calvin: %d\nhobz: %d\n", set_ismember(s, "calvin"), set_ismember(s, "hobz"));*/ r = set_xor(s, t); set_iter_init(r, &i); while((p = set_iterate(r, &i)) != NULL) { printf("member: %s\n", p); } set_destroy(s); set_destroy(t); set_destroy(r); return 0; }
/* set_union */ int set_union(Set *setu, const Set *set1, const Set *set2) { ListElmt *member; void *data; /* Initialize the set for the union. */ set_init(setu, set1->match, NULL); /* Insert the members of the first set. */ for (member = list_head(set1); member != NULL; member = list_next(member)) { data = list_data(member); if (list_ins_next(setu, list_tail(setu), data) != 0) { set_destroy(setu); return -1; } } /* Insert the members of the second set. */ for (member = list_head(set2); member != NULL; member = list_next(member)) { if (set_is_member(set1, list_data(member))) { /* Do not allow the insertion of duplicates. */ continue; } else { data = list_data(member); if (list_ins_next(setu, list_tail(setu), data) != 0) { set_destroy(setu); return -1; } } } return 0; }
range* range_from_braces(range_request* rr, const range* r1, const range* r2, const range* r3) { int i, j, k; set_element** m1; set_element** m2; set_element** m3; set* temp = NULL; range* bigrange; char* bundle; apr_pool_t* pool = range_request_pool(rr); if(r1->nodes->members == 0) { if(!temp) { temp = set_new(pool, 1); set_add(temp, "", NULL); } m1 = set_members(temp); } else m1 = set_members(r1->nodes); if(r2->nodes->members == 0) { if(!temp) { temp = set_new(pool, 1); set_add(temp, "", NULL); } m2 = set_members(temp); } else m2 = set_members(r2->nodes); if(r3->nodes->members == 0) { if(!temp) { temp = set_new(pool, 1); set_add(temp, "", NULL); } m3 = set_members(temp); } else m3 = set_members(r3->nodes); bigrange = range_new(rr); for(i = 0; m1[i]; i++) for(j = 0; m2[j]; j++) for(k = 0; m3[k]; k++) { bundle = apr_pstrcat(pool, m1[i]->name, m2[j]->name, m3[k]->name, NULL); range_add(bigrange, bundle); } if (temp) set_destroy(temp); bigrange->quoted = r1->quoted || r2->quoted || r3->quoted; return bigrange; }
int set_union(Set *setu, const Set *set1, const Set *set2) { ListElem *member; void *data; //init union set_init(setu, set1->match, NULL); //insert members of the first set for (member = list_head(set1); member != NULL; member = list_next(member)) { data = list_data(member); if (list_ins_next(setu,list_tail(setu),data)!=0) { set_destroy(setu); return -1; } } //insert members of the second set for (member = list_head(set2); member != NULL; member = list_next(member)) { if (set_is_member(set1,list_data(member))) { //no duplicates continue; } else { data = list_data(member); if (list_ins_next(setu,list_tail(setu),data)!=0) { set_destroy(setu); return -1; } } } return 0; };
int set_union(Set *setu, const Set *set1, const Set *set2) { ListElmt *member; void *data; set_init(setu, set1->match, NULL); /* Insert all members of the first set */ for (member = list_head(set1); member != NULL; member = list_next(member)) { data = list_data(member); if (list_ins_next(setu, list_tail(setu), data) != 0) { set_destroy(setu); return -1; } } /* Insert members of the second set not already present */ for (member = list_head(set2); member != NULL; member = list_next(member)) { if (set_is_member(set1, list_data(member))) { continue; } else { data = list_data(member); if (list_ins_next(setu, list_tail(setu), data) != 0) { set_destroy(setu); return -1; } } } return 0; }
int set_difference(Set *st, const Set *st1, const Set *st2){ set_init(st, st1->destroy, st1->match); Node *pCurrentNode = list_head(st1); while(pCurrentNode){ if(!set_is_member(st2, list_node_data(pCurrentNode))){ if(set_insert(st, list_node_data(pCurrentNode))!=0){ set_destroy(st); return -1; } } pCurrentNode = list_node_next(pCurrentNode); } return 0; }
int cover(Set *members, Set *subsets, Set *covering){ Set intersection; KSet *subset; ListElmt *member, *max_member; void *data; int max_size; //Initialize the covering. set_init(covering, subsets->match, NULL); //Continue while there are noncovered members and candidate subsets. while(set_size(members)>0 && set_size(subsets)>0){ //Find the subset that covers the most members. max_size = 0; for(member=list_head(subsets); member!=NULL; member=list_next(member)){ if(set_intersection(&intersection, &((KSet *)list_data(member))->set, members) != 0) return -1; if(set_size(&intersection)>max_size){ max_member = member; max_size = set_size(&intersection); } set_destroy(&intersection); } //A covering is not possible if there was no intersection. if(max_size == 0) return 1; //Insert the selected subset into the covering. subset = (KSet *)list_data(max_member); if(set_insert(covering, subset) != 0) return -1; //Remove each covered member from the set of noncovered members. for(member=list_head(&((KSet *)list_data(max_member))->set); member!=NULL; member=list_next(member)){ data = list_data(member); if(set_remove(members, (void**)&data)==0 && members->destroy!=NULL) members->destroy(data); } //Remove the subset from the set of candidate subsets. if(set_remove(subsets, (void **)&subset) != 0) return -1; } //No covering is possible if there are still noncovered members. if(set_size(members) > 0) return -1; return 0; }
int main(int argc, char **argv) { int i = 0; int arg_data_index = 0; struct cmdline_opt options; set_t *set; arg_data_index = parse_args(argc, argv, &options); set = set_create(); for (i = arg_data_index; i < argc; i++) { process_datafile(argv[i], set, &options); } set_destroy(set); return EXIT_SUCCESS; }
int set_intersection(Set *seti, const Set *set1, const Set *set2) { ListElmt *member; void *data; set_init(seti, set1->match, NULL); for (member = list_head(set1); member != NULL; member = list_next(member)) { if (set_is_member(set2, list_data(member))) { data = list_data(member); if (list_ins_next(seti, list_tail(seti), data) != 0) { set_destroy(seti); return -1; } } } return 0; }
void graph_destroy(Graph *graph) { AdjList *adjlist; // 移除每一个相邻的链表结构和链表 while(list_size(&graph->adjlists) > 0) { if (list_rem_next(&graph->adjlists, NULL, (void **)&adjlist) == 0) { set_destroy(&adjlist->adjacent); if (graph->destroy != NULL) { graph->destroy(adjlist->vertex); free(adjlist); } } } list_destory(&graph->adjlists); memset(graph, 0 sizeof(Graph)); return; }
int set_difference(Set *setd, const Set *set1, const Set *set2) { ListElmt *member; void *data; set_init(setd, set1->match, NULL); /* Insert members in set 1 that are not in set 2 */ for (member = list_head(set1); member != NULL; member = list_next(member)) { if (!set_is_member(set2, list_data(member))) { data = list_data(member); if (list_ins_next(setd, list_tail(setd), data) != 0) { set_destroy(setd); return -1; } } } return 0; }
int main(int argc, char **argv) { struct set *set; struct set_entry *entry; int size = 10000; uint32_t keys[size]; uint32_t i, random_value; set = set_create(key_value, uint32_t_key_equals); for (i = 0; i < size; i++) { keys[i] = i; set_add(set, keys + i); } /* Test the no-predicate case. */ entry = set_random_entry(set, NULL); assert(entry); /* Check that we're getting different entries and that the predicate * works. */ for (i = 0; i < 100; i++) { entry = set_random_entry(set, uint32_t_key_is_even); assert(entry); assert((key_value(entry->key) & 1) == 0); if (i == 0 || key_value(entry->key) != random_value) break; random_value = key_value(entry->key); } assert(i != 100); set_destroy(set, NULL); return 0; }
//set_intersection int set_intersection(Set *seti, const Set *set1, const Set *set2) { ListElmt *member; void *data; //Initialize the set for the intersection. set_init(seti, set1->match, NULL); //insert the members present in both sets. for (member = list_head(set1); member != NULL; member = list_next(member)) { if (set_is_member(set2, list_data(member))) { data = list_data(member); if (list_ins_next(seti, list_tail(seti), data) != 0) { set_destroy(seti); return -1; } } } return 0; }
set_t * set(char *setname_p, expr_t * exprlist_p) { set_t *new_p; set_t *old_p; /* does this setname exist already? */ old_p = set_find(setname_p); if (old_p) set_destroy(old_p); /* create a new set */ new_p = new(set_t); queue_init(&new_p->qn); new_p->setname_p = setname_p; new_p->exprlist_p = exprlist_p; /* append the new set to the global list */ (void) queue_append(&g_setlist, &new_p->qn); return (new_p); } /* end set */
int set_difference(Set *setd, const Set *set1, const Set *set2) { ListElem *member; void *data; //init union set_init(setd, set1->match, NULL); //insert members of the first set for (member = list_head(set1); member != NULL; member = list_next(member)) { if (!set_is_member(set2, list_data(member))) { data = list_data(member); if (list_ins_next(setd, list_tail(setd), data) != 0) { set_destroy(setd); return -1; } } } return 0; };
int verify(const char *command, int argc, char **argv) { int ret = 1; unsigned char signature[sizeof(ecdsa_signature_t)]; set pubkeys, signatures; set_init(&pubkeys, sizeof(ecc_25519_work_t), 5); set_init(&signatures, sizeof(signature), 5); size_t min_good_signatures = 1; int opt; while ((opt = getopt(argc, argv, "s:p:n:")) != -1) { ecc_int256_t pubkey_packed; ecc_25519_work_t pubkey; switch (opt) { case 's': if (!parsehex(signature, optarg, sizeof(signature))) { fprintf(stderr, "Error while reading signature %s\n", optarg); break; } if (!set_add(&signatures, signature)) { fprintf(stderr, "Error in array_add\n"); goto out; } break; case 'p': if (!parsehex(pubkey_packed.p, optarg, 32)) { fprintf(stderr, "Error while reading pubkey %s\n", optarg); break; } int ok = ecc_25519_load_packed_legacy(&pubkey, &pubkey_packed); if (!ok || !ecdsa_is_valid_pubkey(&pubkey)) { fprintf(stderr, "Invalid pubkey %s\n", optarg); break; } if (!set_add(&pubkeys, &pubkey)) { fprintf(stderr, "Error in array_add\n"); goto out; } break; case 'n': min_good_signatures = atoi(optarg); } } if (optind > argc) { fprintf(stderr, "Usage: %s [-s signature ...] [-p pubkey ...] [-n num] file\n", command); goto out; } ecc_int256_t hash; if (!sha256_file((optind <= argc) ? argv[optind] : NULL, hash.p)) { fprintf(stderr, "Error while hashing file\n"); goto out; } { ecdsa_verify_context_t ctxs[signatures.size]; for (size_t i = 0; i < signatures.size; i++) ecdsa_verify_prepare_legacy(&ctxs[i], &hash, SET_INDEX(signatures, i)); size_t good_signatures = ecdsa_verify_list_legacy(ctxs, signatures.size, pubkeys.content, pubkeys.size); if (good_signatures >= min_good_signatures) ret = 0; } out: set_destroy(&pubkeys); set_destroy(&signatures); return ret; }
void cpnt_coll_destroy(struct cpnt_coll coll) { set_destroy(coll.nodes); }