-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiter.rs
88 lines (74 loc) · 2.31 KB
/
iter.rs
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
use crate::coord;
use crate::coordinate::Coordinate;
use crate::shape::Shape;
use std::cmp::max;
pub struct IndexIterator {
shape: Shape,
current: Coordinate,
done: bool,
}
impl IndexIterator {
pub fn new(shape: &Shape) -> Self {
// (shape.order() == 0) => `next` returns None before `current` is used
let current = coord![0; max(shape.order(), 1)].unwrap();
IndexIterator {
shape: shape.clone(),
current,
done: false,
}
}
}
impl Iterator for IndexIterator {
type Item = Coordinate;
fn next(&mut self) -> Option<Self::Item> {
if self.done || self.shape.order() == 0 {
return None;
}
let result = self.current.clone();
for i in (0..self.shape.order()).rev() {
if self.current[i] + 1 < self.shape[i] {
self.current[i] += 1;
break;
} else {
self.current[i] = 0;
if i == 0 {
self.done = true;
}
}
}
Some(result)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::shape;
#[test]
fn test_index_iterator() {
let shape = shape![2, 3].unwrap();
let mut iter = IndexIterator::new(&shape);
assert_eq!(iter.next(), Some(coord![0, 0].unwrap()));
assert_eq!(iter.next(), Some(coord![0, 1].unwrap()));
assert_eq!(iter.next(), Some(coord![0, 2].unwrap()));
assert_eq!(iter.next(), Some(coord![1, 0].unwrap()));
assert_eq!(iter.next(), Some(coord![1, 1].unwrap()));
assert_eq!(iter.next(), Some(coord![1, 2].unwrap()));
assert_eq!(iter.next(), None);
}
#[test]
fn test_index_iterator_single_dimension() {
let shape = shape![4].unwrap();
let mut iter = IndexIterator::new(&shape);
assert_eq!(iter.next(), Some(coord![0].unwrap()));
assert_eq!(iter.next(), Some(coord![1].unwrap()));
assert_eq!(iter.next(), Some(coord![2].unwrap()));
assert_eq!(iter.next(), Some(coord![3].unwrap()));
assert_eq!(iter.next(), None);
}
#[test]
fn test_index_iterator_empty_tensor() {
let shape = shape![].unwrap();
let mut iter = IndexIterator::new(&shape);
assert_eq!(iter.next(), None);
}
}