Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f4d0a94

Browse files
authored
Add String.opEquals(AA) (#379)
* Limit imports of emplaceRef * Add String.opEquals(AA) * Make opEquals tests more extensive * Fix syntax error * Adjust logic for computing index in StringMap.opEquals * Improve opEquals test to increase coverage * Add more asserts * Correct comment * Simplify opEquals * Qualify K and V part of opEquals(AA) as const * Simplify StringMap.opEquals to use array comparison * Simplify StringMap.opEquals further
1 parent c72c2d6 commit f4d0a94

File tree

1 file changed

+52
-13
lines changed

1 file changed

+52
-13
lines changed

‎source/mir/string_map.d‎

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ struct StringMap(T, U = uint)
4242
{
4343
// NOTE: moving this to template restriction fails with recursive template instanation
4444
static assert(is(typeof(T.init == V.init) : bool),
45-
"Unsupported rhs of type " ~ typeof(rhs).stringof);// TODO:
45+
"Unsupported rhs of type " ~ typeof(rhs).stringof);
4646
if (keys != rhs.keys)
4747
return false;
4848
if (implementation)
@@ -51,6 +51,27 @@ struct StringMap(T, U = uint)
5151
return false;
5252
return true;
5353
}
54+
/// ditto
55+
bool opEquals(K, V)(scope const const(V)[const(K)] rhs) const
56+
if (is(typeof(K.init == string.init) : bool) &&
57+
is(typeof(V.init == T.init) : bool))
58+
{
59+
if (implementation is null)
60+
return rhs.length == 0;
61+
if (implementation._length != rhs.length)
62+
return false;
63+
foreach (const i; 0 .. implementation._length)
64+
{
65+
if (const valuePtr = implementation.keys[i] in rhs)
66+
{
67+
if (*valuePtr != implementation.values[i])
68+
return false;
69+
}
70+
else
71+
return false;
72+
}
73+
return true;
74+
}
5475

5576
// // linking bug
5677
// version(none)
@@ -658,7 +679,7 @@ struct StringMap(T, U = uint)
658679
}
659680

660681
/++
661-
Converts the associtave array to a common Dlang associative array.
682+
Converts to a builtin associative array.
662683
663684
Complexity: `O(n)`.
664685
+/
@@ -711,7 +732,6 @@ struct StringMap(T, U = uint)
711732
assert(aa["k"] == 1);
712733
}
713734

714-
715735
private static struct Impl
716736
{
717737
import core.lifetime: move;
@@ -954,17 +974,36 @@ version(mir_test)
954974
///
955975
@safe unittest
956976
{
957-
StringMap!int x;
958-
x["L"] = 3;
959-
x["A"] = 2;
960-
x["val"] = 1;
961-
962-
StringMap!uint y;
963-
y["L"] = 3;
964-
y["A"] = 2;
965-
y["val"] = 1;
977+
static void testEquals(X, Y)()
978+
{
979+
X x;
980+
Y y;
981+
assert(x == y);
982+
983+
x["L"] = 3;
984+
assert(x != y);
985+
x["A"] = 2;
986+
assert(x != y);
987+
x["val"] = 1;
988+
assert(x != y);
989+
990+
y["L"] = 3;
991+
assert(x != y);
992+
y["A"] = 2;
993+
assert(x != y);
994+
y["val"] = 1;
995+
assert(x == y);
996+
997+
x = X.init;
998+
assert(x != y);
999+
1000+
y = Y.init;
1001+
assert(x == y);
1002+
}
9661003

967-
assert(x == y);
1004+
testEquals!(StringMap!int, StringMap!uint)();
1005+
testEquals!(StringMap!int, uint[string])();
1006+
testEquals!(uint[string], StringMap!int)();
9681007
}
9691008

9701009
version(mir_test)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /