1 // Iterators -*- C++ -*-
3 // Copyright (C) 2001, 2002 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;
86 reverse_iterator() { }
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>
281 front_inserter(_Container& __x)
282 { return front_insert_iterator<_Container>(__x); }
284 template<typename _Container>
285 class insert_iterator
286 : public iterator<output_iterator_tag, void, void, void, void>
289 _Container* container;
290 typename _Container::iterator iter;
293 typedef _Container container_type;
295 insert_iterator(_Container& __x, typename _Container::iterator __i)
296 : container(&__x), iter(__i) {}
299 operator=(const typename _Container::const_reference __value)
301 iter = container->insert(iter, __value);
307 operator*() { return *this; }
310 operator++() { return *this; }
313 operator++(int) { return *this; }
316 template<typename _Container, typename _Iterator>
317 inline insert_iterator<_Container>
318 inserter(_Container& __x, _Iterator __i)
320 return insert_iterator<_Container>(__x,
321 typename _Container::iterator(__i));
324 // This iterator adapter is 'normal' in the sense that it does not
325 // change the semantics of any of the operators of its iterator
326 // parameter. Its primary purpose is to convert an iterator that is
327 // not a class, e.g. a pointer, into an iterator that is a class.
328 // The _Container parameter exists solely so that different containers
329 // using this template can instantiate different types, even if the
330 // _Iterator parameter is the same.
331 template<typename _Iterator, typename _Container>
332 class __normal_iterator
333 : public iterator<typename iterator_traits<_Iterator>::iterator_category,
334 typename iterator_traits<_Iterator>::value_type,
335 typename iterator_traits<_Iterator>::difference_type,
336 typename iterator_traits<_Iterator>::pointer,
337 typename iterator_traits<_Iterator>::reference>
340 _Iterator _M_current;
343 typedef typename iterator_traits<_Iterator>::difference_type
345 typedef typename iterator_traits<_Iterator>::reference reference;
346 typedef typename iterator_traits<_Iterator>::pointer pointer;
348 __normal_iterator() : _M_current(_Iterator()) { }
351 __normal_iterator(const _Iterator& __i) : _M_current(__i) { }
353 // Allow iterator to const_iterator conversion
354 template<typename _Iter>
355 inline __normal_iterator(const __normal_iterator<_Iter, _Container>& __i)
356 : _M_current(__i.base()) { }
358 // Forward iterator requirements
360 operator*() const { return *_M_current; }
363 operator->() const { return _M_current; }
366 operator++() { ++_M_current; return *this; }
369 operator++(int) { return __normal_iterator(_M_current++); }
371 // Bidirectional iterator requirements
373 operator--() { --_M_current; return *this; }
376 operator--(int) { return __normal_iterator(_M_current--); }
378 // Random access iterator requirements
380 operator[](const difference_type& __n) const
381 { return _M_current[__n]; }
384 operator+=(const difference_type& __n)
385 { _M_current += __n; return *this; }
388 operator+(const difference_type& __n) const
389 { return __normal_iterator(_M_current + __n); }
392 operator-=(const difference_type& __n)
393 { _M_current -= __n; return *this; }
396 operator-(const difference_type& __n) const
397 { return __normal_iterator(_M_current - __n); }
400 operator-(const __normal_iterator& __i) const
401 { return _M_current - __i._M_current; }
404 base() const { return _M_current; }
407 // Forward iterator requirements
408 template<typename _IteratorL, typename _IteratorR, typename _Container>
410 operator==(const __normal_iterator<_IteratorL, _Container>& __lhs,
411 const __normal_iterator<_IteratorR, _Container>& __rhs)
412 { return __lhs.base() == __rhs.base(); }
414 template<typename _IteratorL, typename _IteratorR, typename _Container>
416 operator!=(const __normal_iterator<_IteratorL, _Container>& __lhs,
417 const __normal_iterator<_IteratorR, _Container>& __rhs)
418 { return !(__lhs == __rhs); }
420 // Random access iterator requirements
421 template<typename _IteratorL, typename _IteratorR, typename _Container>
423 operator<(const __normal_iterator<_IteratorL, _Container>& __lhs,
424 const __normal_iterator<_IteratorR, _Container>& __rhs)
425 { return __lhs.base() < __rhs.base(); }
427 template<typename _IteratorL, typename _IteratorR, typename _Container>
429 operator>(const __normal_iterator<_IteratorL, _Container>& __lhs,
430 const __normal_iterator<_IteratorR, _Container>& __rhs)
431 { return __rhs < __lhs; }
433 template<typename _IteratorL, typename _IteratorR, typename _Container>
435 operator<=(const __normal_iterator<_IteratorL, _Container>& __lhs,
436 const __normal_iterator<_IteratorR, _Container>& __rhs)
437 { return !(__rhs < __lhs); }
439 template<typename _IteratorL, typename _IteratorR, typename _Container>
441 operator>=(const __normal_iterator<_IteratorL, _Container>& __lhs,
442 const __normal_iterator<_IteratorR, _Container>& __rhs)
443 { return !(__lhs < __rhs); }
445 template<typename _Iterator, typename _Container>
446 inline __normal_iterator<_Iterator, _Container>
447 operator+(typename __normal_iterator<_Iterator, _Container>::difference_type __n,
448 const __normal_iterator<_Iterator, _Container>& __i)
449 { return __normal_iterator<_Iterator, _Container>(__i.base() + __n); }