aboutsummaryrefslogtreecommitdiff
path: root/lib/std/array_hash_map.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2023-06-24 16:58:19 -0700
committerGitHub <noreply@github.com>2023-06-24 16:58:19 -0700
commit146b79af153bbd5dafda0ba12a040385c7fc58f8 (patch)
tree67e3db8b444d65c667e314770fc983a7fc8ba293 /lib/std/array_hash_map.zig
parent13853bef0df3c90633021850cc6d6abaeea03282 (diff)
parent21ac0beb436f49fe49c6982a872f2dc48e4bea5e (diff)
downloadzig-146b79af153bbd5dafda0ba12a040385c7fc58f8.tar.gz
zig-146b79af153bbd5dafda0ba12a040385c7fc58f8.zip
Merge pull request #16163 from mlugg/feat/builtins-infer-dest-ty
Infer destination type of cast builtins using result type
Diffstat (limited to 'lib/std/array_hash_map.zig')
-rw-r--r--lib/std/array_hash_map.zig46
1 files changed, 23 insertions, 23 deletions
diff --git a/lib/std/array_hash_map.zig b/lib/std/array_hash_map.zig
index d3ad94324e..df4c95cbca 100644
--- a/lib/std/array_hash_map.zig
+++ b/lib/std/array_hash_map.zig
@@ -49,7 +49,7 @@ pub fn eqlString(a: []const u8, b: []const u8) bool {
}
pub fn hashString(s: []const u8) u32 {
- return @truncate(u32, std.hash.Wyhash.hash(0, s));
+ return @as(u32, @truncate(std.hash.Wyhash.hash(0, s)));
}
/// Insertion order is preserved.
@@ -617,7 +617,7 @@ pub fn ArrayHashMapUnmanaged(
return .{
.keys = slice.items(.key).ptr,
.values = slice.items(.value).ptr,
- .len = @intCast(u32, slice.len),
+ .len = @as(u32, @intCast(slice.len)),
};
}
pub const Iterator = struct {
@@ -1409,7 +1409,7 @@ pub fn ArrayHashMapUnmanaged(
indexes: []Index(I),
) void {
const slot = self.getSlotByIndex(old_entry_index, ctx, header, I, indexes);
- indexes[slot].entry_index = @intCast(I, new_entry_index);
+ indexes[slot].entry_index = @as(I, @intCast(new_entry_index));
}
fn removeFromIndexByIndex(self: *Self, entry_index: usize, ctx: ByIndexContext, header: *IndexHeader) void {
@@ -1508,7 +1508,7 @@ pub fn ArrayHashMapUnmanaged(
const new_index = self.entries.addOneAssumeCapacity();
indexes[slot] = .{
.distance_from_start_index = distance_from_start_index,
- .entry_index = @intCast(I, new_index),
+ .entry_index = @as(I, @intCast(new_index)),
};
// update the hash if applicable
@@ -1549,7 +1549,7 @@ pub fn ArrayHashMapUnmanaged(
const new_index = self.entries.addOneAssumeCapacity();
if (store_hash) hashes_array.ptr[new_index] = h;
indexes[slot] = .{
- .entry_index = @intCast(I, new_index),
+ .entry_index = @as(I, @intCast(new_index)),
.distance_from_start_index = distance_from_start_index,
};
distance_from_start_index = slot_data.distance_from_start_index;
@@ -1639,7 +1639,7 @@ pub fn ArrayHashMapUnmanaged(
const start_index = safeTruncate(usize, h);
const end_index = start_index +% indexes.len;
var index = start_index;
- var entry_index = @intCast(I, i);
+ var entry_index = @as(I, @intCast(i));
var distance_from_start_index: I = 0;
while (index != end_index) : ({
index +%= 1;
@@ -1776,7 +1776,7 @@ fn capacityIndexSize(bit_index: u8) usize {
fn safeTruncate(comptime T: type, val: anytype) T {
if (@bitSizeOf(T) >= @bitSizeOf(@TypeOf(val)))
return val;
- return @truncate(T, val);
+ return @as(T, @truncate(val));
}
/// A single entry in the lookup acceleration structure. These structs
@@ -1852,13 +1852,13 @@ const IndexHeader = struct {
fn constrainIndex(header: IndexHeader, i: usize) usize {
// This is an optimization for modulo of power of two integers;
// it requires `indexes_len` to always be a power of two.
- return @intCast(usize, i & header.mask());
+ return @as(usize, @intCast(i & header.mask()));
}
/// Returns the attached array of indexes. I must match the type
/// returned by capacityIndexType.
fn indexes(header: *IndexHeader, comptime I: type) []Index(I) {
- const start_ptr = @ptrCast([*]Index(I), @ptrCast([*]u8, header) + @sizeOf(IndexHeader));
+ const start_ptr: [*]Index(I) = @alignCast(@ptrCast(@as([*]u8, @ptrCast(header)) + @sizeOf(IndexHeader)));
return start_ptr[0..header.length()];
}
@@ -1871,15 +1871,15 @@ const IndexHeader = struct {
return index_capacities[self.bit_index];
}
fn length(self: IndexHeader) usize {
- return @as(usize, 1) << @intCast(math.Log2Int(usize), self.bit_index);
+ return @as(usize, 1) << @as(math.Log2Int(usize), @intCast(self.bit_index));
}
fn mask(self: IndexHeader) u32 {
- return @intCast(u32, self.length() - 1);
+ return @as(u32, @intCast(self.length() - 1));
}
fn findBitIndex(desired_capacity: usize) !u8 {
if (desired_capacity > max_capacity) return error.OutOfMemory;
- var new_bit_index = @intCast(u8, std.math.log2_int_ceil(usize, desired_capacity));
+ var new_bit_index = @as(u8, @intCast(std.math.log2_int_ceil(usize, desired_capacity)));
if (desired_capacity > index_capacities[new_bit_index]) new_bit_index += 1;
if (new_bit_index < min_bit_index) new_bit_index = min_bit_index;
assert(desired_capacity <= index_capacities[new_bit_index]);
@@ -1889,12 +1889,12 @@ const IndexHeader = struct {
/// Allocates an index header, and fills the entryIndexes array with empty.
/// The distance array contents are undefined.
fn alloc(allocator: Allocator, new_bit_index: u8) !*IndexHeader {
- const len = @as(usize, 1) << @intCast(math.Log2Int(usize), new_bit_index);
+ const len = @as(usize, 1) << @as(math.Log2Int(usize), @intCast(new_bit_index));
const index_size = hash_map.capacityIndexSize(new_bit_index);
const nbytes = @sizeOf(IndexHeader) + index_size * len;
const bytes = try allocator.alignedAlloc(u8, @alignOf(IndexHeader), nbytes);
@memset(bytes[@sizeOf(IndexHeader)..], 0xff);
- const result = @ptrCast(*IndexHeader, bytes.ptr);
+ const result: *IndexHeader = @alignCast(@ptrCast(bytes.ptr));
result.* = .{
.bit_index = new_bit_index,
};
@@ -1904,7 +1904,7 @@ const IndexHeader = struct {
/// Releases the memory for a header and its associated arrays.
fn free(header: *IndexHeader, allocator: Allocator) void {
const index_size = hash_map.capacityIndexSize(header.bit_index);
- const ptr = @ptrCast([*]align(@alignOf(IndexHeader)) u8, header);
+ const ptr: [*]align(@alignOf(IndexHeader)) u8 = @ptrCast(header);
const slice = ptr[0 .. @sizeOf(IndexHeader) + header.length() * index_size];
allocator.free(slice);
}
@@ -1912,7 +1912,7 @@ const IndexHeader = struct {
/// Puts an IndexHeader into the state that it would be in after being freshly allocated.
fn reset(header: *IndexHeader) void {
const index_size = hash_map.capacityIndexSize(header.bit_index);
- const ptr = @ptrCast([*]align(@alignOf(IndexHeader)) u8, header);
+ const ptr: [*]align(@alignOf(IndexHeader)) u8 = @ptrCast(header);
const nbytes = @sizeOf(IndexHeader) + header.length() * index_size;
@memset(ptr[@sizeOf(IndexHeader)..nbytes], 0xff);
}
@@ -2020,25 +2020,25 @@ test "iterator hash map" {
var count: usize = 0;
while (it.next()) |entry| : (count += 1) {
- buffer[@intCast(usize, entry.key_ptr.*)] = entry.value_ptr.*;
+ buffer[@as(usize, @intCast(entry.key_ptr.*))] = entry.value_ptr.*;
}
try testing.expect(count == 3);
try testing.expect(it.next() == null);
for (buffer, 0..) |_, i| {
- try testing.expect(buffer[@intCast(usize, keys[i])] == values[i]);
+ try testing.expect(buffer[@as(usize, @intCast(keys[i]))] == values[i]);
}
it.reset();
count = 0;
while (it.next()) |entry| {
- buffer[@intCast(usize, entry.key_ptr.*)] = entry.value_ptr.*;
+ buffer[@as(usize, @intCast(entry.key_ptr.*))] = entry.value_ptr.*;
count += 1;
if (count >= 2) break;
}
for (buffer[0..2], 0..) |_, i| {
- try testing.expect(buffer[@intCast(usize, keys[i])] == values[i]);
+ try testing.expect(buffer[@as(usize, @intCast(keys[i]))] == values[i]);
}
it.reset();
@@ -2336,11 +2336,11 @@ pub fn getAutoHashFn(comptime K: type, comptime Context: type) (fn (Context, K)
fn hash(ctx: Context, key: K) u32 {
_ = ctx;
if (comptime trait.hasUniqueRepresentation(K)) {
- return @truncate(u32, Wyhash.hash(0, std.mem.asBytes(&key)));
+ return @as(u32, @truncate(Wyhash.hash(0, std.mem.asBytes(&key))));
} else {
var hasher = Wyhash.init(0);
autoHash(&hasher, key);
- return @truncate(u32, hasher.final());
+ return @as(u32, @truncate(hasher.final()));
}
}
}.hash;
@@ -2380,7 +2380,7 @@ pub fn getAutoHashStratFn(comptime K: type, comptime Context: type, comptime str
_ = ctx;
var hasher = Wyhash.init(0);
std.hash.autoHashStrat(&hasher, key, strategy);
- return @truncate(u32, hasher.final());
+ return @as(u32, @truncate(hasher.final()));
}
}.hash;
}