orx_linked_list/list/mut_singly.rs
1use super::List;
2use crate::{SinglyIdx, iter::SinglyIterMut, variant::Singly};
3use orx_pinned_vec::PinnedVec;
4use orx_selfref_col::{MemoryPolicy, Node, NodeIdx, Refs};
5
6impl<T, M, P> List<Singly<T>, M, P>
7where
8 M: MemoryPolicy<Singly<T>>,
9 P: PinnedVec<Node<Singly<T>>>,
10{
11 /// ***O(1)*** Sets value of `front` of the list as `new_front` and:
12 /// * returns value of the front element;
13 /// * returns None if the list was empty.
14 ///
15 /// # Examples
16 ///
17 /// ```rust
18 /// use orx_linked_list::*;
19 ///
20 /// let mut list = SinglyList::new();
21 ///
22 /// assert_eq!(0, list.len());
23 ///
24 /// let prior_front = list.swap_front('a');
25 /// assert!(prior_front.is_none());
26 /// assert_eq!(Some(&'a'), list.front());
27 ///
28 /// let prior_front = list.swap_front('z');
29 /// assert_eq!(Some('a'), prior_front);
30 /// assert_eq!(Some(&'z'), list.front());
31 /// ```
32 pub fn swap_front(&mut self, new_front: T) -> Option<T> {
33 match self.0.ends().get() {
34 Some(p) => Some(self.0.swap_data(p, new_front)),
35 None => {
36 self.push_front(new_front);
37 None
38 }
39 }
40 }
41
42 /// ***O(1)*** Pushes the `value` to the `front` of the list.
43 ///
44 /// # Examples
45 ///
46 /// ```rust
47 /// use orx_linked_list::*;
48 ///
49 /// let mut list = DoublyList::new();
50 ///
51 /// list.push_front('a');
52 /// list.push_front('b');
53 ///
54 /// assert_eq!(Some(&'b'), list.front());
55 /// assert_eq!(Some(&'a'), list.back());
56 ///
57 /// let popped = list.pop_front();
58 /// assert_eq!(Some('b'), popped);
59 /// ```
60 pub fn push_front(&mut self, value: T) -> SinglyIdx<T> {
61 let idx = self.0.push(value);
62
63 if let Some(front) = self.0.ends().get() {
64 self.0.node_mut(idx).next_mut().set_some(front);
65 }
66
67 self.0.ends_mut().set_some(idx);
68
69 NodeIdx::new(self.0.memory_state(), idx)
70 }
71
72 /// ***O(1)*** Pops and returns the value at the `front` of the list; returns None if the list is empty.
73 ///
74 /// # Examples
75 ///
76 /// ```rust
77 /// use orx_linked_list::*;
78 ///
79 /// let mut list = SinglyList::new();
80 ///
81 /// let popped = list.pop_front();
82 /// assert!(popped.is_none());
83 ///
84 /// list.push_front('a');
85 /// assert_eq!(Some(&'a'), list.front());
86 ///
87 /// let popped = list.pop_front();
88 /// assert_eq!(Some('a'), popped);
89 /// assert!(list.is_empty());
90 /// ```
91 pub fn pop_front(&mut self) -> Option<T> {
92 self.0.ends().get().map(|front| {
93 match self.0.node(front).next().get() {
94 Some(new_front) => self.0.ends_mut().set_some(new_front),
95 None => self.0.ends_mut().clear(),
96 }
97 self.0.close_and_reclaim(front)
98 })
99 }
100
101 /// Returns a forward iterator of mutable references to elements of the list from front to back.
102 ///
103 /// # Examples
104 ///
105 /// ```rust
106 /// use orx_linked_list::*;
107 ///
108 /// let mut list = SinglyList::new();
109 ///
110 /// list.push_front(2);
111 /// list.push_front(1);
112 /// list.push_front(0);
113 /// assert!(list.eq_to_iter_vals([0, 1, 2]));
114 ///
115 /// for x in list.iter_mut() {
116 /// *x += 40;
117 /// }
118 ///
119 /// assert!(list.eq_to_iter_vals([40, 41, 42]));
120 /// ```
121 pub fn iter_mut(&mut self) -> SinglyIterMut<'_, T, P> {
122 SinglyIterMut::new_old(&mut self.0)
123 }
124}