~ubuntu-branches/ubuntu/warty/htop/warty

« back to all changes in this revision

Viewing changes to TypedVector.c

  • Committer: Bazaar Package Importer
  • Author(s): Bartosz Fenski
  • Date: 2004-06-20 10:33:13 UTC
  • Revision ID: james.westby@ubuntu.com-20040620103313-7tsm7dmr4obb3b5t
Tags: upstream-0.3.3
ImportĀ upstreamĀ versionĀ 0.3.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
htop
 
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.
 
6
*/
 
7
 
 
8
#include "TypedVector.h"
 
9
#include "Object.h"
 
10
#include <stdlib.h>
 
11
#include <string.h>
 
12
#include <assert.h>
 
13
#include <stdbool.h>
 
14
 
 
15
#include "debug.h"
 
16
 
 
17
/*{
 
18
typedef void(*TypedVector_procedure)(void*);
 
19
typedef int(*TypedVector_booleanFunction)(const Object*,const Object*);
 
20
 
 
21
typedef struct TypedVector_ {
 
22
   Object **array;
 
23
   int arraySize;
 
24
   int growthRate;
 
25
   int items;
 
26
   TypedVector_booleanFunction compareFunction;
 
27
   char* vectorType;
 
28
   bool owner;
 
29
} TypedVector;
 
30
 
 
31
}*/
 
32
 
 
33
TypedVector* TypedVector_new(char* vectorType_, bool owner) {
 
34
   TypedVector* this;
 
35
   int arraySize;
 
36
 
 
37
   arraySize = 10;
 
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;
 
43
   this->items = 0;
 
44
   this->vectorType = vectorType_;
 
45
   this->owner = owner;
 
46
   return this;
 
47
}
 
48
 
 
49
void TypedVector_delete(TypedVector* this) {
 
50
   if (this->owner) {
 
51
      for (int i = 0; i < this->items; i++)
 
52
         if (this->array[i])
 
53
            (this->array[i])->delete(this->array[i]);
 
54
   }
 
55
   free(this->array);
 
56
   free(this);
 
57
}
 
58
 
 
59
/* private */
 
60
bool TypedVector_isConsistent(TypedVector* this) {
 
61
   if (this->owner) {
 
62
      for (int i = 0; i < this->items; i++)
 
63
         if (this->array[i]->class != this->vectorType)
 
64
            return false;
 
65
      return true;
 
66
   } else {
 
67
      return true;
 
68
   }
 
69
}
 
70
 
 
71
void TypedVector_prune(TypedVector* this) {
 
72
   assert(TypedVector_isConsistent(this));
 
73
   int i;
 
74
 
 
75
   for (i = 0; i < this->items; i++)
 
76
      if (this->array[i]) {
 
77
         if (this->owner)
 
78
            (this->array[i])->delete(this->array[i]);
 
79
         this->array[i] = NULL;
 
80
      }
 
81
   this->items = 0;
 
82
}
 
83
 
 
84
int TypedVector_compareFunction(const Object* v1, const Object* v2) {
 
85
   return !(v1->equals(v1, v2));
 
86
}
 
87
 
 
88
void TypedVector_setCompareFunction(TypedVector* this, TypedVector_booleanFunction f) {
 
89
   this->compareFunction = f;
 
90
}
 
91
 
 
92
void TypedVector_sort(TypedVector* this) {
 
93
   assert(TypedVector_isConsistent(this));
 
94
   int i, j;
 
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];
 
99
      this->array[j+1] = t;
 
100
   }
 
101
   assert(TypedVector_isConsistent(this));
 
102
 
 
103
   /*
 
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;
 
110
         }
 
111
      }
 
112
   }
 
113
   */
 
114
}
 
115
 
 
116
/* private */
 
117
void TypedVector_checkArraySize(TypedVector* this) {
 
118
   assert(TypedVector_isConsistent(this));
 
119
   if (this->items >= this->arraySize) {
 
120
      int i;
 
121
      i = 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;
 
126
   }
 
127
   assert(TypedVector_isConsistent(this));
 
128
}
 
129
 
 
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];
 
136
   }
 
137
   this->array[index] = data;
 
138
   this->items++;
 
139
   assert(TypedVector_isConsistent(this));
 
140
}
 
141
 
 
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);
 
147
   if (this->owner) {
 
148
      removed->delete(removed);
 
149
   }
 
150
   this->items--;
 
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));
 
155
   if (this->owner)
 
156
      return NULL;
 
157
   else
 
158
      return removed;
 
159
}
 
160
 
 
161
void TypedVector_set(TypedVector* this, int index, void* data_) {
 
162
   assert(index >= 0);
 
163
   assert(((Object*)data_)->class == this->vectorType);
 
164
   Object* data = data_;
 
165
   assert(TypedVector_isConsistent(this));
 
166
 
 
167
   TypedVector_checkArraySize(this);
 
168
   if (index >= this->items) {
 
169
      this->items = index+1;
 
170
   }
 
171
   this->array[index] = data;
 
172
   assert(TypedVector_isConsistent(this));
 
173
}
 
174
 
 
175
Object* TypedVector_get(TypedVector* this, int index) {
 
176
   assert(index < this->items);
 
177
   assert(TypedVector_isConsistent(this));
 
178
   return this->array[index];
 
179
}
 
180
 
 
181
int TypedVector_size(TypedVector* this) {
 
182
   assert(TypedVector_isConsistent(this));
 
183
   return this->items;
 
184
}
 
185
 
 
186
void TypedVector_merge(TypedVector* this, TypedVector* v2) {
 
187
   int i;
 
188
   assert(TypedVector_isConsistent(this));
 
189
   
 
190
   for (i = 0; i < v2->items; i++)
 
191
      TypedVector_add(this, v2->array[i]);
 
192
   v2->items = 0;
 
193
   TypedVector_delete(v2);
 
194
   assert(TypedVector_isConsistent(this));
 
195
}
 
196
 
 
197
void TypedVector_add(TypedVector* this, void* data_) {
 
198
   assert(((Object*)data_)->class == this->vectorType);
 
199
   Object* data = data_;
 
200
   assert(TypedVector_isConsistent(this));
 
201
 
 
202
   TypedVector_set(this, this->items, data);
 
203
   assert(TypedVector_isConsistent(this));
 
204
}
 
205
 
 
206
int TypedVector_indexOf(TypedVector* this, void* search_) {
 
207
   assert(((Object*)search_)->class == this->vectorType);
 
208
   Object* search = search_;
 
209
   assert(TypedVector_isConsistent(this));
 
210
 
 
211
   int i;
 
212
 
 
213
   for (i = 0; i < this->items; i++) {
 
214
      Object* o = (Object*)this->array[i];
 
215
      if (o->equals(o, search))
 
216
         return i;
 
217
   }
 
218
   return -1;
 
219
}
 
220
 
 
221
void TypedVector_foreach(TypedVector* this, TypedVector_procedure f) {
 
222
   int i;
 
223
   assert(TypedVector_isConsistent(this));
 
224
 
 
225
   for (i = 0; i < this->items; i++)
 
226
      f(this->array[i]);
 
227
   assert(TypedVector_isConsistent(this));
 
228
}