LibraryLink Utilities  3.0.1
Modern C++ wrapper over LibraryLink and WSTP
NumericArray.h
Go to the documentation of this file.
1 
9 #ifndef LLU_CONTAINERS_NUMERICARRAY_H_
10 #define LLU_CONTAINERS_NUMERICARRAY_H_
11 
12 #include <initializer_list>
13 #include <type_traits>
14 
17 #include "LLU/LibraryData.h"
18 #include "LLU/Utilities.hpp"
19 
20 namespace LLU {
21 
28  template<typename T>
29  class TypedNumericArray : public MArray<T> {
30  public:
31  using MArray<T>::MArray;
32  private:
37  T* getData() const noexcept override {
38  return static_cast<T*>(LibraryData::NumericArrayAPI()->MNumericArray_getData(this->getInternal()));
39  }
40 
41  virtual MNumericArray getInternal() const = 0;
42  };
43 
53  template<typename T>
55  public:
63  NumericArray(std::initializer_list<T> v);
64 
70  template<class Container, typename = std::enable_if_t<is_iterable_container_with_matching_type_v<Container, T> && has_size_v<Container>>>
71  explicit NumericArray(const Container& c) : NumericArray(c, {static_cast<mint>(c.size())}) {}
72 
79  template<class Container, typename = std::enable_if_t<is_iterable_container_with_matching_type_v<Container, T>>>
80  NumericArray(const Container& c, MArrayDimensions dims) : NumericArray(std::begin(c), std::end(c), std::move(dims)) {}
81 
92  template<class InputIt, typename = enable_if_input_iterator<InputIt>>
93  NumericArray(InputIt first, InputIt last);
94 
100  NumericArray(T init, MArrayDimensions dims);
101 
111  template<class InputIt, typename = enable_if_input_iterator<InputIt>>
112  NumericArray(InputIt first, InputIt last, MArrayDimensions dims);
113 
120  NumericArray(MNumericArray na, Ownership owner);
121 
128  explicit NumericArray(GenericNumericArray na);
129 
136  explicit NumericArray(const GenericNumericArray& other, NA::ConversionMethod method, double param = 0.0);
137 
141  NumericArray() = default;
142 
148  NumericArray clone() const {
150  }
151 
152  private:
153  using GenericBase = GenericNumericArray;
154 
155  MNumericArray getInternal() const noexcept override {
156  return this->getContainer();
157  }
158  };
159 
160  template<typename T>
161  NumericArray<T>::NumericArray(std::initializer_list<T> v) : NumericArray(std::begin(v), std::end(v), {static_cast<mint>(v.size())}) {}
162 
163  template<typename T>
164  template<class InputIt, typename>
165  NumericArray<T>::NumericArray(InputIt first, InputIt last) : NumericArray(first, last, {static_cast<mint>(std::distance(first, last))}) {}
166 
167  template<typename T>
169  : TypedNumericArray<T>(std::move(dims)), GenericBase(NumericArrayType<T>, this->rank(), this->dimensions().data()) {
170  std::fill(this->begin(), this->end(), init);
171  }
172 
173  template<typename T>
174  template<class InputIt, typename>
175  NumericArray<T>::NumericArray(InputIt first, InputIt last, MArrayDimensions dims)
176  : TypedNumericArray<T>(std::move(dims)), GenericBase(NumericArrayType<T>, this->rank(), this->dimensions().data()) {
177  if (std::distance(first, last) != this->getFlattenedLength()) {
178  ErrorManager::throwException(ErrorName::NumericArrayNewError, "Length of data range does not match specified dimensions");
179  }
180  std::copy(first, last, this->begin());
181  }
182 
183  template<typename T>
184  NumericArray<T>::NumericArray(GenericBase na) : TypedNumericArray<T>({na.getDimensions(), na.getRank()}), GenericBase(std::move(na)) {
185  if (NumericArrayType<T> != GenericBase::type()) {
187  }
188  }
189 
190  template<typename T>
191  NumericArray<T>::NumericArray(MNumericArray na, Ownership owner) : NumericArray(GenericBase {na, owner}) {}
192 
193  template<typename T>
195  : TypedNumericArray<T>({other.getDimensions(), other.getRank()}), GenericBase(other.convert(NumericArrayType<T>, method, param)) {}
196 
197 } /* namespace LLU */
198 
199 #endif /* LLU_CONTAINERS_NUMERICARRAY_H_ */
LLU::NumericArray
This is a class template, where template parameter T is the type of data elements....
Definition: NumericArray.h:54
LLU::MContainer< MArgumentType::NumericArray >
MContainer specialization for MNumericArray.
Definition: Generic/NumericArray.hpp:23
LLU::GenericNumericArray
MContainer< MArgumentType::NumericArray > GenericNumericArray
MContainer specialization for MNumericArray is called GenericNumericArray.
Definition: Generic/NumericArray.hpp:17
LLU
Main namespace of LibraryLink Utilities.
Definition: Queue.h:13
LLU::MContainer< MArgumentType::NumericArray >::getRank
mint getRank() const override
Get rank.
Definition: Generic/NumericArray.hpp:62
LibraryData.h
LLU::NA::ConversionMethod
ConversionMethod
Possible methods of handling out-of-range data when converting a NumericArray to different type.
Definition: Utilities.hpp:207
LLU::MArrayDimensions
Helper class that carries meta-information about container's size and dimensions.
Definition: MArrayDimensions.h:23
LLU::NumericArray::clone
NumericArray clone() const
Clone this NumericArray, performing a deep copy of the underlying MNumericArray.
Definition: NumericArray.h:148
LLU::MContainerBase< MArgumentType::NumericArray >::Container
Argument::CType< Type > Container
The type of underlying LibraryLink structure (e.g. MTensor, MImage, etc.) will be called "Container".
Definition: Base.hpp:38
LLU::ErrorName::NumericArrayNewError
const std::string NumericArrayNewError
creating new NumericArray failed
LLU::NumericArray::NumericArray
NumericArray(const Container &c, MArrayDimensions dims)
Constructs a NumericArray with contents copied from a given collection of data and dimensions passed ...
Definition: NumericArray.h:80
LLU::MArray
This is a class template, where template parameter T is the type of data elements....
Definition: MArray.hpp:36
LLU::NumericArray::NumericArray
NumericArray()=default
Default constructor, creates a "hollow" NumericArray that does not have underlying MNumericArray.
LLU::ErrorName::NumericArrayTypeError
const std::string NumericArrayTypeError
NumericArray type mismatch.
LLU::Ownership::Library
@ Library
The library (LLU) is responsible for managing the container's memory. Used for Manual passing and con...
MArray.hpp
Template base class for C++ wrappers of LibraryLink containers.
LLU::TypedNumericArray
Typed interface for NumericArray.
Definition: NumericArray.h:29
LLU::MContainerBase< MArgumentType::NumericArray >::getContainer
Container getContainer() const noexcept
Get internal container.
Definition: Base.hpp:97
LLU::MContainerBase< MArgumentType::NumericArray >::cloneContainer
Container cloneContainer() const
Clone the raw container, if it's present.
Definition: Base.hpp:150
LLU::ErrorManager::throwException
static void throwException(const std::string &errorName, T &&... args)
Throw exception with given name.
Definition: ErrorManager.h:199
LLU::MContainer< MArgumentType::NumericArray >::getFlattenedLength
mint getFlattenedLength() const override
Get length.
Definition: Generic/NumericArray.hpp:72
LLU::MContainer< MArgumentType::NumericArray >::getDimensions
mint const * getDimensions() const override
Get dimensions.
Definition: Generic/NumericArray.hpp:67
LLU::LibraryData::NumericArrayAPI
static const st_WolframNumericArrayLibrary_Functions * NumericArrayAPI()
Get a pointer to structure with function pointers to MNumericArray API.
Definition: LibraryData.cpp:33
LLU::NumericArray::NumericArray
NumericArray(const Container &c)
Constructs flat NumericArray with contents copied from a given collection of data.
Definition: NumericArray.h:71
LLU::IterableContainer::begin
iterator begin() noexcept
Get iterator at the beginning of underlying data.
Definition: IterableContainer.hpp:71
LLU::NumericArrayType
constexpr numericarray_data_t NumericArrayType
Utility structure that matches a C++ type with a corresponding MNumericArray data type.
Definition: Utilities.hpp:265
LLU::Ownership
Ownership
An enum listing possible owners of a LibraryLink container.
Definition: Base.hpp:22
Utilities.hpp
Short but generally useful functions.
NumericArray.hpp
GenericNumericArray definition and implementation.
LLU::IterableContainer::end
iterator end() noexcept
Get iterator after the end of underlying data.
Definition: IterableContainer.hpp:92