Add comparators to Coord/Offset
authorMartin Read <martin@blackswordsonics.com>
Fri, 7 Feb 2014 21:58:31 +0000 (21:58 +0000)
committerMartin Read <martin@blackswordsonics.com>
Fri, 7 Feb 2014 21:58:31 +0000 (21:58 +0000)
coord.hh

index 7df6a9b..0a0db14 100644 (file)
--- a/coord.hh
+++ b/coord.hh
@@ -2,7 +2,7 @@
  * \brief Positions and offsets
  */
 
-/* Copyright 2013 Martin Read
+/* Copyright 2013-2014 Martin Read
  * 
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -37,6 +37,14 @@ template <typename T> T mysign(T val);
 template <typename T> inline T myabs(T val) { return (val < 0) ? -val : val; }
 template <typename T> inline T mysign(T val) { return (val < 0) ? -1 : ((val > 0) ? 1 : 0); }
 
+/*! \brief A two-dimensional vector
+ *
+ *  An Offset represents a vector in a plane. Addition and subtraction of
+ *  Offsets is fully defined.
+ *
+ *  The lt/le/ge/gt operators impose a total ordering, allowing Offset to
+ *  be used as the key-type of std::map.
+ */
 struct Offset
 {
     int y;
@@ -50,8 +58,21 @@ struct Offset
     Offset const& operator -=(Offset const& right) { y -= right.y; x -= right.x; return *this; }
     bool operator !=(Offset const& right) const { return (y != right.y) || (x != right.x); }
     bool operator ==(Offset const& right) const { return (y == right.y) && (x == right.x); }
+    bool operator <(Offset const& right) const { return (y < right.y) || ((y == right.y) && (x < right.x)); }
+    bool operator <=(Offset const& right) const { return (y < right.y) || ((y == right.y) && (x <= right.x)); }
+    bool operator >(Offset const& right) const { return (y > right.y) || ((y == right.y) && (x > right.x)); }
+    bool operator >=(Offset const& right) const { return (y > right.y) || ((y == right.y) && (x >= right.x)); }
 };
 
+/*! \brief A two-dimensional point
+ *
+ *  A Coord approximately represents a point on a plane. Addition of Coords
+ *  to each other is undefined. Addition of an Offset to a Coord yields a
+ *  Coord. Subtraction of a Coord from another Coord yields an Offset.
+ *
+ *  The lt/le/ge/gt operators impose a total ordering, allowing Coord to
+ *  be used as the key-type of std::map.
+ */
 struct Coord
 {
     int y;
@@ -66,6 +87,10 @@ struct Coord
     Coord const& operator -=(Offset const& right) { y -= right.y; x -= right.x; return *this;}
     bool operator !=(Coord const& right) const { return (y != right.y) || (x != right.x); }
     bool operator ==(Coord const& right) const { return (y == right.y) && (x == right.x); }
+    bool operator <(Coord const& right) const { return (y < right.y) || ((y == right.y) && (x < right.x)); }
+    bool operator <=(Coord const& right) const { return (y < right.y) || ((y == right.y) && (x <= right.x)); }
+    bool operator >(Coord const& right) const { return (y > right.y) || ((y == right.y) && (x > right.x)); }
+    bool operator >=(Coord const& right) const { return (y > right.y) || ((y == right.y) && (x >= right.x)); }
 };
 
 extern Coord const Nowhere;