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 //these objects are used for memory tracking system
00048 typedef std::pair<long int*, long int*> ppair;
00049 typedef std::map<std::string, ppair > VectorList;
00050 
00051 typedef std::list<int> lst; //not currently used, bvs.
00052 typedef std::map<std::string, lst*> incr; // not currently used, bvs
00053 
00054 extern VectorList* vectorList_;
00055 extern incr*       vectorIncr_;
00056 
00057 using std::vector;
00058 using std::ostream;
00059 
00061 
00070 template <class T>
00071 
00072 class Vector
00073 {
00074 public:
00076 
00080   inline
00081   Vector()
00082   {
00083   }
00084 
00086 
00089   inline
00090   virtual ~Vector()
00091   {
00092 #ifdef ENABLE_MEMORY_TRACKING  
00093     if(size()>0) decrement(size());
00094 #endif
00095   }
00096 
00098 
00101   inline
00102   Vector(const Vector<T>& invec):v(invec.v)
00103   {
00104 #ifdef ENABLE_MEMORY_TRACKING  
00105     increment(invec.size());
00106 #endif
00107   }
00108 
00110 
00112   inline
00113   Vector(const std::vector<T>& invec):v(invec)
00114   {
00115 #ifdef ENABLE_MEMORY_TRACKING  
00116     increment(invec.size());
00117 #endif
00118   }
00119 
00121 
00123   inline
00124   Vector<T>& operator=(const std::vector<T>& invec)
00125   {
00126 #ifdef ENABLE_MEMORY_TRACKING  
00127     decrement(size());
00128 #endif
00129     v=invec;
00130 #ifdef ENABLE_MEMORY_TRACKING  
00131     increment(size());
00132 #endif
00133     return *this;
00134   }
00135 
00137 
00139   inline
00140   Vector<T>& operator=(const Vector<T>& invec)
00141   {
00142 #ifdef ENABLE_MEMORY_TRACKING  
00143     decrement(size());
00144 #endif
00145     v=invec.v;
00146 #ifdef ENABLE_MEMORY_TRACKING  
00147     increment(size());
00148 #endif
00149     return *this;
00150   }
00151 
00153 
00163   inline
00164   Vector(/*long*/ unsigned int isize)
00165   {
00166     resize(isize);
00167   }
00168 
00170 
00172   inline
00173   void clear()
00174   {
00175 #ifdef ENABLE_MEMORY_TRACKING  
00176     decrement(size());
00177 #endif
00178     v.clear();
00179   }
00180 
00182 
00184   inline
00185   size_t size() const
00186   {
00187     return v.size();
00188   }
00189 
00191 
00201   inline
00202   Vector(/*long*/ unsigned int isize, const T& value) : v(isize, value)
00203   {
00204 #ifdef ENABLE_MEMORY_TRACKING  
00205     increment(isize);
00206 #endif
00207   }
00208 
00210 
00223   inline
00224   T& operator[] (/*long*/ unsigned int n) 
00225   { 
00226     assert(n < size());
00227     //vector<T>* svec = static_cast<vector<T>* >(this);
00228     //return  svec->operator[](n);
00229     //    return  (static_cast<vector<T>* >(this))->operator[](n);
00230     return v[n];
00231   } 
00232 
00234 
00245   inline
00246   const T& operator[] (/*long*/ unsigned int n) const
00247   { 
00248     assert(n < size());
00249     //const vector<T>* svec = static_cast<const vector<T>* >(this);
00250     //return  svec->operator[](n);
00251     //   return  (static_cast<const vector<T>* >(this))->operator[](n);
00252     return v[n];
00253   } 
00254 
00256 
00258   inline 
00259   void push_back(const T& in)
00260   {
00261 #ifdef ENABLE_MEMORY_TRACKING  
00262     increment(1);
00263 #endif
00264     //  (static_cast<vector<T>* >(this))->push_back(in);
00265     v.push_back(in);
00266   }
00267 
00269 
00282   inline
00283   void  append(const Vector<T>& invec)
00284   {
00285 #ifdef ENABLE_MEMORY_TRACKING  
00286     increment(invec.size());
00287 #endif
00288     for (int ivec = 0; ivec <  invec.size(); ivec++)
00289       {
00290         // (static_cast<vector<T>* >(this))->push_back(invec[ivec]);
00291         v.push_back(invec[ivec]);
00292       }
00293   }
00294 
00296 
00298   inline
00299   void resize(/*long*/ unsigned int isize)
00300   {
00301     //    vector<T>* svec = static_cast<vector<T>* >(this);
00302 #ifdef ENABLE_MEMORY_TRACKING  
00303     if(isize > size()) increment(isize-size());
00304     else               decrement(size()-isize);
00305 #endif
00306     unsigned int l= size();
00307     //    svec->resize(isize);
00308     v.resize(isize);
00309     for(; l<isize; ++l)
00310       // svec->operator[](l) = T();
00311       v[l] = T();
00312   }
00313 
00315 
00317   inline
00318   void resize(/*long*/ unsigned int isize, const T& value)
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     //    svec->resize(isize, value);
00326     v.resize(isize, value);
00327   }
00328 
00330 
00332   inline void sort()
00333   {
00334     std::sort(v.begin(), v.end());
00335   }
00336 
00337 #ifdef ENABLE_MEMORY_TRACKING  
00338   // static template memory tracking data
00339   static long int bytes;
00340   static long int peak;
00341   //  static lst*  incrementList;
00342 #endif
00343 
00344 private:
00345 
00346 #ifdef ENABLE_MEMORY_TRACKING  
00347   inline
00348   void increment(unsigned int i)
00349   {
00350     // warning: pointless comparison of unsigned integer with zero
00351     // assert(i>=0);  // commented out (ndk)
00352     static unsigned int sizzle = initFunc();
00353     i*=sizzle;
00354     bytes += i;
00355     //  incrementList->insert(incrementList->begin(), i);
00356     if(bytes > peak) peak = bytes;
00357   }
00358 
00359   inline
00360   void decrement(unsigned int i)
00361   {
00362     if(i==0) return;
00363     //assert(bytes>=0);
00364     i*=sizeof(T);
00365     bytes-=i;
00366     //  incrementList->insert(incrementList->begin(), -i);
00367     //  assert(bytes>=0);
00368   }
00369 #endif
00370 
00371   unsigned int initFunc();
00372   std::vector<T> v;
00373 };
00374 
00375 #ifdef ENABLE_MEMORY_TRACKING  
00376 template <class T>
00377 long int Vector<T>::bytes=0;
00378 
00379 template <class T>
00380 long int Vector<T>::peak=0;
00381 
00382 //template <class T>
00383 //lst*  Vector<T>::incrementList=NULL;
00384 #endif
00385 
00386 //class ostream;
00387 
00388 template <class T>
00389 ostream& operator<<(ostream& os, const Vector<T>& vec)
00390 {
00391   for(int i=0; i<vec.size(); i++) os<<vec[i]<<" ";
00392   return os;
00393 }
00394 
00395 template <class T>
00396 unsigned int  Vector<T>::initFunc()
00397 {
00398   if(vectorList_ == NULL)
00399   {
00400     vectorList_ = new VectorList;
00401     vectorIncr_ = new incr;
00402   }
00403   //  if(incrementList == NULL) incrementList = new lst;
00404   ppair tmp(&bytes, &peak);
00405   vectorList_->operator[](typeid(T).name()) = tmp;
00406   //  vectorIncr_->operator[](typeid(T).name()) = incrementList;
00407   //  bytes = 0;
00408   //  peak = 0;
00409   return sizeof(T);
00410 }
00411 
00412 #endif
00413 

Generated on Tue Jul 2 10:42:21 2002 for Chombo by doxygen1.2.16