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

use std::marker::PhantomData;

use Construct;
use Data;
use Count;
use Of;
use ToIndex;
use ToPos;
use Zero;

/// Same as `Context`, but for directed edges.
pub struct DirectedContext<T = Data>(PhantomData<T>);

impl<T> Construct for DirectedContext<T> {
    fn new() -> DirectedContext<T> { DirectedContext(PhantomData) }
}

impl Count<Vec<usize>> for DirectedContext<Data> {
    fn count(&self, dim: &Vec<usize>) -> usize {
        use NeqPair;

        let pair: NeqPair<Data> = Construct::new();
        let mut sum = pair.count(&dim[0]);
        let mut prod = dim[0];
        for d in &dim[1..] {
            sum = d * sum + pair.count(d) * prod;
            prod *= *d;
        }
        sum
    }
}

impl<T, U> Count<Vec<U>> for DirectedContext<Of<T>>
    where T: Construct + Count<U>
{
    fn count(&self, dim: &Vec<U>) -> usize {
        use NeqPair;

        let of: T = Construct::new();
        let pair: NeqPair<Data> = Construct::new();
        let mut sum = pair.count(&of.count(&dim[0]));
        let mut prod = of.count(&dim[0]);
        for d in &dim[1..] {
            let d = of.count(d);
            sum = d * sum + pair.count(&d) * prod;
            prod *= d;
        }
        sum
    }
}

impl Zero<Vec<usize>, (Vec<usize>, usize, usize)> for DirectedContext<Data> {
    fn zero(&self, dim: &Vec<usize>) -> (Vec<usize>, usize, usize) {
        (vec![0; dim.len()], 0, 0)
    }
}

impl<T, U, V>
Zero<Vec<U>, (Vec<V>, usize, V)>
for DirectedContext<Of<T>>
    where T: Construct + Count<U> + ToPos<U, V> + Zero<U, V>
{
    fn zero(&self, dim: &Vec<U>) -> (Vec<V>, usize, V) {
        let of: T = Construct::new();
        let mut v = Vec::with_capacity(dim.len());
        for i in 0..dim.len() {
            v.push(of.zero(&dim[i]));
        }
        (v, 0, of.zero(&dim[0]))
    }
}

impl ToIndex<Vec<usize>, (Vec<usize>, usize, usize)> for DirectedContext<Data> {
    fn to_index(
        &self, dim: &Vec<usize>,
        &(ref p, ind, b): &(Vec<usize>, usize, usize)
    ) -> usize {
        use Context;

        let context: Context<Data> = Construct::new();
        let index = context.to_index(dim, &(p.clone(), ind, b));
        if p[ind] > b {
            2 * index + 1
        } else {
            2 * index
        }
    }
}

impl<T, U, V> ToIndex<Vec<U>, (Vec<V>, usize, V)>
for DirectedContext<Of<T>>
    where T: Construct + Count<U> + ToIndex<U, V>,
          V: Clone
{
    fn to_index(
        &self,
        dim: &Vec<U>,
        &(ref p, ind, ref b): &(Vec<V>, usize, V)
    ) -> usize {
        use Context;

        let of: T = Construct::new();
        let context: Context<Of<T>> = Construct::new();
        let index = context.to_index(dim, &(p.clone(), ind, b.clone()));
        if of.to_index(&dim[ind], &p[ind]) > of.to_index(&dim[ind], b) {
            2 * index + 1
        } else {
            2 * index
        }
    }
}

impl ToPos<Vec<usize>, (Vec<usize>, usize, usize)> for DirectedContext<Data> {
    fn to_pos(
        &self,
        dim: &Vec<usize>,
        index: usize,
        pos: &mut (Vec<usize>, usize, usize)
    ) {
        use Context;

        let context: Context<Data> = Construct::new();
        if index % 2 == 0 {
            context.to_pos(dim, index / 2, pos);
        } else {
            context.to_pos(dim, (index - 1) / 2, pos);
            let tmp = pos.0[pos.1];
            pos.0[pos.1] = pos.2;
            pos.2 = tmp;
        }
    }
}

impl<T, U, V>
ToPos<Vec<U>, (Vec<V>, usize, V)>
for DirectedContext<Of<T>>
    where T: Construct + Count<U> + ToPos<U, V> + Zero<U, V>,
          V: Clone
{
    fn to_pos(
        &self,
        dim: &Vec<U>,
        index: usize,
        pos: &mut (Vec<V>, usize, V)
    ) {
        use Context;

        let context: Context<Of<T>> = Construct::new();
        if index % 2 == 0 {
            context.to_pos(dim, index / 2, pos);
        } else {
            context.to_pos(dim, (index - 1) / 2, pos);
            let tmp = pos.0[pos.1].clone();
            pos.0[pos.1] = pos.2.clone();
            pos.2 = tmp;
        }
    }
}

#[cfg(test)]
mod tests {
    use super::super::*;

    #[test]
    fn features() {
        is_complete::<DirectedContext, Vec<usize>, (Vec<usize>, usize, usize)>();
        is_complete::<DirectedContext<Of<Pair>>, Vec<usize>,
            (Vec<(usize, usize)>, usize, (usize, usize))>();
    }

    #[test]
    fn data() {
        let x: DirectedContext = Construct::new();
        let ref dim = vec![2, 2, 2];
        // 12 edges on a cube * 2 = 24 directed edges
        assert_eq!(x.count(dim), 24);
        assert_eq!(x.to_index(dim, &(vec![0, 0, 0], 0, 1)), 0);
        assert_eq!(x.to_index(dim, &(vec![1, 0, 0], 0, 0)), 1);
        for i in 0..x.count(dim) {
            let mut pos = (vec![], 0, 0);
            x.to_pos(dim, i, &mut pos);
            println!("{:?}", pos);
            assert_eq!(x.to_index(dim, &pos), i);
        }
        // assert!(false);
    }
}