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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
//! Flat data structures for binary relations.

use crate::flags::Arc;
use crate::iterators;
use crate::iterators::StreamingIterator;
use std::fmt::Debug;
use std::iter::Chain;
use std::mem::swap;
use std::ops::{Index, IndexMut, Neg, Range};

/// Common interface for square matrices stored in a single array while
/// taking advantage of symetries.
pub trait FlatMatrix: Sized {
    /// Type of the entries of the matrix.
    type Item;
    /// Iterator on one line of the matrix.
    type LineIter: Iterator<Item = usize>;
    // needed
    /// Length of the underlying vector depending on the number of
    /// line of the matrix.
    fn data_size(size: usize) -> usize;
    /// Direct access to the underlying vector
    fn get_vec(&self) -> &Vec<Self::Item>;
    /// Mutable access to the underlying vector
    fn get_vec_mut(&mut self) -> &mut Vec<Self::Item>;
    /// Create a matrix by wrapping the underlying vector.
    fn from_vec(_: Vec<Self::Item>) -> Self;
    /// Map a pair of indices to the corresponding index in the matrix.
    fn flat_index(i: usize, j: usize) -> usize;
    /// Iterator on every non-symetric entries of the matrix.
    fn index_iter(n: usize) -> Box<Iterator<Item = (usize, usize)>>;
    /// Iterator on every non-symetric index an line `v`.
    fn line_iter(n: usize, v: usize) -> Self::LineIter;
    // recommended
    /// Give the number of line of the matrix
    fn possible_size(&self) -> usize {
        let mut res = 0;
        let data_size = self.get_vec().len();
        loop {
            debug_assert!(Self::data_size(res) <= data_size);
            if Self::data_size(res) == data_size {
                return res;
            } else {
                res += 1
            }
        }
    }
    // provided
    /// Access to a matrix element.
    #[inline]
    fn get(&self, i: usize, j: usize) -> Self::Item
    where
        Self::Item: Clone,
    {
        self.get_vec()[Self::flat_index(i, j)].clone()
    }
    /// Redefine a matrix entry.
    #[inline]
    fn set(&mut self, ij: (usize, usize), v: Self::Item) {
        self.get_vec_mut()[Self::flat_index(ij.0, ij.1)] = v
    }
    #[inline]
    /// Create a new matrix with `n` lines filled with `elem`.
    fn new(elem: Self::Item, n: usize) -> Self
    where
        Self::Item: Clone,
    {
        Self::from_vec(vec![elem; Self::data_size(n)])
    }
    /// Change the number of line in the matrix to `n`.
    /// If line are added, fill them with `e`.
    #[inline]
    fn resize(&mut self, n: usize, e: Self::Item)
    where
        Self::Item: Clone,
    {
        self.get_vec_mut().resize(Self::data_size(n), e)
    }
}

/// Relation R such that R(x,y) iff R(y,x).
#[derive(Clone, Debug, PartialOrd, Ord, Eq, PartialEq, Serialize, Deserialize)]
pub struct Sym<A>(Vec<A>);

use std;

impl<A> FlatMatrix for Sym<A> {
    type Item = A;
    type LineIter = Range<usize>;
    #[inline]
    fn data_size(size: usize) -> usize {
        (size * (size + 1)) / 2
    }

    #[inline]
    fn flat_index(mut i: usize, mut j: usize) -> usize {
        if j < i {
            swap(&mut i, &mut j)
        };
        Self::data_size(j) + i
    }
    #[inline]
    fn get_vec(&self) -> &Vec<A> {
        &self.0
    }
    #[inline]
    fn get_vec_mut(&mut self) -> &mut Vec<A> {
        &mut self.0
    }
    #[inline]
    fn from_vec(v: Vec<A>) -> Self {
        Sym(v)
    }
    #[inline]
    fn index_iter(n: usize) -> Box<Iterator<Item = (usize, usize)>> {
        Box::new((0..n).flat_map(move |j| (0..=j).map(move |i| (i, j))))
    }
    #[inline]
    fn line_iter(n: usize, v: usize) -> Self::LineIter {
        debug_assert!(v <= n);
        (0..n)
    }
}

impl<A> Index<(usize, usize)> for Sym<A> {
    type Output = A;

    fn index(&self, (i, j): (usize, usize)) -> &Self::Output {
        &self.0[Self::flat_index(i, j)]
    }
}

