File: indexed_example.cpp

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (104 lines) | stat: -rw-r--r-- 2,500 bytes parent folder | download | duplicates (15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Boost.Range library
//
//  Copyright Neil Groves 2009. 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)
//
//
// For more information, see http://www.boost.org/libs/range/
//
//[indexed_example

//<-
#include <boost/config.hpp>
//->

#include <boost/range/adaptor/indexed.hpp>
#include <boost/assign.hpp>
#include <iterator>
#include <iostream>
#include <vector>

//<-
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>

namespace 
{

template<class Iterator1, class Iterator2>
void check_element_and_index(
        Iterator1 test_first,
        Iterator1 test_last,
        Iterator2 reference_first,
        Iterator2 reference_last)
{
    BOOST_CHECK_EQUAL( std::distance(test_first, test_last),
                       std::distance(reference_first, reference_last) );

    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->value(), *reference_it);
        BOOST_CHECK_EQUAL(test_it->index(), reference_index);
    }
}

template<class SinglePassRange1, class SinglePassRange2>
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),
        boost::begin(reference_rng), boost::end(reference_rng));
}
//->

//<-
void indexed_example_test()
//->
//=int main(int argc, const char* argv[])
{
    using namespace boost::assign;
    using namespace boost::adaptors;

    std::vector<int> input;
    input += 10,20,30,40,50,60,70,80,90;

//<-
#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;
//=}
//]
    check_element_and_index(
        input | indexed(0),
        input);
}
}

boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
    boost::unit_test::test_suite* test
        = BOOST_TEST_SUITE( "RangeTestSuite.adaptor.indexed_example" );

    test->add( BOOST_TEST_CASE( &indexed_example_test ) );

    return test;
}