Exemple #1
0
/**
 * Return an iterator that addresses the first element in the hashtable.
 */
_hashtable_iterator_t _hashtable_begin(const _hashtable_t* cpt_hashtable)
{
    vector_iterator_t     it_bucket;
    _hashtable_iterator_t it_iter = _create_hashtable_iterator();

    assert(cpt_hashtable != NULL);
    assert(_hashtable_is_inited(cpt_hashtable));

    for (it_bucket = vector_begin(&cpt_hashtable->_vec_bucket);
         !iterator_equal(it_bucket, vector_end(&cpt_hashtable->_vec_bucket));
         it_bucket = iterator_next(it_bucket)) {
        _HASHTABLE_ITERATOR_BUCKETPOS(it_iter) = _VECTOR_ITERATOR_COREPOS(it_bucket);
        if (*(_hashnode_t**)_HASHTABLE_ITERATOR_BUCKETPOS(it_iter) != NULL) {
            _HASHTABLE_ITERATOR_COREPOS(it_iter) = (_byte_t*)*(_hashnode_t**)_HASHTABLE_ITERATOR_BUCKETPOS(it_iter);
            break;
        }
    }
    if (iterator_equal(it_bucket, vector_end(&cpt_hashtable->_vec_bucket))) {
        assert(_HASHTABLE_ITERATOR_COREPOS(it_iter) == NULL);
        _HASHTABLE_ITERATOR_BUCKETPOS(it_iter) = _VECTOR_ITERATOR_COREPOS(it_bucket);
    }
    _HASHTABLE_ITERATOR_HASHTABLE_POINTER(it_iter) = (_hashtable_t*)cpt_hashtable;

    return it_iter;
}
/**
 * Create hashtable iterator.
 */
_hashtable_iterator_t _create_hashtable_iterator(void)
{
    _hashtable_iterator_t it_iter;

    _HASHTABLE_ITERATOR_BUCKETPOS(it_iter) = NULL;
    _HASHTABLE_ITERATOR_COREPOS(it_iter) = NULL;
    _HASHTABLE_ITERATOR_HASHTABLE_POINTER(it_iter) = NULL;

    _ITERATOR_CONTAINER(it_iter) = NULL;

    return it_iter;
}
Exemple #3
0
/**
 * Return an iterator that addresses the location succeeding the last element in the hashtable.
 */
_hashtable_iterator_t _hashtable_end(const _hashtable_t* cpt_hashtable)
{
    vector_iterator_t     it_bucket;
    _hashtable_iterator_t it_iter = _create_hashtable_iterator();

    assert(cpt_hashtable != NULL);
    assert(_hashtable_is_inited(cpt_hashtable));

    it_bucket = vector_end(&cpt_hashtable->_vec_bucket);
    _HASHTABLE_ITERATOR_BUCKETPOS(it_iter) = _VECTOR_ITERATOR_COREPOS(it_bucket);
    _HASHTABLE_ITERATOR_COREPOS(it_iter) = NULL;
    _HASHTABLE_ITERATOR_HASHTABLE_POINTER(it_iter) = (_hashtable_t*)cpt_hashtable;

    return it_iter;
}
Exemple #4
0
/**
 * Find specific element.
 */
_hashtable_iterator_t _hashtable_find(const _hashtable_t* cpt_hashtable, const void* cpv_value)
{
    _hashtable_iterator_t it_iter = _create_hashtable_iterator();
    size_t                t_bucketcount = 0;
    _hashnode_t*          pt_node = NULL;
    _hashnode_t**         ppt_bucket = NULL;
    size_t                t_tmp = 0;
    size_t                t_pos = 0;
    bool_t                b_less = false;
    bool_t                b_greater = false;

    assert(cpt_hashtable != NULL);
    assert(cpv_value != NULL);
    assert(_hashtable_is_inited(cpt_hashtable));

    t_bucketcount = _hashtable_bucket_count(cpt_hashtable);
    t_tmp = _GET_HASHTABLE_TYPE_SIZE(cpt_hashtable);
    _hashtable_hash_auxiliary(cpt_hashtable, cpv_value, &t_tmp);
    t_pos = t_tmp % t_bucketcount;
    ppt_bucket = (_hashnode_t**)vector_at(&cpt_hashtable->_vec_bucket, t_pos);
    pt_node = *ppt_bucket;

    while (pt_node != NULL) {
        b_less = b_greater = _GET_HASHTABLE_TYPE_SIZE(cpt_hashtable);
        _hashtable_elem_compare_auxiliary(cpt_hashtable, pt_node->_pby_data, cpv_value, &b_less);
        _hashtable_elem_compare_auxiliary(cpt_hashtable, cpv_value, pt_node->_pby_data, &b_greater);
        if (b_less || b_greater) {
            pt_node = pt_node->_pt_next;
        } else {
            break;
        }
    }

    if (pt_node == NULL) {
        return _hashtable_end(cpt_hashtable);
    } else {
        _HASHTABLE_ITERATOR_BUCKETPOS(it_iter) = (_byte_t*)ppt_bucket;
        _HASHTABLE_ITERATOR_COREPOS(it_iter) = (_byte_t*)pt_node;
        _HASHTABLE_ITERATOR_HASHTABLE_POINTER(it_iter) = (_hashtable_t*)cpt_hashtable;

        return it_iter;
    }
}
Exemple #5
0
/**
 * Return an iterator range that is equal to a specific element.
 */
