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

  1. #ifndef __RWTVVECTOR_H__
  2. #define __RWTVVECTOR_H__
  3. pragma push_align_members(64);
  4.  
  5. /*
  6.  * RWTValVector: Parameterized vectors of values
  7.  *
  8.  * $Header:   E:/vcs/rw/tvvector.h_v   1.3   17 Mar 1992 19:21:10   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.  * Stores a *copy* of the inserted item into the collection.
  22.  *
  23.  * Assumes that T has:
  24.  *   - well-defined copy semantics (T::T(const T&) or equiv.);
  25.  *   - well-defined assignment semantics (T::operator=(const T&) or equiv.);
  26.  *   - well-defined equality semantics (T::operator==(const T&)).
  27.  *
  28.  * Note that while these are automatically defined for builtin types
  29.  * (such as "int", "double", or any pointer), you may need to provide
  30.  * appropriate operators for your own classes, particularly those with
  31.  * constructors and/or pointers to other objects.
  32.  *
  33.  ***************************************************************************
  34.  *
  35.  * $Log:   E:/vcs/rw/tvvector.h_v  $
  36.  * 
  37.  *    Rev 1.3   17 Mar 1992 19:21:10   KEFFER
  38.  * Changed BOUNDS_CHECK to RWBOUNDS_CHECK
  39.  * 
  40.  *    Rev 1.2   16 Mar 1992 17:12:18   KEFFER
  41.  * 
  42.  *    Rev 1.0   02 Mar 1992 16:10:56   KEFFER
  43.  * Initial revision.
  44.  */
  45.  
  46. //$DECLARE_TEMPLATE
  47.  
  48. #ifndef __RWTOOLDEFS_H__
  49. #  include "rw/tooldefs.h"
  50. #endif
  51. #include "rw/toolerr.h"
  52.  
  53. template<class T> class RWTValVector {
  54.  
  55. protected:
  56.  
  57.   unsigned        _npts;
  58.   T*            _array;
  59.  
  60. protected:
  61.  
  62.   void            boundsCheck(int i) const;
  63.  
  64. public:
  65.  
  66.   RWTValVector()        {_npts = 0; _array = 0;}
  67.   RWTValVector(unsigned n)    {_npts = n; _array = new T[_npts];}
  68.   RWTValVector(unsigned n, T ival);
  69.   RWTValVector(const RWTValVector&);
  70.   ~RWTValVector()        { delete[] _array; }
  71.  
  72.   RWTValVector&        operator=(const RWTValVector&);
  73.   RWTValVector&        operator=(T ival);
  74.   const T*        data() const     {return _array;}
  75.   unsigned        length() const   {return _npts;}
  76.   const T&        ref(int i) const {return _array[i];}
  77.   void            reshape(unsigned);
  78.   T&            operator()(int i){
  79. #ifdef RWBOUNDS_CHECK
  80.     boundsCheck(i);
  81. #endif
  82.     return _array[i]; }
  83.   T            operator()(int i) const {
  84. #ifdef RWBOUNDS_CHECK
  85.     boundsCheck(i);
  86. #endif
  87.     return _array[i]; }
  88.   T&            operator[](int i)    { boundsCheck(i); return _array[i];}
  89.   T            operator[](int i) const    { boundsCheck(i); return _array[i];}
  90. };
  91.  
  92.  
  93. //$IMPLEMENT_TEMPLATE
  94.  
  95. template<class T>
  96. RWTValVector<T>::RWTValVector(register unsigned n, T ival)
  97. {
  98.   register T* dst = _array = new T[_npts=n];
  99.   while (n--) *dst++ = ival;
  100. }
  101.  
  102. template<class T>
  103. RWTValVector<T>::RWTValVector(const RWTValVector<T>& a)
  104. {
  105.   register unsigned i= _npts = a._npts;
  106.   register T* dst = _array = new T[i];
  107.   register T* src = a._array;
  108.   while (i--) *dst++ = *src++;
  109. }
  110.  
  111. template<class T> RWTValVector<T>&
  112. RWTValVector<T>::operator=(const RWTValVector<T>& a)
  113. {
  114.   if(_array != a._array){
  115.     delete[] _array;    /* Disconnect from old _array */
  116.     register unsigned i = _npts = a._npts;
  117.     register T* dst = _array = new T[i];
  118.     register T* src = a._array;
  119.     while (i--) *dst++ = *src++;
  120.   }
  121.   return *this;
  122. }
  123.                                         
  124. template<class T> RWTValVector<T>&
  125. RWTValVector<T>::operator=(T ival)
  126. {
  127.   for (int i=0; i<_npts; i++) _array[i] = ival;
  128.   return *this;
  129. }
  130.  
  131. template<class T> void
  132. RWTValVector<T>::reshape(unsigned N)
  133. {
  134.   if (N==_npts) return;
  135.   T* newArray = new T[N];
  136.   register unsigned i = (N<=_npts) ? N:_npts;
  137.   register T* src = _array;
  138.   register T* dst = newArray;
  139.   while (i--) *dst++ = *src++;
  140.   delete[] _array;
  141.   _array = newArray;
  142.   _npts = N;
  143. }
  144.  
  145. template<class T> void
  146. RWTValVector<T>::boundsCheck(int i) const
  147. {
  148.   if (i<0 || i>=_npts)
  149.     RWThrow(RWErrObject(TOOL_INDEX,RWFATAL,__RWTEMPLATE),i,(int)(_npts-1));
  150. }
  151.  
  152. pragma pop_align_members();
  153. #endif /* __RWTVVECTOR_H__ */
  154.