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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// 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/.

//! Traits and implementation for properties (key to values mapping).
//!
//! # Examples
//!
//! ```
//! use fera_graph::prelude::*;
//!
//! let g = CompleteGraph::new(4);
//! let mut p = g.default_vertex_prop(0i32);
//! p[3] = -3;
//! assert_eq!(0, p.get(0));
//! assert_eq!(-3, p.get(3));
//! let abs_p = p.map(i32::abs);
//! assert_eq!(3, abs_p.get(3));
//! ```

use prelude::*;
use std::ops::{Index, IndexMut};

mod array;
mod delegate;
#[path = "fn.rs"]
mod fn_;
mod hashmap;
mod ignore;

pub use self::array::*;
pub use self::delegate::*;
pub use self::fn_::*;
pub use self::hashmap::*;
pub use self::ignore::*;

use params::IntoOwned;

/// An abstract property that maps keys in domain `K` to the corresponding values.
pub trait PropGet<K> {
    type Output: Sized;

    /// Returns the value associated with `key`.
    fn get(&self, key: K) -> Self::Output;

    /// Creates a mapped property that maps each property value using `fun`.
    #[inline]
    fn map<F, O>(self, fun: F) -> Map<Self, F>
    where
        Self: Sized,
        F: Fn(Self::Output) -> O,
    {
        Map(self, fun)
    }

    /// Returns a reference to this property.
    #[inline]
    fn by_ref(&self) -> &Self {
        self
    }
}

impl<'a, K, P: PropGet<K>> PropGet<K> for &'a P {
    type Output = P::Output;

    #[inline]
    fn get(&self, key: K) -> Self::Output {
        P::get(self, key)
    }
}

// Indexable properties

// TODO: turn PropIndexMut into a extension trait
/// A property that can be read/write using indexing operations.
pub trait PropIndexMut<Idx>: IndexMut<Idx> {
    /// Set the value associated with each key produced by `iter` to the value associated with the
    /// key in the property `source`.
    fn set_values_from<P, I>(&mut self, iter: I, source: &P)
    where
        I: IntoIterator,
        I::Item: IntoOwned<Idx>,
        Idx: Clone,
        P: Index<Idx, Output = Self::Output>,
        Self::Output: Clone,
    {
        for v in iter {
            let v = v.into_owned();
            self[v.clone()].clone_from(&source[v]);
        }
    }

    /// Set the value associated with keys produced by `iter` to `value`.
    fn set_values<I>(&mut self, iter: I, value: Self::Output)
    where
        I: IntoIterator,
        I::Item: IntoOwned<Idx>,
        Self::Output: Clone,
    {
        for v in iter {
            self[v.into_owned()].clone_from(&value);
        }
    }
}

impl<P: IndexMut<Idx>, Idx> PropIndexMut<Idx> for P {}

// TODO: explain why the trait repetition for VertexProp and EdgeProp (missing trait alias?)

// Vertex

/// A vertex property.
pub trait VertexPropGet<G, T>: PropGet<Vertex<G>, Output = T>
where
    G: WithVertex,
{
}

impl<P, G, T> VertexPropGet<G, T> for P
where
    G: WithVertex,
    P: PropGet<Vertex<G>, Output = T>,
{
}

/// A vertex property that can be read using indexing operation.
pub trait VertexProp<G, T>: Index<Vertex<G>, Output = T>
where
    G: WithVertex,
{
}

impl<P, G, T> VertexProp<G, T> for P
where
    G: WithVertex,
    P: Index<Vertex<G>, Output = T>,
{
}

/// A vertex property that can be read/write using indexing operation.
pub trait VertexPropMut<G, T>: IndexMut<Vertex<G>, Output = T>
where
    G: WithVertex,
{
}

impl<P, G, T> VertexPropMut<G, T> for P
where
    G: WithVertex,
    P: IndexMut<Vertex<G>, Output = T>,
{
}

/// A vertex property that can be created using a graph reference and a value.
pub trait VertexPropMutNew<G, T>: VertexPropMut<G, T>
where
    G: WithVertex,
{
    /// Creates a new vertex prop.
    ///
    /// This method is rarely called explicitly, it is instead used through
    /// [`WithVertex::vertex_prop`].
    ///
    /// [`WithVertex::vertex_prop`]: ../trait.WithVertex.html#method.vertex_prop
    fn new_vertex_prop(g: &G, value: T) -> Self
    where
        T: Clone;
}

/// A graph that has a default vertex property type, that is, has a default implementation to
/// associated values with vertices.
pub trait WithVertexProp<T>: WithVertex {
    /// The vertex property type.
    type VertexProp: VertexPropMutNew<Self, T>;

    /// Creates a new default vertex property where the initial value associated with each vertex
    /// is `value`.
    fn default_vertex_prop(&self, value: T) -> DefaultVertexPropMut<Self, T>
    where
        T: Clone,
    {
        self.vertex_prop(value)
    }

    /// Creates a new default vertex property where the initial value associated with each vertex
    /// `v` is produced by `fun(v)`.
    fn default_vertex_prop_from_fn<P, F>(&self, fun: F) -> P
    where
        Self: VertexList,
        P: VertexPropMutNew<Self, T>,
        F: FnMut(Vertex<Self>) -> T,
        T: Default + Clone,
    {
        self.vertex_prop_from_fn(fun)
    }
}

