Main Page | Modules | 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 #include <iostream>
00047 
00048 #ifdef ENABLE_MEMORY_TRACKING
00049 //these objects are used for memory tracking system
00050 typedef std::pair<long int*, long int*> ppair;
00051 typedef std::map<std::string, ppair > VectorList;
00052 
00053 typedef std::list<int> lst; //not currently used, bvs.
00054 typedef std::map<std::string, lst*> incr; // not currently used, bvs
00055 
00056 extern VectorList* vectorList_;
00057 extern incr*       vectorIncr_;
00058 #endif
00059 
00060 using std::vector;
00061 using std::ostream;
00062 
00064 
00071 template <class T>
00072 
00073 class Vector
00074 {
00075 public:
00077 
00081   inline
00082   Vector()
00083   {
00084   }
00085 
00087 
00090   inline
00091   virtual ~Vector()
00092   {
00093 #ifdef ENABLE_MEMORY_TRACKING  
00094     if(size()>0) decrement(size());
00095 #endif
00096   }
00097 
00099 
00102   inline
00103   Vector(const Vector<T>& invec):v(invec.v)
00104   {
00105 #ifdef ENABLE_MEMORY_TRACKING  
00106     increment(invec.size());
00107 #endif
00108   }
00109 
00111 
00113   inline
00114   Vector(const std::vector<T>& invec):v(invec)
00115   {
00116 #ifdef ENABLE_MEMORY_TRACKING  
00117     increment(invec.size());
00118 #endif
00119   }
00120 
00122 
00124   inline
00125   Vector<T>& operator=(const std::vector<T>& invec)
00126   {
00127 #ifdef ENABLE_MEMORY_TRACKING  
00128     decrement(size());
00129 #endif
00130     v=invec;
00131 #ifdef ENABLE_MEMORY_TRACKING  
00132     increment(size());
00133 #endif
00134     return *this;
00135   }
00136 
00138 
00140   inline
00141   Vector<T>& operator=(const Vector<T>& invec)
00142   {
00143 #ifdef ENABLE_MEMORY_TRACKING  
00144     decrement(size());
00145 #endif
00146     v=invec.v;
00147 #ifdef ENABLE_MEMORY_TRACKING  
00148     increment(size());
00149 #endif
00150     return *this;
00151   }
00152 
00154 
00157   inline
00158   Vector<T>& assign(const T& inval)
00159   {
00160     v.assign( size(),inval );
00161     return *this;
00162   }
00163 
00165 
00175   inline
00176   Vector(/*long*/ unsigned int isize)
00177   {
00178     resize(isize);
00179   }
00180 
00181  
00182   
00184 
00186   inline
00187   void clear()
00188   {
00189 #ifdef ENABLE_MEMORY_TRACKING  
00190     decrement(size());
00191 #endif
00192     v.clear();
00193   }
00194 
00196 
00198   inline
00199   size_t size() const
00200   {
00201     return v.size();
00202   }
00203 
00205 
00215   inline
00216   Vector(/*long*/ unsigned int isize, const T& value) : v(isize, value)
00217   {
00218 #ifdef ENABLE_MEMORY_TRACKING  
00219     increment(isize);
00220 #endif
00221   }
00222 
00224 
00237   inline
00238   T& operator[] (/*long*/ unsigned int n) 
00239   { 
00240     assert(n < size());
00241     //vector<T>* svec = static_cast<vector<T>* >(this);
00242     //return  svec->operator[](n);
00243     //    return  (static_cast<vector<T>* >(this))->operator[](n);
00244     return v[n];
00245   } 
00246 
00248 
00259   inline
00260   const T& operator[] (/*long*/ unsigned int n) const
00261   { 
00262     assert(n < size());
00263     //const vector<T>* svec = static_cast<const vector<T>* >(this);
00264     //return  svec->operator[](n);
00265     //   return  (static_cast<const vector<T>* >(this))->operator[](n);
00266     return v[n];
00267   } 
00268 
00269   inline
00270   void swap(Vector<T>& other)
00271   {
00272         v.swap(other.v);
00273   }
00275 
00277   inline 
00278   void push_back(const T& in)
00279   {
00280 #ifdef ENABLE_MEMORY_TRACKING  
00281     increment(1);
00282 #endif
00283     //  (static_cast<vector<T>* >(this))->push_back(in);
00284     v.push_back(in);
00285   }
00286 
00288 
00301   inline
00302   void  append(const Vector<T>& invec)
00303   {
00304 #ifdef ENABLE_MEMORY_TRACKING  
00305     increment(invec.size());
00306 #endif
00307     for (int ivec = 0; ivec <  invec.size(); ivec++)
00308       {
00309         // (static_cast<vector<T>* >(this))->push_back(invec[ivec]);
00310         v.push_back(invec[ivec]);
00311       }
00312   }
00313 
00315 
00317   inline
00318   void resize(/*long*/ unsigned int isize)
00319   {
00320     //    vector<T>* svec = static_cast<vector<T>* >(this);
00321 #ifdef ENABLE_MEMORY_TRACKING  
00322     if(isize > size()) increment(isize-size());
00323     else               decrement(size()-isize);
00324 #endif
00325     unsigned int l= size();
00326     //    svec->resize(isize);
00327     v.resize(isize);
00328     for(; l<isize; ++l)
00329       // svec->operator[](l) = T();
00330       v[l] = T();
00331   }
00332 
00333   inline 
00334   void reserve(/*long*/ size_t isize)
00335   {
00336         v.reserve(isize);
00337   }
00338 
00339   inline 
00340   size_t capacity() const
00341   {
00342         return v.capacity();
00343   }
00345 
00347   inline
00348   void resize(/*long*/ unsigned int isize, const T& value)
00349   {
00350     //   vector<T>* svec = static_cast<vector<T>* >(this);
00351 #ifdef ENABLE_MEMORY_TRACKING  
00352     if(isize > size()) increment(isize-size());
00353     else               decrement(size()-isize);
00354 #endif
00355     //    svec->resize(isize, value);
00356     v.resize(isize, value);
00357   }
00358 
00360 
00362   inline void sort()
00363   {
00364     std::sort(v.begin(), v.end());
00365   }
00366 
00367 #ifdef ENABLE_MEMORY_TRACKING  
00368   // static template memory tracking data
00369   static long int bytes;
00370   static long int peak;
00371   //  static lst*  incrementList;
00372 #endif
00373 
00374 private:
00375 
00376 #ifdef ENABLE_MEMORY_TRACKING  
00377   inline
00378   void increment(unsigned int i)
00379   {
00380     // warning: pointless comparison of unsigned integer with zero
00381     // assert(i>=0);  // commented out (ndk)
00382     static unsigned int sizzle = initFunc();
00383     i*=sizzle;
00384     bytes += i;
00385     //  incrementList->insert(incrementList->begin(), i);
00386     if(bytes > peak) peak = bytes;
00387   }
00388 
00389   inline
00390   void decrement(unsigned int i)
00391   {
00392     if(i==0) return;
00393     //assert(bytes>=0);
00394     i*=sizeof(T);
00395     bytes-=i;
00396     //  incrementList->insert(incrementList->begin(), -i);
00397     //  assert(bytes>=0);
00398   }
00399 #endif
00400 
00401   unsigned int initFunc();
00402   std::vector<T> v;
00403 };
00404 
00405 #ifdef ENABLE_MEMORY_TRACKING  
00406 template <class T>
00407 long int Vector<T>::bytes=0;
00408 
00409 template <class T>
00410 long int Vector<T>::peak=0;
00411 
00412 //template <class T>
00413 //lst*  Vector<T>::incrementList=NULL;
00414 #endif
00415 
00416 //class ostream;
00417 
00418 template <class T>
00419 ostream& operator<<(ostream& os, const Vector<T>& vec)
00420 {
00421   for(int i=0; i<vec.size(); i++) os<<vec[i]<<" ";
00422   return os;
00423 }
00424 
00425 #ifdef ENABLE_MEMORY_TRACKING  
00426 
00427 template <class T>
00428 unsigned int  Vector<T>::initFunc()
00429 {
00430   if(vectorList_ == NULL)
00431   {
00432     vectorList_ = new VectorList;
00433     vectorIncr_ = new incr;
00434   }
00435   //  if(incrementList == NULL) incrementList = new lst;
00436   ppair tmp(&bytes, &peak);
00437   vectorList_->operator[](typeid(T).name()) = tmp;
00438   //  vectorIncr_->operator[](typeid(T).name()) = incrementList;
00439   //  bytes = 0;
00440   //  peak = 0;
00441   return sizeof(T);
00442 }
00443 
00444 #endif
00445 #endif
00446 

Generated on Fri Jul 2 17:53:43 2004 for Chombo by doxygen 1.3.2