From: Martin Read Date: Fri, 7 Feb 2014 21:58:31 +0000 (+0000) Subject: Add comparators to Coord/Offset X-Git-Tag: printmsg-purged~23 X-Git-Url: http://git.blackswordsonics.com/?a=commitdiff_plain;h=7f15962d0ebe955d73844029914c0bc4d7f8202b;p=victrix-abyssi Add comparators to Coord/Offset --- diff --git a/coord.hh b/coord.hh index 7df6a9b..0a0db14 100644 --- 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 T mysign(T val); template inline T myabs(T val) { return (val < 0) ? -val : val; } template 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;