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
// 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 std::ops::{Index, IndexMut};

// TODO: define a feature to disable bounds check (or a property type?).
/// A vertex property backed by a [`Vec`].
///
/// [`Vec`]: https://doc.rust-lang.org/stable/std/vec/struct.Vec.html
pub type VecVertexProp<G, T> = ArrayProp<VertexIndexProp<G>, Vec<T>>;

/// A edge property backed by a [`Vec`].
///
/// [`Vec`]: https://doc.rust-lang.org/stable/std/vec/struct.Vec.html
pub type VecEdgeProp<G, T> = ArrayProp<EdgeIndexProp<G>, Vec<T>>;

// TODO: Define SliceVertexProp and SliceEdgeProp

// TODO: Rename to SequenceProp
/// A property backed by an array.
#[derive(Clone, Debug)]
pub struct ArrayProp<P, D> {
    index: P,
    data: D,
}

impl<P, D> ArrayProp<P, D> {
    fn new(index: P, data: D) -> Self {
        Self { index, data }
    }
}

impl<I, P, D> PropGet<I> for ArrayProp<P, D>
where
    P: PropGet<I, Output = usize>,
    D: Index<usize>,
    D::Output: Clone + Sized,
{
    type Output = D::Output;

    #[inline(always)]
    fn get(&self, item: I) -> D::Output {
        self.data.index(self.index.get(item)).clone()
    }
}

impl<I, P, D> Index<I> for ArrayProp<P, D>
where
    P: PropGet<I, Output = usize>,
    D: Index<usize>,
{
    type Output = D::Output;

    #[inline(always)]
    fn index(&self, item: I) -> &Self::Output {
        self.data.index(self.index.get(item))
    }
}

impl<I, P, D> IndexMut<I> for ArrayProp<P, D>
where
    P: PropGet<I, Output = usize>,
    D: IndexMut<usize>,
{
    #[inline(always)]
    fn index_mut(&mut self, item: I) -> &mut Self::Output {
        self.data.index_mut(self.index.get(item))
    }
}

impl<T, G> VertexPropMutNew<G, T> for ArrayProp<VertexIndexProp<G>, Vec<T>>
where
    G: VertexList + WithVertexIndexProp,
{
    fn new_vertex_prop(g: &G, value: T) -> Self
    where
        T: Clone,
    {
        ArrayProp::new(g.vertex_index(), vec![value; g.num_vertices()])
    }
}

impl<T, G> EdgePropMutNew<G, T> for ArrayProp<EdgeIndexProp<G>, Vec<T>>
where
    G: EdgeList + WithEdgeIndexProp,
{
    fn new_edge_prop(g: &G, value: T) -> Self
    where
        T: Clone,
    {
        ArrayProp::new(g.edge_index(), vec![value; g.num_edges()])
    }
}