impl<A> IndexMut<(usize, usize)> for Sym<A> {
    fn index_mut(&mut self, (i, j): (usize, usize)) -> &mut A {
        &mut self.0[Self::flat_index(i, j)]
    }
}

/// Symetric relation R such that R(x,x) never holds
#[derive(Clone, Debug, PartialOrd, Ord, Eq, PartialEq, Serialize, Deserialize)]
pub struct SymNonRefl<A>(Vec<A>);

impl<A> FlatMatrix for SymNonRefl<A> {
    type Item = A;
    type LineIter = Chain<Range<usize>, Range<usize>>;

    fn data_size(size: usize) -> usize {
        if size == 0 {
            0
        } else {
            ((size - 1) * (size)) / 2
        }
    }

    fn flat_index(mut i: usize, mut j: usize) -> usize {
        if j < i {
            swap(&mut i, &mut j)
        };
        debug_assert!(j > i);
        Self::data_size(j) + i
    }
    fn get_vec(&self) -> &Vec<A> {
        &self.0
    }
    fn get_vec_mut(&mut self) -> &mut Vec<A> {
        &mut self.0
    }
    fn from_vec(v: Vec<A>) -> Self {
        SymNonRefl(v)
    }
    fn index_iter(n: usize) -> Box<Iterator<Item = (usize, usize)>> {
        Box::new((0..n).flat_map(move |j| (0..j).map(move |i| (i, j))))
    }
    fn line_iter(n: usize, v: usize) -> Self::LineIter {
        debug_assert!(v <= n);
        (0..v).chain(v + 1..n)
    }
}

impl<A> Index<(usize, usize)> for SymNonRefl<A> {
    type Output = A;

    fn index(&self, (i, j): (usize, usize)) -> &Self::Output {
        &self.0[Self::flat_index(i, j)]
    }
}

impl<A> IndexMut<(usize, usize)> for SymNonRefl<A> {
    fn index_mut(&mut self, (i, j): (usize, usize)) -> &mut A {
        &mut self.0[Self::flat_index(i, j)]
    }
}

/// Relation R such that R(x,y) = -R(y,x) and R(x,x) does not hold.
#[derive(Clone, Debug, PartialOrd, Ord, Eq, PartialEq, Serialize, Deserialize)]
pub struct AntiSym<A>(Vec<A>);

impl<A> AntiSym<A>
where
    A: Neg<Output = A> + Copy,
{
    fn flat_index_raw(i: usize, j: usize) -> usize {
        debug_assert!(j > i);
        Self::data_size(j) + i
    }
}

impl<A> FlatMatrix for AntiSym<A>
where
    A: Neg<Output = A> + Copy,
{
    type Item = A;
    type LineIter = Chain<Range<usize>, Range<usize>>;

    fn data_size(size: usize) -> usize {
        SymNonRefl::<A>::data_size(size)
    }
    fn flat_index(i: usize, j: usize) -> usize {
        if j > i {
            Self::flat_index_raw(i, j)
        } else {
            Self::flat_index_raw(j, i)
        }
    }
    fn get_vec(&self) -> &Vec<A> {
        &self.0
    }
    fn get_vec_mut(&mut self) -> &mut Vec<A> {
        &mut self.0
    }
    fn from_vec(v: Vec<A>) -> Self {
        AntiSym(v)
    }
    fn index_iter(n: usize) -> Box<Iterator<Item = (usize, usize)>> {
        Box::new((0..n).flat_map(move |i| (0..i).map(move |j| (i, j))))
    }
    fn line_iter(n: usize, v: usize) -> Self::LineIter {
        debug_assert!(v <= n);
        (0..v).chain(v + 1..n)
    }
    fn get(&self, i: usize, j: usize) -> A {
        debug_assert!(i != j);
        if i < j {
            self.0[Self::flat_index_raw(i, j)]
        } else {
            -self.0[Self::flat_index_raw(j, i)]
        }
    }
    fn set(&mut self, (i, j): (usize, usize), v: A) {
        if i < j {
            self.0[Self::flat_index_raw(i, j)] = v
        } else {
            self.0[Self::flat_index_raw(j, i)] = -v
        }
    }
}

