From 0016c83dab9b17ccb7cf5ca134672be398cc4700 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Mon, 31 Jul 2017 15:41:33 +1000 Subject: [PATCH] coord: add the select function --- coord/ops.hpp | 24 ++++++++++++++++++++++++ test/coord.cpp | 11 +++++++++++ 2 files changed, 35 insertions(+) diff --git a/coord/ops.hpp b/coord/ops.hpp index fb2163b2..925e8853 100644 --- a/coord/ops.hpp +++ b/coord/ops.hpp @@ -854,6 +854,30 @@ namespace util { } + ///------------------------------------------------------------------------ + /// returns an instance of K elementwise using a when s is true, and b + /// otherwise. ie, k[i] = s[i] ? a[i] : b[i]; + /// + /// corresponds to the function `select' from OpenCL. + template < + size_t S, + typename T, + template class K, + typename = std::enable_if_t< + is_coord_v>, + void + > + > + constexpr + K + select (vector s, K a, K b) + { + K k {}; + for (size_t i = 0; i < S; ++i) + k[i] = s[i] ? a[i] : b[i]; + return k; + } + /////////////////////////////////////////////////////////////////////////// template < size_t S, diff --git a/test/coord.cpp b/test/coord.cpp index dfdfdb12..c25585ea 100644 --- a/test/coord.cpp +++ b/test/coord.cpp @@ -53,5 +53,16 @@ main (void) ); } + { + const util::point3f a { -1, 2, 0 }; + const util::point3f b { 1, 0, 2 }; + + const util::point3f lo { -1, 0, 0 }; + const util::point3f hi { 1, 2, 2 }; + + tap.expect_eq (select (a < b, a, b), lo, "select with points and min"); + tap.expect_eq (select (a > b, a, b), hi, "select with points and max"); + }; + return tap.status (); }