Iterators are now working far more reliably and have more extensive tests that randomize inputs and fuzz them to check they keep working.
This commit is contained in:
parent
8e470df554
commit
70cd963e5c
11 changed files with 318 additions and 84 deletions
79
matrix.cpp
79
matrix.cpp
|
@ -3,7 +3,24 @@
|
|||
#include <fmt/core.h>
|
||||
|
||||
using namespace fmt;
|
||||
using matrix::Matrix;
|
||||
using std::min, std::max;
|
||||
|
||||
inline size_t next_x(size_t x, size_t width) {
|
||||
return (x + 1) * ((x + 1) < width);
|
||||
}
|
||||
|
||||
inline size_t next_y(size_t x, size_t y) {
|
||||
return y + (x == 0);
|
||||
}
|
||||
|
||||
inline bool at_end(size_t y, size_t height) {
|
||||
return y < height;
|
||||
}
|
||||
|
||||
inline bool end_row(size_t x, size_t width) {
|
||||
return x == width - 1;
|
||||
}
|
||||
|
||||
|
||||
namespace matrix {
|
||||
|
||||
|
@ -14,10 +31,9 @@ namespace matrix {
|
|||
}
|
||||
|
||||
bool each_cell::next() {
|
||||
x++;
|
||||
x *= (x < width);
|
||||
y = y + (x == 0);
|
||||
return y < height;
|
||||
x = next_x(x, width);
|
||||
y = next_y(x, y);
|
||||
return at_end(y, height);
|
||||
}
|
||||
|
||||
each_row::each_row(Matrix &mat) :
|
||||
|
@ -28,26 +44,59 @@ namespace matrix {
|
|||
}
|
||||
|
||||
bool each_row::next() {
|
||||
x++;
|
||||
x *= (x < width);
|
||||
y = y + (x == 0);
|
||||
row = x == width - 1;
|
||||
cell = y < height ? $mat[y][x] : -1;
|
||||
return y < height;
|
||||
x = next_x(x, width);
|
||||
y = next_y(x, y);
|
||||
row = end_row(x, width);
|
||||
return at_end(y, height);
|
||||
}
|
||||
|
||||
in_box::in_box(Matrix &mat, size_t from_x, size_t from_y, size_t size) {
|
||||
size_t h = mat.size();
|
||||
size_t w = mat[0].size();
|
||||
|
||||
// keeps it from going below zero
|
||||
// need extra -1 to compensate for the first next()
|
||||
left = max(from_x, size) - size;
|
||||
x = left - 1; // must be -1 for next()
|
||||
// keeps it from going above width
|
||||
right = min(from_x + size + 1, w);
|
||||
|
||||
// same for these two
|
||||
top = max(from_y, size) - size;
|
||||
y = top - (left == 0);
|
||||
bottom = min(from_y + size + 1, h);
|
||||
}
|
||||
|
||||
bool in_box::next() {
|
||||
// calc next but allow to go to 0 for next
|
||||
x = next_x(x, right);
|
||||
// x will go to 0, which signals new line
|
||||
y = next_y(x, y); // this must go here
|
||||
// if x==0 then this moves it to min_x
|
||||
x = max(x, left);
|
||||
// and done
|
||||
return at_end(y, bottom);
|
||||
}
|
||||
|
||||
void in_box::dump() {
|
||||
println("BOX: x={},y={}; left={},right={}; top={},bottom={}",
|
||||
x, y, left, right, top, bottom);
|
||||
}
|
||||
|
||||
void dump(const std::string &msg, Matrix &map, int show_x, int show_y) {
|
||||
println("----------------- {}", msg);
|
||||
|
||||
for(each_row it{map}; it.next();) {
|
||||
int cell = map[it.y][it.x];
|
||||
|
||||
if(int(it.x) == show_x && int(it.y) == show_y) {
|
||||
print("{:x}<", it.cell);
|
||||
} else if(it.cell == WALL_PATH_LIMIT) {
|
||||
print("{:x}<", cell);
|
||||
} else if(cell == WALL_PATH_LIMIT) {
|
||||
print("# ");
|
||||
} else if(it.cell > 15) {
|
||||
} else if(cell > 15) {
|
||||
print("* ");
|
||||
} else {
|
||||
print("{:x} ", it.cell);
|
||||
print("{:x} ", cell);
|
||||
}
|
||||
|
||||
if(it.row) print("\n");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue