aboutsummaryrefslogtreecommitdiff
path: root/lib/std/array_hash_map.zig
diff options
context:
space:
mode:
authorMeghan Denny <hello@nektro.net>2025-02-02 00:03:19 -0800
committerAndrew Kelley <andrew@ziglang.org>2025-02-07 17:52:19 -0800
commita8af36ab10562e58ba237fa3dcc582228197402b (patch)
treece1643493977a56f8424fa65c46b9eab3da3dbf5 /lib/std/array_hash_map.zig
parent84d2c6dc72738bede35651a30dbcdbae3f3b30fc (diff)
downloadzig-a8af36ab10562e58ba237fa3dcc582228197402b.tar.gz
zig-a8af36ab10562e58ba237fa3dcc582228197402b.zip
std.ArrayHashMap: popOrNul() -> pop()
Diffstat (limited to 'lib/std/array_hash_map.zig')
-rw-r--r--lib/std/array_hash_map.zig48
1 files changed, 8 insertions, 40 deletions
diff --git a/lib/std/array_hash_map.zig b/lib/std/array_hash_map.zig
index 22c736d0a5..85e73d6dd1 100644
--- a/lib/std/array_hash_map.zig
+++ b/lib/std/array_hash_map.zig
@@ -485,15 +485,10 @@ pub fn ArrayHashMapWithAllocator(
return self.unmanaged.shrinkAndFreeContext(self.allocator, new_len, self.ctx);
}
- /// Removes the last inserted `Entry` in the hash map and returns it.
- pub fn pop(self: *Self) KV {
- return self.unmanaged.popContext(self.ctx);
- }
-
/// Removes the last inserted `Entry` in the hash map and returns it if count is nonzero.
/// Otherwise returns null.
- pub fn popOrNull(self: *Self) ?KV {
- return self.unmanaged.popOrNullContext(self.ctx);
+ pub fn pop(self: *Self) ?KV {
+ return self.unmanaged.popContext(self.ctx);
}
};
}
@@ -1468,12 +1463,14 @@ pub fn ArrayHashMapUnmanaged(
}
/// Removes the last inserted `Entry` in the hash map and returns it.
- pub fn pop(self: *Self) KV {
+ /// Otherwise returns null.
+ pub fn pop(self: *Self) ?KV {
if (@sizeOf(ByIndexContext) != 0)
@compileError("Cannot infer context " ++ @typeName(Context) ++ ", call popContext instead.");
return self.popContext(undefined);
}
- pub fn popContext(self: *Self, ctx: Context) KV {
+ pub fn popContext(self: *Self, ctx: Context) ?KV {
+ if (self.entries.len == 0) return null;
self.pointer_stability.lock();
defer self.pointer_stability.unlock();
@@ -1487,17 +1484,6 @@ pub fn ArrayHashMapUnmanaged(
};
}
- /// Removes the last inserted `Entry` in the hash map and returns it if count is nonzero.
- /// Otherwise returns null.
- pub fn popOrNull(self: *Self) ?KV {
- if (@sizeOf(ByIndexContext) != 0)
- @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call popContext instead.");
- return self.popOrNullContext(undefined);
- }
- pub fn popOrNullContext(self: *Self, ctx: Context) ?KV {
- return if (self.entries.len == 0) null else self.popContext(ctx);
- }
-
fn fetchRemoveByKey(
self: *Self,
key: anytype,
@@ -2425,25 +2411,7 @@ test "shrink" {
}
}
-test "pop" {
- var map = AutoArrayHashMap(i32, i32).init(std.testing.allocator);
- defer map.deinit();
-
- // Insert just enough entries so that the map expands. Afterwards,
- // pop all entries out of the map.
-
- var i: i32 = 0;
- while (i < 9) : (i += 1) {
- try testing.expect((try map.fetchPut(i, i)) == null);
- }
-
- while (i > 0) : (i -= 1) {
- const pop = map.pop();
- try testing.expect(pop.key == i - 1 and pop.value == i - 1);
- }
-}
-
-test "popOrNull" {
+test "pop()" {
var map = AutoArrayHashMap(i32, i32).init(std.testing.allocator);
defer map.deinit();
@@ -2455,7 +2423,7 @@ test "popOrNull" {
try testing.expect((try map.fetchPut(i, i)) == null);
}
- while (map.popOrNull()) |pop| {
+ while (map.pop()) |pop| {
try testing.expect(pop.key == i - 1 and pop.value == i - 1);
i -= 1;
}