From 3340f8831f389c3f4f61112f4bcc842b2b707721 Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Mon, 5 Oct 2020 13:29:23 +0200 Subject: [PATCH] Add wl_list implementation --- src/common_core.zig | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/common_core.zig b/src/common_core.zig index f8281f5..2c38ff6 100644 --- a/src/common_core.zig +++ b/src/common_core.zig @@ -23,6 +23,49 @@ pub const Array = extern struct { data: *c_void, }; +pub const List = extern struct { + prev: *List, + next: *List, + + pub fn init(list: *List) void { + list.* = .{ .prev = list, .next = list }; + } + + pub fn insert(list: *List, elm: *List) void { + elm.prev = list; + elm.next = list.next; + list.next = elm; + elm.next.prev = elm; + } + + pub fn remove(elm: *elm) void { + elm.prev.next = elm.next; + elm.next.prev = elm.prev; + elm = undefined; + } + + pub fn length(list: *const List) usize { + var count: usize = 0; + var current = list.next; + while (current != list) : (current = current.next) { + count += 1; + } + return count; + } + + pub fn empty(list: *const List) bool { + return list.next == list; + } + + pub fn insertList(list: *List, other: *List) void { + if (other.empty()) return; + other.next.prev = list; + other.prev.next = list.next; + list.next.prev = other.prev; + list.next = other.next; + } +}; + pub const Fixed = extern enum(i32) { _,