Skip to content

Commit

Permalink
resize vector in Vector_set and Vector_splice.
Browse files Browse the repository at this point in the history
  • Loading branch information
lbckmnn authored and cgzones committed Apr 4, 2023
1 parent c707b0e commit 45b334c
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions Vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,12 @@ void Vector_insertionSort(Vector* this) {
assert(Vector_isConsistent(this));
}

static void Vector_checkArraySize(Vector* this) {
assert(Vector_isConsistent(this));
if (this->items >= this->arraySize) {
//int i;
//i = this->arraySize;
this->arraySize = this->items + this->growthRate;
static void Vector_resizeIfNecessary(Vector* this, int newSize) {
assert(newSize >= 0);
if (newSize > this->arraySize) {
assert(Vector_isConsistent(this));
int oldSize = this->arraySize;
this->arraySize = newSize + this->growthRate;
this->array = (Object**) xRealloc(this->array, sizeof(Object*) * this->arraySize);
//for (; i < this->arraySize; i++)
// this->array[i] = NULL;
Expand All @@ -215,7 +215,7 @@ void Vector_insert(Vector* this, int idx, void* data_) {
idx = this->items;
}

Vector_checkArraySize(this);
Vector_resizeIfNecessary(this, this->items + 1);
//assert(this->array[this->items] == NULL);
if (idx < this->items) {
memmove(&this->array[idx + 1], &this->array[idx], (this->items - idx) * sizeof(this->array[0]));
Expand Down Expand Up @@ -331,14 +331,15 @@ void Vector_set(Vector* this, int idx, void* data_) {
assert(Object_isA(data, this->type));
assert(Vector_isConsistent(this));

Vector_checkArraySize(this);
Vector_resizeIfNecessary(this, idx + 1);
if (idx >= this->items) {
this->items = idx + 1;
} else {
if (this->owner) {
Object* removed = this->array[idx];
assert (removed != NULL);
Object_delete(removed);
if (removed != NULL) {
Object_delete(removed);
}
}
}
this->array[idx] = data;
Expand Down Expand Up @@ -391,8 +392,8 @@ void Vector_splice(Vector* this, Vector* from) {
assert(!this->owner);

int olditems = this->items;
Vector_resizeIfNecessary(this, this->items + from->items);
this->items += from->items;
Vector_checkArraySize(this);
for (int j = 0; j < from->items; j++) {
this->array[olditems + j] = from->array[j];
}
Expand Down

0 comments on commit 45b334c

Please sign in to comment.