home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 6.ddi / MWHC.006 / S2 < prev    next >
Encoding:
Text File  |  1992-06-07  |  6.7 KB  |  238 lines

  1. #ifndef __RWTVRTARRY_H__
  2. #define __RWTVRTARRY_H__
  3. pragma push_align_members(64);
  4.  
  5. /*
  6.  * RWTValVirtualArray<T>: A swapping virtual array of type T
  7.  *
  8.  * $Header:   E:/vcs/rw/tvrtarry.h_v   1.1   17 Mar 1992 19:41:42   KEFFER  $
  9.  *
  10.  ****************************************************************************
  11.  *
  12.  * Rogue Wave Software, Inc.
  13.  * P.O. Box 2328
  14.  * Corvallis, OR 97339
  15.  *
  16.  * Copyright (C) 1992. This software is subject to copyright 
  17.  * protection under the laws of the United States and other countries.
  18.  *
  19.  ***************************************************************************
  20.  *
  21.  * $Log:   E:/vcs/rw/tvrtarry.h_v  $
  22.  * 
  23.  *    Rev 1.1   17 Mar 1992 19:41:42   KEFFER
  24.  * 
  25.  *    Rev 1.0   11 Mar 1992 14:10:00   KEFFER
  26.  * Initial revision.
  27.  */
  28.  
  29. #include "rw/tvref.h"
  30.  
  31. template <class T> class RWTVirtualElement;
  32. template <class T> class RWTVirtualSlice;
  33.  
  34. /****************************************************************
  35.  *                                *
  36.  *        Declaration of RWTValVirtualArray<T>        *
  37.  *                                *
  38.  ****************************************************************/
  39.  
  40. template <class T> class RWTValVirtualArray {
  41. public:
  42.   RWTValVirtualArray(long size, RWVirtualPageHeap*);
  43.   RWTValVirtualArray(const RWTValVirtualArray&);
  44.   RWTValVirtualArray(const RWTVirtualSlice<T>&);
  45.   ~RWTValVirtualArray();
  46.  
  47.   RWTValVirtualArray&    operator=(const RWTValVirtualArray<T>&);
  48.   void            operator=(const RWTVirtualSlice<T>&);
  49.   T            operator=(T);
  50.  
  51.   long            length() const            // Length of vector
  52.                   {return _vref->length();}
  53.   T            val(long i) const        // Readonly access
  54.                   {return _vref->val(i);}
  55.   void            set(long i, T v)        // Set a value
  56.                   {cow(); _vref->set(i, v);}
  57.   T            operator[](long i) const    // Readonly access
  58.                   {return _vref->val(i);}
  59.   RWTVirtualElement<T>    operator[](long);        // Element as lvalue
  60.   RWTVirtualSlice<T>    slice(long start, long length);    // Slice as lvalue
  61.   void            reshape(long newLength)
  62.                   {cow(); _vref->reshape(newLength);}
  63.   RWVirtualPageHeap*    heap() const
  64.                   {return _vref->heap();}
  65.  
  66. private:
  67.  
  68.   RWTVirtualRef<T>*    _vref;
  69.  
  70.   void            cow();        // Copy On Write
  71. friend class RWTVirtualElement<T>;
  72. friend class RWTVirtualSlice<T>;
  73. };
  74.  
  75.  
  76. /****************************************************************
  77.  *                                *
  78.  *        Declaration of RWTVirtualElement<T>        *
  79.  *                                *
  80.  ****************************************************************/
  81.  
  82. template <class T> class RWTVirtualElement {
  83. friend class RWTValVirtualArray<T>;
  84. public:
  85.   operator        T() const        {return _varray->val(_start);}
  86.   T            operator=(T val)    {_varray->set(_start, val); return val;}
  87. protected:
  88.   RWTValVirtualArray<T>*    _varray;
  89.   long                _start;
  90.   RWTVirtualElement(RWTValVirtualArray<T>* v, long s) : _varray(v), _start(s) { }
  91. };
  92.  
  93.  
  94. /****************************************************************
  95.  *                                *
  96.  *        Declaration of RWTVirtualSlice<T>        *
  97.  *                                *
  98.  ****************************************************************/
  99.  
  100. template <class T> class RWTVirtualSlice : public RWTVirtualElement<T> {
  101. friend class RWTValVirtualArray<T>;
  102. public:
  103.   void            operator=(const RWTVirtualSlice<T>&);
  104.   void            operator=(const RWTValVirtualArray<T>&);
  105.   T            operator=(T val);
  106. protected:
  107.   long            _extent;
  108.   RWTVirtualSlice(RWTValVirtualArray<T>* v, long s, long e) : RWTVirtualElement<T>(v,s), _extent(e) { }
  109. };
  110.  
  111. /****************************************
  112.  *                    *
  113.  *        INLINES            *
  114.  *                    *
  115.  ****************************************/
  116.  
  117. template <class T> inline RWTVirtualElement<T>
  118. RWTValVirtualArray<T>::operator[](long i)
  119. {
  120.   return RWTVirtualElement<T>(this,i);
  121. }
  122.  
  123. template <class T> inline RWTVirtualSlice<T>
  124. RWTValVirtualArray<T>::slice(long start, long length)
  125. {
  126.   return RWTVirtualSlice<T>(this,start,length);
  127. }
  128.  
  129. /****************************************************************
  130.  ****************************************************************
  131.  *                                *
  132.  *            RWTValVirtualArray<T>            *
  133.  *            Definitions                *
  134.  *                                *
  135.  ****************************************************************
  136.  ****************************************************************/
  137.  
  138. #include "rw/vpage.h"
  139.  
  140. template <class T>
  141. RWTValVirtualArray<T>::RWTValVirtualArray(long size, RWVirtualPageHeap* heap)
  142. {
  143.   _vref = new RWTVirtualRef<T>(size, heap);
  144. }
  145.  
  146. template <class T>
  147. RWTValVirtualArray<T>::~RWTValVirtualArray()
  148. {
  149.   if (_vref->removeReference() == 0) delete _vref;
  150. }
  151.  
  152. template <class T>
  153. RWTValVirtualArray<T>::RWTValVirtualArray(const RWTValVirtualArray<T>& v)
  154. {
  155.   _vref = v._vref;
  156.   _vref->addReference();
  157. }
  158.  
  159. template <class T>
  160. RWTValVirtualArray<T>::RWTValVirtualArray(const RWTVirtualSlice<T>& sl)
  161. {
  162.   _vref = new RWTVirtualRef<T>(sl._extent, sl._varray->heap());
  163.   RWTVirtualRef<T>* vr = (RWTVirtualRef<T>*)sl._varray->_vref;
  164.   _vref->conformalCopy(0, *vr, sl._start, sl._extent);
  165. }
  166.  
  167. template <class T> RWTValVirtualArray<T>&
  168. RWTValVirtualArray<T>::operator=(const RWTValVirtualArray<T>& v)
  169. {
  170.   v._vref->addReference();
  171.   if (_vref->removeReference() == 0) delete _vref;
  172.   _vref = v._vref;
  173.   return *this;
  174. }
  175.  
  176. template <class T> void
  177. RWTValVirtualArray<T>::operator=(const RWTVirtualSlice<T>& sl)
  178. {
  179.   RWTValVirtualArray<T>* v2 = (RWTValVirtualArray<T>*)sl._varray;
  180.   RWTVirtualRef<T>* newvref = new RWTVirtualRef<T>(sl._extent, v2->heap());
  181.   newvref->conformalCopy(0, *v2->_vref, sl._start, sl._extent);
  182.   if (_vref->removeReference() == 0) delete _vref;
  183.   _vref = newvref;
  184. }
  185.  
  186. template <class T> T
  187. RWTValVirtualArray<T>::operator=(T val)
  188. {
  189.   slice(0, length()-1) = val;        // Take a slice of self
  190.   return val;
  191. }
  192.  
  193. template <class T> void
  194. RWTValVirtualArray<T>::cow()
  195. {
  196.   if (_vref->references()>1) {
  197.     _vref->removeReference();
  198.     _vref = new RWTVirtualRef<T>(*_vref);
  199.   }
  200. }
  201.  
  202. /****************************************************************
  203.  ****************************************************************
  204.  *                                *
  205.  *            RWTVirtualSlice<T>            *
  206.  *            Definitions                *
  207.  *                                *
  208.  ****************************************************************
  209.  ****************************************************************/
  210.  
  211. template <class T> T
  212. RWTVirtualSlice<T>::operator=(T newVal)
  213. {
  214.   _varray->cow();
  215.   _varray->_vref->set(_start, _extent, newVal);
  216.   return newVal;
  217. }
  218.  
  219. template <class T> void
  220. RWTVirtualSlice<T>::operator=(const RWTVirtualSlice<T>& sl)
  221. {
  222.   RWVirtualRef& vr = (RWVirtualRef&)sl._varray->_vref;
  223.  
  224.   _varray->cow();
  225.   _varray->_vref->setSlice(_start, _extent, vr, sl._start, sl._extent);
  226. }
  227.  
  228. template <class T> void
  229. RWTVirtualSlice<T>::operator=(const RWTValVirtualArray<T>& v)
  230. {
  231.   RWTValVirtualArray<T>& va = (RWTValVirtualArray<T>&)v;
  232.   _varray->cow();
  233.   _varray->_vref->setSlice(_start, _extent, *(va._vref), 0, v.length());
  234. }
  235.  
  236. pragma pop_align_members();
  237. #endif    /* __RWTVRTARRY_H__ */
  238.