1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
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 });
}
};
}
|