diff --git a/include/boost/compute/algorithm.hpp b/include/boost/compute/algorithm.hpp index fe802cc6..bc323b8e 100644 --- a/include/boost/compute/algorithm.hpp +++ b/include/boost/compute/algorithm.hpp @@ -64,6 +64,7 @@ #include #include #include +#include #include #include #include diff --git a/include/boost/compute/algorithm/search.hpp b/include/boost/compute/algorithm/search.hpp new file mode 100644 index 00000000..80f6da3d --- /dev/null +++ b/include/boost/compute/algorithm/search.hpp @@ -0,0 +1,67 @@ +//---------------------------------------------------------------------------// +// Copyright (c) 2014 Roshan +// +// Distributed under the Boost Software License, Version 1.0 +// See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt +// +// See http://kylelutz.github.com/compute for more information. +//---------------------------------------------------------------------------// + +#ifndef BOOST_COMPUTE_ALGORITHM_SEARCH_HPP +#define BOOST_COMPUTE_ALGORITHM_SEARCH_HPP + +#include +#include +#include +#include +#include +#include +#include + +namespace boost { +namespace compute { + +/*! \brief Substring matching algorithm + * + * Searches for the first match of the pattern [p_first, p_last) + * in text [t_first, t_last). + * \return Iterator pointing to beginning of first occurence + * + * \param p_first Iterator pointing to start of pattern + * \param p_last Iterator pointing to end of pattern + * \param t_first Iterator pointing to start of text + * \param t_last Iterator pointing to end of text + * \param queue Queue on which to execute + */ +template +inline TextIterator search(PatternIterator p_first, + PatternIterator p_last, + TextIterator t_first, + TextIterator t_last, + command_queue &queue = system::default_queue()) +{ + vector matching_indices(detail::iterator_range_size(t_first, t_last), + queue.get_context()); + + detail::search_kernel::iterator> kernel; + + kernel.set_range(p_first, p_last, t_first, t_last, matching_indices.begin()); + kernel.exec(queue); + + using boost::compute::_1; + + vector::iterator index = find_if(matching_indices.begin(), + matching_indices.end(), + _1 == 1, + queue); + + return t_first + detail::iterator_range_size(matching_indices.begin(), index); +} + +} //end compute namespace +} //end boost namespace + +#endif // BOOST_COMPUTE_ALGORITHM_SEARCH_HPP diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 00fe3af1..b409ea70 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -88,6 +88,7 @@ add_compute_test("algorithm.rotate" test_rotate.cpp) add_compute_test("algorithm.rotate_copy" test_rotate_copy.cpp) add_compute_test("algorithm.scan" test_scan.cpp) add_compute_test("algorithm.scatter" test_scatter.cpp) +add_compute_test("algorithm.search" test_search.cpp) add_compute_test("algorithm.sort" test_sort.cpp) add_compute_test("algorithm.sort_by_key" test_sort_by_key.cpp) add_compute_test("algorithm.stable_sort" test_stable_sort.cpp) diff --git a/test/test_search.cpp b/test/test_search.cpp new file mode 100644 index 00000000..d3015849 --- /dev/null +++ b/test/test_search.cpp @@ -0,0 +1,52 @@ +//---------------------------------------------------------------------------// +// Copyright (c) 2014 Roshan +// +// Distributed under the Boost Software License, Version 1.0 +// See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt +// +// See http://kylelutz.github.com/compute for more information. +//---------------------------------------------------------------------------// + +#define BOOST_TEST_MODULE TestSearch +#include + +#include +#include +#include +#include +#include + +#include "check_macros.hpp" +#include "context_setup.hpp" + +namespace bc = boost::compute; + +BOOST_AUTO_TEST_CASE(search_int) +{ + int data[] = {1, 4, 2, 6, 3, 2, 6, 3, 4, 6}; + bc::vector vectort(10, context); + + bc::copy_n(data, 10, vectort.begin(), queue); + + int datap[] = {2, 6}; + bc::vector vectorp(2, context); + + bc::copy_n(datap, 2, vectorp.begin(), queue); + + bc::vector::iterator iter = + bc::search(vectorp.begin(), vectorp.end(), + vectort.begin(), vectort.end(), queue); + + BOOST_VERIFY(iter == vectort.begin() + 2); + + vectorp[1] = 9; + + iter = + bc::search(vectorp.begin(), vectorp.end(), + vectort.begin(), vectort.end(), queue); + + BOOST_VERIFY(iter == vectort.begin() + 10); +} + +BOOST_AUTO_TEST_SUITE_END()