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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use prelude::*;
use choose::Choose;

use rand::Rng;
use std::ops::{Index, IndexMut};

impl<'a, 'b, G: WithVertex> VertexTypes<'a, &'b G> for &'b G {
    type VertexIter = VertexIter<'a, G>;
    type OutNeighborIter = OutNeighborIter<'a, G>;
}

impl<'a, G: WithVertex> WithVertex for &'a G {
    type Vertex = Vertex<G>;
    type OptionVertex = OptionVertex<G>;

    fn vertex_none() -> OptionVertex<Self> {
        G::vertex_none()
    }

    fn vertex_some(v: Vertex<Self>) -> OptionVertex<Self> {
        G::vertex_some(v)
    }
}

impl<'a, 'b, G: WithEdge> EdgeTypes<'a, &'b G> for &'b G {
    type EdgeIter = EdgeIter<'a, G>;
    type OutEdgeIter = OutEdgeIter<'a, G>;
}

impl<'a, G: WithEdge> WithEdge for &'a G {
    type Kind = G::Kind;
    type Edge = Edge<G>;
    type OptionEdge = OptionEdge<G>;

    fn source(&self, e: Edge<Self>) -> Vertex<Self> {
        G::source(self, e)
    }

    fn target(&self, e: Edge<Self>) -> Vertex<Self> {
        G::target(self, e)
    }

    fn orientation(&self, e: Edge<Self>) -> Orientation {
        G::orientation(self, e)
    }

    fn end_vertices(&self, e: Edge<Self>) -> (Vertex<Self>, Vertex<Self>) {
        G::end_vertices(self, e)
    }

    fn opposite(&self, u: Vertex<Self>, e: Edge<Self>) -> Vertex<Self> {
        G::opposite(self, u, e)
    }

    // The compiler is not smart enough to allow this, so we use the default reverse
    // implemenetation
    //
    // fn reverse(&self, e: Edge<Self>) -> Edge<Self> where Self: WithEdge<Kind = Undirected> {
    //     G::reverse(self, e)
    // }

    fn get_reverse(&self, e: Edge<Self>) -> Option<Edge<Self>> {
        G::get_reverse(self, e)
    }

    fn edge_none() -> OptionEdge<Self> {
        G::edge_none()
    }

    fn edge_some(e: Edge<Self>) -> OptionEdge<Self> {
        G::edge_some(e)
    }
}

impl<'a, G: VertexList> VertexList for &'a G {
    fn vertices(&self) -> VertexIter<Self> {
        G::vertices(self)
    }

    fn num_vertices(&self) -> usize {
        G::num_vertices(self)
    }
}

impl<'a, G: EdgeList> EdgeList for &'a G {
    fn edges(&self) -> EdgeIter<Self> {
        G::edges(self)
    }

    fn num_edges(&self) -> usize {
        G::num_edges(self)
    }

    fn get_edge_by_ends(&self, u: Vertex<Self>, v: Vertex<Self>) -> Option<Edge<Self>> {
        G::get_edge_by_ends(self, u, v)
    }
}

impl<'a, G: Adjacency> Adjacency for &'a G {
    fn out_neighbors(&self, v: Vertex<Self>) -> OutNeighborIter<Self> {
        G::out_neighbors(self, v)
    }

    fn out_degree(&self, v: Vertex<Self>) -> usize {
        G::out_degree(self, v)
    }
}

impl<'a, G: Incidence> Incidence for &'a G {
    fn out_edges(&self, v: Vertex<Self>) -> OutEdgeIter<Self> {
        G::out_edges(self, v)
    }
}

impl<'a, G: WithVertexIndexProp> WithVertexIndexProp for &'a G {
    type VertexIndexProp = VertexIndexProp<G>;

    fn vertex_index(&self) -> VertexIndexProp<Self> {
        G::vertex_index(self)
    }
}

impl<'a, G: WithEdgeIndexProp> WithEdgeIndexProp for &'a G {
    type EdgeIndexProp = EdgeIndexProp<G>;

    fn edge_index(&self) -> EdgeIndexProp<Self> {
        G::edge_index(self)
    }
}

// Properties

pub struct RefVertexProp<G: WithVertexProp<T>, T>(DefaultVertexPropMut<G, T>);

impl<G: WithVertexProp<T>, T> Index<Vertex<G>> for RefVertexProp<G, T> {
    type Output = T;

    fn index(&self, v: Vertex<G>) -> &Self::Output {
        self.0.index(v)
    }
}

impl<G: WithVertexProp<T>, T> IndexMut<Vertex<G>> for RefVertexProp<G, T> {
    fn index_mut(&mut self, v: Vertex<G>) -> &mut Self::Output {
        self.0.index_mut(v)
    }
}

impl<'a, G: WithVertexProp<T>, T> VertexPropMutNew<&'a G, T> for RefVertexProp<G, T> {
    fn new_vertex_prop(g: &&'a G, value: T) -> Self
    where
        T: Clone,
    {
        RefVertexProp(G::default_vertex_prop(*g, value))
    }
}

impl<'a, G: WithVertexProp<T>, T> WithVertexProp<T> for &'a G {
    type VertexProp = RefVertexProp<G, T>;

    fn default_vertex_prop(&self, value: T) -> DefaultVertexPropMut<Self, T>
    where
        T: Clone,
    {
        RefVertexProp(G::default_vertex_prop(self, value))
    }
}

pub struct RefEdgeProp<G: WithEdgeProp<T>, T>(DefaultEdgePropMut<G, T>);

impl<G: WithEdgeProp<T>, T> Index<Edge<G>> for RefEdgeProp<G, T> {
    type Output = T;

    fn index(&self, v: Edge<G>) -> &Self::Output {
        self.0.index(v)
    }
}

impl<G: WithEdgeProp<T>, T> IndexMut<Edge<G>> for RefEdgeProp<G, T> {
    fn index_mut(&mut self, v: Edge<G>) -> &mut Self::Output {
        self.0.index_mut(v)
    }
}

impl<'a, G: WithEdgeProp<T>, T> EdgePropMutNew<&'a G, T> for RefEdgeProp<G, T> {
    fn new_edge_prop(g: &&'a G, value: T) -> Self
    where
        T: Clone,
    {
        RefEdgeProp(G::default_edge_prop(*g, value))
    }
}

impl<'a, G: WithEdgeProp<T>, T> WithEdgeProp<T> for &'a G {
    type EdgeProp = RefEdgeProp<G, T>;

    fn default_edge_prop(&self, value: T) -> DefaultEdgePropMut<Self, T>
    where
        T: Clone,
    {
        RefEdgeProp(G::default_edge_prop(self, value))
    }
}

impl<'a, G: BasicVertexProps> BasicVertexProps for &'a G {}

impl<'a, G: BasicEdgeProps> BasicEdgeProps for &'a G {}

impl<'a, G: BasicProps> BasicProps for &'a G {}

// Choose

impl<'a, G> Choose for &'a G
where
    G: 'a + Choose,
{
    fn choose_vertex<R: Rng>(&self, rng: R) -> Option<Vertex<Self>> {
        G::choose_vertex(self, rng)
    }

    fn choose_out_neighbor<R: Rng>(&self, v: Vertex<Self>, rng: R) -> Option<Vertex<Self>> {
        G::choose_out_neighbor(self, v, rng)
    }

    fn choose_edge<R: Rng>(&self, rng: R) -> Option<Edge<Self>> {
        G::choose_edge(self, rng)
    }

    fn choose_out_edge<R: Rng>(&self, v: Vertex<Self>, rng: R) -> Option<Edge<Self>> {
        G::choose_out_edge(self, v, rng)
    }
}