home *** CD-ROM | disk | FTP | other *** search
- #ifndef __RWTVRTARRY_H__
- #define __RWTVRTARRY_H__
- pragma push_align_members(64);
-
- /*
- * RWTValVirtualArray<T>: A swapping virtual array of type T
- *
- * $Header: E:/vcs/rw/tvrtarry.h_v 1.1 17 Mar 1992 19:41:42 KEFFER $
- *
- ****************************************************************************
- *
- * Rogue Wave Software, Inc.
- * P.O. Box 2328
- * Corvallis, OR 97339
- *
- * Copyright (C) 1992. This software is subject to copyright
- * protection under the laws of the United States and other countries.
- *
- ***************************************************************************
- *
- * $Log: E:/vcs/rw/tvrtarry.h_v $
- *
- * Rev 1.1 17 Mar 1992 19:41:42 KEFFER
- *
- * Rev 1.0 11 Mar 1992 14:10:00 KEFFER
- * Initial revision.
- */
-
- #include "rw/tvref.h"
-
- template <class T> class RWTVirtualElement;
- template <class T> class RWTVirtualSlice;
-
- /****************************************************************
- * *
- * Declaration of RWTValVirtualArray<T> *
- * *
- ****************************************************************/
-
- template <class T> class RWTValVirtualArray {
- public:
- RWTValVirtualArray(long size, RWVirtualPageHeap*);
- RWTValVirtualArray(const RWTValVirtualArray&);
- RWTValVirtualArray(const RWTVirtualSlice<T>&);
- ~RWTValVirtualArray();
-
- RWTValVirtualArray& operator=(const RWTValVirtualArray<T>&);
- void operator=(const RWTVirtualSlice<T>&);
- T operator=(T);
-
- long length() const // Length of vector
- {return _vref->length();}
- T val(long i) const // Readonly access
- {return _vref->val(i);}
- void set(long i, T v) // Set a value
- {cow(); _vref->set(i, v);}
- T operator[](long i) const // Readonly access
- {return _vref->val(i);}
- RWTVirtualElement<T> operator[](long); // Element as lvalue
- RWTVirtualSlice<T> slice(long start, long length); // Slice as lvalue
- void reshape(long newLength)
- {cow(); _vref->reshape(newLength);}
- RWVirtualPageHeap* heap() const
- {return _vref->heap();}
-
- private:
-
- RWTVirtualRef<T>* _vref;
-
- void cow(); // Copy On Write
- friend class RWTVirtualElement<T>;
- friend class RWTVirtualSlice<T>;
- };
-
-
- /****************************************************************
- * *
- * Declaration of RWTVirtualElement<T> *
- * *
- ****************************************************************/
-
- template <class T> class RWTVirtualElement {
- friend class RWTValVirtualArray<T>;
- public:
- operator T() const {return _varray->val(_start);}
- T operator=(T val) {_varray->set(_start, val); return val;}
- protected:
- RWTValVirtualArray<T>* _varray;
- long _start;
- RWTVirtualElement(RWTValVirtualArray<T>* v, long s) : _varray(v), _start(s) { }
- };
-
-
- /****************************************************************
- * *
- * Declaration of RWTVirtualSlice<T> *
- * *
- ****************************************************************/
-
- template <class T> class RWTVirtualSlice : public RWTVirtualElement<T> {
- friend class RWTValVirtualArray<T>;
- public:
- void operator=(const RWTVirtualSlice<T>&);
- void operator=(const RWTValVirtualArray<T>&);
- T operator=(T val);
- protected:
- long _extent;
- RWTVirtualSlice(RWTValVirtualArray<T>* v, long s, long e) : RWTVirtualElement<T>(v,s), _extent(e) { }
- };
-
- /****************************************
- * *
- * INLINES *
- * *
- ****************************************/
-
- template <class T> inline RWTVirtualElement<T>
- RWTValVirtualArray<T>::operator[](long i)
- {
- return RWTVirtualElement<T>(this,i);
- }
-
- template <class T> inline RWTVirtualSlice<T>
- RWTValVirtualArray<T>::slice(long start, long length)
- {
- return RWTVirtualSlice<T>(this,start,length);
- }
-
- /****************************************************************
- ****************************************************************
- * *
- * RWTValVirtualArray<T> *
- * Definitions *
- * *
- ****************************************************************
- ****************************************************************/
-
- #include "rw/vpage.h"
-
- template <class T>
- RWTValVirtualArray<T>::RWTValVirtualArray(long size, RWVirtualPageHeap* heap)
- {
- _vref = new RWTVirtualRef<T>(size, heap);
- }
-
- template <class T>
- RWTValVirtualArray<T>::~RWTValVirtualArray()
- {
- if (_vref->removeReference() == 0) delete _vref;
- }
-
- template <class T>
- RWTValVirtualArray<T>::RWTValVirtualArray(const RWTValVirtualArray<T>& v)
- {
- _vref = v._vref;
- _vref->addReference();
- }
-
- template <class T>
- RWTValVirtualArray<T>::RWTValVirtualArray(const RWTVirtualSlice<T>& sl)
- {
- _vref = new RWTVirtualRef<T>(sl._extent, sl._varray->heap());
- RWTVirtualRef<T>* vr = (RWTVirtualRef<T>*)sl._varray->_vref;
- _vref->conformalCopy(0, *vr, sl._start, sl._extent);
- }
-
- template <class T> RWTValVirtualArray<T>&
- RWTValVirtualArray<T>::operator=(const RWTValVirtualArray<T>& v)
- {
- v._vref->addReference();
- if (_vref->removeReference() == 0) delete _vref;
- _vref = v._vref;
- return *this;
- }
-
- template <class T> void
- RWTValVirtualArray<T>::operator=(const RWTVirtualSlice<T>& sl)
- {
- RWTValVirtualArray<T>* v2 = (RWTValVirtualArray<T>*)sl._varray;
- RWTVirtualRef<T>* newvref = new RWTVirtualRef<T>(sl._extent, v2->heap());
- newvref->conformalCopy(0, *v2->_vref, sl._start, sl._extent);
- if (_vref->removeReference() == 0) delete _vref;
- _vref = newvref;
- }
-
- template <class T> T
- RWTValVirtualArray<T>::operator=(T val)
- {
- slice(0, length()-1) = val; // Take a slice of self
- return val;
- }
-
- template <class T> void
- RWTValVirtualArray<T>::cow()
- {
- if (_vref->references()>1) {
- _vref->removeReference();
- _vref = new RWTVirtualRef<T>(*_vref);
- }
- }
-
- /****************************************************************
- ****************************************************************
- * *
- * RWTVirtualSlice<T> *
- * Definitions *
- * *
- ****************************************************************
- ****************************************************************/
-
- template <class T> T
- RWTVirtualSlice<T>::operator=(T newVal)
- {
- _varray->cow();
- _varray->_vref->set(_start, _extent, newVal);
- return newVal;
- }
-
- template <class T> void
- RWTVirtualSlice<T>::operator=(const RWTVirtualSlice<T>& sl)
- {
- RWVirtualRef& vr = (RWVirtualRef&)sl._varray->_vref;
-
- _varray->cow();
- _varray->_vref->setSlice(_start, _extent, vr, sl._start, sl._extent);
- }
-
- template <class T> void
- RWTVirtualSlice<T>::operator=(const RWTValVirtualArray<T>& v)
- {
- RWTValVirtualArray<T>& va = (RWTValVirtualArray<T>&)v;
- _varray->cow();
- _varray->_vref->setSlice(_start, _extent, *(va._vref), 0, v.length());
- }
-
- pragma pop_align_members();
- #endif /* __RWTVRTARRY_H__ */
-