Ejemplo n.º 1
0
/*
 * Specifies a new size of a vector.
 */
void vector_resize(vector_t* pvec_vector, size_t t_resize)
{
    vector_iterator_t it_cutpos;     /* the cut position */
    size_t            t_expsize = 0;
    _byte_t*          pby_oldfinish = NULL;

    assert(pvec_vector != NULL);
    assert(_vector_is_inited(pvec_vector));

    if(t_resize == vector_size(pvec_vector))
    {
        return;
    }
    else if(t_resize < vector_size(pvec_vector))
    {
        it_cutpos = vector_begin(pvec_vector);
        it_cutpos = iterator_next_n(it_cutpos, t_resize);
        vector_erase_range(pvec_vector, it_cutpos, vector_end(pvec_vector));
    }
    else
    {
        t_expsize = t_resize - vector_size(pvec_vector);
        if(t_resize > vector_capacity(pvec_vector))
        {
            vector_reserve(pvec_vector, _vector_calculate_new_capacity(vector_size(pvec_vector), t_expsize));
        }

        /* initialize new elements */
        pby_oldfinish = pvec_vector->_pby_finish;
        pvec_vector->_pby_finish += t_expsize * _GET_VECTOR_TYPE_SIZE(pvec_vector);
        _vector_init_elem_range_auxiliary(pvec_vector, pby_oldfinish, pvec_vector->_pby_finish);
    }
}
Ejemplo n.º 2
0
/**
 * Reset the size of vector elements, and filled element is from variable argument list.
 */
void _vector_resize_elem_varg(vector_t* pvec_vector, size_t t_resize, va_list val_elemlist)
{
    vector_iterator_t t_cutpos;     /* the cut position */
    size_t            t_expsize = 0;
    size_t            i = 0;
    void*             pv_varg = NULL;
    _byte_t*          pby_oldfinish = NULL;
    bool_t            b_result = false;

    assert(pvec_vector != NULL);
    assert(_vector_is_inited(pvec_vector));

    if(t_resize == vector_size(pvec_vector))
    {
        return;
    }
    else if(t_resize < vector_size(pvec_vector))
    {
        t_cutpos = vector_begin(pvec_vector);
        t_cutpos = iterator_next_n(t_cutpos, t_resize);
        vector_erase_range(pvec_vector, t_cutpos, vector_end(pvec_vector));
    }
    else
    {
        t_expsize = t_resize - vector_size(pvec_vector);
        if(t_resize > vector_capacity(pvec_vector))
        {
            vector_reserve(pvec_vector, _vector_calculate_new_capacity(vector_size(pvec_vector), t_expsize));
        }

        /* get varg value only once */
        pv_varg = _alloc_allocate(&pvec_vector->_t_allocator, _GET_VECTOR_TYPE_SIZE(pvec_vector), 1);
        assert(pv_varg != NULL);
        _vector_get_varg_value_auxiliary(pvec_vector, val_elemlist, pv_varg);

        /* initialize new elements */
        pby_oldfinish = pvec_vector->_pby_finish;
        pvec_vector->_pby_finish += t_expsize * _GET_VECTOR_TYPE_SIZE(pvec_vector);
        _vector_init_elem_range_auxiliary(pvec_vector, pby_oldfinish, pvec_vector->_pby_finish);
        /* copy value from varg to new elements */
        for(i = 0; i < t_expsize; ++i)
        {
            b_result = _GET_VECTOR_TYPE_SIZE(pvec_vector);
            _GET_VECTOR_TYPE_COPY_FUNCTION(pvec_vector)(pby_oldfinish + i * _GET_VECTOR_TYPE_SIZE(pvec_vector), pv_varg, &b_result);
            assert(b_result);
        }
        /* destroy varg */
        _vector_destroy_varg_value_auxiliary(pvec_vector, pv_varg);
        _alloc_deallocate(&pvec_vector->_t_allocator, pv_varg, _GET_VECTOR_TYPE_SIZE(pvec_vector), 1);
    }
}
Ejemplo n.º 3
0
/**
 * Initialize vector container with mutiple default element.
 */
void vector_init_n(vector_t* pvec_vector, size_t t_count)
{
    assert(pvec_vector != NULL);
    assert(_vector_is_created(pvec_vector));

    if(t_count > 0)
    {
        size_t t_newcapacity = _vector_calculate_new_capacity(0, t_count);

        pvec_vector->_pby_start = _alloc_allocate(&pvec_vector->_t_allocator, _GET_VECTOR_TYPE_SIZE(pvec_vector), t_newcapacity);
        assert(pvec_vector->_pby_start != NULL);
        pvec_vector->_pby_finish = pvec_vector->_pby_start + _GET_VECTOR_TYPE_SIZE(pvec_vector) * t_count;
        pvec_vector->_pby_endofstorage = pvec_vector->_pby_start + _GET_VECTOR_TYPE_SIZE(pvec_vector) * t_newcapacity;

        /* initialize all elements */
        _vector_init_elem_range_auxiliary(pvec_vector, pvec_vector->_pby_start, pvec_vector->_pby_finish);
    }
}
Ejemplo n.º 4
0
/**
 * Add specificed element from variable argument list at the end of vector container. 
 */
