void WFREQ_free_hashtable_ID_1(void) { HASHNODE * hash_table[BUCKETSIZE] = {NULL}; char word[] = "abcd"; char word2[] = "pqrt"; /*when the hashtable is not present*/ free_hashtable(hash_table); add_inc_word_in_hashtbl(word,hash_table); add_inc_word_in_hashtbl(word2,hash_table); /*case 2:*/ free_hashtable(hash_table); }
/******************************************************************************************************************* * Function : WFREQ_myhash_id_1 * Description : 1. Verify that a new memory is allocated when a new element is added to the hash table with a * new index. * 2. Verify that if already there is element at an index in hashtable then only counter is incremented. * Input : Nothing * Output : Nothing * Exception : ********************************************************************************************************************/ void WFREQ_add_inc_word_in_hashtbl_ID_1(void) { HASHNODE *head[BUCKETSIZE] = {{NULL}}; char word[] = "abcd"; int ret = FAILURE; unsigned int hash_val = 0; HASHNODE *temp_hash = NULL; /*case 1: when no element in hash table*/ hash_val = myhash(word); ret = add_inc_word_in_hashtbl(word,head); temp_hash = head[hash_val]; CU_ASSERT(SUCCESS == ret); CU_ASSERT(1 == temp_hash->count); /*case 2 one element already added in hash table*/ ret = add_inc_word_in_hashtbl(word,head); temp_hash = head[hash_val]; CU_ASSERT(SUCCESS == ret); CU_ASSERT(2 == temp_hash->count); /*free the memory after use*/ free_hashtable(head); }
int main() { Status status; char buff[100]; // Setup configuration for validating alphabetic characters Hashtable *config = init_hashtable(); config->add(config, "min_chars", "4"); config->add(config, "max_chars", "10"); // Instantiate the validation class with configuration and validation type IOValidate *v = init_validate(config, VALIDATE_ALPHA); /* // Setup configuration for validating alphanum characters with specific chars Hashtable *config = init_hashtable(); config->add(config, "min_chars", "4"); config->add(config, "max_chars", "10"); config->add(config, "allowed_chars", "abcd"); // Instantiate the validation class with configuration and validation type IOValidate *v = init_validate(config, VALIDATE_ALPHANUM); */ /* // Setup configuration for validating numbers Hashtable *config = init_hashtable(); config->add(config, "min_num", "10"); config->add(config, "max_num", "1000"); // Instantiate the validation class with configuration and validation type IOValidate *v = init_validate(config, VALIDATE_NUM); */ // Get user input fgets(buff, sizeof(buff), stdin); // Test the user provided input with our validation if (v->isvalid(v, &status, buff)) { printf("Validated!\n"); } else { printf("Status code: %d\n", status.code); printf("Status msg: %s\n", status.msg); } release(v); free_hashtable(config); return 0; }
void freeGrafoD(void* grafo, void*** ar0){ if(grafo == NULL) return; Grafo *gr; gr = (Grafo*)grafo; if(ar0!=NULL){ Lista vertices; vertices = KDT_getAll(gr->vertices); int size = Lista_lenght(vertices); freeLista(vertices); for(int i=0; i<size; i++) free(ar0[i]); free(ar0); } if(gr->vertices != NULL) freeKDTree(gr->vertices); if(gr->left != NULL) free_hashtable(gr->left); if(gr->right != NULL) free_hashtable(gr->right); if(gr->ID != NULL) free_hashtable(gr->ID); free(gr); }
void free_gc_cache (struct gc_cache *cache) { struct gc_cache_cell *rest, *next; BLOCK_INPUT; rest = cache->head; while (rest) { XFreeGC (cache->dpy, rest->gc); next = rest->next; xfree (rest); rest = next; } UNBLOCK_INPUT; #ifdef GCCACHE_HASH free_hashtable (cache->table); #endif xfree (cache); }
int main( int argc, char **argv ) { int i; int N=NUMBER_KEYS; // The number of keys to insert in the hash_table char* keys[NUMBER_KEYS]={KEYS}; char* values[NUMBER_KEYS]={VALUES}; hashtable *hash_table = new_hashtable(); //The definition of the zize intialized to 8 srand(time(NULL)); // here we start by genarting a random keys and values printf("############ Initialisation of keys #################################################################\n\n"); for(i=0;i<N;i++) { printf("keys[%d]==\"%s\" with hash %d\n",i,keys[i],hash(keys[i])%hash_table->size); } printf("\n############ Initialisation of values #############################################################\n\n"); for(i=0;i<N;i++) { printf("values[%d]==\"%s\"\n",i,values[i]); } // insertion of key-value pairs in the hash_table printf("\n############ Insertion of (key,value) pairs ########################################################\n\n"); for(i=0;i<N;i++) { insert_resize_pair(&hash_table,keys[i],values[i]); printf("Insertion of (\"%s\",\"%s\") succed , SIZE = %d \n",keys [i],values[i],hash_table->size); } printf("\n Our hash table = "); print_hashtable(hash_table); // reinsertion of some pairs in the hash_table printf("\n############ Reinsertion of some pairs #############################################################\n\n"); insert_pair( hash_table, keys[1], "19" ); printf("Reinsertion of (\"%s\",\"%s\") succed \n",keys [1],"19"); insert_pair( hash_table, keys[2], "13" ); printf("Reinsertion of (\"%s\",\"%s\") succed \n",keys [2],"13"); printf("the hash table = "); print_hashtable(hash_table); // getting the values of a given key printf("\n############ Getting the value for a given key #######################################################\n\n"); for(i=0;i<N;i++) printf("hash_table of \"%s\" gives \"%s\"\n",keys[i], get_value( hash_table, keys[i])); // removing the some keys from the hash table printf("\n############ Removing some keys from hash table ######################################################\n\n"); printf("Initialy we have\n"); print_hashtable(hash_table); for(i=0;2*i<N;i++) { delate_key( hash_table, keys[2*i]); printf("we delate the key \"%s\"\n",keys[2*i]); print_hashtable(hash_table); } printf("\n#### After free we print the hash_table to verify #################################################\n\n"); free_hashtable(hash_table); print_hashtable(hash_table); return 0; }
int main(int argc, char *argv[]){ int numtasks, rank, rc,tag=1; FILE *configFile,*fileOut; char *command; char **argv2; command = "./mapper_program"; char configFileName[] = "config.in"; int maxMappers; int maxReducers; MPI_Status stat; MPI_Comm intercomm; Hashtable hashtable; int i,j; char fileInName[100]; char fileOutName[100]; MPI_Datatype MPI_ENTRY_WORD, oldtypes[2]; int blockcounts[2]; /* MPI_Aint type used to be consistent with syntax of */ /* MPI_Type_extent routine */ MPI_Aint offsets[2], extent; hashtable = create_hashtable(1000); configFile = fopen(configFileName,"r"); if(configFile == NULL) printf("Error opening %s file for configurations\n",configFileName); //citire fisier de configuratii fscanf(configFile,"%d\n",&maxMappers); fscanf(configFile,"%d\n",&maxReducers); fscanf(configFile,"%s\n",fileInName); fscanf(configFile,"%s\n",fileOutName); fileOut = fopen(fileOutName,"w"); printf("Master: mappers %d reducers %d\n",maxMappers,maxReducers); char buff[20]; sprintf(buff,"%d",maxReducers); argv2=(char **)malloc(3 * sizeof(char *)); argv2[0] = (char*)malloc(100*sizeof(char)); strcpy(argv2[0],fileInName); argv2[1] = buff; argv2[2] = NULL; printf("Master: Fisierul de citit %s | %s ------------------\n",argv2[1],argv2[0]); int *errcodes = (int*)malloc(maxMappers * sizeof(int)); rc = MPI_Init(&argc,&argv); if (rc != MPI_SUCCESS) { printf ("Error starting MPI program. Terminating.\n"); MPI_Abort(MPI_COMM_WORLD, rc); } //commit structura mpi offsets[0] = 0; oldtypes[0] = MPI_INT; blockcounts[0] = ENTRIES + 1; /* Setup description of the 2 MPI_INT fields n, type */ /* Need to first figure offset by getting size of MPI_FLOAT */ MPI_Type_extent(MPI_INT, &extent); offsets[1] = (ENTRIES + 1) * extent; oldtypes[1] = MPI_CHAR; blockcounts[1] = ENTRIES * 50; /* Now define structured type and commit it */ MPI_Type_struct(2, blockcounts, offsets, oldtypes, &MPI_ENTRY_WORD); MPI_Type_commit(&MPI_ENTRY_WORD); MPI_Comm_size(MPI_COMM_WORLD,&numtasks); MPI_Comm_rank(MPI_COMM_WORLD,&rank); //proces principal ce va face spawn proceselor map MPI_Comm_spawn(command,argv2,maxMappers,MPI_INFO_NULL,0,MPI_COMM_WORLD,&intercomm,errcodes); //for(i=0;i<maxMappers;i++) // MPI_Send(&i,1,MPI_INT,i,tag,intercomm); /******* do some work *******/ int ok=0; while(ok<maxMappers){ MPI_Recv(&entry,1,MPI_ENTRY_WORD,MPI_ANY_SOURCE,tag,intercomm,&stat); if(entry.aparitii[0] == -1){ ok++; printf("Master:Finalize from %d\n",stat.MPI_SOURCE); } else for(i=0;i<entry.numar_intrari;i++) merge_elements(hashtable,entry.cuvinte[i],entry.aparitii[i]); } MPI_Finalize(); //print_hashtable(hashtable,fileOut); Entry *list; int listSize = 1000; int listPoz = 0; list = (Entry*) malloc(sizeof(Entry)*listSize); printf("Master: start to make list\n"); for(i=0;i<hashtable->size;i++){ if(hashtable->buckets[i].size > 0){ for(j=0;j<hashtable->buckets[i].current;j++){ if(listPoz == listSize){ list = (Entry*)realloc(list,sizeof(Entry)*(listSize+1000)); listSize += 1000; } list[listPoz].value = hashtable->buckets[i].entries[j].value; list[listPoz].number = hashtable->buckets[i].entries[j].number; listPoz++; } } } printf ("Master: %d am terminat\n",rank); // sort the list using quicksort quicksort(list,0,listPoz-1); // print the result //printf("The list after sorting using quicksort algorithm:\n"); printlist(list,listPoz,fileOut); free_hashtable(hashtable); fclose(fileOut); return 0; }
int main(int argc, char *argv[]){ int numtasks, rank, rc,tag=1,i,j; int maxReducers; FILE *file; char *command; command = "./reduce_program"; MPI_File mpi_file; MPI_Status status,stat; int bufsize,filesize,chuncksize; char *buf; int offset; int chunck; int aux; MPI_Comm intercomm_reducers,parentcomm; Hashtable hashtable; hashtable = create_hashtable(500); MPI_Datatype MPI_ENTRY_WORD, oldtypes[2]; int blockcounts[2]; /* MPI_Aint type used to be consistent with syntax of */ /* MPI_Type_extent routine */ MPI_Aint offsets[2], extent; //aflare dimensiune fisier file = fopen(argv[1],"r"); filesize = fsize(file); //printf("Mapper: Fisierul are dimensiunea %d\n",filesize); fclose(file); maxReducers = atoi(argv[2]); int *errcodes = (int*)malloc(maxReducers * sizeof(int)); //printf("Mapper: Fisierul de intrare este %s ---- %s|\n",argv[1],argv[2]); rc = MPI_Init(&argc,&argv); if (rc != MPI_SUCCESS) { printf ("Error starting MPI program. Terminating.\n"); MPI_Abort(MPI_COMM_WORLD, rc); } MPI_Comm_size(MPI_COMM_WORLD,&numtasks); MPI_Comm_rank(MPI_COMM_WORLD,&rank); MPI_Comm_get_parent( &parentcomm ); //commit structura mpi offsets[0] = 0; oldtypes[0] = MPI_INT; blockcounts[0] = ENTRIES + 1; /*Commit structura definita pentru comunicatia informatiilor intre reduceri si mapperi si mapperi si master */ MPI_Type_extent(MPI_INT, &extent); offsets[1] = (ENTRIES + 1) * extent; oldtypes[1] = MPI_CHAR; blockcounts[1] = ENTRIES * 50; /* Now define structured type and commit it */ MPI_Type_struct(2, blockcounts, offsets, oldtypes, &MPI_ENTRY_WORD); MPI_Type_commit(&MPI_ENTRY_WORD); //printf("Redy for spawn"); //proces principal ce va face spawn proceselor map MPI_Comm_spawn(command,MPI_ARGV_NULL,maxReducers,MPI_INFO_NULL,0,MPI_COMM_SELF,&intercomm_reducers,errcodes); for(i=0;i<maxReducers;i++) if(errcodes[i] == MPI_SUCCESS) printf("Mapper %d: spwan reducer %d\n",rank,i); else printf("Mapper %d:error spwn reducer %d\n",rank,i); bufsize = filesize/numtasks; if(rank == numtasks-1){ bufsize = filesize - rank*bufsize; } chuncksize = bufsize/maxReducers; buf = (char*)malloc(sizeof(char)*bufsize); MPI_File_open(MPI_COMM_WORLD, argv[1], MPI_MODE_RDONLY, MPI_INFO_NULL, &mpi_file); MPI_File_read_at(mpi_file, rank*bufsize, buf, bufsize, MPI_CHAR, &status); MPI_File_close(&mpi_file); //printf("Mapper %d: {%s}\n",rank,buf); /******* do some work *******/ chunck = chuncksize; offset=0; //printf("Mapper %d: asteapta procesele reduce\n",rank); /********** asteapta ca toti reducerii sa fie activi**************/ for(i=0;i<maxReducers;i++){ MPI_Recv(&aux,1,MPI_INT,MPI_ANY_SOURCE,tag,intercomm_reducers,&stat); } /********** raspandire chunk-uri catre procesele reduce *************/ for(i=0;i<maxReducers;i++){ if(i == maxReducers-1) { chunck=bufsize-i*chunck; } //printf("Mapper %d: prepare to send data to reducer %d\n",rank,i); MPI_Send(&chunck, 1, MPI_INT, i, tag, intercomm_reducers); MPI_Send(&buf[offset], chunck, MPI_CHAR, i, tag, intercomm_reducers); offset += chunck; } int ok=0; while(ok<maxReducers){ MPI_Recv(&entry,1,MPI_ENTRY_WORD,MPI_ANY_SOURCE,tag,intercomm_reducers,&stat); if(entry.aparitii[0] == -1) ok++; else for(i=0;i<entry.numar_intrari;i++) merge_elements(hashtable,entry.cuvinte[i],entry.aparitii[i]); } printf("Mapper %d: Am toate informatiile de la procesele reduce\n",rank); entry.numar_intrari=0; /******* Trimite informatiile la procesul principal **************/ for(i=0;i<hashtable->size;i++){ for(j=0;j<hashtable->buckets[i].current;j++){ if(entry.numar_intrari == ENTRIES){ MPI_Send(&entry,1,MPI_ENTRY_WORD,0,tag,parentcomm); //printf("Mapper %d: trimite hash date\n",rank); entry.numar_intrari=0; } else { //printf("Mapper %d: Cuv %s\n",rank,hashtable->buckets[i].entries[j].value); entry.aparitii[entry.numar_intrari] = hashtable->buckets[i].entries[j].number; strcpy(entry.cuvinte[entry.numar_intrari],hashtable->buckets[i].entries[j].value); entry.numar_intrari++; } } } if(entry.numar_intrari > 0){ MPI_Send(&entry,1,MPI_ENTRY_WORD,0,tag,parentcomm); //printf("Mapper %d: trimite hash date\n",rank); entry.numar_intrari=0; } /************ Terminare trimitere hashtable *************/ entry.aparitii[0] = -1; //printf("Mapper %d: send finish to main process\n ",rank); MPI_Send(&entry,1,MPI_ENTRY_WORD,0,tag,parentcomm); //printf("Mapper %d: %d %s",rank,bufsize,buf); printf ("Mapper: %d am terminat\n", rank); //print_hashtable(hashtable,stdout); MPI_Finalize(); free_hashtable(hashtable); return 0; }
th_path *ai_shortes_path(int player, int unit, th_point source, th_point goal) { int i, a; int count; th_vector vector; th_point pt; th_point *solution; th_path *path; bheap *open; struct hashtable *closed; bheap_node *e; bheap_node *p; bheap_node *n; // Are the source and goal point valid? if( source.x >= 0 && source.x <= x_tildes && source.y >= 0 && source.y <= y_tildes && goal.x >= 0 && goal.x <= x_tildes && goal.y >= 0 && goal.y <= y_tildes ) { // TODO: Actual way to store binary heap nodes is a bad idea. We need make // te code to use the dinamic strucures in bheap_add. e = (bheap_node *)malloc(1000 * x_tildes * y_tildes * sizeof(bheap_node)); if(e == NULL) return NULL; count = 0; i = 0; // Creating open and closed lists open = bheap_init(x_tildes * y_tildes); closed = make_hashtable(hashtable_default_hash, x_tildes * y_tildes); // Defining the initial node sprintf(e[count].id, "%03d%03d", source.x, source.y); //printf("====================== A* STARTING... =====================\n"); //printf("Element id to store: %s\n", e[count].id); e[count].deph = 0; e[count].point = source; e[count].h = HDIST(e[count].point.x, e[count].point.y, goal.x, goal.y); e[count].g = 0; e[count].val = e[count].g + e[count].h; e[count].index = count; e[count].parent = NULL; // Insert the initial node to the open list if(!bheap_add(open, &e[count])) { printf("Coudn't add element to the open list!\n"); return NULL; } //bheap_print(open); while(open->count >= 0) { //printf("********** New Loop Cycle\n"); // Remove the lowest element in open list // and add it to the closed list n = bheap_del(open); if(n == NULL) { printf("Error deleting the priority element from open list!\n"); return NULL; } //printf("Removed id: %s\n", n->id); //bheap_print(open); //printf("Element id to store in loop: %s, index: %d\n", n->id, n->index); if(!hashtable_add(closed, e[n->index].id, &e[n->index])) { printf("Error adding to hashtable!\n"); return NULL; } //hashtable_iter(closed, hashtable_default_hash); //Is this element the goal? if(n->point.x == goal.x && n->point.y == goal.y) { printf("Solution deph is %d\n", n->deph); solution = (th_point *)malloc(n->deph * sizeof(th_point)); if(!solution) return NULL; path = (th_path *)malloc(sizeof(th_path)); if(!path) return NULL; i=0; while(n->parent) { printf("(%d,%d)\n",n->point.x, n->point.y); solution[i] = n->point; n = n->parent; i++; } path->path = solution; path->size = i - 1; free_hashtable(closed); bheap_free(open); FREE(e); return path; } //printf("This element is not the goal!.. Trying...\n"); //For each valid move for n for(a = 0; a < NUM_DIRS; a++) { vector = get_vector(n->point, a); if(vector.x != -2 && vector.y != -2) { //printf("Vector is valid... \n"); //printf("For %d direction tile in (%d,%d) is valid?\n", a, n->point.x, n->point.y); pt.x = vector.x + n->point.x; pt.y = vector.y + n->point.y; if(ai_valid_tile(player, unit, pt)) { //printf("Adding direction %d to open list!\n", a); //New valid element count++; e[count].deph = n->deph + 1; e[count].point = pt; memset(e[count].id, 0, 7); sprintf(e[count].id, "%03d%03d", pt.x, pt.y); e[count].index = count; e[count].h = HDIST(e[count].point.x, e[count].point.y, goal.x, goal.y); if( a == ISO_N || a == ISO_S || a == ISO_W || a == ISO_E) e[count].g = n->g + 10; else e[count].g = n->g + 14; e[count].val = e[count].g + e[count].h; // F = G + H e[count].parent = n; //printf("Actual id: %s, H: %d G:%d F:%d Deph:%d\n", e[count].id, e[count].h, // e[count].g, e[count].val, e[count].deph); //Is this element in closed list? if((p = hashtable_lookup(closed, e[count].id)) != NULL) { //printf("P exists in cloded list!\n"); if(p->val > e[count].val) { if(!hashtable_remove(closed, p->id)) { printf("Error ocurred while trying to remove key in hashtable!\n"); hashtable_iter(closed, hashtable_default_hash); return NULL; } //else //{ //printf("Removes OK, let's check integrity!\n"); //hashtable_iter(closed, hashtable_default_hash); //} if(!bheap_add(open, p)) { printf("Error ocurred while adding a element to open list\n"); return NULL; } //printf("Succesfully removed from closed list and added to open list\n"); } } else { //printf("P doesn't exist in closed list!\n"); if(!bheap_add(open, &e[count])) { printf("Error ocurred while adding a new element to open list\n"); return NULL; } } //bheap_print(open); } } } } free_hashtable(closed); bheap_free(open); FREE(e); } else { printf("Bad point references : Origin(%d, %d) Dest(%d, %d)\n", source.x, source.y, goal.x, goal.y); } }