Skip to content

Commit

Permalink
std.ArrayHashMap: popOrNul() -> pop()
Browse files Browse the repository at this point in the history
  • Loading branch information
nektro authored and andrewrk committed Feb 8, 2025
1 parent 84d2c6d commit a8af36a
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 47 deletions.
2 changes: 1 addition & 1 deletion lib/std/Build/Cache.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
48 changes: 8 additions & 40 deletions lib/std/array_hash_map.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
}
Expand Down Expand Up @@ -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();

Expand All @@ -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,
Expand Down Expand Up @@ -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();

Expand All @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Zcu.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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, {});
Expand Down Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions src/Zcu/PerThread.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/codegen/c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/link/Coff.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a8af36a

Please sign in to comment.