BEM++  2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
_4d_array.hpp
1 // Copyright (C) 2011-2012 by the Bem++ Authors
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20 
21 #ifndef fiber_4d_array_hpp
22 #define fiber_4d_array_hpp
23 
24 #include "../common/common.hpp"
25 #include "boost/operators.hpp"
26 
27 #include <stdexcept>
28 
29 #ifndef NDEBUG
30 #define FIBER_CHECK_ARRAY_BOUNDS
31 #endif
32 
33 namespace Fiber
34 {
35 
37 template <typename T> class _4dSliceOf4dArray;
38 template <typename T> class _2dSliceOf4dArray;
39 template <typename T> class _1dSliceOf4dArray;
40 template <typename T> class Const4dSliceOf4dArray;
41 template <typename T> class Const2dSliceOf4dArray;
42 template <typename T> class Const1dSliceOf4dArray;
49 template <typename T>
50 class _4dArray : boost::additive<_4dArray<T>
51  , boost::multiplicative<_4dArray<T>,T > >
52 {
53 public:
54  _4dArray();
55  _4dArray(size_t extent0, size_t extent1, size_t extent2, size_t extent3);
56  _4dArray(size_t extent0, size_t extent1, size_t extent2, size_t extent3, T* data);
57  _4dArray(const _4dArray& rhs);
58  _4dArray& operator=(const _4dArray& rhs);
59 
60  ~_4dArray();
61 
62  T& operator()(size_t index0, size_t index1, size_t index2, size_t index3);
63  const T& operator()(size_t index0, size_t index1, size_t index2, size_t index3) const;
64 
65  size_t extent(size_t dimension) const;
66  void set_size(size_t extent0, size_t extent1, size_t extent2, size_t extent3);
67 
68  _4dArray<T>& operator+=(const _4dArray<T>& other);
69  _4dArray<T>& operator*=(const T& other);
70 
71 
72  typedef T* iterator;
73  typedef const T* const_iterator;
74 
75  iterator begin();
76  const_iterator begin() const;
77  iterator end();
78  const_iterator end() const;
79 
80 private:
81  void init_memory(size_t extent0, size_t extent1,
82  size_t extent2, size_t extent3);
83  void free_memory();
84 
85 #ifdef FIBER_CHECK_ARRAY_BOUNDS
86  void check_dimension(size_t dimension) const;
87  void check_extents(size_t extent0, size_t extent1, size_t extent2, size_t extent3) const;
88  void check_indices(size_t index0, size_t index1, size_t index2, size_t index3) const;
89 #endif
90 
91 private:
92  size_t m_extents[4];
93  bool m_owns;
94  T* m_storage;
95 };
96 
98 template <typename T>
100 {
101 public:
102  _3dSliceOf4dArray(_4dArray<T>& array, size_t index3);
103 
111  _3dSliceOf4dArray& self();
112 
113  const T& operator()(size_t index0, size_t index1, size_t index2) const;
114  T& operator()(size_t index0, size_t index1, size_t index2);
115 
116  size_t extent(size_t dimension) const;
117 
118 private:
119  void check_dimension(size_t dimension) const;
120 
121 private:
122  _4dArray<T>& m_array;
123  size_t m_index3;
124 };
125 
127 template <typename T>
129 {
130 public:
131  _3dSliceOfConst4dArray(const _4dArray<T>& array, size_t index3);
132 
133  const T& operator()(size_t index0, size_t index1, size_t index2) const;
134 
135  size_t extent(size_t dimension) const;
136 
137 private:
138  void check_dimension(size_t dimension) const;
139 
140 private:
141  const _4dArray<T>& m_array;
142  size_t m_index3;
143 };
144 
146 template <typename T>
148 {
149 public:
150  _2dSliceOf4dArray(_4dArray<T>& array, size_t index2, size_t index3);
151 
159  _2dSliceOf4dArray& self();
160 
161  const T& operator()(size_t index0, size_t index1) const;
162  T& operator()(size_t index0, size_t index1);
163 
164  size_t extent(size_t dimension) const;
165 
166 private:
167  void check_dimension(size_t dimension) const;
168 
169 private:
170  _4dArray<T>& m_array;
171  size_t m_index2, m_index3;
172 };
173 
175 template <typename T>
177 {
178 public:
179  _2dSliceOfConst4dArray(const _4dArray<T>& array, size_t index2, size_t index3);
180 
181  const T& operator()(size_t index0, size_t index1) const;
182 
183  size_t extent(size_t dimension) const;
184 
185 private:
186  void check_dimension(size_t dimension) const;
187 
188 private:
189  const _4dArray<T>& m_array;
190  size_t m_index2, m_index3;
191 };
192 
194 template <typename T>
196 {
197 public:
199  _1dSliceOf4dArray(_4dArray<T>& array, size_t index1, size_t index2, size_t index3);
200 
208  _1dSliceOf4dArray& self();
209 
210  const T& operator()(size_t index0) const;
211  T& operator()(size_t index0);
212 
213  size_t extent(size_t dimension) const;
214 
215 private:
216  void check_dimension(size_t dimension) const;
217 
218 private:
219  _4dArray<T>& m_array;
220  size_t m_index1, m_index2, m_index3;
221 };
222 
224 template <typename T>
226 {
227 public:
228  _1dSliceOfConst4dArray(const _4dArray<T>& array, size_t index1, size_t index2, size_t index3);
229 
230  const T& operator()(size_t index0) const;
231 
232  size_t extent(size_t dimension) const;
233 
234 private:
235  void check_dimension(size_t dimension) const;
236 
237 private:
238  const _4dArray<T>& m_array;
239  size_t m_index1, m_index2, m_index3;
240 };
241 
242 } // namespace Fiber
243 
244 #include "_4d_array_imp.hpp"
245 
246 #endif
Simple implementation of a 4D Fortran-ordered array.
Definition: _4d_array.hpp:50
_1dSliceOf4dArray(_4dArray< T > &array, size_t index1, size_t index2, size_t index3)
Construct a slice consisting of the elements array(:,index1,index2,index3)
Definition: _4d_array_imp.hpp:373
Lightweight encapsulation of a 2D slice of a constant 4d array.
Definition: _4d_array.hpp:176
Lightweight encapsulation of a 2D slice of a constant 4d array.
Definition: _4d_array.hpp:128
Lightweight encapsulation of a 2D slice of a constant 4d array.
Definition: _4d_array.hpp:225
Lightweight encapsulation of a 3D slice of a 4D array.
Definition: _4d_array.hpp:99
Lightweight encapsulation of a 2D slice of a 4d array.
Definition: _4d_array.hpp:147
Lightweight encapsulation of a 1D slice of a 4d array.
Definition: _4d_array.hpp:195