improve naming and logging
This commit is contained in:
parent
bc3932d432
commit
e2a3e73404
1 changed files with 18 additions and 14 deletions
32
src/main.zig
32
src/main.zig
|
@ -1,5 +1,9 @@
|
|||
const std = @import("std");
|
||||
|
||||
pub const std_options = std.Options{
|
||||
.log_level = .info,
|
||||
};
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
defer _ = gpa.deinit();
|
||||
|
@ -79,7 +83,7 @@ pub const Circuit = struct {
|
|||
allocator: std.mem.Allocator,
|
||||
components: Components,
|
||||
|
||||
process_order: ?ProcessOrder = null,
|
||||
update_order: ?UpdateOrder = null,
|
||||
|
||||
const Components = std.ArrayListUnmanaged(*Component);
|
||||
const ComponentIndex = u32;
|
||||
|
@ -92,7 +96,7 @@ pub const Circuit = struct {
|
|||
}
|
||||
|
||||
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);
|
||||
self.components.deinit(self.allocator);
|
||||
}
|
||||
|
@ -109,16 +113,16 @@ pub const Circuit = struct {
|
|||
}
|
||||
|
||||
pub fn tick(self: *Circuit) !void {
|
||||
if (self.process_order == null) try self.updateProcessOrder();
|
||||
for (self.process_order.?) |component| {
|
||||
std.log.debug("processing component: {}@{d}", .{ component, self.componentIndex(component).? });
|
||||
if (self.update_order == null) try self.calculateUpdateOrder();
|
||||
for (self.update_order.?) |component| {
|
||||
std.log.debug("updating {}@{d}", .{ component, self.componentIndex(component).? });
|
||||
component.process();
|
||||
}
|
||||
}
|
||||
|
||||
fn updateProcessOrder(self: *Circuit) !void {
|
||||
var process_order = std.ArrayList(*Component).init(self.allocator);
|
||||
errdefer process_order.deinit();
|
||||
fn calculateUpdateOrder(self: *Circuit) !void {
|
||||
var update_order = std.ArrayList(*Component).init(self.allocator);
|
||||
errdefer update_order.deinit();
|
||||
|
||||
const visited = try self.allocator.alloc(bool, self.components.items.len);
|
||||
defer self.allocator.free(visited);
|
||||
|
@ -132,7 +136,7 @@ pub const Circuit = struct {
|
|||
continue :source;
|
||||
|
||||
const idx = self.componentIndex(component).?;
|
||||
try process_order.append(component);
|
||||
try update_order.append(component);
|
||||
visited[idx] = true;
|
||||
try source_components.append(idx);
|
||||
}
|
||||
|
@ -163,7 +167,7 @@ pub const Circuit = struct {
|
|||
}
|
||||
|
||||
// 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);
|
||||
visited[new_idx] = true;
|
||||
component_idx = new_idx;
|
||||
|
@ -175,8 +179,8 @@ pub const Circuit = struct {
|
|||
}
|
||||
}
|
||||
|
||||
if (self.process_order) |p| self.allocator.free(p);
|
||||
self.process_order = try process_order.toOwnedSlice();
|
||||
if (self.update_order) |p| self.allocator.free(p);
|
||||
self.update_order = try update_order.toOwnedSlice();
|
||||
}
|
||||
|
||||
fn componentIndex(self: *Circuit, component: *Component) ?ComponentIndex {
|
||||
|
@ -187,7 +191,7 @@ pub const Circuit = struct {
|
|||
return null;
|
||||
}
|
||||
|
||||
const ProcessOrder = []*Component;
|
||||
const UpdateOrder = []*Component;
|
||||
};
|
||||
|
||||
var null_signal = Signal{};
|
||||
|
@ -255,7 +259,7 @@ pub const Component = struct {
|
|||
self.deinitFn(self, allocator);
|
||||
}
|
||||
|
||||
pub fn process(self: *Component) void {
|
||||
pub inline fn process(self: *Component) void {
|
||||
self.processFn(self);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue