4 * Hewlett-Packard Company
6 * Permission to use, copy, modify, distribute and sell this software
7 * and its documentation for any purpose is hereby granted without fee,
8 * provided that the above copyright notice appear in all copies and
9 * that both that copyright notice and this permission notice appear
10 * in supporting documentation. Hewlett-Packard Company makes no
11 * representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
15 * Copyright (c) 1996-1998
16 * Silicon Graphics Computer Systems, Inc.
18 * Permission to use, copy, modify, distribute and sell this software
19 * and its documentation for any purpose is hereby granted without fee,
20 * provided that the above copyright notice appear in all copies and
21 * that both that copyright notice and this permission notice appear
22 * in supporting documentation. Silicon Graphics makes no
23 * representations about the suitability of this software for any
24 * purpose. It is provided "as is" without express or implied warranty.
27 /* NOTE: This is an internal header file, included by other STL headers.
28 * You should not attempt to use it directly.
31 #ifndef __SGI_STL_INTERNAL_ITERATOR_H
32 #define __SGI_STL_INTERNAL_ITERATOR_H
37 template <class _Container>
38 class back_insert_iterator {
40 _Container* container;
42 typedef _Container container_type;
43 typedef output_iterator_tag iterator_category;
44 typedef void value_type;
45 typedef void difference_type;
47 typedef void reference;
49 explicit back_insert_iterator(_Container& __x) : container(&__x) {}
50 back_insert_iterator<_Container>&
51 operator=(const typename _Container::value_type& __value) {
52 container->push_back(__value);
55 back_insert_iterator<_Container>& operator*() { return *this; }
56 back_insert_iterator<_Container>& operator++() { return *this; }
57 back_insert_iterator<_Container>& operator++(int) { return *this; }
60 #ifndef __STL_CLASS_PARTIAL_SPECIALIZATION
62 template <class _Container>
63 inline output_iterator_tag
64 iterator_category(const back_insert_iterator<_Container>&)
66 return output_iterator_tag();
69 #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
71 template <class _Container>
72 inline back_insert_iterator<_Container> back_inserter(_Container& __x) {
73 return back_insert_iterator<_Container>(__x);
76 template <class _Container>
77 class front_insert_iterator {
79 _Container* container;
81 typedef _Container container_type;
82 typedef output_iterator_tag iterator_category;
83 typedef void value_type;
84 typedef void difference_type;
86 typedef void reference;
88 explicit front_insert_iterator(_Container& __x) : container(&__x) {}
89 front_insert_iterator<_Container>&
90 operator=(const typename _Container::value_type& __value) {
91 container->push_front(__value);
94 front_insert_iterator<_Container>& operator*() { return *this; }
95 front_insert_iterator<_Container>& operator++() { return *this; }
96 front_insert_iterator<_Container>& operator++(int) { return *this; }
99 #ifndef __STL_CLASS_PARTIAL_SPECIALIZATION
101 template <class _Container>
102 inline output_iterator_tag
103 iterator_category(const front_insert_iterator<_Container>&)
105 return output_iterator_tag();
108 #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
110 template <class _Container>
111 inline front_insert_iterator<_Container> front_inserter(_Container& __x) {
112 return front_insert_iterator<_Container>(__x);
115 template <class _Container>
116 class insert_iterator {
118 _Container* container;
119 typename _Container::iterator iter;
121 typedef _Container container_type;
122 typedef output_iterator_tag iterator_category;
123 typedef void value_type;
124 typedef void difference_type;
125 typedef void pointer;
126 typedef void reference;
128 insert_iterator(_Container& __x, typename _Container::iterator __i)
129 : container(&__x), iter(__i) {}
130 insert_iterator<_Container>&
131 operator=(const typename _Container::value_type& __value) {
132 iter = container->insert(iter, __value);
136 insert_iterator<_Container>& operator*() { return *this; }
137 insert_iterator<_Container>& operator++() { return *this; }
138 insert_iterator<_Container>& operator++(int) { return *this; }
141 #ifndef __STL_CLASS_PARTIAL_SPECIALIZATION
143 template <class _Container>
144 inline output_iterator_tag
145 iterator_category(const insert_iterator<_Container>&)
147 return output_iterator_tag();
150 #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
152 template <class _Container, class _Iterator>
154 insert_iterator<_Container> inserter(_Container& __x, _Iterator __i)
156 typedef typename _Container::iterator __iter;
157 return insert_iterator<_Container>(__x, __iter(__i));
160 template <class _BidirectionalIterator, class _Tp, class _Reference = _Tp&,
161 class _Distance = ptrdiff_t>
162 class reverse_bidirectional_iterator {
163 typedef reverse_bidirectional_iterator<_BidirectionalIterator, _Tp,
164 _Reference, _Distance> _Self;
166 _BidirectionalIterator current;
168 typedef bidirectional_iterator_tag iterator_category;
169 typedef _Tp value_type;
170 typedef _Distance difference_type;
171 typedef _Tp* pointer;
172 typedef _Reference reference;
174 reverse_bidirectional_iterator() {}
175 explicit reverse_bidirectional_iterator(_BidirectionalIterator __x)
177 _BidirectionalIterator base() const { return current; }
178 _Reference operator*() const {
179 _BidirectionalIterator __tmp = current;
182 #ifndef __SGI_STL_NO_ARROW_OPERATOR
183 pointer operator->() const { return &(operator*()); }
184 #endif /* __SGI_STL_NO_ARROW_OPERATOR */
185 _Self& operator++() {
189 _Self operator++(int) {
194 _Self& operator--() {
198 _Self operator--(int) {
205 #ifndef __STL_CLASS_PARTIAL_SPECIALIZATION
207 template <class _BidirectionalIterator, class _Tp, class _Reference,
209 inline bidirectional_iterator_tag
210 iterator_category(const reverse_bidirectional_iterator<_BidirectionalIterator,
214 return bidirectional_iterator_tag();
217 template <class _BidirectionalIterator, class _Tp, class _Reference,
220 value_type(const reverse_bidirectional_iterator<_BidirectionalIterator, _Tp,
221 _Reference, _Distance>&)
226 template <class _BidirectionalIterator, class _Tp, class _Reference,
229 distance_type(const reverse_bidirectional_iterator<_BidirectionalIterator,
231 _Reference, _Distance>&)
233 return (_Distance*) 0;
236 #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
238 template <class _BiIter, class _Tp, class _Ref, class _Distance>
239 inline bool operator==(
240 const reverse_bidirectional_iterator<_BiIter, _Tp, _Ref, _Distance>& __x,
241 const reverse_bidirectional_iterator<_BiIter, _Tp, _Ref, _Distance>& __y)
243 return __x.base() == __y.base();
246 #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
248 template <class _BiIter, class _Tp, class _Ref, class _Distance>
249 inline bool operator!=(
250 const reverse_bidirectional_iterator<_BiIter, _Tp, _Ref, _Distance>& __x,
251 const reverse_bidirectional_iterator<_BiIter, _Tp, _Ref, _Distance>& __y)
253 return !(__x == __y);
256 #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
259 #ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
261 // This is the new version of reverse_iterator, as defined in the
262 // draft C++ standard. It relies on the iterator_traits template,
263 // which in turn relies on partial specialization. The class
264 // reverse_bidirectional_iterator is no longer part of the draft
265 // standard, but it is retained for backward compatibility.
267 template <class _Iterator>
268 class reverse_iterator
273 typedef typename iterator_traits<_Iterator>::iterator_category
275 typedef typename iterator_traits<_Iterator>::value_type
277 typedef typename iterator_traits<_Iterator>::difference_type
279 typedef typename iterator_traits<_Iterator>::pointer
281 typedef typename iterator_traits<_Iterator>::reference
284 typedef _Iterator iterator_type;
285 typedef reverse_iterator<_Iterator> _Self;
288 reverse_iterator() {}
289 explicit reverse_iterator(iterator_type __x) : current(__x) {}
291 reverse_iterator(const _Self& __x) : current(__x.current) {}
292 #ifdef __STL_MEMBER_TEMPLATES
293 template <class _Iter>
294 reverse_iterator(const reverse_iterator<_Iter>& __x)
295 : current(__x.base()) {}
296 #endif /* __STL_MEMBER_TEMPLATES */
298 iterator_type base() const { return current; }
299 reference operator*() const {
300 _Iterator __tmp = current;
303 #ifndef __SGI_STL_NO_ARROW_OPERATOR
304 pointer operator->() const { return &(operator*()); }
305 #endif /* __SGI_STL_NO_ARROW_OPERATOR */
307 _Self& operator++() {
311 _Self operator++(int) {
316 _Self& operator--() {
320 _Self operator--(int) {
326 _Self operator+(difference_type __n) const {
327 return _Self(current - __n);
329 _Self& operator+=(difference_type __n) {
333 _Self operator-(difference_type __n) const {
334 return _Self(current + __n);
336 _Self& operator-=(difference_type __n) {
340 reference operator[](difference_type __n) const { return *(*this + __n); }
343 template <class _Iterator>
344 inline bool operator==(const reverse_iterator<_Iterator>& __x,
345 const reverse_iterator<_Iterator>& __y) {
346 return __x.base() == __y.base();
349 template <class _Iterator>
350 inline bool operator<(const reverse_iterator<_Iterator>& __x,
351 const reverse_iterator<_Iterator>& __y) {
352 return __y.base() < __x.base();
355 #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
357 template <class _Iterator>
358 inline bool operator!=(const reverse_iterator<_Iterator>& __x,
359 const reverse_iterator<_Iterator>& __y) {
360 return !(__x == __y);
363 template <class _Iterator>
364 inline bool operator>(const reverse_iterator<_Iterator>& __x,
365 const reverse_iterator<_Iterator>& __y) {
369 template <class _Iterator>
370 inline bool operator<=(const reverse_iterator<_Iterator>& __x,
371 const reverse_iterator<_Iterator>& __y) {
375 template <class _Iterator>
376 inline bool operator>=(const reverse_iterator<_Iterator>& __x,
377 const reverse_iterator<_Iterator>& __y) {
381 #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
383 template <class _Iterator>
384 inline typename reverse_iterator<_Iterator>::difference_type
385 operator-(const reverse_iterator<_Iterator>& __x,
386 const reverse_iterator<_Iterator>& __y) {
387 return __y.base() - __x.base();
390 template <class _Iterator>
391 inline reverse_iterator<_Iterator>
392 operator+(typename reverse_iterator<_Iterator>::difference_type __n,
393 const reverse_iterator<_Iterator>& __x) {
394 return reverse_iterator<_Iterator>(__x.base() - __n);
397 #else /* __STL_CLASS_PARTIAL_SPECIALIZATION */
399 // This is the old version of reverse_iterator, as found in the original
400 // HP STL. It does not use partial specialization.
402 template <class _RandomAccessIterator, class _Tp, class _Reference = _Tp&,
403 class _Distance = ptrdiff_t>
404 class reverse_iterator {
405 typedef reverse_iterator<_RandomAccessIterator, _Tp, _Reference, _Distance>
408 _RandomAccessIterator current;
410 typedef random_access_iterator_tag iterator_category;
411 typedef _Tp value_type;
412 typedef _Distance difference_type;
413 typedef _Tp* pointer;
414 typedef _Reference reference;
416 reverse_iterator() {}
417 explicit reverse_iterator(_RandomAccessIterator __x) : current(__x) {}
418 _RandomAccessIterator base() const { return current; }
419 _Reference operator*() const { return *(current - 1); }
420 #ifndef __SGI_STL_NO_ARROW_OPERATOR
421 pointer operator->() const { return &(operator*()); }
422 #endif /* __SGI_STL_NO_ARROW_OPERATOR */
423 _Self& operator++() {
427 _Self operator++(int) {
432 _Self& operator--() {
436 _Self operator--(int) {
441 _Self operator+(_Distance __n) const {
442 return _Self(current - __n);
444 _Self& operator+=(_Distance __n) {
448 _Self operator-(_Distance __n) const {
449 return _Self(current + __n);
451 _Self& operator-=(_Distance __n) {
455 _Reference operator[](_Distance __n) const { return *(*this + __n); }
458 template <class _RandomAccessIterator, class _Tp,
459 class _Reference, class _Distance>
460 inline random_access_iterator_tag
461 iterator_category(const reverse_iterator<_RandomAccessIterator, _Tp,
462 _Reference, _Distance>&)
464 return random_access_iterator_tag();
467 template <class _RandomAccessIterator, class _Tp,
468 class _Reference, class _Distance>
469 inline _Tp* value_type(const reverse_iterator<_RandomAccessIterator, _Tp,
470 _Reference, _Distance>&)
475 template <class _RandomAccessIterator, class _Tp,
476 class _Reference, class _Distance>
478 distance_type(const reverse_iterator<_RandomAccessIterator,
479 _Tp, _Reference, _Distance>&)
481 return (_Distance*) 0;
485 template <class _RandomAccessIterator, class _Tp,
486 class _Reference, class _Distance>
488 operator==(const reverse_iterator<_RandomAccessIterator, _Tp,
489 _Reference, _Distance>& __x,
490 const reverse_iterator<_RandomAccessIterator, _Tp,
491 _Reference, _Distance>& __y)
493 return __x.base() == __y.base();
496 template <class _RandomAccessIterator, class _Tp,
497 class _Reference, class _Distance>
499 operator<(const reverse_iterator<_RandomAccessIterator, _Tp,
500 _Reference, _Distance>& __x,
501 const reverse_iterator<_RandomAccessIterator, _Tp,
502 _Reference, _Distance>& __y)
504 return __y.base() < __x.base();
507 #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
509 template <class _RandomAccessIterator, class _Tp,
510 class _Reference, class _Distance>
512 operator!=(const reverse_iterator<_RandomAccessIterator, _Tp,
513 _Reference, _Distance>& __x,
514 const reverse_iterator<_RandomAccessIterator, _Tp,
515 _Reference, _Distance>& __y) {
516 return !(__x == __y); }
519 template <class _RandomAccessIterator, class _Tp,
520 class _Reference, class _Distance>
522 operator>(const reverse_iterator<_RandomAccessIterator, _Tp,
523 _Reference, _Distance>& __x,
524 const reverse_iterator<_RandomAccessIterator, _Tp,
525 _Reference, _Distance>& __y) {
529 template <class _RandomAccessIterator, class _Tp,
530 class _Reference, class _Distance>
532 operator<=(const reverse_iterator<_RandomAccessIterator, _Tp,
533 _Reference, _Distance>& __x,
534 const reverse_iterator<_RandomAccessIterator, _Tp,
535 _Reference, _Distance>& __y) {
539 template <class _RandomAccessIterator, class _Tp,
540 class _Reference, class _Distance>
542 operator>=(const reverse_iterator<_RandomAccessIterator, _Tp,
543 _Reference, _Distance>& __x,
544 const reverse_iterator<_RandomAccessIterator, _Tp,
545 _Reference, _Distance>& __y) {
549 #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
551 template <class _RandomAccessIterator, class _Tp,
552 class _Reference, class _Distance>
554 operator-(const reverse_iterator<_RandomAccessIterator, _Tp,
555 _Reference, _Distance>& __x,
556 const reverse_iterator<_RandomAccessIterator, _Tp,
557 _Reference, _Distance>& __y)
559 return __y.base() - __x.base();
562 template <class _RandAccIter, class _Tp, class _Ref, class _Dist>
563 inline reverse_iterator<_RandAccIter, _Tp, _Ref, _Dist>
565 const reverse_iterator<_RandAccIter, _Tp, _Ref, _Dist>& __x)
567 return reverse_iterator<_RandAccIter, _Tp, _Ref, _Dist>(__x.base() - __n);
570 #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
572 // istream_iterator and ostream_iterator look very different if we're
573 // using new, templatized iostreams than if we're using the old cfront
576 #ifdef __STL_USE_NEW_IOSTREAMS
579 class _CharT = char, class _Traits = char_traits<_CharT>,
580 class _Dist = ptrdiff_t>
581 class istream_iterator {
583 typedef _CharT char_type;
584 typedef _Traits traits_type;
585 typedef basic_istream<_CharT, _Traits> istream_type;
587 typedef input_iterator_tag iterator_category;
588 typedef _Tp value_type;
589 typedef _Dist difference_type;
590 typedef const _Tp* pointer;
591 typedef const _Tp& reference;
593 istream_iterator() : _M_stream(0), _M_ok(false) {}
594 istream_iterator(istream_type& __s) : _M_stream(&__s) { _M_read(); }
596 reference operator*() const { return _M_value; }
597 pointer operator->() const { return &(operator*()); }
599 istream_iterator& operator++() {
603 istream_iterator operator++(int) {
604 istream_iterator __tmp = *this;
609 bool _M_equal(const istream_iterator& __x) const
610 { return (_M_ok == __x._M_ok) && (!_M_ok || _M_stream == __x._M_stream); }
613 istream_type* _M_stream;
618 _M_ok = (_M_stream && *_M_stream) ? true : false;
620 *_M_stream >> _M_value;
621 _M_ok = *_M_stream ? true : false;
626 template <class _Tp, class _CharT, class _Traits, class _Dist>
628 operator==(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x,
629 const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y) {
630 return __x._M_equal(__y);
633 #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
635 template <class _Tp, class _CharT, class _Traits, class _Dist>
637 operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x,
638 const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y) {
639 return !__x._M_equal(__y);
642 #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
645 class _CharT = char, class _Traits = char_traits<_CharT> >
646 class ostream_iterator {
648 typedef _CharT char_type;
649 typedef _Traits traits_type;
650 typedef basic_ostream<_CharT, _Traits> ostream_type;
652 typedef output_iterator_tag iterator_category;
653 typedef void value_type;
654 typedef void difference_type;
655 typedef void pointer;
656 typedef void reference;
658 ostream_iterator(ostream_type& __s) : _M_stream(&__s), _M_string(0) {}
659 ostream_iterator(ostream_type& __s, const _CharT* __c)
660 : _M_stream(&__s), _M_string(__c) {}
661 ostream_iterator<_Tp>& operator=(const _Tp& __value) {
662 *_M_stream << __value;
663 if (_M_string) *_M_stream << _M_string;
666 ostream_iterator<_Tp>& operator*() { return *this; }
667 ostream_iterator<_Tp>& operator++() { return *this; }
668 ostream_iterator<_Tp>& operator++(int) { return *this; }
670 ostream_type* _M_stream;
671 const _CharT* _M_string;
674 // The default template argument is declared in iosfwd
676 // We do not read any characters until operator* is called. The first
677 // time operator* is called, it calls getc. Subsequent calls to getc
678 // return a cached character, and calls to operator++ use snextc. Before
679 // operator* or operator++ has been called, _M_is_initialized is false.
680 template<class _CharT, class _Traits>
681 class istreambuf_iterator
684 typedef _CharT char_type;
685 typedef _Traits traits_type;
686 typedef typename _Traits::int_type int_type;
687 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
688 typedef basic_istream<_CharT, _Traits> istream_type;
690 typedef input_iterator_tag iterator_category;
691 typedef _CharT value_type;
692 typedef typename _Traits::off_type difference_type;
693 typedef const _CharT* pointer;
694 typedef const _CharT& reference;
697 istreambuf_iterator(streambuf_type* __p = 0) { this->_M_init(__p); }
698 istreambuf_iterator(istream_type& __is) { this->_M_init(__is.rdbuf()); }
700 char_type operator*() const
701 { return _M_is_initialized ? _M_c : _M_dereference_aux(); }
703 istreambuf_iterator& operator++() { this->_M_nextc(); return *this; }
704 istreambuf_iterator operator++(int) {
705 if (!_M_is_initialized)
707 istreambuf_iterator __tmp = *this;
712 bool equal(const istreambuf_iterator& __i) const {
713 return this->_M_is_initialized && __i._M_is_initialized
714 ? this->_M_eof == __i._M_eof
715 : this->_M_equal_aux(__i);
719 void _M_init(streambuf_type* __p) {
722 _M_is_initialized = _M_eof;
725 char_type _M_dereference_aux() const;
726 bool _M_equal_aux(const istreambuf_iterator&) const;
727 void _M_postincr_aux();
730 int_type __c = _M_buf->snextc();
731 _M_c = traits_type::to_char_type(__c);
732 _M_eof = traits_type::eq_int_type(__c, traits_type::eof());
733 _M_is_initialized = true;
736 void _M_getc() const {
737 int_type __c = _M_buf->sgetc();
738 _M_c = traits_type::to_char_type(__c);
739 _M_eof = traits_type::eq_int_type(__c, traits_type::eof());
740 _M_is_initialized = true;
744 streambuf_type* _M_buf;
746 mutable bool _M_eof : 1;
747 mutable bool _M_is_initialized : 1;
750 template<class _CharT, class _Traits>
751 _CharT istreambuf_iterator<_CharT, _Traits>::_M_dereference_aux() const
757 template<class _CharT, class _Traits>
758 bool istreambuf_iterator<_CharT, _Traits>
759 ::_M_equal_aux(const istreambuf_iterator& __i) const
761 if (!this->_M_is_initialized)
763 if (!__i._M_is_initialized)
766 return this->_M_eof == __i._M_eof;
769 template<class _CharT, class _Traits>
770 void istreambuf_iterator<_CharT, _Traits>::_M_postincr_aux()
775 template<class _CharT, class _Traits>
776 inline bool operator==(const istreambuf_iterator<_CharT, _Traits>& __x,
777 const istreambuf_iterator<_CharT, _Traits>& __y) {
778 return __x.equal(__y);
781 #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
783 template<class _CharT, class _Traits>
784 inline bool operator!=(const istreambuf_iterator<_CharT, _Traits>& __x,
785 const istreambuf_iterator<_CharT, _Traits>& __y) {
786 return !__x.equal(__y);
789 #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
791 // The default template argument is declared in iosfwd
792 template<class _CharT, class _Traits>
793 class ostreambuf_iterator
796 typedef _CharT char_type;
797 typedef _Traits traits_type;
798 typedef typename _Traits::int_type int_type;
799 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
800 typedef basic_ostream<_CharT, _Traits> ostream_type;
802 typedef output_iterator_tag iterator_category;
803 typedef void value_type;
804 typedef void difference_type;
805 typedef void pointer;
806 typedef void reference;
809 ostreambuf_iterator(streambuf_type* __buf) : _M_buf(__buf), _M_ok(__buf) {}
810 ostreambuf_iterator(ostream_type& __o)
811 : _M_buf(__o.rdbuf()), _M_ok(__o.rdbuf()) {}
813 ostreambuf_iterator& operator=(char_type __c) {
814 _M_ok = _M_ok && !traits_type::eq_int_type(_M_buf->sputc(__c),
819 ostreambuf_iterator& operator*() { return *this; }
820 ostreambuf_iterator& operator++() { return *this; }
821 ostreambuf_iterator& operator++(int) { return *this; }
823 bool failed() const { return !_M_ok; }
826 streambuf_type* _M_buf;
830 #else /* __STL_USE_NEW_IOSTREAMS */
832 template <class _Tp, class _Dist = ptrdiff_t> class istream_iterator;
834 template <class _Tp, class _Dist>
835 inline bool operator==(const istream_iterator<_Tp, _Dist>&,
836 const istream_iterator<_Tp, _Dist>&);
838 template <class _Tp, class _Dist>
839 class istream_iterator {
840 #ifdef __STL_MEMBER_TEMPLATES
841 template <class _T1, class _D1>
842 friend bool operator==(const istream_iterator<_T1, _D1>&,
843 const istream_iterator<_T1, _D1>&);
844 #else /* __STL_MEMBER_TEMPLATES */
845 friend bool __STD_QUALIFIER
846 operator== __STL_NULL_TMPL_ARGS (const istream_iterator&,
847 const istream_iterator&);
848 #endif /* __STL_MEMBER_TEMPLATES */
855 _M_end_marker = (*_M_stream) ? true : false;
856 if (_M_end_marker) *_M_stream >> _M_value;
857 _M_end_marker = (*_M_stream) ? true : false;
860 typedef input_iterator_tag iterator_category;
861 typedef _Tp value_type;
862 typedef _Dist difference_type;
863 typedef const _Tp* pointer;
864 typedef const _Tp& reference;
866 istream_iterator() : _M_stream(&cin), _M_end_marker(false) {}
867 istream_iterator(istream& __s) : _M_stream(&__s) { _M_read(); }
868 reference operator*() const { return _M_value; }
869 #ifndef __SGI_STL_NO_ARROW_OPERATOR
870 pointer operator->() const { return &(operator*()); }
871 #endif /* __SGI_STL_NO_ARROW_OPERATOR */
872 istream_iterator<_Tp, _Dist>& operator++() {
876 istream_iterator<_Tp, _Dist> operator++(int) {
877 istream_iterator<_Tp, _Dist> __tmp = *this;
883 #ifndef __STL_CLASS_PARTIAL_SPECIALIZATION
885 template <class _Tp, class _Dist>
886 inline input_iterator_tag
887 iterator_category(const istream_iterator<_Tp, _Dist>&)
889 return input_iterator_tag();
892 template <class _Tp, class _Dist>
894 value_type(const istream_iterator<_Tp, _Dist>&) { return (_Tp*) 0; }
896 template <class _Tp, class _Dist>
898 distance_type(const istream_iterator<_Tp, _Dist>&) { return (_Dist*)0; }
900 #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
902 template <class _Tp, class _Distance>
903 inline bool operator==(const istream_iterator<_Tp, _Distance>& __x,
904 const istream_iterator<_Tp, _Distance>& __y) {
905 return (__x._M_stream == __y._M_stream &&
906 __x._M_end_marker == __y._M_end_marker) ||
907 __x._M_end_marker == false && __y._M_end_marker == false;
910 #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
912 template <class _Tp, class _Distance>
913 inline bool operator!=(const istream_iterator<_Tp, _Distance>& __x,
914 const istream_iterator<_Tp, _Distance>& __y) {
915 return !(__x == __y);
918 #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
921 class ostream_iterator {
924 const char* _M_string;
926 typedef output_iterator_tag iterator_category;
927 typedef void value_type;
928 typedef void difference_type;
929 typedef void pointer;
930 typedef void reference;
932 ostream_iterator(ostream& __s) : _M_stream(&__s), _M_string(0) {}
933 ostream_iterator(ostream& __s, const char* __c)
934 : _M_stream(&__s), _M_string(__c) {}
935 ostream_iterator<_Tp>& operator=(const _Tp& __value) {
936 *_M_stream << __value;
937 if (_M_string) *_M_stream << _M_string;
940 ostream_iterator<_Tp>& operator*() { return *this; }
941 ostream_iterator<_Tp>& operator++() { return *this; }
942 ostream_iterator<_Tp>& operator++(int) { return *this; }
945 #ifndef __STL_CLASS_PARTIAL_SPECIALIZATION
948 inline output_iterator_tag
949 iterator_category(const ostream_iterator<_Tp>&) {
950 return output_iterator_tag();
953 #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
955 #endif /* __STL_USE_NEW_IOSTREAMS */
957 // This iterator adapter is 'normal' in the sense that it does not
958 // change the semantics of any of the operators of its itererator
959 // parameter. Its primary purpose is to convert an iterator that is
960 // not a class, e.g. a pointer, into an iterator that is a class.
961 // The _Container parameter exists solely so that different containers
962 // using this template can instantiate different types, even if the
963 // _Iterator parameter is the same.
964 template<typename _Iterator, typename _Container>
965 class __normal_iterator
966 : public iterator<iterator_traits<_Iterator>::iterator_category,
967 iterator_traits<_Iterator>::value_type,
968 iterator_traits<_Iterator>::difference_type,
969 iterator_traits<_Iterator>::pointer,
970 iterator_traits<_Iterator>::reference>
974 _Iterator _M_current;
977 typedef __normal_iterator<_Iterator, _Container> normal_iterator_type;
978 typedef iterator_traits<_Iterator> __traits_type;
979 typedef typename __traits_type::iterator_category iterator_category;
980 typedef typename __traits_type::value_type value_type;
981 typedef typename __traits_type::difference_type difference_type;
982 typedef typename __traits_type::pointer pointer;
983 typedef typename __traits_type::reference reference;
985 __normal_iterator() : _M_current(_Iterator()) { }
987 explicit __normal_iterator(const _Iterator& __i) : _M_current(__i) { }
989 // Allow iterator to const_iterator conversion
990 template<typename _Iter>
991 inline __normal_iterator(const __normal_iterator<_Iter, _Container>& __i)
992 : _M_current(__i.base()) { }
994 // Forward iterator requirements
996 operator*() const { return *_M_current; }
999 operator->() const { return _M_current; }
1001 normal_iterator_type&
1002 operator++() { ++_M_current; return *this; }
1004 normal_iterator_type
1005 operator++(int) { return __normal_iterator(_M_current++); }
1007 // Bidirectional iterator requirements
1008 normal_iterator_type&
1009 operator--() { --_M_current; return *this; }
1011 normal_iterator_type
1012 operator--(int) { return __normal_iterator(_M_current--); }
1014 // Random access iterator requirements
1016 operator[](const difference_type& __n) const
1017 { return _M_current[__n]; }
1019 normal_iterator_type&
1020 operator+=(const difference_type& __n)
1021 { _M_current += __n; return *this; }
1023 normal_iterator_type
1024 operator+(const difference_type& __n) const
1025 { return __normal_iterator(_M_current + __n); }
1027 normal_iterator_type&
1028 operator-=(const difference_type& __n)
1029 { _M_current -= __n; return *this; }
1031 normal_iterator_type
1032 operator-(const difference_type& __n) const
1033 { return __normal_iterator(_M_current - __n); }
1036 operator-(const normal_iterator_type& __i) const
1037 { return _M_current - __i._M_current; }
1040 base() const { return _M_current; }
1043 // forward iterator requirements
1045 template<typename _IteratorL, typename _IteratorR, typename _Container>
1046 bool operator==(const __normal_iterator<_IteratorL, _Container>& __lhs,
1047 const __normal_iterator<_IteratorR, _Container>& __rhs)
1048 { return __lhs.base() == __rhs.base(); }
1050 template<typename _IteratorL, typename _IteratorR, typename _Container>
1051 bool operator!=(const __normal_iterator<_IteratorL, _Container>& __lhs,
1052 const __normal_iterator<_IteratorR, _Container>& __rhs)
1053 { return !(__lhs == __rhs); }
1055 // random access iterator requirements
1057 template<typename _IteratorL, typename _IteratorR, typename _Container>
1058 bool operator<(const __normal_iterator<_IteratorL, _Container>& __lhs,
1059 const __normal_iterator<_IteratorR, _Container>& __rhs)
1060 { return __lhs.base() < __rhs.base(); }
1062 template<typename _IteratorL, typename _IteratorR, typename _Container>
1063 bool operator>(const __normal_iterator<_IteratorL, _Container>& __lhs,
1064 const __normal_iterator<_IteratorR, _Container>& __rhs)
1065 { return __rhs < __lhs; }
1067 template<typename _IteratorL, typename _IteratorR, typename _Container>
1068 bool operator<=(const __normal_iterator<_IteratorL, _Container>& __lhs,
1069 const __normal_iterator<_IteratorR, _Container>& __rhs)
1070 { return !(__rhs < __lhs); }
1072 template<typename _IteratorL, typename _IteratorR, typename _Container>
1073 bool operator>=(const __normal_iterator<_IteratorL, _Container>& __lhs,
1074 const __normal_iterator<_IteratorR, _Container>& __rhs)
1075 { return !(__lhs < __rhs); }
1077 template<typename _Iterator, typename _Container>
1078 inline __normal_iterator<_Iterator, _Container>
1079 operator+(__normal_iterator<_Iterator, _Container>::difference_type __n,
1080 const __normal_iterator<_Iterator, _Container>& __i)
1081 { return __normal_iterator<_Iterator, _Container>(__i.base() + __n); }
1085 #endif /* __SGI_STL_INTERNAL_ITERATOR_H */