A first exploration of doing a matrix iterator.

main
Zed A. Shaw 1 week ago
parent ee1e2e5bc5
commit da0b941dfd
  1. 1
      meson.build
  2. 5
      status.txt
  3. 2
      systems.cpp
  4. 221
      tests/matrix.cpp

@ -37,6 +37,7 @@ runtests = executable('runtests', [
'systems.cpp', 'systems.cpp',
'gui.cpp', 'gui.cpp',
'worldbuilder.cpp', 'worldbuilder.cpp',
'tests/matrix.cpp',
'tests/fsm.cpp', 'tests/fsm.cpp',
'tests/dbc.cpp', 'tests/dbc.cpp',
'tests/map.cpp', 'tests/map.cpp',

@ -1,5 +1,10 @@
TODAY'S GOAL: TODAY'S GOAL:
* Change the test matrix to be irregular dimensions.
* Study https://github.com/hirdrac/gx_lib/blob/main/gx/Unicode.hh
* Study this https://en.cppreference.com/w/cpp/language/explicit
* Study https://en.cppreference.com/w/cpp/language/member_functions#Special_member_functions
* Light should flood using the dijkstra map rather than use a box. * Light should flood using the dijkstra map rather than use a box.
-1. Learn std::initializer_list by using it. -1. Learn std::initializer_list by using it.

@ -10,7 +10,7 @@
#include "dbc.hpp" #include "dbc.hpp"
#include "lights.hpp" #include "lights.hpp"
const bool DEBUG_MAP=true; const bool DEBUG_MAP=false;
using std::string; using std::string;
using namespace fmt; using namespace fmt;

@ -0,0 +1,221 @@
#include <catch2/catch_test_macros.hpp>
#include <fmt/core.h>
#include <string>
#include "config.hpp"
#include "matrix.hpp"
#include "components.hpp"
#include <nlohmann/json.hpp>
#include <fstream>
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;
}
}
};
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<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]") {
}
TEST_CASE("matrix_dump works", "[matrix]") {
}
Loading…
Cancel
Save