Exemplo n.º 1
0
/**
 * Destroy vector container auxiliary function.
 */
void _vector_destroy_auxiliary(vector_t* pvec_vector)
{
    vector_iterator_t it_iter;
    vector_iterator_t it_begin;
    vector_iterator_t it_end;
    bool_t            b_result = false;

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

    /* destroy all elements */
    it_begin = vector_begin(pvec_vector);
    it_end = vector_end(pvec_vector);
    for (it_iter = it_begin; !iterator_equal(it_iter, it_end); it_iter = iterator_next(it_iter)) {
        b_result = _GET_VECTOR_TYPE_SIZE(pvec_vector);
        _GET_VECTOR_TYPE_DESTROY_FUNCTION(pvec_vector)(_VECTOR_ITERATOR_COREPOS(it_iter), &b_result);
        assert(b_result);
    }
    /* free vector memory */
    if (pvec_vector->_pby_start != NULL) {
        _alloc_deallocate(&pvec_vector->_t_allocator, pvec_vector->_pby_start, _GET_VECTOR_TYPE_SIZE(pvec_vector), 
            (pvec_vector->_pby_endofstorage - pvec_vector->_pby_start) / _GET_VECTOR_TYPE_SIZE(pvec_vector));
    }
    _alloc_destroy(&pvec_vector->_t_allocator);

    pvec_vector->_pby_start = NULL;
    pvec_vector->_pby_finish = NULL;
    pvec_vector->_pby_endofstorage = NULL;
}
Exemplo n.º 2
0
/**
 * Set vector capacity.
 */
void vector_reserve(vector_t* pvec_vector, size_t t_reservesize)
{
    _byte_t* pby_reservemem = NULL; /* new memory for reserve */
    _byte_t* pby_newstart = NULL;
    _byte_t* pby_newfinish = NULL;
    _byte_t* pby_newendofstorage = NULL;
    _byte_t* pby_newpos = NULL;
    _byte_t* pby_oldpos = NULL;
    size_t   t_oldsize = 0;
    size_t   t_oldcapacity = 0;
    bool_t   b_result = false;

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

    if(vector_capacity(pvec_vector) < t_reservesize)
    {
        /* allocate the new vector with reserve size */
        pby_reservemem = _alloc_allocate(&pvec_vector->_t_allocator, _GET_VECTOR_TYPE_SIZE(pvec_vector), t_reservesize);
        assert(pby_reservemem != NULL);
        /* get the new position */
        t_oldsize = pvec_vector->_pby_finish - pvec_vector->_pby_start;
        t_oldcapacity = pvec_vector->_pby_endofstorage - pvec_vector->_pby_start;
        pby_newstart = pby_reservemem;
        pby_newfinish = pby_reservemem + t_oldsize;
        pby_newendofstorage = pby_reservemem + _GET_VECTOR_TYPE_SIZE(pvec_vector) * t_reservesize;

        /* initialize new elements */
        _vector_init_elem_range_auxiliary(pvec_vector, pby_newstart, pby_newfinish);

        /* copy elements from old memory and destroy those */
        for(pby_newpos = pby_newstart, pby_oldpos = pvec_vector->_pby_start;
            pby_newpos < pby_newfinish && pby_oldpos < pvec_vector->_pby_finish;
            pby_newpos += _GET_VECTOR_TYPE_SIZE(pvec_vector),
            pby_oldpos += _GET_VECTOR_TYPE_SIZE(pvec_vector))
        {
            /* copy from old vector_t memory */
            b_result = _GET_VECTOR_TYPE_SIZE(pvec_vector);
            _GET_VECTOR_TYPE_COPY_FUNCTION(pvec_vector)(pby_newpos, pby_oldpos, &b_result);
            assert(b_result);
            /* destroy old vector_t memory */
            b_result = _GET_VECTOR_TYPE_SIZE(pvec_vector);
            _GET_VECTOR_TYPE_DESTROY_FUNCTION(pvec_vector)(pby_oldpos, &b_result);
            assert(b_result);
        }
        assert(pby_newpos == pby_newfinish && pby_oldpos == pvec_vector->_pby_finish);

        /* free the old vector element */
        if(pvec_vector->_pby_start != NULL)
        {
            _alloc_deallocate(&pvec_vector->_t_allocator, pvec_vector->_pby_start,
                _GET_VECTOR_TYPE_SIZE(pvec_vector), t_oldcapacity / _GET_VECTOR_TYPE_SIZE(pvec_vector));
        }
        pvec_vector->_pby_start = pby_newstart;
        pvec_vector->_pby_finish = pby_newfinish;
        pvec_vector->_pby_endofstorage = pby_newendofstorage;
    }
}
Exemplo n.º 3
0
/**
 * Destroy data, the data type and vector element data type are same.
 */
