示例#1
0
/**
 * Inserts an unique element into a set with hint.
 */
set_iterator_t _set_insert_hint_varg(set_t* pset_set, set_iterator_t it_hint, va_list val_elemlist)
{
    void* pv_varg = NULL;

    assert(pset_set != NULL);

    pv_varg = _alloc_allocate(&pset_set->_t_tree._t_allocator, _GET_SET_TYPE_SIZE(pset_set), 1);
    assert(pv_varg != NULL);
    _set_get_varg_value_auxiliary(pset_set, val_elemlist, pv_varg);

#ifdef CSTL_SET_AVL_TREE
    it_hint = _avl_tree_insert_unique(&pset_set->_t_tree, pv_varg);
#else
    it_hint = _rb_tree_insert_unique(&pset_set->_t_tree, pv_varg);
#endif

    _set_destroy_varg_value_auxiliary(pset_set, pv_varg);
    _alloc_deallocate(&pset_set->_t_tree._t_allocator, pv_varg, _GET_SET_TYPE_SIZE(pset_set), 1);

    _ITERATOR_CONTAINER(it_hint) = pset_set;
    _SET_ITERATOR_CONTAINER_TYPE(it_hint) = _SET_CONTAINER;
    _SET_ITERATOR_ITERATOR_TYPE(it_hint) = _BIDIRECTIONAL_ITERATOR;

    return it_hint;
}
示例#2
0
/**
 * Inserts an unique element into a map with hint position.
 */
map_iterator_t map_insert_hint(map_t* pmap_map, map_iterator_t it_hint, const pair_t* cppair_pair)
{
    assert(pmap_map != NULL);
    assert(cppair_pair != NULL);
    assert(_pair_is_inited(&pmap_map->_pair_temp));
    assert(_pair_is_inited(cppair_pair));
    assert(_GET_MAP_CONTAINER_TYPE(it_hint) == _MAP_CONTAINER);
    assert(_GET_MAP_ITERATOR_TYPE(it_hint) == _BIDIRECTIONAL_ITERATOR);
    assert(_GET_MAP_CONTAINER(it_hint) == pmap_map);

    /* set key less function and value less function */
    ((pair_t*)cppair_pair)->_bfun_mapkeycompare = pmap_map->_bfun_keycompare;
    ((pair_t*)cppair_pair)->_bfun_mapvaluecompare = pmap_map->_bfun_valuecompare;

    assert(_map_same_pair_type_ex(&pmap_map->_pair_temp, cppair_pair));

#ifdef CSTL_MAP_AVL_TREE
    it_hint = _avl_tree_insert_unique(&pmap_map->_t_tree, cppair_pair);
#else
    it_hint = _rb_tree_insert_unique(&pmap_map->_t_tree, cppair_pair);
#endif

    _GET_CONTAINER(it_hint) = pmap_map;
    _GET_MAP_CONTAINER_TYPE(it_hint) = _MAP_CONTAINER;
    _GET_MAP_ITERATOR_TYPE(it_hint) = _BIDIRECTIONAL_ITERATOR;

    return it_hint;
}
示例#3
0
map_iterator_t map_insert(map_t* pt_map, const pair_t* cpt_pair)
{
    pair_t         t_elempair;
    map_iterator_t t_result;
#ifdef CSTL_MAP_AVL_TREE
    avl_tree_result_pair_t t_avlresult;
#else
    rb_tree_result_pair_t  t_rbresult;
#endif

    assert(pt_map != NULL && cpt_pair != NULL);
    assert(_same_map_pair_type(&pt_map->_t_pair, cpt_pair));

    /* initialize the new pair */
    t_elempair = _create_pair(
        pt_map->_t_pair._t_firsttypesize, pt_map->_t_pair._sz_firsttypename,
        pt_map->_t_pair._t_secondtypesize, pt_map->_t_pair._sz_secondtypename);
    pair_init(&t_elempair);
    memcpy(t_elempair.first, cpt_pair->first, t_elempair._t_firsttypesize);
    memcpy(t_elempair.second, cpt_pair->second, t_elempair._t_secondtypesize);

    /* insert pair into tree */
#ifdef CSTL_MAP_AVL_TREE
    t_avlresult = _avl_tree_insert_unique(_GET_MAP_AVL_TREE(pt_map), &t_elempair);
    if(t_avlresult._t_second._t_bool)
    {
        t_result = t_avlresult._t_first;
    }
    else
    {
        t_result = map_end(pt_map);
        pair_destroy(&t_elempair);
    }
#else
    t_rbresult = _rb_tree_insert_unique(_GET_MAP_RB_TREE(pt_map), &t_elempair);
    if(t_rbresult._t_second._t_bool)
    {
        t_result = t_rbresult._t_first;
    }
    else
    {
        t_result = map_end(pt_map);
        pair_destroy(&t_elempair);
    }
#endif

    _GET_CONTAINER(&t_result) = pt_map;
    _GET_MAP_CONTAINER_TYPE(&t_result) = _MAP_CONTAINER;
    _GET_MAP_ITERATOR_TYPE(&t_result) = _BIDIRECTIONAL_ITERATOR;

    return t_result;
}
示例#4
0
/**
 * Inserts an range of unique element into a avl tree.
 */
