Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Types | Public Member Functions | Protected Attributes | Private Member Functions | Static Private Member Functions | Friends | List of all members
Nektar::Array< OneD, const DataType > Class Template Reference

1D Array of constant elements with garbage collection and bounds checking. More...

#include <SharedArray.hpp>

Inheritance diagram for Nektar::Array< OneD, const DataType >:
Inheritance graph
[legend]

Public Types

typedef DataType * ArrayType
typedef const DataType & const_reference
typedef DataType & reference
typedef const DataType * const_iterator
typedef DataType * iterator
typedef DataType element
typedef unsigned int size_type

Public Member Functions

 Array ()
 Creates an empty array.
 Array (unsigned int dim1Size)
 Creates an array of size dim1Size.
 Array (unsigned int dim1Size, const DataType &initValue)
 Creates a 1D array with each element initialized to an initial value.
 Array (unsigned int dim1Size, const DataType *data)
 Creates a 1D array a copies data into it.
 Array (unsigned int dim1Size, const Array< OneD, const DataType > &rhs)
 Creates a 1D array that references rhs.
 Array (const Array< OneD, const DataType > &rhs)
 Creates a reference to rhs.
 ~Array ()
Array< OneD, const DataType > & operator= (const Array< OneD, const DataType > &rhs)
 Creates a reference to rhs.
const_iterator begin () const
const_iterator end () const
const_reference operator[] (unsigned int i) const
const elementget () const
 Returns a c-style pointer to the underlying array.
const elementdata () const
 Returns a c-style pointer to the underlying array.
size_type num_dimensions () const
 Returns 1.
size_type num_elements () const
 Returns the array's size.
size_type capacity () const
unsigned int GetOffset () const
 Returns the array's offset.
bool Overlaps (const Array< OneD, const DataType > &rhs) const
 Returns true is this array and rhs overlap.

Protected Attributes

unsigned int m_size
unsigned int m_capacity
DataType * m_data
unsigned int * m_count
unsigned int m_offset

Private Member Functions

void CreateStorage (unsigned int size)

Static Private Member Functions

template<typename T >
static Array< OneD, T > CreateWithOffset (const Array< OneD, T > &rhs, unsigned int offset)

Friends

template<typename T1 , typename T2 >
bool operator== (const Array< OneD, T1 > &, const Array< OneD, T2 > &)
bool operator== (const Array< OneD, NekDouble > &, const Array< OneD, NekDouble > &)
bool IsEqual (const Array< OneD, const NekDouble > &, const Array< OneD, const NekDouble > &, NekDouble)
template<typename T >
Array< OneD, T > operator+ (const Array< OneD, T > &lhs, unsigned int offset)
 Creates an array with a specified offset.
template<typename T >
Array< OneD, T > operator+ (unsigned int offset, const Array< OneD, T > &rhs)

Detailed Description

template<typename DataType>
class Nektar::Array< OneD, const DataType >

1D Array of constant elements with garbage collection and bounds checking.

Definition at line 59 of file SharedArray.hpp.

Member Typedef Documentation

template<typename DataType >
typedef DataType* Nektar::Array< OneD, const DataType >::ArrayType

Definition at line 62 of file SharedArray.hpp.

template<typename DataType >
typedef const DataType* Nektar::Array< OneD, const DataType >::const_iterator

Definition at line 66 of file SharedArray.hpp.

template<typename DataType >
typedef const DataType& Nektar::Array< OneD, const DataType >::const_reference

Definition at line 63 of file SharedArray.hpp.

template<typename DataType >
typedef DataType Nektar::Array< OneD, const DataType >::element

Definition at line 69 of file SharedArray.hpp.

template<typename DataType >
typedef DataType* Nektar::Array< OneD, const DataType >::iterator

Definition at line 67 of file SharedArray.hpp.

template<typename DataType >
typedef DataType& Nektar::Array< OneD, const DataType >::reference

Definition at line 64 of file SharedArray.hpp.

template<typename DataType >
typedef unsigned int Nektar::Array< OneD, const DataType >::size_type

Definition at line 70 of file SharedArray.hpp.

Constructor & Destructor Documentation

