Amit's 1d->2d matrix that I can base a rewrite of mine on.

main
Zed A. Shaw 1 week ago
parent 80ef052e15
commit 079734941c
  1. 5
      scratchpad/amitmatrix.hpp
  2. 52
      tests/matrix2.cpp

@ -26,17 +26,18 @@ namespace amt {
base_type data;
size_type size;
constexpr auto operator[](size_type k) const noexcept {
constexpr reference operator[](size_type k) noexcept requires (!IsConst) {
assert(k < size && "Out of bound access");
return data[k];
}
constexpr auto operator[](size_type k) noexcept requires (!IsConst) {
constexpr const_reference operator[](size_type k) const noexcept {
assert(k < size && "Out of bound access");
return data[k];
}
};
constexpr Matrix() noexcept = default;
constexpr Matrix(Matrix const&) = default;
constexpr Matrix& operator=(Matrix const&) = default;

@ -0,0 +1,52 @@
#include <catch2/catch_test_macros.hpp>
#include <fmt/core.h>
#include <string>
#include "rand.hpp"
#include "scratchpad/amitmatrix.hpp"
#include "worldbuilder.hpp"
#include <iostream>
#include <numeric>
#include <algorithm>
using namespace fmt;
using std::string;
inline void random_matrix(amt::Matrix<int> &out) {
for(size_t y = 0; y < out.rows(); y++) {
for(size_t x = 0; x < out.cols(); x++) {
out[y][x] = Random::uniform<int>(-10,10);
}
}
}
TEST_CASE("basic matrix iterator", "[matrix2:basic]") {
size_t width = Random::uniform<size_t>(5, 11);
size_t height = Random::uniform<size_t>(3, 13);
amt::Matrix<int> test{height,width,0};
random_matrix(test);
println("FIRST RANDOM");
std::cout << test << std::endl;
for(auto cell : test) {
print("{} ", cell);
}
println("DIRECT ACCESS");
for(size_t y = 0; y < test.rows(); y++) {
for(size_t x = 0; x < test.cols(); x++) {
print("{} ", test(x, y));
}
}
auto filled = test;
std::fill(filled.begin(), filled.end(), 8);
println("SECOND FILLED");
std::cout << filled << std::endl;
auto iota_style = filled;
std::iota(iota_style.begin(), iota_style.end(), 0);
println("THIRD IOTA");
std::cout << iota_style << std::endl;
}
Loading…
Cancel
Save