LibraryLink Utilities  3.0.1
Modern C++ wrapper over LibraryLink and WSTP
Tensor.h
Go to the documentation of this file.
1 
9 #ifndef LLU_CONTAINERS_TENSOR_H_
10 #define LLU_CONTAINERS_TENSOR_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 TypedTensor : public MArray<T> {
30  public:
31  using MArray<T>::MArray;
32  private:
37  T* getData() const noexcept override;
38 
40  virtual MTensor getInternal() const = 0;
41  };
42 
52  template<typename T>
53  class Tensor : public TypedTensor<T>, public GenericTensor {
54  public:
62  Tensor(std::initializer_list<T> v);
63 
69  template<class Container, typename = std::enable_if_t<is_iterable_container_with_matching_type_v<Container, T> && has_size_v<Container>>>
70  explicit Tensor(const Container& c) : Tensor(c, {static_cast<mint>(c.size())}) {}
71 
78  template<class Container, typename = std::enable_if_t<is_iterable_container_with_matching_type_v<Container, T>>>
79  Tensor(const Container& c, MArrayDimensions dims) : Tensor(std::begin(c), std::end(c), std::move(dims)) {}
80 
91  template<class InputIt, typename = enable_if_input_iterator<InputIt>>
92  Tensor(InputIt first, InputIt last);
93 
99  Tensor(T init, MArrayDimensions dims);
100 
109  template<class InputIt, typename = enable_if_input_iterator<InputIt>>
110  Tensor(InputIt first, InputIt last, MArrayDimensions dims);
111 
118  Tensor(MTensor t, Ownership owner);
119 
125  explicit Tensor(GenericTensor t);
126 
130  Tensor() = default;
131 
137  Tensor clone() const {
139  }
140  private:
141  using GenericBase = MContainer<MArgumentType::Tensor>;
142 
144  MTensor getInternal() const noexcept override {
145  return this->getContainer();
146  }
147  };
148 
149  template<typename T>
150  Tensor<T>::Tensor(std::initializer_list<T> v) : Tensor(std::begin(v), std::end(v), {static_cast<mint>(v.size())}) {}
151 
152  template<typename T>
153  template<class InputIt, typename>
154  Tensor<T>::Tensor(InputIt first, InputIt last) : Tensor(first, last, {static_cast<mint>(std::distance(first, last))}) {}
155 
156  template<typename T>
158  : TypedTensor<T>(std::move(dims)), GenericBase(TensorType<T>, this->rank(), this->dimensions().data()) {
159  std::fill(this->begin(), this->end(), init);
160  }
161 
162  template<typename T>
163  template<class InputIt, typename>
164  Tensor<T>::Tensor(InputIt first, InputIt last, MArrayDimensions dims)
165  : TypedTensor<T>(std::move(dims)), GenericBase(TensorType<T>, this->rank(), this->dimensions().data()) {
166  if (std::distance(first, last) != this->getFlattenedLength()) {
167  ErrorManager::throwException(ErrorName::TensorNewError, "Length of data range does not match specified dimensions");
168  }
169  std::copy(first, last, this->begin());
170  }
171 
172  template<typename T>
173  Tensor<T>::Tensor(GenericBase t) : TypedTensor<T>({t.getDimensions(), t.getRank()}), GenericBase(std::move(t)) {
174  if (TensorType<T> != GenericBase::type()) {
176  }
177  }
178 
179  template<typename T>
180  Tensor<T>::Tensor(MTensor t, Ownership owner) : Tensor(GenericBase {t, owner}) {}
181 
182 
183 } /* namespace LLU */
184 
185 #endif /* LLU_CONTAINERS_TENSOR_H_ */
LLU::Tensor::Tensor
Tensor(T init, MArrayDimensions dims)
Constructs the Tensor of given shape with all elements initialized to given value.
Definition: Tensor.h:157
LLU::TypedTensor
Typed interface for Tensor.
Definition: Tensor.h:29
LLU
Main namespace of LibraryLink Utilities.
Definition: Queue.h:13
LLU::TensorType
constexpr mint TensorType
Utility structure that matches a C++ type with a corresponding MTensor data type.
Definition: Utilities.hpp:295
LLU::ErrorName::TensorNewError
const std::string TensorNewError
creating new MTensor failed
LLU::Tensor::Tensor
Tensor(const Container &c, MArrayDimensions dims)
Constructs a Tensor with contents copied from a given collection of data and dimensions passed as par...
Definition: Tensor.h:79
LLU::Tensor::clone
Tensor clone() const
Clone this Tensor, performing a deep copy of the underlying MTensor.
Definition: Tensor.h:137
LibraryData.h
LLU::MArrayDimensions
Helper class that carries meta-information about container's size and dimensions.
Definition: MArrayDimensions.h:23
LLU::MContainerBase< MArgumentType::Tensor >::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::MArray
This is a class template, where template parameter T is the type of data elements....
Definition: MArray.hpp:36
LLU::Tensor
This is a class template, where template parameter T is the type of data elements....
Definition: Tensor.h:53
LLU::Tensor::Tensor
Tensor(MTensor t, Ownership owner)
Constructs Tensor based on MTensor.
Definition: Tensor.h:180
Tensor.hpp
GenericTensor definition and implementation.
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::MContainerBase< MArgumentType::Tensor >::getContainer
Container getContainer() const noexcept
Get internal container.
Definition: Base.hpp:97
LLU::MContainerBase< MArgumentType::Tensor >::cloneContainer
Container cloneContainer() const
Clone the raw container, if it's present.
Definition: Base.hpp:150
LLU::ErrorName::TensorTypeError
const std::string TensorTypeError
Tensor type mismatch.
LLU::Tensor::Tensor
Tensor()=default
Default constructor, creates a Tensor that does not wrap over any raw MTensor.
LLU::MContainer< MArgumentType::Tensor >::getRank
mint getRank() const override
Get rank.
Definition: Generic/Tensor.hpp:51
LLU::ErrorManager::throwException
static void throwException(const std::string &errorName, T &&... args)
Throw exception with given name.
Definition: ErrorManager.h:199
LLU::Tensor::Tensor
Tensor(GenericTensor t)
Create new Tensor from a GenericTensor.
Definition: Tensor.h:173
LLU::Tensor::Tensor
Tensor(InputIt first, InputIt last, MArrayDimensions dims)
Constructs the Tensor of given shape with elements from range [first, last)
Definition: Tensor.h:164
LLU::MContainer< MArgumentType::Tensor >
MContainer specialization for MTensor.
Definition: Generic/Tensor.hpp:24
LLU::IterableContainer::begin
iterator begin() noexcept
Get iterator at the beginning of underlying data.
Definition: IterableContainer.hpp:71
LLU::Tensor::Tensor
Tensor(InputIt first, InputIt last)
Constructs flat Tensor with elements from range [first, last)
Definition: Tensor.h:154
LLU::MContainer< MArgumentType::Tensor >::getDimensions
mint const * getDimensions() const override
Get dimensions.
Definition: Generic/Tensor.hpp:56
LLU::MContainer< MArgumentType::Tensor >::getFlattenedLength
mint getFlattenedLength() const override
Get total length.
Definition: Generic/Tensor.hpp:61
LLU::Ownership
Ownership
An enum listing possible owners of a LibraryLink container.
Definition: Base.hpp:22
Utilities.hpp
Short but generally useful functions.
LLU::Tensor::Tensor
Tensor(const Container &c)
Constructs flat Tensor with contents copied from a given collection of data.
Definition: Tensor.h:70
LLU::IterableContainer::end
iterator end() noexcept
Get iterator after the end of underlying data.
Definition: IterableContainer.hpp:92
LLU::Tensor::Tensor
Tensor(std::initializer_list< T > v)
Constructs flat Tensor based on a list of elements.
Definition: Tensor.h:150