void init_fonts() { if (!initialized) { font_table = create_hash_table(); binding_table = create_hash_table(); } initialized = True; }
void init_textures() { if (!initialized) { texture_table = create_hash_table(); binding_table = create_hash_table(); } initialized = True; }
bool_t set_saved_race_results( char *player, char *event, char *cup, char *race, difficulty_level_t d, scalar_t time, int herring, int score ) { hash_table_t player_table; hash_table_t event_table; hash_table_t cup_table; hash_table_t race_table; save_info_t *this_save; player_table = results_save_table[d]; if ( !get_hash_entry( player_table, player, (hash_entry_t*)&event_table ) ) { event_table = create_hash_table(); add_hash_entry( player_table, player, (hash_entry_t)event_table ); } if ( !get_hash_entry( event_table, event, (hash_entry_t*)&cup_table ) ) { cup_table = create_hash_table(); add_hash_entry( event_table, event, (hash_entry_t)cup_table ); } if ( !get_hash_entry( cup_table, cup, (hash_entry_t*)&race_table ) ) { race_table = create_hash_table(); add_hash_entry( cup_table, cup, (hash_entry_t)race_table ); } if ( !get_hash_entry( race_table, race, (hash_entry_t*)&this_save ) ) { this_save = (save_info_t*)malloc(sizeof(save_info_t)); memset( this_save, 0, sizeof(save_info_t) ); add_hash_entry( race_table, race, (hash_entry_t)this_save ); this_save->data_type = RACE_RESULTS; } check_assertion( this_save->data_type == RACE_RESULTS, "Invalid data type" ); strcpy( this_save->data.results.event, event ); strcpy( this_save->data.results.cup, cup ); strcpy( this_save->data.results.race, race ); this_save->data.results.difficulty = d; this_save->data.results.time = time; this_save->data.results.herring = herring; this_save->data.results.score = score; #ifdef __APPLE__ // Write as soon as possible write_saved_games(); #endif return True; }
/*! Initializes the ui manager module \return None \author jfpatry \date Created: 2000-09-16 \date Modified: 2000-09-16 */ void init_ui_manager() { if ( !initialized ) { mouse_motion_cbs = create_hash_table(); mouse_down_cbs = create_hash_table(); mouse_up_cbs = create_hash_table(); widget_draw_cbs = create_hash_table(); } initialized = True; }
int main() { FILE *fp = fopen("name.txt","r"); char *str; hash_table_t *my_hash_table; int size_of_table = 12; list_t *item; int count=0; //check how many item does not match /* Create a new hash table */ my_hash_table = create_hash_table(size_of_table); /* Add the string to hash table */ while( (fscanf(fp,"%s",str )) !=EOF) add_string(my_hash_table,str); fclose(fp); fp = fopen("input.txt","r"); while( (fscanf(fp,"%s",str)) != EOF ){ item = lookup_string (my_hash_table , str); if(item == NULL){ printf("Can't found %s \n",str); count++; } else printf("Found %s - %s\n",str , item->str); } printf("%d\n",count); fclose(fp); /* Delete the table */ free_table(my_hash_table); }
hash_table* hash_ids(const char* fn) { fprintf(stderr, "hashing ... \n"); hash_table* T = create_hash_table(); samfile_t* f = samopen(fn, "rb", NULL); if (f == NULL) { fprintf(stderr, "can't open bam file %s\n", fn); exit(1); } bam1_t* b = bam_init1(); uint32_t n = 0; while (samread(f, b) >= 0) { if (++n % 1000000 == 0) { fprintf(stderr, "\t%d reads\n", n); } inc_hash_table(T, bam1_qname(b), b->core.l_qname); } bam_destroy1(b); samclose(f); fprintf(stderr, "done.\n"); return T; }
//finds route to target location and returns a pointer to the board that found it Node* find_solution (Node *start, int height_coordinate, int width_coordinate){ hash *hash_array = create_hash_table (HASH_TABLE_SIZE); set_hash_table (hash_array); Node *current, *previous, *parent; int target_found = 0; current = start; parent = start; while (!target_found){ previous = current; current->next = allocate_new_node (parent, previous); if (find_next_move (hash_array, start, current->next, parent->board, current->next->board) == 0){ if (parent->next == NULL){ printf("Couldn't find a solution\n"); exit (1); } parent = parent->next; current->next->parent = parent; copy_board (current->next->board, parent->board); } target_found = check_for_target (current->next->board, height_coordinate, width_coordinate); current = current->next; } return current; }
void test_hash_table_maybe_grow(void) { // It should be possible for the table to grow, preserving all previous values hash_table_t table; create_hash_table(10, &table); hash_table_set("Pottery_clay0", 0, &table); hash_table_set("Dark_smoked_gla0", 1, &table); hash_table_set("Pottery_clay1", 2, &table); hash_table_set("Dark_smoked_gla1", 3, &table); hash_table_set("Metallic_Varni0", 4, &table); hash_table_set("Body0", 5, &table); hash_table_set("Pottery_clay2", 6, &table); hash_table_set("850matri0", 7, &table); hash_table_set("850matri1", 8, &table); hash_table_set("Pottery_clay3", 9, &table); hash_table_set("Pottery_clay4", 10, &table); TEST_CHECK(table.capacity > 10); TEST_CHECK(table.n == 11); TEST_CHECK(hash_table_get("Pottery_clay0", &table) == 0); TEST_CHECK(hash_table_get("Dark_smoked_gla0", &table) == 1); TEST_CHECK(hash_table_get("Pottery_clay1", &table) == 2); TEST_CHECK(hash_table_get("Dark_smoked_gla1", &table) == 3); TEST_CHECK(hash_table_get("Metallic_Varni0", &table) == 4); TEST_CHECK(hash_table_get("Body0", &table) == 5); TEST_CHECK(hash_table_get("Pottery_clay2", &table) == 6); TEST_CHECK(hash_table_get("850matri0", &table) == 7); TEST_CHECK(hash_table_get("850matri1", &table) == 8); TEST_CHECK(hash_table_get("Pottery_clay3", &table) == 9); TEST_CHECK(hash_table_get("Pottery_clay4", &table) == 10); destroy_hash_table(&table); }
/* * this function allows the user to double the # of buckets at run time * -returns pointer to the hash_table - or NULL if error */ hash_table* double_table_size (hash_table* table, int num_of_buckets) { /* allocate memory for a new hash table - double the size */ /* students may use "realloc()" if they understand it properly! - not required */ hash_table* new_table = create_hash_table(num_of_buckets * 2); if (new_table) { int i; for (i=0; i<table->num_of_buckets; i++) { while (table->table[i]) { char * name = table->table[i]->value; add_to_table(new_table, name); delete_from_table(table, name); } } delete_table(table); return new_table; } else { return NULL; } /* move data from old hash table to new hash table - if necessary */ /* don't forget to free old hash table memory before returning */ }
void main() { int i; int findCnt; list_t* listShow= NULL; char *find[]={"hello","home"}; int wordsToHash = 20;//Remember to define your size!!!(how many phrase is s[] )! static char *s[]={"steve","bOB","apple","ban","Johnson", "banana","ice","happy","home","hello","love","wen","danny" ,"dog","hot" ,"cold","fato","fatrabbit","jerry","tux"}; //timer hash_table_t *my_hash_table; clock_t start, stop; struct timespec go, end; double cpu_time1; int size_of_table = 12; clock_gettime(CLOCK_REALTIME, &go); start = clock(); my_hash_table = create_hash_table(size_of_table); //hashing Remember for( i=0; i<wordsToHash ;i++){ add_string(my_hash_table,s[i]); } listShow = lookup_string(my_hash_table,find[1]); stop = clock(); clock_gettime(CLOCK_REALTIME, &end); cpu_time1 = diff_in_second(go, end); float elapsedTime = (float)(stop - start) /(float)CLOCKS_PER_SEC * 1000.0f; printf( "Time to hash: %3.1f ms\n", elapsedTime ); printf("execution time of cpu : %lf sec\n", cpu_time1); printf("Found the word list \n"); printList(listShow); printf("Found the word is %s \n",listShow->str); printf("-------------------------- \n"); printf("Print hash table content \n"); for(i=0; i<size_of_table ;i++) { printList(my_hash_table->table[i]); printf("\n"); } free_table(my_hash_table); }
int conflict_cost(Graph* graph, char** conflict_vertices, int size){ int cost = 0; hash_table_t* rep = create_hash_table(graph->iterator_size, NULL); for (int i = 0; i < size; i++){ count_cost(graph, conflict_vertices[i], rep, &cost, NULL); } free_hash_table(rep); return cost; }
/* The same as create_hash_table, but stores information about copy functions. There are two such a functions , one for tag and another for cell. These functions must allocate a space, copy a field and return the pointer to it*/ HASH_TABLE create_hash_table_with_copy(word number_of_cells, HASH *hash,CMP *cmp, DESTRUCTOR *destructor, CPY *new_tag, CPY *new_cell) { HASH_TABLE tmp=create_hash_table(number_of_cells,hash,cmp,destructor); tmp->new_tag=new_tag; tmp->new_cell=new_cell; return(tmp); }/*create_hash_table_with_copy*/
hash_table* hash_ids(const char* fn) { fprintf(stderr, "hashing ... \n"); hash_table* T = create_hash_table(); samfile_t* f = samopen(fn, "rb", NULL); if (f == NULL) { fprintf(stderr, "can't open bam file %s\n", fn); exit(1); } bam1_t* b = bam_init1(); uint32_t n = 0; char* qname = NULL; size_t qname_size = 0; while (samread(f, b) >= 0) { if (++n % 1000000 == 0) { fprintf(stderr, "\t%d reads\n", n); } if (qname_size < b->core.l_qname + 3) { qname_size = b->core.l_qname + 3; qname = realloc(qname, qname_size); } memcpy(qname, bam1_qname(b), b->core.l_qname); if (b->core.flag & BAM_FREAD2) { qname[b->core.l_qname] = '/'; qname[b->core.l_qname + 1] = '2'; qname[b->core.l_qname + 2] = '\0'; } else { qname[b->core.l_qname] = '/'; qname[b->core.l_qname + 1] = '1'; qname[b->core.l_qname + 2] = '\0'; } inc_hash_table(T, qname, b->core.l_qname + 2); } free(qname); bam_destroy1(b); samclose(f); fprintf(stderr, "done.\n"); return T; }
static void stat_ps_tree_node(const void * nodep, const VISIT which, const int depth) { int desc_len; PatchSet * ps; struct list_head * next; int counter; void * old; /* Make sure we have it if we do statistics */ if (!author_hash) author_hash = create_hash_table(1023); switch(which) { case postorder: case leaf: ps = *(PatchSet**)nodep; num_patch_sets++; old = NULL; /* Author statistics */ if (put_hash_object_ex(author_hash, ps->author, ps->author, HT_NO_KEYCOPY, NULL, &old) >= 0 && !old) { int len = strlen(ps->author); num_authors++; max_author_len = MAX(max_author_len, len); total_author_len += len; } /* Log message statistics */ desc_len = strlen(ps->descr); max_descr_len = MAX(max_descr_len, desc_len); total_descr_len += desc_len; /* PatchSet member statistics */ counter = 0; next = ps->members.next; while (next != &ps->members) { counter++; next = next->next; } num_ps_member += counter; max_ps_member_in_ps = MAX(max_ps_member_in_ps, counter); break; default: break; } }
http_conn_t* http_conn_create(tcp_conn_t *tcp_conn) { http_conn_t *http_conn = (http_conn_t*)malloc(sizeof(http_conn_t)); http_conn->tcp_conn = NULL; if( tcp_conn ) { http_conn->tcp_conn = tcp_conn; tcp_conn->ctx = http_conn; } http_conn->session = NULL; http_conn->http_header = create_hash_table(100); http_conn->is_keep_alive = 0; return http_conn; }
int crawl (char *start_url, int download_workers, int parse_workers, int queue_size, char *(*_fetch_fn) (char *url), void (*_edge_fn) (char *from, char *to)) { QUEUE_SIZE = queue_size; queue_links = (char **) malloc (sizeof (char *) * queue_size); queue_pages = (list_t *) malloc (sizeof (list_t)); List_Init (queue_pages); urls = create_hash_table(65535); //hash set for unique urls //add start_url to bounded queue //printf("start url = %s\n", start_url); put (start_url); add_string(urls, start_url); /* char *seed_url = NULL; lst_t *l = lookup_string(urls, start_url); if(l!=NULL) seed_url = l->string; if(seed_url != NULL) printf("seed url=%s\n",seed_url); else printf("not found\n"); */ //set sum_queue_lengths to 1 sum_queue_lengths = 1; //create threads pthread_t downloaders[download_workers], parsers[parse_workers]; int i; //printf("creating threads\n"); for (i = 0; i < download_workers; i++) pthread_create (&downloaders[i], NULL, download, _fetch_fn); for (i = 0; i < parse_workers; i++) pthread_create (&parsers[i], NULL, parse, _edge_fn); //printf("created threads\n"); thr_join (); printf ("crawler completed successfully\n"); return 0; }
void test_hash_table_exists(void) { // It should be possible to test for the presence of items in the hash table. hash_table_t table; create_hash_table(20, &table); hash_table_set("potato", 3, &table); hash_table_set("monkey", 8, &table); TEST_CHECK(hash_table_exists("potato", &table)); TEST_CHECK(hash_table_exists("monkey", &table)); TEST_CHECK(!hash_table_exists("radio", &table)); destroy_hash_table(&table); }
void test_create_hash_table(void) { { // Initialised hash table should be initialised with a default capacity. hash_table_t table; create_hash_table(0, &table); TEST_CHECK(table.hashes != NULL); TEST_CHECK(table.entries != NULL); TEST_CHECK(table.capacity == HASH_TABLE_DEFAULT_SIZE); TEST_CHECK(table.n == 0); destroy_hash_table(&table); } { // Initialised hash table should be initialised with supplied capacity. hash_table_t table; create_hash_table(20, &table); TEST_CHECK(table.hashes != NULL); TEST_CHECK(table.entries != NULL); TEST_CHECK(table.capacity == 20); TEST_CHECK(table.n == 0); destroy_hash_table(&table); } }
int main() { int i; key_t sample[] = {47, 7, 11, 11, 29, 16, 92, 22, 8, 3, 33}; chain_t *hash_table[11]; for (i = 0; i < array_length(hash_table); i++) { hash_table[i] = NULL; } create_hash_table(hash_table, sample, array_length(sample)); print_hash_table(hash_table, array_length(hash_table)); return 0; }
int main(int argc, char *argv[]) { unsigned char data = 0; phash_tab_t hash = create_hash_table(); for (data = 0; data < 100; data++) { insert_data_into_hash_table(hash, data); } display_hash_table(hash); delete_data_from_hash(hash, 131); delete_data_from_hash(hash, 33); display_hash_table(hash); delete_hash_table(hash); display_hash_table(hash); return 0; }
void test_hash_table_get(void) { // It should be possible to retrieve item values from the hash table. hash_table_t table; create_hash_table(20, &table); hash_table_set("potato", 3, &table); hash_table_set("monkey", 8, &table); TEST_CHECK(hash_table_get("potato", &table) == 3); TEST_CHECK(hash_table_get("monkey", &table) == 8); // TODO: the value returned by hash_table_get can be from a null pointer. // A default sentinel value could be returned instead e.g. -(2^30) // TEST_CHECK(hash_table_get("radio", &table) == -(2 << 30)); destroy_hash_table(&table); }
int main (int argc, char **argv) { extern FILE *yyin; extern hash_table_t *my_hashtable; argv++, argc--; /* skip over program name */ if (argc > 0) { if (NULL == (yyin = fopen (argv[0], "r"))) { perror("fort320: error:"); exit(EXIT_FAILURE); } } else { fprintf(stdout, "fort320 usage: fort320 in_file.f\n"); exit(EXIT_FAILURE); } if (NULL == (INPUT_FILE_NAME = malloc (NULL_CHAR_SIZE + strlen(argv[0])))) { perror("fort320: error:"); exit(EXIT_FAILURE); } /* For error messages */ strcpy(INPUT_FILE_NAME, argv[0]); list_init(); /* Initialize my_hashtable (defined in parser)*/ my_hashtable = create_hash_table(8); /*custom size of 8 lists*/ if (NULL == my_hashtable) { printf("Hash table allocation error\n"); return -1; } /* Initialize AST */ AST_init(); /* Scan the file */ yyparse(); printf("====== Parse Completed ==============\n\n"); printf("====== Intermediate Representation ===\n"); print_ast(); return 0; }
void init_high_scores( void ) { FILE *score_stream; char score_file[BUFF_LEN]; score_info_t this_score; score_table = create_hash_table(); if ( get_high_score_file_name( score_file, sizeof(score_file) ) == 0 ) { score_stream = fopen( score_file, "r" ); if (score_stream != NULL) { while (fread( &this_score, sizeof(this_score), 1, score_stream)) { set_high_score( this_score.event.event, this_score.event.cup, this_score.player_name, this_score.score ); } if ( fclose( score_stream ) != 0 ) { perror( "fclose" ); } } } }
bool load(const char* dictionary) { //open file and check correct open FILE* dic_file = fopen(dictionary, "r"); if (dic_file == NULL) { printf("Could not open file.\n"); return false; } my_hash_table = create_hash_table(); //read file and adds word to dictionary char temp_string[50]; while(EOF != fscanf(dic_file,"%s",temp_string)) { addNode(temp_string, my_hash_table); word_count++; } return true; }
bool_t set_high_score( char* event, char* cup, char* player, int score ) { hash_table_t *cup_table; score_info_t *this_score; if ( !get_hash_entry( score_table, event, (hash_entry_t*)&cup_table ) ) { cup_table = (hash_table_t*)malloc(sizeof(hash_table_t)); *cup_table = create_hash_table(); add_hash_entry( score_table, event, cup_table ); } if ( !get_hash_entry( *cup_table, cup, (hash_entry_t*)&this_score ) ) { this_score = (score_info_t*)malloc(sizeof(score_info_t)); strcpy(this_score->event.event, event); strcpy(this_score->event.cup, cup); this_score->event.difficulty = (difficulty_level_t)0; this_score->data_type = (score_data_t)0; add_hash_entry( *cup_table, cup, this_score ); } this_score->score = score; strcpy( this_score->player_name, player ); return True; }
bool_t set_last_completed_cup( char* player, char* event, difficulty_level_t d, char* cup ) { hash_table_t event_table; save_info_t *this_save; difficulty_level_t level; int i; if ( !get_hash_entry( progress_save_table, player, (hash_entry_t*)&event_table ) ) { event_table = create_hash_table(); add_hash_entry( progress_save_table, player, event_table ); } if ( !get_hash_entry( event_table, event, (hash_entry_t*)&this_save ) ) { this_save = (save_info_t*)malloc(sizeof(save_info_t)* DIFFICULTY_NUM_LEVELS); memset( this_save, 0, sizeof(save_info_t) * DIFFICULTY_NUM_LEVELS ); for( i=0; i<DIFFICULTY_NUM_LEVELS; i++ ) { level = (difficulty_level_t)i; strcpy( this_save[level].data.event.event, event ); this_save[level].data.event.difficulty = d; if ( level != d ) { this_save[level].data_type = INVALID_DATA; } else { this_save[level].data_type = EVENT_INFO; } } add_hash_entry( event_table, event, this_save ); } this_save[d].data_type = EVENT_INFO; strcpy( this_save[d].data.event.cup, cup ); return True; }
static int Zoltan_LB( ZZ *zz, int include_parts, /* Flag indicating whether to generate part informtion; 0 if called by Zoltan_LB_Balance, 1 if called by Zoltan_LB_Partition. */ int *changes, /* Set to zero or one depending on if Zoltan determines a new decomposition or not: zero - No changes to the decomposition were made by the load-balancing algorithm; migration is not needed. one - A new decomposition is suggested by the load-balancer; migration is needed to establish the new decomposition. */ int *num_gid_entries, /* The number of array entries in a global ID; set to be the max over all processors in zz->Communicator of the parameter Num_Global_ID_Entries. */ int *num_lid_entries, /* The number of array entries in a local ID; set to be the max over all processors in zz->Communicator of the parameter Num_Local_ID_Entries. */ int *num_import_objs, /* The number of non-local objects in the processor's new decomposition. */ ZOLTAN_ID_PTR *import_global_ids,/* Array of global IDs for non-local objects (i.e., objs to be imported) in the processor's new decomposition. */ ZOLTAN_ID_PTR *import_local_ids, /* Array of local IDs for non-local objects (i.e., objs to be imported) in the processor's new decomposition. */ int **import_procs, /* Array of processor IDs for processors currently owning non-local objects (i.e., objs to be imported) in this processor's new decomposition. */ int **import_to_part, /* Partition to which the objects should be imported. */ int *num_export_objs, /* The number of local objects that need to be exported from the processor to establish the new decomposition. */ ZOLTAN_ID_PTR *export_global_ids,/* Array of global IDs for objects that need to be exported (assigned and sent to other processors) to establish the new decomposition. */ ZOLTAN_ID_PTR *export_local_ids, /* Array of local IDs for objects that need to be exported (assigned and sent to other processors) to establish the new decomposition. */ int **export_procs, /* Array of destination processor IDs for objects that need to be exported to establish the new decomposition. */ int **export_to_part /* Partition to which objects should be exported. */ ) { /* * Main load-balancing routine. * Input: a Zoltan structure with appropriate function pointers set. * Output: * changes * num_import_objs * import_global_ids * import_local_ids * import_procs * import_to_part * num_export_objs * export_global_ids * export_local_ids * export_procs * export_to_part * Return values: * Zoltan error code. */ char *yo = "Zoltan_LB"; int gmax; /* Maximum number of imported/exported objects over all processors. */ int error = ZOLTAN_OK; /* Error code */ double start_time, end_time; double lb_time[2] = {0.0,0.0}; char msg[256]; int comm[3],gcomm[3]; float *part_sizes = NULL, *fdummy = NULL; int wgt_dim, part_dim; int all_num_obj, i, ts, idIdx; struct Hash_Node **ht; int *export_all_procs, *export_all_to_part, *parts=NULL; ZOLTAN_ID_PTR all_global_ids=NULL, all_local_ids=NULL; ZOLTAN_ID_PTR gid; #ifdef ZOLTAN_OVIS struct OVIS_parameters ovisParameters; #endif ZOLTAN_TRACE_ENTER(zz, yo); if (zz->Proc == zz->Debug_Proc && zz->Debug_Level >= ZOLTAN_DEBUG_PARAMS){ printf("Build configuration:\n"); Zoltan_Print_Configuration(" "); printf("\n"); Zoltan_Print_Key_Params(zz); } start_time = Zoltan_Time(zz->Timer); #ifdef ZOLTAN_OVIS Zoltan_OVIS_Setup(zz, &ovisParameters); if (zz->Proc == 0) printf("OVIS PARAMETERS %s %s %d %f\n", ovisParameters.hello, ovisParameters.dll, ovisParameters.outputLevel, ovisParameters.minVersion); ovis_enabled(zz->Proc, ovisParameters.dll); #endif /* * Compute Max number of array entries per ID over all processors. * Compute Max number of return arguments for Zoltan_LB_Balance. * This is a sanity-maintaining step; we don't want different * processors to have different values for these numbers. */ comm[0] = zz->Num_GID; comm[1] = zz->Num_LID; comm[2] = zz->LB.Return_Lists; MPI_Allreduce(comm, gcomm, 3, MPI_INT, MPI_MAX, zz->Communicator); zz->Num_GID = *num_gid_entries = gcomm[0]; zz->Num_LID = *num_lid_entries = gcomm[1]; zz->LB.Return_Lists = gcomm[2]; /* assume no changes */ *changes = 0; *num_import_objs = *num_export_objs = 0; *import_global_ids = NULL; *import_local_ids = NULL; *import_procs = NULL; *import_to_part = NULL; *export_global_ids = NULL; *export_local_ids = NULL; *export_procs = NULL; *export_to_part = NULL; /* * Return if this processor is not in the Zoltan structure's * communicator. */ if (ZOLTAN_PROC_NOT_IN_COMMUNICATOR(zz)) goto End; if (zz->LB.Method == NONE) { if (zz->Proc == zz->Debug_Proc && zz->Debug_Level >= ZOLTAN_DEBUG_PARAMS) printf("%s Balancing method selected == NONE; no balancing performed\n", yo); error = ZOLTAN_WARN; goto End; } /* * Sync the random number generator across processors. */ Zoltan_Srand_Sync(Zoltan_Rand(NULL), NULL, zz->Communicator); /* Since generating a new partition, need to free old mapping vector */ zz->LB.OldRemap = zz->LB.Remap; zz->LB.Remap = NULL; error = Zoltan_LB_Build_PartDist(zz); if (error != ZOLTAN_OK && error != ZOLTAN_WARN) goto End; if (zz->Debug_Level >= ZOLTAN_DEBUG_ALL) { int i, np, fp; for (i = 0; i < zz->Num_Proc; i++) { Zoltan_LB_Proc_To_Part(zz, i, &np, &fp); printf("%d Proc_To_Part Proc %d NParts %d FPart %d\n", zz->Proc, i, np, fp); } } /* * Generate parts sizes. */ #ifdef ZOLTAN_OVIS /* set part sizes computed by OVIS, if requested. Processes set only their own value */ { float part_sizes[1]; int part_ids[1], wgt_idx[1]; wgt_idx[0] = 0; part_ids[0] = 0; ovis_getPartsize(&(part_sizes[0])); printf("Rank %d ps %f\n",zz->Proc, part_sizes[0]); /* clear out old part size info first */ Zoltan_LB_Set_Part_Sizes(zz, 0, -1, NULL, NULL, NULL); Zoltan_LB_Set_Part_Sizes(zz, 0, 1, part_ids, wgt_idx, part_sizes); } #endif wgt_dim = zz->Obj_Weight_Dim; part_dim = ((wgt_dim > 0) ? wgt_dim : 1); part_sizes = (float *) ZOLTAN_MALLOC(sizeof(float) * part_dim * zz->LB.Num_Global_Parts); if (part_sizes == NULL) { ZOLTAN_PRINT_ERROR(zz->Proc, yo, "Memory error."); error = ZOLTAN_MEMERR; goto End; } /* Get part sizes. */ Zoltan_LB_Get_Part_Sizes(zz, zz->LB.Num_Global_Parts, part_dim, part_sizes); #ifdef ZOLTAN_OVIS /* if (ovisParameters.outputlevel > 3) */ { int myRank = zz->Proc; if (myRank == 0){ int i, j; for (i = 0; i < zz->LB.Num_Global_Parts; i++){ for (j = 0; j < part_dim; j++){ printf("Rank %d AG: part_sizes[%d] = %f (Num_Global_Parts = %d, part_dim = %d)\n",zz->Proc, (i*part_dim+j), part_sizes[i*part_dim+j],zz->LB.Num_Global_Parts, part_dim); } } } } #endif /* * Call the actual load-balancing function. */ error = zz->LB.LB_Fn(zz, part_sizes, num_import_objs, import_global_ids, import_local_ids, import_procs, import_to_part, num_export_objs, export_global_ids, export_local_ids, export_procs, export_to_part); ZOLTAN_FREE(&part_sizes); if (error == ZOLTAN_FATAL || error == ZOLTAN_MEMERR){ sprintf(msg, "Partitioning routine returned code %d.", error); #ifdef HOST_LINUX if ((error == ZOLTAN_MEMERR) && (Zoltan_Memory_Get_Debug() > 0)){ Zoltan_write_linux_meminfo(0, "State of /proc/meminfo after malloc failure\n", 0); } #endif ZOLTAN_PRINT_ERROR(zz->Proc, yo, msg); goto End; } else if (error){ if (zz->Debug_Level >ZOLTAN_DEBUG_NONE) { sprintf(msg, "Partitioning routine returned code %d.", error); ZOLTAN_PRINT_WARN(zz->Proc, yo, msg); } } ZOLTAN_TRACE_DETAIL(zz, yo, "Done partitioning"); if (*num_import_objs >= 0) MPI_Allreduce(num_import_objs, &gmax, 1, MPI_INT, MPI_MAX, zz->Communicator); else /* use export data */ MPI_Allreduce(num_export_objs, &gmax, 1, MPI_INT, MPI_MAX, zz->Communicator); if (gmax == 0) { /* * Decomposition was not changed by the load balancing; no migration * is needed. */ if (zz->Proc == zz->Debug_Proc && zz->Debug_Level >= ZOLTAN_DEBUG_PARAMS) printf("%s No changes to the decomposition due to partitioning; " "no migration is needed.\n", yo); /* * Reset num_import_objs and num_export_objs; don't want to return * -1 for the arrays that weren't returned by ZOLTAN_LB_FN. */ *num_import_objs = *num_export_objs = 0; if (zz->LB.Return_Lists == ZOLTAN_LB_COMPLETE_EXPORT_LISTS){ /* * This parameter setting requires that all local objects * and their assignments appear in the export list. */ error= Zoltan_Get_Obj_List_Special_Malloc(zz, num_export_objs, export_global_ids, export_local_ids, wgt_dim, &fdummy, export_to_part); if (error == ZOLTAN_OK){ ZOLTAN_FREE(&fdummy); if (Zoltan_Special_Malloc(zz, (void **)export_procs, *num_export_objs, ZOLTAN_SPECIAL_MALLOC_INT)){ for (i=0; i<*num_export_objs; i++) (*export_procs)[i] = zz->Proc; } else{ error = ZOLTAN_MEMERR; } } } goto End; } /* * Check whether we know the import data, export data, or both. * * If we were given the import data, * we know what the new decomposition should look like on the * processor, but we don't know which of our local objects we have * to export to other processors to establish the new decomposition. * Reverse the argument if we were given the export data. * * Unless we were given both maps, compute the inverse map. */ if (zz->LB.Return_Lists == ZOLTAN_LB_NO_LISTS) { if (*num_import_objs >= 0) Zoltan_LB_Special_Free_Part(zz, import_global_ids, import_local_ids, import_procs, import_to_part); if (*num_export_objs >= 0) Zoltan_LB_Special_Free_Part(zz, export_global_ids, export_local_ids, export_procs, export_to_part); *num_import_objs = *num_export_objs = -1; } if (*num_import_objs >= 0){ if (*num_export_objs >= 0) { /* Both maps already available; nothing to do. */; } else if (zz->LB.Return_Lists == ZOLTAN_LB_ALL_LISTS || zz->LB.Return_Lists == ZOLTAN_LB_EXPORT_LISTS || zz->LB.Return_Lists == ZOLTAN_LB_COMPLETE_EXPORT_LISTS) { /* Export lists are requested; compute export map */ error = Zoltan_Invert_Lists(zz, *num_import_objs, *import_global_ids, *import_local_ids, *import_procs, *import_to_part, num_export_objs, export_global_ids, export_local_ids, export_procs, export_to_part); if (error != ZOLTAN_OK && error != ZOLTAN_WARN) { sprintf(msg, "Error building return arguments; " "%d returned by Zoltan_Compute_Destinations\n", error); ZOLTAN_PRINT_ERROR(zz->Proc, yo, msg); goto End; } if (zz->LB.Return_Lists == ZOLTAN_LB_EXPORT_LISTS || zz->LB.Return_Lists == ZOLTAN_LB_COMPLETE_EXPORT_LISTS) { /* Method returned import lists, but only export lists were desired. */ /* Import lists not needed; free them. */ *num_import_objs = -1; Zoltan_LB_Special_Free_Part(zz, import_global_ids, import_local_ids, import_procs, import_to_part); } } } else { /* (*num_import_objs < 0) */ if (*num_export_objs >= 0) { /* Only export lists have been returned. */ if (zz->LB.Return_Lists == ZOLTAN_LB_ALL_LISTS || zz->LB.Return_Lists == ZOLTAN_LB_IMPORT_LISTS) { /* Compute import map */ error = Zoltan_Invert_Lists(zz, *num_export_objs, *export_global_ids, *export_local_ids, *export_procs, *export_to_part, num_import_objs, import_global_ids, import_local_ids, import_procs, import_to_part); if (error != ZOLTAN_OK && error != ZOLTAN_WARN) { sprintf(msg, "Error building return arguments; " "%d returned by Zoltan_Compute_Destinations\n", error); ZOLTAN_PRINT_ERROR(zz->Proc, yo, msg); goto End; } if (zz->LB.Return_Lists == ZOLTAN_LB_IMPORT_LISTS) { /* Method returned export lists, but only import lists are desired. */ /* Export lists not needed; free them. */ *num_export_objs = -1; Zoltan_LB_Special_Free_Part(zz, export_global_ids, export_local_ids, export_procs, export_to_part); } } } else { /* *num_export_objs < 0 && *num_import_objs < 0) */ if (zz->LB.Return_Lists) { /* No map at all available */ ZOLTAN_PRINT_ERROR(zz->Proc, yo, "Load-balancing function returned " "neither import nor export data."); error = ZOLTAN_WARN; goto End; } } } if (zz->LB.Return_Lists == ZOLTAN_LB_COMPLETE_EXPORT_LISTS) { /* * Normally, Zoltan_LB returns in the export lists all local * objects that are moving off processor, or that are assigned * to a part on the local processor that is not the * default part. This setting of Return_Lists requests * that all local objects be included in the export list. */ if (*num_export_objs == 0){ /* all local objects are remaining on processor */ error= Zoltan_Get_Obj_List_Special_Malloc(zz, num_export_objs, export_global_ids, export_local_ids, wgt_dim, &fdummy, export_to_part); if (error == ZOLTAN_OK){ ZOLTAN_FREE(&fdummy); if (*num_export_objs) { if (Zoltan_Special_Malloc(zz, (void **)export_procs, *num_export_objs, ZOLTAN_SPECIAL_MALLOC_INT)){ for (i=0; i<*num_export_objs; i++) (*export_procs)[i] = zz->Proc; } else{ error = ZOLTAN_MEMERR; } } } if ((error != ZOLTAN_OK) && (error != ZOLTAN_WARN)) goto End; } else{ all_num_obj = zz->Get_Num_Obj(zz->Get_Num_Obj_Data, &error); if (*num_export_objs < all_num_obj){ /* Create a lookup table for exported IDs */ ts = Zoltan_Recommended_Hash_Size(*num_export_objs); ht = create_hash_table(zz, *export_global_ids, *num_export_objs, ts); /* Create a list of all gids, lids and parts */ error= Zoltan_Get_Obj_List_Special_Malloc(zz, &all_num_obj, &all_global_ids, &all_local_ids, wgt_dim, &fdummy, &parts); if ((error == ZOLTAN_OK) || (error == ZOLTAN_WARN)){ ZOLTAN_FREE(&fdummy); if ((Zoltan_Special_Malloc(zz, (void **)(void*)&export_all_procs, all_num_obj, ZOLTAN_SPECIAL_MALLOC_INT)==0) || (Zoltan_Special_Malloc(zz, (void **)(void*)&export_all_to_part, all_num_obj, ZOLTAN_SPECIAL_MALLOC_INT)==0)){ error = ZOLTAN_MEMERR; } } if ((error != ZOLTAN_OK) && (error != ZOLTAN_WARN)){ sprintf(msg, "Error building complete export list; " "%d returned by Zoltan_Get_Obj_List\n", error); ZOLTAN_PRINT_ERROR(zz->Proc, yo, msg); goto End; } gid = all_global_ids; for (i=0; i < all_num_obj; i++, gid += zz->Num_GID){ idIdx = search_hash_table(zz, gid, ht, ts); if (idIdx >= 0){ export_all_procs[i] = (*export_procs)[idIdx]; export_all_to_part[i] = (*export_to_part)[idIdx]; } else{ export_all_procs[i] = zz->Proc; export_all_to_part[i] = parts[i]; } } free_hash_table(ht, ts); Zoltan_LB_Special_Free_Part(zz, export_global_ids, export_local_ids, export_procs, export_to_part); Zoltan_Special_Free(zz, (void **)(void*)&parts, ZOLTAN_SPECIAL_MALLOC_INT); *export_global_ids = all_global_ids; *export_local_ids = all_local_ids; *export_procs = export_all_procs; *export_to_part = export_all_to_part; *num_export_objs = all_num_obj; } } } ZOLTAN_TRACE_DETAIL(zz, yo, "Done building return arguments"); end_time = Zoltan_Time(zz->Timer); lb_time[0] = end_time - start_time; if (zz->Debug_Level >= ZOLTAN_DEBUG_LIST) { int i; Zoltan_Print_Sync_Start(zz->Communicator, TRUE); printf("ZOLTAN: Objects to be imported to Proc %d\n", zz->Proc); for (i = 0; i < *num_import_objs; i++) { printf(" Obj: "); ZOLTAN_PRINT_GID(zz, &((*import_global_ids)[i*zz->Num_GID])); printf(" To part: %4d", (*import_to_part != NULL ? (*import_to_part)[i] : zz->Proc)); printf(" From processor: %4d\n", (*import_procs)[i]); } printf("\n"); printf("ZOLTAN: Objects to be exported from Proc %d\n", zz->Proc); for (i = 0; i < *num_export_objs; i++) { printf(" Obj: "); ZOLTAN_PRINT_GID(zz, &((*export_global_ids)[i*zz->Num_GID])); printf(" To part: %4d", (*export_to_part != NULL ? (*export_to_part)[i] : (*export_procs)[i])); printf(" To processor: %4d\n", (*export_procs)[i]); } Zoltan_Print_Sync_End(zz->Communicator, TRUE); } /* * If the Help_Migrate flag is set, perform migration for the application. */ if (zz->Migrate.Auto_Migrate) { ZOLTAN_TRACE_DETAIL(zz, yo, "Begin auto-migration"); start_time = Zoltan_Time(zz->Timer); error = Zoltan_Migrate(zz, *num_import_objs, *import_global_ids, *import_local_ids, *import_procs, *import_to_part, *num_export_objs, *export_global_ids, *export_local_ids, *export_procs, *export_to_part); if (error != ZOLTAN_OK && error != ZOLTAN_WARN) { sprintf(msg, "Error in auto-migration; %d returned from " "Zoltan_Help_Migrate\n", error); ZOLTAN_PRINT_ERROR(zz->Proc, yo, msg); goto End; } end_time = Zoltan_Time(zz->Timer); lb_time[1] = end_time - start_time; ZOLTAN_TRACE_DETAIL(zz, yo, "Done auto-migration"); } /* Print timing info */ if (zz->Debug_Level >= ZOLTAN_DEBUG_ZTIME) { if (zz->Proc == zz->Debug_Proc) { printf("ZOLTAN Times: \n"); } Zoltan_Print_Stats (zz->Communicator, zz->Debug_Proc, lb_time[0], "ZOLTAN Partition: "); if (zz->Migrate.Auto_Migrate) Zoltan_Print_Stats (zz->Communicator, zz->Debug_Proc, lb_time[1], "ZOLTAN Migrate: "); } *changes = 1; End: ZOLTAN_TRACE_EXIT(zz, yo); return (error); }
/*! Initializes the audio module. \author jfpatry \date Created: 2000-08-13 \date Modified: 2000-08-13 */ void init_audio() { int hz, channels, buffer; Uint16 format; check_assertion( !initialized_, "init_audio called twice" ); sound_contexts_ = create_hash_table(); music_contexts_ = create_hash_table(); initialized_ = True; /* * Init SDL Audio */ if ( getparam_no_audio() == False ) { if ( SDL_Init( SDL_INIT_AUDIO ) < 0 ) { handle_error( 1, "Couldn't initialize SDL: %s", SDL_GetError() ); } /* Open the audio device */ switch (getparam_audio_freq_mode()) { case 0: hz = 11025; break; case 1: hz = 22050; break; case 2: hz = 44100; break; default: hz = 22050; setparam_audio_freq_mode(1); } switch ( getparam_audio_format_mode() ) { case 0: format = AUDIO_U8; break; case 1: format = AUDIO_S16SYS; break; default: format = AUDIO_S16SYS; setparam_audio_format_mode( 1 ); } if ( getparam_audio_stereo() ) { channels = 2; } else { channels = 1; } buffer = getparam_audio_buffer_size(); if ( Mix_OpenAudio(hz, format, channels, buffer) < 0 ) { print_warning( 1, "Warning: Couldn't set %d Hz %d-bit audio\n" " Reason: %s\n", hz, getparam_audio_format_mode() == 0 ? 8 : 16, SDL_GetError()); } else { print_debug( DEBUG_SOUND, "Opened audio device at %d Hz %d-bit audio", hz, getparam_audio_format_mode() == 0 ? 8 : 16 ); } } }
void hash_insert(hash_table *t, char *key, void *val) { int idx = hash(t, key); node *n = t->table[idx]; node *prev = NULL; int cols=0; while( n!=NULL && n->key != NULL && strcmp(key, n->key) > 0 ) { prev = n; n = n->next; cols++; } if( n!=NULL && n->key != NULL && strcmp(key, n->key)==0 ) { free(n->value); n->value = val; } else { numElems++; if(cols>0) numCollisions++; node *newnode = malloc(sizeof(node)); newnode->key = strdup(key); newnode->value = val; newnode->next = NULL; if(n==t->table[idx]) { newnode->next = n; t->table[idx]=newnode; } else if(n==NULL) { prev->next = newnode; } else { newnode->next = n; prev->next = newnode; } } // RESIZING OF HASH TABLE IF MORE COLLISIONS START HAPPENING double ratio = ((double)numCollisions)/((double)numElems); if( ratio > 1.5 ) { int curSize = t->size; hash_table * new_table = create_hash_table(curSize*3); int i; struct node_* head = NULL; for(i=0;i<t->size;i++) { head = (struct node_ *)t->table[i]; while(head!=NULL) { void *ret = hash_get(t, head->key); // inserting elements into new bigger hash table basic_hash_insert(new_table,head->key, *((int *)ret)); head=head->next; } } t = new_table; // pointing current table pointer to new bigger table } }
//hash table struct wc * wc_init(char *word_array, long size) { // ht== my_hash_table struct wc *ht; ht = create_hash_table(); //initialize the elements of the table int i; for (i=0; i<MAX; i++) { ht->table[i]=NULL; } struct sc *current, *temp; char *p = word_array; long lo=0; char temp_A[70]; char *temp_B; long hv; //hash valu //ptr = (struct wc *)malloc(sizeof(struct wc)); //assert(wc); while(lo<size) { i=0; while (isspace(*p)) //loop until non-space is reached. { p++; lo++; } while (!(isspace(*p)) && (lo<size)) //extract the word and construct data structure { temp_A[i]=*p; i++; p++; lo++; } temp_A[i+1]='\0'; //temp_B = malloc(sizeof(char)+strlen(temp_A)); temp_B = malloc(strlen(temp_A)+sizeof(NULL)); strcpy(temp_B,temp_A); //temp_B[sizeof(temp_A)]='\0'; memset (temp_A,0,sizeof temp_A); //reset a for next word use. hv = hash(temp_B); // put word in a data structure if (ht->table[hv]!=NULL) //if struct is not null, something exists already. { //current = ptr[hv]; current = ht->table[hv]; if (strcmp(temp_B,current->str)==0) { current->count = current->count + 1; } else { int xyz = 1; while (current != NULL) { if (strcmp(current->str,temp_B)==0) { current->count = current->count + 1; xyz = 0; } else if ((current->next == NULL)&&(xyz==1)) { temp=(struct sc*)malloc(sizeof(struct sc)); if (temp == NULL) return 0; temp->str=(char*)malloc(strlen(temp_B)+sizeof(NULL)); strcpy(temp->str,temp_B); temp->count=1; temp->next= NULL; current->next=temp; current=current->next; } current=current->next; } } } else if(ht->table[hv] == NULL) //if struct is NULL, create the first node and store data { ht->table[hv]=(struct sc *)malloc(sizeof(struct sc)); if (ht->table[hv]==NULL) return 0; current= ht->table[hv]; current->str=(char*)malloc(strlen(temp_B)+sizeof(NULL)); strcpy(current->str,temp_B); current->count=1; current->next=NULL; } free(temp_B); p++; lo++; } return ht; }