コード例 #1
0
ファイル: cstl_set_private.c プロジェクト: Aluonna/libcstl
/**
 * 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
ファイル: cstl_map.c プロジェクト: astrotycoon/libcstl
/**
 * 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
void test__rb_tree_iterator_before__false(void** state)
{
    _rb_tree_t* pt_rb_tree = _create_rb_tree("int");
    _rb_tree_iterator_t it_first;
    _rb_tree_iterator_t it_second;
    int elem = 100;

    _rb_tree_init(pt_rb_tree, NULL);
    _rb_tree_insert_unique(pt_rb_tree, &elem);
    elem = 2;
    _rb_tree_insert_unique(pt_rb_tree, &elem);
    it_first = _rb_tree_end(pt_rb_tree);
    it_second = _rb_tree_begin(pt_rb_tree);
    assert_false(_rb_tree_iterator_before(it_first, it_second));

    _rb_tree_destroy(pt_rb_tree);
}
コード例 #4
0
void test__rb_tree_iterator_distance__greater_0(void** state)
{
    _rb_tree_t* pt_rb_tree = _create_rb_tree("int");
    _rb_tree_iterator_t it_first;
    _rb_tree_iterator_t it_second;
    int elem = 100;

    _rb_tree_init(pt_rb_tree, NULL);
    _rb_tree_insert_unique(pt_rb_tree, &elem);
    elem = 2;
    _rb_tree_insert_unique(pt_rb_tree, &elem);
    it_first = _rb_tree_begin(pt_rb_tree);
    it_second = _rb_tree_iterator_next(it_first);
    assert_true(_rb_tree_iterator_distance(it_first, it_second) == 1);

    _rb_tree_destroy(pt_rb_tree);
}
コード例 #5
0
void test__rb_tree_iterator_prev__last(void** state)
{
    _rb_tree_t* pt_rb_tree = _create_rb_tree("int");
    _rb_tree_iterator_t it_iter;
    int elem = 10;
    _rb_tree_init(pt_rb_tree, NULL);

    _rb_tree_insert_unique(pt_rb_tree, &elem);
    elem = 3;
    _rb_tree_insert_unique(pt_rb_tree, &elem);
    it_iter = _rb_tree_iterator_prev(_rb_tree_end(pt_rb_tree));
    assert_true(*(int*)_rb_tree_iterator_get_pointer(it_iter) == 10);
    it_iter = _rb_tree_iterator_prev(it_iter);
    assert_true(*(int*)_rb_tree_iterator_get_pointer(it_iter) == 3);

    _rb_tree_destroy(pt_rb_tree);
}
コード例 #6
0
ファイル: cstl_map.c プロジェクト: Masshat/C_and_CPP
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;
}
コード例 #7
0
void test__rb_tree_iterator_get_pointer__c_builtin(void** state)
{
    _rb_tree_t* pt_rb_tree = _create_rb_tree("int");
    _rb_tree_iterator_t it_iter;
    int elem = 12;
    _rb_tree_init(pt_rb_tree, NULL);
    _rb_tree_insert_unique(pt_rb_tree, &elem);

    it_iter = _rb_tree_begin(pt_rb_tree);
    assert_true(*(int*)_rb_tree_iterator_get_pointer(it_iter) == 12);

    _rb_tree_destroy(pt_rb_tree);
}
コード例 #8
0
void test__rb_tree_iterator_prev__begin(void** state)
{
    _rb_tree_t* pt_rb_tree = _create_rb_tree("int");
    _rb_tree_iterator_t it_iter;
    int elem = 100;
    _rb_tree_init(pt_rb_tree, NULL);
    _rb_tree_insert_unique(pt_rb_tree, &elem);

    it_iter = _rb_tree_begin(pt_rb_tree);

    expect_assert_failure(_rb_tree_iterator_prev(it_iter));

    _rb_tree_destroy(pt_rb_tree);
}
コード例 #9
0
void test__rb_tree_iterator_get_pointer__libcstl_builtin(void** state)
{
    _rb_tree_t* pt_rb_tree = _create_rb_tree("vector_t<int>");
    _rb_tree_iterator_t it_iter;
    vector_t* pvec = create_vector(int);
    _rb_tree_init(pt_rb_tree, NULL);
    vector_init_n(pvec, 10);
    _rb_tree_insert_unique(pt_rb_tree, pvec);

    it_iter = _rb_tree_begin(pt_rb_tree);
    assert_true(vector_size((vector_t*)_rb_tree_iterator_get_pointer(it_iter)) == 10);

    _rb_tree_destroy(pt_rb_tree);
}
コード例 #10
0
void test__rb_tree_iterator_get_pointer__cstr(void** state)
{
    _rb_tree_t* pt_rb_tree = _create_rb_tree("char*");
    _rb_tree_iterator_t it_iter;
    string_t* pstr = create_string();
    string_init_cstr(pstr, "abc");
    _rb_tree_init(pt_rb_tree, NULL);
    _rb_tree_insert_unique(pt_rb_tree, pstr);

    it_iter = _rb_tree_begin(pt_rb_tree);
    assert_true(strcmp((char*)_rb_tree_iterator_get_pointer(it_iter), "abc") == 0);

    _rb_tree_destroy(pt_rb_tree);
}
コード例 #11
0
void test__rb_tree_iterator_prev__end(void** state)
{
    _rb_tree_t* pt_rb_tree = _create_rb_tree("int");
    _rb_tree_iterator_t it_iter;
    int elem = 10;
    _rb_tree_init(pt_rb_tree, NULL);

    _rb_tree_insert_unique(pt_rb_tree, &elem);
    it_iter = _rb_tree_end(pt_rb_tree);

    it_iter = _rb_tree_iterator_prev(it_iter);
    assert_true(_rb_tree_iterator_equal(it_iter, _rb_tree_begin(pt_rb_tree)));

    _rb_tree_destroy(pt_rb_tree);
}
コード例 #12
0
void test__rb_tree_destroy_auxiliary__non_empty(void** state)
{
    _rb_tree_t* pt_rb_tree = _create_rb_tree("int");
    int elem = 9;
    _rb_tree_init(pt_rb_tree, NULL);
    _rb_tree_insert_unique(pt_rb_tree, &elem);

    _rb_tree_destroy_auxiliary(pt_rb_tree);
    assert_true(pt_rb_tree->_t_rbroot._pt_parent == NULL);
    assert_true(pt_rb_tree->_t_rbroot._pt_left == &pt_rb_tree->_t_rbroot);
    assert_true(pt_rb_tree->_t_rbroot._pt_right == &pt_rb_tree->_t_rbroot);
    assert_true(pt_rb_tree->_t_nodecount == 0);
    assert_true(pt_rb_tree->_t_compare == NULL);
    free(pt_rb_tree);
}
コード例 #13
0
ファイル: cstl_map.c プロジェクト: Masshat/C_and_CPP
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;
}
コード例 #14
0
void test__rb_tree_iterator_get_pointer__user_define(void** state)
{
    _rb_tree_t* pt_rb_tree = NULL;
    _rb_tree_iterator_t it_iter;
    _test__rb_tree_iterator_get_pointer__user_define_t elem;

    type_register(_test__rb_tree_iterator_get_pointer__user_define_t, NULL, NULL, NULL, NULL);
    pt_rb_tree = _create_rb_tree("_test__rb_tree_iterator_get_pointer__user_define_t");
    _rb_tree_init(pt_rb_tree, NULL);
    elem.elem = 100;
    _rb_tree_insert_unique(pt_rb_tree, &elem);

    it_iter = _rb_tree_begin(pt_rb_tree);
    assert_true(((_test__rb_tree_iterator_get_pointer__user_define_t*)_rb_tree_iterator_get_pointer(it_iter))->elem == 100);

    _rb_tree_destroy(pt_rb_tree);
}
コード例 #15
0
ファイル: cstl_map_private.c プロジェクト: coderXing/libcstl
/**
 * 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;
    }
}