Skip to content

std: Support equivalent ArrayList operations in ArrayHashMap #7622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
367 changes: 333 additions & 34 deletions lib/std/array_hash_map.zig

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions lib/std/array_list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {

/// Reduce allocated capacity to `new_len`.
/// May invalidate element pointers.
pub fn shrink(self: *Self, new_len: usize) void {
pub fn shrinkAndFree(self: *Self, new_len: usize) void {
assert(new_len <= self.items.len);

self.items = self.allocator.realloc(self.allocatedSlice(), new_len) catch |e| switch (e) {
Expand Down Expand Up @@ -585,7 +585,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
}

/// Reduce allocated capacity to `new_len`.
pub fn shrink(self: *Self, allocator: *Allocator, new_len: usize) void {
pub fn shrinkAndFree(self: *Self, allocator: *Allocator, new_len: usize) void {
assert(new_len <= self.items.len);

self.items = allocator.realloc(self.allocatedSlice(), new_len) catch |e| switch (e) {
Expand Down Expand Up @@ -1153,7 +1153,7 @@ test "std.ArrayList/ArrayListUnmanaged.shrink still sets length on error.OutOfMe
try list.append(2);
try list.append(3);

list.shrink(1);
list.shrinkAndFree(1);
testing.expect(list.items.len == 1);
}
{
Expand All @@ -1163,7 +1163,7 @@ test "std.ArrayList/ArrayListUnmanaged.shrink still sets length on error.OutOfMe
try list.append(a, 2);
try list.append(a, 3);

list.shrink(a, 1);
list.shrinkAndFree(a, 1);
testing.expect(list.items.len == 1);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/std/fs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2186,7 +2186,7 @@ pub const Walker = struct {
var top = &self.stack.items[self.stack.items.len - 1];
const dirname_len = top.dirname_len;
if (try top.dir_it.next()) |base| {
self.name_buffer.shrink(dirname_len);
self.name_buffer.shrinkAndFree(dirname_len);
try self.name_buffer.append(path.sep);
try self.name_buffer.appendSlice(base.name);
if (base.kind == .Directory) {
Expand Down
6 changes: 3 additions & 3 deletions lib/std/io/reader.zig
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ pub fn Reader(
start_index += bytes_read;

if (start_index - original_len > max_append_size) {
array_list.shrink(original_len + max_append_size);
array_list.shrinkAndFree(original_len + max_append_size);
return error.StreamTooLong;
}

if (bytes_read != dest_slice.len) {
array_list.shrink(start_index);
array_list.shrinkAndFree(start_index);
return;
}

Expand Down Expand Up @@ -111,7 +111,7 @@ pub fn Reader(
delimiter: u8,
max_size: usize,
) !void {
array_list.shrink(0);
array_list.shrinkAndFree(0);
while (true) {
var byte: u8 = try self.readByte();

Expand Down
2 changes: 1 addition & 1 deletion lib/std/json.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1897,7 +1897,7 @@ pub const Parser = struct {

pub fn reset(p: *Parser) void {
p.state = .Simple;
p.stack.shrink(0);
p.stack.shrinkAndFree(0);
}

pub fn parse(p: *Parser, input: []const u8) !ValueTree {
Expand Down
2 changes: 1 addition & 1 deletion lib/std/math/big/int.zig
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ pub const Mutable = struct {
/// it will have the same length as it had when the function was called.
pub fn gcd(rma: *Mutable, x: Const, y: Const, limbs_buffer: *std.ArrayList(Limb)) !void {
const prev_len = limbs_buffer.items.len;
defer limbs_buffer.shrink(prev_len);
defer limbs_buffer.shrinkAndFree(prev_len);
const x_copy = if (rma.limbs.ptr == x.limbs.ptr) blk: {
const start = limbs_buffer.items.len;
try limbs_buffer.appendSlice(x.limbs);
Expand Down
4 changes: 2 additions & 2 deletions lib/std/net.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1200,13 +1200,13 @@ fn linuxLookupNameFromDnsSearch(

var tok_it = mem.tokenize(search, " \t");
while (tok_it.next()) |tok| {
canon.shrink(canon_name.len + 1);
canon.shrinkAndFree(canon_name.len + 1);
try canon.appendSlice(tok);
try linuxLookupNameFromDns(addrs, canon, canon.items, family, rc, port);
if (addrs.items.len != 0) return;
}

canon.shrink(canon_name.len);
canon.shrinkAndFree(canon_name.len);
return linuxLookupNameFromDns(addrs, canon, name, family, rc, port);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Compilation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
}
assert(mem.endsWith(u8, buf.items, ","));
buf.items[buf.items.len - 1] = 0;
buf.shrink(buf.items.len);
buf.shrinkAndFree(buf.items.len);
break :blk buf.items[0 .. buf.items.len - 1 :0].ptr;
} else null;

Expand Down
16 changes: 8 additions & 8 deletions src/Module.zig
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ pub const Scope = struct {
}

pub fn removeDecl(self: *Container, child: *Decl) void {
_ = self.decls.remove(child);
_ = self.decls.swapRemove(child);
}

pub fn fullyQualifiedNameHash(self: *Container, name: []const u8) NameHash {
Expand Down Expand Up @@ -1660,7 +1660,7 @@ pub fn analyzeContainer(self: *Module, container_scope: *Scope.Container) !void
// Update the AST Node index of the decl, even if its contents are unchanged, it may
// have been re-ordered.
decl.src_index = decl_i;
if (deleted_decls.remove(decl) == null) {
if (deleted_decls.swapRemove(decl) == null) {
decl.analysis = .sema_failure;
const err_msg = try Compilation.ErrorMsg.create(self.gpa, tree.token_locs[name_tok].start, "redefinition of '{s}'", .{decl.name});
errdefer err_msg.destroy(self.gpa);
Expand Down Expand Up @@ -1702,7 +1702,7 @@ pub fn analyzeContainer(self: *Module, container_scope: *Scope.Container) !void
// Update the AST Node index of the decl, even if its contents are unchanged, it may
// have been re-ordered.
decl.src_index = decl_i;
if (deleted_decls.remove(decl) == null) {
if (deleted_decls.swapRemove(decl) == null) {
decl.analysis = .sema_failure;
const err_msg = try Compilation.ErrorMsg.create(self.gpa, name_loc.start, "redefinition of '{s}'", .{decl.name});
errdefer err_msg.destroy(self.gpa);
Expand Down Expand Up @@ -1832,7 +1832,7 @@ pub fn deleteDecl(self: *Module, decl: *Decl) !void {
try self.markOutdatedDecl(dep);
}
}
if (self.failed_decls.remove(decl)) |entry| {
if (self.failed_decls.swapRemove(decl)) |entry| {
entry.value.destroy(self.gpa);
}
self.deleteDeclExports(decl);
Expand All @@ -1843,7 +1843,7 @@ pub fn deleteDecl(self: *Module, decl: *Decl) !void {
/// Delete all the Export objects that are caused by this Decl. Re-analysis of
/// this Decl will cause them to be re-created (or not).
fn deleteDeclExports(self: *Module, decl: *Decl) void {
const kv = self.export_owners.remove(decl) orelse return;
const kv = self.export_owners.swapRemove(decl) orelse return;

for (kv.value) |exp| {
if (self.decl_exports.getEntry(exp.exported_decl)) |decl_exports_kv| {
Expand All @@ -1870,10 +1870,10 @@ fn deleteDeclExports(self: *Module, decl: *Decl) void {
if (self.comp.bin_file.cast(link.File.MachO)) |macho| {
macho.deleteExport(exp.link.macho);
}
if (self.failed_exports.remove(exp)) |entry| {
if (self.failed_exports.swapRemove(exp)) |entry| {
entry.value.destroy(self.gpa);
}
_ = self.symbol_exports.remove(exp.options.name);
_ = self.symbol_exports.swapRemove(exp.options.name);
self.gpa.free(exp.options.name);
self.gpa.destroy(exp);
}
Expand Down Expand Up @@ -1918,7 +1918,7 @@ pub fn analyzeFnBody(self: *Module, decl: *Decl, func: *Fn) !void {
fn markOutdatedDecl(self: *Module, decl: *Decl) !void {
log.debug("mark {s} outdated\n", .{decl.name});
try self.comp.work_queue.writeItem(.{ .analyze_decl = decl });
if (self.failed_decls.remove(decl)) |entry| {
if (self.failed_decls.swapRemove(decl)) |entry| {
entry.value.destroy(self.gpa);
}
decl.analysis = .outdated;
Expand Down
2 changes: 1 addition & 1 deletion src/codegen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2123,7 +2123,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
try parent_branch.inst_table.ensureCapacity(self.gpa, parent_branch.inst_table.items().len +
else_branch.inst_table.items().len);
for (else_branch.inst_table.items()) |else_entry| {
const canon_mcv = if (saved_then_branch.inst_table.remove(else_entry.key)) |then_entry| blk: {
const canon_mcv = if (saved_then_branch.inst_table.swapRemove(else_entry.key)) |then_entry| blk: {
// The instruction's MCValue is overridden in both branches.
parent_branch.inst_table.putAssumeCapacity(else_entry.key, then_entry.value);
if (else_entry.value == .dead) {
Expand Down
6 changes: 3 additions & 3 deletions src/libc_installation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ pub const LibCInstallation = struct {
defer result_buf.deinit();

for (searches) |search| {
result_buf.shrink(0);
result_buf.shrinkAndFree(0);
try result_buf.outStream().print("{s}\\Include\\{s}\\ucrt", .{ search.path, search.version });

var dir = fs.cwd().openDir(result_buf.items, .{}) catch |err| switch (err) {
Expand Down Expand Up @@ -383,7 +383,7 @@ pub const LibCInstallation = struct {
};

for (searches) |search| {
result_buf.shrink(0);
result_buf.shrinkAndFree(0);
try result_buf.outStream().print("{s}\\Lib\\{s}\\ucrt\\{s}", .{ search.path, search.version, arch_sub_dir });

var dir = fs.cwd().openDir(result_buf.items, .{}) catch |err| switch (err) {
Expand Down Expand Up @@ -437,7 +437,7 @@ pub const LibCInstallation = struct {
};

for (searches) |search| {
result_buf.shrink(0);
result_buf.shrinkAndFree(0);
const stream = result_buf.outStream();
try stream.print("{s}\\Lib\\{s}\\um\\{s}", .{ search.path, search.version, arch_sub_dir });

Expand Down
10 changes: 5 additions & 5 deletions src/translate_c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2846,7 +2846,7 @@ fn transCase(

// take all pending statements
try switch_scope.pending_block.statements.appendSlice(block_scope.statements.items);
block_scope.statements.shrink(0);
block_scope.statements.shrinkAndFree(0);

const pending_node = try switch_scope.pending_block.complete(rp.c);
switch_scope.pending_block.deinit();
Expand Down Expand Up @@ -2884,7 +2884,7 @@ fn transDefault(

// take all pending statements
try switch_scope.pending_block.statements.appendSlice(block_scope.statements.items);
block_scope.statements.shrink(0);
block_scope.statements.shrinkAndFree(0);

const pending_node = try switch_scope.pending_block.complete(rp.c);
switch_scope.pending_block.deinit();
Expand Down Expand Up @@ -4773,9 +4773,9 @@ const RestorePoint = struct {
src_buf_index: usize,

fn activate(self: RestorePoint) void {
self.c.token_ids.shrink(self.c.gpa, self.token_index);
self.c.token_locs.shrink(self.c.gpa, self.token_index);
self.c.source_buffer.shrink(self.src_buf_index);
self.c.token_ids.shrinkAndFree(self.c.gpa, self.token_index);
self.c.token_locs.shrinkAndFree(self.c.gpa, self.token_index);
self.c.source_buffer.shrinkAndFree(self.src_buf_index);
}
};

Expand Down