blob: ba8542dbda75ddb02266bcdf7b497dfb8d172750 (
plain)
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
|
const std = @import("std");
pub fn Node(T: type) type {
return struct {
const Self = @This();
parent: ?*Node = 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 pop(self: *Self, allocator: std.mem.Allocator) struct { T, *Self } {
const parent = self.parent;
const state = self.state;
allocator.destroy(self);
return .{ state, parent };
}
};
}
|