Commit a8af36ab10

Meghan Denny <hello@nektro.net>
2025-02-02 09:03:19
std.ArrayHashMap: popOrNul() -> pop()
1 parent 84d2c6d
Changed files (6)
lib/std/Build/Cache.zig
@@ -775,7 +775,7 @@ pub const Manifest = struct {
 
         // Remove files not in the initial hash.
         while (self.files.count() != input_file_count) {
-            var file = self.files.pop();
+            var file = self.files.pop().?;
             file.key.deinit(self.cache.gpa);
         }
 
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;
     }
src/codegen/c.zig
@@ -438,7 +438,7 @@ pub const Function = struct {
     fn allocAlignedLocal(f: *Function, inst: ?Air.Inst.Index, local_type: LocalType) !CValue {
         const result: CValue = result: {
             if (f.free_locals_map.getPtr(local_type)) |locals_list| {
-                if (locals_list.popOrNull()) |local_entry| {
+                if (locals_list.pop()) |local_entry| {
                     break :result .{ .new_local = local_entry.key };
                 }
             }
src/link/Coff.zig
@@ -2300,7 +2300,7 @@ fn flushModuleInner(coff: *Coff, arena: Allocator, tid: Zcu.PerThread.Id) !void
         }
     }
 
-    while (coff.unresolved.popOrNull()) |entry| {
+    while (coff.unresolved.pop()) |entry| {
         assert(entry.value);
         const global = coff.globals.items[entry.key];
         const sym = coff.getSymbol(global);
src/Zcu/PerThread.zig
@@ -2089,7 +2089,7 @@ pub fn embedFile(
         errdefer gpa.free(resolved_path);
 
         const gop = try zcu.embed_table.getOrPut(gpa, resolved_path);
-        errdefer assert(std.mem.eql(u8, zcu.embed_table.pop().key, resolved_path));
+        errdefer assert(std.mem.eql(u8, zcu.embed_table.pop().?.key, resolved_path));
 
         if (gop.found_existing) {
             gpa.free(resolved_path); // we're not using this key
@@ -2112,7 +2112,7 @@ pub fn embedFile(
     errdefer gpa.free(resolved_path);
 
     const gop = try zcu.embed_table.getOrPut(gpa, resolved_path);
-    errdefer assert(std.mem.eql(u8, zcu.embed_table.pop().key, resolved_path));
+    errdefer assert(std.mem.eql(u8, zcu.embed_table.pop().?.key, resolved_path));
 
     if (gop.found_existing) {
         gpa.free(resolved_path); // we're not using this key
src/Zcu.zig
@@ -3797,7 +3797,7 @@ fn resolveReferencesInner(zcu: *Zcu) !std.AutoHashMapUnmanaged(AnalUnit, ?Resolv
     }
 
     while (true) {
-        if (type_queue.popOrNull()) |kv| {
+        if (type_queue.pop()) |kv| {
             const ty = kv.key;
             const referencer = kv.value;
             try checked_types.putNoClobber(gpa, ty, {});
@@ -3920,7 +3920,7 @@ fn resolveReferencesInner(zcu: *Zcu) !std.AutoHashMapUnmanaged(AnalUnit, ?Resolv
             }
             continue;
         }
-        if (unit_queue.popOrNull()) |kv| {
+        if (unit_queue.pop()) |kv| {
             const unit = kv.key;
             try result.putNoClobber(gpa, unit, kv.value);