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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//! A fairly straight forward but flexible implementation of a Linked List.
//! No reason to use this over the standard libraries implementation though.

#![warn(missing_docs)]

pub mod iter;

use std::{fmt::Debug, ptr::NonNull};

use crate::iter::{Iter, IterMut};

#[derive(Debug, Clone)]
struct Node<T> {
    next: LinkedList<T>,
    value: T,
}

impl<T> Node<T> {
    const fn new(value: T) -> Self {
        Self {
            next: LinkedList::new(),
            value,
        }
    }
}

/// `LinkedList` is an implementation of a singly-linked-list.
#[derive(Clone)]
pub struct LinkedList<T>(pub(crate) Option<NonNull<Node<T>>>);

impl<T> LinkedList<T> {
    /// Create a new empty linked list
    pub const fn new() -> Self {
        Self(None)
    }

    /// Get the length of the linked list.
    /// This is an O(n) computation
    pub fn len(&self) -> usize {
        self.0
            .as_ref()
            .map_or(0, |node| unsafe { node.as_ref().next.len() + 1 })
    }

    /// Determine if this linked list is empty.
    /// This is an O(1) computation
    pub fn is_empty(&self) -> bool {
        self.0.is_some()
    }

    /// Push to the front of the linked list.
    /// This is O(1)
    ///
    /// ```
    /// # use linked::LinkedList;
    /// let mut ll = LinkedList::new();
    /// ll.push_front(1);
    /// ll.push_front(2);
    /// assert_eq!(ll, LinkedList::from_iter([2, 1]))
    /// ```
    pub fn push_front(&mut self, value: T) {
        let node = Box::leak(Box::new(Node::new(value)));
        node.next = std::mem::replace(self, Self(Some(node.into())));
    }

    /// Pop from the front of the linked list.
    /// This is O(1)
    ///
    /// ```
    /// # use linked::LinkedList;
    /// let mut ll = LinkedList::from_iter([1, 2]);
    /// assert_eq!(ll.pop_front(), Some(1));
    /// assert_eq!(ll.pop_front(), Some(2));
    /// assert_eq!(ll.pop_front(), None);
    /// ```
    pub fn pop_front(&mut self) -> Option<T> {
        self.0.map(|node| unsafe {
            let node = Box::from_raw(node.as_ptr());
            *self = node.next;
            node.value
        })
    }

    /// View the first value in the linked list.
    /// This is O(1)
    pub fn first(&self) -> Option<&T> {
        self.0
            .map(|node| unsafe { node.as_ref() })
            .map(|node| &node.value)
    }

    /// Modify the first value in the linked list.
    /// This is O(1)
    pub fn first_mut(&mut self) -> Option<&mut T> {
        self.0
            .map(|mut node| unsafe { node.as_mut() })
            .map(|node| &mut node.value)
    }

    fn last_node_mut(&mut self) -> &mut Self {
        self.0.map_or(self, |mut node| unsafe {
            node.as_mut().next.last_node_mut()
        })
    }

    /// View the last value in the linked list.
    /// This is O(n)
    pub fn last(&self) -> Option<&T> {
        self.0.map(|node| unsafe {
            let node = node.as_ref();
            node.next.last().unwrap_or(&node.value)
        })
    }

    /// Modify the last value in the linked list.
    /// This is O(n)
    pub fn last_mut(&mut self) -> Option<&mut T> {
        self.0.map(|mut node| unsafe {
            let node = node.as_mut();
            node.next.last_mut().unwrap_or(&mut node.value)
        })
    }

    /// Push to the back of the linked list.
    /// This is O(n)
    ///
    /// ```
    /// # use linked::LinkedList;
    /// let mut ll = LinkedList::new();
    /// ll.push_back(1);
    /// ll.push_back(2);
    /// assert_eq!(ll, LinkedList::from_iter([1, 2]))
    /// ```
    pub fn push_back(&mut self, value: T) {
        self.extend(Some(value));
    }

    /// Pop from the back of the linked list.
    /// This is O(n)
    ///
    /// ```
    /// # use linked::LinkedList;
    /// let mut ll = LinkedList::from_iter([1, 2]);
    /// assert_eq!(ll.pop_back(), Some(2));
    /// assert_eq!(ll.pop_back(), Some(1));
    /// assert_eq!(ll.pop_back(), None);
    /// ```
    pub fn pop_back(&mut self) -> Option<T> {
        let node = self.0.take()?;
        let mut node = unsafe { Box::from_raw(node.as_ptr()) };
        match node.next.pop_back() {
            Some(t) => {
                self.0 = Some(Box::leak(node).into());
                Some(t)
            }
            None => {
                Some(node.value)
            }
        }
    }

