autoc 0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4042 @@
1
+
2
+ /* AUTOMATICALLY GENERATED SOURCE FILE. DO NOT MODIFY. */
3
+ #include "test_auto.h"
4
+
5
+ static void IntSetListCtor(IntSetList*);
6
+ static void IntSetListDtor(IntSetList*);
7
+ static void IntSetListPurge(IntSetList*);
8
+ static IntSetList* IntSetListNew(void);
9
+ static void IntSetListDestroy(IntSetList*);
10
+ static IntSetList* IntSetListAssign(IntSetList*);
11
+ static int IntSetListGet(IntSetList*);
12
+ static void IntSetListAdd(IntSetList*, int);
13
+ static void IntSetListChop(IntSetList*);
14
+ static int IntSetListContains(IntSetList*, int);
15
+ static int IntSetListFind(IntSetList*, int);
16
+ static int IntSetListReplace(IntSetList*, int, int);
17
+ static int IntSetListReplaceAll(IntSetList*, int, int);
18
+ static int IntSetListRemove(IntSetList*, int);
19
+ static int IntSetListRemoveAll(IntSetList*, int);
20
+ static size_t IntSetListSize(IntSetList*);
21
+ static int IntSetListEmpty(IntSetList*);
22
+ static void IntSetListItCtor(IntSetListIt*, IntSetList*);
23
+ static int IntSetListItHasNext(IntSetListIt*);
24
+ static int IntSetListItNext(IntSetListIt*);
25
+
26
+ static void IntSetListCtor(IntSetList* self) {
27
+ assert(self);
28
+ self->head_node = NULL;
29
+ self->node_count = 0;
30
+ }
31
+ static void IntSetListDtor(IntSetList* self) {
32
+ IntSetListNode* node;
33
+ assert(self);
34
+ ;
35
+ node = self->head_node;
36
+ while(node) {
37
+ IntSetListNode* this_node = node;
38
+ node = node->next_node;
39
+ free(this_node);
40
+ }
41
+ }
42
+ static IntSetList* IntSetListNew(void) {
43
+ IntSetList* self = (IntSetList*)malloc(sizeof(IntSetList)); assert(self);
44
+ IntSetListCtor(self);
45
+ self->ref_count = 0;
46
+ return self;
47
+ }
48
+ static void IntSetListDestroy(IntSetList* self) {
49
+ assert(self);
50
+ if(!--self->ref_count) {
51
+ IntSetListDtor(self);
52
+ free(self);
53
+ }
54
+ }
55
+ static IntSetList* IntSetListAssign(IntSetList* self) {
56
+ ++self->ref_count;
57
+ return self;
58
+ }
59
+ static void IntSetListPurge(IntSetList* self) {
60
+ IntSetListDtor(self);
61
+ IntSetListCtor(self);
62
+ }
63
+ static int IntSetListGet(IntSetList* self) {
64
+ assert(self);
65
+ assert(!IntSetListEmpty(self));
66
+ return self->head_node->element;
67
+ }
68
+ static void IntSetListChop(IntSetList* self) {
69
+ IntSetListNode* node;
70
+ assert(self);
71
+ assert(!IntSetListEmpty(self));
72
+ node = self->head_node;
73
+ ;
74
+ self->head_node = self->head_node->next_node;
75
+ --self->node_count;
76
+ free(node);
77
+ }
78
+ static void IntSetListAdd(IntSetList* self, int element) {
79
+ IntSetListNode* node;
80
+ assert(self);
81
+ node = (IntSetListNode*)malloc(sizeof(IntSetListNode)); assert(node);
82
+ node->element = (element);
83
+ node->next_node = self->head_node;
84
+ self->head_node = node;
85
+ ++self->node_count;
86
+ }
87
+ static int IntSetListContains(IntSetList* self, int what) {
88
+ IntSetListNode* node;
89
+ assert(self);
90
+ what = (what);
91
+ node = self->head_node;
92
+ while(node) {
93
+ if((node->element==what)) {
94
+ ;
95
+ return 1;
96
+ }
97
+ node = node->next_node;
98
+ }
99
+ ;
100
+ return 0;
101
+ }
102
+ static int IntSetListFind(IntSetList* self, int what) {
103
+ IntSetListNode* node;
104
+ assert(self);
105
+ what = (what);
106
+ assert(IntSetListContains(self, what));
107
+ node = self->head_node;
108
+ while(node) {
109
+ if((node->element==what)) {
110
+ ;
111
+ return node->element;
112
+ }
113
+ node = node->next_node;
114
+ }
115
+ abort();
116
+ }
117
+ static int IntSetListReplace(IntSetList* self, int what, int with) {
118
+ IntSetListNode* node;
119
+ assert(self);
120
+ what = (what);
121
+ with = (with);
122
+ node = self->head_node;
123
+ while(node) {
124
+ if((node->element==what)) {
125
+ ;
126
+ node->element = (with);
127
+ ;
128
+ ;
129
+ return 1;
130
+ }
131
+ node = node->next_node;
132
+ }
133
+ ;
134
+ ;
135
+ return 0;
136
+ }
137
+ static int IntSetListReplaceAll(IntSetList* self, int what, int with) {
138
+ IntSetListNode* node;
139
+ int count = 0;
140
+ assert(self);
141
+ what = (what);
142
+ with = (with);
143
+ node = self->head_node;
144
+ while(node) {
145
+ if((node->element==what)) {
146
+ ;
147
+ node->element = (with);
148
+ ++count;
149
+ }
150
+ node = node->next_node;
151
+ }
152
+ ;
153
+ ;
154
+ return count;
155
+ }
156
+ static int IntSetListRemove(IntSetList* self, int what) {
157
+ IntSetListNode* node;
158
+ IntSetListNode* prev_node;
159
+ int found = 0;
160
+ assert(self);
161
+ what = (what);
162
+ node = self->head_node;
163
+ prev_node = NULL;
164
+ while(node) {
165
+ if((node->element==what)) {
166
+ ;
167
+ if(prev_node) {
168
+ prev_node->next_node = node->next_node ? node->next_node : NULL;
169
+ } else {
170
+ self->head_node = node->next_node ? node->next_node : NULL;
171
+ }
172
+ --self->node_count;
173
+ free(node);
174
+ found = 1;
175
+ break;
176
+ }
177
+ prev_node = node;
178
+ node = node->next_node;
179
+ }
180
+ ;
181
+ return found;
182
+ }
183
+ static int IntSetListRemoveAll(IntSetList* self, int what) {
184
+ IntSetListNode* node;
185
+ IntSetListNode* prev_node;
186
+ int count = 0;
187
+ assert(self);
188
+ what = (what);
189
+ node = self->head_node;
190
+ prev_node = NULL;
191
+ while(node) {
192
+ if((node->element==what)) {
193
+ ;
194
+ if(prev_node) {
195
+ prev_node->next_node = node->next_node ? node->next_node : NULL;
196
+ } else {
197
+ self->head_node = node->next_node ? node->next_node : NULL;
198
+ }
199
+ --self->node_count;
200
+ free(node);
201
+ ++count;
202
+ }
203
+ prev_node = node;
204
+ node = node->next_node;
205
+ }
206
+ ;
207
+ return count;
208
+ }
209
+ static size_t IntSetListSize(IntSetList* self) {
210
+ assert(self);
211
+ return self->node_count;
212
+ }
213
+ static int IntSetListEmpty(IntSetList* self) {
214
+ assert(self);
215
+ return !self->node_count;
216
+ }
217
+ static void IntSetListItCtor(IntSetListIt* self, IntSetList* list) {
218
+ assert(self);
219
+ assert(list);
220
+ self->next_node = list->head_node;
221
+ }
222
+ static int IntSetListItHasNext(IntSetListIt* self) {
223
+ assert(self);
224
+ return self->next_node != NULL;
225
+ }
226
+ static int IntSetListItNext(IntSetListIt* self) {
227
+ IntSetListNode* node;
228
+ assert(self);
229
+ node = self->next_node;
230
+ self->next_node = self->next_node->next_node;
231
+ return node->element;
232
+ }
233
+
234
+ void IntSetCtor(IntSet* self) {
235
+ assert(self);
236
+ self->min_bucket_count = 16;
237
+ self->min_fill = 20;
238
+ self->max_fill = 80;
239
+ self->min_size = (size_t)((float)self->min_fill/100*self->min_bucket_count);
240
+ self->max_size = (size_t)((float)self->max_fill/100*self->min_bucket_count);
241
+ self->capacity_multiplier = 200;
242
+ self->buckets = NULL;
243
+ IntSetRehash(self);
244
+ }
245
+ void IntSetDtor(IntSet* self) {
246
+ size_t i;
247
+ assert(self);
248
+ for(i = 0; i < self->bucket_count; ++i) {
249
+ IntSetListDtor(&self->buckets[i]);
250
+ }
251
+ free(self->buckets);
252
+ }
253
+ IntSet* IntSetNew(void) {
254
+ IntSet* self = (IntSet*)malloc(sizeof(IntSet)); assert(self);
255
+ IntSetCtor(self);
256
+ self->ref_count = 0;
257
+ return self;
258
+ }
259
+ void IntSetDestroy(IntSet* self) {
260
+ assert(self);
261
+ if(!--self->ref_count) {
262
+ IntSetDtor(self);
263
+ free(self);
264
+ }
265
+ }
266
+ IntSet* IntSetAssign(IntSet* self) {
267
+ ++self->ref_count;
268
+ return self;
269
+ }
270
+ void IntSetPurge(IntSet* self) {
271
+ assert(self);
272
+ IntSetDtor(self);
273
+ self->buckets = NULL;
274
+ IntSetRehash(self);
275
+ }
276
+ void IntSetRehash(IntSet* self) {
277
+ IntSetList* buckets;
278
+ size_t i, bucket_count, size, fill;
279
+ assert(self);
280
+ assert(self->min_fill > 0);
281
+ assert(self->max_fill > 0);
282
+ assert(self->min_fill < self->max_fill);
283
+ assert(self->min_bucket_count > 0);
284
+ if(self->buckets) {
285
+ if(self->min_size < self->size && self->size < self->max_size) return;
286
+ fill = (size_t)((float)self->size/self->bucket_count*100);
287
+ if(fill > self->max_fill) {
288
+ bucket_count = (size_t)((float)self->bucket_count/100*self->capacity_multiplier);
289
+ } else
290
+ if(fill < self->min_fill && self->bucket_count > self->min_bucket_count) {
291
+ bucket_count = (size_t)((float)self->bucket_count/self->capacity_multiplier*100);
292
+ if(bucket_count < self->min_bucket_count) bucket_count = self->min_bucket_count;
293
+ } else
294
+ return;
295
+ size = self->size;
296
+ self->min_size = (size_t)((float)self->min_fill/100*size);
297
+ self->max_size = (size_t)((float)self->max_fill/100*size);
298
+ } else {
299
+ bucket_count = self->min_bucket_count;
300
+ size = 0;
301
+ }
302
+ buckets = (IntSetList*)malloc(bucket_count*sizeof(IntSetList)); assert(buckets);
303
+ for(i = 0; i < bucket_count; ++i) {
304
+ IntSetListCtor(&buckets[i]);
305
+ }
306
+ if(self->buckets) {
307
+ IntSetIt it;
308
+ IntSetItCtor(&it, self);
309
+ while(IntSetItHasNext(&it)) {
310
+ IntSetList* bucket;
311
+ int element = IntSetItNext(&it);
312
+ bucket = &buckets[((size_t)(element)) % bucket_count];
313
+ IntSetListAdd(bucket, element);
314
+ }
315
+ IntSetDtor(self);
316
+ }
317
+ self->buckets = buckets;
318
+ self->bucket_count = bucket_count;
319
+ self->size = size;
320
+ }
321
+ int IntSetContains(IntSet* self, int element) {
322
+ int result;
323
+ assert(self);
324
+ element = (element);
325
+ result = IntSetListContains(&self->buckets[((size_t)(element)) % self->bucket_count], element);
326
+ ;
327
+ return result;
328
+ }
329
+ int IntSetGet(IntSet* self, int element) {
330
+ int result;
331
+ assert(self);
332
+ element = (element);
333
+ assert(IntSetContains(self, element));
334
+ result = IntSetListFind(&self->buckets[((size_t)(element)) % self->bucket_count], element);
335
+ ;
336
+ return result;
337
+ }
338
+ size_t IntSetSize(IntSet* self) {
339
+ assert(self);
340
+ return self->size;
341
+ }
342
+ int IntSetEmpty(IntSet* self) {
343
+ assert(self);
344
+ return !self->size;
345
+ }
346
+ int IntSetPut(IntSet* self, int element) {
347
+ int contained = 1;
348
+ IntSetList* bucket;
349
+ assert(self);
350
+ element = (element);
351
+ bucket = &self->buckets[((size_t)(element)) % self->bucket_count];
352
+ if(!IntSetListContains(bucket, element)) {
353
+ IntSetListAdd(bucket, element);
354
+ ++self->size;
355
+ contained = 0;
356
+ IntSetRehash(self);
357
+ }
358
+ ;
359
+ return contained;
360
+ }
361
+ void IntSetReplace(IntSet* self, int element) {
362
+ IntSetList* bucket;
363
+ assert(self);
364
+ element = (element);
365
+ bucket = &self->buckets[((size_t)(element)) % self->bucket_count];
366
+ if(!IntSetListReplace(bucket, element, element)) {
367
+ IntSetListAdd(bucket, element);
368
+ ++self->size;
369
+ IntSetRehash(self);
370
+ }
371
+ ;
372
+ }
373
+ int IntSetRemove(IntSet* self, int what) {
374
+ int removed = 0;
375
+ IntSetList* bucket;
376
+ assert(self);
377
+ what = (what);
378
+ bucket = &self->buckets[((size_t)(what)) % self->bucket_count];
379
+ if(IntSetListRemove(bucket, what)) {
380
+ --self->size;
381
+ removed = 1;
382
+ IntSetRehash(self);
383
+ }
384
+ ;
385
+ return removed;
386
+ }
387
+ void IntSetNot(IntSet* self, IntSet* other) {
388
+ IntSetIt it;
389
+ assert(self);
390
+ assert(other);
391
+ IntSetItCtor(&it, other);
392
+ while(IntSetItHasNext(&it)) {
393
+ IntSetRemove(self, IntSetItNext(&it));
394
+ }
395
+ IntSetRehash(self);
396
+ }
397
+ void IntSetOr(IntSet* self, IntSet* other) {
398
+ IntSetIt it;
399
+ assert(self);
400
+ assert(other);
401
+ IntSetItCtor(&it, other);
402
+ while(IntSetItHasNext(&it)) {
403
+ IntSetPut(self, IntSetItNext(&it));
404
+ }
405
+ IntSetRehash(self);
406
+ }
407
+ void IntSetAnd(IntSet* self, IntSet* other) {
408
+ IntSetIt it;
409
+ IntSet set;
410
+ assert(self);
411
+ assert(other);
412
+ IntSetCtor(&set);
413
+ IntSetItCtor(&it, self);
414
+ while(IntSetItHasNext(&it)) {
415
+ int element = IntSetItNext(&it);
416
+ if(IntSetContains(other, element)) IntSetPut(&set, element);
417
+ }
418
+ IntSetItCtor(&it, other);
419
+ while(IntSetItHasNext(&it)) {
420
+ int element = IntSetItNext(&it);
421
+ if(IntSetContains(self, element)) IntSetPut(&set, element);
422
+ }
423
+ IntSetDtor(self);
424
+ self->buckets = set.buckets;
425
+ self->size = set.size;
426
+ IntSetRehash(self);
427
+ /*IntSetDtor(&set);*/
428
+ }
429
+ void IntSetXor(IntSet* self, IntSet* other) {
430
+ IntSetIt it;
431
+ IntSet set;
432
+ assert(self);
433
+ assert(other);
434
+ IntSetCtor(&set);
435
+ IntSetItCtor(&it, self);
436
+ while(IntSetItHasNext(&it)) {
437
+ int element = IntSetItNext(&it);
438
+ if(!IntSetContains(other, element)) IntSetPut(&set, element);
439
+ }
440
+ IntSetItCtor(&it, other);
441
+ while(IntSetItHasNext(&it)) {
442
+ int element = IntSetItNext(&it);
443
+ if(!IntSetContains(self, element)) IntSetPut(&set, element);
444
+ }
445
+ IntSetDtor(self);
446
+ self->buckets = set.buckets;
447
+ self->size = set.size;
448
+ IntSetRehash(self);
449
+ /*IntSetDtor(&set);*/
450
+ }
451
+ void IntSetItCtor(IntSetIt* self, IntSet* set) {
452
+ assert(self);
453
+ self->set = set;
454
+ self->bucket_index = 0;
455
+ IntSetListItCtor(&self->it, &set->buckets[0]);
456
+ }
457
+ int IntSetItHasNext(IntSetIt* self) {
458
+ assert(self);
459
+ if(IntSetListItHasNext(&self->it)) {
460
+ return 1;
461
+ } else {
462
+ size_t i; for(i = self->bucket_index+1; i < self->set->bucket_count; ++i) {
463
+ if(!IntSetListEmpty(&self->set->buckets[i])) {
464
+ return 1;
465
+ }
466
+ }
467
+ return 0;
468
+ }
469
+ }
470
+ int IntSetItNext(IntSetIt* self) {
471
+ assert(self);
472
+ assert(IntSetItHasNext(self));
473
+ if(IntSetListItHasNext(&self->it)) {
474
+ return IntSetListItNext(&self->it);
475
+ } else {
476
+ size_t i; for(i = self->bucket_index+1; i < self->set->bucket_count; ++i) {
477
+ if(!IntSetListEmpty(&self->set->buckets[i])) {
478
+ IntSetListItCtor(&self->it, &self->set->buckets[i]);
479
+ self->bucket_index = i;
480
+ return IntSetListItNext(&self->it);
481
+ }
482
+ }
483
+ abort();
484
+ }
485
+ }
486
+
487
+ static void PCharSetListCtor(PCharSetList*);
488
+ static void PCharSetListDtor(PCharSetList*);
489
+ static void PCharSetListPurge(PCharSetList*);
490
+ static PCharSetList* PCharSetListNew(void);
491
+ static void PCharSetListDestroy(PCharSetList*);
492
+ static PCharSetList* PCharSetListAssign(PCharSetList*);
493
+ static const char* PCharSetListGet(PCharSetList*);
494
+ static void PCharSetListAdd(PCharSetList*, const char*);
495
+ static void PCharSetListChop(PCharSetList*);
496
+ static int PCharSetListContains(PCharSetList*, const char*);
497
+ static const char* PCharSetListFind(PCharSetList*, const char*);
498
+ static int PCharSetListReplace(PCharSetList*, const char*, const char*);
499
+ static int PCharSetListReplaceAll(PCharSetList*, const char*, const char*);
500
+ static int PCharSetListRemove(PCharSetList*, const char*);
501
+ static int PCharSetListRemoveAll(PCharSetList*, const char*);
502
+ static size_t PCharSetListSize(PCharSetList*);
503
+ static int PCharSetListEmpty(PCharSetList*);
504
+ static void PCharSetListItCtor(PCharSetListIt*, PCharSetList*);
505
+ static int PCharSetListItHasNext(PCharSetListIt*);
506
+ static const char* PCharSetListItNext(PCharSetListIt*);
507
+
508
+ static void PCharSetListCtor(PCharSetList* self) {
509
+ assert(self);
510
+ self->head_node = NULL;
511
+ self->node_count = 0;
512
+ }
513
+ static void PCharSetListDtor(PCharSetList* self) {
514
+ PCharSetListNode* node;
515
+ assert(self);
516
+ ;
517
+ node = self->head_node;
518
+ while(node) {
519
+ PCharSetListNode* this_node = node;
520
+ node = node->next_node;
521
+ free(this_node);
522
+ }
523
+ }
524
+ static PCharSetList* PCharSetListNew(void) {
525
+ PCharSetList* self = (PCharSetList*)malloc(sizeof(PCharSetList)); assert(self);
526
+ PCharSetListCtor(self);
527
+ self->ref_count = 0;
528
+ return self;
529
+ }
530
+ static void PCharSetListDestroy(PCharSetList* self) {
531
+ assert(self);
532
+ if(!--self->ref_count) {
533
+ PCharSetListDtor(self);
534
+ free(self);
535
+ }
536
+ }
537
+ static PCharSetList* PCharSetListAssign(PCharSetList* self) {
538
+ ++self->ref_count;
539
+ return self;
540
+ }
541
+ static void PCharSetListPurge(PCharSetList* self) {
542
+ PCharSetListDtor(self);
543
+ PCharSetListCtor(self);
544
+ }
545
+ static const char* PCharSetListGet(PCharSetList* self) {
546
+ assert(self);
547
+ assert(!PCharSetListEmpty(self));
548
+ return self->head_node->element;
549
+ }
550
+ static void PCharSetListChop(PCharSetList* self) {
551
+ PCharSetListNode* node;
552
+ assert(self);
553
+ assert(!PCharSetListEmpty(self));
554
+ node = self->head_node;
555
+ ;
556
+ self->head_node = self->head_node->next_node;
557
+ --self->node_count;
558
+ free(node);
559
+ }
560
+ static void PCharSetListAdd(PCharSetList* self, const char* element) {
561
+ PCharSetListNode* node;
562
+ assert(self);
563
+ node = (PCharSetListNode*)malloc(sizeof(PCharSetListNode)); assert(node);
564
+ node->element = (element);
565
+ node->next_node = self->head_node;
566
+ self->head_node = node;
567
+ ++self->node_count;
568
+ }
569
+ static int PCharSetListContains(PCharSetList* self, const char* what) {
570
+ PCharSetListNode* node;
571
+ assert(self);
572
+ what = (what);
573
+ node = self->head_node;
574
+ while(node) {
575
+ if(PCharEqual(node->element,what)) {
576
+ ;
577
+ return 1;
578
+ }
579
+ node = node->next_node;
580
+ }
581
+ ;
582
+ return 0;
583
+ }
584
+ static const char* PCharSetListFind(PCharSetList* self, const char* what) {
585
+ PCharSetListNode* node;
586
+ assert(self);
587
+ what = (what);
588
+ assert(PCharSetListContains(self, what));
589
+ node = self->head_node;
590
+ while(node) {
591
+ if(PCharEqual(node->element,what)) {
592
+ ;
593
+ return node->element;
594
+ }
595
+ node = node->next_node;
596
+ }
597
+ abort();
598
+ }
599
+ static int PCharSetListReplace(PCharSetList* self, const char* what, const char* with) {
600
+ PCharSetListNode* node;
601
+ assert(self);
602
+ what = (what);
603
+ with = (with);
604
+ node = self->head_node;
605
+ while(node) {
606
+ if(PCharEqual(node->element,what)) {
607
+ ;
608
+ node->element = (with);
609
+ ;
610
+ ;
611
+ return 1;
612
+ }
613
+ node = node->next_node;
614
+ }
615
+ ;
616
+ ;
617
+ return 0;
618
+ }
619
+ static int PCharSetListReplaceAll(PCharSetList* self, const char* what, const char* with) {
620
+ PCharSetListNode* node;
621
+ int count = 0;
622
+ assert(self);
623
+ what = (what);
624
+ with = (with);
625
+ node = self->head_node;
626
+ while(node) {
627
+ if(PCharEqual(node->element,what)) {
628
+ ;
629
+ node->element = (with);
630
+ ++count;
631
+ }
632
+ node = node->next_node;
633
+ }
634
+ ;
635
+ ;
636
+ return count;
637
+ }
638
+ static int PCharSetListRemove(PCharSetList* self, const char* what) {
639
+ PCharSetListNode* node;
640
+ PCharSetListNode* prev_node;
641
+ int found = 0;
642
+ assert(self);
643
+ what = (what);
644
+ node = self->head_node;
645
+ prev_node = NULL;
646
+ while(node) {
647
+ if(PCharEqual(node->element,what)) {
648
+ ;
649
+ if(prev_node) {
650
+ prev_node->next_node = node->next_node ? node->next_node : NULL;
651
+ } else {
652
+ self->head_node = node->next_node ? node->next_node : NULL;
653
+ }
654
+ --self->node_count;
655
+ free(node);
656
+ found = 1;
657
+ break;
658
+ }
659
+ prev_node = node;
660
+ node = node->next_node;
661
+ }
662
+ ;
663
+ return found;
664
+ }
665
+ static int PCharSetListRemoveAll(PCharSetList* self, const char* what) {
666
+ PCharSetListNode* node;
667
+ PCharSetListNode* prev_node;
668
+ int count = 0;
669
+ assert(self);
670
+ what = (what);
671
+ node = self->head_node;
672
+ prev_node = NULL;
673
+ while(node) {
674
+ if(PCharEqual(node->element,what)) {
675
+ ;
676
+ if(prev_node) {
677
+ prev_node->next_node = node->next_node ? node->next_node : NULL;
678
+ } else {
679
+ self->head_node = node->next_node ? node->next_node : NULL;
680
+ }
681
+ --self->node_count;
682
+ free(node);
683
+ ++count;
684
+ }
685
+ prev_node = node;
686
+ node = node->next_node;
687
+ }
688
+ ;
689
+ return count;
690
+ }
691
+ static size_t PCharSetListSize(PCharSetList* self) {
692
+ assert(self);
693
+ return self->node_count;
694
+ }
695
+ static int PCharSetListEmpty(PCharSetList* self) {
696
+ assert(self);
697
+ return !self->node_count;
698
+ }
699
+ static void PCharSetListItCtor(PCharSetListIt* self, PCharSetList* list) {
700
+ assert(self);
701
+ assert(list);
702
+ self->next_node = list->head_node;
703
+ }
704
+ static int PCharSetListItHasNext(PCharSetListIt* self) {
705
+ assert(self);
706
+ return self->next_node != NULL;
707
+ }
708
+ static const char* PCharSetListItNext(PCharSetListIt* self) {
709
+ PCharSetListNode* node;
710
+ assert(self);
711
+ node = self->next_node;
712
+ self->next_node = self->next_node->next_node;
713
+ return node->element;
714
+ }
715
+
716
+ void PCharSetCtor(PCharSet* self) {
717
+ assert(self);
718
+ self->min_bucket_count = 16;
719
+ self->min_fill = 20;
720
+ self->max_fill = 80;
721
+ self->min_size = (size_t)((float)self->min_fill/100*self->min_bucket_count);
722
+ self->max_size = (size_t)((float)self->max_fill/100*self->min_bucket_count);
723
+ self->capacity_multiplier = 200;
724
+ self->buckets = NULL;
725
+ PCharSetRehash(self);
726
+ }
727
+ void PCharSetDtor(PCharSet* self) {
728
+ size_t i;
729
+ assert(self);
730
+ for(i = 0; i < self->bucket_count; ++i) {
731
+ PCharSetListDtor(&self->buckets[i]);
732
+ }
733
+ free(self->buckets);
734
+ }
735
+ PCharSet* PCharSetNew(void) {
736
+ PCharSet* self = (PCharSet*)malloc(sizeof(PCharSet)); assert(self);
737
+ PCharSetCtor(self);
738
+ self->ref_count = 0;
739
+ return self;
740
+ }
741
+ void PCharSetDestroy(PCharSet* self) {
742
+ assert(self);
743
+ if(!--self->ref_count) {
744
+ PCharSetDtor(self);
745
+ free(self);
746
+ }
747
+ }
748
+ PCharSet* PCharSetAssign(PCharSet* self) {
749
+ ++self->ref_count;
750
+ return self;
751
+ }
752
+ void PCharSetPurge(PCharSet* self) {
753
+ assert(self);
754
+ PCharSetDtor(self);
755
+ self->buckets = NULL;
756
+ PCharSetRehash(self);
757
+ }
758
+ void PCharSetRehash(PCharSet* self) {
759
+ PCharSetList* buckets;
760
+ size_t i, bucket_count, size, fill;
761
+ assert(self);
762
+ assert(self->min_fill > 0);
763
+ assert(self->max_fill > 0);
764
+ assert(self->min_fill < self->max_fill);
765
+ assert(self->min_bucket_count > 0);
766
+ if(self->buckets) {
767
+ if(self->min_size < self->size && self->size < self->max_size) return;
768
+ fill = (size_t)((float)self->size/self->bucket_count*100);
769
+ if(fill > self->max_fill) {
770
+ bucket_count = (size_t)((float)self->bucket_count/100*self->capacity_multiplier);
771
+ } else
772
+ if(fill < self->min_fill && self->bucket_count > self->min_bucket_count) {
773
+ bucket_count = (size_t)((float)self->bucket_count/self->capacity_multiplier*100);
774
+ if(bucket_count < self->min_bucket_count) bucket_count = self->min_bucket_count;
775
+ } else
776
+ return;
777
+ size = self->size;
778
+ self->min_size = (size_t)((float)self->min_fill/100*size);
779
+ self->max_size = (size_t)((float)self->max_fill/100*size);
780
+ } else {
781
+ bucket_count = self->min_bucket_count;
782
+ size = 0;
783
+ }
784
+ buckets = (PCharSetList*)malloc(bucket_count*sizeof(PCharSetList)); assert(buckets);
785
+ for(i = 0; i < bucket_count; ++i) {
786
+ PCharSetListCtor(&buckets[i]);
787
+ }
788
+ if(self->buckets) {
789
+ PCharSetIt it;
790
+ PCharSetItCtor(&it, self);
791
+ while(PCharSetItHasNext(&it)) {
792
+ PCharSetList* bucket;
793
+ const char* element = PCharSetItNext(&it);
794
+ bucket = &buckets[PCharHash(element) % bucket_count];
795
+ PCharSetListAdd(bucket, element);
796
+ }
797
+ PCharSetDtor(self);
798
+ }
799
+ self->buckets = buckets;
800
+ self->bucket_count = bucket_count;
801
+ self->size = size;
802
+ }
803
+ int PCharSetContains(PCharSet* self, const char* element) {
804
+ int result;
805
+ assert(self);
806
+ element = (element);
807
+ result = PCharSetListContains(&self->buckets[PCharHash(element) % self->bucket_count], element);
808
+ ;
809
+ return result;
810
+ }
811
+ const char* PCharSetGet(PCharSet* self, const char* element) {
812
+ const char* result;
813
+ assert(self);
814
+ element = (element);
815
+ assert(PCharSetContains(self, element));
816
+ result = PCharSetListFind(&self->buckets[PCharHash(element) % self->bucket_count], element);
817
+ ;
818
+ return result;
819
+ }
820
+ size_t PCharSetSize(PCharSet* self) {
821
+ assert(self);
822
+ return self->size;
823
+ }
824
+ int PCharSetEmpty(PCharSet* self) {
825
+ assert(self);
826
+ return !self->size;
827
+ }
828
+ int PCharSetPut(PCharSet* self, const char* element) {
829
+ int contained = 1;
830
+ PCharSetList* bucket;
831
+ assert(self);
832
+ element = (element);
833
+ bucket = &self->buckets[PCharHash(element) % self->bucket_count];
834
+ if(!PCharSetListContains(bucket, element)) {
835
+ PCharSetListAdd(bucket, element);
836
+ ++self->size;
837
+ contained = 0;
838
+ PCharSetRehash(self);
839
+ }
840
+ ;
841
+ return contained;
842
+ }
843
+ void PCharSetReplace(PCharSet* self, const char* element) {
844
+ PCharSetList* bucket;
845
+ assert(self);
846
+ element = (element);
847
+ bucket = &self->buckets[PCharHash(element) % self->bucket_count];
848
+ if(!PCharSetListReplace(bucket, element, element)) {
849
+ PCharSetListAdd(bucket, element);
850
+ ++self->size;
851
+ PCharSetRehash(self);
852
+ }
853
+ ;
854
+ }
855
+ int PCharSetRemove(PCharSet* self, const char* what) {
856
+ int removed = 0;
857
+ PCharSetList* bucket;
858
+ assert(self);
859
+ what = (what);
860
+ bucket = &self->buckets[PCharHash(what) % self->bucket_count];
861
+ if(PCharSetListRemove(bucket, what)) {
862
+ --self->size;
863
+ removed = 1;
864
+ PCharSetRehash(self);
865
+ }
866
+ ;
867
+ return removed;
868
+ }
869
+ void PCharSetNot(PCharSet* self, PCharSet* other) {
870
+ PCharSetIt it;
871
+ assert(self);
872
+ assert(other);
873
+ PCharSetItCtor(&it, other);
874
+ while(PCharSetItHasNext(&it)) {
875
+ PCharSetRemove(self, PCharSetItNext(&it));
876
+ }
877
+ PCharSetRehash(self);
878
+ }
879
+ void PCharSetOr(PCharSet* self, PCharSet* other) {
880
+ PCharSetIt it;
881
+ assert(self);
882
+ assert(other);
883
+ PCharSetItCtor(&it, other);
884
+ while(PCharSetItHasNext(&it)) {
885
+ PCharSetPut(self, PCharSetItNext(&it));
886
+ }
887
+ PCharSetRehash(self);
888
+ }
889
+ void PCharSetAnd(PCharSet* self, PCharSet* other) {
890
+ PCharSetIt it;
891
+ PCharSet set;
892
+ assert(self);
893
+ assert(other);
894
+ PCharSetCtor(&set);
895
+ PCharSetItCtor(&it, self);
896
+ while(PCharSetItHasNext(&it)) {
897
+ const char* element = PCharSetItNext(&it);
898
+ if(PCharSetContains(other, element)) PCharSetPut(&set, element);
899
+ }
900
+ PCharSetItCtor(&it, other);
901
+ while(PCharSetItHasNext(&it)) {
902
+ const char* element = PCharSetItNext(&it);
903
+ if(PCharSetContains(self, element)) PCharSetPut(&set, element);
904
+ }
905
+ PCharSetDtor(self);
906
+ self->buckets = set.buckets;
907
+ self->size = set.size;
908
+ PCharSetRehash(self);
909
+ /*PCharSetDtor(&set);*/
910
+ }
911
+ void PCharSetXor(PCharSet* self, PCharSet* other) {
912
+ PCharSetIt it;
913
+ PCharSet set;
914
+ assert(self);
915
+ assert(other);
916
+ PCharSetCtor(&set);
917
+ PCharSetItCtor(&it, self);
918
+ while(PCharSetItHasNext(&it)) {
919
+ const char* element = PCharSetItNext(&it);
920
+ if(!PCharSetContains(other, element)) PCharSetPut(&set, element);
921
+ }
922
+ PCharSetItCtor(&it, other);
923
+ while(PCharSetItHasNext(&it)) {
924
+ const char* element = PCharSetItNext(&it);
925
+ if(!PCharSetContains(self, element)) PCharSetPut(&set, element);
926
+ }
927
+ PCharSetDtor(self);
928
+ self->buckets = set.buckets;
929
+ self->size = set.size;
930
+ PCharSetRehash(self);
931
+ /*PCharSetDtor(&set);*/
932
+ }
933
+ void PCharSetItCtor(PCharSetIt* self, PCharSet* set) {
934
+ assert(self);
935
+ self->set = set;
936
+ self->bucket_index = 0;
937
+ PCharSetListItCtor(&self->it, &set->buckets[0]);
938
+ }
939
+ int PCharSetItHasNext(PCharSetIt* self) {
940
+ assert(self);
941
+ if(PCharSetListItHasNext(&self->it)) {
942
+ return 1;
943
+ } else {
944
+ size_t i; for(i = self->bucket_index+1; i < self->set->bucket_count; ++i) {
945
+ if(!PCharSetListEmpty(&self->set->buckets[i])) {
946
+ return 1;
947
+ }
948
+ }
949
+ return 0;
950
+ }
951
+ }
952
+ const char* PCharSetItNext(PCharSetIt* self) {
953
+ assert(self);
954
+ assert(PCharSetItHasNext(self));
955
+ if(PCharSetListItHasNext(&self->it)) {
956
+ return PCharSetListItNext(&self->it);
957
+ } else {
958
+ size_t i; for(i = self->bucket_index+1; i < self->set->bucket_count; ++i) {
959
+ if(!PCharSetListEmpty(&self->set->buckets[i])) {
960
+ PCharSetListItCtor(&self->it, &self->set->buckets[i]);
961
+ self->bucket_index = i;
962
+ return PCharSetListItNext(&self->it);
963
+ }
964
+ }
965
+ abort();
966
+ }
967
+ }
968
+
969
+ static Box2BoxMapEntry Box2BoxMapEntryKeyOnly(Box* key) {
970
+ Box2BoxMapEntry entry;
971
+ entry.key = key;
972
+ entry.valid_value = 0;
973
+ return entry;
974
+ }
975
+ static Box2BoxMapEntry Box2BoxMapEntryKeyValue(Box* key, Box* value) {
976
+ Box2BoxMapEntry entry;
977
+ entry.key = key;
978
+ entry.value = value;
979
+ entry.valid_value = 1;
980
+ return entry;
981
+ }
982
+ static size_t Box2BoxMapEntryHash(Box2BoxMapEntry entry) {
983
+ return BoxHash(entry.key);
984
+ }
985
+ static int Box2BoxMapEntryEqual(Box2BoxMapEntry lt, Box2BoxMapEntry rt) {
986
+ return BoxEqual(lt.key,rt.key);
987
+ }
988
+ static Box2BoxMapEntry Box2BoxMapEntryAssign(Box2BoxMapEntry entry) {
989
+ entry.key = BoxAssign(entry.key);
990
+ if(entry.valid_value) entry.value = BoxAssign(entry.value);
991
+ return entry;
992
+ }
993
+ static void Box2BoxMapEntryDtor(Box2BoxMapEntry entry) {
994
+ BoxDestroy(entry.key);
995
+ if(entry.valid_value) BoxDestroy(entry.value);
996
+ }
997
+
998
+ static void Box2BoxMapSetCtor(Box2BoxMapSet*);
999
+ static void Box2BoxMapSetDtor(Box2BoxMapSet*);
1000
+ static Box2BoxMapSet* Box2BoxMapSetNew(void);
1001
+ static void Box2BoxMapSetDestroy(Box2BoxMapSet*);
1002
+ static Box2BoxMapSet* Box2BoxMapSetAssign(Box2BoxMapSet*);
1003
+ static void Box2BoxMapSetPurge(Box2BoxMapSet*);
1004
+ static void Box2BoxMapSetRehash(Box2BoxMapSet*);
1005
+ static int Box2BoxMapSetContains(Box2BoxMapSet*, Box2BoxMapEntry);
1006
+ static Box2BoxMapEntry Box2BoxMapSetGet(Box2BoxMapSet*, Box2BoxMapEntry);
1007
+ static size_t Box2BoxMapSetSize(Box2BoxMapSet*);
1008
+ static int Box2BoxMapSetEmpty(Box2BoxMapSet*);
1009
+ static int Box2BoxMapSetPut(Box2BoxMapSet*, Box2BoxMapEntry);
1010
+ static void Box2BoxMapSetReplace(Box2BoxMapSet*, Box2BoxMapEntry);
1011
+ static int Box2BoxMapSetRemove(Box2BoxMapSet*, Box2BoxMapEntry);
1012
+ static void Box2BoxMapSetNot(Box2BoxMapSet*, Box2BoxMapSet*);
1013
+ static void Box2BoxMapSetAnd(Box2BoxMapSet*, Box2BoxMapSet*);
1014
+ static void Box2BoxMapSetOr(Box2BoxMapSet*, Box2BoxMapSet*);
1015
+ static void Box2BoxMapSetXor(Box2BoxMapSet*, Box2BoxMapSet*);
1016
+ static void Box2BoxMapSetItCtor(Box2BoxMapSetIt*, Box2BoxMapSet*);
1017
+ static int Box2BoxMapSetItHasNext(Box2BoxMapSetIt*);
1018
+ static Box2BoxMapEntry Box2BoxMapSetItNext(Box2BoxMapSetIt*);
1019
+
1020
+ static void Box2BoxMapSetListCtor(Box2BoxMapSetList*);
1021
+ static void Box2BoxMapSetListDtor(Box2BoxMapSetList*);
1022
+ static void Box2BoxMapSetListPurge(Box2BoxMapSetList*);
1023
+ static Box2BoxMapSetList* Box2BoxMapSetListNew(void);
1024
+ static void Box2BoxMapSetListDestroy(Box2BoxMapSetList*);
1025
+ static Box2BoxMapSetList* Box2BoxMapSetListAssign(Box2BoxMapSetList*);
1026
+ static Box2BoxMapEntry Box2BoxMapSetListGet(Box2BoxMapSetList*);
1027
+ static void Box2BoxMapSetListAdd(Box2BoxMapSetList*, Box2BoxMapEntry);
1028
+ static void Box2BoxMapSetListChop(Box2BoxMapSetList*);
1029
+ static int Box2BoxMapSetListContains(Box2BoxMapSetList*, Box2BoxMapEntry);
1030
+ static Box2BoxMapEntry Box2BoxMapSetListFind(Box2BoxMapSetList*, Box2BoxMapEntry);
1031
+ static int Box2BoxMapSetListReplace(Box2BoxMapSetList*, Box2BoxMapEntry, Box2BoxMapEntry);
1032
+ static int Box2BoxMapSetListReplaceAll(Box2BoxMapSetList*, Box2BoxMapEntry, Box2BoxMapEntry);
1033
+ static int Box2BoxMapSetListRemove(Box2BoxMapSetList*, Box2BoxMapEntry);
1034
+ static int Box2BoxMapSetListRemoveAll(Box2BoxMapSetList*, Box2BoxMapEntry);
1035
+ static size_t Box2BoxMapSetListSize(Box2BoxMapSetList*);
1036
+ static int Box2BoxMapSetListEmpty(Box2BoxMapSetList*);
1037
+ static void Box2BoxMapSetListItCtor(Box2BoxMapSetListIt*, Box2BoxMapSetList*);
1038
+ static int Box2BoxMapSetListItHasNext(Box2BoxMapSetListIt*);
1039
+ static Box2BoxMapEntry Box2BoxMapSetListItNext(Box2BoxMapSetListIt*);
1040
+
1041
+ static void Box2BoxMapSetListCtor(Box2BoxMapSetList* self) {
1042
+ assert(self);
1043
+ self->head_node = NULL;
1044
+ self->node_count = 0;
1045
+ }
1046
+ static void Box2BoxMapSetListDtor(Box2BoxMapSetList* self) {
1047
+ Box2BoxMapSetListNode* node;
1048
+ assert(self);
1049
+ {
1050
+ Box2BoxMapSetListIt it;
1051
+ Box2BoxMapSetListItCtor(&it, self);
1052
+ while(Box2BoxMapSetListItHasNext(&it)) {
1053
+ Box2BoxMapEntry e = Box2BoxMapSetListItNext(&it);
1054
+ Box2BoxMapEntryDtor(e);
1055
+ }
1056
+ };
1057
+ node = self->head_node;
1058
+ while(node) {
1059
+ Box2BoxMapSetListNode* this_node = node;
1060
+ node = node->next_node;
1061
+ free(this_node);
1062
+ }
1063
+ }
1064
+ static Box2BoxMapSetList* Box2BoxMapSetListNew(void) {
1065
+ Box2BoxMapSetList* self = (Box2BoxMapSetList*)malloc(sizeof(Box2BoxMapSetList)); assert(self);
1066
+ Box2BoxMapSetListCtor(self);
1067
+ self->ref_count = 0;
1068
+ return self;
1069
+ }
1070
+ static void Box2BoxMapSetListDestroy(Box2BoxMapSetList* self) {
1071
+ assert(self);
1072
+ if(!--self->ref_count) {
1073
+ Box2BoxMapSetListDtor(self);
1074
+ free(self);
1075
+ }
1076
+ }
1077
+ static Box2BoxMapSetList* Box2BoxMapSetListAssign(Box2BoxMapSetList* self) {
1078
+ ++self->ref_count;
1079
+ return self;
1080
+ }
1081
+ static void Box2BoxMapSetListPurge(Box2BoxMapSetList* self) {
1082
+ Box2BoxMapSetListDtor(self);
1083
+ Box2BoxMapSetListCtor(self);
1084
+ }
1085
+ static Box2BoxMapEntry Box2BoxMapSetListGet(Box2BoxMapSetList* self) {
1086
+ assert(self);
1087
+ assert(!Box2BoxMapSetListEmpty(self));
1088
+ return self->head_node->element;
1089
+ }
1090
+ static void Box2BoxMapSetListChop(Box2BoxMapSetList* self) {
1091
+ Box2BoxMapSetListNode* node;
1092
+ assert(self);
1093
+ assert(!Box2BoxMapSetListEmpty(self));
1094
+ node = self->head_node;
1095
+ Box2BoxMapEntryDtor(node->element);
1096
+ self->head_node = self->head_node->next_node;
1097
+ --self->node_count;
1098
+ free(node);
1099
+ }
1100
+ static void Box2BoxMapSetListAdd(Box2BoxMapSetList* self, Box2BoxMapEntry element) {
1101
+ Box2BoxMapSetListNode* node;
1102
+ assert(self);
1103
+ node = (Box2BoxMapSetListNode*)malloc(sizeof(Box2BoxMapSetListNode)); assert(node);
1104
+ node->element = Box2BoxMapEntryAssign(element);
1105
+ node->next_node = self->head_node;
1106
+ self->head_node = node;
1107
+ ++self->node_count;
1108
+ }
1109
+ static int Box2BoxMapSetListContains(Box2BoxMapSetList* self, Box2BoxMapEntry what) {
1110
+ Box2BoxMapSetListNode* node;
1111
+ assert(self);
1112
+ what = Box2BoxMapEntryAssign(what);
1113
+ node = self->head_node;
1114
+ while(node) {
1115
+ if(Box2BoxMapEntryEqual(node->element,what)) {
1116
+ Box2BoxMapEntryDtor(what);
1117
+ return 1;
1118
+ }
1119
+ node = node->next_node;
1120
+ }
1121
+ Box2BoxMapEntryDtor(what);
1122
+ return 0;
1123
+ }
1124
+ static Box2BoxMapEntry Box2BoxMapSetListFind(Box2BoxMapSetList* self, Box2BoxMapEntry what) {
1125
+ Box2BoxMapSetListNode* node;
1126
+ assert(self);
1127
+ what = Box2BoxMapEntryAssign(what);
1128
+ assert(Box2BoxMapSetListContains(self, what));
1129
+ node = self->head_node;
1130
+ while(node) {
1131
+ if(Box2BoxMapEntryEqual(node->element,what)) {
1132
+ Box2BoxMapEntryDtor(what);
1133
+ return node->element;
1134
+ }
1135
+ node = node->next_node;
1136
+ }
1137
+ abort();
1138
+ }
1139
+ static int Box2BoxMapSetListReplace(Box2BoxMapSetList* self, Box2BoxMapEntry what, Box2BoxMapEntry with) {
1140
+ Box2BoxMapSetListNode* node;
1141
+ assert(self);
1142
+ what = Box2BoxMapEntryAssign(what);
1143
+ with = Box2BoxMapEntryAssign(with);
1144
+ node = self->head_node;
1145
+ while(node) {
1146
+ if(Box2BoxMapEntryEqual(node->element,what)) {
1147
+ Box2BoxMapEntryDtor(node->element);
1148
+ node->element = Box2BoxMapEntryAssign(with);
1149
+ Box2BoxMapEntryDtor(what);
1150
+ Box2BoxMapEntryDtor(with);
1151
+ return 1;
1152
+ }
1153
+ node = node->next_node;
1154
+ }
1155
+ Box2BoxMapEntryDtor(what);
1156
+ Box2BoxMapEntryDtor(with);
1157
+ return 0;
1158
+ }
1159
+ static int Box2BoxMapSetListReplaceAll(Box2BoxMapSetList* self, Box2BoxMapEntry what, Box2BoxMapEntry with) {
1160
+ Box2BoxMapSetListNode* node;
1161
+ int count = 0;
1162
+ assert(self);
1163
+ what = Box2BoxMapEntryAssign(what);
1164
+ with = Box2BoxMapEntryAssign(with);
1165
+ node = self->head_node;
1166
+ while(node) {
1167
+ if(Box2BoxMapEntryEqual(node->element,what)) {
1168
+ Box2BoxMapEntryDtor(node->element);
1169
+ node->element = Box2BoxMapEntryAssign(with);
1170
+ ++count;
1171
+ }
1172
+ node = node->next_node;
1173
+ }
1174
+ Box2BoxMapEntryDtor(what);
1175
+ Box2BoxMapEntryDtor(with);
1176
+ return count;
1177
+ }
1178
+ static int Box2BoxMapSetListRemove(Box2BoxMapSetList* self, Box2BoxMapEntry what) {
1179
+ Box2BoxMapSetListNode* node;
1180
+ Box2BoxMapSetListNode* prev_node;
1181
+ int found = 0;
1182
+ assert(self);
1183
+ what = Box2BoxMapEntryAssign(what);
1184
+ node = self->head_node;
1185
+ prev_node = NULL;
1186
+ while(node) {
1187
+ if(Box2BoxMapEntryEqual(node->element,what)) {
1188
+ Box2BoxMapEntryDtor(node->element);
1189
+ if(prev_node) {
1190
+ prev_node->next_node = node->next_node ? node->next_node : NULL;
1191
+ } else {
1192
+ self->head_node = node->next_node ? node->next_node : NULL;
1193
+ }
1194
+ --self->node_count;
1195
+ free(node);
1196
+ found = 1;
1197
+ break;
1198
+ }
1199
+ prev_node = node;
1200
+ node = node->next_node;
1201
+ }
1202
+ Box2BoxMapEntryDtor(what);
1203
+ return found;
1204
+ }
1205
+ static int Box2BoxMapSetListRemoveAll(Box2BoxMapSetList* self, Box2BoxMapEntry what) {
1206
+ Box2BoxMapSetListNode* node;
1207
+ Box2BoxMapSetListNode* prev_node;
1208
+ int count = 0;
1209
+ assert(self);
1210
+ what = Box2BoxMapEntryAssign(what);
1211
+ node = self->head_node;
1212
+ prev_node = NULL;
1213
+ while(node) {
1214
+ if(Box2BoxMapEntryEqual(node->element,what)) {
1215
+ Box2BoxMapEntryDtor(node->element);
1216
+ if(prev_node) {
1217
+ prev_node->next_node = node->next_node ? node->next_node : NULL;
1218
+ } else {
1219
+ self->head_node = node->next_node ? node->next_node : NULL;
1220
+ }
1221
+ --self->node_count;
1222
+ free(node);
1223
+ ++count;
1224
+ }
1225
+ prev_node = node;
1226
+ node = node->next_node;
1227
+ }
1228
+ Box2BoxMapEntryDtor(what);
1229
+ return count;
1230
+ }
1231
+ static size_t Box2BoxMapSetListSize(Box2BoxMapSetList* self) {
1232
+ assert(self);
1233
+ return self->node_count;
1234
+ }
1235
+ static int Box2BoxMapSetListEmpty(Box2BoxMapSetList* self) {
1236
+ assert(self);
1237
+ return !self->node_count;
1238
+ }
1239
+ static void Box2BoxMapSetListItCtor(Box2BoxMapSetListIt* self, Box2BoxMapSetList* list) {
1240
+ assert(self);
1241
+ assert(list);
1242
+ self->next_node = list->head_node;
1243
+ }
1244
+ static int Box2BoxMapSetListItHasNext(Box2BoxMapSetListIt* self) {
1245
+ assert(self);
1246
+ return self->next_node != NULL;
1247
+ }
1248
+ static Box2BoxMapEntry Box2BoxMapSetListItNext(Box2BoxMapSetListIt* self) {
1249
+ Box2BoxMapSetListNode* node;
1250
+ assert(self);
1251
+ node = self->next_node;
1252
+ self->next_node = self->next_node->next_node;
1253
+ return node->element;
1254
+ }
1255
+
1256
+ static void Box2BoxMapSetCtor(Box2BoxMapSet* self) {
1257
+ assert(self);
1258
+ self->min_bucket_count = 16;
1259
+ self->min_fill = 20;
1260
+ self->max_fill = 80;
1261
+ self->min_size = (size_t)((float)self->min_fill/100*self->min_bucket_count);
1262
+ self->max_size = (size_t)((float)self->max_fill/100*self->min_bucket_count);
1263
+ self->capacity_multiplier = 200;
1264
+ self->buckets = NULL;
1265
+ Box2BoxMapSetRehash(self);
1266
+ }
1267
+ static void Box2BoxMapSetDtor(Box2BoxMapSet* self) {
1268
+ size_t i;
1269
+ assert(self);
1270
+ for(i = 0; i < self->bucket_count; ++i) {
1271
+ Box2BoxMapSetListDtor(&self->buckets[i]);
1272
+ }
1273
+ free(self->buckets);
1274
+ }
1275
+ static Box2BoxMapSet* Box2BoxMapSetNew(void) {
1276
+ Box2BoxMapSet* self = (Box2BoxMapSet*)malloc(sizeof(Box2BoxMapSet)); assert(self);
1277
+ Box2BoxMapSetCtor(self);
1278
+ self->ref_count = 0;
1279
+ return self;
1280
+ }
1281
+ static void Box2BoxMapSetDestroy(Box2BoxMapSet* self) {
1282
+ assert(self);
1283
+ if(!--self->ref_count) {
1284
+ Box2BoxMapSetDtor(self);
1285
+ free(self);
1286
+ }
1287
+ }
1288
+ static Box2BoxMapSet* Box2BoxMapSetAssign(Box2BoxMapSet* self) {
1289
+ ++self->ref_count;
1290
+ return self;
1291
+ }
1292
+ static void Box2BoxMapSetPurge(Box2BoxMapSet* self) {
1293
+ assert(self);
1294
+ Box2BoxMapSetDtor(self);
1295
+ self->buckets = NULL;
1296
+ Box2BoxMapSetRehash(self);
1297
+ }
1298
+ static void Box2BoxMapSetRehash(Box2BoxMapSet* self) {
1299
+ Box2BoxMapSetList* buckets;
1300
+ size_t i, bucket_count, size, fill;
1301
+ assert(self);
1302
+ assert(self->min_fill > 0);
1303
+ assert(self->max_fill > 0);
1304
+ assert(self->min_fill < self->max_fill);
1305
+ assert(self->min_bucket_count > 0);
1306
+ if(self->buckets) {
1307
+ if(self->min_size < self->size && self->size < self->max_size) return;
1308
+ fill = (size_t)((float)self->size/self->bucket_count*100);
1309
+ if(fill > self->max_fill) {
1310
+ bucket_count = (size_t)((float)self->bucket_count/100*self->capacity_multiplier);
1311
+ } else
1312
+ if(fill < self->min_fill && self->bucket_count > self->min_bucket_count) {
1313
+ bucket_count = (size_t)((float)self->bucket_count/self->capacity_multiplier*100);
1314
+ if(bucket_count < self->min_bucket_count) bucket_count = self->min_bucket_count;
1315
+ } else
1316
+ return;
1317
+ size = self->size;
1318
+ self->min_size = (size_t)((float)self->min_fill/100*size);
1319
+ self->max_size = (size_t)((float)self->max_fill/100*size);
1320
+ } else {
1321
+ bucket_count = self->min_bucket_count;
1322
+ size = 0;
1323
+ }
1324
+ buckets = (Box2BoxMapSetList*)malloc(bucket_count*sizeof(Box2BoxMapSetList)); assert(buckets);
1325
+ for(i = 0; i < bucket_count; ++i) {
1326
+ Box2BoxMapSetListCtor(&buckets[i]);
1327
+ }
1328
+ if(self->buckets) {
1329
+ Box2BoxMapSetIt it;
1330
+ Box2BoxMapSetItCtor(&it, self);
1331
+ while(Box2BoxMapSetItHasNext(&it)) {
1332
+ Box2BoxMapSetList* bucket;
1333
+ Box2BoxMapEntry element = Box2BoxMapSetItNext(&it);
1334
+ bucket = &buckets[Box2BoxMapEntryHash(element) % bucket_count];
1335
+ Box2BoxMapSetListAdd(bucket, element);
1336
+ }
1337
+ Box2BoxMapSetDtor(self);
1338
+ }
1339
+ self->buckets = buckets;
1340
+ self->bucket_count = bucket_count;
1341
+ self->size = size;
1342
+ }
1343
+ static int Box2BoxMapSetContains(Box2BoxMapSet* self, Box2BoxMapEntry element) {
1344
+ int result;
1345
+ assert(self);
1346
+ element = Box2BoxMapEntryAssign(element);
1347
+ result = Box2BoxMapSetListContains(&self->buckets[Box2BoxMapEntryHash(element) % self->bucket_count], element);
1348
+ Box2BoxMapEntryDtor(element);
1349
+ return result;
1350
+ }
1351
+ static Box2BoxMapEntry Box2BoxMapSetGet(Box2BoxMapSet* self, Box2BoxMapEntry element) {
1352
+ Box2BoxMapEntry result;
1353
+ assert(self);
1354
+ element = Box2BoxMapEntryAssign(element);
1355
+ assert(Box2BoxMapSetContains(self, element));
1356
+ result = Box2BoxMapSetListFind(&self->buckets[Box2BoxMapEntryHash(element) % self->bucket_count], element);
1357
+ Box2BoxMapEntryDtor(element);
1358
+ return result;
1359
+ }
1360
+ static size_t Box2BoxMapSetSize(Box2BoxMapSet* self) {
1361
+ assert(self);
1362
+ return self->size;
1363
+ }
1364
+ static int Box2BoxMapSetEmpty(Box2BoxMapSet* self) {
1365
+ assert(self);
1366
+ return !self->size;
1367
+ }
1368
+ static int Box2BoxMapSetPut(Box2BoxMapSet* self, Box2BoxMapEntry element) {
1369
+ int contained = 1;
1370
+ Box2BoxMapSetList* bucket;
1371
+ assert(self);
1372
+ element = Box2BoxMapEntryAssign(element);
1373
+ bucket = &self->buckets[Box2BoxMapEntryHash(element) % self->bucket_count];
1374
+ if(!Box2BoxMapSetListContains(bucket, element)) {
1375
+ Box2BoxMapSetListAdd(bucket, element);
1376
+ ++self->size;
1377
+ contained = 0;
1378
+ Box2BoxMapSetRehash(self);
1379
+ }
1380
+ Box2BoxMapEntryDtor(element);
1381
+ return contained;
1382
+ }
1383
+ static void Box2BoxMapSetReplace(Box2BoxMapSet* self, Box2BoxMapEntry element) {
1384
+ Box2BoxMapSetList* bucket;
1385
+ assert(self);
1386
+ element = Box2BoxMapEntryAssign(element);
1387
+ bucket = &self->buckets[Box2BoxMapEntryHash(element) % self->bucket_count];
1388
+ if(!Box2BoxMapSetListReplace(bucket, element, element)) {
1389
+ Box2BoxMapSetListAdd(bucket, element);
1390
+ ++self->size;
1391
+ Box2BoxMapSetRehash(self);
1392
+ }
1393
+ Box2BoxMapEntryDtor(element);
1394
+ }
1395
+ static int Box2BoxMapSetRemove(Box2BoxMapSet* self, Box2BoxMapEntry what) {
1396
+ int removed = 0;
1397
+ Box2BoxMapSetList* bucket;
1398
+ assert(self);
1399
+ what = Box2BoxMapEntryAssign(what);
1400
+ bucket = &self->buckets[Box2BoxMapEntryHash(what) % self->bucket_count];
1401
+ if(Box2BoxMapSetListRemove(bucket, what)) {
1402
+ --self->size;
1403
+ removed = 1;
1404
+ Box2BoxMapSetRehash(self);
1405
+ }
1406
+ Box2BoxMapEntryDtor(what);
1407
+ return removed;
1408
+ }
1409
+ static void Box2BoxMapSetNot(Box2BoxMapSet* self, Box2BoxMapSet* other) {
1410
+ Box2BoxMapSetIt it;
1411
+ assert(self);
1412
+ assert(other);
1413
+ Box2BoxMapSetItCtor(&it, other);
1414
+ while(Box2BoxMapSetItHasNext(&it)) {
1415
+ Box2BoxMapSetRemove(self, Box2BoxMapSetItNext(&it));
1416
+ }
1417
+ Box2BoxMapSetRehash(self);
1418
+ }
1419
+ static void Box2BoxMapSetOr(Box2BoxMapSet* self, Box2BoxMapSet* other) {
1420
+ Box2BoxMapSetIt it;
1421
+ assert(self);
1422
+ assert(other);
1423
+ Box2BoxMapSetItCtor(&it, other);
1424
+ while(Box2BoxMapSetItHasNext(&it)) {
1425
+ Box2BoxMapSetPut(self, Box2BoxMapSetItNext(&it));
1426
+ }
1427
+ Box2BoxMapSetRehash(self);
1428
+ }
1429
+ static void Box2BoxMapSetAnd(Box2BoxMapSet* self, Box2BoxMapSet* other) {
1430
+ Box2BoxMapSetIt it;
1431
+ Box2BoxMapSet set;
1432
+ assert(self);
1433
+ assert(other);
1434
+ Box2BoxMapSetCtor(&set);
1435
+ Box2BoxMapSetItCtor(&it, self);
1436
+ while(Box2BoxMapSetItHasNext(&it)) {
1437
+ Box2BoxMapEntry element = Box2BoxMapSetItNext(&it);
1438
+ if(Box2BoxMapSetContains(other, element)) Box2BoxMapSetPut(&set, element);
1439
+ }
1440
+ Box2BoxMapSetItCtor(&it, other);
1441
+ while(Box2BoxMapSetItHasNext(&it)) {
1442
+ Box2BoxMapEntry element = Box2BoxMapSetItNext(&it);
1443
+ if(Box2BoxMapSetContains(self, element)) Box2BoxMapSetPut(&set, element);
1444
+ }
1445
+ Box2BoxMapSetDtor(self);
1446
+ self->buckets = set.buckets;
1447
+ self->size = set.size;
1448
+ Box2BoxMapSetRehash(self);
1449
+ /*Box2BoxMapSetDtor(&set);*/
1450
+ }
1451
+ static void Box2BoxMapSetXor(Box2BoxMapSet* self, Box2BoxMapSet* other) {
1452
+ Box2BoxMapSetIt it;
1453
+ Box2BoxMapSet set;
1454
+ assert(self);
1455
+ assert(other);
1456
+ Box2BoxMapSetCtor(&set);
1457
+ Box2BoxMapSetItCtor(&it, self);
1458
+ while(Box2BoxMapSetItHasNext(&it)) {
1459
+ Box2BoxMapEntry element = Box2BoxMapSetItNext(&it);
1460
+ if(!Box2BoxMapSetContains(other, element)) Box2BoxMapSetPut(&set, element);
1461
+ }
1462
+ Box2BoxMapSetItCtor(&it, other);
1463
+ while(Box2BoxMapSetItHasNext(&it)) {
1464
+ Box2BoxMapEntry element = Box2BoxMapSetItNext(&it);
1465
+ if(!Box2BoxMapSetContains(self, element)) Box2BoxMapSetPut(&set, element);
1466
+ }
1467
+ Box2BoxMapSetDtor(self);
1468
+ self->buckets = set.buckets;
1469
+ self->size = set.size;
1470
+ Box2BoxMapSetRehash(self);
1471
+ /*Box2BoxMapSetDtor(&set);*/
1472
+ }
1473
+ static void Box2BoxMapSetItCtor(Box2BoxMapSetIt* self, Box2BoxMapSet* set) {
1474
+ assert(self);
1475
+ self->set = set;
1476
+ self->bucket_index = 0;
1477
+ Box2BoxMapSetListItCtor(&self->it, &set->buckets[0]);
1478
+ }
1479
+ static int Box2BoxMapSetItHasNext(Box2BoxMapSetIt* self) {
1480
+ assert(self);
1481
+ if(Box2BoxMapSetListItHasNext(&self->it)) {
1482
+ return 1;
1483
+ } else {
1484
+ size_t i; for(i = self->bucket_index+1; i < self->set->bucket_count; ++i) {
1485
+ if(!Box2BoxMapSetListEmpty(&self->set->buckets[i])) {
1486
+ return 1;
1487
+ }
1488
+ }
1489
+ return 0;
1490
+ }
1491
+ }
1492
+ static Box2BoxMapEntry Box2BoxMapSetItNext(Box2BoxMapSetIt* self) {
1493
+ assert(self);
1494
+ assert(Box2BoxMapSetItHasNext(self));
1495
+ if(Box2BoxMapSetListItHasNext(&self->it)) {
1496
+ return Box2BoxMapSetListItNext(&self->it);
1497
+ } else {
1498
+ size_t i; for(i = self->bucket_index+1; i < self->set->bucket_count; ++i) {
1499
+ if(!Box2BoxMapSetListEmpty(&self->set->buckets[i])) {
1500
+ Box2BoxMapSetListItCtor(&self->it, &self->set->buckets[i]);
1501
+ self->bucket_index = i;
1502
+ return Box2BoxMapSetListItNext(&self->it);
1503
+ }
1504
+ }
1505
+ abort();
1506
+ }
1507
+ }
1508
+
1509
+ void Box2BoxMapCtor(Box2BoxMap* self) {
1510
+ assert(self);
1511
+ Box2BoxMapSetCtor(&self->entries);
1512
+ }
1513
+ void Box2BoxMapDtor(Box2BoxMap* self) {
1514
+ assert(self);
1515
+ Box2BoxMapSetDtor(&self->entries);
1516
+ }
1517
+ Box2BoxMap* Box2BoxMapNew(void) {
1518
+ Box2BoxMap* self = (Box2BoxMap*)malloc(sizeof(Box2BoxMap)); assert(self);
1519
+ Box2BoxMapCtor(self);
1520
+ self->ref_count = 0;
1521
+ return self;
1522
+ }
1523
+ void Box2BoxMapDestroy(Box2BoxMap* self) {
1524
+ assert(self);
1525
+ if(!--self->ref_count) {
1526
+ Box2BoxMapDtor(self);
1527
+ free(self);
1528
+ }
1529
+ }
1530
+ void Box2BoxMapRehash(Box2BoxMap* self) {
1531
+ assert(self);
1532
+ Box2BoxMapSetRehash(&self->entries);
1533
+ }
1534
+ Box2BoxMap* Box2BoxMapAssign(Box2BoxMap* self) {
1535
+ assert(self);
1536
+ ++self->ref_count;
1537
+ return self;
1538
+ }
1539
+ void Box2BoxMapPurge(Box2BoxMap* self) {
1540
+ assert(self);
1541
+ Box2BoxMapSetPurge(&self->entries);
1542
+ }
1543
+ size_t Box2BoxMapSize(Box2BoxMap* self) {
1544
+ assert(self);
1545
+ return Box2BoxMapSetSize(&self->entries);
1546
+ }
1547
+ int Box2BoxMapEmpty(Box2BoxMap* self) {
1548
+ assert(self);
1549
+ return Box2BoxMapSetEmpty(&self->entries);
1550
+ }
1551
+ int Box2BoxMapContainsKey(Box2BoxMap* self, Box* key) {
1552
+ int result;
1553
+ Box2BoxMapEntry entry;
1554
+ assert(self);
1555
+ entry = Box2BoxMapEntryAssign(Box2BoxMapEntryKeyOnly(key));
1556
+ result = Box2BoxMapSetContains(&self->entries, entry);
1557
+ Box2BoxMapEntryDtor(entry);
1558
+ return result;
1559
+ }
1560
+ Box* Box2BoxMapGet(Box2BoxMap* self, Box* key) {
1561
+ Box* result;
1562
+ Box2BoxMapEntry entry;
1563
+ assert(self);
1564
+ entry = Box2BoxMapEntryAssign(Box2BoxMapEntryKeyOnly(key));
1565
+ assert(Box2BoxMapContainsKey(self, key));
1566
+ result = Box2BoxMapSetGet(&self->entries, entry).value;
1567
+ Box2BoxMapEntryDtor(entry);
1568
+ return result;
1569
+ }
1570
+ int Box2BoxMapPut(Box2BoxMap* self, Box* key, Box* value) {
1571
+ Box2BoxMapEntry entry = Box2BoxMapEntryAssign(Box2BoxMapEntryKeyValue(key,value));
1572
+ assert(self);
1573
+ if(!Box2BoxMapContainsKey(self, key)) {
1574
+ Box2BoxMapSetPut(&self->entries, entry);
1575
+ Box2BoxMapEntryDtor(entry);
1576
+ return 1;
1577
+ } else {
1578
+ Box2BoxMapEntryDtor(entry);
1579
+ return 0;
1580
+ }
1581
+ }
1582
+ void Box2BoxMapReplace(Box2BoxMap* self, Box* key, Box* value) {
1583
+ Box2BoxMapEntry entry;
1584
+ assert(self);
1585
+ entry = Box2BoxMapEntryAssign(Box2BoxMapEntryKeyValue(key,value));
1586
+ Box2BoxMapSetReplace(&self->entries, entry);
1587
+ Box2BoxMapEntryDtor(entry);
1588
+ }
1589
+ int Box2BoxMapRemove(Box2BoxMap* self, Box* key) {
1590
+ int removed;
1591
+ Box2BoxMapEntry entry;
1592
+ assert(self);
1593
+ entry = Box2BoxMapEntryAssign(Box2BoxMapEntryKeyOnly(key));
1594
+ removed = Box2BoxMapSetRemove(&self->entries, entry);
1595
+ Box2BoxMapEntryDtor(entry);
1596
+ return removed;
1597
+ }
1598
+ void Box2BoxMapItCtor(Box2BoxMapIt* self, Box2BoxMap* map) {
1599
+ assert(self);
1600
+ assert(map);
1601
+ Box2BoxMapSetItCtor(&self->it, &map->entries);
1602
+ }
1603
+ int Box2BoxMapItHasNext(Box2BoxMapIt* self) {
1604
+ assert(self);
1605
+ return Box2BoxMapSetItHasNext(&self->it);
1606
+ }
1607
+ Box* Box2BoxMapItNextKey(Box2BoxMapIt* self) {
1608
+ assert(self);
1609
+ return Box2BoxMapSetItNext(&self->it).key;
1610
+ }
1611
+ Box* Box2BoxMapItNextValue(Box2BoxMapIt* self) {
1612
+ assert(self);
1613
+ return Box2BoxMapSetItNext(&self->it).value;
1614
+ }
1615
+ Box2BoxMapEntry Box2BoxMapItNext(Box2BoxMapIt* self) {
1616
+ assert(self);
1617
+ return Box2BoxMapSetItNext(&self->it);
1618
+ }
1619
+
1620
+ static PChar2IntMapEntry PChar2IntMapEntryKeyOnly(const char* key) {
1621
+ PChar2IntMapEntry entry;
1622
+ entry.key = key;
1623
+ entry.valid_value = 0;
1624
+ return entry;
1625
+ }
1626
+ static PChar2IntMapEntry PChar2IntMapEntryKeyValue(const char* key, int value) {
1627
+ PChar2IntMapEntry entry;
1628
+ entry.key = key;
1629
+ entry.value = value;
1630
+ entry.valid_value = 1;
1631
+ return entry;
1632
+ }
1633
+ static size_t PChar2IntMapEntryHash(PChar2IntMapEntry entry) {
1634
+ return PCharHash(entry.key);
1635
+ }
1636
+ static int PChar2IntMapEntryEqual(PChar2IntMapEntry lt, PChar2IntMapEntry rt) {
1637
+ return PCharEqual(lt.key,rt.key);
1638
+ }
1639
+ static PChar2IntMapEntry PChar2IntMapEntryAssign(PChar2IntMapEntry entry) {
1640
+ entry.key = (entry.key);
1641
+ if(entry.valid_value) entry.value = (entry.value);
1642
+ return entry;
1643
+ }
1644
+ static void PChar2IntMapEntryDtor(PChar2IntMapEntry entry) {
1645
+ ;
1646
+ if(entry.valid_value) ;
1647
+ }
1648
+
1649
+ static void PChar2IntMapSetCtor(PChar2IntMapSet*);
1650
+ static void PChar2IntMapSetDtor(PChar2IntMapSet*);
1651
+ static PChar2IntMapSet* PChar2IntMapSetNew(void);
1652
+ static void PChar2IntMapSetDestroy(PChar2IntMapSet*);
1653
+ static PChar2IntMapSet* PChar2IntMapSetAssign(PChar2IntMapSet*);
1654
+ static void PChar2IntMapSetPurge(PChar2IntMapSet*);
1655
+ static void PChar2IntMapSetRehash(PChar2IntMapSet*);
1656
+ static int PChar2IntMapSetContains(PChar2IntMapSet*, PChar2IntMapEntry);
1657
+ static PChar2IntMapEntry PChar2IntMapSetGet(PChar2IntMapSet*, PChar2IntMapEntry);
1658
+ static size_t PChar2IntMapSetSize(PChar2IntMapSet*);
1659
+ static int PChar2IntMapSetEmpty(PChar2IntMapSet*);
1660
+ static int PChar2IntMapSetPut(PChar2IntMapSet*, PChar2IntMapEntry);
1661
+ static void PChar2IntMapSetReplace(PChar2IntMapSet*, PChar2IntMapEntry);
1662
+ static int PChar2IntMapSetRemove(PChar2IntMapSet*, PChar2IntMapEntry);
1663
+ static void PChar2IntMapSetNot(PChar2IntMapSet*, PChar2IntMapSet*);
1664
+ static void PChar2IntMapSetAnd(PChar2IntMapSet*, PChar2IntMapSet*);
1665
+ static void PChar2IntMapSetOr(PChar2IntMapSet*, PChar2IntMapSet*);
1666
+ static void PChar2IntMapSetXor(PChar2IntMapSet*, PChar2IntMapSet*);
1667
+ static void PChar2IntMapSetItCtor(PChar2IntMapSetIt*, PChar2IntMapSet*);
1668
+ static int PChar2IntMapSetItHasNext(PChar2IntMapSetIt*);
1669
+ static PChar2IntMapEntry PChar2IntMapSetItNext(PChar2IntMapSetIt*);
1670
+
1671
+ static void PChar2IntMapSetListCtor(PChar2IntMapSetList*);
1672
+ static void PChar2IntMapSetListDtor(PChar2IntMapSetList*);
1673
+ static void PChar2IntMapSetListPurge(PChar2IntMapSetList*);
1674
+ static PChar2IntMapSetList* PChar2IntMapSetListNew(void);
1675
+ static void PChar2IntMapSetListDestroy(PChar2IntMapSetList*);
1676
+ static PChar2IntMapSetList* PChar2IntMapSetListAssign(PChar2IntMapSetList*);
1677
+ static PChar2IntMapEntry PChar2IntMapSetListGet(PChar2IntMapSetList*);
1678
+ static void PChar2IntMapSetListAdd(PChar2IntMapSetList*, PChar2IntMapEntry);
1679
+ static void PChar2IntMapSetListChop(PChar2IntMapSetList*);
1680
+ static int PChar2IntMapSetListContains(PChar2IntMapSetList*, PChar2IntMapEntry);
1681
+ static PChar2IntMapEntry PChar2IntMapSetListFind(PChar2IntMapSetList*, PChar2IntMapEntry);
1682
+ static int PChar2IntMapSetListReplace(PChar2IntMapSetList*, PChar2IntMapEntry, PChar2IntMapEntry);
1683
+ static int PChar2IntMapSetListReplaceAll(PChar2IntMapSetList*, PChar2IntMapEntry, PChar2IntMapEntry);
1684
+ static int PChar2IntMapSetListRemove(PChar2IntMapSetList*, PChar2IntMapEntry);
1685
+ static int PChar2IntMapSetListRemoveAll(PChar2IntMapSetList*, PChar2IntMapEntry);
1686
+ static size_t PChar2IntMapSetListSize(PChar2IntMapSetList*);
1687
+ static int PChar2IntMapSetListEmpty(PChar2IntMapSetList*);
1688
+ static void PChar2IntMapSetListItCtor(PChar2IntMapSetListIt*, PChar2IntMapSetList*);
1689
+ static int PChar2IntMapSetListItHasNext(PChar2IntMapSetListIt*);
1690
+ static PChar2IntMapEntry PChar2IntMapSetListItNext(PChar2IntMapSetListIt*);
1691
+
1692
+ static void PChar2IntMapSetListCtor(PChar2IntMapSetList* self) {
1693
+ assert(self);
1694
+ self->head_node = NULL;
1695
+ self->node_count = 0;
1696
+ }
1697
+ static void PChar2IntMapSetListDtor(PChar2IntMapSetList* self) {
1698
+ PChar2IntMapSetListNode* node;
1699
+ assert(self);
1700
+ {
1701
+ PChar2IntMapSetListIt it;
1702
+ PChar2IntMapSetListItCtor(&it, self);
1703
+ while(PChar2IntMapSetListItHasNext(&it)) {
1704
+ PChar2IntMapEntry e = PChar2IntMapSetListItNext(&it);
1705
+ PChar2IntMapEntryDtor(e);
1706
+ }
1707
+ };
1708
+ node = self->head_node;
1709
+ while(node) {
1710
+ PChar2IntMapSetListNode* this_node = node;
1711
+ node = node->next_node;
1712
+ free(this_node);
1713
+ }
1714
+ }
1715
+ static PChar2IntMapSetList* PChar2IntMapSetListNew(void) {
1716
+ PChar2IntMapSetList* self = (PChar2IntMapSetList*)malloc(sizeof(PChar2IntMapSetList)); assert(self);
1717
+ PChar2IntMapSetListCtor(self);
1718
+ self->ref_count = 0;
1719
+ return self;
1720
+ }
1721
+ static void PChar2IntMapSetListDestroy(PChar2IntMapSetList* self) {
1722
+ assert(self);
1723
+ if(!--self->ref_count) {
1724
+ PChar2IntMapSetListDtor(self);
1725
+ free(self);
1726
+ }
1727
+ }
1728
+ static PChar2IntMapSetList* PChar2IntMapSetListAssign(PChar2IntMapSetList* self) {
1729
+ ++self->ref_count;
1730
+ return self;
1731
+ }
1732
+ static void PChar2IntMapSetListPurge(PChar2IntMapSetList* self) {
1733
+ PChar2IntMapSetListDtor(self);
1734
+ PChar2IntMapSetListCtor(self);
1735
+ }
1736
+ static PChar2IntMapEntry PChar2IntMapSetListGet(PChar2IntMapSetList* self) {
1737
+ assert(self);
1738
+ assert(!PChar2IntMapSetListEmpty(self));
1739
+ return self->head_node->element;
1740
+ }
1741
+ static void PChar2IntMapSetListChop(PChar2IntMapSetList* self) {
1742
+ PChar2IntMapSetListNode* node;
1743
+ assert(self);
1744
+ assert(!PChar2IntMapSetListEmpty(self));
1745
+ node = self->head_node;
1746
+ PChar2IntMapEntryDtor(node->element);
1747
+ self->head_node = self->head_node->next_node;
1748
+ --self->node_count;
1749
+ free(node);
1750
+ }
1751
+ static void PChar2IntMapSetListAdd(PChar2IntMapSetList* self, PChar2IntMapEntry element) {
1752
+ PChar2IntMapSetListNode* node;
1753
+ assert(self);
1754
+ node = (PChar2IntMapSetListNode*)malloc(sizeof(PChar2IntMapSetListNode)); assert(node);
1755
+ node->element = PChar2IntMapEntryAssign(element);
1756
+ node->next_node = self->head_node;
1757
+ self->head_node = node;
1758
+ ++self->node_count;
1759
+ }
1760
+ static int PChar2IntMapSetListContains(PChar2IntMapSetList* self, PChar2IntMapEntry what) {
1761
+ PChar2IntMapSetListNode* node;
1762
+ assert(self);
1763
+ what = PChar2IntMapEntryAssign(what);
1764
+ node = self->head_node;
1765
+ while(node) {
1766
+ if(PChar2IntMapEntryEqual(node->element,what)) {
1767
+ PChar2IntMapEntryDtor(what);
1768
+ return 1;
1769
+ }
1770
+ node = node->next_node;
1771
+ }
1772
+ PChar2IntMapEntryDtor(what);
1773
+ return 0;
1774
+ }
1775
+ static PChar2IntMapEntry PChar2IntMapSetListFind(PChar2IntMapSetList* self, PChar2IntMapEntry what) {
1776
+ PChar2IntMapSetListNode* node;
1777
+ assert(self);
1778
+ what = PChar2IntMapEntryAssign(what);
1779
+ assert(PChar2IntMapSetListContains(self, what));
1780
+ node = self->head_node;
1781
+ while(node) {
1782
+ if(PChar2IntMapEntryEqual(node->element,what)) {
1783
+ PChar2IntMapEntryDtor(what);
1784
+ return node->element;
1785
+ }
1786
+ node = node->next_node;
1787
+ }
1788
+ abort();
1789
+ }
1790
+ static int PChar2IntMapSetListReplace(PChar2IntMapSetList* self, PChar2IntMapEntry what, PChar2IntMapEntry with) {
1791
+ PChar2IntMapSetListNode* node;
1792
+ assert(self);
1793
+ what = PChar2IntMapEntryAssign(what);
1794
+ with = PChar2IntMapEntryAssign(with);
1795
+ node = self->head_node;
1796
+ while(node) {
1797
+ if(PChar2IntMapEntryEqual(node->element,what)) {
1798
+ PChar2IntMapEntryDtor(node->element);
1799
+ node->element = PChar2IntMapEntryAssign(with);
1800
+ PChar2IntMapEntryDtor(what);
1801
+ PChar2IntMapEntryDtor(with);
1802
+ return 1;
1803
+ }
1804
+ node = node->next_node;
1805
+ }
1806
+ PChar2IntMapEntryDtor(what);
1807
+ PChar2IntMapEntryDtor(with);
1808
+ return 0;
1809
+ }
1810
+ static int PChar2IntMapSetListReplaceAll(PChar2IntMapSetList* self, PChar2IntMapEntry what, PChar2IntMapEntry with) {
1811
+ PChar2IntMapSetListNode* node;
1812
+ int count = 0;
1813
+ assert(self);
1814
+ what = PChar2IntMapEntryAssign(what);
1815
+ with = PChar2IntMapEntryAssign(with);
1816
+ node = self->head_node;
1817
+ while(node) {
1818
+ if(PChar2IntMapEntryEqual(node->element,what)) {
1819
+ PChar2IntMapEntryDtor(node->element);
1820
+ node->element = PChar2IntMapEntryAssign(with);
1821
+ ++count;
1822
+ }
1823
+ node = node->next_node;
1824
+ }
1825
+ PChar2IntMapEntryDtor(what);
1826
+ PChar2IntMapEntryDtor(with);
1827
+ return count;
1828
+ }
1829
+ static int PChar2IntMapSetListRemove(PChar2IntMapSetList* self, PChar2IntMapEntry what) {
1830
+ PChar2IntMapSetListNode* node;
1831
+ PChar2IntMapSetListNode* prev_node;
1832
+ int found = 0;
1833
+ assert(self);
1834
+ what = PChar2IntMapEntryAssign(what);
1835
+ node = self->head_node;
1836
+ prev_node = NULL;
1837
+ while(node) {
1838
+ if(PChar2IntMapEntryEqual(node->element,what)) {
1839
+ PChar2IntMapEntryDtor(node->element);
1840
+ if(prev_node) {
1841
+ prev_node->next_node = node->next_node ? node->next_node : NULL;
1842
+ } else {
1843
+ self->head_node = node->next_node ? node->next_node : NULL;
1844
+ }
1845
+ --self->node_count;
1846
+ free(node);
1847
+ found = 1;
1848
+ break;
1849
+ }
1850
+ prev_node = node;
1851
+ node = node->next_node;
1852
+ }
1853
+ PChar2IntMapEntryDtor(what);
1854
+ return found;
1855
+ }
1856
+ static int PChar2IntMapSetListRemoveAll(PChar2IntMapSetList* self, PChar2IntMapEntry what) {
1857
+ PChar2IntMapSetListNode* node;
1858
+ PChar2IntMapSetListNode* prev_node;
1859
+ int count = 0;
1860
+ assert(self);
1861
+ what = PChar2IntMapEntryAssign(what);
1862
+ node = self->head_node;
1863
+ prev_node = NULL;
1864
+ while(node) {
1865
+ if(PChar2IntMapEntryEqual(node->element,what)) {
1866
+ PChar2IntMapEntryDtor(node->element);
1867
+ if(prev_node) {
1868
+ prev_node->next_node = node->next_node ? node->next_node : NULL;
1869
+ } else {
1870
+ self->head_node = node->next_node ? node->next_node : NULL;
1871
+ }
1872
+ --self->node_count;
1873
+ free(node);
1874
+ ++count;
1875
+ }
1876
+ prev_node = node;
1877
+ node = node->next_node;
1878
+ }
1879
+ PChar2IntMapEntryDtor(what);
1880
+ return count;
1881
+ }
1882
+ static size_t PChar2IntMapSetListSize(PChar2IntMapSetList* self) {
1883
+ assert(self);
1884
+ return self->node_count;
1885
+ }
1886
+ static int PChar2IntMapSetListEmpty(PChar2IntMapSetList* self) {
1887
+ assert(self);
1888
+ return !self->node_count;
1889
+ }
1890
+ static void PChar2IntMapSetListItCtor(PChar2IntMapSetListIt* self, PChar2IntMapSetList* list) {
1891
+ assert(self);
1892
+ assert(list);
1893
+ self->next_node = list->head_node;
1894
+ }
1895
+ static int PChar2IntMapSetListItHasNext(PChar2IntMapSetListIt* self) {
1896
+ assert(self);
1897
+ return self->next_node != NULL;
1898
+ }
1899
+ static PChar2IntMapEntry PChar2IntMapSetListItNext(PChar2IntMapSetListIt* self) {
1900
+ PChar2IntMapSetListNode* node;
1901
+ assert(self);
1902
+ node = self->next_node;
1903
+ self->next_node = self->next_node->next_node;
1904
+ return node->element;
1905
+ }
1906
+
1907
+ static void PChar2IntMapSetCtor(PChar2IntMapSet* self) {
1908
+ assert(self);
1909
+ self->min_bucket_count = 16;
1910
+ self->min_fill = 20;
1911
+ self->max_fill = 80;
1912
+ self->min_size = (size_t)((float)self->min_fill/100*self->min_bucket_count);
1913
+ self->max_size = (size_t)((float)self->max_fill/100*self->min_bucket_count);
1914
+ self->capacity_multiplier = 200;
1915
+ self->buckets = NULL;
1916
+ PChar2IntMapSetRehash(self);
1917
+ }
1918
+ static void PChar2IntMapSetDtor(PChar2IntMapSet* self) {
1919
+ size_t i;
1920
+ assert(self);
1921
+ for(i = 0; i < self->bucket_count; ++i) {
1922
+ PChar2IntMapSetListDtor(&self->buckets[i]);
1923
+ }
1924
+ free(self->buckets);
1925
+ }
1926
+ static PChar2IntMapSet* PChar2IntMapSetNew(void) {
1927
+ PChar2IntMapSet* self = (PChar2IntMapSet*)malloc(sizeof(PChar2IntMapSet)); assert(self);
1928
+ PChar2IntMapSetCtor(self);
1929
+ self->ref_count = 0;
1930
+ return self;
1931
+ }
1932
+ static void PChar2IntMapSetDestroy(PChar2IntMapSet* self) {
1933
+ assert(self);
1934
+ if(!--self->ref_count) {
1935
+ PChar2IntMapSetDtor(self);
1936
+ free(self);
1937
+ }
1938
+ }
1939
+ static PChar2IntMapSet* PChar2IntMapSetAssign(PChar2IntMapSet* self) {
1940
+ ++self->ref_count;
1941
+ return self;
1942
+ }
1943
+ static void PChar2IntMapSetPurge(PChar2IntMapSet* self) {
1944
+ assert(self);
1945
+ PChar2IntMapSetDtor(self);
1946
+ self->buckets = NULL;
1947
+ PChar2IntMapSetRehash(self);
1948
+ }
1949
+ static void PChar2IntMapSetRehash(PChar2IntMapSet* self) {
1950
+ PChar2IntMapSetList* buckets;
1951
+ size_t i, bucket_count, size, fill;
1952
+ assert(self);
1953
+ assert(self->min_fill > 0);
1954
+ assert(self->max_fill > 0);
1955
+ assert(self->min_fill < self->max_fill);
1956
+ assert(self->min_bucket_count > 0);
1957
+ if(self->buckets) {
1958
+ if(self->min_size < self->size && self->size < self->max_size) return;
1959
+ fill = (size_t)((float)self->size/self->bucket_count*100);
1960
+ if(fill > self->max_fill) {
1961
+ bucket_count = (size_t)((float)self->bucket_count/100*self->capacity_multiplier);
1962
+ } else
1963
+ if(fill < self->min_fill && self->bucket_count > self->min_bucket_count) {
1964
+ bucket_count = (size_t)((float)self->bucket_count/self->capacity_multiplier*100);
1965
+ if(bucket_count < self->min_bucket_count) bucket_count = self->min_bucket_count;
1966
+ } else
1967
+ return;
1968
+ size = self->size;
1969
+ self->min_size = (size_t)((float)self->min_fill/100*size);
1970
+ self->max_size = (size_t)((float)self->max_fill/100*size);
1971
+ } else {
1972
+ bucket_count = self->min_bucket_count;
1973
+ size = 0;
1974
+ }
1975
+ buckets = (PChar2IntMapSetList*)malloc(bucket_count*sizeof(PChar2IntMapSetList)); assert(buckets);
1976
+ for(i = 0; i < bucket_count; ++i) {
1977
+ PChar2IntMapSetListCtor(&buckets[i]);
1978
+ }
1979
+ if(self->buckets) {
1980
+ PChar2IntMapSetIt it;
1981
+ PChar2IntMapSetItCtor(&it, self);
1982
+ while(PChar2IntMapSetItHasNext(&it)) {
1983
+ PChar2IntMapSetList* bucket;
1984
+ PChar2IntMapEntry element = PChar2IntMapSetItNext(&it);
1985
+ bucket = &buckets[PChar2IntMapEntryHash(element) % bucket_count];
1986
+ PChar2IntMapSetListAdd(bucket, element);
1987
+ }
1988
+ PChar2IntMapSetDtor(self);
1989
+ }
1990
+ self->buckets = buckets;
1991
+ self->bucket_count = bucket_count;
1992
+ self->size = size;
1993
+ }
1994
+ static int PChar2IntMapSetContains(PChar2IntMapSet* self, PChar2IntMapEntry element) {
1995
+ int result;
1996
+ assert(self);
1997
+ element = PChar2IntMapEntryAssign(element);
1998
+ result = PChar2IntMapSetListContains(&self->buckets[PChar2IntMapEntryHash(element) % self->bucket_count], element);
1999
+ PChar2IntMapEntryDtor(element);
2000
+ return result;
2001
+ }
2002
+ static PChar2IntMapEntry PChar2IntMapSetGet(PChar2IntMapSet* self, PChar2IntMapEntry element) {
2003
+ PChar2IntMapEntry result;
2004
+ assert(self);
2005
+ element = PChar2IntMapEntryAssign(element);
2006
+ assert(PChar2IntMapSetContains(self, element));
2007
+ result = PChar2IntMapSetListFind(&self->buckets[PChar2IntMapEntryHash(element) % self->bucket_count], element);
2008
+ PChar2IntMapEntryDtor(element);
2009
+ return result;
2010
+ }
2011
+ static size_t PChar2IntMapSetSize(PChar2IntMapSet* self) {
2012
+ assert(self);
2013
+ return self->size;
2014
+ }
2015
+ static int PChar2IntMapSetEmpty(PChar2IntMapSet* self) {
2016
+ assert(self);
2017
+ return !self->size;
2018
+ }
2019
+ static int PChar2IntMapSetPut(PChar2IntMapSet* self, PChar2IntMapEntry element) {
2020
+ int contained = 1;
2021
+ PChar2IntMapSetList* bucket;
2022
+ assert(self);
2023
+ element = PChar2IntMapEntryAssign(element);
2024
+ bucket = &self->buckets[PChar2IntMapEntryHash(element) % self->bucket_count];
2025
+ if(!PChar2IntMapSetListContains(bucket, element)) {
2026
+ PChar2IntMapSetListAdd(bucket, element);
2027
+ ++self->size;
2028
+ contained = 0;
2029
+ PChar2IntMapSetRehash(self);
2030
+ }
2031
+ PChar2IntMapEntryDtor(element);
2032
+ return contained;
2033
+ }
2034
+ static void PChar2IntMapSetReplace(PChar2IntMapSet* self, PChar2IntMapEntry element) {
2035
+ PChar2IntMapSetList* bucket;
2036
+ assert(self);
2037
+ element = PChar2IntMapEntryAssign(element);
2038
+ bucket = &self->buckets[PChar2IntMapEntryHash(element) % self->bucket_count];
2039
+ if(!PChar2IntMapSetListReplace(bucket, element, element)) {
2040
+ PChar2IntMapSetListAdd(bucket, element);
2041
+ ++self->size;
2042
+ PChar2IntMapSetRehash(self);
2043
+ }
2044
+ PChar2IntMapEntryDtor(element);
2045
+ }
2046
+ static int PChar2IntMapSetRemove(PChar2IntMapSet* self, PChar2IntMapEntry what) {
2047
+ int removed = 0;
2048
+ PChar2IntMapSetList* bucket;
2049
+ assert(self);
2050
+ what = PChar2IntMapEntryAssign(what);
2051
+ bucket = &self->buckets[PChar2IntMapEntryHash(what) % self->bucket_count];
2052
+ if(PChar2IntMapSetListRemove(bucket, what)) {
2053
+ --self->size;
2054
+ removed = 1;
2055
+ PChar2IntMapSetRehash(self);
2056
+ }
2057
+ PChar2IntMapEntryDtor(what);
2058
+ return removed;
2059
+ }
2060
+ static void PChar2IntMapSetNot(PChar2IntMapSet* self, PChar2IntMapSet* other) {
2061
+ PChar2IntMapSetIt it;
2062
+ assert(self);
2063
+ assert(other);
2064
+ PChar2IntMapSetItCtor(&it, other);
2065
+ while(PChar2IntMapSetItHasNext(&it)) {
2066
+ PChar2IntMapSetRemove(self, PChar2IntMapSetItNext(&it));
2067
+ }
2068
+ PChar2IntMapSetRehash(self);
2069
+ }
2070
+ static void PChar2IntMapSetOr(PChar2IntMapSet* self, PChar2IntMapSet* other) {
2071
+ PChar2IntMapSetIt it;
2072
+ assert(self);
2073
+ assert(other);
2074
+ PChar2IntMapSetItCtor(&it, other);
2075
+ while(PChar2IntMapSetItHasNext(&it)) {
2076
+ PChar2IntMapSetPut(self, PChar2IntMapSetItNext(&it));
2077
+ }
2078
+ PChar2IntMapSetRehash(self);
2079
+ }
2080
+ static void PChar2IntMapSetAnd(PChar2IntMapSet* self, PChar2IntMapSet* other) {
2081
+ PChar2IntMapSetIt it;
2082
+ PChar2IntMapSet set;
2083
+ assert(self);
2084
+ assert(other);
2085
+ PChar2IntMapSetCtor(&set);
2086
+ PChar2IntMapSetItCtor(&it, self);
2087
+ while(PChar2IntMapSetItHasNext(&it)) {
2088
+ PChar2IntMapEntry element = PChar2IntMapSetItNext(&it);
2089
+ if(PChar2IntMapSetContains(other, element)) PChar2IntMapSetPut(&set, element);
2090
+ }
2091
+ PChar2IntMapSetItCtor(&it, other);
2092
+ while(PChar2IntMapSetItHasNext(&it)) {
2093
+ PChar2IntMapEntry element = PChar2IntMapSetItNext(&it);
2094
+ if(PChar2IntMapSetContains(self, element)) PChar2IntMapSetPut(&set, element);
2095
+ }
2096
+ PChar2IntMapSetDtor(self);
2097
+ self->buckets = set.buckets;
2098
+ self->size = set.size;
2099
+ PChar2IntMapSetRehash(self);
2100
+ /*PChar2IntMapSetDtor(&set);*/
2101
+ }
2102
+ static void PChar2IntMapSetXor(PChar2IntMapSet* self, PChar2IntMapSet* other) {
2103
+ PChar2IntMapSetIt it;
2104
+ PChar2IntMapSet set;
2105
+ assert(self);
2106
+ assert(other);
2107
+ PChar2IntMapSetCtor(&set);
2108
+ PChar2IntMapSetItCtor(&it, self);
2109
+ while(PChar2IntMapSetItHasNext(&it)) {
2110
+ PChar2IntMapEntry element = PChar2IntMapSetItNext(&it);
2111
+ if(!PChar2IntMapSetContains(other, element)) PChar2IntMapSetPut(&set, element);
2112
+ }
2113
+ PChar2IntMapSetItCtor(&it, other);
2114
+ while(PChar2IntMapSetItHasNext(&it)) {
2115
+ PChar2IntMapEntry element = PChar2IntMapSetItNext(&it);
2116
+ if(!PChar2IntMapSetContains(self, element)) PChar2IntMapSetPut(&set, element);
2117
+ }
2118
+ PChar2IntMapSetDtor(self);
2119
+ self->buckets = set.buckets;
2120
+ self->size = set.size;
2121
+ PChar2IntMapSetRehash(self);
2122
+ /*PChar2IntMapSetDtor(&set);*/
2123
+ }
2124
+ static void PChar2IntMapSetItCtor(PChar2IntMapSetIt* self, PChar2IntMapSet* set) {
2125
+ assert(self);
2126
+ self->set = set;
2127
+ self->bucket_index = 0;
2128
+ PChar2IntMapSetListItCtor(&self->it, &set->buckets[0]);
2129
+ }
2130
+ static int PChar2IntMapSetItHasNext(PChar2IntMapSetIt* self) {
2131
+ assert(self);
2132
+ if(PChar2IntMapSetListItHasNext(&self->it)) {
2133
+ return 1;
2134
+ } else {
2135
+ size_t i; for(i = self->bucket_index+1; i < self->set->bucket_count; ++i) {
2136
+ if(!PChar2IntMapSetListEmpty(&self->set->buckets[i])) {
2137
+ return 1;
2138
+ }
2139
+ }
2140
+ return 0;
2141
+ }
2142
+ }
2143
+ static PChar2IntMapEntry PChar2IntMapSetItNext(PChar2IntMapSetIt* self) {
2144
+ assert(self);
2145
+ assert(PChar2IntMapSetItHasNext(self));
2146
+ if(PChar2IntMapSetListItHasNext(&self->it)) {
2147
+ return PChar2IntMapSetListItNext(&self->it);
2148
+ } else {
2149
+ size_t i; for(i = self->bucket_index+1; i < self->set->bucket_count; ++i) {
2150
+ if(!PChar2IntMapSetListEmpty(&self->set->buckets[i])) {
2151
+ PChar2IntMapSetListItCtor(&self->it, &self->set->buckets[i]);
2152
+ self->bucket_index = i;
2153
+ return PChar2IntMapSetListItNext(&self->it);
2154
+ }
2155
+ }
2156
+ abort();
2157
+ }
2158
+ }
2159
+
2160
+ void PChar2IntMapCtor(PChar2IntMap* self) {
2161
+ assert(self);
2162
+ PChar2IntMapSetCtor(&self->entries);
2163
+ }
2164
+ void PChar2IntMapDtor(PChar2IntMap* self) {
2165
+ assert(self);
2166
+ PChar2IntMapSetDtor(&self->entries);
2167
+ }
2168
+ PChar2IntMap* PChar2IntMapNew(void) {
2169
+ PChar2IntMap* self = (PChar2IntMap*)malloc(sizeof(PChar2IntMap)); assert(self);
2170
+ PChar2IntMapCtor(self);
2171
+ self->ref_count = 0;
2172
+ return self;
2173
+ }
2174
+ void PChar2IntMapDestroy(PChar2IntMap* self) {
2175
+ assert(self);
2176
+ if(!--self->ref_count) {
2177
+ PChar2IntMapDtor(self);
2178
+ free(self);
2179
+ }
2180
+ }
2181
+ void PChar2IntMapRehash(PChar2IntMap* self) {
2182
+ assert(self);
2183
+ PChar2IntMapSetRehash(&self->entries);
2184
+ }
2185
+ PChar2IntMap* PChar2IntMapAssign(PChar2IntMap* self) {
2186
+ assert(self);
2187
+ ++self->ref_count;
2188
+ return self;
2189
+ }
2190
+ void PChar2IntMapPurge(PChar2IntMap* self) {
2191
+ assert(self);
2192
+ PChar2IntMapSetPurge(&self->entries);
2193
+ }
2194
+ size_t PChar2IntMapSize(PChar2IntMap* self) {
2195
+ assert(self);
2196
+ return PChar2IntMapSetSize(&self->entries);
2197
+ }
2198
+ int PChar2IntMapEmpty(PChar2IntMap* self) {
2199
+ assert(self);
2200
+ return PChar2IntMapSetEmpty(&self->entries);
2201
+ }
2202
+ int PChar2IntMapContainsKey(PChar2IntMap* self, const char* key) {
2203
+ int result;
2204
+ PChar2IntMapEntry entry;
2205
+ assert(self);
2206
+ entry = PChar2IntMapEntryAssign(PChar2IntMapEntryKeyOnly(key));
2207
+ result = PChar2IntMapSetContains(&self->entries, entry);
2208
+ PChar2IntMapEntryDtor(entry);
2209
+ return result;
2210
+ }
2211
+ int PChar2IntMapGet(PChar2IntMap* self, const char* key) {
2212
+ int result;
2213
+ PChar2IntMapEntry entry;
2214
+ assert(self);
2215
+ entry = PChar2IntMapEntryAssign(PChar2IntMapEntryKeyOnly(key));
2216
+ assert(PChar2IntMapContainsKey(self, key));
2217
+ result = PChar2IntMapSetGet(&self->entries, entry).value;
2218
+ PChar2IntMapEntryDtor(entry);
2219
+ return result;
2220
+ }
2221
+ int PChar2IntMapPut(PChar2IntMap* self, const char* key, int value) {
2222
+ PChar2IntMapEntry entry = PChar2IntMapEntryAssign(PChar2IntMapEntryKeyValue(key,value));
2223
+ assert(self);
2224
+ if(!PChar2IntMapContainsKey(self, key)) {
2225
+ PChar2IntMapSetPut(&self->entries, entry);
2226
+ PChar2IntMapEntryDtor(entry);
2227
+ return 1;
2228
+ } else {
2229
+ PChar2IntMapEntryDtor(entry);
2230
+ return 0;
2231
+ }
2232
+ }
2233
+ void PChar2IntMapReplace(PChar2IntMap* self, const char* key, int value) {
2234
+ PChar2IntMapEntry entry;
2235
+ assert(self);
2236
+ entry = PChar2IntMapEntryAssign(PChar2IntMapEntryKeyValue(key,value));
2237
+ PChar2IntMapSetReplace(&self->entries, entry);
2238
+ PChar2IntMapEntryDtor(entry);
2239
+ }
2240
+ int PChar2IntMapRemove(PChar2IntMap* self, const char* key) {
2241
+ int removed;
2242
+ PChar2IntMapEntry entry;
2243
+ assert(self);
2244
+ entry = PChar2IntMapEntryAssign(PChar2IntMapEntryKeyOnly(key));
2245
+ removed = PChar2IntMapSetRemove(&self->entries, entry);
2246
+ PChar2IntMapEntryDtor(entry);
2247
+ return removed;
2248
+ }
2249
+ void PChar2IntMapItCtor(PChar2IntMapIt* self, PChar2IntMap* map) {
2250
+ assert(self);
2251
+ assert(map);
2252
+ PChar2IntMapSetItCtor(&self->it, &map->entries);
2253
+ }
2254
+ int PChar2IntMapItHasNext(PChar2IntMapIt* self) {
2255
+ assert(self);
2256
+ return PChar2IntMapSetItHasNext(&self->it);
2257
+ }
2258
+ const char* PChar2IntMapItNextKey(PChar2IntMapIt* self) {
2259
+ assert(self);
2260
+ return PChar2IntMapSetItNext(&self->it).key;
2261
+ }
2262
+ int PChar2IntMapItNextValue(PChar2IntMapIt* self) {
2263
+ assert(self);
2264
+ return PChar2IntMapSetItNext(&self->it).value;
2265
+ }
2266
+ PChar2IntMapEntry PChar2IntMapItNext(PChar2IntMapIt* self) {
2267
+ assert(self);
2268
+ return PChar2IntMapSetItNext(&self->it);
2269
+ }
2270
+
2271
+ void PCharQueueCtor(PCharQueue* self) {
2272
+ assert(self);
2273
+ self->head_node = self->tail_node = NULL;
2274
+ self->node_count = 0;
2275
+ }
2276
+ void PCharQueueDtor(PCharQueue* self) {
2277
+ PCharQueueNode* node;
2278
+ assert(self);
2279
+ ;
2280
+ node = self->head_node;
2281
+ while(node) {
2282
+ PCharQueueNode* this_node = node;
2283
+ node = node->next_node;
2284
+ free(this_node);
2285
+ }
2286
+ }
2287
+ PCharQueue* PCharQueueNew(void) {
2288
+ PCharQueue* self = (PCharQueue*)malloc(sizeof(PCharQueue)); assert(self);
2289
+ PCharQueueCtor(self);
2290
+ self->ref_count = 0;
2291
+ return self;
2292
+ }
2293
+ void PCharQueueDestroy(PCharQueue* self) {
2294
+ assert(self);
2295
+ if(!--self->ref_count) {
2296
+ PCharQueueDtor(self);
2297
+ free(self);
2298
+ }
2299
+ }
2300
+ PCharQueue* PCharQueueAssign(PCharQueue* self) {
2301
+ ++self->ref_count;
2302
+ return self;
2303
+ }
2304
+ void PCharQueuePurge(PCharQueue* self) {
2305
+ PCharQueueDtor(self);
2306
+ PCharQueueCtor(self);
2307
+ }
2308
+ const char* PCharQueueHead(PCharQueue* self) {
2309
+ assert(self);
2310
+ assert(!PCharQueueEmpty(self));
2311
+ return self->head_node->element;
2312
+ }
2313
+ const char* PCharQueueTail(PCharQueue* self) {
2314
+ assert(self);
2315
+ assert(!PCharQueueEmpty(self));
2316
+ return self->tail_node->element;
2317
+ }
2318
+ void PCharQueueChopHead(PCharQueue* self) {
2319
+ PCharQueueNode* node;
2320
+ assert(self);
2321
+ assert(!PCharQueueEmpty(self));
2322
+ node = self->head_node;
2323
+ ;
2324
+ self->head_node = self->head_node->next_node;
2325
+ self->head_node->prev_node = NULL;
2326
+ --self->node_count;
2327
+ free(node);
2328
+ }
2329
+ void PCharQueueChopTail(PCharQueue* self) {
2330
+ PCharQueueNode* node;
2331
+ assert(self);
2332
+ assert(!PCharQueueEmpty(self));
2333
+ node = self->tail_node;
2334
+ ;
2335
+ self->tail_node = self->tail_node->prev_node;
2336
+ self->tail_node->next_node = NULL;
2337
+ --self->node_count;
2338
+ free(node);
2339
+ }
2340
+ void PCharQueueAppend(PCharQueue* self, const char* element) {
2341
+ PCharQueueNode* node;
2342
+ assert(self);
2343
+ node = (PCharQueueNode*)malloc(sizeof(PCharQueueNode)); assert(node);
2344
+ node->element = (element);
2345
+ if(PCharQueueEmpty(self)) {
2346
+ node->prev_node = node->next_node = NULL;
2347
+ self->tail_node = self->head_node = node;
2348
+ } else {
2349
+ node->next_node = NULL;
2350
+ node->prev_node = self->tail_node;
2351
+ self->tail_node->next_node = node;
2352
+ self->tail_node = node;
2353
+ }
2354
+ ++self->node_count;
2355
+ }
2356
+ void PCharQueuePrepend(PCharQueue* self, const char* element) {
2357
+ PCharQueueNode* node;
2358
+ assert(self);
2359
+ node = (PCharQueueNode*)malloc(sizeof(PCharQueueNode)); assert(node);
2360
+ node->element = (element);
2361
+ if(PCharQueueEmpty(self)) {
2362
+ node->prev_node = node->next_node = NULL;
2363
+ self->tail_node = self->head_node = node;
2364
+ } else {
2365
+ node->prev_node = NULL;
2366
+ node->next_node = self->head_node;
2367
+ self->head_node->prev_node = node;
2368
+ self->head_node = node;
2369
+ }
2370
+ ++self->node_count;
2371
+ }
2372
+ int PCharQueueContains(PCharQueue* self, const char* what) {
2373
+ PCharQueueNode* node;
2374
+ assert(self);
2375
+ what = (what);
2376
+ node = self->head_node;
2377
+ while(node) {
2378
+ if(PCharEqual(node->element,what)) {
2379
+ ;
2380
+ return 1;
2381
+ }
2382
+ node = node->next_node;
2383
+ }
2384
+ ;
2385
+ return 0;
2386
+ }
2387
+ const char* PCharQueueFind(PCharQueue* self, const char* what) {
2388
+ PCharQueueNode* node;
2389
+ assert(self);
2390
+ what = (what);
2391
+ assert(PCharQueueContains(self, what));
2392
+ node = self->head_node;
2393
+ while(node) {
2394
+ if(PCharEqual(node->element,what)) {
2395
+ ;
2396
+ return node->element;
2397
+ }
2398
+ node = node->next_node;
2399
+ }
2400
+ abort();
2401
+ }
2402
+ int PCharQueueReplace(PCharQueue* self, const char* what, const char* with) {
2403
+ PCharQueueNode* node;
2404
+ assert(self);
2405
+ what = (what);
2406
+ with = (with);
2407
+ node = self->head_node;
2408
+ while(node) {
2409
+ if(PCharEqual(node->element,what)) {
2410
+ ;
2411
+ node->element = (with);
2412
+ ;
2413
+ ;
2414
+ return 1;
2415
+ }
2416
+ node = node->next_node;
2417
+ }
2418
+ ;
2419
+ ;
2420
+ return 0;
2421
+ }
2422
+ int PCharQueueReplaceAll(PCharQueue* self, const char* what, const char* with) {
2423
+ PCharQueueNode* node;
2424
+ int count = 0;
2425
+ assert(self);
2426
+ what = (what);
2427
+ with = (with);
2428
+ node = self->head_node;
2429
+ while(node) {
2430
+ if(PCharEqual(node->element,what)) {
2431
+ ;
2432
+ node->element = (with);
2433
+ ++count;
2434
+ }
2435
+ node = node->next_node;
2436
+ }
2437
+ ;
2438
+ ;
2439
+ return count;
2440
+ }
2441
+ int PCharQueueRemove(PCharQueue* self, const char* what) {
2442
+ PCharQueueNode* node;
2443
+ int found = 0;
2444
+ assert(self);
2445
+ what = (what);
2446
+ node = self->head_node;
2447
+ while(node) {
2448
+ if(PCharEqual(node->element,what)) {
2449
+ PCharQueueNode *prev = node->prev_node, *next = node->next_node;
2450
+ ;
2451
+ if(prev && next) {
2452
+ prev->next_node = next;
2453
+ next->prev_node = prev;
2454
+ } else if(prev) {
2455
+ prev->next_node = NULL;
2456
+ self->tail_node = prev;
2457
+ } else {
2458
+ next->prev_node = NULL;
2459
+ self->head_node = next;
2460
+ }
2461
+ --self->node_count;
2462
+ free(node);
2463
+ found = 1;
2464
+ break;
2465
+ }
2466
+ node = node->next_node;
2467
+ }
2468
+ ;
2469
+ return found;
2470
+ }
2471
+ int PCharQueueRemoveAll(PCharQueue* self, const char* what) {
2472
+ PCharQueueNode* node;
2473
+ int count = 0;
2474
+ assert(self);
2475
+ what = (what);
2476
+ node = self->head_node;
2477
+ while(node) {
2478
+ if(PCharEqual(node->element,what)) {
2479
+ PCharQueueNode *prev = node->prev_node, *next = node->next_node;
2480
+ ;
2481
+ if(prev && next) {
2482
+ prev->next_node = next;
2483
+ next->prev_node = prev;
2484
+ } else if(prev) {
2485
+ prev->next_node = NULL;
2486
+ self->tail_node = prev;
2487
+ } else {
2488
+ next->prev_node = NULL;
2489
+ self->head_node = next;
2490
+ }
2491
+ --self->node_count;
2492
+ free(node);
2493
+ ++count;
2494
+ }
2495
+ node = node->next_node;
2496
+ }
2497
+ ;
2498
+ return count;
2499
+ }
2500
+ size_t PCharQueueSize(PCharQueue* self) {
2501
+ assert(self);
2502
+ return self->node_count;
2503
+ }
2504
+ int PCharQueueEmpty(PCharQueue* self) {
2505
+ assert(self);
2506
+ return !self->node_count;
2507
+ }
2508
+ void PCharQueueItCtor(PCharQueueIt* self, PCharQueue* list, int forward) {
2509
+ assert(self);
2510
+ assert(list);
2511
+ self->forward = forward;
2512
+ self->next_node = forward ? list->head_node : list->tail_node;
2513
+ }
2514
+ int PCharQueueItHasNext(PCharQueueIt* self) {
2515
+ assert(self);
2516
+ return self->next_node != NULL;
2517
+ }
2518
+ const char* PCharQueueItNext(PCharQueueIt* self) {
2519
+ PCharQueueNode* node;
2520
+ assert(self);
2521
+ node = self->next_node;
2522
+ self->next_node = self->forward ? self->next_node->next_node : self->next_node->prev_node;
2523
+ return node->element;
2524
+ }
2525
+
2526
+ void BoxListCtor(BoxList* self) {
2527
+ assert(self);
2528
+ self->head_node = NULL;
2529
+ self->node_count = 0;
2530
+ }
2531
+ void BoxListDtor(BoxList* self) {
2532
+ BoxListNode* node;
2533
+ assert(self);
2534
+ {
2535
+ BoxListIt it;
2536
+ BoxListItCtor(&it, self);
2537
+ while(BoxListItHasNext(&it)) {
2538
+ Box* e = BoxListItNext(&it);
2539
+ BoxDestroy(e);
2540
+ }
2541
+ };
2542
+ node = self->head_node;
2543
+ while(node) {
2544
+ BoxListNode* this_node = node;
2545
+ node = node->next_node;
2546
+ free(this_node);
2547
+ }
2548
+ }
2549
+ BoxList* BoxListNew(void) {
2550
+ BoxList* self = (BoxList*)malloc(sizeof(BoxList)); assert(self);
2551
+ BoxListCtor(self);
2552
+ self->ref_count = 0;
2553
+ return self;
2554
+ }
2555
+ void BoxListDestroy(BoxList* self) {
2556
+ assert(self);
2557
+ if(!--self->ref_count) {
2558
+ BoxListDtor(self);
2559
+ free(self);
2560
+ }
2561
+ }
2562
+ BoxList* BoxListAssign(BoxList* self) {
2563
+ ++self->ref_count;
2564
+ return self;
2565
+ }
2566
+ void BoxListPurge(BoxList* self) {
2567
+ BoxListDtor(self);
2568
+ BoxListCtor(self);
2569
+ }
2570
+ Box* BoxListGet(BoxList* self) {
2571
+ assert(self);
2572
+ assert(!BoxListEmpty(self));
2573
+ return self->head_node->element;
2574
+ }
2575
+ void BoxListChop(BoxList* self) {
2576
+ BoxListNode* node;
2577
+ assert(self);
2578
+ assert(!BoxListEmpty(self));
2579
+ node = self->head_node;
2580
+ BoxDestroy(node->element);
2581
+ self->head_node = self->head_node->next_node;
2582
+ --self->node_count;
2583
+ free(node);
2584
+ }
2585
+ void BoxListAdd(BoxList* self, Box* element) {
2586
+ BoxListNode* node;
2587
+ assert(self);
2588
+ node = (BoxListNode*)malloc(sizeof(BoxListNode)); assert(node);
2589
+ node->element = BoxAssign(element);
2590
+ node->next_node = self->head_node;
2591
+ self->head_node = node;
2592
+ ++self->node_count;
2593
+ }
2594
+ int BoxListContains(BoxList* self, Box* what) {
2595
+ BoxListNode* node;
2596
+ assert(self);
2597
+ what = BoxAssign(what);
2598
+ node = self->head_node;
2599
+ while(node) {
2600
+ if(BoxEqual(node->element,what)) {
2601
+ BoxDestroy(what);
2602
+ return 1;
2603
+ }
2604
+ node = node->next_node;
2605
+ }
2606
+ BoxDestroy(what);
2607
+ return 0;
2608
+ }
2609
+ Box* BoxListFind(BoxList* self, Box* what) {
2610
+ BoxListNode* node;
2611
+ assert(self);
2612
+ what = BoxAssign(what);
2613
+ assert(BoxListContains(self, what));
2614
+ node = self->head_node;
2615
+ while(node) {
2616
+ if(BoxEqual(node->element,what)) {
2617
+ BoxDestroy(what);
2618
+ return node->element;
2619
+ }
2620
+ node = node->next_node;
2621
+ }
2622
+ abort();
2623
+ }
2624
+ int BoxListReplace(BoxList* self, Box* what, Box* with) {
2625
+ BoxListNode* node;
2626
+ assert(self);
2627
+ what = BoxAssign(what);
2628
+ with = BoxAssign(with);
2629
+ node = self->head_node;
2630
+ while(node) {
2631
+ if(BoxEqual(node->element,what)) {
2632
+ BoxDestroy(node->element);
2633
+ node->element = BoxAssign(with);
2634
+ BoxDestroy(what);
2635
+ BoxDestroy(with);
2636
+ return 1;
2637
+ }
2638
+ node = node->next_node;
2639
+ }
2640
+ BoxDestroy(what);
2641
+ BoxDestroy(with);
2642
+ return 0;
2643
+ }
2644
+ int BoxListReplaceAll(BoxList* self, Box* what, Box* with) {
2645
+ BoxListNode* node;
2646
+ int count = 0;
2647
+ assert(self);
2648
+ what = BoxAssign(what);
2649
+ with = BoxAssign(with);
2650
+ node = self->head_node;
2651
+ while(node) {
2652
+ if(BoxEqual(node->element,what)) {
2653
+ BoxDestroy(node->element);
2654
+ node->element = BoxAssign(with);
2655
+ ++count;
2656
+ }
2657
+ node = node->next_node;
2658
+ }
2659
+ BoxDestroy(what);
2660
+ BoxDestroy(with);
2661
+ return count;
2662
+ }
2663
+ int BoxListRemove(BoxList* self, Box* what) {
2664
+ BoxListNode* node;
2665
+ BoxListNode* prev_node;
2666
+ int found = 0;
2667
+ assert(self);
2668
+ what = BoxAssign(what);
2669
+ node = self->head_node;
2670
+ prev_node = NULL;
2671
+ while(node) {
2672
+ if(BoxEqual(node->element,what)) {
2673
+ BoxDestroy(node->element);
2674
+ if(prev_node) {
2675
+ prev_node->next_node = node->next_node ? node->next_node : NULL;
2676
+ } else {
2677
+ self->head_node = node->next_node ? node->next_node : NULL;
2678
+ }
2679
+ --self->node_count;
2680
+ free(node);
2681
+ found = 1;
2682
+ break;
2683
+ }
2684
+ prev_node = node;
2685
+ node = node->next_node;
2686
+ }
2687
+ BoxDestroy(what);
2688
+ return found;
2689
+ }
2690
+ int BoxListRemoveAll(BoxList* self, Box* what) {
2691
+ BoxListNode* node;
2692
+ BoxListNode* prev_node;
2693
+ int count = 0;
2694
+ assert(self);
2695
+ what = BoxAssign(what);
2696
+ node = self->head_node;
2697
+ prev_node = NULL;
2698
+ while(node) {
2699
+ if(BoxEqual(node->element,what)) {
2700
+ BoxDestroy(node->element);
2701
+ if(prev_node) {
2702
+ prev_node->next_node = node->next_node ? node->next_node : NULL;
2703
+ } else {
2704
+ self->head_node = node->next_node ? node->next_node : NULL;
2705
+ }
2706
+ --self->node_count;
2707
+ free(node);
2708
+ ++count;
2709
+ }
2710
+ prev_node = node;
2711
+ node = node->next_node;
2712
+ }
2713
+ BoxDestroy(what);
2714
+ return count;
2715
+ }
2716
+ size_t BoxListSize(BoxList* self) {
2717
+ assert(self);
2718
+ return self->node_count;
2719
+ }
2720
+ int BoxListEmpty(BoxList* self) {
2721
+ assert(self);
2722
+ return !self->node_count;
2723
+ }
2724
+ void BoxListItCtor(BoxListIt* self, BoxList* list) {
2725
+ assert(self);
2726
+ assert(list);
2727
+ self->next_node = list->head_node;
2728
+ }
2729
+ int BoxListItHasNext(BoxListIt* self) {
2730
+ assert(self);
2731
+ return self->next_node != NULL;
2732
+ }
2733
+ Box* BoxListItNext(BoxListIt* self) {
2734
+ BoxListNode* node;
2735
+ assert(self);
2736
+ node = self->next_node;
2737
+ self->next_node = self->next_node->next_node;
2738
+ return node->element;
2739
+ }
2740
+
2741
+ void IntVectorCtor(IntVector* self, size_t element_count) {
2742
+ assert(self);
2743
+ assert(element_count > 0);
2744
+ self->element_count = element_count;
2745
+ self->values = (int*)calloc(element_count, sizeof(int)); assert(self->values);
2746
+ ;
2747
+ }
2748
+ void IntVectorDtor(IntVector* self) {
2749
+ assert(self);
2750
+ ;
2751
+ free(self->values);
2752
+ }
2753
+ IntVector* IntVectorNew(size_t element_count) {
2754
+ IntVector* self = (IntVector*)malloc(sizeof(IntVector)); assert(self);
2755
+ IntVectorCtor(self, element_count);
2756
+ self->ref_count = 0;
2757
+ return self;
2758
+ }
2759
+ void IntVectorDestroy(IntVector* self) {
2760
+ assert(self);
2761
+ if(!--self->ref_count) {
2762
+ IntVectorDtor(self);
2763
+ free(self);
2764
+ }
2765
+ }
2766
+ IntVector* IntVectorAssign(IntVector* self) {
2767
+ ++self->ref_count;
2768
+ return self;
2769
+ }
2770
+ void IntVectorResize(IntVector* self, size_t element_count) {
2771
+ assert(self);
2772
+ if(self->element_count != element_count) {
2773
+ size_t count;
2774
+ int* values = (int*)calloc(element_count, sizeof(int)); assert(values);
2775
+ if(self->element_count > element_count) {
2776
+ ;
2777
+ count = element_count;
2778
+ } else {
2779
+ ;
2780
+ count = self->element_count;
2781
+ }
2782
+ {
2783
+ size_t index;
2784
+ for(index = 0; index < count; ++index) {
2785
+ values[index] = self->values[index];
2786
+ }
2787
+ }
2788
+ free(self->values);
2789
+ self->element_count = element_count;
2790
+ self->values = values;
2791
+ }
2792
+ }
2793
+ int IntVectorWithin(IntVector* self, size_t index) {
2794
+ assert(self);
2795
+ return index < self->element_count;
2796
+ }
2797
+ void IntVectorItCtor(IntVectorIt* self, IntVector* vector) {
2798
+ assert(self);
2799
+ assert(vector);
2800
+ self->vector = vector;
2801
+ self->index = 0;
2802
+ }
2803
+ int IntVectorItHasNext(IntVectorIt* self) {
2804
+ assert(self);
2805
+ return self->index < IntVectorSize(self->vector);
2806
+ }
2807
+ int IntVectorItNext(IntVectorIt* self) {
2808
+ assert(self);
2809
+ return IntVectorGet(self->vector, self->index++);
2810
+ }
2811
+
2812
+ static int IntVectorComparator(const void* lp, const void* rp) {
2813
+ return (*(int*)lp > *(int*)rp ? +1 : (*(int*)lp < *(int*)rp ? -1 : 0));
2814
+ }
2815
+ void IntVectorSort(IntVector* self) {
2816
+ assert(self);
2817
+ qsort(self->values, self->element_count, sizeof(int), IntVectorComparator);
2818
+ }
2819
+
2820
+ void BoxVectorCtor(BoxVector* self, size_t element_count) {
2821
+ assert(self);
2822
+ assert(element_count > 0);
2823
+ self->element_count = element_count;
2824
+ self->values = (Box**)calloc(element_count, sizeof(Box*)); assert(self->values);
2825
+ {
2826
+ size_t index;
2827
+ for(index = 0; index <= self->element_count-1; ++index) self->values[index] = BoxAssign(BoxNew());
2828
+ };
2829
+ }
2830
+ void BoxVectorDtor(BoxVector* self) {
2831
+ assert(self);
2832
+ {
2833
+ size_t index;
2834
+ for(index = 0; index <= self->element_count-1; ++index) BoxDestroy(self->values[index]);
2835
+ };
2836
+ free(self->values);
2837
+ }
2838
+ BoxVector* BoxVectorNew(size_t element_count) {
2839
+ BoxVector* self = (BoxVector*)malloc(sizeof(BoxVector)); assert(self);
2840
+ BoxVectorCtor(self, element_count);
2841
+ self->ref_count = 0;
2842
+ return self;
2843
+ }
2844
+ void BoxVectorDestroy(BoxVector* self) {
2845
+ assert(self);
2846
+ if(!--self->ref_count) {
2847
+ BoxVectorDtor(self);
2848
+ free(self);
2849
+ }
2850
+ }
2851
+ BoxVector* BoxVectorAssign(BoxVector* self) {
2852
+ ++self->ref_count;
2853
+ return self;
2854
+ }
2855
+ void BoxVectorResize(BoxVector* self, size_t element_count) {
2856
+ assert(self);
2857
+ if(self->element_count != element_count) {
2858
+ size_t count;
2859
+ Box** values = (Box**)calloc(element_count, sizeof(Box*)); assert(values);
2860
+ if(self->element_count > element_count) {
2861
+ {
2862
+ size_t index;
2863
+ for(index = element_count; index <= self->element_count-1; ++index) BoxDestroy(self->values[index]);
2864
+ };
2865
+ count = element_count;
2866
+ } else {
2867
+ {
2868
+ size_t index;
2869
+ for(index = self->element_count; index <= element_count-1; ++index) values[index] = BoxAssign(BoxNew());
2870
+ };
2871
+ count = self->element_count;
2872
+ }
2873
+ {
2874
+ size_t index;
2875
+ for(index = 0; index < count; ++index) {
2876
+ values[index] = self->values[index];
2877
+ }
2878
+ }
2879
+ free(self->values);
2880
+ self->element_count = element_count;
2881
+ self->values = values;
2882
+ }
2883
+ }
2884
+ int BoxVectorWithin(BoxVector* self, size_t index) {
2885
+ assert(self);
2886
+ return index < self->element_count;
2887
+ }
2888
+ void BoxVectorItCtor(BoxVectorIt* self, BoxVector* vector) {
2889
+ assert(self);
2890
+ assert(vector);
2891
+ self->vector = vector;
2892
+ self->index = 0;
2893
+ }
2894
+ int BoxVectorItHasNext(BoxVectorIt* self) {
2895
+ assert(self);
2896
+ return self->index < BoxVectorSize(self->vector);
2897
+ }
2898
+ Box* BoxVectorItNext(BoxVectorIt* self) {
2899
+ assert(self);
2900
+ return BoxVectorGet(self->vector, self->index++);
2901
+ }
2902
+
2903
+ static void BoxSetListCtor(BoxSetList*);
2904
+ static void BoxSetListDtor(BoxSetList*);
2905
+ static void BoxSetListPurge(BoxSetList*);
2906
+ static BoxSetList* BoxSetListNew(void);
2907
+ static void BoxSetListDestroy(BoxSetList*);
2908
+ static BoxSetList* BoxSetListAssign(BoxSetList*);
2909
+ static Box* BoxSetListGet(BoxSetList*);
2910
+ static void BoxSetListAdd(BoxSetList*, Box*);
2911
+ static void BoxSetListChop(BoxSetList*);
2912
+ static int BoxSetListContains(BoxSetList*, Box*);
2913
+ static Box* BoxSetListFind(BoxSetList*, Box*);
2914
+ static int BoxSetListReplace(BoxSetList*, Box*, Box*);
2915
+ static int BoxSetListReplaceAll(BoxSetList*, Box*, Box*);
2916
+ static int BoxSetListRemove(BoxSetList*, Box*);
2917
+ static int BoxSetListRemoveAll(BoxSetList*, Box*);
2918
+ static size_t BoxSetListSize(BoxSetList*);
2919
+ static int BoxSetListEmpty(BoxSetList*);
2920
+ static void BoxSetListItCtor(BoxSetListIt*, BoxSetList*);
2921
+ static int BoxSetListItHasNext(BoxSetListIt*);
2922
+ static Box* BoxSetListItNext(BoxSetListIt*);
2923
+
2924
+ static void BoxSetListCtor(BoxSetList* self) {
2925
+ assert(self);
2926
+ self->head_node = NULL;
2927
+ self->node_count = 0;
2928
+ }
2929
+ static void BoxSetListDtor(BoxSetList* self) {
2930
+ BoxSetListNode* node;
2931
+ assert(self);
2932
+ {
2933
+ BoxSetListIt it;
2934
+ BoxSetListItCtor(&it, self);
2935
+ while(BoxSetListItHasNext(&it)) {
2936
+ Box* e = BoxSetListItNext(&it);
2937
+ BoxDestroy(e);
2938
+ }
2939
+ };
2940
+ node = self->head_node;
2941
+ while(node) {
2942
+ BoxSetListNode* this_node = node;
2943
+ node = node->next_node;
2944
+ free(this_node);
2945
+ }
2946
+ }
2947
+ static BoxSetList* BoxSetListNew(void) {
2948
+ BoxSetList* self = (BoxSetList*)malloc(sizeof(BoxSetList)); assert(self);
2949
+ BoxSetListCtor(self);
2950
+ self->ref_count = 0;
2951
+ return self;
2952
+ }
2953
+ static void BoxSetListDestroy(BoxSetList* self) {
2954
+ assert(self);
2955
+ if(!--self->ref_count) {
2956
+ BoxSetListDtor(self);
2957
+ free(self);
2958
+ }
2959
+ }
2960
+ static BoxSetList* BoxSetListAssign(BoxSetList* self) {
2961
+ ++self->ref_count;
2962
+ return self;
2963
+ }
2964
+ static void BoxSetListPurge(BoxSetList* self) {
2965
+ BoxSetListDtor(self);
2966
+ BoxSetListCtor(self);
2967
+ }
2968
+ static Box* BoxSetListGet(BoxSetList* self) {
2969
+ assert(self);
2970
+ assert(!BoxSetListEmpty(self));
2971
+ return self->head_node->element;
2972
+ }
2973
+ static void BoxSetListChop(BoxSetList* self) {
2974
+ BoxSetListNode* node;
2975
+ assert(self);
2976
+ assert(!BoxSetListEmpty(self));
2977
+ node = self->head_node;
2978
+ BoxDestroy(node->element);
2979
+ self->head_node = self->head_node->next_node;
2980
+ --self->node_count;
2981
+ free(node);
2982
+ }
2983
+ static void BoxSetListAdd(BoxSetList* self, Box* element) {
2984
+ BoxSetListNode* node;
2985
+ assert(self);
2986
+ node = (BoxSetListNode*)malloc(sizeof(BoxSetListNode)); assert(node);
2987
+ node->element = BoxAssign(element);
2988
+ node->next_node = self->head_node;
2989
+ self->head_node = node;
2990
+ ++self->node_count;
2991
+ }
2992
+ static int BoxSetListContains(BoxSetList* self, Box* what) {
2993
+ BoxSetListNode* node;
2994
+ assert(self);
2995
+ what = BoxAssign(what);
2996
+ node = self->head_node;
2997
+ while(node) {
2998
+ if(BoxEqual(node->element,what)) {
2999
+ BoxDestroy(what);
3000
+ return 1;
3001
+ }
3002
+ node = node->next_node;
3003
+ }
3004
+ BoxDestroy(what);
3005
+ return 0;
3006
+ }
3007
+ static Box* BoxSetListFind(BoxSetList* self, Box* what) {
3008
+ BoxSetListNode* node;
3009
+ assert(self);
3010
+ what = BoxAssign(what);
3011
+ assert(BoxSetListContains(self, what));
3012
+ node = self->head_node;
3013
+ while(node) {
3014
+ if(BoxEqual(node->element,what)) {
3015
+ BoxDestroy(what);
3016
+ return node->element;
3017
+ }
3018
+ node = node->next_node;
3019
+ }
3020
+ abort();
3021
+ }
3022
+ static int BoxSetListReplace(BoxSetList* self, Box* what, Box* with) {
3023
+ BoxSetListNode* node;
3024
+ assert(self);
3025
+ what = BoxAssign(what);
3026
+ with = BoxAssign(with);
3027
+ node = self->head_node;
3028
+ while(node) {
3029
+ if(BoxEqual(node->element,what)) {
3030
+ BoxDestroy(node->element);
3031
+ node->element = BoxAssign(with);
3032
+ BoxDestroy(what);
3033
+ BoxDestroy(with);
3034
+ return 1;
3035
+ }
3036
+ node = node->next_node;
3037
+ }
3038
+ BoxDestroy(what);
3039
+ BoxDestroy(with);
3040
+ return 0;
3041
+ }
3042
+ static int BoxSetListReplaceAll(BoxSetList* self, Box* what, Box* with) {
3043
+ BoxSetListNode* node;
3044
+ int count = 0;
3045
+ assert(self);
3046
+ what = BoxAssign(what);
3047
+ with = BoxAssign(with);
3048
+ node = self->head_node;
3049
+ while(node) {
3050
+ if(BoxEqual(node->element,what)) {
3051
+ BoxDestroy(node->element);
3052
+ node->element = BoxAssign(with);
3053
+ ++count;
3054
+ }
3055
+ node = node->next_node;
3056
+ }
3057
+ BoxDestroy(what);
3058
+ BoxDestroy(with);
3059
+ return count;
3060
+ }
3061
+ static int BoxSetListRemove(BoxSetList* self, Box* what) {
3062
+ BoxSetListNode* node;
3063
+ BoxSetListNode* prev_node;
3064
+ int found = 0;
3065
+ assert(self);
3066
+ what = BoxAssign(what);
3067
+ node = self->head_node;
3068
+ prev_node = NULL;
3069
+ while(node) {
3070
+ if(BoxEqual(node->element,what)) {
3071
+ BoxDestroy(node->element);
3072
+ if(prev_node) {
3073
+ prev_node->next_node = node->next_node ? node->next_node : NULL;
3074
+ } else {
3075
+ self->head_node = node->next_node ? node->next_node : NULL;
3076
+ }
3077
+ --self->node_count;
3078
+ free(node);
3079
+ found = 1;
3080
+ break;
3081
+ }
3082
+ prev_node = node;
3083
+ node = node->next_node;
3084
+ }
3085
+ BoxDestroy(what);
3086
+ return found;
3087
+ }
3088
+ static int BoxSetListRemoveAll(BoxSetList* self, Box* what) {
3089
+ BoxSetListNode* node;
3090
+ BoxSetListNode* prev_node;
3091
+ int count = 0;
3092
+ assert(self);
3093
+ what = BoxAssign(what);
3094
+ node = self->head_node;
3095
+ prev_node = NULL;
3096
+ while(node) {
3097
+ if(BoxEqual(node->element,what)) {
3098
+ BoxDestroy(node->element);
3099
+ if(prev_node) {
3100
+ prev_node->next_node = node->next_node ? node->next_node : NULL;
3101
+ } else {
3102
+ self->head_node = node->next_node ? node->next_node : NULL;
3103
+ }
3104
+ --self->node_count;
3105
+ free(node);
3106
+ ++count;
3107
+ }
3108
+ prev_node = node;
3109
+ node = node->next_node;
3110
+ }
3111
+ BoxDestroy(what);
3112
+ return count;
3113
+ }
3114
+ static size_t BoxSetListSize(BoxSetList* self) {
3115
+ assert(self);
3116
+ return self->node_count;
3117
+ }
3118
+ static int BoxSetListEmpty(BoxSetList* self) {
3119
+ assert(self);
3120
+ return !self->node_count;
3121
+ }
3122
+ static void BoxSetListItCtor(BoxSetListIt* self, BoxSetList* list) {
3123
+ assert(self);
3124
+ assert(list);
3125
+ self->next_node = list->head_node;
3126
+ }
3127
+ static int BoxSetListItHasNext(BoxSetListIt* self) {
3128
+ assert(self);
3129
+ return self->next_node != NULL;
3130
+ }
3131
+ static Box* BoxSetListItNext(BoxSetListIt* self) {
3132
+ BoxSetListNode* node;
3133
+ assert(self);
3134
+ node = self->next_node;
3135
+ self->next_node = self->next_node->next_node;
3136
+ return node->element;
3137
+ }
3138
+
3139
+ void BoxSetCtor(BoxSet* self) {
3140
+ assert(self);
3141
+ self->min_bucket_count = 16;
3142
+ self->min_fill = 20;
3143
+ self->max_fill = 80;
3144
+ self->min_size = (size_t)((float)self->min_fill/100*self->min_bucket_count);
3145
+ self->max_size = (size_t)((float)self->max_fill/100*self->min_bucket_count);
3146
+ self->capacity_multiplier = 200;
3147
+ self->buckets = NULL;
3148
+ BoxSetRehash(self);
3149
+ }
3150
+ void BoxSetDtor(BoxSet* self) {
3151
+ size_t i;
3152
+ assert(self);
3153
+ for(i = 0; i < self->bucket_count; ++i) {
3154
+ BoxSetListDtor(&self->buckets[i]);
3155
+ }
3156
+ free(self->buckets);
3157
+ }
3158
+ BoxSet* BoxSetNew(void) {
3159
+ BoxSet* self = (BoxSet*)malloc(sizeof(BoxSet)); assert(self);
3160
+ BoxSetCtor(self);
3161
+ self->ref_count = 0;
3162
+ return self;
3163
+ }
3164
+ void BoxSetDestroy(BoxSet* self) {
3165
+ assert(self);
3166
+ if(!--self->ref_count) {
3167
+ BoxSetDtor(self);
3168
+ free(self);
3169
+ }
3170
+ }
3171
+ BoxSet* BoxSetAssign(BoxSet* self) {
3172
+ ++self->ref_count;
3173
+ return self;
3174
+ }
3175
+ void BoxSetPurge(BoxSet* self) {
3176
+ assert(self);
3177
+ BoxSetDtor(self);
3178
+ self->buckets = NULL;
3179
+ BoxSetRehash(self);
3180
+ }
3181
+ void BoxSetRehash(BoxSet* self) {
3182
+ BoxSetList* buckets;
3183
+ size_t i, bucket_count, size, fill;
3184
+ assert(self);
3185
+ assert(self->min_fill > 0);
3186
+ assert(self->max_fill > 0);
3187
+ assert(self->min_fill < self->max_fill);
3188
+ assert(self->min_bucket_count > 0);
3189
+ if(self->buckets) {
3190
+ if(self->min_size < self->size && self->size < self->max_size) return;
3191
+ fill = (size_t)((float)self->size/self->bucket_count*100);
3192
+ if(fill > self->max_fill) {
3193
+ bucket_count = (size_t)((float)self->bucket_count/100*self->capacity_multiplier);
3194
+ } else
3195
+ if(fill < self->min_fill && self->bucket_count > self->min_bucket_count) {
3196
+ bucket_count = (size_t)((float)self->bucket_count/self->capacity_multiplier*100);
3197
+ if(bucket_count < self->min_bucket_count) bucket_count = self->min_bucket_count;
3198
+ } else
3199
+ return;
3200
+ size = self->size;
3201
+ self->min_size = (size_t)((float)self->min_fill/100*size);
3202
+ self->max_size = (size_t)((float)self->max_fill/100*size);
3203
+ } else {
3204
+ bucket_count = self->min_bucket_count;
3205
+ size = 0;
3206
+ }
3207
+ buckets = (BoxSetList*)malloc(bucket_count*sizeof(BoxSetList)); assert(buckets);
3208
+ for(i = 0; i < bucket_count; ++i) {
3209
+ BoxSetListCtor(&buckets[i]);
3210
+ }
3211
+ if(self->buckets) {
3212
+ BoxSetIt it;
3213
+ BoxSetItCtor(&it, self);
3214
+ while(BoxSetItHasNext(&it)) {
3215
+ BoxSetList* bucket;
3216
+ Box* element = BoxSetItNext(&it);
3217
+ bucket = &buckets[BoxHash(element) % bucket_count];
3218
+ BoxSetListAdd(bucket, element);
3219
+ }
3220
+ BoxSetDtor(self);
3221
+ }
3222
+ self->buckets = buckets;
3223
+ self->bucket_count = bucket_count;
3224
+ self->size = size;
3225
+ }
3226
+ int BoxSetContains(BoxSet* self, Box* element) {
3227
+ int result;
3228
+ assert(self);
3229
+ element = BoxAssign(element);
3230
+ result = BoxSetListContains(&self->buckets[BoxHash(element) % self->bucket_count], element);
3231
+ BoxDestroy(element);
3232
+ return result;
3233
+ }
3234
+ Box* BoxSetGet(BoxSet* self, Box* element) {
3235
+ Box* result;
3236
+ assert(self);
3237
+ element = BoxAssign(element);
3238
+ assert(BoxSetContains(self, element));
3239
+ result = BoxSetListFind(&self->buckets[BoxHash(element) % self->bucket_count], element);
3240
+ BoxDestroy(element);
3241
+ return result;
3242
+ }
3243
+ size_t BoxSetSize(BoxSet* self) {
3244
+ assert(self);
3245
+ return self->size;
3246
+ }
3247
+ int BoxSetEmpty(BoxSet* self) {
3248
+ assert(self);
3249
+ return !self->size;
3250
+ }
3251
+ int BoxSetPut(BoxSet* self, Box* element) {
3252
+ int contained = 1;
3253
+ BoxSetList* bucket;
3254
+ assert(self);
3255
+ element = BoxAssign(element);
3256
+ bucket = &self->buckets[BoxHash(element) % self->bucket_count];
3257
+ if(!BoxSetListContains(bucket, element)) {
3258
+ BoxSetListAdd(bucket, element);
3259
+ ++self->size;
3260
+ contained = 0;
3261
+ BoxSetRehash(self);
3262
+ }
3263
+ BoxDestroy(element);
3264
+ return contained;
3265
+ }
3266
+ void BoxSetReplace(BoxSet* self, Box* element) {
3267
+ BoxSetList* bucket;
3268
+ assert(self);
3269
+ element = BoxAssign(element);
3270
+ bucket = &self->buckets[BoxHash(element) % self->bucket_count];
3271
+ if(!BoxSetListReplace(bucket, element, element)) {
3272
+ BoxSetListAdd(bucket, element);
3273
+ ++self->size;
3274
+ BoxSetRehash(self);
3275
+ }
3276
+ BoxDestroy(element);
3277
+ }
3278
+ int BoxSetRemove(BoxSet* self, Box* what) {
3279
+ int removed = 0;
3280
+ BoxSetList* bucket;
3281
+ assert(self);
3282
+ what = BoxAssign(what);
3283
+ bucket = &self->buckets[BoxHash(what) % self->bucket_count];
3284
+ if(BoxSetListRemove(bucket, what)) {
3285
+ --self->size;
3286
+ removed = 1;
3287
+ BoxSetRehash(self);
3288
+ }
3289
+ BoxDestroy(what);
3290
+ return removed;
3291
+ }
3292
+ void BoxSetNot(BoxSet* self, BoxSet* other) {
3293
+ BoxSetIt it;
3294
+ assert(self);
3295
+ assert(other);
3296
+ BoxSetItCtor(&it, other);
3297
+ while(BoxSetItHasNext(&it)) {
3298
+ BoxSetRemove(self, BoxSetItNext(&it));
3299
+ }
3300
+ BoxSetRehash(self);
3301
+ }
3302
+ void BoxSetOr(BoxSet* self, BoxSet* other) {
3303
+ BoxSetIt it;
3304
+ assert(self);
3305
+ assert(other);
3306
+ BoxSetItCtor(&it, other);
3307
+ while(BoxSetItHasNext(&it)) {
3308
+ BoxSetPut(self, BoxSetItNext(&it));
3309
+ }
3310
+ BoxSetRehash(self);
3311
+ }
3312
+ void BoxSetAnd(BoxSet* self, BoxSet* other) {
3313
+ BoxSetIt it;
3314
+ BoxSet set;
3315
+ assert(self);
3316
+ assert(other);
3317
+ BoxSetCtor(&set);
3318
+ BoxSetItCtor(&it, self);
3319
+ while(BoxSetItHasNext(&it)) {
3320
+ Box* element = BoxSetItNext(&it);
3321
+ if(BoxSetContains(other, element)) BoxSetPut(&set, element);
3322
+ }
3323
+ BoxSetItCtor(&it, other);
3324
+ while(BoxSetItHasNext(&it)) {
3325
+ Box* element = BoxSetItNext(&it);
3326
+ if(BoxSetContains(self, element)) BoxSetPut(&set, element);
3327
+ }
3328
+ BoxSetDtor(self);
3329
+ self->buckets = set.buckets;
3330
+ self->size = set.size;
3331
+ BoxSetRehash(self);
3332
+ /*BoxSetDtor(&set);*/
3333
+ }
3334
+ void BoxSetXor(BoxSet* self, BoxSet* other) {
3335
+ BoxSetIt it;
3336
+ BoxSet set;
3337
+ assert(self);
3338
+ assert(other);
3339
+ BoxSetCtor(&set);
3340
+ BoxSetItCtor(&it, self);
3341
+ while(BoxSetItHasNext(&it)) {
3342
+ Box* element = BoxSetItNext(&it);
3343
+ if(!BoxSetContains(other, element)) BoxSetPut(&set, element);
3344
+ }
3345
+ BoxSetItCtor(&it, other);
3346
+ while(BoxSetItHasNext(&it)) {
3347
+ Box* element = BoxSetItNext(&it);
3348
+ if(!BoxSetContains(self, element)) BoxSetPut(&set, element);
3349
+ }
3350
+ BoxSetDtor(self);
3351
+ self->buckets = set.buckets;
3352
+ self->size = set.size;
3353
+ BoxSetRehash(self);
3354
+ /*BoxSetDtor(&set);*/
3355
+ }
3356
+ void BoxSetItCtor(BoxSetIt* self, BoxSet* set) {
3357
+ assert(self);
3358
+ self->set = set;
3359
+ self->bucket_index = 0;
3360
+ BoxSetListItCtor(&self->it, &set->buckets[0]);
3361
+ }
3362
+ int BoxSetItHasNext(BoxSetIt* self) {
3363
+ assert(self);
3364
+ if(BoxSetListItHasNext(&self->it)) {
3365
+ return 1;
3366
+ } else {
3367
+ size_t i; for(i = self->bucket_index+1; i < self->set->bucket_count; ++i) {
3368
+ if(!BoxSetListEmpty(&self->set->buckets[i])) {
3369
+ return 1;
3370
+ }
3371
+ }
3372
+ return 0;
3373
+ }
3374
+ }
3375
+ Box* BoxSetItNext(BoxSetIt* self) {
3376
+ assert(self);
3377
+ assert(BoxSetItHasNext(self));
3378
+ if(BoxSetListItHasNext(&self->it)) {
3379
+ return BoxSetListItNext(&self->it);
3380
+ } else {
3381
+ size_t i; for(i = self->bucket_index+1; i < self->set->bucket_count; ++i) {
3382
+ if(!BoxSetListEmpty(&self->set->buckets[i])) {
3383
+ BoxSetListItCtor(&self->it, &self->set->buckets[i]);
3384
+ self->bucket_index = i;
3385
+ return BoxSetListItNext(&self->it);
3386
+ }
3387
+ }
3388
+ abort();
3389
+ }
3390
+ }
3391
+
3392
+ static PChar2IntVectorMapEntry PChar2IntVectorMapEntryKeyOnly(const char* key) {
3393
+ PChar2IntVectorMapEntry entry;
3394
+ entry.key = key;
3395
+ entry.valid_value = 0;
3396
+ return entry;
3397
+ }
3398
+ static PChar2IntVectorMapEntry PChar2IntVectorMapEntryKeyValue(const char* key, IntVector* value) {
3399
+ PChar2IntVectorMapEntry entry;
3400
+ entry.key = key;
3401
+ entry.value = value;
3402
+ entry.valid_value = 1;
3403
+ return entry;
3404
+ }
3405
+ static size_t PChar2IntVectorMapEntryHash(PChar2IntVectorMapEntry entry) {
3406
+ return PCharHash(entry.key);
3407
+ }
3408
+ static int PChar2IntVectorMapEntryEqual(PChar2IntVectorMapEntry lt, PChar2IntVectorMapEntry rt) {
3409
+ return PCharEqual(lt.key,rt.key);
3410
+ }
3411
+ static PChar2IntVectorMapEntry PChar2IntVectorMapEntryAssign(PChar2IntVectorMapEntry entry) {
3412
+ entry.key = (entry.key);
3413
+ if(entry.valid_value) entry.value = IntVectorAssign(entry.value);
3414
+ return entry;
3415
+ }
3416
+ static void PChar2IntVectorMapEntryDtor(PChar2IntVectorMapEntry entry) {
3417
+ ;
3418
+ if(entry.valid_value) IntVectorDestroy(entry.value);
3419
+ }
3420
+
3421
+ static void PChar2IntVectorMapSetCtor(PChar2IntVectorMapSet*);
3422
+ static void PChar2IntVectorMapSetDtor(PChar2IntVectorMapSet*);
3423
+ static PChar2IntVectorMapSet* PChar2IntVectorMapSetNew(void);
3424
+ static void PChar2IntVectorMapSetDestroy(PChar2IntVectorMapSet*);
3425
+ static PChar2IntVectorMapSet* PChar2IntVectorMapSetAssign(PChar2IntVectorMapSet*);
3426
+ static void PChar2IntVectorMapSetPurge(PChar2IntVectorMapSet*);
3427
+ static void PChar2IntVectorMapSetRehash(PChar2IntVectorMapSet*);
3428
+ static int PChar2IntVectorMapSetContains(PChar2IntVectorMapSet*, PChar2IntVectorMapEntry);
3429
+ static PChar2IntVectorMapEntry PChar2IntVectorMapSetGet(PChar2IntVectorMapSet*, PChar2IntVectorMapEntry);
3430
+ static size_t PChar2IntVectorMapSetSize(PChar2IntVectorMapSet*);
3431
+ static int PChar2IntVectorMapSetEmpty(PChar2IntVectorMapSet*);
3432
+ static int PChar2IntVectorMapSetPut(PChar2IntVectorMapSet*, PChar2IntVectorMapEntry);
3433
+ static void PChar2IntVectorMapSetReplace(PChar2IntVectorMapSet*, PChar2IntVectorMapEntry);
3434
+ static int PChar2IntVectorMapSetRemove(PChar2IntVectorMapSet*, PChar2IntVectorMapEntry);
3435
+ static void PChar2IntVectorMapSetNot(PChar2IntVectorMapSet*, PChar2IntVectorMapSet*);
3436
+ static void PChar2IntVectorMapSetAnd(PChar2IntVectorMapSet*, PChar2IntVectorMapSet*);
3437
+ static void PChar2IntVectorMapSetOr(PChar2IntVectorMapSet*, PChar2IntVectorMapSet*);
3438
+ static void PChar2IntVectorMapSetXor(PChar2IntVectorMapSet*, PChar2IntVectorMapSet*);
3439
+ static void PChar2IntVectorMapSetItCtor(PChar2IntVectorMapSetIt*, PChar2IntVectorMapSet*);
3440
+ static int PChar2IntVectorMapSetItHasNext(PChar2IntVectorMapSetIt*);
3441
+ static PChar2IntVectorMapEntry PChar2IntVectorMapSetItNext(PChar2IntVectorMapSetIt*);
3442
+
3443
+ static void PChar2IntVectorMapSetListCtor(PChar2IntVectorMapSetList*);
3444
+ static void PChar2IntVectorMapSetListDtor(PChar2IntVectorMapSetList*);
3445
+ static void PChar2IntVectorMapSetListPurge(PChar2IntVectorMapSetList*);
3446
+ static PChar2IntVectorMapSetList* PChar2IntVectorMapSetListNew(void);
3447
+ static void PChar2IntVectorMapSetListDestroy(PChar2IntVectorMapSetList*);
3448
+ static PChar2IntVectorMapSetList* PChar2IntVectorMapSetListAssign(PChar2IntVectorMapSetList*);
3449
+ static PChar2IntVectorMapEntry PChar2IntVectorMapSetListGet(PChar2IntVectorMapSetList*);
3450
+ static void PChar2IntVectorMapSetListAdd(PChar2IntVectorMapSetList*, PChar2IntVectorMapEntry);
3451
+ static void PChar2IntVectorMapSetListChop(PChar2IntVectorMapSetList*);
3452
+ static int PChar2IntVectorMapSetListContains(PChar2IntVectorMapSetList*, PChar2IntVectorMapEntry);
3453
+ static PChar2IntVectorMapEntry PChar2IntVectorMapSetListFind(PChar2IntVectorMapSetList*, PChar2IntVectorMapEntry);
3454
+ static int PChar2IntVectorMapSetListReplace(PChar2IntVectorMapSetList*, PChar2IntVectorMapEntry, PChar2IntVectorMapEntry);
3455
+ static int PChar2IntVectorMapSetListReplaceAll(PChar2IntVectorMapSetList*, PChar2IntVectorMapEntry, PChar2IntVectorMapEntry);
3456
+ static int PChar2IntVectorMapSetListRemove(PChar2IntVectorMapSetList*, PChar2IntVectorMapEntry);
3457
+ static int PChar2IntVectorMapSetListRemoveAll(PChar2IntVectorMapSetList*, PChar2IntVectorMapEntry);
3458
+ static size_t PChar2IntVectorMapSetListSize(PChar2IntVectorMapSetList*);
3459
+ static int PChar2IntVectorMapSetListEmpty(PChar2IntVectorMapSetList*);
3460
+ static void PChar2IntVectorMapSetListItCtor(PChar2IntVectorMapSetListIt*, PChar2IntVectorMapSetList*);
3461
+ static int PChar2IntVectorMapSetListItHasNext(PChar2IntVectorMapSetListIt*);
3462
+ static PChar2IntVectorMapEntry PChar2IntVectorMapSetListItNext(PChar2IntVectorMapSetListIt*);
3463
+
3464
+ static void PChar2IntVectorMapSetListCtor(PChar2IntVectorMapSetList* self) {
3465
+ assert(self);
3466
+ self->head_node = NULL;
3467
+ self->node_count = 0;
3468
+ }
3469
+ static void PChar2IntVectorMapSetListDtor(PChar2IntVectorMapSetList* self) {
3470
+ PChar2IntVectorMapSetListNode* node;
3471
+ assert(self);
3472
+ {
3473
+ PChar2IntVectorMapSetListIt it;
3474
+ PChar2IntVectorMapSetListItCtor(&it, self);
3475
+ while(PChar2IntVectorMapSetListItHasNext(&it)) {
3476
+ PChar2IntVectorMapEntry e = PChar2IntVectorMapSetListItNext(&it);
3477
+ PChar2IntVectorMapEntryDtor(e);
3478
+ }
3479
+ };
3480
+ node = self->head_node;
3481
+ while(node) {
3482
+ PChar2IntVectorMapSetListNode* this_node = node;
3483
+ node = node->next_node;
3484
+ free(this_node);
3485
+ }
3486
+ }
3487
+ static PChar2IntVectorMapSetList* PChar2IntVectorMapSetListNew(void) {
3488
+ PChar2IntVectorMapSetList* self = (PChar2IntVectorMapSetList*)malloc(sizeof(PChar2IntVectorMapSetList)); assert(self);
3489
+ PChar2IntVectorMapSetListCtor(self);
3490
+ self->ref_count = 0;
3491
+ return self;
3492
+ }
3493
+ static void PChar2IntVectorMapSetListDestroy(PChar2IntVectorMapSetList* self) {
3494
+ assert(self);
3495
+ if(!--self->ref_count) {
3496
+ PChar2IntVectorMapSetListDtor(self);
3497
+ free(self);
3498
+ }
3499
+ }
3500
+ static PChar2IntVectorMapSetList* PChar2IntVectorMapSetListAssign(PChar2IntVectorMapSetList* self) {
3501
+ ++self->ref_count;
3502
+ return self;
3503
+ }
3504
+ static void PChar2IntVectorMapSetListPurge(PChar2IntVectorMapSetList* self) {
3505
+ PChar2IntVectorMapSetListDtor(self);
3506
+ PChar2IntVectorMapSetListCtor(self);
3507
+ }
3508
+ static PChar2IntVectorMapEntry PChar2IntVectorMapSetListGet(PChar2IntVectorMapSetList* self) {
3509
+ assert(self);
3510
+ assert(!PChar2IntVectorMapSetListEmpty(self));
3511
+ return self->head_node->element;
3512
+ }
3513
+ static void PChar2IntVectorMapSetListChop(PChar2IntVectorMapSetList* self) {
3514
+ PChar2IntVectorMapSetListNode* node;
3515
+ assert(self);
3516
+ assert(!PChar2IntVectorMapSetListEmpty(self));
3517
+ node = self->head_node;
3518
+ PChar2IntVectorMapEntryDtor(node->element);
3519
+ self->head_node = self->head_node->next_node;
3520
+ --self->node_count;
3521
+ free(node);
3522
+ }
3523
+ static void PChar2IntVectorMapSetListAdd(PChar2IntVectorMapSetList* self, PChar2IntVectorMapEntry element) {
3524
+ PChar2IntVectorMapSetListNode* node;
3525
+ assert(self);
3526
+ node = (PChar2IntVectorMapSetListNode*)malloc(sizeof(PChar2IntVectorMapSetListNode)); assert(node);
3527
+ node->element = PChar2IntVectorMapEntryAssign(element);
3528
+ node->next_node = self->head_node;
3529
+ self->head_node = node;
3530
+ ++self->node_count;
3531
+ }
3532
+ static int PChar2IntVectorMapSetListContains(PChar2IntVectorMapSetList* self, PChar2IntVectorMapEntry what) {
3533
+ PChar2IntVectorMapSetListNode* node;
3534
+ assert(self);
3535
+ what = PChar2IntVectorMapEntryAssign(what);
3536
+ node = self->head_node;
3537
+ while(node) {
3538
+ if(PChar2IntVectorMapEntryEqual(node->element,what)) {
3539
+ PChar2IntVectorMapEntryDtor(what);
3540
+ return 1;
3541
+ }
3542
+ node = node->next_node;
3543
+ }
3544
+ PChar2IntVectorMapEntryDtor(what);
3545
+ return 0;
3546
+ }
3547
+ static PChar2IntVectorMapEntry PChar2IntVectorMapSetListFind(PChar2IntVectorMapSetList* self, PChar2IntVectorMapEntry what) {
3548
+ PChar2IntVectorMapSetListNode* node;
3549
+ assert(self);
3550
+ what = PChar2IntVectorMapEntryAssign(what);
3551
+ assert(PChar2IntVectorMapSetListContains(self, what));
3552
+ node = self->head_node;
3553
+ while(node) {
3554
+ if(PChar2IntVectorMapEntryEqual(node->element,what)) {
3555
+ PChar2IntVectorMapEntryDtor(what);
3556
+ return node->element;
3557
+ }
3558
+ node = node->next_node;
3559
+ }
3560
+ abort();
3561
+ }
3562
+ static int PChar2IntVectorMapSetListReplace(PChar2IntVectorMapSetList* self, PChar2IntVectorMapEntry what, PChar2IntVectorMapEntry with) {
3563
+ PChar2IntVectorMapSetListNode* node;
3564
+ assert(self);
3565
+ what = PChar2IntVectorMapEntryAssign(what);
3566
+ with = PChar2IntVectorMapEntryAssign(with);
3567
+ node = self->head_node;
3568
+ while(node) {
3569
+ if(PChar2IntVectorMapEntryEqual(node->element,what)) {
3570
+ PChar2IntVectorMapEntryDtor(node->element);
3571
+ node->element = PChar2IntVectorMapEntryAssign(with);
3572
+ PChar2IntVectorMapEntryDtor(what);
3573
+ PChar2IntVectorMapEntryDtor(with);
3574
+ return 1;
3575
+ }
3576
+ node = node->next_node;
3577
+ }
3578
+ PChar2IntVectorMapEntryDtor(what);
3579
+ PChar2IntVectorMapEntryDtor(with);
3580
+ return 0;
3581
+ }
3582
+ static int PChar2IntVectorMapSetListReplaceAll(PChar2IntVectorMapSetList* self, PChar2IntVectorMapEntry what, PChar2IntVectorMapEntry with) {
3583
+ PChar2IntVectorMapSetListNode* node;
3584
+ int count = 0;
3585
+ assert(self);
3586
+ what = PChar2IntVectorMapEntryAssign(what);
3587
+ with = PChar2IntVectorMapEntryAssign(with);
3588
+ node = self->head_node;
3589
+ while(node) {
3590
+ if(PChar2IntVectorMapEntryEqual(node->element,what)) {
3591
+ PChar2IntVectorMapEntryDtor(node->element);
3592
+ node->element = PChar2IntVectorMapEntryAssign(with);
3593
+ ++count;
3594
+ }
3595
+ node = node->next_node;
3596
+ }
3597
+ PChar2IntVectorMapEntryDtor(what);
3598
+ PChar2IntVectorMapEntryDtor(with);
3599
+ return count;
3600
+ }
3601
+ static int PChar2IntVectorMapSetListRemove(PChar2IntVectorMapSetList* self, PChar2IntVectorMapEntry what) {
3602
+ PChar2IntVectorMapSetListNode* node;
3603
+ PChar2IntVectorMapSetListNode* prev_node;
3604
+ int found = 0;
3605
+ assert(self);
3606
+ what = PChar2IntVectorMapEntryAssign(what);
3607
+ node = self->head_node;
3608
+ prev_node = NULL;
3609
+ while(node) {
3610
+ if(PChar2IntVectorMapEntryEqual(node->element,what)) {
3611
+ PChar2IntVectorMapEntryDtor(node->element);
3612
+ if(prev_node) {
3613
+ prev_node->next_node = node->next_node ? node->next_node : NULL;
3614
+ } else {
3615
+ self->head_node = node->next_node ? node->next_node : NULL;
3616
+ }
3617
+ --self->node_count;
3618
+ free(node);
3619
+ found = 1;
3620
+ break;
3621
+ }
3622
+ prev_node = node;
3623
+ node = node->next_node;
3624
+ }
3625
+ PChar2IntVectorMapEntryDtor(what);
3626
+ return found;
3627
+ }
3628
+ static int PChar2IntVectorMapSetListRemoveAll(PChar2IntVectorMapSetList* self, PChar2IntVectorMapEntry what) {
3629
+ PChar2IntVectorMapSetListNode* node;
3630
+ PChar2IntVectorMapSetListNode* prev_node;
3631
+ int count = 0;
3632
+ assert(self);
3633
+ what = PChar2IntVectorMapEntryAssign(what);
3634
+ node = self->head_node;
3635
+ prev_node = NULL;
3636
+ while(node) {
3637
+ if(PChar2IntVectorMapEntryEqual(node->element,what)) {
3638
+ PChar2IntVectorMapEntryDtor(node->element);
3639
+ if(prev_node) {
3640
+ prev_node->next_node = node->next_node ? node->next_node : NULL;
3641
+ } else {
3642
+ self->head_node = node->next_node ? node->next_node : NULL;
3643
+ }
3644
+ --self->node_count;
3645
+ free(node);
3646
+ ++count;
3647
+ }
3648
+ prev_node = node;
3649
+ node = node->next_node;
3650
+ }
3651
+ PChar2IntVectorMapEntryDtor(what);
3652
+ return count;
3653
+ }
3654
+ static size_t PChar2IntVectorMapSetListSize(PChar2IntVectorMapSetList* self) {
3655
+ assert(self);
3656
+ return self->node_count;
3657
+ }
3658
+ static int PChar2IntVectorMapSetListEmpty(PChar2IntVectorMapSetList* self) {
3659
+ assert(self);
3660
+ return !self->node_count;
3661
+ }
3662
+ static void PChar2IntVectorMapSetListItCtor(PChar2IntVectorMapSetListIt* self, PChar2IntVectorMapSetList* list) {
3663
+ assert(self);
3664
+ assert(list);
3665
+ self->next_node = list->head_node;
3666
+ }
3667
+ static int PChar2IntVectorMapSetListItHasNext(PChar2IntVectorMapSetListIt* self) {
3668
+ assert(self);
3669
+ return self->next_node != NULL;
3670
+ }
3671
+ static PChar2IntVectorMapEntry PChar2IntVectorMapSetListItNext(PChar2IntVectorMapSetListIt* self) {
3672
+ PChar2IntVectorMapSetListNode* node;
3673
+ assert(self);
3674
+ node = self->next_node;
3675
+ self->next_node = self->next_node->next_node;
3676
+ return node->element;
3677
+ }
3678
+
3679
+ static void PChar2IntVectorMapSetCtor(PChar2IntVectorMapSet* self) {
3680
+ assert(self);
3681
+ self->min_bucket_count = 16;
3682
+ self->min_fill = 20;
3683
+ self->max_fill = 80;
3684
+ self->min_size = (size_t)((float)self->min_fill/100*self->min_bucket_count);
3685
+ self->max_size = (size_t)((float)self->max_fill/100*self->min_bucket_count);
3686
+ self->capacity_multiplier = 200;
3687
+ self->buckets = NULL;
3688
+ PChar2IntVectorMapSetRehash(self);
3689
+ }
3690
+ static void PChar2IntVectorMapSetDtor(PChar2IntVectorMapSet* self) {
3691
+ size_t i;
3692
+ assert(self);
3693
+ for(i = 0; i < self->bucket_count; ++i) {
3694
+ PChar2IntVectorMapSetListDtor(&self->buckets[i]);
3695
+ }
3696
+ free(self->buckets);
3697
+ }
3698
+ static PChar2IntVectorMapSet* PChar2IntVectorMapSetNew(void) {
3699
+ PChar2IntVectorMapSet* self = (PChar2IntVectorMapSet*)malloc(sizeof(PChar2IntVectorMapSet)); assert(self);
3700
+ PChar2IntVectorMapSetCtor(self);
3701
+ self->ref_count = 0;
3702
+ return self;
3703
+ }
3704
+ static void PChar2IntVectorMapSetDestroy(PChar2IntVectorMapSet* self) {
3705
+ assert(self);
3706
+ if(!--self->ref_count) {
3707
+ PChar2IntVectorMapSetDtor(self);
3708
+ free(self);
3709
+ }
3710
+ }
3711
+ static PChar2IntVectorMapSet* PChar2IntVectorMapSetAssign(PChar2IntVectorMapSet* self) {
3712
+ ++self->ref_count;
3713
+ return self;
3714
+ }
3715
+ static void PChar2IntVectorMapSetPurge(PChar2IntVectorMapSet* self) {
3716
+ assert(self);
3717
+ PChar2IntVectorMapSetDtor(self);
3718
+ self->buckets = NULL;
3719
+ PChar2IntVectorMapSetRehash(self);
3720
+ }
3721
+ static void PChar2IntVectorMapSetRehash(PChar2IntVectorMapSet* self) {
3722
+ PChar2IntVectorMapSetList* buckets;
3723
+ size_t i, bucket_count, size, fill;
3724
+ assert(self);
3725
+ assert(self->min_fill > 0);
3726
+ assert(self->max_fill > 0);
3727
+ assert(self->min_fill < self->max_fill);
3728
+ assert(self->min_bucket_count > 0);
3729
+ if(self->buckets) {
3730
+ if(self->min_size < self->size && self->size < self->max_size) return;
3731
+ fill = (size_t)((float)self->size/self->bucket_count*100);
3732
+ if(fill > self->max_fill) {
3733
+ bucket_count = (size_t)((float)self->bucket_count/100*self->capacity_multiplier);
3734
+ } else
3735
+ if(fill < self->min_fill && self->bucket_count > self->min_bucket_count) {
3736
+ bucket_count = (size_t)((float)self->bucket_count/self->capacity_multiplier*100);
3737
+ if(bucket_count < self->min_bucket_count) bucket_count = self->min_bucket_count;
3738
+ } else
3739
+ return;
3740
+ size = self->size;
3741
+ self->min_size = (size_t)((float)self->min_fill/100*size);
3742
+ self->max_size = (size_t)((float)self->max_fill/100*size);
3743
+ } else {
3744
+ bucket_count = self->min_bucket_count;
3745
+ size = 0;
3746
+ }
3747
+ buckets = (PChar2IntVectorMapSetList*)malloc(bucket_count*sizeof(PChar2IntVectorMapSetList)); assert(buckets);
3748
+ for(i = 0; i < bucket_count; ++i) {
3749
+ PChar2IntVectorMapSetListCtor(&buckets[i]);
3750
+ }
3751
+ if(self->buckets) {
3752
+ PChar2IntVectorMapSetIt it;
3753
+ PChar2IntVectorMapSetItCtor(&it, self);
3754
+ while(PChar2IntVectorMapSetItHasNext(&it)) {
3755
+ PChar2IntVectorMapSetList* bucket;
3756
+ PChar2IntVectorMapEntry element = PChar2IntVectorMapSetItNext(&it);
3757
+ bucket = &buckets[PChar2IntVectorMapEntryHash(element) % bucket_count];
3758
+ PChar2IntVectorMapSetListAdd(bucket, element);
3759
+ }
3760
+ PChar2IntVectorMapSetDtor(self);
3761
+ }
3762
+ self->buckets = buckets;
3763
+ self->bucket_count = bucket_count;
3764
+ self->size = size;
3765
+ }
3766
+ static int PChar2IntVectorMapSetContains(PChar2IntVectorMapSet* self, PChar2IntVectorMapEntry element) {
3767
+ int result;
3768
+ assert(self);
3769
+ element = PChar2IntVectorMapEntryAssign(element);
3770
+ result = PChar2IntVectorMapSetListContains(&self->buckets[PChar2IntVectorMapEntryHash(element) % self->bucket_count], element);
3771
+ PChar2IntVectorMapEntryDtor(element);
3772
+ return result;
3773
+ }
3774
+ static PChar2IntVectorMapEntry PChar2IntVectorMapSetGet(PChar2IntVectorMapSet* self, PChar2IntVectorMapEntry element) {
3775
+ PChar2IntVectorMapEntry result;
3776
+ assert(self);
3777
+ element = PChar2IntVectorMapEntryAssign(element);
3778
+ assert(PChar2IntVectorMapSetContains(self, element));
3779
+ result = PChar2IntVectorMapSetListFind(&self->buckets[PChar2IntVectorMapEntryHash(element) % self->bucket_count], element);
3780
+ PChar2IntVectorMapEntryDtor(element);
3781
+ return result;
3782
+ }
3783
+ static size_t PChar2IntVectorMapSetSize(PChar2IntVectorMapSet* self) {
3784
+ assert(self);
3785
+ return self->size;
3786
+ }
3787
+ static int PChar2IntVectorMapSetEmpty(PChar2IntVectorMapSet* self) {
3788
+ assert(self);
3789
+ return !self->size;
3790
+ }
3791
+ static int PChar2IntVectorMapSetPut(PChar2IntVectorMapSet* self, PChar2IntVectorMapEntry element) {
3792
+ int contained = 1;
3793
+ PChar2IntVectorMapSetList* bucket;
3794
+ assert(self);
3795
+ element = PChar2IntVectorMapEntryAssign(element);
3796
+ bucket = &self->buckets[PChar2IntVectorMapEntryHash(element) % self->bucket_count];
3797
+ if(!PChar2IntVectorMapSetListContains(bucket, element)) {
3798
+ PChar2IntVectorMapSetListAdd(bucket, element);
3799
+ ++self->size;
3800
+ contained = 0;
3801
+ PChar2IntVectorMapSetRehash(self);
3802
+ }
3803
+ PChar2IntVectorMapEntryDtor(element);
3804
+ return contained;
3805
+ }
3806
+ static void PChar2IntVectorMapSetReplace(PChar2IntVectorMapSet* self, PChar2IntVectorMapEntry element) {
3807
+ PChar2IntVectorMapSetList* bucket;
3808
+ assert(self);
3809
+ element = PChar2IntVectorMapEntryAssign(element);
3810
+ bucket = &self->buckets[PChar2IntVectorMapEntryHash(element) % self->bucket_count];
3811
+ if(!PChar2IntVectorMapSetListReplace(bucket, element, element)) {
3812
+ PChar2IntVectorMapSetListAdd(bucket, element);
3813
+ ++self->size;
3814
+ PChar2IntVectorMapSetRehash(self);
3815
+ }
3816
+ PChar2IntVectorMapEntryDtor(element);
3817
+ }
3818
+ static int PChar2IntVectorMapSetRemove(PChar2IntVectorMapSet* self, PChar2IntVectorMapEntry what) {
3819
+ int removed = 0;
3820
+ PChar2IntVectorMapSetList* bucket;
3821
+ assert(self);
3822
+ what = PChar2IntVectorMapEntryAssign(what);
3823
+ bucket = &self->buckets[PChar2IntVectorMapEntryHash(what) % self->bucket_count];
3824
+ if(PChar2IntVectorMapSetListRemove(bucket, what)) {
3825
+ --self->size;
3826
+ removed = 1;
3827
+ PChar2IntVectorMapSetRehash(self);
3828
+ }
3829
+ PChar2IntVectorMapEntryDtor(what);
3830
+ return removed;
3831
+ }
3832
+ static void PChar2IntVectorMapSetNot(PChar2IntVectorMapSet* self, PChar2IntVectorMapSet* other) {
3833
+ PChar2IntVectorMapSetIt it;
3834
+ assert(self);
3835
+ assert(other);
3836
+ PChar2IntVectorMapSetItCtor(&it, other);
3837
+ while(PChar2IntVectorMapSetItHasNext(&it)) {
3838
+ PChar2IntVectorMapSetRemove(self, PChar2IntVectorMapSetItNext(&it));
3839
+ }
3840
+ PChar2IntVectorMapSetRehash(self);
3841
+ }
3842
+ static void PChar2IntVectorMapSetOr(PChar2IntVectorMapSet* self, PChar2IntVectorMapSet* other) {
3843
+ PChar2IntVectorMapSetIt it;
3844
+ assert(self);
3845
+ assert(other);
3846
+ PChar2IntVectorMapSetItCtor(&it, other);
3847
+ while(PChar2IntVectorMapSetItHasNext(&it)) {
3848
+ PChar2IntVectorMapSetPut(self, PChar2IntVectorMapSetItNext(&it));
3849
+ }
3850
+ PChar2IntVectorMapSetRehash(self);
3851
+ }
3852
+ static void PChar2IntVectorMapSetAnd(PChar2IntVectorMapSet* self, PChar2IntVectorMapSet* other) {
3853
+ PChar2IntVectorMapSetIt it;
3854
+ PChar2IntVectorMapSet set;
3855
+ assert(self);
3856
+ assert(other);
3857
+ PChar2IntVectorMapSetCtor(&set);
3858
+ PChar2IntVectorMapSetItCtor(&it, self);
3859
+ while(PChar2IntVectorMapSetItHasNext(&it)) {
3860
+ PChar2IntVectorMapEntry element = PChar2IntVectorMapSetItNext(&it);
3861
+ if(PChar2IntVectorMapSetContains(other, element)) PChar2IntVectorMapSetPut(&set, element);
3862
+ }
3863
+ PChar2IntVectorMapSetItCtor(&it, other);
3864
+ while(PChar2IntVectorMapSetItHasNext(&it)) {
3865
+ PChar2IntVectorMapEntry element = PChar2IntVectorMapSetItNext(&it);
3866
+ if(PChar2IntVectorMapSetContains(self, element)) PChar2IntVectorMapSetPut(&set, element);
3867
+ }
3868
+ PChar2IntVectorMapSetDtor(self);
3869
+ self->buckets = set.buckets;
3870
+ self->size = set.size;
3871
+ PChar2IntVectorMapSetRehash(self);
3872
+ /*PChar2IntVectorMapSetDtor(&set);*/
3873
+ }
3874
+ static void PChar2IntVectorMapSetXor(PChar2IntVectorMapSet* self, PChar2IntVectorMapSet* other) {
3875
+ PChar2IntVectorMapSetIt it;
3876
+ PChar2IntVectorMapSet set;
3877
+ assert(self);
3878
+ assert(other);
3879
+ PChar2IntVectorMapSetCtor(&set);
3880
+ PChar2IntVectorMapSetItCtor(&it, self);
3881
+ while(PChar2IntVectorMapSetItHasNext(&it)) {
3882
+ PChar2IntVectorMapEntry element = PChar2IntVectorMapSetItNext(&it);
3883
+ if(!PChar2IntVectorMapSetContains(other, element)) PChar2IntVectorMapSetPut(&set, element);
3884
+ }
3885
+ PChar2IntVectorMapSetItCtor(&it, other);
3886
+ while(PChar2IntVectorMapSetItHasNext(&it)) {
3887
+ PChar2IntVectorMapEntry element = PChar2IntVectorMapSetItNext(&it);
3888
+ if(!PChar2IntVectorMapSetContains(self, element)) PChar2IntVectorMapSetPut(&set, element);
3889
+ }
3890
+ PChar2IntVectorMapSetDtor(self);
3891
+ self->buckets = set.buckets;
3892
+ self->size = set.size;
3893
+ PChar2IntVectorMapSetRehash(self);
3894
+ /*PChar2IntVectorMapSetDtor(&set);*/
3895
+ }
3896
+ static void PChar2IntVectorMapSetItCtor(PChar2IntVectorMapSetIt* self, PChar2IntVectorMapSet* set) {
3897
+ assert(self);
3898
+ self->set = set;
3899
+ self->bucket_index = 0;
3900
+ PChar2IntVectorMapSetListItCtor(&self->it, &set->buckets[0]);
3901
+ }
3902
+ static int PChar2IntVectorMapSetItHasNext(PChar2IntVectorMapSetIt* self) {
3903
+ assert(self);
3904
+ if(PChar2IntVectorMapSetListItHasNext(&self->it)) {
3905
+ return 1;
3906
+ } else {
3907
+ size_t i; for(i = self->bucket_index+1; i < self->set->bucket_count; ++i) {
3908
+ if(!PChar2IntVectorMapSetListEmpty(&self->set->buckets[i])) {
3909
+ return 1;
3910
+ }
3911
+ }
3912
+ return 0;
3913
+ }
3914
+ }
3915
+ static PChar2IntVectorMapEntry PChar2IntVectorMapSetItNext(PChar2IntVectorMapSetIt* self) {
3916
+ assert(self);
3917
+ assert(PChar2IntVectorMapSetItHasNext(self));
3918
+ if(PChar2IntVectorMapSetListItHasNext(&self->it)) {
3919
+ return PChar2IntVectorMapSetListItNext(&self->it);
3920
+ } else {
3921
+ size_t i; for(i = self->bucket_index+1; i < self->set->bucket_count; ++i) {
3922
+ if(!PChar2IntVectorMapSetListEmpty(&self->set->buckets[i])) {
3923
+ PChar2IntVectorMapSetListItCtor(&self->it, &self->set->buckets[i]);
3924
+ self->bucket_index = i;
3925
+ return PChar2IntVectorMapSetListItNext(&self->it);
3926
+ }
3927
+ }
3928
+ abort();
3929
+ }
3930
+ }
3931
+
3932
+ void PChar2IntVectorMapCtor(PChar2IntVectorMap* self) {
3933
+ assert(self);
3934
+ PChar2IntVectorMapSetCtor(&self->entries);
3935
+ }
3936
+ void PChar2IntVectorMapDtor(PChar2IntVectorMap* self) {
3937
+ assert(self);
3938
+ PChar2IntVectorMapSetDtor(&self->entries);
3939
+ }
3940
+ PChar2IntVectorMap* PChar2IntVectorMapNew(void) {
3941
+ PChar2IntVectorMap* self = (PChar2IntVectorMap*)malloc(sizeof(PChar2IntVectorMap)); assert(self);
3942
+ PChar2IntVectorMapCtor(self);
3943
+ self->ref_count = 0;
3944
+ return self;
3945
+ }
3946
+ void PChar2IntVectorMapDestroy(PChar2IntVectorMap* self) {
3947
+ assert(self);
3948
+ if(!--self->ref_count) {
3949
+ PChar2IntVectorMapDtor(self);
3950
+ free(self);
3951
+ }
3952
+ }
3953
+ void PChar2IntVectorMapRehash(PChar2IntVectorMap* self) {
3954
+ assert(self);
3955
+ PChar2IntVectorMapSetRehash(&self->entries);
3956
+ }
3957
+ PChar2IntVectorMap* PChar2IntVectorMapAssign(PChar2IntVectorMap* self) {
3958
+ assert(self);
3959
+ ++self->ref_count;
3960
+ return self;
3961
+ }
3962
+ void PChar2IntVectorMapPurge(PChar2IntVectorMap* self) {
3963
+ assert(self);
3964
+ PChar2IntVectorMapSetPurge(&self->entries);
3965
+ }
3966
+ size_t PChar2IntVectorMapSize(PChar2IntVectorMap* self) {
3967
+ assert(self);
3968
+ return PChar2IntVectorMapSetSize(&self->entries);
3969
+ }
3970
+ int PChar2IntVectorMapEmpty(PChar2IntVectorMap* self) {
3971
+ assert(self);
3972
+ return PChar2IntVectorMapSetEmpty(&self->entries);
3973
+ }
3974
+ int PChar2IntVectorMapContainsKey(PChar2IntVectorMap* self, const char* key) {
3975
+ int result;
3976
+ PChar2IntVectorMapEntry entry;
3977
+ assert(self);
3978
+ entry = PChar2IntVectorMapEntryAssign(PChar2IntVectorMapEntryKeyOnly(key));
3979
+ result = PChar2IntVectorMapSetContains(&self->entries, entry);
3980
+ PChar2IntVectorMapEntryDtor(entry);
3981
+ return result;
3982
+ }
3983
+ IntVector* PChar2IntVectorMapGet(PChar2IntVectorMap* self, const char* key) {
3984
+ IntVector* result;
3985
+ PChar2IntVectorMapEntry entry;
3986
+ assert(self);
3987
+ entry = PChar2IntVectorMapEntryAssign(PChar2IntVectorMapEntryKeyOnly(key));
3988
+ assert(PChar2IntVectorMapContainsKey(self, key));
3989
+ result = PChar2IntVectorMapSetGet(&self->entries, entry).value;
3990
+ PChar2IntVectorMapEntryDtor(entry);
3991
+ return result;
3992
+ }
3993
+ int PChar2IntVectorMapPut(PChar2IntVectorMap* self, const char* key, IntVector* value) {
3994
+ PChar2IntVectorMapEntry entry = PChar2IntVectorMapEntryAssign(PChar2IntVectorMapEntryKeyValue(key,value));
3995
+ assert(self);
3996
+ if(!PChar2IntVectorMapContainsKey(self, key)) {
3997
+ PChar2IntVectorMapSetPut(&self->entries, entry);
3998
+ PChar2IntVectorMapEntryDtor(entry);
3999
+ return 1;
4000
+ } else {
4001
+ PChar2IntVectorMapEntryDtor(entry);
4002
+ return 0;
4003
+ }
4004
+ }
4005
+ void PChar2IntVectorMapReplace(PChar2IntVectorMap* self, const char* key, IntVector* value) {
4006
+ PChar2IntVectorMapEntry entry;
4007
+ assert(self);
4008
+ entry = PChar2IntVectorMapEntryAssign(PChar2IntVectorMapEntryKeyValue(key,value));
4009
+ PChar2IntVectorMapSetReplace(&self->entries, entry);
4010
+ PChar2IntVectorMapEntryDtor(entry);
4011
+ }
4012
+ int PChar2IntVectorMapRemove(PChar2IntVectorMap* self, const char* key) {
4013
+ int removed;
4014
+ PChar2IntVectorMapEntry entry;
4015
+ assert(self);
4016
+ entry = PChar2IntVectorMapEntryAssign(PChar2IntVectorMapEntryKeyOnly(key));
4017
+ removed = PChar2IntVectorMapSetRemove(&self->entries, entry);
4018
+ PChar2IntVectorMapEntryDtor(entry);
4019
+ return removed;
4020
+ }
4021
+ void PChar2IntVectorMapItCtor(PChar2IntVectorMapIt* self, PChar2IntVectorMap* map) {
4022
+ assert(self);
4023
+ assert(map);
4024
+ PChar2IntVectorMapSetItCtor(&self->it, &map->entries);
4025
+ }
4026
+ int PChar2IntVectorMapItHasNext(PChar2IntVectorMapIt* self) {
4027
+ assert(self);
4028
+ return PChar2IntVectorMapSetItHasNext(&self->it);
4029
+ }
4030
+ const char* PChar2IntVectorMapItNextKey(PChar2IntVectorMapIt* self) {
4031
+ assert(self);
4032
+ return PChar2IntVectorMapSetItNext(&self->it).key;
4033
+ }
4034
+ IntVector* PChar2IntVectorMapItNextValue(PChar2IntVectorMapIt* self) {
4035
+ assert(self);
4036
+ return PChar2IntVectorMapSetItNext(&self->it).value;
4037
+ }
4038
+ PChar2IntVectorMapEntry PChar2IntVectorMapItNext(PChar2IntVectorMapIt* self) {
4039
+ assert(self);
4040
+ return PChar2IntVectorMapSetItNext(&self->it);
4041
+ }
4042
+