diff --git a/include/pocketpy/common/vector.h b/include/pocketpy/common/vector.h index 93f784f4..1716e645 100644 --- a/include/pocketpy/common/vector.h +++ b/include/pocketpy/common/vector.h @@ -23,6 +23,7 @@ void* c11_vector__emplace(c11_vector* self); bool c11_vector__contains(const c11_vector* self, void* elem); void* c11_vector__submit(c11_vector* self, int* length); void c11_vector__swap(c11_vector* self, c11_vector* other); +int c11_vector__nextcap(c11_vector* self); #define c11__getitem(T, self, index) (((T*)(self)->data)[index]) #define c11__setitem(T, self, index, value) ((T*)(self)->data)[index] = value; @@ -30,7 +31,9 @@ void c11_vector__swap(c11_vector* self, c11_vector* other); #define c11_vector__push(T, self, elem) \ do { \ - if((self)->length == (self)->capacity) c11_vector__reserve((self), (self)->capacity * 2); \ + if((self)->length == (self)->capacity) { \ + c11_vector__reserve((self), c11_vector__nextcap((self))); \ + } \ ((T*)(self)->data)[(self)->length] = (elem); \ (self)->length++; \ } while(0) @@ -42,15 +45,19 @@ void c11_vector__swap(c11_vector* self, c11_vector* other); #define c11_vector__extend(T, self, p, size) \ do { \ int min_capacity = (self)->length + (size); \ - if((self)->capacity < min_capacity) \ - c11_vector__reserve((self), c11__max((self)->capacity * 2, min_capacity)); \ + if((self)->capacity < min_capacity) { \ + int nextcap = c11_vector__nextcap(self); \ + c11_vector__reserve((self), c11__max(nextcap, min_capacity)); \ + } \ memcpy((T*)(self)->data + (self)->length, (p), (size) * sizeof(T)); \ (self)->length += (size); \ } while(0) #define c11_vector__insert(T, self, index, elem) \ do { \ - if((self)->length == (self)->capacity) c11_vector__reserve((self), (self)->capacity * 2); \ + if((self)->length == (self)->capacity) { \ + c11_vector__reserve((self), c11_vector__nextcap(self)); \ + } \ T* p = (T*)(self)->data + (index); \ memmove(p + 1, p, ((self)->length - (index)) * sizeof(T)); \ *p = (elem); \ diff --git a/src/common/vector.c b/src/common/vector.c index 32b97fe4..af767262 100644 --- a/src/common/vector.c +++ b/src/common/vector.c @@ -40,7 +40,7 @@ void c11_vector__reserve(c11_vector* self, int capacity) { void c11_vector__clear(c11_vector* self) { self->length = 0; } void* c11_vector__emplace(c11_vector* self) { - if(self->length == self->capacity) c11_vector__reserve(self, self->capacity * 2); + if(self->length == self->capacity) { c11_vector__reserve(self, c11_vector__nextcap(self)); } void* p = (char*)self->data + (size_t)self->elem_size * (size_t)self->length; self->length++; return p; @@ -63,8 +63,17 @@ void* c11_vector__submit(c11_vector* self, int* length) { return retval; } -void c11_vector__swap(c11_vector *self, c11_vector *other){ +void c11_vector__swap(c11_vector* self, c11_vector* other) { c11_vector tmp = *self; *self = *other; *other = tmp; } + +int c11_vector__nextcap(c11_vector* self) { + if(self->capacity < 1024) { + return self->capacity * 2; + } else { + // increase by 25% + return self->capacity + (self->capacity >> 2); + } +} \ No newline at end of file