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 #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 
00073 template <class T>
00074 
00075 class Vector
00076 {
00077 public:
00079 
00083   inline
00084   Vector()
00085   {
00086   }
00087 
00089 
00092   inline
00093   virtual ~Vector()
00094   {
00095 #ifdef ENABLE_MEMORY_TRACKING  
00096     if(size()>0) decrement(size());
00097 #endif
00098   }
00099 
00101 
00104   inline
00105   Vector(const Vector<T>& invec):v(invec.v)
00106   {
00107 #ifdef ENABLE_MEMORY_TRACKING  
00108     increment(invec.size());
00109 #endif
00110   }
00111 
00113 
00115   inline
00116   Vector(const std::vector<T>& invec):v(invec)
00117   {
00118 #ifdef ENABLE_MEMORY_TRACKING  
00119     increment(invec.size());
00120 #endif
00121   }
00122 
00124 
00126   inline
00127   Vector<T>& operator=(const std::vector<T>& invec)
00128   {
00129 #ifdef ENABLE_MEMORY_TRACKING  
00130     decrement(size());
00131 #endif
00132     v=invec;
00133 #ifdef ENABLE_MEMORY_TRACKING  
00134     increment(size());
00135 #endif
00136     return *this;
00137   }
00138 
00140 
00142   inline
00143   Vector<T>& operator=(const Vector<T>& invec)
00144   {
00145 #ifdef ENABLE_MEMORY_TRACKING  
00146     decrement(size());
00147 #endif
00148     v=invec.v;
00149 #ifdef ENABLE_MEMORY_TRACKING  
00150     increment(size());
00151 #endif
00152     return *this;
00153   }
00154 
00156 
00159   inline
00160   Vector<T>& assign(const T& inval)
00161   {
00162     v.assign( size(),inval );
00163     return *this;
00164   }
00165 
00167 
00177   inline
00178   Vector(/*long*/ unsigned int isize)
00179   {
00180     resize(isize);
00181   }
00182 
00183  
00184   
00186 
00188   inline
00189   void clear()
00190   {
00191 #ifdef ENABLE_MEMORY_TRACKING  
00192     decrement(size());
00193 #endif
00194     v.clear();
00195   }
00196 
00198 
00200   inline
00201   size_t size() const
00202   {
00203     return v.size();
00204   }
00205 
00207 
00217   inline
00218   Vector(/*long*/ unsigned int isize, const T& value) : v(isize, value)
00219   {
00220 #ifdef ENABLE_MEMORY_TRACKING  
00221     increment(isize);
00222 #endif
00223   }
00224 
00226 
00239   inline
00240   T& operator[] (/*long*/ unsigned int n) 
00241   { 
00242     assert(n < size());
00243     //vector<T>* svec = static_cast<vector<T>* >(this);
00244     //return  svec->operator[](n);
00245     //    return  (static_cast<vector<T>* >(this))->operator[](n);
00246     return v[n];
00247   } 
00248 
00250 
00261   inline
00262   const T& operator[] (/*long*/ unsigned int n) const
00263   { 
00264     assert(n < size());
00265     //const vector<T>* svec = static_cast<const vector<T>* >(this);
00266     //return  svec->operator[](n);
00267     //   return  (static_cast<const vector<T>* >(this))->operator[](n);
00268     return v[n];
00269   } 
00270 
00271   inline
00272   void swap(Vector<T>& other)
00273   {
00274         v.swap(other.v);
00275   }
00277 
00279   inline 
00280   void push_back(const T& in)
00281   {
00282 #ifdef ENABLE_MEMORY_TRACKING  
00283     increment(1);
00284 #endif
00285     //  (static_cast<vector<T>* >(this))->push_back(in);
00286     v.push_back(in);
00287   }
00288 
00290 
00303   inline
00304   void  append(const Vector<T>& invec)
00305   {
00306 #ifdef ENABLE_MEMORY_TRACKING  
00307     increment(invec.size());
00308 #endif
00309     for (int ivec = 0; ivec <  invec.size(); ivec++)
00310       {
00311         // (static_cast<vector<T>* >(this))->push_back(invec[ivec]);
00312         v.push_back(invec[ivec]);
00313       }
00314   }
00315 
00317 
00319   inline
00320   void resize(/*long*/ unsigned int isize)
00321   {
00322     //    vector<T>* svec = static_cast<vector<T>* >(this);
00323 #ifdef ENABLE_MEMORY_TRACKING  
00324     if(isize > size()) increment(isize-size());
00325     else               decrement(size()-isize);
00326 #endif
00327     unsigned int l= size();
00328     //    svec->resize(isize);
00329     v.resize(isize);
00330     for(; l<isize; ++l)
00331       // svec->operator[](l) = T();
00332       v[l] = T();
00333   }
00334 
00335   inline 
00336   void reserve(/*long*/ size_t isize)
00337   {
00338         v.reserve(isize);
00339   }
00340 
00341   inline 
00342   size_t capacity() const
00343   {
00344         return v.capacity();
00345   }
00347 
00349   inline
00350   void resize(/*long*/ unsigned int isize, const T& value)
00351   {
00352     //   vector<T>* svec = static_cast<vector<T>* >(this);
00353 #ifdef ENABLE_MEMORY_TRACKING  
00354     if(isize > size()) increment(isize-size());
00355     else               decrement(size()-isize);
00356 #endif
00357     //    svec->resize(isize, value);
00358     v.resize(isize, value);
00359   }
00360 
00362 
00364   inline void sort()
00365   {
00366     std::sort(v.begin(), v.end());
00367   }
00368 
00369 #ifdef ENABLE_MEMORY_TRACKING  
00370   // static template memory tracking data
00371   static long int bytes;
00372   static long int peak;
00373   //  static lst*  incrementList;
00374 #endif
00375 
00376 private:
00377 
00378 #ifdef ENABLE_MEMORY_TRACKING  
00379   inline
00380   void increment(unsigned int i)
00381   {
00382     // warning: pointless comparison of unsigned integer with zero
00383     // assert(i>=0);  // commented out (ndk)
00384     static unsigned int sizzle = initFunc();
00385     i*=sizzle;
00386     bytes += i;
00387     //  incrementList->insert(incrementList->begin(), i);
00388     if(bytes > peak) peak = bytes;
00389   }
00390 
00391   inline
00392   void decrement(unsigned int i)
00393   {
00394     if(i==0) return;
00395     //assert(bytes>=0);
00396     i*=sizeof(T);
00397     bytes-=i;
00398     //  incrementList->insert(incrementList->begin(), -i);
00399     //  assert(bytes>=0);
00400   }
00401 #endif
00402 
00403   unsigned int initFunc();
00404   std::vector<T> v;
00405 };
00406 
00407 #ifdef ENABLE_MEMORY_TRACKING  
00408 template <class T>
00409 long int Vector<T>::bytes=0;
00410 
00411 template <class T>
00412 long int Vector<T>::peak=0;
00413 
00414 //template <class T>
00415 //lst*  Vector<T>::incrementList=NULL;
00416 #endif
00417 
00418 //class ostream;
00419 
00420 template <class T>
00421 ostream& operator<<(ostream& os, const Vector<T>& vec)
00422 {
00423   for(int i=0; i<vec.size(); i++) os<<vec[i]<<" ";
00424   return os;
00425 }
00426 
00427 #ifdef ENABLE_MEMORY_TRACKING  
00428 
00429 template <class T>
00430 unsigned int  Vector<T>::initFunc()
00431 {
00432   if(vectorList_ == NULL)
00433   {
00434     vectorList_ = new VectorList;
00435     vectorIncr_ = new incr;
00436   }
00437   //  if(incrementList == NULL) incrementList = new lst;
00438   ppair tmp(&bytes, &peak);
00439   vectorList_->operator[](typeid(T).name()) = tmp;
00440   //  vectorIncr_->operator[](typeid(T).name()) = incrementList;
00441   //  bytes = 0;
00442   //  peak = 0;
00443   return sizeof(T);
00444 }
00445 
00446 #endif
00447 #endif
00448 

Generated on Wed Jun 2 13:53:35 2004 for Chombo&INSwithParticles by doxygen 1.3.2