permutation_iterator.hpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #ifndef BOOST_PERMUTATION_ITERATOR_HPP
  8. #define BOOST_PERMUTATION_ITERATOR_HPP
  9. #include <iterator>
  10. #include <boost/iterator/iterator_adaptor.hpp>
  11. namespace boost {
  12. namespace iterators {
  13. template< class ElementIterator
  14. , class IndexIterator>
  15. class permutation_iterator
  16. : public iterator_adaptor<
  17. permutation_iterator<ElementIterator, IndexIterator>
  18. , IndexIterator, typename std::iterator_traits<ElementIterator>::value_type
  19. , use_default, typename std::iterator_traits<ElementIterator>::reference>
  20. {
  21. typedef iterator_adaptor<
  22. permutation_iterator<ElementIterator, IndexIterator>
  23. , IndexIterator, typename std::iterator_traits<ElementIterator>::value_type
  24. , use_default, typename std::iterator_traits<ElementIterator>::reference> super_t;
  25. friend class iterator_core_access;
  26. public:
  27. permutation_iterator() : m_elt_iter() {}
  28. explicit permutation_iterator(ElementIterator x, IndexIterator y)
  29. : super_t(y), m_elt_iter(x) {}
  30. template<class OtherElementIterator, class OtherIndexIterator>
  31. permutation_iterator(
  32. permutation_iterator<OtherElementIterator, OtherIndexIterator> const& r
  33. , typename enable_if_convertible<OtherElementIterator, ElementIterator>::type* = 0
  34. , typename enable_if_convertible<OtherIndexIterator, IndexIterator>::type* = 0
  35. )
  36. : super_t(r.base()), m_elt_iter(r.m_elt_iter)
  37. {}
  38. private:
  39. typename super_t::reference dereference() const
  40. { return *(m_elt_iter + *this->base()); }
  41. #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  42. template <class,class> friend class permutation_iterator;
  43. #else
  44. public:
  45. #endif
  46. ElementIterator m_elt_iter;
  47. };
  48. template <class ElementIterator, class IndexIterator>
  49. inline permutation_iterator<ElementIterator, IndexIterator>
  50. make_permutation_iterator( ElementIterator e, IndexIterator i )
  51. {
  52. return permutation_iterator<ElementIterator, IndexIterator>( e, i );
  53. }
  54. } // namespace iterators
  55. using iterators::permutation_iterator;
  56. using iterators::make_permutation_iterator;
  57. } // namespace boost
  58. #endif