#include #include #include #include "config.hpp" #include "matrix.hpp" #include "components.hpp" #include #include using namespace nlohmann; using namespace fmt; using std::string; #include #include 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; } } }; TEST_CASE("basic matrix iterator", "[matrix]") { std::ifstream infile("./tests/dijkstra.json"); json data = json::parse(infile); auto test = data[0]; Matrix walls = test["walls"]; matrix_dump("ITERATOR DUMP", walls); println("VS Matrix1 ------"); for(auto [cell, x, y, row] : MatrixIterator1(walls)) { REQUIRE(walls[y][x] == cell); print("{} ", cell); if(row) print("\n"); } println("VS Matrix2------"); for(MatrixIterator2 mit{walls}; mit.next();) { REQUIRE(walls[mit.y][mit.x] == mit.cell); print("{} ", mit.cell); if(mit.row) print("\n"); } println("END TEST============="); } template 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]") { } TEST_CASE("matrix_dump works", "[matrix]") { }