tiny.zen.diagram.category
Defined in diagram.
API (4)
Actions
Public operations.
Values and defaults
Public values and defaults.
Source
Source: lib/zen/src/diagram/category.zig
zig
const std = @import("std");const model = @import("model.zig");const root = @import("root.zig");const spec = root.spec;const Bounds = model.Bounds;pub const label_buffer_bytes: usize = 512;pub fn appendAssumeCapacity(bounds: *Bounds, label_value: []const u8) void { for (bounds.categories.items) |item| { if (std.mem.eql(u8, item, label_value)) return; } bounds.categories.appendAssumeCapacity(label_value);}pub fn label(buffer: *[label_buffer_bytes]u8, value: spec.XValue) []const u8 { return switch (value) { .number => |number| std.fmt.bufPrint(buffer, "{d:.3}", .{number}) catch unreachable, .text => |text| text, };}pub fn coordinate(label_value: []const u8, bounds: Bounds, left: f64, right: f64) f64 { const count = @max(bounds.categories.items.len, 1); const slot = (right - left) / @as(f64, @floatFromInt(count)); for (bounds.categories.items, 0..) |item, index| { if (std.mem.eql(u8, item, label_value)) return left + slot * (@as(f64, @floatFromInt(index)) + 0.5); } return left + slot * 0.5;}test "category labels fit every finite f64 without allocation" { var buffer: [label_buffer_bytes]u8 = undefined; try std.testing.expect(label(&buffer, .{ .number = std.math.floatMax(f64) }).len < buffer.len); try std.testing.expect(label(&buffer, .{ .number = -std.math.floatMax(f64) }).len < buffer.len); try std.testing.expectEqualStrings("borrowed", label(&buffer, .{ .text = @constCast("borrowed") }));}Source: lib/zen/src/diagram/root.zig:3
zig
pub const category = @import("category.zig");Audit
| Definitions | 5 |
|---|---|
| Public names | 5 |
| Members | 0 |
| Version | 26.7.0 |
| Revision | daab053ee433 |