forked from boostorg/range
change the return type from the indexed adaptor so that the index is available from the range element.
This commit is contained in:
1 parent
344f578934
commit
5ce6dc7814
5 files changed
+545
-223
No files matched your search
+111
-64
@@ -1,6 +1,6 @@
|
||||
// Boost.Range library
|
||||
//
|
||||
// Copyright Neil Groves 2009. Use, modification and
|
||||
// Copyright Neil Groves 2014. Use, modification and
|
||||
// distribution is subject to the Boost Software License, Version
|
||||
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
@@ -14,6 +14,7 @@
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
#include <boost/assign.hpp>
|
||||
#include <boost/foreach.hpp>
|
||||
#include <boost/range/algorithm_ext.hpp>
|
||||
#include <boost/range/concepts.hpp>
|
||||
|
||||
@@ -23,83 +24,129 @@
|
||||
|
||||
#include "../test_utils.hpp"
|
||||
|
||||
namespace boost
|
||||
namespace boost_range_test
|
||||
{
|
||||
namespace
|
||||
{
|
||||
template< class Container >
|
||||
void indexed_test_impl( Container& c )
|
||||
{
|
||||
using namespace boost::adaptors;
|
||||
|
||||
typedef BOOST_DEDUCED_TYPENAME Container::value_type value_t;
|
||||
template<typename Container, typename AdaptedRange>
|
||||
void check_result(
|
||||
const Container& reference_range,
|
||||
const AdaptedRange& adapted_range,
|
||||
std::ptrdiff_t start_index
|
||||
)
|
||||
{
|
||||
typedef typename boost::range_iterator<const Container>::type
|
||||
reference_iterator;
|
||||
|
||||
// This is my preferred syntax using the | operator.
|
||||
std::vector< value_t > test_result1;
|
||||
boost::push_back(test_result1, c | indexed(0));
|
||||
typedef typename boost::range_iterator<const AdaptedRange>::type
|
||||
adapted_iterator;
|
||||
|
||||
// This is an alternative syntax preferred by some.
|
||||
std::vector< value_t > test_result2;
|
||||
boost::push_back(test_result2, adaptors::index(c, 0));
|
||||
BOOST_REQUIRE_EQUAL(boost::size(reference_range),
|
||||
boost::size(adapted_range));
|
||||
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS( c.begin(), c.end(),
|
||||
test_result1.begin(), test_result1.end() );
|
||||
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS( c.begin(), c.end(),
|
||||
test_result2.begin(), test_result2.end() );
|
||||
|
||||
boost::indexed_range< Container > test_result3
|
||||
= c | indexed(0);
|
||||
|
||||
typedef BOOST_DEDUCED_TYPENAME boost::range_const_iterator<
|
||||
boost::indexed_range< Container > >::type iter_t;
|
||||
|
||||
iter_t it = test_result3.begin();
|
||||
for (std::size_t i = 0, count = c.size(); i < count; ++i)
|
||||
{
|
||||
BOOST_CHECK_EQUAL( i, static_cast<std::size_t>(it.index()) );
|
||||
++it;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
template< class Container >
|
||||
void indexed_test_impl()
|
||||
{
|
||||
using namespace boost::assign;
|
||||
|
||||
Container c;
|
||||
|
||||
// test empty container
|
||||
indexed_test_impl(c);
|
||||
|
||||
// test one element
|
||||
c += 1;
|
||||
indexed_test_impl(c);
|
||||
|
||||
// test many elements
|
||||
c += 1,2,2,2,3,4,4,4,4,5,6,7,8,9,9;
|
||||
indexed_test_impl(c);
|
||||
}
|
||||
|
||||
void indexed_test()
|
||||
{
|
||||
indexed_test_impl< std::vector< int > >();
|
||||
indexed_test_impl< std::list< int > >();
|
||||
|
||||
check_random_access_range_concept(std::vector<int>() | boost::adaptors::indexed(0));
|
||||
check_bidirectional_range_concept(std::list<int>() | boost::adaptors::indexed(0));
|
||||
}
|
||||
reference_iterator reference_it = boost::begin(reference_range);
|
||||
adapted_iterator adapted_it = boost::begin(adapted_range);
|
||||
for (std::ptrdiff_t i = start_index;
|
||||
reference_it != boost::end(reference_range);
|
||||
++reference_it, ++adapted_it, ++i)
|
||||
{
|
||||
BOOST_CHECK_EQUAL(i, adapted_it->index());
|
||||
BOOST_CHECK_EQUAL(*reference_it, adapted_it->value());
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Container>
|
||||
void indexed_test_impl(Container& c, std::ptrdiff_t start_index)
|
||||
{
|
||||
// This is my preferred syntax using the | operator.
|
||||
check_result(c, c | boost::adaptors::indexed(), 0);
|
||||
check_result(c, c | boost::adaptors::indexed(start_index), start_index);
|
||||
|
||||
// This is the function syntax
|
||||
check_result(c, boost::adaptors::index(c), 0);
|
||||
check_result(c, boost::adaptors::index(c, start_index), start_index);
|
||||
}
|
||||
|
||||
template<typename Container>
|
||||
void indexed_test_impl(Container& c)
|
||||
{
|
||||
indexed_test_impl(c, 0);
|
||||
indexed_test_impl(c, -1);
|
||||
indexed_test_impl(c, 4);
|
||||
}
|
||||
|
||||
template<typename Container>
|
||||
void indexed_test_impl()
|
||||
{
|
||||
using namespace boost::assign;
|
||||
|
||||
Container c;
|
||||
|
||||
// test empty container
|
||||
indexed_test_impl(c);
|
||||
|
||||
// test one element
|
||||
c += 1;
|
||||
indexed_test_impl(c);
|
||||
|
||||
// test many elements
|
||||
c += 1,2,2,2,3,4,4,4,4,5,6,7,8,9,9;
|
||||
indexed_test_impl(c);
|
||||
}
|
||||
|
||||
template<typename Traversal, typename Range>
|
||||
void check_traversal(const Range& rng)
|
||||
{
|
||||
BOOST_STATIC_ASSERT(
|
||||
boost::is_convertible<
|
||||
typename boost::range_traversal<const Range>::type,
|
||||
Traversal
|
||||
>::value);
|
||||
}
|
||||
|
||||
template<typename Traversal, typename Range>
|
||||
void check_not_traversal(const Range& rng)
|
||||
{
|
||||
BOOST_STATIC_ASSERT(
|
||||
!boost::is_convertible<
|
||||
typename boost::range_traversal<const Range>::type,
|
||||
Traversal
|
||||
>::value);
|
||||
}
|
||||
|
||||
void indexed_test()
|
||||
{
|
||||
indexed_test_impl< std::vector< int > >();
|
||||
indexed_test_impl< std::list< int > >();
|
||||
|
||||
std::vector<int> vi;
|
||||
|
||||
check_traversal<boost::random_access_traversal_tag>(
|
||||
vi | boost::adaptors::indexed());
|
||||
|
||||
std::list<int> li;
|
||||
|
||||
check_traversal<boost::forward_traversal_tag>(
|
||||
li | boost::adaptors::indexed());
|
||||
|
||||
check_not_traversal<boost::bidirectional_traversal_tag>(
|
||||
li | boost::adaptors::indexed());
|
||||
|
||||
check_not_traversal<boost::random_access_traversal_tag>(
|
||||
li | boost::adaptors::indexed());
|
||||
}
|
||||
|
||||
} // anonymous namesapce
|
||||
} // namespace boost_range_test
|
||||
|
||||
boost::unit_test::test_suite*
|
||||
init_unit_test_suite(int argc, char* argv[])
|
||||
init_unit_test_suite(int, char*[])
|
||||
{
|
||||
boost::unit_test::test_suite* test
|
||||
= BOOST_TEST_SUITE( "RangeTestSuite.adaptor.indexed" );
|
||||
= BOOST_TEST_SUITE( "Boost.Range indexed adaptor test suite" );
|
||||
|
||||
test->add( BOOST_TEST_CASE( &boost::indexed_test ) );
|
||||
test->add(BOOST_TEST_CASE(&boost_range_test::indexed_test));
|
||||
|
||||
return test;
|
||||
}
|
||||
@@ -9,8 +9,12 @@
|
||||
// For more information, see http://www.boost.org/libs/range/
|
||||
//
|
||||
//[indexed_example
|
||||
|
||||
//<-
|
||||
#include <boost/config.hpp>
|
||||
//->
|
||||
|
||||
#include <boost/range/adaptor/indexed.hpp>
|
||||
#include <boost/range/algorithm/copy.hpp>
|
||||
#include <boost/assign.hpp>
|
||||
#include <iterator>
|
||||
#include <iostream>
|
||||
@@ -20,8 +24,6 @@
|
||||
#include <boost/test/test_tools.hpp>
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
#include <boost/range/algorithm_ext/push_back.hpp>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
@@ -35,14 +37,14 @@ void check_element_and_index(
|
||||
BOOST_CHECK_EQUAL( std::distance(test_first, test_last),
|
||||
std::distance(reference_first, reference_last) );
|
||||
|
||||
int reference_index = 0;
|
||||
std::ptrdiff_t reference_index = 0;
|
||||
|
||||
Iterator1 test_it = test_first;
|
||||
Iterator2 reference_it = reference_first;
|
||||
for (; test_it != test_last; ++test_it, ++reference_it, ++reference_index)
|
||||
{
|
||||
BOOST_CHECK_EQUAL( *test_it, *reference_it );
|
||||
BOOST_CHECK_EQUAL( test_it.index(), reference_index );
|
||||
BOOST_CHECK_EQUAL(test_it->value(), *reference_it);
|
||||
BOOST_CHECK_EQUAL(test_it->index(), reference_index);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,24 +53,11 @@ void check_element_and_index(
|
||||
const SinglePassRange1& test_rng,
|
||||
const SinglePassRange2& reference_rng)
|
||||
{
|
||||
check_element_and_index(boost::begin(test_rng), boost::end(test_rng),
|
||||
check_element_and_index(
|
||||
boost::begin(test_rng), boost::end(test_rng),
|
||||
boost::begin(reference_rng), boost::end(reference_rng));
|
||||
}
|
||||
//->
|
||||
template<class Iterator>
|
||||
void display_element_and_index(Iterator first, Iterator last)
|
||||
{
|
||||
for (Iterator it = first; it != last; ++it)
|
||||
{
|
||||
std::cout << "Element = " << *it << " Index = " << it.index() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
template<class SinglePassRange>
|
||||
void display_element_and_index(const SinglePassRange& rng)
|
||||
{
|
||||
display_element_and_index(boost::begin(rng), boost::end(rng));
|
||||
}
|
||||
//->
|
||||
|
||||
//<-
|
||||
void indexed_example_test()
|
||||
@@ -81,8 +70,19 @@ void indexed_example_test()
|
||||
std::vector<int> input;
|
||||
input += 10,20,30,40,50,60,70,80,90;
|
||||
|
||||
display_element_and_index( input | indexed(0) );
|
||||
|
||||
//<-
|
||||
#ifndef BOOST_NO_CXX11_RANGE_BASED_FOR
|
||||
//->
|
||||
for (const auto& element : input | indexed(0))
|
||||
{
|
||||
std::cout << "Element = " << element.value()
|
||||
<< " Index = " << element.index()
|
||||
<< std::endl;
|
||||
}
|
||||
//<-
|
||||
#endif // C++11 has range for loop
|
||||
//->
|
||||
|
||||
//= return 0;
|
||||
//=}
|
||||
//]
|
||||
|
||||
Reference in new issue
Block a user