This repository has been archived by the owner on Nov 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.zig
289 lines (269 loc) · 9.41 KB
/
main.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//! This file defines data structures for different types of trees:
//! splay trees.
const std = @import("std");
const WhichEnd = enum { min, max };
pub fn SplayTree(comptime T: type, comptime cmp: fn (T, T) std.math.Order) type {
return struct {
//! A splay tree is a self-organizing data structure. Every operation
//! on the tree causes a splay to happen. The splay moves the requested
//! node to the root of the tree and partly rebalances it.
//!
//! This has the benefit that request locality causes faster lookups as
//! the requested nodes move to the top of the tree. On the other hand,
//! every lookup causes memory writes.
//!
//! The Balance Theorem bounds the total access time for m operations
//! and n inserts on an initially empty tree as O((m + n)lg n). The
//! amortized cost for a sequence of m accesses to a splay tree is O(lg n);
root: ?*Node = null,
pub const Node = struct {
data: T,
left: ?*@This() = null,
right: ?*@This() = null,
};
pub fn init() @This() {
return .{};
}
pub fn isEmpty(head: @This()) bool {
return head.root == null;
}
pub fn min(head: *@This()) ?*Node {
if (head.isEmpty()) return null;
splay_minmax(head, .min);
return head.root;
}
pub fn max(head: *@This()) ?*Node {
if (head.isEmpty()) return null;
splay_minmax(head, .max);
return head.root;
}
pub fn find(head: *@This(), elm: T) ?*Node {
if (head.isEmpty()) return null;
splay(head, elm);
if (cmp(elm, head.root.?.data) == .eq) return head.root;
return null;
}
pub fn next(head: *@This(), elm: *Node) ?*Node {
splay(head, elm.data);
if (elm.right) |child| {
var elm_ = child;
while (elm_.left) |closer| {
elm_ = closer;
}
return elm_;
} else return null;
}
pub fn prev(head: *@This(), elm: *Node) ?*Node {
splay(head, elm.data);
if (elm.left) |child| {
var elm_ = child;
while (elm_.right) |closer| {
elm_ = closer;
}
return elm_;
} else return null;
}
// returns if existing node exists
pub fn insert(head: *@This(), elm: *Node) ?*Node {
if (head.isEmpty()) {
elm.left = null; // ???
elm.right = null; // ???
} else {
splay(head, elm.data);
switch (cmp(elm.data, head.root.?.data)) {
.lt => {
elm.left = head.root.?.left;
elm.right = head.root;
head.root.?.left = null;
},
.gt => {
elm.right = head.root.?.right;
elm.left = head.root;
head.root.?.right = null;
},
.eq => {
return head.root;
},
}
}
head.root = elm;
return null;
}
pub fn remove(head: *@This(), elm: *Node) ?*Node {
if (head.isEmpty()) return null;
splay(head, elm.data);
if (cmp(elm.data, head.root.?.data) == .eq) {
if (head.root.?.left == null) {
head.root = head.root.?.right;
} else {
const tmp = head.root.?.right;
head.root = head.root.?.left;
splay(head, elm.data);
head.root.?.right = tmp;
}
return elm;
}
return null;
}
// rotate{Right,Left} expect that tmp hold {.right,.left}
fn rotateRight(head: *@This(), tmp: *Node) void {
head.root.?.left = tmp.right;
tmp.right = head.root;
head.root = tmp;
}
fn rotateLeft(head: *@This(), tmp: *Node) void {
head.root.?.right = tmp.left;
tmp.left = head.root;
head.root = tmp;
}
fn linkLeft(head: *@This(), tmp: **Node) void {
tmp.*.left = head.root;
tmp.* = head.root.?;
head.root = head.root.?.left;
}
fn linkRight(head: *@This(), tmp: **Node) void {
tmp.*.right = head.root;
tmp.* = head.root.?;
head.root = head.root.?.right;
}
fn assemble(head: *@This(), node: *Node, left: *Node, right: *Node) void {
left.right = head.root.?.left;
right.left = head.root.?.right;
head.root.?.left = node.right;
head.root.?.right = node.left;
}
fn splay(head: *@This(), elm: T) void {
var node: Node = undefined;
node.left = null;
node.right = null;
var left: *Node = &node;
var right: *Node = &node;
var tmp: ?*Node = undefined;
var comp: std.math.Order = undefined;
while (blk: {
comp = cmp(elm, head.root.?.data);
break :blk comp != .eq;
}) {
switch (comp) {
.lt => {
tmp = head.root.?.left;
if (tmp == null) break;
if (cmp(elm, tmp.?.data) == .lt) {
rotateRight(head, tmp.?);
if (head.root.?.left == null) break;
}
linkLeft(head, &right);
},
.gt => {
tmp = head.root.?.right;
if (tmp == null) break;
if (cmp(elm, tmp.?.data) == .gt) {
rotateLeft(head, tmp.?);
if (head.root.?.right == null) break;
}
linkRight(head, &left);
},
.eq => {},
}
}
assemble(head, &node, left, right);
}
/// Splay with either the minimum or the maximum element
/// Used to find minimum or maximum element in tree.
fn splay_minmax(head: *@This(), comp: WhichEnd) void {
var node: Node = undefined;
node.left = null;
node.right = null;
var left: *Node = &node;
var right: *Node = &node;
var tmp: ?*Node = undefined;
while (true) {
switch (comp) {
.min => {
tmp = head.root.?.left;
if (tmp == null) break;
if (comp == .min) { // ???
rotateRight(head, tmp.?);
if (head.root.?.left == null) break;
}
linkLeft(head, &right);
},
.max => {
tmp = head.root.?.right;
if (tmp == null) break;
if (comp == .max) { // ???
rotateLeft(head, tmp.?);
if (head.root.?.right == null) break;
}
linkRight(head, &left);
},
}
}
assemble(head, &node, left, right);
}
};
}
const t = std.testing;
fn _cmp_Entry_u8(lhs: u8, rhs: u8) std.math.Order {
return std.math.order(lhs, rhs);
}
test "foreach" {
const Tree = SplayTree(u8, _cmp_Entry_u8);
var tree = Tree.init();
var arena = std.heap.ArenaAllocator.init(t.allocator);
defer arena.deinit();
for (0..10) |i| {
const node = try arena.allocator().create(Tree.Node);
node.data = @intCast(i);
const existing = tree.insert(node);
try t.expectEqual(@as(?*Tree.Node, null), existing);
}
// iterate min to max
{
var i: u8 = 0;
var x = tree.min();
while (x) |_x| : ({
x = tree.next(_x);
i += 1;
}) {
try t.expectEqual(i, _x.data);
}
try t.expectEqual(@as(u8, 10), i);
}
// iterate max to min
{
var i: u8 = 0;
var x = tree.max();
while (x) |_x| : ({
x = tree.prev(_x);
i += 1;
}) {
try t.expectEqual(9 - i, _x.data);
}
try t.expectEqual(@as(u8, 10), i);
}
// find
for (0..10) |i| {
const node = tree.find(@intCast(i));
_ = node.?;
}
}
test "how to free nodes correctly" {
const Tree = SplayTree(u8, _cmp_Entry_u8);
var tree = Tree.init();
// insert nodes
for (0..10) |i| {
const node = try t.allocator.create(Tree.Node);
node.data = @intCast(i);
const existing = tree.insert(node);
try t.expectEqual(@as(?*Tree.Node, null), existing);
}
// free nodes
var x = tree.min();
while (x) |_x| {
x = tree.next(_x); // must do it before the node is freed
const _x_dup = tree.remove(_x);
try t.expectEqual(_x, _x_dup.?);
t.allocator.destroy(_x);
}
}