14 std::cerr <<
"[matioCpp::CellArray::checkCompatibility] The variable type is not compatible with a cell array." <<
std::endl;
18 if (inputPtr->isComplex)
20 std::cerr <<
"[matioCpp::CellArray::checkCompatibility] Cannot use a complex variable into a non-complex one." <<
std::endl;
29 size_t emptyDimensions[] = {0, 0};
30 initializeVariable(
"unnamed_cell_array",
38 size_t emptyDimensions[] = {0, 0};
39 initializeVariable(name,
51 std::cerr <<
"[ERROR][matioCpp::CellArray::CellArray] Zero dimension detected." <<
std::endl;
56 initializeVariable(name,
70 if (totalElements != elements.
size())
72 std::cerr <<
"[ERROR][matioCpp::CellArray::CellArray] The size of elements vector does not match the provided dimensions. The total number is different." <<
std::endl;
76 for (
size_t i = 0; i < totalElements; ++i)
78 if (!elements[i].isValid())
80 std::cerr <<
"[ERROR][matioCpp::CellArray::CellArray] The element at index "<< i <<
" (0-based) is not valid." <<
std::endl;
86 initializeVariable(name,
89 vectorOfPointers.
data());
99 fromOther(std::forward<CellArray>(other));
108 size_t emptyDimensions[] = {0, 0};
129 fromOther(std::forward<CellArray>(other));
138 totalElements *= dim;
141 if (totalElements != elements.
size())
143 std::cerr <<
"[ERROR][matioCpp::CellArray::fromVectorOfVariables] The size of elements vector does not match the provided dimensions. The total number is different." <<
std::endl;
147 for (
size_t i = 0; i < totalElements; ++i)
149 if (!elements[i].isValid())
151 std::cerr <<
"[ERROR][matioCpp::CellArray::fromVectorOfVariables] The element at index "<< i <<
" (0-based) is not valid." <<
std::endl;
157 return initializeVariable(name(),
160 vectorOfPointers.
data());
165 assert(dimensions().size() > 0 && numberOfElements() > 0 &&
"[matioCpp::CellArray::rawIndexFromIndices] The array is empty.");
166 assert(el.
size() > 0 == dimensions().size() > 0 &&
"[matioCpp::CellArray::rawIndexFromIndices] The input vector el should have the same number of dimensions of the array.");
167 assert(el[0] < dimensions()[0] &&
"[matioCpp::CellArray::rawIndexFromIndices] The required element is out of bounds.");
172 for (
size_t i = 0; i < el.
size(); ++i)
174 assert(el[i] < dimensions()[i] &&
"[matioCpp::CellArray::rawIndexFromIndices] The required element is out of bounds.");
175 index += el[i] * previousDimensionsFactorial;
176 previousDimensionsFactorial *= dimensions()[i];
184 el.
resize(dimensions().size());
186 if (rawIndex >= numberOfElements())
188 std::cerr <<
"[ERROR][matioCpp::CellArray::indicesFromRawIndex] rawIndex is greater than the number of elements." <<
std::endl;
192 size_t previousDimensionsFactorial = dimensions()[0];
196 for (
size_t i = 1; i < el.
size(); ++i)
198 el[i - 1] = previousDimensionsFactorial;
199 previousDimensionsFactorial *= dimensions()[i];
202 size_t remainder = rawIndex;
204 for (
size_t i = el.
size() - 1; i > 0; --i)
206 el[i] = remainder / el[i - 1];
207 remainder -= el[i] * el[i - 1];
216 return changeName(newName);
232 return getArrayNumberOfElements();
239 std::cerr <<
"[ERROR][matioCpp::CellArray::setElement] The CellArray has not been properly initialized." <<
std::endl;
245 std::cerr <<
"[ERROR][matioCpp::CellArray::setElement] The input variable is not valid." <<
std::endl;
249 if (!setCellElement(rawIndexFromIndices(el), newValue))
251 std::cerr <<
"[ERROR][matioCpp::CellArray::setElement] Failed to set the cell element." <<
std::endl;
260 assert(el < numberOfElements() &&
"[matioCpp::CellArray::setElement] The required element is out of bounds.");
264 std::cerr <<
"[ERROR][matioCpp::CellArray::setElement] The CellArray has not been properly initialized." <<
std::endl;
270 std::cerr <<
"[ERROR][matioCpp::CellArray::setElement] The input variable is not valid." <<
std::endl;
274 if (!setCellElement(el, newValue))
276 std::cerr <<
"[ERROR][matioCpp::CellArray::setElement] Failed to set the cell element." <<
std::endl;
285 return getCellElement(rawIndexFromIndices(el));
290 return getCellElement(rawIndexFromIndices(el));
295 assert(el < numberOfElements() &&
"[matioCpp::CellArray::operator()] The required element is out of bounds.");
296 return getCellElement(el);
301 assert(el < numberOfElements() &&
"[matioCpp::CellArray::operator()] The required element is out of bounds.");
302 return getCellElement(el);
307 return getCellElement(rawIndexFromIndices(el));
312 return getCellElement(rawIndexFromIndices(el));
317 assert(el < numberOfElements() &&
"[matioCpp::CellArray::operator[]] The required element is out of bounds.");
318 return getCellElement(el);
323 assert(el < numberOfElements() &&
"[matioCpp::CellArray::operator[]] The required element is out of bounds.");
324 return getCellElement(el);
CellArray is a particular type of Variable specialized for cell arrays.
matioCpp::Variable 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 fromVectorOfVariables(const std::vector< index_type > &dimensions, std::vector< Variable > &elements)
Set from a vector of Variables.
bool setElement(const std::vector< index_type > &el, const Variable &newValue)
Set the element at the specified position.
index_type numberOfElements() const
Get the total number of elements in the array.
matioCpp::Variable operator()(const std::vector< index_type > &el)
Access specified element.
bool setName(const std::string &newName)
Change the name of the Variable.
CellArray & operator=(const CellArray &other)
Assignement operator (copy) from another CellArray.
CellArray()
The type used for indices.
void clear()
Clear the cell array.
bool indicesFromRawIndex(size_t rawIndex, std::vector< index_type > &el) const
Get the indices given the raw index.
void resize(const std::vector< index_type > &newDimensions)
Resize the cell array.
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.
The matioCpp::Variable class is the equivalent of matvar_t in matio.
bool isValid() const
Check if the variable is valid.
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.
VariableType
Define the type of variable.
ValueType
The list of types for an element of a certain variable type.