forked from maniacbug/StandardCplusplus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator_base
305 lines (263 loc) · 11 KB
/
iterator_base
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* Copyright (C) 2004 Garrett A. Kajmowicz
This file is part of the uClibc++ Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <basic_definitions>
#ifndef __STD_HEADER_ITERATOR_BASE
#define __STD_HEADER_ITERATOR_BASE 1
#pragma GCC visibility push(default)
namespace std{
template<class Iterator> struct iterator_traits;
template<class T> struct iterator_traits<T*>;
template<class Category, class T, class Distance = ptrdiff_t, class Pointer = T*, class Reference = T&> struct iterator;
struct _UCXXEXPORT input_iterator_tag {};
struct _UCXXEXPORT output_iterator_tag {};
struct _UCXXEXPORT forward_iterator_tag: public input_iterator_tag {};
struct _UCXXEXPORT bidirectional_iterator_tag: public forward_iterator_tag {};
struct _UCXXEXPORT random_access_iterator_tag: public bidirectional_iterator_tag {};
template <class InputIterator, class Distance> _UCXXEXPORT void advance(InputIterator& i, Distance n){
while(n > 0){
--n;
++i;
}
}
template <class InputIterator> _UCXXEXPORT typename iterator_traits<InputIterator>::difference_type
distance(InputIterator first, InputIterator last)
{
typename iterator_traits<InputIterator>::difference_type d = 0;
while(first++ !=last){
d++;
}
return d;
}
// subclause _lib.predef.iterators_, predefined iterators:
template <class Iterator> class reverse_iterator;
template <class Iterator> bool operator==(const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y);
template <class Iterator> bool operator<(const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y);
template <class Iterator> bool operator!=(const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y);
template <class Iterator> bool operator>(const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y);
template <class Iterator> bool operator>=(const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y);
template <class Iterator> bool operator<=(const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y);
template <class Iterator> typename reverse_iterator<Iterator>::difference_type
operator-( const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y);
template <class Iterator> reverse_iterator<Iterator>
operator+( typename reverse_iterator<Iterator>::difference_type n, const reverse_iterator<Iterator>& x);
template <class Container> class back_insert_iterator;
template <class Container> back_insert_iterator<Container> back_inserter(Container& x);
template <class Container> class front_insert_iterator;
template <class Container> front_insert_iterator<Container> front_inserter(Container& x);
template <class Container> class insert_iterator;
template <class Container, class Iterator>
insert_iterator<Container> inserter(Container& x, Iterator i);
//Actual Template definitions
template<class Iterator> struct _UCXXEXPORT iterator_traits {
typedef typename Iterator::difference_type difference_type;
typedef typename Iterator::value_type value_type;
typedef typename Iterator::pointer pointer;
typedef typename Iterator::reference reference;
typedef typename Iterator::iterator_category iterator_category;
};
//Pointer specialization - required by standard
template<class T> struct _UCXXEXPORT iterator_traits<T*> {
typedef ptrdiff_t difference_type;
typedef T value_type;
typedef T* pointer;
typedef T& reference;
typedef random_access_iterator_tag iterator_category;
};
//Specialization recomended by standard
/* template<class T> struct _UCXXEXPORT iterator_traits<T __far*> {
typedef long difference_type;
typedef T value_type;
typedef T __far* pointer;
typedef T __far& reference;
typedef random_access_iterator_tag iterator_category;
};*/
/* template <class BidirectionalIterator> _UCXXEXPORT void
reverse(BidirectionalIterator first, BidirectionalIterator last)
{
typename iterator_traits<BidirectionalIterator>::difference_type n = distance(first, last);
--n;
while(n > 0){
typename iterator_traits<BidirectionalIterator>::value_type tmp = *first;
*first++ = * --last;
*last = tmp;
n -= 2;
}
};*/
template <class Category, class T, class Distance, class Pointer, class Reference>
struct _UCXXEXPORT iterator
{
typedef T value_type;
typedef Distance difference_type;
typedef Pointer pointer;
typedef Reference reference;
typedef Category iterator_category;
};
template <class Iterator> class _UCXXEXPORT reverse_iterator
: public iterator<typename iterator_traits<Iterator>::iterator_category,
typename iterator_traits<Iterator>::value_type, typename iterator_traits<Iterator>::difference_type,
typename iterator_traits<Iterator>::pointer, typename iterator_traits<Iterator>::reference>
{
protected:
Iterator current;
friend bool operator== <Iterator>(const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y);
friend bool operator< <Iterator>(const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y);
public:
typedef Iterator iterator_type;
reverse_iterator() : current(){};
explicit reverse_iterator(Iterator x) : current(x) { }
template<class U> reverse_iterator(const reverse_iterator<U> &x) : current(x.base()){}
Iterator base() const { return current; } // explicit
typename iterator_traits<Iterator>::reference operator*() const { Iterator tmp = current; return *--tmp; }
typename iterator_traits<Iterator>::pointer operator->() const { return &(operator*()); }
typename iterator_traits<Iterator>::reference operator[](typename iterator_traits<Iterator>::difference_type n) const{
return current[-n-1];
}
reverse_iterator& operator++(){ --current; return *this; }
reverse_iterator operator++(int) {reverse_iterator tmp = *this; --current; return tmp; }
reverse_iterator& operator--() { ++ current; return *this; }
reverse_iterator operator--(int) {reverse_iterator tmp = *this; ++current; return tmp; }
reverse_iterator operator+ (typename iterator_traits<Iterator>::difference_type n) const{
reverse_iterator retval( *this );
retval+=n;
return retval;
}
reverse_iterator& operator+=(typename iterator_traits<Iterator>::difference_type n){
current -= n;
return *this;
}
reverse_iterator operator- (typename iterator_traits<Iterator>::difference_type n) const{
reverse_iterator retval( *this );
retval-=n;
return retval;
}
reverse_iterator& operator-=(typename iterator_traits<Iterator>::difference_type n){
current += n;
return *this;
}
};
template <class Iterator> _UCXXEXPORT bool
operator==(const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y)
{
return x.base() == y.base();
}
template <class Iterator> _UCXXEXPORT bool
operator<(const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y)
{
return x.base() < y.base();
}
template <class Iterator> _UCXXEXPORT bool
operator!=(const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y)
{
return x.base() != y.base();
}
template <class Iterator> _UCXXEXPORT bool
operator>(const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y)
{
return x.base() > y.base();
}
template <class Iterator> _UCXXEXPORT bool
operator>=(const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y)
{
return x.base() >= y.base();
}
template <class Iterator> _UCXXEXPORT bool
operator<=(const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y)
{
return x.base() <= y.base();
}
template <class Iterator> _UCXXEXPORT typename reverse_iterator<Iterator>::difference_type
operator-( const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y)
{
return y.base() - x.base();
}
template <class Iterator> _UCXXEXPORT reverse_iterator<Iterator>
operator+(typename reverse_iterator<Iterator>::difference_type n, const reverse_iterator<Iterator>& x)
{
return reverse_iterator<Iterator> (x.base() - n);
}
template <class Container> class _UCXXEXPORT back_insert_iterator :
public iterator<output_iterator_tag,void,void,void,void>
{
protected:
Container& container;
public:
typedef Container container_type;
explicit back_insert_iterator(Container& x):container(x) {};
back_insert_iterator<Container>& operator=(const typename Container::value_type& value){
container.push_back(value);
return *this;
}
back_insert_iterator<Container>& operator*(){
return *this;
}
back_insert_iterator<Container>& operator++(){
return *this;
}
back_insert_iterator<Container> operator++(int){
return *this;
}
};
template <class Container> _UCXXEXPORT back_insert_iterator<Container>
back_inserter(Container& x)
{
return back_insert_iterator<Container>(x);
}
template <class Container> class _UCXXEXPORT front_insert_iterator
: public iterator<output_iterator_tag,void,void,void,void>
{
protected:
Container& container;
public:
typedef Container container_type;
explicit front_insert_iterator(Container& x): container(x) {}
front_insert_iterator<Container>& operator=(const typename Container::value_type& value){
container.push_front(value);
return *this;
}
front_insert_iterator<Container>& operator*() { return *this; }
front_insert_iterator<Container>& operator++() { return *this; }
front_insert_iterator<Container> operator++(int) { return *this; }
};
template <class Container> _UCXXEXPORT front_insert_iterator<Container>
front_inserter(Container& x)
{
return front_insert_iterator<Container>(x);
}
template <class Container> class _UCXXEXPORT insert_iterator
: public iterator<output_iterator_tag,void,void,void,void>
{
protected:
Container& container;
typename Container::iterator iter;
public:
typedef Container container_type;
insert_iterator(Container& x, typename Container::iterator i) : container(x), iter(i) {}
insert_iterator<Container>& operator=(const typename Container::value_type& value){
iter = container.insert(iter, value);
++iter;
return *this;
}
insert_iterator<Container>& operator*() { return *this; }
insert_iterator<Container>& operator++() { return *this; }
insert_iterator<Container> operator++(int) { return *this; }
};
template <class Container, class Iterator> _UCXXEXPORT insert_iterator<Container>
inserter(Container& x, Iterator i)
{
return insert_iterator<Container>(x,typename Container::iterator(i));
}
}
#pragma GCC visibility pop
#endif