aboutsummaryrefslogtreecommitdiff
path: root/src/gss.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/gss.zig')
-rw-r--r--src/gss.zig76
1 files changed, 56 insertions, 20 deletions
diff --git a/src/gss.zig b/src/gss.zig
index 8f81d80..ef5d221 100644
--- a/src/gss.zig
+++ b/src/gss.zig
@@ -4,34 +4,39 @@ pub fn Node(T: type) type {
return struct {
const Self = @This();
- parent: ?*Self = null,
+ parents: *std.ArrayList(*Self),
state: T,
+ is_toplevel: bool = false,
- pub fn init(state: T) Self {
- return Self { .state = state };
- }
+ pub fn init(state: T, allocator: std.mem.Allocator) !Self {
+ const parents = try allocator.create(std.ArrayList(*Self));
+ parents.* = std.ArrayList(*Self).init(allocator);
- pub fn push(self: *Self, state: T, allocator: std.mem.Allocator) !*Self {
- const node = try allocator.create(Self);
- node.parent = self;
- node.state = state;
- return node;
+ return Self {
+ .state = state,
+ .parents = parents,
+ };
}
- pub fn clone(self: *Self, state: T, allocator: std.mem.Allocator) !*Self {
- const node = try allocator.create(Self);
- node.parent = self.parent;
- node.state = state;
- return node;
+ pub fn clone(self: *Self, state: T) !Self {
+ return Self {
+ .parents = self.parents,
+ .state = state,
+ .is_toplevel = self.is_toplevel,
+ };
}
- pub fn pop(self: *Self, allocator: std.mem.Allocator) struct { T, ?*Self } {
- const parent = self.parent;
- const state = self.state;
+ pub fn push(self: *Self, state: T) !Self {
+ const parents = try self.parents.allocator.create(std.ArrayList(*Self));
+ parents.* = std.ArrayList(*Self).init(self.parents.allocator);
+ var other = Self {
+ .parents = parents,
+ .state = state,
+ };
- allocator.destroy(self);
+ try other.parents.append(self);
- return .{ state, parent };
+ return other;
}
pub fn format(
@@ -43,7 +48,38 @@ pub fn Node(T: type) type {
_ = fmt;
_ = options;
- try writer.print("Node {{ {} }}", .{ self.state });
+ try writer.print("Node {{ {} }}", .{self.state});
+ }
+ };
+}
+
+pub fn Graph(T: type) type {
+ return struct {
+ const Self = @This();
+
+ allocator: std.mem.Allocator,
+
+ pub fn init(allocator: std.mem.Allocator) Self {
+ return Self { .allocator = allocator };
+ }
+
+ pub fn push(self: *Self, node: *Node(T), state: T) !*Node(T) {
+ const child = try self.allocator.create(Node(T));
+ child.* = try node.push(state);
+ return child;
+ }
+
+ pub fn add_toplevel(self: *Self, state: T) !*Node(T) {
+ const node = try self.allocator.create(Node(T));
+ node.* = try Node(T).init(state, self.allocator);
+ node.is_toplevel = true;
+ return node;
+ }
+
+ pub fn clone(self: *Self, node: *Node(T), state: T) !*Node(T) {
+ const sibling = try self.allocator.create(Node(T));
+ sibling.* = try node.clone(state);
+ return sibling;
}
};
}