• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. Copyright David Abrahams 2006. Distributed under the Boost
2.. Software License, Version 1.0. (See accompanying
3.. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
4
5::
6
7    using namespace boost;
8    int i = 0;
9
10    typedef std::vector< int > element_range_type;
11    typedef std::list< int > index_type;
12
13    static const int element_range_size = 10;
14    static const int index_size = 4;
15
16    element_range_type elements( element_range_size );
17    for(element_range_type::iterator el_it = elements.begin() ; el_it != elements.end() ; ++el_it)
18      *el_it = std::distance(elements.begin(), el_it);
19
20    index_type indices( index_size );
21    for(index_type::iterator i_it = indices.begin() ; i_it != indices.end() ; ++i_it )
22      *i_it = element_range_size - index_size + std::distance(indices.begin(), i_it);
23    std::reverse( indices.begin(), indices.end() );
24
25    typedef permutation_iterator< element_range_type::iterator, index_type::iterator > permutation_type;
26    permutation_type begin = make_permutation_iterator( elements.begin(), indices.begin() );
27    permutation_type it = begin;
28    permutation_type end = make_permutation_iterator( elements.begin(), indices.end() );
29
30    std::cout << "The original range is : ";
31    std::copy( elements.begin(), elements.end(), std::ostream_iterator< int >( std::cout, " " ) );
32    std::cout << "\n";
33
34    std::cout << "The reindexing scheme is : ";
35    std::copy( indices.begin(), indices.end(), std::ostream_iterator< int >( std::cout, " " ) );
36    std::cout << "\n";
37
38    std::cout << "The permutated range is : ";
39    std::copy( begin, end, std::ostream_iterator< int >( std::cout, " " ) );
40    std::cout << "\n";
41
42    std::cout << "Elements at even indices in the permutation : ";
43    it = begin;
44    for(i = 0; i < index_size / 2 ; ++i, it+=2 ) std::cout << *it << " ";
45    std::cout << "\n";
46
47    std::cout << "Permutation backwards : ";
48    it = begin + (index_size);
49    assert( it != begin );
50    for( ; it-- != begin ; ) std::cout << *it << " ";
51    std::cout << "\n";
52
53    std::cout << "Iterate backward with stride 2 : ";
54    it = begin + (index_size - 1);
55    for(i = 0 ; i < index_size / 2 ; ++i, it-=2 ) std::cout << *it << " ";
56    std::cout << "\n";
57
58
59The output is::
60
61    The original range is : 0 1 2 3 4 5 6 7 8 9
62    The reindexing scheme is : 9 8 7 6
63    The permutated range is : 9 8 7 6
64    Elements at even indices in the permutation : 9 7
65    Permutation backwards : 6 7 8 9
66    Iterate backward with stride 2 : 6 8
67
68
69The source code for this example can be found `here`__.
70
71__ ../example/permutation_iter_example.cpp
72