diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2022-12-02 23:05:12 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2022-12-04 15:57:40 -0700 |
| commit | 954019983d17fad9dac1c80e2de92cb7ebe7cd08 (patch) | |
| tree | fa35eef75ce4b3743f5a8b1c57f1b2a5337653a9 /lib/std/array_hash_map.zig | |
| parent | 2a0efbee56b03d9deafaa7918433ea10b46305c9 (diff) | |
| download | zig-954019983d17fad9dac1c80e2de92cb7ebe7cd08.tar.gz zig-954019983d17fad9dac1c80e2de92cb7ebe7cd08.zip | |
std: add move() functions to hash maps
Diffstat (limited to 'lib/std/array_hash_map.zig')
| -rw-r--r-- | lib/std/array_hash_map.zig | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/lib/std/array_hash_map.zig b/lib/std/array_hash_map.zig index 10e63ec5ee..1d6d86f8f5 100644 --- a/lib/std/array_hash_map.zig +++ b/lib/std/array_hash_map.zig @@ -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 |
