home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 May / PCP163A.iso / Runimage / Cbuilder4 / Include / ITERATOR.H < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-26  |  27.6 KB  |  869 lines

  1. #ifndef __ITERATOR_H
  2. #define __ITERATOR_H
  3. #pragma option push -b -a8 -pc -Vx- -Ve- -w-inl -w-aus -w-sig
  4. // -*- C++ -*-
  5. #ifndef __STD_ITERATOR__
  6. #define __STD_ITERATOR__
  7.  
  8. /***************************************************************************
  9.  *
  10.  * iterator - iterator declarations for the Standard Library
  11.  *
  12.  ***************************************************************************
  13.  *
  14.  * Copyright (c) 1994
  15.  * Hewlett-Packard Company
  16.  *
  17.  * Permission to use, copy, modify, distribute and sell this software
  18.  * and its documentation for any purpose is hereby granted without fee,
  19.  * provided that the above copyright notice appear in all copies and
  20.  * that both that copyright notice and this permission notice appear
  21.  * in supporting documentation.  Hewlett-Packard Company makes no
  22.  * representations about the suitability of this software for any
  23.  * purpose.  It is provided "as is" without express or implied warranty.
  24.  *
  25.  *
  26.  ***************************************************************************
  27.  *
  28.  * (c) Copyright 1994, 1998 Rogue Wave Software, Inc.
  29.  * ALL RIGHTS RESERVED
  30.  *
  31.  * The software and information contained herein are proprietary to, and
  32.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  33.  * intends to preserve as trade secrets such software and information.
  34.  * This software is furnished pursuant to a written license agreement and
  35.  * may be used, copied, transmitted, and stored only in accordance with
  36.  * the terms of such license and with the inclusion of the above copyright
  37.  * notice.  This software and information or any other copies thereof may
  38.  * not be provided or otherwise made available to any other person.
  39.  *
  40.  * Notwithstanding any other lease or license that may pertain to, or
  41.  * accompany the delivery of, this computer software and information, the
  42.  * rights of the Government regarding its use, reproduction and disclosure
  43.  * are as set forth in Section 52.227-19 of the FARS Computer
  44.  * Software-Restricted Rights clause.
  45.  * 
  46.  * Use, duplication, or disclosure by the Government is subject to
  47.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  48.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  49.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  50.  * P.O. Box 2328, Corvallis, Oregon 97339.
  51.  *
  52.  * This computer software and information is distributed with "restricted
  53.  * rights."  Use, duplication or disclosure is subject to restrictions as
  54.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  55.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  56.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  57.  * then the "Alternate III" clause applies.
  58.  *
  59.  **************************************************************************/
  60.  
  61. #include <stdcomp.h>
  62. #include <rw/stddefs.h>
  63.  
  64. #ifndef _RWSTD_NO_NEW_HEADER
  65. #include <cstddef>
  66. #else
  67. #include <stddef.h>
  68. #endif
  69.  
  70. #ifdef _RWSTD_NO_BASE_CLASS_MATCH
  71. #define _RWSTD_VALUE_TYPE(a) __value_type(*(a))
  72. #else
  73. #define _RWSTD_VALUE_TYPE(a) __value_type(a)
  74. #endif
  75.  
  76. #ifndef _RWSTD_NO_NAMESPACE
  77. namespace std {
  78. #endif
  79.  
  80. //
  81. // Standard iterator tags.
  82. //
  83.   
  84.   struct input_iterator_tag
  85.   {
  86.     input_iterator_tag() {;}
  87.   };
  88.  
  89.   struct output_iterator_tag
  90.   {
  91.     output_iterator_tag() {;}
  92.   };
  93.  
  94.   struct forward_iterator_tag : public input_iterator_tag
  95.   {
  96.     forward_iterator_tag() {;}
  97.   };
  98.  
  99.   struct bidirectional_iterator_tag : public forward_iterator_tag
  100.   {
  101.     bidirectional_iterator_tag() {;}
  102.   };
  103.  
  104.   struct random_access_iterator_tag : public bidirectional_iterator_tag
  105.   {
  106.     random_access_iterator_tag() {;}
  107.   };
  108. //
  109. // Basic iterators.
  110. //
  111.  
  112. //
  113. // Note that _RWSTD_SIMPLE_DEFAULT(x)
  114. // will expand to: ' = x', or nothing,
  115. // depending on your compiler's capabilities and/or
  116. // flag settings (see stdcomp.h).
  117. //
  118.   template <class Category, class T,  
  119.     class Distance _RWSTD_SIMPLE_DEFAULT(ptrdiff_t),
  120.     class Pointer _RWSTD_SIMPLE_DEFAULT(T*),
  121.     class Reference _RWSTD_SIMPLE_DEFAULT(T&)>
  122.   struct iterator
  123.   {
  124.     typedef T value_type;
  125.     typedef Distance difference_type;
  126.     typedef Pointer pointer;
  127.     typedef Reference reference;
  128.     typedef Category iterator_category;
  129.   };
  130.  
  131. #ifndef _RWSTD_NO_CLASS_PARTIAL_SPEC
  132.  
  133.   template <class Iterator> struct iterator_traits
  134.   {
  135.     typedef _TYPENAME Iterator::value_type value_type;
  136.     typedef _TYPENAME Iterator::difference_type difference_type;
  137.     typedef _TYPENAME Iterator::pointer pointer;
  138.     typedef _TYPENAME Iterator::reference reference;
  139.     typedef _TYPENAME Iterator::iterator_category iterator_category;
  140.   };
  141.   template <class T> struct iterator_traits<T*>
  142.   {
  143.     typedef T value_type;
  144.     typedef ptrdiff_t difference_type;
  145.     typedef T* pointer;
  146.     typedef T& reference;
  147.     typedef random_access_iterator_tag iterator_category;
  148.   };
  149.   template <class T> struct iterator_traits<const T*>
  150.   {
  151.     typedef T value_type;
  152.     typedef ptrdiff_t difference_type;
  153.     typedef const T* pointer;
  154.     typedef const T& reference;
  155.     typedef random_access_iterator_tag iterator_category;
  156.   };
  157.  
  158.   template <class ForwardIterator>
  159.   inline _TYPENAME iterator_traits<ForwardIterator>::difference_type
  160.   distance (ForwardIterator first, ForwardIterator last)
  161.   {
  162.     _TYPENAME iterator_traits<ForwardIterator>::difference_type n = 0;
  163.     __distance(first, last, n, 
  164.                iterator_traits<ForwardIterator>::iterator_category());
  165.     return n;
  166.   }
  167.  
  168.   template <class ForwardIterator, class Distance>
  169.   inline void advance (ForwardIterator& i, Distance n)
  170.   {
  171.     __advance(i, n, 
  172.           iterator_traits<ForwardIterator>::iterator_category());
  173.   }
  174.  
  175. #endif /* _RWSTD_NO_CLASS_PARTIAL_SPEC */
  176.  
  177. //
  178. // __iterator_category returns the category of an iterator
  179. //
  180.  
  181.   template <class T>
  182.   inline random_access_iterator_tag 
  183.   __iterator_category (const T*)
  184.   {
  185.     return random_access_iterator_tag();
  186.   }
  187.  
  188.   template <class Category, class T, class Distance, 
  189.             class Pointer, class Reference> 
  190.   inline Category
  191.   __iterator_category (const iterator<Category, T, Distance,Pointer,Reference>&)
  192.   {
  193.     _TYPENAME iterator<Category, T, Distance,T*,T&>::iterator_category tmp;
  194.     return tmp;
  195.   }
  196.  
  197. //
  198. // Special implementation function for determining whether
  199. // or not we can back up an iterator
  200. //
  201.   template <class _TAG>
  202.   inline bool __is_bidirectional_iterator(_TAG)
  203.   { return false; }
  204.  
  205.   template <class _TAG>
  206.   inline bool __is_random_access_iterator(_TAG)
  207.   { return false; }
  208.  
  209.   _RWSTD_TEMPLATE
  210.   inline bool __is_bidirectional_iterator(bidirectional_iterator_tag)
  211.   { return true; }
  212.  
  213.   _RWSTD_TEMPLATE
  214.   inline bool __is_bidirectional_iterator(random_access_iterator_tag)
  215.   { return true; }
  216.  
  217.   _RWSTD_TEMPLATE
  218.   inline bool __is_random_access_iterator(random_access_iterator_tag)
  219.   { return true; }
  220.  
  221. //
  222. // __value_type returns the type of value held by an iterator
  223. //
  224.   template <class Category,class T, class Distance,
  225.             class Pointer, class Reference>
  226.   inline T* __value_type (const iterator<Category,T, Distance,Pointer,Reference>&)
  227.   {
  228.     return _RWSTD_STATIC_CAST(T*,0);
  229.   }
  230.   template <class T>
  231.   inline T* 
  232.   __value_type (const T*)
  233.   {
  234.     return _RWSTD_STATIC_CAST(T*,0);
  235.   }
  236.  
  237. //
  238. // __distance_type returns the difference type of an iterator
  239. //
  240.   template <class Category,class T, class Distance, 
  241.             class Pointer, class Reference>
  242.   inline Distance* 
  243.   __distance_type (const iterator<Category,T, Distance,Pointer,Reference>&)
  244.   {
  245.     return _RWSTD_STATIC_CAST(Distance*,0);
  246.   }
  247.  
  248.   template <class T>
  249.   inline ptrdiff_t* 
  250.   __distance_type (const T*)
  251.   { 
  252.     return _RWSTD_STATIC_CAST(ptrdiff_t*,0);
  253.   }
  254.  
  255. //
  256. // Implementation specific iterator operations.
  257. //
  258.  
  259.   template <class InputIterator, class Distance>
  260.   void __advance (InputIterator& i, Distance n, input_iterator_tag);
  261.  
  262.   template <class ForwardIterator, class Distance>
  263.   void __advance (ForwardIterator& i, Distance n, forward_iterator_tag);
  264.  
  265.   template <class BidirectionalIterator, class Distance>
  266.   void __advance (BidirectionalIterator& i, Distance n, 
  267.                   bidirectional_iterator_tag);
  268.  
  269.   template <class InputIterator, class Distance>
  270.   void __distance (InputIterator first, InputIterator last, Distance& n, 
  271.                    input_iterator_tag);
  272.  
  273.   template <class ForwardIterator, class Distance>
  274.   void __distance (ForwardIterator first, ForwardIterator last, Distance& n, 
  275.                    forward_iterator_tag);
  276.  
  277.   template <class BidirectionalIterator, class Distance>
  278.   void __distance (BidirectionalIterator first, BidirectionalIterator last, 
  279.                    Distance& n, bidirectional_iterator_tag);
  280.  
  281.   template <class RandomAccessIterator, class Distance>
  282.   inline void __distance (RandomAccessIterator first, RandomAccessIterator last, 
  283.                           Distance& n, random_access_iterator_tag)
  284.   {
  285.     n = last - first;
  286.   }
  287.  
  288.   template <class RandomAccessIterator, class Distance>
  289.   inline void __advance (RandomAccessIterator& i, Distance n, 
  290.                          random_access_iterator_tag)
  291.   {
  292.     i += n;
  293.   }
  294.  
  295. //
  296. // End of implemention specific functions
  297. //
  298.  
  299.   template <class ForwardIterator, class Distance>
  300.   inline void distance (ForwardIterator first, ForwardIterator last, Distance& n)
  301.   {
  302. #ifndef _RWSTD_NO_BASE_CLASS_MATCH
  303.     __distance(first, last, n, __iterator_category(first));
  304. #else
  305.     __distance(first, last, n, input_iterator_tag());
  306. #endif
  307.   }
  308. #ifdef _RWSTD_NO_CLASS_PARTIAL_SPEC
  309.   template <class ForwardIterator, class Distance>
  310.   inline void advance (ForwardIterator& i, Distance n)
  311.   {
  312. #ifndef _RWSTD_NO_BASE_CLASS_MATCH
  313.     __advance(i, n, __iterator_category(i));
  314. #else
  315.     __advance(i, n, input_iterator_tag());
  316. #endif
  317.   }
  318. #endif /* _RWSTD_NO_CLASS_PARTIAL_SPEC */
  319.  
  320. //
  321. // Reverse iterator.     
  322. //
  323.  
  324. //
  325. //  Macros for reverse iterator to accomodate non-standard compilers
  326. //
  327. #ifndef _RWSTD_NO_CLASS_PARTIAL_SPEC 
  328. #define _RWSTD_REVERSE_ITERATOR_TEMPLATE template <class Iterator>
  329. #define _RWSTD_REVERSE_ITERATOR_TYPE reverse_iterator<Iterator>
  330. #else
  331. #define _RWSTD_REVERSE_ITERATOR_TEMPLATE \
  332. template <class Iterator, class Category, class T, class Reference,  class Pointer, class Distance>
  333. #define _RWSTD_REVERSE_ITERATOR_TYPE \
  334.  reverse_iterator<Iterator,Category,T,Reference,Pointer,Distance>
  335. #endif
  336. //
  337. // Forward Declarations.
  338. //
  339. #ifdef _RWSTD_NO_UNDEFINED_FRIEND
  340.   _RWSTD_REVERSE_ITERATOR_TEMPLATE class reverse_iterator;
  341.  
  342.   _RWSTD_REVERSE_ITERATOR_TEMPLATE
  343.   inline bool operator== (const _RWSTD_REVERSE_ITERATOR_TYPE& x, 
  344.                           const _RWSTD_REVERSE_ITERATOR_TYPE& y);
  345.  
  346.   _RWSTD_REVERSE_ITERATOR_TEMPLATE
  347.   inline bool operator< (const _RWSTD_REVERSE_ITERATOR_TYPE& x, 
  348.                          const _RWSTD_REVERSE_ITERATOR_TYPE& y);
  349.  
  350.   _RWSTD_REVERSE_ITERATOR_TEMPLATE
  351.   inline 
  352. #ifndef _RWSTD_NO_CLASS_PARTIAL_SPEC 
  353.   _TYPENAME iterator_traits<Iterator>::difference_type
  354. #else
  355.   Distance
  356. #endif // _RWSTD_NO_CLASS_PARTIAL_SPEC 
  357.   operator- (const _RWSTD_REVERSE_ITERATOR_TYPE& x, 
  358.              const _RWSTD_REVERSE_ITERATOR_TYPE& y);
  359.  
  360.   _RWSTD_REVERSE_ITERATOR_TEMPLATE
  361.   inline _RWSTD_REVERSE_ITERATOR_TYPE
  362. #ifndef _RWSTD_NO_CLASS_PARTIAL_SPEC 
  363.   operator+ (_TYPENAME iterator_traits<Iterator>::difference_type n, 
  364.              _RWSTD_REVERSE_ITERATOR_TYPE& x);
  365. #else
  366.   operator+ (Distance n, _RWSTD_REVERSE_ITERATOR_TYPE& x);
  367. #endif // _RWSTD_NO_CLASS_PARTIAL_SPEC 
  368. #endif // _RWSTD_NO_UNDEFINED_FRIEND
  369.  
  370. #ifndef _RWSTD_NO_CLASS_PARTIAL_SPEC 
  371.   template <class Iterator>
  372.   class reverse_iterator
  373.    : public iterator<typename iterator_traits<Iterator>::iterator_category,
  374.             typename iterator_traits<Iterator>::value_type,
  375.             typename iterator_traits<Iterator>::difference_type,           
  376.             typename iterator_traits<Iterator>::pointer,
  377.             typename iterator_traits<Iterator>::reference>
  378.   {
  379.     typedef reverse_iterator<Iterator> self;
  380.   public:
  381.     typedef typename iterator_traits<Iterator>::difference_type difference_type;
  382.     typedef typename iterator_traits<Iterator>::value_type value_type;
  383.     typedef typename iterator_traits<Iterator>::pointer pointer;
  384.     typedef typename iterator_traits<Iterator>::reference reference;
  385.   private:
  386. #else
  387.   template <class Iterator, class Category, class T,
  388.             class Reference _RWSTD_COMPLEX_DEFAULT(T&),
  389.             class Pointer _RWSTD_COMPLEX_DEFAULT(T*),
  390.             class Distance _RWSTD_COMPLEX_DEFAULT(ptrdiff_t) >
  391.   class reverse_iterator
  392.    : public iterator<Category,T, Distance,Pointer,Reference>
  393.  
  394.   {
  395.     typedef reverse_iterator<Iterator,Category,T,Reference,Pointer,Distance> self;
  396.   public:
  397.     typedef Distance difference_type;
  398.     typedef T value_type;
  399.     typedef Reference reference;
  400.     typedef Pointer pointer;
  401.   private:
  402. #endif
  403.  
  404.     friend inline bool (std::operator==)           (const self& x, const self& y);
  405.     friend inline bool (std::operator<)            (const self& x, const self& y);
  406.     friend inline difference_type (std::operator-) (const self& x, const self& y);
  407.     friend inline self (std::operator+)            (difference_type n, const self& x);
  408.  
  409.   protected:
  410.  
  411.     // RW_BUG jww (1998-06-26): For some reason, the functions which
  412.     // immediately follow this class need to access the 'current'
  413.     // member, but none of them are marked as friends!
  414.  
  415. //    Iterator current;  // jjp
  416.  
  417.   public:
  418.     Iterator current;  // jjp
  419.     typedef Iterator iterator_type;
  420.  
  421.     reverse_iterator() {}
  422.     _EXPLICIT reverse_iterator (Iterator x) : current(x) {}
  423. #ifndef _RWSTD_NO_CLASS_PARTIAL_SPEC
  424.     template <class U>
  425.     reverse_iterator (const reverse_iterator<U>& x) : current(x) {}
  426. #endif
  427.     Iterator base () const { return current; }
  428.     reference operator* () const
  429.     { Iterator tmp = current; return *--tmp; }
  430. #ifndef _RWSTD_NO_NONCLASS_ARROW_RETURN
  431.     pointer operator->() const
  432.     { reference tmp = operator*(); return (pointer)&tmp; }
  433. #endif
  434.  
  435.     self& operator++ ()    { --current; return *this;                 }
  436.     self  operator++ (int) { self tmp = *this; --current; return tmp; }
  437.     self& operator-- ()    { ++current; return *this;                 }
  438.     self  operator-- (int) { self tmp = *this; ++current; return tmp; }
  439.  
  440.     self  operator+  (difference_type n) const
  441.     {  self tmp(current - n); return tmp; }
  442.     self& operator+= (difference_type n)       { current -= n; return *this;        }
  443.     self  operator-  (difference_type n) const { self tmp(current + n); return tmp; }
  444.     self& operator-= (difference_type n)       { current += n; return *this;        }
  445.  
  446.     reference operator[] (difference_type n) const { return *(*this + n); }
  447.   };
  448.   _RWSTD_REVERSE_ITERATOR_TEMPLATE
  449.   inline bool operator== (const _RWSTD_REVERSE_ITERATOR_TYPE& x,
  450.                           const _RWSTD_REVERSE_ITERATOR_TYPE& y)
  451.   {
  452.     return x.current == y.current;
  453.   }
  454.  
  455.   _RWSTD_REVERSE_ITERATOR_TEMPLATE
  456.   inline bool operator< (const _RWSTD_REVERSE_ITERATOR_TYPE& x,
  457.                          const _RWSTD_REVERSE_ITERATOR_TYPE& y)
  458.   {
  459.     return y.current < x.current;
  460.   }
  461.  
  462. #if !defined(_RWSTD_NO_NAMESPACE) || !defined(_RWSTD_NO_PART_SPEC_OVERLOAD)
  463.   _RWSTD_REVERSE_ITERATOR_TEMPLATE
  464.   inline bool operator!= (const _RWSTD_REVERSE_ITERATOR_TYPE& x, 
  465.                           const _RWSTD_REVERSE_ITERATOR_TYPE& y)
  466.   {
  467.     return !(x == y);
  468.   }
  469.  
  470.   _RWSTD_REVERSE_ITERATOR_TEMPLATE
  471.   inline bool operator> (const _RWSTD_REVERSE_ITERATOR_TYPE& x, 
  472.                          const _RWSTD_REVERSE_ITERATOR_TYPE& y)
  473.   {
  474.     return y < x;
  475.   }
  476.  
  477.   _RWSTD_REVERSE_ITERATOR_TEMPLATE
  478.   inline bool operator<= (const _RWSTD_REVERSE_ITERATOR_TYPE& x, 
  479.                           const _RWSTD_REVERSE_ITERATOR_TYPE& y)
  480.   {
  481.     return !(y < x);
  482.   }
  483.  
  484.   _RWSTD_REVERSE_ITERATOR_TEMPLATE
  485.   inline bool operator>= (const _RWSTD_REVERSE_ITERATOR_TYPE& x, 
  486.                           const _RWSTD_REVERSE_ITERATOR_TYPE& y)
  487.   {
  488.     return !(x < y);
  489.   }
  490. #endif // _RWSTD_NO_NAMESPACE) || _RWSTD_NO_PART_SPEC_OVERLOAD
  491.  
  492.   _RWSTD_REVERSE_ITERATOR_TEMPLATE
  493.   inline 
  494. #ifndef _RWSTD_NO_CLASS_PARTIAL_SPEC 
  495.   _TYPENAME iterator_traits<Iterator>::difference_type
  496. #else
  497.   Distance
  498. #endif
  499.   operator- (const _RWSTD_REVERSE_ITERATOR_TYPE& x, 
  500.                              const _RWSTD_REVERSE_ITERATOR_TYPE& y)
  501.   {
  502.     return y.current - x.current;
  503.   }
  504.  
  505.   _RWSTD_REVERSE_ITERATOR_TEMPLATE
  506.   inline _RWSTD_REVERSE_ITERATOR_TYPE
  507. #ifndef _RWSTD_NO_CLASS_PARTIAL_SPEC 
  508.   operator+ (_TYPENAME iterator_traits<Iterator>::difference_type n, 
  509.              _RWSTD_REVERSE_ITERATOR_TYPE& x)
  510. #else
  511.   operator+ (Distance n, _RWSTD_REVERSE_ITERATOR_TYPE& x)
  512. #endif
  513.   {
  514.     return _RWSTD_REVERSE_ITERATOR_TYPE(x.current - n);
  515.   }
  516. #ifdef _RWSTD_NO_CLASS_PARTIAL_SPEC
  517. //
  518. // Reverse bidirectional iterator.       
  519. // This is needed to get around non-standard compilers that insist
  520. // on instantiating all members of a class whether they're used 
  521. // or not.
  522. //
  523.  
  524. //
  525. //  Macros for reverse iterator to accomodate non-standard compilers
  526. //
  527. #define _RWSTD_REVERSE_BI_ITERATOR_TYPE \
  528.  __reverse_bi_iterator<Iterator,Category,T,Reference,Pointer,Distance>
  529. //
  530. // Forward Declarations.
  531. //
  532. #ifdef _RWSTD_NO_UNDEFINED_FRIEND
  533.  
  534.   _RWSTD_REVERSE_ITERATOR_TEMPLATE class __reverse_bi_iterator;
  535.  
  536.   _RWSTD_REVERSE_ITERATOR_TEMPLATE
  537.   inline bool operator== (const _RWSTD_REVERSE_BI_ITERATOR_TYPE& x, 
  538.                           const _RWSTD_REVERSE_BI_ITERATOR_TYPE& y);
  539.  
  540. #endif // _RWSTD_NO_UNDEFINED_FRIEND
  541.  
  542.   template <class Iterator, class Category, class T, 
  543.             class Reference _RWSTD_COMPLEX_DEFAULT(T&),
  544.             class Pointer _RWSTD_COMPLEX_DEFAULT(T*), 
  545.             class Distance _RWSTD_COMPLEX_DEFAULT(ptrdiff_t) >
  546.   class __reverse_bi_iterator
  547.    : public iterator<Category,T, Distance,Pointer,Reference>
  548.  
  549.   {
  550.     typedef __reverse_bi_iterator<Iterator,Category,T,Reference,Pointer,Distance> self;
  551.     typedef Distance difference_type;
  552.     typedef T value_type;
  553.     typedef Reference reference;
  554.     typedef Pointer pointer;
  555.  
  556.     friend inline bool (std::operator==)    (const self& x, const self& y);
  557.         
  558.   protected:
  559.  
  560.     Iterator current;
  561.  
  562.   public:
  563.     typedef Iterator iterator_type;
  564.  
  565.     __reverse_bi_iterator() {}
  566.     _EXPLICIT __reverse_bi_iterator (Iterator x) : current(x) {}
  567. #ifndef _RWSTD_NO_CLASS_PARTIAL_SPEC 
  568.     template <class U>
  569.     _EXPLICIT __reverse_bi_iterator (const reverse_iterator<U>) : current(x) {}
  570. #endif
  571.     Iterator base () const { return current; }
  572.     reference operator* () const 
  573.     { Iterator tmp = current; return *--tmp; }
  574. #ifndef _RWSTD_NO_NONCLASS_ARROW_RETURN
  575.     pointer operator->() const 
  576.     { reference tmp = operator*(); return (pointer)&tmp; }
  577. #endif
  578.  
  579.     self& operator++ ()    { --current; return *this;                 }
  580.     self  operator++ (int) { self tmp = *this; --current; return tmp; }
  581.     self& operator-- ()    { ++current; return *this;                 }
  582.     self  operator-- (int) { self tmp = *this; ++current; return tmp; }
  583.   };
  584.   _RWSTD_REVERSE_ITERATOR_TEMPLATE
  585.   inline bool operator== (const _RWSTD_REVERSE_BI_ITERATOR_TYPE& x, 
  586.                           const _RWSTD_REVERSE_BI_ITERATOR_TYPE& y)
  587.   {
  588.     return x.current == y.current;
  589.   }
  590.  
  591. #if !defined(_RWSTD_NO_NAMESPACE) || !defined(_RWSTD_NO_PART_SPEC_OVERLOAD)
  592.   _RWSTD_REVERSE_ITERATOR_TEMPLATE
  593.   inline bool operator!= (const _RWSTD_REVERSE_BI_ITERATOR_TYPE& x, 
  594.                           const _RWSTD_REVERSE_BI_ITERATOR_TYPE& y)
  595.   {
  596.     return !(x == y);
  597.   }
  598.  
  599. #endif  // _RWSTD_NO_NAMESPACE || _RWSTD_NO_PART_SPEC_OVERLOAD
  600. #endif // _RWSTD_NO_CLASS_PARTIAL_SPEC
  601.  
  602. //
  603. // Back insert iterator.
  604. //
  605.  
  606.   template <class Container>
  607.   class back_insert_iterator 
  608.     : public iterator<output_iterator_tag,
  609.                       _TYPENAME Container::value_type,
  610.                       _TYPENAME Container::difference_type,
  611.                       _TYPENAME Container::pointer,
  612.                       _TYPENAME Container::reference> 
  613.   {
  614.   protected:
  615.  
  616.     Container* container;
  617.  
  618.   public:
  619.     typedef Container container_type;
  620.     typedef _TYPENAME Container::value_type value_type;
  621.  
  622.     _EXPLICIT back_insert_iterator (Container& x) : container(&x) {}
  623.     back_insert_iterator<Container>&
  624.     operator= (const _TYPENAME Container::value_type& value)
  625.     {
  626.       container->push_back(value); return *this;
  627.     }
  628.     back_insert_iterator<Container>& operator*  ()    { return *this; }
  629.     back_insert_iterator<Container>& operator++ ()    { return *this; }
  630.     back_insert_iterator<Container> operator++ (int) { return *this; }
  631.   };
  632.  
  633.   template <class Container>
  634.   inline back_insert_iterator<Container> back_inserter (Container& x)
  635.   {
  636.     return back_insert_iterator<Container>(x);
  637.   }
  638.  
  639. //
  640. // Front insert iterator.
  641. //
  642.  
  643.   template <class Container>
  644.   class front_insert_iterator 
  645.     : public iterator<output_iterator_tag,
  646.                       _TYPENAME Container::value_type,
  647.                       _TYPENAME Container::difference_type,
  648.                       _TYPENAME Container::pointer,
  649.                       _TYPENAME Container::reference> 
  650.   {
  651.   protected:
  652.  
  653.     Container* container;
  654.  
  655.   public:
  656.     typedef Container container_type;
  657.     typedef _TYPENAME Container::value_type value_type;
  658.  
  659.     _EXPLICIT front_insert_iterator (Container& x) : container(&x) {}
  660.     front_insert_iterator<Container>&
  661.     operator= (const _TYPENAME Container::value_type& value)
  662.     { 
  663.       container->push_front(value); return *this;
  664.     }
  665.     front_insert_iterator<Container>& operator*  ()    { return *this; }
  666.     front_insert_iterator<Container>& operator++ ()    { return *this; }
  667.     front_insert_iterator<Container> operator++ (int) { return *this; }
  668.   };
  669.  
  670.   template <class Container>
  671.   inline front_insert_iterator<Container> front_inserter (Container& x)
  672.   {
  673.     return front_insert_iterator<Container>(x);
  674.   }
  675.  
  676. //
  677. // Insert iterator.
  678. //
  679.  
  680.   template <class Container>
  681.   class insert_iterator
  682.     : public iterator<output_iterator_tag,
  683.                       _TYPENAME Container::value_type,
  684.                       _TYPENAME Container::difference_type,
  685.                       _TYPENAME Container::pointer,
  686.                       _TYPENAME Container::reference> 
  687.   {
  688.   private:
  689.     _TYPENAME Container::iterator iter;
  690.  
  691.   protected:
  692.     Container*                   container;
  693.  
  694.   public:
  695.     typedef Container container_type;
  696.     typedef _TYPENAME Container::value_type value_type;
  697.  
  698.     insert_iterator (Container& x, _TYPENAME Container::iterator i)
  699.       : container(&x), iter(i) {}
  700.     insert_iterator<Container>&
  701.     operator= (const _TYPENAME Container::value_type& value)
  702.     { 
  703.       iter = container->insert(iter, value); ++iter; return *this;
  704.     }
  705.     insert_iterator<Container>& operator*  ()    { return *this; }
  706.     insert_iterator<Container>& operator++ ()    { return *this; }
  707.     insert_iterator<Container>& operator++ (int) { return *this; }
  708.   };
  709.  
  710.   template <class Container, class Iterator>
  711.   inline insert_iterator<Container> inserter (Container& x, Iterator i)
  712.   {
  713.     _TYPENAME Container::iterator c(i);
  714.     insert_iterator<Container> tmp(x, c);
  715.     return tmp;
  716.   }
  717.  
  718. #ifndef __RW_TRAITS
  719.   template <class charT> struct _RWSTDExportTemplate char_traits;
  720.   _RWSTD_TEMPLATE struct _RWSTDExport char_traits<char>;
  721. #ifndef _RWSTD_NO_WIDE_CHAR
  722.   _RWSTD_TEMPLATE struct _RWSTDExport char_traits<wchar_t>;
  723. #endif
  724. #endif // __RW_TRAITS
  725.  
  726. #ifndef _RWSTD_NO_NAMESPACE
  727. }
  728. #endif
  729.  
  730. #ifdef _RW_STD_IOSTREAM
  731. //#include <iostream>
  732. #else
  733. #include <iostream.h>
  734. #endif
  735.  
  736. #ifndef _RWSTD_NO_NAMESPACE
  737. namespace std {
  738. #endif
  739.  
  740. #ifndef _RW_STD_IOSTREAM
  741. //
  742. // Stream iterators.
  743. //
  744.  
  745. #ifdef _RWSTD_NO_UNDEFINED_FRIEND
  746.   template <class T, class charT, class traits, class Distance>
  747.   class istream_iterator;
  748.  
  749.   template <class T, class charT, class traits, class Distance>
  750.   bool operator== (const istream_iterator<T, charT,traits,Distance>& x,
  751.                    const istream_iterator<T, charT,traits,Distance>& y);
  752. #endif
  753.  
  754.   template <class T, class charT = char, 
  755.             class traits = char_traits<charT>, 
  756.             class Distance = ptrdiff_t>
  757.   class istream_iterator 
  758.     : public iterator<input_iterator_tag,T,Distance,const T*,const T&>
  759.   {
  760.     friend inline bool (std::operator==) (const istream_iterator<T, charT,traits,Distance>& x,
  761.                             const istream_iterator<T, charT,traits,Distance>& y);
  762.   protected:
  763.  
  764.     istream* stream;
  765.     T        value;
  766.     bool     end_marker;
  767.  
  768.     void read ()
  769.     {
  770.       end_marker = (*stream) ? true : false;
  771.       if (end_marker) *stream >> value;
  772.       end_marker = (*stream) ? true : false;
  773.     }
  774.   public:
  775.     typedef T value_type;
  776.     typedef charT char_type;
  777.     typedef traits traits_type;
  778.     typedef istream istream_type;
  779.  
  780.     istream_iterator () : stream(&cin), end_marker(false) {}
  781.     istream_iterator (istream& s) : stream(&s) { read(); }
  782.     istream_iterator ( const istream_iterator<T,charT,traits,Distance>& x )
  783.       :stream(x.stream) , value(x.value) , end_marker(x.end_marker)
  784.     { }
  785.     const T& operator* () const { return value; }
  786. #ifndef _RWSTD_NO_NONCLASS_ARROW_RETURN
  787.     const T* operator->() const { return &value; }
  788. #endif
  789.     istream_iterator<T, charT,traits,Distance>& operator++ ()
  790.     { 
  791.       read(); return *this;
  792.     }
  793.     istream_iterator<T, charT,traits,Distance> operator++ (int)
  794.     {
  795.       istream_iterator<T, charT,traits,Distance> tmp = *this; 
  796.       read(); 
  797.       return tmp;
  798.     }
  799.   };
  800.  
  801.   template <class T, class charT, class traits, class Distance>
  802.   inline bool operator== (const istream_iterator<T, charT,traits,Distance>& x,
  803.                           const istream_iterator<T, charT,traits,Distance>& y)
  804.   {
  805.     return x.stream == y.stream && x.end_marker == y.end_marker ||
  806.     x.end_marker == false && y.end_marker == false;
  807.   }
  808.  
  809. #if !defined(_RWSTD_NO_NAMESPACE) || !defined(_RWSTD_NO_PART_SPEC_OVERLOAD)
  810.   template <class T, class charT, class traits, class Distance>
  811.   inline bool operator!= (const istream_iterator<T, charT,traits,Distance>& x,
  812.                           const istream_iterator<T, charT,traits,Distance>& y)
  813.   {
  814.     return !(x == y);
  815.   }
  816. #endif
  817.  
  818. #ifndef _RWSTD_NO_COMPLEX_DEFAULT_TEMPLATES
  819.   template <class T, class charT = char, 
  820.   class traits = char_traits<charT> >
  821. #else
  822.   template <class T, class charT, class traits>
  823. #endif
  824.   class ostream_iterator : public iterator<output_iterator_tag,T,ptrdiff_t,T*,T&>
  825.   {
  826.   protected:
  827.  
  828.     ostream* stream;
  829.     const char*    str;
  830.  
  831.   public:
  832.     typedef T value_type;
  833.     typedef charT char_type;
  834.     typedef traits traits_type;
  835.     typedef ostream istream_type;
  836.  
  837.     ostream_iterator (ostream& s) : stream(&s), str(0) { ; }
  838.     ostream_iterator (ostream& s,const char* c) 
  839.       : stream(&s), str((char *)c)  { ; }
  840.     ostream_iterator ( const ostream_iterator<T,charT,traits>& x )
  841.       :stream(x.stream) , str(x.str)
  842.     { ; }
  843.     ostream_iterator<T,charT,traits>& operator= (const T& value)
  844.     { 
  845.       *stream << value;
  846.       if (str) *stream << str;
  847.       return *this;
  848.     }
  849.     ostream_iterator<T,charT,traits>& operator*  ()    { return *this; }
  850.     ostream_iterator<T,charT,traits>& operator++ ()    { return *this; } 
  851.     ostream_iterator<T,charT,traits>& operator++ (int) { return *this; }
  852.   };
  853.  
  854. #endif /* _RW_STD_IOSTREAM */
  855. #ifndef _RWSTD_NO_NAMESPACE
  856. }
  857. #endif
  858.  
  859. #ifdef _RWSTD_NO_TEMPLATE_REPOSITORY
  860. #include <iterator.cc>
  861. #endif
  862.  
  863. #endif /* __STD_ITERATOR__ */
  864. #ifndef __USING_STD_NAMES__
  865.   using namespace std;
  866. #endif
  867. #pragma option pop
  868. #endif /* __ITERATOR_H */
  869.