-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterator.cpp
82 lines (66 loc) · 1.85 KB
/
iterator.cpp
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
/*This program uses my own implementation of the backtracking
algorithm described in this wikipedia article:
https://en.wikipedia.org/wiki/Sudoku_solving_algorithms
*/
#include "grid.h"
Grid::Iterator Grid::begin(const int row) {
return Grid::Iterator(row_ptrs[row]);
}
Grid::Iterator Grid::end() {
return Grid::Iterator();
}
Grid::Iterator::Iterator()
: node_ptr(nullptr), direction(true) {}
Grid::Iterator::Iterator(Node * node_in)
: node_ptr(node_in), direction(true) {}
Grid::Iterator& Grid::Iterator::operator++() {
assert(node_ptr);
node_ptr = node_ptr->right;
return *this;
}
Grid::Iterator& Grid::Iterator::operator--() {
assert(node_ptr);
node_ptr = node_ptr->left;
return *this;
}
Grid::Iterator& Grid::Iterator::up() {
assert(node_ptr);
node_ptr = node_ptr->top;
return *this;
}
Grid::Iterator& Grid::Iterator::down() {
assert(node_ptr);
node_ptr = node_ptr->bottom;
return *this;
}
//Check if the space to the right of the iterator is valid
bool Grid::Iterator::r_safe() {
assert(node_ptr);
return node_ptr->right != nullptr;
}
//Check if the space to the left of the iterator is valid
bool Grid::Iterator::l_safe() {
assert(node_ptr);
return node_ptr->left != nullptr;
}
int Grid::Iterator::operator*() {
assert(node_ptr);
return node_ptr->datum;
}
bool Grid::Iterator::operator==(Iterator rhs) {
return node_ptr == rhs.node_ptr;
}
bool Grid::Iterator::operator!=(Iterator rhs) {
return node_ptr != rhs.node_ptr;
}
void Grid::Iterator::fill_value(int new_datum) {
assert(node_ptr);
node_ptr->datum = new_datum;
}
bool Grid::Iterator::get_direction(){
return direction;
}
//Switch the direction of the iterator. Right = true. Left = false.
void Grid::Iterator::change_direction() {
direction = !direction;
}