autoc 1.2 → 1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES +4 -0
  3. data/README +3 -1
  4. data/doc/AutoC.html +23 -7
  5. data/doc/AutoC/Code.html +3 -3
  6. data/doc/AutoC/Collection.html +289 -76
  7. data/doc/AutoC/HashMap.html +44 -46
  8. data/doc/AutoC/HashSet.html +20 -17
  9. data/doc/AutoC/List.html +56 -92
  10. data/doc/AutoC/Module.html +2 -2
  11. data/doc/AutoC/Module/File.html +2 -2
  12. data/doc/AutoC/Module/Header.html +6 -4
  13. data/doc/AutoC/Module/Source.html +26 -26
  14. data/doc/AutoC/Priority.html +2 -2
  15. data/doc/AutoC/Queue.html +30 -92
  16. data/doc/AutoC/Reference.html +217 -61
  17. data/doc/AutoC/String.html +1393 -0
  18. data/doc/AutoC/Type.html +240 -128
  19. data/doc/AutoC/UserDefinedType.html +688 -47
  20. data/doc/AutoC/Vector.html +154 -62
  21. data/doc/_index.html +9 -2
  22. data/doc/class_list.html +1 -1
  23. data/doc/file.CHANGES.html +10 -2
  24. data/doc/file.README.html +5 -3
  25. data/doc/index.html +5 -3
  26. data/doc/method_list.html +235 -97
  27. data/doc/top-level-namespace.html +2 -2
  28. data/lib/autoc.rb +3 -1
  29. data/lib/autoc/code.rb +3 -2
  30. data/lib/autoc/collection.rb +36 -40
  31. data/lib/autoc/collection/hash_map.rb +10 -14
  32. data/lib/autoc/collection/hash_set.rb +12 -11
  33. data/lib/autoc/collection/list.rb +21 -11
  34. data/lib/autoc/collection/queue.rb +5 -8
  35. data/lib/autoc/collection/vector.rb +28 -12
  36. data/lib/autoc/string.rb +492 -0
  37. data/lib/autoc/type.rb +155 -66
  38. data/test/test.rb +157 -35
  39. data/test/test_char_string.rb +270 -0
  40. data/test/test_int_list.rb +35 -0
  41. data/test/test_int_vector.rb +34 -0
  42. data/test/test_value_hash_map.rb +162 -0
  43. data/test/test_value_hash_set.rb +173 -0
  44. data/test/test_value_list.rb +193 -0
  45. data/test/test_value_queue.rb +275 -0
  46. data/test/test_value_vector.rb +155 -0
  47. data/test/value.rb +80 -0
  48. metadata +15 -8
  49. data/test/test.c +0 -1041
  50. data/test/test.h +0 -41
  51. data/test/test_auto.c +0 -3407
  52. data/test/test_auto.h +0 -765
