Skip to content

Commit 4e8aa33

Browse files
committed
add KeyValue to StringMap
1 parent eb81f4d commit 4e8aa33

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

source/mir/string_map.d

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ struct StringMap(T, U = uint)
3636
import core.lifetime: move;
3737
import mir.conv: emplaceRef;
3838

39+
///
40+
static struct KeyValue
41+
{
42+
///
43+
string key;
44+
///
45+
T value;
46+
}
47+
3948
///
4049
alias serdeKeysProxy = Unqual!T;
4150

@@ -242,6 +251,8 @@ struct StringMap(T, U = uint)
242251
{
243252
return implementation ? implementation.keys : null;
244253
}
254+
///
255+
alias byKey = keys;
245256

246257
version(mir_test) static if (is(T == int))
247258
///
@@ -272,6 +283,27 @@ struct StringMap(T, U = uint)
272283
return implementation ? implementation.values : null;
273284
}
274285

286+
/// ditto
287+
alias byValue = values;
288+
289+
version(mir_test) static if (is(T == int))
290+
///
291+
@safe pure unittest
292+
{
293+
StringMap!double map;
294+
assert(map.byKeyValue == StringMap!double.KeyValue[].init);
295+
map["c"] = 4.0;
296+
map["a"] = 3.0;
297+
assert(map.byKeyValue == [StringMap!double.KeyValue("c", 4.0), StringMap!double.KeyValue("a", 3.0)]);
298+
}
299+
300+
///
301+
auto byKeyValue(this This)() @trusted pure nothrow @nogc
302+
{
303+
import mir.ndslice.topology: zip, map;
304+
return keys.zip(values).map!KeyValue;
305+
}
306+
275307
version(mir_test) static if (is(T == int))
276308
///
277309
@safe pure unittest

0 commit comments

Comments
 (0)