int main(int argc, char** argv) { HTree *t = ht_open("*", 0); printf("hash %d\n", ht_get_hash(t, "@", NULL)); printf("%s\n", ht_list(t,"")); ht_clear(t); //printf("hash %d\n", ht_get_hash(t, "@", NULL)); //printf("%s\n", ht_list(t,"")); int i=0; char buf[100]; for (int k=0;k<1;k++){ for (i=0;i<200000;i++){ sprintf(buf, "/photo/photo/%d.jpg", i); //printf(buf); ht_add(t, buf, 1, 3, 0); } printf("add complete\n"); for (i=0;i<1000;i++){ sprintf(buf, "/photo/photo/%d.jpg", i); /*sprintf(buf, "/photo/photo/xxxxxxxxxxxxxxxxxxxxxxfile%d", i);*/ //ht_remove(t, buf, 0); } printf("remove complete\n"); } //remove_from_htree(buf); printf("update complete\n"); // print_tree(&tree); printf("hash %d\n", ht_get_hash(t, "@", NULL)); printf("%s\n", ht_list(t,"")); ht_close(t); return 0; }
void ht_init(struct hashtable *ht,int len,int (*hash)(int),int (*equal)(int,int)) { assert(len>0); ht->tablen=1; len*=LOAD_FACTOR; while(ht->tablen<len) ht->tablen<<=1; ht->limit=ht->tablen/LOAD_FACTOR; ht->table=(int*)m_alloc(ht->tablen<<1,sizeof(int)); /* the second half is hash values */ ht->hash=hash; ht->equal=equal; ht_clear(ht); }
/** * ht_destroy(hnode *table) * * Destroys and frees all the memory used by table. **/ void ht_destroy(htable *table) { // Free all the entries in the table. ht_clear(table); // Free the chains of the table. free(table->array); // Free the table itself free(table); }
//automatically called at exit void protocol_cleanup() { _log(LVL_DEBUG, "Protocol cleanup\n"); if(_proto_init) { ht_clear(cmd_dict, 0); ht_destroy(cmd_dict); _proto_init = false; } }
void ht_destroy() { ht_clear(1); free(hash->table); free(hash); }
static void clear(void) { if(len_txt>LIM_T) {m_free(text); text=(char*)m_alloc(len_txt=LEN_T,sizeof(char));} ht_clear(&ht_s); windup(); }
void rx_clear(void) { ht_clear(&ht_p); ht_clear(&ht_2); ht_clear(&ht_r); ht_clear(&ht_m); windup(); }
/** * ht_resize * * Resizes table if necessary. * * Returns 1 if table was resized, * 2 if no resize was necessary, * 0 if error. **/ int ht_resize(htable *table) { // Find the load factor double loadfactor = (double)table->size/(double)table->length; // Check to see if our table is getting too crowded. if (loadfactor >= LF_THRESH) { // Find our new size PRIME_INDEX++; if (PRIME_INDEX > sizeof(PRIMES)/sizeof(int)) { fputs("Hash table is already too big. Cannot resize.\n", stderr); return 0; } int newsize = PRIMES[PRIME_INDEX] * sizeof(hnode); // Make another array for the table htable temp; // Copy over the pointer to the hash function temp.hash = table->hash; hnode *arr = (hnode *)malloc(newsize); if (! arr) { fputs("Not enough memory to resize the hash table.\n", stderr); return 0; } // Initialize the new array for use. temp.array = arr; temp.length = PRIMES[PRIME_INDEX]; temp.size = 0; memset(temp.array, 0, newsize); int i; for (i=0; i<temp.length; i++) { hnode *chain = &temp.array[i]; chain->length = 0; } // Rehash the contents of the table for (i=0; i<table->length; i++) { hnode chain = table->array[i]; if (chain.length == 0) continue; ht_entry *cur = chain.datum; while (NULL != cur) { ht_entry *entry = (ht_entry *)malloc(sizeof(ht_entry)); strncpy(entry->key, cur->key, 256); // Copy over the data at cur->datum to // entry->datum. this seems much cleaner. entry->datum = malloc(sizeof cur->datum); memcpy(entry->datum, cur->datum, sizeof cur->datum); ht_insert(&temp, entry); cur = cur->next; } } ht_clear(table); free(table->array); table->array = temp.array; table->length = temp.length; return 1; } return 2; }
int main(int argc, char *argv[]) { (void) argc; (void) argv; hash_table ht; ht_init(&ht, HT_KEY_CONST | HT_VALUE_CONST, 0.05); char *s1 = (char*)"teststring 1"; char *s2 = (char*)"teststring 2"; char *s3 = (char*)"teststring 3"; ht_insert(&ht, s1, strlen(s1)+1, s2, strlen(s2)+1); int contains = ht_contains(&ht, s1, strlen(s1)+1); test(contains, "Checking for key \"%s\"", s1); size_t value_size; char *got = ht_get(&ht, s1, strlen(s1)+1, &value_size); fprintf(stderr, "Value size: %zu\n", value_size); fprintf(stderr, "Got: {\"%s\": -----\"%s\"}\n", s1, got); test(value_size == strlen(s2)+1, "Value size was %zu (desired %lu)", value_size, strlen(s2)+1); fprintf(stderr, "Replacing {\"%s\": \"%s\"} with {\"%s\": \"%s\"}\n", s1, s2, s1, s3); ht_insert(&ht, s1, strlen(s1)+1, s3, strlen(s3)+1); unsigned int num_keys; void **keys; keys = ht_keys(&ht, &num_keys); test(num_keys == 1, "HashTable has %d keys", num_keys); test(keys != NULL, "Keys is not null"); if(keys) free(keys); got = ht_get(&ht, s1, strlen(s1)+1, &value_size); fprintf(stderr, "Value size: %zu\n", value_size); fprintf(stderr, "Got: {\"%s\": \"%s\"}\n", s1, got); test(value_size == strlen(s3)+1, "Value size was %zu (desired %lu)", value_size, strlen(s3)+1); fprintf(stderr, "Removing entry with key \"%s\"\n", s1); ht_remove(&ht, s1, strlen(s1)+1); contains = ht_contains(&ht, s1, strlen(s1)+1); test(!contains, "Checking for removal of key \"%s\"", s1); keys = ht_keys(&ht, &num_keys); test(num_keys == 0, "HashTable has %d keys", num_keys); if(keys) free(keys); fprintf(stderr, "Stress test"); int key_count = 1000000; int i; int *many_keys = malloc(key_count * sizeof(*many_keys)); int *many_values = malloc(key_count * sizeof(*many_values)); srand(time(NULL)); for(i = 0; i < key_count; i++) { many_keys[i] = i; many_values[i] = rand(); } struct timespec t1; struct timespec t2; t1 = snap_time(); for(i = 0; i < key_count; i++) { ht_insert(&ht, &(many_keys[i]), sizeof(many_keys[i]), &(many_values[i]), sizeof(many_values[i])); } t2 = snap_time(); fprintf(stderr, "Inserting %d keys took %.2f seconds\n", key_count, get_elapsed(t1, t2)); fprintf(stderr, "Checking inserted keys\n"); int ok_flag = 1; for(i = 0; i < key_count; i++) { if(ht_contains(&ht, &(many_keys[i]), sizeof(many_keys[i]))) { size_t value_size; int value; value = *(int*)ht_get(&ht, &(many_keys[i]), sizeof(many_keys[i]), &value_size); if(value != many_values[i]) { fprintf(stderr, "Key value mismatch. Got {%d: %d} expected: {%d: %d}\n", many_keys[i], value, many_keys[i], many_values[i]); ok_flag = 0; break; } } else { fprintf(stderr, "Missing key-value pair {%d: %d}\n", many_keys[i], many_values[i]); ok_flag = 0; break; } } test(ok_flag == 1, "Result was %d", ok_flag); ht_clear(&ht); ht_resize(&ht, 4194304); t1 = snap_time(); for(i = 0; i < key_count; i++) { ht_insert(&ht, &(many_keys[i]), sizeof(many_keys[i]), &(many_values[i]), sizeof(many_values[i])); } t2 = snap_time(); fprintf(stderr, "Inserting %d keys (on preallocated table) took %.2f seconds\n", key_count, get_elapsed(t1, t2)); for(i = 0; i < key_count; i++) { ht_remove(&ht, &(many_keys[i]), sizeof(many_keys[i])); } test(ht_size(&ht) == 0, "%d keys remaining", ht_size(&ht)); ht_destroy(&ht); free(many_keys); free(many_values); return report_results(); }