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(...@@ -399,6 +399,14 @@ pub fn ArrayHashMap(
399 return other.promoteContext(allocator, ctx);399 return other.promoteContext(allocator, ctx);
400 }400 }
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
402 /// Rebuilds the key indexes. If the underlying entries has been modified directly, users410 /// Rebuilds the key indexes. If the underlying entries has been modified directly, users
403 /// can call `reIndex` to update the indexes to account for these new entries.411 /// can call `reIndex` to update the indexes to account for these new entries.
404 pub fn reIndex(self: *Self) !void {412 pub fn reIndex(self: *Self) !void {
...@@ -1149,6 +1157,8 @@ pub fn ArrayHashMapUnmanaged(...@@ -1149,6 +1157,8 @@ pub fn ArrayHashMapUnmanaged(
1149 errdefer other.entries.deinit(allocator);1157 errdefer other.entries.deinit(allocator);
11501158
1151 if (self.index_header) |header| {1159 if (self.index_header) |header| {
1160 // TODO: I'm pretty sure this could be memcpy'd instead of
1161 // doing all this work.
1152 const new_header = try IndexHeader.alloc(allocator, header.bit_index);1162 const new_header = try IndexHeader.alloc(allocator, header.bit_index);
1153 other.insertAllEntriesIntoNewHeader(if (store_hash) {} else ctx, new_header);1163 other.insertAllEntriesIntoNewHeader(if (store_hash) {} else ctx, new_header);
1154 other.index_header = new_header;1164 other.index_header = new_header;
...@@ -1156,6 +1166,14 @@ pub fn ArrayHashMapUnmanaged(...@@ -1156,6 +1166,14 @@ pub fn ArrayHashMapUnmanaged(
1156 return other;1166 return other;
1157 }1167 }
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
1159 /// Rebuilds the key indexes. If the underlying entries has been modified directly, users1177 /// Rebuilds the key indexes. If the underlying entries has been modified directly, users
1160 /// can call `reIndex` to update the indexes to account for these new entries.1178 /// can call `reIndex` to update the indexes to account for these new entries.
1161 pub fn reIndex(self: *Self, allocator: Allocator) !void {1179 pub fn reIndex(self: *Self, allocator: Allocator) !void {
...@@ -1163,6 +1181,7 @@ pub fn ArrayHashMapUnmanaged(...@@ -1163,6 +1181,7 @@ pub fn ArrayHashMapUnmanaged(
1163 @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call reIndexContext instead.");1181 @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call reIndexContext instead.");
1164 return self.reIndexContext(allocator, undefined);1182 return self.reIndexContext(allocator, undefined);
1165 }1183 }
1184
1166 pub fn reIndexContext(self: *Self, allocator: Allocator, ctx: Context) !void {1185 pub fn reIndexContext(self: *Self, allocator: Allocator, ctx: Context) !void {
1167 if (self.entries.capacity <= linear_scan_max) return;1186 if (self.entries.capacity <= linear_scan_max) return;
1168 // We're going to rebuild the index header and replace the existing one (if any). The1187 // 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(...@@ -673,6 +673,14 @@ pub fn HashMap(
673 var other = try self.unmanaged.cloneContext(new_allocator, new_ctx);673 var other = try self.unmanaged.cloneContext(new_allocator, new_ctx);
674 return other.promoteContext(new_allocator, new_ctx);674 return other.promoteContext(new_allocator, new_ctx);
675 }675 }
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 }
676 };684 };
677}685}
678686
...@@ -1488,6 +1496,14 @@ pub fn HashMapUnmanaged(...@@ -1488,6 +1496,14 @@ pub fn HashMapUnmanaged(
1488 return other;1496 return other;
1489 }1497 }
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
1491 fn grow(self: *Self, allocator: Allocator, new_capacity: Size, ctx: Context) Allocator.Error!void {1507 fn grow(self: *Self, allocator: Allocator, new_capacity: Size, ctx: Context) Allocator.Error!void {
1492 @setCold(true);1508 @setCold(true);
1493 const new_cap = std.math.max(new_capacity, minimal_capacity);1509 const new_cap = std.math.max(new_capacity, minimal_capacity);