]> git.cworth.org Git - vogl/blob - src/extlib/loki/include/loki/yasli/yasli_fill_iterator.h
Initial vogl checkin
[vogl] / src / extlib / loki / include / loki / yasli / yasli_fill_iterator.h
1 #ifndef YASLI_FILL_ITERATOR_H_
2 #define YASLI_FILL_ITERATOR_H_
3
4 // $Id: yasli_fill_iterator.h 754 2006-10-17 19:59:11Z syntheticpp $
5
6
7 #include <iterator>
8 #include <cstddef>
9
10 namespace yasli_nstd
11 {
12 template <class T>
13 class fill_iterator_base
14         : public std::iterator<
15         std::random_access_iterator_tag,
16         T,
17         ptrdiff_t,
18         T *,
19         T &>
20 {
21 };
22
23 template <class T>
24 class fill_iterator_base<T &>
25         : public std::iterator<
26         std::random_access_iterator_tag,
27         T,
28         ptrdiff_t,
29         T *,
30         T &>
31 {
32 };
33
34 template <class T>
35 class fill_iterator : public fill_iterator_base<T>
36 {
37         T value_;
38         /*difference_type*/
39         ptrdiff_t count_;//////////////////////////////////
40
41 public:
42         typedef std::ptrdiff_t difference_type;
43         typedef typename fill_iterator_base<T>::pointer pointer;
44         typedef typename fill_iterator_base<T>::reference reference;
45         //typedef iterator_type;
46
47         fill_iterator()
48         {
49         }
50
51         explicit fill_iterator(reference value, difference_type count = 0)
52                 : value_(value), count_(count)
53         {
54         }
55
56         template<class U>
57         fill_iterator(const fill_iterator<U>& rhs)
58                 : value_(rhs.value_), count_(rhs.count_)
59         {
60         }
61
62         reference operator*() const
63         {
64                 return value_;
65         }
66
67         pointer operator->() const
68         {
69                 return & **this;
70         }
71
72         fill_iterator &operator++()
73         {
74                 ++count_;
75                 return *this;
76         }
77
78         fill_iterator operator++(int)
79         {
80                 fill_iterator it(*this);
81                 ++*this;
82                 return it;
83         }
84
85         fill_iterator &operator--()
86         {
87                 --count_;
88                 return *this;
89         }
90
91         fill_iterator operator--(int)
92         {
93                 fill_iterator it(*this);
94                 --*this;
95                 return it;
96         }
97
98         fill_iterator &operator+=(difference_type d)
99         {
100                 count_ += d;
101                 return *this;
102         }
103
104         fill_iterator operator+(difference_type d) const
105         {
106                 return fill_iterator(*this) += d;
107         }
108
109         fill_iterator &operator-=(difference_type d)
110         {
111                 count_ -= d;
112                 return *this;
113         }
114
115         fill_iterator operator-(difference_type d) const
116         {
117                 return fill_iterator(*this) -= d;
118         }
119
120         difference_type operator-(const fill_iterator<T>& rhs) const
121         {
122                 return count_ - rhs.count_;
123         }
124
125         reference operator[](difference_type) const
126         {
127                 return **this;
128         }
129
130         template <class T2>
131         bool operator==(const fill_iterator<T2>& rhs) const
132         {
133                 return count_ == rhs.count_;
134         }
135
136 };
137
138 template <class T, class D>
139 inline fill_iterator<T> operator+(D lhs, const fill_iterator<T>& rhs)
140 {
141         return rhs + lhs;
142 }
143
144 template <class T>
145 inline bool operator!=(
146     const fill_iterator<T>& lhs,
147     const fill_iterator<T>& rhs)
148 {
149         // test for fill_iterator inequality
150         return !(lhs == rhs);
151 }
152
153 template <class T>
154 inline bool operator<(
155     const fill_iterator<T>& lhs,
156     const fill_iterator<T>& rhs)
157 {
158         return lhs.count_ < rhs.count_;
159 }
160
161 template <class T>
162 inline bool operator>(
163     const fill_iterator<T>& lhs,
164     const fill_iterator<T>& rhs)
165 {
166         return rhs < lhs;
167 }
168
169 template <class T>
170 inline bool operator<=(
171     const fill_iterator<T>& lhs,
172     const fill_iterator<T>& rhs)
173 {
174         return !(rhs < lhs);
175 }
176
177 template <class T>
178 inline bool operator>=(
179     const fill_iterator<T>& lhs,
180     const fill_iterator<T>& rhs)
181 {
182         return !(lhs < rhs);
183 }
184 } // namespace yasli_nstd
185
186 #endif