StringMap.require

Looks up key; if it exists returns corresponding value else evaluates value, adds it to the associative array and returns it.

Complexity: O(log(s)) (exist) or O(n) (not exist), where s is the count of the strings with the same length as they key.

struct StringMap(T, U = uint)
ref
T
require
()
(
string key
,
lazy T value = T.init
)
if (
isMutable!T &&
!__traits(hasMember, T, "opPostMove")
&&
__traits(isUnsigned, U)
)

Examples

StringMap!int map = ["aa": 1];
int add3(ref int x) { x += 3; return x; }
assert(add3(map.require("aa", 10)) == 4);
assert(add3(map.require("bb", 10)) == 13);
assert(map.require("a", 100));
assert(map.require("aa") == 4);
assert(map.require("bb") == 13);
assert(map.keys == ["aa", "bb", "a"]);

Meta