int main(void) { extern hash_t *hash_table[TABLE_SIZE]; char *key = "al"; char *value = "password"; hash_table_insert(key, value); hash_table_insert("ed", "gowanlea"); hash_table_insert("bryan", "leicester"); hash_t *found = hash_table_search("al"); if (found) { printf("%s and %s\n", found->key, found->value); } found = hash_table_search("ed"); if (found) { printf("%s and %s\n", found->key, found->value); } else printf("not found!\n"); hash_table_dump(); // this is slow hash_table_free(); return EXIT_SUCCESS; }
int main(int argc, char *argv[]) { hash_table_t *H; char word[WORDLEN+1]; char *new; int htab_size=HTAB_DEFAULT; int i, dwords=0, nwords=0, max, maxloc, min, mincount; int found; srand(86421357); if (argc>1) { htab_size = atoi(argv[1]); } /* create the empty hash table */ H = hash_table_create(htab_size); while (getword(word, WORDLEN) != EOF) { nwords += 1; /* search the hash table */ found = (hash_table_search(H, word, strcmp) != NULL); if (!found) { /* duplicate the string and insert it */ new = malloc(strlen(word)+1); assert(new); strcpy(new, word); H = hash_table_insert(H, new); dwords += 1; } }
word_info_t* dict_search_word_info(const char* text, size_t tsize) { #ifdef AVLTREE char* buf = (char*)text; char ch = buf[tsize]; buf[tsize] = 0; word_info_t* ret = (word_info_t*)avltree_search(root, text); buf[tsize] = ch; #else word_info_t* ret = (word_info_t*)hash_table_search(root, text, tsize); #endif return ret; }
void hash_table_test() { hash_table ht; ht.size = 569; ht.hash = hash_str; ht.iseq = iseq_str; ht.frac = 0.618; ht.print = print_str; hash_table_alloc(&ht); FILE *fp = Fopen(PATH, "r"); string *strpool[MAX_LINE], *str; size_t i, line_total; for(i = 0; i < MAX_LINE; i++){ str = malloc(MAX_LEN + sizeof(string)); strpool[i] = str; str->strp = (char *)str + sizeof(string); str->nline = i + 1; if(fgets(str->strp, MAX_LEN, fp) != NULL){ str->length = strlen(str->strp); hash_table_insert(&ht, strpool[i]); } else{ strpool[i] = NULL; free(str); break; } } line_total = i; string sample, *result; sample.strp = "{\n"; sample.length = strlen(sample.strp); result = hash_table_search(&ht, &sample); //hash_table_print(&ht); if(result != NULL) printf("%lu\t%s",result->nline, result->strp); else printf("not found\n"); hash_table_destroy(&ht); for(i = 0; i < line_total; i++) free(strpool[i]); Fclose(fp); }
char* better_candidate(char* word, char** possibles, int index){ int i, data, best=0; char* best_candidate = word; // On parcourt toutes les corrections for(i=0; i<index; ++i){ // Pointeur sur la ieme correction char *candidate=*(possibles+i); //printf("%s\n",candidate); // On regarde s'il est dans la table data = hash_table_search(candidate); if(data > best) { best = data; best_candidate = candidate; } } return best_candidate; }