2006-03-04 18:16:49 +00:00
|
|
|
/*
|
2011-12-26 21:35:57 +00:00
|
|
|
htop - Vector.c
|
2011-05-26 16:35:07 +00:00
|
|
|
(C) 2004-2011 Hisham H. Muhammad
|
2020-10-05 07:51:32 +00:00
|
|
|
Released under the GNU GPLv2, see the COPYING file
|
2006-03-04 18:16:49 +00:00
|
|
|
in the source distribution for its full text.
|
|
|
|
*/
|
|
|
|
|
2006-05-30 13:45:40 +00:00
|
|
|
#include "Vector.h"
|
2006-03-04 18:16:49 +00:00
|
|
|
|
|
|
|
#include <assert.h>
|
2011-12-26 21:35:57 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2020-09-19 11:55:23 +00:00
|
|
|
|
|
|
|
#include "XUtils.h"
|
2006-03-04 18:16:49 +00:00
|
|
|
|
|
|
|
|
2020-10-04 15:55:08 +00:00
|
|
|
Vector* Vector_new(const ObjectClass* type, bool owner, int size) {
|
2006-05-30 13:45:40 +00:00
|
|
|
Vector* this;
|
2006-03-04 18:16:49 +00:00
|
|
|
|
2020-11-01 00:09:51 +00:00
|
|
|
if (size == DEFAULT_SIZE) {
|
2006-03-04 18:16:49 +00:00
|
|
|
size = 10;
|
2020-11-01 00:09:51 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 19:45:38 +00:00
|
|
|
assert(size > 0);
|
2016-02-02 14:53:02 +00:00
|
|
|
this = xMalloc(sizeof(Vector));
|
2006-03-04 18:16:49 +00:00
|
|
|
this->growthRate = size;
|
2016-02-02 14:53:02 +00:00
|
|
|
this->array = (Object**) xCalloc(size, sizeof(Object*));
|
2006-03-04 18:16:49 +00:00
|
|
|
this->arraySize = size;
|
|
|
|
this->items = 0;
|
2012-11-10 00:31:37 +00:00
|
|
|
this->type = type;
|
2006-03-04 18:16:49 +00:00
|
|
|
this->owner = owner;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2006-05-30 13:45:40 +00:00
|
|
|
void Vector_delete(Vector* this) {
|
2006-03-04 18:16:49 +00:00
|
|
|
if (this->owner) {
|
2020-11-01 00:09:51 +00:00
|
|
|
for (int i = 0; i < this->items; i++) {
|
|
|
|
if (this->array[i]) {
|
2012-12-05 15:12:20 +00:00
|
|
|
Object_delete(this->array[i]);
|
2020-11-01 00:09:51 +00:00
|
|
|
}
|
|
|
|
}
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
free(this->array);
|
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
2020-09-17 20:27:33 +00:00
|
|
|
#ifndef NDEBUG
|
2006-07-11 06:13:32 +00:00
|
|
|
|
2020-10-15 18:41:35 +00:00
|
|
|
static bool Vector_isConsistent(const Vector* this) {
|
2006-11-08 21:47:11 +00:00
|
|
|
assert(this->items <= this->arraySize);
|
2006-03-04 18:16:49 +00:00
|
|
|
if (this->owner) {
|
2020-11-01 00:09:51 +00:00
|
|
|
for (int i = 0; i < this->items; i++) {
|
|
|
|
if (this->array[i] && !Object_isA(this->array[i], this->type)) {
|
2006-03-04 18:16:49 +00:00
|
|
|
return false;
|
2020-11-01 00:09:51 +00:00
|
|
|
}
|
|
|
|
}
|
2006-03-04 18:16:49 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 18:41:35 +00:00
|
|
|
int Vector_count(const Vector* this) {
|
2006-11-12 21:52:14 +00:00
|
|
|
int items = 0;
|
|
|
|
for (int i = 0; i < this->items; i++) {
|
2020-11-01 00:09:51 +00:00
|
|
|
if (this->array[i]) {
|
2006-11-12 21:52:14 +00:00
|
|
|
items++;
|
2020-11-01 00:09:51 +00:00
|
|
|
}
|
2006-11-12 21:52:14 +00:00
|
|
|
}
|
|
|
|
assert(items == this->items);
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
2020-09-17 20:27:33 +00:00
|
|
|
Object* Vector_get(Vector* this, int idx) {
|
2020-10-31 19:57:22 +00:00
|
|
|
assert(idx >= 0 && idx < this->items);
|
2020-09-17 20:27:33 +00:00
|
|
|
assert(Vector_isConsistent(this));
|
2020-10-31 19:57:22 +00:00
|
|
|
assert(this->array[idx]);
|
2020-09-17 20:27:33 +00:00
|
|
|
return this->array[idx];
|
|
|
|
}
|
|
|
|
|
2020-10-15 18:41:35 +00:00
|
|
|
int Vector_size(const Vector* this) {
|
2020-09-17 20:27:33 +00:00
|
|
|
assert(Vector_isConsistent(this));
|
|
|
|
return this->items;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* NDEBUG */
|
2006-07-11 06:13:32 +00:00
|
|
|
|
2006-05-30 13:45:40 +00:00
|
|
|
void Vector_prune(Vector* this) {
|
|
|
|
assert(Vector_isConsistent(this));
|
2012-11-10 00:31:37 +00:00
|
|
|
if (this->owner) {
|
2014-04-21 22:01:15 +00:00
|
|
|
for (int i = 0; i < this->items; i++)
|
2012-11-10 00:31:37 +00:00
|
|
|
if (this->array[i]) {
|
2012-12-05 15:12:20 +00:00
|
|
|
Object_delete(this->array[i]);
|
2012-11-10 00:31:37 +00:00
|
|
|
//this->array[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
2006-03-04 18:16:49 +00:00
|
|
|
this->items = 0;
|
|
|
|
}
|
|
|
|
|
2020-10-15 19:45:38 +00:00
|
|
|
//static int comparisons = 0;
|
|
|
|
|
|
|
|
static void swap(Object** array, int indexA, int indexB) {
|
|
|
|
assert(indexA >= 0);
|
|
|
|
assert(indexB >= 0);
|
|
|
|
Object* tmp = array[indexA];
|
|
|
|
array[indexA] = array[indexB];
|
|
|
|
array[indexB] = tmp;
|
|
|
|
}
|
2011-11-18 06:08:56 +00:00
|
|
|
|
|
|
|
static int partition(Object** array, int left, int right, int pivotIndex, Object_Compare compare) {
|
2020-10-15 19:45:38 +00:00
|
|
|
const Object* pivotValue = array[pivotIndex];
|
2011-11-18 06:08:56 +00:00
|
|
|
swap(array, pivotIndex, right);
|
|
|
|
int storeIndex = left;
|
|
|
|
for (int i = left; i < right; i++) {
|
2020-10-15 19:45:38 +00:00
|
|
|
//comparisons++;
|
2011-11-18 06:08:56 +00:00
|
|
|
if (compare(array[i], pivotValue) <= 0) {
|
|
|
|
swap(array, i, storeIndex);
|
|
|
|
storeIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
swap(array, storeIndex, right);
|
|
|
|
return storeIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void quickSort(Object** array, int left, int right, Object_Compare compare) {
|
|
|
|
if (left >= right)
|
|
|
|
return;
|
2020-10-31 22:28:02 +00:00
|
|
|
|
|
|
|
int pivotIndex = (left + right) / 2;
|
2011-11-18 06:08:56 +00:00
|
|
|
int pivotNewIndex = partition(array, left, right, pivotIndex, compare);
|
|
|
|
quickSort(array, left, pivotNewIndex - 1, compare);
|
|
|
|
quickSort(array, pivotNewIndex + 1, right, compare);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If I were to use only one sorting algorithm for both cases, it would probably be this one:
|
|
|
|
/*
|
|
|
|
|
|
|
|
static void combSort(Object** array, int left, int right, Object_Compare compare) {
|
|
|
|
int gap = right - left;
|
|
|
|
bool swapped = true;
|
|
|
|
while ((gap > 1) || swapped) {
|
|
|
|
if (gap > 1) {
|
|
|
|
gap = (int)((double)gap / 1.247330950103979);
|
|
|
|
}
|
|
|
|
swapped = false;
|
|
|
|
for (int i = left; gap + i <= right; i++) {
|
|
|
|
comparisons++;
|
|
|
|
if (compare(array[i], array[i+gap]) > 0) {
|
|
|
|
swap(array, i, i+gap);
|
|
|
|
swapped = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void insertionSort(Object** array, int left, int right, Object_Compare compare) {
|
2020-10-31 22:28:02 +00:00
|
|
|
for (int i = left + 1; i <= right; i++) {
|
2020-10-15 19:45:38 +00:00
|
|
|
Object* t = array[i];
|
2011-11-18 06:08:56 +00:00
|
|
|
int j = i - 1;
|
|
|
|
while (j >= left) {
|
2020-10-15 19:45:38 +00:00
|
|
|
//comparisons++;
|
2011-11-18 06:08:56 +00:00
|
|
|
if (compare(array[j], t) <= 0)
|
|
|
|
break;
|
2020-10-31 22:28:02 +00:00
|
|
|
|
|
|
|
array[j + 1] = array[j];
|
2011-11-18 06:08:56 +00:00
|
|
|
j--;
|
|
|
|
}
|
2020-10-31 22:28:02 +00:00
|
|
|
array[j + 1] = t;
|
2011-11-18 06:08:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 19:26:09 +00:00
|
|
|
void Vector_quickSortCustomCompare(Vector* this, Object_Compare compare) {
|
|
|
|
assert(compare);
|
2006-05-30 13:45:40 +00:00
|
|
|
assert(Vector_isConsistent(this));
|
2020-10-21 19:26:09 +00:00
|
|
|
quickSort(this->array, 0, this->items - 1, compare);
|
2011-11-18 06:08:56 +00:00
|
|
|
assert(Vector_isConsistent(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Vector_insertionSort(Vector* this) {
|
2012-12-05 15:12:20 +00:00
|
|
|
assert(this->type->compare);
|
2011-11-18 06:08:56 +00:00
|
|
|
assert(Vector_isConsistent(this));
|
2012-12-05 15:12:20 +00:00
|
|
|
insertionSort(this->array, 0, this->items - 1, this->type->compare);
|
2006-05-30 13:45:40 +00:00
|
|
|
assert(Vector_isConsistent(this));
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2006-06-06 20:41:01 +00:00
|
|
|
static void Vector_checkArraySize(Vector* this) {
|
2006-05-30 13:45:40 +00:00
|
|
|
assert(Vector_isConsistent(this));
|
2006-03-04 18:16:49 +00:00
|
|
|
if (this->items >= this->arraySize) {
|
2012-11-10 00:31:37 +00:00
|
|
|
//int i;
|
|
|
|
//i = this->arraySize;
|
2006-03-04 18:16:49 +00:00
|
|
|
this->arraySize = this->items + this->growthRate;
|
2016-02-02 14:53:02 +00:00
|
|
|
this->array = (Object**) xRealloc(this->array, sizeof(Object*) * this->arraySize);
|
2012-11-10 00:31:37 +00:00
|
|
|
//for (; i < this->arraySize; i++)
|
|
|
|
// this->array[i] = NULL;
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
2006-05-30 13:45:40 +00:00
|
|
|
assert(Vector_isConsistent(this));
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2010-02-25 01:43:18 +00:00
|
|
|
void Vector_insert(Vector* this, int idx, void* data_) {
|
2006-03-04 18:16:49 +00:00
|
|
|
Object* data = data_;
|
2012-11-10 00:31:37 +00:00
|
|
|
assert(idx >= 0);
|
2012-12-05 15:12:20 +00:00
|
|
|
assert(Object_isA(data, this->type));
|
2006-05-30 13:45:40 +00:00
|
|
|
assert(Vector_isConsistent(this));
|
2015-02-03 21:32:07 +00:00
|
|
|
|
|
|
|
if (idx > this->items) {
|
|
|
|
idx = this->items;
|
|
|
|
}
|
2019-10-31 16:39:12 +00:00
|
|
|
|
2006-05-30 13:45:40 +00:00
|
|
|
Vector_checkArraySize(this);
|
2012-11-10 00:31:37 +00:00
|
|
|
//assert(this->array[this->items] == NULL);
|
2020-10-31 19:52:20 +00:00
|
|
|
if (idx < this->items) {
|
2020-10-06 15:19:40 +00:00
|
|
|
memmove(&this->array[idx + 1], &this->array[idx], (this->items - idx) * sizeof(this->array[0]));
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
2010-02-25 01:43:18 +00:00
|
|
|
this->array[idx] = data;
|
2006-03-04 18:16:49 +00:00
|
|
|
this->items++;
|
2006-05-30 13:45:40 +00:00
|
|
|
assert(Vector_isConsistent(this));
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2010-02-25 01:43:18 +00:00
|
|
|
Object* Vector_take(Vector* this, int idx) {
|
|
|
|
assert(idx >= 0 && idx < this->items);
|
2006-05-30 13:45:40 +00:00
|
|
|
assert(Vector_isConsistent(this));
|
2010-02-25 01:43:18 +00:00
|
|
|
Object* removed = this->array[idx];
|
2020-10-15 19:45:38 +00:00
|
|
|
assert(removed);
|
2006-03-04 18:16:49 +00:00
|
|
|
this->items--;
|
2020-10-31 19:52:20 +00:00
|
|
|
if (idx < this->items) {
|
2020-10-06 15:10:23 +00:00
|
|
|
memmove(&this->array[idx], &this->array[idx + 1], (this->items - idx) * sizeof(this->array[0]));
|
|
|
|
}
|
2012-11-10 00:31:37 +00:00
|
|
|
//this->array[this->items] = NULL;
|
2006-05-30 13:45:40 +00:00
|
|
|
assert(Vector_isConsistent(this));
|
2006-03-04 18:16:49 +00:00
|
|
|
return removed;
|
|
|
|
}
|
|
|
|
|
2010-02-25 01:43:18 +00:00
|
|
|
Object* Vector_remove(Vector* this, int idx) {
|
|
|
|
Object* removed = Vector_take(this, idx);
|
2006-03-04 18:16:49 +00:00
|
|
|
if (this->owner) {
|
2012-12-05 15:12:20 +00:00
|
|
|
Object_delete(removed);
|
2006-03-04 18:16:49 +00:00
|
|
|
return NULL;
|
2020-11-01 00:09:51 +00:00
|
|
|
} else {
|
2006-03-04 18:16:49 +00:00
|
|
|
return removed;
|
2020-11-01 00:09:51 +00:00
|
|
|
}
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2010-02-25 01:43:18 +00:00
|
|
|
void Vector_moveUp(Vector* this, int idx) {
|
|
|
|
assert(idx >= 0 && idx < this->items);
|
2006-05-30 13:45:40 +00:00
|
|
|
assert(Vector_isConsistent(this));
|
2020-11-01 00:09:51 +00:00
|
|
|
|
2010-02-25 01:43:18 +00:00
|
|
|
if (idx == 0)
|
2006-03-04 18:16:49 +00:00
|
|
|
return;
|
2020-11-01 00:09:51 +00:00
|
|
|
|
2010-02-25 01:43:18 +00:00
|
|
|
Object* temp = this->array[idx];
|
|
|
|
this->array[idx] = this->array[idx - 1];
|
|
|
|
this->array[idx - 1] = temp;
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2010-02-25 01:43:18 +00:00
|
|
|
void Vector_moveDown(Vector* this, int idx) {
|
|
|
|
assert(idx >= 0 && idx < this->items);
|
2006-05-30 13:45:40 +00:00
|
|
|
assert(Vector_isConsistent(this));
|
2020-11-01 00:09:51 +00:00
|
|
|
|
2010-02-25 01:43:18 +00:00
|
|
|
if (idx == this->items - 1)
|
2006-03-04 18:16:49 +00:00
|
|
|
return;
|
2020-11-01 00:09:51 +00:00
|
|
|
|
2010-02-25 01:43:18 +00:00
|
|
|
Object* temp = this->array[idx];
|
|
|
|
this->array[idx] = this->array[idx + 1];
|
|
|
|
this->array[idx + 1] = temp;
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2010-02-25 01:43:18 +00:00
|
|
|
void Vector_set(Vector* this, int idx, void* data_) {
|
2006-03-04 18:16:49 +00:00
|
|
|
Object* data = data_;
|
2012-12-05 15:12:20 +00:00
|
|
|
assert(idx >= 0);
|
2020-10-15 19:45:38 +00:00
|
|
|
assert(Object_isA(data, this->type));
|
2006-05-30 13:45:40 +00:00
|
|
|
assert(Vector_isConsistent(this));
|
2006-03-04 18:16:49 +00:00
|
|
|
|
2006-05-30 13:45:40 +00:00
|
|
|
Vector_checkArraySize(this);
|
2010-02-25 01:43:18 +00:00
|
|
|
if (idx >= this->items) {
|
2020-10-31 22:28:02 +00:00
|
|
|
this->items = idx + 1;
|
2006-03-04 18:16:49 +00:00
|
|
|
} else {
|
|
|
|
if (this->owner) {
|
2010-02-25 01:43:18 +00:00
|
|
|
Object* removed = this->array[idx];
|
2006-03-04 18:16:49 +00:00
|
|
|
assert (removed != NULL);
|
2020-10-27 10:02:34 +00:00
|
|
|
Object_delete(removed);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-25 01:43:18 +00:00
|
|
|
this->array[idx] = data;
|
2006-05-30 13:45:40 +00:00
|
|
|
assert(Vector_isConsistent(this));
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2008-03-09 08:58:38 +00:00
|
|
|
/*
|
|
|
|
|
|
|
|
static void Vector_merge(Vector* this, Vector* v2) {
|
2006-03-04 18:16:49 +00:00
|
|
|
int i;
|
2006-05-30 13:45:40 +00:00
|
|
|
assert(Vector_isConsistent(this));
|
2019-10-31 16:39:12 +00:00
|
|
|
|
2006-03-04 18:16:49 +00:00
|
|
|
for (i = 0; i < v2->items; i++)
|
2006-05-30 13:45:40 +00:00
|
|
|
Vector_add(this, v2->array[i]);
|
2006-03-04 18:16:49 +00:00
|
|
|
v2->items = 0;
|
2006-05-30 13:45:40 +00:00
|
|
|
Vector_delete(v2);
|
|
|
|
assert(Vector_isConsistent(this));
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2008-03-09 08:58:38 +00:00
|
|
|
*/
|
|
|
|
|
2006-05-30 13:45:40 +00:00
|
|
|
void Vector_add(Vector* this, void* data_) {
|
2006-03-04 18:16:49 +00:00
|
|
|
Object* data = data_;
|
2020-10-15 19:45:38 +00:00
|
|
|
assert(Object_isA(data, this->type));
|
2006-05-30 13:45:40 +00:00
|
|
|
assert(Vector_isConsistent(this));
|
2006-11-12 21:52:14 +00:00
|
|
|
int i = this->items;
|
2006-05-30 13:45:40 +00:00
|
|
|
Vector_set(this, this->items, data);
|
2020-10-31 22:28:02 +00:00
|
|
|
assert(this->items == i + 1); (void)(i);
|
2006-05-30 13:45:40 +00:00
|
|
|
assert(Vector_isConsistent(this));
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 18:41:35 +00:00
|
|
|
int Vector_indexOf(const Vector* this, const void* search_, Object_Compare compare) {
|
2020-10-14 13:19:23 +00:00
|
|
|
const Object* search = search_;
|
|
|
|
assert(Object_isA(search, this->type));
|
2012-12-05 15:12:20 +00:00
|
|
|
assert(compare);
|
2006-05-30 13:45:40 +00:00
|
|
|
assert(Vector_isConsistent(this));
|
2006-07-11 06:13:32 +00:00
|
|
|
for (int i = 0; i < this->items; i++) {
|
2020-10-15 18:41:35 +00:00
|
|
|
const Object* o = this->array[i];
|
2012-11-10 00:31:37 +00:00
|
|
|
assert(o);
|
2020-11-01 00:09:51 +00:00
|
|
|
if (compare(search, o) == 0) {
|
2006-03-04 18:16:49 +00:00
|
|
|
return i;
|
2020-11-01 00:09:51 +00:00
|
|
|
}
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2020-08-26 00:15:00 +00:00
|
|
|
|
|
|
|
void Vector_splice(Vector* this, Vector* from) {
|
|
|
|
assert(Vector_isConsistent(this));
|
|
|
|
assert(Vector_isConsistent(from));
|
|
|
|
assert(!(this->owner && from->owner));
|
|
|
|
|
2020-10-02 12:33:07 +00:00
|
|
|
int olditems = this->items;
|
2020-08-26 00:15:00 +00:00
|
|
|
this->items += from->items;
|
|
|
|
Vector_checkArraySize(this);
|
2020-11-01 00:09:51 +00:00
|
|
|
for (int j = 0; j < from->items; j++) {
|
2020-10-02 12:33:07 +00:00
|
|
|
this->array[olditems + j] = from->array[j];
|
2020-11-01 00:09:51 +00:00
|
|
|
}
|
2020-08-26 00:15:00 +00:00
|
|
|
}
|