/// A graph that has a property that maps each vertex to an integer in the range `0..num_vertices`.
pub trait WithVertexIndexProp: WithVertex {
    type VertexIndexProp: VertexPropGet<Self, usize>;

    /// Creates an vertex index map.
    fn vertex_index(&self) -> VertexIndexProp<Self>;
}

// Edge

/// A edge property.
pub trait EdgePropGet<G, T>: PropGet<Edge<G>, Output = T>
where
    G: WithEdge,
{
}

impl<P, G, T> EdgePropGet<G, T> for P
where
    G: WithEdge,
    P: PropGet<Edge<G>, Output = T>,
{
}

/// An edge property that can be read using indexing operation.
pub trait EdgeProp<G, T>: Index<Edge<G>, Output = T>
where
    G: WithEdge,
{
}

impl<P, G, T> EdgeProp<G, T> for P
where
    G: WithEdge,
    P: Index<Edge<G>, Output = T>,
{
}

/// A edge property that can be read/write using indexing operation.
pub trait EdgePropMut<G, T>: IndexMut<Edge<G>, Output = T>
where
    G: WithEdge,
{
}

impl<P, G, T> EdgePropMut<G, T> for P
where
    G: WithEdge,
    P: IndexMut<Edge<G>, Output = T>,
{
}

/// An edge property that can be read/write using indexing operation.
pub trait EdgePropMutNew<G, T>: EdgePropMut<G, T>
where
    G: WithEdge,
{
    /// Creates a new edge prop.
    ///
    /// This method is rarely called explicitly, it is instead used through
    /// [`WithEdge::edge_prop`].
    ///
    /// [`WithEdge::edge_prop`]: ../trait.WithEdge.html#method.edge_prop
    fn new_edge_prop(g: &G, value: T) -> Self
    where
        T: Clone;
}

/// A graph that has a default edge property type, that is, has a default implementation to
/// associated values with edges.
pub trait WithEdgeProp<T>: WithEdge {
    type EdgeProp: EdgePropMutNew<Self, T>;

    /// Creates a new default edge property where the initial value associated with each edge is
    /// `value`.
    fn default_edge_prop(&self, value: T) -> DefaultEdgePropMut<Self, T>
    where
        T: Clone,
    {
        self.edge_prop(value)
    }

    /// Creates a new default edge property where the initial value associated with each edge `e`
    /// is produced by `fun(e)`.
    fn default_edge_prop_from_fn<P, F>(&self, fun: F) -> P
    where
        Self: EdgeList,
        P: EdgePropMutNew<Self, T>,
        F: FnMut(Edge<Self>) -> T,
        T: Default + Clone,
    {
        self.edge_prop_from_fn(fun)
    }
}

/// A graph that has a property that maps each edge to an integer in the range `0..num_edges`.
pub trait WithEdgeIndexProp: WithEdge {
    type EdgeIndexProp: EdgePropGet<Self, usize>;

    /// Creates an edge index map.
    fn edge_index(&self) -> EdgeIndexProp<Self>;
}

// Basic properties traits

macro_rules! items {
    ($($item:item)*) => ($($item)*);
}

macro_rules! basic_props1 {
    ($($v:ty),* ; $($e:ty),* ; $($c:ty),*) => (
        items! {
            pub trait BasicVertexProps:
                $(WithVertexProp<$v> +)* { }

            pub trait BasicEdgeProps:
                $(WithEdgeProp<$e> +)* { }

            pub trait BasicProps:
                $(WithVertexProp<$c> + WithEdgeProp<$c> +)* { }
        }
    )
}

macro_rules! basic_props2 {
    ($($v:ty),* ; $($e:ty),* ; $($c:ty),* ) => (
        basic_props1! {
            $($v),+ , $(Vec<$v>),+ ;
            $($e),+ , $(Vec<$e>),+ ;
            $($c),+ , $(Vec<$c>),+
        }
    )
}

macro_rules! basic_props {
    ($($t:ty),*) => (
        basic_props2! {
            $($t),+, Vertex<Self>, OptionVertex<Self> ;
            $($t),+, Edge<Self>, OptionEdge<Self> ;
            $($t),+, Vertex<Self>, OptionVertex<Self>, Edge<Self>, OptionEdge<Self>
        }
    )
}

basic_props! {
    bool,
    char,
    i8, i16, i32, i64, isize,
    u8, u16, u32, u64, usize,
    f32, f64,
    &'static str, String,
    Color
}

/// Indicates the status of an item in a traverse algorithm.
///
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub enum Color {
    /// Generally indicates that an item was not discovered in the search.
    White,
    /// Generally indicates that an item was discovered in the search but there is some pending
    /// work to be done with the item.
    Gray,
    /// Generally indicates that the item was discovered and all the work related with the item is
    /// finished.
    Black,
}

impl Default for Color {
    /// Returns `Color::White`.
    #[inline]
    fn default() -> Color {
        Color::White
    }
}

// Adaptors

/// A property that maps the value of a wrapped property with a function.
///
/// This `struct` is created by [`PropGet::map`].
///
/// [`PropGet::map`]: trait.PropGet.html#method.map
pub struct Map<P, F>(P, F);

impl<K, P, F, O> PropGet<K> for Map<P, F>
where
    P: PropGet<K>,
    F: Fn(P::Output) -> O,
{
    type Output = O;

    #[inline]
    fn get(&self, k: K) -> Self::Output {
        (self.1)(self.0.get(k))
    }
}