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
99
100
#[derive(PartialEq, Eq, Debug, Hash, Clone, Copy)]
pub enum Entity {
Global,
Column(usize),
Row(usize),
Cell(usize, usize),
}
impl Entity {
pub fn iter(&self, count_rows: usize, count_cols: usize) -> EntityIterator {
EntityIterator {
entity: *self,
count_rows,
count_cols,
i: 0,
j: 0,
}
}
}
pub type Position = (usize, usize);
impl From<Position> for Entity {
fn from((row, col): Position) -> Self {
Self::Cell(row, col)
}
}
#[derive(Debug)]
pub struct EntityIterator {
entity: Entity,
count_rows: usize,
count_cols: usize,
i: usize,
j: usize,
}
impl Iterator for EntityIterator {
type Item = Position;
fn next(&mut self) -> Option<Self::Item> {
if self.count_rows == 0 || self.count_cols == 0 {
return None;
}
match self.entity {
Entity::Cell(row, col) => {
self.count_cols = 0;
self.count_rows = 0;
Some((row, col))
}
Entity::Column(col) => {
if self.i >= self.count_rows {
return None;
}
let i = self.i;
self.i += 1;
Some((i, col))
}
Entity::Row(row) => {
if self.j >= self.count_cols {
return None;
}
let j = self.j;
self.j += 1;
Some((row, j))
}
Entity::Global => {
if self.j >= self.count_cols {
self.j = 0;
self.i += 1;
if self.i >= self.count_rows {
return None;
}
}
let j = self.j;
self.j += 1;
Some((self.i, j))
}
}
}
}