orx_linked_list/list/
mutate.rs

1use super::List;
2use crate::variant::ListVariant;
3use orx_iterable::CollectionMut;
4use orx_selfref_col::MemoryPolicy;
5
6impl<V, M> List<V, M>
7where
8    V: ListVariant,
9    M: MemoryPolicy<V>,
10{
11    /// Clears the list removing all elements.
12    ///
13    /// # Examples
14    ///
15    /// ```rust
16    /// use orx_linked_list::*;
17    ///
18    /// let mut list = DoublyList::new();
19    ///
20    /// list.push_front('b');
21    /// list.push_back('c');
22    /// list.push_front('a');
23    ///
24    /// assert_eq!(3, list.len());
25    ///
26    /// list.clear();
27    /// assert!(list.is_empty());
28    /// assert!(list.front().is_none());
29    /// ```
30    #[inline(always)]
31    pub fn clear(&mut self) {
32        self.0.clear();
33    }
34
35    /// Returns an arbitrary order iterator of mutable references to elements of the list from front to back.
36    ///
37    /// Note that the iterator created by `iter_mut_x` is often faster than that created by `iter_mut`;
38    /// and hence, can be preferred whenever the iteration order does not matter.
39    ///
40    /// # Examples
41    ///
42    /// ```rust
43    /// use orx_linked_list::*;
44    ///
45    /// let mut list = DoublyList::new();
46    ///
47    /// list.push_front(2);
48    /// list.push_front(1);
49    /// list.push_front(0);
50    /// assert!(list.eq_to_iter_vals([0, 1, 2]));
51    ///
52    /// for x in list.iter_mut_x() {
53    ///     *x += 40;
54    /// }
55    ///
56    /// assert!(list.eq_to_iter_vals([40, 41, 42]));
57    /// ```
58    pub fn iter_mut_x(&mut self) -> impl Iterator<Item = &mut V::Item> {
59        self.0.nodes_mut().iter_mut().filter_map(|x| x.data_mut())
60    }
61}