authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-01-10 10:13:15-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2018-01-10 10:13:15-05:00
logd4f791cf6c560da25a0b84b4eb2ad68f8e7d27fd
treebde223d349ae1f6251495dc5a0e03dcae6513071
parent3c094116aae459b651934663a31981cf09cdb3e4
parent19343db5936abe5e49ea340db838fe769b499944
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #680 from zig-lang/intrusiveLinkedList

Intrusive linked lists

1 files changed, 87 insertions(+), 8 deletions(-)

std/linked_list.zig+87-8
......@@ -4,8 +4,18 @@ const assert = debug.assert;
44const mem = std.mem;
55const Allocator = mem.Allocator;
66
7/// Generic doubly linked list.
7/// Generic non-intrusive doubly linked list.
88pub fn LinkedList(comptime T: type) -> type {
9 return BaseLinkedList(T, void, "");
10}
11
12/// Generic intrusive doubly linked list.
13pub fn IntrusiveLinkedList(comptime ParentType: type, comptime field_name: []const u8) -> type {
14 return BaseLinkedList(void, ParentType, field_name);
15}
16
17/// Generic doubly linked list.
18fn BaseLinkedList(comptime T: type, comptime ParentType: type, comptime field_name: []const u8) -> type {
919 return struct {
1020 const Self = this;
1121
......@@ -15,13 +25,23 @@ pub fn LinkedList(comptime T: type) -> type {
1525 next: ?&Node,
1626 data: T,
1727
18 pub fn init(data: &const T) -> Node {
28 pub fn init(value: &const T) -> Node {
1929 return Node {
2030 .prev = null,
2131 .next = null,
22 .data = *data,
32 .data = *value,
2333 };
2434 }
35
36 pub fn initIntrusive() -> Node {
37 // TODO: when #678 is solved this can become `init`.
38 return Node.init({});
39 }
40
41 pub fn toData(node: &Node) -> &ParentType {
42 comptime assert(isIntrusive());
43 return @fieldParentPtr(ParentType, field_name, node);
44 }
2545 };
2646
2747 first: ?&Node,
......@@ -40,6 +60,10 @@ pub fn LinkedList(comptime T: type) -> type {
4060 };
4161 }
4262
63 fn isIntrusive() -> bool {
64 return ParentType != void or field_name.len != 0;
65 }
66
4367 /// Insert a new node after an existing one.
4468 ///
4569 /// Arguments:
......@@ -167,6 +191,7 @@ pub fn LinkedList(comptime T: type) -> type {
167191 /// Returns:
168192 /// A pointer to the new node.
169193 pub fn allocateNode(list: &Self, allocator: &Allocator) -> %&Node {
194 comptime assert(!isIntrusive());
170195 return allocator.create(Node);
171196 }
172197
......@@ -176,6 +201,7 @@ pub fn LinkedList(comptime T: type) -> type {
176201 /// node: Pointer to the node to deallocate.
177202 /// allocator: Dynamic memory allocator.
178203 pub fn destroyNode(list: &Self, node: &Node, allocator: &Allocator) {
204 comptime assert(!isIntrusive());
179205 allocator.destroy(node);
180206 }
181207
......@@ -188,6 +214,7 @@ pub fn LinkedList(comptime T: type) -> type {
188214 /// Returns:
189215 /// A pointer to the new node.
190216 pub fn createNode(list: &Self, data: &const T, allocator: &Allocator) -> %&Node {
217 comptime assert(!isIntrusive());
191218 var node = try list.allocateNode(allocator);
192219 *node = Node.init(data);
193220 return node;
......@@ -199,11 +226,11 @@ test "basic linked list test" {
199226 const allocator = debug.global_allocator;
200227 var list = LinkedList(u32).init();
201228
202 var one = list.createNode(1, allocator) catch unreachable;
203 var two = list.createNode(2, allocator) catch unreachable;
204 var three = list.createNode(3, allocator) catch unreachable;
205 var four = list.createNode(4, allocator) catch unreachable;
206 var five = list.createNode(5, allocator) catch unreachable;
229 var one = try list.createNode(1, allocator);
230 var two = try list.createNode(2, allocator);
231 var three = try list.createNode(3, allocator);
232 var four = try list.createNode(4, allocator);
233 var five = try list.createNode(5, allocator);
207234 defer {
208235 list.destroyNode(one, allocator);
209236 list.destroyNode(two, allocator);
......@@ -246,3 +273,55 @@ test "basic linked list test" {
246273 assert ((??list.last ).data == 4);
247274 assert (list.len == 2);
248275}
276
277const link = "link";
278const ElementList = IntrusiveLinkedList(Element, link);
279const Element = struct {
280 value: u32,
281 link: IntrusiveLinkedList(Element, link).Node,
282};
283
284test "basic intrusive linked list test" {
285 const allocator = debug.global_allocator;
286 var list = ElementList.init();
287
288 var one = Element { .value = 1, .link = ElementList.Node.initIntrusive() };
289 var two = Element { .value = 2, .link = ElementList.Node.initIntrusive() };
290 var three = Element { .value = 3, .link = ElementList.Node.initIntrusive() };
291 var four = Element { .value = 4, .link = ElementList.Node.initIntrusive() };
292 var five = Element { .value = 5, .link = ElementList.Node.initIntrusive() };
293
294 list.append(&two.link); // {2}
295 list.append(&five.link); // {2, 5}
296 list.prepend(&one.link); // {1, 2, 5}
297 list.insertBefore(&five.link, &four.link); // {1, 2, 4, 5}
298 list.insertAfter(&two.link, &three.link); // {1, 2, 3, 4, 5}
299
300 // Traverse forwards.
301 {
302 var it = list.first;
303 var index: u32 = 1;
304 while (it) |node| : (it = node.next) {
305 assert(node.toData().value == index);
306 index += 1;
307 }
308 }
309
310 // Traverse backwards.
311 {
312 var it = list.last;
313 var index: u32 = 1;
314 while (it) |node| : (it = node.prev) {
315 assert(node.toData().value == (6 - index));
316 index += 1;
317 }
318 }
319
320 var first = list.popFirst(); // {2, 3, 4, 5}
321 var last = list.pop(); // {2, 3, 4}
322 list.remove(&three.link); // {2, 4}
323
324 assert ((??list.first).toData().value == 2);
325 assert ((??list.last ).toData().value == 4);
326 assert (list.len == 2);
327}