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

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

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.

Definition at line 90 of file SharedArray.hpp.

References Nektar::CreateStorage().

90  :
91  m_size(dim1Size),
92  m_capacity(dim1Size),
93  m_data(0),
94  m_count(0),
95  m_offset(0)
96  {
98  ArrayInitializationPolicy<DataType>::Initialize(m_data + 1, m_capacity);
99  }
void CreateStorage(unsigned int size)
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.

Definition at line 110 of file SharedArray.hpp.

References Nektar::CreateStorage().

110  :
111  m_size(dim1Size),
112  m_capacity(dim1Size),
113  m_data(0),
114  m_count(0),
115  m_offset(0)
116  {
118  ArrayInitializationPolicy<DataType>::Initialize(m_data + 1, m_capacity, initValue);
119  }
void CreateStorage(unsigned int size)
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.

Definition at line 128 of file SharedArray.hpp.

References Nektar::CreateStorage().

128  :
129  m_size(dim1Size),
130  m_capacity(dim1Size),
131  m_data(0),
132  m_count(0),
133  m_offset(0)
134  {
136  ArrayInitializationPolicy<DataType>::Initialize(m_data + 1, m_capacity, data);
137  }
void CreateStorage(unsigned int size)
const element * data() const
Returns a c-style pointer to the underlying array.
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.

Definition at line 148 of file SharedArray.hpp.

References ASSERTL0, and num_elements().

148  :
149  m_size(dim1Size),
150  m_capacity(rhs.m_capacity),
151  m_data(rhs.m_data),
152  m_count(rhs.m_count),
153  m_offset(rhs.m_offset)
154  {
155  *m_count += 1;
156  ASSERTL0(m_size <= rhs.num_elements(), "Requested size is larger than input array size.");
157  }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:161
template<typename DataType >
Nektar::Array< OneD, const DataType >::Array ( const Array< OneD, const DataType > &  rhs)
inline

Creates a reference to rhs.

Definition at line 160 of file SharedArray.hpp.

160  :
161  m_size(rhs.m_size),
162  m_capacity(rhs.m_capacity),
163  m_data(rhs.m_data),
164  m_count(rhs.m_count),
165  m_offset(rhs.m_offset)
166  {
167  *m_count += 1;
168  }
template<typename DataType >
Nektar::Array< OneD, const DataType >::~Array ( )
inline

Definition at line 170 of file SharedArray.hpp.

References Nektar::MemoryManager< DataType >::RawDeallocate().

171  {
172  if( m_count == 0 )
173  {
174  return;
175  }
176 
177  *m_count -= 1;
178  if( *m_count == 0 )
179  {
180  ArrayDestructionPolicy<DataType>::Destroy(m_data+1, m_capacity);
182  }
183  }
static void RawDeallocate(DataType *array, unsigned int NumberOfElements)
Deallocates memory allocated from RawAllocate.

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().

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

Definition at line 227 of file SharedArray.hpp.

227 { 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.

References Nektar::MemoryManager< DataType >::RawAllocate().

294  {
295  DataType* storage = MemoryManager<DataType>::RawAllocate(size+1);
296  m_data = storage;
297  m_count = (unsigned int*)storage;
298  *m_count = 1;
299  //return NekPtr<DataType>(storage, size);
300  //return boost::shared_ptr<DataType>(storage,
301  //boost::bind(DeleteStorage<DataType>, storage, size) );
302  // DestroyArray(size), MemoryManager<DataType>() );
303  }
static DataType * RawAllocate(unsigned int NumberOfElements)
Allocates a chunk of raw, uninitialized memory, capable of holding NumberOfElements objects...
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.

307  {
308  Array<OneD, T> result(rhs);
309  result.m_offset += offset;
310  result.m_size = rhs.m_size - offset;
311  return result;
312  }
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.

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

Definition at line 205 of file SharedArray.hpp.

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().

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.

230 { 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.

222 { return 1; }
template<typename DataType >
size_type Nektar::Array< OneD, const DataType >::num_elements ( ) const
inline
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, m_size, and Nektar::MemoryManager< DataType >::RawDeallocate().

187  {
188  *m_count -= 1;
189  if( *m_count == 0 )
190  {
191  ArrayDestructionPolicy<DataType>::Destroy(m_data+1, m_capacity);
193  }
194 
195  m_data = rhs.m_data;
196  m_capacity = rhs.m_capacity;
197  m_count = rhs.m_count;
198  *m_count += 1;
199  m_offset = rhs.m_offset;
200  m_size = rhs.m_size;
201  return *this;
202  }
static void RawDeallocate(DataType *array, unsigned int NumberOfElements)
Deallocates memory allocated from RawAllocate.
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.

208  {
209  ASSERTL1(static_cast<size_type>(i) < m_size, (std::string("Element ") +
210  boost::lexical_cast<std::string>(i) + std::string(" requested in an array of size ") +
211  boost::lexical_cast<std::string>(m_size)));
212  return *(m_data + i + m_offset + 1);
213  }
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode...
Definition: ErrorUtil.hpp:191
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().

234  {
235  const element* start = get();
236  const element* end = start + m_size;
237 
238  const element* rhs_start = rhs.get();
239  const element* rhs_end = rhs_start + rhs.num_elements();
240 
241  return (rhs_start >= start && rhs_start <= end) ||
242  (rhs_end >= start && rhs_end <= end);
243  }

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.

48  {
49  if( lhs.num_elements() != rhs.num_elements() )
50  {
51  return false;
52  }
53 
54  if( lhs.data() == rhs.data() )
55  {
56  return true;
57  }
58 
59  for(unsigned int i = 0; i < lhs.num_elements(); ++i)
60  {
61  if( fabs(lhs[i]-rhs[i]) > tol )
62  {
63  return false;
64  }
65  }
66 
67  return true;
68  }
StandardMatrixTag & lhs
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.

715  {
716  if( lhs.num_elements() != rhs.num_elements() )
717  {
718  return false;
719  }
720 
721  if( lhs.data() == rhs.data() )
722  {
723  return true;
724  }
725 
726  for(unsigned int i = 0; i < lhs.num_elements(); ++i)
727  {
728  if( lhs[i] != rhs[i] )
729  {
730  return false;
731  }
732  }
733 
734  return true;
735  }
StandardMatrixTag & lhs
template<typename DataType >
bool operator== ( const Array< OneD, NekDouble > &  ,
const Array< OneD, NekDouble > &   
)
friend

Definition at line 39 of file ArrayEqualityComparison.cpp.

40  {
41  return IsEqual(lhs,rhs);
42  }
friend bool IsEqual(const Array< OneD, const NekDouble > &, const Array< OneD, const NekDouble > &, NekDouble)

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
template<typename DataType >
unsigned int Nektar::Array< OneD, const DataType >::m_size
protected