コード例 #1
0
ファイル: cstl_multiset.c プロジェクト: cffyh/libcstl
/**
 * Return an iterator that addresses the location succeeding the last element in the multiset.
 */
multiset_iterator_t multiset_end(const multiset_t* cpmset_mset)
{
    multiset_iterator_t it_end;

    assert(cpmset_mset != NULL);

#ifdef CSTL_MULTISET_AVL_TREE
    it_end = _avl_tree_end(&cpmset_mset->_t_tree);
#else
    it_end = _rb_tree_end(&cpmset_mset->_t_tree);
#endif

    _ITERATOR_CONTAINER(it_end) = (multiset_t*)cpmset_mset;
    _MULTISET_ITERATOR_CONTAINER_TYPE(it_end) = _MULTISET_CONTAINER;
    _MULTISET_ITERATOR_ITERATOR_TYPE(it_end) = _BIDIRECTIONAL_ITERATOR;

    return it_end;
}
コード例 #2
0
ファイル: cstl_avl_tree.c プロジェクト: ChenHui109/libcstl
/**
 * Return an iterator to the first element that is greater than a specific element.
 */
_avl_tree_iterator_t _avl_tree_upper_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_result = 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_result = _GET_AVL_TREE_TYPE_SIZE(cpt_avl_tree);
        _avl_tree_elem_compare_auxiliary(cpt_avl_tree, cpv_value, pt_prev->_pby_data, &b_result);

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

            pt_cur = b_result ? pt_prev->_pt_left : pt_prev->_pt_right;
        }

        if (b_result) {
            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;
}
コード例 #3
0
ファイル: cstl_map.c プロジェクト: astrotycoon/libcstl
/**
 * Return an iterator that addresses the location succeeding the last element in the map.
 */
map_iterator_t map_end(const map_t* cpmap_map)
{
    map_iterator_t it_end;

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

#ifdef CSTL_MAP_AVL_TREE
    it_end = _avl_tree_end(&cpmap_map->_t_tree);
#else
    it_end = _rb_tree_end(&cpmap_map->_t_tree);
#endif

    _GET_CONTAINER(it_end) = (map_t*)cpmap_map;
    _GET_MAP_CONTAINER_TYPE(it_end) = _MAP_CONTAINER;
    _GET_MAP_ITERATOR_TYPE(it_end) = _BIDIRECTIONAL_ITERATOR;

    return it_end;
}
コード例 #4
0
ファイル: cstl_avl_tree.c プロジェクト: ChenHui109/libcstl
/**
 * Initialize avl tree container with avl tree.
 */
void _avl_tree_init_copy(_avl_tree_t* pt_dest, const _avl_tree_t* cpt_src)
{
    _avl_tree_iterator_t it_iter;
    _avl_tree_iterator_t it_begin;
    _avl_tree_iterator_t it_end;

    assert(pt_dest != NULL);
    assert(cpt_src != NULL);
    assert(_avl_tree_is_created(pt_dest));
    assert(_avl_tree_is_inited(cpt_src));
    assert(_avl_tree_same_type(pt_dest, cpt_src));

    /* init the avl tree with the src avl tree */
    _avl_tree_init(pt_dest,  cpt_src->_t_compare);
    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));
    }
}
コード例 #5
0
/**
 * Return iterator reference next element.
 */
