authorgravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2019-06-13 07:26:40-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-13 11:41:34-04:00
log82ab006e58230b413277fbffe5c304b83a3767e2
treeba09e9e1c378e1dd7132f39084dcc1f34b55b0e4
parent8a2c2da8051c31f75acd93303e87c5cc039207bc

HashMap.getValue()


1 files changed, 6 insertions(+), 0 deletions(-)

std/hash_map.zig+6
...@@ -204,6 +204,10 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3...@@ -204,6 +204,10 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
204 return hm.internalGet(key);204 return hm.internalGet(key);
205 }205 }
206206
207 pub fn getValue(hm: *const Self, key: K) ?V {
208 return if (hm.get(key)) |kv| kv.value else null;
209 }
210
207 pub fn contains(hm: *const Self, key: K) bool {211 pub fn contains(hm: *const Self, key: K) bool {
208 return hm.get(key) != null;212 return hm.get(key) != null;
209 }213 }
...@@ -427,12 +431,14 @@ test "basic hash map usage" {...@@ -427,12 +431,14 @@ test "basic hash map usage" {
427431
428 testing.expect(map.contains(2));432 testing.expect(map.contains(2));
429 testing.expect(map.get(2).?.value == 22);433 testing.expect(map.get(2).?.value == 22);
434 testing.expect(map.getValue(2).? == 22);
430435
431 const rmv1 = map.remove(2);436 const rmv1 = map.remove(2);
432 testing.expect(rmv1.?.key == 2);437 testing.expect(rmv1.?.key == 2);
433 testing.expect(rmv1.?.value == 22);438 testing.expect(rmv1.?.value == 22);
434 testing.expect(map.remove(2) == null);439 testing.expect(map.remove(2) == null);
435 testing.expect(map.get(2) == null);440 testing.expect(map.get(2) == null);
441 testing.expect(map.getValue(2) == null);
436442
437 map.removeAssertDiscard(3);443 map.removeAssertDiscard(3);
438}444}