Add Point and Rect less-than operator

This allows using them as map and set keys
This commit is contained in:
Dmitry Marakasov 2015-11-26 12:57:36 +03:00
parent f525231b94
commit 95142b065c
5 changed files with 85 additions and 0 deletions

View File

@ -69,3 +69,9 @@ std::ostream& operator<<(std::ostream& stream, const SDL2pp::Point& point) {
stream << "[x:" << point.x << ",y:" << point.y << "]"; stream << "[x:" << point.x << ",y:" << point.y << "]";
return stream; return stream;
} }
bool operator<(const SDL2pp::Point& a, const SDL2pp::Point& b) {
if (a.x == b.x)
return a.y < b.y;
return a.x < b.x;
}

View File

@ -458,4 +458,15 @@ public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
std::ostream& operator<<(std::ostream& stream, const SDL2pp::Point& point); std::ostream& operator<<(std::ostream& stream, const SDL2pp::Point& point);
////////////////////////////////////////////////////////////
/// \brief Less-than operator for SDL2pp::Point
///
/// \param[in] a First comparison argument
/// \param[in] b Second comparison argument
///
/// \returns true if a < b
///
////////////////////////////////////////////////////////////
bool operator<(const SDL2pp::Point& a, const SDL2pp::Point& b);
#endif #endif

View File

@ -88,3 +88,15 @@ std::ostream& operator<<(std::ostream& stream, const SDL2pp::Rect& rect) {
stream << "[x:" << rect.x << ",y:" << rect.y << ",w:" << rect.w << ",h:" << rect.h << "]"; stream << "[x:" << rect.x << ",y:" << rect.y << ",w:" << rect.w << ",h:" << rect.h << "]";
return stream; return stream;
} }
bool operator<(const SDL2pp::Rect& a, const SDL2pp::Rect& b) {
if (a.x == b.x) {
if (a.y == b.y) {
if (a.w == b.w)
return a.h < b.h;
return a.w < b.w;
}
return a.y < b.y;
}
return a.x < b.x;
}

View File

@ -534,4 +534,15 @@ public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
std::ostream& operator<<(std::ostream& stream, const SDL2pp::Rect& rect); std::ostream& operator<<(std::ostream& stream, const SDL2pp::Rect& rect);
////////////////////////////////////////////////////////////
/// \brief Less-than operator for SDL2pp::Rect
///
/// \param[in] a First comparison argument
/// \param[in] b Second comparison argument
///
/// \returns true if a < b
///
////////////////////////////////////////////////////////////
bool operator<(const SDL2pp::Rect& a, const SDL2pp::Rect& b);
#endif #endif

View File

@ -79,6 +79,18 @@ BEGIN_TEST()
EXPECT_EQUAL(p /= Point(5, 3), Point(3, 3)); EXPECT_EQUAL(p /= Point(5, 3), Point(3, 3));
EXPECT_EQUAL(p *= Point(10, 20), Point(30, 60)); EXPECT_EQUAL(p *= Point(10, 20), Point(30, 60));
EXPECT_EQUAL(p %= Point(7, 11), Point(2, 5)); EXPECT_EQUAL(p %= Point(7, 11), Point(2, 5));
// Less-than
EXPECT_TRUE(Point(0, 0) < Point(1, 0));
EXPECT_TRUE(Point(0, 1) < Point(1, 0));
EXPECT_TRUE(Point(0, 1) < Point(1, 1));
EXPECT_TRUE(Point(0, 0) < Point(0, 1));
EXPECT_TRUE(!(Point(1, 0) < Point(0, 0)));
EXPECT_TRUE(!(Point(1, 0) < Point(0, 1)));
EXPECT_TRUE(!(Point(1, 1) < Point(0, 1)));
EXPECT_TRUE(!(Point(0, 1) < Point(0, 0)));
EXPECT_TRUE(!(Point(1, 1) < Point(1, 1)));
} }
{ {
@ -286,6 +298,39 @@ BEGIN_TEST()
EXPECT_EQUAL(r, Rect(-9, -18, 3, 4)); EXPECT_EQUAL(r, Rect(-9, -18, 3, 4));
} }
{
// Less-than
EXPECT_TRUE(!(Rect(0, 0, 0, 0) < Rect(0, 0, 0, 0)));
EXPECT_TRUE(Rect(0, 0, 0, 0) < Rect(0, 0, 0, 1));
EXPECT_TRUE(Rect(0, 0, 0, 0) < Rect(0, 0, 1, 0));
EXPECT_TRUE(Rect(0, 0, 0, 0) < Rect(0, 1, 0, 0));
EXPECT_TRUE(Rect(0, 0, 0, 0) < Rect(1, 0, 0, 0));
EXPECT_TRUE(!(Rect(0, 0, 0, 1) < Rect(0, 0, 0, 0)));
EXPECT_TRUE(!(Rect(0, 0, 0, 1) < Rect(0, 0, 0, 1)));
EXPECT_TRUE(Rect(0, 0, 0, 1) < Rect(0, 0, 1, 0));
EXPECT_TRUE(Rect(0, 0, 0, 1) < Rect(0, 1, 0, 0));
EXPECT_TRUE(Rect(0, 0, 0, 1) < Rect(1, 0, 0, 0));
EXPECT_TRUE(!(Rect(0, 0, 1, 0) < Rect(0, 0, 0, 0)));
EXPECT_TRUE(!(Rect(0, 0, 1, 0) < Rect(0, 0, 0, 1)));
EXPECT_TRUE(!(Rect(0, 0, 1, 0) < Rect(0, 0, 1, 0)));
EXPECT_TRUE(Rect(0, 0, 1, 0) < Rect(0, 1, 0, 0));
EXPECT_TRUE(Rect(0, 0, 1, 0) < Rect(1, 0, 0, 0));
EXPECT_TRUE(!(Rect(0, 1, 0, 0) < Rect(0, 0, 0, 0)));
EXPECT_TRUE(!(Rect(0, 1, 0, 0) < Rect(0, 0, 0, 1)));
EXPECT_TRUE(!(Rect(0, 1, 0, 0) < Rect(0, 0, 1, 0)));
EXPECT_TRUE(!(Rect(0, 1, 0, 0) < Rect(0, 1, 0, 0)));
EXPECT_TRUE(Rect(0, 1, 0, 0) < Rect(1, 0, 0, 0));
EXPECT_TRUE(!(Rect(1, 0, 0, 0) < Rect(0, 0, 0, 0)));
EXPECT_TRUE(!(Rect(1, 0, 0, 0) < Rect(0, 0, 0, 1)));
EXPECT_TRUE(!(Rect(1, 0, 0, 0) < Rect(0, 0, 1, 0)));
EXPECT_TRUE(!(Rect(1, 0, 0, 0) < Rect(0, 1, 0, 0)));
EXPECT_TRUE(!(Rect(1, 0, 0, 0) < Rect(1, 0, 0, 0)));
}
{ {
// Construction from and comparison with SDL objects // Construction from and comparison with SDL objects
SDL_Rect sdlrect = { 1, 2, 3, 4 }; SDL_Rect sdlrect = { 1, 2, 3, 4 };