Skip to main content

platform_trees/lists/
absolute_circular_linked_list.rs

1use crate::{AbsoluteLinkedList, LinkType};
2
3/// Circular doubly-linked list with absolute (direct) head/tail access.
4///
5/// Provides `attach_before`, `attach_after`, `attach_as_first`,
6/// `attach_as_last`, and `detach` operations that maintain circular
7/// links and update the head/tail/size automatically.
8///
9/// All methods have default implementations — an empty `impl` block
10/// is sufficient once [`AbsoluteLinkedList`] is implemented.
11pub trait AbsoluteCircularLinkedList<T: LinkType>: AbsoluteLinkedList<T> {
12    /// Inserts `new_element` immediately before `base_element`.
13    ///
14    /// If `base_element` is the current first element, the first
15    /// pointer is updated to `new_element`.
16    fn attach_before(&mut self, base_element: T, new_element: T) {
17        let base_element_previous = self.get_previous(base_element);
18        self.set_previous(new_element, base_element_previous);
19        self.set_next(new_element, base_element);
20        if base_element == self.get_first() {
21            self.set_first(new_element);
22        }
23        self.set_next(base_element_previous, new_element);
24        self.set_previous(base_element, new_element);
25        self.inc_size();
26    }
27
28    /// Inserts `new_element` immediately after `base_element`.
29    ///
30    /// If `base_element` is the current last element, the last
31    /// pointer is updated to `new_element`.
32    fn attach_after(&mut self, base_element: T, new_element: T) {
33        let base_element_next = self.get_next(base_element);
34        self.set_previous(new_element, base_element);
35        self.set_next(new_element, base_element_next);
36        if base_element == self.get_last() {
37            self.set_last(new_element);
38        }
39        self.set_previous(base_element_next, new_element);
40        self.set_next(base_element, new_element);
41        self.inc_size();
42    }
43
44    /// Inserts `element` as the first element of the list.
45    ///
46    /// If the list is empty, `element` becomes both first and last,
47    /// with its previous and next pointers pointing to itself.
48    fn attach_as_first(&mut self, element: T) {
49        let first = self.get_first();
50        if first == T::funty(0) {
51            self.set_first(element);
52            self.set_last(element);
53            self.set_previous(element, element);
54            self.set_next(element, element);
55            self.inc_size();
56        } else {
57            self.attach_before(first, element);
58        }
59    }
60
61    /// Inserts `element` as the last element of the list.
62    ///
63    /// If the list is empty, delegates to [`attach_as_first`](Self::attach_as_first).
64    fn attach_as_last(&mut self, element: T) {
65        let last = self.get_last();
66        if last == T::funty(0) {
67            self.attach_as_first(element);
68        } else {
69            self.attach_after(last, element);
70        }
71    }
72
73    /// Removes `element` from the list.
74    ///
75    /// Updates head/tail pointers as needed and clears the element's
76    /// previous and next pointers to `T::funty(0)`.
77    fn detach(&mut self, element: T) {
78        let element_previous = self.get_previous(element);
79        let element_next = self.get_next(element);
80        if element_next == element {
81            self.set_first(T::funty(0));
82            self.set_last(T::funty(0));
83        } else {
84            self.set_next(element_previous, element_next);
85            self.set_previous(element_next, element_previous);
86            if element == self.get_first() {
87                self.set_first(element_next);
88            }
89            if element == self.get_last() {
90                self.set_last(element_previous);
91            }
92        }
93        self.set_previous(element, T::funty(0));
94        self.set_next(element, T::funty(0));
95        self.dec_size();
96    }
97}