/// A trait that gives access to the list of the possible values
/// of a type.
pub trait Enum: Sized {
    /// List of possible values of `Self`.
    fn variants() -> Vec<Self>;
}

impl Enum for bool {
    fn variants() -> Vec<Self> {
        vec![true, false]
    }
}

impl Enum for Arc {
    fn variants() -> Vec<Self> {
        vec![Arc::Edge, Arc::BackEdge, Arc::None]
    }
}

/// Generic trait for binary relations.
pub trait BinRelation: Ord + Debug + Clone {
    fn invariant(&self, v: usize) -> Vec<Vec<usize>>;
    fn invariant_size() -> usize;
    fn induce(&self, p: &[usize]) -> Self;
    fn empty() -> Self;
    fn extensions(&self, n: usize) -> Vec<Self>;
}

impl<S> BinRelation for S
where
    S: FlatMatrix + Debug + Clone + Ord,
    S::Item: Enum + Ord + Clone + Copy + Debug + Default,
{
    fn invariant(&self, v: usize) -> Vec<Vec<usize>> {
        let mut variants = S::Item::variants();
        variants.sort();
        let mut res: Vec<Vec<usize>> = vec![Vec::new(); variants.len() - 1];
        let n = self.possible_size();
        for u in Self::line_iter(n, v) {
            let val = self.get(u, v);
            let e = variants.iter().position(|x| x == &val).unwrap();
            if e < variants.len() - 1 {
                res[e].push(u);
            }
        }
        res
    }
    fn invariant_size() -> usize {
        S::Item::variants().len() - 1
    }
    fn extensions(&self, n: usize) -> Vec<Self> {
        assert_eq!(self.get_vec().len(), Self::data_size(n));
        let mut res = Vec::new();
        let variants = S::Item::variants();
        let line_size = Self::line_iter(n + 1, n).count();
        let mut iter = iterators::Functions::new(line_size, variants.len());
        while let Some(f) = iter.next() {
            let mut new = self.clone();
            new.resize(n + 1, S::Item::default());
            for v in Self::line_iter(n + 1, n) {
                new.get_vec_mut()[Self::flat_index(v, n)] = variants[f[v]];
            }
            res.push(new);
        }
        res
    }
    fn induce(&self, p: &[usize]) -> Self
    where
        S::Item: Default + Clone,
    {
        let n = p.len();
        let mut res = Self::new(S::Item::default(), n);
        for (u1, u2) in Self::index_iter(n) {
            res.set((u1, u2), self.get(p[u1], p[u2]));
        }
        res
    }
    fn empty() -> Self {
        Self::new(S::Item::default(), 0)
    }
}

// Extension of BinRelation to small tuples: should be automatized
// size 2
impl<R1, R2> BinRelation for (R1, R2)
where
    R1: BinRelation,
    R2: BinRelation,
{
    fn invariant(&self, v: usize) -> Vec<Vec<usize>> {
        let mut res = self.0.invariant(v);
        res.append(&mut self.1.invariant(v));
        res
    }
    fn invariant_size() -> usize {
        R1::invariant_size() + R2::invariant_size()
    }
    fn induce(&self, p: &[usize]) -> Self {
        (self.0.induce(p), self.1.induce(p))
    }
    fn empty() -> Self {
        (R1::empty(), R2::empty())
    }
    fn extensions(&self, n: usize) -> Vec<Self> {
        let e1 = self.0.extensions(n);
        let e2 = self.1.extensions(n);
        let mut res = Vec::new();
        for x1 in &e1 {
            for x2 in &e2 {
                res.push((x1.clone(), x2.clone()))
            }
        }
        res
    }
}
// size 3
impl<R1, R2, R3> BinRelation for (R1, R2, R3)
where
    R1: BinRelation,
    R2: BinRelation,
    R3: BinRelation,
{
    fn invariant(&self, v: usize) -> Vec<Vec<usize>> {
        let mut res = self.0.invariant(v);
        res.append(&mut self.1.invariant(v));
        res.append(&mut self.2.invariant(v));
        res
    }
    fn invariant_size() -> usize {
        R1::invariant_size() + R2::invariant_size() + R3::invariant_size()
    }
    fn induce(&self, p: &[usize]) -> Self {
        (self.0.induce(p), self.1.induce(p), self.2.induce(p))
    }
    fn empty() -> Self {
        (R1::empty(), R2::empty(), R3::empty())
    }
    fn extensions(&self, n: usize) -> Vec<Self> {
        let e1 = self.0.extensions(n);
        let e2 = self.1.extensions(n);
        let e3 = self.2.extensions(n);
        let mut res = Vec::new();
        for x1 in &e1 {
            for x2 in &e2 {
                for x3 in &e3 {
                    res.push((x1.clone(), x2.clone(), x3.clone()))
                }
            }
        }
        res
    }
}