@@ -1,41 +0,0 @@
1
- #ifndef test_h
2
- #define test_h
3
-
4
-
5
- typedef struct ValueType ValueType;
6
-
7
-
8
- struct ValueType {
9
- int value;
10
- void* block;
11
- };
12
-
13
-
14
- void ValueTypeCtorEx(ValueType*, int);
15
-
16
-
17
- #define ValueTypeCtor(obj) ValueTypeCtorRef(&obj)
18
- void ValueTypeCtorRef(ValueType*);
19
-
20
-
21
- #define ValueTypeDtor(obj) ValueTypeDtorRef(&obj)
22
- void ValueTypeDtorRef(ValueType*);
23
-
24
-
25
- #define ValueTypeCopy(dst, src) ValueTypeCopyRef(&dst, &src)
26
- void ValueTypeCopyRef(ValueType*, ValueType*);
27
-
28
-
29
- #define ValueTypeEqual(lt, rt) ValueTypeEqualRef(&lt, &rt)
30
- int ValueTypeEqualRef(ValueType*, ValueType*);
31
-
32
-
33
- #define ValueTypeLess(lt, rt) ValueTypeLessRef(&lt, &rt)
34
- int ValueTypeLessRef(ValueType*, ValueType*);
35
-
36
-
37
- #define ValueTypeIdentify(obj) ValueTypeIdentifyRef(&obj)
38
- size_t ValueTypeIdentifyRef(ValueType*);
39
-
40
-
41
- #endif
@@ -1,3407 +0,0 @@
1
-
2
- /* AUTOMATICALLY GENERATED SOURCE FILE. DO NOT MODIFY. */
3
- #include "test_auto.h"
4
-
5
- #include <limits.h>
6
- #define AUTOC_MIN(a,b) ((a) > (b) ? (b) : (a))
7
- #define AUTOC_MAX(a,b) ((a) > (b) ? (a) : (b))
8
- #define AUTOC_RCYCLE(x) (((x) << 1) | ((x) >> (sizeof(x)*CHAR_BIT - 1))) /* NOTE : valid for unsigned types only */
9
-
10
- #define AUTOC_COUNTER(p) (*(size_t*)((char*)(p) + sizeof(int)))
11
- int* IntNew() {
12
- int* self = (int*)malloc(sizeof(int) + sizeof(size_t));
13
- assert(self);
14
- ((*self) = 0);
15
- AUTOC_COUNTER(self) = 1;
16
- return self;
17
- }
18
- int* IntRef(int* self) {
19
- assert(self);
20
- ++AUTOC_COUNTER(self);
21
- return self;
22
- }
23
- void IntFree(int* self) {
24
- assert(self);
25
- if(--AUTOC_COUNTER(self) == 0) {
26
- ;
27
- free(self);
28
- }
29
- }
30
- #undef AUTOC_COUNTER
31
-
32
- #define AUTOC_VALID_VALUE 1
33
- #define AUTOC_VALID_KEY 2
34
- #define AUTOC_OWNED_VALUE 4
35
- #define AUTOC_OWNED_KEY 8
36
- static IntStrMapEntry IntStrMapEntryKeyOnlyRef(int* key) {
37
- IntStrMapEntry entry;
38
- entry.key = *key;
39
- entry.flags = AUTOC_VALID_KEY;
40
- return entry;
41
- }
42
- static IntStrMapEntry IntStrMapEntryKeyValueRef(int* key, const char ** value) {
43
- IntStrMapEntry entry;
44
- entry.key = *key;
45
- entry.value = *value;
46
- entry.flags = (AUTOC_VALID_KEY | AUTOC_VALID_VALUE);
47
- return entry;
48
- }
49
- #define IntStrMapEntryIdentify(obj) IntStrMapEntryIdentifyRef(&obj)
50
- static size_t IntStrMapEntryIdentifyRef(IntStrMapEntry* entry) {
51
- return ((size_t)(entry->key));
52
- }
53
- #define IntStrMapEntryEqual(lt, rt) IntStrMapEntryEqualRef(&lt, &rt)
54
- static int IntStrMapEntryEqualRef(IntStrMapEntry* lt, IntStrMapEntry* rt) {
55
- return ((lt->key) == (rt->key));
56
- }
57
- #define IntStrMapEntryCopy(dst, src) IntStrMapEntryCopyRef(&dst, &src)
58
- static void IntStrMapEntryCopyRef(IntStrMapEntry* dst, IntStrMapEntry* src) {
59
- assert(src->flags & AUTOC_VALID_KEY);
60
- dst->flags = (AUTOC_VALID_KEY | AUTOC_OWNED_KEY);
61
- ((dst->key) = (src->key));
62
- if(src->flags & AUTOC_VALID_VALUE) {
63
- dst->flags |= (AUTOC_VALID_VALUE | AUTOC_OWNED_VALUE);
64
- ((dst->value) = (src->value));
65
- }
66
- }
67
- #define IntStrMapEntryDtor(obj) IntStrMapEntryDtorRef(&obj)
68
- static void IntStrMapEntryDtorRef(IntStrMapEntry* entry) {
69
- assert(entry->flags & AUTOC_VALID_KEY);
70
- if(entry->flags & AUTOC_OWNED_KEY) ;
71
- if(entry->flags & AUTOC_VALID_VALUE && entry->flags & AUTOC_OWNED_VALUE) ;
72
- }
73
- static IntStrMapEntry* IntStrMapItGetEntryRef(IntStrMapIt*);
74
- static int IntStrMapContainsAllOf(IntStrMap* self, IntStrMap* other) {
75
- IntStrMapIt it;
76
- IntStrMapItCtor(&it, self);
77
- while(IntStrMapItMove(&it)) {
78
- int found = 0;
79
- IntStrMapEntry* e = IntStrMapItGetEntryRef(&it);
80
- if(IntStrMapContainsKey(other, e->key)) {
81
- const char * other_value = IntStrMapGet(other, e->key);
82
- found = ((e->value) == (other_value));
83
- ;
84
- }
85
- if(!found) return 0;
86
- }
87
- return 1;
88
- }
89
-
90
- #define _IntStrMapSetCtor(self) IntStrMapSetCtor(&self)
91
- #define _IntStrMapSetDtor(self) IntStrMapSetDtor(&self)
92
- #define _IntStrMapSetIdentify(self) IntStrMapSetIdentify(&self)
93
- #define _IntStrMapSetCopy(dst,src) IntStrMapSetCopy(&dst,&src)
94
- #define _IntStrMapSetEqual(lt,rt) IntStrMapSetEqual(&lt,&rt)
95
- #define _IntStrMapSetLess(lt,rt) IntStrMapSetLess(&lt,&rt)
96
-
97
- AUTOC_STATIC void IntStrMapSetCtor(IntStrMapSet*);
98
- AUTOC_STATIC void IntStrMapSetDtor(IntStrMapSet*);
99
- AUTOC_STATIC void IntStrMapSetCopy(IntStrMapSet*,IntStrMapSet*);
100
- AUTOC_STATIC int IntStrMapSetEqual(IntStrMapSet*,IntStrMapSet*);
101
- AUTOC_STATIC size_t IntStrMapSetIdentify(IntStrMapSet*);
102
- AUTOC_STATIC void IntStrMapSetPurge(IntStrMapSet*);
103
- AUTOC_STATIC int IntStrMapSetContains(IntStrMapSet*, IntStrMapEntry);
104
- AUTOC_STATIC IntStrMapEntry IntStrMapSetGet(IntStrMapSet*, IntStrMapEntry);
105
- AUTOC_STATIC size_t IntStrMapSetSize(IntStrMapSet*);
106
- #define IntStrMapSetEmpty(self) (IntStrMapSetSize(self) == 0)
107
- AUTOC_STATIC int IntStrMapSetPut(IntStrMapSet*, IntStrMapEntry);
108
- AUTOC_STATIC int IntStrMapSetReplace(IntStrMapSet*, IntStrMapEntry);
109
- AUTOC_STATIC int IntStrMapSetRemove(IntStrMapSet*, IntStrMapEntry);
110
- AUTOC_STATIC void IntStrMapSetExclude(IntStrMapSet*, IntStrMapSet*);
111
- AUTOC_STATIC void IntStrMapSetRetain(IntStrMapSet*, IntStrMapSet*);
112
- AUTOC_STATIC void IntStrMapSetInclude(IntStrMapSet*, IntStrMapSet*);
113
- AUTOC_STATIC void IntStrMapSetInvert(IntStrMapSet*, IntStrMapSet*);
114
- AUTOC_STATIC void IntStrMapSetItCtor(IntStrMapSetIt*, IntStrMapSet*);
115
- AUTOC_STATIC int IntStrMapSetItMove(IntStrMapSetIt*);
116
- AUTOC_STATIC IntStrMapEntry IntStrMapSetItGet(IntStrMapSetIt*);
117
-
118
- #define _IntStrMapSetListCtor(self) IntStrMapSetListCtor(&self)
119
- #define _IntStrMapSetListDtor(self) IntStrMapSetListDtor(&self)
120
- #define _IntStrMapSetListIdentify(self) IntStrMapSetListIdentify(&self)
121
- #define _IntStrMapSetListCopy(dst,src) IntStrMapSetListCopy(&dst,&src)
122
- #define _IntStrMapSetListEqual(lt,rt) IntStrMapSetListEqual(&lt,&rt)
123
- #define _IntStrMapSetListLess(lt,rt) IntStrMapSetListLess(&lt,&rt)
124
-
125
- AUTOC_STATIC void IntStrMapSetListCtor(IntStrMapSetList*);
126
- AUTOC_STATIC void IntStrMapSetListDtor(IntStrMapSetList*);
127
- AUTOC_STATIC void IntStrMapSetListCopy(IntStrMapSetList*,IntStrMapSetList*);
128
- AUTOC_STATIC int IntStrMapSetListEqual(IntStrMapSetList*,IntStrMapSetList*);
129
- AUTOC_STATIC size_t IntStrMapSetListIdentify(IntStrMapSetList*);
130
- AUTOC_STATIC void IntStrMapSetListPurge(IntStrMapSetList*);
131
- AUTOC_STATIC IntStrMapEntry IntStrMapSetListPeek(IntStrMapSetList*);
132
- AUTOC_STATIC IntStrMapEntry IntStrMapSetListPop(IntStrMapSetList*);
133
- AUTOC_STATIC void IntStrMapSetListPush(IntStrMapSetList*, IntStrMapEntry);
134
- AUTOC_STATIC int IntStrMapSetListContains(IntStrMapSetList*, IntStrMapEntry);
135
- AUTOC_STATIC IntStrMapEntry IntStrMapSetListFind(IntStrMapSetList*, IntStrMapEntry);
136
- #define IntStrMapSetListReplace(self, with) IntStrMapSetListReplaceEx(self, with, 1)
137
- #define IntStrMapSetListReplaceAll(self, with) IntStrMapSetListReplaceEx(self, with, -1)
138
- AUTOC_STATIC int IntStrMapSetListReplaceEx(IntStrMapSetList*, IntStrMapEntry, int);
139
- #define IntStrMapSetListRemove(self, what) IntStrMapSetListRemoveEx(self, what, 1)
140
- #define IntStrMapSetListRemoveAll(self, what) IntStrMapSetListRemoveEx(self, what, -1)
141
- AUTOC_STATIC int IntStrMapSetListRemoveEx(IntStrMapSetList*, IntStrMapEntry, int);
142
- AUTOC_STATIC size_t IntStrMapSetListSize(IntStrMapSetList*);
143
- #define IntStrMapSetListEmpty(self) (IntStrMapSetListSize(self) == 0)
144
- AUTOC_STATIC void IntStrMapSetListItCtor(IntStrMapSetListIt*, IntStrMapSetList*);
145
- AUTOC_STATIC int IntStrMapSetListItMove(IntStrMapSetListIt*);
146
- AUTOC_STATIC IntStrMapEntry IntStrMapSetListItGet(IntStrMapSetListIt*);
147
-
148
- AUTOC_STATIC IntStrMapEntry* IntStrMapSetListItGetRef(IntStrMapSetListIt*);
149
- AUTOC_STATIC void IntStrMapSetListCtor(IntStrMapSetList* self) {
150
- assert(self);
151
- self->head_node = NULL;
152
- self->node_count = 0;
153
- }
154
- AUTOC_STATIC void IntStrMapSetListDtor(IntStrMapSetList* self) {
155
- IntStrMapSetListNode* node;
156
- assert(self);
157
- node = self->head_node;
158
- while(node) {
159
- IntStrMapSetListNode* this_node = node;
160
- node = node->next_node;
161
- IntStrMapEntryDtor(this_node->element);
162
- free(this_node);
163
- }
164
- }
165
- AUTOC_STATIC void IntStrMapSetListCopy(IntStrMapSetList* dst,IntStrMapSetList* src) {
166
- IntStrMapSetListIt it;
167
- assert(src);
168
- assert(dst);
169
- IntStrMapSetListCtor(dst);
170
- IntStrMapSetListItCtor(&it, src);
171
- while(IntStrMapSetListItMove(&it)) {
172
- IntStrMapSetListPush(dst, *IntStrMapSetListItGetRef(&it));
173
- }
174
- }
175
- AUTOC_STATIC int IntStrMapSetListEqual(IntStrMapSetList* lt,IntStrMapSetList* rt) {
176
- if(IntStrMapSetListSize(lt) == IntStrMapSetListSize(rt)) {
177
- IntStrMapSetListIt lit, rit;
178
- IntStrMapSetListItCtor(&lit, lt);
179
- IntStrMapSetListItCtor(&rit, rt);
180
- while(IntStrMapSetListItMove(&lit) && IntStrMapSetListItMove(&rit)) {
181
- int equal;
182
- IntStrMapEntry* le;
183
- IntStrMapEntry* re;
184
- le = IntStrMapSetListItGetRef(&lit);
185
- re = IntStrMapSetListItGetRef(&rit);
186
- equal = IntStrMapEntryEqual(*le,*re);
187
- if(!equal) return 0;
188
- }
189
- return 1;
190
- } else
191
- return 0;
192
- }
193
- AUTOC_STATIC size_t IntStrMapSetListIdentify(IntStrMapSetList* self) {
194
- IntStrMapSetListNode* node;
195
- size_t result = 0;
196
- assert(self);
197
- for(node = self->head_node; node != NULL; node = node->next_node) {
198
- result ^= IntStrMapEntryIdentify(node->element);
199
- result = AUTOC_RCYCLE(result);
200
- }
201
- return result;
202
- }
203
- AUTOC_STATIC void IntStrMapSetListPurge(IntStrMapSetList* self) {
204
- IntStrMapSetListDtor(self);
205
- IntStrMapSetListCtor(self);
206
- }
207
- AUTOC_STATIC IntStrMapEntry IntStrMapSetListPeek(IntStrMapSetList* self) {
208
- IntStrMapEntry result;
209
- assert(self);
210
- assert(!IntStrMapSetListEmpty(self));
211
- IntStrMapEntryCopy(result,self->head_node->element);
212
- return result;
213
- }
214
- AUTOC_STATIC IntStrMapEntry IntStrMapSetListPop(IntStrMapSetList* self) {
215
- IntStrMapSetListNode* node;
216
- IntStrMapEntry result;
217
- assert(self);
218
- assert(!IntStrMapSetListEmpty(self));
219
- node = self->head_node;
220
- result = node->element;
221
- self->head_node = self->head_node->next_node;
222
- --self->node_count;
223
- free(node);
224
- return result;
225
- }
226
- AUTOC_STATIC void IntStrMapSetListPush(IntStrMapSetList* self, IntStrMapEntry element) {
227
- IntStrMapSetListNode* node;
228
- assert(self);
229
- node = (IntStrMapSetListNode*)malloc(sizeof(IntStrMapSetListNode));
230
- assert(node);
231
- IntStrMapEntryCopy(node->element,element);
232
- node->next_node = self->head_node;
233
- self->head_node = node;
234
- ++self->node_count;
235
- }
236
- AUTOC_STATIC int IntStrMapSetListContains(IntStrMapSetList* self, IntStrMapEntry what) {
237
- IntStrMapSetListNode* node;
238
- int found = 0;
239
- assert(self);
240
- node = self->head_node;
241
- while(node) {
242
- if(IntStrMapEntryEqual(node->element,what)) {
243
- found = 1;
244
- break;
245
- }
246
- node = node->next_node;
247
- }
248
- return found;
249
- }
250
- AUTOC_STATIC IntStrMapEntry IntStrMapSetListFind(IntStrMapSetList* self, IntStrMapEntry what) {
251
- IntStrMapSetListNode* node;
252
- assert(self);
253
- assert(IntStrMapSetListContains(self, what));
254
- node = self->head_node;
255
- while(node) {
256
- if(IntStrMapEntryEqual(node->element,what)) {
257
- IntStrMapEntry result;
258
- IntStrMapEntryCopy(result,node->element);
259
- return result;
260
- }
261
- node = node->next_node;
262
- }
263
- abort();
264
- }
265
- AUTOC_STATIC int IntStrMapSetListReplaceEx(IntStrMapSetList* self, IntStrMapEntry with, int count) {
266
- IntStrMapSetListNode* node;
267
- int replaced = 0;
268
- assert(self);
269
- if(count == 0) return 0;
270
- node = self->head_node;
271
- while(node) {
272
- if(IntStrMapEntryEqual(node->element,with)) {
273
- IntStrMapEntryDtor(node->element);
274
- IntStrMapEntryCopy(node->element,with);
275
- ++replaced;
276
- if(count > 0 && replaced >= count) break;
277
- }
278
- node = node->next_node;
279
- }
280
- return replaced;
281
- }
282
- AUTOC_STATIC int IntStrMapSetListRemoveEx(IntStrMapSetList* self, IntStrMapEntry what, int count) {
283
- IntStrMapSetListNode *node, *prev_node;
284
- int removed = 0;
285
- assert(self);
286
- if(count == 0) return 0;
287
- node = self->head_node;
288
- prev_node = NULL;
289
- while(node) {
290
- if(IntStrMapEntryEqual(node->element,what)) {
291
- IntStrMapSetListNode* this_node;
292
- if(prev_node) {
293
- this_node = prev_node->next_node = node->next_node;
294
- } else {
295
- this_node = self->head_node = node->next_node;
296
- }
297
- ++removed;
298
- --self->node_count;
299
- IntStrMapEntryDtor(node->element);
300
- free(node);
301
- node = this_node;
302
- if(count > 0 && removed >= count) break;
303
- } else {
304
- prev_node = node;
305
- node = node->next_node;
306
- }
307
- }
308
- return removed;
309
- }
310
- AUTOC_STATIC size_t IntStrMapSetListSize(IntStrMapSetList* self) {
311
- assert(self);
312
- return self->node_count;
313
- }
314
- AUTOC_STATIC void IntStrMapSetListItCtor(IntStrMapSetListIt* self, IntStrMapSetList* list) {
315
- assert(self);
316
- assert(list);
317
- self->start = 1;
318
- self->list = list;
319
- }
320
- AUTOC_STATIC int IntStrMapSetListItMove(IntStrMapSetListIt* self) {
321
- assert(self);
322
- if(self->start) {
323
- self->this_node = self->list->head_node;
324
- self->start = 0;
325
- } else {
326
- self->this_node = self->this_node ? self->this_node->next_node : NULL;
327
- }
328
- return self->this_node != NULL;
329
- }
330
- AUTOC_STATIC IntStrMapEntry IntStrMapSetListItGet(IntStrMapSetListIt* self) {
331
- IntStrMapEntry result;
332
- assert(self);
333
- assert(self->this_node);
334
- IntStrMapEntryCopy(result,self->this_node->element);
335
- return result;
336
- }
337
- AUTOC_STATIC IntStrMapEntry* IntStrMapSetListItGetRef(IntStrMapSetListIt* self) {
338
- assert(self);
339
- assert(self->this_node);
340
- return &self->this_node->element;
341
- }
342
-
343
- AUTOC_STATIC IntStrMapEntry* IntStrMapSetItGetRef(IntStrMapSetIt*);
344
- static void IntStrMapSetRehash(IntStrMapSet* self) {
345
- IntStrMapSetList* buckets;
346
- size_t index, bucket_count, size, fill;
347
- assert(self);
348
- assert(self->min_fill > 0);
349
- assert(self->max_fill > 0);
350
- assert(self->min_fill < self->max_fill);
351
- assert(self->min_bucket_count > 0);
352
- if(self->buckets) {
353
- if(self->min_size < self->size && self->size < self->max_size) return;
354
- fill = (size_t)((float)self->size/self->bucket_count*100);
355
- if(fill > self->max_fill) {
356
- bucket_count = (size_t)((float)self->bucket_count/100*self->capacity_multiplier);
357
- } else if(fill < self->min_fill && self->bucket_count > self->min_bucket_count) {
358
- bucket_count = (size_t)((float)self->bucket_count/self->capacity_multiplier*100);
359
- if(bucket_count < self->min_bucket_count) bucket_count = self->min_bucket_count;
360
- } else
361
- return;
362
- size = self->size;
363
- self->min_size = (size_t)((float)self->min_fill/100*size);
364
- self->max_size = (size_t)((float)self->max_fill/100*size);
365
- } else {
366
- bucket_count = self->min_bucket_count;
367
- size = 0;
368
- }
369
- buckets = (IntStrMapSetList*)malloc(bucket_count*sizeof(IntStrMapSetList));
370
- assert(buckets);
371
- for(index = 0; index < bucket_count; ++index) {
372
- IntStrMapSetListCtor(&buckets[index]);
373
- }
374
- if(self->buckets) {
375
- IntStrMapSetIt it;
376
- IntStrMapSetItCtor(&it, self);
377
- while(IntStrMapSetItMove(&it)) {
378
- IntStrMapSetList* bucket;
379
- IntStrMapEntry element = IntStrMapSetItGet(&it);
380
- bucket = &buckets[IntStrMapEntryIdentify(element) % bucket_count];
381
- IntStrMapSetListPush(bucket, element);
382
- IntStrMapEntryDtor(element);
383
- }
384
- IntStrMapSetDtor(self);
385
- }
386
- self->buckets = buckets;
387
- self->bucket_count = bucket_count;
388
- self->size = size;
389
- }
390
- static int IntStrMapSetContainsAllOf(IntStrMapSet* self, IntStrMapSet* other) {
391
- IntStrMapSetIt it;
392
- IntStrMapSetItCtor(&it, self);
393
- while(IntStrMapSetItMove(&it)) {
394
- int found = 0;
395
- if(IntStrMapSetContains(other, *IntStrMapSetItGetRef(&it))) found = 1;
396
- if(!found) return 0;
397
- }
398
- return 1;
399
- }
400
- AUTOC_STATIC void IntStrMapSetCtor(IntStrMapSet* self) {
401
- assert(self);
402
- self->min_bucket_count = 16;
403
- self->min_fill = 20;
404
- self->max_fill = 80;
405
- self->min_size = (size_t)((float)self->min_fill/100*self->min_bucket_count);
406
- self->max_size = (size_t)((float)self->max_fill/100*self->min_bucket_count);
407
- self->capacity_multiplier = 200;
408
- self->buckets = NULL;
409
- IntStrMapSetRehash(self);
410
- }
411
- AUTOC_STATIC void IntStrMapSetDtor(IntStrMapSet* self) {
412
- size_t i;
413
- assert(self);
414
- for(i = 0; i < self->bucket_count; ++i) {
415
- IntStrMapSetListDtor(&self->buckets[i]);
416
- }
417
- free(self->buckets);
418
- }
419
- AUTOC_STATIC void IntStrMapSetCopy(IntStrMapSet* dst,IntStrMapSet* src) {
420
- IntStrMapSetIt it;
421
- assert(src);
422
- assert(dst);
423
- IntStrMapSetCtor(dst);
424
- IntStrMapSetItCtor(&it, src);
425
- while(IntStrMapSetItMove(&it)) IntStrMapSetPut(dst, *IntStrMapSetItGetRef(&it));
426
- }
427
- AUTOC_STATIC int IntStrMapSetEqual(IntStrMapSet* lt,IntStrMapSet* rt) {
428
- assert(lt);
429
- assert(rt);
430
- return IntStrMapSetSize(lt) == IntStrMapSetSize(rt) && IntStrMapSetContainsAllOf(lt, rt) && IntStrMapSetContainsAllOf(rt, lt);
431
- }
432
- AUTOC_STATIC size_t IntStrMapSetIdentify(IntStrMapSet* self) {
433
- IntStrMapSetIt it;
434
- size_t result = 0;
435
- assert(self);
436
- IntStrMapSetItCtor(&it, self);
437
- while(IntStrMapSetItMove(&it)) {
438
- IntStrMapEntry* e = IntStrMapSetItGetRef(&it);
439
- result ^= IntStrMapEntryIdentify(*e);
440
- result = AUTOC_RCYCLE(result);
441
- }
442
- return result;
443
- }
444
- AUTOC_STATIC void IntStrMapSetPurge(IntStrMapSet* self) {
445
- assert(self);
446
- IntStrMapSetDtor(self);
447
- self->buckets = NULL;
448
- IntStrMapSetRehash(self);
449
- }
450
- AUTOC_STATIC int IntStrMapSetContains(IntStrMapSet* self, IntStrMapEntry element) {
451
- assert(self);
452
- return IntStrMapSetListContains(&self->buckets[IntStrMapEntryIdentify(element) % self->bucket_count], element);
453
- }
454
- AUTOC_STATIC IntStrMapEntry IntStrMapSetGet(IntStrMapSet* self, IntStrMapEntry element) {
455
- IntStrMapEntry result;
456
- assert(self);
457
- assert(IntStrMapSetContains(self, element));
458
- result = IntStrMapSetListFind(&self->buckets[IntStrMapEntryIdentify(element) % self->bucket_count], element);
459
- return result;
460
- }
461
- AUTOC_STATIC size_t IntStrMapSetSize(IntStrMapSet* self) {
462
- assert(self);
463
- return self->size;
464
- }
465
- AUTOC_STATIC int IntStrMapSetPut(IntStrMapSet* self, IntStrMapEntry element) {
466
- IntStrMapSetList* bucket;
467
- assert(self);
468
- bucket = &self->buckets[IntStrMapEntryIdentify(element) % self->bucket_count];
469
- if(!IntStrMapSetListContains(bucket, element)) {
470
- IntStrMapSetListPush(bucket, element);
471
- ++self->size;
472
- IntStrMapSetRehash(self);
473
- return 1;
474
- }
475
- return 0;
476
- }
477
- AUTOC_STATIC int IntStrMapSetReplace(IntStrMapSet* self, IntStrMapEntry element) {
478
- IntStrMapSetList* bucket;
479
- assert(self);
480
- bucket = &self->buckets[IntStrMapEntryIdentify(element) % self->bucket_count];
481
- return IntStrMapSetListReplace(bucket, element);
482
- }
483
- AUTOC_STATIC int IntStrMapSetRemove(IntStrMapSet* self, IntStrMapEntry element) {
484
- IntStrMapSetList* bucket;
485
- assert(self);
486
- bucket = &self->buckets[IntStrMapEntryIdentify(element) % self->bucket_count];
487
- if(IntStrMapSetListRemove(bucket, element)) {
488
- --self->size;
489
- IntStrMapSetRehash(self);
490
- return 1;
491
- }
492
- return 0;
493
- }
494
- AUTOC_STATIC void IntStrMapSetExclude(IntStrMapSet* self, IntStrMapSet* other) {
495
- IntStrMapSetIt it;
496
- assert(self);
497
- assert(other);
498
- IntStrMapSetItCtor(&it, other);
499
- while(IntStrMapSetItMove(&it)) IntStrMapSetRemove(self, *IntStrMapSetItGetRef(&it));
500
- }
501
- AUTOC_STATIC void IntStrMapSetInclude(IntStrMapSet* self, IntStrMapSet* other) {
502
- IntStrMapSetIt it;
503
- assert(self);
504
- assert(other);
505
- IntStrMapSetItCtor(&it, other);
506
- while(IntStrMapSetItMove(&it)) IntStrMapSetPut(self, *IntStrMapSetItGetRef(&it));
507
- }
508
- AUTOC_STATIC void IntStrMapSetRetain(IntStrMapSet* self, IntStrMapSet* other) {
509
- IntStrMapSetIt it;
510
- IntStrMapSet set;
511
- assert(self);
512
- assert(other);
513
- IntStrMapSetCtor(&set);
514
- IntStrMapSetItCtor(&it, self);
515
- while(IntStrMapSetItMove(&it)) {
516
- IntStrMapEntry* e = IntStrMapSetItGetRef(&it);
517
- if(IntStrMapSetContains(other, *e)) IntStrMapSetPut(&set, *e);
518
- }
519
- IntStrMapSetDtor(self);
520
- *self = set;
521
- }
522
- AUTOC_STATIC void IntStrMapSetInvert(IntStrMapSet* self, IntStrMapSet* other) {
523
- IntStrMapSetIt it;
524
- IntStrMapSet set;
525
- assert(self);
526
- assert(other);
527
- IntStrMapSetCtor(&set);
528
- IntStrMapSetItCtor(&it, self);
529
- while(IntStrMapSetItMove(&it)) {
530
- IntStrMapEntry* e = IntStrMapSetItGetRef(&it);
531
- if(!IntStrMapSetContains(other, *e)) IntStrMapSetPut(&set, *e);
532
- }
533
- IntStrMapSetItCtor(&it, other);
534
- while(IntStrMapSetItMove(&it)) {
535
- IntStrMapEntry* e = IntStrMapSetItGetRef(&it);
536
- if(!IntStrMapSetContains(self, *e)) IntStrMapSetPut(&set, *e);
537
- }
538
- IntStrMapSetDtor(self);
539
- *self = set;
540
- }
541
- AUTOC_STATIC void IntStrMapSetItCtor(IntStrMapSetIt* self, IntStrMapSet* set) {
542
- assert(self);
543
- self->set = set;
544
- self->bucket_index = -1;
545
- }
546
- AUTOC_STATIC int IntStrMapSetItMove(IntStrMapSetIt* self) {
547
- assert(self);
548
- if(self->bucket_index < 0) IntStrMapSetListItCtor(&self->it, &self->set->buckets[self->bucket_index = 0]);
549
- if(IntStrMapSetListItMove(&self->it)) return 1;
550
- while(++self->bucket_index < self->set->bucket_count) {
551
- IntStrMapSetListItCtor(&self->it, &self->set->buckets[self->bucket_index]);
552
- if(IntStrMapSetListItMove(&self->it)) return 1;
553
- }
554
- return 0;
555
- }
556
- AUTOC_STATIC IntStrMapEntry IntStrMapSetItGet(IntStrMapSetIt* self) {
557
- assert(self);
558
- return IntStrMapSetListItGet(&self->it);
559
- }
560
- AUTOC_STATIC IntStrMapEntry* IntStrMapSetItGetRef(IntStrMapSetIt* self) {
561
- assert(self);
562
- return IntStrMapSetListItGetRef(&self->it);
563
- }
564
-
565
- void IntStrMapCtor(IntStrMap* self) {
566
- assert(self);
567
- IntStrMapSetCtor(&self->entries);
568
- }
569
- void IntStrMapDtor(IntStrMap* self) {
570
- assert(self);
571
- IntStrMapSetDtor(&self->entries);
572
- }
573
- static int IntStrMapPutEntryRef(IntStrMap* self, IntStrMapEntry* entry) {
574
- int absent;
575
- assert(self);
576
- assert(entry);
577
- if((absent = !IntStrMapContainsKey(self, entry->key))) {
578
- IntStrMapSetPut(&self->entries, *entry);
579
- }
580
- return absent;
581
- }
582
- void IntStrMapCopy(IntStrMap* dst,IntStrMap* src) {
583
- IntStrMapIt it;
584
- assert(src);
585
- assert(dst);
586
- IntStrMapCtor(dst);
587
- IntStrMapItCtor(&it, src);
588
- while(IntStrMapItMove(&it)) {
589
- IntStrMapEntry* e = IntStrMapItGetEntryRef(&it);
590
- IntStrMapPutEntryRef(dst, e);
591
- }
592
- }
593
- int IntStrMapEqual(IntStrMap* lt,IntStrMap* rt) {
594
- assert(lt);
595
- assert(rt);
596
- return IntStrMapSize(lt) == IntStrMapSize(rt) && IntStrMapContainsAllOf(lt, rt) && IntStrMapContainsAllOf(rt, lt);
597
- }
598
- size_t IntStrMapIdentify(IntStrMap* self) {
599
- assert(self);
600
- return IntStrMapSetIdentify(&self->entries); /* TODO : make use of the values' hashes */
601
- }
602
- void IntStrMapPurge(IntStrMap* self) {
603
- assert(self);
604
- IntStrMapSetPurge(&self->entries);
605
- }
606
- size_t IntStrMapSize(IntStrMap* self) {
607
- assert(self);
608
- return IntStrMapSetSize(&self->entries);
609
- }
610
- int IntStrMapContainsKey(IntStrMap* self, int key) {
611
- int result;
612
- IntStrMapEntry entry;
613
- assert(self);
614
- result = IntStrMapSetContains(&self->entries, entry = IntStrMapEntryKeyOnlyRef(&key));
615
- IntStrMapEntryDtor(entry);
616
- return result;
617
- }
618
- const char * IntStrMapGet(IntStrMap* self, int key) {
619
- const char * result;
620
- IntStrMapEntry entry, existing_entry;
621
- assert(self);
622
- assert(IntStrMapContainsKey(self, key));
623
- existing_entry = IntStrMapSetGet(&self->entries, entry = IntStrMapEntryKeyOnlyRef(&key));
624
- ((result) = (existing_entry.value));
625
- IntStrMapEntryDtor(existing_entry);
626
- IntStrMapEntryDtor(entry);
627
- return result;
628
- }
629
- int IntStrMapPut(IntStrMap* self, int key, const char * value) {
630
- int result;
631
- IntStrMapEntry entry;
632
- assert(self);
633
- entry = IntStrMapEntryKeyValueRef(&key, &value);
634
- result = IntStrMapPutEntryRef(self, &entry);
635
- IntStrMapEntryDtor(entry);
636
- return result;
637
- }
638
- int IntStrMapReplace(IntStrMap* self, int key, const char * value) {
639
- int result;
640
- IntStrMapEntry entry;
641
- assert(self);
642
- entry = IntStrMapEntryKeyValueRef(&key, &value);
643
- result = IntStrMapSetReplace(&self->entries, entry);
644
- IntStrMapEntryDtor(entry);
645
- return result;
646
- }
647
- int IntStrMapRemove(IntStrMap* self, int key) {
648
- int result;
649
- IntStrMapEntry entry;
650
- assert(self);
651
- result = IntStrMapSetRemove(&self->entries, entry = IntStrMapEntryKeyOnlyRef(&key));
652
- IntStrMapEntryDtor(entry);
653
- return result;
654
- }
655
- void IntStrMapItCtor(IntStrMapIt* self, IntStrMap* map) {
656
- assert(self);
657
- assert(map);
658
- IntStrMapSetItCtor(&self->it, &map->entries);
659
- }
660
- int IntStrMapItMove(IntStrMapIt* self) {
661
- assert(self);
662
- return IntStrMapSetItMove(&self->it);
663
- }
664
- int IntStrMapItGetKey(IntStrMapIt* self) {
665
- IntStrMapEntry* e;
666
- int key;
667
- assert(self);
668
- e = IntStrMapItGetEntryRef(self);
669
- ((key) = (e->key));
670
- return key;
671
- }
672
- const char * IntStrMapItGetElement(IntStrMapIt* self) {
673
- IntStrMapEntry* e;
674
- const char * value;
675
- assert(self);
676
- e = IntStrMapItGetEntryRef(self);
677
- assert(e->flags & AUTOC_VALID_VALUE);
678
- ((value) = (e->value));
679
- return value;
680
- }
681
- static IntStrMapEntry* IntStrMapItGetEntryRef(IntStrMapIt* self) {
682
- assert(self);
683
- return IntStrMapSetItGetRef(&self->it);
684
- }
685
- #undef AUTOC_VALID_VALUE
686
- #undef AUTOC_VALID_KEY
687
- #undef AUTOC_OWNED_VALUE
688
- #undef AUTOC_OWNED_KEY
689
-
690
- #define _IntSetListCtor(self) IntSetListCtor(&self)
691
- #define _IntSetListDtor(self) IntSetListDtor(&self)
692
- #define _IntSetListIdentify(self) IntSetListIdentify(&self)
693
- #define _IntSetListCopy(dst,src) IntSetListCopy(&dst,&src)
694
- #define _IntSetListEqual(lt,rt) IntSetListEqual(&lt,&rt)
695
- #define _IntSetListLess(lt,rt) IntSetListLess(&lt,&rt)
696
-
697
- AUTOC_STATIC void IntSetListCtor(IntSetList*);
698
- AUTOC_STATIC void IntSetListDtor(IntSetList*);
699
- AUTOC_STATIC void IntSetListCopy(IntSetList*,IntSetList*);
700
- AUTOC_STATIC int IntSetListEqual(IntSetList*,IntSetList*);
701
- AUTOC_STATIC size_t IntSetListIdentify(IntSetList*);
702
- AUTOC_STATIC void IntSetListPurge(IntSetList*);
703
- AUTOC_STATIC int IntSetListPeek(IntSetList*);
704
- AUTOC_STATIC int IntSetListPop(IntSetList*);
705
- AUTOC_STATIC void IntSetListPush(IntSetList*, int);
706
- AUTOC_STATIC int IntSetListContains(IntSetList*, int);
707
- AUTOC_STATIC int IntSetListFind(IntSetList*, int);
708
- #define IntSetListReplace(self, with) IntSetListReplaceEx(self, with, 1)
709
- #define IntSetListReplaceAll(self, with) IntSetListReplaceEx(self, with, -1)
710
- AUTOC_STATIC int IntSetListReplaceEx(IntSetList*, int, int);
711
- #define IntSetListRemove(self, what) IntSetListRemoveEx(self, what, 1)
712
- #define IntSetListRemoveAll(self, what) IntSetListRemoveEx(self, what, -1)
713
- AUTOC_STATIC int IntSetListRemoveEx(IntSetList*, int, int);
714
- AUTOC_STATIC size_t IntSetListSize(IntSetList*);
715
- #define IntSetListEmpty(self) (IntSetListSize(self) == 0)
716
- AUTOC_STATIC void IntSetListItCtor(IntSetListIt*, IntSetList*);
717
- AUTOC_STATIC int IntSetListItMove(IntSetListIt*);
718
- AUTOC_STATIC int IntSetListItGet(IntSetListIt*);
719
-
720
- AUTOC_STATIC int* IntSetListItGetRef(IntSetListIt*);
721
- AUTOC_STATIC void IntSetListCtor(IntSetList* self) {
722
- assert(self);
723
- self->head_node = NULL;
724
- self->node_count = 0;
725
- }
726
- AUTOC_STATIC void IntSetListDtor(IntSetList* self) {
727
- IntSetListNode* node;
728
- assert(self);
729
- node = self->head_node;
730
- while(node) {
731
- IntSetListNode* this_node = node;
732
- node = node->next_node;
733
- ;
734
- free(this_node);
735
- }
736
- }
737
- AUTOC_STATIC void IntSetListCopy(IntSetList* dst,IntSetList* src) {
738
- IntSetListIt it;
739
- assert(src);
740
- assert(dst);
741
- IntSetListCtor(dst);
742
- IntSetListItCtor(&it, src);
743
- while(IntSetListItMove(&it)) {
744
- IntSetListPush(dst, *IntSetListItGetRef(&it));
745
- }
746
- }
747
- AUTOC_STATIC int IntSetListEqual(IntSetList* lt,IntSetList* rt) {
748
- if(IntSetListSize(lt) == IntSetListSize(rt)) {
749
- IntSetListIt lit, rit;
750
- IntSetListItCtor(&lit, lt);
751
- IntSetListItCtor(&rit, rt);
752
- while(IntSetListItMove(&lit) && IntSetListItMove(&rit)) {
753
- int equal;
754
- int* le;
755
- int* re;
756
- le = IntSetListItGetRef(&lit);
757
- re = IntSetListItGetRef(&rit);
758
- equal = ((*le) == (*re));
759
- if(!equal) return 0;
760
- }
761
- return 1;
762
- } else
763
- return 0;
764
- }
765
- AUTOC_STATIC size_t IntSetListIdentify(IntSetList* self) {
766
- IntSetListNode* node;
767
- size_t result = 0;
768
- assert(self);
769
- for(node = self->head_node; node != NULL; node = node->next_node) {
770
- result ^= ((size_t)(node->element));
771
- result = AUTOC_RCYCLE(result);
772
- }
773
- return result;
774
- }
775
- AUTOC_STATIC void IntSetListPurge(IntSetList* self) {
776
- IntSetListDtor(self);
777
- IntSetListCtor(self);
778
- }
779
- AUTOC_STATIC int IntSetListPeek(IntSetList* self) {
780
- int result;
781
- assert(self);
782
- assert(!IntSetListEmpty(self));
783
- ((result) = (self->head_node->element));
784
- return result;
785
- }
786
- AUTOC_STATIC int IntSetListPop(IntSetList* self) {
787
- IntSetListNode* node;
788
- int result;
789
- assert(self);
790
- assert(!IntSetListEmpty(self));
791
- node = self->head_node;
792
- result = node->element;
793
- self->head_node = self->head_node->next_node;
794
- --self->node_count;
795
- free(node);
796
- return result;
797
- }
798
- AUTOC_STATIC void IntSetListPush(IntSetList* self, int element) {
799
- IntSetListNode* node;
800
- assert(self);
801
- node = (IntSetListNode*)malloc(sizeof(IntSetListNode));
802
- assert(node);
803
- ((node->element) = (element));
804
- node->next_node = self->head_node;
805
- self->head_node = node;
806
- ++self->node_count;
807
- }
808
- AUTOC_STATIC int IntSetListContains(IntSetList* self, int what) {
809
- IntSetListNode* node;
810
- int found = 0;
811
- assert(self);
812
- node = self->head_node;
813
- while(node) {
814
- if(((node->element) == (what))) {
815
- found = 1;
816
- break;
817
- }
818
- node = node->next_node;
819
- }
820
- return found;
821
- }
822
- AUTOC_STATIC int IntSetListFind(IntSetList* self, int what) {
823
- IntSetListNode* node;
824
- assert(self);
825
- assert(IntSetListContains(self, what));
826
- node = self->head_node;
827
- while(node) {
828
- if(((node->element) == (what))) {
829
- int result;
830
- ((result) = (node->element));
831
- return result;
832
- }
833
- node = node->next_node;
834
- }
835
- abort();
836
- }
837
- AUTOC_STATIC int IntSetListReplaceEx(IntSetList* self, int with, int count) {
838
- IntSetListNode* node;
839
- int replaced = 0;
840
- assert(self);
841
- if(count == 0) return 0;
842
- node = self->head_node;
843
- while(node) {
844
- if(((node->element) == (with))) {
845
- ;
846
- ((node->element) = (with));
847
- ++replaced;
848
- if(count > 0 && replaced >= count) break;
849
- }
850
- node = node->next_node;
851
- }
852
- return replaced;
853
- }
854
- AUTOC_STATIC int IntSetListRemoveEx(IntSetList* self, int what, int count) {
855
- IntSetListNode *node, *prev_node;
856
- int removed = 0;
857
- assert(self);
858
- if(count == 0) return 0;
859
- node = self->head_node;
860
- prev_node = NULL;
861
- while(node) {
862
- if(((node->element) == (what))) {
863
- IntSetListNode* this_node;
864
- if(prev_node) {
865
- this_node = prev_node->next_node = node->next_node;
866
- } else {
867
- this_node = self->head_node = node->next_node;
868
- }
869
- ++removed;
870
- --self->node_count;
871
- ;
872
- free(node);
873
- node = this_node;
874
- if(count > 0 && removed >= count) break;
875
- } else {
876
- prev_node = node;
877
- node = node->next_node;
878
- }
879
- }
880
- return removed;
881
- }
882
- AUTOC_STATIC size_t IntSetListSize(IntSetList* self) {
883
- assert(self);
884
- return self->node_count;
885
- }
886
- AUTOC_STATIC void IntSetListItCtor(IntSetListIt* self, IntSetList* list) {
887
- assert(self);
888
- assert(list);
889
- self->start = 1;
890
- self->list = list;
891
- }
892
- AUTOC_STATIC int IntSetListItMove(IntSetListIt* self) {
893
- assert(self);
894
- if(self->start) {
895
- self->this_node = self->list->head_node;
896
- self->start = 0;
897
- } else {
898
- self->this_node = self->this_node ? self->this_node->next_node : NULL;
899
- }
900
- return self->this_node != NULL;
901
- }
902
- AUTOC_STATIC int IntSetListItGet(IntSetListIt* self) {
903
- int result;
904
- assert(self);
905
- assert(self->this_node);
906
- ((result) = (self->this_node->element));
907
- return result;
908
- }
909
- AUTOC_STATIC int* IntSetListItGetRef(IntSetListIt* self) {
910
- assert(self);
911
- assert(self->this_node);
912
- return &self->this_node->element;
913
- }
914
-
915
- int* IntSetItGetRef(IntSetIt*);
916
- static void IntSetRehash(IntSet* self) {
917
- IntSetList* buckets;
918
- size_t index, bucket_count, size, fill;
919
- assert(self);
920
- assert(self->min_fill > 0);
921
- assert(self->max_fill > 0);
922
- assert(self->min_fill < self->max_fill);
923
- assert(self->min_bucket_count > 0);
924
- if(self->buckets) {
925
- if(self->min_size < self->size && self->size < self->max_size) return;
926
- fill = (size_t)((float)self->size/self->bucket_count*100);
927
- if(fill > self->max_fill) {
928
- bucket_count = (size_t)((float)self->bucket_count/100*self->capacity_multiplier);
929
- } else if(fill < self->min_fill && self->bucket_count > self->min_bucket_count) {
930
- bucket_count = (size_t)((float)self->bucket_count/self->capacity_multiplier*100);
931
- if(bucket_count < self->min_bucket_count) bucket_count = self->min_bucket_count;
932
- } else
933
- return;
934
- size = self->size;
935
- self->min_size = (size_t)((float)self->min_fill/100*size);
936
- self->max_size = (size_t)((float)self->max_fill/100*size);
937
- } else {
938
- bucket_count = self->min_bucket_count;
939
- size = 0;
940
- }
941
- buckets = (IntSetList*)malloc(bucket_count*sizeof(IntSetList));
942
- assert(buckets);
943
- for(index = 0; index < bucket_count; ++index) {
944
- IntSetListCtor(&buckets[index]);
945
- }
946
- if(self->buckets) {
947
- IntSetIt it;
948
- IntSetItCtor(&it, self);
949
- while(IntSetItMove(&it)) {
950
- IntSetList* bucket;
951
- int element = IntSetItGet(&it);
952
- bucket = &buckets[((size_t)(element)) % bucket_count];
953
- IntSetListPush(bucket, element);
954
- ;
955
- }
956
- IntSetDtor(self);
957
- }
958
- self->buckets = buckets;
959
- self->bucket_count = bucket_count;
960
- self->size = size;
961
- }
962
- static int IntSetContainsAllOf(IntSet* self, IntSet* other) {
963
- IntSetIt it;
964
- IntSetItCtor(&it, self);
965
- while(IntSetItMove(&it)) {
966
- int found = 0;
967
- if(IntSetContains(other, *IntSetItGetRef(&it))) found = 1;
968
- if(!found) return 0;
969
- }
970
- return 1;
971
- }
972
- void IntSetCtor(IntSet* self) {
973
- assert(self);
974
- self->min_bucket_count = 16;
975
- self->min_fill = 20;
976
- self->max_fill = 80;
977
- self->min_size = (size_t)((float)self->min_fill/100*self->min_bucket_count);
978
- self->max_size = (size_t)((float)self->max_fill/100*self->min_bucket_count);
979
- self->capacity_multiplier = 200;
980
- self->buckets = NULL;
981
- IntSetRehash(self);
982
- }
983
- void IntSetDtor(IntSet* self) {
984
- size_t i;
985
- assert(self);
986
- for(i = 0; i < self->bucket_count; ++i) {
987
- IntSetListDtor(&self->buckets[i]);
988
- }
989
- free(self->buckets);
990
- }
991
- void IntSetCopy(IntSet* dst,IntSet* src) {
992
- IntSetIt it;
993
- assert(src);
994
- assert(dst);
995
- IntSetCtor(dst);
996
- IntSetItCtor(&it, src);
997
- while(IntSetItMove(&it)) IntSetPut(dst, *IntSetItGetRef(&it));
998
- }
999
- int IntSetEqual(IntSet* lt,IntSet* rt) {
1000
- assert(lt);
1001
- assert(rt);
1002
- return IntSetSize(lt) == IntSetSize(rt) && IntSetContainsAllOf(lt, rt) && IntSetContainsAllOf(rt, lt);
1003
- }
1004
- size_t IntSetIdentify(IntSet* self) {
1005
- IntSetIt it;
1006
- size_t result = 0;
1007
- assert(self);
1008
- IntSetItCtor(&it, self);
1009
- while(IntSetItMove(&it)) {
1010
- int* e = IntSetItGetRef(&it);
1011
- result ^= ((size_t)(*e));
1012
- result = AUTOC_RCYCLE(result);
1013
- }
1014
- return result;
1015
- }
1016
- void IntSetPurge(IntSet* self) {
1017
- assert(self);
1018
- IntSetDtor(self);
1019
- self->buckets = NULL;
1020
- IntSetRehash(self);
1021
- }
1022
- int IntSetContains(IntSet* self, int element) {
1023
- assert(self);
1024
- return IntSetListContains(&self->buckets[((size_t)(element)) % self->bucket_count], element);
1025
- }
1026
- int IntSetGet(IntSet* self, int element) {
1027
- int result;
1028
- assert(self);
1029
- assert(IntSetContains(self, element));
1030
- result = IntSetListFind(&self->buckets[((size_t)(element)) % self->bucket_count], element);
1031
- return result;
1032
- }
1033
- size_t IntSetSize(IntSet* self) {
1034
- assert(self);
1035
- return self->size;
1036
- }
1037
- int IntSetPut(IntSet* self, int element) {
1038
- IntSetList* bucket;
1039
- assert(self);
1040
- bucket = &self->buckets[((size_t)(element)) % self->bucket_count];
1041
- if(!IntSetListContains(bucket, element)) {
1042
- IntSetListPush(bucket, element);
1043
- ++self->size;
1044
- IntSetRehash(self);
1045
- return 1;
1046
- }
1047
- return 0;
1048
- }
1049
- int IntSetReplace(IntSet* self, int element) {
1050
- IntSetList* bucket;
1051
- assert(self);
1052
- bucket = &self->buckets[((size_t)(element)) % self->bucket_count];
1053
- return IntSetListReplace(bucket, element);
1054
- }
1055
- int IntSetRemove(IntSet* self, int element) {
1056
- IntSetList* bucket;
1057
- assert(self);
1058
- bucket = &self->buckets[((size_t)(element)) % self->bucket_count];
1059
- if(IntSetListRemove(bucket, element)) {
1060
- --self->size;
1061
- IntSetRehash(self);
1062
- return 1;
1063
- }
1064
- return 0;
1065
- }
1066
- void IntSetExclude(IntSet* self, IntSet* other) {
1067
- IntSetIt it;
1068
- assert(self);
1069
- assert(other);
1070
- IntSetItCtor(&it, other);
1071
- while(IntSetItMove(&it)) IntSetRemove(self, *IntSetItGetRef(&it));
1072
- }
1073
- void IntSetInclude(IntSet* self, IntSet* other) {
1074
- IntSetIt it;
1075
- assert(self);
1076
- assert(other);
1077
- IntSetItCtor(&it, other);
1078
- while(IntSetItMove(&it)) IntSetPut(self, *IntSetItGetRef(&it));
1079
- }
1080
- void IntSetRetain(IntSet* self, IntSet* other) {
1081
- IntSetIt it;
1082
- IntSet set;
1083
- assert(self);
1084
- assert(other);
1085
- IntSetCtor(&set);
1086
- IntSetItCtor(&it, self);
1087
- while(IntSetItMove(&it)) {
1088
- int* e = IntSetItGetRef(&it);
1089
- if(IntSetContains(other, *e)) IntSetPut(&set, *e);
1090
- }
1091
- IntSetDtor(self);
1092
- *self = set;
1093
- }
1094
- void IntSetInvert(IntSet* self, IntSet* other) {
1095
- IntSetIt it;
1096
- IntSet set;
1097
- assert(self);
1098
- assert(other);
1099
- IntSetCtor(&set);
1100
- IntSetItCtor(&it, self);
1101
- while(IntSetItMove(&it)) {
1102
- int* e = IntSetItGetRef(&it);
1103
- if(!IntSetContains(other, *e)) IntSetPut(&set, *e);
1104
- }
1105
- IntSetItCtor(&it, other);
1106
- while(IntSetItMove(&it)) {
1107
- int* e = IntSetItGetRef(&it);
1108
- if(!IntSetContains(self, *e)) IntSetPut(&set, *e);
1109
- }
1110
- IntSetDtor(self);
1111
- *self = set;
1112
- }
1113
- void IntSetItCtor(IntSetIt* self, IntSet* set) {
1114
- assert(self);
1115
- self->set = set;
1116
- self->bucket_index = -1;
1117
- }
1118
- int IntSetItMove(IntSetIt* self) {
1119
- assert(self);
1120
- if(self->bucket_index < 0) IntSetListItCtor(&self->it, &self->set->buckets[self->bucket_index = 0]);
1121
- if(IntSetListItMove(&self->it)) return 1;
1122
- while(++self->bucket_index < self->set->bucket_count) {
1123
- IntSetListItCtor(&self->it, &self->set->buckets[self->bucket_index]);
1124
- if(IntSetListItMove(&self->it)) return 1;
1125
- }
1126
- return 0;
1127
- }
1128
- int IntSetItGet(IntSetIt* self) {
1129
- assert(self);
1130
- return IntSetListItGet(&self->it);
1131
- }
1132
- int* IntSetItGetRef(IntSetIt* self) {
1133
- assert(self);
1134
- return IntSetListItGetRef(&self->it);
1135
- }
1136
-
1137
- static void ValueTypeVectorAllocate(ValueTypeVector* self, size_t element_count) {
1138
- assert(self);
1139
- assert(element_count > 0);
1140
- self->element_count = element_count;
1141
- self->values = (ValueType*)malloc(element_count*sizeof(ValueType));
1142
- assert(self->values);
1143
- }
1144
- void ValueTypeVectorCtor(ValueTypeVector* self,size_t element_count) {
1145
- size_t index;
1146
- assert(self);
1147
- ValueTypeVectorAllocate(self, element_count);
1148
- for(index = 0; index < ValueTypeVectorSize(self); ++index) {
1149
- ValueTypeCtor(self->values[index]);
1150
- }
1151
- }
1152
- void ValueTypeVectorDtor(ValueTypeVector* self) {
1153
- size_t index;
1154
- assert(self);
1155
- for(index = 0; index < ValueTypeVectorSize(self); ++index) {
1156
- ValueTypeDtor(self->values[index]);
1157
- }
1158
- free(self->values);
1159
- }
1160
- void ValueTypeVectorCopy(ValueTypeVector* dst,ValueTypeVector* src) {
1161
- size_t index, size;
1162
- assert(src);
1163
- assert(dst);
1164
- ValueTypeVectorAllocate(dst, size = ValueTypeVectorSize(src));
1165
- for(index = 0; index < size; ++index) {
1166
- ValueTypeCopy(dst->values[index],src->values[index]);
1167
- }
1168
- }
1169
- int ValueTypeVectorEqual(ValueTypeVector* lt,ValueTypeVector* rt) {
1170
- size_t index, size;
1171
- assert(lt);
1172
- assert(rt);
1173
- if(ValueTypeVectorSize(lt) == (size = ValueTypeVectorSize(rt))) {
1174
- for(index = 0; index < size; ++index) {
1175
- if(!ValueTypeEqual(lt->values[index],rt->values[index])) return 0;
1176
- }
1177
- return 1;
1178
- } else
1179
- return 0;
1180
- }
1181
- size_t ValueTypeVectorIdentify(ValueTypeVector* self) {
1182
- size_t index, result = 0;
1183
- assert(self);
1184
- for(index = 0; index < self->element_count; ++index) {
1185
- result ^= ValueTypeIdentify(self->values[index]);
1186
- result = AUTOC_RCYCLE(result);
1187
- }
1188
- return result;
1189
- }
1190
- void ValueTypeVectorResize(ValueTypeVector* self, size_t new_element_count) {
1191
- size_t index, element_count, from, to;
1192
- assert(self);
1193
- if((element_count = ValueTypeVectorSize(self)) != new_element_count) {
1194
- ValueType* values = (ValueType*)malloc(new_element_count*sizeof(ValueType));
1195
- assert(values);
1196
- from = AUTOC_MIN(element_count, new_element_count);
1197
- to = AUTOC_MAX(element_count, new_element_count);
1198
- for(index = 0; index < from; ++index) {
1199
- values[index] = self->values[index];
1200
- }
1201
- if(element_count > new_element_count) {
1202
- for(index = from; index < to; ++index) {
1203
- ValueTypeDtor(self->values[index]);
1204
- }
1205
- } else {
1206
- for(index = from; index < to; ++index) {
1207
- ValueTypeCtor(values[index]);
1208
- }
1209
- }
1210
- free(self->values);
1211
- self->values = values;
1212
- self->element_count = new_element_count;
1213
- }
1214
- }
1215
-
1216
- static int ValueTypeVectorComparator(void* lp_, void* rp_) {
1217
- ValueType* lp = (ValueType*)lp_;
1218
- ValueType* rp = (ValueType*)rp_;
1219
- if(ValueTypeEqual(*lp,*rp)) {
1220
- return 0;
1221
- } else if(ValueTypeLess(*lp,*rp)) {
1222
- return -1;
1223
- } else {
1224
- return +1;
1225
- }
1226
- }
1227
- void ValueTypeVectorSort(ValueTypeVector* self) {
1228
- typedef int (*F)(const void*, const void*);
1229
- assert(self);
1230
- qsort(self->values, ValueTypeVectorSize(self), sizeof(ValueType), (F)ValueTypeVectorComparator);
1231
- }
1232
-
1233
- IntSet* ListIntSetItGetRef(ListIntSetIt*);
1234
- void ListIntSetCtor(ListIntSet* self) {
1235
- assert(self);
1236
- self->head_node = NULL;
1237
- self->node_count = 0;
1238
- }
1239
- void ListIntSetDtor(ListIntSet* self) {
1240
- ListIntSetNode* node;
1241
- assert(self);
1242
- node = self->head_node;
1243
- while(node) {
1244
- ListIntSetNode* this_node = node;
1245
- node = node->next_node;
1246
- _IntSetDtor(this_node->element);
1247
- free(this_node);
1248
- }
1249
- }
1250
- void ListIntSetCopy(ListIntSet* dst,ListIntSet* src) {
1251
- ListIntSetIt it;
1252
- assert(src);
1253
- assert(dst);
1254
- ListIntSetCtor(dst);
1255
- ListIntSetItCtor(&it, src);
1256
- while(ListIntSetItMove(&it)) {
1257
- ListIntSetPush(dst, *ListIntSetItGetRef(&it));
1258
- }
1259
- }
1260
- int ListIntSetEqual(ListIntSet* lt,ListIntSet* rt) {
1261
- if(ListIntSetSize(lt) == ListIntSetSize(rt)) {
1262
- ListIntSetIt lit, rit;
1263
- ListIntSetItCtor(&lit, lt);
1264
- ListIntSetItCtor(&rit, rt);
1265
- while(ListIntSetItMove(&lit) && ListIntSetItMove(&rit)) {
1266
- int equal;
1267
- IntSet* le;
1268
- IntSet* re;
1269
- le = ListIntSetItGetRef(&lit);
1270
- re = ListIntSetItGetRef(&rit);
1271
- equal = _IntSetEqual(*le,*re);
1272
- if(!equal) return 0;
1273
- }
1274
- return 1;
1275
- } else
1276
- return 0;
1277
- }
1278
- size_t ListIntSetIdentify(ListIntSet* self) {
1279
- ListIntSetNode* node;
1280
- size_t result = 0;
1281
- assert(self);
1282
- for(node = self->head_node; node != NULL; node = node->next_node) {
1283
- result ^= _IntSetIdentify(node->element);
1284
- result = AUTOC_RCYCLE(result);
1285
- }
1286
- return result;
1287
- }
1288
- void ListIntSetPurge(ListIntSet* self) {
1289
- ListIntSetDtor(self);
1290
- ListIntSetCtor(self);
1291
- }
1292
- IntSet ListIntSetPeek(ListIntSet* self) {
1293
- IntSet result;
1294
- assert(self);
1295
- assert(!ListIntSetEmpty(self));
1296
- _IntSetCopy(result,self->head_node->element);
1297
- return result;
1298
- }
1299
- IntSet ListIntSetPop(ListIntSet* self) {
1300
- ListIntSetNode* node;
1301
- IntSet result;
1302
- assert(self);
1303
- assert(!ListIntSetEmpty(self));
1304
- node = self->head_node;
1305
- result = node->element;
1306
- self->head_node = self->head_node->next_node;
1307
- --self->node_count;
1308
- free(node);
1309
- return result;
1310
- }
1311
- void ListIntSetPush(ListIntSet* self, IntSet element) {
1312
- ListIntSetNode* node;
1313
- assert(self);
1314
- node = (ListIntSetNode*)malloc(sizeof(ListIntSetNode));
1315
- assert(node);
1316
- _IntSetCopy(node->element,element);
1317
- node->next_node = self->head_node;
1318
- self->head_node = node;
1319
- ++self->node_count;
1320
- }
1321
- int ListIntSetContains(ListIntSet* self, IntSet what) {
1322
- ListIntSetNode* node;
1323
- int found = 0;
1324
- assert(self);
1325
- node = self->head_node;
1326
- while(node) {
1327
- if(_IntSetEqual(node->element,what)) {
1328
- found = 1;
1329
- break;
1330
- }
1331
- node = node->next_node;
1332
- }
1333
- return found;
1334
- }
1335
- IntSet ListIntSetFind(ListIntSet* self, IntSet what) {
1336
- ListIntSetNode* node;
1337
- assert(self);
1338
- assert(ListIntSetContains(self, what));
1339
- node = self->head_node;
1340
- while(node) {
1341
- if(_IntSetEqual(node->element,what)) {
1342
- IntSet result;
1343
- _IntSetCopy(result,node->element);
1344
- return result;
1345
- }
1346
- node = node->next_node;
1347
- }
1348
- abort();
1349
- }
1350
- int ListIntSetReplaceEx(ListIntSet* self, IntSet with, int count) {
1351
- ListIntSetNode* node;
1352
- int replaced = 0;
1353
- assert(self);
1354
- if(count == 0) return 0;
1355
- node = self->head_node;
1356
- while(node) {
1357
- if(_IntSetEqual(node->element,with)) {
1358
- _IntSetDtor(node->element);
1359
- _IntSetCopy(node->element,with);
1360
- ++replaced;
1361
- if(count > 0 && replaced >= count) break;
1362
- }
1363
- node = node->next_node;
1364
- }
1365
- return replaced;
1366
- }
1367
- int ListIntSetRemoveEx(ListIntSet* self, IntSet what, int count) {
1368
- ListIntSetNode *node, *prev_node;
1369
- int removed = 0;
1370
- assert(self);
1371
- if(count == 0) return 0;
1372
- node = self->head_node;
1373
- prev_node = NULL;
1374
- while(node) {
1375
- if(_IntSetEqual(node->element,what)) {
1376
- ListIntSetNode* this_node;
1377
- if(prev_node) {
1378
- this_node = prev_node->next_node = node->next_node;
1379
- } else {
1380
- this_node = self->head_node = node->next_node;
1381
- }
1382
- ++removed;
1383
- --self->node_count;
1384
- _IntSetDtor(node->element);
1385
- free(node);
1386
- node = this_node;
1387
- if(count > 0 && removed >= count) break;
1388
- } else {
1389
- prev_node = node;
1390
- node = node->next_node;
1391
- }
1392
- }
1393
- return removed;
1394
- }
1395
- size_t ListIntSetSize(ListIntSet* self) {
1396
- assert(self);
1397
- return self->node_count;
1398
- }
1399
- void ListIntSetItCtor(ListIntSetIt* self, ListIntSet* list) {
1400
- assert(self);
1401
- assert(list);
1402
- self->start = 1;
1403
- self->list = list;
1404
- }
1405
- int ListIntSetItMove(ListIntSetIt* self) {
1406
- assert(self);
1407
- if(self->start) {
1408
- self->this_node = self->list->head_node;
1409
- self->start = 0;
1410
- } else {
1411
- self->this_node = self->this_node ? self->this_node->next_node : NULL;
1412
- }
1413
- return self->this_node != NULL;
1414
- }
1415
- IntSet ListIntSetItGet(ListIntSetIt* self) {
1416
- IntSet result;
1417
- assert(self);
1418
- assert(self->this_node);
1419
- _IntSetCopy(result,self->this_node->element);
1420
- return result;
1421
- }
1422
- IntSet* ListIntSetItGetRef(ListIntSetIt* self) {
1423
- assert(self);
1424
- assert(self->this_node);
1425
- return &self->this_node->element;
1426
- }
1427
-
1428
- static void PIntVectorAllocate(PIntVector* self, size_t element_count) {
1429
- assert(self);
1430
- assert(element_count > 0);
1431
- self->element_count = element_count;
1432
- self->values = (int**)malloc(element_count*sizeof(int*));
1433
- assert(self->values);
1434
- }
1435
- void PIntVectorCtor(PIntVector* self,size_t element_count) {
1436
- size_t index;
1437
- assert(self);
1438
- PIntVectorAllocate(self, element_count);
1439
- for(index = 0; index < PIntVectorSize(self); ++index) {
1440
- ((self->values[index]) = IntNew());
1441
- }
1442
- }
1443
- void PIntVectorDtor(PIntVector* self) {
1444
- size_t index;
1445
- assert(self);
1446
- for(index = 0; index < PIntVectorSize(self); ++index) {
1447
- IntFree(self->values[index]);
1448
- }
1449
- free(self->values);
1450
- }
1451
- void PIntVectorCopy(PIntVector* dst,PIntVector* src) {
1452
- size_t index, size;
1453
- assert(src);
1454
- assert(dst);
1455
- PIntVectorAllocate(dst, size = PIntVectorSize(src));
1456
- for(index = 0; index < size; ++index) {
1457
- ((dst->values[index]) = IntRef(src->values[index]));
1458
- }
1459
- }
1460
- int PIntVectorEqual(PIntVector* lt,PIntVector* rt) {
1461
- size_t index, size;
1462
- assert(lt);
1463
- assert(rt);
1464
- if(PIntVectorSize(lt) == (size = PIntVectorSize(rt))) {
1465
- for(index = 0; index < size; ++index) {
1466
- if(!((*lt->values[index]) == (*rt->values[index]))) return 0;
1467
- }
1468
- return 1;
1469
- } else
1470
- return 0;
1471
- }
1472
- size_t PIntVectorIdentify(PIntVector* self) {
1473
- size_t index, result = 0;
1474
- assert(self);
1475
- for(index = 0; index < self->element_count; ++index) {
1476
- result ^= ((size_t)(*self->values[index]));
1477
- result = AUTOC_RCYCLE(result);
1478
- }
1479
- return result;
1480
- }
1481
- void PIntVectorResize(PIntVector* self, size_t new_element_count) {
1482
- size_t index, element_count, from, to;
1483
- assert(self);
1484
- if((element_count = PIntVectorSize(self)) != new_element_count) {
1485
- int** values = (int**)malloc(new_element_count*sizeof(int*));
1486
- assert(values);
1487
- from = AUTOC_MIN(element_count, new_element_count);
1488
- to = AUTOC_MAX(element_count, new_element_count);
1489
- for(index = 0; index < from; ++index) {
1490
- values[index] = self->values[index];
1491
- }
1492
- if(element_count > new_element_count) {
1493
- for(index = from; index < to; ++index) {
1494
- IntFree(self->values[index]);
1495
- }
1496
- } else {
1497
- for(index = from; index < to; ++index) {
1498
- ((values[index]) = IntNew());
1499
- }
1500
- }
1501
- free(self->values);
1502
- self->values = values;
1503
- self->element_count = new_element_count;
1504
- }
1505
- }
1506
-
1507
- static int PIntVectorComparator(void* lp_, void* rp_) {
1508
- int** lp = (int**)lp_;
1509
- int** rp = (int**)rp_;
1510
- if(((**lp) == (**rp))) {
1511
- return 0;
1512
- } else if(((**lp) < (**rp))) {
1513
- return -1;
1514
- } else {
1515
- return +1;
1516
- }
1517
- }
1518
- void PIntVectorSort(PIntVector* self) {
1519
- typedef int (*F)(const void*, const void*);
1520
- assert(self);
1521
- qsort(self->values, PIntVectorSize(self), sizeof(int*), (F)PIntVectorComparator);
1522
- }
1523
-
1524
- #define AUTOC_VALID_VALUE 1
1525
- #define AUTOC_VALID_KEY 2
1526
- #define AUTOC_OWNED_VALUE 4
1527
- #define AUTOC_OWNED_KEY 8
1528
- static ValueTypeMapEntry ValueTypeMapEntryKeyOnlyRef(ValueType* key) {
1529
- ValueTypeMapEntry entry;
1530
- entry.key = *key;
1531
- entry.flags = AUTOC_VALID_KEY;
1532
- return entry;
1533
- }
1534
- static ValueTypeMapEntry ValueTypeMapEntryKeyValueRef(ValueType* key, ValueType* value) {
1535
- ValueTypeMapEntry entry;
1536
- entry.key = *key;
1537
- entry.value = *value;
1538
- entry.flags = (AUTOC_VALID_KEY | AUTOC_VALID_VALUE);
1539
- return entry;
1540
- }
1541
- #define ValueTypeMapEntryIdentify(obj) ValueTypeMapEntryIdentifyRef(&obj)
1542
- static size_t ValueTypeMapEntryIdentifyRef(ValueTypeMapEntry* entry) {
1543
- return ValueTypeIdentify(entry->key);
1544
- }
1545
- #define ValueTypeMapEntryEqual(lt, rt) ValueTypeMapEntryEqualRef(&lt, &rt)
1546
- static int ValueTypeMapEntryEqualRef(ValueTypeMapEntry* lt, ValueTypeMapEntry* rt) {
1547
- return ValueTypeEqual(lt->key,rt->key);
1548
- }
1549
- #define ValueTypeMapEntryCopy(dst, src) ValueTypeMapEntryCopyRef(&dst, &src)
1550
- static void ValueTypeMapEntryCopyRef(ValueTypeMapEntry* dst, ValueTypeMapEntry* src) {
1551
- assert(src->flags & AUTOC_VALID_KEY);
1552
- dst->flags = (AUTOC_VALID_KEY | AUTOC_OWNED_KEY);
1553
- ValueTypeCopy(dst->key,src->key);
1554
- if(src->flags & AUTOC_VALID_VALUE) {
1555
- dst->flags |= (AUTOC_VALID_VALUE | AUTOC_OWNED_VALUE);
1556
- ValueTypeCopy(dst->value,src->value);
1557
- }
1558
- }
1559
- #define ValueTypeMapEntryDtor(obj) ValueTypeMapEntryDtorRef(&obj)
1560
- static void ValueTypeMapEntryDtorRef(ValueTypeMapEntry* entry) {
1561
- assert(entry->flags & AUTOC_VALID_KEY);
1562
- if(entry->flags & AUTOC_OWNED_KEY) ValueTypeDtor(entry->key);
1563
- if(entry->flags & AUTOC_VALID_VALUE && entry->flags & AUTOC_OWNED_VALUE) ValueTypeDtor(entry->value);
1564
- }
1565
- static ValueTypeMapEntry* ValueTypeMapItGetEntryRef(ValueTypeMapIt*);
1566
- static int ValueTypeMapContainsAllOf(ValueTypeMap* self, ValueTypeMap* other) {
1567
- ValueTypeMapIt it;
1568
- ValueTypeMapItCtor(&it, self);
1569
- while(ValueTypeMapItMove(&it)) {
1570
- int found = 0;
1571
- ValueTypeMapEntry* e = ValueTypeMapItGetEntryRef(&it);
1572
- if(ValueTypeMapContainsKey(other, e->key)) {
1573
- ValueType other_value = ValueTypeMapGet(other, e->key);
1574
- found = ValueTypeEqual(e->value,other_value);
1575
- ValueTypeDtor(other_value);
1576
- }
1577
- if(!found) return 0;
1578
- }
1579
- return 1;
1580
- }
1581
-
1582
- #define _ValueTypeMapSetCtor(self) ValueTypeMapSetCtor(&self)
1583
- #define _ValueTypeMapSetDtor(self) ValueTypeMapSetDtor(&self)
1584
- #define _ValueTypeMapSetIdentify(self) ValueTypeMapSetIdentify(&self)
1585
- #define _ValueTypeMapSetCopy(dst,src) ValueTypeMapSetCopy(&dst,&src)
1586
- #define _ValueTypeMapSetEqual(lt,rt) ValueTypeMapSetEqual(&lt,&rt)
1587
- #define _ValueTypeMapSetLess(lt,rt) ValueTypeMapSetLess(&lt,&rt)
1588
-
1589
- AUTOC_STATIC void ValueTypeMapSetCtor(ValueTypeMapSet*);
1590
- AUTOC_STATIC void ValueTypeMapSetDtor(ValueTypeMapSet*);
1591
- AUTOC_STATIC void ValueTypeMapSetCopy(ValueTypeMapSet*,ValueTypeMapSet*);
1592
- AUTOC_STATIC int ValueTypeMapSetEqual(ValueTypeMapSet*,ValueTypeMapSet*);
1593
- AUTOC_STATIC size_t ValueTypeMapSetIdentify(ValueTypeMapSet*);
1594
- AUTOC_STATIC void ValueTypeMapSetPurge(ValueTypeMapSet*);
1595
- AUTOC_STATIC int ValueTypeMapSetContains(ValueTypeMapSet*, ValueTypeMapEntry);
1596
- AUTOC_STATIC ValueTypeMapEntry ValueTypeMapSetGet(ValueTypeMapSet*, ValueTypeMapEntry);
1597
- AUTOC_STATIC size_t ValueTypeMapSetSize(ValueTypeMapSet*);
1598
- #define ValueTypeMapSetEmpty(self) (ValueTypeMapSetSize(self) == 0)
1599
- AUTOC_STATIC int ValueTypeMapSetPut(ValueTypeMapSet*, ValueTypeMapEntry);
1600
- AUTOC_STATIC int ValueTypeMapSetReplace(ValueTypeMapSet*, ValueTypeMapEntry);
1601
- AUTOC_STATIC int ValueTypeMapSetRemove(ValueTypeMapSet*, ValueTypeMapEntry);
1602
- AUTOC_STATIC void ValueTypeMapSetExclude(ValueTypeMapSet*, ValueTypeMapSet*);
1603
- AUTOC_STATIC void ValueTypeMapSetRetain(ValueTypeMapSet*, ValueTypeMapSet*);
1604
- AUTOC_STATIC void ValueTypeMapSetInclude(ValueTypeMapSet*, ValueTypeMapSet*);
1605
- AUTOC_STATIC void ValueTypeMapSetInvert(ValueTypeMapSet*, ValueTypeMapSet*);
1606
- AUTOC_STATIC void ValueTypeMapSetItCtor(ValueTypeMapSetIt*, ValueTypeMapSet*);
1607
- AUTOC_STATIC int ValueTypeMapSetItMove(ValueTypeMapSetIt*);
1608
- AUTOC_STATIC ValueTypeMapEntry ValueTypeMapSetItGet(ValueTypeMapSetIt*);
1609
-
1610
- #define _ValueTypeMapSetListCtor(self) ValueTypeMapSetListCtor(&self)
1611
- #define _ValueTypeMapSetListDtor(self) ValueTypeMapSetListDtor(&self)
1612
- #define _ValueTypeMapSetListIdentify(self) ValueTypeMapSetListIdentify(&self)
1613
- #define _ValueTypeMapSetListCopy(dst,src) ValueTypeMapSetListCopy(&dst,&src)
1614
- #define _ValueTypeMapSetListEqual(lt,rt) ValueTypeMapSetListEqual(&lt,&rt)
1615
- #define _ValueTypeMapSetListLess(lt,rt) ValueTypeMapSetListLess(&lt,&rt)
1616
-
1617
- AUTOC_STATIC void ValueTypeMapSetListCtor(ValueTypeMapSetList*);
1618
- AUTOC_STATIC void ValueTypeMapSetListDtor(ValueTypeMapSetList*);
1619
- AUTOC_STATIC void ValueTypeMapSetListCopy(ValueTypeMapSetList*,ValueTypeMapSetList*);
1620
- AUTOC_STATIC int ValueTypeMapSetListEqual(ValueTypeMapSetList*,ValueTypeMapSetList*);
1621
- AUTOC_STATIC size_t ValueTypeMapSetListIdentify(ValueTypeMapSetList*);
1622
- AUTOC_STATIC void ValueTypeMapSetListPurge(ValueTypeMapSetList*);
1623
- AUTOC_STATIC ValueTypeMapEntry ValueTypeMapSetListPeek(ValueTypeMapSetList*);
1624
- AUTOC_STATIC ValueTypeMapEntry ValueTypeMapSetListPop(ValueTypeMapSetList*);
1625
- AUTOC_STATIC void ValueTypeMapSetListPush(ValueTypeMapSetList*, ValueTypeMapEntry);
1626
- AUTOC_STATIC int ValueTypeMapSetListContains(ValueTypeMapSetList*, ValueTypeMapEntry);
1627
- AUTOC_STATIC ValueTypeMapEntry ValueTypeMapSetListFind(ValueTypeMapSetList*, ValueTypeMapEntry);
1628
- #define ValueTypeMapSetListReplace(self, with) ValueTypeMapSetListReplaceEx(self, with, 1)
1629
- #define ValueTypeMapSetListReplaceAll(self, with) ValueTypeMapSetListReplaceEx(self, with, -1)
1630
- AUTOC_STATIC int ValueTypeMapSetListReplaceEx(ValueTypeMapSetList*, ValueTypeMapEntry, int);
1631
- #define ValueTypeMapSetListRemove(self, what) ValueTypeMapSetListRemoveEx(self, what, 1)
1632
- #define ValueTypeMapSetListRemoveAll(self, what) ValueTypeMapSetListRemoveEx(self, what, -1)
1633
- AUTOC_STATIC int ValueTypeMapSetListRemoveEx(ValueTypeMapSetList*, ValueTypeMapEntry, int);
1634
- AUTOC_STATIC size_t ValueTypeMapSetListSize(ValueTypeMapSetList*);
1635
- #define ValueTypeMapSetListEmpty(self) (ValueTypeMapSetListSize(self) == 0)
1636
- AUTOC_STATIC void ValueTypeMapSetListItCtor(ValueTypeMapSetListIt*, ValueTypeMapSetList*);
1637
- AUTOC_STATIC int ValueTypeMapSetListItMove(ValueTypeMapSetListIt*);
1638
- AUTOC_STATIC ValueTypeMapEntry ValueTypeMapSetListItGet(ValueTypeMapSetListIt*);
1639
-
1640
- AUTOC_STATIC ValueTypeMapEntry* ValueTypeMapSetListItGetRef(ValueTypeMapSetListIt*);
1641
- AUTOC_STATIC void ValueTypeMapSetListCtor(ValueTypeMapSetList* self) {
1642
- assert(self);
1643
- self->head_node = NULL;
1644
- self->node_count = 0;
1645
- }
1646
- AUTOC_STATIC void ValueTypeMapSetListDtor(ValueTypeMapSetList* self) {
1647
- ValueTypeMapSetListNode* node;
1648
- assert(self);
1649
- node = self->head_node;
1650
- while(node) {
1651
- ValueTypeMapSetListNode* this_node = node;
1652
- node = node->next_node;
1653
- ValueTypeMapEntryDtor(this_node->element);
1654
- free(this_node);
1655
- }
1656
- }
1657
- AUTOC_STATIC void ValueTypeMapSetListCopy(ValueTypeMapSetList* dst,ValueTypeMapSetList* src) {
1658
- ValueTypeMapSetListIt it;
1659
- assert(src);
1660
- assert(dst);
1661
- ValueTypeMapSetListCtor(dst);
1662
- ValueTypeMapSetListItCtor(&it, src);
1663
- while(ValueTypeMapSetListItMove(&it)) {
1664
- ValueTypeMapSetListPush(dst, *ValueTypeMapSetListItGetRef(&it));
1665
- }
1666
- }
1667
- AUTOC_STATIC int ValueTypeMapSetListEqual(ValueTypeMapSetList* lt,ValueTypeMapSetList* rt) {
1668
- if(ValueTypeMapSetListSize(lt) == ValueTypeMapSetListSize(rt)) {
1669
- ValueTypeMapSetListIt lit, rit;
1670
- ValueTypeMapSetListItCtor(&lit, lt);
1671
- ValueTypeMapSetListItCtor(&rit, rt);
1672
- while(ValueTypeMapSetListItMove(&lit) && ValueTypeMapSetListItMove(&rit)) {
1673
- int equal;
1674
- ValueTypeMapEntry* le;
1675
- ValueTypeMapEntry* re;
1676
- le = ValueTypeMapSetListItGetRef(&lit);
1677
- re = ValueTypeMapSetListItGetRef(&rit);
1678
- equal = ValueTypeMapEntryEqual(*le,*re);
1679
- if(!equal) return 0;
1680
- }
1681
- return 1;
1682
- } else
1683
- return 0;
1684
- }
1685
- AUTOC_STATIC size_t ValueTypeMapSetListIdentify(ValueTypeMapSetList* self) {
1686
- ValueTypeMapSetListNode* node;
1687
- size_t result = 0;
1688
- assert(self);
1689
- for(node = self->head_node; node != NULL; node = node->next_node) {
1690
- result ^= ValueTypeMapEntryIdentify(node->element);
1691
- result = AUTOC_RCYCLE(result);
1692
- }
1693
- return result;
1694
- }
1695
- AUTOC_STATIC void ValueTypeMapSetListPurge(ValueTypeMapSetList* self) {
1696
- ValueTypeMapSetListDtor(self);
1697
- ValueTypeMapSetListCtor(self);
1698
- }
1699
- AUTOC_STATIC ValueTypeMapEntry ValueTypeMapSetListPeek(ValueTypeMapSetList* self) {
1700
- ValueTypeMapEntry result;
1701
- assert(self);
1702
- assert(!ValueTypeMapSetListEmpty(self));
1703
- ValueTypeMapEntryCopy(result,self->head_node->element);
1704
- return result;
1705
- }
1706
- AUTOC_STATIC ValueTypeMapEntry ValueTypeMapSetListPop(ValueTypeMapSetList* self) {
1707
- ValueTypeMapSetListNode* node;
1708
- ValueTypeMapEntry result;
1709
- assert(self);
1710
- assert(!ValueTypeMapSetListEmpty(self));
1711
- node = self->head_node;
1712
- result = node->element;
1713
- self->head_node = self->head_node->next_node;
1714
- --self->node_count;
1715
- free(node);
1716
- return result;
1717
- }
1718
- AUTOC_STATIC void ValueTypeMapSetListPush(ValueTypeMapSetList* self, ValueTypeMapEntry element) {
1719
- ValueTypeMapSetListNode* node;
1720
- assert(self);
1721
- node = (ValueTypeMapSetListNode*)malloc(sizeof(ValueTypeMapSetListNode));
1722
- assert(node);
1723
- ValueTypeMapEntryCopy(node->element,element);
1724
- node->next_node = self->head_node;
1725
- self->head_node = node;
1726
- ++self->node_count;
1727
- }
1728
- AUTOC_STATIC int ValueTypeMapSetListContains(ValueTypeMapSetList* self, ValueTypeMapEntry what) {
1729
- ValueTypeMapSetListNode* node;
1730
- int found = 0;
1731
- assert(self);
1732
- node = self->head_node;
1733
- while(node) {
1734
- if(ValueTypeMapEntryEqual(node->element,what)) {
1735
- found = 1;
1736
- break;
1737
- }
1738
- node = node->next_node;
1739
- }
1740
- return found;
1741
- }
1742
- AUTOC_STATIC ValueTypeMapEntry ValueTypeMapSetListFind(ValueTypeMapSetList* self, ValueTypeMapEntry what) {
1743
- ValueTypeMapSetListNode* node;
1744
- assert(self);
1745
- assert(ValueTypeMapSetListContains(self, what));
1746
- node = self->head_node;
1747
- while(node) {
1748
- if(ValueTypeMapEntryEqual(node->element,what)) {
1749
- ValueTypeMapEntry result;
1750
- ValueTypeMapEntryCopy(result,node->element);
1751
- return result;
1752
- }
1753
- node = node->next_node;
1754
- }
1755
- abort();
1756
- }
1757
- AUTOC_STATIC int ValueTypeMapSetListReplaceEx(ValueTypeMapSetList* self, ValueTypeMapEntry with, int count) {
1758
- ValueTypeMapSetListNode* node;
1759
- int replaced = 0;
1760
- assert(self);
1761
- if(count == 0) return 0;
1762
- node = self->head_node;
1763
- while(node) {
1764
- if(ValueTypeMapEntryEqual(node->element,with)) {
1765
- ValueTypeMapEntryDtor(node->element);
1766
- ValueTypeMapEntryCopy(node->element,with);
1767
- ++replaced;
1768
- if(count > 0 && replaced >= count) break;
1769
- }
1770
- node = node->next_node;
1771
- }
1772
- return replaced;
1773
- }
1774
- AUTOC_STATIC int ValueTypeMapSetListRemoveEx(ValueTypeMapSetList* self, ValueTypeMapEntry what, int count) {
1775
- ValueTypeMapSetListNode *node, *prev_node;
1776
- int removed = 0;
1777
- assert(self);
1778
- if(count == 0) return 0;
1779
- node = self->head_node;
1780
- prev_node = NULL;
1781
- while(node) {
1782
- if(ValueTypeMapEntryEqual(node->element,what)) {
1783
- ValueTypeMapSetListNode* this_node;
1784
- if(prev_node) {
1785
- this_node = prev_node->next_node = node->next_node;
1786
- } else {
1787
- this_node = self->head_node = node->next_node;
1788
- }
1789
- ++removed;
1790
- --self->node_count;
1791
- ValueTypeMapEntryDtor(node->element);
1792
- free(node);
1793
- node = this_node;
1794
- if(count > 0 && removed >= count) break;
1795
- } else {
1796
- prev_node = node;
1797
- node = node->next_node;
1798
- }
1799
- }
1800
- return removed;
1801
- }
1802
- AUTOC_STATIC size_t ValueTypeMapSetListSize(ValueTypeMapSetList* self) {
1803
- assert(self);
1804
- return self->node_count;
1805
- }
1806
- AUTOC_STATIC void ValueTypeMapSetListItCtor(ValueTypeMapSetListIt* self, ValueTypeMapSetList* list) {
1807
- assert(self);
1808
- assert(list);
1809
- self->start = 1;
1810
- self->list = list;
1811
- }
1812
- AUTOC_STATIC int ValueTypeMapSetListItMove(ValueTypeMapSetListIt* self) {
1813
- assert(self);
1814
- if(self->start) {
1815
- self->this_node = self->list->head_node;
1816
- self->start = 0;
1817
- } else {
1818
- self->this_node = self->this_node ? self->this_node->next_node : NULL;
1819
- }
1820
- return self->this_node != NULL;
1821
- }
1822
- AUTOC_STATIC ValueTypeMapEntry ValueTypeMapSetListItGet(ValueTypeMapSetListIt* self) {
1823
- ValueTypeMapEntry result;
1824
- assert(self);
1825
- assert(self->this_node);
1826
- ValueTypeMapEntryCopy(result,self->this_node->element);
1827
- return result;
1828
- }
1829
- AUTOC_STATIC ValueTypeMapEntry* ValueTypeMapSetListItGetRef(ValueTypeMapSetListIt* self) {
1830
- assert(self);
1831
- assert(self->this_node);
1832
- return &self->this_node->element;
1833
- }
1834
-
1835
- AUTOC_STATIC ValueTypeMapEntry* ValueTypeMapSetItGetRef(ValueTypeMapSetIt*);
1836
- static void ValueTypeMapSetRehash(ValueTypeMapSet* self) {
1837
- ValueTypeMapSetList* buckets;
1838
- size_t index, bucket_count, size, fill;
1839
- assert(self);
1840
- assert(self->min_fill > 0);
1841
- assert(self->max_fill > 0);
1842
- assert(self->min_fill < self->max_fill);
1843
- assert(self->min_bucket_count > 0);
1844
- if(self->buckets) {
1845
- if(self->min_size < self->size && self->size < self->max_size) return;
1846
- fill = (size_t)((float)self->size/self->bucket_count*100);
1847
- if(fill > self->max_fill) {
1848
- bucket_count = (size_t)((float)self->bucket_count/100*self->capacity_multiplier);
1849
- } else if(fill < self->min_fill && self->bucket_count > self->min_bucket_count) {
1850
- bucket_count = (size_t)((float)self->bucket_count/self->capacity_multiplier*100);
1851
- if(bucket_count < self->min_bucket_count) bucket_count = self->min_bucket_count;
1852
- } else
1853
- return;
1854
- size = self->size;
1855
- self->min_size = (size_t)((float)self->min_fill/100*size);
1856
- self->max_size = (size_t)((float)self->max_fill/100*size);
1857
- } else {
1858
- bucket_count = self->min_bucket_count;
1859
- size = 0;
1860
- }
1861
- buckets = (ValueTypeMapSetList*)malloc(bucket_count*sizeof(ValueTypeMapSetList));
1862
- assert(buckets);
1863
- for(index = 0; index < bucket_count; ++index) {
1864
- ValueTypeMapSetListCtor(&buckets[index]);
1865
- }
1866
- if(self->buckets) {
1867
- ValueTypeMapSetIt it;
1868
- ValueTypeMapSetItCtor(&it, self);
1869
- while(ValueTypeMapSetItMove(&it)) {
1870
- ValueTypeMapSetList* bucket;
1871
- ValueTypeMapEntry element = ValueTypeMapSetItGet(&it);
1872
- bucket = &buckets[ValueTypeMapEntryIdentify(element) % bucket_count];
1873
- ValueTypeMapSetListPush(bucket, element);
1874
- ValueTypeMapEntryDtor(element);
1875
- }
1876
- ValueTypeMapSetDtor(self);
1877
- }
1878
- self->buckets = buckets;
1879
- self->bucket_count = bucket_count;
1880
- self->size = size;
1881
- }
1882
- static int ValueTypeMapSetContainsAllOf(ValueTypeMapSet* self, ValueTypeMapSet* other) {
1883
- ValueTypeMapSetIt it;
1884
- ValueTypeMapSetItCtor(&it, self);
1885
- while(ValueTypeMapSetItMove(&it)) {
1886
- int found = 0;
1887
- if(ValueTypeMapSetContains(other, *ValueTypeMapSetItGetRef(&it))) found = 1;
1888
- if(!found) return 0;
1889
- }
1890
- return 1;
1891
- }
1892
- AUTOC_STATIC void ValueTypeMapSetCtor(ValueTypeMapSet* self) {
1893
- assert(self);
1894
- self->min_bucket_count = 16;
1895
- self->min_fill = 20;
1896
- self->max_fill = 80;
1897
- self->min_size = (size_t)((float)self->min_fill/100*self->min_bucket_count);
1898
- self->max_size = (size_t)((float)self->max_fill/100*self->min_bucket_count);
1899
- self->capacity_multiplier = 200;
1900
- self->buckets = NULL;
1901
- ValueTypeMapSetRehash(self);
1902
- }
1903
- AUTOC_STATIC void ValueTypeMapSetDtor(ValueTypeMapSet* self) {
1904
- size_t i;
1905
- assert(self);
1906
- for(i = 0; i < self->bucket_count; ++i) {
1907
- ValueTypeMapSetListDtor(&self->buckets[i]);
1908
- }
1909
- free(self->buckets);
1910
- }
1911
- AUTOC_STATIC void ValueTypeMapSetCopy(ValueTypeMapSet* dst,ValueTypeMapSet* src) {
1912
- ValueTypeMapSetIt it;
1913
- assert(src);
1914
- assert(dst);
1915
- ValueTypeMapSetCtor(dst);
1916
- ValueTypeMapSetItCtor(&it, src);
1917
- while(ValueTypeMapSetItMove(&it)) ValueTypeMapSetPut(dst, *ValueTypeMapSetItGetRef(&it));
1918
- }
1919
- AUTOC_STATIC int ValueTypeMapSetEqual(ValueTypeMapSet* lt,ValueTypeMapSet* rt) {
1920
- assert(lt);
1921
- assert(rt);
1922
- return ValueTypeMapSetSize(lt) == ValueTypeMapSetSize(rt) && ValueTypeMapSetContainsAllOf(lt, rt) && ValueTypeMapSetContainsAllOf(rt, lt);
1923
- }
1924
- AUTOC_STATIC size_t ValueTypeMapSetIdentify(ValueTypeMapSet* self) {
1925
- ValueTypeMapSetIt it;
1926
- size_t result = 0;
1927
- assert(self);
1928
- ValueTypeMapSetItCtor(&it, self);
1929
- while(ValueTypeMapSetItMove(&it)) {
1930
- ValueTypeMapEntry* e = ValueTypeMapSetItGetRef(&it);
1931
- result ^= ValueTypeMapEntryIdentify(*e);
1932
- result = AUTOC_RCYCLE(result);
1933
- }
1934
- return result;
1935
- }
1936
- AUTOC_STATIC void ValueTypeMapSetPurge(ValueTypeMapSet* self) {
1937
- assert(self);
1938
- ValueTypeMapSetDtor(self);
1939
- self->buckets = NULL;
1940
- ValueTypeMapSetRehash(self);
1941
- }
1942
- AUTOC_STATIC int ValueTypeMapSetContains(ValueTypeMapSet* self, ValueTypeMapEntry element) {
1943
- assert(self);
1944
- return ValueTypeMapSetListContains(&self->buckets[ValueTypeMapEntryIdentify(element) % self->bucket_count], element);
1945
- }
1946
- AUTOC_STATIC ValueTypeMapEntry ValueTypeMapSetGet(ValueTypeMapSet* self, ValueTypeMapEntry element) {
1947
- ValueTypeMapEntry result;
1948
- assert(self);
1949
- assert(ValueTypeMapSetContains(self, element));
1950
- result = ValueTypeMapSetListFind(&self->buckets[ValueTypeMapEntryIdentify(element) % self->bucket_count], element);
1951
- return result;
1952
- }
1953
- AUTOC_STATIC size_t ValueTypeMapSetSize(ValueTypeMapSet* self) {
1954
- assert(self);
1955
- return self->size;
1956
- }
1957
- AUTOC_STATIC int ValueTypeMapSetPut(ValueTypeMapSet* self, ValueTypeMapEntry element) {
1958
- ValueTypeMapSetList* bucket;
1959
- assert(self);
1960
- bucket = &self->buckets[ValueTypeMapEntryIdentify(element) % self->bucket_count];
1961
- if(!ValueTypeMapSetListContains(bucket, element)) {
1962
- ValueTypeMapSetListPush(bucket, element);
1963
- ++self->size;
1964
- ValueTypeMapSetRehash(self);
1965
- return 1;
1966
- }
1967
- return 0;
1968
- }
1969
- AUTOC_STATIC int ValueTypeMapSetReplace(ValueTypeMapSet* self, ValueTypeMapEntry element) {
1970
- ValueTypeMapSetList* bucket;
1971
- assert(self);
1972
- bucket = &self->buckets[ValueTypeMapEntryIdentify(element) % self->bucket_count];
1973
- return ValueTypeMapSetListReplace(bucket, element);
1974
- }
1975
- AUTOC_STATIC int ValueTypeMapSetRemove(ValueTypeMapSet* self, ValueTypeMapEntry element) {
1976
- ValueTypeMapSetList* bucket;
1977
- assert(self);
1978
- bucket = &self->buckets[ValueTypeMapEntryIdentify(element) % self->bucket_count];
1979
- if(ValueTypeMapSetListRemove(bucket, element)) {
1980
- --self->size;
1981
- ValueTypeMapSetRehash(self);
1982
- return 1;
1983
- }
1984
- return 0;
1985
- }
1986
- AUTOC_STATIC void ValueTypeMapSetExclude(ValueTypeMapSet* self, ValueTypeMapSet* other) {
1987
- ValueTypeMapSetIt it;
1988
- assert(self);
1989
- assert(other);
1990
- ValueTypeMapSetItCtor(&it, other);
1991
- while(ValueTypeMapSetItMove(&it)) ValueTypeMapSetRemove(self, *ValueTypeMapSetItGetRef(&it));
1992
- }
1993
- AUTOC_STATIC void ValueTypeMapSetInclude(ValueTypeMapSet* self, ValueTypeMapSet* other) {
1994
- ValueTypeMapSetIt it;
1995
- assert(self);
1996
- assert(other);
1997
- ValueTypeMapSetItCtor(&it, other);
1998
- while(ValueTypeMapSetItMove(&it)) ValueTypeMapSetPut(self, *ValueTypeMapSetItGetRef(&it));
1999
- }
2000
- AUTOC_STATIC void ValueTypeMapSetRetain(ValueTypeMapSet* self, ValueTypeMapSet* other) {
2001
- ValueTypeMapSetIt it;
2002
- ValueTypeMapSet set;
2003
- assert(self);
2004
- assert(other);
2005
- ValueTypeMapSetCtor(&set);
2006
- ValueTypeMapSetItCtor(&it, self);
2007
- while(ValueTypeMapSetItMove(&it)) {
2008
- ValueTypeMapEntry* e = ValueTypeMapSetItGetRef(&it);
2009
- if(ValueTypeMapSetContains(other, *e)) ValueTypeMapSetPut(&set, *e);
2010
- }
2011
- ValueTypeMapSetDtor(self);
2012
- *self = set;
2013
- }
2014
- AUTOC_STATIC void ValueTypeMapSetInvert(ValueTypeMapSet* self, ValueTypeMapSet* other) {
2015
- ValueTypeMapSetIt it;
2016
- ValueTypeMapSet set;
2017
- assert(self);
2018
- assert(other);
2019
- ValueTypeMapSetCtor(&set);
2020
- ValueTypeMapSetItCtor(&it, self);
2021
- while(ValueTypeMapSetItMove(&it)) {
2022
- ValueTypeMapEntry* e = ValueTypeMapSetItGetRef(&it);
2023
- if(!ValueTypeMapSetContains(other, *e)) ValueTypeMapSetPut(&set, *e);
2024
- }
2025
- ValueTypeMapSetItCtor(&it, other);
2026
- while(ValueTypeMapSetItMove(&it)) {
2027
- ValueTypeMapEntry* e = ValueTypeMapSetItGetRef(&it);
2028
- if(!ValueTypeMapSetContains(self, *e)) ValueTypeMapSetPut(&set, *e);
2029
- }
2030
- ValueTypeMapSetDtor(self);
2031
- *self = set;
2032
- }
2033
- AUTOC_STATIC void ValueTypeMapSetItCtor(ValueTypeMapSetIt* self, ValueTypeMapSet* set) {
2034
- assert(self);
2035
- self->set = set;
2036
- self->bucket_index = -1;
2037
- }
2038
- AUTOC_STATIC int ValueTypeMapSetItMove(ValueTypeMapSetIt* self) {
2039
- assert(self);
2040
- if(self->bucket_index < 0) ValueTypeMapSetListItCtor(&self->it, &self->set->buckets[self->bucket_index = 0]);
2041
- if(ValueTypeMapSetListItMove(&self->it)) return 1;
2042
- while(++self->bucket_index < self->set->bucket_count) {
2043
- ValueTypeMapSetListItCtor(&self->it, &self->set->buckets[self->bucket_index]);
2044
- if(ValueTypeMapSetListItMove(&self->it)) return 1;
2045
- }
2046
- return 0;
2047
- }
2048
- AUTOC_STATIC ValueTypeMapEntry ValueTypeMapSetItGet(ValueTypeMapSetIt* self) {
2049
- assert(self);
2050
- return ValueTypeMapSetListItGet(&self->it);
2051
- }
2052
- AUTOC_STATIC ValueTypeMapEntry* ValueTypeMapSetItGetRef(ValueTypeMapSetIt* self) {
2053
- assert(self);
2054
- return ValueTypeMapSetListItGetRef(&self->it);
2055
- }
2056
-
2057
- void ValueTypeMapCtor(ValueTypeMap* self) {
2058
- assert(self);
2059
- ValueTypeMapSetCtor(&self->entries);
2060
- }
2061
- void ValueTypeMapDtor(ValueTypeMap* self) {
2062
- assert(self);
2063
- ValueTypeMapSetDtor(&self->entries);
2064
- }
2065
- static int ValueTypeMapPutEntryRef(ValueTypeMap* self, ValueTypeMapEntry* entry) {
2066
- int absent;
2067
- assert(self);
2068
- assert(entry);
2069
- if((absent = !ValueTypeMapContainsKey(self, entry->key))) {
2070
- ValueTypeMapSetPut(&self->entries, *entry);
2071
- }
2072
- return absent;
2073
- }
2074
- void ValueTypeMapCopy(ValueTypeMap* dst,ValueTypeMap* src) {
2075
- ValueTypeMapIt it;
2076
- assert(src);
2077
- assert(dst);
2078
- ValueTypeMapCtor(dst);
2079
- ValueTypeMapItCtor(&it, src);
2080
- while(ValueTypeMapItMove(&it)) {
2081
- ValueTypeMapEntry* e = ValueTypeMapItGetEntryRef(&it);
2082
- ValueTypeMapPutEntryRef(dst, e);
2083
- }
2084
- }
2085
- int ValueTypeMapEqual(ValueTypeMap* lt,ValueTypeMap* rt) {
2086
- assert(lt);
2087
- assert(rt);
2088
- return ValueTypeMapSize(lt) == ValueTypeMapSize(rt) && ValueTypeMapContainsAllOf(lt, rt) && ValueTypeMapContainsAllOf(rt, lt);
2089
- }
2090
- size_t ValueTypeMapIdentify(ValueTypeMap* self) {
2091
- assert(self);
2092
- return ValueTypeMapSetIdentify(&self->entries); /* TODO : make use of the values' hashes */
2093
- }
2094
- void ValueTypeMapPurge(ValueTypeMap* self) {
2095
- assert(self);
2096
- ValueTypeMapSetPurge(&self->entries);
2097
- }
2098
- size_t ValueTypeMapSize(ValueTypeMap* self) {
2099
- assert(self);
2100
- return ValueTypeMapSetSize(&self->entries);
2101
- }
2102
- int ValueTypeMapContainsKey(ValueTypeMap* self, ValueType key) {
2103
- int result;
2104
- ValueTypeMapEntry entry;
2105
- assert(self);
2106
- result = ValueTypeMapSetContains(&self->entries, entry = ValueTypeMapEntryKeyOnlyRef(&key));
2107
- ValueTypeMapEntryDtor(entry);
2108
- return result;
2109
- }
2110
- ValueType ValueTypeMapGet(ValueTypeMap* self, ValueType key) {
2111
- ValueType result;
2112
- ValueTypeMapEntry entry, existing_entry;
2113
- assert(self);
2114
- assert(ValueTypeMapContainsKey(self, key));
2115
- existing_entry = ValueTypeMapSetGet(&self->entries, entry = ValueTypeMapEntryKeyOnlyRef(&key));
2116
- ValueTypeCopy(result,existing_entry.value);
2117
- ValueTypeMapEntryDtor(existing_entry);
2118
- ValueTypeMapEntryDtor(entry);
2119
- return result;
2120
- }
2121
- int ValueTypeMapPut(ValueTypeMap* self, ValueType key, ValueType value) {
2122
- int result;
2123
- ValueTypeMapEntry entry;
2124
- assert(self);
2125
- entry = ValueTypeMapEntryKeyValueRef(&key, &value);
2126
- result = ValueTypeMapPutEntryRef(self, &entry);
2127
- ValueTypeMapEntryDtor(entry);
2128
- return result;
2129
- }
2130
- int ValueTypeMapReplace(ValueTypeMap* self, ValueType key, ValueType value) {
2131
- int result;
2132
- ValueTypeMapEntry entry;
2133
- assert(self);
2134
- entry = ValueTypeMapEntryKeyValueRef(&key, &value);
2135
- result = ValueTypeMapSetReplace(&self->entries, entry);
2136
- ValueTypeMapEntryDtor(entry);
2137
- return result;
2138
- }
2139
- int ValueTypeMapRemove(ValueTypeMap* self, ValueType key) {
2140
- int result;
2141
- ValueTypeMapEntry entry;
2142
- assert(self);
2143
- result = ValueTypeMapSetRemove(&self->entries, entry = ValueTypeMapEntryKeyOnlyRef(&key));
2144
- ValueTypeMapEntryDtor(entry);
2145
- return result;
2146
- }
2147
- void ValueTypeMapItCtor(ValueTypeMapIt* self, ValueTypeMap* map) {
2148
- assert(self);
2149
- assert(map);
2150
- ValueTypeMapSetItCtor(&self->it, &map->entries);
2151
- }
2152
- int ValueTypeMapItMove(ValueTypeMapIt* self) {
2153
- assert(self);
2154
- return ValueTypeMapSetItMove(&self->it);
2155
- }
2156
- ValueType ValueTypeMapItGetKey(ValueTypeMapIt* self) {
2157
- ValueTypeMapEntry* e;
2158
- ValueType key;
2159
- assert(self);
2160
- e = ValueTypeMapItGetEntryRef(self);
2161
- ValueTypeCopy(key,e->key);
2162
- return key;
2163
- }
2164
- ValueType ValueTypeMapItGetElement(ValueTypeMapIt* self) {
2165
- ValueTypeMapEntry* e;
2166
- ValueType value;
2167
- assert(self);
2168
- e = ValueTypeMapItGetEntryRef(self);
2169
- assert(e->flags & AUTOC_VALID_VALUE);
2170
- ValueTypeCopy(value,e->value);
2171
- return value;
2172
- }
2173
- static ValueTypeMapEntry* ValueTypeMapItGetEntryRef(ValueTypeMapIt* self) {
2174
- assert(self);
2175
- return ValueTypeMapSetItGetRef(&self->it);
2176
- }
2177
- #undef AUTOC_VALID_VALUE
2178
- #undef AUTOC_VALID_KEY
2179
- #undef AUTOC_OWNED_VALUE
2180
- #undef AUTOC_OWNED_KEY
2181
-
2182
- #define _ValueTypeSetListCtor(self) ValueTypeSetListCtor(&self)
2183
- #define _ValueTypeSetListDtor(self) ValueTypeSetListDtor(&self)
2184
- #define _ValueTypeSetListIdentify(self) ValueTypeSetListIdentify(&self)
2185
- #define _ValueTypeSetListCopy(dst,src) ValueTypeSetListCopy(&dst,&src)
2186
- #define _ValueTypeSetListEqual(lt,rt) ValueTypeSetListEqual(&lt,&rt)
2187
- #define _ValueTypeSetListLess(lt,rt) ValueTypeSetListLess(&lt,&rt)
2188
-
2189
- AUTOC_STATIC void ValueTypeSetListCtor(ValueTypeSetList*);
2190
- AUTOC_STATIC void ValueTypeSetListDtor(ValueTypeSetList*);
2191
- AUTOC_STATIC void ValueTypeSetListCopy(ValueTypeSetList*,ValueTypeSetList*);
2192
- AUTOC_STATIC int ValueTypeSetListEqual(ValueTypeSetList*,ValueTypeSetList*);
2193
- AUTOC_STATIC size_t ValueTypeSetListIdentify(ValueTypeSetList*);
2194
- AUTOC_STATIC void ValueTypeSetListPurge(ValueTypeSetList*);
2195
- AUTOC_STATIC ValueType ValueTypeSetListPeek(ValueTypeSetList*);
2196
- AUTOC_STATIC ValueType ValueTypeSetListPop(ValueTypeSetList*);
2197
- AUTOC_STATIC void ValueTypeSetListPush(ValueTypeSetList*, ValueType);
2198
- AUTOC_STATIC int ValueTypeSetListContains(ValueTypeSetList*, ValueType);
2199
- AUTOC_STATIC ValueType ValueTypeSetListFind(ValueTypeSetList*, ValueType);
2200
- #define ValueTypeSetListReplace(self, with) ValueTypeSetListReplaceEx(self, with, 1)
2201
- #define ValueTypeSetListReplaceAll(self, with) ValueTypeSetListReplaceEx(self, with, -1)
2202
- AUTOC_STATIC int ValueTypeSetListReplaceEx(ValueTypeSetList*, ValueType, int);
2203
- #define ValueTypeSetListRemove(self, what) ValueTypeSetListRemoveEx(self, what, 1)
2204
- #define ValueTypeSetListRemoveAll(self, what) ValueTypeSetListRemoveEx(self, what, -1)
2205
- AUTOC_STATIC int ValueTypeSetListRemoveEx(ValueTypeSetList*, ValueType, int);
2206
- AUTOC_STATIC size_t ValueTypeSetListSize(ValueTypeSetList*);
2207
- #define ValueTypeSetListEmpty(self) (ValueTypeSetListSize(self) == 0)
2208
- AUTOC_STATIC void ValueTypeSetListItCtor(ValueTypeSetListIt*, ValueTypeSetList*);
2209
- AUTOC_STATIC int ValueTypeSetListItMove(ValueTypeSetListIt*);
2210
- AUTOC_STATIC ValueType ValueTypeSetListItGet(ValueTypeSetListIt*);
2211
-
2212
- AUTOC_STATIC ValueType* ValueTypeSetListItGetRef(ValueTypeSetListIt*);
2213
- AUTOC_STATIC void ValueTypeSetListCtor(ValueTypeSetList* self) {
2214
- assert(self);
2215
- self->head_node = NULL;
2216
- self->node_count = 0;
2217
- }
2218
- AUTOC_STATIC void ValueTypeSetListDtor(ValueTypeSetList* self) {
2219
- ValueTypeSetListNode* node;
2220
- assert(self);
2221
- node = self->head_node;
2222
- while(node) {
2223
- ValueTypeSetListNode* this_node = node;
2224
- node = node->next_node;
2225
- ValueTypeDtor(this_node->element);
2226
- free(this_node);
2227
- }
2228
- }
2229
- AUTOC_STATIC void ValueTypeSetListCopy(ValueTypeSetList* dst,ValueTypeSetList* src) {
2230
- ValueTypeSetListIt it;
2231
- assert(src);
2232
- assert(dst);
2233
- ValueTypeSetListCtor(dst);
2234
- ValueTypeSetListItCtor(&it, src);
2235
- while(ValueTypeSetListItMove(&it)) {
2236
- ValueTypeSetListPush(dst, *ValueTypeSetListItGetRef(&it));
2237
- }
2238
- }
2239
- AUTOC_STATIC int ValueTypeSetListEqual(ValueTypeSetList* lt,ValueTypeSetList* rt) {
2240
- if(ValueTypeSetListSize(lt) == ValueTypeSetListSize(rt)) {
2241
- ValueTypeSetListIt lit, rit;
2242
- ValueTypeSetListItCtor(&lit, lt);
2243
- ValueTypeSetListItCtor(&rit, rt);
2244
- while(ValueTypeSetListItMove(&lit) && ValueTypeSetListItMove(&rit)) {
2245
- int equal;
2246
- ValueType* le;
2247
- ValueType* re;
2248
- le = ValueTypeSetListItGetRef(&lit);
2249
- re = ValueTypeSetListItGetRef(&rit);
2250
- equal = ValueTypeEqual(*le,*re);
2251
- if(!equal) return 0;
2252
- }
2253
- return 1;
2254
- } else
2255
- return 0;
2256
- }
2257
- AUTOC_STATIC size_t ValueTypeSetListIdentify(ValueTypeSetList* self) {
2258
- ValueTypeSetListNode* node;
2259
- size_t result = 0;
2260
- assert(self);
2261
- for(node = self->head_node; node != NULL; node = node->next_node) {
2262
- result ^= ValueTypeIdentify(node->element);
2263
- result = AUTOC_RCYCLE(result);
2264
- }
2265
- return result;
2266
- }
2267
- AUTOC_STATIC void ValueTypeSetListPurge(ValueTypeSetList* self) {
2268
- ValueTypeSetListDtor(self);
2269
- ValueTypeSetListCtor(self);
2270
- }
2271
- AUTOC_STATIC ValueType ValueTypeSetListPeek(ValueTypeSetList* self) {
2272
- ValueType result;
2273
- assert(self);
2274
- assert(!ValueTypeSetListEmpty(self));
2275
- ValueTypeCopy(result,self->head_node->element);
2276
- return result;
2277
- }
2278
- AUTOC_STATIC ValueType ValueTypeSetListPop(ValueTypeSetList* self) {
2279
- ValueTypeSetListNode* node;
2280
- ValueType result;
2281
- assert(self);
2282
- assert(!ValueTypeSetListEmpty(self));
2283
- node = self->head_node;
2284
- result = node->element;
2285
- self->head_node = self->head_node->next_node;
2286
- --self->node_count;
2287
- free(node);
2288
- return result;
2289
- }
2290
- AUTOC_STATIC void ValueTypeSetListPush(ValueTypeSetList* self, ValueType element) {
2291
- ValueTypeSetListNode* node;
2292
- assert(self);
2293
- node = (ValueTypeSetListNode*)malloc(sizeof(ValueTypeSetListNode));
2294
- assert(node);
2295
- ValueTypeCopy(node->element,element);
2296
- node->next_node = self->head_node;
2297
- self->head_node = node;
2298
- ++self->node_count;
2299
- }
2300
- AUTOC_STATIC int ValueTypeSetListContains(ValueTypeSetList* self, ValueType what) {
2301
- ValueTypeSetListNode* node;
2302
- int found = 0;
2303
- assert(self);
2304
- node = self->head_node;
2305
- while(node) {
2306
- if(ValueTypeEqual(node->element,what)) {
2307
- found = 1;
2308
- break;
2309
- }
2310
- node = node->next_node;
2311
- }
2312
- return found;
2313
- }
2314
- AUTOC_STATIC ValueType ValueTypeSetListFind(ValueTypeSetList* self, ValueType what) {
2315
- ValueTypeSetListNode* node;
2316
- assert(self);
2317
- assert(ValueTypeSetListContains(self, what));
2318
- node = self->head_node;
2319
- while(node) {
2320
- if(ValueTypeEqual(node->element,what)) {
2321
- ValueType result;
2322
- ValueTypeCopy(result,node->element);
2323
- return result;
2324
- }
2325
- node = node->next_node;
2326
- }
2327
- abort();
2328
- }
2329
- AUTOC_STATIC int ValueTypeSetListReplaceEx(ValueTypeSetList* self, ValueType with, int count) {
2330
- ValueTypeSetListNode* node;
2331
- int replaced = 0;
2332
- assert(self);
2333
- if(count == 0) return 0;
2334
- node = self->head_node;
2335
- while(node) {
2336
- if(ValueTypeEqual(node->element,with)) {
2337
- ValueTypeDtor(node->element);
2338
- ValueTypeCopy(node->element,with);
2339
- ++replaced;
2340
- if(count > 0 && replaced >= count) break;
2341
- }
2342
- node = node->next_node;
2343
- }
2344
- return replaced;
2345
- }
2346
- AUTOC_STATIC int ValueTypeSetListRemoveEx(ValueTypeSetList* self, ValueType what, int count) {
2347
- ValueTypeSetListNode *node, *prev_node;
2348
- int removed = 0;
2349
- assert(self);
2350
- if(count == 0) return 0;
2351
- node = self->head_node;
2352
- prev_node = NULL;
2353
- while(node) {
2354
- if(ValueTypeEqual(node->element,what)) {
2355
- ValueTypeSetListNode* this_node;
2356
- if(prev_node) {
2357
- this_node = prev_node->next_node = node->next_node;
2358
- } else {
2359
- this_node = self->head_node = node->next_node;
2360
- }
2361
- ++removed;
2362
- --self->node_count;
2363
- ValueTypeDtor(node->element);
2364
- free(node);
2365
- node = this_node;
2366
- if(count > 0 && removed >= count) break;
2367
- } else {
2368
- prev_node = node;
2369
- node = node->next_node;
2370
- }
2371
- }
2372
- return removed;
2373
- }
2374
- AUTOC_STATIC size_t ValueTypeSetListSize(ValueTypeSetList* self) {
2375
- assert(self);
2376
- return self->node_count;
2377
- }
2378
- AUTOC_STATIC void ValueTypeSetListItCtor(ValueTypeSetListIt* self, ValueTypeSetList* list) {
2379
- assert(self);
2380
- assert(list);
2381
- self->start = 1;
2382
- self->list = list;
2383
- }
2384
- AUTOC_STATIC int ValueTypeSetListItMove(ValueTypeSetListIt* self) {
2385
- assert(self);
2386
- if(self->start) {
2387
- self->this_node = self->list->head_node;
2388
- self->start = 0;
2389
- } else {
2390
- self->this_node = self->this_node ? self->this_node->next_node : NULL;
2391
- }
2392
- return self->this_node != NULL;
2393
- }
2394
- AUTOC_STATIC ValueType ValueTypeSetListItGet(ValueTypeSetListIt* self) {
2395
- ValueType result;
2396
- assert(self);
2397
- assert(self->this_node);
2398
- ValueTypeCopy(result,self->this_node->element);
2399
- return result;
2400
- }
2401
- AUTOC_STATIC ValueType* ValueTypeSetListItGetRef(ValueTypeSetListIt* self) {
2402
- assert(self);
2403
- assert(self->this_node);
2404
- return &self->this_node->element;
2405
- }
2406
-
2407
- ValueType* ValueTypeSetItGetRef(ValueTypeSetIt*);
2408
- static void ValueTypeSetRehash(ValueTypeSet* self) {
2409
- ValueTypeSetList* buckets;
2410
- size_t index, bucket_count, size, fill;
2411
- assert(self);
2412
- assert(self->min_fill > 0);
2413
- assert(self->max_fill > 0);
2414
- assert(self->min_fill < self->max_fill);
2415
- assert(self->min_bucket_count > 0);
2416
- if(self->buckets) {
2417
- if(self->min_size < self->size && self->size < self->max_size) return;
2418
- fill = (size_t)((float)self->size/self->bucket_count*100);
2419
- if(fill > self->max_fill) {
2420
- bucket_count = (size_t)((float)self->bucket_count/100*self->capacity_multiplier);
2421
- } else if(fill < self->min_fill && self->bucket_count > self->min_bucket_count) {
2422
- bucket_count = (size_t)((float)self->bucket_count/self->capacity_multiplier*100);
2423
- if(bucket_count < self->min_bucket_count) bucket_count = self->min_bucket_count;
2424
- } else
2425
- return;
2426
- size = self->size;
2427
- self->min_size = (size_t)((float)self->min_fill/100*size);
2428
- self->max_size = (size_t)((float)self->max_fill/100*size);
2429
- } else {
2430
- bucket_count = self->min_bucket_count;
2431
- size = 0;
2432
- }
2433
- buckets = (ValueTypeSetList*)malloc(bucket_count*sizeof(ValueTypeSetList));
2434
- assert(buckets);
2435
- for(index = 0; index < bucket_count; ++index) {
2436
- ValueTypeSetListCtor(&buckets[index]);
2437
- }
2438
- if(self->buckets) {
2439
- ValueTypeSetIt it;
2440
- ValueTypeSetItCtor(&it, self);
2441
- while(ValueTypeSetItMove(&it)) {
2442
- ValueTypeSetList* bucket;
2443
- ValueType element = ValueTypeSetItGet(&it);
2444
- bucket = &buckets[ValueTypeIdentify(element) % bucket_count];
2445
- ValueTypeSetListPush(bucket, element);
2446
- ValueTypeDtor(element);
2447
- }
2448
- ValueTypeSetDtor(self);
2449
- }
2450
- self->buckets = buckets;
2451
- self->bucket_count = bucket_count;
2452
- self->size = size;
2453
- }
2454
- static int ValueTypeSetContainsAllOf(ValueTypeSet* self, ValueTypeSet* other) {
2455
- ValueTypeSetIt it;
2456
- ValueTypeSetItCtor(&it, self);
2457
- while(ValueTypeSetItMove(&it)) {
2458
- int found = 0;
2459
- if(ValueTypeSetContains(other, *ValueTypeSetItGetRef(&it))) found = 1;
2460
- if(!found) return 0;
2461
- }
2462
- return 1;
2463
- }
2464
- void ValueTypeSetCtor(ValueTypeSet* self) {
2465
- assert(self);
2466
- self->min_bucket_count = 16;
2467
- self->min_fill = 20;
2468
- self->max_fill = 80;
2469
- self->min_size = (size_t)((float)self->min_fill/100*self->min_bucket_count);
2470
- self->max_size = (size_t)((float)self->max_fill/100*self->min_bucket_count);
2471
- self->capacity_multiplier = 200;
2472
- self->buckets = NULL;
2473
- ValueTypeSetRehash(self);
2474
- }
2475
- void ValueTypeSetDtor(ValueTypeSet* self) {
2476
- size_t i;
2477
- assert(self);
2478
- for(i = 0; i < self->bucket_count; ++i) {
2479
- ValueTypeSetListDtor(&self->buckets[i]);
2480
- }
2481
- free(self->buckets);
2482
- }
2483
- void ValueTypeSetCopy(ValueTypeSet* dst,ValueTypeSet* src) {
2484
- ValueTypeSetIt it;
2485
- assert(src);
2486
- assert(dst);
2487
- ValueTypeSetCtor(dst);
2488
- ValueTypeSetItCtor(&it, src);
2489
- while(ValueTypeSetItMove(&it)) ValueTypeSetPut(dst, *ValueTypeSetItGetRef(&it));
2490
- }
2491
- int ValueTypeSetEqual(ValueTypeSet* lt,ValueTypeSet* rt) {
2492
- assert(lt);
2493
- assert(rt);
2494
- return ValueTypeSetSize(lt) == ValueTypeSetSize(rt) && ValueTypeSetContainsAllOf(lt, rt) && ValueTypeSetContainsAllOf(rt, lt);
2495
- }
2496
- size_t ValueTypeSetIdentify(ValueTypeSet* self) {
2497
- ValueTypeSetIt it;
2498
- size_t result = 0;
2499
- assert(self);
2500
- ValueTypeSetItCtor(&it, self);
2501
- while(ValueTypeSetItMove(&it)) {
2502
- ValueType* e = ValueTypeSetItGetRef(&it);
2503
- result ^= ValueTypeIdentify(*e);
2504
- result = AUTOC_RCYCLE(result);
2505
- }
2506
- return result;
2507
- }
2508
- void ValueTypeSetPurge(ValueTypeSet* self) {
2509
- assert(self);
2510
- ValueTypeSetDtor(self);
2511
- self->buckets = NULL;
2512
- ValueTypeSetRehash(self);
2513
- }
2514
- int ValueTypeSetContains(ValueTypeSet* self, ValueType element) {
2515
- assert(self);
2516
- return ValueTypeSetListContains(&self->buckets[ValueTypeIdentify(element) % self->bucket_count], element);
2517
- }
2518
- ValueType ValueTypeSetGet(ValueTypeSet* self, ValueType element) {
2519
- ValueType result;
2520
- assert(self);
2521
- assert(ValueTypeSetContains(self, element));
2522
- result = ValueTypeSetListFind(&self->buckets[ValueTypeIdentify(element) % self->bucket_count], element);
2523
- return result;
2524
- }
2525
- size_t ValueTypeSetSize(ValueTypeSet* self) {
2526
- assert(self);
2527
- return self->size;
2528
- }
2529
- int ValueTypeSetPut(ValueTypeSet* self, ValueType element) {
2530
- ValueTypeSetList* bucket;
2531
- assert(self);
2532
- bucket = &self->buckets[ValueTypeIdentify(element) % self->bucket_count];
2533
- if(!ValueTypeSetListContains(bucket, element)) {
2534
- ValueTypeSetListPush(bucket, element);
2535
- ++self->size;
2536
- ValueTypeSetRehash(self);
2537
- return 1;
2538
- }
2539
- return 0;
2540
- }
2541
- int ValueTypeSetReplace(ValueTypeSet* self, ValueType element) {
2542
- ValueTypeSetList* bucket;
2543
- assert(self);
2544
- bucket = &self->buckets[ValueTypeIdentify(element) % self->bucket_count];
2545
- return ValueTypeSetListReplace(bucket, element);
2546
- }
2547
- int ValueTypeSetRemove(ValueTypeSet* self, ValueType element) {
2548
- ValueTypeSetList* bucket;
2549
- assert(self);
2550
- bucket = &self->buckets[ValueTypeIdentify(element) % self->bucket_count];
2551
- if(ValueTypeSetListRemove(bucket, element)) {
2552
- --self->size;
2553
- ValueTypeSetRehash(self);
2554
- return 1;
2555
- }
2556
- return 0;
2557
- }
2558
- void ValueTypeSetExclude(ValueTypeSet* self, ValueTypeSet* other) {
2559
- ValueTypeSetIt it;
2560
- assert(self);
2561
- assert(other);
2562
- ValueTypeSetItCtor(&it, other);
2563
- while(ValueTypeSetItMove(&it)) ValueTypeSetRemove(self, *ValueTypeSetItGetRef(&it));
2564
- }
2565
- void ValueTypeSetInclude(ValueTypeSet* self, ValueTypeSet* other) {
2566
- ValueTypeSetIt it;
2567
- assert(self);
2568
- assert(other);
2569
- ValueTypeSetItCtor(&it, other);
2570
- while(ValueTypeSetItMove(&it)) ValueTypeSetPut(self, *ValueTypeSetItGetRef(&it));
2571
- }
2572
- void ValueTypeSetRetain(ValueTypeSet* self, ValueTypeSet* other) {
2573
- ValueTypeSetIt it;
2574
- ValueTypeSet set;
2575
- assert(self);
2576
- assert(other);
2577
- ValueTypeSetCtor(&set);
2578
- ValueTypeSetItCtor(&it, self);
2579
- while(ValueTypeSetItMove(&it)) {
2580
- ValueType* e = ValueTypeSetItGetRef(&it);
2581
- if(ValueTypeSetContains(other, *e)) ValueTypeSetPut(&set, *e);
2582
- }
2583
- ValueTypeSetDtor(self);
2584
- *self = set;
2585
- }
2586
- void ValueTypeSetInvert(ValueTypeSet* self, ValueTypeSet* other) {
2587
- ValueTypeSetIt it;
2588
- ValueTypeSet set;
2589
- assert(self);
2590
- assert(other);
2591
- ValueTypeSetCtor(&set);
2592
- ValueTypeSetItCtor(&it, self);
2593
- while(ValueTypeSetItMove(&it)) {
2594
- ValueType* e = ValueTypeSetItGetRef(&it);
2595
- if(!ValueTypeSetContains(other, *e)) ValueTypeSetPut(&set, *e);
2596
- }
2597
- ValueTypeSetItCtor(&it, other);
2598
- while(ValueTypeSetItMove(&it)) {
2599
- ValueType* e = ValueTypeSetItGetRef(&it);
2600
- if(!ValueTypeSetContains(self, *e)) ValueTypeSetPut(&set, *e);
2601
- }
2602
- ValueTypeSetDtor(self);
2603
- *self = set;
2604
- }
2605
- void ValueTypeSetItCtor(ValueTypeSetIt* self, ValueTypeSet* set) {
2606
- assert(self);
2607
- self->set = set;
2608
- self->bucket_index = -1;
2609
- }
2610
- int ValueTypeSetItMove(ValueTypeSetIt* self) {
2611
- assert(self);
2612
- if(self->bucket_index < 0) ValueTypeSetListItCtor(&self->it, &self->set->buckets[self->bucket_index = 0]);
2613
- if(ValueTypeSetListItMove(&self->it)) return 1;
2614
- while(++self->bucket_index < self->set->bucket_count) {
2615
- ValueTypeSetListItCtor(&self->it, &self->set->buckets[self->bucket_index]);
2616
- if(ValueTypeSetListItMove(&self->it)) return 1;
2617
- }
2618
- return 0;
2619
- }
2620
- ValueType ValueTypeSetItGet(ValueTypeSetIt* self) {
2621
- assert(self);
2622
- return ValueTypeSetListItGet(&self->it);
2623
- }
2624
- ValueType* ValueTypeSetItGetRef(ValueTypeSetIt* self) {
2625
- assert(self);
2626
- return ValueTypeSetListItGetRef(&self->it);
2627
- }
2628
-
2629
- #define AUTOC_COUNTER(p) (*(size_t*)((char*)(p) + sizeof(ValueType)))
2630
- ValueType* ValueTypeNew() {
2631
- ValueType* self = (ValueType*)malloc(sizeof(ValueType) + sizeof(size_t));
2632
- assert(self);
2633
- ValueTypeCtor(*self);
2634
- AUTOC_COUNTER(self) = 1;
2635
- return self;
2636
- }
2637
- ValueType* ValueTypeRef(ValueType* self) {
2638
- assert(self);
2639
- ++AUTOC_COUNTER(self);
2640
- return self;
2641
- }
2642
- void ValueTypeFree(ValueType* self) {
2643
- assert(self);
2644
- if(--AUTOC_COUNTER(self) == 0) {
2645
- ValueTypeDtor(*self);
2646
- free(self);
2647
- }
2648
- }
2649
- #undef AUTOC_COUNTER
2650
-
2651
- ValueType* ValueTypeListItGetRef(ValueTypeListIt*);
2652
- void ValueTypeListCtor(ValueTypeList* self) {
2653
- assert(self);
2654
- self->head_node = NULL;
2655
- self->node_count = 0;
2656
- }
2657
- void ValueTypeListDtor(ValueTypeList* self) {
2658
- ValueTypeListNode* node;
2659
- assert(self);
2660
- node = self->head_node;
2661
- while(node) {
2662
- ValueTypeListNode* this_node = node;
2663
- node = node->next_node;
2664
- ValueTypeDtor(this_node->element);
2665
- free(this_node);
2666
- }
2667
- }
2668
- void ValueTypeListCopy(ValueTypeList* dst,ValueTypeList* src) {
2669
- ValueTypeListIt it;
2670
- assert(src);
2671
- assert(dst);
2672
- ValueTypeListCtor(dst);
2673
- ValueTypeListItCtor(&it, src);
2674
- while(ValueTypeListItMove(&it)) {
2675
- ValueTypeListPush(dst, *ValueTypeListItGetRef(&it));
2676
- }
2677
- }
2678
- int ValueTypeListEqual(ValueTypeList* lt,ValueTypeList* rt) {
2679
- if(ValueTypeListSize(lt) == ValueTypeListSize(rt)) {
2680
- ValueTypeListIt lit, rit;
2681
- ValueTypeListItCtor(&lit, lt);
2682
- ValueTypeListItCtor(&rit, rt);
2683
- while(ValueTypeListItMove(&lit) && ValueTypeListItMove(&rit)) {
2684
- int equal;
2685
- ValueType* le;
2686
- ValueType* re;
2687
- le = ValueTypeListItGetRef(&lit);
2688
- re = ValueTypeListItGetRef(&rit);
2689
- equal = ValueTypeEqual(*le,*re);
2690
- if(!equal) return 0;
2691
- }
2692
- return 1;
2693
- } else
2694
- return 0;
2695
- }
2696
- size_t ValueTypeListIdentify(ValueTypeList* self) {
2697
- ValueTypeListNode* node;
2698
- size_t result = 0;
2699
- assert(self);
2700
- for(node = self->head_node; node != NULL; node = node->next_node) {
2701
- result ^= ValueTypeIdentify(node->element);
2702
- result = AUTOC_RCYCLE(result);
2703
- }
2704
- return result;
2705
- }
2706
- void ValueTypeListPurge(ValueTypeList* self) {
2707
- ValueTypeListDtor(self);
2708
- ValueTypeListCtor(self);
2709
- }
2710
- ValueType ValueTypeListPeek(ValueTypeList* self) {
2711
- ValueType result;
2712
- assert(self);
2713
- assert(!ValueTypeListEmpty(self));
2714
- ValueTypeCopy(result,self->head_node->element);
2715
- return result;
2716
- }
2717
- ValueType ValueTypeListPop(ValueTypeList* self) {
2718
- ValueTypeListNode* node;
2719
- ValueType result;
2720
- assert(self);
2721
- assert(!ValueTypeListEmpty(self));
2722
- node = self->head_node;
2723
- result = node->element;
2724
- self->head_node = self->head_node->next_node;
2725
- --self->node_count;
2726
- free(node);
2727
- return result;
2728
- }
2729
- void ValueTypeListPush(ValueTypeList* self, ValueType element) {
2730
- ValueTypeListNode* node;
2731
- assert(self);
2732
- node = (ValueTypeListNode*)malloc(sizeof(ValueTypeListNode));
2733
- assert(node);
2734
- ValueTypeCopy(node->element,element);
2735
- node->next_node = self->head_node;
2736
- self->head_node = node;
2737
- ++self->node_count;
2738
- }
2739
- int ValueTypeListContains(ValueTypeList* self, ValueType what) {
2740
- ValueTypeListNode* node;
2741
- int found = 0;
2742
- assert(self);
2743
- node = self->head_node;
2744
- while(node) {
2745
- if(ValueTypeEqual(node->element,what)) {
2746
- found = 1;
2747
- break;
2748
- }
2749
- node = node->next_node;
2750
- }
2751
- return found;
2752
- }
2753
- ValueType ValueTypeListFind(ValueTypeList* self, ValueType what) {
2754
- ValueTypeListNode* node;
2755
- assert(self);
2756
- assert(ValueTypeListContains(self, what));
2757
- node = self->head_node;
2758
- while(node) {
2759
- if(ValueTypeEqual(node->element,what)) {
2760
- ValueType result;
2761
- ValueTypeCopy(result,node->element);
2762
- return result;
2763
- }
2764
- node = node->next_node;
2765
- }
2766
- abort();
2767
- }
2768
- int ValueTypeListReplaceEx(ValueTypeList* self, ValueType with, int count) {
2769
- ValueTypeListNode* node;
2770
- int replaced = 0;
2771
- assert(self);
2772
- if(count == 0) return 0;
2773
- node = self->head_node;
2774
- while(node) {
2775
- if(ValueTypeEqual(node->element,with)) {
2776
- ValueTypeDtor(node->element);
2777
- ValueTypeCopy(node->element,with);
2778
- ++replaced;
2779
- if(count > 0 && replaced >= count) break;
2780
- }
2781
- node = node->next_node;
2782
- }
2783
- return replaced;
2784
- }
2785
- int ValueTypeListRemoveEx(ValueTypeList* self, ValueType what, int count) {
2786
- ValueTypeListNode *node, *prev_node;
2787
- int removed = 0;
2788
- assert(self);
2789
- if(count == 0) return 0;
2790
- node = self->head_node;
2791
- prev_node = NULL;
2792
- while(node) {
2793
- if(ValueTypeEqual(node->element,what)) {
2794
- ValueTypeListNode* this_node;
2795
- if(prev_node) {
2796
- this_node = prev_node->next_node = node->next_node;
2797
- } else {
2798
- this_node = self->head_node = node->next_node;
2799
- }
2800
- ++removed;
2801
- --self->node_count;
2802
- ValueTypeDtor(node->element);
2803
- free(node);
2804
- node = this_node;
2805
- if(count > 0 && removed >= count) break;
2806
- } else {
2807
- prev_node = node;
2808
- node = node->next_node;
2809
- }
2810
- }
2811
- return removed;
2812
- }
2813
- size_t ValueTypeListSize(ValueTypeList* self) {
2814
- assert(self);
2815
- return self->node_count;
2816
- }
2817
- void ValueTypeListItCtor(ValueTypeListIt* self, ValueTypeList* list) {
2818
- assert(self);
2819
- assert(list);
2820
- self->start = 1;
2821
- self->list = list;
2822
- }
2823
- int ValueTypeListItMove(ValueTypeListIt* self) {
2824
- assert(self);
2825
- if(self->start) {
2826
- self->this_node = self->list->head_node;
2827
- self->start = 0;
2828
- } else {
2829
- self->this_node = self->this_node ? self->this_node->next_node : NULL;
2830
- }
2831
- return self->this_node != NULL;
2832
- }
2833
- ValueType ValueTypeListItGet(ValueTypeListIt* self) {
2834
- ValueType result;
2835
- assert(self);
2836
- assert(self->this_node);
2837
- ValueTypeCopy(result,self->this_node->element);
2838
- return result;
2839
- }
2840
- ValueType* ValueTypeListItGetRef(ValueTypeListIt* self) {
2841
- assert(self);
2842
- assert(self->this_node);
2843
- return &self->this_node->element;
2844
- }
2845
-
2846
- ValueType* ValueTypeQueueItGetRef(ValueTypeQueueIt*);
2847
- void ValueTypeQueueCtor(ValueTypeQueue* self) {
2848
- assert(self);
2849
- self->head_node = self->tail_node = NULL;
2850
- self->node_count = 0;
2851
- }
2852
- void ValueTypeQueueDtor(ValueTypeQueue* self) {
2853
- ValueTypeQueueNode* node;
2854
- assert(self);
2855
- node = self->head_node;
2856
- while(node) {
2857
- ValueTypeQueueNode* this_node = node;
2858
- node = node->next_node;
2859
- ValueTypeDtor(this_node->element);
2860
- free(this_node);
2861
- }
2862
- }
2863
- void ValueTypeQueueCopy(ValueTypeQueue* dst,ValueTypeQueue* src) {
2864
- ValueTypeQueueIt it;
2865
- assert(src);
2866
- assert(dst);
2867
- ValueTypeQueueCtor(dst);
2868
- ValueTypeQueueItCtor(&it, src);
2869
- while(ValueTypeQueueItMove(&it)) {
2870
- ValueType element;
2871
- ValueTypeQueuePushTail(dst, element = ValueTypeQueueItGet(&it));
2872
- ValueTypeDtor(element);
2873
- }
2874
- }
2875
- int ValueTypeQueueEqual(ValueTypeQueue* lt,ValueTypeQueue* rt) {
2876
- if(ValueTypeQueueSize(lt) == ValueTypeQueueSize(rt)) {
2877
- ValueTypeQueueIt lit, rit;
2878
- ValueTypeQueueItCtor(&lit, lt);
2879
- ValueTypeQueueItCtor(&rit, rt);
2880
- while(ValueTypeQueueItMove(&lit) && ValueTypeQueueItMove(&rit)) {
2881
- int equal;
2882
- ValueType *le, *re;
2883
- le = ValueTypeQueueItGetRef(&lit);
2884
- re = ValueTypeQueueItGetRef(&rit);
2885
- equal = ValueTypeEqual(*le,*re);
2886
- if(!equal) return 0;
2887
- }
2888
- return 1;
2889
- } else
2890
- return 0;
2891
- }
2892
- size_t ValueTypeQueueIdentify(ValueTypeQueue* self) {
2893
- ValueTypeQueueNode* node;
2894
- size_t result = 0;
2895
- assert(self);
2896
- for(node = self->head_node; node != NULL; node = node->next_node) {
2897
- result ^= ValueTypeIdentify(node->element);
2898
- result = AUTOC_RCYCLE(result);
2899
- }
2900
- return result;
2901
- }
2902
- void ValueTypeQueuePurge(ValueTypeQueue* self) {
2903
- ValueTypeQueueDtor(self);
2904
- ValueTypeQueueCtor(self);
2905
- }
2906
- ValueType ValueTypeQueuePeekHead(ValueTypeQueue* self) {
2907
- ValueType result;
2908
- assert(self);
2909
- assert(!ValueTypeQueueEmpty(self));
2910
- ValueTypeCopy(result,self->head_node->element);
2911
- return result;
2912
- }
2913
- ValueType ValueTypeQueuePeekTail(ValueTypeQueue* self) {
2914
- ValueType result;
2915
- assert(self);
2916
- assert(!ValueTypeQueueEmpty(self));
2917
- ValueTypeCopy(result,self->tail_node->element);
2918
- return result;
2919
- }
2920
- ValueType ValueTypeQueuePopHead(ValueTypeQueue* self) {
2921
- ValueTypeQueueNode* node;
2922
- ValueType result;
2923
- assert(self);
2924
- assert(!ValueTypeQueueEmpty(self));
2925
- node = self->head_node;
2926
- result = node->element;
2927
- self->head_node = self->head_node->next_node;
2928
- self->head_node->prev_node = NULL;
2929
- --self->node_count;
2930
- free(node);
2931
- return result;
2932
- }
2933
- ValueType ValueTypeQueuePopTail(ValueTypeQueue* self) {
2934
- ValueTypeQueueNode* node;
2935
- ValueType result;
2936
- assert(self);
2937
- assert(!ValueTypeQueueEmpty(self));
2938
- node = self->tail_node;
2939
- result = node->element;
2940
- self->tail_node = self->tail_node->prev_node;
2941
- self->tail_node->next_node = NULL;
2942
- --self->node_count;
2943
- free(node);
2944
- return result;
2945
- }
2946
- void ValueTypeQueuePushTail(ValueTypeQueue* self, ValueType element) {
2947
- ValueTypeQueueNode* node;
2948
- assert(self);
2949
- node = (ValueTypeQueueNode*)malloc(sizeof(ValueTypeQueueNode));
2950
- assert(node);
2951
- ValueTypeCopy(node->element,element);
2952
- if(ValueTypeQueueEmpty(self)) {
2953
- node->prev_node = node->next_node = NULL;
2954
- self->tail_node = self->head_node = node;
2955
- } else {
2956
- node->next_node = NULL;
2957
- node->prev_node = self->tail_node;
2958
- self->tail_node->next_node = node;
2959
- self->tail_node = node;
2960
- }
2961
- ++self->node_count;
2962
- }
2963
- void ValueTypeQueuePushHead(ValueTypeQueue* self, ValueType element) {
2964
- ValueTypeQueueNode* node;
2965
- assert(self);
2966
- node = (ValueTypeQueueNode*)malloc(sizeof(ValueTypeQueueNode));
2967
- assert(node);
2968
- ValueTypeCopy(node->element,element);
2969
- if(ValueTypeQueueEmpty(self)) {
2970
- node->prev_node = node->next_node = NULL;
2971
- self->tail_node = self->head_node = node;
2972
- } else {
2973
- node->prev_node = NULL;
2974
- node->next_node = self->head_node;
2975
- self->head_node->prev_node = node;
2976
- self->head_node = node;
2977
- }
2978
- ++self->node_count;
2979
- }
2980
- int ValueTypeQueueContains(ValueTypeQueue* self, ValueType what) {
2981
- ValueTypeQueueNode* node;
2982
- int found = 0;
2983
- assert(self);
2984
- node = self->head_node;
2985
- while(node) {
2986
- if(ValueTypeEqual(node->element,what)) {
2987
- found = 1;
2988
- break;
2989
- }
2990
- node = node->next_node;
2991
- }
2992
- return found;
2993
- }
2994
- ValueType ValueTypeQueueFind(ValueTypeQueue* self, ValueType what) {
2995
- ValueTypeQueueNode* node;
2996
- assert(self);
2997
- assert(ValueTypeQueueContains(self, what));
2998
- node = self->head_node;
2999
- while(node) {
3000
- if(ValueTypeEqual(node->element,what)) {
3001
- ValueType result;
3002
- ValueTypeCopy(result,node->element);
3003
- return result;
3004
- }
3005
- node = node->next_node;
3006
- }
3007
- abort();
3008
- }
3009
- int ValueTypeQueueReplaceEx(ValueTypeQueue* self, ValueType with, int count) {
3010
- ValueTypeQueueNode* node;
3011
- int replaced = 0;
3012
- assert(self);
3013
- if(count == 0) return 0;
3014
- node = self->head_node;
3015
- while(node) {
3016
- if(ValueTypeEqual(node->element,with)) {
3017
- ValueTypeDtor(node->element);
3018
- ValueTypeCopy(node->element,with);
3019
- ++replaced;
3020
- if(count > 0 && replaced >= count) break;
3021
- }
3022
- node = node->next_node;
3023
- }
3024
- return replaced;
3025
- }
3026
- int ValueTypeQueueRemoveEx(ValueTypeQueue* self, ValueType what, int count) {
3027
- ValueTypeQueueNode* node;
3028
- int removed = 0;
3029
- assert(self);
3030
- if(count == 0) return 0;
3031
- node = self->head_node;
3032
- while(node) {
3033
- if(ValueTypeEqual(node->element,what)) {
3034
- ValueTypeQueueNode* this_node;
3035
- if(node == self->head_node) {
3036
- assert(!node->prev_node);
3037
- this_node = self->head_node = node->next_node;
3038
- if(self->head_node) self->head_node->prev_node = NULL;
3039
- } else if(node == self->tail_node) {
3040
- assert(!node->next_node);
3041
- self->tail_node = node->prev_node;
3042
- this_node = self->tail_node->next_node = NULL;
3043
- } else {
3044
- assert(node->prev_node);
3045
- assert(node->next_node);
3046
- this_node = node->next_node;
3047
- node->next_node->prev_node = node->prev_node;
3048
- node->prev_node->next_node = node->next_node;
3049
- }
3050
- ++removed;
3051
- --self->node_count;
3052
- ValueTypeDtor(node->element);
3053
- free(node);
3054
- node = this_node;
3055
- if(count > 0 && removed >= count) break;
3056
- } else {
3057
- node = node->next_node;
3058
- }
3059
- }
3060
- return removed;
3061
- }
3062
- size_t ValueTypeQueueSize(ValueTypeQueue* self) {
3063
- assert(self);
3064
- return self->node_count;
3065
- }
3066
- void ValueTypeQueueItCtorEx(ValueTypeQueueIt* self, ValueTypeQueue* queue, int forward) {
3067
- assert(self);
3068
- assert(queue);
3069
- self->start = 1;
3070
- self->queue = queue;
3071
- self->forward = forward;
3072
- }
3073
- int ValueTypeQueueItMove(ValueTypeQueueIt* self) {
3074
- assert(self);
3075
- if(self->start) {
3076
- self->this_node = self->forward ? self->queue->head_node : self->queue->tail_node;
3077
- self->start = 0;
3078
- } else {
3079
- self->this_node = self->forward ? self->this_node->next_node : self->this_node->prev_node;
3080
- }
3081
- return self->this_node != NULL;
3082
- }
3083
- ValueType ValueTypeQueueItGet(ValueTypeQueueIt* self) {
3084
- ValueType result;
3085
- assert(self);
3086
- assert(self->this_node);
3087
- ValueTypeCopy(result,self->this_node->element);
3088
- return result;
3089
- }
3090
- ValueType* ValueTypeQueueItGetRef(ValueTypeQueueIt* self) {
3091
- assert(self);
3092
- assert(self->this_node);
3093
- return &self->this_node->element;
3094
- }
3095
-
3096
- static void PVectorValueAllocate(PVectorValue* self, size_t element_count) {
3097
- assert(self);
3098
- assert(element_count > 0);
3099
- self->element_count = element_count;
3100
- self->values = (ValueType**)malloc(element_count*sizeof(ValueType*));
3101
- assert(self->values);
3102
- }
3103
- void PVectorValueCtor(PVectorValue* self,size_t element_count) {
3104
- size_t index;
3105
- assert(self);
3106
- PVectorValueAllocate(self, element_count);
3107
- for(index = 0; index < PVectorValueSize(self); ++index) {
3108
- ((self->values[index]) = ValueTypeNew());
3109
- }
3110
- }
3111
- void PVectorValueDtor(PVectorValue* self) {
3112
- size_t index;
3113
- assert(self);
3114
- for(index = 0; index < PVectorValueSize(self); ++index) {
3115
- ValueTypeFree(self->values[index]);
3116
- }
3117
- free(self->values);
3118
- }
3119
- void PVectorValueCopy(PVectorValue* dst,PVectorValue* src) {
3120
- size_t index, size;
3121
- assert(src);
3122
- assert(dst);
3123
- PVectorValueAllocate(dst, size = PVectorValueSize(src));
3124
- for(index = 0; index < size; ++index) {
3125
- ((dst->values[index]) = ValueTypeRef(src->values[index]));
3126
- }
3127
- }
3128
- int PVectorValueEqual(PVectorValue* lt,PVectorValue* rt) {
3129
- size_t index, size;
3130
- assert(lt);
3131
- assert(rt);
3132
- if(PVectorValueSize(lt) == (size = PVectorValueSize(rt))) {
3133
- for(index = 0; index < size; ++index) {
3134
- if(!ValueTypeEqual(*lt->values[index],*rt->values[index])) return 0;
3135
- }
3136
- return 1;
3137
- } else
3138
- return 0;
3139
- }
3140
- size_t PVectorValueIdentify(PVectorValue* self) {
3141
- size_t index, result = 0;
3142
- assert(self);
3143
- for(index = 0; index < self->element_count; ++index) {
3144
- result ^= ValueTypeIdentify(*self->values[index]);
3145
- result = AUTOC_RCYCLE(result);
3146
- }
3147
- return result;
3148
- }
3149
- void PVectorValueResize(PVectorValue* self, size_t new_element_count) {
3150
- size_t index, element_count, from, to;
3151
- assert(self);
3152
- if((element_count = PVectorValueSize(self)) != new_element_count) {
3153
- ValueType** values = (ValueType**)malloc(new_element_count*sizeof(ValueType*));
3154
- assert(values);
3155
- from = AUTOC_MIN(element_count, new_element_count);
3156
- to = AUTOC_MAX(element_count, new_element_count);
3157
- for(index = 0; index < from; ++index) {
3158
- values[index] = self->values[index];
3159
- }
3160
- if(element_count > new_element_count) {
3161
- for(index = from; index < to; ++index) {
3162
- ValueTypeFree(self->values[index]);
3163
- }
3164
- } else {
3165
- for(index = from; index < to; ++index) {
3166
- ((values[index]) = ValueTypeNew());
3167
- }
3168
- }
3169
- free(self->values);
3170
- self->values = values;
3171
- self->element_count = new_element_count;
3172
- }
3173
- }
3174
-
3175
- static int PVectorValueComparator(void* lp_, void* rp_) {
3176
- ValueType** lp = (ValueType**)lp_;
3177
- ValueType** rp = (ValueType**)rp_;
3178
- if(ValueTypeEqual(**lp,**rp)) {
3179
- return 0;
3180
- } else if(ValueTypeLess(**lp,**rp)) {
3181
- return -1;
3182
- } else {
3183
- return +1;
3184
- }
3185
- }
3186
- void PVectorValueSort(PVectorValue* self) {
3187
- typedef int (*F)(const void*, const void*);
3188
- assert(self);
3189
- qsort(self->values, PVectorValueSize(self), sizeof(ValueType*), (F)PVectorValueComparator);
3190
- }
3191
-
3192
- #define AUTOC_COUNTER(p) (*(size_t*)((char*)(p) + sizeof(PVectorValue)))
3193
- PVectorValue* PVectorValueNew(size_t element_count) {
3194
- PVectorValue* self = (PVectorValue*)malloc(sizeof(PVectorValue) + sizeof(size_t));
3195
- assert(self);
3196
- _PVectorValueCtor(*self,element_count);
3197
- AUTOC_COUNTER(self) = 1;
3198
- return self;
3199
- }
3200
- PVectorValue* PVectorValueRef(PVectorValue* self) {
3201
- assert(self);
3202
- ++AUTOC_COUNTER(self);
3203
- return self;
3204
- }
3205
- void PVectorValueFree(PVectorValue* self) {
3206
- assert(self);
3207
- if(--AUTOC_COUNTER(self) == 0) {
3208
- _PVectorValueDtor(*self);
3209
- free(self);
3210
- }
3211
- }
3212
- #undef AUTOC_COUNTER
3213
-
3214
- PVectorValue** ListPVectorValueItGetRef(ListPVectorValueIt*);
3215
- void ListPVectorValueCtor(ListPVectorValue* self) {
3216
- assert(self);
3217
- self->head_node = NULL;
3218
- self->node_count = 0;
3219
- }
3220
- void ListPVectorValueDtor(ListPVectorValue* self) {
3221
- ListPVectorValueNode* node;
3222
- assert(self);
3223
- node = self->head_node;
3224
- while(node) {
3225
- ListPVectorValueNode* this_node = node;
3226
- node = node->next_node;
3227
- PVectorValueFree(this_node->element);
3228
- free(this_node);
3229
- }
3230
- }
3231
- void ListPVectorValueCopy(ListPVectorValue* dst,ListPVectorValue* src) {
3232
- ListPVectorValueIt it;
3233
- assert(src);
3234
- assert(dst);
3235
- ListPVectorValueCtor(dst);
3236
- ListPVectorValueItCtor(&it, src);
3237
- while(ListPVectorValueItMove(&it)) {
3238
- ListPVectorValuePush(dst, *ListPVectorValueItGetRef(&it));
3239
- }
3240
- }
3241
- int ListPVectorValueEqual(ListPVectorValue* lt,ListPVectorValue* rt) {
3242
- if(ListPVectorValueSize(lt) == ListPVectorValueSize(rt)) {
3243
- ListPVectorValueIt lit, rit;
3244
- ListPVectorValueItCtor(&lit, lt);
3245
- ListPVectorValueItCtor(&rit, rt);
3246
- while(ListPVectorValueItMove(&lit) && ListPVectorValueItMove(&rit)) {
3247
- int equal;
3248
- PVectorValue** le;
3249
- PVectorValue** re;
3250
- le = ListPVectorValueItGetRef(&lit);
3251
- re = ListPVectorValueItGetRef(&rit);
3252
- equal = _PVectorValueEqual(**le,**re);
3253
- if(!equal) return 0;
3254
- }
3255
- return 1;
3256
- } else
3257
- return 0;
3258
- }
3259
- size_t ListPVectorValueIdentify(ListPVectorValue* self) {
3260
- ListPVectorValueNode* node;
3261
- size_t result = 0;
3262
- assert(self);
3263
- for(node = self->head_node; node != NULL; node = node->next_node) {
3264
- result ^= _PVectorValueIdentify(*node->element);
3265
- result = AUTOC_RCYCLE(result);
3266
- }
3267
- return result;
3268
- }
3269
- void ListPVectorValuePurge(ListPVectorValue* self) {
3270
- ListPVectorValueDtor(self);
3271
- ListPVectorValueCtor(self);
3272
- }
3273
- PVectorValue* ListPVectorValuePeek(ListPVectorValue* self) {
3274
- PVectorValue* result;
3275
- assert(self);
3276
- assert(!ListPVectorValueEmpty(self));
3277
- ((result) = PVectorValueRef(self->head_node->element));
3278
- return result;
3279
- }
3280
- PVectorValue* ListPVectorValuePop(ListPVectorValue* self) {
3281
- ListPVectorValueNode* node;
3282
- PVectorValue* result;
3283
- assert(self);
3284
- assert(!ListPVectorValueEmpty(self));
3285
- node = self->head_node;
3286
- result = node->element;
3287
- self->head_node = self->head_node->next_node;
3288
- --self->node_count;
3289
- free(node);
3290
- return result;
3291
- }
3292
- void ListPVectorValuePush(ListPVectorValue* self, PVectorValue* element) {
3293
- ListPVectorValueNode* node;
3294
- assert(self);
3295
- node = (ListPVectorValueNode*)malloc(sizeof(ListPVectorValueNode));
3296
- assert(node);
3297
- ((node->element) = PVectorValueRef(element));
3298
- node->next_node = self->head_node;
3299
- self->head_node = node;
3300
- ++self->node_count;
3301
- }
3302
- int ListPVectorValueContains(ListPVectorValue* self, PVectorValue* what) {
3303
- ListPVectorValueNode* node;
3304
- int found = 0;
3305
- assert(self);
3306
- node = self->head_node;
3307
- while(node) {
3308
- if(_PVectorValueEqual(*node->element,*what)) {
3309
- found = 1;
3310
- break;
3311
- }
3312
- node = node->next_node;
3313
- }
3314
- return found;
3315
- }
3316
- PVectorValue* ListPVectorValueFind(ListPVectorValue* self, PVectorValue* what) {
3317
- ListPVectorValueNode* node;
3318
- assert(self);
3319
- assert(ListPVectorValueContains(self, what));
3320
- node = self->head_node;
3321
- while(node) {
3322
- if(_PVectorValueEqual(*node->element,*what)) {
3323
- PVectorValue* result;
3324
- ((result) = PVectorValueRef(node->element));
3325
- return result;
3326
- }
3327
- node = node->next_node;
3328
- }
3329
- abort();
3330
- }
3331
- int ListPVectorValueReplaceEx(ListPVectorValue* self, PVectorValue* with, int count) {
3332
- ListPVectorValueNode* node;
3333
- int replaced = 0;
3334
- assert(self);
3335
- if(count == 0) return 0;
3336
- node = self->head_node;
3337
- while(node) {
3338
- if(_PVectorValueEqual(*node->element,*with)) {
3339
- PVectorValueFree(node->element);
3340
- ((node->element) = PVectorValueRef(with));
3341
- ++replaced;
3342
- if(count > 0 && replaced >= count) break;
3343
- }
3344
- node = node->next_node;
3345
- }
3346
- return replaced;
3347
- }
3348
- int ListPVectorValueRemoveEx(ListPVectorValue* self, PVectorValue* what, int count) {
3349
- ListPVectorValueNode *node, *prev_node;
3350
- int removed = 0;
3351
- assert(self);
3352
- if(count == 0) return 0;
3353
- node = self->head_node;
3354
- prev_node = NULL;
3355
- while(node) {
3356
- if(_PVectorValueEqual(*node->element,*what)) {
3357
- ListPVectorValueNode* this_node;
3358
- if(prev_node) {
3359
- this_node = prev_node->next_node = node->next_node;
3360
- } else {
3361
- this_node = self->head_node = node->next_node;
3362
- }
3363
- ++removed;
3364
- --self->node_count;
3365
- PVectorValueFree(node->element);
3366
- free(node);
3367
- node = this_node;
3368
- if(count > 0 && removed >= count) break;
3369
- } else {
3370
- prev_node = node;
3371
- node = node->next_node;
3372
- }
3373
- }
3374
- return removed;
3375
- }
3376
- size_t ListPVectorValueSize(ListPVectorValue* self) {
3377
- assert(self);
3378
- return self->node_count;
3379
- }
3380
- void ListPVectorValueItCtor(ListPVectorValueIt* self, ListPVectorValue* list) {
3381
- assert(self);
3382
- assert(list);
3383
- self->start = 1;
3384
- self->list = list;
3385
- }
3386
- int ListPVectorValueItMove(ListPVectorValueIt* self) {
3387
- assert(self);
3388
- if(self->start) {
3389
- self->this_node = self->list->head_node;
3390
- self->start = 0;
3391
- } else {
3392
- self->this_node = self->this_node ? self->this_node->next_node : NULL;
3393
- }
3394
- return self->this_node != NULL;
3395
- }
3396
- PVectorValue* ListPVectorValueItGet(ListPVectorValueIt* self) {
3397
- PVectorValue* result;
3398
- assert(self);
3399
- assert(self->this_node);
3400
- ((result) = PVectorValueRef(self->this_node->element));
3401
- return result;
3402
- }
3403
- PVectorValue** ListPVectorValueItGetRef(ListPVectorValueIt* self) {
3404
- assert(self);
3405
- assert(self->this_node);
3406
- return &self->this_node->element;
3407
- }