transform_iterator.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2013.
  4. // (C) Copyright Gennaro Prota 2003 - 2004.
  5. //
  6. // Distributed under the Boost Software License, Version 1.0.
  7. // (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // See http://www.boost.org/libs/container for documentation.
  11. //
  12. //////////////////////////////////////////////////////////////////////////////
  13. #ifndef BOOST_CONTAINER_DETAIL_TRANSFORM_ITERATORS_HPP
  14. #define BOOST_CONTAINER_DETAIL_TRANSFORM_ITERATORS_HPP
  15. #ifndef BOOST_CONFIG_HPP
  16. # include <boost/config.hpp>
  17. #endif
  18. #if defined(BOOST_HAS_PRAGMA_ONCE)
  19. # pragma once
  20. #endif
  21. #include <boost/container/detail/config_begin.hpp>
  22. #include <boost/container/detail/workaround.hpp>
  23. #include <boost/container/detail/type_traits.hpp>
  24. #include <boost/container/detail/iterator.hpp>
  25. namespace boost {
  26. namespace container {
  27. template <class PseudoReference>
  28. struct operator_arrow_proxy
  29. {
  30. operator_arrow_proxy(const PseudoReference &px)
  31. : m_value(px)
  32. {}
  33. typedef PseudoReference element_type;
  34. PseudoReference* operator->() const { return &m_value; }
  35. mutable PseudoReference m_value;
  36. };
  37. template <class T>
  38. struct operator_arrow_proxy<T&>
  39. {
  40. operator_arrow_proxy(T &px)
  41. : m_value(px)
  42. {}
  43. typedef T element_type;
  44. T* operator->() const { return const_cast<T*>(&m_value); }
  45. T &m_value;
  46. };
  47. template <class Iterator, class UnaryFunction>
  48. class transform_iterator
  49. : public UnaryFunction
  50. , public boost::container::iterator
  51. < typename Iterator::iterator_category
  52. , typename dtl::remove_reference<typename UnaryFunction::result_type>::type
  53. , typename Iterator::difference_type
  54. , operator_arrow_proxy<typename UnaryFunction::result_type>
  55. , typename UnaryFunction::result_type>
  56. {
  57. public:
  58. explicit transform_iterator(const Iterator &it, const UnaryFunction &f = UnaryFunction())
  59. : UnaryFunction(f), m_it(it)
  60. {}
  61. explicit transform_iterator()
  62. : UnaryFunction(), m_it()
  63. {}
  64. //Constructors
  65. transform_iterator& operator++()
  66. { increment(); return *this; }
  67. transform_iterator operator++(int)
  68. {
  69. transform_iterator result (*this);
  70. increment();
  71. return result;
  72. }
  73. friend bool operator== (const transform_iterator& i, const transform_iterator& i2)
  74. { return i.equal(i2); }
  75. friend bool operator!= (const transform_iterator& i, const transform_iterator& i2)
  76. { return !(i == i2); }
  77. /*
  78. friend bool operator> (const transform_iterator& i, const transform_iterator& i2)
  79. { return i2 < i; }
  80. friend bool operator<= (const transform_iterator& i, const transform_iterator& i2)
  81. { return !(i > i2); }
  82. friend bool operator>= (const transform_iterator& i, const transform_iterator& i2)
  83. { return !(i < i2); }
  84. */
  85. friend typename Iterator::difference_type operator- (const transform_iterator& i, const transform_iterator& i2)
  86. { return i2.distance_to(i); }
  87. //Arithmetic
  88. transform_iterator& operator+=(typename Iterator::difference_type off)
  89. { this->advance(off); return *this; }
  90. transform_iterator operator+(typename Iterator::difference_type off) const
  91. {
  92. transform_iterator other(*this);
  93. other.advance(off);
  94. return other;
  95. }
  96. friend transform_iterator operator+(typename Iterator::difference_type off, const transform_iterator& right)
  97. { return right + off; }
  98. transform_iterator& operator-=(typename Iterator::difference_type off)
  99. { this->advance(-off); return *this; }
  100. transform_iterator operator-(typename Iterator::difference_type off) const
  101. { return *this + (-off); }
  102. typename UnaryFunction::result_type operator*() const
  103. { return dereference(); }
  104. operator_arrow_proxy<typename UnaryFunction::result_type>
  105. operator->() const
  106. { return operator_arrow_proxy<typename UnaryFunction::result_type>(dereference()); }
  107. Iterator & base()
  108. { return m_it; }
  109. const Iterator & base() const
  110. { return m_it; }
  111. private:
  112. Iterator m_it;
  113. void increment()
  114. { ++m_it; }
  115. void decrement()
  116. { --m_it; }
  117. bool equal(const transform_iterator &other) const
  118. { return m_it == other.m_it; }
  119. bool less(const transform_iterator &other) const
  120. { return other.m_it < m_it; }
  121. typename UnaryFunction::result_type dereference() const
  122. { return UnaryFunction::operator()(*m_it); }
  123. void advance(typename Iterator::difference_type n)
  124. { boost::container::iterator_advance(m_it, n); }
  125. typename Iterator::difference_type distance_to(const transform_iterator &other)const
  126. { return boost::container::iterator_distance(other.m_it, m_it); }
  127. };
  128. template <class Iterator, class UnaryFunc>
  129. transform_iterator<Iterator, UnaryFunc>
  130. make_transform_iterator(Iterator it, UnaryFunc fun)
  131. {
  132. return transform_iterator<Iterator, UnaryFunc>(it, fun);
  133. }
  134. } //namespace container {
  135. } //namespace boost {
  136. #include <boost/container/detail/config_end.hpp>
  137. #endif //#ifndef BOOST_CONTAINER_DETAIL_TRANSFORM_ITERATORS_HPP