-
Notifications
You must be signed in to change notification settings - Fork 7
/
assoc_BM.c
1768 lines (1670 loc) · 62.6 KB
/
assoc_BM.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// file assoc_BM.c
// SPDX-License-Identifier: GPL-3.0-or-later
/***
BISMON
Copyright © 2018 - 2022 CEA (Commissariat à l'énergie atomique et aux énergies alternatives)
contributed by Basile Starynkevitch (working at CEA, LIST, France)
<basile@starynkevitch.net> or <basile.starynkevitch@cea.fr>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#include "bismon.h"
/******** ASSOCIATIONS FROM OBJECTS TO VALUES ********/
/// assocpairs for individual buckets, or small associations
// allocated size of typayl_assocpairs_BM
#define ASSOCPAIRSIZE_BM(Asp) ((typedhead_tyBM*)(Asp))->rlen
// used count of typayl_assocpairs_BM
#define ASSOCPAIRUCNT_BM(Asp) ((typedsize_tyBM*)(Asp))->size
/// assoctables for larger associations, pointing to assocpairs (buckets)
// allocated size of typayl_assoctable_BM
#define ASSOCTABLESIZE_BM(Asb) ((typedhead_tyBM*)(Asb))->rlen
// cumulated count - totals of used counts in the many assocpairs
#define ASSOCTABLECUMCNT_BM(Asb) ((typedsize_tyBM*)(Asb))->size
unsigned
assoc_nbkeys_BM (const anyassoc_tyBM * assoc)
{
if (!assoc)
return 0;
int ty = valtype_BM ((value_tyBM) assoc);
if (ty == typayl_assocpairs_BM)
{
struct assocpairs_stBM *apair = (struct assocpairs_stBM *) assoc;
return ASSOCPAIRUCNT_BM (apair);
}
else if (ty == typayl_assoctable_BM)
{
struct assoctable_stBM *atbl = (struct assoctable_stBM *) assoc;
return ASSOCTABLECUMCNT_BM (atbl);
}
return 0;
} /* end assoc_nbkeys_BM */
static struct assocpairs_stBM *
assocpair_put_BM (struct assocpairs_stBM *apairs,
const objectval_tyBM * keyob, value_tyBM val)
{
ASSERT_BM (valtype_BM ((const value_tyBM) keyob) == tyObject_BM);
ASSERT_BM (valtype_BM (val) != tyNone_BM);
struct assocpairs_stBM *newpairs = NULL;
if (!apairs)
{
unsigned newsiz = TINYSIZE_BM / 3;
newpairs =
allocgcty_BM (typayl_assocpairs_BM,
sizeof (struct assocpairs_stBM) +
newsiz * sizeof (struct assocentry_stBM));
ASSOCPAIRSIZE_BM (newpairs) = newsiz;
ASSOCPAIRUCNT_BM (newpairs) = 1;
newpairs->apairs_ent[0].asso_keyob = keyob;
newpairs->apairs_ent[0].asso_val = val;
return newpairs;
};
ASSERT_BM (valtype_BM (apairs) == typayl_assocpairs_BM);
unsigned oldsiz = ASSOCPAIRSIZE_BM (apairs);
unsigned oldcnt = ASSOCPAIRUCNT_BM (apairs);
ASSERT_BM (oldcnt <= oldsiz);
if (oldcnt == oldsiz)
{
// if key was here, put new value in entry
for (unsigned ix = 0; ix < oldsiz; ix++)
{
if (apairs->apairs_ent[ix].asso_keyob == keyob)
{
apairs->apairs_ent[ix].asso_val = val;
return apairs;
}
}
// should grow
unsigned long newsiz =
prime_above_BM (4 * oldsiz / 3 + oldsiz / 64 + ILOG2_BM (oldsiz + 2) +
3);
if (newsiz >= MAXSIZE_BM)
FATAL_BM ("too big assocpairs %lu for oldsiz %u", newsiz, oldsiz);
newpairs =
allocgcty_BM (typayl_assocpairs_BM,
sizeof (struct assocpairs_stBM) +
newsiz * sizeof (struct assocentry_stBM));
ASSOCPAIRSIZE_BM (newpairs) = newsiz;
unsigned cnt = 0;
for (unsigned ix = 0; ix < oldsiz; ix++)
{
if (apairs->apairs_ent[ix].asso_keyob
&& apairs->apairs_ent[ix].asso_val)
{
newpairs->apairs_ent[cnt++] = apairs->apairs_ent[ix];
ASSERT_BM (cnt < newsiz);
}
};
ASSERT_BM (cnt == oldcnt);
newpairs->apairs_ent[cnt].asso_keyob = keyob;
newpairs->apairs_ent[cnt].asso_val = val;
cnt++;
ASSOCPAIRUCNT_BM (newpairs) = cnt;
return newpairs;
}
else
{
// should find some empty slot - or the existing one
ASSERT_BM (oldcnt < oldsiz);
for (unsigned ix = 0; ix < oldsiz; ix++)
{
struct assocentry_stBM *curent = apairs->apairs_ent + ix;
if (!curent->asso_keyob)
{
ASSERT_BM (!curent->asso_val);
curent->asso_keyob = keyob;
curent->asso_val = val;
ASSOCPAIRUCNT_BM (apairs) = oldcnt + 1;
return apairs;
}
else if (curent->asso_keyob == keyob)
{
ASSERT_BM (curent->asso_val != NULL);
curent->asso_val = val;
return apairs;
}
};
// should never be reached
FATAL_BM ("corrupted assocpair @%p", apairs);
}
} /* end assocpair_put_BM */
static unsigned
assoc_buckix_for_key_BM (const struct assoctable_stBM *atble,
const objectval_tyBM * obkey)
{
ASSERT_BM (valtype_BM ((const value_tyBM) atble) == typayl_assoctable_BM);
ASSERT_BM (valtype_BM ((const value_tyBM) obkey) == tyObject_BM);
hash_tyBM hkey = objecthash_BM (obkey);
ASSERT_BM (hkey != 0);
unsigned nbuckets = ASSOCTABLESIZE_BM (atble);
ASSERT_BM (nbuckets > 2 && nbuckets % 2 != 0);
return hkey % nbuckets;
} /* end assoc_buckix_for_key_BM */
void
assoc_reorganize_BM (anyassoc_tyBM ** passoc, unsigned gap)
{
ASSERT_BM (passoc != NULL);
anyassoc_tyBM *oldassoc = *passoc;
bool oldassocistable = valtype_BM (oldassoc) == typayl_assoctable_BM;
bool oldassocispairs = valtype_BM (oldassoc) == typayl_assocpairs_BM;
ASSERT_BM (!oldassoc || isassoc_BM (oldassoc));
unsigned oldcnt = assoc_nbkeys_BM (oldassoc);
unsigned long newish = (oldcnt + gap + oldcnt / 128 + gap / 64);
if (newish <= TINYSIZE_BM)
{
unsigned newpairsiz = 0;
if (newish == 0)
newpairsiz = 0;
else if (newish <= TINYSIZE_BM / 3)
newpairsiz = TINYSIZE_BM / 3;
else if (newish < 2 * TINYSIZE_BM / 3)
newpairsiz = 2 * TINYSIZE_BM / 3;
else
newpairsiz = TINYSIZE_BM;
if (oldassocispairs && newpairsiz == ASSOCPAIRSIZE_BM (oldassoc))
return;
struct assocpairs_stBM *newpairs = newpairsiz ?
allocgcty_BM (typayl_assocpairs_BM,
sizeof (struct assocpairs_stBM) +
newpairsiz * sizeof (struct assocentry_stBM)) : NULL;
if (newpairs)
{
ASSOCPAIRSIZE_BM (newpairs) = newpairsiz;
ASSOCPAIRUCNT_BM (newpairs) = 0;
}
if (oldassocistable)
{
unsigned newpaircnt = 0;
unsigned nbuckets = ASSOCTABLESIZE_BM (oldassoc);
for (unsigned buckix = 0; buckix < nbuckets; buckix++)
{
struct assocpairs_stBM *curbuckpair =
((struct assoctable_stBM *) oldassoc)->abuck_pairs[buckix];
if (curbuckpair)
{
ASSERT_BM (valtype_BM (curbuckpair) ==
typayl_assocpairs_BM);
unsigned bucksiz = ASSOCPAIRSIZE_BM (curbuckpair);
for (unsigned pix = 0; pix < bucksiz; pix++)
{
const objectval_tyBM *curkeyob =
curbuckpair->apairs_ent[pix].asso_keyob;
value_tyBM curval =
curbuckpair->apairs_ent[pix].asso_val;
if (curkeyob && curval)
{
ASSERT_BM (newpairs && newpaircnt < newpairsiz);
ASSERT_BM (valtype_BM ((const value_tyBM) curkeyob)
== tyObject_BM);
ASSERT_BM (valtype_BM (curval) != tyNone_BM);
newpairs->apairs_ent[newpaircnt].asso_keyob =
curkeyob;
newpairs->apairs_ent[newpaircnt].asso_val = curval;
newpaircnt++;
}
}
ASSERT_BM (newpaircnt == oldcnt);
ASSOCPAIRUCNT_BM (newpairs) = newpaircnt;
((struct assoctable_stBM *) oldassoc)->abuck_pairs[buckix]
= NULL;
}
}
*passoc = newpairs;
} /* end if oldassocistable */
else if (oldassocispairs)
{
struct assocpairs_stBM *oldpairs = oldassoc;
ASSERT_BM (valtype_BM (oldpairs) == typayl_assocpairs_BM);
unsigned bucksiz = ASSOCPAIRSIZE_BM (oldpairs);
unsigned newpaircnt = 0;
for (unsigned pix = 0; pix < bucksiz; pix++)
{
const objectval_tyBM *curkeyob =
oldpairs->apairs_ent[pix].asso_keyob;
value_tyBM curval = oldpairs->apairs_ent[pix].asso_val;
if (curkeyob && curval)
{
ASSERT_BM (newpairs && newpaircnt < newpairsiz);
ASSERT_BM (valtype_BM ((const value_tyBM) curkeyob) ==
tyObject_BM);
ASSERT_BM (valtype_BM (curval) != tyNone_BM);
newpairs->apairs_ent[newpaircnt].asso_keyob = curkeyob;
newpairs->apairs_ent[newpaircnt].asso_val = curval;
newpaircnt++;
}
}
ASSERT_BM (newpaircnt == oldcnt);
ASSOCPAIRUCNT_BM (newpairs) = newpaircnt;
*passoc = newpairs;
} /* end if oldassocispairs */
else
*passoc = newpairs;
return;
} /* end if newish <= TINYSIZE_BM */
if (newish > MAXSIZE_BM)
FATAL_BM ("new assoc wish %lu too big", newish);
unsigned newnbuckets =
prime_above_BM (newish / (TINYSIZE_BM - 3) + gap / 128 +
ILOG2_BM (oldcnt + gap + 1) / 4 + 4);
if (newnbuckets > MAXSIZE_BM)
FATAL_BM ("too big #buckets %u in assoc", newnbuckets);
struct assoctable_stBM *newtable = //
allocgcty_BM (typayl_assoctable_BM,
sizeof (struct assoctable_stBM)
+ newnbuckets * sizeof (newtable->abuck_pairs[0]));
ASSOCTABLESIZE_BM (newtable) = newnbuckets;
ASSOCTABLECUMCNT_BM (newtable) = 0;
if (oldassocistable)
{
unsigned addedcnt = 0;
unsigned nbuckets = ASSOCTABLESIZE_BM (oldassoc);
for (unsigned buckix = 0; buckix < nbuckets; buckix++)
{
struct assocpairs_stBM *curbuckpair =
((struct assoctable_stBM *) oldassoc)->abuck_pairs[buckix];
if (curbuckpair)
{
ASSERT_BM (valtype_BM (curbuckpair) == typayl_assocpairs_BM);
unsigned bucklen = ASSOCPAIRSIZE_BM (curbuckpair);
for (unsigned pix = 0; pix < bucklen; pix++)
{
const objectval_tyBM *curkeyob =
curbuckpair->apairs_ent[pix].asso_keyob;
value_tyBM curval = curbuckpair->apairs_ent[pix].asso_val;
if (curkeyob && curval)
{
unsigned newbuckix =
assoc_buckix_for_key_BM (newtable, curkeyob);
struct assocpairs_stBM *prevnewpairs =
newtable->abuck_pairs[newbuckix];
struct assocpairs_stBM *updatednewpairs
= assocpair_put_BM (prevnewpairs, curkeyob, curval);
if (updatednewpairs != prevnewpairs)
newtable->abuck_pairs[newbuckix] = updatednewpairs;
addedcnt++;
}
}
((struct assoctable_stBM *) oldassoc)->abuck_pairs[buckix] =
NULL;
}
}
ASSERT_BM (addedcnt == oldcnt);
ASSOCTABLECUMCNT_BM (newtable) = addedcnt;
}
else if (oldassocispairs)
{
struct assocpairs_stBM *oldpairs = oldassoc;
ASSERT_BM (valtype_BM (oldpairs) == typayl_assocpairs_BM);
unsigned bucklen = ASSOCPAIRSIZE_BM (oldpairs);
unsigned addedcnt = 0;
for (unsigned pix = 0; pix < bucklen; pix++)
{
const objectval_tyBM *curkeyob =
oldpairs->apairs_ent[pix].asso_keyob;
value_tyBM curval = oldpairs->apairs_ent[pix].asso_val;
if (curkeyob && curval)
{
unsigned newbuckix =
assoc_buckix_for_key_BM (newtable, curkeyob);
struct assocpairs_stBM *prevnewpairs =
newtable->abuck_pairs[newbuckix];
struct assocpairs_stBM *updatednewpairs
= assocpair_put_BM (prevnewpairs, curkeyob, curval);
if (updatednewpairs != prevnewpairs)
newtable->abuck_pairs[newbuckix] = updatednewpairs;
addedcnt++;
}
}
ASSERT_BM (addedcnt == oldcnt);
ASSOCTABLECUMCNT_BM (newtable) = addedcnt;
}
*passoc = newtable;
} /* end assoc_reorganize_BM */
void
assocpairgcdestroy_BM (struct garbcoll_stBM *gc, struct assocpairs_stBM *assp)
{
ASSERT_BM (gc && gc->gc_magic == GCMAGIC_BM);
ASSERT_BM (((typedhead_tyBM *) assp)->htyp == typayl_assocpairs_BM);
unsigned len = ASSOCPAIRSIZE_BM (assp);
memset (assp, 0, sizeof (*assp) + len * sizeof (struct assocentry_stBM));
free (assp);
gc->gc_freedbytes += sizeof (*assp) + len * sizeof (struct assocentry_stBM);
} /* end assocpairgcdestroy_BM */
void
assocpairgckeep_BM (struct garbcoll_stBM *gc, struct assocpairs_stBM *assp)
{
ASSERT_BM (gc && gc->gc_magic == GCMAGIC_BM);
ASSERT_BM (((typedhead_tyBM *) assp)->htyp == typayl_assocpairs_BM);
unsigned len = ASSOCPAIRSIZE_BM (assp);
ASSERT_BM (len < MAXSIZE_BM);
gc->gc_keptbytes += sizeof (*assp) + len * sizeof (struct assocentry_stBM);
} /* end assocpairgckeep_BM */
void
assoctablegcdestroy_BM (struct garbcoll_stBM *gc,
struct assoctable_stBM *abuck)
{
ASSERT_BM (gc && gc->gc_magic == GCMAGIC_BM);
ASSERT_BM (((typedhead_tyBM *) abuck)->htyp == typayl_assoctable_BM);
unsigned nbuckets = ASSOCTABLESIZE_BM (abuck);
memset (abuck, 0, sizeof (*abuck) + nbuckets * sizeof (void *));
free (abuck);
gc->gc_freedbytes += sizeof (*abuck) + nbuckets * sizeof (void *);
} /*end assoctablegcdestroy_BM */
void
assoctablegckeep_BM (struct garbcoll_stBM *gc, struct assoctable_stBM *abuck)
{
ASSERT_BM (gc && gc->gc_magic == GCMAGIC_BM);
ASSERT_BM (((typedhead_tyBM *) abuck)->htyp == typayl_assoctable_BM);
unsigned nbuckets = ASSOCTABLESIZE_BM (abuck);
ASSERT_BM (nbuckets < MAXSIZE_BM);
gc->gc_keptbytes += sizeof (*abuck) + nbuckets * sizeof (void *);
} /* end assoctablegckeep_BM */
const setval_tyBM *
assoc_setattrs_BM (const anyassoc_tyBM * assoc)
{
if (!assoc)
return makeset_BM (NULL, 0);
if (!isassoc_BM ((const value_tyBM) assoc))
return NULL;
unsigned nbkeys = assoc_nbkeys_BM (assoc);
if (nbkeys == 0)
return makeset_BM (NULL, 0);
const objectval_tyBM *tinyarr[TINYSIZE_BM] = { };
const objectval_tyBM **arr =
(nbkeys < TINYSIZE_BM) ? tinyarr : calloc (prime_above_BM (nbkeys + 1),
sizeof (void *));
if (!arr)
FATAL_BM ("out of memory for %u keys in assoc", nbkeys);
unsigned keycnt = 0;
if (valtype_BM ((const value_tyBM) assoc) == typayl_assoctable_BM)
{
unsigned nbuckets = ((typedhead_tyBM *) assoc)->rlen;
for (unsigned buckix = 0; buckix < nbuckets; buckix++)
{
struct assocpairs_stBM *curbuckpair =
((struct assoctable_stBM *) assoc)->abuck_pairs[buckix];
if (curbuckpair)
{
ASSERT_BM (valtype_BM (curbuckpair) == typayl_assocpairs_BM);
unsigned bucklen = ((typedhead_tyBM *) curbuckpair)->rlen;
for (unsigned pix = 0; pix < bucklen; pix++)
{
const objectval_tyBM *curkeyob =
curbuckpair->apairs_ent[pix].asso_keyob;
value_tyBM curval = curbuckpair->apairs_ent[pix].asso_val;
if (curkeyob && curval)
{
ASSERT_BM (keycnt < nbkeys);
arr[keycnt++] = curkeyob;
}
}
}
}
}
else if (valtype_BM ((const value_tyBM) assoc) == typayl_assocpairs_BM)
{
const struct assocpairs_stBM *curpairs = assoc;
unsigned bucklen = ASSOCPAIRSIZE_BM (curpairs);
for (unsigned pix = 0; pix < bucklen; pix++)
{
const objectval_tyBM *curkeyob =
curpairs->apairs_ent[pix].asso_keyob;
value_tyBM curval = curpairs->apairs_ent[pix].asso_val;
if (curkeyob && curval)
{
ASSERT_BM (keycnt < nbkeys);
arr[keycnt++] = curkeyob;
}
}
}
else
FATAL_BM ("unexpected assoc @%p", assoc);
ASSERT_BM (keycnt == nbkeys);
const setval_tyBM *keyset = makeset_BM (arr, keycnt);
ASSERT_BM (setcardinal_BM (keyset) == nbkeys);
if (arr != tinyarr)
free (arr);
return keyset;
} /* end assoc_setattrs_BM */
value_tyBM
assoc_getattr_BM (const anyassoc_tyBM * assoc, const objectval_tyBM * obattr)
{
if (!isassoc_BM ((const value_tyBM) assoc))
return NULL;
if (valtype_BM ((const value_tyBM) obattr) != tyObject_BM)
return NULL;
const struct assocpairs_stBM *curpairs = NULL;
if (valtype_BM ((const value_tyBM) assoc) == typayl_assoctable_BM)
{
unsigned buckix = assoc_buckix_for_key_BM (assoc, obattr);
curpairs =
((const struct assoctable_stBM *) assoc)->abuck_pairs[buckix];
ASSERT_BM (!curpairs
|| valtype_BM ((const value_tyBM) curpairs) ==
typayl_assocpairs_BM);
}
else if (valtype_BM ((const value_tyBM) assoc) == typayl_assocpairs_BM)
curpairs = assoc;
if (!curpairs)
return NULL;
unsigned nbent = ((typedhead_tyBM *) curpairs)->rlen;
for (unsigned pix = 0; pix < nbent; pix++)
{
const objectval_tyBM *curkeyob = curpairs->apairs_ent[pix].asso_keyob;
value_tyBM curval = curpairs->apairs_ent[pix].asso_val;
if (curkeyob == obattr)
return curval;
}
return NULL;
} /* end assoc_getattr_BM */
anyassoc_tyBM *
assoc_addattr_BM (anyassoc_tyBM * assoc,
const objectval_tyBM * obattr, value_tyBM val)
{
if (valtype_BM ((const value_tyBM) obattr) != tyObject_BM
|| !isgenuineval_BM (val))
{
if (!isassoc_BM (assoc))
return NULL;
return assoc;
}
if (!isassoc_BM (assoc))
{
return assocpair_put_BM (NULL, obattr, val);
}
unsigned nbkeys = assoc_nbkeys_BM (assoc);
if (valtype_BM ((const value_tyBM) assoc) == typayl_assocpairs_BM)
{
struct assocpairs_stBM *curpairs = assoc;
if (nbkeys < TINYSIZE_BM)
{
return assocpair_put_BM (curpairs, obattr, val);
};
assoc_reorganize_BM (&assoc, 2 + nbkeys / 8);
};
if (valtype_BM ((const value_tyBM) assoc) == typayl_assoctable_BM)
{
struct assoctable_stBM *abuck = (struct assoctable_stBM *) assoc;
unsigned nbbuckets = ((typedhead_tyBM *) abuck)->rlen;
if (nbkeys > nbbuckets * TINYSIZE_BM)
assoc_reorganize_BM (&assoc, 3 + nbkeys / 64);
};
if (valtype_BM ((const value_tyBM) assoc) == typayl_assocpairs_BM)
{
return assocpair_put_BM ((struct assocpairs_stBM *) assoc, obattr, val);
}
else if (valtype_BM ((const value_tyBM) assoc) == typayl_assoctable_BM)
{
unsigned buckix = assoc_buckix_for_key_BM (assoc, obattr);
struct assocpairs_stBM *curpairs =
((const struct assoctable_stBM *) assoc)->abuck_pairs[buckix];
ASSERT_BM (!curpairs
|| valtype_BM ((const value_tyBM) curpairs) ==
typayl_assocpairs_BM);
unsigned oldpaircnt =
curpairs ? ((typedsize_tyBM *) curpairs)->size : 0;
struct assocpairs_stBM *newpairs =
assocpair_put_BM (curpairs, obattr, val);
if (newpairs != curpairs)
((struct assoctable_stBM *) assoc)->abuck_pairs[buckix] = newpairs;
unsigned newpaircnt =
newpairs ? ((typedsize_tyBM *) newpairs)->size : 0;
if (newpaircnt > oldpaircnt)
{
ASSERT_BM (newpaircnt == oldpaircnt + 1);
((typedsize_tyBM *) assoc)->size = nbkeys + 1;
}
return assoc;
}
// should never be reached
FATAL_BM ("something corrupted in assoc@%p", assoc);
} /* end assoc_addattr_BM */
anyassoc_tyBM *
assoc_removeattr_BM (anyassoc_tyBM * assoc, const objectval_tyBM * obattr)
{
if (!isassoc_BM (assoc))
return NULL;
if (valtype_BM ((const value_tyBM) obattr) != tyObject_BM)
return assoc;
unsigned nbkeys = assoc_nbkeys_BM (assoc);
int assotyp = valtype_BM ((const value_tyBM) assoc);
if (assotyp == typayl_assoctable_BM)
{
struct assoctable_stBM *atable = (struct assoctable_stBM *) assoc;
unsigned buckix = assoc_buckix_for_key_BM (assoc, obattr);
struct assocpairs_stBM *curpairs =
((const struct assoctable_stBM *) assoc)->abuck_pairs[buckix];
ASSERT_BM (!curpairs
|| valtype_BM ((const value_tyBM) curpairs) ==
typayl_assocpairs_BM);
if (curpairs)
{
unsigned busiz = ASSOCPAIRSIZE_BM (curpairs);
for (unsigned pix = 0; pix < busiz; pix++)
{
const objectval_tyBM *curkeyob =
curpairs->apairs_ent[pix].asso_keyob;
if (curkeyob == obattr)
{
curpairs->apairs_ent[pix].asso_keyob = NULL;
curpairs->apairs_ent[pix].asso_val = NULL;
ASSOCPAIRUCNT_BM (curpairs)--;
ASSOCTABLECUMCNT_BM (atable)--;
nbkeys--;
goto perhapsreorganize;
}
}
};
return atable;
}
else if (assotyp == typayl_assocpairs_BM)
{
struct assocpairs_stBM *curpairs = assoc;
unsigned pairsiz = ASSOCPAIRSIZE_BM (curpairs);
unsigned cnt = ASSOCPAIRUCNT_BM (curpairs);
for (unsigned pix = 0; pix < pairsiz; pix++)
{
const objectval_tyBM *curkeyob =
curpairs->apairs_ent[pix].asso_keyob;
if (curkeyob == obattr)
{
ASSERT_BM (cnt > 0);
curpairs->apairs_ent[pix].asso_keyob = NULL;
curpairs->apairs_ent[pix].asso_val = NULL;
ASSOCPAIRUCNT_BM (curpairs) = cnt - 1;
nbkeys--;
goto perhapsreorganize;
}
};
return curpairs;
}
else
FATAL_BM ("assoc_removeattr_BM: invalid assotyp #%d assoc@%p", assotyp,
(void *) assoc);
perhapsreorganize:
if (assotyp == typayl_assoctable_BM)
{
if (ASSOCTABLECUMCNT_BM (assoc) <= 2 * TINYSIZE_BM
|| (3 * ASSOCTABLECUMCNT_BM (assoc)) / 2 <
TINYSIZE_BM * ASSOCTABLESIZE_BM (assoc))
{
unsigned aucnt = ASSOCTABLECUMCNT_BM (assoc);
unsigned apsiz = ASSOCTABLESIZE_BM (assoc);
ASSERT_BM (aucnt == nbkeys);
ASSERT_BM (aucnt < apsiz);
unsigned gap = 1 + ILOG2_BM (aucnt + 1);
assoc_reorganize_BM (&assoc, gap);
return assoc;
}
}
else
{
ASSERT_BM (assotyp == typayl_assocpairs_BM);
unsigned aucnt = ASSOCPAIRUCNT_BM (assoc);
unsigned apsiz = ASSOCPAIRSIZE_BM (assoc);
ASSERT_BM (aucnt <= apsiz);
if (aucnt < 3 * apsiz)
{
ASSERT_BM (aucnt == nbkeys);
unsigned gap = 2 + aucnt / 64 + ILOG2_BM (aucnt + 1);
assoc_reorganize_BM (&assoc, gap);
return assoc;
}
}
return assoc;
} /* end assoc_removeattr_BM */
void *
assocpairsgcproc_BM (struct garbcoll_stBM *gc,
struct assocpairs_stBM *apairs,
objectval_tyBM * fromob, int depth)
{
ASSERT_BM (gc && gc->gc_magic == GCMAGIC_BM);
ASSERT_BM (valtype_BM (apairs) == typayl_assocpairs_BM);
ASSERT_BM (!fromob || isobject_BM (fromob));
uint8_t oldmark = ((typedhead_tyBM *) apairs)->hgc;
if (oldmark)
return apairs;
((typedhead_tyBM *) apairs)->hgc = MARKGC_BM;
unsigned pairsiz = ASSOCPAIRSIZE_BM (apairs);
for (unsigned pix = 0; pix < pairsiz; pix++)
{
const objectval_tyBM *curkeyob = apairs->apairs_ent[pix].asso_keyob;
if (!curkeyob || curkeyob == HASHEMPTYSLOT_BM)
{
apairs->apairs_ent[pix].asso_val = NULL;
continue;
};
gcobjmark_BM (gc, (objectval_tyBM *) curkeyob);
value_tyBM curval = apairs->apairs_ent[pix].asso_val;
if (curval)
{
VALUEGCPROC_BM (gc, curval, depth + 1);
apairs->apairs_ent[pix].asso_val = curval;
}
}
gc->gc_nbmarks++;
return apairs;
} /* end assocpairsgcproc_BM */
void *
assoctablegcproc_BM (struct garbcoll_stBM *gc,
struct assoctable_stBM *atable,
objectval_tyBM * fromob, int depth)
{
ASSERT_BM (gc && gc->gc_magic == GCMAGIC_BM);
ASSERT_BM (valtype_BM (atable) == typayl_assoctable_BM);
ASSERT_BM (!fromob || isobject_BM (fromob));
unsigned tblsiz = ASSOCTABLESIZE_BM (atable);
for (unsigned bix = 0; bix < tblsiz; bix++)
{
struct assocpairs_stBM *curbuckpair = atable->abuck_pairs[bix];
if (!curbuckpair)
continue;
ASSERT_BM (valtype_BM (curbuckpair) == typayl_assocpairs_BM);
uint8_t oldbuckmark = ((typedhead_tyBM *) curbuckpair)->hgc;
if (oldbuckmark)
continue;
((typedhead_tyBM *) curbuckpair)->hgc = MARKGC_BM;
gc->gc_nbmarks++;
unsigned bucksiz = ASSOCPAIRSIZE_BM (curbuckpair);
for (unsigned pix = 0; pix < bucksiz; pix++)
{
const objectval_tyBM *curkeyob =
curbuckpair->apairs_ent[pix].asso_keyob;
if (!curkeyob || curkeyob == HASHEMPTYSLOT_BM)
{
curbuckpair->apairs_ent[pix].asso_val = NULL;
continue;
}
gcobjmark_BM (gc, (objectval_tyBM *) curkeyob);
value_tyBM curval = curbuckpair->apairs_ent[pix].asso_val;
if (curval)
{
VALUEGCPROC_BM (gc, curval, depth + 1);
curbuckpair->apairs_ent[pix].asso_val = curval;
}
}
}
return atable;
} /* end assoctablegcproc_BM */
void *
assocgcproc_BM (struct garbcoll_stBM *gc, anyassoc_tyBM * assoc,
objectval_tyBM * fromob, int depth)
{
ASSERT_BM (gc && gc->gc_magic == GCMAGIC_BM);
ASSERT_BM (!fromob || isobject_BM (fromob));
int aty = valtype_BM (assoc);
if (aty == typayl_assoctable_BM)
return assoctablegcproc_BM (gc, (struct assoctable_stBM *) assoc, fromob,
depth);
else if (aty == typayl_assocpairs_BM)
return assocpairsgcproc_BM (gc, (struct assocpairs_stBM *) assoc, fromob,
depth);
else
FATAL_BM ("assogcproc_BM: invalid assoc@%p type#%d fromob %s, depth#%d",
(void *) assoc, aty, objectdbg_BM (fromob), depth);
} /* end assocgcproc_BM */
///// ASSOC PAYLOAD
bool
objputassocpayl_BM (objectval_tyBM * obj, unsigned maxsize)
{
if (!isobject_BM ((value_tyBM) obj))
return false;
if (maxsize > MAXSIZE_BM)
FATAL_BM ("objputassocpayl_BM too big maxsize %u for obj %s", maxsize,
objectdbg_BM (obj));
anyassoc_tyBM *asso = make_assoc_BM (maxsize);
objputpayload_BM (obj, asso);
if (objclass_BM (obj) == BMP_object)
objputclass_BM (obj, BMP_assoc_object);
return true;
} /* end objputassocpayl_BM */
/*********** HASH SET VALUES **********/
void
hashsetvalgcmark_BM (struct garbcoll_stBM *gc, struct hashsetval_stBM *hsv,
objectval_tyBM * fromob, int depth)
{
ASSERT_BM (gc && gc->gc_magic == GCMAGIC_BM);
ASSERT_BM (ishashsetval_BM ((value_tyBM) hsv));
ASSERT_BM (fromob != NULL || isobject_BM (fromob));
uint8_t oldmark = ((typedhead_tyBM *) hsv)->hgc;
if (oldmark)
return;
((typedhead_tyBM *) hsv)->hgc = MARKGC_BM;
gc->gc_nbmarks++;
unsigned siz = ((typedsize_tyBM *) hsv)->size;
for (unsigned ix = 0; ix < siz; ix++)
{
struct hashsetvbucket_stBM *vbu = hsv->hashval_vbuckets[ix];
if (!vbu || vbu == HASHEMPTYSLOT_BM)
continue;
ASSERT_BM (valtype_BM ((value_tyBM) vbu) == typayl_hashsetvbucket_BM);
EXTENDEDGCPROC_BM (gc, vbu, fromob, depth + 1);
}
} /* end hashsetvalgcmark_BM */
void
hashsetvbucketgcmark_BM (struct garbcoll_stBM *gc,
struct hashsetvbucket_stBM *hvb,
objectval_tyBM * fromob, int depth)
{
ASSERT_BM (gc && gc->gc_magic == GCMAGIC_BM);
ASSERT_BM (ishashsetvbucket_BM ((value_tyBM) hvb));
ASSERT_BM (fromob == NULL || isobject_BM (fromob));
uint8_t oldmark = ((typedhead_tyBM *) hvb)->hgc;
if (oldmark)
return;
((typedhead_tyBM *) hvb)->hgc = MARKGC_BM;
gc->gc_nbmarks++;
unsigned siz = ((typedsize_tyBM *) hvb)->size;
for (unsigned ix = 0; ix < siz; ix++)
{
value_tyBM cval = hvb->vbuck_arr[ix];
if (!cval || cval == HASHEMPTYSLOT_BM)
continue;
VALUEGCPROC_BM (gc, hvb->vbuck_arr[ix], depth + 1);
}
} /* end hashsetvbucketgcmark_BM */
void
hashsetvalgcdestroy_BM (struct garbcoll_stBM *gc, struct hashsetval_stBM *hsv)
{
ASSERT_BM (gc && gc->gc_magic == GCMAGIC_BM);
ASSERT_BM (((typedhead_tyBM *) hsv)->htyp == typayl_hashsetval_BM);
unsigned len = ((typedhead_tyBM *) hsv)->rlen;
memset (hsv, 0,
sizeof (*hsv) + len * sizeof (struct hashsetvbucket_stBM *));
free (hsv);
gc->gc_freedbytes +=
sizeof (*hsv) + len * sizeof (struct hashsetvbucket_stBM *);
} /* end hashsetvalgcdestroy_BM */
void
hashsetvbucketgcdestroy_BM (struct garbcoll_stBM *gc,
struct hashsetvbucket_stBM *hvb)
{
ASSERT_BM (gc && gc->gc_magic == GCMAGIC_BM);
ASSERT_BM (((typedhead_tyBM *) hvb)->htyp == typayl_hashsetvbucket_BM);
unsigned len = ((typedhead_tyBM *) hvb)->rlen;
memset (hvb, 0, sizeof (*hvb) + len * sizeof (value_tyBM));
free (hvb);
gc->gc_freedbytes += sizeof (*hvb) + len * sizeof (value_tyBM);
} /* end hashsetvbucketgcdsestroy_BM */
void
hashsetvalgckeep_BM (struct garbcoll_stBM *gc, struct hashsetval_stBM *hsv)
{
ASSERT_BM (gc && gc->gc_magic == GCMAGIC_BM);
ASSERT_BM (((typedhead_tyBM *) hsv)->htyp == typayl_hashsetval_BM);
unsigned len = ((typedhead_tyBM *) hsv)->rlen;
ASSERT_BM (len < MAXSIZE_BM);
gc->gc_keptbytes +=
sizeof (*hsv) + len * sizeof (struct hashsetvbucket_stBM *);
} /* end hashsetvalgckeep_BM */
void
hashsetvbucketgckeep_BM (struct garbcoll_stBM *gc,
struct hashsetvbucket_stBM *hvb)
{
ASSERT_BM (gc && gc->gc_magic == GCMAGIC_BM);
ASSERT_BM (((typedhead_tyBM *) hvb)->htyp == typayl_hashsetvbucket_BM);
unsigned len = ((typedhead_tyBM *) hvb)->rlen;
ASSERT_BM (len < MAXSIZE_BM);
gc->gc_keptbytes += sizeof (*hvb) + len * sizeof (value_tyBM);
} /* end hashsetvbucketgckeep_BM */
#define HASHRATIO_BM 7
#define HASHTHRESHOLD_BM (5*HASHRATIO_BM/3+2)
struct hashpairindexes_stBM
{
int hvi_buckix; /* index of bucket */
int hvi_compix; /* index of component */
};
#define EMPTYPAIRINDEXES_BM ((struct hashpairindexes_stBM){-1,-1})
// give the indexes of a value; if the value is present, give the
// actual indexes; if the value is absent, give the index where it
// should go
static struct hashpairindexes_stBM
hashsetvalfindindexes_BM (struct hashsetval_stBM *hsv, value_tyBM val)
{
ASSERT_BM (hsv && ((typedhead_tyBM *) hsv)->htyp == typayl_hashsetval_BM);
if (!val)
return EMPTYPAIRINDEXES_BM;
unsigned len = ((typedhead_tyBM *) hsv)->rlen;
ASSERT_BM (len < MAXSIZE_BM && len > 2);
hash_tyBM hva = valhash_BM (val);
unsigned bix = hva % len;
struct hashsetvbucket_stBM *curbuck = hsv->hashval_vbuckets[bix];
if (!curbuck)
return (struct hashpairindexes_stBM)
{
.hvi_buckix = bix, //
.hvi_compix = -1
};
ASSERT_BM (((typedhead_tyBM *) curbuck)->htyp == typayl_hashsetvbucket_BM);
unsigned bucklen = ((typedhead_tyBM *) curbuck)->rlen;
int buckpos = -1;
for (unsigned vix = 0; vix < bucklen; vix++)
{
value_tyBM curval = curbuck->vbuck_arr[vix];
if (!curval)
{
if (buckpos < 0)
buckpos = (int) vix;
return (struct hashpairindexes_stBM)
{
.hvi_buckix = bix, //
.hvi_compix = vix
};
}
else if (curval == (value_tyBM) HASHEMPTYSLOT_BM)
{
if (buckpos < 0)
buckpos = (int) vix;
continue;
}
else if (curval == val || valequal_BM (curval, val))
{
return (struct hashpairindexes_stBM)
{
.hvi_buckix = bix, //
.hvi_compix = vix
};
}
}
return EMPTYPAIRINDEXES_BM;
} /* end hashsetvalfindindexes_BM */
bool
hashsetvalcontains_BM (struct hashsetval_stBM *hsv, value_tyBM val)
{
if (!hsv || valtype_BM ((value_tyBM) hsv) != typayl_hashsetval_BM)
return false;
if (!val)
return false;
struct hashpairindexes_stBM hvindexes = hashsetvalfindindexes_BM (hsv, val);
int bix = hvindexes.hvi_buckix;
int compix = hvindexes.hvi_compix;
if (bix < 0 || compix < 0)
return false;
unsigned hslen = ((typedhead_tyBM *) hsv)->rlen;
ASSERT_BM (bix < (int) hslen);
struct hashsetvbucket_stBM *curbuck = hsv->hashval_vbuckets[bix];
ASSERT_BM (curbuck != NULL);
unsigned bucklen = ((typedhead_tyBM *) curbuck)->rlen;
ASSERT_BM (compix < (int) bucklen);
value_tyBM curval = curbuck->vbuck_arr[compix];
if (curval && curval != (value_tyBM) HASHEMPTYSLOT_BM
&& (curval == val || valequal_BM (curval, val)))
return true;
return false;
} /* end hashsetvalcontains_BM */
#define HASHSETVAL_INITBUCKETSIZE_BM 5
static void hashsetvalrawadd_BM (struct hashsetval_stBM *hsv, value_tyBM val);
static void
hashsetvalrawadd_BM (struct hashsetval_stBM *hsv, value_tyBM val)
{
if (!hsv || valtype_BM ((value_tyBM) hsv) != typayl_hashsetval_BM)
return;
if (!val)
return;
struct hashpairindexes_stBM hvindexes = hashsetvalfindindexes_BM (hsv, val);
int bix = hvindexes.hvi_buckix;
int compix = hvindexes.hvi_compix;
if (bix < 0)
return;
unsigned hslen = ((typedhead_tyBM *) hsv)->rlen;
ASSERT_BM (bix < (int) hslen);
struct hashsetvbucket_stBM *curbuck = hsv->hashval_vbuckets[bix];
if (!curbuck)
{
ASSERT_BM (compix < 0);
unsigned newsiz = HASHSETVAL_INITBUCKETSIZE_BM;
struct hashsetvbucket_stBM *newbuck
= allocgcty_BM (typayl_hashsetvbucket_BM,
sizeof (struct hashsetvbucket_stBM) +
newsiz * sizeof (value_tyBM));
((typedhead_tyBM *) newbuck)->rlen = newsiz;
hsv->hashval_vbuckets[bix] = newbuck;
newbuck->vbuck_arr[0] = val;
((struct typedsize_stBM *) newbuck)->size = 1;
((struct typedsize_stBM *) hsv)->size++;
return;
}
unsigned bucklen = ((typedhead_tyBM *) curbuck)->rlen;
if (compix >= 0)
{
ASSERT_BM (compix < (int) bucklen);
if (curbuck->vbuck_arr[compix] == NULL
|| curbuck->vbuck_arr[compix] == (value_tyBM) HASHEMPTYSLOT_BM)
{
curbuck->vbuck_arr[compix] = val;
((struct typedsize_stBM *) curbuck)->size++;
((struct typedsize_stBM *) hsv)->size++;
}
else
{