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>
53 template <
typename Dim,
typename DataType>
class Array;
57 template <
typename DataType>
class Array<
OneD, const DataType>
63 void (*m_callback)(
void *);
82 m_pythonInfo(nullptr),
84 m_size(0), m_capacity(0), m_data(nullptr), m_count(nullptr),
98 m_pythonInfo(nullptr),
100 m_size(dim1Size), m_capacity(dim1Size), m_data(nullptr),
101 m_count(nullptr), m_offset(0)
119 m_pythonInfo(nullptr),
121 m_size(dim1Size), m_capacity(dim1Size), m_data(nullptr),
122 m_count(nullptr), m_offset(0)
139 m_pythonInfo(nullptr),
141 m_size(dim1Size), m_capacity(dim1Size), m_data(nullptr),
142 m_count(nullptr), m_offset(0)
161 m_pythonInfo(rhs.m_pythonInfo),
163 m_size(dim1Size), m_capacity(rhs.m_capacity), m_data(rhs.m_data),
164 m_count(rhs.m_count), m_offset(rhs.m_offset)
168 larger than input array size.");
177 Array(size_type dim1Size, DataType *data,
void *memory_pointer,
178 void (*python_decrement)(
void *))
179 : m_size(dim1Size), m_capacity(dim1Size), m_data(data),
180 m_count(nullptr), m_offset(0)
182 m_count =
new size_type();
185 m_pythonInfo =
new PythonInfo *();
186 *m_pythonInfo =
new PythonInfo();
187 (*m_pythonInfo)->m_callback = python_decrement;
188 (*m_pythonInfo)->m_pyObject = memory_pointer;
196 m_pythonInfo(rhs.m_pythonInfo),
198 m_size(rhs.m_size), m_capacity(rhs.m_capacity), m_data(rhs.m_data),
199 m_count(rhs.m_count), m_offset(rhs.m_offset)
206 #if __GNUC__ == 12 && __GNUC_MINOR__ == 2
207 #pragma GCC diagnostic push
208 #pragma GCC diagnostic ignored "-Wuse-after-free"
212 if (m_count ==
nullptr)
221 if (*m_pythonInfo ==
nullptr)
228 (*m_pythonInfo)->m_callback((*m_pythonInfo)->m_pyObject);
229 delete *m_pythonInfo;
233 m_pythonInfo =
nullptr;
254 if (*m_pythonInfo ==
nullptr)
259 else if ((*rhs.m_pythonInfo) !=
nullptr &&
260 (*m_pythonInfo)->m_pyObject !=
261 (*rhs.m_pythonInfo)->m_pyObject)
263 (*m_pythonInfo)->m_callback((*m_pythonInfo)->m_pyObject);
264 delete *m_pythonInfo;
268 m_pythonInfo =
nullptr;
285 m_pythonInfo = rhs.m_pythonInfo;
289 #if __GNUC__ == 12 && __GNUC_MINOR__ == 2
290 #pragma GCC diagnostic pop
295 return m_data + m_offset;
299 return m_data + m_offset + m_size;
305 std::string(
"Element ") + std::to_string(i) +
306 std::string(
" requested in an array of size ") +
307 std::to_string(m_size));
308 return *(m_data + i + m_offset);
314 return m_data + m_offset;
320 return m_data + m_offset;
339 WARNINGL1(
false,
"member function num_elements() is deprecated, "
340 "use size() instead.");
366 const element *end = start + m_size;
371 return (rhs_start >= start && rhs_start <= end) ||
372 (rhs_end >= start && rhs_end <= end);
378 return *m_pythonInfo !=
nullptr;
381 void ToPythonArray(
void *memory_pointer,
void (*python_decrement)(
void *))
383 *m_pythonInfo =
new PythonInfo();
384 (*m_pythonInfo)->m_callback = python_decrement;
385 (*m_pythonInfo)->m_pyObject = memory_pointer;
399 template <
typename T>
403 template <
typename T>
409 PythonInfo **m_pythonInfo;
450 m_pythonInfo =
new PythonInfo *();
451 *m_pythonInfo =
nullptr;
455 template <
typename T>
460 result.m_offset += offset;
461 result.m_size = rhs.m_size - offset;
467 template <
typename DataType>
class Array<
TwoD, const DataType>
474 typedef typename ArrayType::index
index;
492 m_data->num_elements());
499 m_data->data(), m_data->num_elements(), initValue);
506 m_data->data(), m_data->num_elements(), data);
522 return m_data->begin();
526 return m_data->end();
534 return m_data->data();
538 return m_data->data();
542 return m_data->num_dimensions();
546 return m_data->shape();
551 return m_data->num_elements();
556 WARNINGL1(
false,
"member function num_elements() is deprecated, "
557 "use size() instead.");
558 return m_data->num_elements();
563 return m_data->shape()[0];
567 return m_data->shape()[1];
593 template <
typename DataType>
622 :
BaseType(dim1Size, data, WrapArray)
646 Array(size_type dim1Size, DataType *data,
void *memory_pointer,
647 void (*python_decrement)(
void *))
648 : BaseType(dim1Size, data, memory_pointer, python_decrement)
652 void ToPythonArray(
void *memory_pointer,
void (*python_decrement)(
void *))
654 BaseType::ToPythonArray(memory_pointer, python_decrement);
660 BaseType::operator=(rhs);
679 return this->m_data + this->m_offset;
685 return this->m_data + this->m_offset + this->m_size;
688 using BaseType::operator[];
692 std::string(
"Element ") + std::to_string(i) +
693 std::string(
" requested in an array of size ") +
694 std::to_string(this->size()));
701 return this->m_data + this->m_offset;
704 using BaseType::data;
707 return this->m_data + this->m_offset;
712 template <
typename T1,
typename T3>
friend class NekMatrix;
720 boost::ignore_unused(a);
725 ASSERTL1(newSize <= this->m_capacity,
726 "Can't change an array size to something larger than its "
728 this->m_size = newSize;
735 template <
typename DataType>
756 :
BaseType(dim1Size, dim2Size, initValue)
761 :
BaseType(dim1Size, dim2Size, data)
771 BaseType::operator=(rhs);
775 using BaseType::begin;
778 return this->m_data->begin();
784 return this->m_data->end();
787 using BaseType::operator[];
790 return (*this->m_data)[i];
796 return this->m_data->data();
799 using BaseType::data;
802 return this->m_data->data();
809 template <
typename T>
810 inline bool IsEqualImpl(
const T &lhs,
const T &rhs, std::false_type)
816 template <
typename T>
817 inline bool IsEqualImpl(
const T &lhs,
const T &rhs, std::true_type)
822 template <
typename T>
inline bool IsEqual(
const T &lhs,
const T &rhs)
824 return IsEqualImpl(lhs, rhs, std::is_floating_point<T>());
827 template <
typename T1,
typename T2>
830 if (lhs.size() != rhs.size())
835 if (lhs.data() == rhs.data())
852 template <
typename T1,
typename T2>
855 return !(lhs == rhs);
858 template <
typename DataType>
866 template <
typename DataType>
874 template <
typename ConstDataType,
typename DataType>
878 if (dest.
size() != source.size())
883 std::copy(source.data(), source.data() + source.size(), dest.
data());
886 template <
typename ConstDataType,
typename DataType>
891 if (dest.
size() != n)
927 template <
typename T1,
typename T2>
930 if ((lhs.GetRows() != rhs.GetRows()) ||
931 (lhs.GetColumns() != rhs.GetColumns()))
936 if (lhs.data() == rhs.data())
946 if (!
IsEqual(lhs[i][j], rhs[i][j]))
956 template <
typename T1,
typename T2>
959 return !(lhs == rhs);
962 namespace 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< OneD, const DataType >::const_iterator begin() const
Array(size_type dim1Size, const DataType *data)
Array(size_type dim1Size)
Array(const Array< OneD, const DataType > &rhs)
Array(size_type dim1Size, DataType *data, bool WrapArray)
BaseType::element element
Array(const Array< OneD, const DataType > &rhs, AllowWrappingOfConstArrays a)
static Array< OneD, DataType > CreateWithOffset(const Array< OneD, DataType > &rhs, unsigned int offset)
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.
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< OneD, const DataType > & operator=(const Array< OneD, const DataType > &rhs)
Creates a reference to rhs.
const element * data() const
Returns a c-style pointer to the underlying array.
static Array< OneD, T > CreateWithOffset(const Array< OneD, T > &rhs, size_type offset)
const DataType & const_reference
Array(size_type dim1Size, const Array< OneD, const DataType > &rhs)
Creates a 1D array that references rhs.
friend Array< OneD, T > operator+(typename Array< OneD, T >::size_type offset, const Array< OneD, T > &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.
const_reference operator[](size_type i) const
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.
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.
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< TwoD, DataType > & operator=(const Array< TwoD, DataType > &rhs)
Array(size_type dim1Size, size_type dim2Size)
Array(size_type dim1Size, size_type dim2Size, const DataType &initValue)
BaseType::size_type size_type
BaseType::element element
Array< TwoD, const DataType > BaseType
2D array with garbage collection and bounds checking.
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
Array< TwoD, const DataType > & operator=(const Array< TwoD, const DataType > &rhs)
ArrayType::const_reference const_reference
size_type num_dimensions() 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 size_type * shape() const
const_iterator begin() const
const_reference operator[](index i) const
size_type GetColumns() const
const element * get() const
static void RawDeallocate(DataType *array, size_t NumberOfElements)
Deallocates memory allocated from RawAllocate.
static DataType * RawAllocate(size_t NumberOfElements)
Allocates a chunk of raw, uninitialized memory, capable of holding NumberOfElements objects.
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.
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)
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
bool operator!=(const Array< OneD, T1 > &lhs, const Array< OneD, T2 > &rhs)
void CopyArray(const Array< OneD, ConstDataType > &source, Array< OneD, DataType > &dest)