authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-02 23:05:12-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-04 15:57:40-07:00
log954019983d17fad9dac1c80e2de92cb7ebe7cd08
treefa35eef75ce4b3743f5a8b1c57f1b2a5337653a9
parent2a0efbee56b03d9deafaa7918433ea10b46305c9

std: add move() functions to hash maps


2 files changed, 35 insertions(+), 0 deletions(-)

lib/std/array_hash_map.zig+19
......@@ -399,6 +399,14 @@ pub fn ArrayHashMap(
399399 return other.promoteContext(allocator, ctx);
400400 }
401401
402 /// Set the map to an empty state, making deinitialization a no-op, and
403 /// returning a copy of the original.
404 pub fn move(self: *Self) Self {
405 const result = self.*;
406 self.unmanaged = .{};
407 return result;
408 }
409
402410 /// Rebuilds the key indexes. If the underlying entries has been modified directly, users
403411 /// can call `reIndex` to update the indexes to account for these new entries.
404412 pub fn reIndex(self: *Self) !void {
......@@ -1149,6 +1157,8 @@ pub fn ArrayHashMapUnmanaged(
11491157 errdefer other.entries.deinit(allocator);
11501158
11511159 if (self.index_header) |header| {
1160 // TODO: I'm pretty sure this could be memcpy'd instead of
1161 // doing all this work.
11521162 const new_header = try IndexHeader.alloc(allocator, header.bit_index);
11531163 other.insertAllEntriesIntoNewHeader(if (store_hash) {} else ctx, new_header);
11541164 other.index_header = new_header;
......@@ -1156,6 +1166,14 @@ pub fn ArrayHashMapUnmanaged(
11561166 return other;
11571167 }
11581168
1169 /// Set the map to an empty state, making deinitialization a no-op, and
1170 /// returning a copy of the original.
1171 pub fn move(self: *Self) Self {
1172 const result = self.*;
1173 self.* = .{};
1174 return result;
1175 }
1176
11591177 /// Rebuilds the key indexes. If the underlying entries has been modified directly, users
11601178 /// can call `reIndex` to update the indexes to account for these new entries.
11611179 pub fn reIndex(self: *Self, allocator: Allocator) !void {
......@@ -1163,6 +1181,7 @@ pub fn ArrayHashMapUnmanaged(
11631181 @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call reIndexContext instead.");
11641182 return self.reIndexContext(allocator, undefined);
11651183 }
1184
11661185 pub fn reIndexContext(self: *Self, allocator: Allocator, ctx: Context) !void {
11671186 if (self.entries.capacity <= linear_scan_max) return;
11681187 // We're going to rebuild the index header and replace the existing one (if any). The
lib/std/hash_map.zig+16
......@@ -673,6 +673,14 @@ pub fn HashMap(
673673 var other = try self.unmanaged.cloneContext(new_allocator, new_ctx);
674674 return other.promoteContext(new_allocator, new_ctx);
675675 }
676
677 /// Set the map to an empty state, making deinitialization a no-op, and
678 /// returning a copy of the original.
679 pub fn move(self: *Self) Self {
680 const result = self.*;
681 self.unmanaged = .{};
682 return result;
683 }
676684 };
677685}
678686
......@@ -1488,6 +1496,14 @@ pub fn HashMapUnmanaged(
14881496 return other;
14891497 }
14901498
1499 /// Set the map to an empty state, making deinitialization a no-op, and
1500 /// returning a copy of the original.
1501 pub fn move(self: *Self) Self {
1502 const result = self.*;
1503 self.* = .{};
1504 return result;
1505 }
1506
14911507 fn grow(self: *Self, allocator: Allocator, new_capacity: Size, ctx: Context) Allocator.Error!void {
14921508 @setCold(true);
14931509 const new_cap = std.math.max(new_capacity, minimal_capacity);