Exemple #1
0
/**
 * Test the type and compare function that saved in the avl tree container and referenced by it_iter are same.
 */
bool_t _avl_tree_same_avl_tree_iterator_type_ex(const _avl_tree_t* cpt_avl_tree, _avl_tree_iterator_t it_iter)
{
    assert(cpt_avl_tree != NULL);
    assert( _AVL_TREE_ITERATOR_TREE(it_iter) != NULL);

    return _avl_tree_same_type_ex(cpt_avl_tree, _AVL_TREE_ITERATOR_TREE(it_iter));
}
Exemple #2
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;
    }
}
Exemple #3
0
/**
 * Assign avl tree container.
 */
void _avl_tree_assign(_avl_tree_t* pt_dest, const _avl_tree_t* cpt_src)
{
    assert(pt_dest != NULL);
    assert(cpt_src != NULL);
    assert(_avl_tree_is_inited(pt_dest));
    assert(_avl_tree_is_inited(cpt_src));
    assert(_avl_tree_same_type_ex(pt_dest, cpt_src));

    if (!_avl_tree_equal(pt_dest, cpt_src)) {
        _avl_tree_iterator_t it_iter;
        _avl_tree_iterator_t it_begin;
        _avl_tree_iterator_t it_end;

        /* clear dest avl tree */
        _avl_tree_clear(pt_dest);
        it_begin = _avl_tree_begin(cpt_src);
        it_end = _avl_tree_end(cpt_src);

        /* insert all elements of src into dest */
        for (it_iter = it_begin;
             !_avl_tree_iterator_equal(it_iter, it_end);
             it_iter = _avl_tree_iterator_next(it_iter)) {
            _avl_tree_insert_equal(pt_dest, _avl_tree_iterator_get_pointer_ignore_cstr(it_iter));
        }
    }
}
Exemple #4
0
/**
 * Tests if the two avl tree are equal.
 */
bool_t _avl_tree_equal(const _avl_tree_t* cpt_first, const _avl_tree_t* cpt_second)
{
    _avl_tree_iterator_t it_first;
    _avl_tree_iterator_t it_first_begin;
    _avl_tree_iterator_t it_first_end;
    _avl_tree_iterator_t it_second;
    _avl_tree_iterator_t it_second_begin;
    _avl_tree_iterator_t it_second_end;
    bool_t               b_less = false;
    bool_t               b_greater = false;

    assert(cpt_first != NULL);
    assert(cpt_second != NULL);
    assert(_avl_tree_is_inited(cpt_first));
    assert(_avl_tree_is_inited(cpt_second));
    assert(_avl_tree_same_type_ex(cpt_first, cpt_second));

    if (cpt_first == cpt_second) {
        return true;
    }

    /* test avl tree size */
    if (_avl_tree_size(cpt_first) != _avl_tree_size(cpt_second)) {
        return false;
    }

    it_first_begin = _avl_tree_begin(cpt_first);
    it_first_end = _avl_tree_end(cpt_first);
    it_second_begin = _avl_tree_begin(cpt_second);
    it_second_end = _avl_tree_end(cpt_second);

    /* test each element */
    for (it_first = it_first_begin, it_second = it_second_begin;
         !_avl_tree_iterator_equal(it_first, it_first_end) && !_avl_tree_iterator_equal(it_second, it_second_end);
         it_first = _avl_tree_iterator_next(it_first), it_second = _avl_tree_iterator_next(it_second)) {
        b_less = b_greater = _GET_AVL_TREE_TYPE_SIZE(cpt_first);
        _GET_AVL_TREE_TYPE_LESS_FUNCTION(cpt_first)(
            ((_avlnode_t*)_AVL_TREE_ITERATOR_COREPOS(it_first))->_pby_data,
            ((_avlnode_t*)_AVL_TREE_ITERATOR_COREPOS(it_second))->_pby_data, &b_less);
        _GET_AVL_TREE_TYPE_LESS_FUNCTION(cpt_first)(
            ((_avlnode_t*)_AVL_TREE_ITERATOR_COREPOS(it_second))->_pby_data,
            ((_avlnode_t*)_AVL_TREE_ITERATOR_COREPOS(it_first))->_pby_data, &b_greater);
        if (b_less || b_greater) {
            return false;
        }
    }
    assert(_avl_tree_iterator_equal(it_first, it_first_end) && _avl_tree_iterator_equal(it_second, it_second_end));

    return true;
}
Exemple #5
0
/**
 * Tests if the first avl tree is less than the second avl tree.
 */
bool_t _avl_tree_less(const _avl_tree_t* cpt_first, const _avl_tree_t* cpt_second)
{
    _avl_tree_iterator_t it_first;
    _avl_tree_iterator_t it_first_begin;
    _avl_tree_iterator_t it_first_end;
    _avl_tree_iterator_t it_second;
    _avl_tree_iterator_t it_second_begin;
    _avl_tree_iterator_t it_second_end;
    bool_t               b_result = false;

    assert(cpt_first != NULL);
    assert(cpt_second != NULL);
    assert(_avl_tree_is_inited(cpt_first));
    assert(_avl_tree_is_inited(cpt_second));
    assert(_avl_tree_same_type_ex(cpt_first, cpt_second));

    it_first_begin = _avl_tree_begin(cpt_first);
    it_first_end = _avl_tree_end(cpt_first);
    it_second_begin = _avl_tree_begin(cpt_second);
    it_second_end = _avl_tree_end(cpt_second);

    /* test each element */
    for (it_first = it_first_begin, it_second = it_second_begin;
         !_avl_tree_iterator_equal(it_first, it_first_end) && !_avl_tree_iterator_equal(it_second, it_second_end);
         it_first = _avl_tree_iterator_next(it_first), it_second = _avl_tree_iterator_next(it_second)) {
        b_result = _GET_AVL_TREE_TYPE_SIZE(cpt_first);
        _GET_AVL_TREE_TYPE_LESS_FUNCTION(cpt_first)(
            ((_avlnode_t*)_AVL_TREE_ITERATOR_COREPOS(it_first))->_pby_data,
            ((_avlnode_t*)_AVL_TREE_ITERATOR_COREPOS(it_second))->_pby_data, &b_result);
        if (b_result) {
            return true;
        }
        b_result = _GET_AVL_TREE_TYPE_SIZE(cpt_first);
        _GET_AVL_TREE_TYPE_LESS_FUNCTION(cpt_first)(
            ((_avlnode_t*)_AVL_TREE_ITERATOR_COREPOS(it_second))->_pby_data,
            ((_avlnode_t*)_AVL_TREE_ITERATOR_COREPOS(it_first))->_pby_data, &b_result);
        if (b_result) {
            return false;
        }
    }

    return _avl_tree_size(cpt_first) < _avl_tree_size(cpt_second) ? true : false;
}