// frozen from alpm.v 3d0a6ea for differential tests; never a production backend. // derived from pacman lib/libalpm/alpm_list.c, copyright (c) 2006-2025 // pacman development team and (c) 2002-2006 judd vinet. // SPDX-License-Identifier: GPL-2.0-or-later module coracle #include #include fn C.free(ptr voidptr) // only the head's prev points to the tail; next is never circular. // libc allocation keeps nodes interchangeable with the remaining C backend. pub struct List { pub mut: data voidptr = unsafe { nil } prev &List = unsafe { nil } next &List = unsafe { nil } } type ListFnFree = fn (voidptr) type ListFnCmp = fn (voidptr, voidptr) i32 pub fn (list &List) free() { mut item := unsafe { list } for item != unsafe { nil } { next := item.next C.free(item) item = next } } pub fn (list &List) free_inner(fn_ ListFnFree) { if voidptr(fn_) == unsafe { nil } { return } for item := unsafe { list }; item != unsafe { nil }; item = item.next { if item.data != unsafe { nil } { fn_(item.data) } } } pub fn (list &List) add(data voidptr) &List { mut head := unsafe { list } (&head).append(data) return head } pub fn (list &&List) append(data voidptr) &List { mut node := unsafe { &List(C.malloc(sizeof(List))) } if node == unsafe { nil } { return node } node.data = data node.next = unsafe { nil } unsafe { if *list == nil { *list = node node.prev = node } else { tail := (*list).prev tail.next = node node.prev = tail (*list).prev = node } } return node } pub fn (list &&List) append_strdup(data string) &List { dup := unsafe { C.strdup(data.str) } if dup == unsafe { nil } { return unsafe { nil } } node := list.append(dup) if node == unsafe { nil } { C.free(dup) } return node } pub fn (list &List) add_sorted(data voidptr, fn_ ListFnCmp) &List { if list == unsafe { nil } || voidptr(fn_) == unsafe { nil } { return list.add(data) } mut node := unsafe { &List(C.malloc(sizeof(List))) } if node == unsafe { nil } { return list } node.data = data mut before := &List(unsafe { nil }) mut after := unsafe { list } for after != unsafe { nil } && fn_(data, after.data) > 0 { before = after after = after.next } unsafe { if before == nil { node.prev = list.prev node.next = list list.prev = node return node } node.prev = before node.next = after before.next = node if after == nil { list.prev = node } else { after.prev = node } } return list } pub fn (first &List) join(second &List) &List { if first == unsafe { nil } { return second } if second == unsafe { nil } { return first } unsafe { tail := first.prev tail.next = second first.prev = second.prev second.prev = tail } return first } pub fn (left &List) mmerge(right &List, fn_ ListFnCmp) &List { if left == unsafe { nil } { return right } if right == unsafe { nil } { return left } left_tail := left.prev right_tail := right.prev mut l := unsafe { left } mut r := unsafe { right } mut head := &List(unsafe { nil }) if fn_(l.data, r.data) <= 0 { head = l l = l.next } else { head = r r = r.next } head.prev = unsafe { nil } head.next = unsafe { nil } mut tail := head for l != unsafe { nil } && r != unsafe { nil } { if fn_(l.data, r.data) <= 0 { tail.next = l l.prev = tail l = l.next } else { tail.next = r r.prev = tail r = r.next } tail = tail.next tail.next = unsafe { nil } } if l != unsafe { nil } { tail.next = l l.prev = tail head.prev = left_tail } else if r != unsafe { nil } { tail.next = r r.prev = tail head.prev = right_tail } else { head.prev = tail } return head } pub fn (list &List) msort(n uint, fn_ ListFnCmp) &List { if n <= 1 { return list } half := n / 2 mut last_left := list.nth(half - 1) mut right := last_left.next last_left.next = unsafe { nil } right.prev = list.prev unsafe { list.prev = last_left } left := list.msort(half, fn_) sorted_right := right.msort(n - half, fn_) return left.mmerge(sorted_right, fn_) } // detaching never frees the node; its fields retain pacman's exact state. pub fn (haystack &List) remove_item(item &List) &List { if haystack == unsafe { nil } || item == unsafe { nil } { return haystack } mut head := unsafe { haystack } unsafe { if item == head { head = item.next if head != nil { head.prev = item.prev } item.prev = nil } else if item == head.prev { if item.prev != nil { item.prev.next = item.next head.prev = item.prev item.prev = nil } } else { if item.next != nil { item.next.prev = item.prev } if item.prev != nil { item.prev.next = item.next } } } return head } pub fn (haystack &List) remove(needle voidptr, fn_ ListFnCmp) (&List, voidptr) { if needle == unsafe { nil } { return haystack, unsafe { nil } } for item := unsafe { haystack }; item != unsafe { nil }; item = item.next { if item.data != unsafe { nil } && fn_(item.data, needle) == 0 { head := haystack.remove_item(item) data := item.data C.free(item) return head, data } } return haystack, unsafe { nil } } fn list_string_cmp(a voidptr, b voidptr) i32 { return unsafe { C.strcmp(a, b) } } pub fn (haystack &List) remove_str(needle string) (&List, &u8) { head, data := haystack.remove(needle.str, list_string_cmp) return head, unsafe { &u8(data) } } pub fn (list &List) remove_dupes() &List { mut result := &List(unsafe { nil }) for item := unsafe { list }; item != unsafe { nil }; item = item.next { if _ := result.find_ptr(item.data) { continue } if (&result).append(item.data) == unsafe { nil } { result.free() return unsafe { nil } } } return result } pub fn (list &List) strdup() &List { mut result := &List(unsafe { nil }) for item := unsafe { list }; item != unsafe { nil }; item = item.next { value := unsafe { (&u8(item.data)).vstring() } if (&result).append_strdup(value) == unsafe { nil } { result.free_inner(C.free) result.free() return unsafe { nil } } } return result } pub fn (list &List) copy() &List { mut result := &List(unsafe { nil }) for item := unsafe { list }; item != unsafe { nil }; item = item.next { if (&result).append(item.data) == unsafe { nil } { result.free() return unsafe { nil } } } return result } pub fn (list &List) copy_data(size uint) &List { mut result := &List(unsafe { nil }) for item := unsafe { list }; item != unsafe { nil }; item = item.next { data := unsafe { C.malloc(size) } if data != unsafe { nil } { unsafe { C.memcpy(data, item.data, size) } if (&result).append(data) != unsafe { nil } { continue } C.free(data) } result.free_inner(C.free) result.free() return unsafe { nil } } return result } pub fn (list &List) reverse() &List { mut result := &List(unsafe { nil }) mut item := list.last() for item != unsafe { nil } { if (&result).append(item.data) == unsafe { nil } { result.free() return unsafe { nil } } if item == list { break } item = item.prev } return result } pub fn (list &List) nth(n uint) &List { mut item := unsafe { list } for _ in 0 .. n { // an out-of-range lookup can be answered without dereferencing NULL. if item == unsafe { nil } { return item } item = item.next } return item } pub fn (list &List) next() &List { return if list == unsafe { nil } { unsafe { nil } } else { list.next } } pub fn (list &List) previous() &List { if list != unsafe { nil } && list.prev != unsafe { nil } && list.prev.next != unsafe { nil } { return list.prev } return unsafe { nil } } pub fn (list &List) last() &List { return if list == unsafe { nil } { unsafe { nil } } else { list.prev } } pub fn (list &List) count() uint { mut count := uint(0) for item := unsafe { list }; item != unsafe { nil }; item = item.next { count++ } return count } pub fn (haystack &List) find(needle voidptr, fn_ ListFnCmp) ?voidptr { for item := unsafe { haystack }; item != unsafe { nil }; item = item.next { if item.data != unsafe { nil } && fn_(item.data, needle) == 0 { return item.data } } return none } fn list_pointer_cmp(a voidptr, b voidptr) i32 { return if a == b { 0 } else { 1 } } pub fn (haystack &List) find_ptr(needle voidptr) ?voidptr { return haystack.find(needle, list_pointer_cmp) } pub fn (haystack &List) find_str(needle string) ?string { data := haystack.find(needle.str, list_string_cmp) or { return none } return unsafe { (&u8(data)).vstring() } } pub fn (left &List) cmp_unsorted(right &List, fn_ ListFnCmp) int { mut l := unsafe { left } mut r := unsafe { right } for l != unsafe { nil } && r != unsafe { nil } { l = l.next r = r.next } if l != unsafe { nil } || r != unsafe { nil } { return 0 } l = unsafe { left } r = unsafe { right } for l != unsafe { nil } && fn_(l.data, r.data) == 0 { l = l.next r = r.next } if l == unsafe { nil } { return 1 } remaining := r matched := unsafe { &u8(C.calloc(r.count(), 1)) } if matched == unsafe { nil } { return -1 } defer { C.free(matched) } for l != unsafe { nil } { r = remaining mut n := 0 mut found := false for r != unsafe { nil } { if unsafe { matched[n] } == 0 && fn_(l.data, r.data) == 0 { unsafe { matched[n] = 1 } found = true break } r = r.next n++ } if !found { return 0 } l = l.next } return 1 } pub fn (left &List) diff_sorted(right &List, fn_ ListFnCmp) (&List, &List) { mut onlyleft := &List(unsafe { nil }) mut onlyright := &List(unsafe { nil }) list_diff_sorted(left, right, fn_, &onlyleft, &onlyright) return onlyleft, onlyright } fn list_diff_sorted(left &List, right &List, fn_ ListFnCmp, onlyleft &&List, onlyright &&List) { mut l := unsafe { left } mut r := unsafe { right } for l != unsafe { nil } && r != unsafe { nil } { cmp := fn_(l.data, r.data) if cmp < 0 { if onlyleft != unsafe { nil } { unsafe { *onlyleft = (*onlyleft).add(l.data) } } l = l.next } else if cmp > 0 { if onlyright != unsafe { nil } { unsafe { *onlyright = (*onlyright).add(r.data) } } r = r.next } else { l = l.next r = r.next } } for l != unsafe { nil } { if onlyleft != unsafe { nil } { unsafe { *onlyleft = (*onlyleft).add(l.data) } } l = l.next } for r != unsafe { nil } { if onlyright != unsafe { nil } { unsafe { *onlyright = (*onlyright).add(r.data) } } r = r.next } } pub fn (lhs &List) diff(rhs &List, fn_ ListFnCmp) &List { lcopy := lhs.copy() rcopy := rhs.copy() left := lcopy.msort(lcopy.count(), fn_) right := rcopy.msort(rcopy.count(), fn_) defer { left.free() right.free() } mut result := &List(unsafe { nil }) list_diff_sorted(left, right, fn_, &result, unsafe { nil }) return result } pub fn (list &List) to_array(n uint, size uint) ?voidptr { if n == 0 { return none } // avoid a wrapped allocation on platforms with a smaller size_t. if size > 0 && n > ~uint(0) / size { return none } array := unsafe { &u8(C.malloc(n * size)) } if array == unsafe { nil } { return none } mut item := unsafe { list } for i := uint(0); i < n && item != unsafe { nil }; i++ { unsafe { C.memcpy(array + i * size, item.data, size) } item = item.next } return array }