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
use crate::{common::*, perm_trait::Permutation, size::PermSize};

#[cfg(feature = "std")]
pub use with_std::*;
pub use without_std::*;

/// Generic permutation data structure.
#[derive(Clone, Debug, Eq, Hash)]
pub struct Perm<S>
where
    S: PermSize,
{
    pub(super) indices: S::Container,
}

impl<SL, SR> PartialEq<Perm<SR>> for Perm<SL>
where
    SL: PermSize,
    SR: PermSize,
{
    fn eq(&self, other: &Perm<SR>) -> bool {
        self.indices.as_ref() == other.indices.as_ref()
    }
}

mod without_std {
    use super::*;
    use crate::size::Static;

    /// Permutation type with static size known in compile time.
    pub type PermS<const SIZE: usize> = Perm<Static<{ SIZE }>>;

    /// Permutation type with static size 0.
    pub type Perm0 = PermS<0>;

    /// Permutation type with static size 1.
    pub type Perm1 = PermS<1>;

    /// Permutation type with static size 2.
    pub type Perm2 = PermS<2>;

    /// Permutation type with static size 4.
    pub type Perm4 = PermS<4>;

    /// Permutation type with static size 5.
    pub type Perm5 = PermS<5>;

    /// Permutation type with static size 6.
    pub type Perm6 = PermS<6>;

    /// Permutation type with static size 7.
    pub type Perm7 = PermS<7>;

    /// Permutation type with static size 8.
    pub type Perm8 = PermS<8>;

    /// Permutation type with static size 9.
    pub type Perm9 = PermS<9>;

    /// Permutation type with static size 10.
    pub type Perm10 = PermS<10>;

    /// Permutation type with static size 11.
    pub type Perm11 = PermS<11>;

    /// Permutation type with static size 12.
    pub type Perm12 = PermS<12>;

    /// Permutation type with static size 13.
    pub type Perm13 = PermS<13>;

    /// Permutation type with static size 14.
    pub type Perm14 = PermS<14>;

    /// Permutation type with static size 15.
    pub type Perm15 = PermS<15>;

    /// Permutation type with static size 16.
    pub type Perm16 = PermS<16>;

    /// Permutation type with static size 17.
    pub type Perm17 = PermS<17>;

    /// Permutation type with static size 18.
    pub type Perm18 = PermS<18>;

    /// Permutation type with static size 19.
    pub type Perm19 = PermS<19>;

    /// Permutation type with static size 20.
    pub type Perm20 = PermS<20>;

    /// Permutation type with static size 21.
    pub type Perm21 = PermS<21>;

    /// Permutation type with static size 22.
    pub type Perm22 = PermS<22>;

    /// Permutation type with static size 23.
    pub type Perm23 = PermS<23>;

    /// Permutation type with static size 24.
    pub type Perm24 = PermS<24>;

    /// Permutation type with static size 25.
    pub type Perm25 = PermS<25>;

    /// Permutation type with static size 26.
    pub type Perm26 = PermS<26>;

    /// Permutation type with static size 27.
    pub type Perm27 = PermS<27>;

    /// Permutation type with static size 28.
    pub type Perm28 = PermS<28>;

    /// Permutation type with static size 29.
    pub type Perm29 = PermS<29>;

    /// Permutation type with static size 30.
    pub type Perm30 = PermS<30>;

    /// Permutation type with static size 31.
    pub type Perm31 = PermS<31>;

    /// Permutation type with static size 32.
    pub type Perm32 = PermS<32>;

    impl<const SIZE: usize> PermS<SIZE> {
        pub fn identity() -> Self {
            let mut indices = [0; SIZE];
            (0..SIZE).for_each(|index| indices[index] = index);
            Self { indices }
        }

        pub fn swap(first: usize, second: usize) -> Option<Self> {
            if first >= SIZE || second >= SIZE || first == second {
                return None;
            }

            let min = first.min(second);
            let max = first.max(second);

            let mut indices = [0; SIZE];

            indices[min] = max;
            indices[max] = min;
            (0..min).for_each(|index| {
                indices[index] = index;
            });
            ((min + 1)..max).for_each(|index| {
                indices[index] = index;
            });
            ((max + 1)..SIZE).for_each(|index| {
                indices[index] = index;
            });

            Some(Self { indices })
        }

        pub fn cycle() -> Self {
            let mut indices = [0; SIZE];
            iter::once(SIZE - 1)
                .chain(0..(SIZE - 1))
                .enumerate()
                .for_each(|(dst, src)| {
                    indices[dst] = src;
                });
            Self { indices }
        }

        pub fn reverse_cycle() -> Self {
            let mut indices = [0; SIZE];
            (1..SIZE)
                .chain(iter::once(0))
                .enumerate()
                .for_each(|(dst, src)| {
                    indices[dst] = src;
                });
            Self { indices }
        }

        pub fn permute_indices(&self, perm: &PermS<SIZE>) -> Self {
            self.conjugate_with(perm)
        }

        pub fn conjugate_with(&self, other: &PermS<SIZE>) -> Self {
            &(&other.inverse() * self) * other
        }

        pub fn to_size<const NEW_SIZE: usize>(&self) -> Option<PermS<NEW_SIZE>> {
            if SIZE > NEW_SIZE {
                for dst in NEW_SIZE..SIZE {
                    let src = self.indices[dst];
                    if src != dst {
                        return None;
                    }
                }

                let mut new_indices = [0; NEW_SIZE];
                new_indices.copy_from_slice(&self.indices[..NEW_SIZE]);
                Some(PermS {
                    indices: new_indices,
                })
            } else {
                let mut new_indices = [0; NEW_SIZE];
                new_indices[..SIZE].copy_from_slice(&self.indices[..SIZE]);

                (SIZE..NEW_SIZE).for_each(|index| {
                    new_indices[index] = index;
                });

                Some(PermS {
                    indices: new_indices,
                })
            }
        }
    }

