www.mooseframework.org
MooseArray.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 
12 #include <vector>
13 #include "MooseError.h"
14 
15 template <typename T>
17 {
18 public:
22  MooseArray() : _data(nullptr), _size(0), _allocated_size(0) {}
23 
27  explicit MooseArray(const unsigned int size) : _data(nullptr), _allocated_size(0)
28  {
29  resize(size);
30  }
31 
36  explicit MooseArray(const unsigned int size, const T & default_value)
37  : _data(nullptr), _allocated_size(0)
38  {
39  resize(size);
40 
41  setAllValues(default_value);
42  }
43 
44  explicit MooseArray(const MooseArray & rhs) : _data(nullptr), _allocated_size(0)
45  {
46  resize(rhs._size);
47 
48  for (unsigned int i = 0; i < _size; i++)
49  _data[i] = rhs._data[i];
50  }
51 
56  void setAllValues(const T & value);
57 
61  void release()
62  {
63  if (_data_ptr)
64  {
65  _data_ptr.reset();
66  _data = nullptr;
67  _allocated_size = _size = 0;
68  }
69  }
70 
79  void clear();
80 
92  void resize(unsigned int size);
93 
107  void resize(unsigned int size, const T & default_value);
108 
113  unsigned int size() const;
114 
118  T & operator[](const unsigned int i);
119 
123  const T & operator[](const unsigned int i) const;
124 
129  void swap(MooseArray & rhs);
130 
141  void shallowCopy(const MooseArray & rhs);
142 
159  void shallowCopy(std::vector<T> & rhs);
160 
166  MooseArray<T> & operator=(const std::vector<T> & rhs);
167 
173  MooseArray<T> & operator=(const MooseArray<T> & rhs);
174 
180  std::vector<T> stdVector() const;
181 
185  const T * data() const { return _data; }
186 
187 private:
189  std::unique_ptr<T[]> _data_ptr;
190 
191  // Actual data pointer (from inside of the smart pointer).
192  T * _data;
193 
195  unsigned int _size;
196 
198  unsigned int _allocated_size;
199 };
200 
201 template <typename T>
202 inline void
204 {
205  for (unsigned int i = 0; i < _size; i++)
206  _data[i] = value;
207 }
208 
209 template <typename T>
210 inline void
212 {
213  _size = 0;
214 }
215 
216 template <typename T>
217 inline void
218 MooseArray<T>::resize(unsigned int size)
219 {
220  if (size <= _allocated_size)
221  _size = size;
222  else
223  {
224  _data_ptr.reset(new T[size]);
225  mooseAssert(_data_ptr, "Failed to allocate MooseArray memory!");
226 
227  _data = _data_ptr.get();
228  _allocated_size = size;
229  _size = size;
230  }
231 }
232 
233 template <typename T>
234 inline void
235 MooseArray<T>::resize(unsigned int size, const T & default_value)
236 {
237  if (size > _allocated_size)
238  {
239  T * new_pointer = new T[size];
240  mooseAssert(new_pointer, "Failed to allocate MooseArray memory!");
241 
242  if (_data)
243  for (unsigned int i = 0; i < _size; i++)
244  new_pointer[i] = _data[i];
245 
246  _data_ptr.reset(new_pointer);
247  _data = _data_ptr.get();
248  _allocated_size = size;
249  }
250 
251  for (unsigned int i = _size; i < size; i++)
252  _data[i] = default_value;
253 
254  _size = size;
255 }
256 
257 template <typename T>
258 inline unsigned int
260 {
261  return _size;
262 }
263 
264 template <typename T>
265 inline T & MooseArray<T>::operator[](const unsigned int i)
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 const T & MooseArray<T>::operator[](const unsigned int i) const
275 {
276  mooseAssert(i < _size,
277  "Access out of bounds in MooseArray (i: " << i << " size: " << _size << ")");
278 
279  return _data[i];
280 }
281 
282 template <typename T>
283 inline void
285 {
286  _data_ptr.swap(rhs._data_ptr);
287  std::swap(_data, rhs._data);
288  std::swap(_size, rhs._size);
289  std::swap(_allocated_size, rhs._allocated_size);
290 }
291 
292 template <typename T>
293 inline void
295 {
296  _data_ptr.release();
297  _data = rhs._data;
298  _size = rhs._size;
299  _allocated_size = rhs._allocated_size;
300 }
301 
302 template <typename T>
303 inline void
304 MooseArray<T>::shallowCopy(std::vector<T> & rhs)
305 {
306  _data_ptr.release();
307  _data = &rhs[0];
308  _size = rhs.size();
309  _allocated_size = rhs.size();
310 }
311 
312 template <typename T>
313 inline MooseArray<T> &
314 MooseArray<T>::operator=(const std::vector<T> & rhs)
315 {
316  unsigned int rhs_size = rhs.size();
317 
318  resize(rhs_size);
319 
320  for (unsigned int i = 0; i < rhs_size; i++)
321  _data[i] = rhs[i];
322 
323  return *this;
324 }
325 
326 template <typename T>
327 inline MooseArray<T> &
329 {
330  // mooseError("Shouldn't be doing this!");
331  resize(rhs._size);
332  // memcpy(_data,rhs._data,sizeof(T)*_size);
333 
334  for (unsigned int i = 0; i < _size; i++)
335  _data[i] = rhs._data[i];
336 
337  return *this;
338 }
339 
340 template <class T>
341 std::vector<T>
343 {
344  return std::vector<T>(_data, _data + _size);
345 }
346 
347 template <class T>
348 void
350 {
351  for (unsigned int i = 0; i < a.size(); i++)
352  a[i].release();
353  a.release();
354 }
MooseArray::setAllValues
void setAllValues(const T &value)
Sets all values of the array to the passed in value.
Definition: MooseArray.h:203
MooseArray::operator=
MooseArray< T > & operator=(const std::vector< T > &rhs)
Actual operator=...
Definition: MooseArray.h:314
MooseArray::MooseArray
MooseArray(const MooseArray &rhs)
Definition: MooseArray.h:44
MooseArray::stdVector
std::vector< T > stdVector() const
Extremely inefficient way to produce a std::vector from a MooseArray!
Definition: MooseArray.h:342
MooseArray::_allocated_size
unsigned int _allocated_size
Number of allocated memory positions for storage.
Definition: MooseArray.h:198
freeDoubleMooseArray
void freeDoubleMooseArray(MooseArray< MooseArray< T >> &a)
Definition: MooseArray.h:349
MooseArray::_data
T * _data
Definition: MooseArray.h:192
MooseArray::_data_ptr
std::unique_ptr< T[]> _data_ptr
Smart pointer storage.
Definition: MooseArray.h:189
MooseArray::release
void release()
Manually deallocates the data pointer.
Definition: MooseArray.h:61
MooseArray::clear
void clear()
Change the number of elements the array can store to zero.
Definition: MooseArray.h:211
MooseArray::MooseArray
MooseArray(const unsigned int size, const T &default_value)
Definition: MooseArray.h:36
MooseArray::operator[]
T & operator[](const unsigned int i)
Get element i out of the array.
Definition: MooseArray.h:265
MooseArray::MooseArray
MooseArray(const unsigned int size)
Definition: MooseArray.h:27
MooseArray
forward declarations
Definition: MooseArray.h:16
MooseArray::MooseArray
MooseArray()
Default constructor.
Definition: MooseArray.h:22
swap
X_global swap(X_sys)
MooseArray::resize
void resize(unsigned int size)
Change the number of elements the array can store.
Definition: MooseArray.h:218
MooseError.h
MooseArray::data
const T * data() const
Reference to first element of array.
Definition: MooseArray.h:185
MooseArray::_size
unsigned int _size
The current number of elements the array can hold.
Definition: MooseArray.h:195
MooseArray::shallowCopy
void shallowCopy(const MooseArray &rhs)
Doesn't actually make a copy of the data.
Definition: MooseArray.h:294
MooseArray::swap
void swap(MooseArray &rhs)
Swap memory in this object with the 'rhs' object.
Definition: MooseArray.h:284
MooseArray::size
unsigned int size() const
The number of elements that can currently be stored in the array.
Definition: MooseArray.h:259