| 1 | // (C) Copyright Toon Knapen 2001. |
| 2 | // (C) Copyright David Abrahams 2003. |
| 3 | // (C) Copyright Roland Richter 2003. |
| 4 | // Distributed under the Boost Software License, Version 1.0. (See |
| 5 | // accompanying file LICENSE_1_0.txt or copy at |
| 6 | // http://www.boost.org/LICENSE_1_0.txt) |
| 7 | |
| 8 | #ifndef BOOST_PERMUTATION_ITERATOR_HPP |
| 9 | #define BOOST_PERMUTATION_ITERATOR_HPP |
| 10 | |
| 11 | #include <iterator> |
| 12 | |
| 13 | #include <boost/iterator/iterator_adaptor.hpp> |
| 14 | |
| 15 | |
| 16 | namespace boost { |
| 17 | namespace iterators { |
| 18 | |
| 19 | template< class ElementIterator |
| 20 | , class IndexIterator> |
| 21 | class permutation_iterator |
| 22 | : public iterator_adaptor< |
| 23 | permutation_iterator<ElementIterator, IndexIterator> |
| 24 | , IndexIterator, typename std::iterator_traits<ElementIterator>::value_type |
| 25 | , use_default, typename std::iterator_traits<ElementIterator>::reference> |
| 26 | { |
| 27 | typedef iterator_adaptor< |
| 28 | permutation_iterator<ElementIterator, IndexIterator> |
| 29 | , IndexIterator, typename std::iterator_traits<ElementIterator>::value_type |
| 30 | , use_default, typename std::iterator_traits<ElementIterator>::reference> super_t; |
| 31 | |
| 32 | friend class iterator_core_access; |
| 33 | |
| 34 | public: |
| 35 | permutation_iterator() : m_elt_iter() {} |
| 36 | |
| 37 | explicit permutation_iterator(ElementIterator x, IndexIterator y) |
| 38 | : super_t(y), m_elt_iter(x) {} |
| 39 | |
| 40 | template<class OtherElementIterator, class OtherIndexIterator> |
| 41 | permutation_iterator( |
| 42 | permutation_iterator<OtherElementIterator, OtherIndexIterator> const& r |
| 43 | , typename enable_if_convertible<OtherElementIterator, ElementIterator>::type* = 0 |
| 44 | , typename enable_if_convertible<OtherIndexIterator, IndexIterator>::type* = 0 |
| 45 | ) |
| 46 | : super_t(r.base()), m_elt_iter(r.m_elt_iter) |
| 47 | {} |
| 48 | |
| 49 | private: |
| 50 | typename super_t::reference dereference() const |
| 51 | { return *(m_elt_iter + *this->base()); } |
| 52 | |
| 53 | #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS |
| 54 | template <class,class> friend class permutation_iterator; |
| 55 | #else |
| 56 | public: |
| 57 | #endif |
| 58 | ElementIterator m_elt_iter; |
| 59 | }; |
| 60 | |
| 61 | |
| 62 | template <class ElementIterator, class IndexIterator> |
| 63 | inline permutation_iterator<ElementIterator, IndexIterator> |
| 64 | make_permutation_iterator( ElementIterator e, IndexIterator i ) |
| 65 | { |
| 66 | return permutation_iterator<ElementIterator, IndexIterator>( e, i ); |
| 67 | } |
| 68 | |
| 69 | } // namespace iterators |
| 70 | |
| 71 | using iterators::permutation_iterator; |
| 72 | using iterators::make_permutation_iterator; |
| 73 | |
| 74 | } // namespace boost |
| 75 | |
| 76 | #endif |
| 77 | |