    impl Perm0 {
        pub fn empty() -> Self {
            Self { indices: [] }
        }
    }

    impl Perm1 {
        pub fn unit() -> Self {
            Self { indices: [0] }
        }
    }

    impl Perm2 {
        pub fn swap2() -> Self {
            Self { indices: [1, 0] }
        }
    }

    #[cfg(test)]
    mod tests {
        use super::*;
        use crate::{apply::PermApply, from_indices::PermFromIndices};
        use rand::prelude::*;

        #[test]
        fn static_identity() {
            const SIZE: usize = 2014;

            let perm = PermS::<SIZE>::identity();

            let mut rng = rand::thread_rng();
            let mut orig = [0usize; SIZE];
            rng.fill(orig.as_mut());

            let mut new = orig.clone();
            perm.apply(&mut new);

            assert_eq!(orig, new);
        }

        #[test]
        fn static_swap() {
            let perm = PermS::<6>::swap(5, 3).unwrap();
            assert_eq!(perm.indices(), &[0, 1, 2, 5, 4, 3]);
        }

        #[test]
        fn static_permute_indices() {
            let index_map = PermS::from_indices([3, 5, 0, 1, 2, 4]).unwrap();
            let orig = PermS::<6>::swap(5, 3).unwrap();
            let new = orig.permute_indices(&index_map);
            assert_eq!(new, PermS::<6>::swap(0, 1).unwrap());
        }
    }
}

#[cfg(feature = "std")]
mod with_std {
    use super::*;
    use crate::{product::PermProduct, size::Dynamic};

    /// Permutation type with runtime size.
    pub type PermD = Perm<Dynamic>;

    impl PermD {
        pub fn empty() -> Self {
            Self { indices: vec![] }
        }

        pub fn unit() -> Self {
            Self { indices: vec![0] }
        }

        pub fn swap(size: usize, first: usize, second: usize) -> Option<Self> {
            if first >= size || second >= size || first == second {
                return None;
            }

            let min = first.min(second);
            let max = first.max(second);

            let indices: Vec<_> = (0..min)
                .chain(iter::once(max))
                .chain((min + 1)..max)
                .chain(iter::once(min))
                .chain((max + 1)..size)
                .collect();

            Some(Self { indices })
        }

        pub fn identity(size: usize) -> Self {
            let mut indices = vec![0; size];
            (0..size).for_each(|index| indices[index] = index);
            Self { indices }
        }

        pub fn cycle(size: usize) -> Self {
            let mut indices = vec![0; size];
            iter::once(size - 1)
                .chain(0..(size - 1))
                .enumerate()
                .for_each(|(dst, src)| {
                    indices[dst] = src;
                });
            Self { indices }
        }

        pub fn reverse_cycle(size: usize) -> Self {
            let mut indices = vec![0; size];
            (1..size)
                .chain(iter::once(0))
                .enumerate()
                .for_each(|(dst, src)| {
                    indices[dst] = src;
                });
            Self { indices }
        }

        pub fn permute_indices(&self, perm: &PermD) -> Option<Self> {
            self.conjugate_with(perm)
        }

        pub fn conjugate_with(&self, other: &PermD) -> Option<Self> {
            other.inverse().perm_product(self)?.perm_product(other)
        }

        pub fn to_size(&self, new_size: usize) -> Option<PermD> {
            let orig_size = self.indices.len();
            if orig_size > new_size {
                for dst in new_size..orig_size {
                    let src = self.indices[dst];
                    if src != dst {
                        return None;
                    }
                }

                let mut new_indices = vec![0; new_size];
                new_indices.copy_from_slice(&self.indices[..new_size]);
                Some(PermD {
                    indices: new_indices,
                })
            } else {
                let mut new_indices = vec![0; new_size];
                new_indices[..orig_size].copy_from_slice(&self.indices[..orig_size]);

                (orig_size..new_size).for_each(|index| {
                    new_indices[index] = index;
                });

                Some(PermD {
                    indices: new_indices,
                })
            }
        }

        pub fn into_static<const SIZE: usize>(self) -> Option<PermS<SIZE>> {
            let Self { indices } = self;
            let indices = <[usize; SIZE]>::try_from(indices).ok()?;
            Some(PermS { indices })
        }
    }

    impl<const SIZE: usize> PermS<SIZE> {
        pub fn into_dynamic(self) -> PermD {
            let Self { indices } = self;
            PermD {
                indices: Vec::from(indices),
            }
        }
    }

    #[cfg(test)]
    mod tests {
        use super::*;
        use crate::{apply::PermApply, from_indices::PermFromIndices};
        use rand::prelude::*;

        #[test]
        fn dynamic_identity() {
            const SIZE: usize = 2014;

            let perm = PermD::identity(SIZE);

            let mut rng = rand::thread_rng();
            let mut orig = [0usize; SIZE];
            rng.fill(orig.as_mut());

            let mut new = orig.clone();
            perm.apply(&mut new).unwrap();

            assert_eq!(orig, new);
        }

        #[test]
        fn dynamic_swap() {
            let perm = PermD::swap(6, 5, 3).unwrap();
            assert_eq!(perm.indices(), &[0, 1, 2, 5, 4, 3]);
        }

        #[test]
        fn dynamic_permute_indices() {
            let index_map = PermD::from_indices([3, 5, 0, 1, 2, 4]).unwrap();
            let orig = PermD::swap(6, 5, 3).unwrap();
            let new = orig.permute_indices(&index_map).unwrap();
            assert_eq!(new, PermD::swap(6, 0, 1).unwrap());
        }
    }
}