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
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ProxyMode {
Keep,
Discard,
Collapse,
}
impl ProxyMode {
pub fn symbol(&self) -> &str {
match self {
Self::Keep => "",
Self::Discard => ">X",
Self::Collapse => ">1",
}
}
}
pub fn apply_proxy_to_index(proxy: &[(usize, ProxyMode)], index: &[usize]) -> Vec<usize> {
let mut current_dimension = 0;
let mut result = Vec::new();
for proxy_dimension in proxy {
match proxy_dimension.1 {
ProxyMode::Keep => result.push(index[current_dimension]),
ProxyMode::Discard => (),
ProxyMode::Collapse => result.push(0),
}
current_dimension += 1;
}
result
}
pub struct NDIndexIter {
position: usize,
reverse_dimensions: Vec<usize>,
}
impl Iterator for NDIndexIter {
type Item = Vec<usize>;
fn next(&mut self) -> Option<Self::Item> {
let mut reverse_coord = Vec::new();
let mut current_part = self.position;
for dim in &self.reverse_dimensions {
reverse_coord.push(current_part % dim);
current_part /= dim;
}
if current_part == 0 {
self.position += 1;
reverse_coord.reverse();
Some(reverse_coord)
} else {
None
}
}
}
impl NDIndexIter {
pub fn new(mut dimensions: Vec<usize>) -> NDIndexIter {
dimensions.reverse();
NDIndexIter {
position: 0,
reverse_dimensions: dimensions,
}
}
}