This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 426
/
aaA.d
985 lines (844 loc) · 24.1 KB
/
aaA.d
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
/**
* Implementation of associative arrays.
*
* Copyright: Copyright Digital Mars 2000 - 2010.
* License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
* Authors: Walter Bright, Sean Kelly
*/
/* Copyright Digital Mars 2000 - 2010.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*/
module rt.aaA;
private
{
import core.stdc.stdarg;
import core.stdc.string;
import core.stdc.stdio;
import core.memory;
// Convenience function to make sure the NO_INTERIOR gets set on the
// bucket array.
Entry*[] newBuckets(size_t len)
{
auto ptr = cast(Entry**) GC.calloc(
len * (Entry*).sizeof, GC.BlkAttr.NO_INTERIOR);
return ptr[0..len];
}
}
// Auto-rehash and pre-allocate - Dave Fladebo
static immutable size_t[] prime_list = [
31UL,
97UL, 389UL,
1_543UL, 6_151UL,
24_593UL, 98_317UL,
393_241UL, 1_572_869UL,
6_291_469UL, 25_165_843UL,
100_663_319UL, 402_653_189UL,
1_610_612_741UL, 4_294_967_291UL,
// 8_589_934_513UL, 17_179_869_143UL
];
/* This is the type of the return value for dynamic arrays.
* It should be a type that is returned in registers.
* Although DMD will return types of Array in registers,
* gcc will not, so we instead use a 'long'.
*/
alias void[] ArrayRet_t;
struct Array
{
size_t length;
void* ptr;
}
struct Entry
{
Entry *next;
size_t hash;
/* key */
/* value */
}
struct Impl
{
Entry*[] buckets;
size_t nodes; // total number of entries
TypeInfo keyti; // TODO: replace this with TypeInfo_AssociativeArray when available in _aaGet()
Entry*[4] binit; // initial value of buckets[]
}
/* This is the type actually seen by the programmer, although
* it is completely opaque.
*/
struct AA
{
Impl* impl;
}
/**********************************
* Align to next pointer boundary, so that
* GC won't be faced with misaligned pointers
* in value.
*/
size_t aligntsize(size_t tsize) nothrow
{
version (D_LP64) {
// align to 16 bytes on 64-bit
return (tsize + 15) & ~(15);
}
else {
return (tsize + size_t.sizeof - 1) & ~(size_t.sizeof - 1);
}
}
extern (C):
/*************************************************
* Invariant for aa.
*/
/+
void _aaInvAh(Entry*[] aa)
{
for (size_t i = 0; i < aa.length; i++)
{
if (aa[i])
_aaInvAh_x(aa[i]);
}
}
private int _aaCmpAh_x(Entry *e1, Entry *e2)
{ int c;
c = e1.hash - e2.hash;
if (c == 0)
{
c = e1.key.length - e2.key.length;
if (c == 0)
c = memcmp((char *)e1.key, (char *)e2.key, e1.key.length);
}
return c;
}
private void _aaInvAh_x(Entry *e)
{
size_t key_hash;
Entry *e1;
Entry *e2;
key_hash = getHash(e.key);
assert(key_hash == e.hash);
while (1)
{ int c;
e1 = e.left;
if (e1)
{
_aaInvAh_x(e1); // ordinary recursion
do
{
c = _aaCmpAh_x(e1, e);
assert(c < 0);
e1 = e1.right;
} while (e1 != null);
}
e2 = e.right;
if (e2)
{
do
{
c = _aaCmpAh_x(e, e2);
assert(c < 0);
e2 = e2.left;
} while (e2 != null);
e = e.right; // tail recursion
}
else
break;
}
}
+/
/****************************************************
* Determine number of entries in associative array.
*/
size_t _aaLen(AA aa)
in
{
//printf("_aaLen()+\n");
//_aaInv(aa);
}
out (result)
{
size_t len = 0;
if (aa.impl)
{
foreach (e; aa.impl.buckets)
{
while (e)
{ len++;
e = e.next;
}
}
}
assert(len == result);
//printf("_aaLen()-\n");
}
body
{
return aa.impl ? aa.impl.nodes : 0;
}
/*************************************************
* Get pointer to value in associative array indexed by key.
* Add entry for key if it is not already there.
*/
// retained for backwards compatibility
void* _aaGet(AA* aa, TypeInfo keyti, size_t valuesize, ...)
{
return _aaGetX(aa, keyti, valuesize, cast(void*)(&valuesize + 1));
}
// NOTE: TypeInfo can't be `in` here as it is stored in `aa.impl.keyti`.
void* _aaGetX(AA* aa, TypeInfo keyti, size_t valuesize, void* pkey)
in
{
assert(aa);
}
out (result)
{
assert(result);
assert(aa.impl !is null);
assert(aa.impl.buckets.length);
//assert(_aaInAh(*aa.a, key));
}
body
{
size_t i;
Entry *e;
//printf("keyti = %p\n", keyti);
//printf("aa = %p\n", aa);
immutable keytitsize = keyti.tsize;
if (aa.impl is null)
{ aa.impl = new Impl();
aa.impl.buckets = aa.impl.binit[];
}
//printf("aa = %p\n", aa);
//printf("aa.a = %p\n", aa.a);
aa.impl.keyti = keyti;
auto key_hash = keyti.getHash(pkey);
//printf("hash = %d\n", key_hash);
i = key_hash % aa.impl.buckets.length;
auto pe = &aa.impl.buckets[i];
while ((e = *pe) !is null)
{
if (key_hash == e.hash)
{
auto c = keyti.compare(pkey, e + 1);
if (c == 0)
goto Lret;
}
pe = &e.next;
}
// Not found, create new elem
//printf("create new one\n");
size_t size = Entry.sizeof + aligntsize(keytitsize) + valuesize;
e = cast(Entry *) GC.malloc(size);
e.next = null;
e.hash = key_hash;
ubyte* ptail = cast(ubyte*)(e + 1);
memcpy(ptail, pkey, keytitsize);
memset(ptail + aligntsize(keytitsize), 0, valuesize); // zero value
*pe = e;
auto nodes = ++aa.impl.nodes;
//printf("length = %d, nodes = %d\n", aa.a.buckets.length, nodes);
if (nodes > aa.impl.buckets.length * 4)
{
//printf("rehash\n");
_aaRehash(aa,keyti);
}
Lret:
return cast(void *)(e + 1) + aligntsize(keytitsize);
}
/*************************************************
* Get pointer to value in associative array indexed by key.
* Returns null if it is not already there.
*/
void* _aaGetRvalue(AA aa, in TypeInfo keyti, size_t valuesize, ...)
{
return _aaGetRvalueX(aa, keyti, valuesize, cast(void*)(&valuesize + 1));
}
void* _aaGetRvalueX(AA aa, in TypeInfo keyti, size_t valuesize, void* pkey)
{
//printf("_aaGetRvalue(valuesize = %u)\n", valuesize);
if (aa.impl is null)
return null;
auto keysize = aligntsize(keyti.tsize);
auto len = aa.impl.buckets.length;
if (len)
{
auto key_hash = keyti.getHash(pkey);
//printf("hash = %d\n", key_hash);
size_t i = key_hash % len;
auto e = aa.impl.buckets[i];
while (e !is null)
{
if (key_hash == e.hash)
{
auto c = keyti.compare(pkey, e + 1);
if (c == 0)
return cast(void *)(e + 1) + keysize;
}
e = e.next;
}
}
return null; // not found, caller will throw exception
}
/*************************************************
* Determine if key is in aa.
* Returns:
* null not in aa
* !=null in aa, return pointer to value
*/
void* _aaIn(AA aa, in TypeInfo keyti, ...)
{
return _aaInX(aa, keyti, cast(void*)(&keyti + 1));
}
void* _aaInX(AA aa, in TypeInfo keyti, void* pkey)
in
{
}
out (result)
{
//assert(result == 0 || result == 1);
}
body
{
if (aa.impl)
{
//printf("_aaIn(), .length = %d, .ptr = %x\n", aa.a.length, cast(uint)aa.a.ptr);
auto len = aa.impl.buckets.length;
if (len)
{
auto key_hash = keyti.getHash(pkey);
//printf("hash = %d\n", key_hash);
const i = key_hash % len;
auto e = aa.impl.buckets[i];
while (e !is null)
{
if (key_hash == e.hash)
{
auto c = keyti.compare(pkey, e + 1);
if (c == 0)
return cast(void *)(e + 1) + aligntsize(keyti.tsize);
}
e = e.next;
}
}
}
// Not found
return null;
}
/*************************************************
* Delete key entry in aa[].
* If key is not in aa[], do nothing.
*/
bool _aaDel(AA aa, in TypeInfo keyti, ...)
{
return _aaDelX(aa, keyti, cast(void*)(&keyti + 1));
}
bool _aaDelX(AA aa, in TypeInfo keyti, void* pkey)
{
Entry *e;
if (aa.impl && aa.impl.buckets.length)
{
auto key_hash = keyti.getHash(pkey);
//printf("hash = %d\n", key_hash);
size_t i = key_hash % aa.impl.buckets.length;
auto pe = &aa.impl.buckets[i];
while ((e = *pe) !is null) // null means not found
{
if (key_hash == e.hash)
{
auto c = keyti.compare(pkey, e + 1);
if (c == 0)
{
*pe = e.next;
aa.impl.nodes--;
GC.free(e);
return true;
}
}
pe = &e.next;
}
}
return false;
}
/********************************************
* Produce array of values from aa.
*/
ArrayRet_t _aaValues(AA aa, size_t keysize, size_t valuesize)
{
size_t resi;
Array a;
auto alignsize = aligntsize(keysize);
if (aa.impl !is null)
{
a.length = _aaLen(aa);
a.ptr = cast(byte*) GC.malloc(a.length * valuesize,
valuesize < (void*).sizeof ? GC.BlkAttr.NO_SCAN : 0);
resi = 0;
foreach (e; aa.impl.buckets)
{
while (e)
{
memcpy(a.ptr + resi * valuesize,
cast(byte*)e + Entry.sizeof + alignsize,
valuesize);
resi++;
e = e.next;
}
}
assert(resi == a.length);
}
return *cast(ArrayRet_t*)(&a);
}
/********************************************
* Rehash an array.
*/
void* _aaRehash(AA* paa, in TypeInfo keyti)
in
{
//_aaInvAh(paa);
}
out (result)
{
//_aaInvAh(result);
}
body
{
//printf("Rehash\n");
if (paa.impl !is null)
{
Impl newImpl;
Impl* oldImpl = paa.impl;
auto len = _aaLen(*paa);
if (len)
{ size_t i;
for (i = 0; i < prime_list.length - 1; i++)
{
if (len <= prime_list[i])
break;
}
len = prime_list[i];
newImpl.buckets = newBuckets(len);
foreach (e; oldImpl.buckets)
{
while (e)
{ auto enext = e.next;
const j = e.hash % len;
e.next = newImpl.buckets[j];
newImpl.buckets[j] = e;
e = enext;
}
}
if (oldImpl.buckets.ptr == oldImpl.binit.ptr)
oldImpl.binit[] = null;
else
GC.free(oldImpl.buckets.ptr);
newImpl.nodes = oldImpl.nodes;
newImpl.keyti = oldImpl.keyti;
}
*paa.impl = newImpl;
}
return (*paa).impl;
}
/********************************************
* Produce array of N byte keys from aa.
*/
ArrayRet_t _aaKeys(AA aa, size_t keysize)
{
auto len = _aaLen(aa);
if (!len)
return null;
immutable blkAttr = !(aa.impl.keyti.flags & 1) ? GC.BlkAttr.NO_SCAN : 0;
auto res = (cast(byte*) GC.malloc(len * keysize, blkAttr))[0 .. len * keysize];
size_t resi = 0;
foreach (e; aa.impl.buckets)
{
while (e)
{
memcpy(&res[resi * keysize], cast(byte*)(e + 1), keysize);
resi++;
e = e.next;
}
}
assert(resi == len);
Array a;
a.length = len;
a.ptr = res.ptr;
return *cast(ArrayRet_t*)(&a);
}
unittest
{
int[string] aa;
aa["hello"] = 3;
assert(aa["hello"] == 3);
aa["hello"]++;
assert(aa["hello"] == 4);
assert(aa.length == 1);
string[] keys = aa.keys;
assert(keys.length == 1);
assert(memcmp(keys[0].ptr, cast(char*)"hello", 5) == 0);
int[] values = aa.values;
assert(values.length == 1);
assert(values[0] == 4);
aa.rehash;
assert(aa.length == 1);
assert(aa["hello"] == 4);
aa["foo"] = 1;
aa["bar"] = 2;
aa["batz"] = 3;
assert(aa.keys.length == 4);
assert(aa.values.length == 4);
foreach(a; aa.keys)
{
assert(a.length != 0);
assert(a.ptr != null);
//printf("key: %.*s -> value: %d\n", a.length, a.ptr, aa[a]);
}
foreach(v; aa.values)
{
assert(v != 0);
//printf("value: %d\n", v);
}
}
/**********************************************
* 'apply' for associative arrays - to support foreach
*/
// dg is D, but _aaApply() is C
extern (D) alias int delegate(void *) dg_t;
int _aaApply(AA aa, size_t keysize, dg_t dg)
{
if (aa.impl is null)
{
return 0;
}
immutable alignsize = aligntsize(keysize);
//printf("_aaApply(aa = x%llx, keysize = %d, dg = x%llx)\n", aa.impl, keysize, dg);
foreach (e; aa.impl.buckets)
{
while (e)
{
auto result = dg(cast(void *)(e + 1) + alignsize);
if (result)
return result;
e = e.next;
}
}
return 0;
}
// dg is D, but _aaApply2() is C
extern (D) alias int delegate(void *, void *) dg2_t;
int _aaApply2(AA aa, size_t keysize, dg2_t dg)
{
if (aa.impl is null)
{
return 0;
}
//printf("_aaApply(aa = x%llx, keysize = %d, dg = x%llx)\n", aa.impl, keysize, dg);
immutable alignsize = aligntsize(keysize);
foreach (e; aa.impl.buckets)
{
while (e)
{
auto result = dg(e + 1, cast(void *)(e + 1) + alignsize);
if (result)
return result;
e = e.next;
}
}
return 0;
}
/***********************************
* Construct an associative array of type ti from
* length pairs of key/value pairs.
*/
extern (C)
Impl* _d_assocarrayliteralT(TypeInfo_AssociativeArray ti, size_t length, ...)
{
auto valuesize = ti.next.tsize; // value size
auto keyti = ti.key;
auto keysize = keyti.tsize; // key size
Impl* result;
//printf("_d_assocarrayliteralT(keysize = %d, valuesize = %d, length = %d)\n", keysize, valuesize, length);
//printf("tivalue = %.*s\n", ti.next.classinfo.name);
if (length == 0 || valuesize == 0 || keysize == 0)
{
}
else
{
va_list q;
version (Win64)
va_start(q, length);
else version(X86_64)
va_start(q, __va_argsave);
else
va_start(q, length);
result = new Impl();
result.keyti = keyti;
size_t i;
for (i = 0; i < prime_list.length - 1; i++)
{
if (length <= prime_list[i])
break;
}
auto len = prime_list[i];
result.buckets = newBuckets(len);
size_t keystacksize = (keysize + int.sizeof - 1) & ~(int.sizeof - 1);
size_t valuestacksize = (valuesize + int.sizeof - 1) & ~(int.sizeof - 1);
size_t keytsize = aligntsize(keysize);
for (size_t j = 0; j < length; j++)
{ void* pkey = q;
q += keystacksize;
void* pvalue = q;
q += valuestacksize;
Entry* e;
auto key_hash = keyti.getHash(pkey);
//printf("hash = %d\n", key_hash);
i = key_hash % len;
auto pe = &result.buckets[i];
while (1)
{
e = *pe;
if (!e)
{
// Not found, create new elem
//printf("create new one\n");
e = cast(Entry *) cast(void*) new void[Entry.sizeof + keytsize + valuesize];
memcpy(e + 1, pkey, keysize);
e.hash = key_hash;
*pe = e;
result.nodes++;
break;
}
if (key_hash == e.hash)
{
auto c = keyti.compare(pkey, e + 1);
if (c == 0)
break;
}
pe = &e.next;
}
memcpy(cast(void *)(e + 1) + keytsize, pvalue, valuesize);
}
va_end(q);
}
return result;
}
extern (C)
Impl* _d_assocarrayliteralTX(TypeInfo_AssociativeArray ti, void[] keys, void[] values)
{
auto valuesize = ti.next.tsize; // value size
auto keyti = ti.key;
auto keysize = keyti.tsize; // key size
auto length = keys.length;
Impl* result;
//printf("_d_assocarrayliteralT(keysize = %d, valuesize = %d, length = %d)\n", keysize, valuesize, length);
//printf("tivalue = %.*s\n", ti.next.classinfo.name);
assert(length == values.length);
if (length == 0 || valuesize == 0 || keysize == 0)
{
}
else
{
result = new Impl();
result.keyti = keyti;
size_t i;
for (i = 0; i < prime_list.length - 1; i++)
{
if (length <= prime_list[i])
break;
}
auto len = prime_list[i];
result.buckets = newBuckets(len);
size_t keytsize = aligntsize(keysize);
for (size_t j = 0; j < length; j++)
{ auto pkey = keys.ptr + j * keysize;
auto pvalue = values.ptr + j * valuesize;
Entry* e;
auto key_hash = keyti.getHash(pkey);
//printf("hash = %d\n", key_hash);
i = key_hash % len;
auto pe = &result.buckets[i];
while (1)
{
e = *pe;
if (!e)
{
// Not found, create new elem
//printf("create new one\n");
e = cast(Entry *) cast(void*) new void[Entry.sizeof + keytsize + valuesize];
memcpy(e + 1, pkey, keysize);
e.hash = key_hash;
*pe = e;
result.nodes++;
break;
}
if (key_hash == e.hash)
{
auto c = keyti.compare(pkey, e + 1);
if (c == 0)
break;
}
pe = &e.next;
}
memcpy(cast(void *)(e + 1) + keytsize, pvalue, valuesize);
}
}
return result;
}
static TypeInfo_AssociativeArray _aaUnwrapTypeInfo(const(TypeInfo) tiRaw) nothrow
{
const(TypeInfo)* p = &tiRaw;
TypeInfo_AssociativeArray ti;
while (true)
{
if ((ti = cast(TypeInfo_AssociativeArray)*p) !is null)
break;
if (auto tiConst = cast(TypeInfo_Const)*p) {
// The member in object_.d and object.di differ. This is to ensure
// the file can be compiled both independently in unittest and
// collectively in generating the library. Fixing object.di
// requires changes to std.format in Phobos, fixing object_.d
// makes Phobos's unittest fail, so this hack is employed here to
// avoid irrelevant changes.
static if (is(typeof(&tiConst.base) == TypeInfo*))
p = &tiConst.base;
else
p = &tiConst.next;
} else
assert(0); // ???
}
return ti;
}
/***********************************
* Compare AA contents for equality.
* Returns:
* 1 equal
* 0 not equal
*/
int _aaEqual(in TypeInfo tiRaw, AA e1, AA e2)
{
//printf("_aaEqual()\n");
//printf("keyti = %.*s\n", ti.key.classinfo.name);
//printf("valueti = %.*s\n", ti.next.classinfo.name);
if (e1.impl is e2.impl)
return 1;
size_t len = _aaLen(e1);
if (len != _aaLen(e2))
return 0;
// Check for Bug 5925. ti_raw could be a TypeInfo_Const, we need to unwrap
// it until reaching a real TypeInfo_AssociativeArray.
TypeInfo_AssociativeArray ti = _aaUnwrapTypeInfo(tiRaw);
/* Algorithm: Visit each key/value pair in e1. If that key doesn't exist
* in e2, or if the value in e1 doesn't match the one in e2, the arrays
* are not equal, and exit early.
* After all pairs are checked, the arrays must be equal.
*/
auto keyti = ti.key;
auto valueti = ti.next;
const keysize = aligntsize(keyti.tsize);
const len2 = e2.impl.buckets.length;
int _aaKeys_x(Entry* e)
{
do
{
auto pkey = cast(void*)(e + 1);
auto pvalue = pkey + keysize;
//printf("key = %d, value = %g\n", *cast(int*)pkey, *cast(double*)pvalue);
// We have key/value for e1. See if they exist in e2
auto key_hash = keyti.getHash(pkey);
//printf("hash = %d\n", key_hash);
const i = key_hash % len2;
auto f = e2.impl.buckets[i];
while (1)
{
//printf("f is %p\n", f);
if (f is null)
return 0; // key not found, so AA's are not equal
if (key_hash == f.hash)
{
//printf("hash equals\n");
auto c = keyti.compare(pkey, f + 1);
if (c == 0)
{ // Found key in e2. Compare values
//printf("key equals\n");
auto pvalue2 = cast(void *)(f + 1) + keysize;
if (valueti.equals(pvalue, pvalue2))
{
//printf("value equals\n");
break;
}
else
return 0; // values don't match, so AA's are not equal
}
}
f = f.next;
}
// Look at next entry in e1
e = e.next;
} while (e !is null);
return 1; // this subtree matches
}
foreach (e; e1.impl.buckets)
{
if (e)
{ if (_aaKeys_x(e) == 0)
return 0;
}
}
return 1; // equal
}
/*****************************************
* Computes a hash value for the entire AA
* Returns:
* Hash value
*/
extern (C)
hash_t _aaGetHash(AA* aa, in TypeInfo tiRaw) nothrow
{
import rt.util.hash;
if (aa.impl is null)
return 0;
hash_t h = 0;
TypeInfo_AssociativeArray ti = _aaUnwrapTypeInfo(tiRaw);
auto keyti = ti.key;
auto valueti = ti.next;
const keysize = aligntsize(keyti.tsize);
foreach (e; aa.impl.buckets)
{
while (e)
{
auto pkey = cast(void*)(e + 1);
auto pvalue = pkey + keysize;
// Compute a hash for the key/value pair by hashing their
// respective hash values.
hash_t[2] hpair;
hpair[0] = e.hash;
hpair[1] = valueti.getHash(pvalue);
// Combine the hash of the key/value pair with the running hash
// value using an associative operator (+) so that the resulting
// hash value is independent of the actual order the pairs are
// stored in (important to ensure equality of hash value for two
// AA's containing identical pairs but with different hashtable
// sizes).
h += hashOf(hpair.ptr, hpair.length * hash_t.sizeof);
e = e.next;
}
}
return h;
}
unittest
{
string[int] key1 = [1: "true", 2: "false"];
string[int] key2 = [1: "false", 2: "true"];
// AA lits create a larger hashtable
int[string[int]] aa1 = [key1: 100, key2: 200];
// Ensure consistent hash values are computed for key1
assert((key1 in aa1) !is null);
// Manually assigning to an empty AA creates a smaller hashtable
int[string[int]] aa2;
aa2[key1] = 100;
aa2[key2] = 200;
assert(aa1 == aa2);
// Ensure binary-independence of equal hash keys
string[int] key2a;
key2a[1] = "false";
key2a[2] = "true";
assert(aa1[key2a] == 200);
}