Example #1
0
/**
 * Test the type that saved in the hashtable container is same.
 */
bool_t _hashtable_same_type(const _hashtable_t* cpt_first, const _hashtable_t* cpt_second)
{
    assert(cpt_first != NULL);
    assert(cpt_second != NULL);
    assert(_hashtable_is_inited(cpt_first) || _hashtable_is_created(cpt_first));
    assert(_hashtable_is_inited(cpt_second) || _hashtable_is_created(cpt_second));

    if (cpt_first == cpt_second) {
        return true;
    }

    return (cpt_first->_t_typeinfo._pt_type == cpt_second->_t_typeinfo._pt_type) &&
           (cpt_first->_t_typeinfo._t_style == cpt_second->_t_typeinfo._t_style) &&
           _type_is_same(_GET_HASHTABLE_TYPE_NAME(cpt_first), _GET_HASHTABLE_TYPE_NAME(cpt_second));
}
Example #2
0
/**
 * Erases all the elements of an hashtable.
 */
void _hashtable_clear(_hashtable_t* pt_hashtable)
{
    size_t       t_bucketcount = 0;
    size_t       i = 0;
    _hashnode_t* pt_node = NULL;
    _hashnode_t* pt_deletion = NULL;
    bool_t       b_result = false;

    assert(pt_hashtable != NULL);
    assert(_hashtable_is_inited(pt_hashtable) || _hashtable_is_created(pt_hashtable));

    t_bucketcount = vector_size(&pt_hashtable->_vec_bucket);
    /* iterator all bucket node */
    for(i = 0; i < t_bucketcount; ++i)
    {
        /* iterator all element list for one bucket node */
        pt_node = *(_hashnode_t**)vector_at(&pt_hashtable->_vec_bucket, i);
        *(_hashnode_t**)vector_at(&pt_hashtable->_vec_bucket, i) = NULL;
        while(pt_node != NULL)
        {
            /* delete each element */
            pt_deletion = pt_node;
            pt_node = pt_node->_pt_next;

            /* destroy element */
            b_result = _GET_HASHTABLE_TYPE_SIZE(pt_hashtable);
            _GET_HASHTABLE_TYPE_DESTROY_FUNCTION(pt_hashtable)(pt_deletion->_pby_data, &b_result);
            assert(b_result);
            _alloc_deallocate(&pt_hashtable->_t_allocator,  pt_deletion, 
                _HASHTABLE_NODE_SIZE(_GET_HASHTABLE_TYPE_SIZE(pt_hashtable)), 1);
        }
    }
    
    pt_hashtable->_t_nodecount = 0;
}
Example #3
0
/**
 * Test the type that saved in the hashtable container and referenced by it_iter are same.
 */
bool_t _hashtable_same_iterator_type(const _hashtable_t* cpt_hashtable, iterator_t it_iter)
{
    assert(cpt_hashtable != NULL);
    assert(_hashtable_is_inited(cpt_hashtable) || _hashtable_is_created(cpt_hashtable));
    assert(_iterator_is_valid(it_iter));

    return _type_is_same_ex(&cpt_hashtable->_t_typeinfo, _iterator_get_typeinfo(it_iter));
}
Example #4
0
/**
 * Test the type and compare function that saved in the hashtable container is same.
 */
bool_t _hashtable_same_type_ex(const _hashtable_t* cpt_first, const _hashtable_t* cpt_second)
{
    assert(cpt_first != NULL);
    assert(cpt_second != NULL);
    assert(_hashtable_is_inited(cpt_first) || _hashtable_is_created(cpt_first));
    assert(_hashtable_is_inited(cpt_second) || _hashtable_is_created(cpt_second));

    if (cpt_first == cpt_second) {
        return true;
    }

    return (cpt_first->_t_typeinfo._pt_type == cpt_second->_t_typeinfo._pt_type) &&
           (cpt_first->_t_typeinfo._t_style == cpt_second->_t_typeinfo._t_style) &&
           (cpt_first->_ufun_hash == cpt_second->_ufun_hash) &&
           (cpt_first->_bfun_compare == cpt_second->_bfun_compare) &&
           vector_size(&cpt_first->_vec_bucket) == vector_size(&cpt_second->_vec_bucket) &&
           _type_is_same(_GET_HASHTABLE_TYPE_NAME(cpt_first), _GET_HASHTABLE_TYPE_NAME(cpt_second));
}
Example #5
0
/**
 * Initialize hashtable container with specific array.
 */
void _hashtable_init_copy_unique_array(
    _hashtable_t* pt_dest, const void* cpv_array, size_t t_count, size_t t_bucketcount,
    ufun_t ufun_hash, bfun_t bfun_compare)
{
    assert(pt_dest != NULL);
    assert(_hashtable_is_created(pt_dest));
    assert(cpv_array != NULL);

    _hashtable_init(pt_dest, t_bucketcount, ufun_hash, bfun_compare);
    _hashtable_insert_unique_array(pt_dest, cpv_array, t_count);
}
Example #6
0
/**
 * Initialize hashtable container with specific range.
 */
void _hashtable_init_copy_unique_range(
    _hashtable_t* pt_dest, iterator_t it_begin, iterator_t it_end, size_t t_bucketcount,
    ufun_t ufun_hash, bfun_t bfun_compare)
{
    assert(pt_dest != NULL);
    assert(_hashtable_is_created(pt_dest));
    assert(_hashtable_same_iterator_type(pt_dest, it_begin));
    assert(_hashtable_same_iterator_type(pt_dest, it_end));
    assert(iterator_equal(it_begin, it_end) || _iterator_before(it_begin, it_end));

    _hashtable_init(pt_dest, t_bucketcount, ufun_hash, bfun_compare);
    _hashtable_insert_unique_range(pt_dest, it_begin, it_end);
}
Example #7
0
/**
 * Initialize hashtable container with hashtable.
 */
void _hashtable_init_copy(_hashtable_t* pt_dest, const _hashtable_t* cpt_src)
{
    assert(pt_dest != NULL);
    assert(cpt_src != NULL);
    assert(_hashtable_is_created(pt_dest));
    assert(_hashtable_is_inited(cpt_src));
    assert(_hashtable_same_type(pt_dest, cpt_src));

    /* initialize the dest hashtable with src hashtable attribute */
    _hashtable_init(pt_dest, _hashtable_bucket_count(cpt_src), cpt_src->_ufun_hash, cpt_src->_bfun_compare);
    /* insert node from src to dest */
    if(!_hashtable_empty(cpt_src))
    {
        _hashtable_insert_equal_range(pt_dest, _hashtable_begin(cpt_src), _hashtable_end(cpt_src));
    }
}
Example #8
0
/**
 * Initialize hashtable container with specific range.
 */
void _hashtable_init_copy_range(
    _hashtable_t* pt_dest, _hashtable_iterator_t it_begin, _hashtable_iterator_t it_end,
    size_t t_bucketcount, unary_function_t ufun_hash, binary_function_t bfun_compare)
{
    assert(pt_dest != NULL);
    assert(_hashtable_is_created(pt_dest));
    assert(_hashtable_same_hashtable_iterator_type(pt_dest, it_begin));
    assert(_hashtable_same_hashtable_iterator_type(pt_dest, it_end));
    assert(_hashtable_iterator_equal(it_begin, it_end) || _hashtable_iterator_before(it_begin, it_end));

    /* initialize the dest hashtable with src hashtable attribute */
    _hashtable_init(pt_dest, t_bucketcount, ufun_hash, bfun_compare);
    /* insert node from src to dest */
    if(!_hashtable_empty(_GET_HASHTABLE(it_begin)))
    {
        _hashtable_insert_equal_range(pt_dest, it_begin, it_end);
    }
}
Example #9
0
/**
 * 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);
}
Example #10
0
/**
 * Initialize element auxiliary function
 */
void _hashtable_init_elem_auxiliary(_hashtable_t* pt_hashtable, _hashnode_t* pt_node)
{
    assert(pt_hashtable != NULL);
    assert(pt_node != NULL);
    assert(_hashtable_is_inited(pt_hashtable) || _hashtable_is_created(pt_hashtable));

    /* initialize new elements */
    if (_GET_HASHTABLE_TYPE_STYLE(pt_hashtable) == _TYPE_CSTL_BUILTIN) {
        /* get element type name */
        char s_elemtypename[_TYPE_NAME_SIZE + 1];
        _type_get_elem_typename(_GET_HASHTABLE_TYPE_NAME(pt_hashtable), s_elemtypename);

        _GET_HASHTABLE_TYPE_INIT_FUNCTION(pt_hashtable)(pt_node->_pby_data, s_elemtypename);
    } else {
        bool_t b_result = _GET_HASHTABLE_TYPE_SIZE(pt_hashtable);
        _GET_HASHTABLE_TYPE_INIT_FUNCTION(pt_hashtable)(pt_node->_pby_data, &b_result);
        assert(b_result);
    }
}
Example #11
0
/**
 * Initialize hashtable container with hashtable.
 */
void _hashtable_init_copy(_hashtable_t* pt_dest, const _hashtable_t* cpt_src)
{
    _hashtable_iterator_t it_iter;

    assert(pt_dest != NULL);
    assert(cpt_src != NULL);
    assert(_hashtable_is_created(pt_dest));
    assert(_hashtable_is_inited(cpt_src));
    assert(_hashtable_same_type(pt_dest, cpt_src));

    /* initialize the dest hashtable with src hashtable attribute */
    _hashtable_init(pt_dest, _hashtable_bucket_count(cpt_src), cpt_src->_ufun_hash, cpt_src->_bfun_compare);
    /* insert node from src to dest */
    for (it_iter = _hashtable_begin(cpt_src);
         !_hashtable_iterator_equal(it_iter, _hashtable_end(cpt_src));
         it_iter = _hashtable_iterator_next(it_iter)) {
        _hashtable_insert_equal(pt_dest, _hashtable_iterator_get_pointer_ignore_cstr(it_iter));
    }
}