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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
 * Copyright (c) 2017 Frank Fischer <frank-fischer@shadow-soft.de>
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see  <http://www.gnu.org/licenses/>
 */

//! Graphs embedded in another struct.
//!
//! Sometimes one needs additional attributes associated with the nodes or edges of a graph.
//! The simplest way is to store them in a `NodeVec` or `EdgeVec`. If
//! these attributes are tightly related to the graph, it is convenient to store the graph
//! together with the attributes in a single struct, e.g.
//!
//! ```
//! use graph::LinkedListGraph;
//!
//! struct MyGraph {
//!     graph: LinkedListGraph,
//!     balances: Vec<f64>,
//!     bounds: Vec<f64>,
//! }
//! ```
//!
//! The problem is that `MyGraph` itself does not implement the graph
//! traits `Graph`, `Digraph` and so on, and can therefore not used
//! directly for methods that need values of this type.
//!
//! The trait `WrappedGraph` in this module can be used to automate the
//! tedious task of implementing the traits. If `MyGraph` implements
//! `WrappedGraph`, the graph traits are automatically implemented for
//! `MyGraph` by forwarding all methods to the embedded graph.
//!
//! For a different approach, see the `attributed` module.

use graph::{Graph, Digraph, Network, IndexGraph, IndexNetwork};
use builder::Builder;

use std::marker::PhantomData;

/// An embedded graph.
///
/// This trait is similar to the combination `From<G> +
/// Deref<Target=G>` but specialized for graphs.
pub trait WrappedGraph {
    /// The embedded graph type.
    type Graph;

    /// Create the embedding structure from a graph.
    fn from_graph(g: Self::Graph) -> Self;

    /// Return a reference to the embedded graph.
    fn as_graph(&self) -> &Self::Graph;
}

/// An embedded, mutable graph.
pub trait WrappedGraphMut: WrappedGraph {
    fn as_mut_graph(&mut self) -> &mut Self::Graph;
}

/// A builder for a wrapped graph.
pub struct WrappedBuilder<G,B>(B, PhantomData<G>) where G: WrappedGraph;

impl<G,B> Builder for WrappedBuilder<G,B>
    where G: WrappedGraph,
          B: Builder<Graph=G::Graph>,
{
    type Graph = G;
    type Node = B::Node;
    type Edge = B::Edge;

    fn new() -> Self {
        WrappedBuilder(B::new(), PhantomData)
    }

    fn with_capacities(nnodes: usize, nedges: usize) -> Self {
        WrappedBuilder(B::with_capacities(nnodes, nedges), PhantomData)
    }

    fn reserve(&mut self, nnodes: usize, nedges: usize) {
        self.0.reserve(nnodes, nedges)
    }

    fn add_node(&mut self) -> Self::Node {
        self.0.add_node()
    }

    fn add_nodes(&mut self, n: usize) -> Vec<Self::Node> {
        self.0.add_nodes(n)
    }

    fn add_edge(&mut self, u: Self::Node, v: Self::Node) -> Self::Edge {
        self.0.add_edge(u, v)
    }

    fn node_id(&self, u: Self::Node) -> usize {
        self.0.node_id(u)
    }

    fn edge_id(&self, e: Self::Edge) -> usize {
        self.0.edge_id(e)
    }

    fn to_graph(self) -> Self::Graph {
        WrappedGraph::from_graph(self.0.to_graph())
    }
}

impl<'a,T> Graph<'a> for T
    where T: WrappedGraph,
          T::Graph: Graph<'a>
{
    type Node = <T::Graph as Graph<'a>>::Node;
    type Edge = <T::Graph as Graph<'a>>::Edge;
    type NodeIter = <T::Graph as Graph<'a>>::NodeIter;
    type EdgeIter = <T::Graph as Graph<'a>>::EdgeIter;
    type NeighIter = <T::Graph as Graph<'a>>::NeighIter;
    type Builder = WrappedBuilder<T, <T::Graph as Graph<'a>>::Builder>;

    fn num_nodes(&self) -> usize {
        self.as_graph().num_nodes()
    }

    fn num_edges(&self) -> usize {
        self.as_graph().num_edges()
    }

    fn enodes(&'a self, e: Self::Edge) -> (Self::Node, Self::Node) {
        self.as_graph().enodes(e)
    }

    fn nodes(&'a self) -> Self::NodeIter {
        self.as_graph().nodes()
    }

    fn edges(&'a self) -> Self::EdgeIter {
        self.as_graph().edges()
    }

    fn neighs(&'a self, u: Self::Node) -> Self::NeighIter {
        self.as_graph().neighs(u)
    }
}

