[compute] 40/49: Fix for search and search_n algorithm

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Fri Dec 18 17:58:21 UTC 2015


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to branch master
in repository compute.

commit 0e51ad4ad8d0afad233926d327ec664d85d5efac
Author: Jakub Szuppe <j.szuppe at gmail.com>
Date:   Sat Sep 12 19:29:16 2015 +0200

    Fix for search and search_n algorithm
---
 include/boost/compute/algorithm/search.hpp   | 15 ++++++++++++---
 include/boost/compute/algorithm/search_n.hpp | 17 +++++++++++++----
 test/test_search.cpp                         | 19 ++++++++++++++-----
 test/test_search_n.cpp                       | 15 ++++++++++-----
 4 files changed, 49 insertions(+), 17 deletions(-)

diff --git a/include/boost/compute/algorithm/search.hpp b/include/boost/compute/algorithm/search.hpp
index cd4efe8..3d3d035 100644
--- a/include/boost/compute/algorithm/search.hpp
+++ b/include/boost/compute/algorithm/search.hpp
@@ -26,7 +26,7 @@ namespace compute {
 ///
 /// 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
+/// \return Iterator pointing to beginning of first occurrence
 ///
 /// \param t_first Iterator pointing to start of text
 /// \param t_last Iterator pointing to end of text
@@ -41,9 +41,14 @@ inline TextIterator search(TextIterator t_first,
                            PatternIterator p_last,
                            command_queue &queue = system::default_queue())
 {
-    vector<uint_> matching_indices(detail::iterator_range_size(t_first, t_last),
-                                    queue.get_context());
+    // there is no need to check if pattern starts at last n - 1 indices
+    vector<uint_> matching_indices(
+        detail::iterator_range_size(t_first, t_last)
+            - detail::iterator_range_size(p_first, p_last) + 1,
+        queue.get_context()
+    );
 
+    // search_kernel puts value 1 at every index in vector where pattern starts at
     detail::search_kernel<PatternIterator,
                           TextIterator,
                           vector<uint_>::iterator> kernel;
@@ -55,6 +60,10 @@ inline TextIterator search(TextIterator t_first,
         matching_indices.begin(), matching_indices.end(), uint_(1), queue
     );
 
+    // pattern was not found
+    if(index == matching_indices.end())
+        return t_last;
+
     return t_first + detail::iterator_range_size(matching_indices.begin(), index);
 }
 
diff --git a/include/boost/compute/algorithm/search_n.hpp b/include/boost/compute/algorithm/search_n.hpp
index 9d45f11..9e03111 100644
--- a/include/boost/compute/algorithm/search_n.hpp
+++ b/include/boost/compute/algorithm/search_n.hpp
@@ -92,9 +92,9 @@ private:
 ///
 /// \brief Substring matching algorithm
 ///
-/// Searches for the first occurence of n consecutive occurences of
+/// Searches for the first occurrence of n consecutive occurrences of
 /// value in text [t_first, t_last).
-/// \return Iterator pointing to beginning of first occurence
+/// \return Iterator pointing to beginning of first occurrence
 ///
 /// \param t_first Iterator pointing to start of text
 /// \param t_last Iterator pointing to end of text
@@ -109,9 +109,14 @@ inline TextIterator search_n(TextIterator t_first,
                              ValueType value,
                              command_queue &queue = system::default_queue())
 {
-    vector<uint_> matching_indices(detail::iterator_range_size(t_first, t_last),
-                                    queue.get_context());
+    // there is no need to check if pattern starts at last n - 1 indices
+    vector<uint_> matching_indices(
+        detail::iterator_range_size(t_first, t_last) + 1 - n,
+        queue.get_context()
+    );
 
+    // search_n_kernel puts value 1 at every index in vector where pattern
+    // of n values starts at
     detail::search_n_kernel<TextIterator,
                             vector<uint_>::iterator> kernel;
 
@@ -122,6 +127,10 @@ inline TextIterator search_n(TextIterator t_first,
         matching_indices.begin(), matching_indices.end(), uint_(1), queue
     );
 
+    // pattern was not found
+    if(index == matching_indices.end())
+        return t_last;
+
     return t_first + detail::iterator_range_size(matching_indices.begin(), index);
 }
 
diff --git a/test/test_search.cpp b/test/test_search.cpp
index d3d7da4..5194362 100644
--- a/test/test_search.cpp
+++ b/test/test_search.cpp
@@ -24,8 +24,8 @@ namespace bc = boost::compute;
 
 BOOST_AUTO_TEST_CASE(search_int)
 {
-    int data[] = {1, 4, 2, 6, 3, 2, 6, 3, 4, 6};
-    bc::vector<bc::int_> vectort(data, data + 10, queue);
+    int data[] = {1, 4, 2, 6, 3, 2, 6, 3, 4, 6, 6};
+    bc::vector<bc::int_> vectort(data, data + 11, queue);
 
     int datap[] = {2, 6};
     bc::vector<bc::int_> vectorp(datap, datap + 2, queue);
@@ -34,7 +34,7 @@ BOOST_AUTO_TEST_CASE(search_int)
         bc::search(vectort.begin(), vectort.end(),
                     vectorp.begin(), vectorp.end(), queue);
 
-    BOOST_VERIFY(iter == vectort.begin() + 2);
+    BOOST_CHECK(iter == vectort.begin() + 2);
 
     vectorp[1] = 9;
 
@@ -42,7 +42,16 @@ BOOST_AUTO_TEST_CASE(search_int)
         bc::search(vectort.begin(), vectort.end(),
                     vectorp.begin(), vectorp.end(), queue);
 
-    BOOST_VERIFY(iter == vectort.begin() + 10);
+    BOOST_CHECK(iter == vectort.begin() + 11);
+
+    vectorp[0] = 6;
+    vectorp[1] = 6;
+
+    iter =
+        bc::search(vectort.begin(), vectort.end(),
+                    vectorp.begin(), vectorp.end(), queue);
+
+    BOOST_CHECK(iter == vectort.begin() + 9);
 }
 
 BOOST_AUTO_TEST_CASE(search_string)
@@ -57,7 +66,7 @@ BOOST_AUTO_TEST_CASE(search_string)
         bc::search(vectort.begin(), vectort.end(),
                     vectorp.begin(), vectorp.end(), queue);
 
-    BOOST_VERIFY(iter == vectort.begin() + 2);
+    BOOST_CHECK(iter == vectort.begin() + 2);
 }
 
 BOOST_AUTO_TEST_SUITE_END()
