Implemented getOrPutValue which wraps getOrPut

This commit is contained in:
Jimmi Holst Christensen 2018-11-29 09:10:15 +01:00
parent 078a0a6999
commit f74320d56d

View File

@ -126,6 +126,14 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
};
}
pub fn getOrPutValue(self: *Self, key: K, value: V) !*KV {
const res = try self.getOrPut(key);
if (!res.found_existing)
res.kv.value = value;
return res.kv;
}
fn ensureCapacity(self: *Self) !void {
if (self.entries.len == 0) {
return self.initCapacity(16);
@ -354,6 +362,12 @@ test "basic hash map usage" {
gop2.kv.value = 42;
assert(map.get(99).?.value == 42);
const gop3 = try map.getOrPutValue(5, 5);
assert(gop3.value == 77);
const gop4 = try map.getOrPutValue(100, 41);
assert(gop4.value == 41);
assert(map.contains(2));
assert(map.get(2).?.value == 22);
_ = map.remove(2);