void _vector_destroy_varg_value_auxiliary(vector_t* pvec_vector, void* pv_varg)
{
    bool_t b_result = false;

    assert(pvec_vector != NULL);
    assert(pv_varg != NULL);
    assert(_vector_is_inited(pvec_vector) || _vector_is_created(pvec_vector));

    /* destroy varg value and free memory */
    b_result = _GET_VECTOR_TYPE_SIZE(pvec_vector);
    _GET_VECTOR_TYPE_DESTROY_FUNCTION(pvec_vector)(pv_varg, &b_result);
    assert(b_result);
}
Exemplo n.º 4
0
/**
 * Delete the element at the end of vector.
 */
void vector_pop_back(vector_t* pvec_vector)
{
    bool_t b_result = false;

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

    /* destroy last element */
    b_result = _GET_VECTOR_TYPE_SIZE(pvec_vector);
    _GET_VECTOR_TYPE_DESTROY_FUNCTION(pvec_vector)(pvec_vector->_pby_finish - _GET_VECTOR_TYPE_SIZE(pvec_vector), &b_result);
    assert(b_result);
    pvec_vector->_pby_finish -= _GET_VECTOR_TYPE_SIZE(pvec_vector);
}
Exemplo n.º 5
0
/**
 * Removes a range of elements in vector from specificed position.
 */
vector_iterator_t vector_erase_range(vector_t* pvec_vector, vector_iterator_t it_begin, vector_iterator_t it_end)
{
    size_t            t_erasesize = 0;
    bool_t            b_result = false;
    vector_iterator_t it_iter;

    assert(pvec_vector != NULL);
    assert(_vector_is_inited(pvec_vector));
    assert(_vector_iterator_belong_to_vector(pvec_vector, it_begin));
    assert(_vector_iterator_belong_to_vector(pvec_vector, it_end));
    assert(iterator_equal(it_begin, it_end) || _vector_iterator_before(it_begin, it_end));

    if(iterator_equal(it_begin, it_end))
    {
        return it_end;
    }

    it_iter = it_begin;
    t_erasesize = iterator_distance(it_begin, it_end);

    for(; !iterator_equal(it_end, vector_end(pvec_vector)); it_begin = iterator_next(it_begin), it_end = iterator_next(it_end))
    {
        b_result = _GET_VECTOR_TYPE_SIZE(pvec_vector);
        _GET_VECTOR_TYPE_COPY_FUNCTION(pvec_vector)(_VECTOR_ITERATOR_COREPOS(it_begin), _VECTOR_ITERATOR_COREPOS(it_end), &b_result);
        assert(b_result);
    }
    assert(_VECTOR_ITERATOR_COREPOS(it_begin) == pvec_vector->_pby_finish - t_erasesize * _GET_VECTOR_TYPE_SIZE(pvec_vector));

    /* destroy the deleted elements */
    for(; !iterator_equal(it_begin, it_end); it_begin = iterator_next(it_begin))
    {
        b_result = _GET_VECTOR_TYPE_SIZE(pvec_vector);
        _GET_VECTOR_TYPE_DESTROY_FUNCTION(pvec_vector)(_VECTOR_ITERATOR_COREPOS(it_begin), &b_result);
        assert(b_result);
    }
    pvec_vector->_pby_finish -= t_erasesize * _GET_VECTOR_TYPE_SIZE(pvec_vector);

    return it_iter;
}