-
Notifications
You must be signed in to change notification settings - Fork 2
/
newAA.d
1303 lines (1102 loc) · 29.8 KB
/
newAA.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
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// vim:set ts=4 sw=4 expandtab:
// Developmental version of completely native AA implementation.
version=AAdebug;
version(AAdebug) {
import std.conv;
import std.stdio;
template isAA(T)
{
static if (__traits(isAssociativeArray, T))
{
static if (is(T _ : V[K], K, V))
enum isAA = true;
else
static assert(0);
}
else
enum isAA = false;
}
template KeyType(V : V[K], K)
{
alias K KeyType;
}
template ValueType(V : V[K], K)
{
alias V ValueType;
}
}
import core.exception;
import core.memory;
import rt.util.hash;
// This is a temporary syntactic sugar hack until we manage to get dmd to
// work with us nicely.
version(unittest)
{
template AA(T) if (isAA!T)
{
template ToAA(T)
{
static if (isAA!T)
alias AssociativeArray!(ToAA!(KeyType!T), ToAA!(ValueType!T)) ToAA;
else
alias T ToAA;
}
alias ToAA!T AA;
}
unittest {
AA!(int[string]) aa;
assert(typeid(aa) == typeid(AssociativeArray!(string,int)));
}
}
struct AssociativeArray(Key,Value)
{
alias Key keytype;
alias Value valuetype;
// Temporary stuff that should be removed before merging into druntime.
version(AAdebug)
{
enum isAssociativeArray = true;
}
private:
// Convenience template to check if a given type can be compared with Key
// using ==.
template keyComparable(L) {
enum bool keyComparable = is(typeof(Key.init==L.init) == bool);
}
// Check if a given type is equivalent to the key type (i.e., has the same
// representation, and presumably the same hash computation).
template keyEquiv(L) {
// The following works by making use of qualifier collapsing.
enum keyEquiv = is(immutable(Key)==immutable(L));
}
// Convenience templates to check if a given type can be either implicitly
// converted to Key, or can be converted via .idup. This is for supporting
// assigning char[] keys to X[string], for example.
template keyIdupCompat(L) {
enum keyIdupCompat = is(typeof(L.init.idup) : Key);
}
template keySliceCompat(L) {
// Issue 7665: allow dynamic array assignment to static array keys.
static if (is(Key kbase : kbase[N], int N) && is(L lbase : lbase[]))
enum keySliceCompat = is(lbase : kbase);
else
enum keySliceCompat = false;
}
template keyCompat(L) {
enum bool keyCompat = keyComparable!L && (is(L : Key)
|| keyIdupCompat!L
|| keySliceCompat!L);
}
struct Slot
{
Slot *next;
hash_t hash;
Key key;
Value value;
// This ctor accepts any key type that can either be implicitly
// converted to Key, or has an .idup method that returns a type
// implicitly convertible to Key.
this(K)(hash_t h, K k, Value v, Slot *_next=null) if (keyCompat!K)
{
next = _next;
hash = h;
static if (is(K : Key))
key = k;
else static if (keyIdupCompat!K)
key = k.idup;
else static if (keySliceCompat!K && is(Key b : b[N], int N))
{
if (k.length!=N)
throw new Error("Tried to set key with wrong size in " ~
"associative array with fixed-size key");
key = k[0..N];
}
else
static assert(false);
value = v;
}
}
struct Impl
{
Slot*[] slots;
size_t nodes;
// Prevent extra allocations for very small AA's.
Slot*[4] binit;
}
// Range interface
struct Range
{
Slot*[] slots;
Slot* curslot;
this(Impl *i) pure nothrow @safe
{
if (i !is null)
{
slots = i.slots;
nextSlot();
}
}
void nextSlot() pure nothrow @safe
{
while (slots.length > 0)
{
if (slots[0] !is null)
{
curslot = slots[0];
break;
}
slots = slots[1..$];
}
}
@property bool empty() pure const nothrow @safe
{
return curslot is null;
}
@property ref inout(Slot) front() inout pure const nothrow @safe
{
assert(curslot);
return *curslot;
}
void popFront() pure @safe nothrow
{
assert(curslot);
curslot = curslot.next;
if (curslot is null)
{
slots = slots[1..$];
nextSlot();
}
}
}
// Reference semantics
Impl *impl;
// Preset prime hash sizes for auto-rehashing.
enum 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
];
static size_t findAllocSize(size_t size) pure nothrow @safe
{
size_t i;
for (i=0; i < prime_list.length; i++)
{
if (size <= prime_list[i])
break;
}
return prime_list[i];
}
static Slot*[] alloc(size_t len) @trusted
{
auto slots = new Slot*[len];
GC.setAttr(&slots, GC.BlkAttr.NO_INTERIOR);
return slots;
}
static hash_t hash(K)(K key) inout pure nothrow @safe
if (keyCompat!K)
{
static if (keyEquiv!K || keySliceCompat!K)
return key.toHash();
else static if (is(K : Key))
{
Key k = key;
return k.toHash();
}
else
static assert(0, "Don't know how to compute hash");
}
inout(Slot) *findSlot(K)(in K key) inout pure nothrow @safe
if (keyCompat!K)
{
if (!impl)
return null;
auto keyhash = hash(key);
auto i = keyhash % impl.slots.length;
inout(Slot)* slot = impl.slots[i];
while (slot) {
if (slot.hash == keyhash && key == slot.key)
{
return slot;
}
slot = slot.next;
}
return slot;
}
// Returns true if slot already exists; false otherwise.
bool findSlotLvalue(K)(K key, Value defaultValue, out Slot *slot) @trusted
if (keyCompat!K)
{
if (!impl)
{
impl = new Impl();
impl.slots = impl.binit;
}
auto keyhash = hash(key);
auto i = keyhash % impl.slots.length;
slot = impl.slots[i];
if (slot is null)
{
impl.slots[i] = new Slot(keyhash, key, defaultValue);
slot = impl.slots[i];
}
else
{
for(;;) {
if (slot.hash==keyhash && key==slot.key)
{
return true;
}
else if (!slot.next)
{
slot.next = new Slot(keyhash, key, defaultValue);
slot = slot.next;
break;
}
slot = slot.next;
}
}
if (++impl.nodes > 4*impl.slots.length)
{
this.rehash;
}
return false;
}
public:
static typeof(this) fromLiteral(Key[] keys, Value[] values) @safe
in { assert(keys.length == values.length); }
body
{
typeof(this) aa;
aa.impl = new Impl();
aa.impl.slots = alloc(findAllocSize(keys.length));
foreach (i; 0 .. keys.length)
{
aa[keys[i]] = values[i];
}
return aa;
}
version(AAdebug)
{
this(K,V)(V[K] aa)
{
foreach (key, val; aa)
this[key] = val;
}
void opAssign(K,V)(V[K] aa)
{
foreach (key, val; aa)
this[key] = val;
}
void opAssign()(inout AssociativeArray!(Key,Value) aa) inout @trusted
{
// Stupid evil hack to get around const redtape.
// Can you believe what it takes to duplicate the default opAssign
// just because we need to overload it on a different source type?!
Impl **p = cast(Impl**)&impl;
*p = cast(Impl*)aa.impl;
}
}
hash_t toHash() nothrow pure const @trusted
{
// AA hashes must:
// (1) depend solely on key/value pairs stored in it, regardless of the
// size of the hashtable and/or any other implementation-specific
// states;
// (2) be independent of the order of key/value pairs.
//
// So we compute a hash value for each key/value pair by combining
// their respective hash values, and use a commutative operation
// (addition) to combine these hash values into an overall hash for the
// entire AA.
hash_t h = 0;
if (!impl)
return h;
foreach (const(Slot)* s; impl.slots)
{
while (s)
{
// NOTE: use a non-commutative operation (hashOf) to combine
// the key and value hashes to minimize collisions when dealing
// with things like int[int].
hash_t[2] pairhash;
pairhash[0] = s.hash;
pairhash[1] = s.value.toHash();
h += hashOf(pairhash.ptr, pairhash.length * hash_t.sizeof);
s = s.next;
}
}
return h;
}
@property size_t length() nothrow pure const @safe
{
return impl ? impl.nodes : 0;
}
@property bool empty() nothrow pure const @safe
{
return length == 0;
}
inout(Value) get(K)(in K key, lazy inout(Value) defaultValue) inout pure @safe
if (keyCompat!K)
{
inout(Slot)* s = findSlot(key);
return (s is null) ? defaultValue : s.value;
}
inout(Value) *opBinaryRight(string op, K)(in K key)
nothrow pure inout @safe
if (op=="in" && keyCompat!K)
{
auto slot = findSlot(key);
return (slot) ? &slot.value : null;
}
inout(Value) opIndex(K)(in K key, string file=__FILE__, size_t line=__LINE__)
pure inout @safe
if (keyCompat!K)
{
inout(Value) *valp = opBinaryRight!"in"(key);
if (valp is null)
throw new RangeError(file, line);
return *valp;
}
void opIndexAssign(K)(Value value, K key) @safe
// Note: can't be pure nothrow because we indirectly call alloc()
if (keyCompat!K)
{
Slot *slot;
if (findSlotLvalue(key, value, slot))
slot.value = value;
}
Value opIndexUnary(string op, K)(K key) @safe
if (keyCompat!K)
{
Slot *slot;
findSlotLvalue(key, Value.init, slot);
return mixin(op ~ "slot.value");
}
Value opIndexOpAssign(string op, K)(Value v, K key) @safe
if (keyCompat!K)
{
Slot *slot;
findSlotLvalue(key, Value.init, slot);
return mixin("slot.value" ~ op ~ "=v");
}
bool remove(K)(in K key) pure nothrow @trusted
if (keyCompat!K)
{
if (!impl) return false;
auto keyhash = hash(key);
size_t i = keyhash % impl.slots.length;
auto slot = impl.slots[i];
if (!slot)
return false;
if (slot.hash == keyhash && slot.key == key)
{
impl.slots[i] = slot.next;
impl.nodes--;
return true;
}
while (slot.next)
{
if (slot.next.hash == keyhash && slot.next.key == key)
{
slot.next = slot.next.next;
impl.nodes--;
return true;
}
slot = slot.next;
}
return false;
}
int opApply(scope int delegate(ref Value) dg)
{
if (impl is null)
return 0;
foreach (Slot *slot; impl.slots)
{
while (slot)
{
auto result = dg(slot.value);
if (result)
return result;
slot = slot.next;
}
}
return 0;
}
int opApply(scope int delegate(ref Key, ref Value) dg)
{
if (impl is null)
return 0;
foreach (Slot *slot; impl.slots)
{
while (slot)
{
auto result = dg(slot.key, slot.value);
if (result)
return result;
slot = slot.next;
}
}
return 0;
}
bool opEquals(inout typeof(this) that) inout nothrow pure @safe
{
if (impl is that.impl)
return true;
if (impl is null || that.impl is null)
return false;
if (impl.nodes != that.impl.nodes)
return false;
foreach (inout(Slot)* slot; impl.slots)
{
while (slot)
{
inout(Slot)* s = that.impl.slots[slot.hash % that.impl.slots.length];
// To be equal, it is enough for one of the target slots to
// match the current entry.
while (s)
{
if (slot.hash == s.hash && slot.key == s.key &&
slot.value == s.value)
{
break;
}
s = s.next;
}
// No match found at all; give up.
if (!s) return false;
slot = slot.next;
}
}
return true;
}
@property inout(Key)[] keys() inout @trusted
{
inout(Key)[] k;
if (impl !is null)
{
// Preallocate output array for efficiency
k.reserve(impl.nodes);
foreach (inout(Slot) *slot; impl.slots)
{
while (slot)
{
k ~= slot.key;
slot = slot.next;
}
}
}
return k;
}
@property inout(Value)[] values() inout @trusted
{
inout(Value)[] v;
if (impl !is null)
{
// Preallocate output array for efficiency
v.reserve(impl.nodes);
foreach (inout(Slot) *slot; impl.slots)
{
while (slot)
{
v ~= slot.value;
slot = slot.next;
}
}
}
return v;
}
@property typeof(this) rehash() @safe
{
if (impl is null) return this;
size_t newlen = findAllocSize(impl.nodes);
Slot*[] newslots = alloc(newlen);
foreach (slot; impl.slots)
{
while (slot)
{
auto next = slot.next;
// Transplant slot into new hashtable.
const j = slot.hash % newlen;
slot.next = newslots[j];
newslots[j] = slot;
slot = next;
}
}
// Remove references to slots in old hash table.
if (impl.slots.ptr == impl.binit.ptr)
impl.binit[] = null;
else
delete impl.slots;
impl.slots = newslots;
return this;
}
@property auto dup() @safe
{
AssociativeArray!(Key,Value) result;
if (impl !is null)
{
result.impl = new Impl();
result.impl.slots = alloc(findAllocSize(impl.nodes));
foreach (Slot* slot; impl.slots)
{
while (slot)
{
size_t i = slot.hash % result.impl.slots.length;
Slot *s = result.impl.slots[i];
// FIXME: maybe do shallow copy if value type is immutable?
result.impl.slots[i] = new Slot(slot.hash, slot.key,
slot.value,
result.impl.slots[i]);
result.impl.nodes++;
slot = slot.next;
}
}
}
return result;
}
@property auto byKey() pure nothrow @safe
{
static struct KeyRange
{
Range state;
this(Impl *p) pure nothrow @safe
{
state = Range(p);
}
@property ref Key front() pure nothrow @safe
{
return state.front.key;
}
alias state this;
}
return KeyRange(impl);
}
@property auto byValue() pure nothrow @safe
{
static struct ValueRange
{
Range state;
this(Impl *p) pure nothrow @safe
{
state = Range(p);
}
@property ref Value front() pure nothrow @safe
{
return state.front.value;
}
alias state this;
}
return ValueRange(impl);
}
}
// Test reference semantics
unittest {
AA!(int[string]) aa, bb;
aa["abc"] = 123;
bb = aa;
assert(aa.impl is bb.impl);
aa["def"] = 456;
assert(bb["def"] == 456);
// TBD: should the case where aa is empty when it is assigned to bb work as
// well?
}
// Check consistency with specs
unittest {
AA!(int[string]) aa;
assert(aa.sizeof==4 || aa.sizeof==8);
assert(aa.length==0);
aa["abc"] = 10;
assert(aa.length==1);
aa["def"] = 20;
assert(aa.length==2);
aa["ghi"] = 30;
assert(aa.length==3);
aa.remove("def");
assert(aa.length==2);
}
// Test .empty
unittest {
AA!(int[wstring]) aa;
assert(aa.empty);
aa["abc"w] = 123;
assert(!aa.empty);
aa.remove("abc"w);
assert(aa.empty);
}
// Test .get
unittest {
AA!(int[dstring]) aa;
aa["mykey"d] = 10;
assert(aa.get("mykey"d, 99) == 10);
assert(aa.get("yourkey"d, 99) == 99);
}
// Test opBinaryRight!"in"
unittest {
AA!(bool[wstring]) aa;
aa["abc"w] = true;
aa["def"w] = false;
assert(("abc"w in aa) !is null);
assert(("xyz"w in aa) is null);
}
// Test opIndex*
unittest {
// Test opIndex
AA!(char[char]) aa;
aa['x'] = 'y';
aa['y'] = 'z';
assert(aa[aa['x']] == 'z');
// Test opIndexUnary
++aa['x'];
assert(aa['x'] == 'z');
//aa['x']++; // bug 7733
AA!(int[char]) ii;
ii['a'] = 1;
ii['b'] = 2;
assert(-ii['a'] == -1);
assert(~ii['a'] == ~1);
assert(ii['a'] == 1); // opIndexUnary should not change original value
// Test opIndexOpAssign
ii['b'] += 2;
assert(ii['b'] == 4);
ii['b'] -= 3;
assert(ii['b'] == 1);
}
// Test opApply.
unittest {
AA!(int[int]) aa;
aa[10] = 5;
aa[20] = 17;
aa[30] = 39;
int valsum = 0;
foreach (v; aa) {
valsum += v;
}
assert(valsum == 5+17+39);
int keysum = 0;
valsum = 0;
foreach (k,v; aa) {
keysum += k;
valsum += v;
}
assert(keysum == 10+20+30);
assert(valsum == 5+17+39);
}
// Test opEquals and rehash
unittest {
immutable int[] key1 = [1,2,3];
immutable int[] key2 = [4,5,6];
immutable int[] key3 = [1,3,5];
AA!(char[immutable int[]]) aa, bb;
aa[key1] = '1';
aa[key2] = '2';
aa[key3] = '3';
bb[key3] = '3';
bb[key2] = '2';
bb[key1] = '1';
assert(aa==bb);
// .rehash should not invalidate equality
bb.rehash;
assert(aa==bb);
assert(bb==aa);
}
// Test .keys and .values
unittest {
AA!(int[char]) aa;
aa['a'] = 1;
aa['b'] = 2;
aa['c'] = 3;
assert(aa.keys.sort == ['a', 'b', 'c']);
assert(aa.values.sort == [1,2,3]);
}
// Test .rehash
unittest {
AA!(int[int]) aa;
foreach (i; 0 .. 99) {
aa[i*10] = i^^2;
}
aa.rehash;
foreach (i; 0 .. 99) {
assert(aa[i*10] == i^^2);
}
}
// Test .byKey and .byValue
unittest {
AA!(string[int]) aa;
aa[100] = "a";
aa[200] = "aa";
aa[300] = "aaaa";
int sum = 0;
foreach (k; aa.byKey) {
sum += k;
}
assert(sum == 600);
string x;
foreach(v; aa.byValue) {
x ~= v;
}
assert(x == "aaaaaaa");
}
// Test implicit conversion (feature requested by Andrei)
unittest {
AA!(int[wstring]) aa;
wchar[] key = "abc"w.dup;
aa[key] = 123;
assert(aa["abc"w] == 123);
const wchar[] key2 = "abc"w;
assert(aa[key2] is aa["abc"w]);
assert(*(key in aa) == 123);
assert(*(key2 in aa) == 123);
assert(aa.get(key2, 999) == 123);
}
// Test .remove
unittest {
const int[] key1 = [1,2,3];
const int[] key2 = [2,3,1];
const int[] key3 = [3,1,2];
const int[] key4 = [1,3,2];
AA!(string[const int[]]) aa;
aa[key1] = "abc";
aa[key2] = "def";
aa[key3] = "ghi";
assert((key1 in aa) !is null);
assert((key2 in aa) !is null);
assert((key3 in aa) !is null);
assert(aa.remove(key2));
assert((key1 in aa) !is null);
assert((key2 in aa) is null);
assert((key3 in aa) !is null);
assert(!aa.remove(key4));
assert((key1 in aa) !is null);
assert((key2 in aa) is null);
assert((key3 in aa) !is null);
}
// Test .toHash
unittest {
AA!(int[int]) aa1, aa2, aa3;
aa1[1] = 2;
aa1[2] = 1;
aa2[1] = 1;
aa2[2] = 2;
aa3[2] = 1;
aa3[1] = 2;
aa3.rehash; // make aa3 binary-different from aa1
assert(aa1.toHash() != aa2.toHash());
assert(aa1.toHash() == aa3.toHash());
// Issue 3824
//AA!(const AA!(int,int), string) meta;
AA!(string[const AA!(int[int])]) meta;
meta[aa1] = "abc";
assert(meta[aa3] == "abc");
meta[aa2] = "def";
assert(meta[aa1] == "abc"); // ensure no overwrite
assert(meta[aa2] == "def");
assert(meta.dup == meta);
}
// Test AA literals API
unittest {
auto aa = AA!(int[string]).fromLiteral(
["abc", "def", "ghi"],
[ 123, 456, 789 ]
);
AA!(int[string]) bb;
bb["abc"] = 123;
bb["def"] = 456;
bb["ghi"] = 789;
assert(aa==bb);
}
// Test .dup with a large AA
unittest {
AA!(short[int]) aa;
foreach (short i; 0..100)
aa[i*100] = i;
assert(aa.dup == aa);
}
// Test non-const key type (by Andrei's request)
unittest {
AA!(bool[int]) aa;
aa[123] = true;
aa[321] = false;
const int i = 123;
assert(aa[i] == true);
immutable int j = 321;
assert(aa[j] == false);
}
// Test potential hash computation issue with const(char)[].
unittest {
AA!(int[string]) aa;
char[] key1 = "abcd".dup;
const(char)[] key2 = "abcd";
string key3 = "abcd";
aa[key3] = 123;
assert(aa[key2] == 123);
assert(aa[key1] == 123);
}
// Bug found by Andrej Mitrovic: can't instantiate AA!(string,int[]).
unittest {
AA!(int[][string]) aa;
aa["abc"] = [1,2,3];
assert(aa["abc"] == [1,2,3]);