Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties 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 60 of file SharedArray.hpp.

Member Typedef Documentation

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

Definition at line 63 of file SharedArray.hpp.

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

Definition at line 67 of file SharedArray.hpp.

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

Definition at line 64 of file SharedArray.hpp.

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

Definition at line 70 of file SharedArray.hpp.

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

Definition at line 68 of file SharedArray.hpp.

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

Definition at line 65 of file SharedArray.hpp.

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

Definition at line 71 of file SharedArray.hpp.

Constructor & Destructor Documentation

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

Creates an empty array.

Definition at line 76 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 91 of file SharedArray.hpp.

References Nektar::CreateStorage().

91  :
92  m_size(dim1Size),
93  m_capacity(dim1Size),
94  m_data(0),
95  m_count(0),
96  m_offset(0)
97  {
99  ArrayInitializationPolicy<DataType>::Initialize(m_data + 1, m_capacity);
100  }
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 111 of file SharedArray.hpp.

References Nektar::CreateStorage().

111  :
112  m_size(dim1Size),
113  m_capacity(dim1Size),
114  m_data(0),
115  m_count(0),
116  m_offset(0)
117  {
119  ArrayInitializationPolicy<DataType>::Initialize(m_data + 1, m_capacity, initValue);
120  }
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 129 of file SharedArray.hpp.

References Nektar::CreateStorage().

129  :
130  m_size(dim1Size),
131  m_capacity(dim1Size),
132  m_data(0),
133  m_count(0),
134  m_offset(0)
135  {
137  ArrayInitializationPolicy<DataType>::Initialize(m_data + 1, m_capacity, data);
138  }
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 149 of file SharedArray.hpp.

References ASSERTL0, and num_elements().

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

Creates a reference to rhs.

Definition at line 161 of file SharedArray.hpp.

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

Definition at line 171 of file SharedArray.hpp.

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

172  {
173  if( m_count == 0 )
174  {
175  return;
176  }
177 
178  *m_count -= 1;
179  if( *m_count == 0 )
180  {
181  ArrayDestructionPolicy<DataType>::Destroy(m_data+1, m_capacity);
183  }
184  }
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 205 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 228 of file SharedArray.hpp.

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

Definition at line 294 of file SharedArray.hpp.

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

295  {
296  DataType* storage = MemoryManager<DataType>::RawAllocate(size+1);
297  m_data = storage;
298  m_count = (unsigned int*)storage;
299  *m_count = 1;
300  //return NekPtr<DataType>(storage, size);
301  //return boost::shared_ptr<DataType>(storage,
302  //boost::bind(DeleteStorage<DataType>, storage, size) );
303  // DestroyArray(size), MemoryManager<DataType>() );
304  }
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 307 of file SharedArray.hpp.

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

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

Definition at line 206 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 217 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 231 of file SharedArray.hpp.

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

Returns 1.

Definition at line 223 of file SharedArray.hpp.

223 { 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 187 of file SharedArray.hpp.

References m_capacity, m_count, m_data, m_offset, m_size, and Nektar::MemoryManager< DataType >::RawDeallocate().

188  {
189  *m_count -= 1;
190  if( *m_count == 0 )
191  {
192  ArrayDestructionPolicy<DataType>::Destroy(m_data+1, m_capacity);
194  }
195 
196  m_data = rhs.m_data;
197  m_capacity = rhs.m_capacity;
198  m_count = rhs.m_count;
199  *m_count += 1;
200  m_offset = rhs.m_offset;
201  m_size = rhs.m_size;
202  return *this;
203  }
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 208 of file SharedArray.hpp.

References ASSERTL1.

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

References get(), and num_elements().

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

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 715 of file SharedArray.hpp.

716  {
717  if( lhs.num_elements() != rhs.num_elements() )
718  {
719  return false;
720  }
721 
722  if( lhs.data() == rhs.data() )
723  {
724  return true;
725  }
726 
727  for(unsigned int i = 0; i < lhs.num_elements(); ++i)
728  {
729  if( lhs[i] != rhs[i] )
730  {
731  return false;
732  }
733  }
734 
735  return true;
736  }
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 269 of file SharedArray.hpp.

Referenced by operator=().

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

Definition at line 273 of file SharedArray.hpp.

Referenced by operator=().

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

Definition at line 272 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