/**
 * Calculate distance between two iterators.
 */
int _hashtable_iterator_distance(_hashtable_iterator_t it_first, _hashtable_iterator_t it_second)
{
    _hashtable_iterator_t it_iter;
    int                   n_distance = 0;

    if(_hashtable_iterator_before(it_first, it_second))
    {
        for(it_iter = it_first;
            !_hashtable_iterator_equal(it_iter, it_second);
            it_iter = _hashtable_iterator_next(it_iter))
        {
            n_distance++;
        }
        return n_distance;
    }
    else if(_hashtable_iterator_before(it_second, it_first))
    {
        for(it_iter = it_second;
            !_hashtable_iterator_equal(it_iter, it_first);
            it_iter = _hashtable_iterator_next(it_iter))
        {
            n_distance++;
        }
        return -n_distance;
    }
    else
    {
        return 0;
    }
}
示例#2
0
/*
 * Erase a range of element in an hashtable.
 */
void _hashtable_erase_range(
    _hashtable_t* pt_hashtable, _hashtable_iterator_t it_begin, _hashtable_iterator_t it_end)
{
    _hashtable_iterator_t it_iter;
    _hashtable_iterator_t it_next;

    assert(pt_hashtable != NULL);
    assert(_hashtable_is_inited(pt_hashtable));
    assert(_hashtable_iterator_belong_to_hashtable(pt_hashtable, it_begin));
    assert(_hashtable_iterator_belong_to_hashtable(pt_hashtable, it_end));
    assert(_hashtable_iterator_equal(it_begin, it_end) || _hashtable_iterator_before(it_begin, it_end));

    it_iter = it_next = it_begin;
    if(!_hashtable_iterator_equal(it_next, _hashtable_end(pt_hashtable)))
    {
        it_next = _hashtable_iterator_next(it_next);
    }
    while(!_hashtable_iterator_equal(it_iter, it_end))
    {
        _hashtable_erase_pos(pt_hashtable, it_iter);

        it_iter = it_next;
        if(!_hashtable_iterator_equal(it_next, _hashtable_end(pt_hashtable)))
        {
            it_next = _hashtable_iterator_next(it_next);
        }
    }
}
/**
 * Test the first iterator is before the second.
 */
bool_t _hash_set_iterator_before(hash_set_iterator_t it_first, hash_set_iterator_t it_second)
{
    assert(_GET_HASH_SET_CONTAINER_TYPE(it_first) == _HASH_SET_CONTAINER);
    assert(_GET_HASH_SET_ITERATOR_TYPE(it_first) == _BIDIRECTIONAL_ITERATOR);
    assert(_GET_HASH_SET_CONTAINER_TYPE(it_second) == _HASH_SET_CONTAINER);
    assert(_GET_HASH_SET_ITERATOR_TYPE(it_second) == _BIDIRECTIONAL_ITERATOR);
    assert(_GET_HASH_SET_CONTAINER(it_first) == _GET_HASH_SET_CONTAINER(it_second));

    return _hashtable_iterator_before(it_first, it_second);
}
示例#4
0
/**
 * Inserts an range of unique element into a hashtable.
 */
void _hashtable_insert_unique_range(
    _hashtable_t* pt_hashtable, _hashtable_iterator_t it_begin, _hashtable_iterator_t it_end)
{
    _hashtable_iterator_t it_iter;

    assert(pt_hashtable != NULL);
    assert(_hashtable_is_inited(pt_hashtable));
    assert(_hashtable_same_hashtable_iterator_type(pt_hashtable, it_begin));
    assert(_hashtable_same_hashtable_iterator_type(pt_hashtable, it_end));
    assert(_hashtable_iterator_equal(it_begin, it_end) || _hashtable_iterator_before(it_begin, it_end));

    for(it_iter = it_begin; !_hashtable_iterator_equal(it_iter, it_end); it_iter = _hashtable_iterator_next(it_iter))
    {
        _hashtable_insert_unique(pt_hashtable, ((_hashnode_t*)_GET_HASHTABLE_COREPOS(it_iter))->_pby_data);
    }
}
示例#5
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);
    }
}