]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/stl_iterator.h
[multiple changes]
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / stl_iterator.h
1 // Iterators -*- C++ -*-
2
3 // Copyright (C) 2001 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 /*
31 *
32 * Copyright (c) 1994
33 * Hewlett-Packard Company
34 *
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Hewlett-Packard Company makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
42 *
43 *
44 * Copyright (c) 1996-1998
45 * Silicon Graphics Computer Systems, Inc.
46 *
47 * Permission to use, copy, modify, distribute and sell this software
48 * and its documentation for any purpose is hereby granted without fee,
49 * provided that the above copyright notice appear in all copies and
50 * that both that copyright notice and this permission notice appear
51 * in supporting documentation. Silicon Graphics makes no
52 * representations about the suitability of this software for any
53 * purpose. It is provided "as is" without express or implied warranty.
54 */
55
56 /* NOTE: This is an internal header file, included by other STL headers.
57 * You should not attempt to use it directly.
58 */
59
60 #ifndef __SGI_STL_INTERNAL_ITERATOR_H
61 #define __SGI_STL_INTERNAL_ITERATOR_H
62
63 namespace std
64 {
65 // 24.4.1 Reverse iterators
66 template<typename _Iterator>
67 class reverse_iterator
68 : public iterator<typename iterator_traits<_Iterator>::iterator_category,
69 typename iterator_traits<_Iterator>::value_type,
70 typename iterator_traits<_Iterator>::difference_type,
71 typename iterator_traits<_Iterator>::pointer,
72 typename iterator_traits<_Iterator>::reference>
73 {
74 protected:
75 _Iterator current;
76
77 public:
78 typedef _Iterator iterator_type;
79 typedef typename iterator_traits<_Iterator>::difference_type
80 difference_type;
81 typedef typename iterator_traits<_Iterator>::reference reference;
82 typedef typename iterator_traits<_Iterator>::pointer pointer;
83
84 public:
85 reverse_iterator() {}
86
87 explicit
88 reverse_iterator(iterator_type __x) : current(__x) {}
89
90 reverse_iterator(const reverse_iterator& __x)
91 : current(__x.current) { }
92
93 template<typename _Iter>
94 reverse_iterator(const reverse_iterator<_Iter>& __x)
95 : current(__x.base()) {}
96
97 iterator_type
98 base() const { return current; }
99
100 reference
101 operator*() const
102 {
103 _Iterator __tmp = current;
104 return *--__tmp;
105 }
106
107 pointer
108 operator->() const { return &(operator*()); }
109
110 reverse_iterator&
111 operator++()
112 {
113 --current;
114 return *this;
115 }
116
117 reverse_iterator
118 operator++(int)
119 {
120 reverse_iterator __tmp = *this;
121 --current;
122 return __tmp;
123 }
124
125 reverse_iterator&
126 operator--()
127 {
128 ++current;
129 return *this;
130 }
131
132 reverse_iterator operator--(int)
133 {
134 reverse_iterator __tmp = *this;
135 ++current;
136 return __tmp;
137 }
138
139 reverse_iterator
140 operator+(difference_type __n) const
141 { return reverse_iterator(current - __n); }
142
143 reverse_iterator&
144 operator+=(difference_type __n)
145 {
146 current -= __n;
147 return *this;
148 }
149
150 reverse_iterator
151 operator-(difference_type __n) const
152 { return reverse_iterator(current + __n); }
153
154 reverse_iterator&
155 operator-=(difference_type __n)
156 {
157 _M_current += __n;
158 return *this;
159 }
160
161 reference
162 operator[](difference_type __n) const { return *(*this + __n); }
163 };
164
165 template<typename _Iterator>
166 inline bool
167 operator==(const reverse_iterator<_Iterator>& __x,
168 const reverse_iterator<_Iterator>& __y)
169 { return __x.base() == __y.base(); }
170
171 template<typename _Iterator>
172 inline bool
173 operator<(const reverse_iterator<_Iterator>& __x,
174 const reverse_iterator<_Iterator>& __y)
175 { return __y.base() < __x.base(); }
176
177 template<typename _Iterator>
178 inline bool
179 operator!=(const reverse_iterator<_Iterator>& __x,
180 const reverse_iterator<_Iterator>& __y)
181 { return !(__x == __y); }
182
183 template<typename _Iterator>
184 inline bool
185 operator>(const reverse_iterator<_Iterator>& __x,
186 const reverse_iterator<_Iterator>& __y)
187 { return __y < __x; }
188
189 template<typename _Iterator>
190 inline bool
191 operator<=(const reverse_iterator<_Iterator>& __x,
192 const reverse_iterator<_Iterator>& __y)
193 { return !(__y < __x); }
194
195 template<typename _Iterator>
196 inline bool
197 operator>=(const reverse_iterator<_Iterator>& __x,
198 const reverse_iterator<_Iterator>& __y)
199 { return !(__x < __y); }
200
201 template<typename _Iterator>
202 inline typename reverse_iterator<_Iterator>::difference_type
203 operator-(const reverse_iterator<_Iterator>& __x,
204 const reverse_iterator<_Iterator>& __y)
205 { return __y.base() - __x.base(); }
206
207 template<typename _Iterator>
208 inline reverse_iterator<_Iterator>
209 operator+(typename reverse_iterator<_Iterator>::difference_type __n,
210 const reverse_iterator<_Iterator>& __x)
211 { return reverse_iterator<_Iterator>(__x.base() - __n); }
212
213 // 24.4.2.2.1 back_insert_iterator
214 template<typename _Container>
215 class back_insert_iterator
216 : public iterator<output_iterator_tag, void, void, void, void>
217 {
218 protected:
219 _Container* container;
220
221 public:
222 typedef _Container container_type;
223
224 explicit
225 back_insert_iterator(_Container& __x) : container(&__x) {}
226
227 back_insert_iterator&
228 operator=(typename _Container::const_reference __value)
229 {
230 container->push_back(__value);
231 return *this;
232 }
233
234 back_insert_iterator&
235 operator*() { return *this; }
236
237 back_insert_iterator&
238 operator++() { return *this; }
239
240 back_insert_iterator
241 operator++(int) { return *this; }
242 };
243
244 template<typename _Container>
245 inline back_insert_iterator<_Container>
246 back_inserter(_Container& __x)
247 { return back_insert_iterator<_Container>(__x); }
248
249 template<typename _Container>
250 class front_insert_iterator
251 : public iterator<output_iterator_tag, void, void, void, void>
252 {
253 protected:
254 _Container* container;
255
256 public:
257 typedef _Container container_type;
258
259 explicit front_insert_iterator(_Container& __x) : container(&__x) {}
260
261 front_insert_iterator&
262 operator=(typename _Container::const_reference __value)
263 {
264 container->push_front(__value);
265 return *this;
266 }
267
268 front_insert_iterator&
269 operator*() { return *this; }
270
271 front_insert_iterator&
272 operator++() { return *this; }
273
274 front_insert_iterator
275 operator++(int) { return *this; }
276 };
277
278 template<typename _Container>
279 inline front_insert_iterator<_Container> front_inserter(_Container& __x)
280 { return front_insert_iterator<_Container>(__x); }
281
282 template<typename _Container>
283 class insert_iterator
284 : public iterator<output_iterator_tag, void, void, void, void>
285 {
286 protected:
287 _Container* container;
288 typename _Container::iterator iter;
289
290 public:
291 typedef _Container container_type;
292
293 insert_iterator(_Container& __x, typename _Container::iterator __i)
294 : container(&__x), iter(__i) {}
295
296 insert_iterator&
297 operator=(const typename _Container::const_reference __value)
298 {
299 iter = container->insert(iter, __value);
300 ++iter;
301 return *this;
302 }
303
304 insert_iterator&
305 operator*() { return *this; }
306
307 insert_iterator&
308 operator++() { return *this; }
309
310 insert_iterator&
311 operator++(int) { return *this; }
312 };
313
314 template<typename _Container, typename _Iterator>
315 inline
316 insert_iterator<_Container> inserter(_Container& __x, _Iterator __i)
317 {
318 typedef typename _Container::iterator __iter;
319 return insert_iterator<_Container>(__x, __iter(__i));
320 }
321
322 // This iterator adapter is 'normal' in the sense that it does not
323 // change the semantics of any of the operators of its iterator
324 // parameter. Its primary purpose is to convert an iterator that is
325 // not a class, e.g. a pointer, into an iterator that is a class.
326 // The _Container parameter exists solely so that different containers
327 // using this template can instantiate different types, even if the
328 // _Iterator parameter is the same.
329 template<typename _Iterator, typename _Container>
330 class __normal_iterator
331 : public iterator<typename iterator_traits<_Iterator>::iterator_category,
332 typename iterator_traits<_Iterator>::value_type,
333 typename iterator_traits<_Iterator>::difference_type,
334 typename iterator_traits<_Iterator>::pointer,
335 typename iterator_traits<_Iterator>::reference>
336 {
337 protected:
338 _Iterator _M_current;
339
340 public:
341 typedef typename iterator_traits<_Iterator>::difference_type
342 difference_type;
343 typedef typename iterator_traits<_Iterator>::reference reference;
344 typedef typename iterator_traits<_Iterator>::pointer pointer;
345
346 __normal_iterator() : _M_current(_Iterator()) { }
347
348 explicit
349 __normal_iterator(const _Iterator& __i) : _M_current(__i) { }
350
351 // Allow iterator to const_iterator conversion
352 template<typename _Iter>
353 inline __normal_iterator(const __normal_iterator<_Iter, _Container>& __i)
354 : _M_current(__i.base()) { }
355
356 // Forward iterator requirements
357 reference
358 operator*() const { return *_M_current; }
359
360 pointer
361 operator->() const { return _M_current; }
362
363 __normal_iterator&
364 operator++() { ++_M_current; return *this; }
365
366 __normal_iterator
367 operator++(int) { return __normal_iterator(_M_current++); }
368
369 // Bidirectional iterator requirements
370 __normal_iterator&
371 operator--() { --_M_current; return *this; }
372
373 __normal_iterator
374 operator--(int) { return __normal_iterator(_M_current--); }
375
376 // Random access iterator requirements
377 reference
378 operator[](const difference_type& __n) const
379 { return _M_current[__n]; }
380
381 __normal_iterator&
382 operator+=(const difference_type& __n)
383 { _M_current += __n; return *this; }
384
385 __normal_iterator
386 operator+(const difference_type& __n) const
387 { return __normal_iterator(_M_current + __n); }
388
389 __normal_iterator&
390 operator-=(const difference_type& __n)
391 { _M_current -= __n; return *this; }
392
393 __normal_iterator
394 operator-(const difference_type& __n) const
395 { return __normal_iterator(_M_current - __n); }
396
397 difference_type
398 operator-(const __normal_iterator& __i) const
399 { return _M_current - __i._M_current; }
400
401 const _Iterator&
402 base() const { return _M_current; }
403 };
404
405 // Forward iterator requirements
406 template<typename _IteratorL, typename _IteratorR, typename _Container>
407 inline bool
408 operator==(const __normal_iterator<_IteratorL, _Container>& __lhs,
409 const __normal_iterator<_IteratorR, _Container>& __rhs)
410 { return __lhs.base() == __rhs.base(); }
411
412 template<typename _IteratorL, typename _IteratorR, typename _Container>
413 inline bool
414 operator!=(const __normal_iterator<_IteratorL, _Container>& __lhs,
415 const __normal_iterator<_IteratorR, _Container>& __rhs)
416 { return !(__lhs == __rhs); }
417
418 // Random access iterator requirements
419 template<typename _IteratorL, typename _IteratorR, typename _Container>
420 inline bool
421 operator<(const __normal_iterator<_IteratorL, _Container>& __lhs,
422 const __normal_iterator<_IteratorR, _Container>& __rhs)
423 { return __lhs.base() < __rhs.base(); }
424
425 template<typename _IteratorL, typename _IteratorR, typename _Container>
426 inline bool
427 operator>(const __normal_iterator<_IteratorL, _Container>& __lhs,
428 const __normal_iterator<_IteratorR, _Container>& __rhs)
429 { return __rhs < __lhs; }
430
431 template<typename _IteratorL, typename _IteratorR, typename _Container>
432 inline bool
433 operator<=(const __normal_iterator<_IteratorL, _Container>& __lhs,
434 const __normal_iterator<_IteratorR, _Container>& __rhs)
435 { return !(__rhs < __lhs); }
436
437 template<typename _IteratorL, typename _IteratorR, typename _Container>
438 inline bool
439 operator>=(const __normal_iterator<_IteratorL, _Container>& __lhs,
440 const __normal_iterator<_IteratorR, _Container>& __rhs)
441 { return !(__lhs < __rhs); }
442
443 template<typename _Iterator, typename _Container>
444 inline __normal_iterator<_Iterator, _Container>
445 operator+(typename __normal_iterator<_Iterator, _Container>::difference_type __n,
446 const __normal_iterator<_Iterator, _Container>& __i)
447 { return __normal_iterator<_Iterator, _Container>(__i.base() + __n); }
448 } // namespace std
449
450 #endif
451
452 // Local Variables:
453 // mode:C++
454 // End: