Example #1
0
/**
 * Swap the datas of first avl_tree and second avl_tree.
 */
void _avl_tree_swap(_avl_tree_t* pt_first, _avl_tree_t* pt_second)
{
    _avl_tree_t t_temp;

    assert(pt_first != NULL);
    assert(pt_second != NULL);
    assert(_avl_tree_is_inited(pt_first));
    assert(_avl_tree_is_inited(pt_second));
    assert(_avl_tree_same_type_ex(pt_first, pt_second));

    if (_avl_tree_equal(pt_first, pt_second)) {
        return;
    }

    t_temp = *pt_first;
    *pt_first = *pt_second;
    *pt_second = t_temp;

    if (_avl_tree_empty(pt_first)) {
        pt_first->_t_avlroot._pt_left = &pt_first->_t_avlroot;
        pt_first->_t_avlroot._pt_right = &pt_first->_t_avlroot;
    } else {
        pt_first->_t_avlroot._pt_parent->_pt_parent = &pt_first->_t_avlroot;
    }

    if (_avl_tree_empty(pt_second)) {
        pt_second->_t_avlroot._pt_left = &pt_second->_t_avlroot;
        pt_second->_t_avlroot._pt_right = &pt_second->_t_avlroot;
    } else {
        pt_second->_t_avlroot._pt_parent->_pt_parent = &pt_second->_t_avlroot;
    }
}
Example #2
0
/** exported function implementation section **/
void test_avl_tree(void)
{
    avl_tree_t t_tree = _create_avl_tree(sizeof(int), "int");
    avl_tree_t t_tree2 = _create_avl_tree(sizeof(int), "int");
    int n_value = 900;

    _avl_tree_init(&t_tree, NULL, NULL);
    _avl_tree_insert_equal(&t_tree, &n_value);
    printf("avl tree1 - size:%u, max_size:%u, empty:%d\n",
        _avl_tree_size(&t_tree), _avl_tree_max_size(&t_tree), _avl_tree_empty(&t_tree));
    /*_avl_tree_init_copy_range(*/
    /*&t_tree2, _avl_tree_begin(&t_tree), _avl_tree_begin(&t_tree));*/
    /*printf("avl tree2 - size:%u, max_size:%u, empty:%d\n",*/
    /*_avl_tree_size(&t_tree2), _avl_tree_max_size(&t_tree2), _avl_tree_empty(&t_tree2));*/
    _avl_tree_init_copy_range_cmp(
        &t_tree2, _avl_tree_begin(&t_tree), _avl_tree_begin(&t_tree), NULL);
    printf("avl tree2 - size:%u, max_size:%u, empty:%d\n",
        _avl_tree_size(&t_tree2), _avl_tree_max_size(&t_tree2), _avl_tree_empty(&t_tree2));
    _avl_tree_insert_equal_range(
        &t_tree2, _avl_tree_begin(&t_tree), _avl_tree_begin(&t_tree));
    printf("avl tree2 - size:%u, max_size:%u, empty:%d\n",
        _avl_tree_size(&t_tree2), _avl_tree_max_size(&t_tree2), _avl_tree_empty(&t_tree2));
    _avl_tree_insert_unique_range(
        &t_tree2, _avl_tree_end(&t_tree), _avl_tree_end(&t_tree));
    printf("avl tree2 - size:%u, max_size:%u, empty:%d\n",
        _avl_tree_size(&t_tree2), _avl_tree_max_size(&t_tree2), _avl_tree_empty(&t_tree2));
    _avl_tree_erase_range(&t_tree2, _avl_tree_begin(&t_tree2), _avl_tree_begin(&t_tree2));
    printf("avl tree2 - size:%u, max_size:%u, empty:%d\n",
        _avl_tree_size(&t_tree2), _avl_tree_max_size(&t_tree2), _avl_tree_empty(&t_tree2));

    _avl_tree_destroy(&t_tree);
    _avl_tree_destroy(&t_tree2);
}
Example #3
0
bool_t multimap_empty(const multimap_t* cpt_multimap)
{
    assert(cpt_multimap != NULL);

#ifdef CSTL_MULTIMAP_AVL_TREE
    return _avl_tree_empty(_GET_MULTIMAP_AVL_TREE(cpt_multimap));
#else
    return _rb_tree_empty(_GET_MULTIMAP_RB_TREE(cpt_multimap));
#endif
}
Example #4
0
/**
 * Test if an set is empty.
 */
bool_t set_empty(const set_t* cpset_set)
{
    assert(cpset_set != NULL);

#ifdef CSTL_SET_AVL_TREE
    return _avl_tree_empty(&cpset_set->_t_tree);
#else
    return _rb_tree_empty(&cpset_set->_t_tree);
#endif
}
Example #5
0
/**
 * Test if an multiset is empty.
 */
