deepmesa_collections/linkedlist/
list.rs

1/*
2   Linked List: A fast and flexible doubly linked list that
3   allows for O(1) inserts and removes from the middle of the
4   list. This list preallocates memory and doesn't have to allocate
5   and deallocate memory on every insert / remove operation
6
7   Copyright 2021 "Rahul Singh <rsingh@arrsingh.com>"
8
9   Licensed under the Apache License, Version 2.0 (the "License");
10   you may not use this file except in compliance with the License.
11   You may obtain a copy of the License at
12
13       http://www.apache.org/licenses/LICENSE-2.0
14
15   Unless required by applicable law or agreed to in writing, software
16   distributed under the License is distributed on an "AS IS" BASIS,
17   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   See the License for the specific language governing permissions and
19   limitations under the License.
20*/
21
22use crate::linkedlist::{fl, iter::Iter, iter::IterMut, node::InternalNode, node::NodeHandle};
23use core::ptr;
24
25macro_rules! nid_inc {
26    ($nid: expr) => {{
27        let nid = $nid;
28        $nid += 1;
29        nid
30    }};
31}
32
33/// A [fast doubly linked
34/// list](https://www.arrsingh.com/tag/linkedlist/) that
35/// owns the nodes and pre-allocates memory for performance.
36///
37/// The API is the same as [`std::collections::LinkedList`] however
38/// this list also allows pushing and popping elements from the middle
39/// of the list in constant time.
40///
41/// This list also provides handles to individual nodes that remain
42/// valid even if the list is mutated.
43///
44/// # Getting Started
45///
46/// ```
47/// use deepmesa_collections::LinkedList;
48///
49/// let mut list = LinkedList::<u8>::with_capacity(10);
50/// for i in 0..10 {
51///     list.push_front(i);
52/// }
53///
54/// for e in list.iter() {
55///     println!("{}", e);
56/// }
57/// ```
58///
59/// # Memory Management
60///
61/// This list manages memory via an internal freelist of nodes. When a
62/// new element is added to the list, a preallocated node is acquired
63/// from the freelist. When an element is removed from the list, the
64/// node is returned to the freelist. This ensures that memory is not
65/// allocated and deallocated on every push and pop which makes the
66/// list fast.
67///
68/// All memory for the list is allocated on the heap using the default
69/// allocator. Additional memory is allocated by the freelist when a
70/// new element is added to the list and the capacity is filled.
71///
72/// When the list is dropped, all memory is deallocated and any
73/// elements stored in the list are dropped. If the [`Drop`](Drop)
74/// trait on an element panics the list will deallocate all allocated
75/// memory because elements are removed from the list and dropped only
76/// after all memory is deallocated.
77///
78/// # Capacity & Reallocation
79///
80/// The capacity of the list is the number of elements it can hold
81/// before allocating new memory. The length of the list is the number
82/// of elements it holds. When the length equals the capacity, and a
83/// new element is added to the list, the list will allocate
84/// additional memory.
85///
86/// The amount of memory allocated when the capacity is exhausted
87/// depends on how the list is constructed. If the list is constructed
88/// using [`new()`](#method.new) or [`with_capacity()`](#method.with_capacity) with a
89/// non-zero capacity then the capacity is doubled on every
90/// allocation.
91///
92/// If the list is constructed using [`with_capacity()`](#method.with_capacity)
93/// with a capacity of zero, then the list will not preallocate any
94/// memory on construction. In this case, when a new element is added
95/// to the list, additional memory will be allocated for the new
96/// elememt unless the freelist has available memory from previous
97/// remove operations.
98///
99/// ```
100/// use deepmesa_collections::LinkedList;
101/// // create a list with capacity 0
102/// let mut list = LinkedList::<u8>::with_capacity(0);
103/// assert_eq!(list.len(), 0);
104/// assert_eq!(list.capacity(), 0);
105/// // Pushing elements will cause an allocation every time
106/// for i in 0..10 {
107///     assert_eq!(list.len(), i);
108///     assert_eq!(list.capacity(), i);
109///     list.push_head(1);
110/// }
111///
112/// // Removing an element will not cause a deallocation
113/// list.pop_head();
114/// assert_eq!(list.len(), 9);
115/// assert_eq!(list.capacity(), 10);
116///
117/// // Now that capacity exceeds the length of the list no memory will
118/// // be allocated unless existing capacity is exhausted
119/// list.push_head(1);
120/// assert_eq!(list.len(), 10);
121/// assert_eq!(list.capacity(), 10);
122///
123/// // any further additions to the list will again allocate new
124/// // memory for each element added.
125/// list.push_head(1);
126/// assert_eq!(list.len(), 11);
127/// assert_eq!(list.capacity(), 11);
128/// ```
129///
130/// It is recommended to use [`with_capacity()`](`#method.with_capacity`)
131/// whenever possible and specify how big the list is expected to get.
132///
133/// # Handles
134///
135/// The [`push_head()`](#method.push_head), [`push_tail()`](#method.push_tail)
136/// [`push_next()`](#method.push_next) and [`push_prev()`](#method.push_prev) methods
137/// return handles to the nodes pushed to the linked list. The handles
138/// are implemented as structs of type [`NodeHandle<T>`](NodeHandle) that wrap a
139/// raw pointer to node. However since [`NodeHandle<T>`](NodeHandle) does not
140/// implement the [`Deref`](https://doc.rust-lang.org/std/ops/trait.Deref.html) trait, these raw pointers cannot be
141/// dereferenced directly. Handles can only be used by passing them as
142/// arguments to the [`next()`](#method.next), [`next_mut()`](#method.next_mut),
143/// [`prev()`](#method.prev), [`prev_mut()`](#method.prev_mut),
144/// [`prev_node()`](#method.prev_node), [`next_node()`](#method.next_node),
145/// [`node()`](#method.node), [`node_mut()`](#method.node_mut),
146/// [`has_next()`](#method.has_next), [`has_prev()`](#method.has_prev),
147/// [`pop_next()`](#method.pop_next), [`pop_prev()`](#method.pop_prev),
148/// [`pop_node`](#method.pop_node), [`push_next`](#method.push_next),
149/// [`push_prev()`](#method.push_prev), allows adding, removing and mutating
150/// elements in the middle of the list in O(1) time.
151///
152/// Handles can be copied, cloned and passed around by value or
153/// reference without regard to the lifetime of the list. When an
154/// element is removed from the list, the handle associated with that
155/// node becomes invalid forever. Passing an invalid handle to the
156/// list is safe since all methods that accept a reference to a handle
157/// return None if the handle is invalid.
158///
159/// ```
160/// use deepmesa_collections::LinkedList;
161/// let mut list = LinkedList::<u8>::with_capacity(10);
162/// list.push_head(1);
163/// let middle = list.push_head(100);
164/// list.push_head(2);
165/// // get the value of the node in the middle of the list in O(1)
166/// // time.
167/// assert_eq!(list.node(&middle), Some(&100));
168/// // remove the middle node in O(1) time
169/// list.pop_node(&middle);
170/// // once the middle node is removed, the handle is invalid
171/// assert_eq!(list.node(&middle), None);
172/// assert_eq!(list.len(), 2);
173/// ```
174///
175/// [`NodeHandle<T>`](NodeHandle) implements the [`Default`](Default) trait so you
176/// can store default (invalid) handles in a struct and assign them
177/// later.
178///
179/// ```
180/// use deepmesa_collections::LinkedList;
181/// use deepmesa_collections::linkedlist::NodeHandle;
182/// struct MyStruct<T> {
183///     handle: NodeHandle<T>
184/// };
185/// let mut s = MyStruct::<u8> {
186///     handle: NodeHandle::<u8>::default()
187/// };
188/// let mut list = LinkedList::<u8>::with_capacity(10);
189/// // The default handle is invalid
190/// assert_eq!(list.node(&s.handle), None);
191/// // push a new element and store the handle
192/// s.handle = list.push_head(1);
193/// assert_eq!(list.node(&s.handle), Some(&1));
194/// ```
195/// # Iterators
196///
197
198/// The list supports iterators that can traverse the list in either
199/// direction by reversing the iterator at any time.
200///
201/// ```
202/// use deepmesa_collections::LinkedList;
203/// let mut list = LinkedList::<u8>::with_capacity(10);
204/// for i in 0..10 {
205///     list.push_head(i);
206/// }
207/// let mut iter = list.iter();
208/// assert_eq!(iter.next(), Some(&9));
209/// assert_eq!(iter.next(), Some(&8));
210/// assert_eq!(iter.next(), Some(&7));
211/// //now reverse the iterator
212/// iter = iter.reverse();
213/// assert_eq!(iter.next(), Some(&8));
214/// assert_eq!(iter.next(), Some(&9));
215/// assert_eq!(iter.next(), None);
216/// ```
217///
218/// # Performance
219///
220/// Benchmarks against [`std::collections::LinkedList`] show a
221/// performance gain of almost 2x in push operations when memory is
222/// allocated upfront. Similar results are observed in pop operations
223/// as well.
224#[derive(Debug)]
225pub struct LinkedList<T> {
226    cid: usize,
227    nid: usize,
228    pub(super) head: *mut InternalNode<T>,
229    pub(super) tail: *mut InternalNode<T>,
230    len: usize,
231    fl: fl::FreeList<T>,
232}
233
234fn inc_cid() -> usize {
235    unsafe {
236        static mut LL_COUNTER: usize = 0;
237        LL_COUNTER += 1;
238        return LL_COUNTER;
239    }
240}
241
242impl<T> LinkedList<T> {
243    /// Creates an empty linked list with a
244    /// default capacity.
245    ///
246    /// # Examples
247    ///
248    /// ```
249    /// use deepmesa_collections::LinkedList;
250    /// let list = LinkedList::<u8>::new();
251    /// ```
252    pub fn new() -> LinkedList<T> {
253        LinkedList {
254            cid: inc_cid(),
255            nid: 0,
256            len: 0,
257            head: ptr::null_mut(),
258            tail: ptr::null_mut(),
259            fl: fl::FreeList::new(8),
260        }
261    }
262
263    /// Creates an empty linked list with the specified capacity. The
264    /// list will continue to reallocate additional memory by doubling
265    /// the capacity everytime the capacity is exceeded.
266    ///
267    /// However the list will not deallocate memory when elements are
268    /// removed.
269    ///
270    /// If the capacity is set to 0, and the list is full, then new
271    /// memory will be allocated for one new element everytime an
272    /// element is added to the list.
273    ///
274    /// # Examples
275    ///
276    /// ```
277    /// use deepmesa_collections::LinkedList;
278    /// let mut list = LinkedList::<u8>::with_capacity(10);
279    /// for i in 0..10 {
280    ///     // All these are pushed without any allocations
281    ///     list.push_front(i);
282    /// }
283    ///
284    /// assert_eq!(list.len(), 10);
285    /// assert_eq!(list.capacity(), 10);
286    ///
287    /// // This will result in an allocation and the capacity will be doubled
288    /// list.push_front(1);
289    /// assert_eq!(list.len(), 11);
290    /// assert_eq!(list.capacity(), 20);
291    ///
292    /// // A list with a capacity of 0 will allocate on every push
293    /// let mut list = LinkedList::<u8>::with_capacity(0);
294    /// list.push_front(1);
295    /// assert_eq!(list.len(), 1);
296    /// assert_eq!(list.capacity(), 1);
297    ///
298    /// list.push_front(1);
299    /// assert_eq!(list.len(), 2);
300    /// assert_eq!(list.capacity(), 2);
301    /// ```
302    pub fn with_capacity(capacity: usize) -> LinkedList<T> {
303        LinkedList {
304            cid: inc_cid(),
305            nid: 0,
306            len: 0,
307            head: ptr::null_mut(),
308            tail: ptr::null_mut(),
309            fl: fl::FreeList::new(capacity),
310        }
311    }
312
313    /// Returns a bidirectional iterator over the list
314    ///
315    /// # Examples
316    /// ```
317    /// use deepmesa_collections::LinkedList;
318    /// let mut list = LinkedList::<u8>::new();
319    /// list.push_front(1);
320    /// list.push_front(2);
321    /// list.push_front(3);
322    /// list.push_front(4);
323    /// list.push_front(5);
324    ///
325    /// let mut iter = list.iter();
326    /// assert_eq!(iter.next(), Some(&5));
327    /// assert_eq!(iter.next(), Some(&4));
328    /// assert_eq!(iter.next(), Some(&3));
329    /// iter = iter.reverse();
330    /// assert_eq!(iter.next(), Some(&4));
331    /// assert_eq!(iter.next(), Some(&5));
332    /// assert_eq!(iter.next(), None);
333    /// ```
334    pub fn iter(&self) -> Iter<T> {
335        Iter::new(self)
336    }
337
338    /// Returns a bidirectional iterator over the list with mutable
339    /// references that allows the value to be modified
340    ///
341    /// # Examples
342    /// ```
343    /// use deepmesa_collections::LinkedList;
344    /// let mut list = LinkedList::<u8>::new();
345    /// list.push_front(1);
346    /// list.push_front(2);
347    /// list.push_front(3);
348    ///
349    /// for e in list.iter_mut() {
350    ///     *e += 100;
351    /// }
352    ///
353    /// let mut iter = list.iter();
354    /// assert_eq!(iter.next(), Some(&103));
355    /// assert_eq!(iter.next(), Some(&102));
356    /// assert_eq!(iter.next(), Some(&101));
357    /// assert_eq!(iter.next(), None);
358    /// ```
359    pub fn iter_mut(&mut self) -> IterMut<T> {
360        IterMut::new(self)
361    }
362
363    /// Removes and drops all the elements from this list. This has no
364    /// effect on the allocated capacity of the list.
365    ///
366    /// This method should complete in *O*(*n*) time.
367    ///
368    /// # Examples
369    /// ```
370    /// use deepmesa_collections::LinkedList;
371    /// let mut list = LinkedList::<u8>::with_capacity(10);
372    /// list.push_front(1);
373    /// list.push_front(2);
374    /// list.push_front(3);
375    ///
376    /// assert_eq!(list.len(), 3);
377    /// assert_eq!(list.capacity(), 10);
378    ///
379    /// list.clear();
380    /// assert_eq!(list.len(), 0);
381    /// assert_eq!(list.capacity(), 10);
382    /// ```
383    pub fn clear(&mut self) {
384        let mut cur: *mut InternalNode<T> = self.head;
385        while !cur.is_null() {
386            let node = self.pop_ptr(cur);
387            drop(node);
388            cur = self.head;
389        }
390    }
391
392    /// Returns a reference to the front (head) of the list or `None`
393    /// if the list is empty. This method simply calls
394    /// [`self.head()`](#method.head)
395    ///
396    /// This method should complete in *O*(*1*) time.
397    ///
398    /// # Examples
399    /// ```
400    /// use deepmesa_collections::LinkedList;
401    /// let mut list = LinkedList::<u8>::with_capacity(10);
402    /// assert_eq!(list.front(), None);
403    ///
404    /// list.push_front(1);
405    /// assert_eq!(list.front(), Some(&1));
406    /// ```
407    pub fn front(&self) -> Option<&T> {
408        self.head()
409    }
410
411    /// Returns a reference to the back (tail) of the list or `None`
412    /// if the list is empty. This method simply calls
413    /// [`self.tail()`](#method.tail)
414    ///
415    /// This method should complete in *O*(*1*) time.
416    ///
417    /// # Examples
418    /// ```
419    /// use deepmesa_collections::LinkedList;
420    /// let mut list = LinkedList::<u8>::with_capacity(10);
421    /// assert_eq!(list.back(), None);
422    ///
423    /// list.push_back(1);
424    /// assert_eq!(list.back(), Some(&1));
425    /// ```
426    pub fn back(&self) -> Option<&T> {
427        self.tail()
428    }
429
430    /// Returns a mutable reference to the front (head) of the list or
431    /// `None` if the list is empty. This method simply calls
432    /// [`self.head_mut()`](#method.head_mut)
433    ///
434    /// This method should complete in *O*(*1*) time.
435    ///
436    /// # Examples
437    /// ```
438    /// use deepmesa_collections::LinkedList;
439    /// let mut list = LinkedList::<u8>::with_capacity(10);
440    /// assert_eq!(list.front(), None);
441    ///
442    /// list.push_front(1);
443    /// assert_eq!(list.front(), Some(&1));
444    /// match list.front_mut() {
445    ///     None => {},
446    ///     Some(x) => *x = 5,
447    /// }
448    /// assert_eq!(list.front(), Some(&5));
449    /// ```
450    pub fn front_mut(&mut self) -> Option<&mut T> {
451        self.head_mut()
452    }
453
454    /// Returns a mutable reference to the back (tail) of the list or
455    /// `None` if the list is empty. This method simply calls
456    /// [`self.tail_mut()`](#method.tail_mut)
457    ///
458    /// This method should complete in *O*(*1*) time.
459    ///
460    /// # Examples
461    /// ```
462    /// use deepmesa_collections::LinkedList;
463    /// let mut list = LinkedList::<u8>::with_capacity(10);
464    /// assert_eq!(list.back(), None);
465    ///
466    /// list.push_back(1);
467    /// assert_eq!(list.back(), Some(&1));
468    /// match list.back_mut() {
469    ///     None => {},
470    ///     Some(x) => *x = 5,
471    /// }
472    /// assert_eq!(list.back(), Some(&5));
473    /// ```
474    pub fn back_mut(&mut self) -> Option<&mut T> {
475        self.tail_mut()
476    }
477
478    /// Returns a reference to the back (tail) of the list or `None`
479    /// if the list is empty.
480    ///
481    /// This method should complete in *O*(*1*) time.
482    ///
483    /// # Examples
484    /// ```
485    /// use deepmesa_collections::LinkedList;
486    /// let mut list = LinkedList::<u8>::with_capacity(10);
487    /// assert_eq!(list.tail(), None);
488    ///
489    /// list.push_tail(1);
490    /// assert_eq!(list.tail(), Some(&1));
491    /// ```
492    pub fn tail(&self) -> Option<&T> {
493        if self.head.is_null() {
494            return None;
495        }
496
497        unsafe { Some(&(*self.tail).val) }
498    }
499
500    /// Returns a mutable reference to the back (tail) of the list or `None`
501    /// if the list is empty.
502    ///
503    /// This method should complete in *O*(*1*) time.
504    ///
505    /// # Examples
506    /// ```
507    /// use deepmesa_collections::LinkedList;
508    /// let mut list = LinkedList::<u8>::with_capacity(10);
509    /// assert_eq!(list.tail(), None);
510    ///
511    /// list.push_tail(1);
512    /// assert_eq!(list.tail(), Some(&1));
513    /// match list.tail_mut() {
514    ///     None => {},
515    ///     Some(x) => *x = 5,
516    /// }
517    /// assert_eq!(list.tail(), Some(&5));
518    /// ```
519    pub fn tail_mut(&mut self) -> Option<&mut T> {
520        if self.tail.is_null() {
521            return None;
522        }
523        unsafe { Some(&mut (*self.tail).val) }
524    }
525
526    /// Returns a reference to the front (head) of the list or `None`
527    /// if the list is empty.
528    ///
529    /// This method should complete in *O*(*1*) time.
530    ///
531    /// # Examples
532    /// ```
533    /// use deepmesa_collections::LinkedList;
534    /// let mut list = LinkedList::<u8>::with_capacity(10);
535    /// assert_eq!(list.head(), None);
536    ///
537    /// list.push_head(1);
538    /// assert_eq!(list.head(), Some(&1));
539    /// ```
540    pub fn head(&self) -> Option<&T> {
541        if self.head.is_null() {
542            return None;
543        }
544
545        unsafe { Some(&(*self.head).val) }
546    }
547
548    /// Returns a mutable reference to the front (head) of the list or `None`
549    /// if the list is empty.
550    ///
551    /// This method should complete in *O*(*1*) time.
552    ///
553    /// # Examples
554    /// ```
555    /// use deepmesa_collections::LinkedList;
556    /// let mut list = LinkedList::<u8>::with_capacity(10);
557    /// assert_eq!(list.head(), None);
558    ///
559    /// list.push_head(1);
560    /// assert_eq!(list.head(), Some(&1));
561    /// match list.head_mut() {
562    ///     None => {},
563    ///     Some(x) => *x = 5,
564    /// }
565    /// assert_eq!(list.head(), Some(&5));
566    /// ```
567    pub fn head_mut(&mut self) -> Option<&mut T> {
568        if self.head.is_null() {
569            return None;
570        }
571
572        unsafe { Some(&mut (*self.head).val) }
573    }
574
575    pub unsafe fn head_ptr(&self) -> *const T {
576        if self.head.is_null() {
577            return ptr::null();
578        }
579
580        return &(*self.head).val;
581    }
582
583    pub unsafe fn head_mut_ptr(&mut self) -> *mut T {
584        if self.head.is_null() {
585            return ptr::null_mut();
586        }
587
588        return &mut (*self.head).val;
589    }
590
591    /// Returns a reference to the value of the node immediately after
592    /// the node associated with the specified handle. If the
593    /// specified handle is invalid or there is no next node, this
594    /// method returns None.
595    ///
596    /// This method should complete in *O*(*1*) time.
597    ///
598    /// # Examples
599    /// ```
600    /// use deepmesa_collections::LinkedList;
601    /// let mut list = LinkedList::<u8>::with_capacity(10);
602    /// list.push_head(1);
603    /// let node = list.push_head(2);
604    ///
605    /// assert_eq!(list.next(&node), Some(&1));
606    ///
607    /// list.pop_tail();
608    /// // once the tail is popped, there is no next
609    /// assert_eq!(list.next(&node), None);
610    /// ```
611    pub fn next(&self, node: &NodeHandle<T>) -> Option<&T> {
612        match self.node_ptr(node) {
613            None => None,
614            Some(n_ptr) => unsafe {
615                if (*n_ptr).next.is_null() {
616                    return None;
617                }
618
619                Some(&(*(*n_ptr).next).val)
620            },
621        }
622    }
623
624    /// Returns a mutable reference to the value of the node
625    /// immediately after the node associated with the specified
626    /// handle. If the specified handle is invalid or if there is no
627    /// next node, this method returns None.
628    ///
629    /// This method should complete in *O*(*1*) time.
630    ///
631    /// # Examples
632    /// ```
633    /// use deepmesa_collections::LinkedList;
634    /// let mut list = LinkedList::<u8>::with_capacity(10);
635    /// list.push_head(1);
636    /// let node = list.push_head(2);
637    /// assert_eq!(list.next(&node), Some(&1));
638    ///
639    /// match list.next_mut(&node) {
640    ///     None => {},
641    ///     Some(x) => *x = 100,
642    /// }
643    ///
644    /// assert_eq!(list.next(&node), Some(&100));
645    /// ```
646    pub fn next_mut(&mut self, node: &NodeHandle<T>) -> Option<&mut T> {
647        match self.node_ptr(node) {
648            None => None,
649            Some(n_ptr) => unsafe {
650                if (*n_ptr).next.is_null() {
651                    return None;
652                }
653
654                Some(&mut (*(*n_ptr).next).val)
655            },
656        }
657    }
658
659    /// Returns a reference to the value of the node immediately
660    /// preceeding the node associated with the specified handle.  If
661    /// the specified handle is invalid or if there is no preceeding
662    /// node, this method returns None.
663    ///
664    /// This method should complete in *O*(*1*) time.
665    ///
666    /// # Examples
667    /// ```
668    /// use deepmesa_collections::LinkedList;
669    /// let mut list = LinkedList::<u8>::with_capacity(10);
670    /// let node = list.push_head(1);
671    /// list.push_head(2);
672    ///
673    /// assert_eq!(list.prev(&node), Some(&2));
674    ///
675    /// list.pop_head();
676    /// // once the head is popped, there is no prev
677    /// assert_eq!(list.prev(&node), None);
678    /// ```
679    pub fn prev(&self, node: &NodeHandle<T>) -> Option<&T> {
680        match self.node_ptr(node) {
681            None => None,
682            Some(n_ptr) => unsafe {
683                if (*n_ptr).prev.is_null() {
684                    return None;
685                }
686
687                Some(&(*(*n_ptr).prev).val)
688            },
689        }
690    }
691
692    /// Returns a mutable reference to the value of the node
693    /// immediately preceeding the node associated with the specified
694    /// handle. If the specified handle is invalid or there is no
695    /// preceeding node, this method returns None. This method should
696    /// complete in *O*(*1*) time.
697    ///
698    /// # Examples
699    /// ```
700    /// use deepmesa_collections::LinkedList;
701    /// let mut list = LinkedList::<u8>::with_capacity(10);
702    /// let node = list.push_head(1);
703    /// list.push_head(2);
704    /// assert_eq!(list.prev(&node), Some(&2));
705    ///
706    /// match list.prev_mut(&node) {
707    ///     None => {},
708    ///     Some(x) => *x = 100,
709    /// }
710    ///
711    /// assert_eq!(list.prev(&node), Some(&100));
712    /// ```
713    pub fn prev_mut(&mut self, node: &NodeHandle<T>) -> Option<&mut T> {
714        match self.node_ptr(node) {
715            None => None,
716            Some(n_ptr) => unsafe {
717                if (*n_ptr).prev.is_null() {
718                    return None;
719                }
720
721                Some(&mut (*(*n_ptr).prev).val)
722            },
723        }
724    }
725
726    /// Returns a handle to the node immediately preceeding the node
727    /// associated with the specified handle. If the specified handle
728    /// is invalid or if there is no preceeding node, this method
729    /// returns None.
730    ///
731    /// This method should complete in *O*(*1*) time.
732    ///
733    /// # Examples
734    /// ```
735    /// use deepmesa_collections::LinkedList;
736    /// let mut list = LinkedList::<u8>::with_capacity(10);
737    /// let node = list.push_head(1);
738    /// list.push_head(2);
739    ///
740    /// assert_eq!(list.prev(&node), Some(&2));
741    ///
742    /// list.pop_head();
743    /// //once the head is popped there is no prev node
744    /// assert_eq!(list.prev(&node), None);
745    /// ```
746    pub fn prev_node(&self, node: &NodeHandle<T>) -> Option<NodeHandle<T>> {
747        match self.node_ptr(node) {
748            None => None,
749            Some(n_ptr) => unsafe {
750                if (*n_ptr).prev.is_null() {
751                    return None;
752                }
753
754                Some(NodeHandle::new(
755                    self.cid,
756                    (*(*n_ptr).prev).nid,
757                    (*n_ptr).prev,
758                ))
759            },
760        }
761    }
762
763    /// Returns a handle to the node immediately preceeding the node
764    /// associated with the specified handle. If the handle is invalid
765    /// or if there is no preceeding node, this method returns
766    /// None.
767    ///
768    /// This method should complete in *O*(*1*) time.
769    ///
770    /// # Examples
771    /// ```
772    /// use deepmesa_collections::LinkedList;
773    /// let mut list = LinkedList::<u8>::with_capacity(10);
774    /// list.push_head(1);
775    /// let node = list.push_head(2);
776    ///
777    /// match list.next_node(&node) {
778    ///     None => {},
779    ///     Some(n) => assert_eq!(list.node(&n), Some(&1)),
780    /// }
781    ///
782    /// list.pop_tail();
783    /// // Once the tail node is popped, there is no next node
784    /// assert_eq!(list.next_node(&node), None);
785    /// ```
786    pub fn next_node(&self, node: &NodeHandle<T>) -> Option<NodeHandle<T>> {
787        match self.node_ptr(node) {
788            None => None,
789            Some(n_ptr) => unsafe {
790                if (*n_ptr).next.is_null() {
791                    return None;
792                }
793
794                Some(NodeHandle::new(
795                    self.cid,
796                    (*(*n_ptr).next).nid,
797                    (*n_ptr).next,
798                ))
799            },
800        }
801    }
802
803    /// Returns a reference to the value of the node associated with
804    /// the specified handle.  If the specified handle is invalid this
805    /// method returns None.
806    ///
807    /// This method should complete in *O*(*1*) time.
808    ///
809    /// # Examples
810    /// ```
811    /// use deepmesa_collections::LinkedList;
812    /// let mut list = LinkedList::<u8>::with_capacity(10);
813    /// let node = list.push_head(1);
814    ///
815    /// assert_eq!(list.node(&node), Some(&1));
816    ///
817    /// list.pop_head();
818    /// // once the node is popped the handle becomes invalid
819    /// assert_eq!(list.node(&node), None);
820    /// ```
821    pub fn node(&self, node: &NodeHandle<T>) -> Option<&T> {
822        match self.node_ptr(node) {
823            None => None,
824            Some(n_ptr) => unsafe { Some(&(*n_ptr).val) },
825        }
826    }
827
828    /// Returns a mutable reference to the value of the node associated with
829    /// the specified handle.  If the specified handle is invalid this
830    /// method returns None.
831    ///
832    /// This method should complete in *O*(*1*) time.
833    ///
834    /// # Examples
835    /// ```
836    /// use deepmesa_collections::LinkedList;
837    /// let mut list = LinkedList::<u8>::with_capacity(10);
838    /// let node = list.push_head(1);
839    ///
840    /// assert_eq!(list.node(&node), Some(&1));
841    ///
842    /// match list.node_mut(&node) {
843    ///     None => {},
844    ///     Some(x) => *x = 100,
845    /// }
846    ///
847    /// assert_eq!(list.node(&node), Some(&100));
848    /// ```
849    pub fn node_mut(&mut self, node: &NodeHandle<T>) -> Option<&mut T> {
850        match self.node_ptr(node) {
851            None => None,
852            Some(n_ptr) => unsafe { Some(&mut (*n_ptr).val) },
853        }
854    }
855
856    /// Replaces the value of the node associated withe the specified
857    /// handle and returns the old value. If the node doesn't exist or
858    /// the handle is invalid then this method returns None (no value
859    /// is replaced).
860    ///
861    /// Tthis method should complete in *O*(*1*) time.
862    ///
863    /// # Examples
864    /// ```
865    /// use deepmesa_collections::LinkedList;
866    /// let mut list = LinkedList::<u8>::with_capacity(10);
867    /// let node = list.push_head(1);
868    ///
869    /// let old_val = list.replace(&node, 2);
870    /// assert_eq!(old_val, Some(1));
871    /// assert_eq!(list.node(&node), Some(&2));
872    /// ```
873    pub fn replace(&mut self, node: &NodeHandle<T>, val: T) -> Option<T> {
874        match self.node_ptr(node) {
875            None => {
876                return None;
877            }
878
879            Some(n_ptr) => unsafe {
880                let old_v = std::mem::replace(&mut (*n_ptr).val, val);
881                return Some(old_v);
882            },
883        }
884    }
885
886    /// Returns a handle to the head (front) of the list or None if
887    /// the list is empty.
888    ///
889    /// This method should complete in *O*(*1*) time.
890    ///
891    /// # Examples
892    /// ```
893    /// use deepmesa_collections::LinkedList;
894    /// let mut list = LinkedList::<u8>::with_capacity(10);
895    /// let node = list.push_head(1);
896    ///
897    /// match list.head_node() {
898    ///     None => {},
899    ///     Some(x) => assert_eq!(list.node(&x), Some(&1)),
900    /// }
901    ///
902    /// list.pop_head();
903    /// assert_eq!(list.head_node(), None);
904    /// ```
905    pub fn head_node(&self) -> Option<NodeHandle<T>> {
906        if self.head.is_null() {
907            return None;
908        }
909
910        unsafe { Some(NodeHandle::new(self.cid, (*self.head).nid, self.head)) }
911    }
912
913    /// Returns a handle to the tail (back) of the list or None if the
914    /// list is empty.
915    ///
916    /// This method should complete in *O*(*1*) time.
917    ///
918    /// # Examples
919    /// ```
920    /// use deepmesa_collections::LinkedList;
921    /// let mut list = LinkedList::<u8>::with_capacity(10);
922    /// let node = list.push_tail(1);
923    ///
924    /// match list.tail_node() {
925    ///     None => {},
926    ///     Some(x) => assert_eq!(list.node(&x), Some(&1)),
927    /// }
928    ///
929    /// list.pop_tail();
930    /// assert_eq!(list.tail_node(), None);
931    /// ```
932    pub fn tail_node(&self) -> Option<NodeHandle<T>> {
933        if self.tail.is_null() {
934            return None;
935        }
936        unsafe { Some(NodeHandle::new(self.cid, (*self.tail).nid, self.tail)) }
937    }
938
939    /// Returns true if the node associated with the specified handle
940    /// has a next node and false if it does not. If the specified
941    /// handle is invalid this method returns None.
942    ///
943    /// This method should complete in *O*(*1*) time.
944    ///
945    /// # Examples
946    /// ```
947    /// use deepmesa_collections::LinkedList;
948    /// let mut list = LinkedList::<u8>::with_capacity(10);
949    /// let node1 = list.push_head(1);
950    /// let node2 = list.push_head(2);
951    ///
952    /// assert_eq!(list.has_next(&node1), Some(false));
953    /// assert_eq!(list.has_next(&node2), Some(true));
954    ///
955    /// list.pop_head();
956    /// assert_eq!(list.has_next(&node1), Some(false));
957    /// // once the head is popped node2 becomes an invalid handle
958    /// assert_eq!(list.has_next(&node2), None);
959    /// ```
960    pub fn has_next(&self, node: &NodeHandle<T>) -> Option<bool> {
961        match self.node_ptr(node) {
962            None => None,
963            Some(n_ptr) => unsafe {
964                if (*n_ptr).next.is_null() {
965                    Some(false)
966                } else {
967                    Some(true)
968                }
969            },
970        }
971    }
972
973    /// Returns true if the node associated with the specified handle
974    /// has a previous node and false if it does not. If the specified
975    /// handle is invalid this method returns None.
976    ///
977    /// This method should complete in *O*(*1*) time.
978    ///
979    /// # Examples
980    /// ```
981    /// use deepmesa_collections::LinkedList;
982    /// let mut list = LinkedList::<u8>::with_capacity(10);
983    /// let node1 = list.push_head(1);
984    /// let node2 = list.push_head(2);
985    ///
986    /// assert_eq!(list.has_prev(&node1), Some(true));
987    /// assert_eq!(list.has_prev(&node2), Some(false));
988    ///
989    /// list.pop_head();
990    /// assert_eq!(list.has_prev(&node1), Some(false));
991    /// // once the head is popped node2 becomes an invalid handle
992    /// assert_eq!(list.has_next(&node2), None);
993    /// ```
994    pub fn has_prev(&self, node: &NodeHandle<T>) -> Option<bool> {
995        match self.node_ptr(node) {
996            None => None,
997            Some(n_ptr) => unsafe {
998                if (*n_ptr).prev.is_null() {
999                    Some(false)
1000                } else {
1001                    Some(true)
1002                }
1003            },
1004        }
1005    }
1006
1007    /// Returns true if the list is empty and false otherwise.
1008    ///
1009    /// This method should complete in *O*(*1*) time.
1010    ///
1011    /// # Examples
1012    /// ```
1013    /// use deepmesa_collections::LinkedList;
1014    /// let mut list = LinkedList::<u8>::with_capacity(10);
1015    /// assert_eq!(list.is_empty(), true);
1016    ///
1017    /// list.push_head(1);
1018    /// assert_eq!(list.is_empty(), false);
1019    /// ```
1020    pub fn is_empty(&self) -> bool {
1021        self.len == 0
1022    }
1023
1024    /// Returns true if the list has a head node and false if the list
1025    /// is empty.
1026    ///
1027    /// This method should complete in *O*(*1*) time.
1028    ///
1029    /// # Examples
1030    /// ```
1031    /// use deepmesa_collections::LinkedList;
1032    /// let mut list = LinkedList::<u8>::with_capacity(10);
1033    /// assert_eq!(list.has_head(), false);
1034    /// list.push_head(1);
1035    /// assert_eq!(list.has_head(), true);
1036    /// list.pop_head();
1037    /// assert_eq!(list.has_head(), false);
1038    /// ```
1039    pub fn has_head(&self) -> bool {
1040        !self.head.is_null()
1041    }
1042
1043    /// Returns true if the list has a tail node and false if the list
1044    /// is empty.
1045    ///
1046    /// This method should complete in *O*(*1*) time.
1047    ///
1048    /// # Examples
1049    /// ```
1050    /// use deepmesa_collections::LinkedList;
1051    /// let mut list = LinkedList::<u8>::with_capacity(10);
1052    /// assert_eq!(list.has_tail(), false);
1053    /// list.push_tail(1);
1054    /// assert_eq!(list.has_tail(), true);
1055    /// list.pop_tail();
1056    /// assert_eq!(list.has_tail(), false);
1057    /// ```
1058    pub fn has_tail(&self) -> bool {
1059        !self.tail.is_null()
1060    }
1061
1062    /// Returns the number of elements the list can hold before new
1063    /// memory is allocated.
1064    /// # Examples
1065    /// ```
1066    /// use deepmesa_collections::LinkedList;
1067    /// let mut list = LinkedList::<u8>::with_capacity(10);
1068    /// assert_eq!(list.capacity(), 10);
1069    /// ```
1070    pub fn capacity(&self) -> usize {
1071        self.len() + self.fl.len()
1072    }
1073
1074    /// Returns the number of elements in the list
1075    ///
1076    /// This method should complete in *O*(*1*) time.
1077    ///
1078    /// # Examples
1079    /// ```
1080    /// use deepmesa_collections::LinkedList;
1081    /// let mut list = LinkedList::<u8>::with_capacity(10);
1082    /// assert_eq!(list.len(), 0);
1083    ///
1084    /// list.push_head(1);
1085    /// assert_eq!(list.len(), 1);
1086    /// ```
1087    pub fn len(&self) -> usize {
1088        self.len
1089    }
1090
1091    /// Adds an element to the front (head) of the list. This method
1092    /// simply calls [`self.push_head()`](#method.push_head)
1093    ///
1094    /// This operation should complete in *O*(*1*) time.
1095    ///
1096    /// # Examples
1097    /// ```
1098    /// use deepmesa_collections::LinkedList;
1099    /// let mut list = LinkedList::<u8>::with_capacity(10);
1100    /// list.push_front(1);
1101    /// assert_eq!(list.front(), Some(&1));
1102    ///
1103    /// list.push_front(2);
1104    /// assert_eq!(list.front(), Some(&2));
1105    /// ```
1106    pub fn push_front(&mut self, elem: T) {
1107        self.push_head(elem);
1108    }
1109
1110    /// Adds an element to the back (tail) of the list. This method
1111    /// simply calls [`self.push_tail()`](#method.push_tail)
1112    ///
1113    /// This operation should complete in *O*(*1*) time.
1114    ///
1115    /// # Examples
1116    /// ```
1117    /// use deepmesa_collections::LinkedList;
1118    /// let mut list = LinkedList::<u8>::with_capacity(10);
1119    /// list.push_back(1);
1120    /// assert_eq!(list.back(), Some(&1));
1121    ///
1122    /// list.push_back(2);
1123    /// assert_eq!(list.back(), Some(&2));
1124    /// ```
1125    pub fn push_back(&mut self, elem: T) {
1126        self.push_tail(elem);
1127    }
1128
1129    /// Removes and returns the value at the head (front) of the
1130    /// list or None if the list is empty.
1131    ///
1132    /// This operation should complete in *O*(*1*) time
1133    ///
1134    /// # Examples
1135    /// ```
1136    /// use deepmesa_collections::LinkedList;
1137    /// let mut list = LinkedList::<u8>::with_capacity(10);
1138    /// assert_eq!(list.pop_head(), None);
1139    ///
1140    /// list.push_head(1);
1141    /// list.push_head(2);
1142    /// assert_eq!(list.pop_head(), Some(2));
1143    /// assert_eq!(list.pop_head(), Some(1));
1144    /// assert_eq!(list.pop_head(), None);
1145    /// ```
1146    pub fn pop_head(&mut self) -> Option<T> {
1147        if self.head.is_null() {
1148            return None;
1149        }
1150        Some(self.pop_ptr(self.head))
1151    }
1152
1153    /// Removes and returns the value at the tail (back) of the
1154    /// list or None if the list is empty.
1155    ///
1156    /// This operation should complete in *O*(*1*) time
1157    ///
1158    /// # Examples
1159    /// ```
1160    /// use deepmesa_collections::LinkedList;
1161    /// let mut list = LinkedList::<u8>::with_capacity(10);
1162    /// assert_eq!(list.pop_tail(), None);
1163    ///
1164    /// list.push_tail(1);
1165    /// list.push_tail(2);
1166    /// assert_eq!(list.pop_tail(), Some(2));
1167    /// assert_eq!(list.pop_tail(), Some(1));
1168    /// assert_eq!(list.pop_tail(), None);
1169    /// ```
1170    pub fn pop_tail(&mut self) -> Option<T> {
1171        if self.tail.is_null() {
1172            return None;
1173        }
1174
1175        Some(self.pop_ptr(self.tail))
1176    }
1177
1178    /// Removes and returns the value at the front (head) of the list
1179    /// or None if the list is empty. This method simply calls
1180    /// [`self.pop_head()`](#method.pop_head)
1181    ///
1182    /// This operation should complete in *O*(*1*) time.
1183    ///
1184    /// # Examples
1185    /// ```
1186    /// use deepmesa_collections::LinkedList;
1187    /// let mut list = LinkedList::<u8>::with_capacity(10);
1188    /// assert_eq!(list.pop_front(), None);
1189    ///
1190    /// list.push_front(1);
1191    /// list.push_front(2);
1192    /// assert_eq!(list.pop_front(), Some(2));
1193    /// assert_eq!(list.pop_front(), Some(1));
1194    /// assert_eq!(list.pop_front(), None);
1195    /// ```
1196    pub fn pop_front(&mut self) -> Option<T> {
1197        self.pop_head()
1198    }
1199
1200    /// Removes and returns the value at the back (tail) of the list
1201    /// or None if the list is empty. This method simply calls
1202    /// [`self.pop_tail()`](#method.pop_tail)
1203    ///
1204    /// This operation should complete in *O*(*1*) time
1205    ///
1206    /// # Examples
1207    /// ```
1208    /// use deepmesa_collections::LinkedList;
1209    /// let mut list = LinkedList::<u8>::with_capacity(10);
1210    /// assert_eq!(list.pop_back(), None);
1211    ///
1212    /// list.push_back(1);
1213    /// list.push_back(2);
1214    /// assert_eq!(list.pop_back(), Some(2));
1215    /// assert_eq!(list.pop_back(), Some(1));
1216    /// assert_eq!(list.pop_back(), None);
1217    /// ```
1218    pub fn pop_back(&mut self) -> Option<T> {
1219        self.pop_tail()
1220    }
1221
1222    /// Removes and returns the value of the node immediately after
1223    /// the node associated with the specified handle. If the
1224    /// specified handle is invalid or there is no next node, then
1225    /// this method returns None.
1226    ///
1227    /// This operation should complete in *O*(*1*) time
1228    ///
1229    /// # Examples
1230    /// ```
1231    /// use deepmesa_collections::LinkedList;
1232    /// let mut list = LinkedList::<u8>::with_capacity(10);
1233    ///
1234    /// list.push_head(1);
1235    /// list.push_head(2);
1236    /// let node = list.push_head(3);
1237    /// assert_eq!(list.pop_next(&node), Some(2));
1238    /// assert_eq!(list.pop_next(&node), Some(1));
1239    /// assert_eq!(list.pop_next(&node), None);
1240    /// ```
1241    pub fn pop_next(&mut self, node: &NodeHandle<T>) -> Option<T> {
1242        match self.node_ptr(node) {
1243            None => None,
1244            Some(n_ptr) => unsafe {
1245                if (*n_ptr).next.is_null() {
1246                    return None;
1247                }
1248                Some(self.pop_ptr((*n_ptr).next))
1249            },
1250        }
1251    }
1252
1253    /// Removes and returns the value of the node immediately
1254    /// preceeding the node associated with the specified handle. If
1255    /// the specified handle is invalid or there is no previous node,
1256    /// then this method returns None.
1257    ///
1258    /// This operation should complete in *O*(*1*) time
1259    ///
1260    /// # Examples
1261    /// ```
1262    /// use deepmesa_collections::LinkedList;
1263    /// let mut list = LinkedList::<u8>::with_capacity(10);
1264    ///
1265    /// let node = list.push_head(1);
1266    /// list.push_head(2);
1267    /// list.push_head(3);
1268    /// assert_eq!(list.pop_prev(&node), Some(2));
1269    /// assert_eq!(list.pop_prev(&node), Some(3));
1270    /// assert_eq!(list.pop_prev(&node), None);
1271    /// ```
1272    pub fn pop_prev(&mut self, node: &NodeHandle<T>) -> Option<T> {
1273        match self.node_ptr(node) {
1274            None => None,
1275            Some(n_ptr) => unsafe {
1276                if (*n_ptr).prev.is_null() {
1277                    return None;
1278                }
1279
1280                Some(self.pop_ptr((*n_ptr).prev))
1281            },
1282        }
1283    }
1284
1285    /// Removes and returns the value of the node associated with the
1286    /// specified handle. If the specified handle is invalid then this
1287    /// method returns None.
1288    ///
1289    /// This operation should complete in *O*(*1*) time
1290    ///
1291    /// # Examples
1292    /// ```
1293    /// use deepmesa_collections::LinkedList;
1294    /// let mut list = LinkedList::<u8>::with_capacity(10);
1295    ///
1296    /// let node = list.push_head(1);
1297    /// assert_eq!(list.pop_node(&node), Some(1));
1298    /// assert_eq!(list.pop_node(&node), None);
1299    /// ```
1300    pub fn pop_node(&mut self, node: &NodeHandle<T>) -> Option<T> {
1301        match self.node_ptr(node) {
1302            None => None,
1303            Some(n_ptr) => Some(self.pop_ptr(n_ptr)),
1304        }
1305    }
1306
1307    /// Adds an element to the head (front) of the list and returns a
1308    /// handle to it.
1309    ///
1310    /// This operation should complete in *O*(*1*) time.
1311    ///
1312    /// # Examples
1313    /// ```
1314    /// use deepmesa_collections::LinkedList;
1315    /// let mut list = LinkedList::<u8>::with_capacity(10);
1316    /// let node = list.push_head(1);
1317    /// assert_eq!(list.node(&node), Some(&1));
1318    /// ```
1319    pub fn push_head(&mut self, elem: T) -> NodeHandle<T> {
1320        let nid = nid_inc!(self.nid);
1321        let raw_n = self.fl.acquire(elem, nid);
1322
1323        unsafe {
1324            if self.head.is_null() {
1325                (*raw_n).next = ptr::null_mut();
1326            } else {
1327                (*self.head).prev = raw_n;
1328                (*raw_n).next = self.head;
1329            }
1330
1331            if self.tail.is_null() {
1332                self.tail = raw_n;
1333            }
1334            self.head = raw_n;
1335        }
1336
1337        self.len += 1;
1338        NodeHandle::new(self.cid, nid, raw_n)
1339    }
1340
1341    /// Adds an element to the tail (back) of the list and returns a
1342    /// handle to it.
1343    ///
1344    /// This operation should complete in *O*(*1*) time.
1345    ///
1346    /// # Examples
1347    /// ```
1348    /// use deepmesa_collections::LinkedList;
1349    /// let mut list = LinkedList::<u8>::with_capacity(10);
1350    /// let node = list.push_tail(1);
1351    /// assert_eq!(list.node(&node), Some(&1));
1352    /// ```
1353    pub fn push_tail(&mut self, elem: T) -> NodeHandle<T> {
1354        let nid = nid_inc!(self.nid);
1355        let raw_n = self.fl.acquire(elem, nid);
1356
1357        unsafe {
1358            if self.tail.is_null() {
1359                (*raw_n).prev = ptr::null_mut();
1360            } else {
1361                (*self.tail).next = raw_n;
1362                (*raw_n).prev = self.tail;
1363            }
1364            if self.head.is_null() {
1365                self.head = raw_n;
1366            }
1367            self.tail = raw_n;
1368        }
1369        self.len += 1;
1370        NodeHandle::new(self.cid, nid, raw_n)
1371    }
1372
1373    /// Returns `true` if the `LinkedList` contains an element equal to the
1374    /// given value.
1375    ///
1376    /// This operation should complete in *O*(*n*) time
1377    ///
1378    /// # Examples
1379    ///
1380    /// ```
1381    /// use deepmesa_collections::LinkedList;
1382    /// let mut list = LinkedList::<u8>::with_capacity(10);
1383    ///
1384    /// list.push_back(0);
1385    /// list.push_back(1);
1386    /// list.push_back(2);
1387    ///
1388    /// assert_eq!(list.contains(&0), true);
1389    /// assert_eq!(list.contains(&10), false);
1390    /// ```
1391    pub fn contains(&self, x: &T) -> bool
1392    where
1393        T: PartialEq<T>,
1394    {
1395        self.iter().any(|e| e == x)
1396    }
1397
1398    /// Adds an element immediately after the node associated with the
1399    /// specified handle. Returns the handle to the node thats been
1400    /// added or None if the specified handle is invalid.
1401    ///
1402    /// This operation should complete in *O*(*1*) time.
1403    ///
1404    /// # Examples
1405    ///
1406    /// ```
1407    /// use deepmesa_collections::LinkedList;
1408    /// let mut list = LinkedList::<u8>::with_capacity(10);
1409    ///
1410    /// list.push_head(0);
1411    /// let middle = list.push_head(1);
1412    /// list.push_head(2);
1413    /// list.push_next(&middle, 100);
1414    ///
1415    /// let mut iter = list.iter();
1416    /// assert_eq!(iter.next(), Some(&2));
1417    /// assert_eq!(iter.next(), Some(&1));
1418    /// assert_eq!(iter.next(), Some(&100));
1419    /// assert_eq!(iter.next(), Some(&0));
1420    /// assert_eq!(iter.next(), None);
1421    /// ```
1422    pub fn push_next(&mut self, node: &NodeHandle<T>, elem: T) -> Option<NodeHandle<T>> {
1423        match self.node_ptr(node) {
1424            None => None,
1425            Some(c_ptr) => unsafe {
1426                let nid = nid_inc!(self.nid);
1427                let raw_n = self.fl.acquire(elem, nid);
1428
1429                if !(*c_ptr).next.is_null() {
1430                    (*(*c_ptr).next).prev = raw_n;
1431                    (*raw_n).next = (*c_ptr).next;
1432                }
1433
1434                (*c_ptr).next = raw_n;
1435                (*raw_n).prev = c_ptr;
1436
1437                if self.tail == c_ptr {
1438                    self.tail = raw_n;
1439                }
1440
1441                self.len += 1;
1442                Some(NodeHandle::new(self.cid, nid, raw_n))
1443            },
1444        }
1445    }
1446
1447    /// Adds an element immediately preceedeing the node associated with the
1448    /// specified handle. Returns the handle to the node thats been
1449    /// added or None if the specified handle is invalid.
1450    ///
1451    /// This operation should complete in *O*(*1*) time.
1452    ///
1453    /// # Examples
1454    ///
1455    /// ```
1456    /// use deepmesa_collections::LinkedList;
1457    /// let mut list = LinkedList::<u8>::with_capacity(10);
1458    ///
1459    /// list.push_head(0);
1460    /// let middle = list.push_head(1);
1461    /// list.push_head(2);
1462    /// list.push_prev(&middle, 100);
1463    ///
1464    /// let mut iter = list.iter();
1465    /// assert_eq!(iter.next(), Some(&2));
1466    /// assert_eq!(iter.next(), Some(&100));
1467    /// assert_eq!(iter.next(), Some(&1));
1468    /// assert_eq!(iter.next(), Some(&0));
1469    /// assert_eq!(iter.next(), None);
1470    /// ```
1471    pub fn push_prev(&mut self, node: &NodeHandle<T>, elem: T) -> Option<NodeHandle<T>> {
1472        match self.node_ptr(node) {
1473            None => None,
1474            Some(c_ptr) => unsafe {
1475                let nid = nid_inc!(self.nid);
1476                let raw_n = self.fl.acquire(elem, nid);
1477
1478                if !(*c_ptr).prev.is_null() {
1479                    (*(*c_ptr).prev).next = raw_n;
1480                    (*raw_n).prev = (*c_ptr).prev;
1481                }
1482
1483                (*c_ptr).prev = raw_n;
1484                (*raw_n).next = c_ptr;
1485
1486                if self.head == c_ptr {
1487                    self.head = raw_n;
1488                }
1489
1490                self.len += 1;
1491                Some(NodeHandle::new(self.cid, nid, raw_n))
1492            },
1493        }
1494    }
1495
1496    /// Moves all nodes from the `other` list to the end of this
1497    /// list. After this operation completes, the `other` list is
1498    /// empty and all the nodes from that list have been moved into
1499    /// this one.
1500    ///
1501    /// All handles to nodes previously returned by other list will
1502    /// become invalid after this operation completes.
1503    ///
1504    /// This operation has no effect on the allocated capacity of
1505    /// either list.
1506    ///
1507    /// This operation should compute in *O*(*1*) time
1508    ///
1509    /// # Examples
1510    ///
1511    /// ```
1512    /// use deepmesa_collections::LinkedList;
1513    /// let mut list = LinkedList::<u8>::with_capacity(10);
1514    /// list.push_back(0);
1515    ///
1516    /// let mut list2 = LinkedList::<u8>::with_capacity(10);
1517    /// list2.push_back(1);
1518    /// list2.push_back(2);
1519    ///
1520    /// list.append(&mut list2);
1521    ///
1522    /// let mut iter = list.iter();
1523    /// assert_eq!(iter.next(), Some(&0));
1524    /// assert_eq!(iter.next(), Some(&1));
1525    /// assert_eq!(iter.next(), Some(&2));
1526    /// assert_eq!(iter.next(), None);
1527    ///
1528    /// assert_eq!(list.len(), 3);
1529    /// assert!(list2.is_empty());
1530    /// ```
1531    pub fn append(&mut self, other: &mut Self) {
1532        if self.tail.is_null() {
1533            self.head = other.head;
1534        } else {
1535            unsafe {
1536                (*self.tail).next = other.head;
1537                if !other.head.is_null() {
1538                    (*other.head).prev = self.tail;
1539                }
1540            }
1541        }
1542
1543        self.tail = other.tail;
1544        self.len += other.len;
1545        other.head = ptr::null_mut();
1546        other.tail = ptr::null_mut();
1547        other.len = 0;
1548        other.cid = inc_cid();
1549    }
1550
1551    /// Moves the node associated with the specified handle to the
1552    /// front (head) of the list. If the node is already at the head
1553    /// of the list then this operation has no effect.
1554    ///
1555    /// Returns true if the node is successfully moved to the head of
1556    /// the list (or if it was already at the head) and false if the
1557    /// specified handle is invalid.
1558    ///
1559    /// This operation should complete in *O*(*1*) time.
1560    ///
1561    /// # Examples
1562    ///
1563    /// ```
1564    /// use deepmesa_collections::LinkedList;
1565    /// let mut list = LinkedList::<u8>::with_capacity(3);
1566    /// let hnd0 = list.push_tail(0);
1567    /// let hnd1 = list.push_tail(1);
1568    /// let hnd2 = list.push_tail(2);
1569    ///
1570    /// assert_eq!(list.head(), Some(&0));
1571    /// list.make_head(&hnd2);
1572    /// assert_eq!(list.head(), Some(&2));
1573    /// ```
1574    pub fn make_head(&mut self, node: &NodeHandle<T>) -> bool {
1575        match self.node_ptr(node) {
1576            None => false,
1577            Some(n_ptr) => unsafe {
1578                if n_ptr == self.head {
1579                    return true;
1580                }
1581
1582                if !(*n_ptr).prev.is_null() {
1583                    (*(*n_ptr).prev).next = (*n_ptr).next;
1584                }
1585                if !(*n_ptr).next.is_null() {
1586                    (*(*n_ptr).next).prev = (*n_ptr).prev;
1587                }
1588
1589                if n_ptr == self.tail {
1590                    self.tail = (*n_ptr).prev;
1591                }
1592
1593                (*n_ptr).prev = ptr::null_mut();
1594                (*n_ptr).next = self.head;
1595                (*self.head).prev = n_ptr;
1596                self.head = n_ptr;
1597                true
1598            },
1599        }
1600    }
1601
1602    /// Moves the node associated with the specified handle to the
1603    /// back (tail) of the list. If the node is already at the tail of
1604    /// the list then this operation has no effect.
1605    ///
1606    /// Returns true if the node is successfully moved to the tail of
1607    /// the list (or if it was already at the tail) and false if the
1608    /// specified handle is invalid.
1609    ///
1610    /// This operation should complete in *O*(*1*) time.
1611    ///
1612    /// # Examples
1613    ///
1614    /// ```
1615    /// use deepmesa_collections::LinkedList;
1616    /// let mut list = LinkedList::<u8>::with_capacity(3);
1617    /// let hnd0 = list.push_tail(0);
1618    /// let hnd1 = list.push_tail(1);
1619    /// let hnd2 = list.push_tail(2);
1620    ///
1621    /// assert_eq!(list.tail(), Some(&2));
1622    /// list.make_tail(&hnd0);
1623    /// assert_eq!(list.tail(), Some(&0));
1624    /// ```
1625    pub fn make_tail(&mut self, node: &NodeHandle<T>) -> bool {
1626        match self.node_ptr(node) {
1627            None => false,
1628            Some(n_ptr) => unsafe {
1629                if n_ptr == self.tail {
1630                    return true;
1631                }
1632
1633                if !(*n_ptr).prev.is_null() {
1634                    (*(*n_ptr).prev).next = (*n_ptr).next;
1635                }
1636                if !(*n_ptr).next.is_null() {
1637                    (*(*n_ptr).next).prev = (*n_ptr).prev;
1638                }
1639
1640                if n_ptr == self.head {
1641                    self.head = (*n_ptr).next;
1642                }
1643
1644                (*n_ptr).next = ptr::null_mut();
1645                (*n_ptr).prev = self.tail;
1646                (*self.tail).next = n_ptr;
1647                self.tail = n_ptr;
1648                true
1649            },
1650        }
1651    }
1652
1653    /// Returns `true` if the specified node is immediately previous
1654    /// to `other` and `false` otherwise. If either of the nodes is
1655    /// invalid, this method returns None.
1656    ///
1657    /// This method should complete in *O*(*1*) time.
1658    ///
1659    /// # Examples
1660    /// ```
1661    /// use deepmesa_collections::LinkedList;
1662    /// let mut list = LinkedList::<u8>::with_capacity(4);
1663    /// let hnd0 = list.push_tail(0);
1664    /// let hnd1 = list.push_tail(1);
1665    /// let hnd2 = list.push_tail(2);
1666    /// list.pop_tail();
1667    /// assert_eq!(list.is_prev(&hnd0, &hnd1), Some(true));
1668    /// assert_eq!(list.is_prev(&hnd1, &hnd0), Some(false));
1669    /// assert_eq!(list.is_prev(&hnd1, &hnd2), None);
1670    /// ```
1671    pub fn is_prev(&self, node: &NodeHandle<T>, other: &NodeHandle<T>) -> Option<bool> {
1672        if let Some(n_ptr) = self.node_ptr(node) {
1673            if let Some(o_ptr) = self.node_ptr(other) {
1674                unsafe {
1675                    if (*n_ptr).next == o_ptr {
1676                        return Some(true);
1677                    } else {
1678                        return Some(false);
1679                    }
1680                }
1681            }
1682        }
1683        None
1684    }
1685
1686    /// Returns `true` if the specified node is immediately after
1687    /// `other` and `false` otherwise. If either of the nodes is
1688    /// invalid, this method returns None.
1689    ///
1690    /// This method should complete in *O*(*1*) time.
1691    ///
1692    /// # Examples
1693    /// ```
1694    /// use deepmesa_collections::LinkedList;
1695    /// let mut list = LinkedList::<u8>::with_capacity(4);
1696    /// let hnd0 = list.push_tail(0);
1697    /// let hnd1 = list.push_tail(1);
1698    /// let hnd2 = list.push_tail(2);
1699    /// list.pop_tail();
1700    /// assert_eq!(list.is_next(&hnd1, &hnd0), Some(true));
1701    /// assert_eq!(list.is_next(&hnd0, &hnd1), Some(false));
1702    /// assert_eq!(list.is_next(&hnd2, &hnd1), None);
1703    /// ```
1704    pub fn is_next(&self, node: &NodeHandle<T>, other: &NodeHandle<T>) -> Option<bool> {
1705        if let Some(n_ptr) = self.node_ptr(node) {
1706            if let Some(o_ptr) = self.node_ptr(other) {
1707                unsafe {
1708                    if (*n_ptr).prev == o_ptr {
1709                        return Some(true);
1710                    } else {
1711                        return Some(false);
1712                    }
1713                }
1714            }
1715        }
1716        None
1717    }
1718
1719    /// Returns `true` if the specified node is the head of the list
1720    /// and `false` if its not. If the specified node is invalid, then
1721    /// this method returns `None`
1722    ///
1723    /// This method should complete in *O*(*1*) time.
1724    ///
1725    /// # Examples
1726    /// ```
1727    /// use deepmesa_collections::LinkedList;
1728    /// let mut list = LinkedList::<u8>::with_capacity(4);
1729    /// let hnd0 = list.push_tail(0);
1730    /// let hnd1 = list.push_tail(1);
1731    /// let hnd2 = list.push_tail(2);
1732    /// list.pop_tail();
1733    /// assert_eq!(list.is_head(&hnd0), Some(true));
1734    /// assert_eq!(list.is_head(&hnd1), Some(false));
1735    /// assert_eq!(list.is_head(&hnd2), None);
1736    /// ```
1737    pub fn is_head(&self, node: &NodeHandle<T>) -> Option<bool> {
1738        if let Some(n_ptr) = self.node_ptr(node) {
1739            if n_ptr == self.head {
1740                return Some(true);
1741            } else {
1742                return Some(false);
1743            }
1744        }
1745        None
1746    }
1747
1748    /// Returns `true` if the specified node is the tail of the list
1749    /// and `false` if its not. If the specified node is invalid, then
1750    /// this method returns `None`
1751    ///
1752    /// This method should complete in *O*(*1*) time.
1753    ///
1754    /// # Examples
1755    /// ```
1756    /// use deepmesa_collections::LinkedList;
1757    /// let mut list = LinkedList::<u8>::with_capacity(4);
1758    /// let hnd0 = list.push_tail(0);
1759    /// let hnd1 = list.push_tail(1);
1760    /// let hnd2 = list.push_tail(2);
1761    /// list.pop_tail();
1762    /// assert_eq!(list.is_tail(&hnd0), Some(false));
1763    /// assert_eq!(list.is_tail(&hnd1), Some(true));
1764    /// assert_eq!(list.is_tail(&hnd2), None);
1765    /// ```
1766    pub fn is_tail(&self, node: &NodeHandle<T>) -> Option<bool> {
1767        if let Some(n_ptr) = self.node_ptr(node) {
1768            if n_ptr == self.tail {
1769                return Some(true);
1770            } else {
1771                return Some(false);
1772            }
1773        }
1774        None
1775    }
1776
1777    /// Swaps the position in the list of the two nodes and returns
1778    /// true on success. If either node is invalid then this method
1779    /// returns false.
1780    ///
1781    /// This method should complete in *O*(*1*) time.
1782    ///
1783    /// # Examples
1784    /// ```
1785    /// use deepmesa_collections::LinkedList;
1786    /// let mut list = LinkedList::<u8>::with_capacity(4);
1787    /// let hnd0 = list.push_tail(0);
1788    /// let hnd1 = list.push_tail(1);
1789    /// let hnd2 = list.push_tail(2);
1790    /// list.pop_tail();
1791    /// assert_eq!(list.swap_node(&hnd0, &hnd1), true);
1792    /// assert_eq!(list.swap_node(&hnd1, &hnd2), false);
1793    /// assert_eq!(list.is_head(&hnd1), Some(true));
1794    /// assert_eq!(list.is_tail(&hnd0), Some(true));
1795    /// ```
1796    pub fn swap_node(&mut self, node: &NodeHandle<T>, other: &NodeHandle<T>) -> bool {
1797        if let Some(n_ptr) = self.node_ptr(node) {
1798            if let Some(o_ptr) = self.node_ptr(other) {
1799                if n_ptr == o_ptr {
1800                    return true;
1801                }
1802                unsafe {
1803                    if (*n_ptr).next == o_ptr {
1804                        //n_ptr is after o_pter. Swap them
1805                        self.swap_node_adjacent(n_ptr, o_ptr);
1806                        return true;
1807                    }
1808                    if (*o_ptr).next == n_ptr {
1809                        //o_ptr is after n_ptr. Swap them
1810                        self.swap_node_adjacent(o_ptr, n_ptr);
1811                        return true;
1812                    }
1813                    // n_ptr & o_ptr at disjoint - i.e. have atleast one other node between them
1814                    let np_prev = (*n_ptr).prev;
1815                    let np_next = (*n_ptr).next;
1816                    let op_prev = (*o_ptr).prev;
1817                    let op_next = (*o_ptr).next;
1818
1819                    (*o_ptr).prev = np_prev;
1820                    (*o_ptr).next = np_next;
1821                    (*n_ptr).prev = op_prev;
1822                    (*n_ptr).next = op_next;
1823
1824                    if np_prev.is_null() {
1825                        //n_ptr is at the head. So make o_ptr the head
1826                        self.head = o_ptr;
1827                    } else {
1828                        (*np_prev).next = o_ptr;
1829                    }
1830
1831                    if np_next.is_null() {
1832                        //n_ptr is at the tail. So make o_ptr the tail
1833                        self.tail = o_ptr;
1834                    } else {
1835                        (*np_next).prev = o_ptr;
1836                    }
1837
1838                    if op_prev.is_null() {
1839                        //o_ptr is at the head. So make n_ptr the head
1840                        self.head = n_ptr;
1841                    } else {
1842                        (*op_prev).next = n_ptr;
1843                    }
1844
1845                    if op_next.is_null() {
1846                        //o_ptr is the tail. So make n_ptr the tail
1847                        self.tail = n_ptr;
1848                    } else {
1849                        (*op_next).prev = n_ptr;
1850                    }
1851
1852                    return true;
1853                }
1854            }
1855        }
1856        false
1857    }
1858
1859    ////////////////////
1860    //Private Helpers
1861    ////////////////////
1862
1863    /// Removes and returns the value pointed to by the specified raw
1864    /// pointer. This method will panic if the specified pointer is
1865    /// null. The memory is returned to the free list.
1866    pub(crate) fn pop_ptr(&mut self, ptr: *mut InternalNode<T>) -> T {
1867        if ptr.is_null() {
1868            panic!("cannot pop null pointer");
1869        }
1870
1871        unsafe {
1872            if !(*ptr).next.is_null() {
1873                (*(*ptr).next).prev = (*ptr).prev;
1874            }
1875
1876            if !(*ptr).prev.is_null() {
1877                (*(*ptr).prev).next = (*ptr).next;
1878            }
1879
1880            if self.head == ptr {
1881                self.head = (*ptr).next;
1882            }
1883
1884            if self.tail == ptr {
1885                self.tail = (*ptr).prev;
1886            }
1887            self.len -= 1;
1888            self.fl.release(ptr)
1889        }
1890    }
1891
1892    /// Returns a valid raw pointer to the specified Handle or None if
1893    /// the handle is invalid.  This method checks the container Id
1894    /// (cid) of the handle against the list itself so that handles
1895    /// cannot be used across lists. If the container Id matches then
1896    /// the node Id is checked against the node Id stored at that
1897    /// memory locatiom. If the container Id and the Node Id are a
1898    /// match then a flag indicating whether this node is part of the
1899    /// freelist is checked.
1900
1901    /// If the container Id and the node Id match and the node is not
1902    /// part of the free list then the raw pointer to the Node is
1903    /// returned. If the conditions don't return true this method
1904    /// returns None indicating that the raw pointer does not point to
1905    /// the node that the handle originally referred to.
1906    ///
1907    /// It should be noted that the raw pointer returned will always
1908    /// point to a valid memory location since that memory is
1909    /// allocated and managed by the freelist. However the contents of
1910    /// that memory location can change as nodes are pushed and popped
1911    /// off the list. When the contents change the handle becomes
1912    /// invalid and this method returns None.
1913    fn node_ptr(&self, node: &NodeHandle<T>) -> Option<*mut InternalNode<T>> {
1914        if node.cid != self.cid {
1915            return None;
1916        }
1917        unsafe {
1918            // First check if this node is on the freelist. If it is
1919            // then we don't need to check the node id (nid)
1920            if (*node.ptr).fl_node {
1921                return None;
1922            }
1923
1924            if (*node.ptr).nid != node.nid {
1925                return None;
1926            }
1927        }
1928
1929        Some((*node).ptr)
1930    }
1931
1932    unsafe fn swap_node_adjacent(
1933        &mut self,
1934        n_ptr: *mut InternalNode<T>,
1935        o_ptr: *mut InternalNode<T>,
1936    ) {
1937        // N -> O
1938
1939        if (*n_ptr).prev.is_null() {
1940            //n_ptr is the head
1941            self.head = o_ptr;
1942        } else {
1943            (*(*n_ptr).prev).next = o_ptr;
1944        }
1945
1946        if (*o_ptr).next.is_null() {
1947            //o_ptr is the tail
1948            self.tail = n_ptr;
1949        } else {
1950            (*(*o_ptr).next).prev = n_ptr;
1951        }
1952
1953        (*n_ptr).next = (*o_ptr).next;
1954        (*o_ptr).prev = (*n_ptr).prev;
1955        (*o_ptr).next = n_ptr;
1956        (*n_ptr).prev = o_ptr;
1957    }
1958}
1959
1960#[cfg(test)]
1961mod tests {
1962    use super::*;
1963
1964    const FIRST: u8 = 0;
1965    const LAST: u8 = 1;
1966    const ONLY: u8 = 2;
1967    const MIDDLE: u8 = 3;
1968
1969    macro_rules! assert_is_head {
1970        ($ll:ident, $node:ident) => {
1971            match $ll.node_ptr(&$node) {
1972                None => panic!("Node: {:?} not found. ", $node),
1973                Some(n_ptr) => {
1974                    assert_eq!($ll.head, n_ptr);
1975                }
1976            }
1977        };
1978    }
1979
1980    macro_rules! assert_is_not_head {
1981        ($ll:ident, $node:ident) => {
1982            match $ll.node_ptr(&$node) {
1983                None => panic!("Node: {:?} not found", $node),
1984                Some(n_ptr) => {
1985                    assert_ne!($ll.head, n_ptr);
1986                }
1987            }
1988        };
1989    }
1990
1991    macro_rules! assert_is_tail {
1992        ($ll:ident, $node:ident) => {
1993            match $ll.node_ptr(&$node) {
1994                None => panic!("Node: {:?} not found. ", $node),
1995                Some(n_ptr) => {
1996                    assert_eq!($ll.tail, n_ptr);
1997                }
1998            }
1999        };
2000    }
2001
2002    macro_rules! assert_is_not_tail {
2003        ($ll:ident, $node:ident) => {
2004            match $ll.node_ptr(&$node) {
2005                None => panic!("Node: {:?} not found", $node),
2006                Some(n_ptr) => {
2007                    assert_ne!($ll.tail, n_ptr);
2008                }
2009            }
2010        };
2011    }
2012
2013    macro_rules! assert_node {
2014        ($ll:ident, $node: ident, $pos: ident, $val: expr, $len: literal) => {
2015            assert_eq!($node.cid, $ll.cid);
2016
2017            if $pos == FIRST || $pos == ONLY {
2018                assert_is_head!($ll, $node);
2019                assert_eq!($ll.head(), Some(&$val));
2020                assert_eq!($ll.has_prev(&$node), Some(false));
2021            }
2022
2023            if $pos == LAST || $pos == ONLY {
2024                assert_is_tail!($ll, $node);
2025                assert_eq!($ll.tail(), Some(&$val));
2026                assert_eq!($ll.has_next(&$node), Some(false));
2027            }
2028
2029            if $pos == MIDDLE {
2030                assert_is_not_tail!($ll, $node);
2031                assert_is_not_head!($ll, $node);
2032                assert_eq!($ll.has_next(&$node), Some(true));
2033                assert_eq!($ll.has_prev(&$node), Some(true));
2034            }
2035
2036            assert_eq!($ll.has_tail(), true);
2037            assert_eq!($ll.has_head(), true);
2038
2039            assert_eq!($ll.node(&$node), Some(&$val));
2040            assert_eq!($ll.len(), $len);
2041        };
2042    }
2043
2044    macro_rules! assert_order {
2045        ($ll: ident, $x: ident, $x_val: literal, $y: ident, $y_val: literal) => {
2046            let x_ptr;
2047            let y_ptr;
2048
2049            match $ll.node_ptr(&$x) {
2050                None => panic!("Node: {:?} not found", $x),
2051                Some(n_ptr) => x_ptr = n_ptr,
2052            }
2053
2054            match $ll.node_ptr(&$y) {
2055                None => panic!("Node y: {:?} not found", $y),
2056                Some(n_ptr) => y_ptr = n_ptr,
2057            }
2058
2059            unsafe {
2060                assert_eq!((*x_ptr).next, y_ptr);
2061                assert_eq!((*y_ptr).prev, x_ptr);
2062            }
2063            assert_eq!($ll.next(&$x), Some(&$y_val));
2064            assert_eq!($ll.prev(&$y), Some(&$x_val));
2065            assert_eq!($ll.next_node(&$x).as_ref(), Some(&$y));
2066            assert_eq!($ll.prev_node(&$y).as_ref(), Some(&$x));
2067            assert_eq!($ll.has_next(&$x), Some(true));
2068            assert_eq!($ll.has_prev(&$y), Some(true));
2069        };
2070    }
2071
2072    macro_rules! assert_empty {
2073        ($ll:ident) => {
2074            assert!($ll.head.is_null());
2075            assert!($ll.tail.is_null());
2076            assert_eq!($ll.len(), 0);
2077        };
2078    }
2079
2080    #[test]
2081    fn test_new() {
2082        let ll1 = LinkedList::<u8>::new();
2083        let ll2 = LinkedList::<u8>::new();
2084        assert!(ll1.cid < ll2.cid);
2085    }
2086
2087    #[test]
2088    fn test_push_head() {
2089        let mut ll = LinkedList::<u8>::new();
2090        let node = ll.push_head(11);
2091        assert_node!(ll, node, ONLY, 11, 1);
2092        //now push a second head
2093        let second = ll.push_head(12);
2094        assert_node!(ll, second, FIRST, 12, 2);
2095        assert_node!(ll, node, LAST, 11, 2);
2096        assert_order!(ll, second, 12, node, 11);
2097        //now push a third node
2098        let third = ll.push_head(13);
2099        assert_node!(ll, third, FIRST, 13, 3);
2100        assert_node!(ll, second, MIDDLE, 12, 3);
2101        assert_node!(ll, node, LAST, 11, 3);
2102
2103        assert_order!(ll, third, 13, second, 12);
2104        assert_order!(ll, second, 12, node, 11);
2105
2106        ll.clear();
2107        assert_empty!(ll);
2108    }
2109
2110    #[test]
2111    fn test_push_tail() {
2112        let mut ll = LinkedList::<u8>::new();
2113        let node = ll.push_tail(33);
2114        assert_node!(ll, node, ONLY, 33, 1);
2115        //now push a second head
2116        let second = ll.push_tail(44);
2117        assert_node!(ll, node, FIRST, 33, 2);
2118        assert_node!(ll, second, LAST, 44, 2);
2119
2120        assert_order!(ll, node, 33, second, 44);
2121        //now push a third node
2122        let third = ll.push_tail(55);
2123        assert_node!(ll, node, FIRST, 33, 3);
2124        assert_node!(ll, second, MIDDLE, 44, 3);
2125        assert_node!(ll, third, LAST, 55, 3);
2126
2127        assert_order!(ll, node, 33, second, 44);
2128        assert_order!(ll, second, 44, third, 55);
2129
2130        ll.clear();
2131        assert_empty!(ll);
2132    }
2133
2134    #[test]
2135    fn test_pop_head() {
2136        let mut ll = LinkedList::<u8>::new();
2137        let node = ll.push_head(11);
2138        assert_node!(ll, node, ONLY, 11, 1);
2139        //now pop the head
2140        assert_eq!(ll.pop_head(), Some(11));
2141        assert_empty!(ll);
2142
2143        //now push two nodes
2144        let node = ll.push_head(11);
2145        ll.push_head(12);
2146        //now pop the head
2147        assert_eq!(ll.len(), 2);
2148        assert_eq!(ll.pop_head(), Some(12));
2149        assert_node!(ll, node, ONLY, 11, 1);
2150        //now pop the head again
2151        assert_eq!(ll.pop_head(), Some(11));
2152        assert_empty!(ll);
2153
2154        //now push 3 nodes node
2155        let node = ll.push_head(11);
2156        let second = ll.push_head(12);
2157        ll.push_head(13);
2158        assert_eq!(ll.len(), 3);
2159        //pop the head
2160        assert_eq!(ll.pop_head(), Some(13));
2161        assert_node!(ll, node, LAST, 11, 2);
2162        assert_node!(ll, second, FIRST, 12, 2);
2163        //pop the head again
2164        assert_eq!(ll.pop_head(), Some(12));
2165        assert_node!(ll, node, ONLY, 11, 1);
2166        //pop the head for the last time
2167        assert_eq!(ll.pop_head(), Some(11));
2168        assert_empty!(ll);
2169    }
2170
2171    #[test]
2172    fn test_pop_tail() {
2173        let mut ll = LinkedList::<u8>::new();
2174        let node = ll.push_tail(11);
2175        assert_node!(ll, node, ONLY, 11, 1);
2176        //now pop the tail
2177        assert_eq!(ll.pop_tail(), Some(11));
2178        assert_empty!(ll);
2179
2180        //now push two nodes
2181        ll.push_head(11);
2182        let second = ll.push_head(12);
2183        assert_eq!(ll.len(), 2);
2184        //now pop the tail
2185        assert_eq!(ll.pop_tail(), Some(11));
2186        assert_node!(ll, second, ONLY, 12, 1);
2187        //now pop the head again
2188        assert_eq!(ll.pop_tail(), Some(12));
2189        assert_empty!(ll);
2190
2191        //now push 3 nodes node
2192        ll.push_head(11);
2193        let second = ll.push_head(12);
2194        let third = ll.push_head(13);
2195        assert_eq!(ll.len(), 3);
2196        //pop the tail
2197        assert_eq!(ll.pop_tail(), Some(11));
2198        assert_node!(ll, second, LAST, 12, 2);
2199        assert_node!(ll, third, FIRST, 13, 2);
2200        //pop the head again
2201        assert_eq!(ll.pop_tail(), Some(12));
2202        assert_node!(ll, third, ONLY, 13, 1);
2203        //pop the head for the last time
2204        assert_eq!(ll.pop_tail(), Some(13));
2205        assert_empty!(ll);
2206    }
2207
2208    #[test]
2209    fn test_capacity_zero() {
2210        let mut ll = LinkedList::<u8>::with_capacity(0);
2211        assert_eq!(ll.len(), 0);
2212        assert_eq!(ll.capacity(), 0);
2213        for _ in 0..5 {
2214            ll.push_head(11);
2215        }
2216        assert_eq!(ll.len(), 5);
2217        assert_eq!(ll.capacity(), 5);
2218
2219        for _ in 0..3 {
2220            ll.pop_tail();
2221        }
2222        assert_eq!(ll.len(), 2);
2223        assert_eq!(ll.capacity(), 5);
2224    }
2225
2226    #[test]
2227    fn test_capacity() {
2228        let mut ll = LinkedList::<u8>::with_capacity(2);
2229        assert_eq!(ll.len(), 0);
2230        assert_eq!(ll.capacity(), 2);
2231        for _ in 0..5 {
2232            ll.push_head(11);
2233        }
2234        assert_eq!(ll.len(), 5);
2235        assert_eq!(ll.capacity(), 8);
2236        //now add another 3
2237        for _ in 0..3 {
2238            ll.push_head(11);
2239        }
2240        assert_eq!(ll.len(), 8);
2241        assert_eq!(ll.capacity(), 8);
2242        //add one more
2243        ll.push_head(11);
2244        assert_eq!(ll.len(), 9);
2245        assert_eq!(ll.capacity(), 16);
2246        //now add another 8
2247        for _ in 0..8 {
2248            ll.push_head(11);
2249        }
2250        assert_eq!(ll.len(), 17);
2251        assert_eq!(ll.capacity(), 32);
2252        let mut ll = LinkedList::<u8>::with_capacity(17);
2253        for _ in 0..18 {
2254            ll.push_head(11);
2255        }
2256        assert_eq!(ll.len(), 18);
2257        assert_eq!(ll.capacity(), 34);
2258    }
2259
2260    #[test]
2261    fn test_node_reuse() {
2262        let mut ll = LinkedList::<u8>::with_capacity(8);
2263        //first push one node
2264        let mut node = ll.push_head(1);
2265        for i in 0..7 {
2266            node = ll.push_head(i);
2267        }
2268
2269        //now pop the head
2270        ll.pop_head();
2271
2272        //now push it again
2273        let node2 = ll.push_head(200);
2274        assert_eq!(ll.node(&node2), Some(&200));
2275        assert_eq!(ll.node(&node), None);
2276    }
2277
2278    #[test]
2279    fn test_iter() {
2280        let mut ll = LinkedList::<u8>::with_capacity(10);
2281        for i in 0..10 {
2282            ll.push_head(i);
2283        }
2284
2285        let mut iter = ll.iter();
2286        let mut count = 9;
2287        while let Some(val) = iter.next() {
2288            assert_eq!(*val, count);
2289            if count > 0 {
2290                count -= 1;
2291            }
2292        }
2293
2294        let mut iter_mut = ll.iter_mut();
2295        while let Some(val) = iter_mut.next() {
2296            *val += 1;
2297        }
2298
2299        let iter = ll.iter();
2300        let mut count = 9;
2301        for val in iter {
2302            assert_eq!(*val, count + 1);
2303            if count > 0 {
2304                count -= 1;
2305            }
2306        }
2307        for val in &mut ll {
2308            *val -= 1;
2309        }
2310
2311        let mut count = 9;
2312        for val in &ll {
2313            assert_eq!(*val, count);
2314            if count > 0 {
2315                count -= 1;
2316            }
2317        }
2318    }
2319
2320    #[test]
2321    fn test_iter_reverse() {
2322        let mut ll = LinkedList::<u8>::with_capacity(10);
2323        for i in 0..10 {
2324            ll.push_head(i);
2325        }
2326
2327        let mut iter = ll.iter().reverse();
2328        let mut count = 0;
2329        while let Some(val) = iter.next() {
2330            assert_eq!(*val, count);
2331            count += 1;
2332        }
2333
2334        iter = iter.reverse();
2335        let mut count = 9;
2336        while let Some(val) = iter.next() {
2337            assert_eq!(*val, count);
2338            if count > 0 {
2339                count -= 1;
2340            }
2341        }
2342
2343        iter = iter.reverse();
2344        let mut count = 0;
2345        while let Some(val) = iter.next() {
2346            assert_eq!(*val, count);
2347            count += 1;
2348        }
2349    }
2350
2351    #[test]
2352    fn test_iter_reverse2() {
2353        let mut ll = LinkedList::<u8>::with_capacity(10);
2354        for i in 0..10 {
2355            ll.push_head(i);
2356        }
2357
2358        let mut iter = ll.iter();
2359        assert_eq!(iter.next(), Some(&9));
2360        assert_eq!(iter.next(), Some(&8));
2361        assert_eq!(iter.next(), Some(&7));
2362        assert_eq!(iter.next(), Some(&6));
2363        assert_eq!(iter.next(), Some(&5));
2364        iter = iter.reverse();
2365        assert_eq!(iter.next(), Some(&6));
2366        assert_eq!(iter.next(), Some(&7));
2367        iter = iter.reverse();
2368        assert_eq!(iter.next(), Some(&6));
2369        assert_eq!(iter.next(), Some(&5));
2370        assert_eq!(iter.next(), Some(&4));
2371        assert_eq!(iter.next(), Some(&3));
2372        assert_eq!(iter.next(), Some(&2));
2373        assert_eq!(iter.next(), Some(&1));
2374        assert_eq!(iter.next(), Some(&0));
2375        assert_eq!(iter.next(), None);
2376    }
2377
2378    #[test]
2379    fn test_invalid_node() {
2380        let mut ll = LinkedList::<u8>::with_capacity(10);
2381        let headnode = ll.push_head(12);
2382        let headval = ll.pop_head();
2383        assert_eq!(headval, Some(12));
2384        //Attempting to get the value at headnode should now return
2385        // None
2386        assert_eq!(ll.node(&headnode), None);
2387    }
2388
2389    #[test]
2390    fn test_clear() {
2391        let mut ll = LinkedList::<u8>::with_capacity(10);
2392        ll.push_head(0);
2393        ll.push_head(1);
2394        ll.push_head(2);
2395        assert_eq!(ll.len(), 3);
2396        assert_eq!(ll.capacity(), 10);
2397        ll.clear();
2398        assert_eq!(ll.len(), 0);
2399        assert_eq!(ll.capacity(), 10);
2400    }
2401
2402    #[test]
2403    fn test_push_next() {
2404        let mut ll = LinkedList::<u8>::new();
2405        let node1 = ll.push_head(1);
2406        let node2 = ll.push_head(2);
2407        let node3 = ll.push_head(3);
2408
2409        let n_node1 = ll.push_next(&node1, 11).unwrap();
2410        assert_node!(ll, n_node1, LAST, 11, 4);
2411        assert_order!(ll, node1, 1, n_node1, 11);
2412        assert_order!(ll, node2, 2, node1, 1);
2413        assert_order!(ll, node3, 3, node2, 2);
2414
2415        let n_node2 = ll.push_next(&node2, 22).unwrap();
2416        assert_node!(ll, n_node2, MIDDLE, 22, 5);
2417        assert_order!(ll, node1, 1, n_node1, 11);
2418        assert_order!(ll, n_node2, 22, node1, 1);
2419        assert_order!(ll, node2, 2, n_node2, 22);
2420        assert_order!(ll, node3, 3, node2, 2);
2421
2422        let n_node3 = ll.push_next(&node3, 33).unwrap();
2423        assert_node!(ll, n_node3, MIDDLE, 33, 6);
2424
2425        assert_order!(ll, node1, 1, n_node1, 11);
2426        assert_order!(ll, n_node2, 22, node1, 1);
2427        assert_order!(ll, node2, 2, n_node2, 22);
2428        assert_order!(ll, n_node3, 33, node2, 2);
2429        assert_order!(ll, node3, 3, n_node3, 33);
2430    }
2431
2432    #[test]
2433    fn test_push_prev() {
2434        let mut ll = LinkedList::<u8>::new();
2435        let node1 = ll.push_head(1);
2436        let node2 = ll.push_head(2);
2437        let node3 = ll.push_head(3);
2438
2439        let n_node1 = ll.push_prev(&node1, 11).unwrap();
2440        assert_node!(ll, node1, LAST, 1, 4);
2441        assert_node!(ll, n_node1, MIDDLE, 11, 4);
2442        assert_order!(ll, n_node1, 11, node1, 1);
2443        assert_order!(ll, node2, 2, n_node1, 11);
2444        assert_order!(ll, node3, 3, node2, 2);
2445
2446        let n_node2 = ll.push_prev(&node2, 22).unwrap();
2447        assert_node!(ll, n_node2, MIDDLE, 22, 5);
2448
2449        assert_order!(ll, n_node1, 11, node1, 1);
2450        assert_order!(ll, node2, 2, n_node1, 11);
2451        assert_order!(ll, n_node2, 22, node2, 2);
2452        assert_order!(ll, node3, 3, n_node2, 22);
2453
2454        let n_node3 = ll.push_prev(&node3, 33).unwrap();
2455        assert_node!(ll, n_node3, FIRST, 33, 6);
2456        assert_order!(ll, n_node1, 11, node1, 1);
2457        assert_order!(ll, node2, 2, n_node1, 11);
2458        assert_order!(ll, n_node2, 22, node2, 2);
2459        assert_order!(ll, node3, 3, n_node2, 22);
2460        assert_order!(ll, n_node3, 33, node3, 3);
2461    }
2462
2463    #[test]
2464    fn test_append() {
2465        let mut ll = LinkedList::<u8>::with_capacity(4);
2466        for i in 0..4 {
2467            ll.push_tail(i);
2468        }
2469        assert_eq!(ll.len(), 4);
2470        assert_eq!(ll.capacity(), 4);
2471        assert_eq!(ll.fl.len(), 0);
2472
2473        let mut otherll = LinkedList::<u8>::with_capacity(10);
2474        for i in 0..10 {
2475            otherll.push_tail(i);
2476        }
2477        assert_eq!(otherll.len(), 10);
2478        assert_eq!(otherll.fl.len(), 0);
2479        assert_eq!(otherll.capacity(), 10);
2480
2481        ll.append(&mut otherll);
2482        assert_eq!(otherll.len(), 0);
2483        assert_eq!(otherll.capacity(), 0);
2484        assert_eq!(otherll.fl.len(), 0);
2485
2486        assert_eq!(ll.len(), 14);
2487        assert_eq!(ll.capacity(), 14);
2488        assert_eq!(ll.fl.len(), 0);
2489        for i in 0..4 {
2490            ll.push_tail(i + 100);
2491        }
2492
2493        assert_eq!(ll.len(), 18);
2494        assert_eq!(ll.capacity(), 18);
2495        assert_eq!(ll.fl.len(), 0);
2496        ll.push_head(111);
2497        assert_eq!(ll.len(), 19);
2498        assert_eq!(ll.capacity(), 26);
2499        assert_eq!(ll.fl.len(), 7);
2500        //now clear the lists
2501        ll.clear();
2502        assert_eq!(ll.len(), 0);
2503        assert_eq!(ll.capacity(), 26);
2504        assert_eq!(ll.fl.len(), 26);
2505        otherll.clear();
2506        assert_eq!(otherll.len(), 0);
2507        assert_eq!(otherll.capacity(), 0);
2508        assert_eq!(otherll.fl.len(), 0);
2509    }
2510
2511    #[test]
2512    fn test_append_handle() {
2513        let mut ll = LinkedList::<u8>::with_capacity(4);
2514        ll.push_tail(1);
2515        ll.push_tail(2);
2516        ll.push_tail(3);
2517
2518        let hnd: NodeHandle<u8>;
2519        {
2520            let mut other = LinkedList::<u8>::with_capacity(4);
2521            other.push_tail(4);
2522            hnd = other.push_tail(5);
2523            other.push_tail(6);
2524
2525            //its a valid handle
2526            assert_eq!(other.node(&hnd), Some(&5));
2527            ll.append(&mut other);
2528            //handle should now be invalid
2529            assert_eq!(other.node(&hnd), None);
2530            assert_eq!(other.len(), 0);
2531            assert!(other.is_empty());
2532        }
2533        assert_eq!(ll.node(&hnd), None);
2534        let mut count = 1;
2535        for val in ll.iter() {
2536            assert_eq!(val, &count);
2537            count += 1;
2538        }
2539    }
2540
2541    #[test]
2542    fn test_make_head() {
2543        let mut ll = LinkedList::<u8>::with_capacity(4);
2544        let hnd1 = ll.push_tail(1);
2545        let hnd2 = ll.push_tail(2);
2546        let hnd3 = ll.push_tail(3);
2547        assert!(ll.make_head(&hnd1));
2548        assert_order!(ll, hnd1, 1, hnd2, 2);
2549        assert_order!(ll, hnd2, 2, hnd3, 3);
2550
2551        assert_node!(ll, hnd1, FIRST, 1, 3);
2552        assert_node!(ll, hnd2, MIDDLE, 2, 3);
2553        assert_node!(ll, hnd3, LAST, 3, 3);
2554
2555        assert!(ll.make_head(&hnd2));
2556        assert_order!(ll, hnd2, 2, hnd1, 1);
2557        assert_order!(ll, hnd1, 1, hnd3, 3);
2558
2559        assert_node!(ll, hnd2, FIRST, 2, 3);
2560        assert_node!(ll, hnd1, MIDDLE, 1, 3);
2561        assert_node!(ll, hnd3, LAST, 3, 3);
2562
2563        assert!(ll.make_head(&hnd3));
2564        assert_order!(ll, hnd3, 3, hnd2, 2);
2565        assert_order!(ll, hnd2, 2, hnd1, 1);
2566
2567        assert_node!(ll, hnd3, FIRST, 3, 3);
2568        assert_node!(ll, hnd2, MIDDLE, 2, 3);
2569        assert_node!(ll, hnd1, LAST, 1, 3);
2570    }
2571
2572    #[test]
2573    fn test_make_tail() {
2574        let mut ll = LinkedList::<u8>::with_capacity(4);
2575        let hnd1 = ll.push_tail(1);
2576        let hnd2 = ll.push_tail(2);
2577        let hnd3 = ll.push_tail(3);
2578        assert!(ll.make_tail(&hnd3));
2579        assert_order!(ll, hnd1, 1, hnd2, 2);
2580        assert_order!(ll, hnd2, 2, hnd3, 3);
2581
2582        assert_node!(ll, hnd1, FIRST, 1, 3);
2583        assert_node!(ll, hnd2, MIDDLE, 2, 3);
2584        assert_node!(ll, hnd3, LAST, 3, 3);
2585
2586        assert!(ll.make_tail(&hnd2));
2587        assert_order!(ll, hnd1, 1, hnd3, 3);
2588        assert_order!(ll, hnd3, 3, hnd2, 2);
2589
2590        assert_node!(ll, hnd1, FIRST, 1, 3);
2591        assert_node!(ll, hnd3, MIDDLE, 3, 3);
2592        assert_node!(ll, hnd2, LAST, 2, 3);
2593
2594        assert!(ll.make_tail(&hnd1));
2595        assert_order!(ll, hnd3, 3, hnd2, 2);
2596        assert_order!(ll, hnd2, 2, hnd1, 1);
2597
2598        assert_node!(ll, hnd3, FIRST, 3, 3);
2599        assert_node!(ll, hnd2, MIDDLE, 2, 3);
2600        assert_node!(ll, hnd1, LAST, 1, 3);
2601    }
2602
2603    #[test]
2604    fn test_is_next() {
2605        let mut ll = LinkedList::<u8>::with_capacity(4);
2606        let hnd1 = ll.push_tail(1);
2607        let hnd2 = ll.push_tail(2);
2608        let hnd3 = ll.push_tail(3);
2609        ll.pop_tail();
2610        assert_eq!(ll.is_next(&hnd1, &hnd1), Some(false));
2611        assert_eq!(ll.is_next(&hnd1, &hnd2), Some(false));
2612        assert_eq!(ll.is_next(&hnd2, &hnd1), Some(true));
2613        assert_eq!(ll.is_next(&hnd2, &hnd3), None);
2614        assert_eq!(ll.is_next(&hnd1, &hnd3), None);
2615        assert_eq!(ll.is_next(&hnd3, &hnd3), None);
2616    }
2617
2618    #[test]
2619    fn test_is_prev() {
2620        let mut ll = LinkedList::<u8>::with_capacity(4);
2621        let hnd1 = ll.push_tail(1);
2622        let hnd2 = ll.push_tail(2);
2623        let hnd3 = ll.push_tail(3);
2624        ll.pop_tail();
2625        assert_eq!(ll.is_prev(&hnd1, &hnd1), Some(false));
2626        assert_eq!(ll.is_prev(&hnd1, &hnd2), Some(true));
2627        assert_eq!(ll.is_prev(&hnd2, &hnd1), Some(false));
2628        assert_eq!(ll.is_prev(&hnd2, &hnd3), None);
2629        assert_eq!(ll.is_prev(&hnd1, &hnd3), None);
2630        assert_eq!(ll.is_prev(&hnd3, &hnd3), None);
2631    }
2632
2633    #[test]
2634    fn test_swap_node() {
2635        let mut ll = LinkedList::<u8>::with_capacity(4);
2636        let hnd0 = ll.push_tail(0);
2637        let hnd1 = ll.push_tail(1);
2638        let hnd2 = ll.push_tail(2);
2639        let hnd3 = ll.push_tail(3);
2640
2641        ll.swap_node(&hnd0, &hnd3);
2642        assert_node!(ll, hnd3, FIRST, 3, 4);
2643        assert_node!(ll, hnd1, MIDDLE, 1, 4);
2644        assert_node!(ll, hnd2, MIDDLE, 2, 4);
2645        assert_node!(ll, hnd0, LAST, 0, 4);
2646
2647        assert_order!(ll, hnd3, 3, hnd1, 1);
2648        assert_order!(ll, hnd1, 1, hnd2, 2);
2649        assert_order!(ll, hnd2, 2, hnd0, 0);
2650
2651        ll.swap_node(&hnd1, &hnd2);
2652        assert_node!(ll, hnd3, FIRST, 3, 4);
2653        assert_node!(ll, hnd2, MIDDLE, 2, 4);
2654        assert_node!(ll, hnd1, MIDDLE, 1, 4);
2655        assert_node!(ll, hnd0, LAST, 0, 4);
2656
2657        assert_order!(ll, hnd3, 3, hnd2, 2);
2658        assert_order!(ll, hnd2, 2, hnd1, 1);
2659        assert_order!(ll, hnd1, 1, hnd0, 0);
2660
2661        ll.swap_node(&hnd3, &hnd2);
2662        assert_node!(ll, hnd2, FIRST, 2, 4);
2663        assert_node!(ll, hnd3, MIDDLE, 3, 4);
2664        assert_node!(ll, hnd1, MIDDLE, 1, 4);
2665        assert_node!(ll, hnd0, LAST, 0, 4);
2666
2667        assert_order!(ll, hnd2, 2, hnd3, 3);
2668        assert_order!(ll, hnd3, 3, hnd1, 1);
2669        assert_order!(ll, hnd1, 1, hnd0, 0);
2670
2671        ll.swap_node(&hnd1, &hnd0);
2672        assert_node!(ll, hnd2, FIRST, 2, 4);
2673        assert_node!(ll, hnd3, MIDDLE, 3, 4);
2674        assert_node!(ll, hnd0, MIDDLE, 0, 4);
2675        assert_node!(ll, hnd1, LAST, 1, 4);
2676
2677        assert_order!(ll, hnd2, 2, hnd3, 3);
2678        assert_order!(ll, hnd3, 3, hnd0, 0);
2679        assert_order!(ll, hnd0, 0, hnd1, 1);
2680
2681        ll.swap_node(&hnd3, &hnd2);
2682        assert_node!(ll, hnd3, FIRST, 3, 4);
2683        assert_node!(ll, hnd2, MIDDLE, 2, 4);
2684        assert_node!(ll, hnd0, MIDDLE, 0, 4);
2685        assert_node!(ll, hnd1, LAST, 1, 4);
2686
2687        assert_order!(ll, hnd3, 3, hnd2, 2);
2688        assert_order!(ll, hnd2, 2, hnd0, 0);
2689        assert_order!(ll, hnd0, 0, hnd1, 1);
2690
2691        ll.swap_node(&hnd1, &hnd0);
2692        assert_node!(ll, hnd3, FIRST, 3, 4);
2693        assert_node!(ll, hnd2, MIDDLE, 2, 4);
2694        assert_node!(ll, hnd1, MIDDLE, 1, 4);
2695        assert_node!(ll, hnd0, LAST, 0, 4);
2696
2697        assert_order!(ll, hnd3, 3, hnd2, 2);
2698        assert_order!(ll, hnd2, 2, hnd1, 1);
2699        assert_order!(ll, hnd1, 1, hnd0, 0);
2700    }
2701
2702    #[test]
2703    fn test_replace() {
2704        #[derive(Debug)]
2705        pub struct TestObj {
2706            int_v: u16,
2707        }
2708
2709        impl TestObj {
2710            fn new(int_v: u16) -> TestObj {
2711                return TestObj { int_v };
2712            }
2713        }
2714
2715        impl PartialEq for TestObj {
2716            fn eq(&self, other: &TestObj) -> bool {
2717                return self.int_v == other.int_v;
2718            }
2719        }
2720        impl Eq for TestObj {}
2721
2722        let mut ll = LinkedList::<TestObj>::with_capacity(4);
2723        let hnd0 = ll.push_tail(TestObj::new(0));
2724        let hnd1 = ll.push_tail(TestObj::new(1));
2725        let hnd2 = ll.push_tail(TestObj::new(2));
2726        let hnd3 = ll.push_tail(TestObj::new(3));
2727
2728        let old0 = ll.replace(&hnd0, TestObj::new(20));
2729        assert_eq!(old0.unwrap().int_v, 0);
2730        assert_node!(ll, hnd0, FIRST, TestObj::new(20), 4);
2731
2732        let old1 = ll.replace(&hnd1, TestObj::new(21));
2733        assert_eq!(old1.unwrap().int_v, 1);
2734        assert_node!(ll, hnd1, MIDDLE, TestObj::new(21), 4);
2735
2736        let old2 = ll.replace(&hnd2, TestObj::new(22));
2737        assert_eq!(old2.unwrap().int_v, 2);
2738        assert_node!(ll, hnd2, MIDDLE, TestObj::new(22), 4);
2739
2740        let old3 = ll.replace(&hnd3, TestObj::new(23));
2741        assert_eq!(old3.unwrap().int_v, 3);
2742        assert_node!(ll, hnd3, LAST, TestObj::new(23), 4);
2743    }
2744}