T Set_minus(T t, T s) { if (t == NULL){ assert(s); return Set_new(s->size, s->cmp, s->hash); } else if (s == NULL) return copy(t, t->size); else { T set = Set_new(Arith_min(s->size, t->size), s->cmp, s->hash); assert(s->cmp == t->cmp && s->hash == t->hash); { int i; struct member *q; for (i = 0; i < t->size; i++) for (q = t->buckets[i]; q; q = q->link) if (!Set_member(s, q->member)) { struct member *p; const void *member = q->member; int i = (*set->hash)(member)%set->size; NEW(p); p->member = member; p->link = set->buckets[i]; set->buckets[i] = p; set->length++; } } return set; } }
T Set_minus(T t, T s) { if (t == NULL) { assert(s); return Set_new(s->size, s->cmp, s->hash); } else if (s == NULL) { return copy(t, t->size); } else { T set = Set_new(Arith_min(s->size, t->size), s->cmp, s->hash); assert(s->cmp == t->cmp && s->hash == t->hash); //<for each member q in t 112> int i; struct member *q; for (i = 0; i < t->size; i++) { for (q = t->buckets[i]; q; q = q->link) { if (!Set_member(s, q->member)) { //<add q->member to set 112> struct member *p; const void *member = q->member; int j = (*set->hash)(member) %set->size; //<add member to set 109> NEW(p); p->member = member; p->link = set->buckets[j]; set->buckets[j] = p; set->length++; } } } return set; } }
/*functions*/ T combine_if_match(T t, T s, int match) { T set = Set_new(Arith_min(s->m_size, t->m_size), s->m_compare, s->m_hash); /*for each member q in t*/ size_t i; struct element* q; for (i = 0; i < t->m_size; i++) { for (q = t->m_buckets[i]; q != NULL; q = q->m_next) { /*\for each member q in t*/ if (Set_member(s, q->m_value) == match) { /*add q->member to set*/ struct element* p; const void* member = q->m_value; size_t i = (*set->m_hash)(member) % set->m_size; /*add member to set*/ NEW(p); p->m_value = member; p->m_next = set->m_buckets[i]; set->m_buckets[i] = p; set->m_length++; /*\add member to set*/ /*\add q->member to set*/ } } } return set; }
static T copy(T t, size_t hint) { T set; assert(t); set = Set_new(hint, t->m_compare, t->m_hash); { /*for each member q in t*/ size_t i; struct element* q; for (i = 0; i < t->m_size; i++) { for (q = t->m_buckets[i]; q != NULL; q = q->m_next) { /*for each member q in t*/ /*add q->member to set*/ struct element* p; const void* member = q->m_value; int i = (*set->m_hash)(member) % set->m_size; /*add member to set*/ NEW(p); p->m_value = member; p->m_next = set->m_buckets[i]; set->m_buckets[i] = p; set->m_length++; /*\add member to set*/ /*\add q->member to set*/ } } } return set; }
T Set_union (T set1, T set2) { T newSet; List_t p; Assert_ASSERT(set1); Assert_ASSERT(set2); newSet = Set_new (set1->equals); p = List_getFirst (set1->list); while (p){ Set_insert (newSet, p->data); p = p->next; } p = List_getFirst (set2->list); while (p){ Poly_t v = (Poly_t)p->data; if (!List_exists (set1->list, v, set1->equals)) Set_insert (newSet, v); else; p = p->next; } return newSet; }
void xref(const char *name, FILE *fp, Table_T identifiers){ char buf[128]; if (name == NULL) name = ""; name = Atom_string(name); linenum = 1; while (getword(fp, buf, sizeof buf, first, rest)) { Set_T set; Table_T files; const char *id = Atom_string(buf); files = Table_get(identifiers, id); if (files == NULL) { files = Table_new(0, NULL, NULL); Table_put(identifiers, id, files); } set = Table_get(files, name); if (set == NULL) { set = Set_new(0, intcmp, inthash); Table_put(files, name, set); } { int *p = &linenum; if (!Set_member(set, p)) { NEW(p); *p = linenum; Set_put(set, p); } } } }
Set* Set_copy(const Set* set) { Set* ret = Set_new(); ret->concrete = set->concrete; if(ret->concrete) { ret->concreteSet = ConcreteSet_copy(set->concreteSet); } else { #warning fix after AbstractSet ret->abstractSet = NULL; /* AbstractSet_copy(set->abstractSet); */ } return ret; }
T Set_inter(T s, T t) { if (s == NULL) { assert(t); return Set_new(t->m_size, t->m_compare, t->m_hash); } else if (t == NULL) { return Set_new(s->m_size, s->m_compare, s->m_hash); } else if (s->m_length < t->m_length) { return Set_inter(t, s); } else { //T set = Set_new(Arith_min(s->size, t->size), s->cmp, s->hash); T set; assert(s->m_compare == t->m_compare && s->m_hash == t->m_hash); set = combine_if_match(t, s, TRUE); return set; } }
void readAll( Table_T fpTable ) { Set_T nameSet = Set_new( initSetSize, NULL, NULL ); fpPair pair; while( !feof(stdin) ) { pair = readLine(); addToTable( nameSet, fpTable, pair ); } Set_free(&nameSet); }
/*Inserts the value into the table under the corresponding fingerprint*/ void insert_atom(Table_T* table, char* fingerprint, char* name) { const char *key; const char *value; Set_T old_set; /*Turns fingerprint and name strings into Hanson atoms*/ key = Atom_string(fingerprint); value = Atom_string(name); old_set = Table_get(*table, key); if (old_set == NULL) { Set_T new_set = Set_new(1, NULL, NULL); Set_put(new_set, value); Table_put(*table, key, new_set); } else { Set_put(old_set, value); } }
T Set_minus(T s, T t) { if (t == NULL) { assert(s); return Set_new(s->m_size, s->m_compare, s->m_hash); } else if (s == NULL) { return copy(t, t->m_size); } else { //T set = Set_new(Arith_min(s->size, t->size), s->cmp, s->hash); T set; assert(s->m_compare == t->m_compare && s->m_hash == t->m_hash); set = combine_if_match(t, s, FALSE); return set; } }
static T copy(T t, int hint) { T set; assert(t); set = Set_new(hint, t->cmp, t->hash); { int i; struct member *q; for (i = 0; i < t->size; i++) for (q = t->buckets[i]; q; q = q->link) { struct member *p; const void *member = q->member; int i = (*set->hash)(member)%set->size; NEW(p); p->member = member; p->link = set->buckets[i]; set->buckets[i] = p; set->length++; } } return set; }
int main(int argc, char* argv[]) { Set s = Set_new(); int a = 42; char* b = "hello"; assert(Set_size(s) == 0); Set_show(s, stdout, &show_func); Set_add(s, &a); assert(Set_size(s) == 1); Set_show(s, stdout, &show_func); Set_add(s, b); assert(Set_size(s) == 2); Set_show(s, stdout, &show_func); Set_remove(s, b); assert(Set_size(s) == 1); Set_show(s, stdout, &show_func); Set_remove(s, b); assert(Set_size(s) == 1); Set_show(s, stdout, &show_func); fprintf(stdout, "All tests passed!\n"); }
void xref(const char *name, FILE *fp, Table_T identifiers){ char buf[128]; if (name == NULL){ name = ""; } name = Atom_string(name); linenum = 1; while (getword(fp, buf, sizeof(buf), first, rest)){ Set_T set; Table_T files; const char *id = Atom_string(buf); // files <- file table in identifiers associated with id files = Table_get(identifiers, id); if (files == NULL){ files = Table_new(0, NULL, NULL); Table_put(identifiers, id, files); } // set <- set in files associated with name set = Table_get(files, name); if (set == NULL){ set = Set_new(0, intcmp, inthash); Table_put(files, name, set); } // add linenum to set, if necessary { int *p = &linenum; if (!Set_member(set, p)){ NEW(p); *p = linenum; Set_put(set, p); } } } }
//calculate the most unique fld in item against the map of lists of templates //put the tuple in that list associated with the most unique key static int _add_template(T _this_, item_ *item) { Table_T items = _this_->items; //calc most unique key oe_scalar key = NULL; int key_idx = 0; int set_size = 0; Set_T curset = NULL; for ( int i = 0; item->object[i]; i++ ) { oe_scalar tmpkey = item->object[i]; if (strcmp("_", tmpkey) == 0) continue; //don't index by wildcard Set_T s = (Set_T) Table_get( items, tmpkey ); if (s) { int size = Set_length( s ); if ( !key || size < set_size ) { key = tmpkey; key_idx = i; set_size = size; curset = s; } } else { //found an unused key key = tmpkey; key_idx = i; set_size = 0; curset = Set_new( 10, _itemcmp, _itemhash ); Table_put( items, key, curset ); break; } } if (!curset) { OE_ERR(0, "Dispatcher can not add template, make sure it is not all '_' fields\n"); //i18n return -1; } item->items = curset; item->key_idx = key_idx; //for self remove of empty sets Set_put( curset, item ); return 0; }
void send_chunk_radius(struct PL_entry *player, int32_t xin, int32_t zin, int radius) { // Get "chunk coords" xin /= 16; zin /= 16; LOG(LOG_DEBUG, "Sending new chunks center:(%d, %d)", xin, zin); Set_T oldchunkset = player->loadedchunks; // Make a new set containing coordinates of all chunks client should have Set_T newchunkset = Set_new(400, chunkcoordcmp, chunkcoordhash); for(int x = -radius; x < radius; x++) { for(int z = -radius; z < radius; z++) { // Use a circular send pattern based on Bravo/MostAwesomeDude's algo if ( x*x + z*z <= radius*radius ) { chunk_coord *coord = Malloc(sizeof(chunk_coord)); coord->x = x + xin; coord->z = z + zin; Set_put(newchunkset, coord); } } } Set_T toremove = Set_minus(oldchunkset, newchunkset); Set_T toadd = Set_minus(newchunkset, oldchunkset); Set_map(toremove, &chunkradiusunload, (void *)player); Set_map(toadd, &chunkradiusload, (void *)player); player->loadedchunks = newchunkset; Set_map(oldchunkset, &chunkradiusfree, NULL); Set_free(&oldchunkset); }
static T copy(T t, int hint) { T set; assert(t); set = Set_new(hint, t->cmp, t->hash); //<for each member in t 112> int i; struct member *q; for (i = 0; i < t->size; i++) { for (q = t->buckets[i]; q; q = q->link) { //<add q->member to set 112> struct member *p; const void *member = q->member; int j = (*set->hash)(member) % set->size; //<add member to set 109> NEW(p); p->member = member; p->link = set->bucket[j]; set->buckets[j] = p; set->length++; } } }
/*! \brief Finds loops in the CFG. Builds lp hdr, body and exit bb sets, and * puts this lp info in a PC_Loop struct, which is appended to the CFG lp list. * * \param cfg * control flow graph for the func being processed * * Multiple back edges coming into the same loop header are considered as one * loop. The loop body is a union of the bodies of the loops corresponding to * each one of these back edges. * * \return void */ void PC_FindLoops (PC_Graph cfg) { PC_Block h_bb; /* build dominator sets for each bb in the cfg */ PC_BuildDomSets (cfg); /* check each cfg bb in turn to see if it's a lp header bb */ for (h_bb = cfg->first_bb; h_bb; h_bb = h_bb->next) { PC_Flow be; PC_Loop *loop; int num_back_edge; int num_exit; int s_lp_head = -1; Set s_lp_body = Set_new (), s_lp_exits = Set_new (); /* check all edges coming into h_bb to see if they are back edges */ num_back_edge = 0; for (be = h_bb->p_flow; be; be = be->p_next_flow) { /* check to see if be is indeed a back edge */ if (PC_BB1DominatesBB2 (be->dest_bb, be->src_bb)) { PC_Block bb; Stack *st = New_Stack (); Set lp_body = Set_new (); num_back_edge++; s_lp_head = be->dest_bb->ID; if (be->src_bb->ID == be->dest_bb->ID) P_warn ("PC_FindNaturalLoop: back edge head & tail are same."); /* Build the set of all bbs in the lp body. */ lp_body = Set_add (lp_body, s_lp_head); if (!Set_in (lp_body, be->src_bb->ID)) { lp_body = Set_add (lp_body, be->src_bb->ID); Push_Top (st, be->src_bb); } while ((bb = Pop (st)) != ((void*)-1)) { PC_Flow fl; for (fl = bb->p_flow; fl; fl = fl->p_next_flow) { if (!Set_in (lp_body, fl->src_bb->ID)) { lp_body = Set_add (lp_body, fl->src_bb->ID); Push_Top (st, fl->src_bb); } } } /* the s_lp body is a union of all of these 'inner' lp bodies */ s_lp_body = Set_union (s_lp_body, lp_body); Clear_Stack (st); Set_dispose (lp_body); } } /* If a loop was found, build its set of exits bbs, and append it to the list of loops in the cfg. */ if (s_lp_head != -1) { int *s_lp_bod; int lp_size = 0, i; /* Build the set of all bbs that are exits out of the lp. */ s_lp_bod = (int *) calloc (Set_size (s_lp_body), sizeof (int)); lp_size = Set_2array (s_lp_body, s_lp_bod); num_exit = 0; for (i = 0; i < lp_size; i++) { PC_Block bb; PC_Flow fl; bb = PC_FindBlock (cfg, s_lp_bod[i]); for (fl = bb->s_flow; fl; fl = fl->s_next_flow) if (!Set_in (s_lp_body, fl->dest_bb->ID)) { num_exit++; s_lp_exits = Set_add (s_lp_exits, fl->dest_bb->ID); } } free (s_lp_bod); /* check for redundant loops */ { PC_Loop lp = NULL; int same = 0; for (lp = cfg->lp; lp; lp = lp->next) if (Set_same (s_lp_body, lp->body)) { same = 1; break; } if (same) { Set_dispose (s_lp_body); Set_dispose (s_lp_exits); continue; } } /* Create a new PC_Loop and append it to the current CFG loop list. */ loop = &cfg->lp; while (*loop) loop = &(*loop)->next; *loop = PC_NewLoop (s_lp_head, s_lp_body, s_lp_exits, num_back_edge, num_exit); cfg->lp_tree = PC_LoopTreeInsert (cfg->lp_tree, *loop, 1); } Set_dispose (s_lp_body); Set_dispose (s_lp_exits); } PC_SetBlockLoops (cfg); return; }
T Oec_IdSet_new (int hint) { T obj; obj = Mem_calloc(1, sizeof *obj, __FILE__, __LINE__); obj->members = Set_new( 100, itemcmp, itemhash ); return obj; }
/*! \brief Finds the natural loop corresponding to a back edge * * \param cfg * control flow graph for the func being processed * \param be * back edge going to the head of this particular natural loop * * *** CURRENTLY NOT IN USE *** * * \return void */ void PC_FindNaturalLoop (PC_Graph cfg, PC_Flow be) { PC_Loop *loop = NULL; PC_Block bb = NULL; PC_Flow fl = NULL; Stack *st = New_Stack (); int lp_head = be->dest_bb->ID, lp_size = 0, i; int *lp_bod; Set lp = Set_new (), lp_exits = Set_new (); int num_back_edge = 0; int num_exit = 0; if (be->src_bb->ID == be->dest_bb->ID) P_warn ("PC_FindNaturalLoop: head and tail of back edge are same."); /* Build the set of all bbs in the lp body. */ lp = Set_add (lp, lp_head); if (!Set_in (lp, be->src_bb->ID)) { lp = Set_add (lp, be->src_bb->ID); Push_Top (st, be->src_bb); } while ((bb = Pop (st)) != ((void*)-1)) { for (fl = bb->p_flow; fl; fl = fl->p_next_flow) { if (!Set_in (lp, fl->src_bb->ID)) { lp = Set_add (lp, fl->src_bb->ID); Push_Top (st, fl->src_bb); } } } /* Build the set of all bbs that are exits out of the lp. */ lp_bod = (int *) calloc (Set_size (lp), sizeof (int)); lp_size = Set_2array (lp, lp_bod); for (i = 0; i < lp_size; i++) { bb = PC_FindBlock (cfg, lp_bod[i]); for (fl = bb->s_flow; fl; fl = fl->s_next_flow) if (!Set_in (lp, fl->dest_bb->ID)) { lp_exits = Set_add (lp_exits, fl->dest_bb->ID); num_exit++; } } free (lp_bod); /* Append this loop to the list of loops in the PC_Graph. */ loop = &cfg->lp; while (*loop) { loop = &(*loop)->next; } *loop = PC_NewLoop (lp_head, lp, lp_exits, num_back_edge, num_exit); Set_dispose (lp); Set_dispose (lp_exits); }
int main() { //Set console default size. system(CONSOLE_SIZE); //Testing fields int testSize = 10; Set_T * mainSet = Set_new(testSize); //Fill the set Set_fill(mainSet); //Print it out puts("**********"); printf("Creating a new set...\n" "Filling it with non-repeatable numbers...\n"); Set_print(mainSet); puts("**********"); //Get some elements printf("Getting element on the index 4...\n"); int test_a = Set_getValueAt(mainSet,4); printf("Element with the index 4 is %d\n", test_a); printf("Getting element on the index 10...\n"); int test_b = Set_getValueAt(mainSet,10); printf("Element with the index 10 is %d\n", test_b); //Set some elements printf("Setting element with index 3 to \"123\"\n"); Set_setValueAt(mainSet, 3, 123); printf("Setting element with index 10 to \"123\"\n"); Set_setValueAt(mainSet, 10, 123); //Print set again puts("**********"); Set_print(mainSet); //Delete some elements printf("Deleting element index 3\n"); Set_removeValueAt(mainSet, 3); //Print the set again puts("**********"); Set_print(mainSet); //In the end - free allocated memory puts("----------------------------------------------------------------------"); printf("Creating two more sets.\n"); Set_T * first = Set_new(testSize+1); Set_T * second = Set_new(testSize+4); //15 Set_fill(first); Set_fill(second); puts("**********"); printf("First set:\n"); Set_print(first); printf("Second set:\n"); Set_print(second); puts("**********"); printf("Union with the first set and the second set.\n"); Set_T * test_unionSet = Set_union(first, second); printf("The 'union' set:\n"); Set_print(test_unionSet); puts("**********"); printf("Intersection with the first and the second set.\n"); Set_T * test_intersectionSet = Set_intersection(first, second); printf("The 'intersection' set:\n"); Set_print(test_intersectionSet); puts("**********"); printf("Difference between the first and the second set.\n"); for(int i = 0; i < Set_getSize(first); i++) { Set_setValueAt(first, i, 8+i); } printf("Changed first set:\n"); Set_print(first); Set_T * test_differenceSet = Set_difference(first, second); printf("The 'difference' set:\n"); Set_print(test_differenceSet); puts("**********"); //free allocated memory Set_delete(first); Set_delete(second); Set_delete(mainSet); Set_delete(test_unionSet); Set_delete(test_intersectionSet); Set_delete(test_differenceSet); puts("allocated memory was deleted."); return (0); }