-
Notifications
You must be signed in to change notification settings - Fork 16
/
intrusive.zig
179 lines (151 loc) · 5.35 KB
/
intrusive.zig
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const std = @import("std");
const meta = std.meta;
/// A singly-linked list. Keeps track of one head pointer.
pub fn SinglyLinkedList(comptime T: type, comptime next_field: meta.FieldEnum(T)) type {
const next = meta.fieldInfo(T, next_field).name;
return struct {
const Self = @This();
head: ?*T = null,
pub fn isEmpty(self: *const Self) bool {
return self.head == null;
}
pub fn prepend(self: *Self, value: *T) void {
if (!self.isEmpty() and self.head == value) return;
@field(value, next) = self.head;
self.head = value;
}
pub fn popFirst(self: *Self) ?*T {
const head = self.head orelse return null;
self.head = @field(head, next);
@field(head, next) = null;
return head;
}
};
}
/// A double-ended doubly-linked list (doubly-linked deque). Keeps track of two pointers: one head pointer, and one tail pointer.
pub fn DoublyLinkedDeque(comptime T: type, comptime next_field: anytype, comptime prev_field: anytype) type {
const next = meta.fieldInfo(T, next_field).name;
const prev = meta.fieldInfo(T, prev_field).name;
return struct {
const Self = @This();
head: ?*T = null,
tail: ?*T = null,
pub fn isEmpty(self: *const Self) bool {
return self.head == null;
}
pub fn prepend(self: *Self, value: *T) void {
if (self.head) |head| {
if (head == value) return;
@field(head, prev) = value;
} else {
self.tail = value;
}
@field(value, prev) = null;
@field(value, next) = self.head;
self.head = value;
}
pub fn append(self: *Self, value: *T) void {
if (self.tail) |tail| {
if (tail == value) return;
@field(tail, next) = value;
} else {
self.head = value;
}
@field(value, prev) = self.tail;
@field(value, next) = null;
self.tail = value;
}
pub fn concat(self: *Self, other: Self) void {
if (self.tail) |tail| {
@field(tail, next) = other.head;
if (other.head) |other_head| {
@field(other_head, prev) = self.tail;
}
} else {
self.head = other.head;
}
self.tail = other.tail;
}
pub fn popFirst(self: *Self) ?*T {
const head = self.head orelse return null;
if (@field(head, next)) |next_value| {
@field(next_value, prev) = null;
} else {
self.tail = null;
}
self.head = @field(head, next);
@field(head, next) = null;
@field(head, prev) = null;
return head;
}
pub fn pop(self: *Self) ?*T {
const tail = self.tail orelse return null;
if (@field(tail, prev)) |prev_value| {
@field(prev_value, next) = null;
} else {
self.head = null;
}
self.tail = @field(tail, prev);
@field(tail, next) = null;
@field(tail, prev) = null;
return tail;
}
pub fn remove(self: *Self, value: *T) bool {
if (self.head == null) {
return false;
}
if (self.head != value and @field(value, next) == null and @field(value, prev) == null) {
return false;
}
if (@field(value, next)) |next_value| {
@field(next_value, prev) = @field(value, prev);
} else {
self.tail = @field(value, prev);
}
if (@field(value, prev)) |prev_value| {
@field(prev_value, next) = @field(value, next);
} else {
self.head = @field(value, next);
}
@field(value, next) = null;
@field(value, prev) = null;
return true;
}
};
}
/// A double-ended singly-linked list (singly-linked deque). Keeps track of two pointers: one head pointer, and one tail pointer.
pub fn SinglyLinkedDeque(comptime T: type, comptime next_field: meta.FieldEnum(T)) type {
const next = meta.fieldInfo(T, next_field).name;
return struct {
const Self = @This();
head: ?*T = null,
tail: ?*T = null,
pub fn isEmpty(self: *const Self) bool {
return self.head == null;
}
pub fn prepend(self: *Self, value: *T) void {
if (self.head) |head| {
if (head == value) return;
} else {
self.tail = value;
}
@field(value, next_field) = self.head;
self.head = value;
}
pub fn append(self: *Self, value: *T) void {
if (self.tail) |tail| {
if (tail == value) return;
@field(tail, next) = value;
} else {
self.head = value;
}
self.tail = value;
}
pub fn popFirst(self: *Self) ?*T {
const head = self.head orelse return null;
self.head = @field(head, next);
@field(head, next) = null;
return head;
}
};
}