dune-common  2.3.1
array.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 
4 #ifndef DUNE_ARRAY_HH
5 #define DUNE_ARRAY_HH
6 
11 #include <iostream>
12 #include <iomanip>
13 #include <string>
14 
15 // Include system implementation of array class if present
16 #ifdef HAVE_ARRAY
17 #include <array>
18 #else
19 #include <algorithm>
20 #endif
21 
22 #include "deprecated.hh"
23 
24 namespace Dune
25 {
31 #ifdef HAVE_ARRAY
32  using std::array;
33 #else
34 
39  template<class T, size_t N>
40  class array {
41  public:
42 
44  typedef T value_type;
45 
48 
50  typedef const value_type& const_reference;
51 
53  typedef value_type* iterator;
54 
56  typedef const value_type* const_iterator;
57 
59  typedef std::size_t size_type;
60 
62  typedef std::ptrdiff_t difference_type;
63 
65  typedef std::reverse_iterator<iterator> reverse_iterator;
66 
68  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
69 
71  size_type size() const {return N;}
72 
74  array<T,N>& operator= (const T& t)
75  {
76  fill(t);
77  return (*this);
78  }
79 
81  void assign(const T& t) DUNE_DEPRECATED
82  {
83  fill(t);
84  }
85 
87  void fill(const T& t)
88  {
89  for (size_type i=0; i<N; i++) a[i]=t;
90  }
91 
94  {
95  return a[i];
96  }
97 
100  {
101  return a[i];
102  }
103 
105  {
106  return a;
107  }
108 
110  {
111  return a;
112  }
113 
115  {
116  return a + N;
117  }
118 
120  {
121  return a + N;
122  }
123 
124  T a[(N > 0) ? N : 1];
125  };
126 
127 
128 
129  // Comparison Operators (see [lib.container.requirements])
130  // -------------------------------------------------------
131 
132  template< class T, size_t N >
133  inline bool operator< ( const array< T, N > &a, const array< T, N > &b )
134  {
135  return std::lexicographical_compare( a.begin(), a.end(), b.begin(), b.end() );
136  }
137 
138  template< class T, size_t N >
139  inline bool operator> ( const array< T, N > &a, const array< T, N > &b )
140  {
141  return b < a;
142  }
143 
144  template< class T, size_t N >
145  inline bool operator<= ( const array< T, N > &a, const array< T, N > &b )
146  {
147  return !(a > b);
148  }
149 
150  template< class T, size_t N >
151  inline bool operator>= ( const array< T, N > &a, const array< T, N > &b )
152  {
153  return !(a < b);
154  }
155 #endif
156 
158  template < class T, size_t N >
159  inline std::ostream& operator<< (std::ostream& s, const array<T,N>& e)
160  {
161  if (N == 0)
162  {
163  s << "[]";
164  return s;
165  }
166 
167  s << "[";
168  for (size_t i=0; i<N-1; i++) s << e[i] << ",";
169  s << e[N-1] << "]";
170  return s;
171  }
172 
173 #ifndef DOXYGEN
174  template<class T>
175  array<T, 1> make_array(const T &t0) {
176  array<T, 1> result = { {t0} };
177  return result;
178  }
179 
180  template<class T>
181  array<T, 2> make_array(const T &t0, const T &t1) {
182  array<T, 2> result = { {t0, t1} };
183  return result;
184  }
185 
186  template<class T>
187  array<T, 3> make_array(const T &t0, const T &t1, const T &t2) {
188  array<T, 3> result = { {t0, t1, t2} };
189  return result;
190  }
191 
192  template<class T>
193  array<T, 4> make_array(const T &t0, const T &t1, const T &t2, const T &t3) {
194  array<T, 4> result = { {t0, t1, t2, t3} };
195  return result;
196  }
197 
198  template<class T>
199  array<T, 5> make_array(const T &t0, const T &t1, const T &t2, const T &t3,
200  const T &t4)
201  {
202  array<T, 5> result = { {t0, t1, t2, t3, t4} };
203  return result;
204  }
205 
206  template<class T>
207  array<T, 6> make_array(const T &t0, const T &t1, const T &t2, const T &t3,
208  const T &t4, const T &t5)
209  {
210  array<T, 6> result = { {t0, t1, t2, t3, t4, t5} };
211  return result;
212  }
213 
214  template<class T>
215  array<T, 7> make_array(const T &t0, const T &t1, const T &t2, const T &t3,
216  const T &t4, const T &t5, const T &t6)
217  {
218  array<T, 7> result = { {t0, t1, t2, t3, t4, t5, t6} };
219  return result;
220  }
221 
222  template<class T>
223  array<T, 8> make_array(const T &t0, const T &t1, const T &t2, const T &t3,
224  const T &t4, const T &t5, const T &t6, const T &t7)
225  {
226  array<T, 8> result = { {t0, t1, t2, t3, t4, t5, t6, t7} };
227  return result;
228  }
229 
230  template<class T>
231  array<T, 9> make_array(const T &t0, const T &t1, const T &t2, const T &t3,
232  const T &t4, const T &t5, const T &t6, const T &t7,
233  const T &t8)
234  {
235  array<T, 9> result = { {t0, t1, t2, t3, t4, t5, t6, t7, t8} };
236  return result;
237  }
238 #endif // !DOXYGEN
239 
241 
247  template<class T>
248  array<T, 10> make_array(const T &t0, const T &t1, const T &t2, const T &t3,
249  const T &t4, const T &t5, const T &t6, const T &t7,
250  const T &t8, const T &t9)
251  {
252  array<T, 10> result = { t0, t1, t2, t3, t4, t5, t6, t7, t8, t9 };
253  return result;
254  }
255 
257 
260  template<typename T, std::size_t n>
262  {
263  array<T,n> r;
264  r.fill(t);
265 #if HAVE_RVALUE_REFERENCES
266  return std::move(r);
267 #else
268  return r;
269 #endif
270  }
271 
274 } // end namespace Dune
275 
276 #endif
value_type & reference
Reference to an object.
Definition: array.hh:47
T a[(N > 0)?N:1]
Definition: array.hh:124
#define DUNE_DEPRECATED
Mark some entity as deprecated.
Definition: deprecated.hh:84
array< T, 10 > make_array(const T &t0, const T &t1, const T &t2, const T &t3, const T &t4, const T &t5, const T &t6, const T &t7, const T &t8, const T &t9)
create an initialize an array
Definition: array.hh:248
bool operator>(const array< T, N > &a, const array< T, N > &b)
Definition: array.hh:139
const_iterator begin() const
Definition: array.hh:109
const value_type & const_reference
Const reference to an object.
Definition: array.hh:50
const_iterator end() const
Definition: array.hh:119
T t
Definition: alignment.hh:38
iterator begin()
Definition: array.hh:104
void fill(const T &t)
Assign value to all entries (according to C++0x the fill method is to be prefered) ...
Definition: array.hh:87
Simple fixed size array class. This replaces std::array, if that is not available.
Definition: array.hh:40
Definition of the DUNE_DEPRECATED macro for the case that config.h is not available.
reference operator[](size_type i)
Component access.
Definition: array.hh:93
iterator end()
Definition: array.hh:114
T value_type
Remember the storage type.
Definition: array.hh:44
std::reverse_iterator< iterator > reverse_iterator
Reverse iterator type.
Definition: array.hh:65
const value_type * const_iterator
Const iterator type.
Definition: array.hh:56
std::ptrdiff_t difference_type
Difference type.
Definition: array.hh:62
size_type size() const
Return array size.
Definition: array.hh:71
std::size_t size_type
Type used for array indices.
Definition: array.hh:59
value_type * iterator
Iterator type.
Definition: array.hh:53
bool operator>=(const array< T, N > &a, const array< T, N > &b)
Definition: array.hh:151
array< T, N > & operator=(const T &t)
Assign value to all entries.
Definition: array.hh:74
void assign(const T &t)
Assign value to all entries (according to C++0x the fill method is to be prefered) ...
Definition: array.hh:81
std::reverse_iterator< const_iterator > const_reverse_iterator
Const reverse iterator type.
Definition: array.hh:68
array< T, n > fill_array(const T &t)
Create an array and fill it with copies of the provided value.
Definition: array.hh:261