Brian Silverman | 5962333 | 2018-08-04 23:36:56 -0700 | [diff] [blame^] | 1 | // Copyright (C) 2004 Jeremy Siek <jsiek@cs.indiana.edu> |
| 2 | // Distributed under the Boost Software License, Version 1.0. (See |
| 3 | // accompanying file LICENSE_1_0.txt or copy at |
| 4 | // http://www.boost.org/LICENSE_1_0.txt) |
| 5 | |
| 6 | #include <iostream> |
| 7 | #include <vector> |
| 8 | #include <deque> |
| 9 | #include <algorithm> |
| 10 | #include <boost/iterator/permutation_iterator.hpp> |
| 11 | #include <boost/cstdlib.hpp> |
| 12 | #include <assert.h> |
| 13 | |
| 14 | |
| 15 | int main() { |
| 16 | using namespace boost; |
| 17 | int i = 0; |
| 18 | |
| 19 | typedef std::vector< int > element_range_type; |
| 20 | typedef std::deque< int > index_type; |
| 21 | |
| 22 | static const int element_range_size = 10; |
| 23 | static const int index_size = 4; |
| 24 | |
| 25 | element_range_type elements( element_range_size ); |
| 26 | for(element_range_type::iterator el_it = elements.begin() ; el_it != elements.end() ; ++el_it) |
| 27 | *el_it = std::distance(elements.begin(), el_it); |
| 28 | |
| 29 | index_type indices( index_size ); |
| 30 | for(index_type::iterator i_it = indices.begin() ; i_it != indices.end() ; ++i_it ) |
| 31 | *i_it = element_range_size - index_size + std::distance(indices.begin(), i_it); |
| 32 | std::reverse( indices.begin(), indices.end() ); |
| 33 | |
| 34 | typedef permutation_iterator< element_range_type::iterator, index_type::iterator > permutation_type; |
| 35 | permutation_type begin = make_permutation_iterator( elements.begin(), indices.begin() ); |
| 36 | permutation_type it = begin; |
| 37 | permutation_type end = make_permutation_iterator( elements.begin(), indices.end() ); |
| 38 | |
| 39 | std::cout << "The original range is : "; |
| 40 | std::copy( elements.begin(), elements.end(), std::ostream_iterator< int >( std::cout, " " ) ); |
| 41 | std::cout << "\n"; |
| 42 | |
| 43 | std::cout << "The reindexing scheme is : "; |
| 44 | std::copy( indices.begin(), indices.end(), std::ostream_iterator< int >( std::cout, " " ) ); |
| 45 | std::cout << "\n"; |
| 46 | |
| 47 | std::cout << "The permutated range is : "; |
| 48 | std::copy( begin, end, std::ostream_iterator< int >( std::cout, " " ) ); |
| 49 | std::cout << "\n"; |
| 50 | |
| 51 | std::cout << "Elements at even indices in the permutation : "; |
| 52 | it = begin; |
| 53 | for(i = 0; i < index_size / 2 ; ++i, it+=2 ) std::cout << *it << " "; |
| 54 | std::cout << "\n"; |
| 55 | |
| 56 | std::cout << "Permutation backwards : "; |
| 57 | it = begin + (index_size); |
| 58 | assert( it != begin ); |
| 59 | for( ; it-- != begin ; ) std::cout << *it << " "; |
| 60 | std::cout << "\n"; |
| 61 | |
| 62 | std::cout << "Iterate backward with stride 2 : "; |
| 63 | it = begin + (index_size - 1); |
| 64 | for(i = 0 ; i < index_size / 2 ; ++i, it-=2 ) std::cout << *it << " "; |
| 65 | std::cout << "\n"; |
| 66 | |
| 67 | return boost::exit_success; |
| 68 | } |