Nektar++
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 >:
[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 size_t size_type
 

Public Member Functions

 Array ()
 Creates an empty array. More...
 
 Array (size_type dim1Size)
 Creates an array of size dim1Size. More...
 
 Array (size_type dim1Size, const DataType &initValue)
 Creates a 1D array with each element initialized to an initial value. More...
 
 Array (size_type dim1Size, const DataType *data)
 Creates a 1D array a copies data into it. More...
 
 Array (size_type 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[] (size_type 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 size () const
 Returns the array's size. More...
 
size_type num_elements () const
 Returns the array's size. Deprecated. More...
 
size_type capacity () const
 Returns the array's capacity. More...
 
size_type GetOffset () const
 Returns the array's offset. More...
 
size_type GetCount () const
 Returns the array's reference counter. More...
 
bool Overlaps (const Array< OneD, const DataType > &rhs) const
 Returns true is this array and rhs overlap. More...
 

Protected Attributes

size_type m_size
 
size_type m_capacity
 
DataType * m_data
 
size_typem_count
 
size_type m_offset
 

Private Member Functions

void CreateStorage (size_type size)
 

Static Private Member Functions

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

Friends

template<typename T >
Array< OneD, T > operator+ (const Array< OneD, T > &lhs, typename Array< OneD, T >::size_type offset)
 Creates an array with a specified offset. More...
 
template<typename T >
Array< OneD, T > operator+ (typename Array< OneD, T >::size_type 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 55 of file SharedArray.hpp.

Member Typedef Documentation

◆ ArrayType

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

Definition at line 65 of file SharedArray.hpp.

◆ const_iterator

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

Definition at line 70 of file SharedArray.hpp.

◆ const_reference

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

Definition at line 67 of file SharedArray.hpp.

◆ element

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

Definition at line 73 of file SharedArray.hpp.

◆ iterator

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

Definition at line 71 of file SharedArray.hpp.

◆ reference

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

Definition at line 68 of file SharedArray.hpp.

◆ size_type

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

Definition at line 74 of file SharedArray.hpp.

Constructor & Destructor Documentation

◆ Array() [1/6]

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

Creates an empty array.

Definition at line 78 of file SharedArray.hpp.

79 :
80#ifdef WITH_PYTHON
81 m_pythonInfo(nullptr),
82#endif
83 m_size(0), m_capacity(0), m_data(nullptr), m_count(nullptr),
84 m_offset(0)
85 {
87 }

References Nektar::CreateStorage().

◆ Array() [2/6]

template<typename DataType >
Nektar::Array< OneD, const DataType >::Array ( size_type  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 94 of file SharedArray.hpp.

95 :
96#ifdef WITH_PYTHON
97 m_pythonInfo(nullptr),
98#endif
99 m_size(dim1Size), m_capacity(dim1Size), m_data(nullptr),
100 m_count(nullptr), m_offset(0)
101 {
103 ArrayInitializationPolicy<DataType>::Initialize(m_data, m_capacity);
104 }

References Nektar::CreateStorage().

◆ Array() [3/6]

template<typename DataType >
Nektar::Array< OneD, const DataType >::Array ( size_type  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 115 of file SharedArray.hpp.

116 :
117#ifdef WITH_PYTHON
118 m_pythonInfo(nullptr),
119#endif
120 m_size(dim1Size), m_capacity(dim1Size), m_data(nullptr),
121 m_count(nullptr), m_offset(0)
122 {
124 ArrayInitializationPolicy<DataType>::Initialize(m_data, m_capacity,
125 initValue);
126 }

References Nektar::CreateStorage().

◆ Array() [4/6]

template<typename DataType >
Nektar::Array< OneD, const DataType >::Array ( size_type  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 135 of file SharedArray.hpp.

136 :
137#ifdef WITH_PYTHON
138 m_pythonInfo(nullptr),
139#endif
140 m_size(dim1Size), m_capacity(dim1Size), m_data(nullptr),
141 m_count(nullptr), m_offset(0)
142 {
144 ArrayInitializationPolicy<DataType>::Initialize(m_data, m_capacity,
145 data);
146 }
const element * data() const
Returns a c-style pointer to the underlying array.

References Nektar::CreateStorage().

◆ Array() [5/6]

template<typename DataType >
Nektar::Array< OneD, const DataType >::Array ( size_type  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 157 of file SharedArray.hpp.

158 :
159#ifdef WITH_PYTHON
160 m_pythonInfo(rhs.m_pythonInfo),
161#endif
162 m_size(dim1Size), m_capacity(rhs.m_capacity), m_data(rhs.m_data),
163 m_count(rhs.m_count), m_offset(rhs.m_offset)
164 {
165 *m_count += 1;
166 ASSERTL0(m_size <= rhs.size(), "Requested size is \
167 larger than input array size.");
168 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:208

References ASSERTL0, and size().

◆ Array() [6/6]

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

Creates a reference to rhs.

Definition at line 192 of file SharedArray.hpp.

193 :
194#ifdef WITH_PYTHON
195 m_pythonInfo(rhs.m_pythonInfo),
196#endif
197 m_size(rhs.m_size), m_capacity(rhs.m_capacity), m_data(rhs.m_data),
198 m_count(rhs.m_count), m_offset(rhs.m_offset)
199 {
200 *m_count += 1;
201 }

◆ ~Array()

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

Definition at line 209 of file SharedArray.hpp.

210 {
211 if (m_count == nullptr)
212 {
213 return;
214 }
215
216 *m_count -= 1;
217 if (*m_count == 0)
218 {
219#ifdef WITH_PYTHON
220 if (*m_pythonInfo == nullptr)
221 {
222 ArrayDestructionPolicy<DataType>::Destroy(m_data, m_capacity);
224 }
225 else
226 {
227 (*m_pythonInfo)->m_callback((*m_pythonInfo)->m_pyObject);
228 delete *m_pythonInfo;
229 }
230
231 delete m_pythonInfo;
232 m_pythonInfo = nullptr;
233#else
234
235 ArrayDestructionPolicy<DataType>::Destroy(m_data, m_capacity);
237
238#endif
239
240 delete m_count; // Clean up the memory used for the reference count.
241 m_count = nullptr;
242 }
243 }
static void RawDeallocate(DataType *array, size_t NumberOfElements)
Deallocates memory allocated from RawAllocate.

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

Member Function Documentation

◆ begin()

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

Definition at line 292 of file SharedArray.hpp.

293 {
294 return m_data + m_offset;
295 }

◆ capacity()

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

Returns the array's capacity.

Definition at line 345 of file SharedArray.hpp.

346 {
347 return m_capacity;
348 }

◆ CreateStorage()

template<typename DataType >
void Nektar::Array< OneD, const DataType >::CreateStorage ( size_type  size)
inlineprivate

Definition at line 424 of file SharedArray.hpp.

425 {
426 DataType *storage = MemoryManager<DataType>::RawAllocate(size);
427 m_data = storage;
428
429 // Allocate an integer to hold the reference count. Note 1, all arrays
430 // that share this array's data (ie, point to m_data) will also share
431 // the m_count data. Note 2, previously m_count pointed to "(unsigned
432 // int*)storage".
433 m_count = new size_type();
434 *m_count = 1;
435#ifdef WITH_PYTHON
436 m_pythonInfo = new PythonInfo *();
437 *m_pythonInfo = nullptr;
438#endif
439 }
size_type size() const
Returns the array's size.
static DataType * RawAllocate(size_t NumberOfElements)
Allocates a chunk of raw, uninitialized memory, capable of holding NumberOfElements objects.

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

◆ CreateWithOffset()

template<typename DataType >
template<typename T >
static Array< OneD, T > Nektar::Array< OneD, const DataType >::CreateWithOffset ( const Array< OneD, T > &  rhs,
size_type  offset 
)
inlinestaticprivate

Definition at line 442 of file SharedArray.hpp.

444 {
445 Array<OneD, T> result(rhs);
446 result.m_offset += offset;
447 result.m_size = rhs.m_size - offset;
448 return result;
449 }

◆ data()

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

318 {
319 return m_data + m_offset;
320 }

◆ end()

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

Definition at line 296 of file SharedArray.hpp.

297 {
298 return m_data + m_offset + m_size;
299 }

◆ get()

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

312 {
313 return m_data + m_offset;
314 }

Referenced by Overlaps().

◆ GetCount()

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

Returns the array's reference counter.

Definition at line 357 of file SharedArray.hpp.

358 {
359 return *m_count;
360 }

◆ GetOffset()

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

Returns the array's offset.

Definition at line 351 of file SharedArray.hpp.

352 {
353 return m_offset;
354 }

◆ num_dimensions()

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

Returns 1.

Definition at line 323 of file SharedArray.hpp.

324 {
325 return 1;
326 }

◆ num_elements()

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

Returns the array's size. Deprecated.

Definition at line 336 of file SharedArray.hpp.

338 {
339 WARNINGL1(false, "member function num_elements() is deprecated, "
340 "use size() instead.");
341 return m_size;
342 }
#define WARNINGL1(condition, msg)
Definition: ErrorUtil.hpp:243

References WARNINGL1.

◆ operator=()

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

248 {
249 *m_count -= 1;
250 if (*m_count == 0)
251 {
252#ifdef WITH_PYTHON
253 if (*m_pythonInfo == nullptr)
254 {
255 ArrayDestructionPolicy<DataType>::Destroy(m_data, m_capacity);
257 }
258 else if ((*rhs.m_pythonInfo) != nullptr &&
259 (*m_pythonInfo)->m_pyObject !=
260 (*rhs.m_pythonInfo)->m_pyObject)
261 {
262 (*m_pythonInfo)->m_callback((*m_pythonInfo)->m_pyObject);
263 delete *m_pythonInfo;
264 }
265
266 delete m_pythonInfo;
267 m_pythonInfo = nullptr;
268#else
269
270 ArrayDestructionPolicy<DataType>::Destroy(m_data, m_capacity);
272#endif
273 delete m_count; // Clean up the memory used for the reference count.
274 m_count = nullptr;
275 }
276
277 m_data = rhs.m_data;
278 m_capacity = rhs.m_capacity;
279 m_count = rhs.m_count;
280 *m_count += 1;
281 m_offset = rhs.m_offset;
282 m_size = rhs.m_size;
283#ifdef WITH_PYTHON
284 m_pythonInfo = rhs.m_pythonInfo;
285#endif
286 return *this;
287 }

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

◆ operator[]()

template<typename DataType >
const_reference Nektar::Array< OneD, const DataType >::operator[] ( size_type  i) const
inline

Definition at line 301 of file SharedArray.hpp.

302 {
303 ASSERTL1(i < m_size,
304 std::string("Element ") + std::to_string(i) +
305 std::string(" requested in an array of size ") +
306 std::to_string(m_size));
307 return *(m_data + i + m_offset);
308 }
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
Definition: ErrorUtil.hpp:242

References ASSERTL1.

◆ Overlaps()

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

364 {
365 const element *start = get();
366 const element *end = start + m_size;
367
368 const element *rhs_start = rhs.get();
369 const element *rhs_end = rhs_start + rhs.size();
370
371 return (rhs_start >= start && rhs_start <= end) ||
372 (rhs_end >= start && rhs_end <= end);
373 }
const element * get() const
Returns a c-style pointer to the underlying array.

References get(), and size().

◆ size()

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

Friends And Related Function Documentation

◆ operator+ [1/2]

template<typename DataType >
template<typename T >
Array< OneD, T > operator+ ( const Array< OneD, T > &  lhs,
typename Array< OneD, T >::size_type  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:

result[0] == anArray[10];

◆ operator+ [2/2]

template<typename DataType >
template<typename T >
Array< OneD, T > operator+ ( typename Array< OneD, T >::size_type  offset,
const Array< OneD, T > &  rhs 
)
friend

Member Data Documentation

◆ m_capacity

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

Definition at line 413 of file SharedArray.hpp.

Referenced by operator=().

◆ m_count

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

Definition at line 419 of file SharedArray.hpp.

Referenced by operator=().

◆ m_data

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

Definition at line 414 of file SharedArray.hpp.

Referenced by operator=().

◆ m_offset

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

◆ m_size

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