_avl_tree_iterator_t _avl_tree_iterator_next(_avl_tree_iterator_t it_iter)
{
    _avlnode_t* pt_next = NULL;  /* next pos */
    _avlnode_t* pt_cur = (_avlnode_t*)_AVL_TREE_ITERATOR_COREPOS(it_iter);

    assert(_avl_tree_iterator_belong_to_avl_tree(_AVL_TREE_ITERATOR_TREE(it_iter), it_iter));
    assert(!_avl_tree_iterator_equal(it_iter, _avl_tree_end(_AVL_TREE_ITERATOR_TREE(it_iter))));

    if (pt_cur->_pt_right != NULL) {
        /*
         *        A
         *       / \
         *      B   C
         *     / \   \
         *    D   E   F
         *       /
         *      G
         *  current : B
         *  next    : G
         */
        pt_next = pt_cur->_pt_right;
        while (pt_next->_pt_left != NULL) {
            pt_next = pt_next->_pt_left;
        }

        _AVL_TREE_ITERATOR_COREPOS(it_iter) = (_byte_t*)pt_next;
    } else {
        /*
         *     avlroot
         *        |
         *        A
         *       / \
         *      B   C
         *     / \   \
         *    D   E   F
         *       /
         *      G
         */
        pt_next = pt_cur->_pt_parent;
        while (pt_cur == pt_next->_pt_right) {
            pt_cur = pt_next;
            pt_next = pt_next->_pt_parent;
        }
        /* 
         * this is special condition, when the next pos is root's parent.
         * when the avl tree has only one node, this condition is came.
         */
        if (pt_cur->_pt_right != pt_next) {
            /*
             * current : E or F
             * next    : A or avlroot
             */
            _AVL_TREE_ITERATOR_COREPOS(it_iter) = (_byte_t*)pt_next;
        } else {
            /* here is special condition */
            /*
             *   avlroot
             *      |
             *      A
             */
            _AVL_TREE_ITERATOR_COREPOS(it_iter) = (_byte_t*)pt_cur;
        }
    }

    return it_iter;
}
コード例 #6
0
ファイル: cstl_avl_tree.c プロジェクト: ChenHui109/libcstl
/*
 * Erase an element in an avl tree from specificed position.
 */
