Interface Documentation
Version: invalid
dimensioned_array.hh
Go to the documentation of this file.
1 /*
2  @@@@@@@@ @@ @@@@@@ @@@@@@@@ @@
3  /@@///// /@@ @@////@@ @@////// /@@
4  /@@ /@@ @@@@@ @@ // /@@ /@@
5  /@@@@@@@ /@@ @@///@@/@@ /@@@@@@@@@/@@
6  /@@//// /@@/@@@@@@@/@@ ////////@@/@@
7  /@@ /@@/@@//// //@@ @@ /@@/@@
8  /@@ @@@//@@@@@@ //@@@@@@ @@@@@@@@ /@@
9  // /// ////// ////// //////// //
10 
11  Copyright (c) 2016, Los Alamos National Security, LLC
12  All rights reserved.
13  */
14 #pragma once
15 
18 #include <array>
19 #include <cmath>
20 #include <ostream>
21 
22 #include "flecsi/util/common.hh"
23 
24 namespace flecsi {
25 namespace util {
26 
27 template<typename... CONDITIONS>
28 struct and_ : std::true_type {};
29 
30 template<typename CONDITION, typename... CONDITIONS>
31 struct and_<CONDITION, CONDITIONS...>
32  : std::conditional<CONDITION::value, and_<CONDITIONS...>, std::false_type>::
33  type {}; // struct and_
34 
35 template<typename TARGET, typename... TARGETS>
37 
38 //----------------------------------------------------------------------------//
40 //----------------------------------------------------------------------------//
41 
42 enum class axis : size_t { x = 0, y = 1, z = 2 };
43 
44 //----------------------------------------------------------------------------//
55 //----------------------------------------------------------------------------//
56 
57 template<typename TYPE, size_t DIMENSION, size_t NAMESPACE>
59 {
60 public:
62  dimensioned_array() = default;
63 
65  dimensioned_array(dimensioned_array const &) = default;
66 
67  //--------------------------------------------------------------------------//
69  //--------------------------------------------------------------------------//
70 
71  constexpr dimensioned_array(std::initializer_list<TYPE> list) : data_() {
72  assert(list.size() == DIMENSION && "dimension size mismatch");
73  auto p = list.begin();
74  for(auto & x : data_)
75  x = *p++; // std::copy isn't constexpr until C++20
76  } // dimensioned_array
77 
78  //--------------------------------------------------------------------------//
80  //--------------------------------------------------------------------------//
81 
82  template<typename... ARGS,
83  typename = typename std::enable_if<sizeof...(ARGS) == DIMENSION &&
85  constexpr dimensioned_array(ARGS... args)
86  : data_{args...} {} // dimensioned_array
87 
88  //--------------------------------------------------------------------------//
90  //--------------------------------------------------------------------------//
91 
92  constexpr dimensioned_array(TYPE const & val) : data_() {
93  for(auto & x : data_)
94  x = val; // array::fill isn't constexpr until C++20
95  } // dimensioned_array
96 
97  //--------------------------------------------------------------------------//
99  //--------------------------------------------------------------------------//
100 
101  static constexpr size_t size() {
102  return DIMENSION;
103  }; // size
104 
105  //--------------------------------------------------------------------------//
108  //--------------------------------------------------------------------------//
109 
110  template<typename ENUM_TYPE>
111  constexpr TYPE & operator[](ENUM_TYPE e) {
112  return data_[static_cast<size_t>(e)];
113  } // operator []
114 
115  //--------------------------------------------------------------------------//
118  //--------------------------------------------------------------------------//
119 
120  template<typename ENUM_TYPE>
121  constexpr TYPE const & operator[](ENUM_TYPE e) const {
122  return data_[static_cast<size_t>(e)];
123  } // operator []
124 
125  //--------------------------------------------------------------------------//
127  //--------------------------------------------------------------------------//
128 
129  constexpr dimensioned_array & operator=(dimensioned_array const & rhs) {
130  if(this != &rhs) {
131  data_ = rhs.data_;
132  } // if
133 
134  return *this;
135  } // operator =
136 
137  //--------------------------------------------------------------------------//
139  //--------------------------------------------------------------------------//
140 
141  constexpr dimensioned_array & operator=(const TYPE & val) {
142  for(size_t i = 0; i < DIMENSION; i++) {
143  data_[i] = val;
144  } // for
145 
146  return *this;
147  } // operator =
148 
149  //--------------------------------------------------------------------------//
150  // Macro to avoid code replication.
151  //--------------------------------------------------------------------------//
152 
153 #define define_operator(op) \
154  constexpr dimensioned_array & operator op(dimensioned_array const & rhs) { \
155  if(this != &rhs) { \
156  for(size_t i{0}; i < DIMENSION; i++) { \
157  data_[i] op rhs[i]; \
158  } /* for */ \
159  } /* if */ \
160  \
161  return *this; \
162  }
163 
164  //--------------------------------------------------------------------------//
165  // Macro to avoid code replication.
166  //--------------------------------------------------------------------------//
167 
168 #define define_operator_type(op) \
169  constexpr dimensioned_array & operator op(TYPE val) { \
170  for(size_t i{0}; i < DIMENSION; i++) { \
171  data_[i] op val; \
172  } /* for */ \
173  \
174  return *this; \
175  }
176 
177  //--------------------------------------------------------------------------//
179  //--------------------------------------------------------------------------//
180 
181  define_operator(+=);
182 
183  //--------------------------------------------------------------------------//
185  //--------------------------------------------------------------------------//
186 
187  define_operator_type(+=);
188 
189  //--------------------------------------------------------------------------//
191  //--------------------------------------------------------------------------//
192 
193  define_operator(-=);
194 
195  //--------------------------------------------------------------------------//
197  //--------------------------------------------------------------------------//
198 
199  define_operator_type(-=);
200 
201  //--------------------------------------------------------------------------//
203  //--------------------------------------------------------------------------//
204 
205  define_operator(*=);
206 
207  //--------------------------------------------------------------------------//
209  //--------------------------------------------------------------------------//
210 
211  define_operator_type(*=);
212 
213  //--------------------------------------------------------------------------//
215  //--------------------------------------------------------------------------//
216 
217  define_operator(/=);
218 
219  //--------------------------------------------------------------------------//
221  //--------------------------------------------------------------------------//
222 
223  define_operator_type(/=);
224 
225  //--------------------------------------------------------------------------//
227  //--------------------------------------------------------------------------//
228 
229  constexpr bool operator==(const dimensioned_array & da) {
230  return this->data_ == da.data_;
231  }
232 
236  constexpr dimensioned_array operator/(TYPE val) {
237  dimensioned_array tmp(*this);
238  tmp /= val;
239 
240  return tmp;
241  } // operator /
242 
243 private:
244  std::array<TYPE, DIMENSION> data_;
245 
246 }; // class dimensioned_array
247 
248 //----------------------------------------------------------------------------//
257 //----------------------------------------------------------------------------//
258 
259 template<typename TYPE, size_t DIMENSION, size_t NAMESPACE>
264  tmp += rhs;
265  return tmp;
266 } // operator +
267 
268 //----------------------------------------------------------------------------//
277 //----------------------------------------------------------------------------//
278 
279 template<typename TYPE, size_t DIMENSION, size_t NAMESPACE>
284  tmp -= rhs;
285  return tmp;
286 } // operator -
287 
288 //----------------------------------------------------------------------------//
300 //----------------------------------------------------------------------------//
301 
302 template<typename TYPE, size_t DIMENSION, size_t NAMESPACE>
303 std::ostream &
304 operator<<(std::ostream & stream,
306  stream << "[";
307 
308  for(size_t i = 0; i < DIMENSION; i++) {
309  stream << " " << a[i];
310  } // for
311 
312  stream << " ]";
313 
314  return stream;
315 } // operator <<
316 
317 } // namespace util
318 } // namespace flecsi
constexpr dimensioned_array(ARGS... args)
Variadic constructor.
Definition: dimensioned_array.hh:85
constexpr dimensioned_array & operator=(const TYPE &val)
Assignment operator.
Definition: dimensioned_array.hh:141
constexpr TYPE & operator[](ENUM_TYPE e)
Definition: dimensioned_array.hh:111
constexpr dimensioned_array< TYPE, DIMENSION, NAMESPACE > operator+(const dimensioned_array< TYPE, DIMENSION, NAMESPACE > &lhs, const dimensioned_array< TYPE, DIMENSION, NAMESPACE > &rhs)
Definition: dimensioned_array.hh:261
std::ostream & operator<<(std::ostream &ostr, const filling_curve< D, T, DER > &k)
output for filling_curve using output_ function defined in the class
Definition: filling_curve.hh:206
constexpr dimensioned_array operator/(TYPE val)
Division operator involving a constant.
Definition: dimensioned_array.hh:236
std::string type()
Definition: demangle.hh:44
axis
Enumeration for axes.
Definition: dimensioned_array.hh:42
constexpr dimensioned_array(std::initializer_list< TYPE > list)
Initializer list constructor.
Definition: dimensioned_array.hh:71
constexpr bool operator==(const dimensioned_array &da)
Equality operator.
Definition: dimensioned_array.hh:229
constexpr dimensioned_array(TYPE const &val)
Constructor (fill with given value).
Definition: dimensioned_array.hh:92
Definition: dimensioned_array.hh:58
constexpr dimensioned_array & operator=(dimensioned_array const &rhs)
Assignment operator.
Definition: dimensioned_array.hh:129
constexpr dimensioned_array< TYPE, DIMENSION, NAMESPACE > operator-(const dimensioned_array< TYPE, DIMENSION, NAMESPACE > &lhs, const dimensioned_array< TYPE, DIMENSION, NAMESPACE > &rhs)
Definition: dimensioned_array.hh:281
constexpr TYPE const & operator[](ENUM_TYPE e) const
Definition: dimensioned_array.hh:121
static constexpr size_t size()
Return the size of the array.
Definition: dimensioned_array.hh:101
Definition: dimensioned_array.hh:28
Definition: control.hh:31