BEM++  2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
collection_of_2d_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_2d_arrays.hpp"
22 
23 namespace Fiber
24 {
25 
26 // CollectionOf2dArrays
27 
28 template <typename T>
29 inline CollectionOf2dArrays<T>::CollectionOf2dArrays() :
30  m_size(0)
31 {
32 }
33 
34 template <typename T>
35 inline CollectionOf2dArrays<T>::CollectionOf2dArrays(size_t size) :
36  m_size(size), m_arrays(new _2dArray<T>[size])
37 {
38  // should we initialise to 0?
39 }
40 
41 
42 template <typename T>
43 inline CollectionOf2dArrays<T>::CollectionOf2dArrays(
44  const CollectionOf2dArrays& other) :
45  m_size(other.m_size), m_arrays(new _2dArray<T>[other.m_size])
46 {
47  for (size_t a = 0; a < m_size; ++a)
48  m_arrays[a] = other.array(a);
49 }
50 
51 template <typename T>
52 inline CollectionOf2dArrays<T>& CollectionOf2dArrays<T>::operator=(
53  const CollectionOf2dArrays& rhs)
54 {
55  if (&rhs != this) {
56  set_size(rhs.m_size);
57  for (size_t a = 0; a < m_size; ++a)
58  m_arrays[a] = rhs.array(a);
59  }
60  return *this;
61 }
62 
63 template <typename T>
64 inline void CollectionOf2dArrays<T>::set_size(size_t new_size)
65 {
66  if (new_size == m_size)
67  return;
68  m_arrays.reset(new_size == 0 ? 0 : new _2dArray<T>[new_size]);
69  m_size = new_size;
70 }
71 
72 template <typename T>
73 inline size_t CollectionOf2dArrays<T>::size() const
74 {
75  return m_size;
76 }
77 
78 template <typename T>
79 inline void CollectionOf2dArrays<T>::fill(const T& value)
80 {
81  for (size_t a = 0; a < m_size; ++a)
82  std::fill((*this)[a].begin(), (*this)[a].end(), value);
83 }
84 
85 template <typename T>
86 inline _2dArray<T>& CollectionOf2dArrays<T>::operator[](size_t index)
87 {
88  return array(index);
89 }
90 
91 template <typename T>
92 inline const _2dArray<T>& CollectionOf2dArrays<T>::operator[](size_t index) const
93 {
94  return array(index);
95 }
96 
97 template <typename T>
98 inline CollectionOf1dSlicesOf2dArrays<T> CollectionOf2dArrays<T>::slice(size_t index1)
99 {
100  return CollectionOf1dSlicesOf2dArrays<T>(*this, index1);
101 }
102 
103 template <typename T>
104 inline CollectionOf1dSlicesOfConst2dArrays<T> CollectionOf2dArrays<T>::const_slice(
105  size_t index1) const
106 {
107  return CollectionOf1dSlicesOfConst2dArrays<T>(*this, index1);
108 }
109 
110 template <typename T>
111 inline _2dArray<T>& CollectionOf2dArrays<T>::array(size_t i)
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 const _2dArray<T>& CollectionOf2dArrays<T>::array(size_t i) const
121 {
122 #ifdef FIBER_CHECK_ARRAY_BOUNDS
123  check_array_index(i);
124 #endif
125  return m_arrays[i];
126 }
127 
128 template <typename T>
129 inline void CollectionOf2dArrays<T>::check_array_index(size_t array_index) const
130 {
131 #ifdef FIBER_CHECK_ARRAY_BOUNDS
132  if (m_size <= array_index)
133  throw std::invalid_argument("Invalid array index");
134 #endif
135 }
136 
137 // CollectionOf1dSlicesOf2dArrays
138 
139 template <typename T>
140 inline CollectionOf1dSlicesOf2dArrays<T>::
141 CollectionOf1dSlicesOf2dArrays(CollectionOf2dArrays<T>& collection,
142  size_t index1) :
143  m_collection(collection), m_index1(index1)
144 {}
145 
146 template <typename T>
147 inline CollectionOf1dSlicesOf2dArrays<T>& CollectionOf1dSlicesOf2dArrays<T>::
148 self() {
149  return *this;
150 }
151 
152 template <typename T>
154 operator[](size_t index) const {
155  return _1dSliceOfConst2dArray<T>(m_collection[index], m_index1);
156 }
157 
158 template <typename T>
159 inline _1dSliceOf2dArray<T> CollectionOf1dSlicesOf2dArrays<T>::
160 operator[](size_t index) {
161  return _1dSliceOf2dArray<T>(m_collection[index], m_index1);
162 }
163 
164 template <typename T>
165 inline size_t CollectionOf1dSlicesOf2dArrays<T>::size() const {
166  return m_collection.size();
167 }
168 
169 // CollectionOf1dSlicesOfConst2dArrays
170 
171 template <typename T>
172 inline CollectionOf1dSlicesOfConst2dArrays<T>::
173 CollectionOf1dSlicesOfConst2dArrays(const CollectionOf2dArrays<T>& collection,
174  size_t index1) :
175  m_collection(collection), m_index1(index1)
176 {}
177 
178 template <typename T>
179 inline _1dSliceOfConst2dArray<T> CollectionOf1dSlicesOfConst2dArrays<T>::
180 operator[](size_t index) const {
181  return _1dSliceOfConst2dArray<T>(m_collection[index], m_index1);
182 }
183 
184 template <typename T>
185 inline size_t CollectionOf1dSlicesOfConst2dArrays<T>::size() const {
186  return m_collection.size();
187 }
188 
189 } // namespace Fiber
Definition: collection_of_2d_arrays.hpp:68
CollectionOf1dSlicesOf2dArrays & self()
Returns a reference to self.
Definition: collection_of_2d_arrays_imp.hpp:148
Lightweight encapsulation of a 1D slice of a constant 2D array.
Definition: _2d_array.hpp:119