platform_trees/lists/relative_doubly_linked_list.rs
1use crate::LinkedList;
2use platform_num::LinkReference;
3
4/// Linked list with head-relative access to first, last, and size.
5///
6/// Unlike [`AbsoluteLinkedList`](super::AbsoluteLinkedList), this trait
7/// stores head/tail/size per `head` index, allowing multiple independent
8/// lists to share the same underlying node storage.
9pub trait RelativeLinkedList<T: LinkReference>: LinkedList<T> {
10 /// Returns the first element of the list identified by `head`.
11 fn get_first(&self, head: T) -> T;
12
13 /// Returns the last element of the list identified by `head`.
14 fn get_last(&self, head: T) -> T;
15
16 /// Returns the size of the list identified by `head`.
17 fn get_size(&self, head: T) -> T;
18
19 /// Sets the first element of the list identified by `head`.
20 fn set_first(&mut self, head: T, element: T);
21
22 /// Sets the last element of the list identified by `head`.
23 fn set_last(&mut self, head: T, element: T);
24
25 /// Sets the size of the list identified by `head`.
26 fn set_size(&mut self, head: T, size: T);
27
28 /// Increments the size of the list identified by `head` by one.
29 fn inc_size(&mut self, head: T) {
30 self.set_size(head, self.get_size(head) + T::from_byte(1));
31 }
32
33 /// Decrements the size of the list identified by `head` by one.
34 fn dec_size(&mut self, head: T) {
35 self.set_size(head, self.get_size(head) - T::from_byte(1));
36 }
37}