2006-03-04 18:16:49 +00:00
|
|
|
/*
|
|
|
|
htop
|
|
|
|
(C) 2004-2006 Hisham H. Muhammad
|
|
|
|
Released under the GNU GPL, see the COPYING file
|
|
|
|
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 "Object.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
#include "debug.h"
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
/*{
|
|
|
|
|
|
|
|
#ifndef DEFAULT_SIZE
|
|
|
|
#define DEFAULT_SIZE -1
|
|
|
|
#endif
|
|
|
|
|
2006-05-30 13:45:40 +00:00
|
|
|
typedef void(*Vector_procedure)(void*);
|
2006-03-04 18:16:49 +00:00
|
|
|
|
2006-05-30 13:45:40 +00:00
|
|
|
typedef struct Vector_ {
|
2006-03-04 18:16:49 +00:00
|
|
|
Object **array;
|
2006-07-11 06:13:32 +00:00
|
|
|
Object_Compare compare;
|
2006-03-04 18:16:49 +00:00
|
|
|
int arraySize;
|
|
|
|
int growthRate;
|
|
|
|
int items;
|
|
|
|
char* vectorType;
|
|
|
|
bool owner;
|
2006-05-30 13:45:40 +00:00
|
|
|
} Vector;
|
2006-03-04 18:16:49 +00:00
|
|
|
|
|
|
|
}*/
|
|
|
|
|
2006-07-11 06:13:32 +00:00
|
|
|
Vector* Vector_new(char* vectorType_, bool owner, int size, Object_Compare compare) {
|
2006-05-30 13:45:40 +00:00
|
|
|
Vector* this;
|
2006-03-04 18:16:49 +00:00
|
|
|
|
|
|
|
if (size == DEFAULT_SIZE)
|
|
|
|
size = 10;
|
2006-05-30 13:45:40 +00:00
|
|
|
this = (Vector*) malloc(sizeof(Vector));
|
2006-03-04 18:16:49 +00:00
|
|
|
this->growthRate = size;
|
|
|
|
this->array = (Object**) calloc(size, sizeof(Object*));
|
|
|
|
this->arraySize = size;
|
|
|
|
this->items = 0;
|
|
|
|
this->vectorType = vectorType_;
|
|
|
|
this->owner = owner;
|
2006-07-11 06:13:32 +00:00
|
|
|
this->compare = compare;
|
2006-03-04 18:16:49 +00:00
|
|
|
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) {
|
|
|
|
for (int i = 0; i < this->items; i++)
|
|
|
|
if (this->array[i])
|
|
|
|
(this->array[i])->delete(this->array[i]);
|
|
|
|
}
|
|
|
|
free(this->array);
|
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
2006-07-11 06:13:32 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
|
2006-06-06 20:41:01 +00:00
|
|
|
static inline bool Vector_isConsistent(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) {
|
|
|
|
for (int i = 0; i < this->items; i++)
|
|
|
|
if (this->array[i] && this->array[i]->class != this->vectorType)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-12 21:52:14 +00:00
|
|
|
int Vector_count(Vector* this) {
|
|
|
|
int items = 0;
|
|
|
|
for (int i = 0; i < this->items; i++) {
|
|
|
|
if (this->array[i])
|
|
|
|
items++;
|
|
|
|
}
|
|
|
|
assert(items == this->items);
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
2006-07-11 06:13:32 +00:00
|
|
|
#endif
|
|
|
|
|
2006-05-30 13:45:40 +00:00
|
|
|
void Vector_prune(Vector* this) {
|
|
|
|
assert(Vector_isConsistent(this));
|
2006-03-04 18:16:49 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < this->items; i++)
|
|
|
|
if (this->array[i]) {
|
|
|
|
if (this->owner)
|
|
|
|
(this->array[i])->delete(this->array[i]);
|
|
|
|
this->array[i] = NULL;
|
|
|
|
}
|
|
|
|
this->items = 0;
|
|
|
|
}
|
|
|
|
|
2006-05-30 13:45:40 +00:00
|
|
|
void Vector_sort(Vector* this) {
|
2006-07-11 06:13:32 +00:00
|
|
|
assert(this->compare);
|
2006-05-30 13:45:40 +00:00
|
|
|
assert(Vector_isConsistent(this));
|
2006-07-11 06:13:32 +00:00
|
|
|
Object_Compare compare = this->compare;
|
|
|
|
/* Insertion sort works best on mostly-sorted arrays. */
|
|
|
|
for (int i = 1; i < this->items; i++) {
|
|
|
|
int j;
|
2006-03-04 18:16:49 +00:00
|
|
|
void* t = this->array[i];
|
2006-07-11 06:13:32 +00:00
|
|
|
for (j = i-1; j >= 0 && compare(this->array[j], t) > 0; j--)
|
2006-03-04 18:16:49 +00:00
|
|
|
this->array[j+1] = this->array[j];
|
|
|
|
this->array[j+1] = t;
|
|
|
|
}
|
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) {
|
|
|
|
int i;
|
|
|
|
i = this->arraySize;
|
|
|
|
this->arraySize = this->items + this->growthRate;
|
|
|
|
this->array = (Object**) realloc(this->array, sizeof(Object*) * this->arraySize);
|
|
|
|
for (; i < this->arraySize; i++)
|
|
|
|
this->array[i] = NULL;
|
|
|
|
}
|
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
|
|
|
void Vector_insert(Vector* this, int index, void* data_) {
|
2006-03-04 18:16:49 +00:00
|
|
|
assert(index >= 0);
|
|
|
|
assert(((Object*)data_)->class == this->vectorType);
|
|
|
|
Object* data = data_;
|
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);
|
2006-03-04 18:16:49 +00:00
|
|
|
assert(this->array[this->items] == NULL);
|
|
|
|
for (int i = this->items; i >= index; i--) {
|
|
|
|
this->array[i+1] = this->array[i];
|
|
|
|
}
|
|
|
|
this->array[index] = data;
|
|
|
|
this->items++;
|
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
|
|
|
Object* Vector_take(Vector* this, int index) {
|
2006-03-04 18:16:49 +00:00
|
|
|
assert(index >= 0 && index < this->items);
|
2006-05-30 13:45:40 +00:00
|
|
|
assert(Vector_isConsistent(this));
|
2006-03-04 18:16:49 +00:00
|
|
|
Object* removed = this->array[index];
|
|
|
|
assert (removed != NULL);
|
|
|
|
this->items--;
|
|
|
|
for (int i = index; i < this->items; i++)
|
|
|
|
this->array[i] = this->array[i+1];
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2006-05-30 13:45:40 +00:00
|
|
|
Object* Vector_remove(Vector* this, int index) {
|
|
|
|
Object* removed = Vector_take(this, index);
|
2006-03-04 18:16:49 +00:00
|
|
|
if (this->owner) {
|
|
|
|
removed->delete(removed);
|
|
|
|
return NULL;
|
|
|
|
} else
|
|
|
|
return removed;
|
|
|
|
}
|
|
|
|
|
2006-05-30 13:45:40 +00:00
|
|
|
void Vector_moveUp(Vector* this, int index) {
|
2006-03-04 18:16:49 +00:00
|
|
|
assert(index >= 0 && index < this->items);
|
2006-05-30 13:45:40 +00:00
|
|
|
assert(Vector_isConsistent(this));
|
2006-03-04 18:16:49 +00:00
|
|
|
if (index == 0)
|
|
|
|
return;
|
|
|
|
Object* temp = this->array[index];
|
|
|
|
this->array[index] = this->array[index - 1];
|
|
|
|
this->array[index - 1] = temp;
|
|
|
|
}
|
|
|
|
|
2006-05-30 13:45:40 +00:00
|
|
|
void Vector_moveDown(Vector* this, int index) {
|
2006-03-04 18:16:49 +00:00
|
|
|
assert(index >= 0 && index < this->items);
|
2006-05-30 13:45:40 +00:00
|
|
|
assert(Vector_isConsistent(this));
|
2006-03-04 18:16:49 +00:00
|
|
|
if (index == this->items - 1)
|
|
|
|
return;
|
|
|
|
Object* temp = this->array[index];
|
|
|
|
this->array[index] = this->array[index + 1];
|
|
|
|
this->array[index + 1] = temp;
|
|
|
|
}
|
|
|
|
|
2006-05-30 13:45:40 +00:00
|
|
|
void Vector_set(Vector* this, int index, void* data_) {
|
2006-03-04 18:16:49 +00:00
|
|
|
assert(index >= 0);
|
|
|
|
assert(((Object*)data_)->class == this->vectorType);
|
|
|
|
Object* data = data_;
|
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);
|
2006-03-04 18:16:49 +00:00
|
|
|
if (index >= this->items) {
|
|
|
|
this->items = index+1;
|
|
|
|
} else {
|
|
|
|
if (this->owner) {
|
|
|
|
Object* removed = this->array[index];
|
|
|
|
assert (removed != NULL);
|
|
|
|
if (this->owner) {
|
|
|
|
removed->delete(removed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this->array[index] = data;
|
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
|
|
|
inline Object* Vector_get(Vector* this, int index) {
|
2006-03-04 18:16:49 +00:00
|
|
|
assert(index < this->items);
|
2006-05-30 13:45:40 +00:00
|
|
|
assert(Vector_isConsistent(this));
|
2006-03-04 18:16:49 +00:00
|
|
|
return this->array[index];
|
|
|
|
}
|
|
|
|
|
2006-05-30 13:45:40 +00:00
|
|
|
inline int Vector_size(Vector* this) {
|
|
|
|
assert(Vector_isConsistent(this));
|
2006-03-04 18:16:49 +00:00
|
|
|
return this->items;
|
|
|
|
}
|
|
|
|
|
2006-05-30 13:45:40 +00:00
|
|
|
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));
|
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
|
|
|
}
|
|
|
|
|
2006-05-30 13:45:40 +00:00
|
|
|
void Vector_add(Vector* this, void* data_) {
|
2006-03-04 18:16:49 +00:00
|
|
|
assert(data_ && ((Object*)data_)->class == this->vectorType);
|
|
|
|
Object* data = data_;
|
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);
|
2006-11-12 21:52:14 +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
|
|
|
}
|
|
|
|
|
2006-07-11 06:13:32 +00:00
|
|
|
inline int Vector_indexOf(Vector* this, void* search_, Object_Compare compare) {
|
2006-03-04 18:16:49 +00:00
|
|
|
assert(((Object*)search_)->class == this->vectorType);
|
2006-07-11 06:13:32 +00:00
|
|
|
assert(this->compare);
|
2006-03-04 18:16:49 +00:00
|
|
|
Object* search = search_;
|
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++) {
|
2006-03-04 18:16:49 +00:00
|
|
|
Object* o = (Object*)this->array[i];
|
2006-07-11 06:13:32 +00:00
|
|
|
if (o && compare(search, o) == 0)
|
2006-03-04 18:16:49 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-05-30 13:45:40 +00:00
|
|
|
void Vector_foreach(Vector* this, Vector_procedure f) {
|
2006-03-04 18:16:49 +00:00
|
|
|
int i;
|
2006-05-30 13:45:40 +00:00
|
|
|
assert(Vector_isConsistent(this));
|
2006-03-04 18:16:49 +00:00
|
|
|
|
|
|
|
for (i = 0; i < this->items; i++)
|
|
|
|
f(this->array[i]);
|
2006-05-30 13:45:40 +00:00
|
|
|
assert(Vector_isConsistent(this));
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|