Skip to content

Commit

Permalink
refactoring hashtable.
Browse files Browse the repository at this point in the history
  • Loading branch information
activesys committed Aug 30, 2011
1 parent 87693a5 commit 08fa6c2
Show file tree
Hide file tree
Showing 6 changed files with 275 additions and 88 deletions.
8 changes: 4 additions & 4 deletions src/cstl_hashtable.c
Expand Up @@ -703,7 +703,7 @@ void _hashtable_swap(
{
_hashtable_t t_hashtableswap;

assert(_hashtable_same_type(pt_hashtablefirst, pt_hashtablesecond));
assert(_hashtable_same_type_ex(pt_hashtablefirst, pt_hashtablesecond));

t_hashtableswap = *pt_hashtablefirst;
*pt_hashtablefirst = *pt_hashtablesecond;
Expand Down Expand Up @@ -846,7 +846,7 @@ void _hashtable_init_copy_range(_hashtable_t* pt_hashtabledest,
void _hashtable_assign(
_hashtable_t* pt_hashtabledest, const _hashtable_t* cpt_hashtablesrc)
{
assert(_hashtable_same_type(pt_hashtabledest, cpt_hashtablesrc));
assert(_hashtable_same_type_ex(pt_hashtabledest, cpt_hashtablesrc));

/* clear all elements */
_hashtable_clear(pt_hashtabledest);
Expand Down Expand Up @@ -999,7 +999,7 @@ bool_t _hashtable_equal(
assert(cpt_hashtablefirst != NULL && cpt_hashtablesecond != NULL);

/* check type */
if(!_hashtable_same_type(cpt_hashtablefirst, cpt_hashtablesecond))
if(!_hashtable_same_type_ex(cpt_hashtablefirst, cpt_hashtablesecond))
{
return false;
}
Expand Down Expand Up @@ -1049,7 +1049,7 @@ bool_t _hashtable_less(
bool_t t_less = false;
bool_t t_greater = false;

assert(_hashtable_same_type(cpt_hashtablefirst, cpt_hashtablesecond));
assert(_hashtable_same_type_ex(cpt_hashtablefirst, cpt_hashtablesecond));

/* check vector bucket count */
if(vector_size(&cpt_hashtablefirst->_t_bucket) ==
Expand Down
94 changes: 71 additions & 23 deletions src/cstl_hashtable_aux.c
Expand Up @@ -39,6 +39,7 @@
#include "cstl_hashtable_aux.h"

/** local constant declaration and local macro section **/
#define _HASHTABLE_FIRST_PRIME_BUCKET_COUNT 53

/** local data type declaration and local struct, union, enum section **/

Expand Down Expand Up @@ -90,35 +91,70 @@ bool_t _hashtable_is_created(const _hashtable_t* cpt_hashtable)
return _alloc_is_inited(&cpt_hashtable->_t_allocator);
}

bool_t _hashtable_iterator_belong_to_hashtable(
const _hashtable_t* cpt_hashtable, _hashtable_iterator_t t_iter)
/**
* Test hashtable is initialized by hashtable initialization functions.
*/
bool_t _hashtable_is_inited(const _hashtable_t* cpt_hashtable)
{
vector_iterator_t t_vectoriterator;
assert(cpt_hashtable != NULL);

if(cpt_hashtable->_t_typeinfo._t_style != _TYPE_C_BUILTIN &&
cpt_hashtable->_t_typeinfo._t_style != _TYPE_CSTL_BUILTIN &&
cpt_hashtable->_t_typeinfo._t_style != _TYPE_USER_DEFINE)
{
return false;
}
if(cpt_hashtable->_t_typeinfo._pt_type == NULL)
{
return false;
}

if(!_vector_is_inited(&cpt_hashtable->_t_bucket) ||
vector_size(&cpt_hashtable->_t_bucket) < _HASHTABLE_FIRST_PRIME_BUCKET_COUNT)
{
return false;
}

if(cpt_hashtable->_t_hash == NULL || cpt_hashtable->_t_compare == NULL)
{
return false;
}

return true;
}

/**
* Test iterator referenced data is within the hashtable.
*/
bool_t _hashtable_iterator_belong_to_hashtable(const _hashtable_t* cpt_hashtable, _hashtable_iterator_t it_iter)
{
vector_iterator_t it_bucket;

assert(cpt_hashtable != NULL);
assert(_GET_HASHTABLE(t_iter) == cpt_hashtable);
assert(_hashtable_is_inited(cpt_hashtable));
assert(_GET_HASHTABLE_BUCKETPOS(it_iter) != NULL);
assert(_GET_HASHTABLE(it_iter) == cpt_hashtable);

/* check for the end node */
t_vectoriterator = vector_end(&cpt_hashtable->_t_bucket);
if(_GET_VECTOR_COREPOS(t_vectoriterator) == _GET_HASHTABLE_BUCKETPOS(t_iter) &&
_GET_HASHTABLE_COREPOS(t_iter) == NULL)
it_bucket = vector_end(&cpt_hashtable->_t_bucket);
if(_GET_VECTOR_COREPOS(it_bucket) == _GET_HASHTABLE_BUCKETPOS(it_iter) &&
_GET_HASHTABLE_COREPOS(it_iter) == NULL)
{
return true;
}
else
{
_hashnode_t* pt_node = NULL;
for(t_vectoriterator = vector_begin(&cpt_hashtable->_t_bucket);
!iterator_equal(t_vectoriterator, vector_end(&cpt_hashtable->_t_bucket));
t_vectoriterator = iterator_next(t_vectoriterator))
for(it_bucket = vector_begin(&cpt_hashtable->_t_bucket);
!iterator_equal(it_bucket, vector_end(&cpt_hashtable->_t_bucket));
it_bucket = iterator_next(it_bucket))
{
if(_GET_HASHTABLE_BUCKETPOS(t_iter) ==
_GET_VECTOR_COREPOS(t_vectoriterator))
if(_GET_HASHTABLE_BUCKETPOS(it_iter) == _GET_VECTOR_COREPOS(it_bucket))
{
pt_node = *(_hashnode_t**)_GET_VECTOR_COREPOS(t_vectoriterator);
pt_node = *(_hashnode_t**)_GET_VECTOR_COREPOS(it_bucket);
while(pt_node != NULL)
{
if(pt_node == (_hashnode_t*)_GET_HASHTABLE_COREPOS(t_iter))
if(pt_node == (_hashnode_t*)_GET_HASHTABLE_COREPOS(it_iter))
{
return true;
}
Expand All @@ -132,16 +168,15 @@ bool_t _hashtable_iterator_belong_to_hashtable(
}
}

bool_t _hashtable_same_hashtable_iterator_type(
const _hashtable_t* cpt_hashtable, _hashtable_iterator_t t_iter)
/**
* Test the type that saved in the hashtable container and referenced by it_iter are same.
*/
bool_t _hashtable_same_hashtable_iterator_type(const _hashtable_t* cpt_hashtable, _hashtable_iterator_t it_iter)
{
assert(cpt_hashtable != NULL && _GET_HASHTABLE(t_iter) != NULL);
return _type_is_same(_GET_HASHTABLE_TYPE_NAME(cpt_hashtable),
_GET_HASHTABLE_TYPE_NAME(_GET_HASHTABLE(t_iter))) &&
(cpt_hashtable->_t_typeinfo._pt_type ==
_GET_HASHTABLE(t_iter)->_t_typeinfo._pt_type) &&
(cpt_hashtable->_t_typeinfo._t_style ==
_GET_HASHTABLE(t_iter)->_t_typeinfo._t_style);
assert(cpt_hashtable != NULL);
assert(_GET_HASHTABLE(it_iter) != NULL);

return _hashtable_same_type(cpt_hashtable, _GET_HASHTABLE(it_iter));
}
#endif /* NDEBUG */

Expand All @@ -150,6 +185,19 @@ bool_t _hashtable_same_type(
{
assert(cpt_hashtablefirst != NULL && cpt_hashtablesecond != NULL);

return _type_is_same(_GET_HASHTABLE_TYPE_NAME(cpt_hashtablefirst),
_GET_HASHTABLE_TYPE_NAME(cpt_hashtablesecond)) &&
(cpt_hashtablefirst->_t_typeinfo._pt_type ==
cpt_hashtablesecond->_t_typeinfo._pt_type) &&
(cpt_hashtablefirst->_t_typeinfo._t_style ==
cpt_hashtablesecond->_t_typeinfo._t_style);
}

bool_t _hashtable_same_type_ex(
const _hashtable_t* cpt_hashtablefirst, const _hashtable_t* cpt_hashtablesecond)
{
assert(cpt_hashtablefirst != NULL && cpt_hashtablesecond != NULL);

return _type_is_same(_GET_HASHTABLE_TYPE_NAME(cpt_hashtablefirst),
_GET_HASHTABLE_TYPE_NAME(cpt_hashtablesecond)) &&
(cpt_hashtablefirst->_t_typeinfo._pt_type ==
Expand Down
25 changes: 19 additions & 6 deletions src/cstl_hashtable_aux.h
Expand Up @@ -66,18 +66,31 @@ extern bool_t _hashtable_is_created(const _hashtable_t* cpt_hashtable);
*/
extern bool_t _hashtable_is_inited(const _hashtable_t* cpt_hashtable);

/*
* Assert support.
/**
* Test iterator referenced data is within the hashtable.
* @param cpt_hashtable point to hashtable.
* @param it_iter hashtable iterator.
* @return if iterator referenced is within the hashtable, then return true, otherwise return false.
* @remarks if cpt_hashtable == NULL, then the behavior is undefined, cpt_hashtable must be initialized, otherwise the
* behavior is undefined. the it_iter must be valie hashtable iterator, otherwist the behavior is undefined.
*/
extern bool_t _hashtable_iterator_belong_to_hashtable(const _hashtable_t* cpt_hashtable, _hashtable_iterator_t it_iter);

/**
* Test the type that saved in the hashtable container and referenced by it_iter are same.
* @param cpt_hashtable hashtable container.
* @param it_iter hashtable iterator.
* @return if the type is same, return true, else return false.
* @remarks if cpt_hashtable == NULL or it_iter is not hashtable iterator, then the behavior is undefined.
*/
extern bool_t _hashtable_iterator_belong_to_hashtable(
const _hashtable_t* cpt_hashtable, _hashtable_iterator_t t_iter);
extern bool_t _hashtable_same_hashtable_iterator_type(
const _hashtable_t* cpt_hashtable, _hashtable_iterator_t t_iter);
extern bool_t _hashtable_same_hashtable_iterator_type(const _hashtable_t* cpt_hashtable, _hashtable_iterator_t it_iter);

#endif /* NDEBUG */

extern bool_t _hashtable_same_type(
const _hashtable_t* cpt_hashtablefirst, const _hashtable_t* cpt_hashtablesecond);
extern bool_t _hashtable_same_type_ex(
const _hashtable_t* cpt_hashtablefirst, const _hashtable_t* cpt_hashtablesecond);

/* init, copy, less and destroy function for _hashnode_t* type */
extern void _hashnode_init(const void* cpv_input, void* pv_output);
Expand Down
25 changes: 10 additions & 15 deletions tags
Expand Up @@ -1748,7 +1748,7 @@ UT_CSTL_DEQUE_AUX_CASE test/ut/ut_cstl_deque_aux.h 235;" d
UT_CSTL_DEQUE_CASE test/ut/ut_cstl_deque.h 353;" d
UT_CSTL_DEQUE_ITERATOR_CASE test/ut/ut_cstl_deque_iterator.h 214;" d
UT_CSTL_DEQUE_PRIVATE_CASE test/ut/ut_cstl_deque_private.h 119;" d
UT_CSTL_HASHTABLE_AUX_CASE test/ut/ut_cstl_hashtable_aux.h 33;" d
UT_CSTL_HASHTABLE_AUX_CASE test/ut/ut_cstl_hashtable_aux.h 30;" d
UT_CSTL_LIST_AUX_CASE test/ut/ut_cstl_list_aux.h 196;" d
UT_CSTL_LIST_CASE test/ut/ut_cstl_list.h 556;" d
UT_CSTL_LIST_ITERATOR_CASE test/ut/ut_cstl_list_iterator.h 108;" d
Expand Down Expand Up @@ -3470,6 +3470,7 @@ _hashtable_insert_unique_range cstl/cstl_hashtable_private.h /^extern void _hash
_hashtable_insert_unique_range src/cstl_hashtable.c /^void _hashtable_insert_unique_range($/;" f signature:( _hashtable_t* pt_hashtable, _hashtable_iterator_t t_begin, _hashtable_iterator_t t_end)
_hashtable_is_created src/cstl_hashtable_aux.c /^bool_t _hashtable_is_created(const _hashtable_t* cpt_hashtable)$/;" f signature:(const _hashtable_t* cpt_hashtable)
_hashtable_is_created src/cstl_hashtable_aux.h /^extern bool_t _hashtable_is_created(const _hashtable_t* cpt_hashtable);$/;" p signature:(const _hashtable_t* cpt_hashtable)
_hashtable_is_inited src/cstl_hashtable_aux.c /^bool_t _hashtable_is_inited(const _hashtable_t* cpt_hashtable)$/;" f signature:(const _hashtable_t* cpt_hashtable)
_hashtable_is_inited src/cstl_hashtable_aux.h /^extern bool_t _hashtable_is_inited(const _hashtable_t* cpt_hashtable);$/;" p signature:(const _hashtable_t* cpt_hashtable)
_hashtable_iterator_before cstl/cstl_hashtable_iterator.h /^extern bool_t _hashtable_iterator_before($/;" p signature:( _hashtable_iterator_t t_iterfirst, _hashtable_iterator_t t_itersecond)
_hashtable_iterator_before src/cstl_hashtable.c /^bool_t _hashtable_iterator_before($/;" f signature:( _hashtable_iterator_t t_iterfirst, _hashtable_iterator_t t_itersecond)
Expand Down Expand Up @@ -11638,14 +11639,10 @@ test__hashtable_is_created__invalid_compare test/ut/ut_cstl_hashtable_aux.c /^vo
test__hashtable_is_created__invalid_compare test/ut/ut_cstl_hashtable_aux.h /^void test__hashtable_is_created__invalid_compare(void** state);$/;" p signature:(void** state)
test__hashtable_is_created__invalid_nodecount test/ut/ut_cstl_hashtable_aux.c /^void test__hashtable_is_created__invalid_nodecount(void** state)$/;" f signature:(void** state)
test__hashtable_is_created__invalid_nodecount test/ut/ut_cstl_hashtable_aux.h /^void test__hashtable_is_created__invalid_nodecount(void** state);$/;" p signature:(void** state)
test__hashtable_is_created__invalid_rbroot_color test/ut/ut_cstl_hashtable_aux.c /^void test__hashtable_is_created__invalid_rbroot_color(void** state)$/;" f signature:(void** state)
test__hashtable_is_created__invalid_rbroot_color test/ut/ut_cstl_hashtable_aux.h /^void test__hashtable_is_created__invalid_rbroot_color(void** state);$/;" p signature:(void** state)
test__hashtable_is_created__invalid_rbroot_left test/ut/ut_cstl_hashtable_aux.c /^void test__hashtable_is_created__invalid_rbroot_left(void** state)$/;" f signature:(void** state)
test__hashtable_is_created__invalid_rbroot_left test/ut/ut_cstl_hashtable_aux.h /^void test__hashtable_is_created__invalid_rbroot_left(void** state);$/;" p signature:(void** state)
test__hashtable_is_created__invalid_rbroot_parent test/ut/ut_cstl_hashtable_aux.c /^void test__hashtable_is_created__invalid_rbroot_parent(void** state)$/;" f signature:(void** state)
test__hashtable_is_created__invalid_rbroot_parent test/ut/ut_cstl_hashtable_aux.h /^void test__hashtable_is_created__invalid_rbroot_parent(void** state);$/;" p signature:(void** state)
test__hashtable_is_created__invalid_rbroot_right test/ut/ut_cstl_hashtable_aux.c /^void test__hashtable_is_created__invalid_rbroot_right(void** state)$/;" f signature:(void** state)
test__hashtable_is_created__invalid_rbroot_right test/ut/ut_cstl_hashtable_aux.h /^void test__hashtable_is_created__invalid_rbroot_right(void** state);$/;" p signature:(void** state)
test__hashtable_is_created__invalid_rbroot_bucket test/ut/ut_cstl_hashtable_aux.c /^void test__hashtable_is_created__invalid_rbroot_bucket(void** state)$/;" f signature:(void** state)
test__hashtable_is_created__invalid_rbroot_bucket test/ut/ut_cstl_hashtable_aux.h /^void test__hashtable_is_created__invalid_rbroot_bucket(void** state);$/;" p signature:(void** state)
test__hashtable_is_created__invalid_rbroot_hash test/ut/ut_cstl_hashtable_aux.c /^void test__hashtable_is_created__invalid_rbroot_hash(void** state)$/;" f signature:(void** state)
test__hashtable_is_created__invalid_rbroot_hash test/ut/ut_cstl_hashtable_aux.h /^void test__hashtable_is_created__invalid_rbroot_hash(void** state);$/;" p signature:(void** state)
test__hashtable_is_created__invalid_typeinfo_style test/ut/ut_cstl_hashtable_aux.c /^void test__hashtable_is_created__invalid_typeinfo_style(void** state)$/;" f signature:(void** state)
test__hashtable_is_created__invalid_typeinfo_style test/ut/ut_cstl_hashtable_aux.h /^void test__hashtable_is_created__invalid_typeinfo_style(void** state);$/;" p signature:(void** state)
test__hashtable_is_created__invalid_typeinfo_type test/ut/ut_cstl_hashtable_aux.c /^void test__hashtable_is_created__invalid_typeinfo_type(void** state)$/;" f signature:(void** state)
Expand All @@ -11655,14 +11652,12 @@ test__hashtable_is_created__non_inited_allocator test/ut/ut_cstl_hashtable_aux.h
test__hashtable_is_created__null_hashtable test/ut/ut_cstl_hashtable_aux.h /^void test__hashtable_is_created__null_hashtable(void** state);$/;" p signature:(void** state)
test__hashtable_is_inited__inited test/ut/ut_cstl_hashtable_aux.c /^void test__hashtable_is_inited__inited(void** state)$/;" f signature:(void** state)
test__hashtable_is_inited__inited test/ut/ut_cstl_hashtable_aux.h /^void test__hashtable_is_inited__inited(void** state);$/;" p signature:(void** state)
test__hashtable_is_inited__invalid_bucket test/ut/ut_cstl_hashtable_aux.c /^void test__hashtable_is_inited__invalid_bucket(void** state)$/;" f signature:(void** state)
test__hashtable_is_inited__invalid_bucket test/ut/ut_cstl_hashtable_aux.h /^void test__hashtable_is_inited__invalid_bucket(void** state);$/;" p signature:(void** state)
test__hashtable_is_inited__invalid_compare test/ut/ut_cstl_hashtable_aux.c /^void test__hashtable_is_inited__invalid_compare(void** state)$/;" f signature:(void** state)
test__hashtable_is_inited__invalid_compare test/ut/ut_cstl_hashtable_aux.h /^void test__hashtable_is_inited__invalid_compare(void** state);$/;" p signature:(void** state)
test__hashtable_is_inited__invalid_rbroot_color test/ut/ut_cstl_hashtable_aux.c /^void test__hashtable_is_inited__invalid_rbroot_color(void** state)$/;" f signature:(void** state)
test__hashtable_is_inited__invalid_rbroot_color test/ut/ut_cstl_hashtable_aux.h /^void test__hashtable_is_inited__invalid_rbroot_color(void** state);$/;" p signature:(void** state)
test__hashtable_is_inited__invalid_rbroot_left test/ut/ut_cstl_hashtable_aux.c /^void test__hashtable_is_inited__invalid_rbroot_left(void** state)$/;" f signature:(void** state)
test__hashtable_is_inited__invalid_rbroot_left test/ut/ut_cstl_hashtable_aux.h /^void test__hashtable_is_inited__invalid_rbroot_left(void** state);$/;" p signature:(void** state)
test__hashtable_is_inited__invalid_rbroot_right test/ut/ut_cstl_hashtable_aux.c /^void test__hashtable_is_inited__invalid_rbroot_right(void** state)$/;" f signature:(void** state)
test__hashtable_is_inited__invalid_rbroot_right test/ut/ut_cstl_hashtable_aux.h /^void test__hashtable_is_inited__invalid_rbroot_right(void** state);$/;" p signature:(void** state)
test__hashtable_is_inited__invalid_hash test/ut/ut_cstl_hashtable_aux.c /^void test__hashtable_is_inited__invalid_hash(void** state)$/;" f signature:(void** state)
test__hashtable_is_inited__invalid_hash test/ut/ut_cstl_hashtable_aux.h /^void test__hashtable_is_inited__invalid_hash(void** state);$/;" p signature:(void** state)
test__hashtable_is_inited__invalid_typeinfo_style test/ut/ut_cstl_hashtable_aux.c /^void test__hashtable_is_inited__invalid_typeinfo_style(void** state)$/;" f signature:(void** state)
test__hashtable_is_inited__invalid_typeinfo_style test/ut/ut_cstl_hashtable_aux.h /^void test__hashtable_is_inited__invalid_typeinfo_style(void** state);$/;" p signature:(void** state)
test__hashtable_is_inited__invalid_typeinfo_type test/ut/ut_cstl_hashtable_aux.c /^void test__hashtable_is_inited__invalid_typeinfo_type(void** state)$/;" f signature:(void** state)
Expand Down

0 comments on commit 08fa6c2

Please sign in to comment.