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 /* NOTE: This is an internal header file, included by other STL headers.
57 * You should not attempt to use it directly.
60 #ifndef __SGI_STL_INTERNAL_ITERATOR_H
61 #define __SGI_STL_INTERNAL_ITERATOR_H
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
>
78 typedef _Iterator iterator_type
;
79 typedef typename iterator_traits
<_Iterator
>::difference_type
81 typedef typename iterator_traits
<_Iterator
>::reference reference
;
82 typedef typename iterator_traits
<_Iterator
>::pointer pointer
;
88 reverse_iterator(iterator_type __x
) : current(__x
) {}
90 reverse_iterator(const reverse_iterator
& __x
)
91 : current(__x
.current
) { }
93 template<typename _Iter
>
94 reverse_iterator(const reverse_iterator
<_Iter
>& __x
)
95 : current(__x
.base()) {}
98 base() const { return current
; }
103 _Iterator __tmp
= current
;
108 operator->() const { return &(operator*()); }
120 reverse_iterator __tmp
= *this;
132 reverse_iterator
operator--(int)
134 reverse_iterator __tmp
= *this;
140 operator+(difference_type __n
) const
141 { return reverse_iterator(current
- __n
); }
144 operator+=(difference_type __n
)
151 operator-(difference_type __n
) const
152 { return reverse_iterator(current
+ __n
); }
155 operator-=(difference_type __n
)
162 operator[](difference_type __n
) const { return *(*this + __n
); }
165 template<typename _Iterator
>
167 operator==(const reverse_iterator
<_Iterator
>& __x
,
168 const reverse_iterator
<_Iterator
>& __y
)
169 { return __x
.base() == __y
.base(); }
171 template<typename _Iterator
>
173 operator<(const reverse_iterator
<_Iterator
>& __x
,
174 const reverse_iterator
<_Iterator
>& __y
)
175 { return __y
.base() < __x
.base(); }
177 template<typename _Iterator
>
179 operator!=(const reverse_iterator
<_Iterator
>& __x
,
180 const reverse_iterator
<_Iterator
>& __y
)
181 { return !(__x
== __y
); }
183 template<typename _Iterator
>
185 operator>(const reverse_iterator
<_Iterator
>& __x
,
186 const reverse_iterator
<_Iterator
>& __y
)
187 { return __y
< __x
; }
189 template<typename _Iterator
>
191 operator<=(const reverse_iterator
<_Iterator
>& __x
,
192 const reverse_iterator
<_Iterator
>& __y
)
193 { return !(__y
< __x
); }
195 template<typename _Iterator
>
197 operator>=(const reverse_iterator
<_Iterator
>& __x
,
198 const reverse_iterator
<_Iterator
>& __y
)
199 { return !(__x
< __y
); }
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(); }
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
); }
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>
219 _Container
* container
;
222 typedef _Container container_type
;
225 back_insert_iterator(_Container
& __x
) : container(&__x
) {}
227 back_insert_iterator
&
228 operator=(typename
_Container::const_reference __value
)
230 container
->push_back(__value
);
234 back_insert_iterator
&
235 operator*() { return *this; }
237 back_insert_iterator
&
238 operator++() { return *this; }
241 operator++(int) { return *this; }
244 template<typename _Container
>
245 inline back_insert_iterator
<_Container
>
246 back_inserter(_Container
& __x
)
247 { return back_insert_iterator
<_Container
>(__x
); }
249 template<typename _Container
>
250 class front_insert_iterator
251 : public iterator
<output_iterator_tag
, void, void, void, void>
254 _Container
* container
;
257 typedef _Container container_type
;
259 explicit front_insert_iterator(_Container
& __x
) : container(&__x
) {}
261 front_insert_iterator
&
262 operator=(typename
_Container::const_reference __value
)
264 container
->push_front(__value
);
268 front_insert_iterator
&
269 operator*() { return *this; }
271 front_insert_iterator
&
272 operator++() { return *this; }
274 front_insert_iterator
275 operator++(int) { return *this; }
278 template<typename _Container
>
279 inline front_insert_iterator
<_Container
> front_inserter(_Container
& __x
)
280 { return front_insert_iterator
<_Container
>(__x
); }
282 template<typename _Container
>
283 class insert_iterator
284 : public iterator
<output_iterator_tag
, void, void, void, void>
287 _Container
* container
;
288 typename
_Container::iterator iter
;
291 typedef _Container container_type
;
293 insert_iterator(_Container
& __x
, typename
_Container::iterator __i
)
294 : container(&__x
), iter(__i
) {}
297 operator=(const typename
_Container::const_reference __value
)
299 iter
= container
->insert(iter
, __value
);
305 operator*() { return *this; }
308 operator++() { return *this; }
311 operator++(int) { return *this; }
314 template<typename _Container
, typename _Iterator
>
316 insert_iterator
<_Container
> inserter(_Container
& __x
, _Iterator __i
)
318 typedef typename
_Container::iterator __iter
;
319 return insert_iterator
<_Container
>(__x
, __iter(__i
));
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
>
338 _Iterator _M_current
;
341 typedef typename iterator_traits
<_Iterator
>::difference_type
343 typedef typename iterator_traits
<_Iterator
>::reference reference
;
344 typedef typename iterator_traits
<_Iterator
>::pointer pointer
;
346 __normal_iterator() : _M_current(_Iterator()) { }
349 __normal_iterator(const _Iterator
& __i
) : _M_current(__i
) { }
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()) { }
356 // Forward iterator requirements
358 operator*() const { return *_M_current
; }
361 operator->() const { return _M_current
; }
364 operator++() { ++_M_current
; return *this; }
367 operator++(int) { return __normal_iterator(_M_current
++); }
369 // Bidirectional iterator requirements
371 operator--() { --_M_current
; return *this; }
374 operator--(int) { return __normal_iterator(_M_current
--); }
376 // Random access iterator requirements
378 operator[](const difference_type
& __n
) const
379 { return _M_current
[__n
]; }
382 operator+=(const difference_type
& __n
)
383 { _M_current
+= __n
; return *this; }
386 operator+(const difference_type
& __n
) const
387 { return __normal_iterator(_M_current
+ __n
); }
390 operator-=(const difference_type
& __n
)
391 { _M_current
-= __n
; return *this; }
394 operator-(const difference_type
& __n
) const
395 { return __normal_iterator(_M_current
- __n
); }
398 operator-(const __normal_iterator
& __i
) const
399 { return _M_current
- __i
._M_current
; }
402 base() const { return _M_current
; }
405 // Forward iterator requirements
406 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
408 operator==(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
409 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
410 { return __lhs
.base() == __rhs
.base(); }
412 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
414 operator!=(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
415 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
416 { return !(__lhs
== __rhs
); }
418 // Random access iterator requirements
419 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
421 operator<(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
422 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
423 { return __lhs
.base() < __rhs
.base(); }
425 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
427 operator>(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
428 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
429 { return __rhs
< __lhs
; }
431 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
433 operator<=(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
434 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
435 { return !(__rhs
< __lhs
); }
437 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
439 operator>=(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
440 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
441 { return !(__lhs
< __rhs
); }
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
); }