00001 #ifndef DUNE_FIXEDARRAY_HH
00002 #define DUNE_FIXEDARRAY_HH
00003
00009 #include<iostream>
00010 #include<iomanip>
00011 #include<string>
00012
00013 #include <dune/common/deprecated.hh>
00014
00015
00016 #ifdef HAVE_ARRAY
00017 #include <array>
00018 #endif
00019 #ifdef HAVE_TR1_ARRAY
00020 #include <tr1/array>
00021 #endif
00022
00023
00024 namespace Dune
00025 {
00031 #ifdef HAVE_ARRAY
00032 using std::array;
00033 #elif defined HAVE_TR1_ARRAY
00034 using std::tr1::array;
00035 #else
00036
00040 template<class T, int N>
00041 class array {
00042 public:
00043
00045 typedef T value_type;
00046
00048 typedef value_type& reference;
00049
00051 typedef const value_type& const_reference;
00052
00054 typedef value_type* iterator;
00055
00057 typedef const value_type* const_iterator;
00058
00060 typedef std::size_t size_type;
00061
00063 typedef std::ptrdiff_t difference_type;
00064
00066 typedef std::reverse_iterator<iterator> reverse_iterator;
00067
00069 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
00070
00072 array () {}
00073
00077 array (const T& t) DUNE_DEPRECATED
00078 {
00079 for (int i=0; i<N; i++) a[i]=t;
00080 }
00081
00083 size_type size() const {return N;}
00084
00086 array<T,N>& operator= (const T& t)
00087 {
00088 for (int i=0; i<N; i++) a[i]=t;
00089 return (*this);
00090 }
00091
00093 void assign(const T& t)
00094 {
00095 for (int i=0; i<N; i++) a[i]=t;
00096 }
00097
00099 reference operator[] (size_type i)
00100 {
00101 return a[i];
00102 }
00103
00105 const_reference operator[] (size_type i) const
00106 {
00107 return a[i];
00108 }
00109
00110 protected:
00111 T a[(N > 0) ? N : 1];
00112 };
00113 #endif
00115 template <class T, int N>
00116 inline std::ostream& operator<< (std::ostream& s, array<T,N> e)
00117 {
00118 s << "[";
00119 for (int i=0; i<N-1; i++) s << e[i] << ",";
00120 s << e[N-1] << "]";
00121 return s;
00122 }
00123
00127 template<class T, int N>
00128 class FixedArray {
00129 public:
00130
00132 typedef T MemberType;
00133
00136 enum { n = (N > 0) ? N : 1 };
00137
00139 enum { dimension = N };
00140
00142 FixedArray () {}
00143
00145 FixedArray (T t) DUNE_DEPRECATED
00146 {
00147 for (int i=0; i<N; i++) a[i]=t;
00148 }
00149
00151 int size() const {return N;}
00152
00156 FixedArray<T,N>& operator= (const T& t) DUNE_DEPRECATED
00157 {
00158 assign(t);
00159 return (*this);
00160 }
00161
00163 void assign(const T& t)
00164 {
00165 for (int i=0; i<N; i++) a[i]=t;
00166 }
00167
00169 T& operator[] (int i)
00170 {
00171 return a[i];
00172 }
00173
00175 const T& operator[] (int i) const
00176 {
00177 return a[i];
00178 }
00179
00181 FixedArray<T,N-1> shrink (int comp)
00182 {
00183 FixedArray<T,N-1> x;
00184 for (int i=0; i<comp; i++) x[i] = a[i];
00185 for (int i=comp+1; i<N; i++) x[i-1] = a[i];
00186 return x;
00187 }
00188
00190 FixedArray<T,N+1> expand (int comp, T value)
00191 {
00192 FixedArray<T,N+1> x;
00193 for (int i=0; i<comp; i++) x[i] = a[i];
00194 x[comp] = value;
00195 for (int i=comp+1; i<N+1; i++) x[i] = a[i-1];
00196 return x;
00197 }
00198
00199 protected:
00200 T a[n];
00201 } DUNE_DEPRECATED;
00202
00204 template <class T, int N>
00205 inline std::ostream& operator<< (std::ostream& s, FixedArray<T,N> e)
00206 {
00207 s << "[";
00208 for (int i=0; i<N-1; i++) s << e[i] << ",";
00209 s << e[N-1] << "]";
00210 return s;
00211 }
00212
00215 }
00216
00217 #endif