BEM++  2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
_3d_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_3d_array_hpp
22 #define fiber_3d_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 _2dSliceOf3dArray;
38 template <typename T> class _1dSliceOf3dArray;
39 template <typename T> class Const2dSliceOf3dArray;
40 template <typename T> class Const1dSliceOf3dArray;
47 template <typename T>
48 class _3dArray : boost::additive<_3dArray<T>
49  , boost::multiplicative<_3dArray<T>,T > >
50 {
51 public:
52  _3dArray();
53  _3dArray(size_t extent0, size_t extent1, size_t extent2);
54  _3dArray(size_t extent0, size_t extent1, size_t extent2, T* data,
55  bool strict = false);
56  _3dArray(const _3dArray& other);
57  _3dArray& operator=(const _3dArray& rhs);
58 
59  ~_3dArray();
60 
61  T& operator()(size_t index0, size_t index1, size_t index2);
62  const T& operator()(size_t index0, size_t index1, size_t index2) const;
63 
64  _3dArray<T>& operator+=(const _3dArray<T>& other);
65  _3dArray<T>& operator*=(const T& other);
66 
67 
68 // so far unused, to be implemented later
69 // _2dSliceOf3dArray slice(size_t index2);
70 // Const2dSliceOf3dArray const_slice(size_t index2) const;
71 
72 // _1dSliceOf3dArray slice(size_t index1, size_t index2);
73 // Const1dSliceOf3dArray const_slice(size_t index2) const;
74 
75  size_t extent(size_t dimension) const;
76  void set_size(size_t extent0, size_t extent1, size_t extent2);
77  bool is_empty() const;
78 
79  typedef T* iterator;
80  typedef const T* const_iterator;
81 
82  iterator begin();
83  const_iterator begin() const;
84  iterator end();
85  const_iterator end() const;
86 
87 private:
88  void init_memory(size_t extent0, size_t extent1, size_t extent2);
89  void init_empty();
90  void free_memory();
91 
92 #ifdef FIBER_CHECK_ARRAY_BOUNDS
93  void check_dimension(size_t dimension) const;
94  void check_extents(size_t extent0, size_t extent1, size_t extent2) const;
95  void check_indices(size_t index0, size_t index1, size_t index2) const;
96 #endif
97 
98 private:
99  size_t m_extents[3];
100  bool m_owns;
101  bool m_strict;
102  T* m_storage;
103 };
104 
106 template <typename T>
108 {
109 public:
110  _2dSliceOf3dArray(_3dArray<T>& array, size_t index2);
111 
119  _2dSliceOf3dArray& self();
120 
121  const T& operator()(size_t index0, size_t index1) const;
122  T& operator()(size_t index0, size_t index1);
123 
124  size_t extent(size_t dimension) const;
125 
126 private:
127  void check_dimension(size_t dimension) const;
128 
129 private:
130  _3dArray<T>& m_array;
131  size_t m_index2;
132 };
133 
135 template <typename T>
137 {
138 public:
139  _2dSliceOfConst3dArray(const _3dArray<T>& array, size_t index2);
140 
141  const T& operator()(size_t index0, size_t index1) const;
142 
143  size_t extent(size_t dimension) const;
144 
145 private:
146  void check_dimension(size_t dimension) const;
147 
148 private:
149  const _3dArray<T>& m_array;
150  size_t m_index2;
151 };
152 
154 template <typename T>
156 {
157 public:
159  _1dSliceOf3dArray(_3dArray<T>& array, size_t index1, size_t index2);
160 
168  _1dSliceOf3dArray& self();
169 
170  const T& operator()(size_t index0) const;
171  T& operator()(size_t index0);
172 
173  size_t extent(size_t dimension) const;
174 
175 private:
176  void check_dimension(size_t dimension) const;
177 
178 private:
179  _3dArray<T>& m_array;
180  size_t m_index1, m_index2;
181 };
182 
184 template <typename T>
186 {
187 public:
188  _1dSliceOfConst3dArray(const _3dArray<T>& array, size_t index1, size_t index2);
189 
190  const T& operator()(size_t index0) const;
191 
192  size_t extent(size_t dimension) const;
193 
194 private:
195  void check_dimension(size_t dimension) const;
196 
197 private:
198  const _3dArray<T>& m_array;
199  size_t m_index1, m_index2;
200 };
201 
202 } // namespace Fiber
203 
204 #include "_3d_array_imp.hpp"
205 
206 #endif
_1dSliceOf3dArray(_3dArray< T > &array, size_t index1, size_t index2)
Construct a slice consisting of the elements array(:,index1,index2)
Definition: _3d_array_imp.hpp:316
Lightweight encapsulation of a 2D slice of a constant 3D array.
Definition: _3d_array.hpp:136
Lightweight encapsulation of a 1D slice of a constant 3D array.
Definition: _3d_array.hpp:185
Lightweight encapsulation of a 2D slice of a 3D array.
Definition: _3d_array.hpp:107
Lightweight encapsulation of a 1D slice of a 3D array.
Definition: _3d_array.hpp:155
Simple implementation of a 3D Fortran-ordered array.
Definition: _3d_array.hpp:48