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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use alloc::collections::BTreeSet;

/// `Vertices` returns the set of the vertices which comprise the graph.
///
/// # Example
///
/// ```
/// use btree_graph::{BTreeGraph, AddVertex, Vertices};
/// let mut graph: BTreeGraph<String, usize> = BTreeGraph::new();
///
/// assert_eq!(graph.vertices().len(), 0);
/// ```
pub trait Vertices<T>
where
    T: Ord,
{
    fn vertices(&self) -> BTreeSet<&T>;
}

/// `Edges` returns the set edges which comprise the graph.
///
/// # Example
///
/// ```
/// use btree_graph::{BTreeGraph, AddVertex, AddEdge, Edges};
/// let mut graph: BTreeGraph<String, usize> = BTreeGraph::new();
///
/// assert_eq!(graph.edges().len(), 0);
/// ```
pub trait Edges<T>
where
    T: Ord,
{
    fn edges(&self) -> BTreeSet<&T>;
}

/// `AddVertex` adds the vertex x, if it is not there.
///
/// # Example
///
/// ```
/// use btree_graph::{BTreeGraph, AddVertex, Vertices};
/// use std::collections::BTreeSet;
/// let mut graph: BTreeGraph<String, usize> = BTreeGraph::new();
/// let old_vertex_value: Option<BTreeSet<usize>> = graph.add_vertex(String::from("origin"));
///
/// assert!(old_vertex_value.is_none());
/// assert_eq!(graph.vertices().len(), 1)
/// ```
pub trait AddVertex<V, E>
where
    E: Ord,
{
    fn add_vertex(&mut self, x: V) -> Option<BTreeSet<E>>;
}

/// `AddEdge` add an edge from the vertex x to the vertex y, if it is not there.
///
/// # Example
///
/// ```
/// use btree_graph::{BTreeGraph, AddVertex, AddEdge, Edges};
/// let mut graph: BTreeGraph<String, usize> = BTreeGraph::new();
/// graph.add_vertex(String::from("origin"));
/// graph.add_vertex(String::from("destination"));
/// let old_edge_value: Option<(String, String)> = graph.add_edge(String::from("origin"), String::from("destination"), 10).unwrap();
///
/// assert!(old_edge_value.is_none());
/// assert_eq!(graph.edges().len(), 1)
/// ```
pub trait AddEdge<V, E> {
    type Error;
    fn add_edge(&mut self, x: V, y: V, e: E) -> Result<Option<(V, V)>, Self::Error>;
}

/// `GetEdgeValue` returns the value associated with the edge (x, y).
///
/// # Example
///
/// ```
/// use btree_graph::{BTreeGraph, AddVertex, AddEdge, GetEdgeValue};
/// let mut graph: BTreeGraph<String, usize> = BTreeGraph::new();
/// graph.add_vertex(String::from("origin"));
/// graph.add_vertex(String::from("destination"));
/// graph.add_edge(String::from("origin"), String::from("destination"), 10);
///
/// let edge_value: &(String, String) = graph.get_edge_value(10).unwrap();
/// assert_eq!(edge_value.0, String::from("origin"));
/// assert_eq!(edge_value.1, String::from("destination"));
/// ```
pub trait GetEdgeValue<V, E> {
    fn get_edge_value(&self, x: E) -> Option<&(V, V)>;
}

/// `GetVertexValue` returns the value associated with the vertex x.
///
/// # Example
///
/// ```
/// extern crate alloc;
/// use alloc::collections::btree_set::BTreeSet;
/// use btree_graph::{BTreeGraph, AddVertex, AddEdge, GetEdgeValue, GetVertexValue};
/// let mut graph: BTreeGraph<String, usize> = BTreeGraph::new();
/// graph.add_vertex(String::from("origin"));
/// graph.add_vertex(String::from("destination"));
/// graph.add_edge(String::from("origin"), String::from("destination"), 10);
///
/// let vertex_value: &BTreeSet<usize> = graph.get_vertex_value(String::from("origin")).unwrap();
/// assert!(vertex_value.contains(&10));
/// ```
pub trait GetVertexValue<V, E>
where
    E: Ord,
{
    fn get_vertex_value(&self, x: V) -> Option<&BTreeSet<E>>;
}