/// Tests
#[cfg(test)]
mod tests {
    use super::*;
    use crate::flags::*;
    use crate::Flag;

    fn auto_test_flatmatrix<S: FlatMatrix<Item = i64>>(n: usize) {
        assert_eq!(S::index_iter(n).count(), S::data_size(n));
        println!("{}_{}", n + 1, n / 2);
        assert_eq!(
            S::line_iter(n + 1, n / 2).count(),
            S::data_size(n + 1) - S::data_size(n)
        );
        // injectivity of line_iter
        for u in 0..n {
            let mut x = S::new(0, n);
            for v in S::line_iter(n, u) {
                assert_eq!(x.get(u, v), 0);
                x.set((u, v), 1);
            }
        }
        // injectivity of index_iter
        let mut x = S::new(0, n);
        for (u, v) in S::index_iter(n) {
            assert_eq!(x.get(u, v), 0);
            x.set((u, v), 1);
        }
    }

    #[test]
    fn generate_graph() {
        for (size, &nb) in [1, 1, 2, 4, 11, 34].iter().enumerate() {
            assert_eq!(Graph::generate(size).len(), nb);
        }
    }

    // #[test]
    // fn symflatmatrix_size_unit() {
    //     for i in 0..4 {
    //         let m = Sym::new(0,i);
    //         assert_eq!(i, m.size())
    //     }
    // }

    #[test]
    fn symnonrefl() {
        assert_eq!(SymNonRefl::new(42, 0).0.len(), 0);
        assert_eq!(SymNonRefl::new(42, 5)[(4, 3)], 42);
        let mut m = SymNonRefl::new(0, 12);
        m[(3, 2)] = 11;
        assert_eq!(m[(2, 3)], 11);
        m[(3, 4)] = 22;
        assert_eq!(m[(4, 3)], 22);

        let n = 10;
        let mut m = SymNonRefl::new(0, n);
        for i in 0..n {
            for j in 0..n {
                if i != j {
                    m[(i, j)] += 1;
                }
            }
        }
        for &x in m.0.iter() {
            assert_eq!(x, 2)
        }
        //        assert_eq!(SymNonRefl::<bool>::invariant_size(), 1)
    }

    #[test]
    fn antisym() {
        assert_eq!(AntiSym::new(42, 0).0.len(), 0);
        let mut rel = AntiSym::new(0, 12);
        rel.set((5, 2), 42);
        assert_eq!(rel.get(5, 2), 42);
        assert_eq!(rel.get(2, 5), -42);
        let n = 10;
        let mut m = AntiSym::new(0, n);
        for i in 0..n {
            for j in 0..n {
                if i != j {
                    let v = m.get(i, j);
                    if i < j {
                        assert_eq!(v, 0);
                        m.set((i, j), 42)
                    } else {
                        assert_eq!(v, -42)
                    }
                }
            }
        }
        for &x in m.0.iter() {
            assert!(x != 0)
        }
    }

    #[test]
    fn flatmatrix_generic() {
        auto_test_flatmatrix::<Sym<_>>(0);
        auto_test_flatmatrix::<Sym<_>>(1);
        auto_test_flatmatrix::<Sym<_>>(5);
        //
        auto_test_flatmatrix::<AntiSym<_>>(0);
        auto_test_flatmatrix::<AntiSym<_>>(1);
        auto_test_flatmatrix::<AntiSym<_>>(5);
        //
        // auto_test_flatmatrix::<NonRefl<_>>(0);
        // auto_test_flatmatrix::<NonRefl<_>>(1);
        // auto_test_flatmatrix::<NonRefl<_>>(5);
        //
        auto_test_flatmatrix::<SymNonRefl<_>>(0);
        auto_test_flatmatrix::<SymNonRefl<_>>(1);
        auto_test_flatmatrix::<SymNonRefl<_>>(5);
    }
}