1 // Iterators -*- C++ -*-
3 // Copyright (C) 2001 Free Software Foundation, Inc.
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)
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.
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,
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.
33 * Hewlett-Packard Company
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.
44 * Copyright (c) 1996-1998
45 * Silicon Graphics Computer Systems, Inc.
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.
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.
61 #ifndef __GLIBCPP_INTERNAL_ITERATOR_H
62 #define __GLIBCPP_INTERNAL_ITERATOR_H
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
>
79 typedef _Iterator iterator_type
;
80 typedef typename iterator_traits
<_Iterator
>::difference_type
82 typedef typename iterator_traits
<_Iterator
>::reference reference
;
83 typedef typename iterator_traits
<_Iterator
>::pointer pointer
;
89 reverse_iterator(iterator_type __x
) : current(__x
) {}
91 reverse_iterator(const reverse_iterator
& __x
)
92 : current(__x
.current
) { }
94 template<typename _Iter
>
95 reverse_iterator(const reverse_iterator
<_Iter
>& __x
)
96 : current(__x
.base()) {}
99 base() const { return current
; }
104 _Iterator __tmp
= current
;
109 operator->() const { return &(operator*()); }
121 reverse_iterator __tmp
= *this;
133 reverse_iterator
operator--(int)
135 reverse_iterator __tmp
= *this;
141 operator+(difference_type __n
) const
142 { return reverse_iterator(current
- __n
); }
145 operator+=(difference_type __n
)
152 operator-(difference_type __n
) const
153 { return reverse_iterator(current
+ __n
); }
156 operator-=(difference_type __n
)
163 operator[](difference_type __n
) const { return *(*this + __n
); }
166 template<typename _Iterator
>
168 operator==(const reverse_iterator
<_Iterator
>& __x
,
169 const reverse_iterator
<_Iterator
>& __y
)
170 { return __x
.base() == __y
.base(); }
172 template<typename _Iterator
>
174 operator<(const reverse_iterator
<_Iterator
>& __x
,
175 const reverse_iterator
<_Iterator
>& __y
)
176 { return __y
.base() < __x
.base(); }
178 template<typename _Iterator
>
180 operator!=(const reverse_iterator
<_Iterator
>& __x
,
181 const reverse_iterator
<_Iterator
>& __y
)
182 { return !(__x
== __y
); }
184 template<typename _Iterator
>
186 operator>(const reverse_iterator
<_Iterator
>& __x
,
187 const reverse_iterator
<_Iterator
>& __y
)
188 { return __y
< __x
; }
190 template<typename _Iterator
>
192 operator<=(const reverse_iterator
<_Iterator
>& __x
,
193 const reverse_iterator
<_Iterator
>& __y
)
194 { return !(__y
< __x
); }
196 template<typename _Iterator
>
198 operator>=(const reverse_iterator
<_Iterator
>& __x
,
199 const reverse_iterator
<_Iterator
>& __y
)
200 { return !(__x
< __y
); }
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(); }
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
); }
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>
220 _Container
* container
;
223 typedef _Container container_type
;
226 back_insert_iterator(_Container
& __x
) : container(&__x
) {}
228 back_insert_iterator
&
229 operator=(typename
_Container::const_reference __value
)
231 container
->push_back(__value
);
235 back_insert_iterator
&
236 operator*() { return *this; }
238 back_insert_iterator
&
239 operator++() { return *this; }
242 operator++(int) { return *this; }
245 template<typename _Container
>
246 inline back_insert_iterator
<_Container
>
247 back_inserter(_Container
& __x
)
248 { return back_insert_iterator
<_Container
>(__x
); }
250 template<typename _Container
>
251 class front_insert_iterator
252 : public iterator
<output_iterator_tag
, void, void, void, void>
255 _Container
* container
;
258 typedef _Container container_type
;
260 explicit front_insert_iterator(_Container
& __x
) : container(&__x
) {}
262 front_insert_iterator
&
263 operator=(typename
_Container::const_reference __value
)
265 container
->push_front(__value
);
269 front_insert_iterator
&
270 operator*() { return *this; }
272 front_insert_iterator
&
273 operator++() { return *this; }
275 front_insert_iterator
276 operator++(int) { return *this; }
279 template<typename _Container
>
280 inline front_insert_iterator
<_Container
> front_inserter(_Container
& __x
)
281 { return front_insert_iterator
<_Container
>(__x
); }
283 template<typename _Container
>
284 class insert_iterator
285 : public iterator
<output_iterator_tag
, void, void, void, void>
288 _Container
* container
;
289 typename
_Container::iterator iter
;
292 typedef _Container container_type
;
294 insert_iterator(_Container
& __x
, typename
_Container::iterator __i
)
295 : container(&__x
), iter(__i
) {}
298 operator=(const typename
_Container::const_reference __value
)
300 iter
= container
->insert(iter
, __value
);
306 operator*() { return *this; }
309 operator++() { return *this; }
312 operator++(int) { return *this; }
315 template<typename _Container
, typename _Iterator
>
317 insert_iterator
<_Container
> inserter(_Container
& __x
, _Iterator __i
)
319 typedef typename
_Container::iterator __iter
;
320 return insert_iterator
<_Container
>(__x
, __iter(__i
));
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
>
339 _Iterator _M_current
;
342 typedef typename iterator_traits
<_Iterator
>::difference_type
344 typedef typename iterator_traits
<_Iterator
>::reference reference
;
345 typedef typename iterator_traits
<_Iterator
>::pointer pointer
;
347 __normal_iterator() : _M_current(_Iterator()) { }
350 __normal_iterator(const _Iterator
& __i
) : _M_current(__i
) { }
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()) { }
357 // Forward iterator requirements
359 operator*() const { return *_M_current
; }
362 operator->() const { return _M_current
; }
365 operator++() { ++_M_current
; return *this; }
368 operator++(int) { return __normal_iterator(_M_current
++); }
370 // Bidirectional iterator requirements
372 operator--() { --_M_current
; return *this; }
375 operator--(int) { return __normal_iterator(_M_current
--); }
377 // Random access iterator requirements
379 operator[](const difference_type
& __n
) const
380 { return _M_current
[__n
]; }
383 operator+=(const difference_type
& __n
)
384 { _M_current
+= __n
; return *this; }
387 operator+(const difference_type
& __n
) const
388 { return __normal_iterator(_M_current
+ __n
); }
391 operator-=(const difference_type
& __n
)
392 { _M_current
-= __n
; return *this; }
395 operator-(const difference_type
& __n
) const
396 { return __normal_iterator(_M_current
- __n
); }
399 operator-(const __normal_iterator
& __i
) const
400 { return _M_current
- __i
._M_current
; }
403 base() const { return _M_current
; }
406 // Forward iterator requirements
407 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
409 operator==(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
410 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
411 { return __lhs
.base() == __rhs
.base(); }
413 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
415 operator!=(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
416 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
417 { return !(__lhs
== __rhs
); }
419 // Random access iterator requirements
420 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
422 operator<(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
423 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
424 { return __lhs
.base() < __rhs
.base(); }
426 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
428 operator>(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
429 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
430 { return __rhs
< __lhs
; }
432 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
434 operator<=(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
435 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
436 { return !(__rhs
< __lhs
); }
438 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
440 operator>=(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
441 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
442 { return !(__lhs
< __rhs
); }
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
); }