void _avl_tree_insert_unique_range(_avl_tree_t* pt_avl_tree, iterator_t it_begin, iterator_t it_end)
{
    iterator_t it_iter;

    assert(pt_avl_tree != NULL);
    assert(_avl_tree_is_inited(pt_avl_tree));
    assert(_avl_tree_same_iterator_type(pt_avl_tree, it_begin));
    assert(_avl_tree_same_iterator_type(pt_avl_tree, it_end));
    assert(iterator_equal(it_begin, it_end) || _iterator_before(it_begin, it_end));

    for (it_iter = it_begin; !iterator_equal(it_iter, it_end); it_iter = iterator_next(it_iter)) {
        _avl_tree_insert_unique(pt_avl_tree, _iterator_get_pointer_ignore_cstr(it_iter));
    }
}
示例#5
0
/**
 * Inserts an array of unique element into a avl tree.
 */
void _avl_tree_insert_unique_array(_avl_tree_t* pt_avl_tree, const void* cpv_array, size_t t_count)
{
    size_t i = 0;

    assert(pt_avl_tree != NULL);
    assert(_avl_tree_is_inited(pt_avl_tree));
    assert(cpv_array != NULL);

    /*
     * Copy the elements from src array to dest avl tree.
     * The array of c builtin and user define or cstl builtin are different,
     * the elements of c builtin array are element itself, but the elements of 
     * c string, user define or cstl are pointer of element.
     */
    if (strncmp(_GET_AVL_TREE_TYPE_BASENAME(pt_avl_tree), _C_STRING_TYPE, _TYPE_NAME_SIZE) == 0) {
        /*
         * We need built a string_t for c string element.
         */
        string_t* pstr_elem = create_string();
        assert(pstr_elem != NULL);
        string_init(pstr_elem);
        for (i = 0; i < t_count; ++i) {
            string_assign_cstr(pstr_elem, *((const char**)cpv_array + i));
            _avl_tree_insert_unique(pt_avl_tree, pstr_elem);
        }
        string_destroy(pstr_elem);
    } else if (_GET_AVL_TREE_TYPE_STYLE(pt_avl_tree) == _TYPE_C_BUILTIN) {
        for (i = 0; i < t_count; ++i) {
            _avl_tree_insert_unique(pt_avl_tree, (unsigned char*)cpv_array + i * _GET_AVL_TREE_TYPE_SIZE(pt_avl_tree));
        }
    } else {
        for (i = 0; i < t_count; ++i) {
            _avl_tree_insert_unique(pt_avl_tree, *((void**)cpv_array + i));
        }
    }
}
void test__avl_tree_destroy_auxiliary__non_empty(void** state)
{
    _avl_tree_t* pt_avl_tree = _create_avl_tree("int");
    int elem = 9;
    _avl_tree_init(pt_avl_tree, NULL);
    _avl_tree_insert_unique(pt_avl_tree, &elem);

    _avl_tree_destroy_auxiliary(pt_avl_tree);
    assert_true(pt_avl_tree->_t_avlroot._pt_parent == NULL);
    assert_true(pt_avl_tree->_t_avlroot._pt_left == &pt_avl_tree->_t_avlroot);
    assert_true(pt_avl_tree->_t_avlroot._pt_right == &pt_avl_tree->_t_avlroot);
    assert_true(pt_avl_tree->_t_nodecount == 0);
    assert_true(pt_avl_tree->_t_compare == NULL);
    free(pt_avl_tree);
}
示例#7
0
void* _map_at_varg(map_t* pt_map, va_list val_elemlist)
{
    pair_t  t_elempair;
    bool_t  t_result = false;
    void*   pv_value = NULL;
#ifdef CSTL_MAP_AVL_TREE
    avl_tree_result_pair_t t_avlresult;
#else
    rb_tree_result_pair_t  t_rbresult;
#endif

    assert(pt_map != NULL);

    t_elempair = _create_pair(
        pt_map->_t_pair._t_firsttypesize, pt_map->_t_pair._sz_firsttypename,
        pt_map->_t_pair._t_secondtypesize, pt_map->_t_pair._sz_secondtypename);
    pair_init(&t_elempair);
    _get_varg_value(
        t_elempair.first,
        val_elemlist,
        t_elempair._t_firsttypesize,
        t_elempair._sz_firsttypename);
    memset(t_elempair.second, 0x00, t_elempair._t_secondtypesize);

#ifdef CSTL_MAP_AVL_TREE
    t_avlresult = 
        _avl_tree_insert_unique(_GET_MAP_AVL_TREE(pt_map), &t_elempair);
    pv_value = ((pair_t*)((avlnode_t*)
        _GET_AVL_TREE_COREPOS(&t_avlresult._t_first))->_pc_data)->second;
    t_result = t_avlresult._t_second._t_bool;
#else
    t_rbresult = 
        _rb_tree_insert_unique(_GET_MAP_RB_TREE(pt_map), &t_elempair);
    pv_value = ((pair_t*)((rbnode_t*)
        _GET_RB_TREE_COREPOS(&t_rbresult._t_first))->_pc_data)->second;
    t_result = t_rbresult._t_second._t_bool;
#endif
    assert(pv_value != NULL);

    /* destroy the pair when inserting failed */
    if(!t_result)
    {
        pair_destroy(&t_elempair);
    }

    return pv_value;
}
示例#8
0
/**
 * Access an element with specific index.
 */
void* _map_at_varg(map_t* pmap_map, va_list val_elemlist)
{
    map_iterator_t it_iter;
    va_list val_elemlist_copy;

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

    va_copy(val_elemlist_copy, val_elemlist);
    _type_get_varg_value(&pmap_map->_pair_temp._t_typeinfofirst, val_elemlist, pmap_map->_pair_temp._pv_first);
#ifdef CSTL_MAP_AVL_TREE
    it_iter = _avl_tree_insert_unique(&pmap_map->_t_tree, &pmap_map->_pair_temp);
#else
    it_iter = _rb_tree_insert_unique(&pmap_map->_t_tree, &pmap_map->_pair_temp);
#endif

    _ITERATOR_CONTAINER(it_iter) = pmap_map;
    _MAP_ITERATOR_CONTAINER_TYPE(it_iter) = _MAP_CONTAINER;
    _MAP_ITERATOR_ITERATOR_TYPE(it_iter) = _BIDIRECTIONAL_ITERATOR;

    if(iterator_equal(it_iter, map_end(pmap_map)))
    {
        it_iter = _map_find_varg(pmap_map, val_elemlist_copy);
    }

    va_end(val_elemlist_copy);

    /* char* */
    if(strncmp(_GET_MAP_SECOND_TYPE_BASENAME(pmap_map), _C_STRING_TYPE, _TYPE_NAME_SIZE) == 0)
    {
        return (char*)string_c_str((string_t*)((pair_t*)iterator_get_pointer(it_iter))->_pv_second);
    }
    else
    {
        return ((pair_t*)iterator_get_pointer(it_iter))->_pv_second;
    }
}