authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2025-02-02 00:03:19-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-02-07 17:52:19-08:00
loga8af36ab10562e58ba237fa3dcc582228197402b
treece1643493977a56f8424fa65c46b9eab3da3dbf5
parent84d2c6dc72738bede35651a30dbcdbae3f3b30fc

std.ArrayHashMap: popOrNul() -> pop()


6 files changed, 15 insertions(+), 47 deletions(-)

lib/std/Build/Cache.zig+1-1
...@@ -775,7 +775,7 @@ pub const Manifest = struct {...@@ -775,7 +775,7 @@ pub const Manifest = struct {
775775
776 // Remove files not in the initial hash.776 // Remove files not in the initial hash.
777 while (self.files.count() != input_file_count) {777 while (self.files.count() != input_file_count) {
778 var file = self.files.pop();778 var file = self.files.pop().?;
779 file.key.deinit(self.cache.gpa);779 file.key.deinit(self.cache.gpa);
780 }780 }
781781
lib/std/array_hash_map.zig+8-40
...@@ -485,15 +485,10 @@ pub fn ArrayHashMapWithAllocator(...@@ -485,15 +485,10 @@ pub fn ArrayHashMapWithAllocator(
485 return self.unmanaged.shrinkAndFreeContext(self.allocator, new_len, self.ctx);485 return self.unmanaged.shrinkAndFreeContext(self.allocator, new_len, self.ctx);
486 }486 }
487487
488 /// Removes the last inserted `Entry` in the hash map and returns it.
489 pub fn pop(self: *Self) KV {
490 return self.unmanaged.popContext(self.ctx);
491 }
492
493 /// Removes the last inserted `Entry` in the hash map and returns it if count is nonzero.488 /// Removes the last inserted `Entry` in the hash map and returns it if count is nonzero.
494 /// Otherwise returns null.489 /// Otherwise returns null.
495 pub fn popOrNull(self: *Self) ?KV {490 pub fn pop(self: *Self) ?KV {
496 return self.unmanaged.popOrNullContext(self.ctx);491 return self.unmanaged.popContext(self.ctx);
497 }492 }
498 };493 };
499}494}
...@@ -1468,12 +1463,14 @@ pub fn ArrayHashMapUnmanaged(...@@ -1468,12 +1463,14 @@ pub fn ArrayHashMapUnmanaged(
1468 }1463 }
14691464
1470 /// Removes the last inserted `Entry` in the hash map and returns it.1465 /// Removes the last inserted `Entry` in the hash map and returns it.
1471 pub fn pop(self: *Self) KV {1466 /// Otherwise returns null.
1467 pub fn pop(self: *Self) ?KV {
1472 if (@sizeOf(ByIndexContext) != 0)1468 if (@sizeOf(ByIndexContext) != 0)
1473 @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call popContext instead.");1469 @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call popContext instead.");
1474 return self.popContext(undefined);1470 return self.popContext(undefined);
1475 }1471 }
1476 pub fn popContext(self: *Self, ctx: Context) KV {1472 pub fn popContext(self: *Self, ctx: Context) ?KV {
1473 if (self.entries.len == 0) return null;
1477 self.pointer_stability.lock();1474 self.pointer_stability.lock();
1478 defer self.pointer_stability.unlock();1475 defer self.pointer_stability.unlock();
14791476
...@@ -1487,17 +1484,6 @@ pub fn ArrayHashMapUnmanaged(...@@ -1487,17 +1484,6 @@ pub fn ArrayHashMapUnmanaged(
1487 };1484 };
1488 }1485 }
14891486
1490 /// Removes the last inserted `Entry` in the hash map and returns it if count is nonzero.
1491 /// Otherwise returns null.
1492 pub fn popOrNull(self: *Self) ?KV {
1493 if (@sizeOf(ByIndexContext) != 0)
1494 @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call popContext instead.");
1495 return self.popOrNullContext(undefined);
1496 }
1497 pub fn popOrNullContext(self: *Self, ctx: Context) ?KV {
1498 return if (self.entries.len == 0) null else self.popContext(ctx);
1499 }
1500
1501 fn fetchRemoveByKey(1487 fn fetchRemoveByKey(
1502 self: *Self,1488 self: *Self,
1503 key: anytype,1489 key: anytype,
...@@ -2425,25 +2411,7 @@ test "shrink" {...@@ -2425,25 +2411,7 @@ test "shrink" {
2425 }2411 }
2426}2412}
24272413
2428test "pop" {2414test "pop()" {
2429 var map = AutoArrayHashMap(i32, i32).init(std.testing.allocator);
2430 defer map.deinit();
2431
2432 // Insert just enough entries so that the map expands. Afterwards,
2433 // pop all entries out of the map.
2434
2435 var i: i32 = 0;
2436 while (i < 9) : (i += 1) {
2437 try testing.expect((try map.fetchPut(i, i)) == null);
2438 }
2439
2440 while (i > 0) : (i -= 1) {
2441 const pop = map.pop();
2442 try testing.expect(pop.key == i - 1 and pop.value == i - 1);
2443 }
2444}
2445
2446test "popOrNull" {
2447 var map = AutoArrayHashMap(i32, i32).init(std.testing.allocator);2415 var map = AutoArrayHashMap(i32, i32).init(std.testing.allocator);
2448 defer map.deinit();2416 defer map.deinit();
24492417
...@@ -2455,7 +2423,7 @@ test "popOrNull" {...@@ -2455,7 +2423,7 @@ test "popOrNull" {
2455 try testing.expect((try map.fetchPut(i, i)) == null);2423 try testing.expect((try map.fetchPut(i, i)) == null);
2456 }2424 }
24572425
2458 while (map.popOrNull()) |pop| {2426 while (map.pop()) |pop| {
2459 try testing.expect(pop.key == i - 1 and pop.value == i - 1);2427 try testing.expect(pop.key == i - 1 and pop.value == i - 1);
2460 i -= 1;2428 i -= 1;
2461 }2429 }
src/Zcu.zig+2-2
...@@ -3797,7 +3797,7 @@ fn resolveReferencesInner(zcu: *Zcu) !std.AutoHashMapUnmanaged(AnalUnit, ?Resolv...@@ -3797,7 +3797,7 @@ fn resolveReferencesInner(zcu: *Zcu) !std.AutoHashMapUnmanaged(AnalUnit, ?Resolv
3797 }3797 }
37983798
3799 while (true) {3799 while (true) {
3800 if (type_queue.popOrNull()) |kv| {3800 if (type_queue.pop()) |kv| {
3801 const ty = kv.key;3801 const ty = kv.key;
3802 const referencer = kv.value;3802 const referencer = kv.value;
3803 try checked_types.putNoClobber(gpa, ty, {});3803 try checked_types.putNoClobber(gpa, ty, {});
...@@ -3920,7 +3920,7 @@ fn resolveReferencesInner(zcu: *Zcu) !std.AutoHashMapUnmanaged(AnalUnit, ?Resolv...@@ -3920,7 +3920,7 @@ fn resolveReferencesInner(zcu: *Zcu) !std.AutoHashMapUnmanaged(AnalUnit, ?Resolv
3920 }3920 }
3921 continue;3921 continue;
3922 }3922 }
3923 if (unit_queue.popOrNull()) |kv| {3923 if (unit_queue.pop()) |kv| {
3924 const unit = kv.key;3924 const unit = kv.key;
3925 try result.putNoClobber(gpa, unit, kv.value);3925 try result.putNoClobber(gpa, unit, kv.value);
39263926
src/Zcu/PerThread.zig+2-2
...@@ -2089,7 +2089,7 @@ pub fn embedFile(...@@ -2089,7 +2089,7 @@ pub fn embedFile(
2089 errdefer gpa.free(resolved_path);2089 errdefer gpa.free(resolved_path);
20902090
2091 const gop = try zcu.embed_table.getOrPut(gpa, resolved_path);2091 const gop = try zcu.embed_table.getOrPut(gpa, resolved_path);
2092 errdefer assert(std.mem.eql(u8, zcu.embed_table.pop().key, resolved_path));2092 errdefer assert(std.mem.eql(u8, zcu.embed_table.pop().?.key, resolved_path));
20932093
2094 if (gop.found_existing) {2094 if (gop.found_existing) {
2095 gpa.free(resolved_path); // we're not using this key2095 gpa.free(resolved_path); // we're not using this key
...@@ -2112,7 +2112,7 @@ pub fn embedFile(...@@ -2112,7 +2112,7 @@ pub fn embedFile(
2112 errdefer gpa.free(resolved_path);2112 errdefer gpa.free(resolved_path);
21132113
2114 const gop = try zcu.embed_table.getOrPut(gpa, resolved_path);2114 const gop = try zcu.embed_table.getOrPut(gpa, resolved_path);
2115 errdefer assert(std.mem.eql(u8, zcu.embed_table.pop().key, resolved_path));2115 errdefer assert(std.mem.eql(u8, zcu.embed_table.pop().?.key, resolved_path));
21162116
2117 if (gop.found_existing) {2117 if (gop.found_existing) {
2118 gpa.free(resolved_path); // we're not using this key2118 gpa.free(resolved_path); // we're not using this key
src/codegen/c.zig+1-1
...@@ -438,7 +438,7 @@ pub const Function = struct {...@@ -438,7 +438,7 @@ pub const Function = struct {
438 fn allocAlignedLocal(f: *Function, inst: ?Air.Inst.Index, local_type: LocalType) !CValue {438 fn allocAlignedLocal(f: *Function, inst: ?Air.Inst.Index, local_type: LocalType) !CValue {
439 const result: CValue = result: {439 const result: CValue = result: {
440 if (f.free_locals_map.getPtr(local_type)) |locals_list| {440 if (f.free_locals_map.getPtr(local_type)) |locals_list| {
441 if (locals_list.popOrNull()) |local_entry| {441 if (locals_list.pop()) |local_entry| {
442 break :result .{ .new_local = local_entry.key };442 break :result .{ .new_local = local_entry.key };
443 }443 }
444 }444 }
src/link/Coff.zig+1-1
...@@ -2300,7 +2300,7 @@ fn flushModuleInner(coff: *Coff, arena: Allocator, tid: Zcu.PerThread.Id) !void...@@ -2300,7 +2300,7 @@ fn flushModuleInner(coff: *Coff, arena: Allocator, tid: Zcu.PerThread.Id) !void
2300 }2300 }
2301 }2301 }
23022302
2303 while (coff.unresolved.popOrNull()) |entry| {2303 while (coff.unresolved.pop()) |entry| {
2304 assert(entry.value);2304 assert(entry.value);
2305 const global = coff.globals.items[entry.key];2305 const global = coff.globals.items[entry.key];
2306 const sym = coff.getSymbol(global);2306 const sym = coff.getSymbol(global);