Skip to content

Commit 3af7aca

Browse files
committed
fix constexpr-ness of a couple of algorithhms - and tests. Based on Pull Request boostorg#44 by Flast - thanks!
1 parent 509201f commit 3af7aca

File tree

7 files changed

+52
-47
lines changed

7 files changed

+52
-47
lines changed

include/boost/algorithm/cxx11/one_of.hpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#ifndef BOOST_ALGORITHM_ONE_OF_HPP
1313
#define BOOST_ALGORITHM_ONE_OF_HPP
1414

15-
#include <algorithm> // for std::find and std::find_if
1615
#include <boost/algorithm/cxx11/none_of.hpp>
1716

1817
#include <boost/range/begin.hpp>
@@ -30,10 +29,14 @@ namespace boost { namespace algorithm {
3029
template<typename InputIterator, typename Predicate>
3130
BOOST_CXX14_CONSTEXPR bool one_of ( InputIterator first, InputIterator last, Predicate p )
3231
{
33-
InputIterator i = std::find_if (first, last, p);
34-
if (i == last)
32+
// find_if
33+
for (; first != last; ++first)
34+
if (p(*first))
35+
break;
36+
37+
if (first == last)
3538
return false; // Didn't occur at all
36-
return boost::algorithm::none_of (++i, last, p);
39+
return boost::algorithm::none_of (++first, last, p);
3740
}
3841

3942
/// \fn one_of ( const Range &r, Predicate p )
@@ -43,7 +46,7 @@ BOOST_CXX14_CONSTEXPR bool one_of ( InputIterator first, InputIterator last, Pre
4346
/// \param p A predicate for testing the elements of the range
4447
///
4548
template<typename Range, typename Predicate>
46-
BOOST_CXX14_CONSTEXPR bool one_of ( const Range &r, Predicate p )
49+
BOOST_CXX14_CONSTEXPR bool one_of ( const Range &r, Predicate p )
4750
{
4851
return boost::algorithm::one_of ( boost::begin (r), boost::end (r), p );
4952
}
@@ -57,12 +60,16 @@ BOOST_CXX14_CONSTEXPR bool one_of ( const Range &r, Predicate p )
5760
/// \param val A value to compare against
5861
///
5962
template<typename InputIterator, typename V>
60-
bool one_of_equal ( InputIterator first, InputIterator last, const V &val )
63+
BOOST_CXX14_CONSTEXPR bool one_of_equal ( InputIterator first, InputIterator last, const V &val )
6164
{
62-
InputIterator i = std::find (first, last, val); // find first occurrence of 'val'
63-
if (i == last)
65+
// find
66+
for (; first != last; ++first)
67+
if (*first == val)
68+
break;
69+
70+
if (first == last)
6471
return false; // Didn't occur at all
65-
return boost::algorithm::none_of_equal (++i, last, val);
72+
return boost::algorithm::none_of_equal (++first, last, val);
6673
}
6774

6875
/// \fn one_of_equal ( const Range &r, const V &val )
@@ -72,7 +79,7 @@ bool one_of_equal ( InputIterator first, InputIterator last, const V &val )
7279
/// \param val A value to compare against
7380
///
7481
template<typename Range, typename V>
75-
bool one_of_equal ( const Range &r, const V &val )
82+
BOOST_CXX14_CONSTEXPR bool one_of_equal ( const Range &r, const V &val )
7683
{
7784
return boost::algorithm::one_of_equal ( boost::begin (r), boost::end (r), val );
7885
}

include/boost/algorithm/cxx14/equal.hpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,8 @@
1212
#ifndef BOOST_ALGORITHM_EQUAL_HPP
1313
#define BOOST_ALGORITHM_EQUAL_HPP
1414

15-
#include <algorithm> // for std::equal
1615
#include <iterator>
1716

18-
#ifdef __cpp_lib_array_constexpr // or cpp17 compiler
19-
// if compiled according to C++17 standard or std functions are constexpr
20-
#define BOOST_CONSTEXPR_IF_STD_CONSTEXPR constexpr
21-
#else
22-
#define BOOST_CONSTEXPR_IF_STD_CONSTEXPR
23-
#endif
24-
2517
namespace boost { namespace algorithm {
2618

2719
namespace detail {
@@ -32,20 +24,24 @@ namespace detail {
3224
};
3325

3426
template <class RandomAccessIterator1, class RandomAccessIterator2, class BinaryPredicate>
35-
BOOST_CONSTEXPR_IF_STD_CONSTEXPR
27+
BOOST_CXX14_CONSTEXPR
3628
bool equal ( RandomAccessIterator1 first1, RandomAccessIterator1 last1,
3729
RandomAccessIterator2 first2, RandomAccessIterator2 last2, BinaryPredicate pred,
3830
std::random_access_iterator_tag, std::random_access_iterator_tag )
3931
{
4032
// Random-access iterators let is check the sizes in constant time
4133
if ( std::distance ( first1, last1 ) != std::distance ( first2, last2 ))
4234
return false;
43-
// If we know that the sequences are the same size, the original version is fine
44-
return std::equal ( first1, last1, first2, pred );
35+
36+
// std::equal
37+
for (; first1 != last1; ++first1, ++first2)
38+
if (!pred(*first1, *first2))
39+
return false;
40+
return true;
4541
}
4642

4743
template <class InputIterator1, class InputIterator2, class BinaryPredicate>
48-
BOOST_CXX14_CONSTEXPR
44+
BOOST_CXX14_CONSTEXPR
4945
bool equal ( InputIterator1 first1, InputIterator1 last1,
5046
InputIterator2 first2, InputIterator2 last2, BinaryPredicate pred,
5147
std::input_iterator_tag, std::input_iterator_tag )

test/all_of_test.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,13 @@ void test_all ()
7777
BOOST_CHECK ( ba::all_of_equal ( li.begin(), l_iter, 1 ));
7878
BOOST_CHECK ( ba::all_of ( li.begin(), l_iter, is_<int> ( 1 )));
7979

80-
BOOST_CXX14_CONSTEXPR bool constexpr_res = (
81-
!ba::all_of_equal ( some_numbers, 1 )
82-
&& !ba::all_of ( some_numbers, is_<int> ( 1 ))
83-
&& ba::all_of_equal ( some_numbers, some_numbers + 3, 1 )
84-
&& ba::all_of ( some_numbers, some_numbers + 3, is_<int> ( 1 ))
85-
);
80+
BOOST_CXX14_CONSTEXPR bool constexpr_res =
81+
!ba::all_of_equal ( some_numbers, 1 ) &&
82+
!ba::all_of ( some_numbers, is_<int> ( 1 )) &&
83+
ba::all_of_equal ( some_numbers, some_numbers + 3, 1 ) &&
84+
ba::all_of ( some_numbers, some_numbers + 3, is_<int> ( 1 )) &&
85+
true;
86+
8687
BOOST_CHECK ( constexpr_res );
8788
}
8889

test/any_of_test.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,13 @@ void test_any ()
9797
BOOST_CHECK (!ba::any_of_equal ( li.begin(), l_iter, 18 ));
9898
BOOST_CHECK (!ba::any_of ( li.begin(), l_iter, is_<int> ( 18 )));
9999

100-
101-
BOOST_CXX14_CONSTEXPR bool constexpr_res = (
102-
ba::any_of_equal ( some_numbers, 1 )
103-
&& ba::any_of ( some_numbers, is_<int> ( 1 ))
104-
&& !ba::any_of_equal ( some_numbers, some_numbers + 3, 777 )
105-
&& !ba::any_of ( some_numbers, some_numbers + 3, is_<int> ( 777 ))
106-
);
100+
BOOST_CXX14_CONSTEXPR bool constexpr_res =
101+
ba::any_of_equal ( some_numbers, 1 ) &&
102+
ba::any_of ( some_numbers, is_<int> ( 1 )) &&
103+
!ba::any_of_equal ( some_numbers, some_numbers + 3, 777 ) &&
104+
!ba::any_of ( some_numbers, some_numbers + 3, is_<int> ( 777 )) &&
105+
true;
106+
107107
BOOST_CHECK ( constexpr_res );
108108
}
109109

test/clamp_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ void test_constexpr()
306306
BOOST_CHECK(check_max_out);
307307
}
308308
{
309-
short foo = 50;
309+
BOOST_CXX14_CONSTEXPR short foo = 50;
310310
BOOST_CXX14_CONSTEXPR bool check_float = ( 56 == ba::clamp ( foo, 56.9, 129 ));
311311
BOOST_CHECK(check_float);
312312
BOOST_CXX14_CONSTEXPR bool check_over = ( 24910 == ba::clamp ( foo, 12345678, 123456999 ));

test/none_of_test.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,14 @@ void test_none()
8888
BOOST_CHECK ( ba::none_of_equal ( li.begin(), l_iter, 18 ));
8989
BOOST_CHECK ( ba::none_of ( li.begin(), l_iter, is_<int> ( 18 )));
9090
BOOST_CHECK (!ba::none_of ( li.begin(), l_iter, is_<int> ( 5 )));
91-
92-
BOOST_CXX14_CONSTEXPR bool constexpr_res = (
93-
!ba::none_of_equal ( some_numbers, 1 )
94-
&& !ba::none_of ( some_numbers, is_<int> ( 1 ))
95-
&& ba::none_of_equal ( some_numbers, some_numbers + 3, 100 )
96-
&& ba::none_of ( some_numbers, some_numbers + 3, is_<int> ( 100 ))
97-
);
91+
92+
BOOST_CXX14_CONSTEXPR bool constexpr_res =
93+
!ba::none_of_equal ( some_numbers, 1 ) &&
94+
!ba::none_of ( some_numbers, is_<int> ( 1 )) &&
95+
ba::none_of_equal ( some_numbers, some_numbers + 3, 100 ) &&
96+
ba::none_of ( some_numbers, some_numbers + 3, is_<int> ( 100 )) &&
97+
true;
98+
9899
BOOST_CHECK ( constexpr_res );
99100
}
100101

test/one_of_test.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ void test_one ()
9292
BOOST_CHECK (!ba::one_of_equal ( li.begin(), l_iter, 3 ));
9393
BOOST_CHECK (!ba::one_of ( li.begin(), l_iter, is_<int> ( 3 )));
9494

95-
BOOST_CXX14_CONSTEXPR bool constexpr_res = (
96-
!ba::one_of ( some_numbers, is_<int> ( 6 ))
97-
&& ba::one_of ( some_numbers + 1, some_numbers + 3, is_<int> ( 1 ))
98-
);
95+
BOOST_CXX14_CONSTEXPR bool constexpr_res =
96+
!ba::one_of ( some_numbers, is_<int> ( 6 )) &&
97+
ba::one_of ( some_numbers + 1, some_numbers + 3, is_<int> ( 1 )) &&
98+
true;
9999

100100
BOOST_CHECK ( constexpr_res );
101101
}

0 commit comments

Comments
 (0)