home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 June / PCFJune.iso / Xenon / C++ / FreeCommandLineTools.exe / Include / Rw / iterator.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-31  |  27.4 KB  |  879 lines

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