BEM++  2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
collection_of_3d_arrays_imp.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 #include "collection_of_3d_arrays.hpp"
22 
23 namespace Fiber
24 {
25 
26 // CollectionOf3dArrays
27 
28 template <typename T>
29 inline CollectionOf3dArrays<T>::CollectionOf3dArrays() :
30  m_size(0)
31 {
32 }
33 
34 template <typename T>
35 inline CollectionOf3dArrays<T>::CollectionOf3dArrays(size_t size) :
36  m_size(size), m_arrays(new _3dArray<T>[size])
37 {
38  // should we initialise to 0?
39 }
40 
41 template <typename T>
42 inline void CollectionOf3dArrays<T>::set_size(size_t new_size)
43 {
44  if (new_size == m_size)
45  return;
46  m_arrays.reset(new_size == 0 ? 0 : new _3dArray<T>[new_size]);
47  m_size = new_size;
48 }
49 
50 template <typename T>
51 inline size_t CollectionOf3dArrays<T>::size() const
52 {
53  return m_size;
54 }
55 
56 template <typename T>
57 inline void CollectionOf3dArrays<T>::fill(const T& value)
58 {
59  for (size_t a = 0; a < m_size; ++a)
60  std::fill((*this)[a].begin(), (*this)[a].end(), value);
61 }
62 
63 template <typename T>
64 inline _3dArray<T>& CollectionOf3dArrays<T>::operator[](size_t index)
65 {
66  return array(index);
67 }
68 
69 template <typename T>
70 inline const _3dArray<T>& CollectionOf3dArrays<T>::operator[](size_t index) const
71 {
72  return array(index);
73 }
74 
75 template <typename T>
76 inline CollectionOf2dSlicesOf3dArrays<T> CollectionOf3dArrays<T>::slice(size_t index2)
77 {
78  return CollectionOf2dSlicesOf3dArrays<T>(*this, index2);
79 }
80 
81 template <typename T>
82 inline CollectionOf2dSlicesOfConst3dArrays<T> CollectionOf3dArrays<T>::const_slice(
83  size_t index2) const
84 {
85  return CollectionOf2dSlicesOfConst3dArrays<T>(*this, index2);
86 }
87 
88 template <typename T>
89 inline CollectionOf1dSlicesOf3dArrays<T> CollectionOf3dArrays<T>::slice(size_t index1, size_t index2)
90 {
91  return CollectionOf1dSlicesOf3dArrays<T>(*this, index1, index2);
92 }
93 
94 template <typename T>
95 inline CollectionOf1dSlicesOfConst3dArrays<T> CollectionOf3dArrays<T>::const_slice(
96  size_t index1, size_t index2) const
97 {
98  return CollectionOf1dSlicesOfConst3dArrays<T>(*this, index1, index2);
99 }
100 
101 template <typename T>
102 inline _3dArray<T>& CollectionOf3dArrays<T>::array(size_t i)
103 {
104 #ifdef FIBER_CHECK_ARRAY_BOUNDS
105  check_array_index(i);
106 #endif
107  return m_arrays[i];
108 }
109 
110 template <typename T>
111 inline const _3dArray<T>& CollectionOf3dArrays<T>::array(size_t i) const
112 {
113 #ifdef FIBER_CHECK_ARRAY_BOUNDS
114  check_array_index(i);
115 #endif
116  return m_arrays[i];
117 }
118 
119 template <typename T>
120 inline void CollectionOf3dArrays<T>::check_array_index(size_t array_index) const
121 {
122 #ifdef FIBER_CHECK_ARRAY_BOUNDS
123  if (m_size <= array_index)
124  throw std::invalid_argument("Invalid array index");
125 #endif
126 }
127 
128 // CollectionOf2dSlicesOf3dArrays
129 
130 template <typename T>
131 inline CollectionOf2dSlicesOf3dArrays<T>::
132 CollectionOf2dSlicesOf3dArrays(CollectionOf3dArrays<T>& collection, size_t index2) :
133  m_collection(collection), m_index2(index2)
134 {}
135 
136 template <typename T>
137 inline CollectionOf2dSlicesOf3dArrays<T>& CollectionOf2dSlicesOf3dArrays<T>::
138 self() {
139  return *this;
140 }
141 
142 template <typename T>
144 operator[](size_t index) const {
145  return _2dSliceOfConst3dArray<T>(m_collection[index], m_index2);
146 }
147 
148 template <typename T>
149 inline _2dSliceOf3dArray<T> CollectionOf2dSlicesOf3dArrays<T>::
150 operator[](size_t index) {
151  return _2dSliceOf3dArray<T>(m_collection[index], m_index2);
152 }
153 
154 template <typename T>
155 inline size_t CollectionOf2dSlicesOf3dArrays<T>::size() const {
156  return m_collection.size();
157 }
158 
159 // CollectionOf2dSlicesOfConst3dArrays
160 
161 template <typename T>
162 inline CollectionOf2dSlicesOfConst3dArrays<T>::
163 CollectionOf2dSlicesOfConst3dArrays(const CollectionOf3dArrays<T>& collection,
164  size_t index2) :
165  m_collection(collection), m_index2(index2)
166 {}
167 
168 template <typename T>
169 inline _2dSliceOfConst3dArray<T> CollectionOf2dSlicesOfConst3dArrays<T>::
170 operator[](size_t index) const {
171  return _2dSliceOfConst3dArray<T>(m_collection[index], m_index2);
172 }
173 
174 template <typename T>
175 inline size_t CollectionOf2dSlicesOfConst3dArrays<T>::size() const {
176  return m_collection.size();
177 }
178 
179 // CollectionOf1dSlicesOf3dArrays
180 
181 template <typename T>
182 inline CollectionOf1dSlicesOf3dArrays<T>::
183 CollectionOf1dSlicesOf3dArrays(CollectionOf3dArrays<T>& collection,
184  size_t index1, size_t index2) :
185  m_collection(collection), m_index1(index1), m_index2(index2)
186 {}
187 
188 template <typename T>
189 inline CollectionOf1dSlicesOf3dArrays<T>& CollectionOf1dSlicesOf3dArrays<T>::
190 self() {
191  return *this;
192 }
193 
194 template <typename T>
196 operator[](size_t index) const {
197  return _1dSliceOfConst3dArray<T>(m_collection[index], m_index1, m_index2);
198 }
199 
200 template <typename T>
201 inline _1dSliceOf3dArray<T> CollectionOf1dSlicesOf3dArrays<T>::
202 operator[](size_t index) {
203  return _1dSliceOf3dArray<T>(m_collection[index], m_index1, m_index2);
204 }
205 
206 template <typename T>
207 inline size_t CollectionOf1dSlicesOf3dArrays<T>::size() const {
208  return m_collection.size();
209 }
210 
211 // CollectionOf1dSlicesOfConst3dArrays
212 
213 template <typename T>
214 inline CollectionOf1dSlicesOfConst3dArrays<T>::
215 CollectionOf1dSlicesOfConst3dArrays(const CollectionOf3dArrays<T>& collection,
216  size_t index1, size_t index2) :
217  m_collection(collection), m_index1(index1), m_index2(index2)
218 {}
219 
220 template <typename T>
221 inline _1dSliceOfConst3dArray<T> CollectionOf1dSlicesOfConst3dArrays<T>::
222 operator[](size_t index) const {
223  return _1dSliceOfConst3dArray<T>(m_collection[index], m_index1, m_index2);
224 }
225 
226 template <typename T>
227 inline size_t CollectionOf1dSlicesOfConst3dArrays<T>::size() const {
228  return m_collection.size();
229 }
230 
231 } // namespace Fiber
CollectionOf2dSlicesOf3dArrays & self()
Returns a reference to self.
Definition: collection_of_3d_arrays_imp.hpp:138
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
CollectionOf1dSlicesOf3dArrays & self()
Returns a reference to self.
Definition: collection_of_3d_arrays_imp.hpp:190
Definition: collection_of_3d_arrays.hpp:121
Definition: collection_of_3d_arrays.hpp:74