authorgravatar for 69403556+SeanTheGleaming@users.noreply.github.comSean <69403556+SeanTheGleaming@users.noreply.github.com> 2024-05-16 18:56:35-04:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2024-05-27 13:22:50+03:00
logc0da92f71476651ac654249d54512e514b55df55
tree6e97c4981735facf5d044fb148ceaa91c6d3b79c
parent389181f6be8810b5cd432e236a962229257a5b59

hash_map.zig: Pass `self` by value and less pointer-int conversion

- Used `Self` instead of `*const Self` where appropriate (orignally proposed in #19770) - Replaced `@intFromPtr` and `@ptrFromInt` with `@ptrCast`, `@alignCast`, and pointer arithmetic where appropriate With this, the only remaining instance on pointer-int conversion in hash_map.zig is in `HashMapUnmanaged.removeByPtr`, which easily be able to be eliminated once pointer subtraction is supported.

1 files changed, 20 insertions(+), 20 deletions(-)

lib/std/hash_map.zig+20-20
...@@ -471,13 +471,13 @@ pub fn HashMap(...@@ -471,13 +471,13 @@ pub fn HashMap(
471471
472 /// Create an iterator over the keys in the map.472 /// Create an iterator over the keys in the map.
473 /// The iterator is invalidated if the map is modified.473 /// The iterator is invalidated if the map is modified.
474 pub fn keyIterator(self: *const Self) KeyIterator {474 pub fn keyIterator(self: Self) KeyIterator {
475 return self.unmanaged.keyIterator();475 return self.unmanaged.keyIterator();
476 }476 }
477477
478 /// Create an iterator over the values in the map.478 /// Create an iterator over the values in the map.
479 /// The iterator is invalidated if the map is modified.479 /// The iterator is invalidated if the map is modified.
480 pub fn valueIterator(self: *const Self) ValueIterator {480 pub fn valueIterator(self: Self) ValueIterator {
481 return self.unmanaged.valueIterator();481 return self.unmanaged.valueIterator();
482 }482 }
483483
...@@ -542,7 +542,7 @@ pub fn HashMap(...@@ -542,7 +542,7 @@ pub fn HashMap(
542542
543 /// Returns the number of total elements which may be present before it is543 /// Returns the number of total elements which may be present before it is
544 /// no longer guaranteed that no allocations will be performed.544 /// no longer guaranteed that no allocations will be performed.
545 pub fn capacity(self: *Self) Size {545 pub fn capacity(self: Self) Size {
546 return self.unmanaged.capacity();546 return self.unmanaged.capacity();
547 }547 }
548548
...@@ -977,23 +977,23 @@ pub fn HashMapUnmanaged(...@@ -977,23 +977,23 @@ pub fn HashMapUnmanaged(
977 self.available = 0;977 self.available = 0;
978 }978 }
979979
980 pub fn count(self: *const Self) Size {980 pub fn count(self: Self) Size {
981 return self.size;981 return self.size;
982 }982 }
983983
984 fn header(self: *const Self) *Header {984 fn header(self: Self) *Header {
985 return @ptrCast(@as([*]Header, @ptrCast(@alignCast(self.metadata.?))) - 1);985 return @ptrCast(@as([*]Header, @ptrCast(@alignCast(self.metadata.?))) - 1);
986 }986 }
987987
988 fn keys(self: *const Self) [*]K {988 fn keys(self: Self) [*]K {
989 return self.header().keys;989 return self.header().keys;
990 }990 }
991991
992 fn values(self: *const Self) [*]V {992 fn values(self: Self) [*]V {
993 return self.header().values;993 return self.header().values;
994 }994 }
995995
996 pub fn capacity(self: *const Self) Size {996 pub fn capacity(self: Self) Size {
997 if (self.metadata == null) return 0;997 if (self.metadata == null) return 0;
998998
999 return self.header().capacity;999 return self.header().capacity;
...@@ -1003,7 +1003,7 @@ pub fn HashMapUnmanaged(...@@ -1003,7 +1003,7 @@ pub fn HashMapUnmanaged(
1003 return .{ .hm = self };1003 return .{ .hm = self };
1004 }1004 }
10051005
1006 pub fn keyIterator(self: *const Self) KeyIterator {1006 pub fn keyIterator(self: Self) KeyIterator {
1007 if (self.metadata) |metadata| {1007 if (self.metadata) |metadata| {
1008 return .{1008 return .{
1009 .len = self.capacity(),1009 .len = self.capacity(),
...@@ -1019,7 +1019,7 @@ pub fn HashMapUnmanaged(...@@ -1019,7 +1019,7 @@ pub fn HashMapUnmanaged(
1019 }1019 }
1020 }1020 }
10211021
1022 pub fn valueIterator(self: *const Self) ValueIterator {1022 pub fn valueIterator(self: Self) ValueIterator {
1023 if (self.metadata) |metadata| {1023 if (self.metadata) |metadata| {
1024 return .{1024 return .{
1025 .len = self.capacity(),1025 .len = self.capacity(),
...@@ -1439,15 +1439,15 @@ pub fn HashMapUnmanaged(...@@ -1439,15 +1439,15 @@ pub fn HashMapUnmanaged(
1439 }1439 }
14401440
1441 /// Return true if there is a value associated with key in the map.1441 /// Return true if there is a value associated with key in the map.
1442 pub fn contains(self: *const Self, key: K) bool {1442 pub fn contains(self: Self, key: K) bool {
1443 if (@sizeOf(Context) != 0)1443 if (@sizeOf(Context) != 0)
1444 @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call containsContext instead.");1444 @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call containsContext instead.");
1445 return self.containsContext(key, undefined);1445 return self.containsContext(key, undefined);
1446 }1446 }
1447 pub fn containsContext(self: *const Self, key: K, ctx: Context) bool {1447 pub fn containsContext(self: Self, key: K, ctx: Context) bool {
1448 return self.containsAdapted(key, ctx);1448 return self.containsAdapted(key, ctx);
1449 }1449 }
1450 pub fn containsAdapted(self: *const Self, key: anytype, ctx: anytype) bool {1450 pub fn containsAdapted(self: Self, key: anytype, ctx: anytype) bool {
1451 return self.getIndex(key, ctx) != null;1451 return self.getIndex(key, ctx) != null;
1452 }1452 }
14531453
...@@ -1501,7 +1501,7 @@ pub fn HashMapUnmanaged(...@@ -1501,7 +1501,7 @@ pub fn HashMapUnmanaged(
15011501
1502 // This counts the number of occupied slots (not counting tombstones), which is1502 // This counts the number of occupied slots (not counting tombstones), which is
1503 // what has to stay under the max_load_percentage of capacity.1503 // what has to stay under the max_load_percentage of capacity.
1504 fn load(self: *const Self) Size {1504 fn load(self: Self) Size {
1505 const max_load = (self.capacity() * max_load_percentage) / 100;1505 const max_load = (self.capacity() * max_load_percentage) / 100;
1506 assert(max_load >= self.available);1506 assert(max_load >= self.available);
1507 return @as(Size, @truncate(max_load - self.available));1507 return @as(Size, @truncate(max_load - self.available));
...@@ -1603,19 +1603,19 @@ pub fn HashMapUnmanaged(...@@ -1603,19 +1603,19 @@ pub fn HashMapUnmanaged(
1603 const total_size = std.mem.alignForward(usize, vals_end, max_align);1603 const total_size = std.mem.alignForward(usize, vals_end, max_align);
16041604
1605 const slice = try allocator.alignedAlloc(u8, max_align, total_size);1605 const slice = try allocator.alignedAlloc(u8, max_align, total_size);
1606 const ptr = @intFromPtr(slice.ptr);1606 const ptr: [*]u8 = @ptrCast(slice.ptr);
16071607
1608 const metadata = ptr + @sizeOf(Header);1608 const metadata = ptr + @sizeOf(Header);
16091609
1610 const hdr = @as(*Header, @ptrFromInt(ptr));1610 const hdr = @as(*Header, @ptrCast(@alignCast(ptr)));
1611 if (@sizeOf([*]V) != 0) {1611 if (@sizeOf([*]V) != 0) {
1612 hdr.values = @as([*]V, @ptrFromInt(ptr + vals_start));1612 hdr.values = @ptrCast(@alignCast((ptr + vals_start)));
1613 }1613 }
1614 if (@sizeOf([*]K) != 0) {1614 if (@sizeOf([*]K) != 0) {
1615 hdr.keys = @as([*]K, @ptrFromInt(ptr + keys_start));1615 hdr.keys = @ptrCast(@alignCast((ptr + keys_start)));
1616 }1616 }
1617 hdr.capacity = new_capacity;1617 hdr.capacity = new_capacity;
1618 self.metadata = @as([*]Metadata, @ptrFromInt(metadata));1618 self.metadata = @ptrCast(@alignCast(metadata));
1619 }1619 }
16201620
1621 fn deallocate(self: *Self, allocator: Allocator) void {1621 fn deallocate(self: *Self, allocator: Allocator) void {
...@@ -1638,7 +1638,7 @@ pub fn HashMapUnmanaged(...@@ -1638,7 +1638,7 @@ pub fn HashMapUnmanaged(
16381638
1639 const total_size = std.mem.alignForward(usize, vals_end, max_align);1639 const total_size = std.mem.alignForward(usize, vals_end, max_align);
16401640
1641 const slice = @as([*]align(max_align) u8, @ptrFromInt(@intFromPtr(self.header())))[0..total_size];1641 const slice = @as([*]align(max_align) u8, @alignCast(@ptrCast(self.header())))[0..total_size];
1642 allocator.free(slice);1642 allocator.free(slice);
16431643
1644 self.metadata = null;1644 self.metadata = null;