1use prelude::*;
22use std::ops::{Index, IndexMut};
23
24mod array;
25mod delegate;
26#[path = "fn.rs"]
27mod fn_;
28mod hashmap;
29mod ignore;
30
31pub use self::array::*;
32pub use self::delegate::*;
33pub use self::fn_::*;
34pub use self::hashmap::*;
35pub use self::ignore::*;
36
37use params::IntoOwned;
38
39pub trait PropGet<K> {
41    type Output: Sized;
42
43    fn get(&self, key: K) -> Self::Output;
45
46    #[inline]
48    fn map<F, O>(self, fun: F) -> Map<Self, F>
49    where
50        Self: Sized,
51        F: Fn(Self::Output) -> O,
52    {
53        Map(self, fun)
54    }
55
56    #[inline]
58    fn by_ref(&self) -> &Self {
59        self
60    }
61}
62
63impl<'a, K, P: PropGet<K>> PropGet<K> for &'a P {
64    type Output = P::Output;
65
66    #[inline]
67    fn get(&self, key: K) -> Self::Output {
68        P::get(self, key)
69    }
70}
71
72pub trait PropIndexMut<Idx>: IndexMut<Idx> {
77    fn set_values_from<P, I>(&mut self, iter: I, source: &P)
80    where
81        I: IntoIterator,
82        I::Item: IntoOwned<Idx>,
83        Idx: Clone,
84        P: Index<Idx, Output = Self::Output>,
85        Self::Output: Clone,
86    {
87        for v in iter {
88            let v = v.into_owned();
89            self[v.clone()].clone_from(&source[v]);
90        }
91    }
92
93    fn set_values<I>(&mut self, iter: I, value: Self::Output)
95    where
96        I: IntoIterator,
97        I::Item: IntoOwned<Idx>,
98        Self::Output: Clone,
99    {
100        for v in iter {
101            self[v.into_owned()].clone_from(&value);
102        }
103    }
104}
105
106impl<P: IndexMut<Idx>, Idx> PropIndexMut<Idx> for P {}
107
108pub trait VertexPropGet<G, T>: PropGet<Vertex<G>, Output = T>
114where
115    G: WithVertex,
116{
117}
118
119impl<P, G, T> VertexPropGet<G, T> for P
120where
121    G: WithVertex,
122    P: PropGet<Vertex<G>, Output = T>,
123{
124}
125
126pub trait VertexProp<G, T>: Index<Vertex<G>, Output = T>
128where
129    G: WithVertex,
130{
131}
132
133impl<P, G, T> VertexProp<G, T> for P
134where
135    G: WithVertex,
136    P: Index<Vertex<G>, Output = T>,
137{
138}
139
140pub trait VertexPropMut<G, T>: IndexMut<Vertex<G>, Output = T>
142where
143    G: WithVertex,
144{
145}
146
147impl<P, G, T> VertexPropMut<G, T> for P
148where
149    G: WithVertex,
150    P: IndexMut<Vertex<G>, Output = T>,
151{
152}
153
154pub trait VertexPropMutNew<G, T>: VertexPropMut<G, T>
156where
157    G: WithVertex,
158{
159    fn new_vertex_prop(g: &G, value: T) -> Self
166    where
167        T: Clone;
168}
169
170pub trait WithVertexProp<T>: WithVertex {
173    type VertexProp: VertexPropMutNew<Self, T>;
175
176    fn default_vertex_prop(&self, value: T) -> DefaultVertexPropMut<Self, T>
179    where
180        T: Clone,
181    {
182        self.vertex_prop(value)
183    }
184
185    fn default_vertex_prop_from_fn<P, F>(&self, fun: F) -> P
188    where
189        Self: VertexList,
190        P: VertexPropMutNew<Self, T>,
191        F: FnMut(Vertex<Self>) -> T,
192        T: Default + Clone,
193    {
194        self.vertex_prop_from_fn(fun)
195    }
196}
197
198pub trait WithVertexIndexProp: WithVertex {
200    type VertexIndexProp: VertexPropGet<Self, usize>;
201
202    fn vertex_index(&self) -> VertexIndexProp<Self>;
204}
205
206pub trait EdgePropGet<G, T>: PropGet<Edge<G>, Output = T>
210where
211    G: WithEdge,
212{
213}
214
215impl<P, G, T> EdgePropGet<G, T> for P
216where
217    G: WithEdge,
218    P: PropGet<Edge<G>, Output = T>,
219{
220}
221
222pub trait EdgeProp<G, T>: Index<Edge<G>, Output = T>
224where
225    G: WithEdge,
226{
227}
228
229impl<P, G, T> EdgeProp<G, T> for P
230where
231    G: WithEdge,
232    P: Index<Edge<G>, Output = T>,
233{
234}
235
236pub trait EdgePropMut<G, T>: IndexMut<Edge<G>, Output = T>
238where
239    G: WithEdge,
240{
241}
242
243impl<P, G, T> EdgePropMut<G, T> for P
244where
245    G: WithEdge,
246    P: IndexMut<Edge<G>, Output = T>,
247{
248}
249
250pub trait EdgePropMutNew<G, T>: EdgePropMut<G, T>
252where
253    G: WithEdge,
254{
255    fn new_edge_prop(g: &G, value: T) -> Self
262    where
263        T: Clone;
264}
265
266pub trait WithEdgeProp<T>: WithEdge {
269    type EdgeProp: EdgePropMutNew<Self, T>;
270
271    fn default_edge_prop(&self, value: T) -> DefaultEdgePropMut<Self, T>
274    where
275        T: Clone,
276    {
277        self.edge_prop(value)
278    }
279
280    fn default_edge_prop_from_fn<P, F>(&self, fun: F) -> P
283    where
284        Self: EdgeList,
285        P: EdgePropMutNew<Self, T>,
286        F: FnMut(Edge<Self>) -> T,
287        T: Default + Clone,
288    {
289        self.edge_prop_from_fn(fun)
290    }
291}
292
293pub trait WithEdgeIndexProp: WithEdge {
295    type EdgeIndexProp: EdgePropGet<Self, usize>;
296
297    fn edge_index(&self) -> EdgeIndexProp<Self>;
299}
300
301macro_rules! items {
304    ($($item:item)*) => ($($item)*);
305}
306
307macro_rules! basic_props1 {
308    ($($v:ty),* ; $($e:ty),* ; $($c:ty),*) => (
309        items! {
310            pub trait BasicVertexProps:
311                $(WithVertexProp<$v> +)* { }
312
313            pub trait BasicEdgeProps:
314                $(WithEdgeProp<$e> +)* { }
315
316            pub trait BasicProps:
317                $(WithVertexProp<$c> + WithEdgeProp<$c> +)* { }
318        }
319    )
320}
321
322macro_rules! basic_props2 {
323    ($($v:ty),* ; $($e:ty),* ; $($c:ty),* ) => (
324        basic_props1! {
325            $($v),+ , $(Vec<$v>),+ ;
326            $($e),+ , $(Vec<$e>),+ ;
327            $($c),+ , $(Vec<$c>),+
328        }
329    )
330}
331
332macro_rules! basic_props {
333    ($($t:ty),*) => (
334        basic_props2! {
335            $($t),+, Vertex<Self>, OptionVertex<Self> ;
336            $($t),+, Edge<Self>, OptionEdge<Self> ;
337            $($t),+, Vertex<Self>, OptionVertex<Self>, Edge<Self>, OptionEdge<Self>
338        }
339    )
340}
341
342basic_props! {
343    bool,
344    char,
345    i8, i16, i32, i64, isize,
346    u8, u16, u32, u64, usize,
347    f32, f64,
348    &'static str, String,
349    Color
350}
351
352#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
355pub enum Color {
356    White,
358    Gray,
361    Black,
364}
365
366impl Default for Color {
367    #[inline]
369    fn default() -> Color {
370        Color::White
371    }
372}
373
374pub struct Map<P, F>(P, F);
382
383impl<K, P, F, O> PropGet<K> for Map<P, F>
384where
385    P: PropGet<K>,
386    F: Fn(P::Output) -> O,
387{
388    type Output = O;
389
390    #[inline]
391    fn get(&self, k: K) -> Self::Output {
392        (self.1)(self.0.get(k))
393    }
394}