Vector.c 6.46 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
2
3
4
5
6
7
/*
htop
(C) 2004-2006 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/

8
#include "Vector.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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

23
typedef void(*Vector_procedure)(void*);
Hisham Muhammad's avatar
Hisham Muhammad committed
24

25
typedef struct Vector_ {
Hisham Muhammad's avatar
Hisham Muhammad committed
26
   Object **array;
27
   Object_Compare compare;
Hisham Muhammad's avatar
Hisham Muhammad committed
28
29
30
31
32
   int arraySize;
   int growthRate;
   int items;
   char* vectorType;
   bool owner;
33
} Vector;
Hisham Muhammad's avatar
Hisham Muhammad committed
34
35
36

}*/

37
Vector* Vector_new(char* vectorType_, bool owner, int size, Object_Compare compare) {
38
   Vector* this;
Hisham Muhammad's avatar
Hisham Muhammad committed
39
40
41

   if (size == DEFAULT_SIZE)
      size = 10;
42
   this = (Vector*) malloc(sizeof(Vector));
Hisham Muhammad's avatar
Hisham Muhammad committed
43
44
45
46
47
48
   this->growthRate = size;
   this->array = (Object**) calloc(size, sizeof(Object*));
   this->arraySize = size;
   this->items = 0;
   this->vectorType = vectorType_;
   this->owner = owner;
49
   this->compare = compare;
Hisham Muhammad's avatar
Hisham Muhammad committed
50
51
52
   return this;
}

53
void Vector_delete(Vector* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
54
55
56
57
58
59
60
61
62
   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);
}

63
64
#ifdef DEBUG

65
static inline bool Vector_isConsistent(Vector* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
66
   assert(this->items <= this->arraySize);
Hisham Muhammad's avatar
Hisham Muhammad committed
67
68
69
70
71
72
73
74
75
76
   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;
   }
}

Hisham Muhammad's avatar
Hisham Muhammad committed
77
78
79
80
81
82
83
84
85
86
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;
}

87
88
#endif