template<typename DataType >
Nektar::Array< OneD, const DataType >::Array ( )
inline

Creates an empty array.

Reimplemented in Nektar::Array< OneD, DataType >.

Definition at line 75 of file SharedArray.hpp.

References Nektar::CreateStorage().

template<typename DataType >
Nektar::Array< OneD, const DataType >::Array ( unsigned int  dim1Size)
inlineexplicit

Creates an array of size dim1Size.

If DataType is a fundamental type (double, int, etc.), then the allocated array is uninitialized. If it is any other type, each element is initialized with DataType's default constructor.

Reimplemented in Nektar::Array< OneD, DataType >.

Definition at line 90 of file SharedArray.hpp.

References Nektar::CreateStorage().

:
m_size(dim1Size),
m_capacity(dim1Size),
m_data(0),
m_count(0),
{
ArrayInitializationPolicy<DataType>::Initialize(m_data + 1, m_capacity);
}
template<typename DataType >
Nektar::Array< OneD, const DataType >::Array ( unsigned int  dim1Size,
const DataType &  initValue 
)
inline

Creates a 1D array with each element initialized to an initial value.

Parameters
dim1SizeThe array's size.
initValueEach element's initial value.

If DataType is a fundamental type (double, int, etc.), then the initial value is copied directly into each element. Otherwise, the DataType's copy constructor is used to initialize each element.

Reimplemented in Nektar::Array< OneD, DataType >.

Definition at line 110 of file SharedArray.hpp.

References Nektar::CreateStorage().

:
m_size(dim1Size),
m_capacity(dim1Size),
m_data(0),
m_count(0),
{
ArrayInitializationPolicy<DataType>::Initialize(m_data + 1, m_capacity, initValue);
}
template<typename DataType >
Nektar::Array< OneD, const DataType >::Array ( unsigned int  dim1Size,
const DataType *  data 
)
inline

Creates a 1D array a copies data into it.

Parameters
dim1Sizethe array's size.
dataThe data to copy.

If DataType is a fundamental type (double, int, etc.), then data is copied directly into the underlying storage. Otherwise, the DataType's copy constructor is used to copy each element.

Reimplemented in Nektar::Array< OneD, DataType >.

Definition at line 128 of file SharedArray.hpp.

References Nektar::CreateStorage().

:
m_size(dim1Size),
m_capacity(dim1Size),
m_data(0),
m_count(0),
{
ArrayInitializationPolicy<DataType>::Initialize(m_data + 1, m_capacity, data);
}
template<typename DataType >
Nektar::Array< OneD, const DataType >::Array ( unsigned int  dim1Size,
const Array< OneD, const DataType > &  rhs 
)
inline

Creates a 1D array that references rhs.

Parameters
dim1SizeThe size of the array. This is useful when you want this array to reference a subset of the elements in rhs.
rhsArray to reference. This constructor creates an array that references rhs. Any changes to rhs will be reflected in this array. The memory for the array will only be deallocated when both rhs and this array have gone out of scope.

Reimplemented in Nektar::Array< OneD, DataType >.

Definition at line 148 of file SharedArray.hpp.

References ASSERTL0, and num_elements().

:
m_size(dim1Size),
m_capacity(rhs.m_capacity),
m_data(rhs.m_data),
m_count(rhs.m_count),
m_offset(rhs.m_offset)
{
*m_count += 1;
ASSERTL0(m_size <= rhs.num_elements(), "Requested size is larger than input array size.");
}
template<typename DataType >
Nektar::Array< OneD, const DataType >::Array ( const Array< OneD, const DataType > &  rhs)
inline

Creates a reference to rhs.

Reimplemented in Nektar::Array< OneD, DataType >.

Definition at line 160 of file SharedArray.hpp.

:
m_size(rhs.m_size),
m_capacity(rhs.m_capacity),
m_data(rhs.m_data),
m_count(rhs.m_count),
m_offset(rhs.m_offset)
{
*m_count += 1;
}
template<typename DataType >
Nektar::Array< OneD, const DataType >::~Array ( )
inline

Definition at line 170 of file SharedArray.hpp.

