MAIA bb96820c
Multiphysics at AIA
Loading...
Searching...
No Matches
maia::tensor::detail_::TensorStorage< T > Class Template Reference

A vector-like data storage class that supports both internal and external storage mechanisms. More...

#include <tensor.h>

Inheritance diagram for maia::tensor::detail_::TensorStorage< T >:
[legend]

Public Types

typedef MLong size_type
 
typedef T value_type
 
typedef value_typepointer
 
typedef const value_typeconst_pointer
 
typedef value_typereference
 
typedef const value_typeconst_reference
 
typedef pointer iterator
 
typedef const_pointer const_iterator
 

Public Member Functions

 TensorStorage ()
 Default constructor does not allocate any memory but creates a zero-sized storage container. More...
 
 TensorStorage (const TensorStorage< T > &ts)
 Copy constructor creates a copy of the provided object. More...
 
 TensorStorage (size_type n)
 Creates a new TensorStorage object using internal storage. More...
 
 TensorStorage (size_type n, T *data)
 Creates a new TensorStorage object using external storage, i.e. no memory is allocated. More...
 
 ~TensorStorage ()
 The destructor only calls clear(). More...
 
size_type size () const
 Returns the current size of the data container. More...
 
void resize (size_type n)
 Resizes the container by first dropping all existing elements and then reallocating new memory. More...
 
void resize (size_type n, T *data)
 Resizes the container by first dropping all existing elements and then using the external data pointer for the new storage. More...
 
void clear ()
 Clears the data storage by deallocating any previously allocated memory and setting the size to zero. More...
 
void swap (TensorStorage< T > &other)
 Swap the contents of the existing TensorStorage object with another one. More...
 
TensorStorage< T > & operator= (TensorStorage< T > copy)
 Assignment operator copies another object to the current one. More...
 
T & operator[] (size_type n)
 Returns a reference to the element at position n (non-const version). More...
 
const T & operator[] (size_type n) const
 Returns a reference to the element at position n (const version). More...
 

Private Member Functions

MBool isAllocated ()
 Returns true if this class has allocated its own memory, and false if external memory is used. More...
 
void reallocate (size_type n)
 Clears any existing internal data storage and then allocates new memory to store as many elements as specified. More...
 

Private Attributes

T * m_data
 
size_type m_size
 
MBool m_isAllocated
 

Friends

template<class TT >
void swap (TensorStorage< TT > &a, TensorStorage< TT > &b)
 Swap the contents between two TensorStorage objects. More...
 

Detailed Description

template<class T>
class maia::tensor::detail_::TensorStorage< T >
Author
Michael Schlottke (mic) mic@a.nosp@m.ia.r.nosp@m.wth-a.nosp@m.ache.nosp@m.n.de
Date
2012-11-14
Template Parameters
TScalar data type of the tensor elements.

By default, an internal storage mechanism is used, i.e. memory is allocated and deallocated automatically. However, there is also the possibility to provide a pointer to an existing storage location, which allows this class to be used as a "viewport" to data in memory. For the design of the class, a close look was taken at std::vector, in order to support most of the dynamic reallocation methods and other operations that std::vector has as well.

This class is mainly used by Tensor as its internal storage.

Definition at line 780 of file tensor.h.

Member Typedef Documentation

◆ const_iterator

template<class T >
typedef const_pointer maia::tensor::detail_::TensorStorage< T >::const_iterator

Definition at line 790 of file tensor.h.

◆ const_pointer

template<class T >
typedef const value_type* maia::tensor::detail_::TensorStorage< T >::const_pointer

Definition at line 786 of file tensor.h.

◆ const_reference

template<class T >
typedef const value_type& maia::tensor::detail_::TensorStorage< T >::const_reference

Definition at line 788 of file tensor.h.

◆ iterator

template<class T >
typedef pointer maia::tensor::detail_::TensorStorage< T >::iterator

Definition at line 789 of file tensor.h.

◆ pointer

template<class T >
typedef value_type* maia::tensor::detail_::TensorStorage< T >::pointer

Definition at line 785 of file tensor.h.

◆ reference

template<class T >
typedef value_type& maia::tensor::detail_::TensorStorage< T >::reference

Definition at line 787 of file tensor.h.

◆ size_type

template<class T >
typedef MLong maia::tensor::detail_::TensorStorage< T >::size_type

Definition at line 783 of file tensor.h.

◆ value_type

template<class T >
typedef T maia::tensor::detail_::TensorStorage< T >::value_type

Definition at line 784 of file tensor.h.

Constructor & Destructor Documentation

◆ TensorStorage() [1/4]

template<class T >
maia::tensor::detail_::TensorStorage< T >::TensorStorage
inline
Author
Michael Schlottke (mic) mic@a.nosp@m.ia.r.nosp@m.wth-a.nosp@m.ache.nosp@m.n.de
Date
2012-11-14

Definition at line 830 of file tensor.h.

830 : m_data(0), m_size(0), m_isAllocated(false) {
831 // Nothing here
832}

