This repository has been archived by the owner on Jun 20, 2019. It is now read-only.
/
aaA.d
927 lines (800 loc) · 22.6 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
/**
* 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(in size_t len) @trusted pure nothrow
{
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;
Entry*[4] binit; // initial value of buckets[]
@property const(TypeInfo) keyti() const @safe pure nothrow @nogc
{ return _keyti; }
}
/* 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(in size_t tsize) @safe pure nothrow @nogc
{
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):
/****************************************************
* Determine number of entries in associative array.
*/
size_t _aaLen(in AA aa) pure nothrow @nogc
in
{
//printf("_aaLen()+\n");
//_aaInv(aa);
}
out (result)
{
size_t len = 0;
if (aa.impl)
{
foreach (const(Entry)* 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.
*/
void* _aaGetX(AA* aa, const TypeInfo keyti, in size_t valuesize, in 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 = cast() 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)
{
if (keyti.equals(pkey, e + 1))
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.
*/
inout(void)* _aaGetRvalueX(inout AA aa, in TypeInfo keyti, in size_t valuesize, in 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;
inout(Entry)* e = aa.impl.buckets[i];
while (e !is null)
{
if (key_hash == e.hash)
{
if (keyti.equals(pkey, e + 1))
return cast(inout 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
*/
inout(void)* _aaInX(inout AA aa, in TypeInfo keyti, in 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;
inout(Entry)* e = aa.impl.buckets[i];
while (e !is null)
{
if (key_hash == e.hash)
{
if (keyti.equals(pkey, e + 1))
return cast(inout 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 _aaDelX(AA aa, in TypeInfo keyti, in 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)
{
if (keyti.equals(pkey, e + 1))
{
*pe = e.next;
aa.impl.nodes--;
GC.free(e);
return true;
}
}
pe = &e.next;
}
}
return false;
}
/********************************************
* Produce array of values from aa.
*/
inout(ArrayRet_t) _aaValues(inout AA aa, in size_t keysize, in size_t valuesize) pure nothrow
{
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 (inout(Entry)* 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(inout ArrayRet_t*)(&a);
}
/********************************************
* Rehash an array.
*/
void* _aaRehash(AA* paa, in TypeInfo keyti) pure nothrow
in
{
//_aaInvAh(paa);
}
out (result)
{
//_aaInvAh(result);
}
body
{
//printf("Rehash\n");
if (paa.impl !is null)
{
auto len = _aaLen(*paa);
if (len)
{
Impl newImpl;
Impl* oldImpl = paa.impl;
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;
}
else
{
if (paa.impl.buckets.ptr != paa.impl.binit.ptr)
GC.free(paa.impl.buckets.ptr);
paa.impl.buckets = paa.impl.binit[];
}
}
return (*paa).impl;
}
/********************************************
* Produce array of N byte keys from aa.
*/
inout(ArrayRet_t) _aaKeys(inout AA aa, in size_t keysize) pure nothrow
{
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 (inout(Entry)* 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(inout ArrayRet_t*)(&a);
}
pure nothrow 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);
}
}
unittest // Test for Issue 10381
{
alias II = int[int];
II aa1 = [0: 1];
II aa2 = [0: 1];
II aa3 = [0: 2];
assert(aa1 == aa2); // Passes
assert( typeid(II).equals(&aa1, &aa2));
assert(!typeid(II).equals(&aa1, &aa3));
}
/**********************************************
* '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, in 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, in 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.
*/
Impl* _d_assocarrayliteralTX(const TypeInfo_AssociativeArray ti, void[] keys, void[] values)
{
const valuesize = ti.next.tsize; // value size
const keyti = ti.key;
const keysize = keyti.tsize; // key size
const length = keys.length;
Impl* result;
//printf("_d_assocarrayliteralT(keysize = %d, valuesize = %d, length = %d)\n", keysize, valuesize, length);
//printf("tivalue = %.*s\n", typeid(ti.next).name);
assert(length == values.length);
if (length == 0 || valuesize == 0 || keysize == 0)
{
}
else
{
result = new Impl();
result._keyti = cast() 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)
{
if (keyti.equals(pkey, e + 1))
break;
}
pe = &e.next;
}
memcpy(cast(void *)(e + 1) + keytsize, pvalue, valuesize);
}
}
return result;
}
const(TypeInfo_AssociativeArray) _aaUnwrapTypeInfo(const(TypeInfo) tiRaw) pure nothrow @nogc
{
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, in AA e1, in AA e2)
{
//printf("_aaEqual()\n");
//printf("keyti = %.*s\n", typeid(ti.key).name);
//printf("valueti = %.*s\n", typeid(ti.next).name);
if (e1.impl is e2.impl)
return 1;
size_t len = _aaLen(e1);
if (len != _aaLen(e2))
return 0;
// Bug 9852: at this point, e1 and e2 have the same length, so if one is
// null, the other must either also be null or have zero entries, so they
// must be equal. We check this here to avoid dereferencing null later on.
if (e1.impl is null || e2.impl is null)
return 1;
// Check for Bug 5925. ti_raw could be a TypeInfo_Const, we need to unwrap
// it until reaching a real TypeInfo_AssociativeArray.
const 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.
*/
const keyti = ti.key;
const valueti = ti.next;
const keysize = aligntsize(keyti.tsize);
assert(e2.impl !is null);
const len2 = e2.impl.buckets.length;
int _aaKeys_x(const(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;
const(Entry)* 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");
if (keyti.equals(pkey, f + 1))
{ // 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
*/
hash_t _aaGetHash(in AA* aa, in TypeInfo tiRaw) nothrow
{
import rt.util.hash;
if (aa.impl is null)
return 0;
hash_t h = 0;
const TypeInfo_AssociativeArray ti = _aaUnwrapTypeInfo(tiRaw);
const keyti = ti.key;
const valueti = ti.next;
const keysize = aligntsize(keyti.tsize);
foreach (const(Entry)* 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;
}
pure nothrow 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);
}
// Issue 9852
pure nothrow unittest
{
// Original test case (revised, original assert was wrong)
int[string] a;
a["foo"] = 0;
a.remove("foo");
assert(a == null); // should not crash
int[string] b;
assert(b is null);
assert(a == b); // should not deref null
assert(b == a); // ditto
int[string] c;
c["a"] = 1;
assert(a != c); // comparison with empty non-null AA
assert(c != a);
assert(b != c); // comparison with null AA
assert(c != b);
}
/**
* _aaRange implements a ForwardRange
*/
struct Range
{
Impl* impl;
Entry* current;
}
Range _aaRange(AA aa) pure nothrow @nogc
{
typeof(return) res;
if (aa.impl is null)
return res;
res.impl = aa.impl;
foreach (entry; aa.impl.buckets)
{
if (entry !is null)
{
res.current = entry;
break;
}
}
return res;
}
bool _aaRangeEmpty(Range r) pure nothrow @nogc
{
return r.current is null;
}
void* _aaRangeFrontKey(Range r) pure nothrow @nogc
in
{
assert(r.current !is null);
}
body
{
return cast(void*)r.current + Entry.sizeof;
}
void* _aaRangeFrontValue(Range r) pure nothrow @nogc
in
{
assert(r.current !is null);
assert(r.impl.keyti !is null); // set on first insert
}
body
{
return cast(void*)r.current + Entry.sizeof + aligntsize(r.impl.keyti.tsize);
}
void _aaRangePopFront(ref Range r) pure nothrow @nogc
{
if (r.current.next !is null)
{
r.current = r.current.next;
}
else
{
immutable idx = r.current.hash % r.impl.buckets.length;
r.current = null;
foreach (entry; r.impl.buckets[idx + 1 .. $])
{
if (entry !is null)
{
r.current = entry;
break;
}
}
}
}