{
if( m_count == 0 )
{
return;
}
*m_count -= 1;
if( *m_count == 0 )
{
ArrayDestructionPolicy<DataType>::Destroy(m_data+1, m_capacity);
MemoryManager<DataType>::RawDeallocate(m_data, m_capacity+1);
}
}

Member Function Documentation

template<typename DataType >
const_iterator Nektar::Array< OneD, const DataType >::begin ( ) const
inline

Definition at line 204 of file SharedArray.hpp.

Referenced by Nektar::NekVector< DataType >::NekVector().

{ return m_data + m_offset + 1; }
template<typename DataType >
size_type Nektar::Array< OneD, const DataType >::capacity ( ) const
inline

Definition at line 227 of file SharedArray.hpp.

{ return m_capacity; }
template<typename DataType >
void Nektar::Array< OneD, const DataType >::CreateStorage ( unsigned int  size)
inlineprivate

Definition at line 293 of file SharedArray.hpp.

{
DataType* storage = MemoryManager<DataType>::RawAllocate(size+1);
m_data = storage;
m_count = (unsigned int*)storage;
*m_count = 1;
//return NekPtr<DataType>(storage, size);
//return boost::shared_ptr<DataType>(storage,
//boost::bind(DeleteStorage<DataType>, storage, size) );
// DestroyArray(size), MemoryManager<DataType>() );
}
template<typename DataType >
template<typename T >
static Array<OneD, T> Nektar::Array< OneD, const DataType >::CreateWithOffset ( const Array< OneD, T > &  rhs,
unsigned int  offset 
)
inlinestaticprivate

Definition at line 306 of file SharedArray.hpp.

Referenced by Nektar::operator+().

{
Array<OneD, T> result(rhs);
result.m_offset += offset;
result.m_size = rhs.m_size - offset;
return result;
}
template<typename DataType >
const element* Nektar::Array< OneD, const DataType >::data ( ) const
inline

Returns a c-style pointer to the underlying array.

Definition at line 219 of file SharedArray.hpp.

{ return m_data+m_offset+1; }
template<typename DataType >
const_iterator Nektar::Array< OneD, const DataType >::end ( ) const
inline

Definition at line 205 of file SharedArray.hpp.

{ return m_data + m_offset + m_size + 1; }
template<typename DataType >
const element* Nektar::Array< OneD, const DataType >::get ( ) const
inline

Returns a c-style pointer to the underlying array.

Definition at line 216 of file SharedArray.hpp.

Referenced by Overlaps().

{ return m_data+m_offset+1; }
template<typename DataType >
unsigned int Nektar::Array< OneD, const DataType >::GetOffset ( ) const
inline

Returns the array's offset.

Definition at line 230 of file SharedArray.hpp.

{ return m_offset; }
template<typename DataType >
size_type Nektar::Array< OneD, const DataType >::num_dimensions ( ) const
inline

Returns 1.

Definition at line 222 of file SharedArray.hpp.

{ return 1; }
template<typename DataType >
size_type Nektar::Array< OneD, const DataType >::num_elements ( ) const
inline

Returns the array's size.

Definition at line 225 of file SharedArray.hpp.

Referenced by Array(), Nektar::StorageSmvBsr< T >::const_iterator::const_iterator(), Nektar::CopyArray(), Nektar::CopyArrayN(), Nektar::StorageSmvBsr< T >::const_iterator::forward(), Nektar::NekMatrix< DataType, StandardMatrixTag >::NekMatrix(), Nektar::NekVector< DataType >::NekVector(), Overlaps(), and Nektar::NekMatrix< DataType, StandardMatrixTag >::PerformCopyConstruction().

{ return m_size; }
template<typename DataType >
Array<OneD, const DataType>& Nektar::Array< OneD, const DataType >::operator= ( const Array< OneD, const DataType > &  rhs)
inline

Creates a reference to rhs.

Definition at line 186 of file SharedArray.hpp.

References m_capacity, m_count, m_data, m_offset, and m_size.

{
*m_count -= 1;
if( *m_count == 0 )
{
ArrayDestructionPolicy<DataType>::Destroy(m_data+1, m_capacity);
MemoryManager<DataType>::RawDeallocate(m_data, m_capacity+1);
}
m_data = rhs.m_data;
m_capacity = rhs.m_capacity;
m_count = rhs.m_count;
*m_count += 1;
m_offset = rhs.m_offset;
m_size = rhs.m_size;
return *this;
}
template<typename DataType >
const_reference Nektar::Array< OneD, const DataType >::operator[] ( unsigned int  i) const
inline