range_t _hashtable_equal_range(const _hashtable_t* cpt_hashtable, const void* cpv_value)
{
    range_t       r_result;
    size_t        t_bucketcount = 0;
    _hashnode_t*  pt_begin = NULL;
    _hashnode_t*  pt_end = NULL;
    _hashnode_t** ppt_bucket = NULL;
    size_t        t_tmp = 0;
    size_t        t_pos = 0;
    bool_t        b_less = false;
    bool_t        b_greater = false;
    size_t        i = 0;

    assert(cpt_hashtable != NULL);
    assert(cpv_value != NULL);
    assert(_hashtable_is_inited(cpt_hashtable));

    r_result.it_begin = _create_hashtable_iterator();
    r_result.it_end = _create_hashtable_iterator();

    t_bucketcount = _hashtable_bucket_count(cpt_hashtable);
    t_tmp = _GET_HASHTABLE_TYPE_SIZE(cpt_hashtable);
    _hashtable_hash_auxiliary(cpt_hashtable, cpv_value, &t_tmp);
    t_pos = t_tmp % t_bucketcount;
    ppt_bucket = (_hashnode_t**)vector_at(&cpt_hashtable->_vec_bucket, t_pos);

    for (pt_begin = *ppt_bucket; pt_begin != NULL; pt_begin = pt_begin->_pt_next) {
        b_less = b_greater = _GET_HASHTABLE_TYPE_SIZE(cpt_hashtable);
        _hashtable_elem_compare_auxiliary(cpt_hashtable, pt_begin->_pby_data, cpv_value, &b_less);
        _hashtable_elem_compare_auxiliary(cpt_hashtable, cpv_value, pt_begin->_pby_data, &b_greater);
        if (!b_less && !b_greater) {
            for (pt_end = pt_begin->_pt_next; pt_end != NULL; pt_end = pt_end->_pt_next) {
                b_less = b_greater = _GET_HASHTABLE_TYPE_SIZE(cpt_hashtable);
                _hashtable_elem_compare_auxiliary(cpt_hashtable, pt_end->_pby_data, cpv_value, &b_less);
                _hashtable_elem_compare_auxiliary(cpt_hashtable, cpv_value, pt_end->_pby_data, &b_greater);
                if (b_less || b_greater) {
                    _HASHTABLE_ITERATOR_BUCKETPOS(r_result.it_begin) = (_byte_t*)ppt_bucket;
                    _HASHTABLE_ITERATOR_COREPOS(r_result.it_begin) = (_byte_t*)pt_begin;
                    _HASHTABLE_ITERATOR_HASHTABLE_POINTER(r_result.it_begin) = (_hashtable_t*)cpt_hashtable;

                    _HASHTABLE_ITERATOR_BUCKETPOS(r_result.it_end) = (_byte_t*)ppt_bucket;
                    _HASHTABLE_ITERATOR_COREPOS(r_result.it_end) = (_byte_t*)pt_end;
                    _HASHTABLE_ITERATOR_HASHTABLE_POINTER(r_result.it_end) = (_hashtable_t*)cpt_hashtable;

                    return r_result;
                }
            }
            
            _HASHTABLE_ITERATOR_BUCKETPOS(r_result.it_begin) = (_byte_t*)ppt_bucket;
            _HASHTABLE_ITERATOR_COREPOS(r_result.it_begin) = (_byte_t*)pt_begin;
            _HASHTABLE_ITERATOR_HASHTABLE_POINTER(r_result.it_begin) = (_hashtable_t*)cpt_hashtable;

            for (i = t_pos + 1; i < t_bucketcount; ++i) {
                ppt_bucket = (_hashnode_t**)vector_at(&cpt_hashtable->_vec_bucket, i);
                pt_end = *ppt_bucket;
                if (pt_end != NULL) {
                    _HASHTABLE_ITERATOR_BUCKETPOS(r_result.it_end) = (_byte_t*)ppt_bucket;
                    _HASHTABLE_ITERATOR_COREPOS(r_result.it_end) = (_byte_t*)pt_end;
                    _HASHTABLE_ITERATOR_HASHTABLE_POINTER(r_result.it_end) = (_hashtable_t*)cpt_hashtable;

                    return r_result;
                }
            }

            r_result.it_end = _hashtable_end(cpt_hashtable);

            return r_result;
        }
    }

    r_result.it_begin = _hashtable_end(cpt_hashtable);
    r_result.it_end = _hashtable_end(cpt_hashtable);

    return r_result;
}
Exemple #6
0
/**
 * Inserts an element into a hashtable.
 */
