1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use std::sync::Arc;

#[derive(Clone)]
pub struct DigraphNode<T> {
    pub next: DigraphNodeRef<T>, // I made it `pub` to be able `item.next.next()` to remove an item from the middle.
    data: T,
}

impl<T> DigraphNode<T> {
    fn new(next: DigraphNodeRef<T>, data: T) -> Self {
        Self { next, data }
    }
}

pub struct DigraphNodeRef<T> {
    rc: Option<Arc<DigraphNode<T>>>,
}

impl<T> DigraphNodeRef<T> {
    pub fn new() -> Self {
        Self {
            rc: None
        }
    }
    pub fn from_node(value: DigraphNode<T>) -> Self {
        Self::from(Some(Arc::new(value)))
    }
    pub fn from(rc: Option<Arc<DigraphNode<T>>>) -> Self {
        Self {
            rc
        }
    }
    pub fn remove(&mut self) -> bool {
        if let Some(rc) = self.rc.clone() {
            self.rc = rc.next.rc.clone();
            true
        } else {
            false
        }
    }
    pub fn prepend(&mut self, value: T) -> Self {
        let new_node = DigraphNode::new(self.clone(), value);
        let new_node_ref = DigraphNodeRef::from_node(new_node);
        *self = new_node_ref.clone();
        new_node_ref
    }
    pub fn node(&self) -> Option<DigraphNode<T>>
        where T: Clone
    {
        self.rc.clone().map(|node| (*node).clone())
    }
    /// TODO: Should return a reference.
    pub fn data(&self) -> Option<T>
        where T: Clone
    {
        self.rc.clone().map(|node| (*node).data.clone())
    }
    pub fn values(self) -> DigraphNodeValuesIterator<T> {
        DigraphNodeValuesIterator {
            underlying: self.clone()
        }
    }
    pub fn is_empty(&self) -> bool {
        self.rc.is_none()
    }
}

impl<T> Clone for DigraphNodeRef<T> {
    fn clone(&self) -> Self {
        Self { rc: self.rc.clone() }
    }
}

impl<T> Iterator for DigraphNodeRef<T> {
    type Item = Arc<DigraphNode<T>>;

    fn next(&mut self) -> Option<Self::Item> {
        if let Some(rc) = self.rc.clone() {
            self.rc = rc.next.rc.clone();
            Some(rc.clone())
        } else {
            None
        }
    }
}

pub struct DigraphNodeValuesIterator<T> {
    underlying: DigraphNodeRef<T>,
}

impl<T: Clone> Iterator for DigraphNodeValuesIterator<T> {
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        self.underlying.next().map(|node| node.data.clone())
    }
}

#[cfg(test)]
mod tests {
    use crate::DigraphNodeRef;

    #[test]
    fn insert() {
        let mut list = DigraphNodeRef::new();
        for i in 0..10 {
            list.prepend(i);
        }
        assert_eq!(list.values().collect::<Vec<i32>>(), (0..10).rev().collect::<Vec<i32>>());
    }

    #[test]
    fn remove() {
        let mut list = DigraphNodeRef::new();
        for i in 0..10 {
            list.prepend(i);
        }
        for _ in 0..5 {
            list.remove();
        }
        assert_eq!(list.values().collect::<Vec<i32>>(), (0..5).rev().collect::<Vec<i32>>());
    }
}