orx_linked_list/iter/
doubly_iter_ptr.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use crate::Doubly;
use core::iter::FusedIterator;
use orx_pinned_vec::PinnedVec;
use orx_selfref_col::{CoreCol, Node, NodePtr};

/// An ordered iterator over pointers to the elements of the doubly linked list.
///
/// Can be created by calling the `iter_ptr` method.
pub struct DoublyIterPtr<'a, T, P>
where
    P: PinnedVec<Node<Doubly<T>>>,
{
    pub(crate) col: &'a CoreCol<Doubly<T>, P>,
    current: Option<NodePtr<Doubly<T>>>,
    current_back: Option<NodePtr<Doubly<T>>>,
}

impl<'a, T, P> DoublyIterPtr<'a, T, P>
where
    P: PinnedVec<Node<Doubly<T>>>,
{
    pub(crate) fn new(
        col: &'a CoreCol<Doubly<T>, P>,
        current: Option<NodePtr<Doubly<T>>>,
        current_back: Option<NodePtr<Doubly<T>>>,
    ) -> Self {
        Self {
            col,
            current,
            current_back,
        }
    }

    pub(crate) fn end(&mut self) {
        self.current = None;
        self.current_back = None;
    }
}

impl<'a, T, P> Iterator for DoublyIterPtr<'a, T, P>
where
    P: PinnedVec<Node<Doubly<T>>>,
{
    type Item = NodePtr<Doubly<T>>;

    fn next(&mut self) -> Option<Self::Item> {
        match &self.current {
            Some(p) => {
                let ptr = Some(p.clone());
                match self.current == self.current_back {
                    false => self.current = self.col.node(p).next().get(),
                    true => self.end(),
                }

                ptr
            }
            None => None,
        }
    }
}

impl<'a, T, P> DoubleEndedIterator for DoublyIterPtr<'a, T, P>
where
    P: PinnedVec<Node<Doubly<T>>>,
{
    fn next_back(&mut self) -> Option<Self::Item> {
        match &self.current_back {
            Some(p) => {
                let ptr = Some(p.clone());

                match self.current == self.current_back {
                    false => self.current_back = self.col.node(p).prev().get(),
                    true => self.end(),
                }

                ptr
            }
            None => None,
        }
    }
}

impl<'a, T, P> FusedIterator for DoublyIterPtr<'a, T, P> where P: PinnedVec<Node<Doubly<T>>> {}

impl<'a, T, P> Clone for DoublyIterPtr<'a, T, P>
where
    P: PinnedVec<Node<Doubly<T>>>,
{
    fn clone(&self) -> Self {
        Self {
            col: self.col,
            current: self.current.clone(),
            current_back: self.current_back.clone(),
        }
    }
}