_hashtable_iterator_t _hashtable_insert_equal(_hashtable_t* pt_hashtable, const void* cpv_value)
{
    size_t                t_bucketcount = 0;
    _hashnode_t*          pt_node = NULL;
    _hashnode_t*          pt_cur = NULL;
    _hashnode_t**         ppt_nodelist = NULL;
    _hashtable_iterator_t it_iter = _create_hashtable_iterator();
    bool_t                b_result = false;
    size_t                t_tmp = 0;
    size_t                t_pos = 0;
    bool_t                b_less = false;
    bool_t                b_greater = false;

    assert(pt_hashtable != NULL);
    assert(cpv_value != NULL);
    assert(_hashtable_is_inited(pt_hashtable));

    /* resize */
    _hashtable_resize(pt_hashtable, _hashtable_size(pt_hashtable) + 1);

    /* allocate node */
    pt_node = _alloc_allocate(
        &pt_hashtable->_t_allocator, _HASHTABLE_NODE_SIZE(_GET_HASHTABLE_TYPE_SIZE(pt_hashtable)), 1);
    assert(pt_node != NULL);
    _hashtable_init_elem_auxiliary(pt_hashtable, pt_node);
    b_result = _GET_HASHTABLE_TYPE_SIZE(pt_hashtable);
    _GET_HASHTABLE_TYPE_COPY_FUNCTION(pt_hashtable)(pt_node->_pby_data, cpv_value, &b_result);
    assert(b_result);

    /* hash */
    t_bucketcount = _hashtable_bucket_count(pt_hashtable);
    t_tmp = _GET_HASHTABLE_TYPE_SIZE(pt_hashtable);
    _hashtable_hash_auxiliary(pt_hashtable, pt_node->_pby_data, &t_tmp);
    t_pos = t_tmp % t_bucketcount;

    /* insert node into hashtable, note the node has same value together */
    ppt_nodelist = (_hashnode_t**)vector_at(&pt_hashtable->_vec_bucket, t_pos);
    assert(ppt_nodelist != NULL);
    pt_cur = *ppt_nodelist;
    if (pt_cur == NULL) {
        pt_node->_pt_next = pt_cur;
        *ppt_nodelist = pt_node;
    } else {
        b_less = b_greater = _GET_HASHTABLE_TYPE_SIZE(pt_hashtable);
        _hashtable_elem_compare_auxiliary(pt_hashtable, pt_cur->_pby_data, pt_node->_pby_data, &b_less);
        _hashtable_elem_compare_auxiliary(pt_hashtable, pt_node->_pby_data, pt_cur->_pby_data, &b_greater);
        if (!b_less && !b_greater) {
            pt_node->_pt_next = pt_cur;
            *ppt_nodelist = pt_node;
        } else {
            while (pt_cur->_pt_next != NULL) {
                b_less = b_greater = _GET_HASHTABLE_TYPE_SIZE(pt_hashtable);
                _hashtable_elem_compare_auxiliary(
                    pt_hashtable, pt_cur->_pt_next->_pby_data, pt_node->_pby_data, &b_less);
                _hashtable_elem_compare_auxiliary(
                    pt_hashtable, pt_node->_pby_data, pt_cur->_pt_next->_pby_data, &b_greater);

                if (b_less || b_greater) {
                    pt_cur = pt_cur->_pt_next;
                } else {
                    break;
                }
            } 
            pt_node->_pt_next = pt_cur->_pt_next;
            pt_cur->_pt_next = pt_node;
        }
    }
    pt_hashtable->_t_nodecount++;

    _HASHTABLE_ITERATOR_BUCKETPOS(it_iter) = (_byte_t*)ppt_nodelist;
    _HASHTABLE_ITERATOR_COREPOS(it_iter) = (_byte_t*)pt_node;
    _HASHTABLE_ITERATOR_HASHTABLE_POINTER(it_iter) = pt_hashtable;

    return it_iter;
}