1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
mod api;
mod test;

use alloc::collections::{BTreeMap, BTreeSet};
use core::default::Default;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

pub use api::*;
use crate::error::Error;

/// `BTreeDag` is an implementation of a directed acyclic graph (abstract data structure)
/// which utilizes `BTreeMap` for the vertex adjacency list.
#[derive(PartialEq, Eq, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct BTreeDag<T>
where
    T: Ord,
{
    vertices: BTreeMap<T, BTreeSet<T>>,
}

impl<T> BTreeDag<T>
where
    T: Ord,
{
    pub fn new() -> Self {
        let vertices: BTreeMap<T, BTreeSet<T>> = BTreeMap::new();
        BTreeDag { vertices }
    }

    fn cyclic_relationship_exists(&self, x: &T, y: &T) -> Result<(), Error> {
        if let Some(adj_y) = self.vertices.get(y) {
            // If y has adjacent vertices, then have we need to
            // check if x exists in these adjacent vertices;
            if !adj_y.contains(x) {
                // if it does not, then recurse. Making sure x
                // is not adjacent to any of y's adjacent vertices.
                for adj in adj_y {
                    self.cyclic_relationship_exists(x, adj)?;
                }
                // If no error has been thrown by this line, then
                // we must not have found x in any of the adjacency lists.
                return Ok(());
            }
            return Err(Error::EdgeExistsError);
        }
        // If y has no adjacent vertices, then we can be sure there
        // no circular relationship.
        Ok(())
    }
}

impl<T> Default for BTreeDag<T>
where
    T: Ord,
{
    fn default() -> Self {
        Self::new()
    }
}

impl<T> Vertices<T> for BTreeDag<T>
where
    T: Ord,
{
    fn vertices(&self) -> BTreeSet<&T> {
        self.vertices.keys().collect()
    }
}

impl<T> AddVertex<T> for BTreeDag<T>
where
    T: Ord,
{
    fn add_vertex(&mut self, x: T) -> Option<BTreeSet<T>> {
        self.vertices.insert(x, BTreeSet::new())
    }
}

/// When you add an edge, you should make sure that the x, and y vertices exist.
impl<T> AddEdge<T> for BTreeDag<T>
where
    T: Ord + Clone,
{
    type Error = Error;
    fn add_edge(&mut self, x: T, y: T) -> Result<(), Self::Error> {
        if self.vertices.get(&y).is_some() {
            if let Some(adj_x) = self.vertices.get(&x) {
                self.cyclic_relationship_exists(&x, &y)?;
                // Add y to x's adjacency list.
                let mut adj_x: BTreeSet<T> = adj_x.clone();
                adj_x.insert(y.clone());

                self.vertices.insert(x, adj_x);
                return Ok(());
            }
        }
        Err(Error::VertexDoesNotExist)
    }
}

impl<T> GetVertexValue<T> for BTreeDag<T>
where
    T: Ord,
{
    fn get_vertex_value(&self, v: T) -> Option<&BTreeSet<T>> {
        self.vertices.get(&v)
    }
}

/// When an edge is removed, you should find the incident vertex and ensure the edge
/// is removed from the vertex's adjacency list.
impl<T> RemoveEdge<T> for BTreeDag<T>
where
    T: Ord + Clone,
{
    type Error = Error;
    fn remove_edge(&mut self, x: T, y: T) -> Result<(), Self::Error> {
        if self.vertices.get(&y).is_some() {
            if let Some(adj_x) = self.vertices.get(&x) {
                // Remove y from x's adjacency list.
                let mut adj_x = adj_x.clone();
                adj_x.remove(&y);

                // Update vertices.
                self.vertices.insert(x, adj_x);
                return Ok(());
            }
        }
        Err(Error::VertexDoesNotExist)
    }
}

/// When you remove a vertex, you should ensure there are no dangling edges.
impl<T> RemoveVertex<T> for BTreeDag<T>
where
    T: Ord + Clone,
{
    type Error = Error;
    fn remove_vertex(&mut self, x: T) -> Result<(), Self::Error> {
        self.vertices
            .clone()
            .into_iter()
            .filter(|v| -> bool { v.1.contains(&x) })
            .try_for_each(|v| { self.remove_edge(v.0.clone(), x.clone()) })?;
        // At this point, no other vertices should point to x,
        // and so x can be removed.
        self.vertices.remove(&x);
        Ok(())
    }
}

impl<T> Adjacent<T> for BTreeDag<T>
where
    T: Ord,
{
    type Error = Error;
    fn adjacent(&self, x: T, y: T) -> Result<bool, Self::Error> {
        if self.vertices.get(&y).is_some() {
            if let Some(adj_x) = self.vertices.get(&x) {
                if adj_x.contains(&y) {
                    return Ok(true);
                }
                return Ok(false);
            }
        }
        Err(Error::VertexDoesNotExist)
    }
}

impl<T> Connections<T> for BTreeDag<T>
where
    T: Ord,
{
    fn connections(&self, x: T) -> Option<&BTreeSet<T>> {
        self.vertices.get(&x)
    }
}