improve naming and logging

This commit is contained in:
Jeeves 2025-04-29 07:13:46 -06:00
parent bc3932d432
commit e2a3e73404

View file

@ -1,5 +1,9 @@
const std = @import("std"); const std = @import("std");
pub const std_options = std.Options{
.log_level = .info,
};
pub fn main() !void { pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit(); defer _ = gpa.deinit();
@ -79,7 +83,7 @@ pub const Circuit = struct {
allocator: std.mem.Allocator, allocator: std.mem.Allocator,
components: Components, components: Components,
process_order: ?ProcessOrder = null, update_order: ?UpdateOrder = null,
const Components = std.ArrayListUnmanaged(*Component); const Components = std.ArrayListUnmanaged(*Component);
const ComponentIndex = u32; const ComponentIndex = u32;
@ -92,7 +96,7 @@ pub const Circuit = struct {
} }
pub fn deinit(self: *Circuit) void { pub fn deinit(self: *Circuit) void {
if (self.process_order) |p| self.allocator.free(p); if (self.update_order) |p| self.allocator.free(p);
for (0..self.components.items.len) |i| self.components.items[i].deinit(self.allocator); for (0..self.components.items.len) |i| self.components.items[i].deinit(self.allocator);
self.components.deinit(self.allocator); self.components.deinit(self.allocator);
} }
@ -109,16 +113,16 @@ pub const Circuit = struct {
} }
pub fn tick(self: *Circuit) !void { pub fn tick(self: *Circuit) !void {
if (self.process_order == null) try self.updateProcessOrder(); if (self.update_order == null) try self.calculateUpdateOrder();
for (self.process_order.?) |component| { for (self.update_order.?) |component| {
std.log.debug("processing component: {}@{d}", .{ component, self.componentIndex(component).? }); std.log.debug("updating {}@{d}", .{ component, self.componentIndex(component).? });
component.process(); component.process();
} }
} }
fn updateProcessOrder(self: *Circuit) !void { fn calculateUpdateOrder(self: *Circuit) !void {
var process_order = std.ArrayList(*Component).init(self.allocator); var update_order = std.ArrayList(*Component).init(self.allocator);
errdefer process_order.deinit(); errdefer update_order.deinit();
const visited = try self.allocator.alloc(bool, self.components.items.len); const visited = try self.allocator.alloc(bool, self.components.items.len);
defer self.allocator.free(visited); defer self.allocator.free(visited);
@ -132,7 +136,7 @@ pub const Circuit = struct {
continue :source; continue :source;
const idx = self.componentIndex(component).?; const idx = self.componentIndex(component).?;
try process_order.append(component); try update_order.append(component);
visited[idx] = true; visited[idx] = true;
try source_components.append(idx); try source_components.append(idx);
} }
@ -163,7 +167,7 @@ pub const Circuit = struct {
} }
// component is ready, continue the DFS down that branch // component is ready, continue the DFS down that branch
try process_order.append(connection.component); try update_order.append(connection.component);
try stack.append(new_idx); try stack.append(new_idx);
visited[new_idx] = true; visited[new_idx] = true;
component_idx = new_idx; component_idx = new_idx;
@ -175,8 +179,8 @@ pub const Circuit = struct {
} }
} }
if (self.process_order) |p| self.allocator.free(p); if (self.update_order) |p| self.allocator.free(p);
self.process_order = try process_order.toOwnedSlice(); self.update_order = try update_order.toOwnedSlice();
} }
fn componentIndex(self: *Circuit, component: *Component) ?ComponentIndex { fn componentIndex(self: *Circuit, component: *Component) ?ComponentIndex {
@ -187,7 +191,7 @@ pub const Circuit = struct {
return null; return null;
} }
const ProcessOrder = []*Component; const UpdateOrder = []*Component;
}; };
var null_signal = Signal{}; var null_signal = Signal{};
@ -255,7 +259,7 @@ pub const Component = struct {
self.deinitFn(self, allocator); self.deinitFn(self, allocator);
} }
pub fn process(self: *Component) void { pub inline fn process(self: *Component) void {
self.processFn(self); self.processFn(self);
} }