/
MooseArray.h
339 lines (289 loc) · 7.38 KB
/
MooseArray.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html
#ifndef ARRAY_H
#define ARRAY_H
#include <vector>
#include "MooseError.h"
template <typename T>
class MooseArray
{
public:
/**
* Default constructor. Doesn't initialize anything.
*/
MooseArray() : _data(NULL), _size(0), _allocated_size(0) {}
/**
* @param size The initial size of the array.
*/
explicit MooseArray(const unsigned int size) : _data(NULL), _allocated_size(0) { resize(size); }
/**
* @param size The initial size of the array.
* @param default_value The default value to set.
*/
explicit MooseArray(const unsigned int size, const T & default_value)
: _data(NULL), _allocated_size(0)
{
resize(size);
setAllValues(default_value);
}
/**
* Sets all values of the array to the passed in value
* @param value The value every entry of the array will be set to.
*/
void setAllValues(const T & value);
/**
* Manually deallocates the data pointer
*/
void release()
{
if (_data != NULL)
{
delete[] _data;
_data = NULL;
_allocated_size = _size = 0;
}
}
/**
* Change the number of elements the array can store to zero.
*
* Will destroy data currently in array!
*
* Note that this does _not_ free unused memory.
* This is done for speed.
*/
void clear();
/**
* Change the number of elements the array can store.
*
* Will allocate more memory if necessary.
*
* Can destroy data currently in array!
* Basically, data retention not guaranteed.
*
* Note that this does _not_ free unused memory.
* This is done for speed.
*/
void resize(const unsigned int size);
/**
* Change the number of elements the array can store.
*
* Will allocate more memory if necessary.
*
* Can destroy data currently in array!
* Basically, data retention not guaranteed.
*
* Note that this does _not_ free unused memory.
* This is done for speed.
*
* Also note that default_value is only applied to NEW entries.
*/
void resize(const unsigned int size, const T & default_value);
/**
* The number of elements that can currently
* be stored in the array.
*/
unsigned int size() const;
/**
* Get element i out of the array.
*/
T & operator[](const unsigned int i);
/**
* Get element i out of the array.
*/
const T & operator[](const unsigned int i) const;
/**
* Swap memory in this object with the 'rhs' object
* @param rhs The object we are swapping with
*/
void swap(MooseArray & rhs);
/**
* Doesn't actually make a copy of the data.
*
* Just makes _this_ object operate on the same data.
*
* Note! You can leak memory with this function if you
* don't take care to have a copy of _this_ array somewhere
* else. This is because the data pointer will get overridden
* here.
*/
void shallowCopy(const MooseArray & rhs);
/**
* Doesn't actually make a copy of the data.
*
* Just makes _this_ object operate on the same data.
*
* Note! You can leak memory with this function if you
* don't take care to have a copy of _this_ array somewhere
* else. This is because the data pointer will get overridden
* here.
*
* I repeat! This is an extremely dangerous function!
*
* DO NOT USE UNLESS YOU KNOW WHAT YOU ARE DOING!
*
* YOU HAVE BEEN WARNED!
*/
void shallowCopy(std::vector<T> & rhs);
/**
* Actual operator=... really does make a copy of the data
*
* If you don't want a copy use shallowCopy()
*/
MooseArray<T> & operator=(const std::vector<T> & rhs);
/**
* Actual operator=... really does make a copy of the data
*
* If you don't want a copy use shallowCopy()
*/
MooseArray<T> & operator=(const MooseArray<T> & rhs);
/**
* Extremely inefficient way to produce a std::vector from a MooseArray!
*
* @return A _copy_ of the MooseArray contents.
*/
std::vector<T> stdVector() const;
private:
/// Actual data pointer.
T * _data;
/// The current number of elements the array can hold.
unsigned int _size;
/// Number of allocated memory positions for storage.
unsigned int _allocated_size;
};
template <typename T>
inline void
MooseArray<T>::setAllValues(const T & value)
{
for (unsigned int i = 0; i < _size; i++)
_data[i] = value;
}
template <typename T>
inline void
MooseArray<T>::clear()
{
_size = 0;
}
template <typename T>
inline void
MooseArray<T>::resize(const unsigned int size)
{
if (size <= _allocated_size)
_size = size;
else
{
T * new_pointer = new T[size];
mooseAssert(new_pointer, "Failed to allocate MooseArray memory!");
if (_data != NULL)
delete[] _data;
_data = new_pointer;
_allocated_size = size;
_size = size;
}
}
template <typename T>
inline void
MooseArray<T>::resize(const unsigned int size, const T & default_value)
{
if (size > _allocated_size)
{
T * new_pointer = new T[size];
mooseAssert(new_pointer, "Failed to allocate MooseArray memory!");
if (_data != NULL)
{
for (unsigned int i = 0; i < _size; i++)
new_pointer[i] = _data[i];
delete[] _data;
}
_data = new_pointer;
_allocated_size = size;
}
for (unsigned int i = _size; i < size; i++)
_data[i] = default_value;
_size = size;
}
template <typename T>
inline unsigned int
MooseArray<T>::size() const
{
return _size;
}
template <typename T>
inline T & MooseArray<T>::operator[](const unsigned int i)
{
mooseAssert(i < _size,
"Access out of bounds in MooseArray (i: " << i << " size: " << _size << ")");
return _data[i];
}
template <typename T>
inline const T & MooseArray<T>::operator[](const unsigned int i) const
{
mooseAssert(i < _size,
"Access out of bounds in MooseArray (i: " << i << " size: " << _size << ")");
return _data[i];
}
template <typename T>
inline void
MooseArray<T>::swap(MooseArray & rhs)
{
std::swap(_data, rhs._data);
std::swap(_size, rhs._size);
std::swap(_allocated_size, rhs._allocated_size);
}
template <typename T>
inline void
MooseArray<T>::shallowCopy(const MooseArray & rhs)
{
_data = rhs._data;
_size = rhs._size;
_allocated_size = rhs._allocated_size;
}
template <typename T>
inline void
MooseArray<T>::shallowCopy(std::vector<T> & rhs)
{
_data = &rhs[0];
_size = rhs.size();
_allocated_size = rhs.size();
}
template <typename T>
inline MooseArray<T> &
MooseArray<T>::operator=(const std::vector<T> & rhs)
{
unsigned int rhs_size = rhs.size();
resize(rhs_size);
for (unsigned int i = 0; i < rhs_size; i++)
_data[i] = rhs[i];
return *this;
}
template <typename T>
inline MooseArray<T> &
MooseArray<T>::operator=(const MooseArray<T> & rhs)
{
// mooseError("Shouldn't be doing this!");
resize(rhs._size);
// memcpy(_data,rhs._data,sizeof(T)*_size);
for (unsigned int i = 0; i < _size; i++)
_data[i] = rhs._data[i];
return *this;
}
template <class T>
std::vector<T>
MooseArray<T>::stdVector() const
{
return std::vector<T>(_data, _data + _size);
}
template <class T>
void
freeDoubleMooseArray(MooseArray<MooseArray<T>> & a)
{
for (unsigned int i = 0; i < a.size(); i++)
a[i].release();
a.release();
}
#endif // ARRAY_H