Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

Vector.H

Go to the documentation of this file.
00001 /* _______              __
00002   / ___/ /  ___  __ _  / /  ___
00003  / /__/ _ \/ _ \/  ' \/ _ \/ _ \
00004  \___/_//_/\___/_/_/_/_.__/\___/ 
00005 */
00006 //
00007 // This software is copyright (C) by the Lawrence Berkeley
00008 // National Laboratory.  Permission is granted to reproduce
00009 // this software for non-commercial purposes provided that
00010 // this notice is left intact.
00011 // 
00012 // It is acknowledged that the U.S. Government has rights to
00013 // this software under Contract DE-AC03-765F00098 between
00014 // the U.S.  Department of Energy and the University of
00015 // California.
00016 //
00017 // This software is provided as a professional and academic
00018 // contribution for joint exchange. Thus it is experimental,
00019 // is provided ``as is'', with no warranties of any kind
00020 // whatsoever, no support, no promise of updates, or printed
00021 // documentation. By using this software, you acknowledge
00022 // that the Lawrence Berkeley National Laboratory and
00023 // Regents of the University of California shall have no
00024 // liability with respect to the infringement of other
00025 // copyrights by any part of this software.
00026 //
00027 
00028 // 
00029 //  The simplest of all possible vector classes
00030 //  dtg
00031 //  ANAG 10-28-97
00032 // 
00033 
00034 #ifndef _SIMP_VECTOR_H_
00035 #define _SIMP_VECTOR_H_
00036 
00037 #include <cmath>
00038 #include <cstdlib>
00039 #include <cassert>
00040 #include <vector>
00041 #include <typeinfo>
00042 #include <string>
00043 #include <map>
00044 #include <list>
00045 #include <algorithm>
00046 
00047 #ifdef ENABLE_MEMORY_TRACKING
00048 //these objects are used for memory tracking system
00049 typedef std::pair<long int*, long int*> ppair;
00050 typedef std::map<std::string, ppair > VectorList;
00051 
00052 typedef std::list<int> lst; //not currently used, bvs.
00053 typedef std::map<std::string, lst*> incr; // not currently used, bvs
00054 
00055 extern VectorList* vectorList_;
00056 extern incr*       vectorIncr_;
00057 #endif
00058 
00059 using std::vector;
00060 using std::ostream;
00061 
00063 
00072 template <class T>
00073 
00074 class Vector
00075 {
00076 public:
00078 
00082   inline
00083   Vector()
00084   {
00085   }
00086 
00088 
00091   inline
00092   virtual ~Vector()
00093   {
00094 #ifdef ENABLE_MEMORY_TRACKING  
00095     if(size()>0) decrement(size());
00096 #endif
00097   }
00098 
00100 
00103   inline
00104   Vector(const Vector<T>& invec):v(invec.v)
00105   {
00106 #ifdef ENABLE_MEMORY_TRACKING  
00107     increment(invec.size());
00108 #endif
00109   }
00110 
00112 
00114   inline
00115   Vector(const std::vector<T>& invec):v(invec)
00116   {
00117 #ifdef ENABLE_MEMORY_TRACKING  
00118     increment(invec.size());
00119 #endif
00120   }
00121 
00123 
00125   inline
00126   Vector<T>& operator=(const std::vector<T>& invec)
00127   {
00128 #ifdef ENABLE_MEMORY_TRACKING  
00129     decrement(size());
00130 #endif
00131     v=invec;
00132 #ifdef ENABLE_MEMORY_TRACKING  
00133     increment(size());
00134 #endif
00135     return *this;
00136   }
00137 
00139 
00141   inline
00142   Vector<T>& operator=(const Vector<T>& invec)
00143   {
00144 #ifdef ENABLE_MEMORY_TRACKING  
00145     decrement(size());
00146 #endif
00147     v=invec.v;
00148 #ifdef ENABLE_MEMORY_TRACKING  
00149     increment(size());
00150 #endif
00151     return *this;
00152   }
00153 
00155 
00158   inline
00159   Vector<T>& assign(const T& inval)
00160   {
00161     v.assign( size(),inval );
00162     return *this;
00163   }
00164 
00166 
00176   inline
00177   Vector(/*long*/ unsigned int isize)
00178   {
00179     resize(isize);
00180   }
00181 
00182  
00183   
00185 
00187   inline
00188   void clear()
00189   {
00190 #ifdef ENABLE_MEMORY_TRACKING  
00191     decrement(size());
00192 #endif
00193     v.clear();
00194   }
00195 
00197 
00199   inline
00200   size_t size() const
00201   {
00202     return v.size();
00203   }
00204 
00206 
00216   inline
00217   Vector(/*long*/ unsigned int isize, const T& value) : v(isize, value)
00218   {
00219 #ifdef ENABLE_MEMORY_TRACKING  
00220     increment(isize);
00221 #endif
00222   }
00223 
00225 
00238   inline
00239   T& operator[] (/*long*/ unsigned int n) 
00240   { 
00241     assert(n < size());
00242     //vector<T>* svec = static_cast<vector<T>* >(this);
00243     //return  svec->operator[](n);
00244     //    return  (static_cast<vector<T>* >(this))->operator[](n);
00245     return v[n];
00246   } 
00247 
00249 
00260   inline
00261   const T& operator[] (/*long*/ unsigned int n) const
00262   { 
00263     assert(n < size());
00264     //const vector<T>* svec = static_cast<const vector<T>* >(this);
00265     //return  svec->operator[](n);
00266     //   return  (static_cast<const vector<T>* >(this))->operator[](n);
00267     return v[n];
00268   } 
00269 
00271 
00273   inline 
00274   void push_back(const T& in)
00275   {
00276 #ifdef ENABLE_MEMORY_TRACKING  
00277     increment(1);
00278 #endif
00279     //  (static_cast<vector<T>* >(this))->push_back(in);
00280     v.push_back(in);
00281   }
00282 
00284 
00297   inline
00298   void  append(const Vector<T>& invec)
00299   {
00300 #ifdef ENABLE_MEMORY_TRACKING  
00301     increment(invec.size());
00302 #endif
00303     for (int ivec = 0; ivec <  invec.size(); ivec++)
00304       {
00305         // (static_cast<vector<T>* >(this))->push_back(invec[ivec]);
00306         v.push_back(invec[ivec]);
00307       }
00308   }
00309 
00311 
00313   inline
00314   void resize(/*long*/ unsigned int isize)
00315   {
00316     //    vector<T>* svec = static_cast<vector<T>* >(this);
00317 #ifdef ENABLE_MEMORY_TRACKING  
00318     if(isize > size()) increment(isize-size());
00319     else               decrement(size()-isize);
00320 #endif
00321     unsigned int l= size();
00322     //    svec->resize(isize);
00323     v.resize(isize);
00324     for(; l<isize; ++l)
00325       // svec->operator[](l) = T();
00326       v[l] = T();
00327   }
00328 
00329   inline 
00330   void reserve(/*long*/ size_t isize)
00331   {
00332         v.reserve(isize);
00333   }
00334 
00335   inline 
00336   size_t capacity() const
00337   {
00338         return v.capacity();
00339   }
00341 
00343   inline
00344   void resize(/*long*/ unsigned int isize, const T& value)
00345   {
00346     //   vector<T>* svec = static_cast<vector<T>* >(this);
00347 #ifdef ENABLE_MEMORY_TRACKING  
00348     if(isize > size()) increment(isize-size());
00349     else               decrement(size()-isize);
00350 #endif
00351     //    svec->resize(isize, value);
00352     v.resize(isize, value);
00353   }
00354 
00356 
00358   inline void sort()
00359   {
00360     std::sort(v.begin(), v.end());
00361   }
00362 
00363 #ifdef ENABLE_MEMORY_TRACKING  
00364   // static template memory tracking data
00365   static long int bytes;
00366   static long int peak;
00367   //  static lst*  incrementList;
00368 #endif
00369 
00370 private:
00371 
00372 #ifdef ENABLE_MEMORY_TRACKING  
00373   inline
00374   void increment(unsigned int i)
00375   {
00376     // warning: pointless comparison of unsigned integer with zero
00377     // assert(i>=0);  // commented out (ndk)
00378     static unsigned int sizzle = initFunc();
00379     i*=sizzle;
00380     bytes += i;
00381     //  incrementList->insert(incrementList->begin(), i);
00382     if(bytes > peak) peak = bytes;
00383   }
00384 
00385   inline
00386   void decrement(unsigned int i)
00387   {
00388     if(i==0) return;
00389     //assert(bytes>=0);
00390     i*=sizeof(T);
00391     bytes-=i;
00392     //  incrementList->insert(incrementList->begin(), -i);
00393     //  assert(bytes>=0);
00394   }
00395 #endif
00396 
00397   unsigned int initFunc();
00398   std::vector<T> v;
00399 };
00400 
00401 #ifdef ENABLE_MEMORY_TRACKING  
00402 template <class T>
00403 long int Vector<T>::bytes=0;
00404 
00405 template <class T>
00406 long int Vector<T>::peak=0;
00407 
00408 //template <class T>
00409 //lst*  Vector<T>::incrementList=NULL;
00410 #endif
00411 
00412 //class ostream;
00413 
00414 template <class T>
00415 ostream& operator<<(ostream& os, const Vector<T>& vec)
00416 {
00417   for(int i=0; i<vec.size(); i++) os<<vec[i]<<" ";
00418   return os;
00419 }
00420 
00421 #ifdef ENABLE_MEMORY_TRACKING  
00422 
00423 template <class T>
00424 unsigned int  Vector<T>::initFunc()
00425 {
00426   if(vectorList_ == NULL)
00427   {
00428     vectorList_ = new VectorList;
00429     vectorIncr_ = new incr;
00430   }
00431   //  if(incrementList == NULL) incrementList = new lst;
00432   ppair tmp(&bytes, &peak);
00433   vectorList_->operator[](typeid(T).name()) = tmp;
00434   //  vectorIncr_->operator[](typeid(T).name()) = incrementList;
00435   //  bytes = 0;
00436   //  peak = 0;
00437   return sizeof(T);
00438 }
00439 
00440 #endif
00441 #endif
00442 

Generated on Wed Apr 16 14:31:05 2003 for EBChombo by doxygen1.2.16