void _vector_push_back_varg(vector_t* pvec_vector, va_list val_elemlist)
{
    _byte_t*  pby_last = NULL;

    assert(pvec_vector != NULL);
    assert(_vector_is_inited(pvec_vector));

    /* if the remain capacity is less then the element count */
    if (vector_capacity(pvec_vector) == vector_size(pvec_vector)) {
        vector_reserve(pvec_vector, _vector_calculate_new_capacity(vector_size(pvec_vector), 1));
    }

    /* initialize the last element */
    pby_last = pvec_vector->_pby_finish;
    assert(pby_last != NULL);
    pvec_vector->_pby_finish += _GET_VECTOR_TYPE_SIZE(pvec_vector);
    _vector_init_elem_auxiliary(pvec_vector, pby_last);
    /* copy value from varg */
    _type_get_varg_value(&pvec_vector->_t_typeinfo, val_elemlist, pby_last);
}
Ejemplo n.º 5
0
/**
 * Insert multiple copys of element befor specificed position, the element is from variable argument list.
 */
vector_iterator_t _vector_insert_n_varg(vector_t* pvec_vector, vector_iterator_t it_pos, size_t t_count, va_list val_elemlist)
{
    void*  pv_varg = NULL;
    bool_t b_result = false;

    /* test the vector and iterator is valid */
    assert(pvec_vector != NULL);
    assert(_vector_is_inited(pvec_vector));
    assert(_vector_iterator_belong_to_vector(pvec_vector, it_pos));

    if (t_count > 0) {
        size_t i = 0;
        _byte_t*  pby_oldfinish = NULL;
        _byte_t*  pby_pos = NULL;   /* for initialize elments and insert elements */
        _byte_t*  pby_destpos = NULL;

        /* if the remain capacity is less then the element count */
        if (vector_size(pvec_vector) + t_count > vector_capacity(pvec_vector)) {
            size_t t_insertpos = iterator_distance(vector_begin(pvec_vector), it_pos);
            /* reserve the new size */
            vector_reserve(pvec_vector, _vector_calculate_new_capacity(vector_size(pvec_vector), t_count));
            it_pos = iterator_next_n(vector_begin(pvec_vector), t_insertpos);
        }

        /* insert the element counts of element to the position */
        /* 
         * move the elements from pos to the finish of vector to the 
         * memory that after the pos element count.
         * +-----------------------------------------+
         * |          |             |                |
         * +-----------------------------------------+
         * ^          ^             ^
         * |          |             |
         * start     it_pos         finish
         *                           _t_count__
         *                          /          \
         * +-----------------------------------------+
         * |          |             |new element|    |
         * +-----------------------------------------+
         * ^          ^             ^           ^
         * |          |             |           |
         * start     it_pos       pby_oldfinish  finish
         */
        /* initialize new elements */
        pby_oldfinish = pvec_vector->_pby_finish;
        assert(pby_oldfinish != NULL);
        pvec_vector->_pby_finish += t_count * _GET_VECTOR_TYPE_SIZE(pvec_vector);
        _vector_init_elem_range_auxiliary(pvec_vector, pby_oldfinish, pvec_vector->_pby_finish);
        /* move element from old finish to new finish */
        for (pby_pos = pby_oldfinish - _GET_VECTOR_TYPE_SIZE(pvec_vector),
             pby_destpos = pvec_vector->_pby_finish - _GET_VECTOR_TYPE_SIZE(pvec_vector);
             pby_pos >= _VECTOR_ITERATOR_COREPOS(it_pos);
             pby_pos -= _GET_VECTOR_TYPE_SIZE(pvec_vector),
             pby_destpos -= _GET_VECTOR_TYPE_SIZE(pvec_vector)) {
            b_result = _GET_VECTOR_TYPE_SIZE(pvec_vector);
            _GET_VECTOR_TYPE_COPY_FUNCTION(pvec_vector)(pby_destpos, pby_pos, &b_result);
            assert(b_result);
        }

        /* get varg value only once */
        pv_varg = _alloc_allocate(&pvec_vector->_t_allocator, _GET_VECTOR_TYPE_SIZE(pvec_vector), 1);
        assert(pv_varg != NULL);
        _vector_get_varg_value_auxiliary(pvec_vector, val_elemlist, pv_varg);
        /* copy value for varg */
        for (i = 0; i < t_count; ++i) {
            b_result = _GET_VECTOR_TYPE_SIZE(pvec_vector);
            _GET_VECTOR_TYPE_COPY_FUNCTION(pvec_vector)(_VECTOR_ITERATOR_COREPOS(it_pos) + i * _GET_VECTOR_TYPE_SIZE(pvec_vector), pv_varg, &b_result);
            assert(b_result);
        }
        /* destroy varg and free memory */
        _vector_destroy_varg_value_auxiliary(pvec_vector, pv_varg);
        _alloc_deallocate(&pvec_vector->_t_allocator, pv_varg, _GET_VECTOR_TYPE_SIZE(pvec_vector), 1);
    }

    return it_pos;
}
Ejemplo n.º 6
0
/**
 * Insert a range of elements into vector at a specificed position.
 */
