3
(C) 2004 Hisham H. Muhammad
4
Released under the GNU GPL, see the COPYING file
5
in the source distribution for its full text.
8
#include "TypedVector.h"
18
typedef void(*TypedVector_procedure)(void*);
19
typedef int(*TypedVector_booleanFunction)(const Object*,const Object*);
21
typedef struct TypedVector_ {
26
TypedVector_booleanFunction compareFunction;
33
TypedVector* TypedVector_new(char* vectorType_, bool owner) {
38
this = (TypedVector*) malloc(sizeof(TypedVector));
39
this->growthRate = arraySize;
40
this->array = (Object**) calloc(arraySize, sizeof(Object*));
41
this->arraySize = arraySize;
42
this->compareFunction = TypedVector_compareFunction;
44
this->vectorType = vectorType_;
49
void TypedVector_delete(TypedVector* this) {
51
for (int i = 0; i < this->items; i++)
53
(this->array[i])->delete(this->array[i]);
60
bool TypedVector_isConsistent(TypedVector* this) {
62
for (int i = 0; i < this->items; i++)
63
if (this->array[i]->class != this->vectorType)
71
void TypedVector_prune(TypedVector* this) {
72
assert(TypedVector_isConsistent(this));
75
for (i = 0; i < this->items; i++)
78
(this->array[i])->delete(this->array[i]);
79
this->array[i] = NULL;
84
int TypedVector_compareFunction(const Object* v1, const Object* v2) {
85
return !(v1->equals(v1, v2));
88
void TypedVector_setCompareFunction(TypedVector* this, TypedVector_booleanFunction f) {
89
this->compareFunction = f;
92
void TypedVector_sort(TypedVector* this) {
93
assert(TypedVector_isConsistent(this));
95
for (i = 1; i < this->items; i++) {
96
void* t = this->array[i];
97
for (j = i-1; j >= 0 && this->compareFunction(this->array[j], t) < 0; j--)
98
this->array[j+1] = this->array[j];
101
assert(TypedVector_isConsistent(this));
104
for (int i = 0; i < this->items; i++) {
105
for (int j = i+1; j < this->items; j++) {
106
if (this->compareFunction(this->array[i], this->array[j]) < 0) {
107
void* tmp = this->array[i];
108
this->array[i] = this->array[j];
109
this->array[j] = tmp;
117
void TypedVector_checkArraySize(TypedVector* this) {
118
assert(TypedVector_isConsistent(this));
119
if (this->items >= this->arraySize) {
122
this->arraySize = this->items + this->growthRate;
123
this->array = (Object**) realloc(this->array, sizeof(Object*) * this->arraySize);
124
for (; i < this->arraySize; i++)
125
this->array[i] = NULL;
127
assert(TypedVector_isConsistent(this));
130
void TypedVector_insert(TypedVector* this, int index, Object* data) {
131
assert(index >= 0 && index < this->items);
132
TypedVector_checkArraySize(this);
133
assert(this->array[this->items] == NULL);
134
for (int i = this->items; i >= index; i--) {
135
this->array[i+1] = this->array[i];
137
this->array[index] = data;
139
assert(TypedVector_isConsistent(this));
142
Object* TypedVector_remove(TypedVector* this, int index) {
143
assert(index >= 0 && index < this->items);
144
assert(TypedVector_isConsistent(this));
145
Object* removed = this->array[index];
146
assert (removed != NULL);
148
removed->delete(removed);
151
for (int i = index; i < this->items; i++)
152
this->array[i] = this->array[i+1];
153
this->array[this->items] = NULL;
154
assert(TypedVector_isConsistent(this));
161
void TypedVector_set(TypedVector* this, int index, void* data_) {
163
assert(((Object*)data_)->class == this->vectorType);
164
Object* data = data_;
165
assert(TypedVector_isConsistent(this));
167
TypedVector_checkArraySize(this);
168
if (index >= this->items) {
169
this->items = index+1;
171
this->array[index] = data;
172
assert(TypedVector_isConsistent(this));
175
Object* TypedVector_get(TypedVector* this, int index) {
176
assert(index < this->items);
177
assert(TypedVector_isConsistent(this));
178
return this->array[index];
181
int TypedVector_size(TypedVector* this) {
182
assert(TypedVector_isConsistent(this));
186
void TypedVector_merge(TypedVector* this, TypedVector* v2) {
188
assert(TypedVector_isConsistent(this));
190
for (i = 0; i < v2->items; i++)
191
TypedVector_add(this, v2->array[i]);
193
TypedVector_delete(v2);
194
assert(TypedVector_isConsistent(this));
197
void TypedVector_add(TypedVector* this, void* data_) {
198
assert(((Object*)data_)->class == this->vectorType);
199
Object* data = data_;
200
assert(TypedVector_isConsistent(this));
202
TypedVector_set(this, this->items, data);
203
assert(TypedVector_isConsistent(this));
206
int TypedVector_indexOf(TypedVector* this, void* search_) {
207
assert(((Object*)search_)->class == this->vectorType);
208
Object* search = search_;
209
assert(TypedVector_isConsistent(this));
213
for (i = 0; i < this->items; i++) {
214
Object* o = (Object*)this->array[i];
215
if (o->equals(o, search))
221
void TypedVector_foreach(TypedVector* this, TypedVector_procedure f) {
223
assert(TypedVector_isConsistent(this));
225
for (i = 0; i < this->items; i++)
227
assert(TypedVector_isConsistent(this));