From: Benjamin Kosnik Date: Sat, 23 Jun 2001 00:08:47 +0000 (+0000) Subject: stl_iterator.h (reverse_iterator): Inherit from iterator. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=c766fc5f1c12823fed58d9a3d3ef4d67fca19985;p=gcc.git stl_iterator.h (reverse_iterator): Inherit from iterator. 2001-06-22 Benjamin Kosnik * include/bits/stl_iterator.h (reverse_iterator): Inherit from iterator. (back_insert_iterator): Same. (front_insert_iterator): Same. (insert_iterator): Same. * testsuite/20_util/raw_storage_iterator.cc: Modify. * testsuite/24_iterators/reverse_iterator.cc: New file. * testsuite/24_iterators/back_insert_iterator.cc: New file. * testsuite/24_iterators/front_insert_iterator.cc: New file. * testsuite/24_iterators/insert_iterator.cc: New file. From-SVN: r43524 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index c8e5f2147e4..9623194beb4 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,17 @@ +2001-06-22 Benjamin Kosnik + + * include/bits/stl_iterator.h (reverse_iterator): Inherit from + iterator. + (back_insert_iterator): Same. + (front_insert_iterator): Same. + (insert_iterator): Same. + + * testsuite/20_util/raw_storage_iterator.cc: Modify. + * testsuite/24_iterators/reverse_iterator.cc: New file. + * testsuite/24_iterators/back_insert_iterator.cc: New file. + * testsuite/24_iterators/front_insert_iterator.cc: New file. + * testsuite/24_iterators/insert_iterator.cc: New file. + 2001-06-22 Phil Edwards * include/*: Revert comment/license change from yesterday for all diff --git a/libstdc++-v3/include/bits/stl_iterator.h b/libstdc++-v3/include/bits/stl_iterator.h index a062d4dcb01..148cad6a184 100644 --- a/libstdc++-v3/include/bits/stl_iterator.h +++ b/libstdc++-v3/include/bits/stl_iterator.h @@ -33,117 +33,272 @@ namespace std { - -template -class back_insert_iterator { -protected: - _Container* container; -public: - typedef _Container container_type; - typedef output_iterator_tag iterator_category; - typedef void value_type; - typedef void difference_type; - typedef void pointer; - typedef void reference; - - explicit back_insert_iterator(_Container& __x) : container(&__x) {} - back_insert_iterator<_Container>& - operator=(const typename _Container::value_type& __value) { - container->push_back(__value); - return *this; - } - back_insert_iterator<_Container>& operator*() { return *this; } - back_insert_iterator<_Container>& operator++() { return *this; } - back_insert_iterator<_Container>& operator++(int) { return *this; } -}; - -template -inline back_insert_iterator<_Container> back_inserter(_Container& __x) { - return back_insert_iterator<_Container>(__x); -} - -template -class front_insert_iterator { -protected: - _Container* container; -public: - typedef _Container container_type; - typedef output_iterator_tag iterator_category; - typedef void value_type; - typedef void difference_type; - typedef void pointer; - typedef void reference; - - explicit front_insert_iterator(_Container& __x) : container(&__x) {} - front_insert_iterator<_Container>& - operator=(const typename _Container::value_type& __value) { - container->push_front(__value); - return *this; + // 24.4.1 Reverse iterators + template + class reverse_iterator + : public iterator::iterator_category, + iterator_traits<_Iterator>::value_type, + iterator_traits<_Iterator>::difference_type, + iterator_traits<_Iterator>::pointer, + iterator_traits<_Iterator>::reference> + { + protected: + _Iterator current; + + public: + typedef iterator_traits<_Iterator> __traits_type; + typedef typename __traits_type::iterator_category iterator_category; + typedef typename __traits_type::value_type value_type; + typedef typename __traits_type::difference_type difference_type; + typedef typename __traits_type::pointer pointer; + typedef typename __traits_type::reference reference; + + typedef _Iterator iterator_type; + typedef reverse_iterator<_Iterator> _Self; + + public: + reverse_iterator() {} + + explicit + reverse_iterator(iterator_type __x) : current(__x) {} + + reverse_iterator(const _Self& __x) : current(__x.current) {} + + template + reverse_iterator(const reverse_iterator<_Iter>& __x) + : current(__x.base()) {} + + iterator_type + base() const { return current; } + + reference + operator*() const + { + _Iterator __tmp = current; + return *--__tmp; + } + + pointer + operator->() const { return &(operator*()); } + + _Self& + operator++() + { + --current; + return *this; + } + + _Self + operator++(int) + { + _Self __tmp = *this; + --current; + return __tmp; + } + + _Self& + operator--() + { + ++current; + return *this; + } + + _Self operator--(int) + { + _Self __tmp = *this; + ++current; + return __tmp; + } + + _Self + operator+(difference_type __n) const + { return _Self(current - __n); } + + _Self& + operator+=(difference_type __n) + { + current -= __n; + return *this; + } + + _Self + operator-(difference_type __n) const + { return _Self(current + __n); } + + _Self& + operator-=(difference_type __n) + { + current += __n; + return *this; + } + + reference + operator[](difference_type __n) const { return *(*this + __n); } + }; + + template + inline bool + operator==(const reverse_iterator<_Iterator>& __x, + const reverse_iterator<_Iterator>& __y) + { return __x.base() == __y.base(); } + + template + inline bool + operator<(const reverse_iterator<_Iterator>& __x, + const reverse_iterator<_Iterator>& __y) + { return __y.base() < __x.base(); } + + template + inline bool + operator!=(const reverse_iterator<_Iterator>& __x, + const reverse_iterator<_Iterator>& __y) + { return !(__x == __y); } + + template + inline bool + operator>(const reverse_iterator<_Iterator>& __x, + const reverse_iterator<_Iterator>& __y) + { return __y < __x; } + + template + inline bool + operator<=(const reverse_iterator<_Iterator>& __x, + const reverse_iterator<_Iterator>& __y) + { return !(__y < __x); } + + template + inline bool + operator>=(const reverse_iterator<_Iterator>& __x, + const reverse_iterator<_Iterator>& __y) + { return !(__x < __y); } + + template + inline typename reverse_iterator<_Iterator>::difference_type + operator-(const reverse_iterator<_Iterator>& __x, + const reverse_iterator<_Iterator>& __y) + { return __y.base() - __x.base(); } + + template + inline reverse_iterator<_Iterator> + operator+(typename reverse_iterator<_Iterator>::difference_type __n, + const reverse_iterator<_Iterator>& __x) + { return reverse_iterator<_Iterator>(__x.base() - __n); } + + // 24.4.2.2.1 back_insert_iterator + template + class back_insert_iterator + : public iterator + { + protected: + _Container* container; + + public: + typedef _Container container_type; + + explicit + back_insert_iterator(_Container& __x) : container(&__x) {} + + back_insert_iterator<_Container>& + operator=(const typename _Container::value_type& __value) + { + container->push_back(__value); + return *this; + } + + back_insert_iterator<_Container>& + operator*() { return *this; } + + back_insert_iterator<_Container>& + operator++() { return *this; } + + back_insert_iterator<_Container>& + operator++(int) { return *this; } + }; + + template + inline back_insert_iterator<_Container> + back_inserter(_Container& __x) + { return back_insert_iterator<_Container>(__x); } + + template + class front_insert_iterator + : public iterator + { + protected: + _Container* container; + + public: + typedef _Container container_type; + + explicit front_insert_iterator(_Container& __x) : container(&__x) {} + front_insert_iterator<_Container>& + operator=(const typename _Container::value_type& __value) { + container->push_front(__value); + return *this; + } + front_insert_iterator<_Container>& operator*() { return *this; } + front_insert_iterator<_Container>& operator++() { return *this; } + front_insert_iterator<_Container>& operator++(int) { return *this; } + }; + + template + inline front_insert_iterator<_Container> front_inserter(_Container& __x) { + return front_insert_iterator<_Container>(__x); } - front_insert_iterator<_Container>& operator*() { return *this; } - front_insert_iterator<_Container>& operator++() { return *this; } - front_insert_iterator<_Container>& operator++(int) { return *this; } -}; - -template -inline front_insert_iterator<_Container> front_inserter(_Container& __x) { - return front_insert_iterator<_Container>(__x); -} -template -class insert_iterator { -protected: - _Container* container; - typename _Container::iterator iter; -public: - typedef _Container container_type; - typedef output_iterator_tag iterator_category; - typedef void value_type; - typedef void difference_type; - typedef void pointer; - typedef void reference; - - insert_iterator(_Container& __x, typename _Container::iterator __i) - : container(&__x), iter(__i) {} - insert_iterator<_Container>& - operator=(const typename _Container::value_type& __value) { - iter = container->insert(iter, __value); - ++iter; - return *this; + template + class insert_iterator + : public iterator + { + protected: + _Container* container; + typename _Container::iterator iter; + + public: + typedef _Container container_type; + + insert_iterator(_Container& __x, typename _Container::iterator __i) + : container(&__x), iter(__i) {} + + insert_iterator<_Container>& + operator=(const typename _Container::value_type& __value) { + iter = container->insert(iter, __value); + ++iter; + return *this; + } + insert_iterator<_Container>& operator*() { return *this; } + insert_iterator<_Container>& operator++() { return *this; } + insert_iterator<_Container>& operator++(int) { return *this; } + }; + + template + inline + insert_iterator<_Container> inserter(_Container& __x, _Iterator __i) + { + typedef typename _Container::iterator __iter; + return insert_iterator<_Container>(__x, __iter(__i)); } - insert_iterator<_Container>& operator*() { return *this; } - insert_iterator<_Container>& operator++() { return *this; } - insert_iterator<_Container>& operator++(int) { return *this; } -}; - -template -inline -insert_iterator<_Container> inserter(_Container& __x, _Iterator __i) -{ - typedef typename _Container::iterator __iter; - return insert_iterator<_Container>(__x, __iter(__i)); -} - -template -class reverse_bidirectional_iterator { - typedef reverse_bidirectional_iterator<_BidirectionalIterator, _Tp, - _Reference, _Distance> _Self; -protected: - _BidirectionalIterator current; -public: - typedef bidirectional_iterator_tag iterator_category; + + template + class reverse_bidirectional_iterator { + typedef reverse_bidirectional_iterator<_BidirectionalIterator, _Tp, + _Reference, _Distance> _Self; + protected: + _BidirectionalIterator current; + public: + typedef bidirectional_iterator_tag iterator_category; typedef _Tp value_type; - typedef _Distance difference_type; + typedef _Distance difference_type; typedef _Tp* pointer; - typedef _Reference reference; - + typedef _Reference reference; + reverse_bidirectional_iterator() {} - explicit reverse_bidirectional_iterator(_BidirectionalIterator __x) - : current(__x) {} - _BidirectionalIterator base() const { return current; } - _Reference operator*() const { - _BidirectionalIterator __tmp = current; + explicit reverse_bidirectional_iterator(_BidirectionalIterator __x) + : current(__x) {} + _BidirectionalIterator base() const { return current; } + _Reference operator*() const { + _BidirectionalIterator __tmp = current; return *--__tmp; } pointer operator->() const { return &(operator*()); } @@ -184,134 +339,6 @@ inline bool operator!=( } -// This is the new version of reverse_iterator, as defined in the -// draft C++ standard. It relies on the iterator_traits template, -// which in turn relies on partial specialization. The class -// reverse_bidirectional_iterator is no longer part of the draft -// standard, but it is retained for backward compatibility. - -template -class reverse_iterator -{ -protected: - _Iterator current; -public: - typedef typename iterator_traits<_Iterator>::iterator_category - iterator_category; - typedef typename iterator_traits<_Iterator>::value_type - value_type; - typedef typename iterator_traits<_Iterator>::difference_type - difference_type; - typedef typename iterator_traits<_Iterator>::pointer - pointer; - typedef typename iterator_traits<_Iterator>::reference - reference; - - typedef _Iterator iterator_type; - typedef reverse_iterator<_Iterator> _Self; - -public: - reverse_iterator() {} - explicit reverse_iterator(iterator_type __x) : current(__x) {} - - reverse_iterator(const _Self& __x) : current(__x.current) {} - template - reverse_iterator(const reverse_iterator<_Iter>& __x) - : current(__x.base()) {} - - iterator_type base() const { return current; } - reference operator*() const { - _Iterator __tmp = current; - return *--__tmp; - } - pointer operator->() const { return &(operator*()); } - - _Self& operator++() { - --current; - return *this; - } - _Self operator++(int) { - _Self __tmp = *this; - --current; - return __tmp; - } - _Self& operator--() { - ++current; - return *this; - } - _Self operator--(int) { - _Self __tmp = *this; - ++current; - return __tmp; - } - - _Self operator+(difference_type __n) const { - return _Self(current - __n); - } - _Self& operator+=(difference_type __n) { - current -= __n; - return *this; - } - _Self operator-(difference_type __n) const { - return _Self(current + __n); - } - _Self& operator-=(difference_type __n) { - current += __n; - return *this; - } - reference operator[](difference_type __n) const { return *(*this + __n); } -}; - -template -inline bool operator==(const reverse_iterator<_Iterator>& __x, - const reverse_iterator<_Iterator>& __y) { - return __x.base() == __y.base(); -} - -template -inline bool operator<(const reverse_iterator<_Iterator>& __x, - const reverse_iterator<_Iterator>& __y) { - return __y.base() < __x.base(); -} - -template -inline bool operator!=(const reverse_iterator<_Iterator>& __x, - const reverse_iterator<_Iterator>& __y) { - return !(__x == __y); -} - -template -inline bool operator>(const reverse_iterator<_Iterator>& __x, - const reverse_iterator<_Iterator>& __y) { - return __y < __x; -} - -template -inline bool operator<=(const reverse_iterator<_Iterator>& __x, - const reverse_iterator<_Iterator>& __y) { - return !(__y < __x); -} - -template -inline bool operator>=(const reverse_iterator<_Iterator>& __x, - const reverse_iterator<_Iterator>& __y) { - return !(__x < __y); -} - -template -inline typename reverse_iterator<_Iterator>::difference_type -operator-(const reverse_iterator<_Iterator>& __x, - const reverse_iterator<_Iterator>& __y) { - return __y.base() - __x.base(); -} - -template -inline reverse_iterator<_Iterator> -operator+(typename reverse_iterator<_Iterator>::difference_type __n, - const reverse_iterator<_Iterator>& __x) { - return reverse_iterator<_Iterator>(__x.base() - __n); -} - template , @@ -428,12 +455,6 @@ protected: public: typedef __normal_iterator<_Iterator, _Container> normal_iterator_type; - typedef iterator_traits<_Iterator> __traits_type; - typedef typename __traits_type::iterator_category iterator_category; - typedef typename __traits_type::value_type value_type; - typedef typename __traits_type::difference_type difference_type; - typedef typename __traits_type::pointer pointer; - typedef typename __traits_type::reference reference; __normal_iterator() : _M_current(_Iterator()) { } diff --git a/libstdc++-v3/testsuite/20_util/raw_storage_iterator.cc b/libstdc++-v3/testsuite/20_util/raw_storage_iterator.cc index de1fe691e14..7d59a8a6099 100644 --- a/libstdc++-v3/testsuite/20_util/raw_storage_iterator.cc +++ b/libstdc++-v3/testsuite/20_util/raw_storage_iterator.cc @@ -28,16 +28,17 @@ void test01() // Check for required base class. long l; - raw_storage_iterator rs_it(&l); - iterator* base = &rs_it; + typedef raw_storage_iterator test_iterator; + typedef iterator base_iterator; + test_iterator rs_it(&l); + base_iterator* base = &rs_it; // Check for required typedefs - typedef raw_storage_iterator::value_type value_type; - typedef raw_storage_iterator::difference_type difference_type; - typedef raw_storage_iterator::pointer pointer; - typedef raw_storage_iterator::reference reference; - typedef raw_storage_iterator::iterator_category iteratory_category; - + typedef test_iterator::value_type value_type; + typedef test_iterator::difference_type difference_type; + typedef test_iterator::pointer pointer; + typedef test_iterator::reference reference; + typedef test_iterator::iterator_category iteratory_category; } int main() diff --git a/libstdc++-v3/testsuite/24_iterators/back_insert_iterator.cc b/libstdc++-v3/testsuite/24_iterators/back_insert_iterator.cc new file mode 100644 index 00000000000..87d2d7966ed --- /dev/null +++ b/libstdc++-v3/testsuite/24_iterators/back_insert_iterator.cc @@ -0,0 +1,50 @@ +// 2001-06-21 Benjamin Kosnik + +// Copyright (C) 2001 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 2, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING. If not, write to the Free +// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, +// USA. + +// 24.4.2.1 Template class back_insert_iterator + +#include +#include + +void test01() +{ + using namespace std; + + // Check for required base class. + list l; + typedef back_insert_iterator > test_iterator; + typedef iterator base_iterator; + test_iterator r_it(l); + base_iterator* base = &r_it; + + // Check for required typedefs + typedef test_iterator::value_type value_type; + typedef test_iterator::difference_type difference_type; + typedef test_iterator::pointer pointer; + typedef test_iterator::reference reference; + typedef test_iterator::iterator_category iteratory_category; + typedef test_iterator::container_type container_type; +} + +int main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/24_iterators/front_insert_iterator.cc b/libstdc++-v3/testsuite/24_iterators/front_insert_iterator.cc new file mode 100644 index 00000000000..5e0d04ff1c5 --- /dev/null +++ b/libstdc++-v3/testsuite/24_iterators/front_insert_iterator.cc @@ -0,0 +1,50 @@ +// 2001-06-21 Benjamin Kosnik + +// Copyright (C) 2001 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 2, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING. If not, write to the Free +// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, +// USA. + +// 24.4.2.3 Template class front_insert_iterator + +#include +#include + +void test01() +{ + using namespace std; + + // Check for required base class. + list l; + typedef front_insert_iterator > test_iterator; + typedef iterator base_iterator; + test_iterator r_it(l); + base_iterator* base = &r_it; + + // Check for required typedefs + typedef test_iterator::value_type value_type; + typedef test_iterator::difference_type difference_type; + typedef test_iterator::pointer pointer; + typedef test_iterator::reference reference; + typedef test_iterator::iterator_category iteratory_category; + typedef test_iterator::container_type container_type; +} + +int main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/24_iterators/insert_iterator.cc b/libstdc++-v3/testsuite/24_iterators/insert_iterator.cc new file mode 100644 index 00000000000..aee24700d8a --- /dev/null +++ b/libstdc++-v3/testsuite/24_iterators/insert_iterator.cc @@ -0,0 +1,52 @@ +// 2001-06-21 Benjamin Kosnik + +// Copyright (C) 2001 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 2, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING. If not, write to the Free +// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, +// USA. + +// 24.4.2.5 Template class insert_iterator + +#include +#include + +void test01() +{ + using namespace std; + + // Check for required base class. + list l; + list::iterator li; + + typedef insert_iterator > test_iterator; + typedef iterator base_iterator; + test_iterator r_it(l, li); + base_iterator* base = &r_it; + + // Check for required typedefs + typedef test_iterator::value_type value_type; + typedef test_iterator::difference_type difference_type; + typedef test_iterator::pointer pointer; + typedef test_iterator::reference reference; + typedef test_iterator::iterator_category iteratory_category; + typedef test_iterator::container_type container_type; +} + +int main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/24_iterators/reverse_iterator.cc b/libstdc++-v3/testsuite/24_iterators/reverse_iterator.cc new file mode 100644 index 00000000000..606d393bf37 --- /dev/null +++ b/libstdc++-v3/testsuite/24_iterators/reverse_iterator.cc @@ -0,0 +1,53 @@ +// 2001-06-21 Benjamin Kosnik + +// Copyright (C) 2001 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 2, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING. If not, write to the Free +// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, +// USA. + +// 24.4.1.2 Reverse iterators + +#include + +void test01() +{ + using namespace std; + + // Check for required base class. + long l; + typedef reverse_iterator test_iterator; + typedef iterator::iterator_category, + iterator_traits::value_type, + iterator_traits::difference_type, + iterator_traits::pointer, + iterator_traits::reference> + base_iterator; + test_iterator r_it(&l); + base_iterator* base = &r_it; + + // Check for required typedefs + typedef test_iterator::value_type value_type; + typedef test_iterator::difference_type difference_type; + typedef test_iterator::pointer pointer; + typedef test_iterator::reference reference; + typedef test_iterator::iterator_category iteratory_category; +} + +int main() +{ + test01(); + return 0; +}