void _avl_tree_erase_pos(_avl_tree_t* pt_avl_tree, _avl_tree_iterator_t it_pos)
{
    _avlnode_t* pt_parent = NULL;
    _avlnode_t* pt_cur = NULL;
    bool_t      b_result = false;

    assert(pt_avl_tree != NULL);
    assert(_avl_tree_is_inited(pt_avl_tree));
    assert(_avl_tree_iterator_belong_to_avl_tree(pt_avl_tree, it_pos));
    assert(!_avl_tree_iterator_equal(it_pos, _avl_tree_end(pt_avl_tree)));
    
    pt_cur = (_avlnode_t*)_AVL_TREE_ITERATOR_COREPOS(it_pos);
    pt_parent = pt_cur->_pt_parent;

    /* delete node X express deleting */
    if (pt_cur->_pt_left == NULL && pt_cur->_pt_right == NULL) {
        if (pt_parent == &pt_avl_tree->_t_avlroot) {
            /*
             *  P       P
             *  |   =>   
             *  X
             */
            pt_parent->_pt_parent = NULL;
        } else if (pt_cur == pt_parent->_pt_left) {
            /*
             *    P       P
             *   /   =>   
             *  X
             */
            pt_parent->_pt_left = NULL;
        } else {
            /*
             *   P         P
             *    \   =>  
             *     X
             */
            pt_parent->_pt_right = NULL;
        }
    } else if (pt_cur->_pt_left != NULL && pt_cur->_pt_right == NULL) {
        if (pt_parent == &pt_avl_tree->_t_avlroot) {
            /*
             *   P          P
             *   |          |
             *   X     =>   L
             *  /
             * L
             */
            pt_parent->_pt_parent = pt_cur->_pt_left;
            pt_parent->_pt_parent->_pt_parent = pt_parent;
        } else if (pt_cur == pt_parent->_pt_left) {
            /*
             *     P          P
             *    /          /
             *   X     =>   L
             *  /
             * L
             */
            pt_parent->_pt_left = pt_cur->_pt_left;
            pt_parent->_pt_left->_pt_parent = pt_parent;
        } else {
            /*
             *  P           P
             *   \           \
             *    X   =>      L
             *   /
             *  L
             */
            pt_parent->_pt_right = pt_cur->_pt_left;
            pt_parent->_pt_right->_pt_parent = pt_parent;
        }
    } else if (pt_cur->_pt_left == NULL && pt_cur->_pt_right != NULL) {
        if (pt_parent == &pt_avl_tree->_t_avlroot) {
            /*
             *     P             P
             *     |             |
             *     X     =>      R
             *      \
             *       R
             */
            pt_parent->_pt_parent = pt_cur->_pt_right;
            pt_parent->_pt_parent->_pt_parent = pt_parent;
        } else if (pt_cur == pt_parent->_pt_right) {
            /*
             *   P             P
             *    \             \
             *     X     =>      R
             *      \
             *       R
             */
            pt_parent->_pt_right = pt_cur->_pt_right;
            pt_parent->_pt_right->_pt_parent = pt_parent;
        } else {
            /*
             *    P              R
             *   /              /
             *  X       =>     R
             *   \
             *    R
             */
            pt_parent->_pt_left = pt_cur->_pt_right;
            pt_parent->_pt_left->_pt_parent = pt_parent;
        }
    } else {
        _avlnode_t* pt_parenttmp = NULL;
        _avlnode_t* pt_curtmp = NULL;

        if (pt_parent == &pt_avl_tree->_t_avlroot) {
            pt_curtmp = _avl_tree_get_min_avlnode(pt_cur->_pt_right);
            if (pt_cur == pt_curtmp->_pt_parent) {
                /*
                 *     P              P
                 *     |              | 
                 *     X       =>     B
                 *    / \            / \
                 *   A   B          A   C
                 *        \
                 *         C
                 */
                /* pt_curtmp express B */
                pt_curtmp->_pt_left = pt_cur->_pt_left;
                pt_curtmp->_pt_left->_pt_parent = pt_curtmp;
                pt_curtmp->_pt_parent = pt_cur->_pt_parent;
                pt_curtmp->_pt_parent->_pt_parent = pt_curtmp;

                pt_parent = pt_curtmp;
            } else {
                /*
                 *     P              P
                 *     |              | 
                 *     X       =>     S
                 *    / \            / \
                 *   A   B          A   B
                 *      / \            / \
                 *     S   C          D   C
                 *      \
                 *       D
                 */
                /* pt_curtmp express S; pt_parenttmp express B */
                pt_parenttmp = pt_curtmp->_pt_parent;
                pt_parenttmp->_pt_left = pt_curtmp->_pt_right;
                if (pt_parenttmp->_pt_left != NULL) {
                    pt_parenttmp->_pt_left->_pt_parent = pt_parenttmp;
                }

                pt_curtmp->_pt_left = pt_cur->_pt_left;
                pt_curtmp->_pt_left->_pt_parent = pt_curtmp;
                pt_curtmp->_pt_right = pt_cur->_pt_right;
                pt_curtmp->_pt_right->_pt_parent = pt_curtmp;
                pt_curtmp->_pt_parent = pt_cur->_pt_parent;
                pt_curtmp->_pt_parent->_pt_parent = pt_curtmp;

                pt_parent = pt_parenttmp;
            }
        } else if (pt_cur == pt_parent->_pt_left) {
            pt_curtmp = _avl_tree_get_min_avlnode(pt_cur->_pt_right);
            if (pt_cur == pt_curtmp->_pt_parent) {
                /*
                 *       P              P
                 *      /              / 
                 *     X       =>     B
                 *    / \            / \
                 *   A   B          A   C
                 *        \
                 *         C
                 */
                /* pt_curtmp express B */
                pt_curtmp->_pt_left = pt_cur->_pt_left;
                pt_curtmp->_pt_left->_pt_parent = pt_curtmp;
                pt_curtmp->_pt_parent = pt_cur->_pt_parent;
                pt_curtmp->_pt_parent->_pt_left = pt_curtmp;

                pt_parent = pt_curtmp;
            } else {
                /*
                 *       P              P
                 *      /              / 
                 *     X       =>     S
                 *    / \            / \
                 *   A   B          A   B
                 *      / \            / \
                 *     S   C          D   C
                 *      \
                 *       D
                 */
                /* pt_curtmp express S; pt_parenttmp express B */
                pt_parenttmp = pt_curtmp->_pt_parent;
                pt_parenttmp->_pt_left = pt_curtmp->_pt_right;
                if (pt_parenttmp->_pt_left != NULL) {
                    pt_parenttmp->_pt_left->_pt_parent = pt_parenttmp;
                }

                pt_curtmp->_pt_left = pt_cur->_pt_left;
                pt_curtmp->_pt_left->_pt_parent = pt_curtmp;
                pt_curtmp->_pt_right = pt_cur->_pt_right;
                pt_curtmp->_pt_right->_pt_parent = pt_curtmp;
                pt_curtmp->_pt_parent = pt_cur->_pt_parent;
                pt_curtmp->_pt_parent->_pt_left = pt_curtmp;

                pt_parent = pt_parenttmp;
            }
        } else {
            pt_curtmp = _avl_tree_get_min_avlnode(pt_cur->_pt_right);
            if (pt_cur == pt_curtmp->_pt_parent) {
                /*
                 *      P            P
                 *       \            \
                 *        X     =>     B
                 *       / \          / \
                 *      A   B        A   C
                 *           \
                 *            C
                 */
                /* pt_curtmp express B */
                pt_curtmp->_pt_left = pt_cur->_pt_left;
                pt_curtmp->_pt_left->_pt_parent = pt_curtmp;
                pt_curtmp->_pt_parent = pt_cur->_pt_parent;
                pt_curtmp->_pt_parent->_pt_right = pt_curtmp;

                pt_parent = pt_curtmp;
            } else {
                /*
                 *      P            P
                 *       \            \
                 *        X     =>     S
                 *       / \          / \
                 *      A   B        A   B
                 *         / \          / \
                 *        C   D        C   D
                 *       / \          / \
                 *      S   E        F   E
                 *       \
                 *        F
                 */
                /* pt_curtmp express S; pt_parenttmp express C */
                pt_parenttmp = pt_curtmp->_pt_parent;
                pt_parenttmp->_pt_left = pt_curtmp->_pt_right;
                if (pt_parenttmp->_pt_left != NULL) {
                    pt_parenttmp->_pt_left->_pt_parent = pt_parenttmp;
                }

                pt_curtmp->_pt_left = pt_cur->_pt_left;
                pt_curtmp->_pt_left->_pt_parent = pt_curtmp;
                pt_curtmp->_pt_right = pt_cur->_pt_right;
                pt_curtmp->_pt_right->_pt_parent = pt_curtmp;
                pt_curtmp->_pt_parent = pt_cur->_pt_parent;
                pt_curtmp->_pt_parent->_pt_right = pt_curtmp;

                pt_parent = pt_parenttmp;
            }
        }
    }

    /* rebalance until to root */
    if (pt_parent != &pt_avl_tree->_t_avlroot) {
        _avlnode_t* pt_newcur = pt_parent;
        pt_parent = pt_newcur->_pt_parent;
        while (pt_parent != &pt_avl_tree->_t_avlroot) {
            if (pt_newcur == pt_parent->_pt_left) {
                pt_parent->_pt_left = _avl_tree_rebalance(pt_parent->_pt_left);
                pt_parent->_pt_left->_pt_parent = pt_parent;
            } else {
                pt_parent->_pt_right = _avl_tree_rebalance(pt_parent->_pt_right);
                pt_parent->_pt_right->_pt_parent = pt_parent;
            }

            pt_newcur = pt_parent;
            pt_parent = pt_newcur->_pt_parent;
        }
    }

    /* rebalance root */
    if (pt_parent->_pt_parent != NULL) {
        pt_parent->_pt_parent = _avl_tree_rebalance(pt_parent->_pt_parent);
        pt_parent->_pt_parent->_pt_parent = pt_parent;
    }

    /* destroy node */
    b_result = _GET_AVL_TREE_TYPE_SIZE(pt_avl_tree);
    _GET_AVL_TREE_TYPE_DESTROY_FUNCTION(pt_avl_tree)(pt_cur->_pby_data, &b_result);
    assert(b_result);
    _alloc_deallocate(&pt_avl_tree->_t_allocator, pt_cur,
        _AVL_TREE_NODE_SIZE(_GET_AVL_TREE_TYPE_SIZE(pt_avl_tree)), 1);
    pt_avl_tree->_t_nodecount--;
    if (pt_avl_tree->_t_nodecount == 0) {
        pt_avl_tree->_t_avlroot._pt_parent = NULL;
        pt_avl_tree->_t_avlroot._pt_left = &pt_avl_tree->_t_avlroot;
        pt_avl_tree->_t_avlroot._pt_right = &pt_avl_tree->_t_avlroot;
    } else {
        pt_avl_tree->_t_avlroot._pt_left = _avl_tree_get_min_avlnode(pt_avl_tree->_t_avlroot._pt_parent);
        pt_avl_tree->_t_avlroot._pt_right = _avl_tree_get_max_avlnode(pt_avl_tree->_t_avlroot._pt_parent);
    }
}