-
Notifications
You must be signed in to change notification settings - Fork 9
/
Vector.c
249 lines (213 loc) · 6.29 KB
/
Vector.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include "Prototypes.h"
/*{
#ifndef DEFAULT_SIZE
#define DEFAULT_SIZE -1
#endif
typedef void(*Vector_procedure)(void*);
typedef int(*Vector_booleanFunction)(const Object*,const Object*);
struct Vector_ {
ObjectClass* type;
Object **array;
int items;
int arraySize;
int growthRate;
Vector_booleanFunction compareFunction;
bool owner;
};
}*/
Vector* Vector_new(ObjectClass* type, bool owner, int size) {
if (size == DEFAULT_SIZE)
size = 10;
Vector* this = (Vector*) malloc(sizeof(Vector));
this->growthRate = size;
this->array = (Object**) calloc(size, sizeof(Object*));
this->arraySize = size;
this->compareFunction = Vector_compareFunction;
this->items = 0;
this->type = type;
this->owner = owner;
return this;
}
void Vector_delete(Vector* this) {
Vector_prune(this);
free(this->array);
free(this);
}
static inline bool Vector_isConsistent(Vector* this) {
if (this->owner) {
for (int i = 0; i < this->items; i++)
if ( ! Call(Object, instanceOf, this->array[i], this->type) )
return false;
return true;
} else {
return true;
}
}
void Vector_prune(Vector* this) {
assert(Vector_isConsistent(this));
int i;
for (i = 0; i < this->items; i++)
if (this->array[i]) {
if (this->owner)
Msg0(Object, delete, this->array[i]);
this->array[i] = NULL;
}
this->items = 0;
}
int Vector_compareFunction(const Object* v1, const Object* v2) {
return !(Msg(Object, equals, v1, v2));
}
void Vector_setCompareFunction(Vector* this, Vector_booleanFunction f) {
this->compareFunction = f;
}
void Vector_sort(Vector* this) {
assert(Vector_isConsistent(this));
int i, j;
/* Insertion sort -- behaves best when array is mostly sorted */
for (i = 1; i < this->items; i++) {
void* t = this->array[i];
for (j = i-1; j >= 0 && this->compareFunction(this->array[j], t) < 0; j--)
this->array[j+1] = this->array[j];
this->array[j+1] = t;
}
assert(Vector_isConsistent(this));
/* Bubble sort -- "reference" implementation */
/*
for (int i = 0; i < this->items; i++) {
for (int j = i+1; j < this->items; j++) {
if (this->compareFunction(this->array[i], this->array[j]) < 0) {
void* tmp = this->array[i];
this->array[i] = this->array[j];
this->array[j] = tmp;
}
}
}
*/
}
static inline 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;
this->array = (Object**) realloc(this->array, sizeof(Object*) * this->arraySize);
for (; i < this->arraySize; i++)
this->array[i] = NULL;
}
assert(Vector_isConsistent(this));
}
void Vector_insert(Vector* this, int index, void* data_) {
assert(index >= 0);
assert( Call(Object, instanceOf, data_, this->type) );
Object* data = data_;
assert(Vector_isConsistent(this));
Vector_checkArraySize(this);
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++;
assert(Vector_isConsistent(this));
}
Object* Vector_take(Vector* this, int index) {
assert(index >= 0 && index < this->items);
assert(Vector_isConsistent(this));
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;
assert(Vector_isConsistent(this));
return removed;
}
Object* Vector_remove(Vector* this, int index) {
Object* removed = Vector_take(this, index);
if (this->owner) {
Msg0(Object, delete, removed);
return NULL;
} else
return removed;
}
void Vector_moveUp(Vector* this, int index) {
assert(index >= 0 && index < this->items);
assert(Vector_isConsistent(this));
if (index == 0)
return;
Object* temp = this->array[index];
this->array[index] = this->array[index - 1];
this->array[index - 1] = temp;
}
void Vector_moveDown(Vector* this, int index) {
assert(index >= 0 && index < this->items);
assert(Vector_isConsistent(this));
if (index == this->items - 1)
return;
Object* temp = this->array[index];
this->array[index] = this->array[index + 1];
this->array[index + 1] = temp;
}
void Vector_set(Vector* this, int index, void* data_) {
assert(index >= 0);
assert( Call(Object, instanceOf, data_, this->type) );
Object* data = data_;
assert(Vector_isConsistent(this));
Vector_checkArraySize(this);
if (index >= this->items) {
this->items = index+1;
} else {
if (this->owner) {
Object* removed = this->array[index];
assert (removed != NULL);
if (this->owner)
Msg0(Object, delete, removed);
}
}
this->array[index] = data;
assert(Vector_isConsistent(this));
}
inline Object* Vector_get(Vector* this, int index) {
assert(index < this->items);
assert(Vector_isConsistent(this));
return this->array[index];
}
inline int Vector_size(Vector* this) {
assert(Vector_isConsistent(this));
return this->items;
}
void Vector_merge(Vector* this, Vector* v2) {
int i;
assert(Vector_isConsistent(this));
for (i = 0; i < v2->items; i++)
Vector_add(this, v2->array[i]);
v2->items = 0;
Vector_delete(v2);
assert(Vector_isConsistent(this));
}
void Vector_add(Vector* this, void* data_) {
assert( Call(Object, instanceOf, data_, this->type) );
Object* data = data_;
assert(Vector_isConsistent(this));
Vector_set(this, this->items, data);
assert(Vector_isConsistent(this));
}
int Vector_indexOf(Vector* this, void* search_) {
assert( Call(Object, instanceOf, search_, this->type) );
Object* search = search_;
assert(Vector_isConsistent(this));
int i;
for (i = 0; i < this->items; i++) {
Object* o = (Object*)this->array[i];
if (o && Msg(Object, equals, o, search))
return i;
}
return -1;
}
void Vector_foreach(Vector* this, Vector_procedure f) {
int i;
assert(Vector_isConsistent(this));
for (i = 0; i < this->items; i++)
f(this->array[i]);
assert(Vector_isConsistent(this));
}