8 #ifndef MATIOCPP_MULTIDIMENSIONALARRAY_TPP
9 #define MATIOCPP_MULTIDIMENSIONALARRAY_TPP
19 std::cerr <<
"[matioCpp::MultiDimensionalArray::checkCompatibility] The variable type is not compatible with a multidimensional array." <<
std::endl;
23 if (inputPtr->isComplex)
25 std::cerr <<
"[matioCpp::MultiDimensionalArray::checkCompatibility] Cannot use a complex variable into a non-complex one." <<
std::endl;
29 if (!matioCpp::is_convertible_to_primitive_type<T>(valueType))
36 std::cerr <<
"[matioCpp::MultiDimensionalArray::checkCompatibility] The value type is not convertible to " <<
37 get_type<T>::toString() <<
"." <<
std::endl <<
38 " Input class type: " << classType <<
std::endl <<
39 " Input data type: " << dataType <<
std::endl;
49 constexpr
size_t emptyDimensions[] = {0, 0, 0};
50 initializeVariable(
"unnamed_multidimensional_array",
51 VariableType::MultiDimensionalArray,
60 constexpr
size_t emptyDimensions[] = {0, 0, 0};
61 initializeVariable(name,
62 VariableType::MultiDimensionalArray,
75 std::cerr <<
"[ERROR][matioCpp::MultiDimensionalArray::MultiDimensionalArray] Zero dimension detected." <<
std::endl;
84 initializeVariable(name,
85 VariableType::MultiDimensionalArray,
97 std::cerr <<
"[ERROR][matioCpp::MultiDimensionalArray::MultiDimensionalArray] Zero dimension detected." <<
std::endl;
102 initializeVariable(name,
103 VariableType::MultiDimensionalArray,
128 constexpr
size_t emptyDimensions[] = {0, 0, 0};
132 (
void*)empty.
data());
163 std::cerr <<
"[ERROR][matioCpp::MultiDimensionalArray::fromVectorizedArray] Zero dimension detected." <<
std::endl;
168 return initializeVariable(name(),
177 assert(dimensions().size() > 0 && numberOfElements() > 0 &&
"[matioCpp::MultiDimensionalArray::rawIndexFromIndices] The array is empty.");
178 assert(el.size() > 0 == dimensions().size() > 0 &&
"[matioCpp::MultiDimensionalArray::rawIndexFromIndices] The input vector el should have the same number of dimensions of the array.");
179 assert(el[0] < dimensions()[0] &&
"[matioCpp::MultiDimensionalArray::rawIndexFromIndices] The required element is out of bounds.");
184 for (
size_t i = 0; i < el.size(); ++i)
186 assert(el[i] < dimensions()[i] &&
"[matioCpp::MultiDimensionalArray::rawIndexFromIndices] The required element is out of bounds.");
187 index += el[i] * previousDimensionsFactorial;
188 previousDimensionsFactorial *= dimensions()[i];
197 el.resize(dimensions().size());
199 if (rawIndex >= numberOfElements())
201 std::cerr <<
"[ERROR][matioCpp::MultiDimensionalArray::indicesFromRawIndex] rawIndex is greater than the number of elements." <<
std::endl;
209 for (
size_t i = 1; i < el.size(); ++i)
211 el[i - 1] = previousDimensionsFactorial;
212 previousDimensionsFactorial *= dimensions()[i];
217 for (
size_t i = el.size() - 1; i > 0; --i)
219 el[i] = remainder / el[i - 1];
220 remainder -= el[i] * el[i - 1];
242 return changeName(newName);
253 std::cerr <<
"[ERROR][matioCpp::MultiDimensionalArray::resize] Zero dimension detected." <<
std::endl;
257 totalElements *= dim;
262 initializeVariable(name(),
289 return getArrayNumberOfElements();
295 return data()[rawIndexFromIndices(el)];
301 return data()[rawIndexFromIndices(el)];
307 assert(el < numberOfElements() &&
"[matioCpp::MultiDimensionalArray::operator()] The required element is out of bounds.");
314 assert(el < numberOfElements() &&
"[matioCpp::MultiDimensionalArray::operator()] The required element is out of bounds.");
321 return data()[rawIndexFromIndices(el)];
327 return data()[rawIndexFromIndices(el)];
333 assert(el < numberOfElements() &&
"[matioCpp::MultiDimensionalArray::operator[]] The required element is out of bounds.");
340 assert(el < numberOfElements() &&
"[matioCpp::MultiDimensionalArray::operator[]] The required element is out of bounds.");
ValueType valueType() const
Get the value type of the pointer.
virtual matvar_t * get() const =0
Get the shared matvar_t pointer.
VariableType variableType() const
Get the variable type of the pointer.
MultiDimensionalArray is a particular type of Variable specialized for multidimensional arrays of a g...
index_type numberOfElements() const
Get the total number of elements in the array.
reference operator[](const std::vector< index_type > &el)
Access specified element.
matioCpp::Span< element_type > toSpan()
Get this MultiDimensionalArray as a Span.
~MultiDimensionalArray()
Destructor.
MultiDimensionalArray()
The const pointer type.
typename std::allocator_traits< std::allocator< element_type > >::const_pointer const_pointer
The pointer type.
MultiDimensionalArray< T > & operator=(const MultiDimensionalArray< T > &other)
Assignement operator (copy) from another MultiDimensionalArray.
bool setName(const std::string &newName)
Change the name of the Variable.
bool fromVectorizedArray(const std::vector< index_type > &dimensions, const_pointer inputVector)
Set from a vectorized array.
bool indicesFromRawIndex(size_t rawIndex, std::vector< index_type > &el) const
Get the indices given the raw index.
reference operator()(const std::vector< index_type > &el)
Access specified element.
void clear()
Clear the multidimensional array.
void resize(const std::vector< index_type > &newDimensions)
Resize the vector.
pointer data()
Direct access to the underlying array.
size_t index_type
Defines how to allocate T.
typename std::allocator_traits< std::allocator< element_type > >::pointer pointer
The reference type.
element_type & reference
The type used for indices.
std::remove_cv_t< element_type > value_type
Defines the type of an element of the MultiDimensionalArray.
index_type rawIndexFromIndices(const std::vector< index_type > &el) const
Get the index in the vectorized array corresponding to the provided indices.
The matioCpp::Variable class is the equivalent of matvar_t in matio.
matioCpp::MultiDimensionalArray< T > asMultiDimensionalArray()
Cast the variable as a MultiDimensionalArray.
bool initializeVariable(const std::string &name, const VariableType &variableType, const ValueType &valueType, matioCpp::Span< const size_t > dimensions, void *data)
Initialize the variable.
MATIOCPP_CONSTEXPR Span< ElementType > make_span(ElementType *ptr, typename Span< ElementType >::index_type count)
VariableType
Define the type of variable.
ValueType
The list of types for an element of a certain variable type.
bool get_types_names_from_matvart(const matvar_t *input, std::string &classType, std::string &dataType)
Get the type names from the input pointer.
Utility metafunction to get the ValueType from a given primitive type.