Definition at line 207 of file SharedArray.hpp.

References ASSERTL1.

{
ASSERTL1(static_cast<size_type>(i) < m_size, (std::string("Element ") +
boost::lexical_cast<std::string>(i) + std::string(" requested in an array of size ") +
boost::lexical_cast<std::string>(m_size)));
return *(m_data + i + m_offset + 1);
}
template<typename DataType >
bool Nektar::Array< OneD, const DataType >::Overlaps ( const Array< OneD, const DataType > &  rhs) const
inline

Returns true is this array and rhs overlap.

Definition at line 233 of file SharedArray.hpp.

References get(), and num_elements().

{
const element* start = get();
const element* end = start + m_size;
const element* rhs_start = rhs.get();
const element* rhs_end = rhs_start + rhs.num_elements();
return (rhs_start >= start && rhs_start <= end) ||
(rhs_end >= start && rhs_end <= end);
}

Friends And Related Function Documentation

template<typename DataType >
bool IsEqual ( const Array< OneD, const NekDouble > &  ,
const Array< OneD, const NekDouble > &  ,
NekDouble   
)
friend

Definition at line 45 of file ArrayEqualityComparison.cpp.

{
if( lhs.num_elements() != rhs.num_elements() )
{
return false;
}
if( lhs.data() == rhs.data() )
{
return true;
}
for(unsigned int i = 0; i < lhs.num_elements(); ++i)
{
if( fabs(lhs[i]-rhs[i]) > tol )
{
return false;
}
}
return true;
}
template<typename DataType >
template<typename T >
Array<OneD, T> operator+ ( const Array< OneD, T > &  lhs,
unsigned int  offset 
)
friend

Creates an array with a specified offset.

The return value will reference the same array as lhs, but with an offset.

For example, in the following:

Array<OneD, const double> result = anArray + 10;

result[0] == anArray[10];

template<typename DataType >
template<typename T >
Array<OneD, T> operator+ ( unsigned int  offset,
const Array< OneD, T > &  rhs 
)
friend
template<typename DataType >
template<typename T1 , typename T2 >
bool operator== ( const Array< OneD, T1 > &  lhs,
const Array< OneD, T2 > &  rhs 
)
friend

Definition at line 714 of file SharedArray.hpp.

{
if( lhs.num_elements() != rhs.num_elements() )
{
return false;
}
if( lhs.data() == rhs.data() )
{
return true;
}
for(unsigned int i = 0; i < lhs.num_elements(); ++i)
{
if( lhs[i] != rhs[i] )
{
return false;
}
}
return true;
}
template<typename DataType >
bool operator== ( const Array< OneD, NekDouble > &  ,
const Array< OneD, NekDouble > &   
)
friend

Definition at line 39 of file ArrayEqualityComparison.cpp.

{
return IsEqual(lhs,rhs);
}

Member Data Documentation

template<typename DataType >
unsigned int Nektar::Array< OneD, const DataType >::m_capacity
protected

Definition at line 268 of file SharedArray.hpp.

Referenced by operator=().

template<typename DataType >
unsigned int* Nektar::Array< OneD, const DataType >::m_count
protected

Definition at line 272 of file SharedArray.hpp.

Referenced by operator=().

template<typename DataType >
DataType* Nektar::Array< OneD, const DataType >::m_data
protected

Definition at line 271 of file SharedArray.hpp.

Referenced by operator=().

template<typename DataType >
unsigned int Nektar::Array< OneD, const DataType >::m_offset
protected

Definition at line 273 of file SharedArray.hpp.

Referenced by Nektar::Array< OneD, DataType >::CreateWithOffset(), and operator=().

template<typename DataType >
unsigned int Nektar::Array< OneD, const DataType >::m_size
protected

Definition at line 267 of file SharedArray.hpp.

Referenced by Nektar::Array< OneD, DataType >::CreateWithOffset(), and operator=().