/elec/propeller-clock

To get this branch, use:
bzr branch http://bzr.ed.am/elec/propeller-clock

« back to all changes in this revision

Viewing changes to src/utility/iterator_base

  • Committer: edam
  • Date: 2012-02-25 01:31:43 UTC
  • Revision ID: tim@ed.am-20120225013143-9fet2y2d3fjlrwez
added ulibc

Show diffs side-by-side

added added

removed removed

 
1
/*      Copyright (C) 2004 Garrett A. Kajmowicz
 
2
 
 
3
        This file is part of the uClibc++ Library.
 
4
 
 
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.
 
9
 
 
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.
 
14
 
 
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
 
18
*/
 
19
 
 
20
#include <basic_definitions>
 
21
 
 
22
#ifndef __STD_HEADER_ITERATOR_BASE
 
23
#define __STD_HEADER_ITERATOR_BASE 1
 
24
 
 
25
#pragma GCC visibility push(default)
 
26
 
 
27
namespace std{
 
28
        template<class Iterator> struct iterator_traits;
 
29
        template<class T> struct iterator_traits<T*>;
 
30
 
 
31
        template<class Category, class T, class Distance = ptrdiff_t, class Pointer = T*, class Reference = T&> struct iterator;
 
32
 
 
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 {};
 
38
 
 
39
        template <class InputIterator, class Distance> _UCXXEXPORT void advance(InputIterator& i, Distance n){
 
40
                while(n > 0){
 
41
                        --n;
 
42
                        ++i;
 
43
                }
 
44
        }
 
45
 
 
46
        template <class InputIterator> _UCXXEXPORT typename iterator_traits<InputIterator>::difference_type 
 
47
                distance(InputIterator first, InputIterator last)
 
48
        {
 
49
                typename iterator_traits<InputIterator>::difference_type d = 0;
 
50
                while(first++ !=last){
 
51
                        d++;
 
52
                }
 
53
                return d;
 
54
        }
 
55
 
 
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);
 
75
        
 
76
        //Actual Template definitions
 
77
 
 
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;
 
84
        };
 
85
 
 
86
        //Pointer specialization - required by standard
 
87
        template<class T> struct _UCXXEXPORT iterator_traits<T*> {
 
88
                typedef ptrdiff_t difference_type;
 
89
                typedef T value_type;
 
90
                typedef T* pointer;
 
91
                typedef T& reference;
 
92
                typedef random_access_iterator_tag iterator_category;
 
93
        };
 
94
 
 
95
        //Specialization recomended by standard
 
96
/*      template<class T> struct _UCXXEXPORT iterator_traits<T __far*> {
 
97
                typedef long difference_type;
 
98
                typedef T value_type;
 
99
                typedef T __far* pointer;
 
100
                typedef T __far& reference;
 
101
                typedef random_access_iterator_tag iterator_category;
 
102
        };*/
 
103
 
 
104
/*      template <class BidirectionalIterator> _UCXXEXPORT void
 
105
                reverse(BidirectionalIterator first, BidirectionalIterator last)
 
106
        {
 
107
                typename iterator_traits<BidirectionalIterator>::difference_type n = distance(first, last);
 
108
                --n;
 
109
                while(n > 0){
 
110
                        typename iterator_traits<BidirectionalIterator>::value_type tmp = *first;
 
111
                        *first++ = * --last;
 
112
                        *last = tmp;
 
113
                        n -= 2;
 
114
                }
 
115
        };*/
 
116
 
 
117
 
 
118
        template <class Category, class T, class Distance, class Pointer, class Reference> 
 
119
                struct _UCXXEXPORT iterator
 
120
        {
 
121
                typedef T         value_type;
 
122
                typedef Distance  difference_type;
 
123
                typedef Pointer   pointer;
 
124
                typedef Reference reference;
 
125
                typedef Category  iterator_category;
 
126
        };
 
127
 
 
128
 
 
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>
 
133
        {
 
134
        protected:
 
135
                Iterator current;
 
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);
 
138
 
 
139
        public:
 
140
                typedef Iterator iterator_type;
 
141
 
 
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()){}
 
145
 
 
146
                Iterator base() const { return current; }        // explicit
 
147
 
 
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];
 
152
                }
 
153
 
 
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; }
 
158
 
 
159
                reverse_iterator  operator+ (typename iterator_traits<Iterator>::difference_type n) const{
 
160
                        reverse_iterator retval( *this );
 
161
                        retval+=n;
 
162
                        return retval;
 
163
                }
 
164
                reverse_iterator& operator+=(typename iterator_traits<Iterator>::difference_type n){
 
165
                        current -= n;
 
166
                        return *this;
 
167
                }
 
