diff --git a/coord/base.hpp b/coord/base.hpp index 22bf4601..e63e4a52 100644 --- a/coord/base.hpp +++ b/coord/base.hpp @@ -185,5 +185,6 @@ namespace util::coord { }; } +#include "../vector.hpp" #endif diff --git a/coord/ops.hpp b/coord/ops.hpp index b86b0fc4..7ed7183e 100644 --- a/coord/ops.hpp +++ b/coord/ops.hpp @@ -11,11 +11,11 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * Copyright 2012-2016 Danny Robson + * Copyright 2012-2017 Danny Robson */ -#ifndef __UTIL_COORDS_OPS -#define __UTIL_COORDS_OPS +#ifndef CRUFT_UTIL_COORDS_OPS +#define CRUFT_UTIL_COORDS_OPS #include "./fwd.hpp" @@ -28,6 +28,7 @@ #include #include #include +#include namespace util { /////////////////////////////////////////////////////////////////////// @@ -293,6 +294,63 @@ namespace util { /////////////////////////////////////////////////////////////////////////// // logic operators + namespace detail { + template < + std::size_t S, + typename T, + template class K, + typename FuncT, + typename = std::enable_if_t< + is_coord_v>, + void + >, + std::size_t ...Indices + > + constexpr auto + compare (FuncT &&func, std::index_sequence, const K a, const K b) + { + return vector { + std::invoke (func, a[Indices], b[Indices])... + }; + } + } + + + //------------------------------------------------------------------------- + template < + std::size_t S, + typename T, + template class K, + typename FuncT, + typename = std::enable_if_t< + is_coord_v>, + void + >, + typename Indices = std::make_index_sequence + > + constexpr auto + compare (const K a, const K b, FuncT &&func) + { + return detail::compare (std::forward (func), Indices{}, a, b); + } + + + //------------------------------------------------------------------------- + template < + std::size_t S, + typename T, + template class K, + typename = std::enable_if_t< + is_coord_v>, + void + > + > + constexpr auto + compare (const K a, const K b) + { + return compare (a, b, std::equal_to {}); + } + /// elementwise equality operator template < @@ -303,16 +361,10 @@ namespace util { is_coord_v>, void > > - constexpr - bool + constexpr bool operator== (const K a, const K b) { - bool (*predicate)(const T&, const T&) = almost_equal; - - return std::equal (std::cbegin (a), - std::cend (a), - std::cbegin (b), - predicate); + return all (compare (a, b, std::equal_to {})); } ///------------------------------------------------------------------------ @@ -325,11 +377,10 @@ namespace util { is_coord_v>, void > > - constexpr - bool - operator!= (K a, K b) + constexpr bool + operator!= (const K a, const K b) { - return !(a == b); + return any (compare (a, b, std::not_equal_to {})); } @@ -909,6 +960,7 @@ namespace util { SCALAR_OP(>) SCALAR_OP(<=) SCALAR_OP(>=) + SCALAR_OP(==) SCALAR_OP(&&) SCALAR_OP(||) @@ -1026,6 +1078,7 @@ namespace util { return k; } + /////////////////////////////////////////////////////////////////////////// template < size_t S, diff --git a/test/coord.cpp b/test/coord.cpp index 4836918c..a11aede1 100644 --- a/test/coord.cpp +++ b/test/coord.cpp @@ -23,13 +23,6 @@ main (void) tap.expect_eq (-p, util::point2i { 1, -2 }, "unary point negation"); tap.expect_eq ( p, p, "unary point addition"); - tap.expect ( - std::is_same< - bool, - decltype(!p)::value_type - >::value, - "unary point boolean negation has type bool" - ); auto vec = util::vector4f (0.5f); tap.expect_eq (vec, util::normalised (vec), "normalisation of normalised vector"); diff --git a/test/matrix.cpp b/test/matrix.cpp index 844a8ef8..0e077098 100644 --- a/test/matrix.cpp +++ b/test/matrix.cpp @@ -203,7 +203,14 @@ main (void) euler = mod (euler + 4 * PI2, PI2); truth = mod (truth + 4 * PI2, PI2); - tap.expect_eq (truth, euler, "matrix-to-euler, %s", t.msg); + tap.expect ( + all (compare ( + truth, euler, + [] (auto a, auto b) { return util::almost_equal (a, b); } + )), + "matrix-to-euler, %s", + t.msg + ); } } diff --git a/vector.hpp b/vector.hpp index 2d58c1bc..fcf5e9e4 100644 --- a/vector.hpp +++ b/vector.hpp @@ -11,15 +11,21 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * Copyright 2011-2016 Danny Robson + * Copyright 2011-2017 Danny Robson */ -#ifndef __UTIL_VECTOR_HPP -#define __UTIL_VECTOR_HPP +#ifndef CRUFT_UTIL_VECTOR_HPP +#define CRUFT_UTIL_VECTOR_HPP + +#include "./coord/fwd.hpp" +#include "./coord.hpp" -#include "coord.hpp" #include "json/fwd.hpp" +#include + + +/////////////////////////////////////////////////////////////////////////////// namespace util { template struct vector : public coord::base