Example #1
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;
}
Example #2
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;
}
Example #3
0
/**
 * Initialize avl tree container.
 */
void _avl_tree_init(_avl_tree_t* pt_avl_tree, bfun_t t_compare)
{
    assert(pt_avl_tree != NULL);
    assert(_avl_tree_is_created(pt_avl_tree));

    pt_avl_tree->_t_avlroot._pt_left = &pt_avl_tree->_t_avlroot;
    pt_avl_tree->_t_avlroot._pt_right = &pt_avl_tree->_t_avlroot;

    if (t_compare != NULL) {
        pt_avl_tree->_t_compare = t_compare;
    } else {
        pt_avl_tree->_t_compare = _GET_AVL_TREE_TYPE_LESS_FUNCTION(pt_avl_tree);
    }
}
Example #4
0
/**
 * Element compare function auxiliary
 */
void _avl_tree_elem_compare_auxiliary(
    const _avl_tree_t* cpt_avl_tree, const void* cpv_first, const void* cpv_second, void* pv_output)
{
    assert(cpt_avl_tree != NULL);
    assert(cpv_first != NULL);
    assert(cpv_second != NULL);
    assert(pv_output != NULL);
    assert(_avl_tree_is_inited(cpt_avl_tree));

    if (strncmp(_GET_AVL_TREE_TYPE_BASENAME(cpt_avl_tree), _C_STRING_TYPE, _TYPE_NAME_SIZE) == 0 &&
        cpt_avl_tree->_t_compare != _GET_AVL_TREE_TYPE_LESS_FUNCTION(cpt_avl_tree)) {
        cpt_avl_tree->_t_compare(string_c_str((string_t*)cpv_first), string_c_str((string_t*)cpv_second), pv_output);
    } else {
        cpt_avl_tree->_t_compare(cpv_first, cpv_second, pv_output);
    }
}