OSDN Git Service

2002-01-16 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / stl_iterator.h
1 // Iterators -*- C++ -*-
2
3 // Copyright (C) 2001, 2002 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         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> 
281     front_inserter(_Container& __x) 
282     { return front_insert_iterator<_Container>(__x); }
283
284   template<typename _Container>
285     class insert_iterator 
286     : public iterator<output_iterator_tag, void, void, void, void>
287     {
288     protected:
289       _Container* container;
290       typename _Container::iterator iter;
291
292     public:
293       typedef _Container          container_type;
294       
295       insert_iterator(_Container& __x, typename _Container::iterator __i) 
296       : container(&__x), iter(__i) {}
297    
298       insert_iterator&
299       operator=(const typename _Container::const_reference __value) 
300       { 
301         iter = container->insert(iter, __value);
302         ++iter;
303         return *this;
304       }
305
306       insert_iterator& 
307       operator*() { return *this; }
308
309       insert_iterator& 
310       operator++() { return *this; }
311
312       insert_iterator& 
313       operator++(int) { return *this; }
314     };
315   
316   template<typename _Container, typename _Iterator>
317     inline insert_iterator<_Container> 
318     inserter(_Container& __x, _Iterator __i)
319     {
320       return insert_iterator<_Container>(__x, 
321                                          typename _Container::iterator(__i));
322     }
323   
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>
338     {
339     protected:
340       _Iterator _M_current;
341       
342     public:
343       typedef typename iterator_traits<_Iterator>::difference_type      
344                                                                difference_type;
345       typedef typename iterator_traits<_Iterator>::reference   reference;
346       typedef typename iterator_traits<_Iterator>::pointer     pointer;
347
348       __normal_iterator() : _M_current(_Iterator()) { }
349
350       explicit 
351       __normal_iterator(const _Iterator& __i) : _M_current(__i) { }
352
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()) { }
357
358       // Forward iterator requirements
359       reference
360       operator*() const { return *_M_current; }
361       
362       pointer
363       operator->() const { return _M_current; }
364       
365       __normal_iterator&
366       operator++() { ++_M_current; return *this; }
367       
368       __normal_iterator
369       operator++(int) { return __normal_iterator(_M_current++); }
370       
371       // Bidirectional iterator requirements
372       __normal_iterator&
373       operator--() { --_M_current; return *this; }
374       
375       __normal_iterator
376       operator--(int) { return __normal_iterator(_M_current--); }
377       
378       // Random access iterator requirements
379       reference
380       operator[](const difference_type& __n) const
381       { return _M_current[__n]; }
382       
383       __normal_iterator&
384       operator+=(const difference_type& __n)
385       { _M_current += __n; return *this; }
386
387       __normal_iterator
388       operator+(const difference_type& __n) const
389       { return __normal_iterator(_M_current + __n); }
390       
391       __normal_iterator&
392       operator-=(const difference_type& __n)
393       { _M_current -= __n; return *this; }
394       
395       __normal_iterator
396       operator-(const difference_type& __n) const
397       { return __normal_iterator(_M_current - __n); }
398       
399       difference_type
400       operator-(const __normal_iterator& __i) const
401       { return _M_current - __i._M_current; }
402       
403       const _Iterator& 
404       base() const { return _M_current; }
405     };
406
407   // Forward iterator requirements
408   template<typename _IteratorL, typename _IteratorR, typename _Container>
409   inline bool
410   operator==(const __normal_iterator<_IteratorL, _Container>& __lhs,
411              const __normal_iterator<_IteratorR, _Container>& __rhs)
412   { return __lhs.base() == __rhs.base(); }
413
414   template<typename _IteratorL, typename _IteratorR, typename _Container>
415   inline bool
416   operator!=(const __normal_iterator<_IteratorL, _Container>& __lhs,
417              const __normal_iterator<_IteratorR, _Container>& __rhs)
418   { return !(__lhs == __rhs); }
419
420   // Random access iterator requirements
421   template<typename _IteratorL, typename _IteratorR, typename _Container>
422   inline bool 
423   operator<(const __normal_iterator<_IteratorL, _Container>& __lhs,
424             const __normal_iterator<_IteratorR, _Container>& __rhs)
425   { return __lhs.base() < __rhs.base(); }
426
427   template<typename _IteratorL, typename _IteratorR, typename _Container>
428   inline bool
429   operator>(const __normal_iterator<_IteratorL, _Container>& __lhs,
430             const __normal_iterator<_IteratorR, _Container>& __rhs)
431   { return __rhs < __lhs; }
432
433   template<typename _IteratorL, typename _IteratorR, typename _Container>
434   inline bool
435   operator<=(const __normal_iterator<_IteratorL, _Container>& __lhs,
436              const __normal_iterator<_IteratorR, _Container>& __rhs)
437   { return !(__rhs < __lhs); }
438
439   template<typename _IteratorL, typename _IteratorR, typename _Container>
440   inline bool
441   operator>=(const __normal_iterator<_IteratorL, _Container>& __lhs,
442              const __normal_iterator<_IteratorR, _Container>& __rhs)
443   { return !(__lhs < __rhs); }
444
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); }
450 } // namespace std
451
452 #endif 
453
454 // Local Variables:
455 // mode:C++
456 // End: