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
/// Creates a linear grid space over range with a fixed number of width and height steps
///
/// ```
/// use iter_num_tools::grid_space;
/// use itertools::Itertools;
///
/// let it = grid_space((0.0, 0.0)..(1.0, 2.0), (2, 4));
/// itertools::assert_equal(it, vec![
///     (0.0, 0.0), (0.0, 0.5), (0.0, 1.0), (0.0, 1.5),
///     (0.5, 0.0), (0.5, 0.5), (0.5, 1.0), (0.5, 1.5),
/// ]);
///
/// // inclusive and with a single step count
/// let it = grid_space((0.0, 0.0)..=(1.0, 2.0), 3);
/// itertools::assert_equal(it, vec![
///     (0.0, 0.0), (0.0, 1.0), (0.0, 2.0),
///     (0.5, 0.0), (0.5, 1.0), (0.5, 2.0),
///     (1.0, 0.0), (1.0, 1.0), (1.0, 2.0),
/// ]);
///
/// // even 3d spaces
/// let it = grid_space((0, 0, 0)..=(1, 1, 1), 2);
/// itertools::assert_equal(it, vec![
///     (0, 0, 0), (0, 0, 1),
///     (0, 1, 0), (0, 1, 1),
///
///     (1, 0, 0), (1, 0, 1),
///     (1, 1, 0), (1, 1, 1),
/// ]);
/// ```
pub fn grid_space<R, S>(range: R, size: S) -> <R as IntoGridSpace<S>>::GridSpace
where
    R: IntoGridSpace<S>,
{
    range.into_grid_space(size)
}

/// Used by [grid_space]
pub trait IntoGridSpace<S> {
    type GridSpace;
    fn into_grid_space(self, size: S) -> Self::GridSpace;
}

use crate::{LinSpace, Linear, grid::{Grid2, Grid3, Grid4, Transpose, grid}, lin_space};
use std::ops::{Range, RangeInclusive};

macro_rules! impl_grid_space {
    ($Grid:ident: $($t:ident;$u:ty: $r:ident;$s:ident),*) => {

impl<$($t),*> IntoGridSpace<($($u),*)> for Range<($($t),*)> where $($t: Linear),* {
    type GridSpace = $Grid<$(LinSpace<$t>),*>;

    fn into_grid_space(self, ($($s),*): ($($u),*)) -> Self::GridSpace {
        let ($($r),*) = self.transpose();
        grid(($(
            lin_space($r, $s),
        )*))
    }
}

impl<$($t),*> IntoGridSpace<usize> for Range<($($t),*)> where $($t: Linear),* {
    type GridSpace = $Grid<$(LinSpace<$t>),*>;

    fn into_grid_space(self, s: usize) -> Self::GridSpace {
        $(
            let $s = s;
        )*
        self.into_grid_space(($($s),*))
    }
}
impl<$($t),*> IntoGridSpace<($($u),*)> for RangeInclusive<($($t),*)> where $($t: Linear),* {
    type GridSpace = $Grid<$(LinSpace<$t>),*>;

    fn into_grid_space(self, ($($s),*): ($($u),*)) -> Self::GridSpace {
        let ($($r),*) = self.transpose();
        grid(($(
            lin_space($r, $s),
        )*))
    }
}

impl<$($t),*> IntoGridSpace<usize> for RangeInclusive<($($t),*)> where $($t: Linear),* {
    type GridSpace = $Grid<$(LinSpace<$t>),*>;

    fn into_grid_space(self, s: usize) -> Self::GridSpace {
        $(
            let $s = s;
        )*
        self.into_grid_space(($($s),*))
    }
}

    };
}

impl_grid_space!(Grid2: T0;usize: r0;s0, T1;usize: r1;s1); // 2d grid
impl_grid_space!(Grid3: T0;usize: r0;s0, T1;usize: r1;s1, T2;usize: r2;s2); // 3d grid
impl_grid_space!(Grid4: T0;usize: r0;s0, T1;usize: r1;s1, T2;usize: r2;s2, T3;usize: r3;s3); // 4d grid