motiejus/zig

fork of https://codeberg.org/ziglang/zig
git clone https://git.jakstys.lt/motiejus/zig.git
Log | Tree | Refs | README | LICENSE

commit 82ab006e58230b413277fbffe5c304b83a3767e2 (tree)
parent 8a2c2da8051c31f75acd93303e87c5cc039207bc
Author: Josh Wolfe <thejoshwolfe@gmail.com>
Date:   Thu, 13 Jun 2019 07:26:40 -0400

HashMap.getValue()

Diffstat:
Mstd/hash_map.zig | 6++++++
1 file changed, 6 insertions(+), 0 deletions(-)

diff --git a/std/hash_map.zig b/std/hash_map.zig @@ -204,6 +204,10 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3 return hm.internalGet(key); } + pub fn getValue(hm: *const Self, key: K) ?V { + return if (hm.get(key)) |kv| kv.value else null; + } + pub fn contains(hm: *const Self, key: K) bool { return hm.get(key) != null; } @@ -427,12 +431,14 @@ test "basic hash map usage" { testing.expect(map.contains(2)); testing.expect(map.get(2).?.value == 22); + testing.expect(map.getValue(2).? == 22); const rmv1 = map.remove(2); testing.expect(rmv1.?.key == 2); testing.expect(rmv1.?.value == 22); testing.expect(map.remove(2) == null); testing.expect(map.get(2) == null); + testing.expect(map.getValue(2) == null); map.removeAssertDiscard(3); }