1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-03-31 22:05:10 +02:00

vcmi: make int3 as constexpr class

This allows us to calculate distances and check const vector validity
at compile time
This commit is contained in:
Konstantin 2023-02-12 04:03:04 +03:00
parent b30d23d278
commit 038db5c71b
2 changed files with 35 additions and 36 deletions

View File

@ -746,6 +746,12 @@ namespace vstd
} }
return std::to_string(number) + *iter; return std::to_string(number) + *iter;
} }
///compile-time version of std::abs for ints for int3, in clang++15 std::abs is constexpr
static constexpr int abs(int i) {
if(i < 0) return -i;
return i;
}
} }
using vstd::operator-=; using vstd::operator-=;

View File

@ -18,48 +18,41 @@ public:
si32 x, y, z; si32 x, y, z;
//c-tor: x, y, z initialized to 0 //c-tor: x, y, z initialized to 0
int3() : x(0), y(0), z(0) {} // I think that x, y, z should be left uninitialized. constexpr int3() : x(0), y(0), z(0) {} // I think that x, y, z should be left uninitialized.
//c-tor: x, y, z initialized to i //c-tor: x, y, z initialized to i
explicit int3(const si32 i) : x(i), y(i), z(i) {} explicit constexpr int3(const si32 i) : x(i), y(i), z(i) {}
//c-tor: x, y, z initialized to X, Y, Z //c-tor: x, y, z initialized to X, Y, Z
int3(const si32 X, const si32 Y, const si32 Z) : x(X), y(Y), z(Z) {} constexpr int3(const si32 X, const si32 Y, const si32 Z) : x(X), y(Y), z(Z) {}
int3(const int3 & c) : x(c.x), y(c.y), z(c.z) {} // Should be set to default (C++11)? constexpr int3(const int3 & c) = default;
int3 & operator=(const int3 & c) // Should be set to default (C++11)? constexpr int3 & operator=(const int3 & c) = default;
{ constexpr int3 operator-() const { return int3(-x, -y, -z); }
x = c.x;
y = c.y;
z = c.z;
return *this; constexpr int3 operator+(const int3 & i) const { return int3(x + i.x, y + i.y, z + i.z); }
} constexpr int3 operator-(const int3 & i) const { return int3(x - i.x, y - i.y, z - i.z); }
int3 operator-() const { return int3(-x, -y, -z); }
int3 operator+(const int3 & i) const { return int3(x + i.x, y + i.y, z + i.z); }
int3 operator-(const int3 & i) const { return int3(x - i.x, y - i.y, z - i.z); }
//returns int3 with coordinates increased by given number //returns int3 with coordinates increased by given number
int3 operator+(const si32 i) const { return int3(x + i, y + i, z + i); } constexpr int3 operator+(const si32 i) const { return int3(x + i, y + i, z + i); }
//returns int3 with coordinates decreased by given number //returns int3 with coordinates decreased by given number
int3 operator-(const si32 i) const { return int3(x - i, y - i, z - i); } constexpr int3 operator-(const si32 i) const { return int3(x - i, y - i, z - i); }
//returns int3 with coordinates multiplied by given number //returns int3 with coordinates multiplied by given number
int3 operator*(const double i) const { return int3((int)(x * i), (int)(y * i), (int)(z * i)); } constexpr int3 operator*(const double i) const { return int3((int)(x * i), (int)(y * i), (int)(z * i)); }
//returns int3 with coordinates divided by given number //returns int3 with coordinates divided by given number
int3 operator/(const double i) const { return int3((int)(x / i), (int)(y / i), (int)(z / i)); } constexpr int3 operator/(const double i) const { return int3((int)(x / i), (int)(y / i), (int)(z / i)); }
//returns int3 with coordinates multiplied by given number //returns int3 with coordinates multiplied by given number
int3 operator*(const si32 i) const { return int3(x * i, y * i, z * i); } constexpr int3 operator*(const si32 i) const { return int3(x * i, y * i, z * i); }
//returns int3 with coordinates divided by given number //returns int3 with coordinates divided by given number
int3 operator/(const si32 i) const { return int3(x / i, y / i, z / i); } constexpr int3 operator/(const si32 i) const { return int3(x / i, y / i, z / i); }
int3 & operator+=(const int3 & i) constexpr int3 & operator+=(const int3 & i)
{ {
x += i.x; x += i.x;
y += i.y; y += i.y;
z += i.z; z += i.z;
return *this; return *this;
} }
int3 & operator-=(const int3 & i) constexpr int3 & operator-=(const int3 & i)
{ {
x -= i.x; x -= i.x;
y -= i.y; y -= i.y;
@ -68,7 +61,7 @@ public:
} }
//increases all coordinates by given number //increases all coordinates by given number
int3 & operator+=(const si32 i) constexpr int3 & operator+=(const si32 i)
{ {
x += i; x += i;
y += i; y += i;
@ -76,7 +69,7 @@ public:
return *this; return *this;
} }
//decreases all coordinates by given number //decreases all coordinates by given number
int3 & operator-=(const si32 i) constexpr int3 & operator-=(const si32 i)
{ {
x -= i; x -= i;
y -= i; y -= i;
@ -84,10 +77,10 @@ public:
return *this; return *this;
} }
bool operator==(const int3 & i) const { return (x == i.x && y == i.y && z == i.z); } constexpr bool operator==(const int3 & i) const { return (x == i.x && y == i.y && z == i.z); }
bool operator!=(const int3 & i) const { return (x != i.x || y != i.y || z != i.z); } constexpr bool operator!=(const int3 & i) const { return (x != i.x || y != i.y || z != i.z); }
bool operator<(const int3 & i) const constexpr bool operator<(const int3 & i) const
{ {
if (z < i.z) if (z < i.z)
return true; return true;
@ -130,7 +123,7 @@ public:
} }
//returns squared distance on Oxy plane (z coord is not used) //returns squared distance on Oxy plane (z coord is not used)
ui32 dist2dSQ(const int3 & o) const constexpr ui32 dist2dSQ(const int3 & o) const
{ {
const si32 dx = (x - o.x); const si32 dx = (x - o.x);
const si32 dy = (y - o.y); const si32 dy = (y - o.y);
@ -142,17 +135,17 @@ public:
return std::sqrt((double)dist2dSQ(o)); return std::sqrt((double)dist2dSQ(o));
} }
//manhattan distance used for patrol radius (z coord is not used) //manhattan distance used for patrol radius (z coord is not used)
double mandist2d(const int3 & o) const constexpr double mandist2d(const int3 & o) const
{ {
return abs(o.x - x) + abs(o.y - y); return vstd::abs(o.x - x) + vstd::abs(o.y - y);
} }
//chebyshev distance used for ambient sounds (z coord is not used) //chebyshev distance used for ambient sounds (z coord is not used)
double chebdist2d(const int3 & o) const constexpr double chebdist2d(const int3 & o) const
{ {
return std::max(std::abs(o.x - x), std::abs(o.y - y)); return std::max(vstd::abs(o.x - x), vstd::abs(o.y - y));
} }
bool areNeighbours(const int3 & o) const constexpr bool areNeighbours(const int3 & o) const
{ {
return (dist2dSQ(o) < 4) && (z == o.z); return (dist2dSQ(o) < 4) && (z == o.z);
} }
@ -169,7 +162,7 @@ public:
return result; return result;
} }
bool valid() const //Should be named "isValid"? constexpr bool valid() const //Should be named "isValid"?
{ {
return z >= 0; //minimal condition that needs to be fulfilled for tiles in the map return z >= 0; //minimal condition that needs to be fulfilled for tiles in the map
} }
@ -182,7 +175,7 @@ public:
h & z; h & z;
} }
static std::array<int3, 8> getDirs() constexpr static std::array<int3, 8> getDirs()
{ {
return { { int3(0,1,0),int3(0,-1,0),int3(-1,0,0),int3(+1,0,0), return { { int3(0,1,0),int3(0,-1,0),int3(-1,0,0),int3(+1,0,0),
int3(1,1,0),int3(-1,1,0),int3(1,-1,0),int3(-1,-1,0) } }; int3(1,1,0),int3(-1,1,0),int3(1,-1,0),int3(-1,-1,0) } };