1#ifndef PRESSIO_DATA_CPP_H
2#define PRESSIO_DATA_CPP_H
14#include "std_compat/utility.h"
15#include "std_compat/optional.h"
27 return [](
void* data,
void*){
28 T* data_t =
static_cast<T*
>(data);
154 void*
data =
nullptr;
156 data = malloc(bytes);
157 memcpy(
data, src, bytes);
173 void*
data =
nullptr;
174 if(bytes != 0)
data = malloc(bytes);
211 unsigned char*
data =
nullptr;
212 if(bytes != 0 && src.
data() !=
nullptr) {
213 data =
static_cast<unsigned char*
>(malloc(bytes));
228 metadata_ptr(nullptr),
235 if(deleter!=
nullptr) deleter(data_ptr,metadata_ptr);
243 if(
this == &rhs)
return *
this;
244 data_dtype = rhs.data_dtype;
246 if(deleter !=
nullptr) deleter(data_ptr, metadata_ptr);
252 metadata_ptr =
nullptr;
263 data_dtype(rhs.data_dtype),
265 metadata_ptr(nullptr),
281 data_dtype(rhs.data_dtype),
282 data_ptr(compat::exchange(rhs.data_ptr,
nullptr)),
283 metadata_ptr(compat::exchange(rhs.metadata_ptr,
nullptr)),
284 deleter(compat::exchange(rhs.deleter,
nullptr)),
285 dims(compat::exchange(rhs.dims, {})),
286 capacity(compat::exchange(rhs.capacity, 0))
296 if(
this==&rhs)
return *
this;
297 if(deleter!=
nullptr) deleter(data_ptr,metadata_ptr);
298 data_dtype = rhs.data_dtype,
299 data_ptr = compat::exchange(rhs.data_ptr,
nullptr),
300 metadata_ptr = compat::exchange(rhs.metadata_ptr,
nullptr),
301 deleter = compat::exchange(rhs.deleter,
nullptr),
302 dims = compat::exchange(rhs.dims, {});
303 capacity = compat::exchange(rhs.capacity, 0);
315 data_ptr((il.size() == 0)? nullptr:malloc(il.size() * sizeof(T))),
316 metadata_ptr(nullptr),
320 std::copy(std::begin(il), std::end(il),
static_cast<T*
>(data_ptr));
374 std::vector<size_t>
normalized_dims(compat::optional<size_t> n={},
size_t fill=0)
const;
389 void* tmp = malloc(new_size);
394 if(deleter!=
nullptr) deleter(data_ptr,metadata_ptr);
398 metadata_ptr =
nullptr;
402 this->dims = std::move(dims);
412 else return dims[idx];
448 std::vector<size_t>
const& stride = {},
449 std::vector<size_t>
const& count = {},
450 std::vector<size_t>
const& block = {})
const;
463 int reshape(std::vector<size_t>
const& new_dimensions) {
467 dims = new_dimensions;
469 if(old_size == new_size) {
471 }
else if (old_size > new_size){
485 if(pressio_dtype_from_type<T>() ==
dtype()) {
488 auto casted =
cast(pressio_dtype_from_type<T>());
489 return std::vector<T>(
static_cast<T*
>(casted.data()),
static_cast<T*
>(casted.data()) + casted.num_elements());
499 template <
class ForwardIt>
503 metadata_ptr(nullptr),
505 dims({
static_cast<size_t>(std::distance(begin, end))})
507 using out_t =
typename std::add_pointer<
typename std::decay<
508 typename std::iterator_traits<ForwardIt>::value_type>::type>::type;
510 std::copy(begin, end,
static_cast<out_t
>(data_ptr));
538 void (*deleter)(
void*,
void*),
544 metadata_ptr(metadata),
561 void (*deleter)(
void*,
void*),
568 metadata_ptr(metadata),
576 void (*deleter)(
void*,
void*);
577 std::vector<size_t> dims;
588template <
class ReturnType,
class Function>
594 return std::forward<Function>(f)(
595 static_cast<double*
>(data.
data()),
599 return std::forward<Function>(f)(
600 static_cast<float*
>(data.
data()),
604 return std::forward<Function>(f)(
605 static_cast<uint8_t*
>(data.
data()),
609 return std::forward<Function>(f)(
610 static_cast<uint16_t*
>(data.
data()),
614 return std::forward<Function>(f)(
615 static_cast<uint32_t*
>(data.
data()),
619 return std::forward<Function>(f)(
620 static_cast<uint64_t*
>(data.
data()),
624 return std::forward<Function>(f)(
625 static_cast<int8_t*
>(data.
data()),
629 return std::forward<Function>(f)(
630 static_cast<int16_t*
>(data.
data()),
634 return std::forward<Function>(f)(
635 static_cast<int32_t*
>(data.
data()),
639 return std::forward<Function>(f)(
640 static_cast<int64_t*
>(data.
data()),
645 return std::forward<Function>(f)(
646 static_cast<unsigned char*
>(data.
data()),
659template <
class ReturnType,
class Function>
665 return std::forward<Function>(f)(
666 static_cast<double*
>(data.
data()),
670 return std::forward<Function>(f)(
671 static_cast<float*
>(data.
data()),
675 return std::forward<Function>(f)(
676 static_cast<uint8_t*
>(data.
data()),
680 return std::forward<Function>(f)(
681 static_cast<uint16_t*
>(data.
data()),
685 return std::forward<Function>(f)(
686 static_cast<uint32_t*
>(data.
data()),
690 return std::forward<Function>(f)(
691 static_cast<uint64_t*
>(data.
data()),
695 return std::forward<Function>(f)(
696 static_cast<int8_t*
>(data.
data()),
700 return std::forward<Function>(f)(
701 static_cast<int16_t*
>(data.
data()),
705 return std::forward<Function>(f)(
706 static_cast<int32_t*
>(data.
data()),
710 return std::forward<Function>(f)(
711 static_cast<int64_t*
>(data.
data()),
716 return std::forward<Function>(f)(
717 static_cast<unsigned char*
>(data.
data()),
724 template <
class ReturnType,
class Function,
class Type1,
class Type2>
726 return std::forward<Function>(f)(
727 static_cast<Type1*
>(data.
data()),
729 static_cast<Type2*
>(data2.
data()),
733 template <
class ReturnType,
class Function,
class Type1>
735 switch(data2.
dtype()) {
737 return pressio_data_for_each_call<ReturnType, Function, Type1, double>(data, data2, std::forward<Function>(f));
739 return pressio_data_for_each_call<ReturnType, Function, Type1, float>(data, data2, std::forward<Function>(f));
741 return pressio_data_for_each_call<ReturnType, Function, Type1, uint8_t>(data, data2, std::forward<Function>(f));
743 return pressio_data_for_each_call<ReturnType, Function, Type1, uint16_t>(data, data2, std::forward<Function>(f));
745 return pressio_data_for_each_call<ReturnType, Function, Type1, uint32_t>(data, data2, std::forward<Function>(f));
747 return pressio_data_for_each_call<ReturnType, Function, Type1, uint64_t>(data, data2, std::forward<Function>(f));
749 return pressio_data_for_each_call<ReturnType, Function, Type1, int8_t>(data, data2, std::forward<Function>(f));
751 return pressio_data_for_each_call<ReturnType, Function, Type1, int16_t>(data, data2, std::forward<Function>(f));
753 return pressio_data_for_each_call<ReturnType, Function, Type1, int32_t>(data, data2, std::forward<Function>(f));
755 return pressio_data_for_each_call<ReturnType, Function, Type1, int64_t>(data, data2, std::forward<Function>(f));
757 return pressio_data_for_each_call<ReturnType, Function, Type1, char>(data, data2, std::forward<Function>(f));
760 template <
class ReturnType,
class Function,
class Type1,
class Type2>
762 return std::forward<Function>(f)(
763 static_cast<Type1*
>(data.
data()),
765 static_cast<Type2*
>(data2.
data())
768 template <
class ReturnType,
class Function,
class Type1>
770 switch(data2.
dtype()) {
772 return pressio_data_for_each_call<ReturnType, Function, Type1, double>(data, data2, std::forward<Function>(f));
774 return pressio_data_for_each_call<ReturnType, Function, Type1, float>(data, data2, std::forward<Function>(f));
776 return pressio_data_for_each_call<ReturnType, Function, Type1, uint8_t>(data, data2, std::forward<Function>(f));
778 return pressio_data_for_each_call<ReturnType, Function, Type1, uint16_t>(data, data2, std::forward<Function>(f));
780 return pressio_data_for_each_call<ReturnType, Function, Type1, uint32_t>(data, data2, std::forward<Function>(f));
782 return pressio_data_for_each_call<ReturnType, Function, Type1, uint64_t>(data, data2, std::forward<Function>(f));
784 return pressio_data_for_each_call<ReturnType, Function, Type1, int8_t>(data, data2, std::forward<Function>(f));
786 return pressio_data_for_each_call<ReturnType, Function, Type1, int16_t>(data, data2, std::forward<Function>(f));
788 return pressio_data_for_each_call<ReturnType, Function, Type1, int32_t>(data, data2, std::forward<Function>(f));
790 return pressio_data_for_each_call<ReturnType, Function, Type1, int64_t>(data, data2, std::forward<Function>(f));
792 return pressio_data_for_each_call<ReturnType, Function, Type1, char>(data, data2, std::forward<Function>(f));
806template <
class ReturnType,
class Function>
809 switch(data.
dtype()) {
811 return pressio_data_for_each_type2_switch<ReturnType, Function, double>(data, data2, std::forward<Function>(f));
813 return pressio_data_for_each_type2_switch<ReturnType, Function, float>(data, data2, std::forward<Function>(f));
815 return pressio_data_for_each_type2_switch<ReturnType, Function, uint8_t>(data, data2, std::forward<Function>(f));
817 return pressio_data_for_each_type2_switch<ReturnType, Function, uint16_t>(data, data2, std::forward<Function>(f));
819 return pressio_data_for_each_type2_switch<ReturnType, Function, uint32_t>(data, data2, std::forward<Function>(f));
821 return pressio_data_for_each_type2_switch<ReturnType, Function, uint64_t>(data, data2, std::forward<Function>(f));
823 return pressio_data_for_each_type2_switch<ReturnType, Function, int8_t>(data, data2, std::forward<Function>(f));
825 return pressio_data_for_each_type2_switch<ReturnType, Function, int16_t>(data, data2, std::forward<Function>(f));
827 return pressio_data_for_each_type2_switch<ReturnType, Function, int32_t>(data, data2, std::forward<Function>(f));
829 return pressio_data_for_each_type2_switch<ReturnType, Function, int64_t>(data, data2, std::forward<Function>(f));
831 return pressio_data_for_each_type2_switch<ReturnType, Function, char>(data, data2, std::forward<Function>(f));
842template <
class ReturnType,
class Function>
845 switch(data.
dtype()) {
847 return pressio_data_for_each_type2_switch<ReturnType, Function, double>(data, data2, std::forward<Function>(f));
849 return pressio_data_for_each_type2_switch<ReturnType, Function, float>(data, data2, std::forward<Function>(f));
851 return pressio_data_for_each_type2_switch<ReturnType, Function, uint8_t>(data, data2, std::forward<Function>(f));
853 return pressio_data_for_each_type2_switch<ReturnType, Function, uint16_t>(data, data2, std::forward<Function>(f));
855 return pressio_data_for_each_type2_switch<ReturnType, Function, uint32_t>(data, data2, std::forward<Function>(f));
857 return pressio_data_for_each_type2_switch<ReturnType, Function, uint64_t>(data, data2, std::forward<Function>(f));
859 return pressio_data_for_each_type2_switch<ReturnType, Function, int8_t>(data, data2, std::forward<Function>(f));
861 return pressio_data_for_each_type2_switch<ReturnType, Function, int16_t>(data, data2, std::forward<Function>(f));
863 return pressio_data_for_each_type2_switch<ReturnType, Function, int32_t>(data, data2, std::forward<Function>(f));
865 return pressio_data_for_each_type2_switch<ReturnType, Function, int64_t>(data, data2, std::forward<Function>(f));
867 return pressio_data_for_each_type2_switch<ReturnType, Function, char>(data, data2, std::forward<Function>(f));
size_t data_size_in_bytes(pressio_dtype type, size_t const dimensions, size_t const dims[])
pressio_data_delete_fn pressio_new_free_fn()
Definition: data.h:26
ReturnType pressio_data_for_each(pressio_data const &data, Function &&f)
Definition: data.h:589
size_t data_size_in_elements(size_t dimensions, size_t const dims[])
C++ interface to data types.
constexpr pressio_dtype pressio_dtype_from_type()
Definition: dtype.h:42
an abstraction for a contagious memory region of a specified type
void pressio_data_libc_free_fn(void *data, void *metadata)
void(* pressio_data_delete_fn)(void *data, void *metadata)
Definition: pressio_data.h:26
pressio_dtype
Definition: pressio_dtype.h:16
@ pressio_byte_dtype
Definition: pressio_dtype.h:27
@ pressio_double_dtype
Definition: pressio_dtype.h:17
@ pressio_int16_dtype
Definition: pressio_dtype.h:24
@ pressio_uint64_dtype
Definition: pressio_dtype.h:22
@ pressio_float_dtype
Definition: pressio_dtype.h:18
@ pressio_uint16_dtype
Definition: pressio_dtype.h:20
@ pressio_int64_dtype
Definition: pressio_dtype.h:26
@ pressio_int8_dtype
Definition: pressio_dtype.h:23
@ pressio_uint8_dtype
Definition: pressio_dtype.h:19
@ pressio_uint32_dtype
Definition: pressio_dtype.h:21
@ pressio_int32_dtype
Definition: pressio_dtype.h:25
int pressio_dtype_size(enum pressio_dtype dtype)
static pressio_data nonowning(const pressio_dtype dtype, void *data, size_t const num_dimensions, size_t const dimensions[])
Definition: data.h:138
static pressio_data nonowning(const pressio_dtype dtype, void *data, std::vector< size_t > const &dimensions)
Definition: data.h:74
static pressio_data owning(const pressio_dtype dtype, std::vector< size_t > const &dimensions)
Definition: data.h:93
std::vector< T > to_vector() const
Definition: data.h:484
void * data() const
Definition: data.h:327
static pressio_data clone(pressio_data const &src)
Definition: data.h:209
size_t capacity_in_bytes() const
Definition: data.h:425
static pressio_data move(const pressio_dtype dtype, void *data, size_t const num_dimensions, size_t const dimensions[], pressio_data_delete_fn deleter, void *metadata)
Definition: data.h:191
pressio_data(pressio_data &&rhs) noexcept
Definition: data.h:280
bool has_data() const
Definition: data.h:334
int reshape(std::vector< size_t > const &new_dimensions)
Definition: data.h:463
pressio_dtype dtype() const
Definition: data.h:341
static pressio_data empty(const pressio_dtype dtype, std::vector< size_t > const &dimensions)
Definition: data.h:62
pressio_data transpose(std::vector< size_t > const &axis={}) const
size_t num_dimensions() const
Definition: data.h:360
size_t size_in_bytes() const
Definition: data.h:418
bool operator==(pressio_data const &rhs) const
size_t num_elements() const
Definition: data.h:432
pressio_data(ForwardIt begin, ForwardIt end)
Definition: data.h:500
void set_dtype(pressio_dtype dtype)
Definition: data.h:348
size_t set_dimensions(std::vector< size_t > &&dims)
Definition: data.h:386
std::vector< size_t > normalized_dims(compat::optional< size_t > n={}, size_t fill=0) const
pressio_data(pressio_data const &rhs)
Definition: data.h:262
std::vector< size_t > const & dimensions() const
Definition: data.h:367
pressio_data cast(pressio_dtype dtype) const
static pressio_data empty(const pressio_dtype dtype, size_t const num_dimensions, size_t const dimensions[])
Definition: data.h:124
size_t get_dimension(size_t idx) const
Definition: data.h:410
static pressio_data copy(const enum pressio_dtype dtype, const void *src, std::vector< size_t > const &dimensions)
Definition: data.h:82
pressio_data(std::initializer_list< T > il)
Definition: data.h:313
pressio_data & operator=(pressio_data const &rhs)
Definition: data.h:242
static pressio_data move(const pressio_dtype dtype, void *data, std::vector< size_t > const &dimensions, pressio_data_delete_fn deleter, void *metadata)
Definition: data.h:107
pressio_data & operator=(pressio_data &&rhs) noexcept
Definition: data.h:295
pressio_data select(std::vector< size_t > const &start={}, std::vector< size_t > const &stride={}, std::vector< size_t > const &count={}, std::vector< size_t > const &block={}) const
static pressio_data owning(const pressio_dtype dtype, size_t const num_dimensions, size_t const dimensions[])
Definition: data.h:171
static pressio_data copy(const enum pressio_dtype dtype, const void *src, size_t const num_dimensions, size_t const dimensions[])
Definition: data.h:152