aocpp/lib/include/aocpp/Vec.hpp
Eric Mertens a80e2e63b1 2022-18
2023-01-19 17:13:42 -08:00

144 lines
3.1 KiB
C++

#ifndef AOCPP_VEC_HPP_
#define AOCPP_VEC_HPP_
#include <array>
#include <cstdint>
#include <iostream>
#include <tuple>
#include <assert.h>
namespace aocpp {
template <typename T, std::size_t N>
class Vec {
T arr[N];
auto range_check(std::size_t i) -> void {
if (i >= N) {
throw std::out_of_range{std::to_string(i) + " >= " + std::to_string(N)};
}
}
public:
Vec() = default;
auto operator<=>(const Vec&) const = default;
explicit Vec(T x) {
std::fill_n(arr, N, x);
}
Vec(std::initializer_list<T> xs) {
assert(N == xs.size());
std::move(xs.begin(), xs.end(), arr);
}
auto operator[](std::size_t i) const -> T {
return arr[i];
}
auto operator[](std::size_t i) -> T & {
return arr[i];
}
auto at(std::size_t i) const -> T {
range_check(i);
return arr[i];
}
auto at(std::size_t i) -> T & {
range_check(i);
return arr[i];
}
auto operator+(Vec const& rhs) const -> Vec
{
Vec result;
for (std::size_t i = 0; i < N; ++i) result[i] = arr[i] + rhs[i];
return result;
}
auto operator-(Vec const& rhs) const -> Vec {
Vec result;
for (std::size_t i = 0; i < N; ++i) result[i] = arr[i] - rhs[i];
return result;
}
auto operator-() const -> Vec {
Vec result;
for (std::size_t i = 0; i < N; ++i) result[i] = -arr[i];
return result;
}
auto operator+=(Vec const& rhs) -> Vec & {
for (std::size_t i = 0; i < N; ++i) arr[i] += rhs[i];
return *this;
}
auto operator-=(Vec const& rhs) -> Vec &
{
for (std::size_t i = 0; i < N; ++i) arr[i] -= rhs[i];
return *this;
}
/// @brief Check inclusion in a cuboid
/// @param lo lower-corner
/// @param hi upper-corner
/// @return this point is contained in the cuboid
auto InRange(Vec const& lo, Vec const& hi) const -> bool
{
for (std::size_t i = 0; i < N; ++i) {
if (arr[i] < lo[i] || hi[i] < arr[i]) return false;
}
return true;
}
/// @brief Find the corners of the smallest cuboid containing all the given points.
/// @tparam It InputIterator
/// @param first beginning of input range
/// @param last end of input range
/// @return pair of lower and upper cuboid corners
template <typename It>
static auto BoundingBox(It first, It last) -> std::pair<Vec, Vec>
{
Vec lo(0), hi(0);
std::for_each(first, last, [&](auto const& x) {
for (std::size_t i = 0; i < N; ++i) {
if (lo[i] > x[i]) lo[i] = x[i];
if (hi[i] < x[i]) hi[i] = x[i];
}
});
return {lo, hi};
}
/// @brief Apply a continuation to each neighbor of a vector
/// @param k continuation applied to cardinal direction neighbors
auto EachNeighbor(std::invocable<Vec const&> auto k) const -> void
{
auto x = *this;
for (std::size_t i = 0; i < N; ++i) {
x[i] += 1;
k(std::as_const(x));
x[i] -= 2;
k(std::as_const(x));
x[i] += 1;
}
}
};
template <typename T, std::size_t N>
auto operator<<(std::ostream & out, Vec<T,N> const& rhs) -> std::ostream &
{
out << "{";
if (0 < N) {
out << rhs[0];
}
for (std::size_t i = 1; i < N; ++i) {
out << "," << rhs[i];
}
return out << "}";
}
}
#endif