PSCF v1.5.0
DArray.h
1#ifndef UTIL_D_ARRAY_H
2#define UTIL_D_ARRAY_H
3
4/*
5* Util Package - C++ Utilities for Scientific Computation
6*
7* Copyright 2010 - 2017, David Morse (morse012@um.edu)
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <util/containers/Array.h>
12#include <util/misc/Memory.h>
13#include <util/global.h>
14
15namespace Util
16{
17
30 template <typename Data>
31 class DArray : public Array<Data>
32 {
33
34 public:
35
40
47
55 DArray(DArray<Data> const & other);
56
62 virtual ~DArray();
63
78
93 DArray<Data>& operator = (Array<Data> const & other);
94
103
110
123
130 template <class Archive>
131 void serialize(Archive& ar, const unsigned int version);
132
133 using Array<Data>::isAllocated;
134
135 protected:
136
137 using Array<Data>::data_;
138 using Array<Data>::capacity_;
139
140 };
141
142 // Non-inline member function definitions
143
144 /*
145 * Default constructor.
146 */
147 template <typename Data>
149 : Array<Data>()
150 {}
151
152 /*
153 * Allocating constructor.
154 */
155 template <typename Data>
157 : Array<Data>()
158 { allocate(capacity); }
159
160 /*
161 * Copy constructor.
162 *
163 * Allocates memory and copies all elements by value.
164 *
165 *\param other the DArray to be copied.
166 */
167 template <typename Data>
169 : Array<Data>()
170 {
171 if (!other.isAllocated()) {
172 UTIL_THROW("Other DArray must be allocated.");
173 }
175 capacity_ = other.capacity_;
176 for (int i = 0; i < capacity_; ++i) {
177 data_[i] = other.data_[i];
178 }
179 }
180
181 /*
182 * Destructor.
183 */
184 template <typename Data>
186 {
187 if (isAllocated()) {
188 try {
190 } catch (...) {
191 data_ = nullptr;
192 std::cout << "Exception in DArray destructor";
193 }
194 capacity_ = 0;
195 }
196 }
197
198 /*
199 * Assignment, element-by-element.
200 *
201 * This operator will allocate memory if not allocated previously.
202 */
203 template <typename Data>
205 {
206 // Check for self assignment
207 if (this == &other) return *this;
208
209 // Precondition - require that other (RHS) array is allocated
210 UTIL_CHECK(other.isAllocated());
211 UTIL_CHECK(other.capacity() > 0);
212
213 // Allocate this (LHS) array if necessary
214 if (!isAllocated()) {
215 allocate(other.capacity());
216 }
217
218 // Require equal capacities
219 UTIL_CHECK (capacity_ == other.capacity_);
220
221 // Copy all elements
222 for (int i = 0; i < capacity_; ++i) {
223 data_[i] = other[i];
224 }
225
226 return *this;
227 }
228
229 /*
230 * Assignment from an Array<Data> (deep copy).
231 */
232 template <typename Data>
233 DArray<Data>& DArray<Data>::operator = (Array<Data> const & other)
234 {
235 // Check for self assignment
236 if (dynamic_cast< Array<Data>* >(this) == &other) return *this;
237
238 // Preconditions - require that other (RHS) array is allocated
239 UTIL_CHECK(other.isAllocated());
240 UTIL_CHECK(other.capacity() > 0);
241
242 // If this LHS array is not allocated, then allocate
243 if (!isAllocated()) {
244 allocate(other.capacity());
245 }
246
247 // Require equal capacities
248 UTIL_CHECK (capacity_ == other.capacity());
249
250 // Copy elements
251 for (int i = 0; i < capacity_; ++i) {
252 data_[i] = other[i];
253 }
254
255 return *this;
256 }
257
258 /*
259 * Allocate the underlying C array.
260 */
261 template <typename Data>
263 {
264 if (capacity <= 0) {
265 UTIL_THROW("Attempt to allocate with capacity <= 0");
266 }
267 if (isAllocated()) {
268 UTIL_THROW("Attempt to re-allocate a DArray");
269 }
272 }
273
274 /*
275 * Deallocate the underlying C array.
276 */
277 template <typename Data>
279 {
280 if (!isAllocated()) {
281 UTIL_THROW("Array is not allocated");
282 }
284 capacity_ = 0;
285 }
286
287 /*
288 * Reallocate and copy the underlying C array.
289 */
290 template <typename Data>
303
304 /*
305 * Serialize a DArray to/from an Archive.
306 */
307 template <typename Data>
308 template <class Archive>
309 void DArray<Data>::serialize(Archive& ar, const unsigned int version)
310 {
311 int capacity;
312 if (Archive::is_saving()) {
314 }
315 ar & capacity;
316 if (Archive::is_loading()) {
317 if (!isAllocated()) {
318 if (capacity > 0) {
320 }
321 } else {
322 if (capacity != capacity_) {
323 UTIL_THROW("Inconsistent DArray capacities");
324 }
325 }
326 }
327 if (isAllocated()) {
328 for (int i = 0; i < capacity_; ++i) {
329 ar & data_[i];
330 }
331 }
332 }
333
334}
335#endif
Data * data_
Pointer to an array of Data elements.
Definition Array.h:141
int capacity() const
Return allocated size.
Definition Array.h:250
bool isAllocated() const
Does this array have associated data?
Definition Array.h:236
int capacity_
Allocated size of the data_ array.
Definition Array.h:144
DArray(DArray< Data > const &other)
Copy constructor.
Definition DArray.h:168
void allocate(int capacity)
Allocate the underlying C array.
Definition DArray.h:262
void deallocate()
Deallocate the underlying C array.
Definition DArray.h:278
void serialize(Archive &ar, const unsigned int version)
Serialize a DArray to/from an Archive.
Definition DArray.h:309
DArray()
Default constructor.
Definition DArray.h:148
virtual ~DArray()
Destructor.
Definition DArray.h:185
void reallocate(int capacity)
Reallocate the underlying C array and copy to new location.
Definition DArray.h:291
DArray(int capacity)
Allocating constructor.
Definition DArray.h:156
DArray< Data > & operator=(DArray< Data > const &other)
Assignment from a DArray<Data> container.
Definition DArray.h:204
static void deallocate(Data *&ptr, size_t size)
Deallocate a C++ array.
Definition Memory.h:202
static void reallocate(Data *&ptr, size_t oldSize, size_t newSize)
Reallocate a C++ array.
Definition Memory.h:230
static void allocate(Data *&ptr, size_t size)
Allocate a C++ array.
Definition Memory.h:172
File containing preprocessor macros for error handling.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition global.h:49
Utility classes for scientific computation.