small stylistic changes

This commit is contained in:
Mitchell Hashimoto
2024-06-23 09:44:54 -07:00
parent 3b36dbb53e
commit eebc7d4c3a
4 changed files with 41 additions and 63 deletions

View File

@ -46,7 +46,7 @@ pub fn CacheTable(
comptime bucket_size: u8, comptime bucket_size: u8,
) type { ) type {
return struct { return struct {
const Self = CacheTable(K, V, Context, bucket_count, bucket_size); const Self = @This();
const KV = struct { const KV = struct {
key: K, key: K,

View File

@ -197,7 +197,7 @@ pub const Page = struct {
.styles = style.Set.init( .styles = style.Set.init(
buf.add(l.styles_start), buf.add(l.styles_start),
l.styles_layout, l.styles_layout,
style.StyleSetContext{}, .{},
), ),
.grapheme_alloc = GraphemeAlloc.init( .grapheme_alloc = GraphemeAlloc.init(
buf.add(l.grapheme_alloc_start), buf.add(l.grapheme_alloc_start),

View File

@ -1,12 +1,12 @@
const std = @import("std");
const assert = std.debug.assert;
const size = @import("size.zig"); const size = @import("size.zig");
const Offset = size.Offset; const Offset = size.Offset;
const OffsetBuf = size.OffsetBuf; const OffsetBuf = size.OffsetBuf;
const fastmem = @import("../fastmem.zig"); const fastmem = @import("../fastmem.zig");
const std = @import("std");
const assert = std.debug.assert;
/// A reference counted set. /// A reference counted set.
/// ///
/// This set is created with some capacity in mind. You can determine /// This set is created with some capacity in mind. You can determine
@ -40,19 +40,18 @@ const assert = std.debug.assert;
/// A type containing methods to define behaviors. /// A type containing methods to define behaviors.
/// - `fn hash(*Context, T) u64` - Return a hash for an item. /// - `fn hash(*Context, T) u64` - Return a hash for an item.
/// - `fn eql(*Context, T, T) bool` - Check two items for equality. /// - `fn eql(*Context, T, T) bool` - Check two items for equality.
///
/// - `fn deleted(*Context, T) void` - [OPTIONAL] Deletion callback. /// - `fn deleted(*Context, T) void` - [OPTIONAL] Deletion callback.
/// If present, called whenever an item is finally deleted. /// If present, called whenever an item is finally deleted.
/// Useful if the item has memory that needs to be freed. /// Useful if the item has memory that needs to be freed.
/// ///
pub fn RefCountedSet( pub fn RefCountedSet(
comptime T: type, comptime T: type,
comptime Id: type, comptime IdT: type,
comptime RefCountInt: type, comptime RefCountInt: type,
comptime Context: type, comptime ContextT: type,
) type { ) type {
return struct { return struct {
const Self = RefCountedSet(T, Id, RefCountInt, Context); const Self = @This();
pub const base_align = @max( pub const base_align = @max(
@alignOf(Context), @alignOf(Context),
@ -65,6 +64,7 @@ pub fn RefCountedSet(
pub const Item = struct { pub const Item = struct {
/// The value this item represents. /// The value this item represents.
value: T = undefined, value: T = undefined,
/// Metadata for this item. /// Metadata for this item.
meta: Metadata = .{}, meta: Metadata = .{},
@ -83,6 +83,10 @@ pub fn RefCountedSet(
}; };
}; };
// Re-export these types so they can be referenced by the caller.
pub const Id = IdT;
pub const Context = ContextT;
/// A hash table of item indices /// A hash table of item indices
table: Offset(Id), table: Offset(Id),
@ -178,47 +182,37 @@ pub fn RefCountedSet(
}; };
} }
/// Add an item to the set if not present /// Add an item to the set if not present and increment its ref count.
/// and increment its reference count.
/// ///
/// Returns the item's ID. /// Returns the item's ID.
/// ///
/// If the set has no more room, then an /// If the set has no more room, then an OutOfMemory error is returned.
/// OutOfMemory error is returned instead.
pub fn add(self: *Self, base: anytype, value: T) error{OutOfMemory}!Id { pub fn add(self: *Self, base: anytype, value: T) error{OutOfMemory}!Id {
const items = self.items.ptr(base); const items = self.items.ptr(base);
// Trim dead items from the end of the list. // Trim dead items from the end of the list.
while (self.next_id > 1 and items[self.next_id - 1].meta.ref == 0) { while (self.next_id > 1 and items[self.next_id - 1].meta.ref == 0) {
self.next_id -= 1; self.next_id -= 1;
self.deleteItem(base, self.next_id); self.deleteItem(base, self.next_id);
} }
// If we still don't have an available ID, we're out of memory. // If we still don't have an available ID, we're out of memory.
if (self.next_id >= self.layout.cap) { if (self.next_id >= self.layout.cap) return error.OutOfMemory;
return error.OutOfMemory;
}
const id = self.upsert(base, value, self.next_id); const id = self.upsert(base, value, self.next_id);
items[id].meta.ref += 1; items[id].meta.ref += 1;
if (id == self.next_id) { if (id == self.next_id) self.next_id += 1;
self.next_id += 1;
}
return id; return id;
} }
/// Add an item to the set if not present /// Add an item to the set if not present and increment its
/// and increment its reference count. /// ref count. If possible, use the provided ID.
/// If possible, use the provided ID.
/// ///
/// Returns the item's ID, or null /// Returns the item's ID, or null if the provided ID was used.
/// if the provided ID was used.
/// ///
/// If the set has no more room, then an /// If the set has no more room, then an OutOfMemory error is returned.
/// OutOfMemory error is returned instead.
pub fn addWithId(self: *Self, base: anytype, value: T, id: Id) error{OutOfMemory}!?Id { pub fn addWithId(self: *Self, base: anytype, value: T, id: Id) error{OutOfMemory}!?Id {
const items = self.items.ptr(base); const items = self.items.ptr(base);
@ -371,11 +365,6 @@ pub fn RefCountedSet(
return tb_ct; return tb_ct;
} }
//================================================//
// The functions below are all internal functions //
// for performing operations on the hash table. //
//================================================//
/// Delete an item, removing any references from /// Delete an item, removing any references from
/// the table, and freeing its ID to be re-used. /// the table, and freeing its ID to be re-used.
fn deleteItem(self: *Self, base: anytype, id: Id) void { fn deleteItem(self: *Self, base: anytype, id: Id) void {
@ -384,13 +373,9 @@ pub fn RefCountedSet(
const item = items[id]; const item = items[id];
if (item.meta.bucket > self.layout.table_cap) { if (item.meta.bucket > self.layout.table_cap) return;
return;
}
if (table[item.meta.bucket] != id) { if (table[item.meta.bucket] != id) return;
return;
}
if (comptime @hasDecl(Context, "deleted")) { if (comptime @hasDecl(Context, "deleted")) {
// Inform the context struct that we're // Inform the context struct that we're
@ -471,12 +456,11 @@ pub fn RefCountedSet(
/// Find the provided value in the hash table, or add a new item /// Find the provided value in the hash table, or add a new item
/// for it if not present. If a new item is added, `new_id` will /// for it if not present. If a new item is added, `new_id` will
/// be used as the ID. /// be used as the ID. If an existing item is found, the `new_id`
/// is ignored and the existing item's ID is returned.
fn upsert(self: *Self, base: anytype, value: T, new_id: Id) Id { fn upsert(self: *Self, base: anytype, value: T, new_id: Id) Id {
// If the item already exists, return it. // If the item already exists, return it.
if (self.lookup(base, value)) |id| { if (self.lookup(base, value)) |id| return id;
return id;
}
const table = self.table.ptr(base); const table = self.table.ptr(base);
const items = self.items.ptr(base); const items = self.items.ptr(base);
@ -484,10 +468,7 @@ pub fn RefCountedSet(
// The new item that we'll put in to the table. // The new item that we'll put in to the table.
var new_item: Item = .{ var new_item: Item = .{
.value = value, .value = value,
.meta = .{ .meta = .{ .psl = 0, .ref = 0 },
.psl = 0,
.ref = 0,
},
}; };
const hash: u64 = self.context.hash(value); const hash: u64 = self.context.hash(value);
@ -508,7 +489,6 @@ pub fn RefCountedSet(
held_item.meta.bucket = p; held_item.meta.bucket = p;
self.psl_stats[held_item.meta.psl] += 1; self.psl_stats[held_item.meta.psl] += 1;
self.max_psl = @max(self.max_psl, held_item.meta.psl); self.max_psl = @max(self.max_psl, held_item.meta.psl);
break; break;
} }

View File

@ -62,11 +62,11 @@ pub const Style = struct {
_ = try writer.write("Color.none"); _ = try writer.write("Color.none");
}, },
.palette => |p| { .palette => |p| {
_ = try writer.print("Color.palette{{ {} }}", .{ p }); _ = try writer.print("Color.palette{{ {} }}", .{p});
}, },
.rgb => |rgb| { .rgb => |rgb| {
_ = try writer.print("Color.rgb{{ {}, {}, {} }}", .{ rgb.r, rgb.g, rgb.b }); _ = try writer.print("Color.rgb{{ {}, {}, {} }}", .{ rgb.r, rgb.g, rgb.b });
} },
} }
} }
}; };
@ -243,23 +243,21 @@ pub const Style = struct {
} }
}; };
pub const StyleSetContext = struct {
pub fn hash(self: *StyleSetContext, style: Style) u64 {
_ = self;
return style.hash();
}
pub fn eql(self: *StyleSetContext, a: Style, b: Style) bool {
_ = self;
return a.eql(b);
}
};
pub const Set = RefCountedSet( pub const Set = RefCountedSet(
Style, Style,
Id, Id,
size.CellCountInt, size.CellCountInt,
StyleSetContext, struct {
pub fn hash(self: *const @This(), style: Style) u64 {
_ = self;
return style.hash();
}
pub fn eql(self: *const @This(), a: Style, b: Style) bool {
_ = self;
return a.eql(b);
}
},
); );
test "Set basic usage" { test "Set basic usage" {
@ -272,7 +270,7 @@ test "Set basic usage" {
const style: Style = .{ .flags = .{ .bold = true } }; const style: Style = .{ .flags = .{ .bold = true } };
const style2: Style = .{ .flags = .{ .italic = true } }; const style2: Style = .{ .flags = .{ .italic = true } };
var set = Set.init(OffsetBuf.init(buf), layout, StyleSetContext{}); var set = Set.init(OffsetBuf.init(buf), layout, .{});
// Add style // Add style
const id = try set.add(buf, style); const id = try set.add(buf, style);