fera_graph/graphs/
ref_.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5use choose::Choose;
6use prelude::*;
7
8use rand::Rng;
9use std::ops::{Index, IndexMut};
10
11impl<'a, 'b, G: WithVertex> VertexTypes<'a, &'b G> for &'b G {
12    type VertexIter = VertexIter<'a, G>;
13    type OutNeighborIter = OutNeighborIter<'a, G>;
14}
15
16impl<'a, G: WithVertex> WithVertex for &'a G {
17    type Vertex = Vertex<G>;
18    type OptionVertex = OptionVertex<G>;
19
20    fn vertex_none() -> OptionVertex<Self> {
21        G::vertex_none()
22    }
23
24    fn vertex_some(v: Vertex<Self>) -> OptionVertex<Self> {
25        G::vertex_some(v)
26    }
27}
28
29impl<'a, 'b, G: WithEdge> EdgeTypes<'a, &'b G> for &'b G {
30    type EdgeIter = EdgeIter<'a, G>;
31    type OutEdgeIter = OutEdgeIter<'a, G>;
32}
33
34impl<'a, G: WithEdge> WithEdge for &'a G {
35    type Kind = G::Kind;
36    type Edge = Edge<G>;
37    type OptionEdge = OptionEdge<G>;
38
39    fn source(&self, e: Edge<Self>) -> Vertex<Self> {
40        G::source(self, e)
41    }
42
43    fn target(&self, e: Edge<Self>) -> Vertex<Self> {
44        G::target(self, e)
45    }
46
47    fn orientation(&self, e: Edge<Self>) -> Orientation {
48        G::orientation(self, e)
49    }
50
51    fn end_vertices(&self, e: Edge<Self>) -> (Vertex<Self>, Vertex<Self>) {
52        G::end_vertices(self, e)
53    }
54
55    fn opposite(&self, u: Vertex<Self>, e: Edge<Self>) -> Vertex<Self> {
56        G::opposite(self, u, e)
57    }
58
59    // The compiler is not smart enough to allow this, so we use the default reverse
60    // implemenetation
61    //
62    // fn reverse(&self, e: Edge<Self>) -> Edge<Self> where Self: WithEdge<Kind = Undirected> {
63    //     G::reverse(self, e)
64    // }
65
66    fn get_reverse(&self, e: Edge<Self>) -> Option<Edge<Self>> {
67        G::get_reverse(self, e)
68    }
69
70    fn edge_none() -> OptionEdge<Self> {
71        G::edge_none()
72    }
73
74    fn edge_some(e: Edge<Self>) -> OptionEdge<Self> {
75        G::edge_some(e)
76    }
77}
78
79impl<'a, G: VertexList> VertexList for &'a G {
80    fn vertices(&self) -> VertexIter<Self> {
81        G::vertices(self)
82    }
83
84    fn num_vertices(&self) -> usize {
85        G::num_vertices(self)
86    }
87}
88
89impl<'a, G: EdgeList> EdgeList for &'a G {
90    fn edges(&self) -> EdgeIter<Self> {
91        G::edges(self)
92    }
93
94    fn num_edges(&self) -> usize {
95        G::num_edges(self)
96    }
97
98    fn get_edge_by_ends(&self, u: Vertex<Self>, v: Vertex<Self>) -> Option<Edge<Self>> {
99        G::get_edge_by_ends(self, u, v)
100    }
101}
102
103impl<'a, G: Adjacency> Adjacency for &'a G {
104    fn out_neighbors(&self, v: Vertex<Self>) -> OutNeighborIter<Self> {
105        G::out_neighbors(self, v)
106    }
107
108    fn out_degree(&self, v: Vertex<Self>) -> usize {
109        G::out_degree(self, v)
110    }
111}
112
113impl<'a, G: Incidence> Incidence for &'a G {
114    fn out_edges(&self, v: Vertex<Self>) -> OutEdgeIter<Self> {
115        G::out_edges(self, v)
116    }
117}
118
119impl<'a, G: WithVertexIndexProp> WithVertexIndexProp for &'a G {
120    type VertexIndexProp = VertexIndexProp<G>;
121
122    fn vertex_index(&self) -> VertexIndexProp<Self> {
123        G::vertex_index(self)
124    }
125}
126
127impl<'a, G: WithEdgeIndexProp> WithEdgeIndexProp for &'a G {
128    type EdgeIndexProp = EdgeIndexProp<G>;
129
130    fn edge_index(&self) -> EdgeIndexProp<Self> {
131        G::edge_index(self)
132    }
133}
134
135// Properties
136
137pub struct RefVertexProp<G: WithVertexProp<T>, T>(DefaultVertexPropMut<G, T>);
138
139impl<G: WithVertexProp<T>, T> Index<Vertex<G>> for RefVertexProp<G, T> {
140    type Output = T;
141
142    fn index(&self, v: Vertex<G>) -> &Self::Output {
143        self.0.index(v)
144    }
145}
146
147impl<G: WithVertexProp<T>, T> IndexMut<Vertex<G>> for RefVertexProp<G, T> {
148    fn index_mut(&mut self, v: Vertex<G>) -> &mut Self::Output {
149        self.0.index_mut(v)
150    }
151}
152
153impl<'a, G: WithVertexProp<T>, T> VertexPropMutNew<&'a G, T> for RefVertexProp<G, T> {
154    fn new_vertex_prop(g: &&'a G, value: T) -> Self
155    where
156        T: Clone,
157    {
158        RefVertexProp(G::default_vertex_prop(*g, value))
159    }
160}
161
162impl<'a, G: WithVertexProp<T>, T> WithVertexProp<T> for &'a G {
163    type VertexProp = RefVertexProp<G, T>;
164
165    fn default_vertex_prop(&self, value: T) -> DefaultVertexPropMut<Self, T>
166    where
167        T: Clone,
168    {
169        RefVertexProp(G::default_vertex_prop(self, value))
170    }
171}
172
173pub struct RefEdgeProp<G: WithEdgeProp<T>, T>(DefaultEdgePropMut<G, T>);
174
175impl<G: WithEdgeProp<T>, T> Index<Edge<G>> for RefEdgeProp<G, T> {
176    type Output = T;
177
178    fn index(&self, v: Edge<G>) -> &Self::Output {
179        self.0.index(v)
180    }
181}
182
183impl<G: WithEdgeProp<T>, T> IndexMut<Edge<G>> for RefEdgeProp<G, T> {
184    fn index_mut(&mut self, v: Edge<G>) -> &mut Self::Output {
185        self.0.index_mut(v)
186    }
187}
188
189impl<'a, G: WithEdgeProp<T>, T> EdgePropMutNew<&'a G, T> for RefEdgeProp<G, T> {
190    fn new_edge_prop(g: &&'a G, value: T) -> Self
191    where
192        T: Clone,
193    {
194        RefEdgeProp(G::default_edge_prop(*g, value))
195    }
196}
197
198impl<'a, G: WithEdgeProp<T>, T> WithEdgeProp<T> for &'a G {
199    type EdgeProp = RefEdgeProp<G, T>;
200
201    fn default_edge_prop(&self, value: T) -> DefaultEdgePropMut<Self, T>
202    where
203        T: Clone,
204    {
205        RefEdgeProp(G::default_edge_prop(self, value))
206    }
207}
208
209impl<'a, G: BasicVertexProps> BasicVertexProps for &'a G {}
210
211impl<'a, G: BasicEdgeProps> BasicEdgeProps for &'a G {}
212
213impl<'a, G: BasicProps> BasicProps for &'a G {}
214
215// Choose
216
217impl<'a, G> Choose for &'a G
218where
219    G: 'a + Choose,
220{
221    fn choose_vertex<R: Rng>(&self, rng: R) -> Option<Vertex<Self>> {
222        G::choose_vertex(self, rng)
223    }
224
225    fn choose_out_neighbor<R: Rng>(&self, v: Vertex<Self>, rng: R) -> Option<Vertex<Self>> {
226        G::choose_out_neighbor(self, v, rng)
227    }
228
229    fn choose_edge<R: Rng>(&self, rng: R) -> Option<Edge<Self>> {
230        G::choose_edge(self, rng)
231    }
232
233    fn choose_out_edge<R: Rng>(&self, v: Vertex<Self>, rng: R) -> Option<Edge<Self>> {
234        G::choose_out_edge(self, v, rng)
235    }
236}