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