From c74c2abc4fad4e4aea83f90a7634424209df45af Mon Sep 17 00:00:00 2001 From: Kyle Lutz Date: Tue, 30 Dec 2014 13:44:24 -0800 Subject: [PATCH] Add four-iterator versions of equal() and mismatch() --- include/boost/compute/algorithm/equal.hpp | 15 +++++++++++ include/boost/compute/algorithm/mismatch.hpp | 19 ++++++++++++++ test/test_equal.cpp | 15 +++++++++++ test/test_mismatch.cpp | 26 ++++++++++++++++++++ 4 files changed, 75 insertions(+) diff --git a/include/boost/compute/algorithm/equal.hpp b/include/boost/compute/algorithm/equal.hpp index de63ad62..e8752062 100644 --- a/include/boost/compute/algorithm/equal.hpp +++ b/include/boost/compute/algorithm/equal.hpp @@ -32,6 +32,21 @@ inline bool equal(InputIterator1 first1, queue).first == last1; } +/// \overload +template +inline bool equal(InputIterator1 first1, + InputIterator1 last1, + InputIterator2 first2, + InputIterator2 last2, + command_queue &queue = system::default_queue()) +{ + if(std::distance(first1, last1) != std::distance(first2, last2)){ + return false; + } + + return ::boost::compute::equal(first1, last1, first2, queue); +} + } // end compute namespace } // end boost namespace diff --git a/include/boost/compute/algorithm/mismatch.hpp b/include/boost/compute/algorithm/mismatch.hpp index 21cf4837..9c4a8761 100644 --- a/include/boost/compute/algorithm/mismatch.hpp +++ b/include/boost/compute/algorithm/mismatch.hpp @@ -64,6 +64,25 @@ mismatch(InputIterator1 first1, return std::make_pair(iter, first2 + std::distance(first1, iter)); } +/// \overload +template +inline std::pair +mismatch(InputIterator1 first1, + InputIterator1 last1, + InputIterator2 first2, + InputIterator2 last2, + command_queue &queue = system::default_queue()) +{ + if(std::distance(first1, last1) < std::distance(first2, last2)){ + return ::boost::compute::mismatch(first1, last1, first2, queue); + } + else { + return ::boost::compute::mismatch( + first1, first1 + std::distance(first2, last2), first2, queue + ); + } +} + } // end compute namespace } // end boost namespace diff --git a/test/test_equal.cpp b/test/test_equal.cpp index e86ca9d6..27299e98 100644 --- a/test/test_equal.cpp +++ b/test/test_equal.cpp @@ -12,6 +12,7 @@ #include #include +#include #include #include @@ -40,4 +41,18 @@ BOOST_AUTO_TEST_CASE(equal_string) BOOST_CHECK(boost::compute::equal(a.begin(), a.end(), c.begin()) == false); } +BOOST_AUTO_TEST_CASE(equal_different_range_sizes) +{ + boost::compute::vector a(10, context); + boost::compute::vector b(20, context); + + boost::compute::fill(a.begin(), a.end(), 3, queue); + boost::compute::fill(b.begin(), b.end(), 3, queue); + + BOOST_CHECK(boost::compute::equal(a.begin(), a.end(), b.begin(), b.end(), queue) == false); + BOOST_CHECK(boost::compute::equal(a.begin(), a.end(), a.begin(), a.end(), queue) == true); + BOOST_CHECK(boost::compute::equal(b.begin(), b.end(), a.begin(), a.end(), queue) == false); + BOOST_CHECK(boost::compute::equal(b.begin(), b.end(), b.begin(), b.end(), queue) == true); +} + BOOST_AUTO_TEST_SUITE_END() diff --git a/test/test_mismatch.cpp b/test/test_mismatch.cpp index 338afc87..70588671 100644 --- a/test/test_mismatch.cpp +++ b/test/test_mismatch.cpp @@ -11,6 +11,7 @@ #define BOOST_TEST_MODULE TestMismatch #include +#include #include #include @@ -34,4 +35,29 @@ BOOST_AUTO_TEST_CASE(mismatch_int) BOOST_CHECK_EQUAL(int(*location.second), int(7)); } +BOOST_AUTO_TEST_CASE(mismatch_different_range_sizes) +{ + boost::compute::vector a(10, context); + boost::compute::vector b(20, context); + + boost::compute::fill(a.begin(), a.end(), 3, queue); + boost::compute::fill(b.begin(), b.end(), 3, queue); + + typedef boost::compute::vector::iterator iter; + + std::pair location; + + location = boost::compute::mismatch( + a.begin(), a.end(), b.begin(), b.end(), queue + ); + BOOST_CHECK(location.first == a.end()); + BOOST_CHECK(location.second == b.begin() + 10); + + location = boost::compute::mismatch( + b.begin(), b.end(), a.begin(), a.end(), queue + ); + BOOST_CHECK(location.first == b.begin() + 10); + BOOST_CHECK(location.second == a.end()); +} + BOOST_AUTO_TEST_SUITE_END()