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