168
                reverse_iterator operator- (typename iterator_traits<Iterator>::difference_type n) const{
 
169
                        reverse_iterator retval( *this );
 
170
                        retval-=n;
 
171
                        return retval;
 
172
                }
 
173
                reverse_iterator& operator-=(typename iterator_traits<Iterator>::difference_type n){
 
174
                        current += n;
 
175
                        return *this;
 
176
                }
 
177
         };
 
178
 
 
179
 
 
180
        template <class Iterator> _UCXXEXPORT bool
 
181
                operator==(const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y)
 
182
        {
 
183
                return x.base() == y.base();
 
184
        }
 
185
        template <class Iterator> _UCXXEXPORT bool
 
186
                operator<(const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y)
 
187
        {
 
188
                return x.base() < y.base();
 
189
        }
 
190
        template <class Iterator> _UCXXEXPORT bool
 
191
                operator!=(const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y)
 
192
        {
 
193
                return x.base() != y.base();
 
194
        }
 
195
        template <class Iterator> _UCXXEXPORT bool
 
196
                operator>(const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y)
 
197
        {
 
198
                return x.base() > y.base();
 
199
        }
 
200
        template <class Iterator> _UCXXEXPORT bool
 
201
                operator>=(const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y)
 
202
        {
 
203
                return x.base() >= y.base();
 
204
        }
 
205
        template <class Iterator> _UCXXEXPORT bool
 
206
                operator<=(const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y)
 
207
        {
 
208
                return x.base() <= y.base();
 
209
        }
 
210
        template <class Iterator> _UCXXEXPORT typename reverse_iterator<Iterator>::difference_type
 
211
                operator-( const reverse_iterator<Iterator>& x, const reverse_iterator<Iterator>& y)
 
212
        {
 
213
                return y.base() - x.base();
 
214
        }
 
215
        template <class Iterator> _UCXXEXPORT reverse_iterator<Iterator>
 
216
                operator+(typename reverse_iterator<Iterator>::difference_type n, const reverse_iterator<Iterator>& x)
 
217
        {
 
218
                return reverse_iterator<Iterator> (x.base() - n);
 
219
        }
 
220
 
 
221
        template <class Container> class _UCXXEXPORT back_insert_iterator : 
 
222
                public iterator<output_iterator_tag,void,void,void,void>
 
223
        {
 
224
        protected:
 
225
                Container& container;
 
226
        public:
 
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);
 
231
                        return *this;
 
232
                }
 
233
                back_insert_iterator<Container>& operator*(){
 
234
                        return *this;
 
235
                }
 
236
                back_insert_iterator<Container>& operator++(){
 
237
                        return *this;
 
238
                }
 
239
                back_insert_iterator<Container>  operator++(int){
 
240
                        return *this;
 
241
                }
 
242
        };
 
243
 
 
244
        template <class Container> _UCXXEXPORT back_insert_iterator<Container>
 
245
                back_inserter(Container& x)
 
246
        {
 
247
                return back_insert_iterator<Container>(x);
 
248
        }
 
249
 
 
250
        template <class Container> class _UCXXEXPORT front_insert_iterator
 
251
                : public iterator<output_iterator_tag,void,void,void,void>
 
252
        {
 
253
        protected:
 
254
                Container& container;
 
255
        public:
 
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);
 
260
                        return *this;
 
261
                }
 
262
 
 
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; }
 
266
        };
 
267
 
 
268
        template <class Container> _UCXXEXPORT front_insert_iterator<Container>
 
269
                front_inserter(Container& x)
 
270
        {
 
271
                return front_insert_iterator<Container>(x);
 
272
        }
 
273
 
 
274
        template <class Container> class _UCXXEXPORT insert_iterator
 
275
                : public iterator<output_iterator_tag,void,void,void,void>
 
276
        {
 
277
        protected:
 
278
                Container& container;
 
279
                typename Container::iterator iter;
 
280
        public:
 
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);
 
285
                        ++iter;
 
286
                        return *this;
 
287
                }
 
288
                insert_iterator<Container>& operator*() { return *this; } 
 
289
                insert_iterator<Container>& operator++() { return *this; }
 
290
                insert_iterator<Container> operator++(int) { return *this; }
 
291
        };
 
292
 
 
293
        template <class Container, class Iterator> _UCXXEXPORT insert_iterator<Container>
 
294
                inserter(Container& x, Iterator i)
 
295
        {
 
296
                return insert_iterator<Container>(x,typename Container::iterator(i));
 
297
        }
 
298
 
 
299
}
 
300
 
 
301
#pragma GCC visibility pop
 
302
 
 
303
#endif
 
304
 
 
305