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); }
/** Creates the hash table for speeding up file finding. * * @size the size we want to make this hash table * @head the first gd_fs_entry_t in the list of files * * @returns 0 on success, 1 on failure */ int create_hash_table(size_t size, const struct gd_fs_entry_t* head) { int ret = hcreate(size); if(!ret) { fprintf(stderr, "hcreate failed\n"); return 1; } ENTRY entry; struct gd_fs_entry_t *iter = head; while(iter != NULL) { entry.key = iter->filename.str; entry.data = iter; ENTRY* entered = hsearch(entry, ENTER); if(0 && !entered) { fprintf(stderr, "hsearch: %s\n", strerror(errno)); destroy_hash_table(); return 1; } iter = iter->next; } return 0; }
// Destroy all resource connected to a cache object void destroy_cache(cache_t cache){ //Destroy double linked list and hash table destroy_linked_list(cache->linked_list); destroy_hash_table(cache->hash_table); free(cache->hash_table); free(cache->linked_list); }
int main() { hash_table_t * table = NULL; init_hash_table(&table,10,10); int * num = NULL; int i = 0; char iNum[40]; bzero(iNum,40); for (i=0;i<64;i++) { sprintf(iNum,"%d",i); num = (int *) malloc(sizeof(int)); *num = i; insert_hash_elem(*table,iNum,num); } void * element = NULL; sprintf(iNum,"%d",20); get_value(*table,iNum,&element); if (element) { printf("%d\n", *((int*)element)); } destroy_hash_table(&table,10); return 0; }
void destroy_http_conn(http_conn_t *http_conn) { if( http_conn->tcp_conn ) { destroy_tcp_conn(http_conn->tcp_conn); http_conn->tcp_conn = NULL; } if( http_conn->http_header ) { destroy_hash_table(http_conn->http_header); http_conn->http_header = NULL; } http_conn->session = NULL; free(http_conn); }
int main(int argc, char* argv[]) { if (argc < 2) { usage(); exit(1); } hash_table* T = hash_ids(argv[1]); filter_by_id(argv[1], T); destroy_hash_table(T); 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); } }
void cleanup_mem(void) { int i; destroy_url_list(); history_destroy(); command_cleanup(); queue_destroy(buddy_request_queue); cleanup_manufacture(); cleanup_text_buffers(); cleanup_fonts(); destroy_all_actors(); end_actors_lists(); cleanup_lights(); /* 2d objects */ destroy_all_2d_objects(); destroy_all_2d_object_defs(); /* 3d objects */ destroy_all_3d_objects(); /* caches */ cache_e3d->free_item = &destroy_e3d; cache_delete(cache_e3d); cache_e3d = NULL; #ifdef NEW_TEXTURES free_texture_cache(); #endif // This should be fixed now Sir_Odie cache_delete(cache_system); cache_system = NULL; /* map location information */ for (i = 0; continent_maps[i].name; i++) { free(continent_maps[i].name); } free (continent_maps); destroy_hash_table(server_marks); for (i = 0; i < video_modes_count; i++) { if (video_modes[i].name) free(video_modes[i].name); } free_shaders(); }
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 find_path( int in_room_vnum, int out_room_vnum, CHAR_DATA *ch, int depth, int in_zone ) { struct room_q *tmp_q, *q_head, *q_tail; struct hash_header x_room; int i, tmp_room, count=0, thru_doors; ROOM_INDEX_DATA *herep; ROOM_INDEX_DATA *startp; EXIT_DATA *exitp; if ( depth <0 ) { thru_doors = TRUE; depth = -depth; } else { thru_doors = FALSE; } startp = get_room_index( in_room_vnum ); init_hash_table( &x_room, sizeof(int), 2048 ); hash_enter( &x_room, in_room_vnum, (void *) - 1 ); /* initialize queue */ q_head = (struct room_q *) malloc(sizeof(struct room_q)); q_tail = q_head; q_tail->room_nr = in_room_vnum; q_tail->next_q = 0; while(q_head) { herep = get_room_index( q_head->room_nr ); /* for each room test all directions */ if( herep->area == startp->area || !in_zone ) { /* only look in this zone... saves cpu time and makes world safer for players */ for( i = 0; i <= 5; i++ ) { exitp = herep->exit[i]; if( exit_ok(exitp) && ( thru_doors ? GO_OK_SMARTER : GO_OK ) ) { /* next room */ tmp_room = herep->exit[i]->to_room->vnum; if( tmp_room != out_room_vnum ) { /* shall we add room to queue ? count determines total breadth and depth */ if( !hash_find( &x_room, tmp_room ) && ( count < depth ) ) /* && !IS_SET( RM_FLAGS(tmp_room), DEATH ) ) */ { count++; /* mark room as visted and put on queue */ tmp_q = (struct room_q *) malloc(sizeof(struct room_q)); tmp_q->room_nr = tmp_room; tmp_q->next_q = 0; q_tail->next_q = tmp_q; q_tail = tmp_q; /* ancestor for first layer is the direction */ hash_enter( &x_room, tmp_room, ((int)hash_find(&x_room,q_head->room_nr) == -1) ? (void*)(i+1) : hash_find(&x_room,q_head->room_nr)); } } else { /* have reached our goal so free queue */ tmp_room = q_head->room_nr; for(;q_head;q_head = tmp_q) { tmp_q = q_head->next_q; free(q_head); } /* return direction if first layer */ if ((int)hash_find(&x_room,tmp_room)==-1) { if (x_room.buckets) { /* junk left over from a previous track */ destroy_hash_table(&x_room, donothing); } return(i); } else { /* else return the ancestor */ int i; i = (int)hash_find(&x_room,tmp_room); if (x_room.buckets) { /* junk left over from a previous track */ destroy_hash_table(&x_room, donothing); } return( -1+i); } } } } } /* free queue head and point to next entry */ tmp_q = q_head->next_q; free(q_head); q_head = tmp_q; } /* couldn't find path */ if( x_room.buckets ) { /* junk left over from a previous track */ destroy_hash_table( &x_room, donothing ); } return -1; }
void init_server_markers(){ //init hash table destroy_hash_table(server_marks); server_marks= create_hash_table(50,hash_fn_int,cmp_fn_int,free); }
void test_hash_table_set(void) { { // Values should be stored in the hash table. hash_table_t table; int foundFirst = 0; int foundSecond = 0; int foundOther = 0; int i; 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); hash_table_set("potato", 3, &table); hash_table_set("monkey", 8, &table); TEST_CHECK(table.n == 2); for (i = 0; i < table.capacity; ++i) { hash_table_entry_t n = table.entries[i]; if (n.value == 3) { foundFirst++; } else if (n.value == 8) { foundSecond++; } else { foundOther++; } } TEST_CHECK(foundFirst == 1); TEST_CHECK(foundSecond == 1); TEST_CHECK(foundOther == 18); destroy_hash_table(&table); } { // Values with different hashes but same % capacity value should exist in the // the same hash table. hash_table_t table; int foundFirst = 0; int foundSecond = 0; int foundOther = 0; int i; 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); // hashes are different but % 20 value is the same - they will end up in the same bucket hash_table_set("potato", 3, &table); // hash 356168476 hash_table_set("potat[", 3, &table); // hash 356168456 // hashes are different but % 20 value is the same - they will end up in the same bucket hash_table_set("monkey", 8, &table); // hash 238557080 hash_table_set("monkee", 8, &table); // hash 238557060 TEST_CHECK(table.n == 4); for (i = 0; i < table.capacity; ++i) { if ((table.entries + i)->value == 3) { foundFirst++; } else if ((table.entries + i)->value == 8) { foundSecond++; } else { foundOther++; } } TEST_CHECK(foundFirst == 2); TEST_CHECK(foundSecond == 2); TEST_CHECK(foundOther == 16); destroy_hash_table(&table); } { // TODO: This fails. Should the key also be stored in the entry so it can be compared // with the key being inserted? // Values with identical hashes but different keys should exist in the same hash table. hash_table_t table; int foundFirst = 0; int foundSecond = 0; int foundOther = 0; int i; 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); hash_table_set("potato", 3, &table); // hash 356168476 hash_table_set("potbSo", 3, &table); // hash 356168476 (identical) hash_table_set("monkey", 8, &table); // hash 238557080 hash_table_set("monkfX", 8, &table); // hash 238557080 (identical) // TEST_CHECK(table.n == 4); for (i = 0; i < table.capacity; ++i) { hash_table_entry_t n = table.entries[i]; if (n.value == 3) { foundFirst++; } else if (n.value == 8) { foundSecond++; } else { foundOther++; } } // TEST_CHECK(foundFirst == 2); // TEST_CHECK(foundSecond == 2); // TEST_CHECK(foundOther == 16); destroy_hash_table(&table); } }