array.hh
Go to the documentation of this file.00001 #ifndef DUNE_ARRAY_HH
00002 #define DUNE_ARRAY_HH
00003
00008 #include<iostream>
00009 #include<iomanip>
00010 #include<string>
00011
00012
00013 #ifdef HAVE_ARRAY
00014 #include <array>
00015 #else
00016 #include <algorithm>
00017 #endif
00018
00019
00020 namespace Dune
00021 {
00027 #ifdef HAVE_ARRAY
00028 using std::array;
00029 #else
00030
00035 template<class T, int N>
00036 class array {
00037 public:
00038
00040 typedef T value_type;
00041
00043 typedef value_type& reference;
00044
00046 typedef const value_type& const_reference;
00047
00049 typedef value_type* iterator;
00050
00052 typedef const value_type* const_iterator;
00053
00055 typedef std::size_t size_type;
00056
00058 typedef std::ptrdiff_t difference_type;
00059
00061 typedef std::reverse_iterator<iterator> reverse_iterator;
00062
00064 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
00065
00067 array () {}
00068
00070 size_type size() const {return N;}
00071
00073 array<T,N>& operator= (const T& t)
00074 {
00075 for (int i=0; i<N; i++) a[i]=t;
00076 return (*this);
00077 }
00078
00080 void assign(const T& t)
00081 {
00082 for (int i=0; i<N; i++) a[i]=t;
00083 }
00084
00086 reference operator[] (size_type i)
00087 {
00088 return a[i];
00089 }
00090
00092 const_reference operator[] (size_type i) const
00093 {
00094 return a[i];
00095 }
00096
00097 iterator begin ()
00098 {
00099 return a;
00100 }
00101
00102 const_iterator begin () const
00103 {
00104 return a;
00105 }
00106
00107 iterator end ()
00108 {
00109 return a + N;
00110 }
00111
00112 const_iterator end () const
00113 {
00114 return a + N;
00115 }
00116
00117 protected:
00118 T a[(N > 0) ? N : 1];
00119 };
00120
00121
00122
00123
00124
00125
00126 template< class T, int N >
00127 inline bool operator< ( const array< T, N > &a, const array< T, N > &b )
00128 {
00129 return std::lexicographical_compare( a.begin(), a.end(), b.begin(), b.end() );
00130 }
00131
00132 template< class T, int N >
00133 inline bool operator> ( const array< T, N > &a, const array< T, N > &b )
00134 {
00135 return b < a;
00136 }
00137
00138 template< class T, int N >
00139 inline bool operator<= ( const array< T, N > &a, const array< T, N > &b )
00140 {
00141 return !(a > b);
00142 }
00143
00144 template< class T, int N >
00145 inline bool operator>= ( const array< T, N > &a, const array< T, N > &b )
00146 {
00147 return !(a < b);
00148 }
00149 #endif
00150
00151 template <class T, int N>
00152 inline std::ostream& operator<< (std::ostream& s, array<T,N> e)
00153 {
00154 s << "[";
00155 for (int i=0; i<N-1; i++) s << e[i] << ",";
00156 s << e[N-1] << "]";
00157 return s;
00158 }
00159
00162 }
00163
00164 #endif