16 std::cerr <<
"[matioCpp::StructArray::checkCompatibility] The variable type is not compatible with a struct array." <<
std::endl;
20 if (inputPtr->isComplex)
22 std::cerr <<
"[matioCpp::StructArray::checkCompatibility] Cannot use a complex variable into a non-complex one." <<
std::endl;
31 size_t emptyDimensions[] = {0, 0};
32 initializeVariable(
"unnamed_struct_array",
40 size_t emptyDimensions[] = {0, 0};
41 initializeVariable(name,
53 std::cerr <<
"[ERROR][matioCpp::StructArray::StructArray] Zero dimension detected." <<
std::endl;
58 initializeVariable(name,
73 if (totalElements != elements.
size())
75 std::cerr <<
"[ERROR][matioCpp::StructArray::StructArray] The size of elements vector does not match the provided dimensions. The total number is different." <<
std::endl;
81 if (!abort && elements.
size() > 0)
83 size_t firstNumberOfFields = elements[0].numberOfFields();
84 char *
const * firstFields = elements[0].getStructFields();
88 size_t innerIndex = 0;
90 for (
size_t i = 0; (i < totalElements) && !abort; ++i)
92 if (!elements[i].isValid())
94 std::cerr <<
"[ERROR][matioCpp::StructArray::StructArray] The element at index "<< i <<
" (0-based) is not valid." <<
std::endl;
99 if (elements[i].numberOfFields() != firstNumberOfFields)
101 std::cerr <<
"[ERROR][matioCpp::StructArray::StructArray] The element at index "<< i <<
" (0-based) has a number of fields different from the others. All Structs are supposed to have the same set of fields." <<
std::endl;
105 char *
const * otherFields = elements[i].getStructFields();
107 for (
size_t field = 0; (field < firstNumberOfFields) && !abort; ++field)
109 if (strcmp(firstFields[field], otherFields[field]) != 0)
111 std::cerr <<
"[ERROR][matioCpp::StructArray::StructArray] The element at index "<< i <<
" (0-based) has a set of fields different from the others. All Structs are supposed to have the same set of fields." <<
std::endl;
119 assert(innerIndex == totalElements * firstNumberOfFields);
123 initializeVariable(name,
126 vectorOfPointers.
data());
130 initializeVariable(name,
139 initializeVariable(name,
153 std::cerr <<
"[ERROR][matioCpp::StructArray::StructArray] Zero dimension detected." <<
std::endl;
158 initializeVariable(name,
176 fromOther(std::forward<StructArray>(other));
185 size_t emptyDimensions[] = {0, 0};
206 fromOther(std::forward<StructArray>(other));
215 totalElements *= dim;
218 if (totalElements != elements.
size())
220 std::cerr <<
"[ERROR][matioCpp::StructArray::fromVectorOfStructs] The size of elements vector does not match the provided dimensions. The total number is different." <<
std::endl;
225 if (elements.
size() > 0)
227 size_t firstNumberOfFields = elements[0].numberOfFields();
228 char *
const * firstFields = elements[0].getStructFields();
232 size_t innerIndex = 0;
234 for (
size_t i = 0; i < totalElements; ++i)
236 if (!elements[i].isValid())
238 std::cerr <<
"[ERROR][matioCpp::StructArray::fromVectorOfStructs] The element at index "<< i <<
" (0-based) is not valid." <<
std::endl;
242 if (elements[i].numberOfFields() != firstNumberOfFields)
244 std::cerr <<
"[ERROR][matioCpp::StructArray::fromVectorOfStructs] The element at index "<< i <<
" (0-based) has a number of fields different from the others. All Structs are supposed to have the same set of fields." <<
std::endl;
247 char *
const * otherFields = elements[i].getStructFields();
249 for (
size_t field = 0; field < firstNumberOfFields; ++field)
251 if (strcmp(firstFields[field], otherFields[field]) != 0)
253 std::cerr <<
"[ERROR][matioCpp::StructArray::fromVectorOfStructs] The element at index "<< i <<
" (0-based) has a set of fields different from the others. All Structs are supposed to have the same set of fields." <<
std::endl;
260 assert(innerIndex == totalElements * firstNumberOfFields);
263 return initializeVariable(name(),
266 vectorOfPointers.
data());
271 return initializeVariable(name(),
280 assert(dimensions().size() > 0 && numberOfElements() > 0 &&
"[matioCpp::StructArray::rawIndexFromIndices] The array is empty.");
281 assert(el.
size() > 0 == dimensions().size() > 0 &&
"[matioCpp::StructArray::rawIndexFromIndices] The input vector el should have the same number of dimensions of the array.");
282 assert(el[0] < dimensions()[0] &&
"[matioCpp::StructArray::rawIndexFromIndices] The required element is out of bounds.");
287 for (
size_t i = 0; i < el.
size(); ++i)
289 assert(el[i] < dimensions()[i] &&
"[matioCpp::StructArray::rawIndexFromIndices] The required element is out of bounds.");
290 index += el[i] * previousDimensionsFactorial;
291 previousDimensionsFactorial *= dimensions()[i];
299 el.
resize(dimensions().size());
301 if (rawIndex >= numberOfElements())
303 std::cerr <<
"[ERROR][matioCpp::StructArray::indicesFromRawIndex] rawIndex is greater than the number of elements." <<
std::endl;
307 size_t previousDimensionsFactorial = dimensions()[0];
311 for (
size_t i = 1; i < el.
size(); ++i)
313 el[i - 1] = previousDimensionsFactorial;
314 previousDimensionsFactorial *= dimensions()[i];
317 size_t remainder = rawIndex;
319 for (
size_t i = el.
size() - 1; i > 0; --i)
321 el[i] = remainder / el[i - 1];
322 remainder -= el[i] * el[i - 1];
331 return changeName(newName);
342 return getArrayNumberOfElements();
347 return getStructNumberOfFields();
353 char *
const * matvarOutput = getStructFields();
356 size_t numberOfFields = getStructNumberOfFields();
357 output.
reserve(numberOfFields);
358 for (
size_t i = 0; i < numberOfFields; ++i)
374 return getStructFieldIndex(field) < numberOfFields();
379 return getStructFieldIndex(field);
384 if (!addStructField(newField))
386 std::cerr <<
"[ERROR][matioCpp::StructArray::addField] Failed to add field " << newField <<
"." <<
std::endl;
395 if (!addField(field))
405 return setElement(rawIndexFromIndices(el), newValue);
410 assert(el < numberOfElements() &&
"The requested element is out of bounds.");
412 char *
const * arrayFields = getStructFields();
417 std::cerr <<
"[ERROR][matioCpp::StructArray::setElement] The input struct is supposed to have the same number of fields of the struct array." <<
std::endl;
421 for (
size_t i = 0; i < numberOfFields(); ++i)
423 if (strcmp(arrayFields[i], structFields[i]) != 0)
425 std::cerr <<
"[ERROR][matioCpp::StructArray::setElement] The field " << structFields[i] <<
" of the input struct is supposed to be " << arrayFields[i]
426 <<
". Cannot insert in a struct array a new field in a single element." <<
std::endl;
430 bool ok = setStructField(i, newValue(i), el);
433 std::cerr <<
"[ERROR][matioCpp::StructArray::setElement] Failed to set field " << structFields[i] <<
"." <<
std::endl;
455 assert(el < numberOfElements());
461 assert(el < numberOfElements());
479 assert(el < numberOfElements());
485 assert(el < numberOfElements());
static matvar_t * GetMatvarDuplicate(const matvar_t *inputPtr)
Get a duplicate of the input matvar pointer/.
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.
StructArray is a particular type of Variable specialized for array of structs.
bool fromVectorOfStructs(const std::vector< index_type > &dimensions, const std::vector< matioCpp::Struct > &elements)
Set from a vector of Variables.
bool addField(const std::string &newField)
Add a new field to all the structs.
void clear()
Clear the struct array.
index_type numberOfFields() const
Get the total number of fields in the struct.
Element operator()(const std::vector< index_type > &el)
Access specified element.
index_type rawIndexFromIndices(const std::vector< index_type > &el) const
Get the linear index corresponding to the provided indices.
bool setName(const std::string &newName)
Change the name of the Variable.
void resize(const std::vector< index_type > &newDimensions)
Resize the array.
StructArrayElement< true > ConstElement
Non-const version of StructArrayElement.
size_t getFieldIndex(const std::string &field) const
Get the index of the specified field in the struct by performing a linear search.
StructArray()
Const version of Element.
bool isFieldExisting(const std::string &field) const
Check if a field is existing It performs a linear search over the output of fields().
Element operator[](const std::vector< index_type > &el)
Access specified element.
StructArrayElement< false > Element
The type used for indices.
std::vector< std::string > fields() const
Get the list of fields.
bool indicesFromRawIndex(size_t rawIndex, std::vector< index_type > &el) const
Get the indices given the raw index.
~StructArray()
Destructor.
bool addFields(const std::vector< std::string > &newFields)
Add the fields to all the structs.
StructArray & operator=(const StructArray &other)
Assignement operator (copy) from another StructArray.
index_type numberOfElements() const
Get the total number of elements in the array.
bool setElement(const std::vector< index_type > &el, const matioCpp::Struct &newValue)
Set the element at the specified position.
Struct is a particular type of Variable specialized for structs.
index_type numberOfFields() const
Get the total number of fields in the struct.
The matioCpp::Variable class is the equivalent of matvar_t in matio.
char *const * getStructFields() const
Get the list of fields in the variable, considered as a struct.
matioCpp::VariableType variableType() const
Get the VariableType.
bool initializeVariable(const std::string &name, const VariableType &variableType, const ValueType &valueType, matioCpp::Span< const size_t > dimensions, void *data)
Initialize the variable.
T emplace_back(T... args)
VariableType
Define the type of variable.
ValueType
The list of types for an element of a certain variable type.