// Performs a union of two sets, does not allow duplicate values struct set_t *set_union(struct set_t *set1, struct set_t *set2) { struct set_t *temp_set_head = NULL; struct set_t *temp_set_it = NULL; struct set_t *set_it = set1; // Add each element of set1 while(set_it != NULL) { if(temp_set_head == NULL) { temp_set_head = new_set(set_it->value); temp_set_it = temp_set_head; } else { temp_set_it->next = set_add(temp_set_head, set_it->value); if(temp_set_it->next != NULL) temp_set_it = temp_set_it->next; } set_it = set_it->next; } // Add each element of set2 set_it = set2; while(set_it != NULL) { if(temp_set_head == NULL) { temp_set_head = new_set(set_it->value); temp_set_it = temp_set_head; } else { temp_set_it->next = set_add(temp_set_head, set_it->value); if(temp_set_it->next != NULL) temp_set_it = temp_set_it->next; } set_it = set_it->next; } return temp_set_head; }
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; }
/** * Implements #prpl->init(). This initializes the an account. * * @param acc The #account_t. **/ static void steam_init(account_t *acc) { set_t *s; s = set_add(&acc->set, "authcode", NULL, steam_eval_accounton, acc); s->flags = SET_NULL_OK | SET_HIDDEN | SET_NOSAVE; s = set_add(&acc->set, "captcha", NULL, steam_eval_accounton, acc); s->flags = SET_NULL_OK | SET_HIDDEN | SET_NOSAVE; s = set_add(&acc->set, "esid", NULL, NULL, acc); s->flags = SET_NULL_OK | SET_HIDDEN | SET_NOSAVE; s = set_add(&acc->set, "cgid", NULL, NULL, acc); s->flags = SET_NULL_OK | SET_HIDDEN | SET_NOSAVE; s = set_add(&acc->set, "umqid", NULL, NULL, acc); s->flags = SET_NULL_OK | SET_HIDDEN; s = set_add(&acc->set, "token", NULL, NULL, acc); s->flags = SET_NULL_OK | SET_HIDDEN | SET_PASSWORD; s = set_add(&acc->set, "sessid", NULL, NULL, acc); s->flags = SET_NULL_OK | SET_HIDDEN | SET_PASSWORD; s = set_add(&acc->set, "show_playing", "%", steam_eval_show_playing, acc); s->flags = SET_NULL_OK; set_add(&acc->set, "game_status", "false", steam_eval_game_status, acc); set_add(&acc->set, "password", NULL, steam_eval_password, acc); }
int main(int argc, char *argv[]) { void *a = new(String, "a"); void *b = new(String, "bbb"); void *s = new(Set); printf("LENGTH A: %lu\n", string_length(a)); printf("LENGTH B: %lu\n", string_length(b)); printf("Prev: %s\n", string_get(a)); string_set(a, "New String"); printf("New: %s\n", string_get(a)); set_add(s, a); set_add(s, b); printf("CONTAINS A: %u\n", set_contains(s, a)); printf("CONTAINS B: %u\n", set_contains(s, b)); printf("DROP B: %p\n", set_drop(s, b)); printf("DROP B: %p\n", set_drop(s, b)); printf("ADD B: %p\n", set_add(s, b)); delete(s); delete(a); delete(b); return 0; }
irc_channel_t *irc_channel_new(irc_t *irc, const char *name) { irc_channel_t *ic; set_t *s; if (!irc_channel_name_ok(name) || irc_channel_by_name(irc, name)) { return NULL; } ic = g_new0(irc_channel_t, 1); ic->irc = irc; ic->name = g_strdup(name); strcpy(ic->mode, CMODE); irc_channel_add_user(ic, irc->root); irc->channels = g_slist_append(irc->channels, ic); set_add(&ic->set, "auto_join", "false", set_eval_bool, ic); s = set_add(&ic->set, "type", "control", set_eval_channel_type, ic); s->flags |= SET_NOSAVE; /* Layer violation (XML format detail) */ if (name[0] == '&') { set_setstr(&ic->set, "type", "control"); } else { /* if( name[0] == '#' ) */ set_setstr(&ic->set, "type", "chat"); } return ic; }
static void insert_loop_node (flowloop_t *loop, unsigned n, set_t *stack) { if (!set_is_member (loop->nodes, n)) { set_add (loop->nodes, n); set_add (stack, n); } }
int main(void) { set s1, s2; int m = 10; s1 = set_init(m); set_add(s1, 1); set_add(s1, 3); set_add(s1, 5); s2 = set_init(m + 2); set_add(s2, 0); set_add(s2, 2); set_add(s2, 3); set_add(s2, 4); set_add(s2, 5); set_add(s2, 11); set_print(s1); printf("\n"); set_print(s2); printf("\nIntersection: "); set_print(set_intersection(s1, s2)); printf("\nUnion: "); set_print(set_union(s1, s2)); printf("\nComplement for s2: "); set_print(set_complement(s2)); printf("\n"); return 0; }
static struct hamt_node *__hamt_new_node2(struct hamt_root *root, void *item1, int slice1, void *item2, int slice2) { struct hamt_node *node = \ __hamt_new_node(root, set_add(set_add(0ULL, slice1), slice2), 2); node->slots[set_slot_number(node->mask, slice1)] = item_to_slot(item1); node->slots[set_slot_number(node->mask, slice2)] = item_to_slot(item2); return node; }
static void msn_init( account_t *acc ) { set_t *s; s = set_add( &acc->set, "display_name", NULL, set_eval_display_name, acc ); s->flags |= SET_NOSAVE | ACC_SET_ONLINE_ONLY; set_add( &acc->set, "mail_notifications", "false", set_eval_bool, acc ); set_add( &acc->set, "switchboard_keepalives", "false", set_eval_bool, acc ); acc->flags |= ACC_FLAG_AWAY_MESSAGE | ACC_FLAG_STATUS_MESSAGE | ACC_FLAG_HANDLE_DOMAINS; }
void set_test() { printf("SET S: "); set* s = set_init(); set_add(s, (SETDATA*)1); set_add(s, (SETDATA*)2); set_add(s, (SETDATA*)3); set_add(s, (SETDATA*)4); set_add(s, (SETDATA*)5); set_add(s, (SETDATA*)6); set_print(s, set_testcb); printf("S \\ 2, 6: "); set_remove(s, (SETDATA*)6); set_remove(s, (SETDATA*)2); set_print(s, set_testcb); printf("SET T: "); set* t = set_init(); set_add(t, (SETDATA*)1); set_add(t, (SETDATA*)2); set_add(t, (SETDATA*)9); set_add(t, (SETDATA*)4); set_print(t, set_testcb); printf("UNION OF S, T: "); set* u = set_init(); set_union(u, s, t); set_print(u, set_testcb); printf("INTERSECTION OF S, T: "); set* i = set_init(); set_intersection(i, s, t); set_print(i, set_testcb); printf("DIFFERENCE OF S, T: "); set* d = set_init(); set_difference(d, s, t); set_print(d, set_testcb); printf("CLEAR S: "); set_clear(s); set_print(s, set_testcb); set_free(s); set_free(t); set_free(u); set_free(i); set_free(d); }
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; }
void set_add(set_t** set, void* object) { assert(set != NULL); if (*set == NULL) { *set = malloc(sizeof(set_t)); (*set)->data = object; (*set)->left = NULL; (*set)->right = NULL; } else if ((*set)->data > object) { set_add(&(*set)->left, object); } else if ((*set)->data < object) { set_add(&(*set)->right, object); } }
int main() { std::vector<int> set({10}); set_add(set, 16); set_add(set, 16); set_add(set, 10); set_add(set, 5); set_add(set, 5); print(set.begin(), set.end()); std::cout << count_of_equals(set.begin(), set.end(), 16) << std::endl; return 0; }
STATIC mp_obj_t set_intersect_int(mp_obj_t self_in, mp_obj_t other, bool update) { if (update) { check_set(self_in); } else { check_set_or_frozenset(self_in); } if (self_in == other) { return update ? mp_const_none : set_copy(self_in); } mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in); mp_obj_set_t *out = MP_OBJ_TO_PTR(mp_obj_new_set(0, NULL)); mp_obj_t iter = mp_getiter(other, NULL); mp_obj_t next; while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) { if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) { set_add(MP_OBJ_FROM_PTR(out), next); } } if (update) { m_del(mp_obj_t, self->set.table, self->set.alloc); self->set.alloc = out->set.alloc; self->set.used = out->set.used; self->set.table = out->set.table; } return update ? mp_const_none : MP_OBJ_FROM_PTR(out); }
int ro2sslose (struct assocblk *acb, int result) { int len; char *base; PE pe; struct SSAPindication sis; base = NULL, len = 0; /* begin AbortInformation PSDU (pseudo) */ if (pe = pe_alloc (PE_CLASS_UNIV, PE_FORM_CONS, PE_CONS_SET)) { if (set_add (pe, num2prim ((integer) result, PE_CLASS_CONT, 0)) != NOTOK) pe2ssdu (pe, &base, &len); PLOGP (rosap_log,OACS_AbortInformation, pe, "AbortInformation", 0); pe_free (pe); } /* end AbortInformation PSDU */ SUAbortRequest (acb -> acb_fd, base, len, &sis); acb -> acb_fd = NOTOK; if (base) free (base); }
_lang_token* _langspec_translatetoken ( _langspec_token* token, set* keywordset, errorlog* log ) { switch (token->type) { case LANGSPEC_TERMINAL: { char* str = token->value; if (strcmp(str, "#") == 0) { return _lang_inittoken(LANG_NUMBER, str); } else if (strcmp(str, "@") == 0) { return _lang_inittoken(LANG_STRING, str); } else if (strcmp(str, "%") == 0) { return _lang_inittoken(LANG_IDENTIFIER, str); } else { set_add(keywordset, str); return _lang_inittoken(LANG_KEYWORD, str); } } break; case LANGSPEC_NONTERMINAL: { return _lang_inittoken(LANG_NONTERMINAL, token->value); } break; default: _langspec_generator_error(log, "Expected TERMINAL or NONTERMINAL, got %s", _langspec_tokentypestrings[token->type]); return NULL; } }
STATIC mp_obj_t set_intersect_int(mp_obj_t self_in, mp_obj_t other, bool update) { assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set)); if (self_in == other) { return update ? mp_const_none : set_copy(self_in); } mp_obj_set_t *self = self_in; mp_obj_set_t *out = mp_obj_new_set(0, NULL); mp_obj_t iter = mp_getiter(other); mp_obj_t next; while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) { if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) { set_add(out, next); } } if (update) { m_del(mp_obj_t, self->set.table, self->set.alloc); self->set.alloc = out->set.alloc; self->set.used = out->set.used; self->set.table = out->set.table; } return update ? mp_const_none : out; }
static void add_depend(const char *source, set_t *incl, map_t *deps) { set_t *mydeps; llnode_t *tmp; int i,num; if (source == NULL) return; mydeps = map_find(deps,source); if (mydeps != NULL) { num = mydeps->nbuckets; for (i = 0; i < num; ++i) { tmp = mydeps->buckets + i; while (tmp->next != NULL) { if (set_find(incl,tmp->key) == 0) { set_add(incl,tmp->key); add_depend(tmp->key,incl,deps); } tmp = tmp->next; } } } }
/* add an entry to the map */ static void map_add(map_t *m, const char *key, const char *val) { mapnode_t *tmp; unsigned int idx; if (!m) return; idx = hash_func(key) % m->nbuckets; tmp = m->buckets + idx; while (tmp->next != NULL) { if (strcmp(tmp->key,key) == 0) break; tmp = tmp->next; } /* add new entry to map */ if (tmp->next == NULL) { m->count ++; tmp->key = my_strdup(key); tmp->val = set_init(50); /* XXX: chosen arbitrarily */ tmp->next = (mapnode_t *)malloc(sizeof(mapnode_t)); tmp->next->key = NULL; tmp->next->val = NULL; tmp->next->next = NULL; } set_add(tmp->val,val); }
int add_acls(char *acl, set *s, struct hashtable *aclshash) { char **acls; int a, i; if (split(acl, ",", &acls) == -1) return 0; for (i = 0; acls[i] != NULL; i++) { char *entry; if (strcmp(acls[i], "") == 0) { free(acls[i]); continue; } if ((entry = hashtable_search(aclshash, acls[i])) == NULL) { entry = acls[i]; hashtable_insert(aclshash, acls[i], acls[i]); } else { free(acls[i]); } if (set_add(s, entry) == 2) ; } free(acls); return 1; }
int conf_set(char *parameter, char *value) { SETTING *s; if ((s = set_find(parameter)) == NULL) { /* can only set existing settings */ log(LOG_WARNING, "No such configuration symbol: %s", parameter); log(LOG_WARNING, "Assuming that %s is a setting of type STRING",parameter); set_add(SET_STRING, parameter, ""); s = set_find(parameter); } switch (s->type) { case SET_BOOL: set_set(s, (strcmp(value, "yes") == 0 && atoi(value) == 1)?1:0); break; case SET_FLOAT: set_set(s, atof(value)); break; case SET_INT: set_set(s, atol(value)); break; case SET_STRING: case SET_LONGSTRING: set_set(s, value); break; } return 0; }
// Relative complement of set1 in set2. Returns a set of elements that exist in set2 but not in set1 struct set_t *set_difference(struct set_t *set1, struct set_t *set2) { struct set_t *temp_set_head = NULL; struct set_t *temp_set_it = NULL; struct set_t *set_it = set2; // Loop through every element in set2 while(set_it != NULL) { // Element must not exist in set1 if(!set_contains(set1, set_it->value)) { if(temp_set_head == NULL) { temp_set_head = new_set(set_it->value); temp_set_it = temp_set_head; } else { temp_set_it->next = set_add(temp_set_head, set_it->value); if(temp_set_it->next != NULL) temp_set_it = temp_set_it->next; } } set_it = set_it->next; } return temp_set_head; }
// Performs an intersection of two sets struct set_t *set_intersection(struct set_t *set1, struct set_t *set2) { if(set1 == EMPTY || set2 == EMPTY) return EMPTY; struct set_t *temp_set_head = NULL; struct set_t *temp_set_it = NULL; struct set_t *set_it = set1; // Go through every element of set 1 set_it = set1; while(set_it != NULL) { // See if the elements value is also in set2 if(set_contains(set2, set_it->value)) { if(temp_set_head == NULL) { temp_set_head = new_set(set_it->value); temp_set_it = temp_set_head; } else { // set_add will resolve any attempts to add duplicate elements temp_set_it->next = set_add(temp_set_head, set_it->value); if(temp_set_it->next != NULL) temp_set_it = temp_set_it->next; } } set_it = set_it->next; } return temp_set_head; }
Set *set_union(Set *a, Set *b) { Set *r = b; for (; a; a = a->next) if (!set_has(b, a->v)) r = set_add(r, a->v); return r; }
Set *set_intersection(Set *a, Set *b) { Set *r = NULL; for (; a; a = a->next) if (set_has(b, a->v)) r = set_add(r, a->v); return r; }
void *test(void *data) { unsigned int mySeed = seed + sched_getcpu(); long myOps = operations / nb_threads; long val = -1; int op; while (myOps > 0) { op = rand_r(&mySeed) % 100; if (op < update) { if (val == -1) { /* Add random value */ val = (rand_r(&mySeed) % range) + 1; if(set_add(val) == 0) { val = -1; } } else { /* Remove random value */ int res = set_remove( val); val = -1; } } else { /* Look for random value */ long tmp = (rand_r(&mySeed) % range) + 1; set_contains(tmp); } myOps--; } return NULL; }
static struct hamt_node *__hamt_add_slot(struct hamt_root *root, struct hamt_slot *slot_ptr, void *item, uint128_t *item_hash, int level) { uint64_t slice = slice_get(*item_hash, level); struct hamt_node *old_node = ston(*slot_ptr); int old_size = set_count(old_node->mask); int new_size = old_size + 1; struct hamt_node *node = __hamt_new_node(root, set_add(old_node->mask, slice), new_size); *slot_ptr = ntos(node); int slot = set_slot_number(node->mask, slice); memcpy(&node->slots[0], &old_node->slots[0], sizeof(struct hamt_slot)*slot); memcpy(&node->slots[slot+1], &old_node->slots[slot], sizeof(struct hamt_slot)*(old_size-slot)); node->slots[slot] = item_to_slot(item); __hamt_free_node(root, old_node); return node; }
static int add_function(libcrange* lr, set* functions, void* handle, const char* module, const char* prefix, const char* function) { void *f; /* it's actually a function pointer but since we're adding it * to a set, let's leave it as void * */ const char* err; char function_name[512] = "rangefunc_"; strncat(function_name, function, sizeof function_name); function_name[sizeof function_name - 1] = '\0'; f = dlsym(handle, function_name); if ((err = dlerror()) != NULL) { fprintf(stderr, "Module %s: error getting %s\n", module, function_name); return 1; } /* reusing function_name */ assert(strlen(prefix) < 16); assert(strlen(function) < 256); strcpy(function_name, prefix); strcat(function_name, function); set_add(functions, function_name, f); return 0; }
static set* _get_clusters(range_request* rr) { const char** all_clusters = _all_clusters(rr); const char** p_cl = all_clusters; apr_pool_t* pool = range_request_pool(rr); set* node_cluster = set_new(pool, 40000); if(p_cl == NULL) { return node_cluster; } while (*p_cl) { range* nodes_r = _expand_cluster(rr, *p_cl, "CLUSTER"); const char** nodes = range_get_hostnames(pool, nodes_r); const char** p_nodes = nodes; while (*p_nodes) { apr_array_header_t* clusters = set_get_data(node_cluster, *p_nodes); if (!clusters) { clusters = apr_array_make(pool, 1, sizeof(char*)); set_add(node_cluster, *p_nodes, clusters); } *(const char**)apr_array_push(clusters) = *p_cl; ++p_nodes; } ++p_cl; } return node_cluster; }
void set_union ( set* result, set* a, set* b ) { for (int i = 0; i < a->size; i++) { set_add(result, a->items[i]); } for (int i = 0; i < b->size; i++) { set_add(result, b->items[i]); } }