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 (m_count ==
nullptr)
215 if (*m_pythonInfo ==
nullptr)
222 (*m_pythonInfo)->m_callback((*m_pythonInfo)->m_pyObject);
223 delete *m_pythonInfo;
247 if (*m_pythonInfo ==
nullptr)
252 else if ((*rhs.m_pythonInfo) !=
nullptr &&
253 (*m_pythonInfo)->m_pyObject !=
254 (*rhs.m_pythonInfo)->m_pyObject)
256 (*m_pythonInfo)->m_callback((*m_pythonInfo)->m_pyObject);
257 delete *m_pythonInfo;
276 m_pythonInfo = rhs.m_pythonInfo;
283 return m_data + m_offset;
287 return m_data + m_offset + m_size;
293 std::string(
"Element ") + std::to_string(i) +
294 std::string(
" requested in an array of size ") +
295 std::to_string(m_size));
296 return *(m_data + i + m_offset);
302 return m_data + m_offset;
308 return m_data + m_offset;
327 WARNINGL1(
false,
"member function num_elements() is deprecated, "
328 "use size() instead.");
354 const element *end = start + m_size;
359 return (rhs_start >= start && rhs_start <= end) ||
360 (rhs_end >= start && rhs_end <= end);
366 return *m_pythonInfo !=
nullptr;
369 void ToPythonArray(
void *memory_pointer,
void (*python_decrement)(
void *))
371 *m_pythonInfo =
new PythonInfo();
372 (*m_pythonInfo)->m_callback = python_decrement;
373 (*m_pythonInfo)->m_pyObject = memory_pointer;
387 template <
typename T>
391 template <
typename T>
397 PythonInfo **m_pythonInfo;
438 m_pythonInfo =
new PythonInfo *();
439 *m_pythonInfo =
nullptr;
443 template <
typename T>
448 result.m_offset += offset;
449 result.m_size = rhs.m_size - offset;
455 template <
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];
581 template <
typename DataType>
629 Array(size_type dim1Size, DataType *data,
void *memory_pointer,
630 void (*python_decrement)(
void *))
631 : BaseType(dim1Size, data, memory_pointer, python_decrement)
635 void ToPythonArray(
void *memory_pointer,
void (*python_decrement)(
void *))
637 BaseType::ToPythonArray(memory_pointer, python_decrement);
643 BaseType::operator=(rhs);
662 return this->m_data + this->m_offset;
668 return this->m_data + this->m_offset + this->m_size;
671 using BaseType::operator[];
675 std::string(
"Element ") + std::to_string(i) +
676 std::string(
" requested in an array of size ") +
677 std::to_string(this->size()));
684 return this->m_data + this->m_offset;
687 using BaseType::data;
690 return this->m_data + this->m_offset;
695 template <
typename T1,
typename T3>
friend class NekMatrix;
703 boost::ignore_unused(a);
708 ASSERTL1(newSize <= this->m_capacity,
709 "Can't change an array size to something larger than its "
711 this->m_size = newSize;
718 template <
typename DataType>
739 :
BaseType(dim1Size, dim2Size, initValue)
744 :
BaseType(dim1Size, dim2Size, data)
754 BaseType::operator=(rhs);
758 using BaseType::begin;
761 return this->m_data->begin();
767 return this->m_data->end();
770 using BaseType::operator[];
773 return (*this->m_data)[i];
779 return this->m_data->data();
782 using BaseType::data;
785 return this->m_data->data();
792 template <
typename T>
793 inline bool IsEqualImpl(
const T &lhs,
const T &rhs, std::false_type)
799 template <
typename T>
800 inline bool IsEqualImpl(
const T &lhs,
const T &rhs, std::true_type)
805 template <
typename T>
inline bool IsEqual(
const T &lhs,
const T &rhs)
807 return IsEqualImpl(lhs, rhs, std::is_floating_point<T>());
810 template <
typename T1,
typename T2>
813 if (lhs.size() != rhs.size())
818 if (lhs.data() == rhs.data())
835 template <
typename T1,
typename T2>
838 return !(lhs == rhs);
841 template <
typename DataType>
849 template <
typename DataType>
857 template <
typename ConstDataType,
typename DataType>
861 if (dest.
size() != source.size())
866 std::copy(source.data(), source.data() + source.size(), dest.
data());
869 template <
typename ConstDataType,
typename DataType>
874 if (dest.
size() != n)
910 template <
typename T1,
typename T2>
913 if ((lhs.GetRows() != rhs.GetRows()) ||
914 (lhs.GetColumns() != rhs.GetColumns()))
919 if (lhs.data() == rhs.data())
929 if (!
IsEqual(lhs[i][j], rhs[i][j]))
939 template <
typename T1,
typename T2>
942 return !(lhs == rhs);
945 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)
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)