|
|
|
@ -10,117 +10,7 @@ |
|
|
|
|
using namespace nlohmann; |
|
|
|
|
using namespace fmt; |
|
|
|
|
using std::string; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <algorithm> |
|
|
|
|
#include <iostream> |
|
|
|
|
|
|
|
|
|
struct ItStep { |
|
|
|
|
int cell; |
|
|
|
|
size_t x; |
|
|
|
|
size_t y; |
|
|
|
|
bool row; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
class MatrixIterator1 { |
|
|
|
|
public: |
|
|
|
|
class iterator |
|
|
|
|
{ |
|
|
|
|
public: |
|
|
|
|
Matrix &mat; |
|
|
|
|
size_t x = 0; |
|
|
|
|
size_t y = 0; |
|
|
|
|
|
|
|
|
|
using iterator_category = std::input_iterator_tag; |
|
|
|
|
using value_type = ItStep; |
|
|
|
|
using difference_type = ItStep; |
|
|
|
|
using pointer = ItStep *; |
|
|
|
|
using reference = ItStep; |
|
|
|
|
|
|
|
|
|
explicit iterator(Matrix &_mat) |
|
|
|
|
: mat(_mat) {} |
|
|
|
|
|
|
|
|
|
iterator& operator++() { |
|
|
|
|
x++; |
|
|
|
|
if(x < mat[0].size()) { |
|
|
|
|
return *this; |
|
|
|
|
} else { |
|
|
|
|
x = 0; |
|
|
|
|
y++; |
|
|
|
|
return *this; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
iterator operator++(int) { |
|
|
|
|
iterator retval = *this; |
|
|
|
|
++(*this); |
|
|
|
|
return retval; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool operator==(iterator other) const { |
|
|
|
|
return x == other.x && y == other.y; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
reference operator*() const { |
|
|
|
|
return { |
|
|
|
|
.cell = mat[y][x], |
|
|
|
|
.x = x, |
|
|
|
|
.y = y, |
|
|
|
|
.row = x >= mat[0].size() - 1 |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Matrix &mat; |
|
|
|
|
|
|
|
|
|
MatrixIterator1(Matrix &mat_) : |
|
|
|
|
mat(mat_) |
|
|
|
|
{ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
iterator begin() { |
|
|
|
|
return iterator(mat); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
iterator end() { |
|
|
|
|
iterator it(mat); |
|
|
|
|
it.y = mat.size(); |
|
|
|
|
it.x = 0; |
|
|
|
|
return it; |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
struct MatrixIterator2 { |
|
|
|
|
Matrix &$mat; |
|
|
|
|
size_t x = ~0; |
|
|
|
|
size_t y = ~0; |
|
|
|
|
size_t width = 0; |
|
|
|
|
size_t height = 0; |
|
|
|
|
int cell = 0; |
|
|
|
|
bool row = false; |
|
|
|
|
|
|
|
|
|
MatrixIterator2(Matrix &mat) : |
|
|
|
|
$mat(mat) |
|
|
|
|
{ |
|
|
|
|
height = $mat.size(); |
|
|
|
|
width = $mat[0].size(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool next() { |
|
|
|
|
x++; |
|
|
|
|
x *= (x < width); |
|
|
|
|
y = y + (x == 0); |
|
|
|
|
row = x == width - 1; |
|
|
|
|
|
|
|
|
|
if(y < height) { |
|
|
|
|
cell = $mat[y][x]; |
|
|
|
|
return true; |
|
|
|
|
} else { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
using matrix::Matrix; |
|
|
|
|
|
|
|
|
|
TEST_CASE("basic matrix iterator", "[matrix]") { |
|
|
|
|
std::ifstream infile("./tests/dijkstra.json"); |
|
|
|
@ -129,89 +19,30 @@ TEST_CASE("basic matrix iterator", "[matrix]") { |
|
|
|
|
|
|
|
|
|
Matrix walls = test["walls"]; |
|
|
|
|
|
|
|
|
|
matrix_dump("ITERATOR DUMP", walls); |
|
|
|
|
matrix::dump("ITERATOR DUMP", walls); |
|
|
|
|
|
|
|
|
|
println("VS Matrix1 ------"); |
|
|
|
|
println("VS matrix::each_row ------"); |
|
|
|
|
|
|
|
|
|
for(auto [cell, x, y, row] : MatrixIterator1(walls)) { |
|
|
|
|
REQUIRE(walls[y][x] == cell); |
|
|
|
|
print("{} ", cell); |
|
|
|
|
if(row) print("\n"); |
|
|
|
|
for(matrix::each_row it{walls}; it.next();) { |
|
|
|
|
REQUIRE(walls[it.y][it.x] == it.cell); |
|
|
|
|
print("{} ", it.cell); |
|
|
|
|
if(it.row) print("\n"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
println("VS Matrix2------"); |
|
|
|
|
// tests going through straight cells but also
|
|
|
|
|
// using two iterators on one matrix (or two)
|
|
|
|
|
matrix::each_cell cells{walls}; |
|
|
|
|
cells.next(); // kick it off
|
|
|
|
|
|
|
|
|
|
for(MatrixIterator2 mit{walls}; mit.next();) { |
|
|
|
|
REQUIRE(walls[mit.y][mit.x] == mit.cell); |
|
|
|
|
print("{} ", mit.cell); |
|
|
|
|
if(mit.row) print("\n"); |
|
|
|
|
for(matrix::each_row it{walls}; |
|
|
|
|
it.next(); cells.next()) |
|
|
|
|
{ |
|
|
|
|
REQUIRE(walls[cells.y][cells.x] == it.cell); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
println("END TEST============="); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<long FROM, long TO> |
|
|
|
|
class Range { |
|
|
|
|
public: |
|
|
|
|
class iterator |
|
|
|
|
{ |
|
|
|
|
long num = FROM; |
|
|
|
|
|
|
|
|
|
public: |
|
|
|
|
using iterator_category = std::input_iterator_tag; |
|
|
|
|
using value_type = long; |
|
|
|
|
using difference_type = long; |
|
|
|
|
using pointer = const long *; |
|
|
|
|
using reference = long; |
|
|
|
|
|
|
|
|
|
explicit iterator(long _num = 0) : num(_num) {} |
|
|
|
|
|
|
|
|
|
iterator& operator++() { |
|
|
|
|
num = TO >= FROM ? num + 1 : num - 1; |
|
|
|
|
return *this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
iterator operator++(int) { |
|
|
|
|
iterator retval = *this; |
|
|
|
|
++(*this); |
|
|
|
|
return retval; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool operator==(iterator other) const { |
|
|
|
|
return num == other.num; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool operator!=(iterator other) const { |
|
|
|
|
return !(*this == other); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
reference operator*() const { |
|
|
|
|
return num; |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
iterator begin() { |
|
|
|
|
return iterator(FROM); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
iterator end() { |
|
|
|
|
return iterator(TO >= FROM ? TO + 1 : TO - 1); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
TEST_CASE("basic matrix example", "[matrix]") { |
|
|
|
|
auto range = Range<15, 25>(); |
|
|
|
|
auto itr = std::find(range.begin(), range.end(), 18); |
|
|
|
|
std::cout << *itr << '\n'; |
|
|
|
|
|
|
|
|
|
for(long l : Range<3, 5>()) { |
|
|
|
|
std::cout << l << ' '; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
std::cout << '\n'; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
TEST_CASE("matrix_assign works", "[matrix]") { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|