void vector_insert_range(vector_t* pvec_vector, vector_iterator_t it_pos, vector_iterator_t it_begin, vector_iterator_t it_end)
{
    size_t            t_count = 0; /* the element count */
    bool_t            b_result = false;
    vector_iterator_t it_first;
    vector_iterator_t it_second;
    _byte_t*          pby_oldfinish = NULL;
    _byte_t*          pby_pos = NULL;
    _byte_t*          pby_destpos = NULL;

    /* test the vector and iterator is valid */
    assert(pvec_vector != NULL);
    assert(_vector_is_inited(pvec_vector));
    assert(_vector_iterator_belong_to_vector(pvec_vector, it_pos));
    /*assert(!_vector_iterator_belong_to_vector(pvec_vector, it_begin));*/
    /*assert(!_vector_iterator_belong_to_vector(pvec_vector, it_end));*/
    assert(_vector_same_vector_iterator_type(pvec_vector, it_begin));
    assert(_vector_same_vector_iterator_type(pvec_vector, it_end));
    assert(iterator_equal(it_begin, it_end) || _vector_iterator_before(it_begin, it_end));

    t_count = iterator_distance(it_begin, it_end);
    if(t_count > 0)
    {
        /* if the remain capacity is less then the element count */
        if(vector_size(pvec_vector) + t_count > vector_capacity(pvec_vector))
        {
            size_t t_distance = _VECTOR_ITERATOR_COREPOS(it_pos) - pvec_vector->_pby_start;
            /* reserve the new size */
            vector_reserve(pvec_vector, _vector_calculate_new_capacity(vector_size(pvec_vector), t_count));
            _VECTOR_ITERATOR_COREPOS(it_pos) = pvec_vector->_pby_start + t_distance;
        }

        /* initialize new elements */
        pby_oldfinish = pvec_vector->_pby_finish;
        assert(pby_oldfinish != NULL);
        pvec_vector->_pby_finish += t_count * _GET_VECTOR_TYPE_SIZE(pvec_vector);
        _vector_init_elem_range_auxiliary(pvec_vector, pby_oldfinish, pvec_vector->_pby_finish);

        /* move element from old finish to new finish */
        for(pby_pos = pby_oldfinish - _GET_VECTOR_TYPE_SIZE(pvec_vector),
            pby_destpos = pvec_vector->_pby_finish - _GET_VECTOR_TYPE_SIZE(pvec_vector);
            pby_pos >= _VECTOR_ITERATOR_COREPOS(it_pos);
            pby_pos -= _GET_VECTOR_TYPE_SIZE(pvec_vector),
            pby_destpos -= _GET_VECTOR_TYPE_SIZE(pvec_vector))
        {
            b_result = _GET_VECTOR_TYPE_SIZE(pvec_vector);
            _GET_VECTOR_TYPE_COPY_FUNCTION(pvec_vector)(pby_destpos, pby_pos, &b_result);
            assert(b_result);
        }

        /* insert element counts copys to the pos */
        for(it_first = it_pos, it_second = it_begin;
            !iterator_equal(it_second, it_end);
            it_first = iterator_next(it_first), it_second = iterator_next(it_second))
        {
            b_result = _GET_VECTOR_TYPE_SIZE(pvec_vector);
            _GET_VECTOR_TYPE_COPY_FUNCTION(pvec_vector)(_VECTOR_ITERATOR_COREPOS(it_first), _VECTOR_ITERATOR_COREPOS(it_second), &b_result);
            assert(b_result);
        }
        assert(_VECTOR_ITERATOR_COREPOS(it_first) == _VECTOR_ITERATOR_COREPOS(it_pos) + 
               (_VECTOR_ITERATOR_COREPOS(it_end) - _VECTOR_ITERATOR_COREPOS(it_begin)));
    }
}