fixedarray.hh

00001 #ifndef DUNE_FIXEDARRAY_HH
00002 #define DUNE_FIXEDARRAY_HH
00003 
00004 
00005 //***********************************************************************
00006 //
00007 //  implementation of peter array
00008 //
00009 //***********************************************************************
00010 
00011 #include<iostream>
00012 #include<iomanip>
00013 #include<string>
00014 
00015 namespace Dune 
00016 {
00025   template<class T, int N>
00026   class FixedArray {
00027   public:
00028 
00030       typedef T MemberType; 
00031       
00034       enum { n = (N > 0) ? N : 1 };
00035 
00037       enum { dimension = N };
00038 
00040       FixedArray () {}
00041       
00043       FixedArray (T t)
00044       {
00045           for (int i=0; i<N; i++) a[i]=t;
00046       }
00047 
00049       int size() const {return N;}
00050 
00052       FixedArray<T,N>& operator= (const T& t)
00053       {
00054           for (int i=0; i<N; i++) a[i]=t;
00055           return (*this);
00056       }
00057         
00059       T& operator[] (int i)
00060       {
00061           return a[i];
00062       }
00063 
00065       const T& operator[] (int i) const
00066       {
00067           return a[i];
00068       }
00069 
00071       FixedArray<T,N-1> shrink (int comp)
00072       {
00073           FixedArray<T,N-1> x;
00074           for (int i=0; i<comp; i++) x[i] = a[i];
00075           for (int i=comp+1; i<N; i++) x[i-1] = a[i];
00076           return x;
00077       }
00078       
00080       FixedArray<T,N+1> expand (int comp, T value)
00081       {
00082           FixedArray<T,N+1> x;
00083           for (int i=0; i<comp; i++) x[i] = a[i];
00084           x[comp] = value;
00085           for (int i=comp+1; i<N+1; i++) x[i] = a[i-1];
00086           return x;
00087       }
00088       
00089   protected:
00090       T a[n];
00091   };
00092     
00094     template <class T, int N>
00095     inline std::ostream& operator<< (std::ostream& s, FixedArray<T,N> e)
00096     {
00097         s << "[";
00098         for (int i=0; i<N-1; i++) s << e[i] << ",";
00099         s << e[N-1] << "]";
00100         return s;
00101     }
00102     
00105 } // end namespace Dune
00106 
00107 #endif

Generated on 12 Dec 2007 with Doxygen (ver 1.5.1)