1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
//! This library implements a heapless linked list by keeping each element on the stack. //! //! The primary purpose is to efficiently keep a context when traversing trees recursively. //! //! Compared to a traditional linked list, this has a few drawbacks: //! * Not "self-contained": you cannot easily store a `Node` as a member like you would a //! `LinkedList`. //! * Many methods use recursion and lead to increase stack memory usage (because of function //! frames). //! //! The main advantage is that it does not need any heap allocation, and can grow to arbitrary //! sizes (only limited by your stack size). #![no_std] /// A stack-based linked list. /// /// This is a node in a traditional linked list. The main difference is that it does not box the /// next element, but simply points to it. As a result, it is harder to pass around or to mutate, /// but has the advantage of not requiring any heap allocation. #[derive(Debug, Clone, Copy, Eq, PartialEq)] pub enum Node<'a, T> { /// An empty list. Root, /// A node in the list with some data and a tail. Head { /// Some data attached on this node. This is the first element of this list. data: T, /// The rest of the list as a tail node. tail: &'a Node<'a, T>, }, } /// Convenient macro to define a stack list from a slice literal. /// /// Note that this will actually define a bunch of local stack variables (one per item in the /// list). This is _not_ an expression to define a standalone stack list, this would be impossible. /// /// # Examples /// /// ```rust /// stack_list::make!(let my_list = [1, 2, 3, 4]); /// assert_eq!(my_list.len(), 4); /// assert_eq!(my_list.get(3), Some(&4)); /// /// println!("{:?}", my_list); /// ``` #[macro_export] macro_rules! make { ( let $name:ident = [ $head:expr $(,)? ] ) => { let root = $crate::Node::Root; let $name = root.prepend($head); }; ( let $name:ident = [ $head:expr , $($tail:expr),* ] ) => { $crate::make!(let n = [ $($tail),* ] ); let $name = n.prepend($head); }; } impl<'a, T> Node<'a, T> { /// Create a new empty list. pub fn new() -> Self { Node::Root } /// Create a new list by prepending `self` with `data`. pub fn prepend(&'a self, data: T) -> Self { Node::Head { data, tail: self } } /// Returns an iterator on this list. pub fn iter(&'a self) -> impl Iterator<Item = &'a T> { self } /// Run the given closure on each item from this list, starting from the end. pub fn for_each_rev<F>(&self, mut f: F) where F: FnMut(&T), { self.for_each_rev_inner(&mut f); } /// Same as `for_each_rev`, but takes a `&mut F` so it can more easily recurse. fn for_each_rev_inner<F>(&self, f: &mut F) where F: FnMut(&T), { if let &Node::Head { ref data, ref tail } = self { tail.for_each_rev_inner(f); f(data); } } /// Returns the next element in the list, if any. pub fn tail(&self) -> Option<&Self> { match self { Node::Root => None, Node::Head { tail, .. } => Some(tail), } } /// Returns the first data element in this list, if any. pub fn first(&self) -> Option<&T> { match self { Node::Root => None, Node::Head { data, .. } => Some(&data), } } /// Returns the length of this list. pub fn len(&self) -> usize { self.tail().map_or(0, |t| 1 + t.len()) } /// Returns `true` if this list is empty. pub fn is_empty(&self) -> bool { match self { Node::Root => true, _ => false, } } /// Returns a sublist made by skipping the `n` first elements. /// /// Returns `None` if `n >= self.len()`. pub fn skip(&self, n: usize) -> Option<&Self> { match n { 0 => Some(self), n => self.tail().and_then(|t| t.skip(n - 1)), } } /// Returns the data at index `n`. /// /// Returns `None` if `n >= self.len()`. pub fn get(&self, n: usize) -> Option<&T> { self.skip(n).and_then(Node::first) } /// Returns the data for the last item in this list. /// /// Returns `None` if `self` is empty. pub fn last(&self) -> Option<&T> { match self { Node::Root => None, Node::Head { data, tail } => Some(tail.last().unwrap_or(data)), } } /// Fold the given function over this list. /// /// Conceptually, runs the code: /// /// ```rust,ignore /// for data in self { /// start = f(start, data); /// } /// start /// ``` pub fn fold<F, S>(&self, start: S, mut f: F) -> S where F: FnMut(S, T) -> S, T: Clone, { match self { Node::Root => start, Node::Head { tail, data } => tail.fold(f(start, data.clone()), f), } } /// Reverse this list. /// /// This does not return a stacklist; instead, it runs `f` on a reversed version of `self`. pub fn reverse<F, R>(&self, f: F) -> R where F: for<'b> FnOnce(&'b Node<'b, T>) -> R, R: 'static, T: Clone, { fn reverse_inner<'b, T, F, R>(target: &Node<'b, T>, source: &Node<'b, T>, f: F) -> R where F: for<'c> FnOnce(&'c Node<'c, T>) -> R, R: 'static, T: Clone, { match source { Node::Root => f(target), Node::Head { data, tail } => reverse_inner(&target.prepend(data.clone()), tail, f), } } reverse_inner(&Node::Root, self, f) } /// Prepend all the elements from the given iterator, in reverse order. /// /// # Examples /// /// ```rust /// stack_list::make!(let a = [3, 4, 5]); /// assert_eq!(a.get(0), Some(&3)); /// /// a.prepend_all_rev([2, 1].iter().copied(), |b| { /// assert_eq!(b.get(0), Some(&1)); /// }); /// ``` pub fn prepend_all_rev<I, F, R>(&self, mut i: I, f: F) -> R where I: Iterator<Item = T>, F: for<'b> FnOnce(&'b Node<'b, T>) -> R, R: 'static, { match i.next() { None => f(self), Some(x) => self.prepend(x).prepend_all_rev(i, f), } } /// Prepend all the elements from the given iterator. /// /// # Examples /// /// ```rust /// stack_list::make!(let a = [3, 4, 5]); /// assert_eq!(a.get(0), Some(&3)); /// /// a.prepend_all([1, 2].iter().copied(), |b| { /// assert_eq!(b.get(0), Some(&1)); /// }); /// ``` pub fn prepend_all<I, F, R>(&self, i: I, f: F) -> R where I: DoubleEndedIterator<Item = T>, F: for<'b> FnOnce(&'b Node<'b, T>) -> R, R: 'static, { self.prepend_all_rev(i.rev(), f) } /// Build a stacklist using the items from the iterator in reverse order. /// /// Note: this does not return the stacklist. Instead, it calls the given closure /// with the generated list. pub fn from_rev_iterator<I, F, R>(i: I, f: F) -> R where I: Iterator<Item = T>, F: for<'b> FnOnce(&'b Node<'b, T>) -> R, R: 'static, { Node::prepend_all_rev(&Node::Root, i, f) } /// Build a stacklist using the items from the iterator. /// /// Note: this does not return the stacklist. Instead, it calls the given closure /// with the generated list. pub fn from_iterator<I, F, R>(i: I, f: F) -> R where I: DoubleEndedIterator<Item = T>, F: for<'b> FnOnce(&'b Node<'b, T>) -> R, R: 'static, { Node::from_rev_iterator(i.rev(), f) } } impl<'b, T> Iterator for &'b Node<'b, T> { type Item = &'b T; fn next(&mut self) -> Option<Self::Item> { match self { Node::Root => None, Node::Head { data, tail } => { *self = tail; Some(&data) } } } }