/** * Initialize hashtable container. */ void _hashtable_init(_hashtable_t* pt_hashtable, size_t t_bucketcount, ufun_t ufun_hash, bfun_t bfun_compare) { assert(pt_hashtable != NULL); assert(_hashtable_is_created(pt_hashtable)); /* initialize the bucket vector and node count */ vector_init(&pt_hashtable->_vec_bucket); if (t_bucketcount > 0) { vector_resize(&pt_hashtable->_vec_bucket, _hashtable_get_prime(t_bucketcount)); } else { vector_resize(&pt_hashtable->_vec_bucket, _hashtable_get_prime(_HASHTABLE_DEFAULT_BUCKET_COUNT)); } pt_hashtable->_t_nodecount = 0; /* initialize the hash, compare and destroy element function */ pt_hashtable->_ufun_hash = ufun_hash != NULL ? ufun_hash : _hashtable_default_hash; pt_hashtable->_bfun_compare = bfun_compare != NULL ? bfun_compare : _GET_HASHTABLE_TYPE_LESS_FUNCTION(pt_hashtable); }
/** * Resize. */ void _hashtable_resize(_hashtable_t* pt_hashtable, size_t t_resize) { size_t t_tmp = 0; size_t t_pos = 0; size_t i = 0; size_t t_bucketcount = 0; _hashnode_t* pt_node = NULL; _hashnode_t* pt_nodelist = NULL; _hashnode_t** ppt_bucket = NULL; assert(pt_hashtable != NULL); if(t_resize > _hashtable_bucket_count(pt_hashtable)) { /* select all element in hash node list */ for(i = 0; i < vector_size(&pt_hashtable->_vec_bucket); ++i) { ppt_bucket = (_hashnode_t**)vector_at(&pt_hashtable->_vec_bucket, i); pt_node = *ppt_bucket; while(pt_node != NULL) { *ppt_bucket = pt_node->_pt_next; pt_node->_pt_next = pt_nodelist; pt_nodelist = pt_node; pt_node = *ppt_bucket; } } /* resize vector bucket */ vector_resize(&pt_hashtable->_vec_bucket, _hashtable_get_prime(t_resize)); t_bucketcount = _hashtable_bucket_count(pt_hashtable); /* rehash */ while(pt_nodelist != NULL) { pt_node = pt_nodelist; pt_nodelist = pt_node->_pt_next; t_tmp = _GET_HASHTABLE_TYPE_SIZE(pt_hashtable); _hashtable_hash_auxiliary(pt_hashtable, pt_node->_pby_data, &t_tmp); t_pos = t_tmp % t_bucketcount; ppt_bucket = (_hashnode_t**)vector_at(&pt_hashtable->_vec_bucket, t_pos); pt_node->_pt_next = *ppt_bucket; *ppt_bucket = pt_node; } } }