resize vector in Vector_set and Vector_splice.

portage
Lukas Beckmann 3 years ago committed by cgzones
parent c707b0eb52
commit 45b334c851
  1. 23
      Vector.c

@ -192,12 +192,12 @@ void Vector_insertionSort(Vector* this) {
assert(Vector_isConsistent(this)); assert(Vector_isConsistent(this));
} }
static void Vector_checkArraySize(Vector* this) { static void Vector_resizeIfNecessary(Vector* this, int newSize) {
assert(Vector_isConsistent(this)); assert(newSize >= 0);
if (this->items >= this->arraySize) { if (newSize > this->arraySize) {
//int i; assert(Vector_isConsistent(this));
//i = this->arraySize; int oldSize = this->arraySize;
this->arraySize = this->items + this->growthRate; this->arraySize = newSize + this->growthRate;
this->array = (Object**) xRealloc(this->array, sizeof(Object*) * this->arraySize); this->array = (Object**) xRealloc(this->array, sizeof(Object*) * this->arraySize);
//for (; i < this->arraySize; i++) //for (; i < this->arraySize; i++)
// this->array[i] = NULL; // this->array[i] = NULL;
@ -215,7 +215,7 @@ void Vector_insert(Vector* this, int idx, void* data_) {
idx = this->items; idx = this->items;
} }
Vector_checkArraySize(this); Vector_resizeIfNecessary(this, this->items + 1);
//assert(this->array[this->items] == NULL); //assert(this->array[this->items] == NULL);
if (idx < this->items) { if (idx < this->items) {
memmove(&this->array[idx + 1], &this->array[idx], (this->items - idx) * sizeof(this->array[0])); memmove(&this->array[idx + 1], &this->array[idx], (this->items - idx) * sizeof(this->array[0]));
@ -331,14 +331,15 @@ void Vector_set(Vector* this, int idx, void* data_) {
assert(Object_isA(data, this->type)); assert(Object_isA(data, this->type));
assert(Vector_isConsistent(this)); assert(Vector_isConsistent(this));
Vector_checkArraySize(this); Vector_resizeIfNecessary(this, idx + 1);
if (idx >= this->items) { if (idx >= this->items) {
this->items = idx + 1; this->items = idx + 1;
} else { } else {
if (this->owner) { if (this->owner) {
Object* removed = this->array[idx]; Object* removed = this->array[idx];
assert (removed != NULL); if (removed != NULL) {
Object_delete(removed); Object_delete(removed);
}
} }
} }
this->array[idx] = data; this->array[idx] = data;
@ -391,8 +392,8 @@ void Vector_splice(Vector* this, Vector* from) {
assert(!this->owner); assert(!this->owner);
int olditems = this->items; int olditems = this->items;
Vector_resizeIfNecessary(this, this->items + from->items);
this->items += from->items; this->items += from->items;
Vector_checkArraySize(this);
for (int j = 0; j < from->items; j++) { for (int j = 0; j < from->items; j++) {
this->array[olditems + j] = from->array[j]; this->array[olditems + j] = from->array[j];
} }

Loading…
Cancel
Save