irange: introduce one-parameter variant of irange

this simplifies the use of boost::irange, as boost::irange( 0, foo );
may compile or not depending on the platform-specific integer type of
foo, requiring an explicit cast of 0 to decltype(foo).
This commit is contained in:
Tim Blechmann
2016-01-17 11:40:55 +01:00
parent 5a37aa4e98
commit bb43887430
3 changed files with 59 additions and 11 deletions

View File

@ -10,7 +10,11 @@
`` ``
template<class Integer> template<class Integer>
iterator_range< range_detail::integer_iterator<Integer> > iterator_range< range_detail::integer_iterator<Integer> >
irange(Integer first, Integer last); irange(Integer last);
template<class Integer>
iterator_range< range_detail::integer_iterator<Integer> >
irange(Integer first, Integer last);
template<class Integer, class StepSize> template<class Integer, class StepSize>
iterator_range< range_detail::integer_iterator_with_step<Integer, StepSize> > iterator_range< range_detail::integer_iterator_with_step<Integer, StepSize> >
@ -37,4 +41,3 @@ Defined in the header file `boost/range/irange.hpp`
Constant. Since this function generates a new range the most significant performance cost is incurred through the iteration of the generated range. Constant. Since this function generates a new range the most significant performance cost is incurred through the iteration of the generated range.
[endsect] [endsect]

View File

@ -231,6 +231,13 @@ namespace boost
iterator_t(first, num_steps, step_size)); iterator_t(first, num_steps, step_size));
} }
template<typename Integer>
integer_range<Integer>
irange(Integer last)
{
return integer_range<Integer>(static_cast<Integer>(0), last);
}
} // namespace boost } // namespace boost
#endif // include guard #endif // include guard

View File

@ -18,6 +18,23 @@
namespace boost namespace boost
{ {
// Test an integer range with a step size of 1.
template<typename Integer>
void test_irange_impl(Integer last)
{
std::vector<Integer> reference;
for (Integer i = static_cast<Integer>(0); i < last; ++i)
{
reference.push_back(i);
}
std::vector<Integer> test;
boost::push_back(test, boost::irange(last));
BOOST_CHECK_EQUAL_COLLECTIONS( test.begin(), test.end(),
reference.begin(), reference.end() );
}
// Test an integer range with a step size of 1. // Test an integer range with a step size of 1.
template<typename Integer> template<typename Integer>
void test_irange_impl(Integer first, Integer last) void test_irange_impl(Integer first, Integer last)
@ -64,6 +81,22 @@ namespace boost
reference.begin(), reference.end() ); reference.begin(), reference.end() );
} }
// Test driver function that for an integer range [first, last)
// drives the test implementation through various integer
// types.
void test_irange(int last)
{
test_irange_impl<signed char>(last);
test_irange_impl<unsigned char>(last);
test_irange_impl<signed short>(last);
test_irange_impl<unsigned short>(last);
test_irange_impl<signed int>(last);
test_irange_impl<unsigned int>(last);
test_irange_impl<signed long>(last);
test_irange_impl<unsigned long>(last);
}
// Test driver function that for an integer range [first, last) // Test driver function that for an integer range [first, last)
// drives the test implementation through various integer // drives the test implementation through various integer
// types. // types.
@ -102,6 +135,11 @@ namespace boost
// number of implementation branches. // number of implementation branches.
void irange_unit_test() void irange_unit_test()
{ {
// Test the single-step version of irange(last)
test_irange(0);
test_irange(1);
test_irange(10);
// Test the single-step version of irange(first, last) // Test the single-step version of irange(first, last)
test_irange(0, 0); test_irange(0, 0);
test_irange(0, 1); test_irange(0, 1);