www.mooseframework.org
Public Member Functions | Private Attributes | List of all members
MooseArray< T > Class Template Reference

forward declarations More...

#include <MooseArray.h>

Public Member Functions

 MooseArray ()
 Default constructor. More...
 
 MooseArray (const unsigned int size)
 
 MooseArray (const unsigned int size, const T &default_value)
 
 MooseArray (const MooseArray &rhs)
 
void setAllValues (const T &value)
 Sets all values of the array to the passed in value. More...
 
void release ()
 Manually deallocates the data pointer. More...
 
void clear ()
 Change the number of elements the array can store to zero. More...
 
void resize (unsigned int size)
 Change the number of elements the array can store. More...
 
void resize (unsigned int size, const T &default_value)
 Change the number of elements the array can store. More...
 
unsigned int size () const
 The number of elements that can currently be stored in the array. More...
 
T & operator[] (const unsigned int i)
 Get element i out of the array. More...
 
const T & operator[] (const unsigned int i) const
 Get element i out of the array. More...
 
void swap (MooseArray &rhs)
 Swap memory in this object with the 'rhs' object. More...
 
void shallowCopy (const MooseArray &rhs)
 Doesn't actually make a copy of the data. More...
 
void shallowCopy (std::vector< T > &rhs)
 Doesn't actually make a copy of the data. More...
 
MooseArray< T > & operator= (const std::vector< T > &rhs)
 Actual operator=... More...
 
MooseArray< T > & operator= (const MooseArray< T > &rhs)
 Actual operator=... More...
 
std::vector< T > stdVector () const
 Extremely inefficient way to produce a std::vector from a MooseArray! More...
 
const T * data () const
 Reference to first element of array. More...
 

Private Attributes

std::unique_ptr< T[]> _data_ptr
 Smart pointer storage. More...
 
T * _data
 
unsigned int _size
 The current number of elements the array can hold. More...
 
unsigned int _allocated_size
 Number of allocated memory positions for storage. More...
 

Detailed Description

template<typename T>
class MooseArray< T >

forward declarations

Definition at line 17 of file MooseArray.h.

Constructor & Destructor Documentation

◆ MooseArray() [1/4]

template<typename T>
MooseArray< T >::MooseArray ( )
inline

Default constructor.

Doesn't initialize anything.

Definition at line 23 of file MooseArray.h.

23 : _data(nullptr), _size(0), _allocated_size(0) {}
unsigned int _size
The current number of elements the array can hold.
Definition: MooseArray.h:196
unsigned int _allocated_size
Number of allocated memory positions for storage.
Definition: MooseArray.h:199

◆ MooseArray() [2/4]

template<typename T>
MooseArray< T >::MooseArray ( const unsigned int  size)
inlineexplicit
Parameters
sizeThe initial size of the array.

Definition at line 28 of file MooseArray.h.

28  : _data(nullptr), _allocated_size(0)
29  {
30  resize(size);
31  }
void resize(unsigned int size)
Change the number of elements the array can store.
Definition: MooseArray.h:219
unsigned int size() const
The number of elements that can currently be stored in the array.
Definition: MooseArray.h:260
unsigned int _allocated_size
Number of allocated memory positions for storage.
Definition: MooseArray.h:199

◆ MooseArray() [3/4]

template<typename T>
MooseArray< T >::MooseArray ( const unsigned int  size,
const T &  default_value 
)
inlineexplicit
Parameters
sizeThe initial size of the array.
default_valueThe default value to set.

Definition at line 37 of file MooseArray.h.

38  : _data(nullptr), _allocated_size(0)
39  {
40  resize(size);
41 
42  setAllValues(default_value);
43  }
void resize(unsigned int size)
Change the number of elements the array can store.
Definition: MooseArray.h:219
void setAllValues(const T &value)
Sets all values of the array to the passed in value.
Definition: MooseArray.h:204
unsigned int size() const
The number of elements that can currently be stored in the array.
Definition: MooseArray.h:260
unsigned int _allocated_size
Number of allocated memory positions for storage.
Definition: MooseArray.h:199

◆ MooseArray() [4/4]

template<typename T>
MooseArray< T >::MooseArray ( const MooseArray< T > &  rhs)
inlineexplicit

Definition at line 45 of file MooseArray.h.

45  : _data(nullptr), _allocated_size(0)
46  {
47  resize(rhs._size);
48 
49  for (unsigned int i = 0; i < _size; i++)
50  _data[i] = rhs._data[i];
51  }
void resize(unsigned int size)
Change the number of elements the array can store.
Definition: MooseArray.h:219
unsigned int _size
The current number of elements the array can hold.
Definition: MooseArray.h:196
unsigned int _allocated_size
Number of allocated memory positions for storage.
Definition: MooseArray.h:199

Member Function Documentation

◆ clear()

template<typename T >
void MooseArray< T >::clear ( )
inline

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.

