www.mooseframework.org
MooseArray.h
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #ifndef ARRAY_H
16 #define ARRAY_H
17 
18 #include <vector>
19 #include "MooseError.h"
20 
21 template <typename T>
23 {
24 public:
28  MooseArray() : _data(NULL), _size(0), _allocated_size(0) {}
29 
33  explicit MooseArray(const unsigned int size) : _data(NULL), _allocated_size(0) { resize(size); }
34 
39  explicit MooseArray(const unsigned int size, const T & default_value)
40  : _data(NULL), _allocated_size(0)
41  {
42  resize(size);
43 
44  setAllValues(default_value);
45  }
46 
51  void setAllValues(const T & value);
52 
56  void release()
57  {
58  if (_data != NULL)
59  {
60  delete[] _data;
61  _data = NULL;
62  _allocated_size = _size = 0;
63  }
64  }
65 
74  void clear();
75 
87  void resize(const unsigned int size);
88 
102  void resize(const unsigned int size, const T & default_value);
103 
108  unsigned int size() const;
109 
113  T & operator[](const unsigned int i);
114 
118  const T & operator[](const unsigned int i) const;
119 
124  void swap(MooseArray & rhs);
125 
136  void shallowCopy(const MooseArray & rhs);
137 
154  void shallowCopy(std::vector<T> & rhs);
155 
161  MooseArray<T> & operator=(const std::vector<T> & rhs);
162 
168  MooseArray<T> & operator=(const MooseArray<T> & rhs);
169 
175  std::vector<T> stdVector();
176 
177 private:
179  T * _data;
180 
182  unsigned int _size;
183 
185  unsigned int _allocated_size;
186 };
187 
188 template <typename T>
189 inline void
191 {
192  for (unsigned int i = 0; i < _size; i++)
193  _data[i] = value;
194 }
195 
196 template <typename T>
197 inline void
199 {
200  _size = 0;
201 }
202 
203 template <typename T>
204 inline void
205 MooseArray<T>::resize(const unsigned int size)
206 {
207  if (size <= _allocated_size)
208  _size = size;
209  else
210  {
211  T * new_pointer = new T[size];
212  mooseAssert(new_pointer, "Failed to allocate MooseArray memory!");
213 
214  if (_data != NULL)
215  delete[] _data;
216  _data = new_pointer;
218  _size = size;
219  }
220 }
221 
222 template <typename T>
223 inline void
224 MooseArray<T>::resize(const unsigned int size, const T & default_value)
225 {
226  if (size > _allocated_size)
227  {
228  T * new_pointer = new T[size];
229  mooseAssert(new_pointer, "Failed to allocate MooseArray memory!");
230 
231  if (_data != NULL)
232  {
233  for (unsigned int i = 0; i < _size; i++)
234  new_pointer[i] = _data[i];
235  delete[] _data;
236  }
237 
238  _data = new_pointer;
240  }
241 
242  for (unsigned int i = _size; i < size; i++)
243  _data[i] = default_value;
244 
245  _size = size;
246 }
247 
248 template <typename T>
249 inline unsigned int
251 {
252  return _size;
253 }
254 
255 template <typename T>
256 inline T & MooseArray<T>::operator[](const unsigned int i)
257 {
258  mooseAssert(i < _size,
259  "Access out of bounds in MooseArray (i: " << i << " size: " << _size << ")");
260 
261  return _data[i];
262 }
263 
264 template <typename T>
265 inline const T & MooseArray<T>::operator[](const unsigned int i) const
266 {
267  mooseAssert(i < _size,
268  "Access out of bounds in MooseArray (i: " << i << " size: " << _size << ")");
269 
270  return _data[i];
271 }
272 
273 template <typename T>
274 inline void
276 {
277  std::swap(_data, rhs._data);
278  std::swap(_size, rhs._size);
280 }
281 
282 template <typename T>
283 inline void
285 {
286  _data = rhs._data;
287  _size = rhs._size;
289 }
290 
291 template <typename T>
292 inline void
293 MooseArray<T>::shallowCopy(std::vector<T> & rhs)
294 {
295  _data = &rhs[0];
296  _size = rhs.size();
297  _allocated_size = rhs.size();
298 }
299 
300 template <typename T>
301 inline MooseArray<T> &
302 MooseArray<T>::operator=(const std::vector<T> & rhs)
303 {
304  unsigned int rhs_size = rhs.size();
305 
306  resize(rhs_size);
307 
308  for (unsigned int i = 0; i < rhs_size; i++)
309  _data[i] = rhs[i];
310 
311  return *this;
312 }
313 
314 template <typename T>
315 inline MooseArray<T> &
317 {
318  // mooseError("Shouldn't be doing this!");
319  resize(rhs._size);
320  // memcpy(_data,rhs._data,sizeof(T)*_size);
321 
322  for (unsigned int i = 0; i < _size; i++)
323  _data[i] = rhs._data[i];
324 
325  return *this;
326 }
327 
328 template <class T>
329 std::vector<T>
331 {
332  return std::vector<T>(_data, _data + _size);
333 }
334 
335 template <class T>
336 void
338 {
339  for (unsigned int i = 0; i < a.size(); i++)
340  a[i].release();
341  a.release();
342 }
343 
344 #endif // ARRAY_H
MooseArray(const unsigned int size, const T &default_value)
Definition: MooseArray.h:39
MooseArray()
Default constructor.
Definition: MooseArray.h:28
void resize(const unsigned int size)
Change the number of elements the array can store.
Definition: MooseArray.h:205
void swap(MooseArray &rhs)
Swap memory in this object with the &#39;rhs&#39; object.
Definition: MooseArray.h:275
void shallowCopy(const MooseArray &rhs)
Doesn&#39;t actually make a copy of the data.
Definition: MooseArray.h:284
void setAllValues(const T &value)
Sets all values of the array to the passed in value.
Definition: MooseArray.h:190
void freeDoubleMooseArray(MooseArray< MooseArray< T >> &a)
Definition: MooseArray.h:337
MooseArray< T > & operator=(const std::vector< T > &rhs)
Actual operator=...
Definition: MooseArray.h:302
unsigned int _size
The current number of elements the array can hold.
Definition: MooseArray.h:182
std::vector< T > stdVector()
Extremely inefficient way to produce a std::vector from a MooseArray!
Definition: MooseArray.h:330
unsigned int _allocated_size
Number of allocated memory positions for storage.
Definition: MooseArray.h:185
X_global swap(X_sys)
unsigned int size() const
The number of elements that can currently be stored in the array.
Definition: MooseArray.h:250
T * _data
Actual data pointer.
Definition: MooseArray.h:179
void release()
Manually deallocates the data pointer.
Definition: MooseArray.h:56
void clear()
Change the number of elements the array can store to zero.
Definition: MooseArray.h:198
T & operator[](const unsigned int i)
Get element i out of the array.
Definition: MooseArray.h:256
MooseArray(const unsigned int size)
Definition: MooseArray.h:33