1 2 // Copyright 2005-2010 Daniel James. 3 // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 5 6 #if !defined(BOOST_UNORDERED_TEST_HELPERS_INPUT_ITERATOR_HEADER) 7 #define BOOST_UNORDERED_TEST_HELPERS_INPUT_ITERATOR_HEADER 8 9 #include <boost/config.hpp> 10 #include <iterator> 11 12 namespace test { 13 template <class Iterator> struct proxy 14 { 15 typedef typename Iterator::value_type value_type; 16 proxytest::proxy17 explicit proxy(value_type const& v) : v_(v) {} proxytest::proxy18 proxy(proxy const& x) : v_(x.v_) {} operator value_type const&test::proxy19 operator value_type const&() const { return v_; } 20 21 value_type v_; 22 23 private: 24 proxy& operator=(proxy const&); 25 }; 26 27 template <class Iterator> struct input_iterator_adaptor 28 { 29 typedef typename std::iterator_traits<Iterator>::value_type value_type; 30 typedef typename std::iterator_traits<Iterator>::pointer pointer; 31 typedef proxy<Iterator> reference; 32 typedef std::ptrdiff_t difference_type; 33 typedef std::input_iterator_tag iterator_category; 34 input_iterator_adaptortest::input_iterator_adaptor35 input_iterator_adaptor() : base_() {} input_iterator_adaptortest::input_iterator_adaptor36 explicit input_iterator_adaptor(Iterator& it) : base_(&it) {} operator *test::input_iterator_adaptor37 proxy<Iterator> operator*() const { return proxy<Iterator>(**base_); } operator ->test::input_iterator_adaptor38 value_type* operator->() const { return &**base_; } operator ++test::input_iterator_adaptor39 input_iterator_adaptor& operator++() 40 { 41 ++*base_; 42 return *this; 43 } 44 // input_iterator_adaptor operator++(int) { 45 //} operator ==test::input_iterator_adaptor46 bool operator==(input_iterator_adaptor const& x) const 47 { 48 return *base_ == *x.base_; 49 } operator !=test::input_iterator_adaptor50 bool operator!=(input_iterator_adaptor const& x) const 51 { 52 return *base_ != *x.base_; 53 } 54 55 private: 56 Iterator* base_; 57 }; 58 59 template <class Iterator> input_iterator(Iterator & it)60 input_iterator_adaptor<Iterator> input_iterator(Iterator& it) 61 { 62 return input_iterator_adaptor<Iterator>(it); 63 } 64 65 template <class Iterator> struct copy_iterator_adaptor 66 { 67 typedef typename std::iterator_traits<Iterator>::value_type value_type; 68 typedef 69 typename std::iterator_traits<Iterator>::difference_type difference_type; 70 typedef typename std::iterator_traits<Iterator>::iterator_category 71 iterator_category; 72 typedef typename std::iterator_traits<Iterator>::pointer pointer; 73 typedef proxy<Iterator> reference; 74 copy_iterator_adaptortest::copy_iterator_adaptor75 copy_iterator_adaptor() : base_() {} copy_iterator_adaptortest::copy_iterator_adaptor76 explicit copy_iterator_adaptor(Iterator const& it) : base_(it) {} operator *test::copy_iterator_adaptor77 value_type operator*() const { return *base_; } operator ->test::copy_iterator_adaptor78 value_type* operator->() const { return &*base_; } operator []test::copy_iterator_adaptor79 value_type operator[](difference_type d) { return base_[d]; } operator ++test::copy_iterator_adaptor80 copy_iterator_adaptor& operator++() 81 { 82 ++base_; 83 return *this; 84 } operator ++test::copy_iterator_adaptor85 copy_iterator_adaptor operator++(int) 86 { 87 copy_iterator_adaptor tmp(*this); 88 ++base_; 89 return tmp; 90 } operator --test::copy_iterator_adaptor91 copy_iterator_adaptor& operator--() 92 { 93 --base_; 94 return *this; 95 } operator --test::copy_iterator_adaptor96 copy_iterator_adaptor operator--(int) 97 { 98 copy_iterator_adaptor tmp(*this); 99 --base_; 100 return tmp; 101 } operator +=test::copy_iterator_adaptor102 copy_iterator_adaptor operator+=(difference_type x) 103 { 104 base_ += x; 105 return *this; 106 } operator -=test::copy_iterator_adaptor107 copy_iterator_adaptor operator-=(difference_type x) 108 { 109 base_ -= x; 110 return *this; 111 } operator +test::copy_iterator_adaptor112 copy_iterator_adaptor operator+(difference_type n) 113 { 114 return copy_iterator_adaptor(base_ + n); 115 } operator -test::copy_iterator_adaptor116 copy_iterator_adaptor operator-(difference_type n) 117 { 118 return copy_iterator_adaptor(base_ - n); 119 } operator +(difference_type n,copy_iterator_adaptor x)120 friend copy_iterator_adaptor operator+( 121 difference_type n, copy_iterator_adaptor x) 122 { 123 return x + n; 124 } operator -test::copy_iterator_adaptor125 difference_type operator-(copy_iterator_adaptor const& other) 126 { 127 return base_ - other.base_; 128 } operator ==test::copy_iterator_adaptor129 bool operator==(copy_iterator_adaptor const& x) const 130 { 131 return base_ == x.base_; 132 } operator !=test::copy_iterator_adaptor133 bool operator!=(copy_iterator_adaptor const& x) const 134 { 135 return base_ != x.base_; 136 } operator <test::copy_iterator_adaptor137 bool operator<(copy_iterator_adaptor const& x) const 138 { 139 return base_ < x.base_; 140 } operator >test::copy_iterator_adaptor141 bool operator>(copy_iterator_adaptor const& x) const 142 { 143 return base_ > x.base_; 144 } operator <=test::copy_iterator_adaptor145 bool operator<=(copy_iterator_adaptor const& x) const 146 { 147 return base_ <= x.base_; 148 } operator >=test::copy_iterator_adaptor149 bool operator>=(copy_iterator_adaptor const& x) const 150 { 151 return base_ >= x.base_; 152 } 153 154 private: 155 Iterator base_; 156 }; 157 158 template <class Iterator> copy_iterator(Iterator const & it)159 copy_iterator_adaptor<Iterator> copy_iterator(Iterator const& it) 160 { 161 return copy_iterator_adaptor<Iterator>(it); 162 } 163 } 164 165 #endif 166