Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:37:11

0001 // (C) Copyright Jeremy Siek 2001.
0002 // Distributed under the Boost Software License, Version 1.0. (See
0003 // accompanying file LICENSE_1_0.txt or copy at
0004 // http://www.boost.org/LICENSE_1_0.txt)
0005 
0006 #ifndef BOOST_SHADOW_ITERATOR_HPP
0007 #define BOOST_SHADOW_ITERATOR_HPP
0008 
0009 #include <boost/iterator_adaptors.hpp>
0010 #include <boost/operators.hpp>
0011 
0012 namespace boost
0013 {
0014 
0015 namespace detail
0016 {
0017 
0018     template < class A, class B, class D >
0019     class shadow_proxy : boost::operators< shadow_proxy< A, B, D > >
0020     {
0021         typedef shadow_proxy self;
0022 
0023     public:
0024         inline shadow_proxy(A aa, B bb) : a(aa), b(bb) {}
0025         inline shadow_proxy(const self& x) : a(x.a), b(x.b) {}
0026         template < class Self > inline shadow_proxy(Self x) : a(x.a), b(x.b) {}
0027         inline self& operator=(const self& x)
0028         {
0029             a = x.a;
0030             b = x.b;
0031             return *this;
0032         }
0033         inline self& operator++()
0034         {
0035             ++a;
0036             return *this;
0037         }
0038         inline self& operator--()
0039         {
0040             --a;
0041             return *this;
0042         }
0043         inline self& operator+=(const self& x)
0044         {
0045             a += x.a;
0046             return *this;
0047         }
0048         inline self& operator-=(const self& x)
0049         {
0050             a -= x.a;
0051             return *this;
0052         }
0053         inline self& operator*=(const self& x)
0054         {
0055             a *= x.a;
0056             return *this;
0057         }
0058         inline self& operator/=(const self& x)
0059         {
0060             a /= x.a;
0061             return *this;
0062         }
0063         inline self& operator%=(const self& x) { return *this; } // JGS
0064         inline self& operator&=(const self& x) { return *this; } // JGS
0065         inline self& operator|=(const self& x) { return *this; } // JGS
0066         inline self& operator^=(const self& x) { return *this; } // JGS
0067         inline friend D operator-(const self& x, const self& y)
0068         {
0069             return x.a - y.a;
0070         }
0071         inline bool operator==(const self& x) const { return a == x.a; }
0072         inline bool operator<(const self& x) const { return a < x.a; }
0073         //  protected:
0074         A a;
0075         B b;
0076     };
0077 
0078     struct shadow_iterator_policies
0079     {
0080         template < typename iter_pair > void initialize(const iter_pair&) {}
0081 
0082         template < typename Iter >
0083         typename Iter::reference dereference(const Iter& i) const
0084         {
0085             typedef typename Iter::reference R;
0086             return R(*i.base().first, *i.base().second);
0087         }
0088         template < typename Iter >
0089         bool equal(const Iter& p1, const Iter& p2) const
0090         {
0091             return p1.base().first == p2.base().first;
0092         }
0093         template < typename Iter > void increment(Iter& i)
0094         {
0095             ++i.base().first;
0096             ++i.base().second;
0097         }
0098 
0099         template < typename Iter > void decrement(Iter& i)
0100         {
0101             --i.base().first;
0102             --i.base().second;
0103         }
0104 
0105         template < typename Iter > bool less(const Iter& x, const Iter& y) const
0106         {
0107             return x.base().first < y.base().first;
0108         }
0109         template < typename Iter >
0110         typename Iter::difference_type distance(
0111             const Iter& x, const Iter& y) const
0112         {
0113             return y.base().first - x.base().first;
0114         }
0115         template < typename D, typename Iter > void advance(Iter& p, D n)
0116         {
0117             p.base().first += n;
0118             p.base().second += n;
0119         }
0120     };
0121 
0122 } // namespace detail
0123 
0124 template < typename IterA, typename IterB > struct shadow_iterator_generator
0125 {
0126 
0127     // To use the iterator_adaptor we can't derive from
0128     // random_access_iterator because we don't have a real reference.
0129     // However, we want the STL algorithms to treat the shadow
0130     // iterator like a random access iterator.
0131     struct shadow_iterator_tag : public std::input_iterator_tag
0132     {
0133         operator std::random_access_iterator_tag()
0134         {
0135             return std::random_access_iterator_tag();
0136         };
0137     };
0138     typedef typename std::iterator_traits< IterA >::value_type Aval;
0139     typedef typename std::iterator_traits< IterB >::value_type Bval;
0140     typedef typename std::iterator_traits< IterA >::reference Aref;
0141     typedef typename std::iterator_traits< IterB >::reference Bref;
0142     typedef typename std::iterator_traits< IterA >::difference_type D;
0143     typedef detail::shadow_proxy< Aval, Bval, Aval > V;
0144     typedef detail::shadow_proxy< Aref, Bref, Aval > R;
0145     typedef iterator_adaptor< std::pair< IterA, IterB >,
0146         detail::shadow_iterator_policies, V, R, V*, shadow_iterator_tag, D >
0147         type;
0148 };
0149 
0150 // short cut for creating a shadow iterator
0151 template < class IterA, class IterB >
0152 inline typename shadow_iterator_generator< IterA, IterB >::type
0153 make_shadow_iter(IterA a, IterB b)
0154 {
0155     typedef typename shadow_iterator_generator< IterA, IterB >::type Iter;
0156     return Iter(std::make_pair(a, b));
0157 }
0158 
0159 template < class Cmp > struct shadow_cmp
0160 {
0161     inline shadow_cmp(const Cmp& c) : cmp(c) {}
0162     template < class ShadowProxy1, class ShadowProxy2 >
0163     inline bool operator()(const ShadowProxy1& x, const ShadowProxy2& y) const
0164     {
0165         return cmp(x.a, y.a);
0166     }
0167     Cmp cmp;
0168 };
0169 
0170 } // namespace boost
0171 
0172 namespace std
0173 {
0174 template < class A1, class B1, class D1, class A2, class B2, class D2 >
0175 void swap(boost::detail::shadow_proxy< A1&, B1&, D1 > x,
0176     boost::detail::shadow_proxy< A2&, B2&, D2 > y)
0177 {
0178     std::swap(x.a, y.a);
0179     std::swap(x.b, y.b);
0180 }
0181 }
0182 
0183 #endif // BOOST_SHADOW_ITERATOR_HPP