35#ifndef NEKTAR_LIB_UTILITIES_BASIC_UTILS_SHARED_ARRAY_HPP
36#define NEKTAR_LIB_UTILITIES_BASIC_UTILS_SHARED_ARRAY_HPP
45#include <boost/core/ignore_unused.hpp>
46#include <boost/multi_array.hpp>
53template <
typename Dim,
typename DataType>
class Array;
57template <
typename DataType>
class Array<
OneD, const DataType>
63 void (*m_callback)(
void *);
83 m_pythonInfo(nullptr),
85 m_size(0), m_capacity(0), m_data(nullptr), m_count(nullptr),
99 m_pythonInfo(nullptr),
101 m_size(dim1Size), m_capacity(dim1Size), m_data(nullptr),
102 m_count(nullptr), m_offset(0)
120 m_pythonInfo(nullptr),
122 m_size(dim1Size), m_capacity(dim1Size), m_data(nullptr),
123 m_count(nullptr), m_offset(0)
140 m_pythonInfo(nullptr),
142 m_size(dim1Size), m_capacity(dim1Size), m_data(nullptr),
143 m_count(nullptr), m_offset(0)
162 m_pythonInfo(rhs.m_pythonInfo),
164 m_size(dim1Size), m_capacity(rhs.m_capacity), m_data(rhs.m_data),
165 m_count(rhs.m_count), m_offset(rhs.m_offset)
169 larger than input array size.");
178 Array(size_type dim1Size, DataType *data,
void *memory_pointer,
179 void (*python_decrement)(
void *))
180 : m_size(dim1Size), m_capacity(dim1Size), m_data(data),
181 m_count(nullptr), m_offset(0)
183 m_count =
new size_type();
186 m_pythonInfo =
new PythonInfo *();
187 *m_pythonInfo =
new PythonInfo();
188 (*m_pythonInfo)->m_callback = python_decrement;
189 (*m_pythonInfo)->m_pyObject = memory_pointer;
197 m_pythonInfo(rhs.m_pythonInfo),
199 m_size(rhs.m_size), m_capacity(rhs.m_capacity), m_data(rhs.m_data),
200 m_count(rhs.m_count), m_offset(rhs.m_offset)
207#if __GNUC__ == 12 && __GNUC_MINOR__ == 2
208#pragma GCC diagnostic push
209#pragma GCC diagnostic ignored "-Wuse-after-free"
213 if (m_count ==
nullptr)
222 if (*m_pythonInfo ==
nullptr)
229 (*m_pythonInfo)->m_callback((*m_pythonInfo)->m_pyObject);
230 delete *m_pythonInfo;
234 m_pythonInfo =
nullptr;
255 if (*m_pythonInfo ==
nullptr)
260 else if ((*rhs.m_pythonInfo) !=
nullptr &&
261 (*m_pythonInfo)->m_pyObject !=
262 (*rhs.m_pythonInfo)->m_pyObject)
264 (*m_pythonInfo)->m_callback((*m_pythonInfo)->m_pyObject);
265 delete *m_pythonInfo;
269 m_pythonInfo =
nullptr;
286 m_pythonInfo = rhs.m_pythonInfo;
290#if __GNUC__ == 12 && __GNUC_MINOR__ == 2
291#pragma GCC diagnostic pop
296 return m_data + m_offset;
300 return m_data + m_offset + m_size;
306 std::string(
"Element ") + std::to_string(i) +
307 std::string(
" requested in an array of size ") +
308 std::to_string(m_size));
309 return *(m_data + i + m_offset);
315 return m_data + m_offset;
321 return m_data + m_offset;
340 WARNINGL1(
false,
"member function num_elements() is deprecated, "
341 "use size() instead.");
367 const element *end = start + m_size;
372 return (rhs_start >= start && rhs_start <= end) ||
373 (rhs_end >= start && rhs_end <= end);
379 return *m_pythonInfo !=
nullptr;
382 void ToPythonArray(
void *memory_pointer,
void (*python_decrement)(
void *))
384 *m_pythonInfo =
new PythonInfo();
385 (*m_pythonInfo)->m_callback = python_decrement;
386 (*m_pythonInfo)->m_pyObject = memory_pointer;
400 template <
typename T>
404 template <
typename T>
410 PythonInfo **m_pythonInfo;
437 m_pythonInfo =
new PythonInfo *();
438 *m_pythonInfo =
nullptr;
442 template <
typename T>
447 result.m_offset += offset;
448 result.m_size = rhs.m_size - offset;
454template <
typename DataType>
class Array<
TwoD, const DataType>
462 typedef typename ArrayType::index
index;
480 m_data->num_elements());
487 m_data->data(), m_data->num_elements(), initValue);
494 m_data->data(), m_data->num_elements(), data);
510 return m_data->begin();
514 return m_data->end();
522 return m_data->data();
526 return m_data->data();
530 return m_data->num_dimensions();
534 return m_data->shape();
539 return m_data->num_elements();
544 WARNINGL1(
false,
"member function num_elements() is deprecated, "
545 "use size() instead.");
546 return m_data->num_elements();
551 return m_data->shape()[0];
555 return m_data->shape()[1];
581template <
typename DataType>
610 :
BaseType(dim1Size, data, WrapArray)
634 Array(size_type dim1Size, DataType *data,
void *memory_pointer,
635 void (*python_decrement)(
void *))
636 : BaseType(dim1Size, data, memory_pointer, python_decrement)
640 void ToPythonArray(
void *memory_pointer,
void (*python_decrement)(
void *))
642 BaseType::ToPythonArray(memory_pointer, python_decrement);
648 BaseType::operator=(rhs);
667 return this->m_data + this->m_offset;
673 return this->m_data + this->m_offset + this->m_size;
676 using BaseType::operator[];
680 std::string(
"Element ") + std::to_string(i) +
681 std::string(
" requested in an array of size ") +
682 std::to_string(this->size()));
689 return this->m_data + this->m_offset;
692 using BaseType::data;
695 return this->m_data + this->m_offset;
700 template <
typename T1,
typename T3>
friend class NekMatrix;
708 boost::ignore_unused(a);
713 ASSERTL1(newSize <= this->m_capacity,
714 "Can't change an array size to something larger than its "
716 this->m_size = newSize;
723template <
typename DataType>
744 :
BaseType(dim1Size, dim2Size, initValue)
749 :
BaseType(dim1Size, dim2Size, data)
759 BaseType::operator=(rhs);
763 using BaseType::begin;
766 return this->m_data->begin();
772 return this->m_data->end();
775 using BaseType::operator[];
778 return (*this->m_data)[i];
784 return this->m_data->data();
787 using BaseType::data;
790 return this->m_data->data();
798inline bool IsEqualImpl(
const T &lhs,
const T &rhs, std::false_type)
805inline bool IsEqualImpl(
const T &lhs,
const T &rhs, std::true_type)
810template <
typename T>
inline bool IsEqual(
const T &lhs,
const T &rhs)
812 return IsEqualImpl(lhs, rhs, std::is_floating_point<T>());
815template <
typename T1,
typename T2>
818 if (lhs.size() != rhs.size())
823 if (lhs.data() == rhs.data())
840template <
typename T1,
typename T2>
843 return !(lhs == rhs);
846template <
typename DataType>
854template <
typename DataType>
862template <
typename ConstDataType,
typename DataType>
866 if (dest.
size() != source.size())
871 std::copy(source.data(), source.data() + source.size(), dest.
data());
874template <
typename ConstDataType,
typename DataType>
879 if (dest.
size() != n)
912template <
typename T1,
typename T2>
915 if ((lhs.GetRows() != rhs.GetRows()) ||
916 (lhs.GetColumns() != rhs.GetColumns()))
921 if (lhs.data() == rhs.data())
931 if (!
IsEqual(lhs[i][j], rhs[i][j]))
941template <
typename T1,
typename T2>
944 return !(lhs == rhs);
947namespace LibUtilities
#define WARNINGL1(condition, msg)
#define ASSERTL0(condition, msg)
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
reference operator[](size_type i)
Array(size_type dim1Size, const Array< OneD, DataType > &rhs)
BaseType::reference reference
Array(size_type dim1Size, const DataType *data)
Array(size_type dim1Size)
Array(const Array< OneD, const DataType > &rhs)
static Array< OneD, DataType > CreateWithOffset(const Array< OneD, DataType > &rhs, unsigned int offset)
Array(size_type dim1Size, DataType *data, bool WrapArray)
BaseType::element element
Array(const Array< OneD, const DataType > &rhs, AllowWrappingOfConstArrays a)
Array< OneD, constDataType >::const_iterator begin() const
BaseType::size_type size_type
BaseType::iterator iterator
Array(const Array< OneD, DataType > &rhs)
Array< OneD, const DataType > BaseType
Array(size_type dim1Size, const DataType &initValue)
Array(size_type dim1Size, const Array< OneD, const DataType > &rhs)
Array< OneD, DataType > & operator=(const Array< OneD, DataType > &rhs)
void ChangeSize(size_type newSize)
1D Array of constant elements with garbage collection and bounds checking.
const DataType & const_reference
friend Array< OneD, T > operator+(const Array< OneD, T > &lhs, typename Array< OneD, T >::size_type offset)
Creates an array with a specified offset.
Array(size_type dim1Size, const Array< OneD, const DataType > &rhs)
Creates a 1D array that references rhs.
size_type GetCount() const
Returns the array's reference counter.
void CreateStorage(size_type size)
Array(size_type dim1Size, const DataType *data)
Creates a 1D array a copies data into it.
const_iterator end() const
Array()
Creates an empty array.
const_iterator begin() const
bool Overlaps(const Array< OneD, const DataType > &rhs) const
Returns true is this array and rhs overlap.
Array(size_type dim1Size)
Creates an array of size dim1Size.
friend Array< OneD, T > operator+(typename Array< OneD, T >::size_type offset, const Array< OneD, T > &rhs)
const_reference operator[](size_type i) const
Array< OneD, const DataType > & operator=(const Array< OneD, const DataType > &rhs)
Creates a reference to rhs.
const element * get() const
Returns a c-style pointer to the underlying array.
size_type capacity() const
Returns the array's capacity.
Array(const Array< OneD, const DataType > &rhs)
Creates a reference to rhs.
size_type size() const
Returns the array's size.
size_type num_dimensions() const
Returns 1.
DEPRECATED(5.1.0, size) size_type num_elements() const
Returns the array's size. Deprecated.
static Array< OneD, T > CreateWithOffset(const Array< OneD, T > &rhs, size_type offset)
size_type GetOffset() const
Returns the array's offset.
const DataType * const_iterator
Array(size_type dim1Size, const DataType &initValue)
Creates a 1D array with each element initialized to an initial value.
const element * data() const
Returns a c-style pointer to the underlying array.
BaseType::iterator iterator
Array(const Array< TwoD, DataType > &rhs)
Array(size_type dim1Size, size_type dim2Size, const DataType *data)
BaseType::reference reference
reference operator[](index i)
Array(size_type dim1Size, size_type dim2Size)
Array(size_type dim1Size, size_type dim2Size, const DataType &initValue)
BaseType::size_type size_type
Array< TwoD, DataType > & operator=(const Array< TwoD, DataType > &rhs)
BaseType::element element
Array< TwoD, const DataType > BaseType
2D array with garbage collection and bounds checking.
const element * get() const
Array< TwoD, const DataType > & operator=(const Array< TwoD, const DataType > &rhs)
const element * data() const
Array(size_type dim1Size, size_type dim2Size, const DataType &initValue)
ArrayType::size_type size_type
Array(size_type dim1Size, size_type dim2Size, const DataType *data)
const_iterator end() const
ArrayType::const_reference const_reference
size_type num_dimensions() const
const size_type * shape() const
ArrayType::element element
Array(size_type dim1Size, size_type dim2Size)
Constructs a 2 dimensional array. The elements of the array are not initialized.
ArrayType::iterator iterator
std::shared_ptr< ArrayType > m_data
ArrayType::const_iterator const_iterator
boost::multi_array_ref< DataType, 2 > ArrayType
size_type GetRows() const
Array(const Array< TwoD, const DataType > &rhs)
DEPRECATED(5.1.0, size) size_type num_elements() const
ArrayType::reference reference
const_iterator begin() const
const_reference operator[](index i) const
size_type GetColumns() const
static DataType * RawAllocate(size_t NumberOfElements)
Allocates a chunk of raw, uninitialized memory, capable of holding NumberOfElements objects.
static void RawDeallocate(DataType *array, size_t NumberOfElements)
Deallocates memory allocated from RawAllocate.
static std::vector< unsigned int > NullUnsignedIntVector
static std::vector< std::vector< NekDouble > > NullVectorNekDoubleVector
static std::vector< NekDouble > NullNekDoubleVector
bool IsRealEqual(T1 &&lhs, T2 &&rhs, const unsigned int factor=NekConstants::kNekFloatCompFact)
compare reals of same type with relative tolerance
The above copyright notice and this permission notice shall be included.
bool operator==(const Array< OneD, T1 > &lhs, const Array< OneD, T2 > &rhs)
AllowWrappingOfConstArrays
static Array< OneD, int > NullInt1DArray
Array< OneD, DataType > operator+(const Array< OneD, DataType > &lhs, typename Array< OneD, DataType >::size_type offset)
static Array< OneD, Array< OneD, NekDouble > > NullNekDoubleArrayOfArray
bool IsEqual(const T &lhs, const T &rhs)
static Array< OneD, Array< OneD, Array< OneD, NekDouble > > > NullNekDoubleTensorOfArray3D
bool IsEqualImpl(const T &lhs, const T &rhs, std::false_type)
void CopyArrayN(const Array< OneD, ConstDataType > &source, Array< OneD, DataType > &dest, typename Array< OneD, DataType >::size_type n)
static Array< OneD, NekDouble > NullNekDouble1DArray
std::shared_ptr< boost::multi_array_ref< DataType, Dim::Value > > CreateStorage(const ExtentListType &extent)
bool operator!=(const Array< OneD, T1 > &lhs, const Array< OneD, T2 > &rhs)
void CopyArray(const Array< OneD, ConstDataType > &source, Array< OneD, DataType > &dest)