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
use core::mem::swap;

/// Solution is a set/tuple of flattened and indexed variables.
pub trait Solution {
  const MAX_LEN: usize;

  fn has_var(&self, idx: usize) -> bool;

  fn inter_swap(&mut self, other: &mut Self, idx: usize);

  fn intra_swap(&mut self, a: usize, b: usize);

  fn is_empty(&self) -> bool {
    self.len() == 0
  }

  fn len(&self) -> usize;
}

macro_rules! array_impls {
  ($($N:expr),+) => {
    $(
      impl<T> Solution for [T; $N] {
        const MAX_LEN: usize = $N;

        fn has_var(&self, idx: usize) -> bool {
          idx < self.len()
        }

        fn inter_swap(&mut self, other: &mut Self, idx: usize) {
          assert!(idx < self.len());
          swap(&mut self[idx], &mut other[idx]);
        }

        fn intra_swap(&mut self, a: usize, b: usize) {
          self.swap(a, b);
        }

        fn len(&self) -> usize {
          $N
        }
      }

      impl<T> Solution for arrayvec::ArrayVec<[T; $N]> {
        const MAX_LEN: usize = $N;

        fn has_var(&self, idx: usize) -> bool {
          idx < self.len()
        }

        fn inter_swap(&mut self, other: &mut Self, idx: usize) {
          assert!(idx < self.len());
          swap(&mut self[idx], &mut other[idx]);
        }

        fn intra_swap(&mut self, a: usize, b: usize) {
          self.swap(a, b);
        }

        fn len(&self) -> usize {
          self.len()
        }
      }

      #[cfg(feature = "with-ndsparse")]
      impl<DATA, DS, IS, OS> Solution for ndsparse::csl::Csl<[usize; $N], DS, IS, OS>
      where
        DS: AsMut<[DATA]> + AsRef<[DATA]> + cl_traits::Storage<Item = DATA>,
        IS: AsRef<[usize]>,
        OS: AsRef<[usize]>,
      {
        const MAX_LEN: usize = $N;

        fn has_var(&self, idx: usize) -> bool {
          idx < self.len()
        }

        fn inter_swap(&mut self, other: &mut Self, idx: usize) {
          assert!(idx < self.len());
          swap(&mut self.data_mut()[idx], &mut other.data_mut()[idx]);
        }

        fn intra_swap(&mut self, a: usize, b: usize) {
          self.data_mut().swap(a, b);
        }

        fn len(&self) -> usize {
          self.data().len()
        }
      }
    )+
  }
}

array_impls!(
  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
);