nspkg / tests / coracle / list.v
428 lines · 392 sloc · 10.87 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1// frozen from alpm.v 3d0a6ea for differential tests; never a production backend.
2// derived from pacman lib/libalpm/alpm_list.c, copyright (c) 2006-2025
3// pacman development team and (c) 2002-2006 judd vinet.
4// SPDX-License-Identifier: GPL-2.0-or-later
5module coracle
6
7#include <stdlib.h>
8#include <string.h>
9
10fn C.free(ptr voidptr)
11
12// only the head's prev points to the tail; next is never circular.
13// libc allocation keeps nodes interchangeable with the remaining C backend.
14pub struct List {
15pub mut:
16 data voidptr = unsafe { nil }
17 prev &List = unsafe { nil }
18 next &List = unsafe { nil }
19}
20
21type ListFnFree = fn (voidptr)
22type ListFnCmp = fn (voidptr, voidptr) i32
23
24pub fn (list &List) free() {
25 mut item := unsafe { list }
26 for item != unsafe { nil } {
27 next := item.next
28 C.free(item)
29 item = next
30 }
31}
32
33pub fn (list &List) free_inner(fn_ ListFnFree) {
34 if voidptr(fn_) == unsafe { nil } { return }
35 for item := unsafe { list }; item != unsafe { nil }; item = item.next {
36 if item.data != unsafe { nil } { fn_(item.data) }
37 }
38}
39
40pub fn (list &List) add(data voidptr) &List {
41 mut head := unsafe { list }
42 (&head).append(data)
43 return head
44}
45
46pub fn (list &&List) append(data voidptr) &List {
47 mut node := unsafe { &List(C.malloc(sizeof(List))) }
48 if node == unsafe { nil } { return node }
49 node.data = data
50 node.next = unsafe { nil }
51 unsafe {
52 if *list == nil {
53 *list = node
54 node.prev = node
55 } else {
56 tail := (*list).prev
57 tail.next = node
58 node.prev = tail
59 (*list).prev = node
60 }
61 }
62 return node
63}
64
65pub fn (list &&List) append_strdup(data string) &List {
66 dup := unsafe { C.strdup(data.str) }
67 if dup == unsafe { nil } { return unsafe { nil } }
68 node := list.append(dup)
69 if node == unsafe { nil } { C.free(dup) }
70 return node
71}
72
73pub fn (list &List) add_sorted(data voidptr, fn_ ListFnCmp) &List {
74 if list == unsafe { nil } || voidptr(fn_) == unsafe { nil } {
75 return list.add(data)
76 }
77 mut node := unsafe { &List(C.malloc(sizeof(List))) }
78 if node == unsafe { nil } { return list }
79 node.data = data
80 mut before := &List(unsafe { nil })
81 mut after := unsafe { list }
82 for after != unsafe { nil } && fn_(data, after.data) > 0 {
83 before = after
84 after = after.next
85 }
86 unsafe {
87 if before == nil {
88 node.prev = list.prev
89 node.next = list
90 list.prev = node
91 return node
92 }
93 node.prev = before
94 node.next = after
95 before.next = node
96 if after == nil { list.prev = node } else { after.prev = node }
97 }
98 return list
99}
100
101pub fn (first &List) join(second &List) &List {
102 if first == unsafe { nil } { return second }
103 if second == unsafe { nil } { return first }
104 unsafe {
105 tail := first.prev
106 tail.next = second
107 first.prev = second.prev
108 second.prev = tail
109 }
110 return first
111}
112
113pub fn (left &List) mmerge(right &List, fn_ ListFnCmp) &List {
114 if left == unsafe { nil } { return right }
115 if right == unsafe { nil } { return left }
116 left_tail := left.prev
117 right_tail := right.prev
118 mut l := unsafe { left }
119 mut r := unsafe { right }
120 mut head := &List(unsafe { nil })
121 if fn_(l.data, r.data) <= 0 {
122 head = l
123 l = l.next
124 } else {
125 head = r
126 r = r.next
127 }
128 head.prev = unsafe { nil }
129 head.next = unsafe { nil }
130 mut tail := head
131 for l != unsafe { nil } && r != unsafe { nil } {
132 if fn_(l.data, r.data) <= 0 {
133 tail.next = l
134 l.prev = tail
135 l = l.next
136 } else {
137 tail.next = r
138 r.prev = tail
139 r = r.next
140 }
141 tail = tail.next
142 tail.next = unsafe { nil }
143 }
144 if l != unsafe { nil } {
145 tail.next = l
146 l.prev = tail
147 head.prev = left_tail
148 } else if r != unsafe { nil } {
149 tail.next = r
150 r.prev = tail
151 head.prev = right_tail
152 } else {
153 head.prev = tail
154 }
155 return head
156}
157
158pub fn (list &List) msort(n uint, fn_ ListFnCmp) &List {
159 if n <= 1 { return list }
160 half := n / 2
161 mut last_left := list.nth(half - 1)
162 mut right := last_left.next
163 last_left.next = unsafe { nil }
164 right.prev = list.prev
165 unsafe { list.prev = last_left }
166 left := list.msort(half, fn_)
167 sorted_right := right.msort(n - half, fn_)
168 return left.mmerge(sorted_right, fn_)
169}
170
171// detaching never frees the node; its fields retain pacman's exact state.
172pub fn (haystack &List) remove_item(item &List) &List {
173 if haystack == unsafe { nil } || item == unsafe { nil } { return haystack }
174 mut head := unsafe { haystack }
175 unsafe {
176 if item == head {
177 head = item.next
178 if head != nil { head.prev = item.prev }
179 item.prev = nil
180 } else if item == head.prev {
181 if item.prev != nil {
182 item.prev.next = item.next
183 head.prev = item.prev
184 item.prev = nil
185 }
186 } else {
187 if item.next != nil { item.next.prev = item.prev }
188 if item.prev != nil { item.prev.next = item.next }
189 }
190 }
191 return head
192}
193
194pub fn (haystack &List) remove(needle voidptr, fn_ ListFnCmp) (&List, voidptr) {
195 if needle == unsafe { nil } { return haystack, unsafe { nil } }
196 for item := unsafe { haystack }; item != unsafe { nil }; item = item.next {
197 if item.data != unsafe { nil } && fn_(item.data, needle) == 0 {
198 head := haystack.remove_item(item)
199 data := item.data
200 C.free(item)
201 return head, data
202 }
203 }
204 return haystack, unsafe { nil }
205}
206
207fn list_string_cmp(a voidptr, b voidptr) i32 {
208 return unsafe { C.strcmp(a, b) }
209}
210
211pub fn (haystack &List) remove_str(needle string) (&List, &u8) {
212 head, data := haystack.remove(needle.str, list_string_cmp)
213 return head, unsafe { &u8(data) }
214}
215
216pub fn (list &List) remove_dupes() &List {
217 mut result := &List(unsafe { nil })
218 for item := unsafe { list }; item != unsafe { nil }; item = item.next {
219 if _ := result.find_ptr(item.data) { continue }
220 if (&result).append(item.data) == unsafe { nil } {
221 result.free()
222 return unsafe { nil }
223 }
224 }
225 return result
226}
227
228pub fn (list &List) strdup() &List {
229 mut result := &List(unsafe { nil })
230 for item := unsafe { list }; item != unsafe { nil }; item = item.next {
231 value := unsafe { (&u8(item.data)).vstring() }
232 if (&result).append_strdup(value) == unsafe { nil } {
233 result.free_inner(C.free)
234 result.free()
235 return unsafe { nil }
236 }
237 }
238 return result
239}
240
241pub fn (list &List) copy() &List {
242 mut result := &List(unsafe { nil })
243 for item := unsafe { list }; item != unsafe { nil }; item = item.next {
244 if (&result).append(item.data) == unsafe { nil } {
245 result.free()
246 return unsafe { nil }
247 }
248 }
249 return result
250}
251
252pub fn (list &List) copy_data(size uint) &List {
253 mut result := &List(unsafe { nil })
254 for item := unsafe { list }; item != unsafe { nil }; item = item.next {
255 data := unsafe { C.malloc(size) }
256 if data != unsafe { nil } {
257 unsafe { C.memcpy(data, item.data, size) }
258 if (&result).append(data) != unsafe { nil } { continue }
259 C.free(data)
260 }
261 result.free_inner(C.free)
262 result.free()
263 return unsafe { nil }
264 }
265 return result
266}
267
268pub fn (list &List) reverse() &List {
269 mut result := &List(unsafe { nil })
270 mut item := list.last()
271 for item != unsafe { nil } {
272 if (&result).append(item.data) == unsafe { nil } {
273 result.free()
274 return unsafe { nil }
275 }
276 if item == list { break }
277 item = item.prev
278 }
279 return result
280}
281
282pub fn (list &List) nth(n uint) &List {
283 mut item := unsafe { list }
284 for _ in 0 .. n {
285 // an out-of-range lookup can be answered without dereferencing NULL.
286 if item == unsafe { nil } { return item }
287 item = item.next
288 }
289 return item
290}
291
292pub fn (list &List) next() &List {
293 return if list == unsafe { nil } { unsafe { nil } } else { list.next }
294}
295
296pub fn (list &List) previous() &List {
297 if list != unsafe { nil } && list.prev != unsafe { nil } && list.prev.next != unsafe { nil } {
298 return list.prev
299 }
300 return unsafe { nil }
301}
302
303pub fn (list &List) last() &List {
304 return if list == unsafe { nil } { unsafe { nil } } else { list.prev }
305}
306
307pub fn (list &List) count() uint {
308 mut count := uint(0)
309 for item := unsafe { list }; item != unsafe { nil }; item = item.next { count++ }
310 return count
311}
312
313pub fn (haystack &List) find(needle voidptr, fn_ ListFnCmp) ?voidptr {
314 for item := unsafe { haystack }; item != unsafe { nil }; item = item.next {
315 if item.data != unsafe { nil } && fn_(item.data, needle) == 0 { return item.data }
316 }
317 return none
318}
319
320fn list_pointer_cmp(a voidptr, b voidptr) i32 { return if a == b { 0 } else { 1 } }
321
322pub fn (haystack &List) find_ptr(needle voidptr) ?voidptr {
323 return haystack.find(needle, list_pointer_cmp)
324}
325
326pub fn (haystack &List) find_str(needle string) ?string {
327 data := haystack.find(needle.str, list_string_cmp) or { return none }
328 return unsafe { (&u8(data)).vstring() }
329}
330
331pub fn (left &List) cmp_unsorted(right &List, fn_ ListFnCmp) int {
332 mut l := unsafe { left }
333 mut r := unsafe { right }
334 for l != unsafe { nil } && r != unsafe { nil } {
335 l = l.next
336 r = r.next
337 }
338 if l != unsafe { nil } || r != unsafe { nil } { return 0 }
339 l = unsafe { left }
340 r = unsafe { right }
341 for l != unsafe { nil } && fn_(l.data, r.data) == 0 {
342 l = l.next
343 r = r.next
344 }
345 if l == unsafe { nil } { return 1 }
346 remaining := r
347 matched := unsafe { &u8(C.calloc(r.count(), 1)) }
348 if matched == unsafe { nil } { return -1 }
349 defer { C.free(matched) }
350 for l != unsafe { nil } {
351 r = remaining
352 mut n := 0
353 mut found := false
354 for r != unsafe { nil } {
355 if unsafe { matched[n] } == 0 && fn_(l.data, r.data) == 0 {
356 unsafe { matched[n] = 1 }
357 found = true
358 break
359 }
360 r = r.next
361 n++
362 }
363 if !found { return 0 }
364 l = l.next
365 }
366 return 1
367}
368
369pub fn (left &List) diff_sorted(right &List, fn_ ListFnCmp) (&List, &List) {
370 mut onlyleft := &List(unsafe { nil })
371 mut onlyright := &List(unsafe { nil })
372 list_diff_sorted(left, right, fn_, &onlyleft, &onlyright)
373 return onlyleft, onlyright
374}
375
376fn list_diff_sorted(left &List, right &List, fn_ ListFnCmp, onlyleft &&List, onlyright &&List) {
377 mut l := unsafe { left }
378 mut r := unsafe { right }
379 for l != unsafe { nil } && r != unsafe { nil } {
380 cmp := fn_(l.data, r.data)
381 if cmp < 0 {
382 if onlyleft != unsafe { nil } { unsafe { *onlyleft = (*onlyleft).add(l.data) } }
383 l = l.next
384 } else if cmp > 0 {
385 if onlyright != unsafe { nil } { unsafe { *onlyright = (*onlyright).add(r.data) } }
386 r = r.next
387 } else {
388 l = l.next
389 r = r.next
390 }
391 }
392 for l != unsafe { nil } {
393 if onlyleft != unsafe { nil } { unsafe { *onlyleft = (*onlyleft).add(l.data) } }
394 l = l.next
395 }
396 for r != unsafe { nil } {
397 if onlyright != unsafe { nil } { unsafe { *onlyright = (*onlyright).add(r.data) } }
398 r = r.next
399 }
400}
401
402pub fn (lhs &List) diff(rhs &List, fn_ ListFnCmp) &List {
403 lcopy := lhs.copy()
404 rcopy := rhs.copy()
405 left := lcopy.msort(lcopy.count(), fn_)
406 right := rcopy.msort(rcopy.count(), fn_)
407 defer {
408 left.free()
409 right.free()
410 }
411 mut result := &List(unsafe { nil })
412 list_diff_sorted(left, right, fn_, &result, unsafe { nil })
413 return result
414}
415
416pub fn (list &List) to_array(n uint, size uint) ?voidptr {
417 if n == 0 { return none }
418 // avoid a wrapped allocation on platforms with a smaller size_t.
419 if size > 0 && n > ~uint(0) / size { return none }
420 array := unsafe { &u8(C.malloc(n * size)) }
421 if array == unsafe { nil } { return none }
422 mut item := unsafe { list }
423 for i := uint(0); i < n && item != unsafe { nil }; i++ {
424 unsafe { C.memcpy(array + i * size, item.data, size) }
425 item = item.next
426 }
427 return array
428}
429