void test_set_to_array (void) { Set* set; int values[100]; int** array; int i; /* Create a set containing pointers to all entries in the "values" * array. */ set = set_new (pointer_hash, pointer_equal); for (i = 0; i < 100; ++i) { values[i] = 1; set_insert (set, &values[i]); } array = (int**) set_to_array (set); /* Check the array */ for (i = 0; i < 100; ++i) { assert (*array[i] == 1); *array[i] = 0; } /* Test out of memory scenario */ alloc_test_set_limit (0); assert (set_to_array (set) == NULL); free (array); set_free (set); }
/* cube_setup -- assume that the fields "num_vars", "num_binary_vars", and part_size[num_binary_vars .. num_vars-1] are setup, and initialize the rest of cube and cdata. If a part_size is < 0, then the field size is abs(part_size) and the field read from the input is symbolic. */ void cube_setup() { register int i, var; register pcube p; if (cube.num_binary_vars < 0 || cube.num_vars < cube.num_binary_vars) fatal("cube size is silly, error in .i/.o or .mv"); cube.num_mv_vars = cube.num_vars - cube.num_binary_vars; cube.output = cube.num_mv_vars > 0 ? cube.num_vars - 1 : -1; cube.size = 0; cube.first_part = ALLOC(int, cube.num_vars); cube.last_part = ALLOC(int, cube.num_vars); cube.first_word = ALLOC(int, cube.num_vars); cube.last_word = ALLOC(int, cube.num_vars); for(var = 0; var < cube.num_vars; var++) { if (var < cube.num_binary_vars) cube.part_size[var] = 2; cube.first_part[var] = cube.size; cube.first_word[var] = WHICH_WORD(cube.size); cube.size += ABS(cube.part_size[var]); cube.last_part[var] = cube.size - 1; cube.last_word[var] = WHICH_WORD(cube.size - 1); } cube.var_mask = ALLOC(pset, cube.num_vars); cube.sparse = ALLOC(int, cube.num_vars); cube.binary_mask = new_cube(); cube.mv_mask = new_cube(); for(var = 0; var < cube.num_vars; var++) { p = cube.var_mask[var] = new_cube(); for(i = cube.first_part[var]; i <= cube.last_part[var]; i++) set_insert(p, i); if (var < cube.num_binary_vars) { INLINEset_or(cube.binary_mask, cube.binary_mask, p); cube.sparse[var] = 0; } else { INLINEset_or(cube.mv_mask, cube.mv_mask, p); cube.sparse[var] = 1; } } if (cube.num_binary_vars == 0) cube.inword = -1; else { cube.inword = cube.last_word[cube.num_binary_vars - 1]; cube.inmask = cube.binary_mask[cube.inword] & DISJOINT; } cube.temp = ALLOC(pset, CUBE_TEMP); for(i = 0; i < CUBE_TEMP; i++) cube.temp[i] = new_cube(); cube.fullset = set_fill(new_cube(), cube.size); cube.emptyset = new_cube(); cdata.part_zeros = ALLOC(int, cube.size); cdata.var_zeros = ALLOC(int, cube.num_vars); cdata.parts_active = ALLOC(int, cube.num_vars); cdata.is_unate = ALLOC(int, cube.num_vars); }
void test_set_free_function (void) { Set* set; int i; int* value; /* Create a set and fill it with 1000 values */ set = set_new (int_hash, int_equal); set_register_free_function (set, free_value); allocated_values = 0; for (i = 0; i < 1000; ++i) { value = new_value (i); set_insert (set, value); } assert (allocated_values == 1000); /* Test removing a value */ i = 500; set_remove (set, &i); assert (allocated_values == 999); /* Test freeing the set */ set_free (set); assert (allocated_values == 0); }
/* * read_data() * * read data from a file to a set * */ void read_data(set_t *pset,char *pname) { FILE *fp; int item; int sucess; /* use to check if sucess insert the item into set or not */ /* the defalut path for the test data */ char path[PATH_LENGTH] = "/public/courses/AlgorithmsAndAnalysis/proj2/"; /* put the file name with defalut path together */ pname = safe_strcat(path,pname); fp = fopen(pname,"r"); /* check if file exit or not */ if(!fp){ fprintf(stderr,"Can not open %s!!!\n",pname); exit(EXIT_FAILURE); } /* get the numbers to the end of file */ while(fscanf(fp,"%d",&item) == 1 ){ sucess = set_insert(pset,item); /* exit if have not sucess insert the number */ if(sucess != TRUE){ fprintf(stderr,"Can not insert the data in the set!!\n"); exit(EXIT_FAILURE); } } /* close the file */ free(pname); fclose(fp); }
/* graph_ins_edge */ int graph_ins_edge(Graph *graph, const void *data1, const void *data2) { ListElmt *element; int retval; /* Don not allow insertion of an edge without both its vertices in the graph. */ for (element = list_head(&graph->adjlists); element != NULL; element = list_next(element)) { if (graph->match(data2, ((AdjList *)list_data(element))->vertex)) break; } if (element == NULL) return -1; for (element = list_head(&graph->adjlists); element != NULL; element = list_next(element)) { if (graph->match(data1, ((AdjList *)list_data(element))->vertex)) break; } if (element == NULL) return -1; /* Insert the second vertex into the adjacency list of the first vertex. */ if ((retval = set_insert(&((AdjList *)list_data(element))->adjacent, data2)) != 0) { return retval; } /* Adjust the edge count to account for the inserted edge. */ graph->ecount++; return 0; }
int lc_arg_register(lc_arg_env_t *env, const char *name, char letter, const lc_arg_handler_t *handler) { lc_arg_t arg; arg.name = name; arg.letter = letter; arg.handler = handler; lc_arg_t **map = NULL; int base = 0; if (isupper((unsigned char)letter)) { map = env->upper; base = 'A'; } else if (islower((unsigned char)letter)) { map = env->lower; base = 'a'; } lc_arg_t *ent = set_insert(lc_arg_t, env->args, &arg, sizeof(arg), hash_str(name)); if (ent && base != 0) map[letter - base] = ent; return ent != NULL; }
static void load_set(Set *set, int num1, int num2, int inc1, int inc2) { int i; for (i = num1; i <= MAX_ELEMENTS; i += inc1) set_insert(set, i); for (i = num2; i <= MAX_ELEMENTS; i += inc2) set_delete(set, i); }
struct dag *dag_create() { struct dag *d = malloc(sizeof(*d)); if(!d) { debug(D_DEBUG, "makeflow: could not allocate new dag : %s\n", strerror(errno)); return NULL; } else { memset(d, 0, sizeof(*d)); d->nodes = 0; d->filename = NULL; d->node_table = itable_create(0); d->local_job_table = itable_create(0); d->remote_job_table = itable_create(0); d->file_table = hash_table_create(0, 0); d->completed_files = hash_table_create(0, 0); d->symlinks_created = list_create(); d->variables = hash_table_create(0, 0); d->local_jobs_running = 0; d->local_jobs_max = 1; d->remote_jobs_running = 0; d->remote_jobs_max = MAX_REMOTE_JOBS_DEFAULT; d->nodeid_counter = 0; d->collect_table = set_create(0); d->export_vars = set_create(0); d->special_vars = set_create(0); d->task_categories = hash_table_create(0, 0); /* Add GC_*_LIST to variables table to ensure it is in * global DAG scope. */ hash_table_insert(d->variables, GC_COLLECT_LIST, dag_variable_create(NULL, "")); hash_table_insert(d->variables, GC_PRESERVE_LIST, dag_variable_create(NULL, "")); /* Declare special variables */ set_insert(d->special_vars, RESOURCES_CATEGORY); set_insert(d->special_vars, RESOURCES_CORES); set_insert(d->special_vars, RESOURCES_MEMORY); set_insert(d->special_vars, RESOURCES_DISK); set_insert(d->special_vars, RESOURCES_GPUS); memset(d->node_states, 0, sizeof(int) * DAG_NODE_STATE_MAX); return d; } }
struct catalog_query *catalog_query_create(const char *hosts, struct jx *filter_expr, time_t stoptime) { struct catalog_query *q = NULL; char *n; struct catalog_host *h; struct list *sorted_hosts = catalog_query_sort_hostlist(hosts); int backoff_interval = 1; list_first_item(sorted_hosts); while(time(NULL) < stoptime) { if(!(h = list_next_item(sorted_hosts))) { list_first_item(sorted_hosts); sleep(backoff_interval); int max_backoff_interval = MAX(0, stoptime - time(NULL)); backoff_interval = MIN(backoff_interval * 2, max_backoff_interval); continue; } struct jx *j = catalog_query_send_query(h->url, time(NULL) + 5); if(j) { q = xxmalloc(sizeof(*q)); q->data = j; q->current = j->u.items; q->filter_expr = filter_expr; if(h->down) { debug(D_DEBUG,"catalog server at %s is back up", h->host); set_first_element(down_hosts); while((n = set_next_element(down_hosts))) { if(!strcmp(n, h->host)) { free(n); set_remove(down_hosts, n); break; } } } break; } else { if(!h->down) { debug(D_DEBUG,"catalog server at %s seems to be down", h->host); set_insert(down_hosts, xxstrdup(h->host)); } } } list_first_item(sorted_hosts); while((h = list_next_item(sorted_hosts))) { free(h->host); free(h->url); free(h); } list_delete(sorted_hosts); return q; }
void explode(int var, int z) { int i, last = cube.last_part[var]; for(i=cube.first_part[var], z *= cube.part_size[var]; i<=last; i++, z++) if (is_in_set(Gcube, i)) if (var == 0) set_insert(Gminterm, z); else explode(var-1, z); }
/************************************************************************* * *N bounding_select * *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * Purpose: *P * This function reads the bounding rectangle table to weed out the * local primitives. *E *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * Parameters: *A * path <input> == (char *) path to the bounding rectangle table. * mapextent <input> == (extent_type) map extent to compare. * dec_degrees <input> == (int) flag to indicate if data is in decimal * degrees. * bounding_select <output> == (set_type) set of bounding rectangle ids. *E *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * History: *H * Barry Michaels May 1991 DOS Turbo C *E *************************************************************************/ set_type bounding_select( char *path, extent_type mapextent, int dec_degrees ) { vpf_table_type table; set_type set; rspf_int32 i, count; extent_type box, pextent; double x1,y1,x2,y2; row_type row; int XMIN_,YMIN_,XMAX_,YMAX_; /* Project all extents to plate-carree for cartesian comparisons */ /* (decimal degree coordinate systems) */ x1 = mapextent.x1; y1 = mapextent.y1; x2 = mapextent.x2; y2 = mapextent.y2; if (dec_degrees) { set_plate_carree_parameters( central_meridian(x1,x2), 0.0, 1.0 ); pcarree_xy(&x1,&y1); pcarree_xy(&x2,&y2); } pextent.x1 = x1; pextent.y1 = y1; pextent.x2 = x2; pextent.y2 = y2; table = vpf_open_table(path,disk,"rb",NULL); XMIN_ = table_pos("XMIN",table); YMIN_ = table_pos("YMIN",table); XMAX_ = table_pos("XMAX",table); YMAX_ = table_pos("YMAX",table); set = set_init(table.nrows+1); for (i=1;i<=table.nrows;i++) { row = read_next_row(table); get_table_element(XMIN_,row,table,&box.x1,&count); get_table_element(YMIN_,row,table,&box.y1,&count); get_table_element(XMAX_,row,table,&box.x2,&count); get_table_element(YMAX_,row,table,&box.y2,&count); free_row(row,table); x1 = box.x1; y1 = box.y1; x2 = box.x2; y2 = box.y2; if (dec_degrees) { pcarree_xy(&x1,&y1); pcarree_xy(&x2,&y2); } box.x1 = x1; box.y1 = y1; box.x2 = x2; box.y2 = y2; if ( contained(box,pextent) || contained(pextent,box) ) { set_insert(i,set); } } vpf_close_table(&table); return set; }
int set_insert_set(struct set *s, struct set *s2) { set_first_element(s2); int additions = 0; const void *element; while((element = set_next_element(s2))){ additions += set_insert(s, element); } return additions; }
int set_insert_list(struct set *s, struct list *l) { list_first_item(l); int additions = 0; const void *element; while((element = list_next_item(l))){ additions += set_insert(s, element); } return additions; }
void test_set_out_of_memory (void) { Set* set; int values[66]; unsigned int i; set = set_new (int_hash, int_equal); /* Test normal failure */ alloc_test_set_limit (0); values[0] = 0; assert (set_insert (set, &values[0]) == 0); assert (set_num_entries (set) == 0); alloc_test_set_limit (-1); /* Test failure when increasing table size. * The initial table size is 193 entries. The table increases in * size when 1/3 full, so the 66th entry should cause the insert * to fail. */ for (i = 0; i < 65; ++i) { values[i] = (int) i; assert (set_insert (set, &values[i]) != 0); assert (set_num_entries (set) == i + 1); } assert (set_num_entries (set) == 65); /* Test the 66th insert */ alloc_test_set_limit (0); values[65] = 65; assert (set_insert (set, &values[65]) == 0); assert (set_num_entries (set) == 65); set_free (set); }
void dag_compile_ancestors(struct dag *d) { struct dag_node *n, *m; struct dag_file *f; char *name; hash_table_firstkey(d->file_table); while(hash_table_nextkey(d->file_table, &name, (void **) &f)) { m = f->target_of; if(!m) continue; list_first_item(f->needed_by); while((n = list_next_item(f->needed_by))) { debug(D_DEBUG, "rule %d ancestor of %d\n", m->nodeid, n->nodeid); set_insert(m->descendants, n); set_insert(n->ancestors, m); } } }
struct set *set_duplicate(struct set *s) { struct set *s2; s2 = set_create(0); set_first_element(s); const void *element; while((element = set_next_element(s))) set_insert(s2, element); return s2; }
struct set *set_union(struct set *s1, struct set *s2) { struct set *s = set_duplicate(s1); set_first_element(s2); const void *element; while((element = set_next_element(s2))) set_insert(s, element); return s; }
int set_intersection(Set *seti, const Set *set1, const Set *set2) { SetElm *member; set_init(seti, set1->match, NULL); for (member=set_head(set1); member != NULL; member = set_next(member)) { if (set_is_member(set2, set_data(member))) { if (set_insert(seti, set_data(member)) != 0) { set_destory(seti); return -1; } } } return 0; }
void test_set() { U_set set = NULL; // 一定要初始化为NULL,要不可能会出问题 string s = "A"; set_insert(&set,&s,STRING); s = "B"; set_insert(&set, &s, STRING); s = "A"; set_insert(&set, &s, STRING); s = "A"; set_insert(&set, &s, STRING); U_set set_ = NULL; s = "C"; set_insert(&set_, &s, STRING); for(U_set p=set; p; p=p->next) { printf("set: %s\n", p->val.s); } printf("\n"); set_union(&set, &set_); for(U_set p=set; p; p=p->next) { printf("set: %s\n", p->val.s); } }
static int dag_parse_export(struct lexer *bk) { struct token *t, *vtoken, *vname; const char *name; int count = 0; while((t = lexer_peek_next_token(bk)) && t->type != TOKEN_NEWLINE) { switch(t->type) { case TOKEN_VARIABLE: vtoken = lexer_next_token(bk); //Save VARIABLE token. vname = lexer_peek_next_token(bk); if(vname->type == TOKEN_LITERAL) { name = xxstrdup(vname->lexeme); } else { lexer_report_error(bk, "Variable definition has name missing.\n"); } lexer_preppend_token(bk, vtoken); //Restore VARIABLE token. dag_parse_variable(bk, NULL); break; case TOKEN_LITERAL: t = lexer_next_token(bk); name = xxstrdup(t->lexeme); lexer_free_token(t); break; default: lexer_report_error(bk, "Malformed export syntax.\n"); break; } set_insert(bk->d->export_vars, name); count++; debug(D_MAKEFLOW_PARSER, "export variable: %s", name); } if(t) { //Free newline t = lexer_next_token(bk); lexer_free_token(t); } if(count < 1) { lexer_report_error(bk, "The export syntax needs the explicit name of the variables to be exported.\n"); } return 1; }
void test_set_insert (void) { Set* set; int numbers1[] = { 1, 2, 3, 4, 5, 6 }; int numbers2[] = { 5, 6, 7, 8, 9, 10 }; int i; /* Perform a union of numbers1 and numbers2. Cannot add the same * value twice. */ set = set_new (int_hash, int_equal); for (i = 0; i < 6; ++i) { set_insert (set, &numbers1[i]); } for (i = 0; i < 6; ++i) { set_insert (set, &numbers2[i]); } assert (set_num_entries (set) == 10); set_free (set); }
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 set_union (Set *setu, const Set *set1, const Set *set2) { SetElm *member; set_init(setu, set1->match, NULL); for (member=set_head(set1); member != NULL; member = set_next(member)) { if (set_insert(setu, set_data(member)) != 0) { set_destory(setu); return -1; } } for (member=set_head(set2); member != NULL; member = set_next(member)) { switch (set_insert(setu, set_data(member))) { case 0: case 1: break; case -1: set_destory(setu); return -1; break; } } 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; }
void archive(struct file_info_t *info, struct file_entry_t *entry) { Set *hash_set = trie_lookup(info->hash_trie, entry->hash); if (hash_set == TRIE_NULL) { /* Otherwise, the value needs a new list */ hash_set = set_new(&pointer_hash, &pointer_equal); slist_prepend(&info->duplicates, hash_set); trie_insert(info->hash_trie, entry->hash, hash_set); } if (!set_insert(hash_set, entry)) { #ifndef NDEBUG fprintf(stderr, "[DEBUG] '%s' (extra file)\n", entry->path); #endif } }
static AstarArray* search(AstarNode *current, int dst, Set *open_set, Set *close_set, void *ud) { AstarArray *adjs = NULL; if (!current) return NULL; adjs = get_adjs(ud, current->index); int j; for (j = 0; j < adjs->len; j++) { int i = adjs->arr[j]; if (i == dst) { return path_backtrace(current, dst); } if (set_find(close_set, i) != -1) continue; int new_g = gscore(ud, current->index, i) + current->g; int index; if ((index = set_find(open_set, i)) != -1) { AstarNode *node = set_index(open_set, index); if (node->g < new_g) { continue; } node->g = new_g; node->parent = current; set_bubble(open_set, index); } else { AstarNode *node = create_node(ud, i, dst, current); set_insert(open_set, node); } } array_release(&adjs); int x = current->index % 30; int y = current->index / 30; printf("current is %d %d\n",x,y ); fflush(stdout); set_push(close_set, current); AstarNode *next = set_pop(open_set); return search(next, dst, open_set, close_set, ud); }
/*@ set_union - find the difference of two sets Input arguments: + s - The first set - t - The second set Output arguments: None Returns: A new set that contains all the elements of s not in t, NULL otherwise. Notes: s and t must contain the same types of elements @*/ struct set *set_diff(struct set *s, struct set *t) { void * e; struct set *tmp; /* Try to make sure that these two sets contain the same types */ if (s->cmp != t->cmp || s->elemsize != t->elemsize) return NULL; tmp = set_create(s->maxsize+t->maxsize, s->elemsize, s->cmp); if (tmp == NULL) return NULL; /* Insert all the elements of s not in t */ for (set_reset(s), e = set_next(s); e != NULL; e = set_next(s)){ if (!set_exists(t, e)) set_insert(tmp, e); } return tmp; }
static int set_double_buckets(struct set *s) { struct set *sn = set_create(2 * s->bucket_count); if(!sn) return 0; /* Move elements to new set */ void *element; set_first_element(s); while( (element = set_next_element(s)) ) if(!set_insert(sn, element)) { set_delete(sn); return 0; } /* Delete all elements */ struct entry *e, *f; int i; for(i = 0; i < s->bucket_count; i++) { e = s->buckets[i]; while(e) { f = e->next; free(e); e = f; } } /* Make the old point to the new */ free(s->buckets); s->buckets = sn->buckets; s->bucket_count = sn->bucket_count; s->size = sn->size; /* Delete reference to new, so old is safe */ free(sn); return 1; }
Set *set_intersection(Set *set1, Set *set2) { Set *new_set; SetIterator iterator; SetValue value; new_set = set_new(set1->hash_func, set2->equal_func); if (new_set == NULL) { return NULL; } /* Iterate over all values in set 1. */ set_iterate(set1, &iterator); while (set_iter_has_more(&iterator)) { /* Get the next value */ value = set_iter_next(&iterator); /* Is this value in set 2 as well? If so, it should be * in the new set. */ if (set_query(set2, value) != 0) { /* Copy the value first before inserting, * if necessary */ if (!set_insert(new_set, value)) { set_free(new_set); return NULL; } } } return new_set; }
void listenSSIDThread(void* arg) { int ret = pthread_detach(pthread_self()); long svr_socketfd = (long)arg; unsigned int sin_size = sizeof(struct sockaddr_in); struct sockaddr_in their_addr; /* 客户地址信息 */ int new_fd = 0; while (1) { //Info("ssid_fdset size[%d]wait connect...", set_get_elem_num(&ssid_fdset)); //Info("ssid_fdset size[%d]wait connect...", set_get_elem_num(&ssid_fdset)); if ((new_fd = accept(svr_socketfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) { Error("accept error!"); //Info("Error:accept error!"); perror("accept!\n"); continue; } //Info("sockfd[%d]got connection from %s fd[%d] check user...", svr_socketfd, inet_ntoa(their_addr.sin_addr), new_fd); //Info("sockfd[%d]got connection from %s fd[%d] check user...", svr_socketfd, inet_ntoa(their_addr.sin_addr), new_fd); set_insert(new_fd, &ssid_fdset); } }