void * guestfs_get_private (guestfs_h *g, const char *key) { if (g->pda == NULL) return NULL; /* no keys have been set */ const struct pda_entry k = { .key = (char *) key }; struct pda_entry *entry = hash_lookup (g->pda, &k); if (entry) return entry->data; else return NULL; } /* Iterator. */ void * guestfs_first_private (guestfs_h *g, const char **key_rtn) { if (g->pda == NULL) return NULL; g->pda_next = hash_get_first (g->pda); /* Ignore any keys with NULL data pointers. */ while (g->pda_next && g->pda_next->data == NULL) g->pda_next = hash_get_next (g->pda, g->pda_next); if (g->pda_next == NULL) return NULL; *key_rtn = g->pda_next->key; return g->pda_next->data; } void * guestfs_next_private (guestfs_h *g, const char **key_rtn) { if (g->pda == NULL) return NULL; if (g->pda_next == NULL) return NULL; /* Walk to the next key with a non-NULL data pointer. */ do { g->pda_next = hash_get_next (g->pda, g->pda_next); } while (g->pda_next && g->pda_next->data == NULL); if (g->pda_next == NULL) return NULL; *key_rtn = g->pda_next->key; return g->pda_next->data; }
void update_selectables(){ int pos,i; hash_entry *he; i=0; pos=vscrollbar_get_pos(emotes_win, EMOTES_SCROLLBAR_ITEMS); memset(selectables,0,sizeof(emote_data*)*EMOTES_SHOWN); hash_start_iterator(emotes); while((he=hash_get_next(emotes))&&i<EMOTES_SHOWN){ emote_data *emote; emote=((emote_data *)he->item); if(!cur_cat&&emote->pose>EMOTE_STANDING) { //emotes pos--; if(pos>=0) continue; selectables[i]=emote; i++; } else if(cur_cat&&emote->pose==(cur_cat-1)){ //poses pos--; if(pos>=0) continue; selectables[i]=emote; i++; } } emote_str1[1]=emote_str2[0]=emote_str2[1]=0; if(emote_sel[cur_cat]){ emote_dict *emd; emote_str1[0]=127+c_orange2; safe_strcat((char*)emote_str1,(char*)emote_sel[cur_cat]->desc,/*sizeof(emote_str1)*/23); hash_start_iterator(emote_cmds); while((he=hash_get_next(emote_cmds))){ emd = (emote_dict*)he->item; if (emd->emote==emote_sel[cur_cat]){ int ll; //draw command if(!emote_str2[0]) { emote_str2[0]=127+c_grey1; safe_strcat((char*)emote_str2,"Trigger:",10); } ll=strlen((char*)emote_str2); emote_str2[ll]=127+c_green3; emote_str2[ll+1]=emote_str2[ll+2]=' '; emote_str2[ll+3]=0; safe_strcat((char*)emote_str2,emd->command,/*sizeof(emote_str2)*/23); break; //just one command } } } }
void save_server_markings(){ char fname[128]; FILE *fp; server_mark *sm; hash_entry *he; if(!server_marks) return; //open server markings file safe_snprintf(fname, sizeof(fname), "servermarks_%s.dat",get_lowercase_username()); fp = open_file_config(fname,"w"); if(fp == NULL){ LOG_ERROR("%s: %s \"%s\": %s\n", reg_error_str, cant_open_file, fname, strerror(errno)); return; } hash_start_iterator(server_marks); while((he=hash_get_next(server_marks))){ sm = (server_mark *) he->item; fprintf(fp,"%d %d %d %s %s\n",sm->id, sm->x, sm->y, sm->map_name, sm->text); } fclose (fp); LOG_DEBUG("Wrote server markings to file '%s'", fname); }
void add_server_markers(){ hash_entry *he; server_mark *sm; int i,l; char *mapname = map_file_name; //find the slot to add server marks for(i=0;i<max_mark;i++) if(marks[i].server_side) break; l=i; if(!server_marks) init_server_markers(); if(server_marks) { hash_start_iterator(server_marks); while((he=hash_get_next(server_marks))){ sm = (server_mark *) he->item; //is it in this map? if(strcmp(mapname,sm->map_name)) continue; //find the next slot. If not there, add 1 for(i=l;i<MAX_MARKINGS;i++) if(marks[i].server_side||i>=max_mark) {l=i; if(l>=max_mark) max_mark=l+1; break;} //add the marker marks[l].x=sm->x; marks[l].y=sm->y; marks[l].server_side=1; marks[l].server_side_id=sm->id; safe_strncpy(marks[l].text, sm->text, sizeof(marks[l].text)); l++; } //remove server side markings if necessary for(i=l+1;i<max_mark;i++) if(marks[i].server_side) {marks[i].server_side=0;marks[i].server_side_id=marks[i].x=marks[i].y=-1;} } }
int print_emotes(char *text, int len){ hash_entry *he;; LOG_TO_CONSOLE(c_orange1,"EMOTES"); LOG_TO_CONSOLE(c_orange1,"--------------------"); hash_start_iterator(emote_cmds); while((he=hash_get_next(emote_cmds))) LOG_TO_CONSOLE(c_orange1,((emote_dict *)he->item)->command); return 1; }
/** ** Uses the above routine to print the hashtable ** It takes a pointer to a function as argument to print values **/ void hash_print_next( struct node ** hashtable, char * (*vprint)(void *) ) { struct node * p = *hashtable; while ( (p = hash_get_next( hashtable, p )) ) { fprintf( stdout, "Hashtable[%ld]: key=%s value=%s\n", get_hash( p->key ), p->key, (*vprint)( p->value )); } }
/* Iterator. */ void * guestfs_first_private (guestfs_h *g, const char **key_rtn) { if (g->pda == NULL) return NULL; g->pda_next = hash_get_first (g->pda); /* Ignore any keys with NULL data pointers. */ while (g->pda_next && g->pda_next->data == NULL) g->pda_next = hash_get_next (g->pda, g->pda_next); if (g->pda_next == NULL) return NULL; *key_rtn = g->pda_next->key; return g->pda_next->data; }
void * guestfs_next_private (guestfs_h *g, const char **key_rtn) { if (g->pda == NULL) return NULL; if (g->pda_next == NULL) return NULL; /* Walk to the next key with a non-NULL data pointer. */ do { g->pda_next = hash_get_next (g->pda, g->pda_next); } while (g->pda_next && g->pda_next->data == NULL); if (g->pda_next == NULL) return NULL; *key_rtn = g->pda_next->key; return g->pda_next->data; }
int main (int argc, char **argv) { unsigned int i; unsigned int k; unsigned int table_size[] = {1, 2, 3, 4, 5, 23, 53}; Hash_table *ht; Hash_tuning tuning; hash_reset_tuning (&tuning); tuning.shrink_threshold = 0.3; tuning.shrink_factor = 0.707; tuning.growth_threshold = 1.5; tuning.growth_factor = 2.0; tuning.is_n_buckets = true; if (1 < argc) { unsigned int seed; if (get_seed (argv[1], &seed) != 0) { fprintf (stderr, "invalid seed: %s\n", argv[1]); exit (EXIT_FAILURE); } srand (seed); } for (i = 0; i < ARRAY_CARDINALITY (table_size); i++) { size_t sz = table_size[i]; ht = hash_initialize (sz, NULL, hash_pjw, hash_compare_strings, NULL); ASSERT (ht); insert_new (ht, "a"); { char *str1 = strdup ("a"); char *str2; ASSERT (str1); str2 = hash_insert (ht, str1); ASSERT (str1 != str2); ASSERT (STREQ (str1, str2)); free (str1); } insert_new (ht, "b"); insert_new (ht, "c"); i = 0; ASSERT (hash_do_for_each (ht, walk, &i) == 3); ASSERT (i == 7); { void *buf[5] = { NULL }; ASSERT (hash_get_entries (ht, NULL, 0) == 0); ASSERT (hash_get_entries (ht, buf, 5) == 3); ASSERT (STREQ (buf[0], "a") || STREQ (buf[0], "b") || STREQ (buf[0], "c")); } ASSERT (hash_delete (ht, "a")); ASSERT (hash_delete (ht, "a") == NULL); ASSERT (hash_delete (ht, "b")); ASSERT (hash_delete (ht, "c")); ASSERT (hash_rehash (ht, 47)); ASSERT (hash_rehash (ht, 467)); /* Free an empty table. */ hash_clear (ht); hash_free (ht); ht = hash_initialize (sz, NULL, hash_pjw, hash_compare_strings, NULL); ASSERT (ht); insert_new (ht, "z"); insert_new (ht, "y"); insert_new (ht, "x"); insert_new (ht, "w"); insert_new (ht, "v"); insert_new (ht, "u"); hash_clear (ht); ASSERT (hash_get_n_entries (ht) == 0); hash_free (ht); /* Test pointer hashing. */ ht = hash_initialize (sz, NULL, NULL, NULL, NULL); ASSERT (ht); { char *str = strdup ("a"); ASSERT (str); insert_new (ht, "a"); insert_new (ht, str); ASSERT (hash_lookup (ht, str) == str); free (str); } hash_free (ht); } hash_reset_tuning (&tuning); tuning.shrink_threshold = 0.3; tuning.shrink_factor = 0.707; tuning.growth_threshold = 1.5; tuning.growth_factor = 2.0; tuning.is_n_buckets = true; /* Invalid tuning. */ ht = hash_initialize (4651, &tuning, hash_pjw, hash_compare_strings, hash_freer); ASSERT (!ht); /* Alternate tuning. */ tuning.growth_threshold = 0.89; /* Run with default tuning, then with custom tuning settings. */ for (k = 0; k < 2; k++) { Hash_tuning const *tune = (k == 0 ? NULL : &tuning); /* Now, each entry is malloc'd. */ ht = hash_initialize (4651, tune, hash_pjw, hash_compare_strings, hash_freer); ASSERT (ht); for (i = 0; i < 10000; i++) { unsigned int op = rand () % 10; switch (op) { case 0: case 1: case 2: case 3: case 4: case 5: { char buf[50]; char const *p = uinttostr (i, buf); char *p_dup = strdup (p); ASSERT (p_dup); insert_new (ht, p_dup); } break; case 6: { size_t n = hash_get_n_entries (ht); ASSERT (hash_rehash (ht, n + rand () % 20)); } break; case 7: { size_t n = hash_get_n_entries (ht); size_t delta = rand () % 20; if (delta < n) ASSERT (hash_rehash (ht, n - delta)); } break; case 8: case 9: { /* Delete a random entry. */ size_t n = hash_get_n_entries (ht); if (n) { size_t kk = rand () % n; void const *p; void *v; for (p = hash_get_first (ht); kk; --kk, p = hash_get_next (ht, p)) { /* empty */ } ASSERT (p); v = hash_delete (ht, p); ASSERT (v); free (v); } break; } } ASSERT (hash_table_ok (ht)); } hash_free (ht); } return 0; }