mirror of
https://github.com/ziglang/zig.git
synced 2026-02-18 07:18:38 +00:00
Implemented getOrPutValue which wraps getOrPut
This commit is contained in:
parent
078a0a6999
commit
f74320d56d
@ -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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user