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
//! A variety of [`Index`]es and [`View`]s based on `usize`.
//!
//! `usize` itself implements `Index`.
//!
//! [`View`]: super::View

use super::{div_mod, NonTuple, Index};

impl Index for usize {
    type Size = usize;

    fn length(size: Self::Size) -> usize { size }

    fn to_usize(self, size: Self::Size) -> usize {
        assert!(self < size, "Index {:?} is out of bounds for size {:?}", self, size);
        self
    }

    fn from_usize(size: Self::Size, index: usize) -> (usize, Self) { div_mod(index, size) }

    fn each(size: Self::Size, mut f: impl FnMut(Self)) {
        for i in 0..size { f(i); }
    }
}

// ----------------------------------------------------------------------------

/// An `usize`-like [`Index`] with a compile-time constant size.
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Fixed<const SIZE: usize>(pub usize);

impl<const SIZE: usize> NonTuple for Fixed<SIZE> {}

impl<const SIZE: usize> Index for Fixed<SIZE> {
    type Size = ();

    fn length(_: ()) -> usize { SIZE }

    fn to_usize(self, _: ()) -> usize { self.0 }

    fn from_usize(_: Self::Size, index: usize) -> (usize, Self) {
        let (index, i) = div_mod(index, SIZE);
        (index, Fixed(i))
    }

    fn each(_: (), mut f: impl FnMut(Self)) {
        for i in 0..SIZE { f(Fixed(i)); }
    }
}

// ----------------------------------------------------------------------------

fn reverse(size: usize, index: usize) -> usize {
    (size - 1) - index
}

/// A `usize`-like [`Index`] that counts backwards.
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Reversed(pub usize);

impl NonTuple for Reversed {}

impl Index for Reversed {
    type Size = usize;

    fn length(size: Self::Size) -> usize { size }

    fn to_usize(self, size: Self::Size) -> usize { reverse(size, self.0) }

    fn from_usize(size: Self::Size, index: usize) -> (usize, Self) {
        let (q, r) = div_mod(index, size);
        (q, Reversed(reverse(size, r)))
    }

    fn each(size: Self::Size, mut f: impl FnMut(Self)) {
        for i in 0..size { f(Reversed(reverse(size, i))); }
    }
}