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 {
775775
776776 // Remove files not in the initial hash.
777777 while (self.files.count() != input_file_count) {
778 var file = self.files.pop();
778 var file = self.files.pop().?;
779779 file.key.deinit(self.cache.gpa);
780780 }
781781
lib/std/array_hash_map.zig+8-40
......@@ -485,15 +485,10 @@ pub fn ArrayHashMapWithAllocator(
485485 return self.unmanaged.shrinkAndFreeContext(self.allocator, new_len, self.ctx);
486486 }
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
493488 /// Removes the last inserted `Entry` in the hash map and returns it if count is nonzero.
494489 /// Otherwise returns null.
495 pub fn popOrNull(self: *Self) ?KV {
496 return self.unmanaged.popOrNullContext(self.ctx);
490 pub fn pop(self: *Self) ?KV {
491 return self.unmanaged.popContext(self.ctx);
497492 }
498493 };
499494}
......@@ -1468,12 +1463,14 @@ pub fn ArrayHashMapUnmanaged(
14681463 }
14691464
14701465 /// 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 {
14721468 if (@sizeOf(ByIndexContext) != 0)
14731469 @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call popContext instead.");
14741470 return self.popContext(undefined);
14751471 }
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;
14771474 self.pointer_stability.lock();
14781475 defer self.pointer_stability.unlock();
14791476
......@@ -1487,17 +1484,6 @@ pub fn ArrayHashMapUnmanaged(
14871484 };
14881485 }
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
15011487 fn fetchRemoveByKey(
15021488 self: *Self,
15031489 key: anytype,
......@@ -2425,25 +2411,7 @@ test "shrink" {
24252411 }
24262412}
24272413
2428test "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" {
2414test "pop()" {
24472415 var map = AutoArrayHashMap(i32, i32).init(std.testing.allocator);
24482416 defer map.deinit();
24492417
......@@ -2455,7 +2423,7 @@ test "popOrNull" {
24552423 try testing.expect((try map.fetchPut(i, i)) == null);
24562424 }
24572425
2458 while (map.popOrNull()) |pop| {
2426 while (map.pop()) |pop| {
24592427 try testing.expect(pop.key == i - 1 and pop.value == i - 1);
24602428 i -= 1;
24612429 }
src/Zcu.zig+2-2
......@@ -3797,7 +3797,7 @@ fn resolveReferencesInner(zcu: *Zcu) !std.AutoHashMapUnmanaged(AnalUnit, ?Resolv
37973797 }
37983798
37993799 while (true) {
3800 if (type_queue.popOrNull()) |kv| {
3800 if (type_queue.pop()) |kv| {
38013801 const ty = kv.key;
38023802 const referencer = kv.value;
38033803 try checked_types.putNoClobber(gpa, ty, {});
......@@ -3920,7 +3920,7 @@ fn resolveReferencesInner(zcu: *Zcu) !std.AutoHashMapUnmanaged(AnalUnit, ?Resolv
39203920 }
39213921 continue;
39223922 }
3923 if (unit_queue.popOrNull()) |kv| {
3923 if (unit_queue.pop()) |kv| {
39243924 const unit = kv.key;
39253925 try result.putNoClobber(gpa, unit, kv.value);
39263926
src/Zcu/PerThread.zig+2-2
......@@ -2089,7 +2089,7 @@ pub fn embedFile(
20892089 errdefer gpa.free(resolved_path);
20902090
20912091 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
20942094 if (gop.found_existing) {
20952095 gpa.free(resolved_path); // we're not using this key
......@@ -2112,7 +2112,7 @@ pub fn embedFile(
21122112 errdefer gpa.free(resolved_path);
21132113
21142114 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
21172117 if (gop.found_existing) {
21182118 gpa.free(resolved_path); // we're not using this key
src/codegen/c.zig+1-1
......@@ -438,7 +438,7 @@ pub const Function = struct {
438438 fn allocAlignedLocal(f: *Function, inst: ?Air.Inst.Index, local_type: LocalType) !CValue {
439439 const result: CValue = result: {
440440 if (f.free_locals_map.getPtr(local_type)) |locals_list| {
441 if (locals_list.popOrNull()) |local_entry| {
441 if (locals_list.pop()) |local_entry| {
442442 break :result .{ .new_local = local_entry.key };
443443 }
444444 }
src/link/Coff.zig+1-1
......@@ -2300,7 +2300,7 @@ fn flushModuleInner(coff: *Coff, arena: Allocator, tid: Zcu.PerThread.Id) !void
23002300 }
23012301 }
23022302
2303 while (coff.unresolved.popOrNull()) |entry| {
2303 while (coff.unresolved.pop()) |entry| {
23042304 assert(entry.value);
23052305 const global = coff.globals.items[entry.key];
23062306 const sym = coff.getSymbol(global);