1
+ use crate :: coord;
2
+ use crate :: coordinate:: Coordinate ;
3
+ use crate :: shape:: Shape ;
4
+
1
5
pub struct IndexIterator {
2
- shape : Vec < usize > ,
3
- current : Vec < usize > ,
6
+ shape : Shape ,
7
+ current : Coordinate ,
4
8
done : bool ,
5
9
}
6
10
7
11
impl IndexIterator {
8
- pub fn new ( shape : & [ usize ] ) -> Self {
9
- let current = vec ! [ 0 ; shape. len ( ) ] ;
12
+ pub fn new ( shape : & Shape ) -> Self {
13
+ let current = coord ! [ 0 ; shape. order ( ) ] ;
10
14
IndexIterator {
11
- shape : shape. to_vec ( ) ,
15
+ shape : shape. clone ( ) ,
12
16
current,
13
17
done : false ,
14
18
}
15
19
}
16
20
}
17
21
18
22
impl Iterator for IndexIterator {
19
- type Item = Vec < usize > ;
23
+ type Item = Coordinate ;
20
24
21
25
fn next ( & mut self ) -> Option < Self :: Item > {
22
- if self . done || self . shape . len ( ) == 0 {
26
+ if self . done || self . shape . order ( ) == 0 {
23
27
return None ;
24
28
}
25
29
26
30
let result = self . current . clone ( ) ;
27
31
28
- for i in ( 0 ..self . shape . len ( ) ) . rev ( ) {
32
+ for i in ( 0 ..self . shape . order ( ) ) . rev ( ) {
29
33
if self . current [ i] + 1 < self . shape [ i] {
30
34
self . current [ i] += 1 ;
31
35
break ;
@@ -45,36 +49,37 @@ impl Iterator for IndexIterator {
45
49
#[ cfg( test) ]
46
50
mod tests {
47
51
use super :: * ;
52
+ use crate :: shape;
48
53
49
54
#[ test]
50
55
fn test_index_iterator ( ) {
51
- let shape = vec ! [ 2 , 3 ] ;
56
+ let shape = shape ! [ 2 , 3 ] . unwrap ( ) ;
52
57
let mut iter = IndexIterator :: new ( & shape) ;
53
58
54
- assert_eq ! ( iter. next( ) , Some ( vec ![ 0 , 0 ] ) ) ;
55
- assert_eq ! ( iter. next( ) , Some ( vec ![ 0 , 1 ] ) ) ;
56
- assert_eq ! ( iter. next( ) , Some ( vec ![ 0 , 2 ] ) ) ;
57
- assert_eq ! ( iter. next( ) , Some ( vec ![ 1 , 0 ] ) ) ;
58
- assert_eq ! ( iter. next( ) , Some ( vec ![ 1 , 1 ] ) ) ;
59
- assert_eq ! ( iter. next( ) , Some ( vec ![ 1 , 2 ] ) ) ;
59
+ assert_eq ! ( iter. next( ) , Some ( coord ![ 0 , 0 ] ) ) ;
60
+ assert_eq ! ( iter. next( ) , Some ( coord ![ 0 , 1 ] ) ) ;
61
+ assert_eq ! ( iter. next( ) , Some ( coord ![ 0 , 2 ] ) ) ;
62
+ assert_eq ! ( iter. next( ) , Some ( coord ![ 1 , 0 ] ) ) ;
63
+ assert_eq ! ( iter. next( ) , Some ( coord ![ 1 , 1 ] ) ) ;
64
+ assert_eq ! ( iter. next( ) , Some ( coord ![ 1 , 2 ] ) ) ;
60
65
assert_eq ! ( iter. next( ) , None ) ;
61
66
}
62
67
63
68
#[ test]
64
69
fn test_index_iterator_single_dimension ( ) {
65
- let shape = vec ! [ 4 ] ;
70
+ let shape = shape ! [ 4 ] . unwrap ( ) ;
66
71
let mut iter = IndexIterator :: new ( & shape) ;
67
72
68
- assert_eq ! ( iter. next( ) , Some ( vec ![ 0 ] ) ) ;
69
- assert_eq ! ( iter. next( ) , Some ( vec ![ 1 ] ) ) ;
70
- assert_eq ! ( iter. next( ) , Some ( vec ![ 2 ] ) ) ;
71
- assert_eq ! ( iter. next( ) , Some ( vec ![ 3 ] ) ) ;
73
+ assert_eq ! ( iter. next( ) , Some ( coord ![ 0 ] ) ) ;
74
+ assert_eq ! ( iter. next( ) , Some ( coord ![ 1 ] ) ) ;
75
+ assert_eq ! ( iter. next( ) , Some ( coord ![ 2 ] ) ) ;
76
+ assert_eq ! ( iter. next( ) , Some ( coord ![ 3 ] ) ) ;
72
77
assert_eq ! ( iter. next( ) , None ) ;
73
78
}
74
79
75
80
#[ test]
76
81
fn test_index_iterator_empty_tensor ( ) {
77
- let shape = vec ! [ ] ;
82
+ let shape = shape ! [ ] . unwrap ( ) ;
78
83
let mut iter = IndexIterator :: new ( & shape) ;
79
84
80
85
assert_eq ! ( iter. next( ) , None ) ;
0 commit comments