◆ TensorStorage() [2/4]

template<class T >
maia::tensor::detail_::TensorStorage< T >::TensorStorage ( const TensorStorage< T > &  ts)
inline
Author
Michael Schlottke (mic) mic@a.nosp@m.ia.r.nosp@m.wth-a.nosp@m.ache.nosp@m.n.de
Date
2012-11-14
Parameters
[in]tsObject that is copied.

Note: The copy constructor will always allocate memory, even if the original object used external storage.

Definition at line 847 of file tensor.h.

847 : m_data(0), m_size(0), m_isAllocated(false) {
848 // Allocate new memory and copy the data members
849 reallocate(ts.size());
850 std::copy(&ts.m_data[0], &ts.m_data[0] + ts.size(), m_data);
851}
void reallocate(size_type n)
Clears any existing internal data storage and then allocates new memory to store as many elements as ...
Definition: tensor.h:1080

◆ TensorStorage() [3/4]

template<class T >
maia::tensor::detail_::TensorStorage< T >::TensorStorage ( size_type  n)
inlineexplicit
Author
Michael Schlottke (mic) mic@a.nosp@m.ia.r.nosp@m.wth-a.nosp@m.ache.nosp@m.n.de
Date
2012-11-14
Parameters
[in]nSize of the new container.

Definition at line 863 of file tensor.h.

863 : m_data(0), m_size(0), m_isAllocated(false) {
864 resize(n);
865}
void resize(size_type n)
Resizes the container by first dropping all existing elements and then reallocating new memory.
Definition: tensor.h:924

◆ TensorStorage() [4/4]

template<class T >
maia::tensor::detail_::TensorStorage< T >::TensorStorage ( size_type  n,
T *  data 
)
inline
Author
Michael Schlottke (mic) mic@a.nosp@m.ia.r.nosp@m.wth-a.nosp@m.ache.nosp@m.n.de
Date
2012-11-14
Parameters
[in]nSize of the storage in memory.
[in]dataPointer to existing memory location.

Since this is supposed to be a very lightweight and fast way to access memory through TensorStorage, no data is changed to some initial value when this constructor is called. Furthermore, the user has to ensure that the data location that is pointed to does not get deallocated before the TensorStorage object is destroyed.

Definition at line 883 of file tensor.h.

883 : m_data(data), m_size(n), m_isAllocated(false) {
884 ASSERT(n >= 0, "New storage size must be greater or equal to zero.");
885 ASSERT(data != 0, "Data pointer may not be a null pointer.");
886}

◆ ~TensorStorage()

template<class T >
maia::tensor::detail_::TensorStorage< T >::~TensorStorage
inline
Author
Michael Schlottke (mic) mic@a.nosp@m.ia.r.nosp@m.wth-a.nosp@m.ache.nosp@m.n.de
Date
2012-11-14

Definition at line 896 of file tensor.h.

896 {
897 clear();
898}
void clear()
Clears the data storage by deallocating any previously allocated memory and setting the size to zero.
Definition: tensor.h:959

Member Function Documentation

◆ clear()

template<class T >
void maia::tensor::detail_::TensorStorage< T >::clear
inline
Author
Michael Schlottke (mic) mic@a.nosp@m.ia.r.nosp@m.wth-a.nosp@m.ache.nosp@m.n.de
Date
2012-11-14

Definition at line 959 of file tensor.h.

959 {
960 if(isAllocated()) {
961 delete[] m_data;
962 }
963
964 m_data = 0;
965 m_size = 0;
966 m_isAllocated = false;
967}
MBool isAllocated()
Returns true if this class has allocated its own memory, and false if external memory is used.
Definition: tensor.h:1064

◆ isAllocated()

template<class T >
MBool maia::tensor::detail_::TensorStorage< T >::isAllocated
inlineprivate
Author
Michael Schlottke (mic) mic@a.nosp@m.ia.r.nosp@m.wth-a.nosp@m.ache.nosp@m.n.de
Date
2013-05-08

Definition at line 1064 of file tensor.h.

1064 {
1065 return m_isAllocated;
1066}

◆ operator=()

template<class T >
TensorStorage< T > & maia::tensor::detail_::TensorStorage< T >::operator= ( TensorStorage< T >  copy)
inline
Author
Michael Schlottke (mic) mic@a.nosp@m.ia.r.nosp@m.wth-a.nosp@m.ache.nosp@m.n.de
Date
2012-11-14
Parameters
[in]copyThe object that is copied.
Returns
The assigned object (*this)

Definition at line 1014 of file tensor.h.

1014 {
1015 swap(copy);
1016
1017 return *this;
1018}
friend void swap(TensorStorage< TT > &a, TensorStorage< TT > &b)
Swap the contents between two TensorStorage objects.
Definition: tensor.h:998

◆ operator[]() [1/2]

template<class T >
T & maia::tensor::detail_::TensorStorage< T >::operator[] ( size_type  n)
inline
Author
Michael Schlottke (mic) mic@a.nosp@m.ia.r.nosp@m.wth-a.nosp@m.ache.nosp@m.n.de
Date
2012-11-14
Parameters
[in]nPosition of the element in the data storage.
Returns
The element at the specified position.

