deepmesa_collections/linkedlist/
node.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::list::LinkedList;
23use core::ptr;
24
25#[derive(Debug, PartialEq, Eq)]
26pub(crate) struct InternalNode<T> {
27    pub(super) val: T,
28    pub(super) fl_node: bool,
29    pub(super) nid: usize,
30    pub(super) prev: *mut InternalNode<T>,
31    pub(super) next: *mut InternalNode<T>,
32}
33
34/// A handle to a node in the [`LinkedList`](../struct.LinkedList.html).
35///
36/// This struct wraps a raw pointer to memory but does not implement
37/// the [`Deref`](https://doc.rust-lang.org/std/ops/trait.Deref.html) trait so you cannot dereference that pointer directly.
38/// Handles can be used only by methods of the linkedlist that they
39/// were obtained from. They can be copied and passed around by value
40/// regardless of the lifetime of the linkedlist. Once the element
41/// that the handle refers to is removed from the linked list, the
42/// handle then becomes invalid. Passing an invalid handle into the
43/// linked list is safe since all methods that accept a reference to a
44/// handle return `None` if the handle is invalid.
45///
46
47/// The
48/// [`push_head()`](../struct.LinkedList.html#method.push_head),
49/// [`push_tail()`](../struct.LinkedList.html#method.push_tail),
50/// [`push_next()`](../struct.LinkedList.html#method.push_next)
51/// and
52/// [`push_prev()`](../struct.LinkedList.html#method.push_prev)
53/// methods of [`LinkedList`](../struct.LinkedList.html)
54/// return handles to the nodes pushed to the linked list. Handles can
55/// only be used by passing them as arguments to the
56/// [`next()`](../struct.LinkedList.html#method.next),
57/// [`next_mut()`](../struct.LinkedList.html#method.next_mut),
58/// [`prev()`](../struct.LinkedList.html#method.prev),
59/// [`prev_mut()`](../struct.LinkedList.html#method.prev_mut),
60/// [`prev_node()`](../struct.LinkedList.html#method.prev_node),
61/// [`next_node()`](../struct.LinkedList.html#method.next_node),
62/// [`node()`](../struct.LinkedList.html#method.node),
63/// [`node_mut()`](../struct.LinkedList.html#method.node_mut),
64/// [`has_next()`](../struct.LinkedList.html#method.has_next),
65/// [`has_prev()`](../struct.LinkedList.html#method.has_prev),
66/// [`pop_next()`](../struct.LinkedList.html#method.pop_next),
67/// [`pop_prev()`](../struct.LinkedList.html#method.pop_prev),
68/// [`pop_node()`](../struct.LinkedList.html#method.pop_node),
69/// [`push_next()`](../struct.LinkedList.html#method.push_next),
70/// [`push_prev()`](../struct.LinkedList.html#method.push_prev),
71/// methods of the list. This allows adding, removing and mutating
72/// elements in the middle of the list in *O*(*1*) time.
73
74///
75/// Handles can be copied, cloned and passed around by value or
76/// reference without regard to the lifetime of the list. When an
77/// element is removed from the list, the handle associated with that
78/// node becomes invalid forever. Passing an invalid handle to the
79/// list is safe since all methods that accept a reference to a handle
80/// return None if the handle is invalid.
81///
82/// ### Example
83/// ```
84/// use deepmesa_collections::LinkedList;
85/// let mut list = LinkedList::<u8>::with_capacity(10);
86/// list.push_head(1);
87/// let middle = list.push_head(100);
88/// list.push_head(2);
89///
90/// // get the value of the node in the middle of the list in *O*(*1*)
91/// // time.
92/// assert_eq!(list.node(&middle), Some(&100));
93/// // remove the middle node in *O*(*1*) time
94/// list.pop_node(&middle);
95/// // once the middle node is removed, the handle is invalid
96/// assert_eq!(list.node(&middle), None);
97/// assert_eq!(list.len(), 2);
98/// ```
99///
100/// [`NodeHandle<T>`] implements the [`Default`] trait so you can store
101/// default (invalid) handles in a struct and assign them later.
102/// ### Example
103/// ```
104/// use deepmesa_collections::LinkedList;
105/// use deepmesa_collections::linkedlist::NodeHandle;
106///
107/// struct MyStruct<T> {
108///    handle: NodeHandle<T>
109/// };
110///
111/// let mut s = MyStruct::<u8>{
112///     handle: NodeHandle::<u8>::default()
113/// };
114///
115/// let mut list = LinkedList::<u8>::with_capacity(10);
116/// // The default handle is invalid
117/// assert_eq!(list.node(&s.handle), None);
118/// // push a new element and store the handle
119/// s.handle = list.push_head(1);
120/// assert_eq!(list.node(&s.handle), Some(&1));
121/// ```
122#[derive(Debug, PartialEq, Eq, Copy)]
123pub struct NodeHandle<T> {
124    pub(super) cid: usize,
125    pub(super) nid: usize,
126    pub(super) ptr: *mut InternalNode<T>,
127}
128
129unsafe impl<T> Send for NodeHandle<T> {}
130unsafe impl<T> Sync for NodeHandle<T> {}
131
132impl<T> Clone for NodeHandle<T> {
133    fn clone(&self) -> Self {
134        Self { ..*self }
135    }
136}
137
138impl<T> Default for NodeHandle<T> {
139    fn default() -> Self {
140        Self {
141            cid: 0,
142            nid: 0,
143            ptr: ptr::null_mut(),
144        }
145    }
146}
147
148impl<T> InternalNode<T> {
149    pub(super) fn new(val: T, nid: usize) -> InternalNode<T> {
150        InternalNode {
151            val,
152            fl_node: false,
153            nid,
154            next: ptr::null_mut(),
155            prev: ptr::null_mut(),
156        }
157    }
158}
159
160impl<T> NodeHandle<T> {
161    pub(super) fn new(cid: usize, nid: usize, ptr: *mut InternalNode<T>) -> NodeHandle<T> {
162        NodeHandle { cid, nid, ptr }
163    }
164
165    /// Returns `Some(true)` if the specified node is the head of the
166    /// list and `Some(false)` if its not. If the specified node is
167    /// invalid then this method returns `None`. This method simply
168    /// calls
169    /// [`LinkedList::is_head()`](../struct.LinkedList.html#method.is_head)
170    ///
171    /// This method should complete in *O*(*1*) time.
172    ///
173    /// # Examples
174    /// ```
175    /// use deepmesa_collections::LinkedList;
176    /// let mut list = LinkedList::<u8>::with_capacity(4);
177    /// let hnd0 = list.push_tail(0);
178    /// let hnd1 = list.push_tail(1);
179    /// let hnd2 = list.push_tail(2);
180    /// list.pop_tail();
181    /// assert_eq!(hnd0.is_head(&list), Some(true));
182    /// assert_eq!(hnd1.is_head(&list), Some(false));
183    /// assert_eq!(hnd2.is_head(&list), None);
184    /// ```
185    pub fn is_head(&self, list: &LinkedList<T>) -> Option<bool> {
186        list.is_head(self)
187    }
188
189    /// Returns `true` if the specified node is the tail of the list
190    /// and `false` if its not. If the specified node is invalid, then
191    /// this method returns `None`. This method simply calls
192    /// [`LinkedList::is_tail()`](../struct.LinkedList.html#method.is_tail)
193    ///
194    /// This method should complete in *O*(*1*) time.
195    ///
196    /// # Examples
197    /// ```
198    /// use deepmesa_collections::LinkedList;
199    /// let mut list = LinkedList::<u8>::with_capacity(4);
200    /// let hnd0 = list.push_tail(0);
201    /// let hnd1 = list.push_tail(1);
202    /// let hnd2 = list.push_tail(2);
203    /// list.pop_tail();
204    /// assert_eq!(hnd0.is_tail(&list), Some(false));
205    /// assert_eq!(hnd1.is_tail(&list), Some(true));
206    /// assert_eq!(hnd2.is_tail(&list), None);
207    /// ```
208    pub fn is_tail(&self, list: &LinkedList<T>) -> Option<bool> {
209        list.is_tail(self)
210    }
211
212    /// Returns `true` if the specified node is immediately previous
213    /// to `other` and `false` otherwise. If either of the nodes is
214    /// invalid this method returns None. This method simply calls
215    /// [`LinkedList::is_prev()`](../struct.LinkedList.html#method.is_prev)
216    ///
217    /// This method should return in *O*(*1*) time.
218    ///
219    /// # Examples
220    /// ```
221    /// use deepmesa_collections::LinkedList;
222    /// let mut list = LinkedList::<u8>::with_capacity(4);
223    /// let hnd0 = list.push_tail(0);
224    /// let hnd1 = list.push_tail(1);
225    /// let hnd2 = list.push_tail(2);
226    /// list.pop_tail();
227    /// assert_eq!(hnd0.is_prev(&hnd1, &list), Some(true));
228    /// assert_eq!(hnd1.is_prev(&hnd0, &list), Some(false));
229    /// assert_eq!(hnd1.is_prev(&hnd2, &list), None);
230    /// ```
231    pub fn is_prev(&self, other: &NodeHandle<T>, list: &LinkedList<T>) -> Option<bool> {
232        list.is_prev(self, other)
233    }
234
235    /// Returns `true` if the specified node is immediately after
236    /// `other` and `false` otherwise. If either of the nodes is
237    /// invalid, this method returns None. This method simply calls
238    /// [`LinkedList::is_next()`](../struct.LinkedList.html#method.is_next)
239    ///
240    /// This method should complete in *O*(*1*) time.
241    ///
242    /// # Examples
243    /// ```
244    /// use deepmesa_collections::LinkedList;
245    /// let mut list = LinkedList::<u8>::with_capacity(4);
246    /// let hnd0 = list.push_tail(0);
247    /// let hnd1 = list.push_tail(1);
248    /// let hnd2 = list.push_tail(2);
249    /// list.pop_tail();
250    /// assert_eq!(hnd1.is_next(&hnd0, &list), Some(true));
251    /// assert_eq!(hnd0.is_next(&hnd1, &list), Some(false));
252    /// assert_eq!(hnd2.is_next(&hnd1, &list), None);
253    /// ```
254    pub fn is_next(&self, other: &NodeHandle<T>, list: &LinkedList<T>) -> Option<bool> {
255        list.is_next(self, other)
256    }
257
258    /// Returns true if the node associated with the specified handle
259    /// has a next node and false if it does not. If the specified
260    /// handle is invalid this method returns None. This method simply
261    /// calls
262    /// [`LinkedList::has_next()`](../struct.LinkedList.html#method.has_next)
263    ///
264    ///This method should complete in *O*(*1*) time.
265    ///
266    /// # Examples
267    /// ```
268    /// use deepmesa_collections::LinkedList;
269    /// let mut list = LinkedList::<u8>::with_capacity(10);
270    /// let node1 = list.push_head(1);
271    /// let node2 = list.push_head(2);
272    ///
273    /// assert_eq!(node1.has_next(&list), Some(false));
274    /// assert_eq!(node2.has_next(&list), Some(true));
275    ///
276    /// list.pop_head();
277    /// assert_eq!(node1.has_next(&list), Some(false));
278    /// // once the head is popped node2 becomes an invalid handle
279    /// assert_eq!(node2.has_next(&list), None);
280    /// ```
281    pub fn has_next(&self, list: &LinkedList<T>) -> Option<bool> {
282        list.has_next(self)
283    }
284
285    /// Returns true if the node associated with the specified handle
286    /// has a previous node and false if it does not. If the specified
287    /// handle is invalid this method returns None. This method simply
288    /// calls
289    /// [`LinkedList::has_prev()`](../struct.LinkedList.html#method.has_prev)
290    ///
291    ///This method should complete in *O*(*1*) time.
292    ///
293    /// # Examples
294    /// ```
295    /// use deepmesa_collections::LinkedList;
296    /// let mut list = LinkedList::<u8>::with_capacity(10);
297    /// let node1 = list.push_head(1);
298    /// let node2 = list.push_head(2);
299    ///
300    /// assert_eq!(node1.has_prev(&list), Some(true));
301    /// assert_eq!(node2.has_prev(&list), Some(false));
302    ///
303    /// list.pop_head();
304    /// assert_eq!(node1.has_prev(&list), Some(false));
305    /// // once the head is popped node2 becomes an invalid handle
306    /// assert_eq!(node2.has_next(&list), None);
307    /// ```
308    pub fn has_prev(&self, list: &LinkedList<T>) -> Option<bool> {
309        list.has_prev(self)
310    }
311
312    /// Returns a reference to the value of the node immediately after
313    /// the node associated with this handle. If this handle is
314    /// invalid in the specified list or there is no next node, this
315    /// method returns None. This method simply calls
316    /// [`LinkedList::next()`](../struct.LinkedList.html#method.next)
317    ///
318    /// This method should complete in *O*(*1*) time.
319    ///
320    /// # Examples
321    /// ```
322    /// use deepmesa_collections::LinkedList;
323    /// let mut list = LinkedList::<u8>::with_capacity(10);
324    /// list.push_head(1);
325    /// let node = list.push_head(2);
326    ///
327    /// assert_eq!(node.next(&list), Some(&1));
328    ///
329    /// list.pop_tail();
330    /// // once the tail is popped, there is no next
331    /// assert_eq!(node.next(&list), None);
332    /// ```
333    pub fn next<'a>(&self, list: &'a LinkedList<T>) -> Option<&'a T> {
334        list.next(self)
335    }
336
337    /// Returns a mutable reference to the value of the node
338    /// immediately after the node associated with this handle. If the
339    /// this handle is invalid in the specified list or if there is no
340    /// next node, this method returns None. This method simply calls
341    /// [`LinkedList::next_mut()`](../struct.LinkedList.html#method.next_mut)
342    ///
343    /// This method should complete in *O*(*1*) time.
344    ///
345    /// # Examples
346    /// ```
347    /// use deepmesa_collections::LinkedList;
348    /// let mut list = LinkedList::<u8>::with_capacity(10);
349    /// list.push_head(1);
350    /// let node = list.push_head(2);
351    /// assert_eq!(node.next(&list), Some(&1));
352    ///
353    /// match node.next_mut(&mut list) {
354    ///     None => {},
355    ///     Some(x) => *x = 100,
356    /// }
357    ///
358    /// assert_eq!(node.next(&list), Some(&100));
359    /// ```
360    pub fn next_mut<'a>(&self, list: &'a mut LinkedList<T>) -> Option<&'a mut T> {
361        list.next_mut(self)
362    }
363
364    /// Returns a reference to the value of the node immediately
365    /// preceeding the node associated with this handle.  If this
366    /// handle is invalid in the specified list or if there is no
367    /// preceeding node, this method returns None. This method simply
368    /// calls
369    /// [`LinkedList::prev()`](../struct.LinkedList.html#method.prev)
370    ///
371    /// This method should complete in *O*(*1*) time.
372    ///
373    /// # Examples
374    /// ```
375    /// use deepmesa_collections::LinkedList;
376    /// let mut list = LinkedList::<u8>::with_capacity(10);
377    /// let node = list.push_head(1);
378    /// list.push_head(2);
379    ///
380    /// assert_eq!(node.prev(&list), Some(&2));
381    ///
382    /// list.pop_head();
383    /// // once the head is popped, there is no prev
384    /// assert_eq!(node.prev(&list), None);
385    /// ```
386    pub fn prev<'a>(&self, list: &'a LinkedList<T>) -> Option<&'a T> {
387        list.prev(self)
388    }
389
390    /// Returns a mutable reference to the value of the node
391    /// immediately preceeding the node associated with this
392    /// handle. If this handle is invalid in the specified list or
393    /// there is no preceeding node, this method returns None. This
394    /// method simply calls
395    /// [`LinkedList::prev_mut()`](../struct.LinkedList.html#method.prev_mut)
396    ///
397    /// This method should complete in *O*(*1*) time.
398    ///
399    /// # Examples
400    /// ```
401    /// use deepmesa_collections::LinkedList;
402    /// let mut list = LinkedList::<u8>::with_capacity(10);
403    /// let node = list.push_head(1);
404    /// list.push_head(2);
405    /// assert_eq!(node.prev(&list), Some(&2));
406    ///
407    /// match node.prev_mut(&mut list) {
408    ///     None => {},
409    ///     Some(x) => *x = 100,
410    /// }
411    ///
412    /// assert_eq!(node.prev(&list), Some(&100));
413    /// ```
414    pub fn prev_mut<'a>(&self, list: &'a mut LinkedList<T>) -> Option<&'a mut T> {
415        list.prev_mut(self)
416    }
417
418    /// Returns a handle to the node immediately preceeding the node
419    /// associated with this handle. If this handle is invalid in the
420    /// specified list or if there is no preceeding node, this method
421    /// returns None. This method simply calls
422    /// [`LinkedList::next_node()`](../struct.LinkedList.html#method.next_node)
423    ///
424    /// This method should complete in *O*(*1*) time.
425    ///
426    /// # Examples
427    /// ```
428    /// use deepmesa_collections::LinkedList;
429    /// let mut list = LinkedList::<u8>::with_capacity(10);
430    /// list.push_head(1);
431    /// let node = list.push_head(2);
432    ///
433    /// match node.next_node(&list) {
434    ///     None => {},
435    ///     Some(n) => assert_eq!(n.val(&list), Some(&1)),
436    /// }
437    ///
438    /// list.pop_tail();
439    /// // Once the tail node is popped, there is no next node
440    /// assert_eq!(node.next_node(&list), None);
441    /// ```
442    pub fn next_node(&self, list: &LinkedList<T>) -> Option<NodeHandle<T>> {
443        list.next_node(self)
444    }
445
446    /// Returns a handle to the node immediately preceeding the node
447    /// associated with this handle. If this handle is invalid in the
448    /// specified list or if there is no preceeding node, this method
449    /// returns None. This method simply calls
450    /// [`LinkedList::prev_node()`](../struct.LinkedList.html#method.prev_node)
451    ///
452    /// This method should complete in *O*(*1*) time.
453    ///
454    /// # Examples
455    /// ```
456    /// use deepmesa_collections::LinkedList;
457    /// let mut list = LinkedList::<u8>::with_capacity(10);
458    /// let node = list.push_head(1);
459    /// list.push_head(2);
460    ///
461    /// assert_eq!(node.prev(&list), Some(&2));
462    ///
463    /// list.pop_head();
464    /// //once the head is popped there is no prev node
465    /// assert_eq!(node.prev(&list), None);
466    /// ```
467    pub fn prev_node(&self, list: &LinkedList<T>) -> Option<NodeHandle<T>> {
468        list.prev_node(self)
469    }
470
471    /// Returns a reference to the value of the node associated with
472    /// this handle.  If this handle is invalid in the specified list,
473    /// this method returns None. This method simply calls
474    /// [`LinkedList::node()`](../struct.LinkedList.html#method.node)
475    ///
476    /// This method should complete in *O*(*1*) time.
477    ///
478    /// # Examples
479    /// ```
480    /// use deepmesa_collections::LinkedList;
481    /// let mut list = LinkedList::<u8>::with_capacity(10);
482    /// let node = list.push_head(1);
483    ///
484    /// assert_eq!(node.val(&list), Some(&1));
485    ///
486    /// list.pop_head();
487    /// // once the node is popped the handle becomes invalid
488    /// assert_eq!(node.val(&list), None);
489    /// ```
490    pub fn val<'a>(&self, list: &'a LinkedList<T>) -> Option<&'a T> {
491        list.node(self)
492    }
493
494    /// Returns a mutable reference to the value of the node
495    /// associated with this handle.  If this handle is invalid in the
496    /// specified list, this method returns None. This method simply
497    /// calls
498    /// [`LinkedList::node_mut()`](../struct.LinkedList.html#method.node_mut)
499    ///
500    /// This method should complete in *O*(*1*) time.
501    ///
502    /// # Examples
503    /// ```
504    /// use deepmesa_collections::LinkedList;
505    /// let mut list = LinkedList::<u8>::with_capacity(10);
506    /// let node = list.push_head(1);
507    ///
508    /// assert_eq!(node.val(&list), Some(&1));
509    ///
510    /// match node.val_mut(&mut list) {
511    ///     None => {},
512    ///     Some(x) => *x = 100,
513    /// }
514    ///
515    /// assert_eq!(node.val(&list), Some(&100));
516    /// ```
517    pub fn val_mut<'a>(&self, list: &'a mut LinkedList<T>) -> Option<&'a mut T> {
518        list.node_mut(self)
519    }
520
521    //TODO: Write docs
522    pub fn replace<'a>(&self, list: &'a mut LinkedList<T>, val: T) -> Option<T> {
523        list.replace(self, val)
524    }
525
526    /// Removes and returns the value of the node immediately after
527    /// the node associated with this handle. If this handle is
528    /// invalid in the specified list or there is no next node, then
529    /// this method returns None. This method simply calls
530    /// [`LinkedList::pop_next()`](../struct.LinkedList.html#method.pop_next)
531    ///
532    /// This operation should complete in *O*(*1*) time
533    ///
534    /// # Examples
535    /// ```
536    /// use deepmesa_collections::LinkedList;
537    /// let mut list = LinkedList::<u8>::with_capacity(10);
538    ///
539    /// list.push_head(1);
540    /// list.push_head(2);
541    /// let node = list.push_head(3);
542    /// assert_eq!(node.pop_next(&mut list), Some(2));
543    /// assert_eq!(node.pop_next(&mut list), Some(1));
544    /// assert_eq!(node.pop_next(&mut list), None);
545    /// ```
546    pub fn pop_next(&self, list: &mut LinkedList<T>) -> Option<T> {
547        list.pop_next(self)
548    }
549
550    /// Removes and returns the value of the node immediately
551    /// preceeding the node associated with this handle. If this
552    /// handle is invalid in the specified list or there is no
553    /// previous node, then this method returns None. This method
554    /// simply calls
555    /// [`LinkedList::pop_next()`](../struct.LinkedList.html#method.pop_next)
556    ///
557    /// This operation should complete in *O*(*1*) time
558    ///
559    /// # Examples
560    /// ```
561    /// use deepmesa_collections::LinkedList;
562    /// let mut list = LinkedList::<u8>::with_capacity(10);
563    ///
564    /// let node = list.push_head(1);
565    /// list.push_head(2);
566    /// list.push_head(3);
567    /// assert_eq!(node.pop_prev(&mut list), Some(2));
568    /// assert_eq!(node.pop_prev(&mut list), Some(3));
569    /// assert_eq!(node.pop_prev(&mut list), None);
570    /// ```
571    pub fn pop_prev(&self, list: &mut LinkedList<T>) -> Option<T> {
572        list.pop_prev(self)
573    }
574
575    /// Removes and returns the value of the node associated with this
576    /// handle. If this handle is invalid in the specified list then
577    /// this method returns None. This method simply calls
578    /// [`LinkedList::pop_node()`](../struct.LinkedList.html#method.pop_node)
579    ///
580    /// This operation should complete in *O*(*1*) time
581    ///
582    /// # Examples
583    /// ```
584    /// use deepmesa_collections::LinkedList;
585    /// let mut list = LinkedList::<u8>::with_capacity(10);
586    ///
587    /// let node = list.push_head(1);
588    /// assert_eq!(node.pop(&mut list), Some(1));
589    /// assert_eq!(node.pop(&mut list), None);
590    /// ```
591    pub fn pop(&self, list: &mut LinkedList<T>) -> Option<T> {
592        list.pop_node(self)
593    }
594
595    /// Adds an element immediately after the node associated with
596    /// this handle. Returns the handle to the node thats been added
597    /// or None if this handle is invalid in the specified list. This
598    /// method simply calls
599    /// [`LinkedList::push_next_()`](../struct.LinkedList.html#method.push_next)
600    ///
601    /// This operation should complete in *O*(*1*) time.
602    ///
603    /// # Examples
604    ///
605    /// ```
606    /// use deepmesa_collections::LinkedList;
607    /// let mut list = LinkedList::<u8>::with_capacity(10);
608    ///
609    /// list.push_head(0);
610    /// let middle = list.push_head(1);
611    /// list.push_head(2);
612    /// middle.push_next(100, &mut list);
613    ///
614    /// let mut iter = list.iter();
615    /// assert_eq!(iter.next(), Some(&2));
616    /// assert_eq!(iter.next(), Some(&1));
617    /// assert_eq!(iter.next(), Some(&100));
618    /// assert_eq!(iter.next(), Some(&0));
619    /// assert_eq!(iter.next(), None);
620    /// ```
621    pub fn push_next(&self, elem: T, list: &mut LinkedList<T>) -> Option<NodeHandle<T>> {
622        list.push_next(self, elem)
623    }
624
625    /// Adds an element immediately preceedeing the node associated
626    /// with this handle. Returns the handle to the node thats been
627    /// added or None if this handle is invalid in the specified
628    /// list. This method simply calls
629    /// [`LinkedList::push_prev_()`](../struct.LinkedList.html#method.push_prev)
630    ///
631    /// This operation should complete in *O*(*1*) time.
632    ///
633    /// # Examples
634    ///
635    /// ```
636    /// use deepmesa_collections::LinkedList;
637    /// let mut list = LinkedList::<u8>::with_capacity(10);
638    ///
639    /// list.push_head(0);
640    /// let middle = list.push_head(1);
641    /// list.push_head(2);
642    /// middle.push_prev(100, &mut list);
643    ///
644    /// let mut iter = list.iter();
645    /// assert_eq!(iter.next(), Some(&2));
646    /// assert_eq!(iter.next(), Some(&100));
647    /// assert_eq!(iter.next(), Some(&1));
648    /// assert_eq!(iter.next(), Some(&0));
649    /// assert_eq!(iter.next(), None);
650    /// ```
651    pub fn push_prev(&self, elem: T, list: &mut LinkedList<T>) -> Option<NodeHandle<T>> {
652        list.push_prev(self, elem)
653    }
654
655    /// Moves the node associated with this handle to the front (head)
656    /// of the list. If the node is already at the head of the list
657    /// then this operation has no effect.
658    ///
659    /// Returns true if the node is successfully moved to the head of
660    /// the list (or if it was already at the head) and false if this
661    /// handle is invalid in the specified list. This method simply
662    /// calls
663    /// [`LinkedList::make_head()`](../struct.LinkedList.html#method.make_head)
664    ///
665    /// This operation should complete in *O*(*1*) time.
666    ///
667    /// # Examples
668    ///
669    /// ```
670    /// use deepmesa_collections::LinkedList;
671    /// let mut list = LinkedList::<u8>::with_capacity(3);
672    /// let hnd0 = list.push_tail(0);
673    /// let hnd1 = list.push_tail(1);
674    /// let hnd2 = list.push_tail(2);
675    ///
676    /// assert_eq!(list.head(), Some(&0));
677    /// hnd2.make_head(&mut list);
678    /// assert_eq!(list.head(), Some(&2));
679    /// ```
680    pub fn make_head(&self, list: &mut LinkedList<T>) -> bool {
681        list.make_head(self)
682    }
683
684    /// Moves the node associated with this handle to the back (tail)
685    /// of the list. If the node is already at the tail of the list
686    /// then this operation has no effect.
687    ///
688    /// Returns true if the node is successfully moved to the tail of
689    /// the list (or if it was already at the tail) and false if this
690    /// handle is invalid in the specified list. This method simply
691    /// calls
692    /// [`LinkedList::make_tail()`](../struct.LinkedList.html#method.make_tail)
693    ///
694    /// This operation should complete in *O*(*1*) time.
695    ///
696    /// # Examples
697    ///
698    /// ```
699    /// use deepmesa_collections::LinkedList;
700    /// let mut list = LinkedList::<u8>::with_capacity(3);
701    /// let hnd0 = list.push_tail(0);
702    /// let hnd1 = list.push_tail(1);
703    /// let hnd2 = list.push_tail(2);
704    ///
705    /// assert_eq!(list.tail(), Some(&2));
706    /// hnd0.make_tail(&mut list);
707    /// assert_eq!(list.tail(), Some(&0));
708    /// ```
709    pub fn make_tail(&self, list: &mut LinkedList<T>) -> bool {
710        list.make_tail(self)
711    }
712
713    /// Swaps the position of the node associated with this handle in
714    /// the specified list with the position of `other` and returns
715    /// true on success. If either node is invalid in the specified
716    /// list then this method returns false. This method simply calls
717    /// [`LinkedList::swap_node()`](../struct.LinkedList.html#method.swap_node)
718    ///
719    /// This method should complete in *O*(*1*) time.
720    ///
721    /// # Examples
722    /// ```
723    /// use deepmesa_collections::LinkedList;
724    /// let mut list = LinkedList::<u8>::with_capacity(4);
725    /// let hnd0 = list.push_tail(0);
726    /// let hnd1 = list.push_tail(1);
727    /// let hnd2 = list.push_tail(2);
728    /// list.pop_tail();
729    /// assert_eq!(hnd0.swap_node(&hnd1, &mut list), true);
730    /// assert_eq!(hnd1.swap_node(&hnd2, &mut list), false);
731    /// assert_eq!(hnd1.is_head(&list), Some(true));
732    /// assert_eq!(hnd0.is_tail(&list), Some(true));
733    /// ```
734    pub fn swap_node(&self, other: &NodeHandle<T>, list: &mut LinkedList<T>) -> bool {
735        list.swap_node(self, other)
736    }
737}