std: add move() functions to hash maps

This commit is contained in:
Andrew Kelley 2022-12-02 23:05:12 -07:00
parent 2a0efbee56
commit 954019983d
2 changed files with 35 additions and 0 deletions

View file

@ -399,6 +399,14 @@ pub fn ArrayHashMap(
return other.promoteContext(allocator, ctx);
}
/// Set the map to an empty state, making deinitialization a no-op, and
/// returning a copy of the original.
pub fn move(self: *Self) Self {
const result = self.*;
self.unmanaged = .{};
return result;
}
/// Rebuilds the key indexes. If the underlying entries has been modified directly, users
/// can call `reIndex` to update the indexes to account for these new entries.
pub fn reIndex(self: *Self) !void {
@ -1149,6 +1157,8 @@ pub fn ArrayHashMapUnmanaged(
errdefer other.entries.deinit(allocator);
if (self.index_header) |header| {
// TODO: I'm pretty sure this could be memcpy'd instead of
// doing all this work.
const new_header = try IndexHeader.alloc(allocator, header.bit_index);
other.insertAllEntriesIntoNewHeader(if (store_hash) {} else ctx, new_header);
other.index_header = new_header;
@ -1156,6 +1166,14 @@ pub fn ArrayHashMapUnmanaged(
return other;
}
/// Set the map to an empty state, making deinitialization a no-op, and
/// returning a copy of the original.
pub fn move(self: *Self) Self {
const result = self.*;
self.* = .{};
return result;
}
/// Rebuilds the key indexes. If the underlying entries has been modified directly, users
/// can call `reIndex` to update the indexes to account for these new entries.
pub fn reIndex(self: *Self, allocator: Allocator) !void {
@ -1163,6 +1181,7 @@ pub fn ArrayHashMapUnmanaged(
@compileError("Cannot infer context " ++ @typeName(Context) ++ ", call reIndexContext instead.");
return self.reIndexContext(allocator, undefined);
}
pub fn reIndexContext(self: *Self, allocator: Allocator, ctx: Context) !void {
if (self.entries.capacity <= linear_scan_max) return;
// We're going to rebuild the index header and replace the existing one (if any). The

View file

@ -673,6 +673,14 @@ pub fn HashMap(
var other = try self.unmanaged.cloneContext(new_allocator, new_ctx);
return other.promoteContext(new_allocator, new_ctx);
}
/// Set the map to an empty state, making deinitialization a no-op, and
/// returning a copy of the original.
pub fn move(self: *Self) Self {
const result = self.*;
self.unmanaged = .{};
return result;
}
};
}
@ -1488,6 +1496,14 @@ pub fn HashMapUnmanaged(
return other;
}
/// Set the map to an empty state, making deinitialization a no-op, and
/// returning a copy of the original.
pub fn move(self: *Self) Self {
const result = self.*;
self.* = .{};
return result;
}
fn grow(self: *Self, allocator: Allocator, new_capacity: Size, ctx: Context) Allocator.Error!void {
@setCold(true);
const new_cap = std.math.max(new_capacity, minimal_capacity);