| ... | @@ -414,6 +414,12 @@ pub fn ArrayHashMap( | ... | @@ -414,6 +414,12 @@ pub fn ArrayHashMap( |
| 414 | pub fn pop(self: *Self) KV { | 414 | pub fn pop(self: *Self) KV { |
| 415 | return self.unmanaged.popContext(self.ctx); | 415 | return self.unmanaged.popContext(self.ctx); |
| 416 | } | 416 | } |
| | 417 | |
| | 418 | /// Removes the last inserted `Entry` in the hash map and returns it if count is nonzero. |
| | 419 | /// Otherwise returns null. |
| | 420 | pub fn popOrNull(self: *Self) ?KV { |
| | 421 | return self.unmanaged.popOrNullContext(self.ctx); |
| | 422 | } |
| 417 | }; | 423 | }; |
| 418 | } | 424 | } |
| 419 | | 425 | |
| ... | @@ -1181,6 +1187,17 @@ pub fn ArrayHashMapUnmanaged( | ... | @@ -1181,6 +1187,17 @@ pub fn ArrayHashMapUnmanaged( |
| 1181 | }; | 1187 | }; |
| 1182 | } | 1188 | } |
| 1183 | | 1189 | |
| | 1190 | /// Removes the last inserted `Entry` in the hash map and returns it if count is nonzero. |
| | 1191 | /// Otherwise returns null. |
| | 1192 | pub fn popOrNull(self: *Self) ?KV { |
| | 1193 | if (@sizeOf(ByIndexContext) != 0) |
| | 1194 | @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call popContext instead."); |
| | 1195 | return self.popOrNullContext(undefined); |
| | 1196 | } |
| | 1197 | pub fn popOrNullContext(self: *Self, ctx: Context) ?KV { |
| | 1198 | return if (self.entries.len == 0) null else self.popContext(ctx); |
| | 1199 | } |
| | 1200 | |
| 1184 | // ------------------ No pub fns below this point ------------------ | 1201 | // ------------------ No pub fns below this point ------------------ |
| 1185 | | 1202 | |
| 1186 | fn fetchRemoveByKey(self: *Self, key: anytype, key_ctx: anytype, ctx: ByIndexContext, comptime removal_type: RemovalType) ?KV { | 1203 | fn fetchRemoveByKey(self: *Self, key: anytype, key_ctx: anytype, ctx: ByIndexContext, comptime removal_type: RemovalType) ?KV { |
| ... | @@ -2094,6 +2111,26 @@ test "pop" { | ... | @@ -2094,6 +2111,26 @@ test "pop" { |
| 2094 | } | 2111 | } |
| 2095 | } | 2112 | } |
| 2096 | | 2113 | |
| | 2114 | test "popOrNull" { |
| | 2115 | var map = AutoArrayHashMap(i32, i32).init(std.testing.allocator); |
| | 2116 | defer map.deinit(); |
| | 2117 | |
| | 2118 | // Insert just enough entries so that the map expands. Afterwards, |
| | 2119 | // pop all entries out of the map. |
| | 2120 | |
| | 2121 | var i: i32 = 0; |
| | 2122 | while (i < 9) : (i += 1) { |
| | 2123 | try testing.expect((try map.fetchPut(i, i)) == null); |
| | 2124 | } |
| | 2125 | |
| | 2126 | while (map.popOrNull()) |pop| { |
| | 2127 | try testing.expect(pop.key == i - 1 and pop.value == i - 1); |
| | 2128 | i -= 1; |
| | 2129 | } |
| | 2130 | |
| | 2131 | try testing.expect(map.count() == 0); |
| | 2132 | } |
| | 2133 | |
| 2097 | test "reIndex" { | 2134 | test "reIndex" { |
| 2098 | var map = ArrayHashMap(i32, i32, AutoContext(i32), true).init(std.testing.allocator); | 2135 | var map = ArrayHashMap(i32, i32, AutoContext(i32), true).init(std.testing.allocator); |
| 2099 | defer map.deinit(); | 2136 | defer map.deinit(); |