fera_graph/
params.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
5//! Support for generic algorithms parameters.
6
7// TODO: new names: Param, OwnedParam, IntoIteratorParam
8use prelude::*;
9
10use std::ops::{Deref, DerefMut};
11
12macro_rules! generic_struct {
13    ($(#[$attr:meta])* pub struct $S:ident($zero:ident)) => (
14        $(#[$attr])*
15        pub struct $S<A>(pub A);
16
17        impl<A> $S<A> {
18            pub fn $zero<N>(self, zero: N) -> $S<N> {
19                $S(zero)
20            }
21        }
22    );
23    ($(#[$attr:meta])* pub struct $S:ident($zero:ident, $one:ident)) => (
24        $(#[$attr])*
25        pub struct $S<A, B>(pub A, pub B);
26
27        impl<A, B> $S<A, B> {
28            pub fn $zero<N>(self, zero: N) -> $S<N, B> {
29                $S(zero, self.1)
30            }
31
32            pub fn $one<N>(self, one: N) -> $S<A, N> {
33                $S(self.0, one)
34            }
35        }
36    );
37    ($(#[$attr:meta])* pub struct $S:ident($zero:ident, $one:ident, $two:ident)) => (
38        $(#[$attr])*
39        pub struct $S<A, B, C>(pub A, pub B, pub C);
40
41        impl<A, B, C> $S<A, B, C> {
42            pub fn $zero<N>(self, zero: N) -> $S<N, B, C> {
43                $S(zero, self.1, self.2)
44            }
45
46            pub fn $one<N>(self, one: N) -> $S<A, N, C> {
47                $S(self.0, one, self.2)
48            }
49
50            pub fn $two<N>(self, two: N) -> $S<A, B, N> {
51                $S(self.0, self.1, two)
52            }
53        }
54    );
55    ($(#[$attr:meta])* pub struct $S:ident($zero:ident, $one:ident, $two:ident,
56                                           $three:ident)) => (
57        $(#[$attr])*
58        pub struct $S<A, B, C, D>(pub A, pub B, pub C, pub D);
59
60        impl<A, B, C, D> $S<A, B, C, D> {
61            pub fn $zero<N>(self, zero: N) -> $S<N, B, C, D> {
62                $S(zero, self.1, self.2, self.3)
63            }
64
65            pub fn $one<N>(self, one: N) -> $S<A, N, C, D> {
66                $S(self.0, one, self.2, self.3)
67            }
68
69            pub fn $two<N>(self, two: N) -> $S<A, B, N, D> {
70                $S(self.0, self.1, two, self.3)
71            }
72
73            pub fn $three<N>(self, three: N) -> $S<A, B, C, N> {
74                $S(self.0, self.1, self.2, three)
75            }
76        }
77    );
78    ($(#[$attr:meta])* pub struct $S:ident($zero:ident, $one:ident, $two:ident,
79                                           $three:ident, $four:ident)) => (
80        $(#[$attr])*
81        pub struct $S<A, B, C, D, E>(pub A, pub B, pub C, pub D, pub E);
82
83        impl<A, B, C, D, E> $S<A, B, C, D, E> {
84            pub fn $zero<N>(self, zero: N) -> $S<N, B, C, D, E> {
85                $S(zero, self.1, self.2, self.3, self.4)
86            }
87
88            pub fn $one<N>(self, one: N) -> $S<A, N, C, D, E> {
89                $S(self.0, one, self.2, self.3, self.4)
90            }
91
92            pub fn $two<N>(self, two: N) -> $S<A, B, N, D, E> {
93                $S(self.0, self.1, two, self.3, self.4)
94            }
95
96            pub fn $three<N>(self, three: N) -> $S<A, B, C, N, E> {
97                $S(self.0, self.1, self.2, three, self.4)
98            }
99
100            pub fn $four<N>(self, four: N) -> $S<A, B, C, D, N> {
101                $S(self.0, self.1, self.2, self.3, four)
102            }
103        }
104    );
105    ($(#[$attr:meta])* pub struct $S:ident($zero:ident, $one:ident, $two:ident,
106                                           $three:ident, $four:ident, $five:ident)) => (
107        $(#[$attr])*
108        pub struct $S<A, B, C, D, E, F>(pub A, pub B, pub C, pub D, pub E, pub F);
109
110        impl<A, B, C, D, E, F> $S<A, B, C, D, E, F> {
111            pub fn $zero<N>(self, zero: N) -> $S<N, B, C, D, E, F> {
112                $S(zero, self.1, self.2, self.3, self.4, self.5)
113            }
114
115            pub fn $one<N>(self, one: N) -> $S<A, N, C, D, E, F> {
116                $S(self.0, one, self.2, self.3, self.4, self.5)
117            }
118
119            pub fn $two<N>(self, two: N) -> $S<A, B, N, D, E, F> {
120                $S(self.0, self.1, two, self.3, self.4, self.5)
121            }
122
123            pub fn $three<N>(self, three: N) -> $S<A, B, C, N, E, F> {
124                $S(self.0, self.1, self.2, three, self.4, self.5)
125            }
126
127            pub fn $four<N>(self, four: N) -> $S<A, B, C, D, N, F> {
128                $S(self.0, self.1, self.2, self.3, four, self.5)
129            }
130
131            pub fn $five<N>(self, five: N) -> $S<A, B, C, D, E, N> {
132                $S(self.0, self.1, self.2, self.3, self.4, five)
133            }
134        }
135    );
136}
137
138pub trait ParamDerefMut {
139    type Target;
140    type Output: DerefMut<Target = Self::Target>;
141
142    fn build(self) -> Self::Output;
143}
144
145impl<'a, T> ParamDerefMut for &'a mut T {
146    type Target = T;
147    type Output = &'a mut T;
148
149    fn build(self) -> Self::Output {
150        self
151    }
152}
153
154// TODO: Create an IntoIteratorOwned
155pub trait IntoOwned<Owned> {
156    fn into_owned(self) -> Owned;
157}
158
159impl<T> IntoOwned<T> for T {
160    #[inline]
161    fn into_owned(self) -> T {
162        self
163    }
164}
165
166impl<'a, T: Clone> IntoOwned<T> for &'a T {
167    #[inline]
168    fn into_owned(self) -> T {
169        T::clone(self)
170    }
171}
172
173impl<'a, T: Clone> IntoOwned<T> for &'a mut T {
174    #[inline]
175    fn into_owned(self) -> T {
176        T::clone(self)
177    }
178}
179
180pub struct Owned<T>(pub T);
181
182impl<T> Deref for Owned<T> {
183    type Target = T;
184
185    #[inline]
186    fn deref(&self) -> &Self::Target {
187        &self.0
188    }
189}
190
191impl<T> DerefMut for Owned<T> {
192    #[inline]
193    fn deref_mut(&mut self) -> &mut Self::Target {
194        &mut self.0
195    }
196}
197
198impl<T> ParamDerefMut for Owned<T> {
199    type Target = T;
200    type Output = Self;
201
202    fn build(self) -> Self::Output {
203        self
204    }
205}
206
207pub struct NewVertexProp<'a, G: 'a, T>(pub &'a G, pub T);
208
209impl<'a, G, T> ParamDerefMut for NewVertexProp<'a, G, T>
210where
211    G: 'a + WithVertexProp<T>,
212    T: Clone,
213{
214    type Target = DefaultVertexPropMut<G, T>;
215    type Output = Owned<DefaultVertexPropMut<G, T>>;
216
217    fn build(self) -> Self::Output {
218        Owned(self.0.vertex_prop(self.1))
219    }
220}
221
222// TODO: create NewEdgeProp
223
224// Iterator
225
226// TODO: find a better prefix than All
227
228pub struct AllVertices<'a, G: 'a>(pub &'a G);
229
230impl<'a, G> IntoIterator for AllVertices<'a, G>
231where
232    G: 'a + VertexList,
233{
234    type Item = Vertex<G>;
235    type IntoIter = VertexIter<'a, G>;
236
237    fn into_iter(self) -> Self::IntoIter {
238        self.0.vertices()
239    }
240}
241
242pub struct AllEdges<'a, G: 'a>(pub &'a G);
243
244impl<'a, G> IntoIterator for AllEdges<'a, G>
245where
246    G: 'a + EdgeList,
247{
248    type Item = Edge<G>;
249    type IntoIter = EdgeIter<'a, G>;
250
251    fn into_iter(self) -> Self::IntoIter {
252        self.0.edges()
253    }
254}
255
256// TODO: create AllOutEdges