bool_t multiset_empty(const multiset_t* cpmset_mset)
{
    assert(cpmset_mset != NULL);

#ifdef CSTL_MULTISET_AVL_TREE
    return _avl_tree_empty(&cpmset_mset->_t_tree);
#else
    return _rb_tree_empty(&cpmset_mset->_t_tree);
#endif
}
Example #6
0
/**
 * Test if an map is empty.
 */
bool_t map_empty(const map_t* cpmap_map)
{
    assert(cpmap_map != NULL);
    assert(_pair_is_inited(&cpmap_map->_pair_temp));

#ifdef CSTL_MAP_AVL_TREE
    return _avl_tree_empty(&cpmap_map->_t_tree);
#else
    return _rb_tree_empty(&cpmap_map->_t_tree);
#endif
}
Example #7
0
/**
 * Return an iterator to the first element that is equal to or greater than a specific element.
 */
_avl_tree_iterator_t _avl_tree_lower_bound(const _avl_tree_t* cpt_avl_tree, const void* cpv_value)
{
    _avlnode_t*          pt_cur = NULL;
    _avlnode_t*          pt_prev = NULL;
    bool_t               b_less = false;
    bool_t               b_greater = false;
    _avl_tree_iterator_t it_iter;

    assert(cpt_avl_tree != NULL);
    assert(cpv_value != NULL);
    assert(_avl_tree_is_inited(cpt_avl_tree));

    it_iter = _create_avl_tree_iterator();
    _AVL_TREE_ITERATOR_TREE_POINTER(it_iter) = (void*)cpt_avl_tree;

    if (!_avl_tree_empty(cpt_avl_tree)) {
        pt_prev = cpt_avl_tree->_t_avlroot._pt_parent;

        b_less = b_greater = _GET_AVL_TREE_TYPE_SIZE(cpt_avl_tree);
        _avl_tree_elem_compare_auxiliary(cpt_avl_tree, cpv_value, pt_prev->_pby_data, &b_less);
        _avl_tree_elem_compare_auxiliary(cpt_avl_tree, pt_prev->_pby_data, cpv_value, &b_greater);

        pt_cur = (b_less || !b_greater) ? pt_prev->_pt_left : pt_prev->_pt_right;
        while (pt_cur != NULL) {
            pt_prev = pt_cur;
            b_less = b_greater = _GET_AVL_TREE_TYPE_SIZE(cpt_avl_tree);
            _avl_tree_elem_compare_auxiliary(cpt_avl_tree, cpv_value, pt_prev->_pby_data, &b_less);
            _avl_tree_elem_compare_auxiliary(cpt_avl_tree, pt_prev->_pby_data, cpv_value, &b_greater);

            pt_cur = (b_less || !b_greater) ? pt_prev->_pt_left : pt_prev->_pt_right;
        }

        if (b_less || !b_greater) {
            assert(pt_prev->_pt_left == NULL);
            _AVL_TREE_ITERATOR_COREPOS(it_iter) = (_byte_t*)pt_prev;
            assert(_avl_tree_iterator_belong_to_avl_tree(cpt_avl_tree, it_iter));
        } else {
            assert(pt_prev->_pt_right == NULL);
            _AVL_TREE_ITERATOR_COREPOS(it_iter) = (_byte_t*)pt_prev;
            it_iter = _avl_tree_iterator_next(it_iter);
        }
    } else {
        it_iter = _avl_tree_end(cpt_avl_tree);
    }

    return it_iter;
}
Example #8
0
/**
 * Inserts an unique element into a avl tree.
 */
_avl_tree_iterator_t _avl_tree_insert_unique(_avl_tree_t* pt_avl_tree, const void* cpv_value)
{
    assert(pt_avl_tree != NULL);
    assert(cpv_value != NULL);
    assert(_avl_tree_is_inited(pt_avl_tree));

    /* if the avl tree is empty */
    if (_avl_tree_empty(pt_avl_tree)) {
        return _avl_tree_insert_equal(pt_avl_tree, cpv_value);
    } else {
        /* find value in avl tree */
        _avl_tree_iterator_t it_iter = _avl_tree_find(pt_avl_tree, cpv_value);
        /* if the value is exist */
        if (!_avl_tree_iterator_equal(it_iter, _avl_tree_end(pt_avl_tree))) {
            return _avl_tree_end(pt_avl_tree);
        } else {
            /* insert value into avl tree */
            return _avl_tree_insert_equal(pt_avl_tree, cpv_value);
        }
    }
}