https://mooseframework.inl.gov
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Private Attributes | List of all members
MooseArray< T > Class Template Reference

forward declarations More...

#include <MooseArray.h>

Public Types

typedef T value_type
 

Public Member Functions

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

Private Attributes

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

Detailed Description

template<typename T>
class MooseArray< T >

forward declarations

Definition at line 17 of file MooseArray.h.

Member Typedef Documentation

◆ value_type

template<typename T >
typedef T MooseArray< T >::value_type

Definition at line 20 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 25 of file MooseArray.h.

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

◆ MooseArray() [2/4]

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

Definition at line 30 of file MooseArray.h.

30 : _data(nullptr), _size(0), _allocated_size(0)
31 {
32 resize(size);
33 }
void resize(unsigned int size)
Change the number of elements the array can store.
Definition MooseArray.h:216
unsigned int size() const
The number of elements that can currently be stored in the array.
Definition MooseArray.h:259

◆ 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 39 of file MooseArray.h.

40 : _data(nullptr), _size(0), _allocated_size(0)
41 {
42 resize(size);
43
44 setAllValues(default_value);
45 }
void setAllValues(const T &value)
Sets all values of the array to the passed in value.
Definition MooseArray.h:200

◆ MooseArray() [4/4]

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

Definition at line 47 of file MooseArray.h.

47 : _data(nullptr), _size(0), _allocated_size(0)
48 {
49 resize(rhs._size);
50
51 for (unsigned int i = 0; i < _size; i++)
52 _data[i] = rhs._data[i];
53 }

◆ ~MooseArray()

template<typename T >
MooseArray< T >::~MooseArray ( )
default

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 208 of file MooseArray.h.

209{
210 _size = 0;
211}

◆ data() [1/2]

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

Definition at line 181 of file MooseArray.h.

181{ return _data; }

◆ data() [2/2]

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

Reference to first element of array.

Definition at line 180 of file MooseArray.h.

180{ return _data; }

◆ operator=() [1/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 330 of file MooseArray.h.

331{
332 // mooseError("Shouldn't be doing this!");
333 resize(rhs._size);
334 // memcpy(_data,rhs._data,sizeof(T)*_size);
335
336 for (unsigned int i = 0; i < _size; i++)
337 _data[i] = rhs._data[i];
338
339 return *this;
340}

◆ operator=() [2/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 316 of file MooseArray.h.

317{
318 unsigned int rhs_size = rhs.size();
319
320 resize(rhs_size);
321
322 for (unsigned int i = 0; i < rhs_size; i++)
323 _data[i] = rhs[i];
324
325 return *this;
326}

◆ 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}

◆ 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 276 of file MooseArray.h.

277{
278 mooseAssert(i < _size,
279 "Access out of bounds in MooseArray (i: " << i << " size: " << _size << ")");
280
281 return _data[i];
282}

◆ release()

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

Manually deallocates the data pointer.

Definition at line 66 of file MooseArray.h.

67 {
68 if (_data_ptr)
69 {
70 _data_ptr.reset();
71 _data = nullptr;
73 }
74 }
std::unique_ptr< T[]> _data_ptr
Smart pointer storage.
Definition MooseArray.h:186

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

◆ resize() [1/2]

template<typename T >
template<bool value_initialize>
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.

Parameters
sizeThe new size of the array
Template Parameters
value_initializeWhether to perform value initialization of the array instead of default initialization

Definition at line 216 of file MooseArray.h.

217{
218 if (size > _allocated_size)
219 {
220 if constexpr (value_initialize)
221 _data_ptr.reset(new T[size]());
222 else
223 _data_ptr = std::make_unique<T[]>(size);
224 mooseAssert(_data_ptr, "Failed to allocate MooseArray memory!");
225
226 _data = _data_ptr.get();
228 }
229
230 _size = size;
231}

Referenced by MooseVariableScalar::computeAD(), Assembly::computeADFace(), Assembly::computeFaceMap(), MooseVariableData< OutputType >::computeIncrementAtQps(), Coupleable::getADDefaultCurl(), Coupleable::getADDefaultGradient(), Coupleable::getADDefaultSecond(), Coupleable::getADDefaultVectorGradient(), MooseVariableDataBase< OutputType >::getArrayDofValues(), MooseVariableDataBase< OutputType >::matrixTagValue(), MooseArray< T >::MooseArray(), MooseArray< T >::MooseArray(), MooseArray< T >::MooseArray(), MooseVariableDataBase< OutputType >::MooseVariableDataBase(), MooseVariableDataBase< OutputType >::nodalMatrixTagValue(), Assembly::reinitFVFace(), Assembly::reinitNeighborAtPhysical(), and Assembly::resizeADMappingObjects().

◆ 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 235 of file MooseArray.h.

236{
237 if (size > _allocated_size)
238 {
239 auto new_pointer = std::make_unique<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 = std::move(new_pointer);
247 _data = _data_ptr.get();
249 }
250
251 for (unsigned int i = _size; i < size; i++)
252 _data[i] = default_value;
253
254 _size = size;
255}

◆ 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 200 of file MooseArray.h.

201{
202 for (unsigned int i = 0; i < _size; i++)
203 _data[i] = value;
204}

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

◆ 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.

Definition at line 296 of file MooseArray.h.

297{
298 _data_ptr.reset();
299 _data = rhs._data;
300 _size = rhs._size;
302}

Referenced by Assembly::reinitElemFaceRef(), Assembly::reinitFE(), Assembly::reinitFEFace(), Assembly::reinitFEFaceNeighbor(), Assembly::reinitMortarElem(), Assembly::reinitNeighbor(), and Assembly::reinitNeighborFaceRef().

◆ 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.

Definition at line 306 of file MooseArray.h.

307{
308 _data_ptr.reset();
309 _data = &rhs[0];
310 _size = rhs.size();
311 _allocated_size = rhs.size();
312}

◆ 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 344 of file MooseArray.h.

345{
346 return std::vector<T>(_data, _data + _size);
347}

Referenced by Assembly::reinitElemAndNeighbor().

◆ 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 286 of file MooseArray.h.

287{
288 _data_ptr.swap(rhs._data_ptr);
289 std::swap(_data, rhs._data);
290 std::swap(_size, rhs._size);
291 std::swap(_allocated_size, rhs._allocated_size);
292}

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 195 of file MooseArray.h.

Referenced by MooseArray< T >::release(), MooseArray< T >::shallowCopy(), and MooseArray< T >::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 186 of file MooseArray.h.

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

◆ _size

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

The current number of elements the array can hold.

Definition at line 192 of file MooseArray.h.

Referenced by MooseArray< T >::MooseArray(), MooseArray< T >::operator=(), MooseArray< T >::release(), MooseArray< T >::shallowCopy(), and MooseArray< T >::swap().


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