    /// Create an iter over this linked list
    ///
    /// ```
    /// # use linked::LinkedList;
    /// let mut ll = LinkedList::from_iter([1, 2, 3]);
    /// assert_eq!(ll.iter().cloned().collect::<Vec<_>>(), vec![1, 2, 3]);
    /// ```
    pub const fn iter(&self) -> Iter<'_, T> {
        Iter(&self.0)
    }

    /// Create a mutable iter over this linked list
    ///
    /// ```
    /// # use linked::LinkedList;
    /// let mut ll = LinkedList::from_iter([1, 2, 3]);
    /// ll.iter_mut().for_each(|i| *i *= 2);
    /// assert_eq!(ll, LinkedList::from_iter([2, 4, 6]));
    /// ```
    pub fn iter_mut(&mut self) -> IterMut<'_, T> {
        IterMut(&mut self.0)
    }

    /// Add one linked list to the end of this linked list
    ///
    /// ```
    /// # use linked::LinkedList;
    /// let mut ll = LinkedList::from_iter(0..3);
    /// ll.append(LinkedList::from_iter(3..6));
    ///
    /// assert_eq!(ll, LinkedList::from_iter(0..6));
    /// ```
    pub fn append(&mut self, other: Self) {
        *self.last_node_mut() = other;
    }
}

impl<T: Debug> Debug for LinkedList<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_list().entries(self.iter()).finish()
    }
}

impl<T> FromIterator<T> for LinkedList<T> {
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
        let mut head = LinkedList::new();
        head.extend(iter);
        head
    }
}

impl<T> Extend<T> for LinkedList<T> {
    fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
        let mut end = self.last_node_mut();
        for v in iter {
            let node = Box::leak(Box::new(Node::new(v)));
            end.0 = Some(node.into());
            end = &mut node.next;
        }
    }
}

impl<T> Default for LinkedList<T> {
    fn default() -> Self {
        Self(None)
    }
}

impl<T, U> PartialEq<LinkedList<U>> for LinkedList<T>
where
    T: PartialEq<U>,
{
    fn eq(&self, other: &LinkedList<U>) -> bool {
        match (self.0, other.0) {
            (None, None) => true,
            (Some(a), Some(b)) => unsafe {
                a.as_ref().eq(b.as_ref())
            },
            _ => false,
        }
    }
}

impl<T, U> PartialEq<Node<U>> for Node<T>
where
    T: PartialEq<U>,
{
    fn eq(&self, other: &Node<U>) -> bool {
        self.value == other.value && self.next == other.next
    }
}

impl<T> Drop for LinkedList<T> {
    fn drop(&mut self) {
        self.0.map(|node| unsafe {
            Box::from_raw(node.as_ptr())
        });
    }
}

#[cfg(test)]
mod tests {
    use std::fmt::{Debug};

    use crate::LinkedList;

    #[test]
    fn push() {
        let mut ll = crate::LinkedList::new();

        ll.push_front(1);
        ll.push_front(2);
        ll.push_front(3);

        assert_eq!(ll.len(), 3);

        assert_eq!(ll.pop_front(), Some(3));
        assert_eq!(ll.pop_front(), Some(2));
        assert_eq!(ll.pop_front(), Some(1));

        assert_eq!(ll.len(), 0);

        assert_eq!(ll.pop_front(), None);
        assert_eq!(ll.len(), 0);
    }

    #[test]
    fn debug() {
        let mut ll = crate::LinkedList::new();
        assert_eq!(format!("{:?}", ll), "[]");

        ll.extend([1, 2, 3]);

        assert_eq!(format!("{:?}", ll), "[1, 2, 3]");

        assert_eq!(
            format!("{:#?}", ll),
            r"[
    1,
    2,
    3,
]"
        );
    }

    struct DropCheck<T>(T, Box<dyn FnMut()>);
    impl<T> Drop for DropCheck<T> {
        fn drop(&mut self) {
            self.1()
        }
    }
    impl<T: Debug> Debug for DropCheck<T> {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            write!(f, "{:?}", self.0)
        }
    }

    #[test]
    fn drop_check() {
        let td = testdrop::TestDrop::new();
        let ll: LinkedList<_> = (0..10).map(|_| td.new_item().1).collect();

        assert_eq!(td.num_tracked_items(), 10);
        assert_eq!(td.num_dropped_items(), 0);

        drop(ll);

        assert_eq!(td.num_dropped_items(), 10);
    }
}