impl<'a,T> Digraph<'a> for T
    where T: WrappedGraph,
          T::Graph: Digraph<'a>
{
    type OutEdgeIter = <T::Graph as Digraph<'a>>::OutEdgeIter;
    type InEdgeIter = <T::Graph as Digraph<'a>>::InEdgeIter;

    fn src(&'a self, e: Self::Edge) -> Self::Node {
        self.as_graph().src(e)
    }

    fn snk(&'a self, e: Self::Edge) -> Self::Node {
        self.as_graph().snk(e)
    }

    fn outedges(&'a self, u: Self::Node) -> Self::OutEdgeIter {
        self.as_graph().outedges(u)
    }

    fn inedges(&'a self, u: Self::Node) -> Self::InEdgeIter {
        self.as_graph().inedges(u)
    }
}

impl<'a,T> Network<'a> for T
    where T: WrappedGraph,
          T::Graph: Network<'a>
{
    fn is_reverse(&self, e: Self::Edge, f: Self::Edge) -> bool {
        self.as_graph().is_reverse(e, f)
    }

    fn reverse(&'a self, e: Self::Edge) -> Self::Edge {
        self.as_graph().reverse(e)
    }

    fn is_forward(&self, e: Self::Edge) -> bool {
        self.as_graph().is_forward(e)
    }

    fn forward(&'a self, e: Self::Edge) -> Self::Edge {
        self.as_graph().forward(e)
    }

    fn is_backward(&self, e: Self::Edge) -> bool {
        self.as_graph().is_backward(e)
    }

    fn backward(&'a self, e: Self::Edge) -> Self::Edge {
        self.as_graph().backward(e)
    }

    fn bisrc(&'a self, e: Self::Edge) -> Self::Node {
        self.as_graph().bisrc(e)
    }

    fn bisnk(&'a self, e: Self::Edge) -> Self::Node {
        self.as_graph().bisnk(e)
    }
}

impl <'a,T> IndexGraph<'a> for T
    where T: WrappedGraph,
          T::Graph: IndexGraph<'a>
{
    fn node_id(&self, u: Self::Node) -> usize {
        self.as_graph().node_id(u)
    }

    fn id2node(&'a self, id: usize) -> Self::Node {
        self.as_graph().id2node(id)
    }

    fn edge_id(&self, e: Self::Edge) -> usize {
        self.as_graph().edge_id(e)
    }

    fn id2edge(&'a self, id: usize) -> Self::Edge {
        self.as_graph().id2edge(id)
    }
}

impl <'a,T> IndexNetwork<'a> for T
    where T: WrappedGraph,
          T::Graph: IndexNetwork<'a>
{
    fn biedge_id(&self, e: Self::Edge) -> usize {
        self.as_graph().biedge_id(e)
    }

    fn id2biedge(&'a self, id: usize) -> Self::Edge {
        self.as_graph().id2biedge(id)
    }
}

impl<T> Builder for T
    where T: WrappedGraphMut,
          T::Graph: Builder,
{
    type Graph = T;
    type Node = <T::Graph as Builder>::Node;
    type Edge = <T::Graph as Builder>::Edge;

    fn new() -> Self {
        Self::from_graph(<T::Graph as Builder>::new())
    }

    fn with_capacities(nnodes: usize, nedges: usize) -> Self {
        Self::from_graph(<T::Graph as Builder>::with_capacities(nnodes, nedges))
    }

    fn reserve(&mut self, nnodes: usize, nedges: usize) {
        self.as_mut_graph().reserve(nnodes, nedges)
    }

    fn add_node(&mut self) -> Self::Node {
        self.as_mut_graph().add_node()
    }

    fn add_nodes(&mut self, n: usize) -> Vec<Self::Node> {
        self.as_mut_graph().add_nodes(n)
    }

    fn add_edge(&mut self, u: Self::Node, v: Self::Node) -> Self::Edge {
        self.as_mut_graph().add_edge(u, v)
    }

    fn node_id(&self, u: Self::Node) -> usize {
        self.as_graph().node_id(u)
    }

    fn edge_id(&self, e: Self::Edge) -> usize {
        self.as_graph().edge_id(e)
    }

    fn to_graph(self) -> Self::Graph {
        self
    }
}