transform_iterator.hpp 5.2 KB

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