fixed counting_range from a range.

This commit is contained in:
Neil Groves
2014-02-27 17:42:32 +00:00
parent 19f0726533
commit 3af0142509
2 changed files with 42 additions and 15 deletions

View File

@ -17,11 +17,11 @@
#include <boost/range/iterator_range_core.hpp> #include <boost/range/iterator_range_core.hpp>
#include <boost/range/value_type.hpp> #include <boost/range/value_type.hpp>
#include <boost/range/iterator.hpp>
#include <boost/iterator/counting_iterator.hpp> #include <boost/iterator/counting_iterator.hpp>
namespace boost namespace boost
{ {
template<class Value> template<class Value>
inline iterator_range<counting_iterator<Value> > inline iterator_range<counting_iterator<Value> >
counting_range(Value first, Value last) counting_range(Value first, Value last)
@ -33,29 +33,39 @@ namespace boost
} }
template<class Range> template<class Range>
inline iterator_range<counting_iterator<BOOST_DEDUCED_TYPENAME range_value<const Range>::type> > inline iterator_range<
counting_iterator<
BOOST_DEDUCED_TYPENAME range_iterator<const Range>::type
>
>
counting_range(const Range& rng) counting_range(const Range& rng)
{ {
typedef counting_iterator<BOOST_DEDUCED_TYPENAME range_value<const Range>::type> counting_iterator_t; typedef counting_iterator<
BOOST_DEDUCED_TYPENAME range_iterator<const Range>::type
> counting_iterator_t;
typedef iterator_range<counting_iterator_t> result_t; typedef iterator_range<counting_iterator_t> result_t;
return boost::empty(rng)
? result_t() return result_t(counting_iterator_t(boost::begin(rng)),
: result_t( counting_iterator_t(boost::end(rng)));
counting_iterator_t(*boost::begin(rng)),
counting_iterator_t(*boost::prior(boost::end(rng))));
} }
template<class Range> template<class Range>
inline iterator_range<counting_iterator<BOOST_DEDUCED_TYPENAME range_value<Range>::type> > inline iterator_range<
counting_iterator<
BOOST_DEDUCED_TYPENAME range_iterator<Range>::type
>
>
counting_range(Range& rng) counting_range(Range& rng)
{ {
typedef counting_iterator<BOOST_DEDUCED_TYPENAME range_value<Range>::type> counting_iterator_t; typedef counting_iterator<
BOOST_DEDUCED_TYPENAME range_iterator<Range>::type
> counting_iterator_t;
typedef iterator_range<counting_iterator_t> result_t; typedef iterator_range<counting_iterator_t> result_t;
return boost::empty(rng)
? result_t() return result_t(counting_iterator_t(boost::begin(rng)),
: result_t( counting_iterator_t(boost::end(rng)));
counting_iterator_t(*boost::begin(rng)),
counting_iterator_t(*boost::prior(boost::end(rng))));
} }
} // namespace boost } // namespace boost

View File

@ -11,6 +11,7 @@
// Disable a warning from <xutility> since this noise might // Disable a warning from <xutility> since this noise might
// stop us detecting a problem in our code. // stop us detecting a problem in our code.
#include <boost/range/counting_range.hpp> #include <boost/range/counting_range.hpp>
#include <boost/range/adaptor/indirected.hpp>
#include <boost/test/test_tools.hpp> #include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
@ -51,6 +52,22 @@ namespace boost
counting_range_test_impl<Container>(-100, 100); counting_range_test_impl<Container>(-100, 100);
counting_range_test_impl<Container>(50, 55); counting_range_test_impl<Container>(50, 55);
} }
void counting_range_test_range()
{
std::vector<int> v;
for (int i = 0; i < 10; ++i)
v.push_back(i);
std::vector<std::vector<int>::iterator> x;
push_back(x, counting_range(v));
std::vector<int> t;
push_back(t, x | boost::adaptors::indirected);
BOOST_CHECK_EQUAL_COLLECTIONS(t.begin(), t.end(),
v.begin(), v.end());
}
} }
void counting_range_test() void counting_range_test()