diff --git a/test/test_search_n.cpp b/test/test_search_n.cpp
index e871917..c8b5af0 100644
--- a/test/test_search_n.cpp
+++ b/test/test_search_n.cpp
@@ -23,18 +23,23 @@ namespace bc = boost::compute;
 
 BOOST_AUTO_TEST_CASE(search_int)
 {
-    int data[] = {1, 2, 2, 2, 3, 2, 2, 2, 4, 6};
-    bc::vector<bc::int_> vectort(data, data + 10, queue);
+    int data[] = {1, 2, 2, 2, 3, 2, 2, 2, 4, 6, 6};
+    bc::vector<bc::int_> vectort(data, data + 11, queue);
 
     bc::vector<bc::int_>::iterator iter =
         bc::search_n(vectort.begin(), vectort.end(), 3, 2, queue);
 
-    BOOST_VERIFY(iter == vectort.begin() + 1);
+    BOOST_CHECK(iter == vectort.begin() + 1);
 
     iter =
         bc::search_n(vectort.begin(), vectort.end(), 5, 2, queue);
 
-    BOOST_VERIFY(iter == vectort.begin() + 10);
+    BOOST_CHECK(iter == vectort.begin() + 11);
+
+    iter =
+        bc::search_n(vectort.begin(), vectort.end(), 2, 6, queue);
+
+    BOOST_CHECK(iter == vectort.begin() + 9);
 }
 
 BOOST_AUTO_TEST_CASE(search_string)
@@ -45,7 +50,7 @@ BOOST_AUTO_TEST_CASE(search_string)
     bc::vector<bc::char_>::iterator iter =
         bc::search_n(vectort.begin(), vectort.end(), 2, 'a', queue);
 
-    BOOST_VERIFY(iter == vectort.begin() + 2);
+    BOOST_CHECK(iter == vectort.begin() + 2);
 }
 
 BOOST_AUTO_TEST_SUITE_END()

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/compute.git



More information about the debian-science-commits mailing list