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