home *** CD-ROM | disk | FTP | other *** search
- #ifndef __RWTVVECTOR_H__
- #define __RWTVVECTOR_H__
- pragma push_align_members(64);
-
- /*
- * RWTValVector: Parameterized vectors of values
- *
- * $Header: E:/vcs/rw/tvvector.h_v 1.3 17 Mar 1992 19:21:10 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.
- *
- ***************************************************************************
- *
- * Stores a *copy* of the inserted item into the collection.
- *
- * Assumes that T has:
- * - well-defined copy semantics (T::T(const T&) or equiv.);
- * - well-defined assignment semantics (T::operator=(const T&) or equiv.);
- * - well-defined equality semantics (T::operator==(const T&)).
- *
- * Note that while these are automatically defined for builtin types
- * (such as "int", "double", or any pointer), you may need to provide
- * appropriate operators for your own classes, particularly those with
- * constructors and/or pointers to other objects.
- *
- ***************************************************************************
- *
- * $Log: E:/vcs/rw/tvvector.h_v $
- *
- * Rev 1.3 17 Mar 1992 19:21:10 KEFFER
- * Changed BOUNDS_CHECK to RWBOUNDS_CHECK
- *
- * Rev 1.2 16 Mar 1992 17:12:18 KEFFER
- *
- * Rev 1.0 02 Mar 1992 16:10:56 KEFFER
- * Initial revision.
- */
-
- //$DECLARE_TEMPLATE
-
- #ifndef __RWTOOLDEFS_H__
- # include "rw/tooldefs.h"
- #endif
- #include "rw/toolerr.h"
-
- template<class T> class RWTValVector {
-
- protected:
-
- unsigned _npts;
- T* _array;
-
- protected:
-
- void boundsCheck(int i) const;
-
- public:
-
- RWTValVector() {_npts = 0; _array = 0;}
- RWTValVector(unsigned n) {_npts = n; _array = new T[_npts];}
- RWTValVector(unsigned n, T ival);
- RWTValVector(const RWTValVector&);
- ~RWTValVector() { delete[] _array; }
-
- RWTValVector& operator=(const RWTValVector&);
- RWTValVector& operator=(T ival);
- const T* data() const {return _array;}
- unsigned length() const {return _npts;}
- const T& ref(int i) const {return _array[i];}
- void reshape(unsigned);
- T& operator()(int i){
- #ifdef RWBOUNDS_CHECK
- boundsCheck(i);
- #endif
- return _array[i]; }
- T operator()(int i) const {
- #ifdef RWBOUNDS_CHECK
- boundsCheck(i);
- #endif
- return _array[i]; }
- T& operator[](int i) { boundsCheck(i); return _array[i];}
- T operator[](int i) const { boundsCheck(i); return _array[i];}
- };
-
-
- //$IMPLEMENT_TEMPLATE
-
- template<class T>
- RWTValVector<T>::RWTValVector(register unsigned n, T ival)
- {
- register T* dst = _array = new T[_npts=n];
- while (n--) *dst++ = ival;
- }
-
- template<class T>
- RWTValVector<T>::RWTValVector(const RWTValVector<T>& a)
- {
- register unsigned i= _npts = a._npts;
- register T* dst = _array = new T[i];
- register T* src = a._array;
- while (i--) *dst++ = *src++;
- }
-
- template<class T> RWTValVector<T>&
- RWTValVector<T>::operator=(const RWTValVector<T>& a)
- {
- if(_array != a._array){
- delete[] _array; /* Disconnect from old _array */
- register unsigned i = _npts = a._npts;
- register T* dst = _array = new T[i];
- register T* src = a._array;
- while (i--) *dst++ = *src++;
- }
- return *this;
- }
-
- template<class T> RWTValVector<T>&
- RWTValVector<T>::operator=(T ival)
- {
- for (int i=0; i<_npts; i++) _array[i] = ival;
- return *this;
- }
-
- template<class T> void
- RWTValVector<T>::reshape(unsigned N)
- {
- if (N==_npts) return;
- T* newArray = new T[N];
- register unsigned i = (N<=_npts) ? N:_npts;
- register T* src = _array;
- register T* dst = newArray;
- while (i--) *dst++ = *src++;
- delete[] _array;
- _array = newArray;
- _npts = N;
- }
-
- template<class T> void
- RWTValVector<T>::boundsCheck(int i) const
- {
- if (i<0 || i>=_npts)
- RWThrow(RWErrObject(TOOL_INDEX,RWFATAL,__RWTEMPLATE),i,(int)(_npts-1));
- }
-
- pragma pop_align_members();
- #endif /* __RWTVVECTOR_H__ */
-