/// `RemoveEdge` removes the edge from the vertex x to the vertex y, if it is there. If the
/// edge does not exist, an error will be raised.
///
/// # Example
///
/// ```
/// use btree_graph::{BTreeGraph, AddVertex, AddEdge, Edges, RemoveEdge, GetVertexValue};
/// let mut graph: BTreeGraph<String, usize> = BTreeGraph::new();
/// graph.add_vertex(String::from("origin"));
/// graph.add_vertex(String::from("destination"));
/// graph.add_edge(String::from("origin"), String::from("destination"), 10);
///
/// assert_eq!(graph.edges().len(), 1);
///
/// graph.remove_edge(10);
///
/// assert_eq!(graph.edges().len(), 0);
///
/// // Note: deletion of edges cascade i.e. the edge is also deleted from any incident
/// // vertices' adjacency lists.
/// assert_eq!(graph.get_vertex_value(String::from("origin")).unwrap().len(), 0);
/// ```
pub trait RemoveEdge<V, E> {
    type Error;
    fn remove_edge(&mut self, x: E) -> Result<(), Self::Error>;
}

/// `RemoveVertex` removes the vertex x, if it is there. If the vertex does not exist,
/// an error is raised.
///
/// # Example
///
/// ```
/// extern crate alloc;
/// use alloc::collections::btree_set::BTreeSet;
/// use btree_graph::{BTreeGraph, AddVertex, AddEdge, Edges, RemoveVertex, GetVertexValue, Vertices};
/// let mut graph: BTreeGraph<String, usize> = BTreeGraph::new();
/// graph.add_vertex(String::from("origin"));
/// graph.add_vertex(String::from("destination"));
/// graph.add_edge(String::from("origin"), String::from("destination"), 10);
///
///
/// graph.remove_vertex(String::from("destination"));
/// assert_eq!(graph.vertices().len(), 1);
///
/// // Note: removing a vertex will also cascade delete any incident edges, which will then
/// // cascade delete any edges from the origin existing vertices' adjacency list.
/// assert_eq!(graph.edges().len(), 0);
/// assert_eq!(graph.get_vertex_value(String::from("origin")).unwrap().len(), 0);
/// ```
pub trait RemoveVertex<V, E>
where
    E: Ord,
{
    type Error;
    fn remove_vertex(&mut self, x: V) -> Result<(), Self::Error>;
}

/// `Adjacent` tests whether there is an edge from the vertex x to the vertex y.
/// An error is thrown if either x, or y do not exist. By definition of adjacent there
/// must exist an edge e, with value (x, y) in order for vertices x, and y to be
/// considered adjacent.
///
/// # Example
///
/// ```
/// use btree_graph::{BTreeGraph, AddVertex, AddEdge, Adjacent};
/// let mut graph: BTreeGraph<String, usize> = BTreeGraph::new();
/// graph.add_vertex(String::from("origin"));
/// graph.add_vertex(String::from("destination"));
/// graph.add_edge(String::from("origin"), String::from("destination"), 10);
///
/// assert!(graph.adjacent(String::from("origin"), String::from("destination")).unwrap());
/// // Note: the graph is directed, and the definition of adjacent
/// // can be phrased, if there exists a relationship from x to y. Therefore
/// // A and B adjacent does not imply B and A are adjacent.
/// assert!(!graph.adjacent(String::from("destination"), String::from("origin")).unwrap());
/// ```
pub trait Adjacent<T> {
    type Error;
    fn adjacent(&self, x: T, y: T) -> Result<bool, Self::Error>;
}

/// `Connections` lists all vertices y such that there is an edge from the vertex x to
/// the vertex y. An error is thrown if x does not exist.
///
/// # Example
///
/// ```
/// use btree_graph::{BTreeGraph, AddVertex, AddEdge, Connections};
/// let mut graph: BTreeGraph<String, usize> = BTreeGraph::new();
/// graph.add_vertex(String::from("origin"));
/// graph.add_vertex(String::from("destination"));
/// graph.add_edge(String::from("origin"), String::from("destination"), 10);
///
/// assert!(graph.connections(String::from("origin")).unwrap().contains(&String::from("destination")));
/// ```
pub trait Connections<T> {
    type Error;
    fn connections(&self, x: T) -> Result<BTreeSet<&T>, Self::Error>;
}