Remove nearby and add units to util file

This commit is contained in:
eriedaberrie 2024-12-19 22:01:51 -08:00
parent 2af5aa7121
commit ed3742c0c9

View file

@ -1,6 +1,7 @@
#pragma once
#include <vector>
#include <array>
#include <functional>
typedef long long ll;
typedef unsigned long long ull;
@ -15,6 +16,15 @@ template <typename T> struct Coord {
this->y = y;
}
static inline const std::array<Coord<T>, 4> units() {
return {
Coord<T>(0, 1),
Coord<T>(1, 0),
Coord<T>(0, -1),
Coord<T>(-1, 0),
};
}
Coord<T> operator-() { return {-this->x, -this->y}; }
Coord<T> &operator+=(const Coord<T> &other) {
@ -40,27 +50,6 @@ template <typename T> struct Coord {
y /= other;
return *this;
}
std::vector<Coord<T>> nearby(const Coord<T> &cMin, const Coord<T> &cMax) {
std::vector<Coord<T>> ret;
const T xMin = std::max(this->x - 1, cMin.x);
const T xMax = std::min(this->x + 1, cMax.x);
const T yMin = std::max(this->y - 1, cMin.y);
const T yMax = std::min(this->y + 1, cMax.y);
for (T x = xMin; x <= xMax; x++) {
for (T y = yMin; y <= yMax; y++) {
Coord<T> xy(x, y);
if (*this != xy) {
ret.push_back(xy);
}
}
}
return ret;
}
std::vector<Coord<T>> nearby(const Coord<T> &cMax) {
return this->nearby(Coord<T>(), cMax);
}
};
template <typename T>