shadow_iterator.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // (C) Copyright Jeremy Siek 2001.
  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. #ifndef BOOST_SHADOW_ITERATOR_HPP
  6. #define BOOST_SHADOW_ITERATOR_HPP
  7. #include <boost/iterator_adaptors.hpp>
  8. #include <boost/operators.hpp>
  9. namespace boost {
  10. namespace detail {
  11. template <class A, class B, class D>
  12. class shadow_proxy
  13. : boost::operators< shadow_proxy<A,B,D> >
  14. {
  15. typedef shadow_proxy self;
  16. public:
  17. inline shadow_proxy(A aa, B bb) : a(aa), b(bb) { }
  18. inline shadow_proxy(const self& x) : a(x.a), b(x.b) { }
  19. template <class Self>
  20. inline shadow_proxy(Self x) : a(x.a), b(x.b) { }
  21. inline self& operator=(const self& x) { a = x.a; b = x.b; return *this; }
  22. inline self& operator++() { ++a; return *this; }
  23. inline self& operator--() { --a; return *this; }
  24. inline self& operator+=(const self& x) { a += x.a; return *this; }
  25. inline self& operator-=(const self& x) { a -= x.a; return *this; }
  26. inline self& operator*=(const self& x) { a *= x.a; return *this; }
  27. inline self& operator/=(const self& x) { a /= x.a; return *this; }
  28. inline self& operator%=(const self& x) { return *this; } // JGS
  29. inline self& operator&=(const self& x) { return *this; } // JGS
  30. inline self& operator|=(const self& x) { return *this; } // JGS
  31. inline self& operator^=(const self& x) { return *this; } // JGS
  32. inline friend D operator-(const self& x, const self& y) {
  33. return x.a - y.a;
  34. }
  35. inline bool operator==(const self& x) const { return a == x.a; }
  36. inline bool operator<(const self& x) const { return a < x.a; }
  37. // protected:
  38. A a;
  39. B b;
  40. };
  41. struct shadow_iterator_policies
  42. {
  43. template <typename iter_pair>
  44. void initialize(const iter_pair&) { }
  45. template <typename Iter>
  46. typename Iter::reference dereference(const Iter& i) const {
  47. typedef typename Iter::reference R;
  48. return R(*i.base().first, *i.base().second);
  49. }
  50. template <typename Iter>
  51. bool equal(const Iter& p1, const Iter& p2) const {
  52. return p1.base().first == p2.base().first;
  53. }
  54. template <typename Iter>
  55. void increment(Iter& i) { ++i.base().first; ++i.base().second; }
  56. template <typename Iter>
  57. void decrement(Iter& i) { --i.base().first; --i.base().second; }
  58. template <typename Iter>
  59. bool less(const Iter& x, const Iter& y) const {
  60. return x.base().first < y.base().first;
  61. }
  62. template <typename Iter>
  63. typename Iter::difference_type
  64. distance(const Iter& x, const Iter& y) const {
  65. return y.base().first - x.base().first;
  66. }
  67. template <typename D, typename Iter>
  68. void advance(Iter& p, D n) { p.base().first += n; p.base().second += n; }
  69. };
  70. } // namespace detail
  71. template <typename IterA, typename IterB>
  72. struct shadow_iterator_generator {
  73. // To use the iterator_adaptor we can't derive from
  74. // random_access_iterator because we don't have a real reference.
  75. // However, we want the STL algorithms to treat the shadow
  76. // iterator like a random access iterator.
  77. struct shadow_iterator_tag : public std::input_iterator_tag {
  78. operator std::random_access_iterator_tag() {
  79. return std::random_access_iterator_tag();
  80. };
  81. };
  82. typedef typename std::iterator_traits<IterA>::value_type Aval;
  83. typedef typename std::iterator_traits<IterB>::value_type Bval;
  84. typedef typename std::iterator_traits<IterA>::reference Aref;
  85. typedef typename std::iterator_traits<IterB>::reference Bref;
  86. typedef typename std::iterator_traits<IterA>::difference_type D;
  87. typedef detail::shadow_proxy<Aval,Bval,Aval> V;
  88. typedef detail::shadow_proxy<Aref,Bref,Aval> R;
  89. typedef iterator_adaptor< std::pair<IterA, IterB>,
  90. detail::shadow_iterator_policies,
  91. V, R, V*, shadow_iterator_tag,
  92. D> type;
  93. };
  94. // short cut for creating a shadow iterator
  95. template <class IterA, class IterB>
  96. inline typename shadow_iterator_generator<IterA,IterB>::type
  97. make_shadow_iter(IterA a, IterB b) {
  98. typedef typename shadow_iterator_generator<IterA,IterB>::type Iter;
  99. return Iter(std::make_pair(a,b));
  100. }
  101. template <class Cmp>
  102. struct shadow_cmp {
  103. inline shadow_cmp(const Cmp& c) : cmp(c) { }
  104. template <class ShadowProxy1, class ShadowProxy2>
  105. inline bool operator()(const ShadowProxy1& x, const ShadowProxy2& y) const
  106. {
  107. return cmp(x.a, y.a);
  108. }
  109. Cmp cmp;
  110. };
  111. } // namespace boost
  112. namespace std {
  113. template <class A1, class B1, class D1,
  114. class A2, class B2, class D2>
  115. void swap(boost::detail::shadow_proxy<A1&,B1&,D1> x,
  116. boost::detail::shadow_proxy<A2&,B2&,D2> y)
  117. {
  118. std::swap(x.a, y.a);
  119. std::swap(x.b, y.b);
  120. }
  121. }
  122. #endif // BOOST_SHADOW_ITERATOR_HPP