Exemple #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;
}
Exemple #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;
}
/**
 * Return iterator reference previous element.
 */
_hashtable_iterator_t _hashtable_iterator_prev(_hashtable_iterator_t it_iter)
{
    vector_iterator_t it_bucket;
    _hashnode_t*      pt_node = NULL;
    _hashnode_t*      pt_prevnode = NULL;

    assert(_hashtable_iterator_belong_to_hashtable(_HASHTABLE_ITERATOR_HASHTABLE(it_iter), it_iter));
    assert(!_hashtable_iterator_equal(it_iter, _hashtable_begin(_HASHTABLE_ITERATOR_HASHTABLE(it_iter))));

	/* hashtable end is specifical condition. corepos == NULL and bucketpos is invalid pos */
	if(_hashtable_iterator_equal(it_iter, _hashtable_end(_HASHTABLE_ITERATOR_HASHTABLE(it_iter))))
	{
		it_bucket = iterator_prev(vector_end(&_HASHTABLE_ITERATOR_HASHTABLE(it_iter)->_vec_bucket));
		_HASHTABLE_ITERATOR_BUCKETPOS(it_iter) = _VECTOR_ITERATOR_COREPOS(it_bucket);
	}

    pt_node = (_hashnode_t*)_HASHTABLE_ITERATOR_COREPOS(it_iter);
	pt_prevnode = *(_hashnode_t**)_HASHTABLE_ITERATOR_BUCKETPOS(it_iter);

	/* current node is first node in current bucket */
	if(pt_prevnode == pt_node)
	{
		it_bucket = vector_begin(&(_HASHTABLE_ITERATOR_HASHTABLE(it_iter)->_vec_bucket));
		_VECTOR_ITERATOR_COREPOS(it_bucket) = _HASHTABLE_ITERATOR_BUCKETPOS(it_iter);
		for(it_bucket = iterator_prev(it_bucket);
			iterator_greater_equal(it_bucket, vector_begin(&(_HASHTABLE_ITERATOR_HASHTABLE(it_iter)->_vec_bucket)));
			it_bucket = iterator_prev(it_bucket))
		{
			pt_node = *(_hashnode_t**)_VECTOR_ITERATOR_COREPOS(it_bucket);
			if(pt_node != NULL)
			{
				/* get the last hashnode */
				while(pt_node->_pt_next != NULL)
				{
					pt_node = pt_node->_pt_next;
				}
				/* set bucket pos and core pos */
				_HASHTABLE_ITERATOR_BUCKETPOS(it_iter) = _VECTOR_ITERATOR_COREPOS(it_bucket);
				_HASHTABLE_ITERATOR_COREPOS(it_iter) = (_byte_t*)pt_node;
				break;
			}
		}
	}
	else
	{
		while(pt_prevnode->_pt_next != pt_node)
		{
			pt_prevnode = pt_prevnode->_pt_next;
		}
		/* set core pos */
		_HASHTABLE_ITERATOR_COREPOS(it_iter) = (_byte_t*)pt_prevnode;
	}

    return it_iter;
}
Exemple #4
0
/**
 * Return an iterator that addresses the first element in the hash_set.
 */
hash_set_iterator_t hash_set_begin(const hash_set_t* cphset_set)
{
    hash_set_iterator_t it_begin;

    assert(cphset_set != NULL);

    it_begin = _hashtable_begin(&cphset_set->_t_hashtable);

    _ITERATOR_CONTAINER(it_begin) = (hash_set_t*)cphset_set;
    _HASH_SET_ITERATOR_CONTAINER_TYPE(it_begin) = _HASH_SET_CONTAINER;
    _HASH_SET_ITERATOR_ITERATOR_TYPE(it_begin) = _BIDIRECTIONAL_ITERATOR;

    return it_begin;
}
/**
 * Return an iterator that addresses the first element in the hash_multimap.
 */
hash_multimap_iterator_t hash_multimap_begin(const hash_multimap_t* cphmmap_map)
{
    hash_multimap_iterator_t it_begin;

    assert(cphmmap_map != NULL);
    assert(_pair_is_inited(&cphmmap_map->_pair_temp));

    it_begin = _hashtable_begin(&cphmmap_map->_t_hashtable);

    _GET_CONTAINER(it_begin) = (hash_multimap_t*)cphmmap_map;
    _GET_HASH_MULTIMAP_CONTAINER_TYPE(it_begin) = _HASH_MULTIMAP_CONTAINER;
    _GET_HASH_MULTIMAP_ITERATOR_TYPE(it_begin) = _BIDIRECTIONAL_ITERATOR;

    return it_begin;
}
Exemple #6
0
/**
 * Assign hashtable container.
 */
void _hashtable_assign(_hashtable_t* pt_dest, const _hashtable_t* cpt_src)
{
    assert(pt_dest != NULL);
    assert(cpt_src != NULL);
    assert(_hashtable_is_inited(pt_dest));
    assert(_hashtable_is_inited(cpt_src));
    assert(_hashtable_same_type_ex(pt_dest, cpt_src));

    /* clear all elements */
    _hashtable_clear(pt_dest);
    /* 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));
    }
}
Exemple #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));
    }
}
Exemple #8
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));
    }
}
Exemple #9
0
/**
 * Assign hashtable container.
 */
void _hashtable_assign(_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_inited(pt_dest));
    assert(_hashtable_is_inited(cpt_src));
    assert(_hashtable_same_type_ex(pt_dest, cpt_src));

    /* clear all elements */
    _hashtable_clear(pt_dest);
    /* 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));
    }
}