rs_result rs_build_hash_table(rs_signature_t *sig) { rs_block_match_t m; rs_block_sig_t *b; int i; rs_signature_check(sig); sig->hashtable = hashtable_new(sig->count); if (!sig->hashtable) return RS_MEM_ERROR; for (i = 0; i < sig->count; i++) { b = rs_block_sig_ptr(sig, i); rs_block_match_init(&m, sig, b->weak_sum, &b->strong_sum, NULL, 0); if (!hashtable_find(sig->hashtable, &m)) hashtable_add(sig->hashtable, b); } hashtable_stats_init(sig->hashtable); return RS_DONE; }
static HashTable create_pixel_hash(Pixel *pixelData,unsigned long nPixels) { PixelHashData *d; HashTable *hash; unsigned long i; unsigned long timer,timer2,timer3; d=malloc(sizeof(PixelHashData)); if (!d) return NULL; hash=hashtable_new(pixel_hash,pixel_cmp); hashtable_set_user_data(hash,d); d->scale=0; timer=timer3=clock(); for (i=0;i<nPixels;i++) { if (!hashtable_insert_or_update_computed(hash, (void *)pixelData[i].v, new_count_func, exists_count_func)) {; } while (hashtable_get_count(hash)>MAX_HASH_ENTRIES) { d->scale++; #ifndef NO_OUTPUT printf ("rehashing - new scale: %d\n",(int)d->scale); #endif timer2=clock(); hashtable_rehash_compute(hash,rehash_collide); timer2=clock()-timer2; #ifndef NO_OUTPUT printf ("rehash took %f sec\n",timer2/(double)CLOCKS_PER_SEC); #endif timer+=timer2; } } #ifndef NO_OUTPUT printf ("inserts took %f sec\n",(clock()-timer)/(double)CLOCKS_PER_SEC); #endif #ifndef NO_OUTPUT printf ("total %f sec\n",(clock()-timer3)/(double)CLOCKS_PER_SEC); #endif return hash; }
void sst_open(sstable_t* sst,sst_status status) { //create sstable in type sst->htable = (hashtable_t*)hashtable_new(sst->id); if (status == READ) { hashtable_open(sst->htable); sst->status = READ; } else if (status == WRITE) { hashtable_build(sst->htable); sst->status = WRITE; } else if (status == COMPACT) //COMPACT sstable can not get... { hashtable_build(sst->htable); sst->status = COMPACT; hashtable_writehead(sst->htable); //why I write in this? because I want to keep a clear architecture } }
int main(){ hashtable_t * table = hashtable_new(5); int i = 0; for(;i<5;i++){ hashtable_add(table,"one"); hashtable_add(table,"two"); hashtable_add(table,"three"); hashtable_add(table,"four"); hashtable_add(table,"five"); } i=0;/* for(;i<5;i++){ hashtable_remove(table,"one"); hashtable_remove(table,"two"); hashtable_remove(table,"three"); hashtable_remove(table,"four"); hashtable_remove(table,"five"); } */ hashtable_print(table); hashtable_free(table); return 1; }
int main(int argc, char **argv) { hashtable ht = hashtable_new(8); int i = 1; while (i < argc) { char *str = argv[i++]; if (i < argc && strcmp(str, "-") == 0) { str = argv[i++]; hashtable_remove(ht, str); } else if (i < argc && strcmp(str, "+") == 0) { str = argv[i++]; hashtable_insert(ht, str); } else { hashtable_insert(ht, str); } } hashtable_print(ht); hashtable_free(ht); }
hashtable* hashtable_clone(hashtable* table, dict_key_datum_clone_func clone_func) { ASSERT(table); hashtable* clone = hashtable_new(table->cmp_func, table->hash_func, table->del_func, table->size); if (clone) { clone->count = table->count; for (unsigned slot = 0; slot < table->size; ++slot) { hash_node* prev = NULL; hash_node* node = table->table[slot]; for (; node; node = node->next) { hash_node* add = MALLOC(sizeof(*add)); if (!add) { hashtable_free(clone); return NULL; } add->key = node->key; add->datum = node->datum; if (clone_func) clone_func(&add->key, &add->datum); add->next = NULL; add->prev = prev; if (prev) prev->next = add; else clone->table[slot] = add; add->hash = node->hash; prev = add; } } } return clone; }
int main() { HashTable *c = hashtable_new(20); HashEnum b; ObjList *l = objlist_new(); char *g; ObjListEnum o; hashtable_insert(c, 1, "1pp"); hashtable_insert(c, 2, "2pp"); hashtable_insert(c, 3, "3pp"); hashtable_insert(c, 4, "4pp"); hashtable_insert(c, 6, "6pp"); hashtable_insert(c, 8, "8pp"); hashtable_insert(c, 9, "9pp"); b = hashenum_create(c); while ((g = hashenum_next(&b)) != NULL) printf("%s\n", g); hashtable_free(c); objlist_insert(l, "a"); objlist_insert(l, "b"); objlist_insert(l, "c"); objlist_insert(l, "d"); objlist_insert(l, "e"); o = objlistenum_create(l); while((g = objlistenum_next(&o)) != NULL) printf("%s\n", g); objlist_free(l); return 0; }
static void initializeStaticBondTable(void) { destroyStaticBondTable(); periodicHashtable = hashtable_new(64); bondStretchHashtable = hashtable_new(40); deHashtable = hashtable_new(10); bendDataHashtable = hashtable_new(40); vanDerWaalsHashtable = hashtable_new(40); electrostaticHashtable = hashtable_new(40); patternParameterHashtable = hashtable_new(40); // groups 9-22 are lanthanides // groups 8-31 are transition metals // // mass in yg (yoctograms, or 1e-24 g) // rvdW is in 1e-10 m or angstroms or 0.1 nm // evdW in zJ (zepto Joules, or milli atto Joules, or 1e-21 J) // an e_vanDerWaals value < 0 will be calculated in setElement() // rCovalent in pm (picoMeters, or 1e-12 m) // chrg in multiples of proton charge // NOTE: change MAX_VDW_RADIUS in part.[ch] if adding an atom larger than Si // // protons, group, period, symbol, name, mass, vanDerWaalsRadius, // e_vanDerWaals, n_bonds, covalentRadius, charge, isVirtual // // Z grp per par sym name mass rvdW evdW bnds rcov chrg virt // setElement(999, 0, 0, NULL, "All", "AllAtomTypes", 0.0, 0.0, 0.0, 0, 0, 0, 0); setElement(998, 0, 0, "All","Elt", "Elements", 0.0, 0.0, 0.0, 0, 0, 0, 0); setElement( 0, 1, 1, "Elt", "X", "Singlet", 17.000, 1.1, -1.00, 1, 0, 0, 0); setElement( 1, 1, 1, "Elt", "H", "Hydrogen", 1.6737, 1.5 , 0.382, 1, 30, 0, 0); setElement( 2, 0, 1, "Elt", "He", "Helium", 6.646, 1.4, -1.00, 0, 0, 0, 0); setElement( 3, 1, 2, "Elt", "Li", "Lithium", 11.525, 0.97, -1.00, 1, 152, 0, 0); setElement( 4, 2, 2, "Elt", "Be", "Beryllium", 14.964, 1.10, -1.00, 2, 114, 0, 0); setElement( 5, 3, 2, "Elt", "B", "Boron", 17.949, 1.99, -1.00, 3, 83, 0, 0); setElement( 6, 4, 2, "Elt", "C", "Carbon", 19.925, 1.94, 0.357, 4, 77, 0, 0); setElement( 7, 5, 2, "Elt", "N", "Nitrogen", 23.257, 1.82, 0.447, 3, 70, 0, 0); setElement( 8, 6, 2, "Elt", "O", "Oxygen", 26.565, 1.74, 0.406, 2, 66, 0, 0); setElement( 9, 7, 2, "Elt", "F", "Fluorine", 31.545, 1.65, 0.634, 1, 64, 0, 0); setElement(10, 0, 2, "Elt", "Ne", "Neon", 33.49, 1.82, -1.00, 0, 0, 0, 0); setElement(11, 1, 3, "Elt", "Na", "Sodium", 38.1726, 1.29, -1.00, 1, 186, 0, 0); setElement(12, 2, 3, "Elt", "Mg", "Magnesium", 40.356, 1.15, -1.00, 2, 160, 0, 0); setElement(13, 3, 3, "Elt", "Al", "Aluminum", 44.7997, 2.0, -1.00, 3, 125, 0, 0); setElement(14, 4, 3, "Elt", "Si", "Silicon", 46.6245, 2.25, 1.137, 4, 116, 0, 0); setElement(15, 5, 3, "Elt", "P", "Phosphorus", 51.429, 2.18, 1.365, 3, 110, 0, 0); setElement(16, 6, 3, "Elt", "S", "Sulfur", 53.233, 2.10, 1.641, 2, 104, 0, 0); setElement(17, 7, 3, "Elt", "Cl", "Chlorine", 58.867, 2.03, 1.950, 1, 99, 0, 0); setElement(18, 0, 3, "Elt", "Ar", "Argon", 62.33, 1.88, -1.00, 0, 0, 0, 0); setElement(19, 1, 4, "Elt", "K", "Potassium", 64.9256, 1.59, -1.00, 1, 231, 0, 0); setElement(20, 2, 4, "Elt", "Ca", "Calcium", 66.5495, 1.27, -1.00, 2, 197, 0, 0); setElement(21, 8, 4, "Elt", "Sc", "Scandium", 74.646, 2.0, -1.00, 0, 60, 0, 0); setElement(22, 23, 4, "Elt", "Ti", "Titanium", 79.534, 2.0, -1.00, 0, 147, 0, 0); setElement(23, 24, 4, "Elt", "V", "Vanadium", 84.584, 2.0, -1.00, 0, 132, 0, 0); setElement(24, 25, 4, "Elt", "Cr", "Chromium", 86.335, 2.0, -1.00, 0, 125, 0, 0); setElement(25, 26, 4, "Elt", "Mn", "Manganese", 91.22, 2.0, -1.00, 0, 112, 0, 0); setElement(26, 27, 4, "Elt", "Fe", "Iron", 92.729, 2.0, -1.00, 0, 124, 0, 0); setElement(27, 28, 4, "Elt", "Co", "Cobalt", 97.854, 2.0, -1.00, 0, 125, 0, 0); setElement(28, 29, 4, "Elt", "Ni", "Nickel", 97.483, 2.3, -1.00, 0, 125, 0, 0); setElement(29, 30, 4, "Elt", "Cu", "Copper", 105.513, 2.3, -1.00, 0, 128, 0, 0); setElement(30, 31, 4, "Elt", "Zn", "Zinc", 108.541, 2.3, -1.00, 0, 133, 0, 0); setElement(31, 3, 4, "Elt", "Ga", "Gallium", 115.764, 2.3, -1.00, 0, 135, 0, 0); setElement(32, 4, 4, "Elt", "Ge", "Germanium", 120.53, 2.0, -1.00, 4, 122, 0, 0); setElement(33, 5, 4, "Elt", "As", "Arsenic", 124.401, 2.0, -1.00, 3, 120, 0, 0); setElement(34, 6, 4, "Elt", "Se", "Selenium", 131.106, 1.88, -1.00, 2, 119, 0, 0); setElement(35, 7, 4, "Elt", "Br", "Bromine", 132.674, 1.83, -1.00, 1, 119, 0, 0); setElement(36, 0, 4, "Elt", "Kr", "Krypton", 134.429, 1.9, -1.00, 0, 0, 0, 0); setElement(51, 5, 5, "Elt", "Sb", "Antimony", 124.401, 2.2, -1.00, 3, 144, 0, 0); setElement(52, 6, 5, "Elt", "Te", "Tellurium", 131.106, 2.1, -1.00, 2, 142, 0, 0); setElement(53, 7, 5, "Elt", "I", "Iodine", 132.674, 2.0, -1.00, 1, 141, 0, 0); setElement(54, 0, 5, "Elt", "Xe", "Xenon", 134.429, 1.9, -1.00, 0, 0, 0, 0); #define MAX_REAL_ELEMENT 54 #include "bonds.gen" addDeTableEntry("H-1-N", 0.75); addDeTableEntry("N-1-O", 0.383); addDeTableEntry("N-1-F", 0.422); addDeTableEntry("F-1-S", 0.166); addDeTableEntry("O-1-F", 0.302); addDeTableEntry("C-1-I", 0.373); addDeTableEntry("O-1-Cl", 0.397); addDeTableEntry("O-1-I", 0.354); addDeTableEntry("S-1-Cl", 0.489); #include "bends.gen" }
void hook_line_exec (struct t_gui_line *line) { struct t_hook *ptr_hook, *next_hook; struct t_hashtable *hashtable, *hashtable2; char str_value[128], *str_tags; if (!weechat_hooks[HOOK_TYPE_LINE]) return; hashtable = NULL; hook_exec_start (); ptr_hook = weechat_hooks[HOOK_TYPE_LINE]; while (ptr_hook) { next_hook = ptr_hook->next_hook; if (!ptr_hook->deleted && !ptr_hook->running && ((HOOK_LINE(ptr_hook, buffer_type) == -1) || ((int)(line->data->buffer->type) == (HOOK_LINE(ptr_hook, buffer_type)))) && string_match_list (line->data->buffer->full_name, (const char **)HOOK_LINE(ptr_hook, buffers), 0) && (!HOOK_LINE(ptr_hook, tags_array) || gui_line_match_tags (line->data, HOOK_LINE(ptr_hook, tags_count), HOOK_LINE(ptr_hook, tags_array)))) { /* create the hashtable that will be sent to callback */ if (!hashtable) { hashtable = hashtable_new (32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING, NULL, NULL); if (!hashtable) break; } HASHTABLE_SET_POINTER("buffer", line->data->buffer); HASHTABLE_SET_STR("buffer_name", line->data->buffer->full_name); HASHTABLE_SET_STR("buffer_type", gui_buffer_type_string[line->data->buffer->type]); HASHTABLE_SET_INT("y", line->data->y); HASHTABLE_SET_TIME("date", line->data->date); HASHTABLE_SET_TIME("date_printed", line->data->date_printed); HASHTABLE_SET_STR_NOT_NULL("str_time", line->data->str_time); HASHTABLE_SET_INT("tags_count", line->data->tags_count); str_tags = string_build_with_split_string ( (const char **)line->data->tags_array, ","); HASHTABLE_SET_STR_NOT_NULL("tags", str_tags); if (str_tags) free (str_tags); HASHTABLE_SET_INT("displayed", line->data->displayed); HASHTABLE_SET_INT("notify_level", line->data->notify_level); HASHTABLE_SET_INT("highlight", line->data->highlight); HASHTABLE_SET_STR_NOT_NULL("prefix", line->data->prefix); HASHTABLE_SET_STR_NOT_NULL("message", line->data->message); /* run callback */ ptr_hook->running = 1; hashtable2 = (HOOK_LINE(ptr_hook, callback)) (ptr_hook->callback_pointer, ptr_hook->callback_data, hashtable); ptr_hook->running = 0; if (hashtable2) { gui_line_hook_update (line, hashtable, hashtable2); hashtable_free (hashtable2); if (!line->data->buffer) break; } } ptr_hook = next_hook; } hook_exec_end (); if (hashtable) hashtable_free (hashtable); }
int quantize(Pixel *pixelData, uint32_t nPixels, uint32_t nQuantPixels, Pixel **palette, uint32_t *paletteLength, uint32_t **quantizedPixels, int kmeans) { PixelList *hl[3]; HashTable *h; BoxNode *root; uint32_t i; uint32_t *qp; uint32_t nPaletteEntries; uint32_t *avgDist; uint32_t **avgDistSortKey; Pixel *p; #ifndef NO_OUTPUT uint32_t timer,timer2; #endif #ifndef NO_OUTPUT timer2=clock(); printf ("create hash table..."); fflush(stdout); timer=clock(); #endif h=create_pixel_hash(pixelData,nPixels); #ifndef NO_OUTPUT printf ("done (%f)\n",(clock()-timer)/(double)CLOCKS_PER_SEC); #endif if (!h) { goto error_0; } #ifndef NO_OUTPUT printf ("create lists from hash table..."); fflush(stdout); timer=clock(); #endif hl[0]=hl[1]=hl[2]=NULL; hashtable_foreach(h,hash_to_list,hl); #ifndef NO_OUTPUT printf ("done (%f)\n",(clock()-timer)/(double)CLOCKS_PER_SEC); #endif if (!hl[0]) { goto error_1; } #ifndef NO_OUTPUT printf ("mergesort lists..."); fflush(stdout); timer=clock(); #endif for(i=0;i<3;i++) { hl[i]=mergesort_pixels(hl[i],i); } #ifdef TEST_MERGESORT if (!test_sorted(hl)) { printf ("bug in mergesort\n"); goto error_1; } #endif #ifndef NO_OUTPUT printf ("done (%f)\n",(clock()-timer)/(double)CLOCKS_PER_SEC); #endif #ifndef NO_OUTPUT printf ("median cut..."); fflush(stdout); timer=clock(); #endif root=median_cut(hl,nPixels,nQuantPixels); #ifndef NO_OUTPUT printf ("done (%f)\n",(clock()-timer)/(double)CLOCKS_PER_SEC); #endif if (!root) { goto error_1; } nPaletteEntries=0; #ifndef NO_OUTPUT printf ("median cut tree to hash table..."); fflush(stdout); timer=clock(); #endif annotate_hash_table(root,h,&nPaletteEntries); #ifndef NO_OUTPUT printf ("done (%f)\n",(clock()-timer)/(double)CLOCKS_PER_SEC); #endif #ifndef NO_OUTPUT printf ("compute palette...\n"); fflush(stdout); timer=clock(); #endif if (!compute_palette_from_median_cut(pixelData,nPixels,h,&p,nPaletteEntries)) { goto error_3; } #ifndef NO_OUTPUT printf ("done (%f)\n",(clock()-timer)/(double)CLOCKS_PER_SEC); #endif free_box_tree(root); root=NULL; /* malloc check ok, using calloc for overflow */ qp=calloc(nPixels, sizeof(uint32_t)); if (!qp) { goto error_4; } if (nPaletteEntries > UINT32_MAX / nPaletteEntries ) { goto error_5; } /* malloc check ok, using calloc for overflow, check of n*n above */ avgDist=calloc(nPaletteEntries*nPaletteEntries, sizeof(uint32_t)); if (!avgDist) { goto error_5; } /* malloc check ok, using calloc for overflow, check of n*n above */ avgDistSortKey=calloc(nPaletteEntries*nPaletteEntries, sizeof(uint32_t *)); if (!avgDistSortKey) { goto error_6; } if (!build_distance_tables(avgDist,avgDistSortKey,p,nPaletteEntries)) { goto error_7; } if (!map_image_pixels_from_median_box(pixelData,nPixels,p,nPaletteEntries,h,avgDist,avgDistSortKey,qp)) { goto error_7; } #ifdef TEST_NEAREST_NEIGHBOUR #include <math.h> { uint32_t bestmatch,bestdist,dist; HashTable *h2; printf ("nearest neighbour search (full search)..."); fflush(stdout); timer=clock(); h2=hashtable_new(unshifted_pixel_hash,unshifted_pixel_cmp); for (i=0;i<nPixels;i++) { if (hashtable_lookup(h2,pixelData[i],&paletteEntry)) { bestmatch=paletteEntry; } else { bestmatch=0; bestdist= _SQR(pixelData[i].c.r-p[0].c.r)+ _SQR(pixelData[i].c.g-p[0].c.g)+ _SQR(pixelData[i].c.b-p[0].c.b); for (j=1;j<nPaletteEntries;j++) { dist= _SQR(pixelData[i].c.r-p[j].c.r)+ _SQR(pixelData[i].c.g-p[j].c.g)+ _SQR(pixelData[i].c.b-p[j].c.b); if (dist==bestdist && j==qp[i]) { bestmatch=j; } if (dist<bestdist) { bestdist=dist; bestmatch=j; } } hashtable_insert(h2,pixelData[i],bestmatch); } if (qp[i]!=bestmatch ) { printf ("discrepancy in matching algorithms pixel %d [%d %d] %f %f\n", i,qp[i],bestmatch, sqrt((double)(_SQR(pixelData[i].c.r-p[qp[i]].c.r)+ _SQR(pixelData[i].c.g-p[qp[i]].c.g)+ _SQR(pixelData[i].c.b-p[qp[i]].c.b))), sqrt((double)(_SQR(pixelData[i].c.r-p[bestmatch].c.r)+ _SQR(pixelData[i].c.g-p[bestmatch].c.g)+ _SQR(pixelData[i].c.b-p[bestmatch].c.b))) ); } } hashtable_free(h2); } #endif #ifndef NO_OUTPUT printf ("k means...\n"); fflush(stdout); timer=clock(); #endif if (kmeans) k_means(pixelData,nPixels,p,nPaletteEntries,qp,kmeans-1); #ifndef NO_OUTPUT printf ("done (%f)\n",(clock()-timer)/(double)CLOCKS_PER_SEC); #endif *quantizedPixels=qp; *palette=p; *paletteLength=nPaletteEntries; #ifndef NO_OUTPUT printf ("cleanup..."); fflush(stdout); timer=clock(); #endif if (avgDist) free(avgDist); if (avgDistSortKey) free(avgDistSortKey); destroy_pixel_hash(h); #ifndef NO_OUTPUT printf ("done (%f)\n",(clock()-timer)/(double)CLOCKS_PER_SEC); printf ("-----\ntotal time %f\n",(clock()-timer2)/(double)CLOCKS_PER_SEC); #endif return 1; error_7: if (avgDistSortKey) free(avgDistSortKey); error_6: if (avgDist) free(avgDist); error_5: if (qp) free(qp); error_4: if (p) free(p); error_3: if (root) free_box_tree(root); error_1: destroy_pixel_hash(h); error_0: *quantizedPixels=NULL; *paletteLength=0; *palette=NULL; return 0; }
TEST(Eval, EvalReplaceRegex) { struct t_hashtable *pointers, *extra_vars, *options; char *value; regex_t regex; pointers = hashtable_new (32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_POINTER, NULL, NULL); CHECK(pointers); extra_vars = hashtable_new (32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING, NULL, NULL); CHECK(extra_vars); hashtable_set (extra_vars, "test", "value"); options = hashtable_new (32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING, NULL, NULL); CHECK(options); /* replace regex by empty string */ hashtable_remove (pointers, "regex"); hashtable_set (options, "regex", ".*"); hashtable_set (options, "regex_replace", ""); WEE_CHECK_EVAL("", "test"); /* replace empty regex */ hashtable_remove (pointers, "regex"); hashtable_set (options, "regex", ""); hashtable_set (options, "regex_replace", "abc"); WEE_CHECK_EVAL("test", "test"); /* replace empty regex by empty string */ hashtable_remove (pointers, "regex"); hashtable_set (options, "regex", ""); hashtable_set (options, "regex_replace", ""); WEE_CHECK_EVAL("test", "test"); /* add brackets around URLs (regex as string) */ hashtable_remove (pointers, "regex"); hashtable_set (options, "regex", "\\w+://\\S+"); hashtable_set (options, "regex_replace", "[ ${re:0} ]"); WEE_CHECK_EVAL("test: [ https://weechat.org ]", "test: https://weechat.org"); /* add brackets around URLs (compiled regex) */ LONGS_EQUAL(0, string_regcomp (®ex, "\\w+://\\S+", REG_EXTENDED | REG_ICASE)); hashtable_set (pointers, "regex", ®ex); hashtable_remove (options, "regex"); hashtable_set (options, "regex_replace", "[ ${re:0} ]"); WEE_CHECK_EVAL("test: [ https://weechat.org ]", "test: https://weechat.org"); regfree (®ex); /* hide passwords (regex as string) */ hashtable_remove (pointers, "regex"); hashtable_set (options, "regex", "(password=)(\\S+)"); hashtable_set (options, "regex_replace", "${re:1}${hide:*,${re:2}}"); WEE_CHECK_EVAL("password=*** password=***", "password=abc password=def"); /* hide passwords (compiled regex) */ LONGS_EQUAL(0, string_regcomp (®ex, "(password=)(\\S+)", REG_EXTENDED | REG_ICASE)); hashtable_set (pointers, "regex", ®ex); hashtable_remove (options, "regex"); hashtable_set (options, "regex_replace", "${re:1}${hide:*,${re:2}}"); WEE_CHECK_EVAL("password=*** password=***", "password=abc password=def"); regfree (®ex); hashtable_free (pointers); hashtable_free (extra_vars); hashtable_free (options); }
void * hdata_search (struct t_hdata *hdata, void *pointer, const char *search, int move) { char *result; int rc; if (!hdata || !pointer || !search || !search[0] || (move == 0)) return NULL; /* clear or create hashtable with pointer for search */ if (hdata_search_pointers) { hashtable_remove_all (hdata_search_pointers); } else { hdata_search_pointers = hashtable_new (32, DOGECHAT_HASHTABLE_STRING, DOGECHAT_HASHTABLE_POINTER, NULL, NULL); } /* * create hashtable with extra vars (empty hashtable) * (hashtable would be created in eval_expression(), but it's created here * so it will not be created for each call to eval_expression()) */ if (!hdata_search_extra_vars) { hdata_search_extra_vars = hashtable_new (32, DOGECHAT_HASHTABLE_STRING, DOGECHAT_HASHTABLE_STRING, NULL, NULL); } if (!hdata_search_options) { hdata_search_options = hashtable_new (32, DOGECHAT_HASHTABLE_STRING, DOGECHAT_HASHTABLE_STRING, NULL, NULL); if (hdata_search_options) hashtable_set (hdata_search_options, "type", "condition"); } while (pointer) { /* set pointer in hashtable (used for evaluating expression) */ hashtable_set (hdata_search_pointers, hdata->name, pointer); /* evaluate expression */ result = eval_expression (search, hdata_search_pointers, hdata_search_extra_vars, hdata_search_options); rc = eval_is_true (result); if (result) free (result); if (rc) return pointer; pointer = hdata_move (hdata, pointer, move); } return NULL; }
void process_frames(){ if(queue_nitems(receiver) == 0){ return; } size_t len = 0; FRAME *f = queue_peek(receiver, &len); int source_nodenumber = find_nodenumber(f->payload.source); if(node_buffer[source_nodenumber].busy){ free(f); return; } node_buffer[source_nodenumber].busy = true; f = queue_remove(receiver, &len); //printf("Received frames for message #%d Expecting %d\n", f->payload.mesg_seq_no, node_buffer[source_nodenumber].mesg_seq_no_to_receive); //If ack has not been received at the sender side and an old frame from an old message has been received if(f->payload.mesg_seq_no < node_buffer[source_nodenumber].mesg_seq_no_to_receive){ //printf("Message received is old, dropped :-( Message #%d\n", f->payload.mesg_seq_no); node_buffer[source_nodenumber].busy = false; free(f); process_frames(); return; } if(f->payload.mesg_seq_no > node_buffer[source_nodenumber].mesg_seq_no_to_receive){ queue_add(receiver, f, len); //printf("Message received is new, pushed back ;-) Message #%d\n", f->payload.mesg_seq_no); node_buffer[source_nodenumber].busy = false; free(f); //process_frames(); return; } int seq_no = f->payload.A; char seq_str[5]; sprintf(seq_str, "%d", seq_no); if(seq_no == node_buffer[source_nodenumber].next_seq_number_to_add){ //send ack here create_ack(f->payload); // add to the incomplete data object //printf("Frame appending %d with length %d | MSG #%d\n",seq_no, f->payload.len, f->payload.mesg_seq_no); memcpy(&node_buffer[source_nodenumber].incomplete_data[0] + node_buffer[source_nodenumber].bytes_added, &f->payload.data[0], f->payload.len); node_buffer[source_nodenumber].bytes_added += f->payload.len; node_buffer[source_nodenumber].next_seq_number_to_add++; while(true){ int next_seq = node_buffer[source_nodenumber].next_seq_number_to_add; char next_seq_str[5]; sprintf(next_seq_str, "%d", next_seq); size_t plen; PACKET *pkt = hashtable_find(node_buffer[source_nodenumber].ooo_packets, next_seq_str, &plen); if(plen == 0) break; //printf("In While loop:Next frame %d found in HT\n",next_seq); pkt = hashtable_remove(node_buffer[source_nodenumber].ooo_packets, next_seq_str, &plen); create_ack(*pkt); memcpy(&node_buffer[source_nodenumber].incomplete_data[0] + node_buffer[source_nodenumber].bytes_added, &pkt->data, pkt->len); node_buffer[source_nodenumber].bytes_added += pkt->len; node_buffer[source_nodenumber].next_seq_number_to_add++; } // check for the last packet if(f->payload.flag_offset == 1) { //printf("\t\t\t\t\t\tBytes in reconstructed message is %d and is sent by %d\n",node_buffer[source_nodenumber].bytes_added, source_nodenumber); CHECK(CNET_write_application((char*)&node_buffer[source_nodenumber].incomplete_data[0], &node_buffer[source_nodenumber].bytes_added)); node_buffer[source_nodenumber].next_seq_number_to_add = 0; memset(node_buffer[source_nodenumber].incomplete_data, '\0', MAX_MESSAGE_SIZE); hashtable_free(node_buffer[source_nodenumber].ooo_packets); // Overriding default bucket size of 1023 node_buffer[source_nodenumber].ooo_packets = hashtable_new(256); printf("\t\t\t\t\t\tSuccessfully Written to Application. Bytes in the reconstructed message #%d are %d sent by %d.\n", f->payload.mesg_seq_no, node_buffer[source_nodenumber].bytes_added, source_nodenumber); node_buffer[source_nodenumber].bytes_added = 0; node_buffer[source_nodenumber].mesg_seq_no_to_receive++; //= f->payload.mesg_seq_no; } } else { size_t plen; hashtable_find(node_buffer[source_nodenumber].ooo_packets, seq_str, &plen); if(plen == 0){ hashtable_add(node_buffer[source_nodenumber].ooo_packets, seq_str, &f->payload, len - sizeof(uint32_t)); } } node_buffer[source_nodenumber].busy = false; process_frames(); free(f); }
/* main */ int main(int argc, char *argv[]) { struct hashtable * hashtable; struct hashtable_iterator iter; struct hashtable_entry entry; void * valuepointer; int valueint, seed = 0, stringcount = 0, stringlength, key_bytes = 0, value_bytes = 0, entrynumber, collisions = 0, delete_count; char * source, * destination, * value; diag("02b-hashtable"); plan(4); hashtable = hashtable_new(); srand(seed); /* TODO: get a portable seed from for example current time */ while (stringcount<STRINGCOUNT) { /* nondeterministic because of collisions */ char * key = random_string(MAXKEYLENGTH); /* create a value consisting of the key reversed followed by */ /* the original key, for example 'abc' -> 'cbaabc' */ stringlength = strlen(key); value = (char *) malloc(2 * stringlength + 1); destination=value+stringlength; * destination -- = '\0'; for (source=key; stringlength-->0; ) { * destination -- = * source ++; } strcat( value, key ); /* test whether the key is already in the hashtable */ if ( hashtable_fetch(hashtable, key, strlen(key), & valuepointer, & valueint) ) { /* it is already in the hash table, free these values */ free(key); free(value); ++ collisions; } else { /* it is not already in the hash table, add it */ hashtable_store(hashtable, key, strlen(key), value, strlen(value)); key_bytes += strlen(key); value_bytes += strlen(value); ++ stringcount; } } is_ii( stringcount, STRINGCOUNT, "created a hash with 5000 entries"); srand(seed); /* Test 2 - iterate the entries and delete them */ hashtable_iterator_init(hashtable, & iter); delete_count = 0; while (hashtable_iterator_next(& iter, & entry)) { key_bytes -= strlen(entry.keypointer); value_bytes -= strlen(entry.valuepointer); /* fprintf(stderr,"iter A '%s' => '%s'\n", (char *) entry.keypointer, (char *) entry.valuepointer); */ free(entry.keypointer); free(entry.valuepointer); ++delete_count; } is_ii(delete_count, stringcount, "iterate 5000 entries and delete"); /* Test 3 - verify total number of bytes in keys */ is_ii(key_bytes, 0, "all bytes in keys reclaimed"); /* Test 4 - verify total number of bytes in keys */ is_ii(value_bytes, 0, "all bytes in values reclaimed"); /* Cannot test this internally, but Valgrind should show that no */ /* bytes remain allocated on the heap. */ hashtable_free(hashtable); return 0; }
TEST(Eval, EvalExpression) { struct t_hashtable *pointers, *extra_vars, *options; struct t_config_option *ptr_option; char *value, str_value[256]; pointers = NULL; extra_vars = hashtable_new (32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING, NULL, NULL); CHECK(extra_vars); hashtable_set (extra_vars, "test", "value"); options = NULL; POINTERS_EQUAL(NULL, eval_expression (NULL, NULL, NULL, NULL)); /* test with simple strings */ WEE_CHECK_EVAL("", ""); WEE_CHECK_EVAL("a b c", "a b c"); WEE_CHECK_EVAL("$", "$"); WEE_CHECK_EVAL("", "${"); WEE_CHECK_EVAL("}", "}"); WEE_CHECK_EVAL("", "${}"); WEE_CHECK_EVAL("", "${xyz}"); /* test eval of substring */ WEE_CHECK_EVAL("\t", "${eval:${\\t}}"); /* test value from extra_vars */ WEE_CHECK_EVAL("value", "${test}"); /* test escaped chars */ WEE_CHECK_EVAL("\t", "${\\t}"); WEE_CHECK_EVAL("\t", "${esc:\t}"); /* test hidden chars */ WEE_CHECK_EVAL("********", "${hide:*,password}"); WEE_CHECK_EVAL("\u2603\u2603\u2603", "${hide:${esc:\u2603},abc}"); /* test color */ WEE_CHECK_EVAL(gui_color_get_custom ("green"), "${color:green}"); WEE_CHECK_EVAL(gui_color_get_custom ("*214"), "${color:*214}"); snprintf (str_value, sizeof (str_value), "%s-test-", gui_color_from_option (config_color_chat_delimiters)); WEE_CHECK_EVAL(str_value, "${color:chat_delimiters}-test-"); config_file_search_with_string ("irc.color.message_join", NULL, NULL, &ptr_option, NULL); if (!ptr_option) { FAIL("ERROR: option irc.color.message_join not found."); } snprintf (str_value, sizeof (str_value), "%s-test-", gui_color_from_option (ptr_option)); WEE_CHECK_EVAL(str_value, "${color:irc.color.message_join}-test-"); WEE_CHECK_EVAL("test", "${option.not.found}test"); /* test info */ WEE_CHECK_EVAL(version_get_version (), "${info:version}"); /* test date */ value = eval_expression ("${date}", pointers, extra_vars, options); LONGS_EQUAL(19, strlen (value)); free (value); value = eval_expression ("${date:%H:%M:%S}", pointers, extra_vars, options); LONGS_EQUAL(8, strlen (value)); free (value); /* test option */ snprintf (str_value, sizeof (str_value), "%d", CONFIG_INTEGER(config_look_scroll_amount)); WEE_CHECK_EVAL(str_value, "${weechat.look.scroll_amount}"); WEE_CHECK_EVAL(str_value, "${${window.buffer.name}.look.scroll_amount}"); /* test hdata */ WEE_CHECK_EVAL("x", "x${buffer.number"); WEE_CHECK_EVAL("x${buffer.number}1", "x\\${buffer.number}${buffer.number}"); WEE_CHECK_EVAL("1", "${buffer.number}"); WEE_CHECK_EVAL("1", "${window.buffer.number}"); WEE_CHECK_EVAL("core.weechat", "${buffer.full_name}"); WEE_CHECK_EVAL("core.weechat", "${window.buffer.full_name}"); hashtable_free (extra_vars); }
TEST(Hashtable, SetGetRemove) { struct t_hashtable *hashtable, *hashtable2; struct t_hashtable_item *item, *ptr_item, *ptr_item2; const char *str_key = HASHTABLE_TEST_KEY; const char *str_value = HASHTABLE_TEST_VALUE; const char *ptr_value; unsigned long long hash; int i; hashtable = hashtable_new (32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING, &test_hashtable_hash_key_cb, &test_hashtable_keycmp_cb); LONGS_EQUAL(32, hashtable->size); LONGS_EQUAL(0, hashtable->items_count); /* invalid set of items */ POINTERS_EQUAL(NULL, hashtable_set_with_size (NULL, NULL, -1, NULL, -1)); POINTERS_EQUAL(NULL, hashtable_set_with_size (NULL, NULL, -1, NULL, -1)); /* add an item in hashtable with NULL value */ item = hashtable_set (hashtable, str_key, NULL); CHECK(item); LONGS_EQUAL(1, hashtable->items_count); STRCMP_EQUAL(str_key, (const char *)item->key); LONGS_EQUAL(strlen (str_key) + 1, item->key_size); POINTERS_EQUAL(NULL, item->value); LONGS_EQUAL(0, item->value_size); POINTERS_EQUAL(NULL, item->prev_item); POINTERS_EQUAL(NULL, item->next_item); /* set a string value for the same key */ item = hashtable_set (hashtable, str_key, str_value); CHECK(item); LONGS_EQUAL(1, hashtable->items_count); STRCMP_EQUAL(str_key, (const char *)item->key); LONGS_EQUAL(strlen (str_key) + 1, item->key_size); STRCMP_EQUAL(str_value, (const char *)item->value); LONGS_EQUAL(strlen (str_value) + 1, item->value_size); POINTERS_EQUAL(NULL, item->prev_item); POINTERS_EQUAL(NULL, item->next_item); /* get item */ item = hashtable_get_item (hashtable, str_key, &hash); CHECK(item); STRCMP_EQUAL(str_key, (const char *)item->key); STRCMP_EQUAL(str_value, (const char *)item->value); LONGS_EQUAL(2, hash); /* get value */ ptr_value = (const char *)hashtable_get (hashtable, str_key); CHECK(ptr_value); STRCMP_EQUAL(ptr_value, str_value); /* check if key is in hashtable */ LONGS_EQUAL(0, hashtable_has_key (hashtable, NULL)); LONGS_EQUAL(0, hashtable_has_key (hashtable, "")); LONGS_EQUAL(0, hashtable_has_key (hashtable, "xxx")); LONGS_EQUAL(1, hashtable_has_key (hashtable, str_key)); /* delete an item */ hashtable_remove (hashtable, str_key); LONGS_EQUAL(0, hashtable->items_count); /* add an item with size in hashtable */ item = hashtable_set_with_size (hashtable, str_key, strlen (str_key) + 1, str_value, strlen (str_value) + 1); CHECK(item); LONGS_EQUAL(1, hashtable->items_count); STRCMP_EQUAL(str_key, (const char *)item->key); LONGS_EQUAL(strlen (str_key) + 1, item->key_size); STRCMP_EQUAL(str_value, (const char *)item->value); LONGS_EQUAL(strlen (str_value) + 1, item->value_size); /* add another item */ hashtable_set (hashtable, "xxx", "zzz"); LONGS_EQUAL(2, hashtable->items_count); /* * test duplication of hashtable and check that duplicated content is * exactly the same as initial hashtable */ hashtable2 = hashtable_dup (hashtable); CHECK(hashtable2); LONGS_EQUAL(hashtable->size, hashtable2->size); LONGS_EQUAL(hashtable->items_count, hashtable2->items_count); for (i = 0; i < hashtable->size; i++) { if (hashtable->htable[i]) { ptr_item = hashtable->htable[i]; ptr_item2 = hashtable2->htable[i]; while (ptr_item && ptr_item2) { LONGS_EQUAL(ptr_item->key_size, ptr_item2->key_size); LONGS_EQUAL(ptr_item->value_size, ptr_item2->value_size); if (ptr_item->key) { STRCMP_EQUAL((const char *)ptr_item->key, (const char *)ptr_item2->key); } else { POINTERS_EQUAL(ptr_item->key, ptr_item2->key); } if (ptr_item->value) { STRCMP_EQUAL((const char *)ptr_item->value, (const char *)ptr_item2->value); } else { POINTERS_EQUAL(ptr_item->value, ptr_item2->value); } ptr_item = ptr_item->next_item; ptr_item2 = ptr_item2->next_item; CHECK((ptr_item && ptr_item2) || (!ptr_item && !ptr_item2)); } } else { POINTERS_EQUAL(hashtable->htable[i], hashtable2->htable[i]); } } /* remove all items */ hashtable_remove_all (hashtable); LONGS_EQUAL(0, hashtable->items_count); /* free hashtables */ hashtable_free (hashtable); hashtable_free (hashtable2); /* * create a hashtable with size 8, and add 6 items, * to check if many items with same hashed key work fine, * the expected htable inside hashtable is: * +-----+ * | 0 | * +-----+ * | 1 | * +-----+ * | 2 | --> "extensible" * +-----+ * | 3 | --> "fast" --> "light" * +-----+ * | 4 | * +-----+ * | 5 | --> "chat" * +-----+ * | 6 | --> "client" * +-----+ * | 7 | --> "weechat" * +-----+ */ hashtable = hashtable_new (8, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING, NULL, NULL); LONGS_EQUAL(8, hashtable->size); LONGS_EQUAL(0, hashtable->items_count); item = hashtable_set (hashtable, "weechat", NULL); CHECK(item); POINTERS_EQUAL(item, hashtable->htable[7]); item = hashtable_set (hashtable, "fast", NULL); CHECK(item); POINTERS_EQUAL(item, hashtable->htable[3]); item = hashtable_set (hashtable, "light", NULL); CHECK(item); POINTERS_EQUAL(item, hashtable->htable[3]->next_item); item = hashtable_set (hashtable, "extensible", NULL); CHECK(item); POINTERS_EQUAL(item, hashtable->htable[2]); item = hashtable_set (hashtable, "chat", NULL); CHECK(item); POINTERS_EQUAL(item, hashtable->htable[5]); item = hashtable_set (hashtable, "client", NULL); CHECK(item); POINTERS_EQUAL(item, hashtable->htable[6]); /* free hashtable */ hashtable_free (hashtable); }
void hashtable_set(hashtable_t* input, const void* key, size_t key_length, void* value) { //fprintf(stderr,"store(%s,%p)\n",key,value); uint32_t hashcode=_hashtable_function(key, key_length); uint32_t bucket=hashcode%input->size; hashelement_t* element=input->buckets[bucket]; #ifdef HASHTABLE_REORDER_ON_ACCESS hashelement_t* previous_element=NULL; #endif #ifdef HASHTABLE_GATHER_STATS unsigned int num_hits=1; #endif while(element!=NULL) { if(element->key_length==key_length && memcmp(element->key,key,key_length)==0) { element->value=value; #ifdef HASHTABLE_GATHER_STATS input->num_hits_per_access+=num_hits; input->num_accesses++; #endif #ifdef HASHTABLE_REORDER_ON_ACCESS if(previous_element!=NULL) { previous_element->next_in_bucket=element->next_in_bucket; element->next_in_bucket=input->buckets[bucket]; input->buckets[bucket]=element; } #endif return; } #ifdef HASHTABLE_GATHER_STATS num_hits++; #endif #ifdef HASHTABLE_REORDER_ON_ACCESS previous_element=element; #endif element=element->next_in_bucket; } #ifdef HASHTABLE_GATHER_STATS input->num_hits_per_access+=num_hits; input->num_accesses++; #endif element = (hashelement_t*)MALLOC(sizeof(hashelement_t)); element->hashcode=hashcode; #ifdef HASHTABLE_CENTRALIZE_KEYS if(input==_hashtable_key_repository) { element->key=key; } else { if(_hashtable_key_repository==NULL) _hashtable_key_repository=hashtable_new(); char* repository_key=hashtable_fetch(_hashtable_key_repository, key, key_length); if(repository_key==NULL) { repository_key=MALLOC(key_length); memcpy(repository_key,key,key_length); hashtable_store(_hashtable_key_repository, repository_key, key_length, repository_key); } element->key=repository_key; } #else element->key=MALLOC(key_length); memcpy(element->key,key,key_length); #endif element->key_length=key_length; element->value=value; element->next_in_bucket=input->buckets[bucket]; input->buckets[bucket]=element; input->length++; #ifdef HASHTABLE_INCREASE_SIZE if(input->length/input->size>input->max_average_collisions)hashtable_resize(input, (size_t)(input->size*(input->resize_factor)+1));//+input->size); #endif }
int attr_cache_init() { attr_ht = hashtable_new(hash_int64, equal_int64, 1024); return 0; }
/* Test driver for hashtable. */ int main(int argc, char **argv) { /* Test key_hashtable instance. */ hashtable_t *kt; hashtable_iter_t ki; key_t k1, k2; key_init(&k1, 1); key_init(&k2, 2); assert((kt = key_hashtable_new(16)) != NULL); assert(key_hashtable_add(kt, &k1) == &k1); assert(key_hashtable_find(kt, &k1) == &k1); assert(key_hashtable_find(kt, &k2) == NULL); assert(key_hashtable_iter(&ki, kt) == &k1); assert(key_hashtable_next(&ki) == NULL); /* Test hashtable instance. */ hashtable_t *t; entry_t entry[256]; entry_t e; match_t m; int i; entry_init(&e, 0); for (i = 0; i < 256; i++) entry_init(&entry[i], i); /* Test hashtable_new() */ t = hashtable_new(256); assert(t->size == 512); assert(t->count == 0); assert(t->etable != NULL); assert(t->ktable != NULL); /* Test hashtable_add() */ assert(hashtable_add(t, &e) == &e); /* Added duplicated copy. */ assert(hashtable_add(t, &entry[0]) == &entry[0]); /* Added duplicated instance. */ for (i = 0; i < 256; i++) assert(hashtable_add(t, &entry[i]) == &entry[i]); assert(t->count == 258); /* Test hashtable_find() */ match_init(&m, 0); assert(hashtable_find(t, &m) == &e); /* Finds first duplicate added. */ assert(m.value == m.source); /* match_cmp() updated m.value. */ for (i = 1; i < 256; i++) { match_init(&m, i); assert(hashtable_find(t, &m) == &entry[i]); assert(m.value == m.source); /* match_cmp() updated m.value. */ } match_init(&m, 256); assert(hashtable_find(t, &m) == NULL); /* Find missing entry. */ assert(m.value == 0); /* match_cmp() didn't update m.value. */ #ifndef HASHTABLE_NSTATS assert(t->find_count == 257); assert(t->match_count == 256); assert(t->hashcmp_count >= 256); assert(t->entrycmp_count >= 256); hashtable_stats_init(t); assert(t->find_count == 0); assert(t->match_count == 0); assert(t->hashcmp_count == 0); assert(t->entrycmp_count == 0); #endif /* Test hashtable iterators */ entry_t *p; hashtable_iter_t iter; int count = 0; for (p = hashtable_iter(&iter, t); p != NULL; p = hashtable_next(&iter)) { assert(p == &e || (&entry[0] <= p && p <= &entry[255])); count++; } assert(count == 258); hashtable_free(t); return 0; }
void GUIID_Table_Create() { g_guid_table = hashtable_new(0x1000); }
struct t_hashtable * gui_focus_to_hashtable (struct t_gui_focus_info *focus_info, const char *key) { struct t_hashtable *hashtable; char str_value[128], *str_time, *str_prefix, *str_tags, *str_message; const char *nick; hashtable = hashtable_new (32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING, NULL, NULL); if (!hashtable) return NULL; /* key (key from keyboard or mouse event) */ FOCUS_STR("_key", key); /* x,y */ FOCUS_INT("_x", focus_info->x); FOCUS_INT("_y", focus_info->y); /* window */ FOCUS_PTR("_window", focus_info->window); if (focus_info->window) { FOCUS_INT("_window_number", (focus_info->window)->number); } else { FOCUS_STR("_window_number", "*"); } /* buffer */ FOCUS_PTR("_buffer", focus_info->buffer); if (focus_info->buffer) { FOCUS_INT("_buffer_number", (focus_info->buffer)->number); FOCUS_STR("_buffer_plugin", plugin_get_name ((focus_info->buffer)->plugin)); FOCUS_STR("_buffer_name", (focus_info->buffer)->name); FOCUS_STR("_buffer_full_name", (focus_info->buffer)->full_name); hashtable_map ((focus_info->buffer)->local_variables, &gui_focus_buffer_localvar_map_cb, hashtable); } else { FOCUS_PTR("_buffer", NULL); FOCUS_STR("_buffer_number", "-1"); FOCUS_STR("_buffer_plugin", ""); FOCUS_STR("_buffer_name", ""); FOCUS_STR("_buffer_full_name", ""); } /* chat area */ FOCUS_INT("_chat", focus_info->chat); str_time = NULL; str_prefix = NULL; if (focus_info->chat_line) { str_time = gui_color_decode (((focus_info->chat_line)->data)->str_time, NULL); str_prefix = gui_color_decode (((focus_info->chat_line)->data)->prefix, NULL); str_tags = string_build_with_split_string ((const char **)((focus_info->chat_line)->data)->tags_array, ","); str_message = gui_color_decode (((focus_info->chat_line)->data)->message, NULL); nick = gui_line_get_nick_tag (focus_info->chat_line); FOCUS_PTR("_chat_line", focus_info->chat_line); FOCUS_INT("_chat_line_x", focus_info->chat_line_x); FOCUS_INT("_chat_line_y", ((focus_info->chat_line)->data)->y); FOCUS_TIME("_chat_line_date", ((focus_info->chat_line)->data)->date); FOCUS_TIME("_chat_line_date_printed", ((focus_info->chat_line)->data)->date_printed); FOCUS_STR_VAR("_chat_line_time", str_time); FOCUS_STR_VAR("_chat_line_tags", str_tags); FOCUS_STR_VAR("_chat_line_nick", nick); FOCUS_STR_VAR("_chat_line_prefix", str_prefix); FOCUS_STR_VAR("_chat_line_message", str_message); if (str_time) free (str_time); if (str_prefix) free (str_prefix); if (str_tags) free (str_tags); if (str_message) free (str_message); } else { FOCUS_PTR("_chat_line", NULL); FOCUS_STR("_chat_line_x", "-1"); FOCUS_STR("_chat_line_y", "-1"); FOCUS_STR("_chat_line_date", "-1"); FOCUS_STR("_chat_line_date_printed", "-1"); FOCUS_STR("_chat_line_time", ""); FOCUS_STR("_chat_line_tags", ""); FOCUS_STR("_chat_line_nick", ""); FOCUS_STR("_chat_line_prefix", ""); FOCUS_STR("_chat_line_message", ""); } FOCUS_STR_VAR("_chat_word", focus_info->chat_word); FOCUS_STR_VAR("_chat_bol", focus_info->chat_bol); FOCUS_STR_VAR("_chat_eol", focus_info->chat_eol); /* bar/item */ if (focus_info->bar_window) { FOCUS_STR("_bar_name", ((focus_info->bar_window)->bar)->name); FOCUS_STR("_bar_filling", gui_bar_filling_string[gui_bar_get_filling ((focus_info->bar_window)->bar)]); } else { FOCUS_STR("_bar_name", ""); FOCUS_STR("_bar_filling", ""); } FOCUS_STR_VAR("_bar_item_name", focus_info->bar_item); FOCUS_INT("_bar_item_line", focus_info->bar_item_line); FOCUS_INT("_bar_item_col", focus_info->bar_item_col); return hashtable; }
bool init_verb_table() { G_VERB_LIST = calloc(VERB_LAST, sizeof(Verb)); for (int i = 0; i < VERB_LAST; ++i) { G_VERB_LIST[i] = (Verb)i; } G_WORD_TABLE_VERBS = hashtable_new(64, NULL); if (!G_WORD_TABLE_VERBS) { return false; } ADD_VERB("look", VERB_LOOK); ADD_VERB("examine", VERB_LOOK); ADD_VERB("east", VERB_EAST); ADD_VERB("e", VERB_EAST); ADD_VERB("west", VERB_WEST); ADD_VERB("w", VERB_WEST); ADD_VERB("north", VERB_NORTH); ADD_VERB("n", VERB_NORTH); ADD_VERB("south", VERB_SOUTH); ADD_VERB("s", VERB_SOUTH); ADD_VERB("up", VERB_UP); ADD_VERB("u", VERB_UP); ADD_VERB("down", VERB_DOWN); ADD_VERB("d", VERB_DOWN); ADD_VERB("go", VERB_GO); ADD_VERB("g", VERB_GO); ADD_VERB("move", VERB_GO); ADD_VERB("m", VERB_GO); ADD_VERB("take", VERB_TAKE); ADD_VERB("use", VERB_USE); ADD_VERB("quit", VERB_QUIT); ADD_VERB("q", VERB_QUIT); ADD_VERB("exit", VERB_QUIT); ADD_VERB("help", VERB_HELP); ADD_VERB("color", VERB_COLOR); ADD_VERB("save", VERB_SAVE); ADD_VERB("load", VERB_LOAD); ADD_VERB("continue", VERB_CONTINUE); G_VERB_PATTERNS[VERB_INVALID] = VP_NONE; G_VERB_PATTERNS[VERB_LAST] = VP_NONE; G_VERB_PATTERNS[VERB_LOOK] = VP_VERB | VP_VERB_SUBJECT | VP_VERB_SUBJECT_SUBJECT; G_VERB_PATTERNS[VERB_EAST] = VP_VERB; G_VERB_PATTERNS[VERB_WEST] = VP_VERB; G_VERB_PATTERNS[VERB_NORTH] = VP_VERB; G_VERB_PATTERNS[VERB_SOUTH] = VP_VERB; G_VERB_PATTERNS[VERB_UP] = VP_VERB; G_VERB_PATTERNS[VERB_DOWN] = VP_VERB; G_VERB_PATTERNS[VERB_GO] = VP_VERB_SUBJECT; G_VERB_PATTERNS[VERB_TAKE] = VP_VERB_SUBJECT | VP_VERB_SUBJECT_SUBJECT; G_VERB_PATTERNS[VERB_USE] = VP_VERB_SUBJECT | VP_VERB_SUBJECT_SUBJECT; G_VERB_PATTERNS[VERB_QUIT] = VP_COMMAND; G_VERB_PATTERNS[VERB_HELP] = VP_COMMAND; G_VERB_PATTERNS[VERB_COLOR] = VP_COMMAND; return true; }
int main(int argc, char **argv) { int num_threads = 1; int max_values_to_add = 10; int num_hash_buckets = 13; int c; while ((c = getopt(argc, argv, "t:m:s:h")) != -1) { switch(c) { case 't': num_threads = atoi(optarg); if (num_threads < 1 || num_threads > 100) { usage(argv[0]); } break; case 'm': max_values_to_add = atoi(optarg); if (max_values_to_add < 1) { usage(argv[0]); } break; case 's': num_hash_buckets = atoi(optarg); if (num_hash_buckets < 1) { usage(argv[0]); } break; case 'h': default: usage(argv[0]); break; } } // see the RNG srandom(time(NULL)); // set up thread arguments struct args thread_args; thread_args.hash = hashtable_new(num_hash_buckets); thread_args.max_values = max_values_to_add; thread_args.num_words = 0; thread_args.words = NULL; thread_args.done_adding = 0; pthread_mutex_init(&thread_args.done_adding_mutex, NULL); pthread_cond_init(&thread_args.done_adding_condv, NULL); // load up words from text file load_words("words.txt", &thread_args); // here are our threads... pthread_t threads[num_threads]; int i = 0; // start up the threads; they'll start adding to the hashtable // immediately. for (i = 0; i < num_threads; i++) { if (0 > pthread_create(&threads[i], NULL, hasher_thread, (void*)&thread_args)) { fprintf(stderr, "Error creating thread: %s\n", strerror(errno)); } } // do nothing in the main thread while worker // threads are adding to hashtable. while (thread_args.done_adding < num_threads) { // sleep for half a second usleep(500000); } // threads are done adding - dump the hashtable printf("Dump of the hash table (which should be as full as it's gonna get).\n"); hashtable_print(thread_args.hash); // signal worker threads to start removing from hashtable pthread_mutex_lock(&thread_args.done_adding_mutex); thread_args.done_adding = -1; pthread_mutex_unlock(&thread_args.done_adding_mutex); pthread_cond_broadcast(&thread_args.done_adding_condv); // wait for workers to complete for (i = 0; i < num_threads; i++) { pthread_join(threads[i], NULL); } printf("Dump of the hash table (which should be empty!)\n"); hashtable_print(thread_args.hash); for (i = 0; i < thread_args.num_words; i++) { free(thread_args.words[i]); } free(thread_args.words); hashtable_free(thread_args.hash); exit(0); }
TEST(Eval, EvalCondition) { struct t_hashtable *pointers, *extra_vars, *options; char *value; pointers = NULL; extra_vars = hashtable_new (32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING, NULL, NULL); CHECK(extra_vars); hashtable_set (extra_vars, "test", "value"); options = hashtable_new (32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING, NULL, NULL); CHECK(options); hashtable_set (options, "type", "condition"); POINTERS_EQUAL(NULL, eval_expression (NULL, NULL, NULL, options)); /* conditions evaluated as false */ WEE_CHECK_EVAL("0", ""); WEE_CHECK_EVAL("0", "0"); WEE_CHECK_EVAL("0", "1 == 2"); WEE_CHECK_EVAL("0", "1 >= 2"); WEE_CHECK_EVAL("0", "2 <= 1"); WEE_CHECK_EVAL("0", "2 != 2"); WEE_CHECK_EVAL("0", "18 < 5"); WEE_CHECK_EVAL("0", "5 > 18"); WEE_CHECK_EVAL("0", "1 == 5 > 18"); WEE_CHECK_EVAL("0", "abc == def"); WEE_CHECK_EVAL("0", "()"); WEE_CHECK_EVAL("0", "(5 > 26)"); WEE_CHECK_EVAL("0", "((5 > 26))"); WEE_CHECK_EVAL("0", "(26 < 5)"); WEE_CHECK_EVAL("0", "abc > def"); WEE_CHECK_EVAL("0", "1 && 0"); WEE_CHECK_EVAL("0", "abc && 0"); WEE_CHECK_EVAL("0", "0 || 0"); WEE_CHECK_EVAL("0", "0 || 0 || 0"); WEE_CHECK_EVAL("0", "0 || 1 && 0"); WEE_CHECK_EVAL("0", "0 || (1 && 0)"); WEE_CHECK_EVAL("0", "0 || (0 || (1 && 0))"); WEE_CHECK_EVAL("0", "1 && (0 || 0)"); WEE_CHECK_EVAL("0", "(0 || 1) && 0"); WEE_CHECK_EVAL("0", "((0 || 1) && 1) && 0"); WEE_CHECK_EVAL("0", "abcd =~ (?-i)^ABC"); WEE_CHECK_EVAL("0", "abcd =~ \\(abcd\\)"); WEE_CHECK_EVAL("0", "(abcd) =~ \\(\\(abcd\\)\\)"); WEE_CHECK_EVAL("0", "${test} == test"); WEE_CHECK_EVAL("0", "${test2} == value2"); WEE_CHECK_EVAL("0", "${buffer.number} == 2"); WEE_CHECK_EVAL("0", "${window.buffer.number} == 2"); /* conditions evaluated as true */ WEE_CHECK_EVAL("1", "1"); WEE_CHECK_EVAL("1", "123"); WEE_CHECK_EVAL("1", "abc"); WEE_CHECK_EVAL("1", "2 == 2"); WEE_CHECK_EVAL("1", "2 >= 1"); WEE_CHECK_EVAL("1", "1 <= 2"); WEE_CHECK_EVAL("1", "1 != 2"); WEE_CHECK_EVAL("1", "18 > 5"); WEE_CHECK_EVAL("1", "5 < 18"); WEE_CHECK_EVAL("1", "1 == 18 > 5"); WEE_CHECK_EVAL("1", "abc == abc"); WEE_CHECK_EVAL("1", "(26 > 5)"); WEE_CHECK_EVAL("1", "((26 > 5))"); WEE_CHECK_EVAL("1", "(5 < 26)"); WEE_CHECK_EVAL("1", "def > abc"); WEE_CHECK_EVAL("1", "1 && 1"); WEE_CHECK_EVAL("1", "abc && 1"); WEE_CHECK_EVAL("1", "0 || 1"); WEE_CHECK_EVAL("1", "0 || 0 || 1"); WEE_CHECK_EVAL("1", "1 || 1 && 0"); WEE_CHECK_EVAL("1", "0 || (1 && 1)"); WEE_CHECK_EVAL("1", "0 || (0 || (1 && 1))"); WEE_CHECK_EVAL("1", "1 && (0 || 1)"); WEE_CHECK_EVAL("1", "(0 || 1) && 1"); WEE_CHECK_EVAL("1", "((0 || 1) && 1) && 1"); WEE_CHECK_EVAL("1", "abcd =~ ^ABC"); WEE_CHECK_EVAL("1", "abcd =~ (?-i)^abc"); WEE_CHECK_EVAL("1", "(abcd) =~ (abcd)"); WEE_CHECK_EVAL("1", "(abcd) =~ \\(abcd\\)"); WEE_CHECK_EVAL("1", "((abcd)) =~ \\(\\(abcd\\)\\)"); WEE_CHECK_EVAL("1", "${test} == value"); WEE_CHECK_EVAL("1", "${test2} =="); WEE_CHECK_EVAL("1", "${buffer.number} == 1"); WEE_CHECK_EVAL("1", "${window.buffer.number} == 1"); hashtable_free (extra_vars); hashtable_free (options); }
char * eval_expression (const char *expr, struct t_hashtable *pointers, struct t_hashtable *extra_vars, struct t_hashtable *options) { int condition, extra_vars_eval, rc, pointers_allocated, regex_allocated; char *value; const char *prefix, *suffix; const char *default_prefix = EVAL_DEFAULT_PREFIX; const char *default_suffix = EVAL_DEFAULT_SUFFIX; const char *ptr_value, *regex_replace; struct t_gui_window *window; regex_t *regex; if (!expr) return NULL; condition = 0; extra_vars_eval = 0; pointers_allocated = 0; regex_allocated = 0; prefix = default_prefix; suffix = default_suffix; regex = NULL; regex_replace = NULL; if (pointers) { regex = (regex_t *)hashtable_get (pointers, "regex"); } else { /* create hashtable pointers if it's NULL */ pointers = hashtable_new (32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_POINTER, NULL, NULL); if (!pointers) return NULL; pointers_allocated = 1; } /* * set window/buffer with pointer to current window/buffer * (if not already defined in the hashtable) */ if (gui_current_window) { if (!hashtable_has_key (pointers, "window")) hashtable_set (pointers, "window", gui_current_window); if (!hashtable_has_key (pointers, "buffer")) { window = (struct t_gui_window *)hashtable_get (pointers, "window"); if (window) hashtable_set (pointers, "buffer", window->buffer); } } /* read options */ if (options) { /* check the type of evaluation */ ptr_value = hashtable_get (options, "type"); if (ptr_value && (strcmp (ptr_value, "condition") == 0)) condition = 1; /* check if extra vars must be evaluated */ ptr_value = hashtable_get (options, "extra"); if (ptr_value && (strcmp (ptr_value, "eval") == 0)) extra_vars_eval = 1; /* check for custom prefix */ ptr_value = hashtable_get (options, "prefix"); if (ptr_value && ptr_value[0]) prefix = ptr_value; /* check for custom suffix */ ptr_value = hashtable_get (options, "suffix"); if (ptr_value && ptr_value[0]) suffix = ptr_value; /* check for regex */ ptr_value = hashtable_get (options, "regex"); if (ptr_value) { regex = malloc (sizeof (*regex)); if (string_regcomp (regex, ptr_value, REG_EXTENDED | REG_ICASE) == 0) { regex_allocated = 1; } else { free (regex); regex = NULL; } } /* check for regex replacement (evaluated later) */ ptr_value = hashtable_get (options, "regex_replace"); if (ptr_value) { regex_replace = ptr_value; } } /* evaluate expression */ if (condition) { /* evaluate as condition (return a boolean: "0" or "1") */ value = eval_expression_condition (expr, pointers, extra_vars, extra_vars_eval, prefix, suffix); rc = eval_is_true (value); if (value) free (value); value = strdup ((rc) ? EVAL_STR_TRUE : EVAL_STR_FALSE); } else { if (regex && regex_replace) { /* replace with regex */ value = eval_replace_regex (expr, regex, regex_replace, pointers, extra_vars, extra_vars_eval, prefix, suffix); } else { /* only replace variables in expression */ value = eval_replace_vars (expr, pointers, extra_vars, extra_vars_eval, prefix, suffix, NULL); } } if (pointers_allocated) hashtable_free (pointers); if (regex && regex_allocated) { regfree (regex); free (regex); } return value; }
char * eval_expression (const char *expr, struct t_hashtable *pointers, struct t_hashtable *extra_vars, struct t_hashtable *options) { int condition, rc, pointers_allocated; char *value; const char *prefix, *suffix; const char *default_prefix = EVAL_DEFAULT_PREFIX; const char *default_suffix = EVAL_DEFAULT_SUFFIX; const char *ptr_value; struct t_gui_window *window; if (!expr) return NULL; condition = 0; pointers_allocated = 0; prefix = default_prefix; suffix = default_suffix; /* create hashtable pointers if it's NULL */ if (!pointers) { pointers = hashtable_new (32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_POINTER, NULL, NULL); if (!pointers) return NULL; pointers_allocated = 1; } /* * set window/buffer with pointer to current window/buffer * (if not already defined in the hashtable) */ if (gui_current_window) { if (!hashtable_has_key (pointers, "window")) hashtable_set (pointers, "window", gui_current_window); if (!hashtable_has_key (pointers, "buffer")) { window = (struct t_gui_window *)hashtable_get (pointers, "window"); if (window) hashtable_set (pointers, "buffer", window->buffer); } } /* read options */ if (options) { /* check the type of evaluation */ ptr_value = hashtable_get (options, "type"); if (ptr_value && (strcmp (ptr_value, "condition") == 0)) condition = 1; /* check for custom prefix */ ptr_value = hashtable_get (options, "prefix"); if (ptr_value && ptr_value[0]) prefix = ptr_value; /* check for custom suffix */ ptr_value = hashtable_get (options, "suffix"); if (ptr_value && ptr_value[0]) suffix = ptr_value; } /* evaluate expression */ if (condition) { /* evaluate as condition (return a boolean: "0" or "1") */ value = eval_expression_condition (expr, pointers, extra_vars, prefix, suffix); rc = eval_is_true (value); if (value) free (value); value = strdup ((rc) ? EVAL_STR_TRUE : EVAL_STR_FALSE); } else { /* only replace variables in expression */ value = eval_replace_vars (expr, pointers, extra_vars, prefix, suffix); } if (pointers_allocated) hashtable_free (pointers); return value; }
struct t_gui_hotlist * gui_hotlist_add (struct t_gui_buffer *buffer, enum t_gui_hotlist_priority priority, struct timeval *creation_time) { struct t_gui_hotlist *new_hotlist, *ptr_hotlist; int i, count[GUI_HOTLIST_NUM_PRIORITIES], rc; char *value, str_value[32]; if (!buffer || !gui_add_hotlist) return NULL; /* do not add core buffer if upgrading */ if (weechat_upgrading && (buffer == gui_buffer_search_main ())) return NULL; if (priority > GUI_HOTLIST_MAX) priority = GUI_HOTLIST_MAX; /* check if priority is OK according to buffer notify level value */ if (!gui_hotlist_check_buffer_notify (buffer, priority)) return NULL; /* create hashtable if needed (to evaluate conditions) */ if (!gui_hotlist_hashtable_add_conditions_pointers) { gui_hotlist_hashtable_add_conditions_pointers = hashtable_new ( 32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_POINTER, NULL, NULL); if (!gui_hotlist_hashtable_add_conditions_pointers) return NULL; } if (!gui_hotlist_hashtable_add_conditions_vars) { gui_hotlist_hashtable_add_conditions_vars = hashtable_new ( 32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING, NULL, NULL); if (!gui_hotlist_hashtable_add_conditions_vars) return NULL; } if (!gui_hotlist_hashtable_add_conditions_options) { gui_hotlist_hashtable_add_conditions_options = hashtable_new ( 32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING, NULL, NULL); if (!gui_hotlist_hashtable_add_conditions_options) return NULL; hashtable_set (gui_hotlist_hashtable_add_conditions_options, "type", "condition"); } /* set data in hashtables */ hashtable_set (gui_hotlist_hashtable_add_conditions_pointers, "window", gui_current_window); hashtable_set (gui_hotlist_hashtable_add_conditions_pointers, "buffer", buffer); snprintf (str_value, sizeof (str_value), "%d", priority); hashtable_set (gui_hotlist_hashtable_add_conditions_vars, "priority", str_value); /* check if conditions are true */ value = eval_expression (CONFIG_STRING(config_look_hotlist_add_conditions), gui_hotlist_hashtable_add_conditions_pointers, gui_hotlist_hashtable_add_conditions_vars, gui_hotlist_hashtable_add_conditions_options); rc = (value && (strcmp (value, "1") == 0)); if (value) free (value); if (!rc) return NULL; /* init count */ for (i = 0; i < GUI_HOTLIST_NUM_PRIORITIES; i++) { count[i] = 0; } ptr_hotlist = gui_hotlist_search (gui_hotlist, buffer); if (ptr_hotlist) { /* return if priority is greater or equal than the one to add */ if (ptr_hotlist->priority >= priority) { ptr_hotlist->count[priority]++; gui_hotlist_changed_signal (); return ptr_hotlist; } /* * if buffer is present with lower priority: save counts, remove it * and go on */ memcpy (count, ptr_hotlist->count, sizeof (ptr_hotlist->count)); gui_hotlist_free (&gui_hotlist, &last_gui_hotlist, ptr_hotlist); } new_hotlist = malloc (sizeof (*new_hotlist)); if (!new_hotlist) return NULL; new_hotlist->priority = priority; if (creation_time) { memcpy (&(new_hotlist->creation_time), creation_time, sizeof (*creation_time)); } else gettimeofday (&(new_hotlist->creation_time), NULL); new_hotlist->buffer = buffer; memcpy (new_hotlist->count, count, sizeof (new_hotlist->count)); new_hotlist->count[priority]++; new_hotlist->next_hotlist = NULL; new_hotlist->prev_hotlist = NULL; gui_hotlist_add_hotlist (&gui_hotlist, &last_gui_hotlist, new_hotlist); gui_hotlist_changed_signal (); return new_hotlist; }
int main() { Queue *q = queue_new(); HashTable *h = hashtable_new(512, header_hash_fn, header_eq_fn); HashEnum e; IsHeader *hp; int i, j; printf("Testing Queue...\n"); queue_insert(q, 1); queue_insert(q, 2); queue_insert(q, 3); queue_insert(q, 4); queue_insert(q, 5); queue_remove(q); queue_insert(q, 1); queue_insert(q, 2); queue_insert(q, 3); queue_insert(q, 4); queue_remove(q); queue_remove(q); queue_insert(q, 5); while (!queue_isempty(q)) printf("%u ", (unsigned)queue_remove(q)); printf("\n"); printf("Testing Hash...\n"); for (i=1; i<1024; i++) hashtable_insert(h, i, malloc(sizeof(IsHeader))); j = 0; e = hashenum_create(h); while((hp = hashenum_next(&e)) != NULL) { hp->offset = header_hash_fn(j); for (i=0; i<32; i++) hp->offsets[i] = i; hp->name = malloc(sizeof(char)*64); sprintf(hp->name, "I am number %d", j++); } for (j=10; j<800; j+=31) { hp = hashtable_find(h, (uint16)j); if (hp == NULL) { printf("Did not find key %d\n", j); continue; } printf("Found key %d - offset: %d name: %s\n", j, hp->offset, hp->name); printf("Offsets:"); for (i=0; i<32; i++) printf(" %d", hp->offsets[i]); printf("\n"); } printf("*** Super Test ***\n"); e = hashenum_create(h); while((hp = hashenum_next(&e)) != NULL) { printf("%d ", hp->offset%1024 ); } printf("\n"); hashtable_freecontents(h); free(h); free(q); return 0; }