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    / /__/ _ \/ _ \/  V \/ _ \/ _ \
00004    \___/_//_/\___/_/_/_/_.__/\___/
00005 */
00006 // CHOMBO Copyright (c) 2000-2004, The Regents of the University of
00007 // California, through Lawrence Berkeley National Laboratory (subject to
00008 // receipt of any required approvals from U.S. Dept. of Energy).  All
00009 // rights reserved.
00010 //
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are met:
00013 //
00014 // (1) Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 // (2) Redistributions in binary form must reproduce the above copyright
00017 // notice, this list of conditions and the following disclaimer in the
00018 // documentation and/or other materials provided with the distribution.
00019 // (3) Neither the name of Lawrence Berkeley National Laboratory, U.S.
00020 // Dept. of Energy nor the names of its contributors may be used to endorse
00021 // or promote products derived from this software without specific prior
00022 // written permission.
00023 //
00024 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00025 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00026 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00027 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
00028 // OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00029 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00030 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00031 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00032 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00033 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00034 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035 //
00036 // You are under no obligation whatsoever to provide any bug fixes,
00037 // patches, or upgrades to the features, functionality or performance of
00038 // the source code ("Enhancements") to anyone; however, if you choose to
00039 // make your Enhancements available either publicly, or directly to
00040 // Lawrence Berkeley National Laboratory, without imposing a separate
00041 // written license agreement for such Enhancements, then you hereby grant
00042 // the following license: a non-exclusive, royalty-free perpetual license
00043 // to install, use, modify, prepare derivative works, incorporate into
00044 // other computer software, distribute, and sublicense such Enhancements or
00045 // derivative works thereof, in binary and source code form.
00046 //
00047 // TRADEMARKS. Product and company names mentioned herein may be the
00048 // trademarks of their respective owners.  Any rights not expressly granted
00049 // herein are reserved.
00050 //
00051 
00052 #ifndef _VECTOR_H_
00053 #define _VECTOR_H_
00054 
00055 #include <cmath>
00056 #include <cstdlib>
00057 #include <cassert>
00058 #include <vector>
00059 #include <typeinfo>
00060 #include <string>
00061 #include <map>
00062 #include <list>
00063 #include <algorithm>
00064 #include <iostream>
00065 
00066 #ifdef ENABLE_MEMORY_TRACKING
00067 //these objects are used for memory tracking system
00068 typedef std::pair<long int*, long int*> ppair;
00069 typedef std::map<std::string, ppair > VectorList;
00070 
00071 typedef std::list<int> lst; //not currently used, bvs.
00072 typedef std::map<std::string, lst*> incr; // not currently used, bvs
00073 
00074 extern VectorList* vectorList_;
00075 extern incr*       vectorIncr_;
00076 #endif
00077 
00078 using std::vector;
00079 using std::ostream;
00080 
00082 
00089 template <class T>
00090 
00091 class Vector
00092 {
00093 public:
00095 
00099   inline
00100   Vector()
00101   {
00102   }
00103 
00105 
00108   inline
00109   virtual ~Vector()
00110   {
00111 #ifdef ENABLE_MEMORY_TRACKING
00112     if(size()>0) decrement(size());
00113 #endif
00114   }
00115 
00117 
00120   inline
00121   Vector(const Vector<T>& invec):v(invec.v)
00122   {
00123 #ifdef ENABLE_MEMORY_TRACKING
00124     increment(invec.size());
00125 #endif
00126   }
00127 
00129 
00131   inline
00132   Vector(const std::vector<T>& invec):v(invec)
00133   {
00134 #ifdef ENABLE_MEMORY_TRACKING
00135     increment(invec.size());
00136 #endif
00137   }
00138 
00140 
00142   inline
00143   Vector<T>& operator=(const std::vector<T>& invec)
00144   {
00145 #ifdef ENABLE_MEMORY_TRACKING
00146     decrement(size());
00147 #endif
00148     v=invec;
00149 #ifdef ENABLE_MEMORY_TRACKING
00150     increment(size());
00151 #endif
00152     return *this;
00153   }
00154 
00156 
00158   inline
00159   Vector<T>& operator=(const Vector<T>& invec)
00160   {
00161 #ifdef ENABLE_MEMORY_TRACKING
00162     decrement(size());
00163 #endif
00164     v=invec.v;
00165 #ifdef ENABLE_MEMORY_TRACKING
00166     increment(size());
00167 #endif
00168     return *this;
00169   }
00170 
00172 
00175   inline
00176   Vector<T>& assign(const T& inval)
00177   {
00178     v.assign( size(),inval );
00179     return *this;
00180   }
00181 
00183 
00193   inline
00194   Vector(/*long*/ unsigned int isize)
00195   {
00196     resize(isize);
00197   }
00198 
00200 
00202   inline
00203   void clear()
00204   {
00205 #ifdef ENABLE_MEMORY_TRACKING
00206     decrement(size());
00207 #endif
00208     v.clear();
00209   }
00210 
00212 
00214   inline
00215   size_t size() const
00216   {
00217     return v.size();
00218   }
00219 
00221 
00231   inline
00232   Vector(/*long*/ unsigned int isize, const T& value) : v(isize, value)
00233   {
00234 #ifdef ENABLE_MEMORY_TRACKING
00235     increment(isize);
00236 #endif
00237   }
00238 
00240 
00253   inline
00254   T& operator[] (/*long*/ unsigned int n)
00255   {
00256     assert(n < size());
00257     //vector<T>* svec = static_cast<vector<T>* >(this);
00258     //return  svec->operator[](n);
00259     //    return  (static_cast<vector<T>* >(this))->operator[](n);
00260     return v[n];
00261   }
00262 
00264 
00275   inline
00276   const T& operator[] (/*long*/ unsigned int n) const
00277   {
00278     assert(n < size());
00279     //const vector<T>* svec = static_cast<const vector<T>* >(this);
00280     //return  svec->operator[](n);
00281     //   return  (static_cast<const vector<T>* >(this))->operator[](n);
00282     return v[n];
00283   }
00284 
00285   inline
00286   const T& back() const
00287   {
00288     return v.back();
00289   }
00290 
00291   inline
00292   void swap(Vector<T>& other)
00293   {
00294     v.swap(other.v);
00295   }
00297 
00299   inline
00300   void push_back(const T& in)
00301   {
00302 #ifdef ENABLE_MEMORY_TRACKING
00303     increment(1);
00304 #endif
00305     //  (static_cast<vector<T>* >(this))->push_back(in);
00306     v.push_back(in);
00307   }
00308 
00310 
00323   inline
00324   void  append(const Vector<T>& invec)
00325   {
00326 #ifdef ENABLE_MEMORY_TRACKING
00327     increment(invec.size());
00328 #endif
00329     for (int ivec = 0; ivec <  invec.size(); ivec++)
00330       {
00331         // (static_cast<vector<T>* >(this))->push_back(invec[ivec]);
00332         v.push_back(invec[ivec]);
00333       }
00334   }
00335 
00337 
00339   inline
00340   void resize(/*long*/ unsigned int isize)
00341   {
00342     //    vector<T>* svec = static_cast<vector<T>* >(this);
00343 #ifdef ENABLE_MEMORY_TRACKING
00344     if(isize > size()) increment(isize-size());
00345     else               decrement(size()-isize);
00346 #endif
00347     unsigned int l= size();
00348     //    svec->resize(isize);
00349     v.resize(isize);
00350     for(; l<isize; ++l)
00351       // svec->operator[](l) = T();
00352       v[l] = T();
00353   }
00354 
00355   inline
00356   void reserve(/*long*/ size_t isize)
00357   {
00358     v.reserve(isize);
00359   }
00360 
00361   inline
00362   size_t capacity() const
00363   {
00364     return v.capacity();
00365   }
00367 
00369   inline
00370   void resize(/*long*/ unsigned int isize, const T& value)
00371   {
00372     //   vector<T>* svec = static_cast<vector<T>* >(this);
00373 #ifdef ENABLE_MEMORY_TRACKING
00374     if(isize > size()) increment(isize-size());
00375     else               decrement(size()-isize);
00376 #endif
00377     //    svec->resize(isize, value);
00378     v.resize(isize, value);
00379   }
00380 
00382 
00384   inline void sort()
00385   {
00386     std::sort(v.begin(), v.end());
00387   }
00388 
00389 #ifdef ENABLE_MEMORY_TRACKING
00390   // static template memory tracking data
00391   static long int bytes;
00392   static long int peak;
00393   //  static lst*  incrementList;
00394 #endif
00395 
00396 private:
00397 
00398 #ifdef ENABLE_MEMORY_TRACKING
00399   inline
00400   void increment(unsigned int i)
00401   {
00402     // warning: pointless comparison of unsigned integer with zero
00403     // assert(i>=0);  // commented out (ndk)
00404     static unsigned int sizzle = initFunc();
00405     i*=sizzle;
00406     bytes += i;
00407     //  incrementList->insert(incrementList->begin(), i);
00408     if(bytes > peak) peak = bytes;
00409   }
00410 
00411   inline
00412   void decrement(unsigned int i)
00413   {
00414     if(i==0) return;
00415     //assert(bytes>=0);
00416     i*=sizeof(T);
00417     bytes-=i;
00418     //  incrementList->insert(incrementList->begin(), -i);
00419     //  assert(bytes>=0);
00420   }
00421 #endif
00422 
00423   unsigned int initFunc();
00424   std::vector<T> v;
00425 };
00426 
00427 #ifdef ENABLE_MEMORY_TRACKING
00428 template <class T>
00429 long int Vector<T>::bytes=0;
00430 
00431 template <class T>
00432 long int Vector<T>::peak=0;
00433 
00434 //template <class T>
00435 //lst*  Vector<T>::incrementList=NULL;
00436 #endif
00437 
00438 //class ostream;
00439 
00440 template <class T>
00441 ostream& operator<<(ostream& os, const Vector<T>& vec)
00442 {
00443   for(int i=0; i<vec.size(); i++) os<<vec[i]<<" ";
00444   return os;
00445 }
00446 
00447 #ifdef ENABLE_MEMORY_TRACKING
00448 
00449 template <class T>
00450 unsigned int  Vector<T>::initFunc()
00451 {
00452   if(vectorList_ == NULL)
00453   {
00454     vectorList_ = new VectorList;
00455     vectorIncr_ = new incr;
00456   }
00457   //  if(incrementList == NULL) incrementList = new lst;
00458   ppair tmp(&bytes, &peak);
00459   vectorList_->operator[](typeid(T).name()) = tmp;
00460   //  vectorIncr_->operator[](typeid(T).name()) = incrementList;
00461   //  bytes = 0;
00462   //  peak = 0;
00463   return sizeof(T);
00464 }
00465 
00466 #endif
00467 
00468 #endif

Generated on Wed Jan 19 17:51:27 2005 for Chombo&INSwithParticles by doxygen1.2.16