/**
 * Test first iterator is less than or equal to second iterator.
 */
bool_t iterator_less_equal(iterator_t it_first, iterator_t it_second)
{
    if (iterator_less(it_first, it_second) || iterator_equal(it_first, it_second)) {
        return true;
    } else {
        return false;
    }
}
Example #2
0
/**
 * Move element range to deque begin.
 */
deque_iterator_t _deque_move_elem_to_begin(
    deque_t* pdeq_deque, deque_iterator_t it_begin, deque_iterator_t it_end, size_t t_step)
{
    assert(pdeq_deque != NULL);
    assert(_deque_is_inited(pdeq_deque));
    assert(_deque_iterator_belong_to_deque(pdeq_deque, it_begin));
    assert(_deque_iterator_belong_to_deque(pdeq_deque, it_end));
    assert(iterator_equal(it_begin, it_end) || _deque_iterator_before(it_begin, it_end));

    if(!iterator_equal(it_begin, it_end) && t_step != 0)
    {
        deque_iterator_t it_targetbegin;
        deque_iterator_t it_targetend;
        bool_t           b_result = false;

        it_targetbegin = it_begin;
        it_targetend = it_end;
        it_targetbegin = iterator_prev_n(it_targetbegin, t_step);
        it_targetend = iterator_prev_n(it_targetend, t_step);
        assert(_deque_iterator_before(it_targetbegin, it_targetend));

        for(;
            iterator_less(it_targetbegin, it_targetend) && iterator_less(it_begin, it_end);
            it_targetbegin = iterator_next(it_targetbegin), it_begin = iterator_next(it_begin))
        {
            b_result = _GET_DEQUE_TYPE_SIZE(pdeq_deque);
            _GET_DEQUE_TYPE_COPY_FUNCTION(pdeq_deque)(
                _deque_iterator_get_pointer_auxiliary(it_targetbegin),
                _deque_iterator_get_pointer_auxiliary(it_begin), &b_result);
            assert(b_result);
        }
        assert(iterator_equal(it_targetbegin, it_targetend) && iterator_equal(it_begin, it_end));

        return it_targetend;
    }
    else
    {
        return iterator_prev_n(it_end, t_step);
    }
}
Example #3
0
/**
 * Quick sort or heap sort for specify range.
 */
void _algo_intro_sort_if(
    random_access_iterator_t it_first, random_access_iterator_t it_last, bfun_t bfun_op, size_t t_depth, void* pv_value)
{
    iterator_t it_pivot;
    iterator_t it_begin;
    iterator_t it_end;
    iterator_t it_prev;
    bool_t     b_result = false;

    assert(_iterator_valid_range(it_first, it_last, _RANDOM_ACCESS_ITERATOR));
    assert(pv_value != NULL);
    assert(bfun_op != NULL);

    if (iterator_distance(it_first, it_last) < 2) {
        return;
    }

    if (t_depth == 0){
        /* do heap sort */
        algo_partial_sort_if(it_first, it_last, it_last, bfun_op);
        return;
    }
    t_depth--;

    it_pivot = iterator_advance(it_first, iterator_distance(it_first, it_last) / 2);
    it_prev = iterator_prev(it_last);
    it_pivot = _algo_median_of_three_if(it_first, it_pivot, it_prev, bfun_op);

    /* the pv_value must be string_t type when the container type is char* */
    if (strncmp(_iterator_get_typebasename(it_first), _C_STRING_TYPE, _TYPE_NAME_SIZE) == 0) {
        string_assign_cstr((string_t*)pv_value, (char*)iterator_get_pointer(it_pivot));
        it_begin = it_first;
        it_end = it_last;

        for (;;) {
            /* move begin */
            (*bfun_op)(iterator_get_pointer(it_begin), string_c_str((string_t*)pv_value), &b_result);
            while (b_result) {
                it_begin = iterator_next(it_begin);
                (*bfun_op)(iterator_get_pointer(it_begin), string_c_str((string_t*)pv_value), &b_result);
            }
            /* move end */
            it_end = iterator_prev(it_end);
            (*bfun_op)(string_c_str((string_t*)pv_value), iterator_get_pointer(it_end), &b_result);
            while (b_result) {
                it_end = iterator_prev(it_end);
                (*bfun_op)(string_c_str((string_t*)pv_value), iterator_get_pointer(it_end), &b_result);
            }

            /* across */
            if (!iterator_less(it_begin, it_end)) {
                it_pivot = it_begin;
                break;
            } else {
                algo_iter_swap(it_begin, it_end);
                it_begin = iterator_next(it_begin);
            }
        }
    } else {
        iterator_get_value(it_pivot, pv_value);
        it_begin = it_first;
        it_end = it_last;

        for (;;) {
            /* move begin */
            (*bfun_op)(iterator_get_pointer(it_begin), pv_value, &b_result);
            while (b_result) {
                it_begin = iterator_next(it_begin);
                (*bfun_op)(iterator_get_pointer(it_begin), pv_value, &b_result);
            }
            /* move end */
            it_end = iterator_prev(it_end);
            (*bfun_op)(pv_value, iterator_get_pointer(it_end), &b_result);
            while (b_result) {
                it_end = iterator_prev(it_end);
                (*bfun_op)(pv_value, iterator_get_pointer(it_end), &b_result);
            }

            /* across */
            if (!iterator_less(it_begin, it_end)) {
                it_pivot = it_begin;
                break;
            } else {
                algo_iter_swap(it_begin, it_end);
                it_begin = iterator_next(it_begin);
            }
        }
    }

    /* sort [it_first, it_pivot) */
    _algo_intro_sort_if(it_first, it_pivot, bfun_op, t_depth, pv_value);
    /* sort [it_pivot, it_last) */
    _algo_intro_sort_if(it_pivot, it_last, bfun_op, t_depth, pv_value);
}
/**
 * Test first iterator is greater than second iterator.
 */
bool_t iterator_greater(iterator_t it_first, iterator_t it_second)
{
    return iterator_less(it_second, it_first);
}