]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/stl_iterator.h
std_bitset.h: Use GLIBCPP in multiple-inclusion guard.
[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 /** @file stl_iterator.h
57 * This is an internal header file, included by other library headers.
58 * You should not attempt to use it directly.
59 */
60
61 #ifndef __GLIBCPP_INTERNAL_ITERATOR_H
62 #define __GLIBCPP_INTERNAL_ITERATOR_H
63
64 namespace std
65 {
66 // 24.4.1 Reverse iterators
67 template<typename _Iterator>
68 class reverse_iterator
69 : public iterator<typename iterator_traits<_Iterator>::iterator_category,
70 typename iterator_traits<_Iterator>::value_type,
71 typename iterator_traits<_Iterator>::difference_type,
72 typename iterator_traits<_Iterator>::pointer,
73 typename iterator_traits<_Iterator>::reference>
74 {
75 protected:
76 _Iterator current;
77
78 public:
79 typedef _Iterator iterator_type;
80 typedef typename iterator_traits<_Iterator>::difference_type
81 difference_type;
82 typedef typename iterator_traits<_Iterator>::reference reference;
83 typedef typename iterator_traits<_Iterator>::pointer pointer;
84
85 public:
86 reverse_iterator() {}
87
88 explicit
89 reverse_iterator(iterator_type __x) : current(__x) {}
90
91 reverse_iterator(const reverse_iterator& __x)
92 : current(__x.current) { }
93
94 template<typename _Iter>
95 reverse_iterator(const reverse_iterator<_Iter>& __x)
96 : current(__x.base()) {}
97
98 iterator_type
99 base() const { return current; }
100
101 reference
102 operator*() const
103 {
104 _Iterator __tmp = current;
105 return *--__tmp;
106 }
107
108 pointer
109 operator->() const { return &(operator*()); }
110
111 reverse_iterator&
112 operator++()
113 {
114 --current;
115 return *this;
116 }
117
118 reverse_iterator
119 operator++(int)
120 {
121 reverse_iterator __tmp = *this;
122 --current;
123 return __tmp;
124 }
125
126 reverse_iterator&
127 operator--()
128 {
129 ++current;
130 return *this;
131 }
132
133 reverse_iterator operator--(int)
134 {
135 reverse_iterator __tmp = *this;
136 ++current;
137 return __tmp;
138 }
139
140 reverse_iterator
141 operator+(difference_type __n) const
142 { return reverse_iterator(current - __n); }
143
144 reverse_iterator&
145 operator+=(difference_type __n)
146 {
147 current -= __n;
148 return *this;
149 }
150
151 reverse_iterator
152 operator-(difference_type __n) const
153 { return reverse_iterator(current + __n); }
154
155 reverse_iterator&
156 operator-=(difference_type __n)
157 {
158 _M_current += __n;
159 return *this;
160 }
161
162 reference
163 operator[](difference_type __n) const { return *(*this + __n); }
164 };
165
166 template<typename _Iterator>
167 inline bool
168 operator==(const reverse_iterator<_Iterator>& __x,
169 const reverse_iterator<_Iterator>& __y)
170 { return __x.base() == __y.base(); }
171
172 template<typename _Iterator>
173 inline bool
174 operator<(const reverse_iterator<_Iterator>& __x,
175 const reverse_iterator<_Iterator>& __y)
176 { return __y.base() < __x.base(); }
177
178 template<typename _Iterator>
179 inline bool
180 operator!=(const reverse_iterator<_Iterator>& __x,
181 const reverse_iterator<_Iterator>& __y)
182 { return !(__x == __y); }
183
184 template<typename _Iterator>
185 inline bool
186 operator>(const reverse_iterator<_Iterator>& __x,
187 const reverse_iterator<_Iterator>& __y)
188 { return __y < __x; }
189
190 template<typename _Iterator>
191 inline bool
192 operator<=(const reverse_iterator<_Iterator>& __x,
193 const reverse_iterator<_Iterator>& __y)
194 { return !(__y < __x); }
195
196 template<typename _Iterator>
197 inline bool
198 operator>=(const reverse_iterator<_Iterator>& __x,
199 const reverse_iterator<_Iterator>& __y)
200 { return !(__x < __y); }
201
202 template<typename _Iterator>
203 inline typename reverse_iterator<_Iterator>::difference_type
204 operator-(const reverse_iterator<_Iterator>& __x,
205 const reverse_iterator<_Iterator>& __y)
206 { return __y.base() - __x.base(); }
207
208 template<typename _Iterator>
209 inline reverse_iterator<_Iterator>
210 operator+(typename reverse_iterator<_Iterator>::difference_type __n,
211 const reverse_iterator<_Iterator>& __x)
212 { return reverse_iterator<_Iterator>(__x.base() - __n); }
213
214 // 24.4.2.2.1 back_insert_iterator
215 template<typename _Container>
216 class back_insert_iterator
217 : public iterator<output_iterator_tag, void, void, void, void>
218 {
219 protected:
220 _Container* container;
221
222 public:
223 typedef _Container container_type;
224
225 explicit
226 back_insert_iterator(_Container& __x) : container(&__x) {}
227
228 back_insert_iterator&
229 operator=(typename _Container::const_reference __value)
230 {
231 container->push_back(__value);
232 return *this;
233 }
234
235 back_insert_iterator&
236 operator*() { return *this; }
237
238 back_insert_iterator&
239 operator++() { return *this; }
240
241 back_insert_iterator
242 operator++(int) { return *this; }
243 };
244
245 template<typename _Container>
246 inline back_insert_iterator<_Container>
247 back_inserter(_Container& __x)
248 { return back_insert_iterator<_Container>(__x); }
249
250 template<typename _Container>
251 class front_insert_iterator
252 : public iterator<output_iterator_tag, void, void, void, void>
253 {
254 protected:
255 _Container* container;
256
257 public:
258 typedef _Container container_type;
259
260 explicit front_insert_iterator(_Container& __x) : container(&__x) {}
261
262 front_insert_iterator&
263 operator=(typename _Container::const_reference __value)
264 {
265 container->push_front(__value);
266 return *this;
267 }
268
269 front_insert_iterator&
270 operator*() { return *this; }
271
272 front_insert_iterator&
273 operator++() { return *this; }
274
275 front_insert_iterator
276 operator++(int) { return *this; }
277 };
278
279 template<typename _Container>
280 inline front_insert_iterator<_Container> front_inserter(_Container& __x)
281 { return front_insert_iterator<_Container>(__x); }
282
283 template<typename _Container>
284 class insert_iterator
285 : public iterator<output_iterator_tag, void, void, void, void>
286 {
287 protected:
288 _Container* container;
289 typename _Container::iterator iter;
290
291 public:
292 typedef _Container container_type;
293
294 insert_iterator(_Container& __x, typename _Container::iterator __i)
295 : container(&__x), iter(__i) {}
296
297 insert_iterator&
298 operator=(const typename _Container::const_reference __value)
299 {
300 iter = container->insert(iter, __value);
301 ++iter;
302 return *this;
303 }
304
305 insert_iterator&
306 operator*() { return *this; }
307
308 insert_iterator&
309 operator++() { return *this; }
310
311 insert_iterator&
312 operator++(int) { return *this; }
313 };
314
315 template<typename _Container, typename _Iterator>
316 inline
317 insert_iterator<_Container> inserter(_Container& __x, _Iterator __i)
318 {
319 typedef typename _Container::iterator __iter;
320 return insert_iterator<_Container>(__x, __iter(__i));
321 }
322
323 // This iterator adapter is 'normal' in the sense that it does not
324 // change the semantics of any of the operators of its iterator
325 // parameter. Its primary purpose is to convert an iterator that is
326 // not a class, e.g. a pointer, into an iterator that is a class.
327 // The _Container parameter exists solely so that different containers
328 // using this template can instantiate different types, even if the
329 // _Iterator parameter is the same.
330 template<typename _Iterator, typename _Container>
331 class __normal_iterator
332 : public iterator<typename iterator_traits<_Iterator>::iterator_category,
333 typename iterator_traits<_Iterator>::value_type,
334 typename iterator_traits<_Iterator>::difference_type,
335 typename iterator_traits<_Iterator>::pointer,
336 typename iterator_traits<_Iterator>::reference>
337 {
338 protected:
339 _Iterator _M_current;
340
341 public:
342 typedef typename iterator_traits<_Iterator>::difference_type
343 difference_type;
344 typedef typename iterator_traits<_Iterator>::reference reference;
345 typedef typename iterator_traits<_Iterator>::pointer pointer;
346
347 __normal_iterator() : _M_current(_Iterator()) { }
348
349 explicit
350 __normal_iterator(const _Iterator& __i) : _M_current(__i) { }
351
352 // Allow iterator to const_iterator conversion
353 template<typename _Iter>
354 inline __normal_iterator(const __normal_iterator<_Iter, _Container>& __i)
355 : _M_current(__i.base()) { }
356
357 // Forward iterator requirements
358 reference
359 operator*() const { return *_M_current; }
360
361 pointer
362 operator->() const { return _M_current; }
363
364 __normal_iterator&
365 operator++() { ++_M_current; return *this; }
366
367 __normal_iterator
368 operator++(int) { return __normal_iterator(_M_current++); }
369
370 // Bidirectional iterator requirements
371 __normal_iterator&
372 operator--() { --_M_current; return *this; }
373
374 __normal_iterator
375 operator--(int) { return __normal_iterator(_M_current--); }
376
377 // Random access iterator requirements
378 reference
379 operator[](const difference_type& __n) const
380 { return _M_current[__n]; }
381
382 __normal_iterator&
383 operator+=(const difference_type& __n)
384 { _M_current += __n; return *this; }
385
386 __normal_iterator
387 operator+(const difference_type& __n) const
388 { return __normal_iterator(_M_current + __n); }
389
390 __normal_iterator&
391 operator-=(const difference_type& __n)
392 { _M_current -= __n; return *this; }
393
394 __normal_iterator
395 operator-(const difference_type& __n) const
396 { return __normal_iterator(_M_current - __n); }
397
398 difference_type
399 operator-(const __normal_iterator& __i) const
400 { return _M_current - __i._M_current; }
401
402 const _Iterator&
403 base() const { return _M_current; }
404 };
405
406 // Forward iterator requirements
407 template<typename _IteratorL, typename _IteratorR, typename _Container>
408 inline bool
409 operator==(const __normal_iterator<_IteratorL, _Container>& __lhs,
410 const __normal_iterator<_IteratorR, _Container>& __rhs)
411 { return __lhs.base() == __rhs.base(); }
412
413 template<typename _IteratorL, typename _IteratorR, typename _Container>
414 inline bool
415 operator!=(const __normal_iterator<_IteratorL, _Container>& __lhs,
416 const __normal_iterator<_IteratorR, _Container>& __rhs)
417 { return !(__lhs == __rhs); }
418
419 // Random access iterator requirements
420 template<typename _IteratorL, typename _IteratorR, typename _Container>
421 inline bool
422 operator<(const __normal_iterator<_IteratorL, _Container>& __lhs,
423 const __normal_iterator<_IteratorR, _Container>& __rhs)
424 { return __lhs.base() < __rhs.base(); }
425
426 template<typename _IteratorL, typename _IteratorR, typename _Container>
427 inline bool
428 operator>(const __normal_iterator<_IteratorL, _Container>& __lhs,
429 const __normal_iterator<_IteratorR, _Container>& __rhs)
430 { return __rhs < __lhs; }
431
432 template<typename _IteratorL, typename _IteratorR, typename _Container>
433 inline bool
434 operator<=(const __normal_iterator<_IteratorL, _Container>& __lhs,
435 const __normal_iterator<_IteratorR, _Container>& __rhs)
436 { return !(__rhs < __lhs); }
437
438 template<typename _IteratorL, typename _IteratorR, typename _Container>
439 inline bool
440 operator>=(const __normal_iterator<_IteratorL, _Container>& __lhs,
441 const __normal_iterator<_IteratorR, _Container>& __rhs)
442 { return !(__lhs < __rhs); }
443
444 template<typename _Iterator, typename _Container>
445 inline __normal_iterator<_Iterator, _Container>
446 operator+(typename __normal_iterator<_Iterator, _Container>::difference_type __n,
447 const __normal_iterator<_Iterator, _Container>& __i)
448 { return __normal_iterator<_Iterator, _Container>(__i.base() + __n); }
449 } // namespace std
450
451 #endif
452
453 // Local Variables:
454 // mode:C++
455 // End: