1
/* Copyright (C) 2004 Garrett A. Kajmowicz
3
This file is part of the uClibc++ Library.
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Lesser General Public
7
License as published by the Free Software Foundation; either
8
version 2.1 of the License, or (at your option) any later version.
10
This library is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Lesser General Public License for more details.
15
You should have received a copy of the GNU Lesser General Public
16
License along with this library; if not, write to the Free Software
17
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
#include <basic_definitions>
22
#ifndef __STD_HEADER_ITERATOR_BASE
23
#define __STD_HEADER_ITERATOR_BASE 1
25
#pragma GCC visibility push(default)
28
template<class Iterator> struct iterator_traits;
29
template<class T> struct iterator_traits<T*>;
31
template<class Category, class T, class Distance = ptrdiff_t, class Pointer = T*, class Reference = T&> struct iterator;
33
struct _UCXXEXPORT input_iterator_tag {};
34
struct _UCXXEXPORT output_iterator_tag {};
35
struct _UCXXEXPORT forward_iterator_tag: public input_iterator_tag {};
36
struct _UCXXEXPORT bidirectional_iterator_tag: public forward_iterator_tag {};
37
struct _UCXXEXPORT random_access_iterator_tag: public bidirectional_iterator_tag {};
39
template <class InputIterator, class Distance> _UCXXEXPORT void advance(InputIterator& i, Distance n){
46
template <class InputIterator> _UCXXEXPORT typename iterator_traits<InputIterator>::difference_type
47
distance(InputIterator first, InputIterator last)
49
typename iterator_traits<InputIterator>::difference_type d = 0;
50
while(first++ !=last){
56
// subclause _lib.predef.iterators_, predefined iterators:
57
template <class Iterator> class reverse_iterator;
58
template <class Iterator> bool operator==(const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y);
59
template <class Iterator> bool operator<(const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y);
60
template <class Iterator> bool operator!=(const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y);
61
template <class Iterator> bool operator>(const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y);
62
template <class Iterator> bool operator>=(const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y);
63
template <class Iterator> bool operator<=(const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y);
64
template <class Iterator> typename reverse_iterator<Iterator>::difference_type
65
operator-( const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y);
66
template <class Iterator> reverse_iterator<Iterator>
67
operator+( typename reverse_iterator<Iterator>::difference_type n, const reverse_iterator<Iterator>& x);
68
template <class Container> class back_insert_iterator;
69
template <class Container> back_insert_iterator<Container> back_inserter(Container& x);
70
template <class Container> class front_insert_iterator;
71
template <class Container> front_insert_iterator<Container> front_inserter(Container& x);
72
template <class Container> class insert_iterator;
73
template <class Container, class Iterator>
74
insert_iterator<Container> inserter(Container& x, Iterator i);
76
//Actual Template definitions
78
template<class Iterator> struct _UCXXEXPORT iterator_traits {
79
typedef typename Iterator::difference_type difference_type;
80
typedef typename Iterator::value_type value_type;
81
typedef typename Iterator::pointer pointer;
82
typedef typename Iterator::reference reference;
83
typedef typename Iterator::iterator_category iterator_category;
86
//Pointer specialization - required by standard
87
template<class T> struct _UCXXEXPORT iterator_traits<T*> {
88
typedef ptrdiff_t difference_type;
92
typedef random_access_iterator_tag iterator_category;
95
//Specialization recomended by standard
96
/* template<class T> struct _UCXXEXPORT iterator_traits<T __far*> {
97
typedef long difference_type;
99
typedef T __far* pointer;
100
typedef T __far& reference;
101
typedef random_access_iterator_tag iterator_category;
104
/* template <class BidirectionalIterator> _UCXXEXPORT void
105
reverse(BidirectionalIterator first, BidirectionalIterator last)
107
typename iterator_traits<BidirectionalIterator>::difference_type n = distance(first, last);
110
typename iterator_traits<BidirectionalIterator>::value_type tmp = *first;
118
template <class Category, class T, class Distance, class Pointer, class Reference>
119
struct _UCXXEXPORT iterator
121
typedef T value_type;
122
typedef Distance difference_type;
123
typedef Pointer pointer;
124
typedef Reference reference;
125
typedef Category iterator_category;
129
template <class Iterator> class _UCXXEXPORT reverse_iterator
130
: public iterator<typename iterator_traits<Iterator>::iterator_category,
131
typename iterator_traits<Iterator>::value_type, typename iterator_traits<Iterator>::difference_type,
132
typename iterator_traits<Iterator>::pointer, typename iterator_traits<Iterator>::reference>
136
friend bool operator== <Iterator>(const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y);
137
friend bool operator< <Iterator>(const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y);
140
typedef Iterator iterator_type;
142
reverse_iterator() : current(){};
143
explicit reverse_iterator(Iterator x) : current(x) { }
144
template<class U> reverse_iterator(const reverse_iterator<U> &x) : current(x.base()){}
146
Iterator base() const { return current; } // explicit
148
typename iterator_traits<Iterator>::reference operator*() const { Iterator tmp = current; return *--tmp; }
149
typename iterator_traits<Iterator>::pointer operator->() const { return &(operator*()); }
150
typename iterator_traits<Iterator>::reference operator[](typename iterator_traits<Iterator>::difference_type n) const{
151
return current[-n-1];
154
reverse_iterator& operator++(){ --current; return *this; }
155
reverse_iterator operator++(int) {reverse_iterator tmp = *this; --current; return tmp; }
156
reverse_iterator& operator--() { ++ current; return *this; }
157
reverse_iterator operator--(int) {reverse_iterator tmp = *this; ++current; return tmp; }
159
reverse_iterator operator+ (typename iterator_traits<Iterator>::difference_type n) const{
160
reverse_iterator retval( *this );
164
reverse_iterator& operator+=(typename iterator_traits<Iterator>::difference_type n){
168
reverse_iterator operator- (typename iterator_traits<Iterator>::difference_type n) const{
169
reverse_iterator retval( *this );
173
reverse_iterator& operator-=(typename iterator_traits<Iterator>::difference_type n){
180
template <class Iterator> _UCXXEXPORT bool
181
operator==(const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y)
183
return x.base() == y.base();
185
template <class Iterator> _UCXXEXPORT bool
186
operator<(const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y)
188
return x.base() < y.base();
190
template <class Iterator> _UCXXEXPORT bool
191
operator!=(const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y)
193
return x.base() != y.base();
195
template <class Iterator> _UCXXEXPORT bool
196
operator>(const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y)
198
return x.base() > y.base();
200
template <class Iterator> _UCXXEXPORT bool
201
operator>=(const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y)
203
return x.base() >= y.base();
205
template <class Iterator> _UCXXEXPORT bool
206
operator<=(const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y)
208
return x.base() <= y.base();
210
template <class Iterator> _UCXXEXPORT typename reverse_iterator<Iterator>::difference_type
211
operator-( const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y)
213
return y.base() - x.base();
215
template <class Iterator> _UCXXEXPORT reverse_iterator<Iterator>
216
operator+(typename reverse_iterator<Iterator>::difference_type n, const reverse_iterator<Iterator>& x)
218
return reverse_iterator<Iterator> (x.base() - n);
221
template <class Container> class _UCXXEXPORT back_insert_iterator :
222
public iterator<output_iterator_tag,void,void,void,void>
225
Container& container;
227
typedef Container container_type;
228
explicit back_insert_iterator(Container& x):container(x) {};
229
back_insert_iterator<Container>& operator=(const typename Container::value_type& value){
230
container.push_back(value);
233
back_insert_iterator<Container>& operator*(){
236
back_insert_iterator<Container>& operator++(){
239
back_insert_iterator<Container> operator++(int){
244
template <class Container> _UCXXEXPORT back_insert_iterator<Container>
245
back_inserter(Container& x)
247
return back_insert_iterator<Container>(x);
250
template <class Container> class _UCXXEXPORT front_insert_iterator
251
: public iterator<output_iterator_tag,void,void,void,void>
254
Container& container;
256
typedef Container container_type;
257
explicit front_insert_iterator(Container& x): container(x) {}
258
front_insert_iterator<Container>& operator=(const typename Container::value_type& value){
259
container.push_front(value);
263
front_insert_iterator<Container>& operator*() { return *this; }
264
front_insert_iterator<Container>& operator++() { return *this; }
265
front_insert_iterator<Container> operator++(int) { return *this; }
268
template <class Container> _UCXXEXPORT front_insert_iterator<Container>
269
front_inserter(Container& x)
271
return front_insert_iterator<Container>(x);
274
template <class Container> class _UCXXEXPORT insert_iterator
275
: public iterator<output_iterator_tag,void,void,void,void>
278
Container& container;
279
typename Container::iterator iter;
281
typedef Container container_type;
282
insert_iterator(Container& x, typename Container::iterator i) : container(x), iter(i) {}
283
insert_iterator<Container>& operator=(const typename Container::value_type& value){
284
iter = container.insert(iter, value);
288
insert_iterator<Container>& operator*() { return *this; }
289
insert_iterator<Container>& operator++() { return *this; }
290
insert_iterator<Container> operator++(int) { return *this; }
293
template <class Container, class Iterator> _UCXXEXPORT insert_iterator<Container>
294
inserter(Container& x, Iterator i)
296
return insert_iterator<Container>(x,typename Container::iterator(i));
301
#pragma GCC visibility pop