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(
471471
472472 /// Create an iterator over the keys in the map.
473473 /// The iterator is invalidated if the map is modified.
474 pub fn keyIterator(self: *const Self) KeyIterator {
474 pub fn keyIterator(self: Self) KeyIterator {
475475 return self.unmanaged.keyIterator();
476476 }
477477
478478 /// Create an iterator over the values in the map.
479479 /// The iterator is invalidated if the map is modified.
480 pub fn valueIterator(self: *const Self) ValueIterator {
480 pub fn valueIterator(self: Self) ValueIterator {
481481 return self.unmanaged.valueIterator();
482482 }
483483
......@@ -542,7 +542,7 @@ pub fn HashMap(
542542
543543 /// Returns the number of total elements which may be present before it is
544544 /// no longer guaranteed that no allocations will be performed.
545 pub fn capacity(self: *Self) Size {
545 pub fn capacity(self: Self) Size {
546546 return self.unmanaged.capacity();
547547 }
548548
......@@ -977,23 +977,23 @@ pub fn HashMapUnmanaged(
977977 self.available = 0;
978978 }
979979
980 pub fn count(self: *const Self) Size {
980 pub fn count(self: Self) Size {
981981 return self.size;
982982 }
983983
984 fn header(self: *const Self) *Header {
984 fn header(self: Self) *Header {
985985 return @ptrCast(@as([*]Header, @ptrCast(@alignCast(self.metadata.?))) - 1);
986986 }
987987
988 fn keys(self: *const Self) [*]K {
988 fn keys(self: Self) [*]K {
989989 return self.header().keys;
990990 }
991991
992 fn values(self: *const Self) [*]V {
992 fn values(self: Self) [*]V {
993993 return self.header().values;
994994 }
995995
996 pub fn capacity(self: *const Self) Size {
996 pub fn capacity(self: Self) Size {
997997 if (self.metadata == null) return 0;
998998
999999 return self.header().capacity;
......@@ -1003,7 +1003,7 @@ pub fn HashMapUnmanaged(
10031003 return .{ .hm = self };
10041004 }
10051005
1006 pub fn keyIterator(self: *const Self) KeyIterator {
1006 pub fn keyIterator(self: Self) KeyIterator {
10071007 if (self.metadata) |metadata| {
10081008 return .{
10091009 .len = self.capacity(),
......@@ -1019,7 +1019,7 @@ pub fn HashMapUnmanaged(
10191019 }
10201020 }
10211021
1022 pub fn valueIterator(self: *const Self) ValueIterator {
1022 pub fn valueIterator(self: Self) ValueIterator {
10231023 if (self.metadata) |metadata| {
10241024 return .{
10251025 .len = self.capacity(),
......@@ -1439,15 +1439,15 @@ pub fn HashMapUnmanaged(
14391439 }
14401440
14411441 /// 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 {
14431443 if (@sizeOf(Context) != 0)
14441444 @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call containsContext instead.");
14451445 return self.containsContext(key, undefined);
14461446 }
1447 pub fn containsContext(self: *const Self, key: K, ctx: Context) bool {
1447 pub fn containsContext(self: Self, key: K, ctx: Context) bool {
14481448 return self.containsAdapted(key, ctx);
14491449 }
1450 pub fn containsAdapted(self: *const Self, key: anytype, ctx: anytype) bool {
1450 pub fn containsAdapted(self: Self, key: anytype, ctx: anytype) bool {
14511451 return self.getIndex(key, ctx) != null;
14521452 }
14531453
......@@ -1501,7 +1501,7 @@ pub fn HashMapUnmanaged(
15011501
15021502 // This counts the number of occupied slots (not counting tombstones), which is
15031503 // what has to stay under the max_load_percentage of capacity.
1504 fn load(self: *const Self) Size {
1504 fn load(self: Self) Size {
15051505 const max_load = (self.capacity() * max_load_percentage) / 100;
15061506 assert(max_load >= self.available);
15071507 return @as(Size, @truncate(max_load - self.available));
......@@ -1603,19 +1603,19 @@ pub fn HashMapUnmanaged(
16031603 const total_size = std.mem.alignForward(usize, vals_end, max_align);
16041604
16051605 const slice = try allocator.alignedAlloc(u8, max_align, total_size);
1606 const ptr = @intFromPtr(slice.ptr);
1606 const ptr: [*]u8 = @ptrCast(slice.ptr);
16071607
16081608 const metadata = ptr + @sizeOf(Header);
16091609
1610 const hdr = @as(*Header, @ptrFromInt(ptr));
1610 const hdr = @as(*Header, @ptrCast(@alignCast(ptr)));
16111611 if (@sizeOf([*]V) != 0) {
1612 hdr.values = @as([*]V, @ptrFromInt(ptr + vals_start));
1612 hdr.values = @ptrCast(@alignCast((ptr + vals_start)));
16131613 }
16141614 if (@sizeOf([*]K) != 0) {
1615 hdr.keys = @as([*]K, @ptrFromInt(ptr + keys_start));
1615 hdr.keys = @ptrCast(@alignCast((ptr + keys_start)));
16161616 }
16171617 hdr.capacity = new_capacity;
1618 self.metadata = @as([*]Metadata, @ptrFromInt(metadata));
1618 self.metadata = @ptrCast(@alignCast(metadata));
16191619 }
16201620
16211621 fn deallocate(self: *Self, allocator: Allocator) void {
......@@ -1638,7 +1638,7 @@ pub fn HashMapUnmanaged(
16381638
16391639 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];
16421642 allocator.free(slice);
16431643
16441644 self.metadata = null;