Add a VECTOR_INSERT method to insert a range of elements at once

This commit is contained in:
Chris Robinson 2014-04-09 22:50:28 -07:00
parent 023981acb9
commit 90ae4b7c0f
3 changed files with 29 additions and 10 deletions

View File

@ -796,6 +796,26 @@ ALboolean vector_resize(void *ptr, size_t base_size, size_t obj_count, size_t ob
return AL_TRUE; return AL_TRUE;
} }
ALboolean vector_insert(void *ptr, size_t base_size, size_t obj_size, ptrdiff_t ins_elem, const void *datstart, ptrdiff_t numins)
{
vector_ *vecptr = ptr;
if(*vecptr || numins > 0)
{
if(!vector_reserve(vecptr, base_size, VECTOR_SIZE(*vecptr)+numins, obj_size, AL_TRUE))
return AL_FALSE;
if(ins_elem < (*vecptr)->Size)
{
memmove((char*)(*vecptr) + base_size + ((ins_elem+numins)*obj_size),
(char*)(*vecptr) + base_size + ((ins_elem )*obj_size),
((*vecptr)->Size-ins_elem)*obj_size);
}
memcpy((char*)(*vecptr) + base_size + (ins_elem*obj_size),
datstart, numins*obj_size);
(*vecptr)->Size += (ALsizei)numins;
}
return AL_TRUE;
}
extern inline ALsizei al_string_length(const_al_string str); extern inline ALsizei al_string_length(const_al_string str);
extern inline ALsizei al_string_empty(const_al_string str); extern inline ALsizei al_string_empty(const_al_string str);

View File

@ -346,20 +346,16 @@ static void GenModList_Destruct(GenModList *self)
static GenModList GenModList_clone(const GenModList *self) static GenModList GenModList_clone(const GenModList *self)
{ {
ALsizei count, i;
GenModList ret; GenModList ret;
GenModList_Construct(&ret); GenModList_Construct(&ret);
count = VECTOR_SIZE(self->gens); VECTOR_INSERT(ret.gens, VECTOR_ITER_END(ret.gens),
VECTOR_RESERVE(ret.gens, count); VECTOR_ITER_BEGIN(self->gens), VECTOR_ITER_END(self->gens)
for(i = 0;i < count;i++) );
VECTOR_PUSH_BACK(ret.gens, VECTOR_ELEM(self->gens, i)); VECTOR_INSERT(ret.mods, VECTOR_ITER_END(ret.mods),
VECTOR_ITER_BEGIN(self->mods), VECTOR_ITER_END(self->mods)
count = VECTOR_SIZE(self->mods); );
VECTOR_RESERVE(ret.mods, count);
for(i = 0;i < count;i++)
VECTOR_PUSH_BACK(ret.mods, VECTOR_ELEM(self->mods, i));
return ret; return ret;
} }

View File

@ -35,6 +35,9 @@ ALboolean vector_resize(void *ptr, size_t base_size, size_t obj_count, size_t ob
#define VECTOR_ITER_BEGIN(_x) ((_x)->Data) #define VECTOR_ITER_BEGIN(_x) ((_x)->Data)
#define VECTOR_ITER_END(_x) ((_x)->Data + VECTOR_SIZE((_x))) #define VECTOR_ITER_END(_x) ((_x)->Data + VECTOR_SIZE((_x)))
ALboolean vector_insert(void *ptr, size_t base_size, size_t obj_size, ptrdiff_t ins_elem, const void *datstart, ptrdiff_t numins);
#define VECTOR_INSERT(_x, _i, _s, _e) (vector_insert(&(_x), sizeof(*(_x)), sizeof((_x)->Data[0]), (_i)-VECTOR_ITER_BEGIN(_x), (_s), (_e)-(_s)))
#define VECTOR_PUSH_BACK(_x, _obj) (vector_reserve(&(_x), sizeof(*(_x)), VECTOR_SIZE(_x)+1, sizeof((_x)->Data[0]), AL_FALSE) && \ #define VECTOR_PUSH_BACK(_x, _obj) (vector_reserve(&(_x), sizeof(*(_x)), VECTOR_SIZE(_x)+1, sizeof((_x)->Data[0]), AL_FALSE) && \
(((_x)->Data[(_x)->Size++] = (_obj)),AL_TRUE)) (((_x)->Data[(_x)->Size++] = (_obj)),AL_TRUE))
#define VECTOR_POP_BACK(_x) ((void)((_x)->Size--)) #define VECTOR_POP_BACK(_x) ((void)((_x)->Size--))