Skip to content

Commit b21ff32

Browse files
authored
Make StringMap.opEquals independent of insert order of elements in this and rhs (#384)
1 parent f4d0a94 commit b21ff32

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

Diff for: source/mir/string_map.d

+12-8
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,21 @@ struct StringMap(T, U = uint)
3838

3939
///
4040
// current implementation is workaround for linking bugs when used in self referencing algebraic types
41-
bool opEquals(V)(scope const StringMap!(V, U) rhs) const
41+
bool opEquals(V)(scope const StringMap!(V, U) rhs) const @trusted
4242
{
4343
// NOTE: moving this to template restriction fails with recursive template instanation
4444
static assert(is(typeof(T.init == V.init) : bool),
4545
"Unsupported rhs of type " ~ typeof(rhs).stringof);
46-
if (keys != rhs.keys)
46+
if (implementation is null)
47+
return rhs.length == 0;
48+
if (rhs.implementation is null)
49+
return length == 0;
50+
if (implementation._length != rhs.implementation._length)
4751
return false;
48-
if (implementation)
49-
foreach (const i; 0 .. implementation._length)
50-
if (implementation.values[i] != rhs.implementation.values[i]) // needs `values` instead of `_values` to be @safe
51-
return false;
52+
foreach (const i, const index; implementation.indices)
53+
if (implementation._keys[index] != rhs.implementation._keys[rhs.implementation._indices[i]] ||
54+
implementation._values[index] != rhs.implementation._values[rhs.implementation._indices[i]])
55+
return false;
5256
return true;
5357
}
5458
/// ditto
@@ -987,11 +991,11 @@ version(mir_test)
987991
x["val"] = 1;
988992
assert(x != y);
989993

994+
y["val"] = 1;
995+
assert(x != y);
990996
y["L"] = 3;
991997
assert(x != y);
992998
y["A"] = 2;
993-
assert(x != y);
994-
y["val"] = 1;
995999
assert(x == y);
9961000

9971001
x = X.init;

0 commit comments

Comments
 (0)