diff --git a/Global.h b/Global.h index c1ee6ccb2..66f61d4d4 100644 --- a/Global.h +++ b/Global.h @@ -746,6 +746,12 @@ namespace vstd } 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-=; diff --git a/lib/int3.h b/lib/int3.h index 73bda7d76..b665ff2db 100644 --- a/lib/int3.h +++ b/lib/int3.h @@ -18,48 +18,41 @@ public: si32 x, y, z; //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 - 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 - 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 si32 X, const si32 Y, const si32 Z) : x(X), y(Y), z(Z) {} + constexpr int3(const int3 & c) = default; - int3 & operator=(const int3 & c) // Should be set to default (C++11)? - { - x = c.x; - y = c.y; - z = c.z; + constexpr int3 & operator=(const int3 & c) = default; + constexpr int3 operator-() const { return int3(-x, -y, -z); } - return *this; - } - 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); } + 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); } //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 - 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 - 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 - 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 - 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 - 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; y += i.y; z += i.z; return *this; } - int3 & operator-=(const int3 & i) + constexpr int3 & operator-=(const int3 & i) { x -= i.x; y -= i.y; @@ -68,7 +61,7 @@ public: } //increases all coordinates by given number - int3 & operator+=(const si32 i) + constexpr int3 & operator+=(const si32 i) { x += i; y += i; @@ -76,7 +69,7 @@ public: return *this; } //decreases all coordinates by given number - int3 & operator-=(const si32 i) + constexpr int3 & operator-=(const si32 i) { x -= i; y -= i; @@ -84,10 +77,10 @@ public: return *this; } - 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); } + 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) return true; @@ -130,7 +123,7 @@ public: } //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 dy = (y - o.y); @@ -142,17 +135,17 @@ public: return std::sqrt((double)dist2dSQ(o)); } //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) - 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); } @@ -169,7 +162,7 @@ public: 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 } @@ -182,7 +175,7 @@ public: h & z; } - static std::array getDirs() + constexpr static std::array getDirs() { 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) } };