tiny.simd.TypedBins
Defined in stats.
Source
Source: lib/simd/src/stats.zig:7
zig
pub fn TypedBins(comptime capacity: usize, comptime Count: type) type { comptime requireBins(capacity, Count); return struct { counts: [capacity]Count = @splat(0), const Self = @This(); pub const bin_count = capacity; pub const CountType = Count; pub fn notify(self: *Self, bin_value: anytype) void { const bin_index = index(bin_value); self.counts[bin_index] +%= 1; } pub fn incrementBy(self: *Self, bin_value: anytype, count: u32) void { const bin_index = index(bin_value); self.counts[bin_index] +%= countValue(count); } pub fn bin(self: *const Self, bin_index: usize) Count { std.debug.assert(bin_index < capacity); return self.counts[bin_index]; } pub fn resetBin(self: *Self, bin_index: usize) void { std.debug.assert(bin_index < capacity); self.counts[bin_index] = 0; } pub fn assimilate(self: *Self, other: *const Self) void { for (&self.counts, other.counts) |*count, other_count| { count.* +%= other_count; } } pub fn firstNonzero(self: *const Self) usize { for (self.counts, 0..) |count, bin_index| { if (count != 0) return bin_index; } return capacity; } pub fn lastNonzero(self: *const Self) usize { var bin_index = capacity; while (bin_index != 0) { bin_index -= 1; if (self.counts[bin_index] != 0) return bin_index; } return 0; } pub fn numNonzero(self: *const Self) usize { var count: usize = 0; for (self.counts) |bin_count_value| count += @intFromBool(bin_count_value != 0); return count; } pub fn modalBinIndex(self: *const Self) usize { var maximum: Count = 0; var maximum_index: usize = 0; for (self.counts, 0..) |count, bin_index| { if (count > maximum) { maximum = count; maximum_index = bin_index; } } return maximum_index; } pub fn write( self: *const Self, writer: *std.Io.Writer, caption: []const u8, skip_zero: bool, ) std.Io.Writer.Error!void { try writer.print("\n{s} [{d}, modal idx {d}]\n", .{ caption, capacity, self.modalBinIndex(), }); const first = self.firstNonzero(); if (first == capacity) return; const last = self.lastNonzero(); for (first..last + 1) |bin_index| { const count = self.counts[bin_index]; if (!skip_zero or count != 0) { try writer.print(" {d:3}: {d}\n", .{ bin_index, count }); } } } pub fn reset(self: *Self) void { @memset(&self.counts, 0); } fn index(bin_value: anytype) usize { const T = @TypeOf(bin_value); comptime switch (@typeInfo(T)) { .int, .comptime_int => {}, else => @compileError("bin index must be an integer"), }; switch (@typeInfo(T)) { .int => |info| if (info.signedness == .signed) { std.debug.assert(bin_value >= 0); }, .comptime_int => std.debug.assert(bin_value >= 0), else => unreachable, } const bin_index: usize = @intCast(bin_value); std.debug.assert(bin_index < capacity); return bin_index; } fn countValue(count: u32) Count { if (@bitSizeOf(Count) >= @bitSizeOf(u32)) return @intCast(count); return @truncate(count); } };}Source: lib/simd/src/root.zig:559
zig
pub const TypedBins = stats.TypedBins;Audit
| Definitions | 1 |
|---|---|
| Public names | 2 |
| Members | 0 |
| Version | 26.7.0 |
| Revision | daab053ee433 |