From 3af0142509f34de502f9c7da87e4aa918fe0ad96 Mon Sep 17 00:00:00 2001 From: Neil Groves Date: Thu, 27 Feb 2014 17:42:32 +0000 Subject: [PATCH] fixed counting_range from a range. --- include/boost/range/counting_range.hpp | 40 ++++++++++++++++---------- test/counting_range.cpp | 17 +++++++++++ 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/include/boost/range/counting_range.hpp b/include/boost/range/counting_range.hpp index b8e4e3a..d886a21 100644 --- a/include/boost/range/counting_range.hpp +++ b/include/boost/range/counting_range.hpp @@ -17,11 +17,11 @@ #include #include +#include #include namespace boost { - template inline iterator_range > counting_range(Value first, Value last) @@ -33,29 +33,39 @@ namespace boost } template - inline iterator_range::type> > + inline iterator_range< + counting_iterator< + BOOST_DEDUCED_TYPENAME range_iterator::type + > + > counting_range(const Range& rng) { - typedef counting_iterator::type> counting_iterator_t; + typedef counting_iterator< + BOOST_DEDUCED_TYPENAME range_iterator::type + > counting_iterator_t; + typedef iterator_range result_t; - return boost::empty(rng) - ? result_t() - : result_t( - counting_iterator_t(*boost::begin(rng)), - counting_iterator_t(*boost::prior(boost::end(rng)))); + + return result_t(counting_iterator_t(boost::begin(rng)), + counting_iterator_t(boost::end(rng))); } template - inline iterator_range::type> > + inline iterator_range< + counting_iterator< + BOOST_DEDUCED_TYPENAME range_iterator::type + > + > counting_range(Range& rng) { - typedef counting_iterator::type> counting_iterator_t; + typedef counting_iterator< + BOOST_DEDUCED_TYPENAME range_iterator::type + > counting_iterator_t; + typedef iterator_range result_t; - return boost::empty(rng) - ? result_t() - : result_t( - counting_iterator_t(*boost::begin(rng)), - counting_iterator_t(*boost::prior(boost::end(rng)))); + + return result_t(counting_iterator_t(boost::begin(rng)), + counting_iterator_t(boost::end(rng))); } } // namespace boost diff --git a/test/counting_range.cpp b/test/counting_range.cpp index 18aceba..d1f1d3f 100644 --- a/test/counting_range.cpp +++ b/test/counting_range.cpp @@ -11,6 +11,7 @@ // Disable a warning from since this noise might // stop us detecting a problem in our code. #include +#include #include #include @@ -51,6 +52,22 @@ namespace boost counting_range_test_impl(-100, 100); counting_range_test_impl(50, 55); } + + void counting_range_test_range() + { + std::vector v; + for (int i = 0; i < 10; ++i) + v.push_back(i); + + std::vector::iterator> x; + push_back(x, counting_range(v)); + + std::vector t; + push_back(t, x | boost::adaptors::indirected); + + BOOST_CHECK_EQUAL_COLLECTIONS(t.begin(), t.end(), + v.begin(), v.end()); + } } void counting_range_test()