Definition at line 212 of file MooseArray.h.

213 {
214  _size = 0;
215 }
unsigned int _size
The current number of elements the array can hold.
Definition: MooseArray.h:196

◆ data()

template<typename T>
const T* MooseArray< T >::data ( ) const
inline

Reference to first element of array.

Definition at line 186 of file MooseArray.h.

186 { return _data; }

◆ operator=() [1/2]

template<typename T>
MooseArray< T > & MooseArray< T >::operator= ( const std::vector< T > &  rhs)
inline

Actual operator=...

really does make a copy of the data

If you don't want a copy use shallowCopy()

Definition at line 313 of file MooseArray.h.

314 {
315  unsigned int rhs_size = rhs.size();
316 
317  resize(rhs_size);
318 
319  for (unsigned int i = 0; i < rhs_size; i++)
320  _data[i] = rhs[i];
321 
322  return *this;
323 }
void resize(unsigned int size)
Change the number of elements the array can store.
Definition: MooseArray.h:219

◆ operator=() [2/2]

template<typename T>
MooseArray< T > & MooseArray< T >::operator= ( const MooseArray< T > &  rhs)
inline

Actual operator=...

really does make a copy of the data

If you don't want a copy use shallowCopy()

Definition at line 327 of file MooseArray.h.

328 {
329  // mooseError("Shouldn't be doing this!");
330  resize(rhs._size);
331  // memcpy(_data,rhs._data,sizeof(T)*_size);
332 
333  for (unsigned int i = 0; i < _size; i++)
334  _data[i] = rhs._data[i];
335 
336  return *this;
337 }
void resize(unsigned int size)
Change the number of elements the array can store.
Definition: MooseArray.h:219
unsigned int _size
The current number of elements the array can hold.
Definition: MooseArray.h:196

◆ operator[]() [1/2]

template<typename T >
T & MooseArray< T >::operator[] ( const unsigned int  i)
inline

Get element i out of the array.

Definition at line 266 of file MooseArray.h.

267 {
268  mooseAssert(i < _size,
269  "Access out of bounds in MooseArray (i: " << i << " size: " << _size << ")");
270 
271  return _data[i];
272 }
unsigned int _size
The current number of elements the array can hold.
Definition: MooseArray.h:196

◆ operator[]() [2/2]

template<typename T >
const T & MooseArray< T >::operator[] ( const unsigned int  i) const
inline

Get element i out of the array.

Definition at line 275 of file MooseArray.h.

276 {
277  mooseAssert(i < _size,
278  "Access out of bounds in MooseArray (i: " << i << " size: " << _size << ")");
279 
280  return _data[i];
281 }
unsigned int _size
The current number of elements the array can hold.
Definition: MooseArray.h:196

◆ release()

template<typename T>
void MooseArray< T >::release ( )
inline

Manually deallocates the data pointer.

Definition at line 62 of file MooseArray.h.

Referenced by Assembly::modifyFaceWeightsDueToXFEM(), Assembly::modifyWeightsDueToXFEM(), Assembly::~Assembly(), and Coupleable::~Coupleable().

63  {
64  if (_data_ptr)
65  {
66  _data_ptr.reset();
67  _data = nullptr;
68  _allocated_size = _size = 0;
69  }
70  }
unsigned int _size
The current number of elements the array can hold.
Definition: MooseArray.h:196
unsigned int _allocated_size
Number of allocated memory positions for storage.
Definition: MooseArray.h:199
std::unique_ptr< T[]> _data_ptr
Smart pointer storage.
Definition: MooseArray.h:190

◆ resize() [1/2]

template<typename T >
void MooseArray< T >::resize ( unsigned int  size)
inline

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.

Definition at line 219 of file MooseArray.h.

Referenced by MooseVariableConstMonomial::computeMonomialNeighborValuesHelper(), MooseVariableConstMonomial::computeMonomialValuesHelper(), Coupleable::Coupleable(), MooseArray< Real >::MooseArray(), MooseVariableFE< Real >::MooseVariableFE(), Assembly::reinitFE(), Assembly::reinitFEFace(), Assembly::reinitNeighbor(), and Assembly::setCoordinateTransformation().

220 {
221  if (size <= _allocated_size)
222  _size = size;
223  else
224  {
225  _data_ptr.reset(new T[size]);
226  mooseAssert(_data_ptr, "Failed to allocate MooseArray memory!");
227 
228  _data = _data_ptr.get();
230  _size = size;
231  }
232 }
unsigned int _size
The current number of elements the array can hold.
Definition: MooseArray.h:196
unsigned int size() const
The number of elements that can currently be stored in the array.
Definition: MooseArray.h:260
unsigned int _allocated_size
Number of allocated memory positions for storage.
Definition: MooseArray.h:199
std::unique_ptr< T[]> _data_ptr
Smart pointer storage.
Definition: MooseArray.h:190

◆ resize() [2/2]

template<typename T>
void MooseArray< T >::resize ( unsigned int  size,
const T &  default_value 
)
inline

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.

Definition at line 236 of file MooseArray.h.

237 {
238  if (size > _allocated_size)
239  {
240  T * new_pointer = new T[size];
241  mooseAssert(new_pointer, "Failed to allocate MooseArray memory!");
242 
243  if (_data)
244  for (unsigned int i = 0; i < _size; i++)
245  new_pointer[i] = _data[i];
246 
247  _data_ptr.reset(new_pointer);
248  _data = _data_ptr.get();
250  }
251 
252  for (unsigned int i = _size; i < size; i++)
253  _data[i] = default_value;
254 
255  _size = size;
256 }
unsigned int _size
The current number of elements the array can hold.
Definition: MooseArray.h:196
unsigned int size() const
The number of elements that can currently be stored in the array.
Definition: MooseArray.h:260
unsigned int _allocated_size
Number of allocated memory positions for storage.
Definition: MooseArray.h:199
std::unique_ptr< T[]> _data_ptr
Smart pointer storage.
Definition: MooseArray.h:190

◆ setAllValues()

template<typename T>
void MooseArray< T >::setAllValues ( const T &  value)
inline

Sets all values of the array to the passed in value.

Parameters
valueThe value every entry of the array will be set to.

Definition at line 204 of file MooseArray.h.

Referenced by MooseArray< Real >::MooseArray().

205 {
206  for (unsigned int i = 0; i < _size; i++)
207  _data[i] = value;
208 }
unsigned int _size
The current number of elements the array can hold.
Definition: MooseArray.h:196

◆ shallowCopy() [1/2]

template<typename T >
void MooseArray< T >::shallowCopy ( const MooseArray< T > &  rhs)
inline

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.

Definition at line 295 of file MooseArray.h.

Referenced by Assembly::reinitFE(), Assembly::reinitFEFace(), and Assembly::reinitNeighborAtPhysical().

296 {
297  _data = rhs._data;
298  _size = rhs._size;
300 }
unsigned int _size
The current number of elements the array can hold.
Definition: MooseArray.h:196
unsigned int _allocated_size
Number of allocated memory positions for storage.
Definition: MooseArray.h:199

◆ shallowCopy() [2/2]

template<typename T>
void MooseArray< T >::shallowCopy ( std::vector< T > &  rhs)
inline

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!

Definition at line 304 of file MooseArray.h.

305 {
306  _data = &rhs[0];
307  _size = rhs.size();
308  _allocated_size = rhs.size();
309 }
unsigned int _size
The current number of elements the array can hold.
Definition: MooseArray.h:196
unsigned int _allocated_size
Number of allocated memory positions for storage.
Definition: MooseArray.h:199

◆ size()

template<typename T >
unsigned int MooseArray< T >::size ( ) const
inline

◆ stdVector()

template<class T >
std::vector< T > MooseArray< T >::stdVector ( ) const

Extremely inefficient way to produce a std::vector from a MooseArray!

Returns
A copy of the MooseArray contents.

Definition at line 341 of file MooseArray.h.

Referenced by Assembly::reinitElemAndNeighbor().

342 {
343  return std::vector<T>(_data, _data + _size);
344 }
unsigned int _size
The current number of elements the array can hold.
Definition: MooseArray.h:196

◆ swap()

template<typename T >
void MooseArray< T >::swap ( MooseArray< T > &  rhs)
inline

Swap memory in this object with the 'rhs' object.

Parameters
rhsThe object we are swapping with

Definition at line 285 of file MooseArray.h.

286 {
287  _data_ptr.swap(rhs._data_ptr);
288  std::swap(_data, rhs._data);
289  std::swap(_size, rhs._size);
291 }
unsigned int _size
The current number of elements the array can hold.
Definition: MooseArray.h:196
unsigned int _allocated_size
Number of allocated memory positions for storage.
Definition: MooseArray.h:199
X_global swap(X_sys)
std::unique_ptr< T[]> _data_ptr
Smart pointer storage.
Definition: MooseArray.h:190

Member Data Documentation

◆ _allocated_size

template<typename T>
unsigned int MooseArray< T >::_allocated_size
private

Number of allocated memory positions for storage.

Definition at line 199 of file MooseArray.h.

Referenced by MooseArray< Real >::release(), MooseArray< Real >::shallowCopy(), and MooseArray< Real >::swap().

◆ _data

template<typename T>
T* MooseArray< T >::_data
private

◆ _data_ptr

template<typename T>
std::unique_ptr<T[]> MooseArray< T >::_data_ptr
private

Smart pointer storage.

Definition at line 190 of file MooseArray.h.

Referenced by MooseArray< Real >::release(), and MooseArray< Real >::swap().

◆ _size

template<typename T>
unsigned int MooseArray< T >::_size
private

The documentation for this class was generated from the following file: