Skip to content

Commit 0e51ad4

Browse files
committed
Fix for search and search_n algorithm
1 parent 377e509 commit 0e51ad4

File tree

4 files changed

+49
-17
lines changed

4 files changed

+49
-17
lines changed

include/boost/compute/algorithm/search.hpp

+12-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace compute {
2626
///
2727
/// Searches for the first match of the pattern [p_first, p_last)
2828
/// in text [t_first, t_last).
29-
/// \return Iterator pointing to beginning of first occurence
29+
/// \return Iterator pointing to beginning of first occurrence
3030
///
3131
/// \param t_first Iterator pointing to start of text
3232
/// \param t_last Iterator pointing to end of text
@@ -41,9 +41,14 @@ inline TextIterator search(TextIterator t_first,
4141
PatternIterator p_last,
4242
command_queue &queue = system::default_queue())
4343
{
44-
vector<uint_> matching_indices(detail::iterator_range_size(t_first, t_last),
45-
queue.get_context());
44+
// there is no need to check if pattern starts at last n - 1 indices
45+
vector<uint_> matching_indices(
46+
detail::iterator_range_size(t_first, t_last)
47+
- detail::iterator_range_size(p_first, p_last) + 1,
48+
queue.get_context()
49+
);
4650

51+
// search_kernel puts value 1 at every index in vector where pattern starts at
4752
detail::search_kernel<PatternIterator,
4853
TextIterator,
4954
vector<uint_>::iterator> kernel;
@@ -55,6 +60,10 @@ inline TextIterator search(TextIterator t_first,
5560
matching_indices.begin(), matching_indices.end(), uint_(1), queue
5661
);
5762

63+
// pattern was not found
64+
if(index == matching_indices.end())
65+
return t_last;
66+
5867
return t_first + detail::iterator_range_size(matching_indices.begin(), index);
5968
}
6069

include/boost/compute/algorithm/search_n.hpp

+13-4
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ class search_n_kernel : public meta_kernel
9292
///
9393
/// \brief Substring matching algorithm
9494
///
95-
/// Searches for the first occurence of n consecutive occurences of
95+
/// Searches for the first occurrence of n consecutive occurrences of
9696
/// value in text [t_first, t_last).
97-
/// \return Iterator pointing to beginning of first occurence
97+
/// \return Iterator pointing to beginning of first occurrence
9898
///
9999
/// \param t_first Iterator pointing to start of text
100100
/// \param t_last Iterator pointing to end of text
@@ -109,9 +109,14 @@ inline TextIterator search_n(TextIterator t_first,
109109
ValueType value,
110110
command_queue &queue = system::default_queue())
111111
{
112-
vector<uint_> matching_indices(detail::iterator_range_size(t_first, t_last),
113-
queue.get_context());
112+
// there is no need to check if pattern starts at last n - 1 indices
113+
vector<uint_> matching_indices(
114+
detail::iterator_range_size(t_first, t_last) + 1 - n,
115+
queue.get_context()
116+
);
114117

118+
// search_n_kernel puts value 1 at every index in vector where pattern
119+
// of n values starts at
115120
detail::search_n_kernel<TextIterator,
116121
vector<uint_>::iterator> kernel;
117122

@@ -122,6 +127,10 @@ inline TextIterator search_n(TextIterator t_first,
122127
matching_indices.begin(), matching_indices.end(), uint_(1), queue
123128
);
124129

130+
// pattern was not found
131+
if(index == matching_indices.end())
132+
return t_last;
133+
125134
return t_first + detail::iterator_range_size(matching_indices.begin(), index);
126135
}
127136

test/test_search.cpp

+14-5
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ namespace bc = boost::compute;
2424

2525
BOOST_AUTO_TEST_CASE(search_int)
2626
{
27-
int data[] = {1, 4, 2, 6, 3, 2, 6, 3, 4, 6};
28-
bc::vector<bc::int_> vectort(data, data + 10, queue);
27+
int data[] = {1, 4, 2, 6, 3, 2, 6, 3, 4, 6, 6};
28+
bc::vector<bc::int_> vectort(data, data + 11, queue);
2929

3030
int datap[] = {2, 6};
3131
bc::vector<bc::int_> vectorp(datap, datap + 2, queue);
@@ -34,15 +34,24 @@ BOOST_AUTO_TEST_CASE(search_int)
3434
bc::search(vectort.begin(), vectort.end(),
3535
vectorp.begin(), vectorp.end(), queue);
3636

37-
BOOST_VERIFY(iter == vectort.begin() + 2);
37+
BOOST_CHECK(iter == vectort.begin() + 2);
3838

3939
vectorp[1] = 9;
4040

4141
iter =
4242
bc::search(vectort.begin(), vectort.end(),
4343
vectorp.begin(), vectorp.end(), queue);
4444

45-
BOOST_VERIFY(iter == vectort.begin() + 10);
45+
BOOST_CHECK(iter == vectort.begin() + 11);
46+
47+
vectorp[0] = 6;
48+
vectorp[1] = 6;
49+
50+
iter =
51+
bc::search(vectort.begin(), vectort.end(),
52+
vectorp.begin(), vectorp.end(), queue);
53+
54+
BOOST_CHECK(iter == vectort.begin() + 9);
4655
}
4756

4857
BOOST_AUTO_TEST_CASE(search_string)
@@ -57,7 +66,7 @@ BOOST_AUTO_TEST_CASE(search_string)
5766
bc::search(vectort.begin(), vectort.end(),
5867
vectorp.begin(), vectorp.end(), queue);
5968

60-
BOOST_VERIFY(iter == vectort.begin() + 2);
69+
BOOST_CHECK(iter == vectort.begin() + 2);
6170
}
6271

6372
BOOST_AUTO_TEST_SUITE_END()

test/test_search_n.cpp

+10-5
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,23 @@ namespace bc = boost::compute;
2323

2424
BOOST_AUTO_TEST_CASE(search_int)
2525
{
26-
int data[] = {1, 2, 2, 2, 3, 2, 2, 2, 4, 6};
27-
bc::vector<bc::int_> vectort(data, data + 10, queue);
26+
int data[] = {1, 2, 2, 2, 3, 2, 2, 2, 4, 6, 6};
27+
bc::vector<bc::int_> vectort(data, data + 11, queue);
2828

2929
bc::vector<bc::int_>::iterator iter =
3030
bc::search_n(vectort.begin(), vectort.end(), 3, 2, queue);
3131

32-
BOOST_VERIFY(iter == vectort.begin() + 1);
32+
BOOST_CHECK(iter == vectort.begin() + 1);
3333

3434
iter =
3535
bc::search_n(vectort.begin(), vectort.end(), 5, 2, queue);
3636

37-
BOOST_VERIFY(iter == vectort.begin() + 10);
37+
BOOST_CHECK(iter == vectort.begin() + 11);
38+
39+
iter =
40+
bc::search_n(vectort.begin(), vectort.end(), 2, 6, queue);
41+
42+
BOOST_CHECK(iter == vectort.begin() + 9);
3843
}
3944

4045
BOOST_AUTO_TEST_CASE(search_string)
@@ -45,7 +50,7 @@ BOOST_AUTO_TEST_CASE(search_string)
4550
bc::vector<bc::char_>::iterator iter =
4651
bc::search_n(vectort.begin(), vectort.end(), 2, 'a', queue);
4752

48-
BOOST_VERIFY(iter == vectort.begin() + 2);
53+
BOOST_CHECK(iter == vectort.begin() + 2);
4954
}
5055

5156
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)