const std = @import("std"); pub fn Node(T: type) type { return struct { const Self = @This(); parent: ?*Self = null, state: T, pub fn init(state: T) Self { return Self { .state = state }; } 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; } 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 pop(self: *Self, allocator: std.mem.Allocator) struct { T, ?*Self } { const parent = self.parent; const state = self.state; allocator.destroy(self); return .{ state, parent }; } pub fn format( self: *const Self, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype, ) !void { _ = fmt; _ = options; try writer.print("Node {{ {} }}", .{ self.state }); } }; }