Definition at line 1032 of file tensor.h.

1032 {
1033 ASSERT(n >= 0 && n < size(), "Index out of bounds");
1034
1035 return m_data[n];
1036}
size_type size() const
Returns the current size of the data container.
Definition: tensor.h:910

◆ operator[]() [2/2]

template<class T >
const T & maia::tensor::detail_::TensorStorage< T >::operator[] ( size_type  n) const
inline
Author
Michael Schlottke (mic) mic@a.nosp@m.ia.r.nosp@m.wth-a.nosp@m.ache.nosp@m.n.de
Date
2012-11-14
Parameters
[in]nPosition of the element in the data storage.
Returns
The element at the specified position.

Definition at line 1050 of file tensor.h.

1050 {
1051 ASSERT(n >= 0 && n < size(), "Index out of bounds");
1052
1053 return m_data[n];
1054}

◆ reallocate()

template<class T >
void maia::tensor::detail_::TensorStorage< T >::reallocate ( size_type  n)
private
Author
Michael Schlottke (mic) mic@a.nosp@m.ia.r.nosp@m.wth-a.nosp@m.ache.nosp@m.n.de
Date
2012-11-14
Parameters
[in]nThe number of elements that can be stored in the new container.

Definition at line 1080 of file tensor.h.

1080 {
1081 clear();
1082
1083 m_data = (n <= 0) ? nullptr : new T[std::min(n, PTRDIFF_MAX)];
1084 m_size = n;
1085 m_isAllocated = true;
1086}

◆ resize() [1/2]

template<class T >
void maia::tensor::detail_::TensorStorage< T >::resize ( size_type  n)
inline
Author
Michael Schlottke (mic) mic@a.nosp@m.ia.r.nosp@m.wth-a.nosp@m.ache.nosp@m.n.de
Date
2012-11-14
Parameters
[in]nNew size of the data container.

Definition at line 924 of file tensor.h.

924 {
925 ASSERT(n >= 0, "New storage size must be greater or equal to zero.");
926
927 reallocate(n);
928}

◆ resize() [2/2]

template<class T >
void maia::tensor::detail_::TensorStorage< T >::resize ( size_type  n,
T *  data 
)
inline
Author
Michael Schlottke (mic) mic@a.nosp@m.ia.r.nosp@m.wth-a.nosp@m.ache.nosp@m.n.de
Date
2012-11-14
Parameters
[in]nNew size of the data container.
[in]dataPointer to existing memory location.

Definition at line 942 of file tensor.h.

942 {
943 ASSERT(n >= 0, "New storage size must be greater or equal to zero.");
944 ASSERT(data != 0, "Data pointer may not be a null pointer.");
945
946 clear();
947 m_size = n;
948 m_data = data;
949}

◆ size()

template<class T >
TensorStorage< T >::size_type maia::tensor::detail_::TensorStorage< T >::size
inline
Author
Michael Schlottke (mic) mic@a.nosp@m.ia.r.nosp@m.wth-a.nosp@m.ache.nosp@m.n.de
Date
2012-11-14
Returns
Length of the data array.

Definition at line 910 of file tensor.h.

910 {
911 return m_size;
912}

◆ swap()

template<class T >
void maia::tensor::detail_::TensorStorage< T >::swap ( TensorStorage< T > &  other)
inline
Author
Michael Schlottke (mic) mic@a.nosp@m.ia.r.nosp@m.wth-a.nosp@m.ache.nosp@m.n.de
Date
2012-11-14
Parameters
[in]otherThe object with which the contents are exchanged.

Definition at line 979 of file tensor.h.

979 {
980 using std::swap;
981 swap(m_data, other.m_data);
982 swap(m_size, other.m_size);
983 swap(m_isAllocated, other.m_isAllocated);
984}

Friends And Related Function Documentation

◆ swap

template<class T >
template<class TT >
void swap ( TensorStorage< TT > &  a,
TensorStorage< TT > &  b 
)
friend
Author
Michael Schlottke (mic) mic@a.nosp@m.ia.r.nosp@m.wth-a.nosp@m.ache.nosp@m.n.de
Date
2012-11-14
Template Parameters
TT
Parameters
[in]aFirst object.
[in]bSecond object.

Definition at line 998 of file tensor.h.

998 {
999 a.swap(b);
1000}
Definition: contexttypes.h:19

Member Data Documentation

◆ m_data

template<class T >
T* maia::tensor::detail_::TensorStorage< T >::m_data
private

Definition at line 817 of file tensor.h.

◆ m_isAllocated

template<class T >
MBool maia::tensor::detail_::TensorStorage< T >::m_isAllocated
private

Definition at line 819 of file tensor.h.

◆ m_size

template<class T >
size_type maia::tensor::detail_::TensorStorage< T >::m_size
private

Definition at line 818 of file tensor.h.


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