Beispiel #1
0
/**
 * Tests if the two rb tree are equal.
 */
bool_t _hashtable_equal(const _hashtable_t* cpt_first, const _hashtable_t* cpt_second)
{
    _hashtable_iterator_t it_first;
    _hashtable_iterator_t it_second;
    bool_t                b_less = false;
    bool_t                b_greater = false;

    assert(cpt_first != NULL);
    assert(cpt_second != NULL);
    assert(_hashtable_is_inited(cpt_first));
    assert(_hashtable_is_inited(cpt_second));

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

    /* check type */
    if(!_hashtable_same_type_ex(cpt_first, cpt_second))
    {
        return false;
    }
    /* check size or bucket count*/
    if(_hashtable_size(cpt_first) != _hashtable_size(cpt_second) ||
       _hashtable_bucket_count(cpt_first) != _hashtable_bucket_count(cpt_second))
    {
        return false;
    }
    /* check each element */
    for(it_first = _hashtable_begin(cpt_first),
        it_second = _hashtable_begin(cpt_second);
        !_hashtable_iterator_equal(it_first, _hashtable_end(cpt_first)) && 
        !_hashtable_iterator_equal(it_second, _hashtable_end(cpt_second));
        it_first = _hashtable_iterator_next(it_first),
        it_second = _hashtable_iterator_next(it_second))
    {
        b_less = b_greater = _GET_HASHTABLE_TYPE_SIZE(cpt_first);
        _GET_HASHTABLE_TYPE_LESS_FUNCTION(cpt_first)(
                ((_hashnode_t*)_GET_HASHTABLE_COREPOS(it_first))->_pby_data,
                ((_hashnode_t*)_GET_HASHTABLE_COREPOS(it_second))->_pby_data, &b_less);
        _GET_HASHTABLE_TYPE_LESS_FUNCTION(cpt_first)(
                ((_hashnode_t*)_GET_HASHTABLE_COREPOS(it_second))->_pby_data,
                ((_hashnode_t*)_GET_HASHTABLE_COREPOS(it_first))->_pby_data, &b_greater);
        if(b_less || b_greater)
        {
            return false;
        }
    }

    assert(_hashtable_iterator_equal(it_first, _hashtable_end(cpt_first)) &&
           _hashtable_iterator_equal(it_second, _hashtable_end(cpt_second)));

    return true;
}
Beispiel #2
0
/**
 * Tests if the first rb tree is less than the second rb tree.
 */
bool_t _hashtable_less(const _hashtable_t* cpt_first, const _hashtable_t* cpt_second)
{
    _hashtable_iterator_t it_first;
    _hashtable_iterator_t it_second;
    bool_t                b_less = false;
    bool_t                b_greater = false;

    assert(cpt_first != NULL);
    assert(cpt_second != NULL);
    assert(_hashtable_is_inited(cpt_first));
    assert(_hashtable_is_inited(cpt_second));
    assert(_hashtable_same_type_ex(cpt_first, cpt_second));

    /* check vector bucket count */
    if(vector_size(&cpt_first->_vec_bucket) == vector_size(&cpt_second->_vec_bucket))
    {
        /* check each element */
        for(it_first = _hashtable_begin(cpt_first),
            it_second = _hashtable_begin(cpt_second);
            !_hashtable_iterator_equal(it_first, _hashtable_end(cpt_first)) && 
            !_hashtable_iterator_equal(it_second, _hashtable_end(cpt_second));
            it_first = _hashtable_iterator_next(it_first),
            it_second = _hashtable_iterator_next(it_second))
        {
            b_less = _GET_HASHTABLE_TYPE_SIZE(cpt_first);
            _GET_HASHTABLE_TYPE_LESS_FUNCTION(cpt_first)(
                    ((_hashnode_t*)_GET_HASHTABLE_COREPOS(it_first))->_pby_data,
                    ((_hashnode_t*)_GET_HASHTABLE_COREPOS(it_second))->_pby_data, &b_less);
            if(b_less)
            {
                return true;
            }
            b_greater = _GET_HASHTABLE_TYPE_SIZE(cpt_first);
            _GET_HASHTABLE_TYPE_LESS_FUNCTION(cpt_first)(
                    ((_hashnode_t*)_GET_HASHTABLE_COREPOS(it_second))->_pby_data,
                    ((_hashnode_t*)_GET_HASHTABLE_COREPOS(it_first))->_pby_data, &b_greater);
            if(b_greater)
            {
                return false;
            }
        }
    }

    return _hashtable_size(cpt_first) < _hashtable_size(cpt_second) ? true : false;
}
/**
 * Element compare function auxiliary
 */
void _hashtable_elem_compare_auxiliary(
    const _hashtable_t* cpt_hashtable, const void* cpv_first, const void* cpv_second, void* pv_output)
{
    assert(cpt_hashtable != NULL);
    assert(cpv_first != NULL);
    assert(cpv_second != NULL);
    assert(pv_output != NULL);
    assert(_hashtable_is_inited(cpt_hashtable));

    if (strncmp(_GET_HASHTABLE_TYPE_NAME(cpt_hashtable), _C_STRING_TYPE, _TYPE_NAME_SIZE) == 0 &&
        cpt_hashtable->_bfun_compare != _GET_HASHTABLE_TYPE_LESS_FUNCTION(cpt_hashtable)) {
        cpt_hashtable->_bfun_compare(string_c_str((string_t*)cpv_first), string_c_str((string_t*)cpv_second), pv_output);
    } else {
        cpt_hashtable->_bfun_compare(cpv_first, cpv_second, pv_output);
    }
}
Beispiel #4
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);
}