From: fluffymormegil Date: Sat, 9 Oct 2010 11:50:31 +0000 (+0100) Subject: Added Coord and abs headers X-Git-Tag: v1.0.0~22^2~33 X-Git-Url: http://git.blackswordsonics.com/?a=commitdiff_plain;h=0ffe8ac5a8d6d565fe0841aae62695d3332d838d;p=libmormegil Added Coord and abs headers --- diff --git a/include/libmormegil/Coord.hh b/include/libmormegil/Coord.hh new file mode 100644 index 0000000..d5b72d5 --- /dev/null +++ b/include/libmormegil/Coord.hh @@ -0,0 +1,95 @@ +// libmormegil/Coord.hh +// +// Copyright 2010 Martin Read. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// 1. Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// 3. Neither the name of the author nor the names of any other contributors +// may be used to endorse or promote products derived from this software +// without their specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +// SUCH DAMAGE. + +// We need std::max<>, std::min<>. The thing called std::abs<> is in +// and is intended for use on complex numbers; dude what the fuck? + +#ifndef libmormegil_Coord_hh +#define libmormegil_Coord_hh +#include + +#ifndef libmormegil_abs_hh +#include +#endif + +namespace libmormegil +{ + template struct basic_offset + { + typedef basic_offset& ref; + typedef const basic_offset& const_ref; + int y; + int x; + ref operator +=(const_ref right) + { + y += right.y; + x += right.x; + return *this; + } + + ref operator -=(const_ref right) + { + y -= right.y; + x -= right.x; + return *this; + } + + int length_inf() { return std::min(std::abs(y), std::abs(x)); } + }; + + template struct basic_coord + { + typedef basic_coord& ref; + typedef const basic_coord& const_ref; + int y; + int x; + ref operator +=(const basic_offset& right) + { + y += right.y; + x += right.x; + return *this; + } + ref operator -=(const basic_offset& right) + { + y -= right.y; + x -= right.x; + return *this; + } + basic_offset operator -(const_ref right) const + { + basic_offset tmp = { y - right.y, x - right.x }; + return tmp; + } + }; + + typedef basic_offset Offset; + typedef basic_coord Coord; +} +#endif // libmormegil_Coord_hh + +// vim:ts=8:sw=4:expandtab diff --git a/include/libmormegil/abs.hh b/include/libmormegil/abs.hh new file mode 100644 index 0000000..9200e55 --- /dev/null +++ b/include/libmormegil/abs.hh @@ -0,0 +1,41 @@ +// libmormegil/abs.hh +// +// Copyright 2010 Martin Read. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// 1. Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// 3. Neither the name of the author nor the names of any other contributors +// may be used to endorse or promote products derived from this software +// without their specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +// SUCH DAMAGE. + +#ifndef libmormegil_abs_hh +#define libmormegil_abs_hh + +namespace libmormegil +{ + inline template T abs(const T& i) { i < T(0) ? -i : i; } + int length_inf() { return std::min(std::abs(y), std::abs(x)); } + }; +} +#endif // libmormegil_abs_hh + +// vim:ts=8:sw=4:expandtab +