stl_iterator.h

Go to the documentation of this file.
00001 // Iterators -*- C++ -*- 00002 00003 // Copyright (C) 2001, 2002 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 2, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // You should have received a copy of the GNU General Public License along 00017 // with this library; see the file COPYING. If not, write to the Free 00018 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 00019 // USA. 00020 00021 // As a special exception, you may use this file as part of a free software 00022 // library without restriction. Specifically, if other files instantiate 00023 // templates or use macros or inline functions from this file, or you compile 00024 // this file and link it with other files to produce an executable, this 00025 // file does not by itself cause the resulting executable to be covered by 00026 // the GNU General Public License. This exception does not however 00027 // invalidate any other reasons why the executable file might be covered by 00028 // the GNU General Public License. 00029 00030 /* 00031 * 00032 * Copyright (c) 1994 00033 * Hewlett-Packard Company 00034 * 00035 * Permission to use, copy, modify, distribute and sell this software 00036 * and its documentation for any purpose is hereby granted without fee, 00037 * provided that the above copyright notice appear in all copies and 00038 * that both that copyright notice and this permission notice appear 00039 * in supporting documentation. Hewlett-Packard Company makes no 00040 * representations about the suitability of this software for any 00041 * purpose. It is provided "as is" without express or implied warranty. 00042 * 00043 * 00044 * Copyright (c) 1996-1998 00045 * Silicon Graphics Computer Systems, Inc. 00046 * 00047 * Permission to use, copy, modify, distribute and sell this software 00048 * and its documentation for any purpose is hereby granted without fee, 00049 * provided that the above copyright notice appear in all copies and 00050 * that both that copyright notice and this permission notice appear 00051 * in supporting documentation. Silicon Graphics makes no 00052 * representations about the suitability of this software for any 00053 * purpose. It is provided "as is" without express or implied warranty. 00054 */ 00055 00065 #ifndef __GLIBCPP_INTERNAL_ITERATOR_H 00066 #define __GLIBCPP_INTERNAL_ITERATOR_H 00067 00068 namespace std 00069 { 00070 // 24.4.1 Reverse iterators 00089 template<typename _Iterator> 00090 class reverse_iterator 00091 : public iterator<typename iterator_traits<_Iterator>::iterator_category, 00092 typename iterator_traits<_Iterator>::value_type, 00093 typename iterator_traits<_Iterator>::difference_type, 00094 typename iterator_traits<_Iterator>::pointer, 00095 typename iterator_traits<_Iterator>::reference> 00096 { 00097 protected: 00098 _Iterator current; 00099 00100 public: 00101 typedef _Iterator iterator_type; 00102 typedef typename iterator_traits<_Iterator>::difference_type 00103 difference_type; 00104 typedef typename iterator_traits<_Iterator>::reference reference; 00105 typedef typename iterator_traits<_Iterator>::pointer pointer; 00106 00107 public: 00111 reverse_iterator() { } 00112 00116 explicit 00117 reverse_iterator(iterator_type __x) : current(__x) { } 00118 00122 reverse_iterator(const reverse_iterator& __x) 00123 : current(__x.current) { } 00124 00129 template<typename _Iter> 00130 reverse_iterator(const reverse_iterator<_Iter>& __x) 00131 : current(__x.base()) { } 00132 00136 iterator_type 00137 base() const { return current; } 00138 00144 reference 00145 operator*() const 00146 { 00147 _Iterator __tmp = current; 00148 return *--__tmp; 00149 } 00150 00156 pointer 00157 operator->() const { return &(operator*()); } 00158 00164 reverse_iterator& 00165 operator++() 00166 { 00167 --current; 00168 return *this; 00169 } 00170 00176 reverse_iterator 00177 operator++(int) 00178 { 00179 reverse_iterator __tmp = *this; 00180 --current; 00181 return __tmp; 00182 } 00183 00189 reverse_iterator& 00190 operator--() 00191 { 00192 ++current; 00193 return *this; 00194 } 00195 00201 reverse_iterator operator--(int) 00202 { 00203 reverse_iterator __tmp = *this; 00204 ++current; 00205 return __tmp; 00206 } 00207 00213 reverse_iterator 00214 operator+(difference_type __n) const 00215 { return reverse_iterator(current - __n); } 00216 00222 reverse_iterator& 00223 operator+=(difference_type __n) 00224 { 00225 current -= __n; 00226 return *this; 00227 } 00228 00234 reverse_iterator 00235 operator-(difference_type __n) const 00236 { return reverse_iterator(current + __n); } 00237 00243 reverse_iterator& 00244 operator-=(difference_type __n) 00245 { 00246 current += __n; 00247 return *this; 00248 } 00249 00255 reference 00256 operator[](difference_type __n) const { return *(*this + __n); } 00257 }; 00258 00260 00269 template<typename _Iterator> 00270 inline bool 00271 operator==(const reverse_iterator<_Iterator>& __x, 00272 const reverse_iterator<_Iterator>& __y) 00273 { return __x.base() == __y.base(); } 00274 00275 template<typename _Iterator> 00276 inline bool 00277 operator<(const reverse_iterator<_Iterator>& __x, 00278 const reverse_iterator<_Iterator>& __y) 00279 { return __y.base() < __x.base(); } 00280 00281 template<typename _Iterator> 00282 inline bool 00283 operator!=(const reverse_iterator<_Iterator>& __x, 00284 const reverse_iterator<_Iterator>& __y) 00285 { return !(__x == __y); } 00286 00287 template<typename _Iterator> 00288 inline bool 00289 operator>(const reverse_iterator<_Iterator>& __x, 00290 const reverse_iterator<_Iterator>& __y) 00291 { return __y < __x; } 00292 00293 template<typename _Iterator> 00294 inline bool 00295 operator<=(const reverse_iterator<_Iterator>& __x, 00296 const reverse_iterator<_Iterator>& __y) 00297 { return !(__y < __x); } 00298 00299 template<typename _Iterator> 00300 inline bool 00301 operator>=(const reverse_iterator<_Iterator>& __x, 00302 const reverse_iterator<_Iterator>& __y) 00303 { return !(__x < __y); } 00304 00305 template<typename _Iterator> 00306 inline typename reverse_iterator<_Iterator>::difference_type 00307 operator-(const reverse_iterator<_Iterator>& __x, 00308 const reverse_iterator<_Iterator>& __y) 00309 { return __y.base() - __x.base(); } 00310 00311 template<typename _Iterator> 00312 inline reverse_iterator<_Iterator> 00313 operator+(typename reverse_iterator<_Iterator>::difference_type __n, 00314 const reverse_iterator<_Iterator>& __x) 00315 { return reverse_iterator<_Iterator>(__x.base() - __n); } 00317 00318 // 24.4.2.2.1 back_insert_iterator 00327 template<typename _Container> 00328 class back_insert_iterator 00329 : public iterator<output_iterator_tag, void, void, void, void> 00330 { 00331 protected: 00332 _Container* container; 00333 00334 public: 00336 typedef _Container container_type; 00337 00339 explicit 00340 back_insert_iterator(_Container& __x) : container(&__x) { } 00341 00353 back_insert_iterator& 00354 operator=(typename _Container::const_reference __value) 00355 { 00356 container->push_back(__value); 00357 return *this; 00358 } 00359 00361 back_insert_iterator& 00362 operator*() { return *this; } 00363 00365 back_insert_iterator& 00366 operator++() { return *this; } 00367 00369 back_insert_iterator 00370 operator++(int) { return *this; } 00371 }; 00372 00384 template<typename _Container> 00385 inline back_insert_iterator<_Container> 00386 back_inserter(_Container& __x) 00387 { return back_insert_iterator<_Container>(__x); } 00388 00397 template<typename _Container> 00398 class front_insert_iterator 00399 : public iterator<output_iterator_tag, void, void, void, void> 00400 { 00401 protected: 00402 _Container* container; 00403 00404 public: 00406 typedef _Container container_type; 00407 00409 explicit front_insert_iterator(_Container& __x) : container(&__x) { } 00410 00422 front_insert_iterator& 00423 operator=(typename _Container::const_reference __value) 00424 { 00425 container->push_front(__value); 00426 return *this; 00427 } 00428 00430 front_insert_iterator& 00431 operator*() { return *this; } 00432 00434 front_insert_iterator& 00435 operator++() { return *this; } 00436 00438 front_insert_iterator 00439 operator++(int) { return *this; } 00440 }; 00441 00453 template<typename _Container> 00454 inline front_insert_iterator<_Container> 00455 front_inserter(_Container& __x) 00456 { return front_insert_iterator<_Container>(__x); } 00457 00470 template<typename _Container> 00471 class insert_iterator 00472 : public iterator<output_iterator_tag, void, void, void, void> 00473 { 00474 protected: 00475 _Container* container; 00476 typename _Container::iterator iter; 00477 00478 public: 00480 typedef _Container container_type; 00481 00486 insert_iterator(_Container& __x, typename _Container::iterator __i) 00487 : container(&__x), iter(__i) {} 00488 00512 insert_iterator& 00513 operator=(const typename _Container::const_reference __value) 00514 { 00515 iter = container->insert(iter, __value); 00516 ++iter; 00517 return *this; 00518 } 00519 00521 insert_iterator& 00522 operator*() { return *this; } 00523 00525 insert_iterator& 00526 operator++() { return *this; } 00527 00529 insert_iterator& 00530 operator++(int) { return *this; } 00531 }; 00532 00544 template<typename _Container, typename _Iterator> 00545 inline insert_iterator<_Container> 00546 inserter(_Container& __x, _Iterator __i) 00547 { 00548 return insert_iterator<_Container>(__x, 00549 typename _Container::iterator(__i)); 00550 } 00551 } // namespace std 00552 00553 namespace __gnu_cxx 00554 { 00555 // This iterator adapter is 'normal' in the sense that it does not 00556 // change the semantics of any of the operators of its iterator 00557 // parameter. Its primary purpose is to convert an iterator that is 00558 // not a class, e.g. a pointer, into an iterator that is a class. 00559 // The _Container parameter exists solely so that different containers 00560 // using this template can instantiate different types, even if the 00561 // _Iterator parameter is the same. 00562 using std::iterator_traits; 00563 using std::iterator; 00564 template<typename _Iterator, typename _Container> 00565 class __normal_iterator 00566 : public iterator<typename iterator_traits<_Iterator>::iterator_category, 00567 typename iterator_traits<_Iterator>::value_type, 00568 typename iterator_traits<_Iterator>::difference_type, 00569 typename iterator_traits<_Iterator>::pointer, 00570 typename iterator_traits<_Iterator>::reference> 00571 { 00572 protected: 00573 _Iterator _M_current; 00574 00575 public: 00576 typedef typename iterator_traits<_Iterator>::difference_type 00577 difference_type; 00578 typedef typename iterator_traits<_Iterator>::reference reference; 00579 typedef typename iterator_traits<_Iterator>::pointer pointer; 00580 00581 __normal_iterator() : _M_current(_Iterator()) { } 00582 00583 explicit 00584 __normal_iterator(const _Iterator& __i) : _M_current(__i) { } 00585 00586 // Allow iterator to const_iterator conversion 00587 template<typename _Iter> 00588 inline __normal_iterator(const __normal_iterator<_Iter, _Container>& __i) 00589 : _M_current(__i.base()) { } 00590 00591 // Forward iterator requirements 00592 reference 00593 operator*() const { return *_M_current; } 00594 00595 pointer 00596 operator->() const { return _M_current; } 00597 00598 __normal_iterator& 00599 operator++() { ++_M_current; return *this; } 00600 00601 __normal_iterator 00602 operator++(int) { return __normal_iterator(_M_current++); } 00603 00604 // Bidirectional iterator requirements 00605 __normal_iterator& 00606 operator--() { --_M_current; return *this; } 00607 00608 __normal_iterator 00609 operator--(int) { return __normal_iterator(_M_current--); } 00610 00611 // Random access iterator requirements 00612 reference 00613 operator[](const difference_type& __n) const 00614 { return _M_current[__n]; } 00615 00616 __normal_iterator& 00617 operator+=(const difference_type& __n) 00618 { _M_current += __n; return *this; } 00619 00620 __normal_iterator 00621 operator+(const difference_type& __n) const 00622 { return __normal_iterator(_M_current + __n); } 00623 00624 __normal_iterator& 00625 operator-=(const difference_type& __n) 00626 { _M_current -= __n; return *this; } 00627 00628 __normal_iterator 00629 operator-(const difference_type& __n) const 00630 { return __normal_iterator(_M_current - __n); } 00631 00632 const _Iterator& 00633 base() const { return _M_current; } 00634 }; 00635 00636 // Note: In what follows, the left- and right-hand-side iterators are 00637 // allowed to vary in types (conceptually in cv-qualification) so that 00638 // comparaison between cv-qualified and non-cv-qualified iterators be 00639 // valid. However, the greedy and unfriendly operators in std::rel_ops 00640 // will make overload resolution ambiguous (when in scope) if we don't 00641 // provide overloads whose operands are of the same type. Can someone 00642 // remind me what generic programming is about? -- Gaby 00643 00644 // Forward iterator requirements 00645 template<typename _IteratorL, typename _IteratorR, typename _Container> 00646 inline bool 00647 operator==(const __normal_iterator<_IteratorL, _Container>& __lhs, 00648 const __normal_iterator<_IteratorR, _Container>& __rhs) 00649 { return __lhs.base() == __rhs.base(); } 00650 00651 template<typename _Iterator, typename _Container> 00652 inline bool 00653 operator==(const __normal_iterator<_Iterator, _Container>& __lhs, 00654 const __normal_iterator<_Iterator, _Container>& __rhs) 00655 { return __lhs.base() == __rhs.base(); } 00656 00657 template<typename _IteratorL, typename _IteratorR, typename _Container> 00658 inline bool 00659 operator!=(const __normal_iterator<_IteratorL, _Container>& __lhs, 00660 const __normal_iterator<_IteratorR, _Container>& __rhs) 00661 { return __lhs.base() != __rhs.base(); } 00662 00663 template<typename _Iterator, typename _Container> 00664 inline bool 00665 operator!=(const __normal_iterator<_Iterator, _Container>& __lhs, 00666 const __normal_iterator<_Iterator, _Container>& __rhs) 00667 { return __lhs.base() != __rhs.base(); } 00668 00669 // Random access iterator requirements 00670 template<typename _IteratorL, typename _IteratorR, typename _Container> 00671 inline bool 00672 operator<(const __normal_iterator<_IteratorL, _Container>& __lhs, 00673 const __normal_iterator<_IteratorR, _Container>& __rhs) 00674 { return __lhs.base() < __rhs.base(); } 00675 00676 template<typename _Iterator, typename _Container> 00677 inline bool 00678 operator<(const __normal_iterator<_Iterator, _Container>& __lhs, 00679 const __normal_iterator<_Iterator, _Container>& __rhs) 00680 { return __lhs.base() < __rhs.base(); } 00681 00682 template<typename _IteratorL, typename _IteratorR, typename _Container> 00683 inline bool 00684 operator>(const __normal_iterator<_IteratorL, _Container>& __lhs, 00685 const __normal_iterator<_IteratorR, _Container>& __rhs) 00686 { return __lhs.base() > __rhs.base(); } 00687 00688 template<typename _Iterator, typename _Container> 00689 inline bool 00690 operator>(const __normal_iterator<_Iterator, _Container>& __lhs, 00691 const __normal_iterator<_Iterator, _Container>& __rhs) 00692 { return __lhs.base() > __rhs.base(); } 00693 00694 template<typename _IteratorL, typename _IteratorR, typename _Container> 00695 inline bool 00696 operator<=(const __normal_iterator<_IteratorL, _Container>& __lhs, 00697 const __normal_iterator<_IteratorR, _Container>& __rhs) 00698 { return __lhs.base() <= __rhs.base(); } 00699 00700 template<typename _Iterator, typename _Container> 00701 inline bool 00702 operator<=(const __normal_iterator<_Iterator, _Container>& __lhs, 00703 const __normal_iterator<_Iterator, _Container>& __rhs) 00704 { return __lhs.base() <= __rhs.base(); } 00705 00706 template<typename _IteratorL, typename _IteratorR, typename _Container> 00707 inline bool 00708 operator>=(const __normal_iterator<_IteratorL, _Container>& __lhs, 00709 const __normal_iterator<_IteratorR, _Container>& __rhs) 00710 { return __lhs.base() >= __rhs.base(); } 00711 00712 template<typename _Iterator, typename _Container> 00713 inline bool 00714 operator>=(const __normal_iterator<_Iterator, _Container>& __lhs, 00715 const __normal_iterator<_Iterator, _Container>& __rhs) 00716 { return __lhs.base() >= __rhs.base(); } 00717 00718 // _GLIBCPP_RESOLVE_LIB_DEFECTS 00719 // According to the resolution of DR179 not only the various comparison 00720 // operators but also operator- must accept mixed iterator/const_iterator 00721 // parameters. 00722 template<typename _IteratorL, typename _IteratorR, typename _Container> 00723 inline typename __normal_iterator<_IteratorL, _Container>::difference_type 00724 operator-(const __normal_iterator<_IteratorL, _Container>& __lhs, 00725 const __normal_iterator<_IteratorR, _Container>& __rhs) 00726 { return __lhs.base() - __rhs.base(); } 00727 00728 template<typename _Iterator, typename _Container> 00729 inline __normal_iterator<_Iterator, _Container> 00730 operator+(typename __normal_iterator<_Iterator, _Container>::difference_type __n, 00731 const __normal_iterator<_Iterator, _Container>& __i) 00732 { return __normal_iterator<_Iterator, _Container>(__i.base() + __n); } 00733 } // namespace __gnu_cxx 00734 00735 #endif 00736 00737 // Local Variables: 00738 // mode:C++ 00739 // End:

Generated on Wed Sep 29 13:54:51 2004 for libstdc++-v3 Source by doxygen 1.3.7