-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterator.cpp
148 lines (126 loc) · 3.24 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <iostream>
#include "matrix.hpp"
// ---------- ROW ITERATOR ----------
Matrix::RowIterator::RowIterator(size_t row, size_t col, Matrix &m) : mat(m)
{
num_row = row;
cur_column = col;
};
Matrix::RowIterator &Matrix::RowIterator::operator++()
{
cur_column++;
if (cur_column >= mat.num_columns())
{
cur_column = mat.num_columns();
return *this;
}
while ((cur_column < mat.num_columns()) &&
(mat.get(num_row, cur_column) < eps) && (mat.get(num_row, cur_column) > -eps))
cur_column++;
if (cur_column == mat.num_columns())
{
return *this;
}
return *this;
};
Matrix::RowIterator Matrix::RowIterator::operator++(int)
{
RowIterator prev = *this;
++(*this);
return prev;
};
bool Matrix::RowIterator::operator==(const Matrix::RowIterator &other) const
{
return ((mat == other.mat) && (num_row == other.num_row) && (cur_column == other.cur_column));
};
bool Matrix::RowIterator::operator!=(const Matrix::RowIterator &other) const
{
return !(*this == other);
};
const double &Matrix::RowIterator::operator*()
{
return mat.at(num_row, cur_column);
};
double Matrix::RowIterator::operator*() const
{
return mat.get(num_row, cur_column);
};
Matrix::RowIterator Matrix::iterate_rows(size_t row)
{
size_t col = 0;
while ((col < num_columns()) &&
(get(row, col) < eps) && (get(row, col) > -eps))
{
col++;
}
return RowIterator(row, col, *this);
};
Matrix::RowIterator Matrix::end_rows(size_t row)
{
return RowIterator(row, num_columns(), *this);
};
size_t Matrix::RowIterator::column() const
{
return cur_column;
};
// ---------- COLUMN ITERATOR ----------
Matrix::ColumnIterator::ColumnIterator(size_t row, size_t col, Matrix &m) : mat(m)
{
num_column = col;
cur_row = row;
};
Matrix::ColumnIterator &Matrix::ColumnIterator::operator++()
{
cur_row++;
if (cur_row >= mat.num_rows())
{
cur_row = mat.num_rows();
return *this;
}
while ((cur_row < mat.num_rows()) &&
(mat.get(cur_row, num_column) < eps) && (mat.get(cur_row, num_column) > -eps))
cur_row++;
if (cur_row == mat.num_rows())
{
return *this;
}
return *this;
};
Matrix::ColumnIterator Matrix::ColumnIterator::operator++(int)
{
ColumnIterator prev = *this;
++(*this);
return prev;
};
bool Matrix::ColumnIterator::operator==(const Matrix::ColumnIterator &other) const
{
return ((mat == other.mat) && (cur_row == other.cur_row) && (num_column == other.num_column));
};
bool Matrix::ColumnIterator::operator!=(const Matrix::ColumnIterator &other) const
{
return !(*this == other);
};
const double &Matrix::ColumnIterator::operator*()
{
return mat.at(cur_row, num_column);
};
double Matrix::ColumnIterator::operator*() const
{
return mat.get(cur_row, num_column);
};
Matrix::ColumnIterator Matrix::iterate_columns(size_t col)
{
size_t row = 0;
while ((row < num_rows()) &&
(get(row, col) < eps) && (get(row, col) > -eps))
row++;
return ColumnIterator(row, col, *this);
};
Matrix::ColumnIterator Matrix::end_columns(size_t col)
{
return ColumnIterator(num_rows(), col, *this);
};
size_t Matrix::ColumnIterator::row() const
{
return cur_row;
};