home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 May / VPR9705A.ISO / VPR_DATA / PROGRAM / CBTRIAL / SETUP / DATA.Z / ITERATOR.H < prev    next >
C/C++ Source or Header  |  1997-02-14  |  22KB  |  756 lines

  1. #ifndef __STD_ITERATOR__
  2. #define __STD_ITERATOR__
  3. /* $Revision:   8.1  $ */
  4.  
  5. /***************************************************************************
  6.  *
  7.  * iterator - iterator declarations for the Standard Library
  8.  *
  9.  * $Id: iterator,v 1.32 1995/10/03 17:39:30 lijewski Exp $
  10.  *
  11.  ***************************************************************************
  12.  *
  13.  * Copyright (c) 1994
  14.  * Hewlett-Packard Company
  15.  *
  16.  * Permission to use, copy, modify, distribute and sell this software
  17.  * and its documentation for any purpose is hereby granted without fee,
  18.  * provided that the above copyright notice appear in all copies and
  19.  * that both that copyright notice and this permission notice appear
  20.  * in supporting documentation.  Hewlett-Packard Company makes no
  21.  * representations about the suitability of this software for any
  22.  * purpose.  It is provided "as is" without express or implied warranty.
  23.  *
  24.  *
  25.  ***************************************************************************
  26.  *
  27.  * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
  28.  * ALL RIGHTS RESERVED
  29.  *
  30.  * The software and information contained herein are proprietary to, and
  31.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  32.  * intends to preserve as trade secrets such software and information.
  33.  * This software is furnished pursuant to a written license agreement and
  34.  * may be used, copied, transmitted, and stored only in accordance with
  35.  * the terms of such license and with the inclusion of the above copyright
  36.  * notice.  This software and information or any other copies thereof may
  37.  * not be provided or otherwise made available to any other person.
  38.  *
  39.  * Notwithstanding any other lease or license that may pertain to, or
  40.  * accompany the delivery of, this computer software and information, the
  41.  * rights of the Government regarding its use, reproduction and disclosure
  42.  * are as set forth in Section 52.227-19 of the FARS Computer
  43.  * Software-Restricted Rights clause.
  44.  *
  45.  * Use, duplication, or disclosure by the Government is subject to
  46.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  47.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  48.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  49.  * P.O. Box 2328, Corvallis, Oregon 97339.
  50.  *
  51.  * This computer software and information is distributed with "restricted
  52.  * rights."  Use, duplication or disclosure is subject to restrictions as
  53.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  54.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  55.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  56.  * then the "Alternate III" clause applies.
  57.  *
  58.  **************************************************************************/
  59.  
  60. #include <stdcomp.h>
  61.  
  62. #ifndef RWSTD_NO_NEW_HEADER
  63. #include <cstddef>
  64. #else
  65. #include <stddef.h>
  66. #endif
  67.  
  68. #ifdef RW_STD_IOSTREAM
  69. #include <iostream>
  70. #else
  71. #include <iostream.h>
  72. #endif
  73.  
  74. #include <function>
  75.  
  76. #ifdef RWSTD_NO_BASE_CLASS_MATCH
  77. #define RWSTD_VALUE_TYPE(a) value_type(*(a))
  78. #else
  79. #define RWSTD_VALUE_TYPE(a) value_type(a)
  80. #endif
  81.  
  82. #ifndef RWSTD_NO_NAMESPACE
  83. namespace std {
  84. #endif
  85.  
  86. //
  87. // Standard iterator tags.
  88. //
  89.  
  90. struct input_iterator_tag
  91. {
  92.     input_iterator_tag() {;}
  93. };
  94.  
  95. struct output_iterator_tag
  96. {
  97.     output_iterator_tag() {;}
  98. };
  99.  
  100. struct forward_iterator_tag
  101. {
  102.     forward_iterator_tag() {;}
  103. };
  104.  
  105. struct bidirectional_iterator_tag
  106. {
  107.     bidirectional_iterator_tag() {;}
  108. };
  109.  
  110. struct random_access_iterator_tag
  111. {
  112.     random_access_iterator_tag() {;}
  113. };
  114.  
  115. //
  116. // Basic iterators.
  117. //
  118.  
  119. template <class T, class Distance> struct input_iterator
  120. {
  121. #ifdef RWSTD_NO_BASE_CLASS_MATCH
  122.     T* operator* () { return (T*)(0); }
  123. #endif
  124. };
  125.  
  126. struct output_iterator {};
  127. template <class T, class Distance> struct forward_iterator
  128. {
  129. #ifdef RWSTD_NO_BASE_CLASS_MATCH
  130.     T* operator* () { return (T*)(0); }
  131. #endif
  132. };
  133.  
  134. template <class T, class Distance> struct bidirectional_iterator
  135. {
  136. #ifdef RWSTD_NO_BASE_CLASS_MATCH
  137.     T* operator* () { return (T*)(0); }
  138. #endif
  139. };
  140.  
  141. template <class T, class Distance> struct random_access_iterator
  142. {
  143. #ifdef RWSTD_NO_BASE_CLASS_MATCH
  144.     T* operator* () { return (T*)(0); }
  145. #endif
  146. };
  147.  
  148. //
  149. // Iterator category.
  150. //
  151.  
  152. template <class T, class Distance>
  153. inline input_iterator_tag
  154. iterator_category (const input_iterator<T, Distance>&)
  155. {
  156.     return input_iterator_tag();
  157. }
  158.  
  159. inline output_iterator_tag iterator_category (const output_iterator&)
  160. {
  161.     return output_iterator_tag();
  162. }
  163.  
  164. template <class T, class Distance>
  165. inline forward_iterator_tag
  166. iterator_category (const forward_iterator<T, Distance>&)
  167. {
  168.     return forward_iterator_tag();
  169. }
  170.  
  171. template <class T, class Distance>
  172. inline bidirectional_iterator_tag
  173. iterator_category (const bidirectional_iterator<T, Distance>&)
  174. {
  175.     return bidirectional_iterator_tag();
  176. }
  177.  
  178. template <class T, class Distance>
  179. inline random_access_iterator_tag
  180. iterator_category (const random_access_iterator<T, Distance>&)
  181. {
  182.     return random_access_iterator_tag();
  183. }
  184.  
  185. template <class T>
  186. inline random_access_iterator_tag iterator_category (const T*)
  187. {
  188.     return random_access_iterator_tag();
  189. }
  190.  
  191. //
  192. // Value type.
  193. //
  194.  
  195. #ifndef RWSTD_NO_BASE_CLASS_MATCH
  196. template <class T, class Distance>
  197. inline T* value_type (const input_iterator<T, Distance>&)
  198. {
  199.     return (T*)(0);
  200. }
  201.  
  202. template <class T, class Distance>
  203. inline T* value_type (const forward_iterator<T, Distance>&)
  204. {
  205.     return (T*)(0);
  206. }
  207.  
  208. template <class T, class Distance>
  209. inline T* value_type (const bidirectional_iterator<T, Distance>&)
  210. {
  211.     return (T*)(0);
  212. }
  213.  
  214. template <class T, class Distance>
  215. inline T* value_type (const random_access_iterator<T, Distance>&)
  216. {
  217.     return (T*)(0);
  218. }
  219.  
  220. template <class T>
  221. inline T* value_type (const T*) { return (T*)(0); }
  222. #else
  223. template <class T>
  224. inline T* value_type (const T) { return (T*)(0); }
  225. #endif
  226.  
  227. //
  228. // Distance type.
  229. //
  230.  
  231. #ifndef RWSTD_NO_BASE_CLASS_MATCH
  232. template <class T, class Distance>
  233. inline Distance* distance_type (const input_iterator<T, Distance>&)
  234. {
  235.     return (Distance*)(0);
  236. }
  237.  
  238. template <class T, class Distance>
  239. inline Distance* distance_type (const forward_iterator<T, Distance>&)
  240. {
  241.     return (Distance*)(0);
  242. }
  243.  
  244. template <class T, class Distance>
  245. inline Distance*
  246. distance_type (const bidirectional_iterator<T, Distance>&)
  247. {
  248.     return (Distance*)(0);
  249. }
  250.  
  251. template <class T, class Distance>
  252. inline Distance*
  253. distance_type (const random_access_iterator<T, Distance>&)
  254. {
  255.     return (Distance*)(0);
  256. }
  257.  
  258. template <class T>
  259. inline ptrdiff_t* distance_type (const T*) { return (ptrdiff_t*)(0); }
  260. #else
  261. template <class T>
  262. inline ptrdiff_t* distance_type (const T)  { return (ptrdiff_t*)(0); }
  263. #endif
  264.  
  265. //
  266. // Iterator operations.
  267. //
  268.  
  269. template <class InputIterator, class Distance>
  270. void __distance (InputIterator first, InputIterator last, Distance& n,
  271.                  input_iterator_tag)
  272. {
  273.     while (first != last) { ++first; ++n; }
  274. }
  275.  
  276. template <class ForwardIterator, class Distance>
  277. void __distance (ForwardIterator first, ForwardIterator last, Distance& n,
  278.                  forward_iterator_tag)
  279. {
  280.     while (first != last) { ++first; ++n; }
  281. }
  282.  
  283. template <class BidirectionalIterator, class Distance>
  284. void __distance (BidirectionalIterator first, BidirectionalIterator last,
  285.                  Distance& n, bidirectional_iterator_tag)
  286. {
  287.     while (first != last) { ++first; ++n; }
  288. }
  289.  
  290. template <class RandomAccessIterator, class Distance>
  291. inline void __distance (RandomAccessIterator first, RandomAccessIterator last,
  292.                         Distance& n, random_access_iterator_tag)
  293. {
  294.     n = last - first;
  295. }
  296.  
  297. template <class InputIterator, class Distance>
  298. inline void distance (InputIterator first, InputIterator last, Distance& n)
  299. {
  300. #ifndef RWSTD_NO_BASE_CLASS_MATCH
  301.     __distance(first, last, n, iterator_category(first));
  302. #else
  303.     __distance(first, last, n, input_iterator_tag());
  304. #endif
  305. }
  306.  
  307. #ifdef RWSTD_NO_BASE_CLASS_MATCH
  308. //
  309. // We include assert() to test for possible problem in advance().
  310. // Furthermore, we FORCE assert() to always expand.
  311. //
  312. #ifdef  NDEBUG
  313. #define __RW_NDEBUG
  314. #undef  NDEBUG
  315. #endif
  316. #ifndef RWSTD_NO_NEW_HEADER
  317. #include <cassert>
  318. #else
  319. #include <assert.h>
  320. #endif
  321.  
  322. #endif /*RWSTD_NO_BASE_CLASS_MATCH*/
  323.  
  324. template <class InputIterator, class Distance>
  325. void __advance (InputIterator& i, Distance n, input_iterator_tag)
  326. {
  327. #ifdef RWSTD_NO_BASE_CLASS_MATCH
  328.     //
  329.     // All uses of advance() end up calling this template, even
  330.     // when advance() is being invoked on a bidirectional or random
  331.     // iterator.  We need to check that n is non-negative, or else
  332.     // this algorithm will fail horribly.  We MUST document the
  333.     // restriction that advance() only be called with non-negative
  334.     // Distance.  There don't appear to be any explicit uses of advance()
  335.     // with a negative Distance argument in the STL library itself.
  336.     //
  337.     // This assert() is ALWAYS on -- see how it's included'd above.
  338.     //
  339.     assert(n >= 0);
  340. #endif /*RWSTD_NO_BASE_CLASS_MATCH*/
  341.     while (n--) ++i;
  342. }
  343.  
  344. //
  345. // Don't forget to turn off expansion of assert() if that's what the
  346. // user expects.
  347. //
  348. #ifdef  __RW_NDEBUG
  349. #define NDEBUG
  350. #undef  __RW_NDEBUG
  351. #endif
  352.  
  353. template <class ForwardIterator, class Distance>
  354. void __advance (ForwardIterator& i, Distance n, forward_iterator_tag)
  355. {
  356.     while (n--) ++i;
  357. }
  358.  
  359. template <class BidirectionalIterator, class Distance>
  360. void __advance (BidirectionalIterator& i, Distance n,
  361.                 bidirectional_iterator_tag)
  362. {
  363.     if (n >= 0)
  364.         while (n--) ++i;
  365.     else
  366.         while (n++) --i;
  367. }
  368.  
  369. template <class RandomAccessIterator, class Distance>
  370. inline void __advance (RandomAccessIterator& i, Distance n,
  371.                        random_access_iterator_tag)
  372. {
  373.     i += n;
  374. }
  375.  
  376. template <class InputIterator, class Distance>
  377. inline void advance (InputIterator& i, Distance n)
  378. {
  379. #ifndef RWSTD_NO_BASE_CLASS_MATCH
  380.     __advance(i, n, iterator_category(i));
  381. #else
  382.     __advance(i, n, input_iterator_tag());
  383. #endif
  384. }
  385.  
  386. //
  387. // Reverse bidirectional iterator.
  388. //
  389.  
  390.     //
  391.     // Forward declarations.
  392.     //
  393.  
  394. #ifdef RWSTD_NO_UNDECLARED_FRIEND
  395. template <class BidirectionalIterator, class T, class Reference,
  396.           class Distance> class reverse_bidirectional_iterator;
  397.  
  398. template <class BidirectionalIterator, class T, class Reference,
  399.           class Distance>
  400. bool operator== (
  401.       const reverse_bidirectional_iterator<BidirectionalIterator, T, Reference,
  402.                                            Distance>& x,
  403.       const reverse_bidirectional_iterator<BidirectionalIterator, T, Reference,
  404.                                            Distance>& y);
  405. #endif
  406.  
  407. #ifndef RWSTD_NO_DEFAULT_TEMPLATES
  408. template <class BidirectionalIterator, class T, class Reference = T&,
  409.           class Distance = ptrdiff_t>
  410. #else
  411. template <class BidirectionalIterator, class T, class Reference,
  412.           class Distance>
  413. #endif
  414.  
  415. //
  416. // Reference = T&
  417. // Distance  = ptrdiff_t
  418. //
  419. class reverse_bidirectional_iterator
  420.     : public bidirectional_iterator<T,Distance>
  421. {
  422.     typedef reverse_bidirectional_iterator<BidirectionalIterator, T, Reference,
  423.                                            Distance> self;
  424.     friend bool operator== (const self& x, const self& y);
  425.  
  426.   protected:
  427.  
  428.     BidirectionalIterator current;
  429.  
  430.   public:
  431.  
  432.     reverse_bidirectional_iterator() {}
  433.     explicit reverse_bidirectional_iterator (BidirectionalIterator x)
  434.         : current(x) {}
  435.     BidirectionalIterator base() { return current; }
  436.     Reference operator* () const
  437.     {
  438.         BidirectionalIterator tmp = current; return *--tmp;
  439.     }
  440.     self& operator++ ()    { --current; return *this;                 }
  441.     self  operator++ (int) { self tmp = *this; --current; return tmp; }
  442.     self& operator-- ()    { ++current; return *this;                 }
  443.     self  operator-- (int) { self tmp = *this; ++current; return tmp; }
  444. };
  445.  
  446. template <class BidirectionalIterator, class T, class Reference,
  447.           class Distance>
  448. inline bool operator== (
  449.     const reverse_bidirectional_iterator<BidirectionalIterator, T, Reference,
  450.                                          Distance>& x,
  451.     const reverse_bidirectional_iterator<BidirectionalIterator, T, Reference,
  452.                                          Distance>& y)
  453. {
  454.     return x.current == y.current;
  455. }
  456.  
  457. //
  458. // Reverse iterator.
  459. //
  460.  
  461.     //
  462.     // Forward Declarations.
  463.     //
  464. #ifdef RWSTD_NO_UNDECLARED_FRIEND
  465. template <class RandomAccessIterator, class T, class Reference,
  466.           class Distance>  class reverse_iterator;
  467.  
  468. template <class RandomAccessIterator, class T, class Reference, class Distance>
  469. bool operator== (const reverse_iterator<RandomAccessIterator, T,
  470.                  Reference, Distance>& x,
  471.                  const reverse_iterator<RandomAccessIterator, T,
  472.                  Reference, Distance>& y);
  473.  
  474. template <class RandomAccessIterator, class T, class Reference, class Distance>
  475. bool operator< (const reverse_iterator<RandomAccessIterator, T,
  476.                 Reference, Distance>& x,
  477.                 const reverse_iterator<RandomAccessIterator, T,
  478.                 Reference, Distance>& y);
  479.  
  480. template <class RandomAccessIterator, class T, class Reference, class Distance>
  481. Distance operator- (const reverse_iterator<RandomAccessIterator, T,
  482.                     Reference, Distance>& x,
  483.                     const reverse_iterator<RandomAccessIterator, T,
  484.                     Reference, Distance>& y);
  485.  
  486. template <class RandomAccessIterator, class T, class Reference, class Distance>
  487. reverse_iterator<RandomAccessIterator, T, Reference, Distance>
  488. operator+ (Distance n,
  489.       const reverse_iterator<RandomAccessIterator, T, Reference, Distance>& x);
  490. #endif
  491.  
  492. #ifndef RWSTD_NO_DEFAULT_TEMPLATES
  493. template <class RandomAccessIterator, class T, class Reference = T&,
  494.           class Distance = ptrdiff_t>
  495. #else
  496. template <class RandomAccessIterator, class T, class Reference,
  497.           class Distance>
  498. #endif
  499.  
  500. //
  501. // Reference = T&
  502. // Distance  = ptrdiff_t
  503. //
  504. class reverse_iterator : public random_access_iterator<T, Distance>
  505. {
  506.     typedef reverse_iterator<RandomAccessIterator,T,Reference,Distance> self;
  507.  
  508.     friend bool operator==    (const self& x, const self& y);
  509.     friend bool operator<     (const self& x, const self& y);
  510.     friend Distance operator- (const self& x, const self& y);
  511.     friend self operator+     (Distance n, const self& x);
  512.  
  513.   protected:
  514.  
  515.     RandomAccessIterator current;
  516.  
  517.   public:
  518.  
  519.     reverse_iterator() {}
  520.     explicit reverse_iterator (RandomAccessIterator x) : current(x) {}
  521.     RandomAccessIterator base () { return current; }
  522.     Reference operator* () const { return *(current - 1); }
  523.  
  524.     self& operator++ ()    { --current; return *this;                 }
  525.     self  operator++ (int) { self tmp = *this; --current; return tmp; }
  526.     self& operator-- ()    { ++current; return *this;                 }
  527.     self  operator-- (int) { self tmp = *this; ++current; return tmp; }
  528.  
  529.     self  operator+  (Distance n) const { self tmp(current - n); return tmp; }
  530.     self& operator+= (Distance n)       { current -= n; return *this;        }
  531.     self  operator-  (Distance n) const { self tmp(current + n); return tmp; }
  532.     self& operator-= (Distance n)       { current += n; return *this;        }
  533.  
  534.     Reference operator[] (Distance n) { return *(*this + n); }
  535. };
  536.  
  537. template <class RandomAccessIterator, class T, class Reference, class Distance>
  538. inline bool operator== (const reverse_iterator<RandomAccessIterator, T,
  539.                         Reference, Distance>& x,
  540.                         const reverse_iterator<RandomAccessIterator, T,
  541.                         Reference, Distance>& y)
  542. {
  543.     return x.current == y.current;
  544. }
  545.  
  546. template <class RandomAccessIterator, class T, class Reference, class Distance>
  547. inline bool operator< (const reverse_iterator<RandomAccessIterator, T,
  548.                        Reference, Distance>& x,
  549.                        const reverse_iterator<RandomAccessIterator, T,
  550.                        Reference, Distance>& y)
  551. {
  552.     return y.current < x.current;
  553. }
  554.  
  555. template <class RandomAccessIterator, class T, class Reference, class Distance>
  556. inline Distance operator- (const reverse_iterator<RandomAccessIterator, T,
  557.                            Reference, Distance>& x,
  558.                            const reverse_iterator<RandomAccessIterator, T,
  559.                            Reference, Distance>& y)
  560. {
  561.     return y.current - x.current;
  562. }
  563.  
  564. template <class RandomAccessIterator, class T, class Reference, class Distance>
  565. inline reverse_iterator<RandomAccessIterator, T, Reference, Distance>
  566. operator+ (Distance n, const reverse_iterator<RandomAccessIterator, T,
  567.            Reference, Distance>& x)
  568. {
  569.     return reverse_iterator<RandomAccessIterator, T, Reference, Distance>
  570.     (x.current - n);
  571. }
  572.  
  573. //
  574. // Back insert iterator.
  575. //
  576.  
  577. template <class Container>
  578. class back_insert_iterator : public output_iterator
  579. {
  580.   protected:
  581.  
  582.     Container& container;
  583.  
  584.   public:
  585.  
  586.     explicit back_insert_iterator (Container& x) : container(x) {}
  587.     back_insert_iterator<Container>&
  588.     operator= (const typename Container::value_type& value)
  589.     {
  590.         container.push_back(value); return *this;
  591.     }
  592.     back_insert_iterator<Container>& operator*  ()    { return *this; }
  593.     back_insert_iterator<Container>& operator++ ()    { return *this; }
  594.     back_insert_iterator<Container>& operator++ (int) { return *this; }
  595. };
  596.  
  597. template <class Container>
  598. inline back_insert_iterator<Container> back_inserter (Container& x)
  599. {
  600.     return back_insert_iterator<Container>(x);
  601. }
  602.  
  603. //
  604. // Front insert iterator.
  605. //
  606.  
  607. template <class Container>
  608. class front_insert_iterator : public output_iterator
  609. {
  610.   protected:
  611.  
  612.     Container& container;
  613.  
  614.   public:
  615.  
  616.     explicit front_insert_iterator (Container& x) : container(x) {}
  617.     front_insert_iterator<Container>&
  618.     operator= (const typename Container::value_type& value)
  619.     {
  620.         container.push_front(value); return *this;
  621.     }
  622.     front_insert_iterator<Container>& operator*  ()    { return *this; }
  623.     front_insert_iterator<Container>& operator++ ()    { return *this; }
  624.     front_insert_iterator<Container>& operator++ (int) { return *this; }
  625. };
  626.  
  627. template <class Container>
  628. inline front_insert_iterator<Container> front_inserter (Container& x)
  629. {
  630.     return front_insert_iterator<Container>(x);
  631. }
  632.  
  633. //
  634. // Insert iterator.
  635. //
  636.  
  637. template <class Container>
  638. class insert_iterator : public output_iterator
  639. {
  640.   protected:
  641.  
  642.     Container&                   container;
  643.     typename Container::iterator iter;
  644.  
  645.   public:
  646.  
  647.     insert_iterator (Container& x, typename Container::iterator i)
  648.         : container(x), iter(i) {}
  649.     insert_iterator<Container>&
  650.     operator= (const typename Container::value_type& value)
  651.     {
  652.         iter = container.insert(iter, value); ++iter; return *this;
  653.     }
  654.     insert_iterator<Container>& operator*  ()    { return *this; }
  655.     insert_iterator<Container>& operator++ ()    { return *this; }
  656.     insert_iterator<Container>& operator++ (int) { return *this; }
  657. };
  658.  
  659. template <class Container, class Iterator>
  660. inline insert_iterator<Container> inserter (Container& x, Iterator i)
  661. {
  662.     typename Container::iterator c(i);
  663.     insert_iterator<Container> tmp(x, c);
  664.     return tmp;
  665. }
  666.  
  667. //
  668. // Stream iterators.
  669. //
  670.  
  671. #ifdef RWSTD_NO_UNDECLARED_FRIEND
  672. template <class T, class Distance>
  673. class istream_iterator;
  674.  
  675. template <class T, class Distance>
  676. bool operator== (const istream_iterator<T, Distance>& x,
  677.                  const istream_iterator<T, Distance>& y);
  678. #endif
  679.  
  680. #ifndef RWSTD_NO_DEFAULT_TEMPLATES
  681. template <class T, class Distance = ptrdiff_t> // Distance == ptrdiff_t
  682. #else
  683. template <class T, class Distance>             // Distance == ptrdiff_t
  684. #endif
  685. class istream_iterator : public input_iterator<T, Distance>
  686. {
  687.   friend bool operator== (const istream_iterator<T, Distance>& x,
  688.                           const istream_iterator<T, Distance>& y);
  689. protected:
  690.  
  691.     istream* stream;
  692.     T        value;
  693.     bool     end_marker;
  694.  
  695.     void read ()
  696.     {
  697.         end_marker = (*stream) ? true : false;
  698.         if (end_marker) *stream >> value;
  699.         end_marker = (*stream) ? true : false;
  700.     }
  701. public:
  702.  
  703.     istream_iterator () : stream(&cin), end_marker(false) {}
  704.     istream_iterator (istream& s) : stream(&s) { read(); }
  705.     const T& operator* () const { return value; }
  706.     istream_iterator<T, Distance>& operator++ ()
  707.     {
  708.         read(); return *this;
  709.     }
  710.     istream_iterator<T, Distance> operator++ (int)
  711.     {
  712.         istream_iterator<T, Distance> tmp = *this; read(); return tmp;
  713.     }
  714. };
  715.  
  716. template <class T, class Distance>
  717. inline bool operator== (const istream_iterator<T, Distance>& x,
  718.                         const istream_iterator<T, Distance>& y)
  719. {
  720.     return x.stream == y.stream && x.end_marker == y.end_marker ||
  721.            x.end_marker == false && y.end_marker == false;
  722. }
  723.  
  724. template <class T>
  725. class ostream_iterator : public output_iterator
  726. {
  727. protected:
  728.  
  729.     ostream* stream;
  730.     char*    string;
  731.  
  732. public:
  733.  
  734.     ostream_iterator (ostream& s) : stream(&s), string(0) {}
  735.     ostream_iterator (ostream& s, char* c) : stream(&s), string(c)  {}
  736.     ostream_iterator<T>& operator= (const T& value)
  737.     {
  738.         *stream << value;
  739.         if (string) *stream << string;
  740.         return *this;
  741.     }
  742.     ostream_iterator<T>& operator*  ()    { return *this; }
  743.     ostream_iterator<T>& operator++ ()    { return *this; }
  744.     ostream_iterator<T>& operator++ (int) { return *this; }
  745. };
  746.  
  747. //
  748. // istreambuf_iterator and ostreambuf_iterator eventually go here.
  749. //
  750.  
  751. #ifndef RWSTD_NO_NAMESPACE
  752. }
  753. #endif
  754.  
  755. #endif
  756.