89
90
void Vector_prune(Vector* this) {
   assert(Vector_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
91
92
93
94
95
96
97
98
99
100
101
   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;
}

102
void Vector_sort(Vector* this) {
103
   assert(this->compare);
104
   assert(Vector_isConsistent(this));
105
106
107
108
   Object_Compare compare = this->compare;
   /* Insertion sort works best on mostly-sorted arrays. */
   for (int i = 1; i < this->items; i++) {
      int j;
Hisham Muhammad's avatar
Hisham Muhammad committed
109
      void* t = this->array[i];
110
      for (j = i-1; j >= 0 && compare(this->array[j], t) > 0; j--)
Hisham Muhammad's avatar
Hisham Muhammad committed
111
112
113
         this->array[j+1] = this->array[j];
      this->array[j+1] = t;
   }
114
   assert(Vector_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
115
116
}

117
static void Vector_checkArraySize(Vector* this) {
118
   assert(Vector_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
119
120
121
122
123
124
125
126
   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;
   }
127
   assert(Vector_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
128
129
}

130
void Vector_insert(Vector* this, int index, void* data_) {
Hisham Muhammad's avatar
Hisham Muhammad committed
131
132
133
   assert(index >= 0);
   assert(((Object*)data_)->class == this->vectorType);
   Object* data = data_;
134
   assert(Vector_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
135
   
136
   Vector_checkArraySize(this);
Hisham Muhammad's avatar
Hisham Muhammad committed
137
138
139
140
141
142
   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++;
143
   assert(Vector_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
144
145
}

146
Object* Vector_take(Vector* this, int index) {
Hisham Muhammad's avatar
Hisham Muhammad committed
147
   assert(index >= 0 && index < this->items);
148
   assert(Vector_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
149
150
151
152
153
154
   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;
155
   assert(Vector_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
156
157
158
   return removed;
}

159
160
Object* Vector_remove(Vector* this, int index) {
   Object* removed = Vector_take(this, index);
Hisham Muhammad's avatar
Hisham Muhammad committed
161
162
163
164
165
166
167
   if (this->owner) {
      removed->delete(removed);
      return NULL;
   } else
      return removed;
}

168
void Vector_moveUp(Vector* this, int index) {
Hisham Muhammad's avatar
Hisham Muhammad committed
169
   assert(index >= 0 && index < this->items);
170
   assert(Vector_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
171
172
173
174
175
176
177
   if (index == 0)
      return;
   Object* temp = this->array[index];
   this->array[index] = this->array[index - 1];
   this->array[index - 1] = temp;
}

178
void Vector_moveDown(Vector* this, int index) {
Hisham Muhammad's avatar
Hisham Muhammad committed
179
   assert(index >= 0 && index < this->items);
180
   assert(Vector_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
181
182
183
184
185
186
187
   if (index == this->items - 1)
      return;
   Object* temp = this->array[index];
   this->array[index] = this->array[index + 1];
   this->array[index + 1] = temp;
}

188
void Vector_set(Vector* this, int index, void* data_) {
Hisham Muhammad's avatar
Hisham Muhammad committed
189
190
191
   assert(index >= 0);
   assert(((Object*)data_)->class == this->vectorType);
   Object* data = data_;
192
   assert(Vector_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
193

194
   Vector_checkArraySize(this);
Hisham Muhammad's avatar
Hisham Muhammad committed
195
196
197
198
199
200
201
202
203
204
205
206
   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;
207
   assert(Vector_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
208
209
}

210
inline Object* Vector_get(Vector* this, int index) {
Hisham Muhammad's avatar
Hisham Muhammad committed
211
   assert(index < this->items);
212
   assert(Vector_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
213
214
215
   return this->array[index];
}

216
217
inline int Vector_size(Vector* this) {
   assert(Vector_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
218
219
220
   return this->items;
}

221
void Vector_merge(Vector* this, Vector* v2) {
Hisham Muhammad's avatar
Hisham Muhammad committed
222
   int i;
223
   assert(Vector_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
224
225
   
   for (i = 0; i < v2->items; i++)
226
      Vector_add(this, v2->array[i]);
Hisham Muhammad's avatar
Hisham Muhammad committed
227
   v2->items = 0;
228
229
   Vector_delete(v2);
   assert(Vector_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
230
231
}

232
void Vector_add(Vector* this, void* data_) {
Hisham Muhammad's avatar
Hisham Muhammad committed
233
234
   assert(data_ && ((Object*)data_)->class == this->vectorType);
   Object* data = data_;
235
   assert(Vector_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
236
   int i = this->items;
237
   Vector_set(this, this->items, data);
Hisham Muhammad's avatar
Hisham Muhammad committed
238
   assert(this->items == i+1); (void)(i);
239
   assert(Vector_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
240
241
}

242
inline int Vector_indexOf(Vector* this, void* search_, Object_Compare compare) {
Hisham Muhammad's avatar
Hisham Muhammad committed
243
   assert(((Object*)search_)->class == this->vectorType);
244
   assert(this->compare);
Hisham Muhammad's avatar
Hisham Muhammad committed
245
   Object* search = search_;
246
   assert(Vector_isConsistent(this));
247
   for (int i = 0; i < this->items; i++) {
Hisham Muhammad's avatar
Hisham Muhammad committed
248
      Object* o = (Object*)this->array[i];
249
      if (o && compare(search, o) == 0)
Hisham Muhammad's avatar
Hisham Muhammad committed
250
251
252
253
254
         return i;
   }
   return -1;
}

255
void Vector_foreach(Vector* this, Vector_procedure f) {
Hisham Muhammad's avatar
Hisham Muhammad committed
256
   int i;
257
   assert(Vector_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
258
259
260

   for (i = 0; i < this->items; i++)
      f(this->array[i]);
261
   assert(Vector_isConsistent(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
262
}