-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.zig
159 lines (129 loc) · 5.24 KB
/
list.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
const std = @import("std");
const assert = std.debug.assert;
const constants = @import("constants.zig");
const stdx = @import("stdx.zig");
/// An intrusive doubly-linked list.
/// Currently it is LIFO for simplicity because its consumer (IO.awaiting) doesn't care about order.
pub fn DoublyLinkedListType(
comptime Node: type,
comptime field_back_enum: std.meta.FieldEnum(Node),
comptime field_next_enum: std.meta.FieldEnum(Node),
) type {
assert(@typeInfo(Node) == .Struct);
assert(field_back_enum != field_next_enum);
assert(std.meta.FieldType(Node, field_back_enum) == ?*Node);
assert(std.meta.FieldType(Node, field_next_enum) == ?*Node);
const field_back = @tagName(field_back_enum);
const field_next = @tagName(field_next_enum);
return struct {
const DoublyLinkedList = @This();
tail: ?*Node = null,
count: u32 = 0,
pub fn verify(list: *const DoublyLinkedList) void {
assert((list.count == 0) == (list.tail == null));
var count: u32 = 0;
var iterator = list.tail;
if (iterator) |node| {
assert(@field(node, field_next) == null);
}
while (iterator) |node| {
const back = @field(node, field_back);
if (back) |back_node| {
assert(back_node != node); // There are no cycles.
assert(@field(back_node, field_next) == node);
}
count += 1;
iterator = back;
}
assert(count == list.count);
}
fn contains(list: *const DoublyLinkedList, target: *const Node) bool {
var count: u32 = 0;
var iterator = list.tail;
while (iterator) |node| {
if (node == target) return true;
iterator = @field(node, field_back);
count += 1;
}
assert(count == list.count);
return false;
}
pub fn empty(list: *const DoublyLinkedList) bool {
assert((list.count == 0) == (list.tail == null));
return list.count == 0;
}
pub fn push(list: *DoublyLinkedList, node: *Node) void {
if (constants.verify) assert(!list.contains(node));
if (constants.verify) list.verify();
assert(@field(node, field_back) == null);
assert(@field(node, field_next) == null);
if (list.tail) |tail| {
assert(list.count > 0);
assert(@field(tail, field_next) == null);
@field(node, field_back) = tail;
@field(tail, field_next) = node;
} else {
assert(list.count == 0);
}
list.tail = node;
list.count += 1;
}
pub fn pop(list: *DoublyLinkedList) ?*Node {
if (constants.verify) list.verify();
if (list.tail) |tail_old| {
assert(list.count > 0);
assert(@field(tail_old, field_next) == null);
list.tail = @field(tail_old, field_back);
list.count -= 1;
if (list.tail) |tail_new| {
assert(@field(tail_new, field_next) == tail_old);
@field(tail_new, field_next) = null;
}
@field(tail_old, field_back) = null;
return tail_old;
} else {
assert(list.count == 0);
return null;
}
}
pub fn remove(list: *DoublyLinkedList, node: *Node) void {
if (constants.verify) assert(list.contains(node));
if (constants.verify) list.verify();
assert(list.count > 0);
assert(list.tail != null);
const tail = list.tail.?;
if (node == tail) {
// Pop the last element of the list.
assert(@field(node, field_next) == null);
list.tail = @field(node, field_back);
}
if (@field(node, field_back)) |node_back| {
assert(@field(node_back, field_next).? == node);
@field(node_back, field_next) = @field(node, field_next);
}
if (@field(node, field_next)) |node_next| {
assert(@field(node_next, field_back).? == node);
@field(node_next, field_back) = @field(node, field_back);
}
@field(node, field_back) = null;
@field(node, field_next) = null;
list.count -= 1;
if (constants.verify) list.verify();
assert((list.count == 0) == (list.tail == null));
}
};
}
test "DoublyLinkedList LIFO" {
const Node = struct { id: u32, back: ?*@This() = null, next: ?*@This() = null };
const List = DoublyLinkedListType(Node, .back, .next);
var nodes: [3]Node = undefined;
for (&nodes, 0..) |*node, i| node.* = .{ .id = @intCast(i) };
var list = List{};
list.push(&nodes[0]);
list.push(&nodes[1]);
list.push(&nodes[2]);
try std.testing.expectEqual(list.pop().?, &nodes[2]);
try std.testing.expectEqual(list.pop().?, &nodes[1]);
try std.testing.expectEqual(list.pop().?, &nodes[0]);
try std.testing.expectEqual(list.pop(), null);
}