matio-cpp v0.3.0
A C++ wrapper of the matio library, with memory ownership handling, to read and write .mat files.
Loading...
Searching...
No Matches
CellArray.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2020 Fondazione Istituto Italiano di Tecnologia
3 *
4 * This software may be modified and distributed under the terms of the
5 * BSD-2-Clause license (https://opensource.org/licenses/BSD-2-Clause).
6 */
7
9
10bool matioCpp::CellArray::checkCompatibility(const matvar_t* inputPtr, matioCpp::VariableType variableType, matioCpp::ValueType) const
11{
13 {
14 std::cerr << "[matioCpp::CellArray::checkCompatibility] The variable type is not compatible with a cell array." << std::endl;
15 return false;
16 }
17
18 if (inputPtr->isComplex)
19 {
20 std::cerr << "[matioCpp::CellArray::checkCompatibility] Cannot use a complex variable into a non-complex one." << std::endl;
21 return false;
22 }
23
24 return true;
25}
26
28{
29 size_t emptyDimensions[] = {0, 0};
30 initializeVariable("unnamed_cell_array",
33 nullptr);
34}
35
37{
38 size_t emptyDimensions[] = {0, 0};
39 initializeVariable(name,
42 nullptr);
43}
44
46{
47 for (matioCpp::CellArray::index_type dim : dimensions)
48 {
49 if (dim == 0)
50 {
51 std::cerr << "[ERROR][matioCpp::CellArray::CellArray] Zero dimension detected." << std::endl;
52 assert(false);
53 }
54 }
55
56 initializeVariable(name,
59 nullptr);
60}
61
63{
65 for (matioCpp::CellArray::index_type dim : dimensions)
66 {
68 }
69
70 if (totalElements != elements.size())
71 {
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;
73 assert(false);
74 }
76 for (size_t i = 0; i < totalElements; ++i)
77 {
78 if (!elements[i].isValid())
79 {
80 std::cerr << "[ERROR][matioCpp::CellArray::CellArray] The element at index "<< i << " (0-based) is not valid." << std::endl;
81 assert(false);
82 }
84 }
85
86 initializeVariable(name,
90}
91
93{
94 fromOther(other);
95}
96
98{
99 fromOther(std::forward<CellArray>(other));
100}
101
104{
105 if (!handler.get() || !checkCompatibility(handler.get(), handler.variableType(), handler.valueType()))
106 {
107 assert(false);
108 size_t emptyDimensions[] = {0, 0};
109 initializeVariable("unnamed_cell_array",
112 nullptr);
113 }
114}
115
120
122{
123 fromOther(other);
124 return *this;
125}
126
128{
129 fromOther(std::forward<CellArray>(other));
130 return *this;
131}
132
134{
136 for (matioCpp::CellArray::index_type dim : dimensions)
137 {
139 }
140
141 if (totalElements != elements.size())
142 {
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;
144 return false;
145 }
147 for (size_t i = 0; i < totalElements; ++i)
148 {
149 if (!elements[i].isValid())
150 {
151 std::cerr << "[ERROR][matioCpp::CellArray::fromVectorOfVariables] The element at index "<< i << " (0-based) is not valid." << std::endl;
152 return false;
153 }
155 }
156
157 return initializeVariable(name(),
161}
162
164{
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.");
168
169 typename matioCpp::CellArray::index_type index = 0;
171
172 for (size_t i = 0; i < el.size(); ++i)
173 {
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];
177 }
178
179 return index;
180}
181
183{
184 el.resize(dimensions().size());
185
186 if (rawIndex >= numberOfElements())
187 {
188 std::cerr << "[ERROR][matioCpp::CellArray::indicesFromRawIndex] rawIndex is greater than the number of elements." << std::endl;
189 return false;
190 }
191
192 size_t previousDimensionsFactorial = dimensions()[0];
193
194 //First we fill el with the factorial of the dimensions
195
196 for (size_t i = 1; i < el.size(); ++i)
197 {
199 previousDimensionsFactorial *= dimensions()[i];
200 }
201
202 size_t remainder = rawIndex;
203
204 for (size_t i = el.size() - 1; i > 0; --i)
205 {
206 el[i] = remainder / el[i - 1];
207 remainder -= el[i] * el[i - 1];
208 }
209 el[0] = remainder;
210
211 return true;
212}
213
215{
216 return changeName(newName);
217}
218
224
226{
227 fromOther(std::move(CellArray(name())));
228}
229
231{
232 return getArrayNumberOfElements();
233}
234
236{
237 if (!isValid())
238 {
239 std::cerr << "[ERROR][matioCpp::CellArray::setElement] The CellArray has not been properly initialized." << std::endl;
240 return false;
241 }
242
243 if (!newValue.isValid())
244 {
245 std::cerr << "[ERROR][matioCpp::CellArray::setElement] The input variable is not valid." << std::endl;
246 return false;
247 }
248
249 if (!setCellElement(rawIndexFromIndices(el), newValue))
250 {
251 std::cerr << "[ERROR][matioCpp::CellArray::setElement] Failed to set the cell element." << std::endl;
252 return false;
253 }
254
255 return true;
256}
257
259{
260 assert(el < numberOfElements() && "[matioCpp::CellArray::setElement] The required element is out of bounds.");
261
262 if (!isValid())
263 {
264 std::cerr << "[ERROR][matioCpp::CellArray::setElement] The CellArray has not been properly initialized." << std::endl;
265 return false;
266 }
267
268 if (!newValue.isValid())
269 {
270 std::cerr << "[ERROR][matioCpp::CellArray::setElement] The input variable is not valid." << std::endl;
271 return false;
272 }
273
274 if (!setCellElement(el, newValue))
275 {
276 std::cerr << "[ERROR][matioCpp::CellArray::setElement] Failed to set the cell element." << std::endl;
277 return false;
278 }
279
280 return true;
281}
282
284{
285 return getCellElement(rawIndexFromIndices(el));
286}
287
289{
290 return getCellElement(rawIndexFromIndices(el));
291}
292
294{
295 assert(el < numberOfElements() && "[matioCpp::CellArray::operator()] The required element is out of bounds.");
296 return getCellElement(el);
297}
298
300{
301 assert(el < numberOfElements() && "[matioCpp::CellArray::operator()] The required element is out of bounds.");
302 return getCellElement(el);
303}
304
306{
307 return getCellElement(rawIndexFromIndices(el));
308}
309
311{
312 return getCellElement(rawIndexFromIndices(el));
313}
314
316{
317 assert(el < numberOfElements() && "[matioCpp::CellArray::operator[]] The required element is out of bounds.");
318 return getCellElement(el);
319}
320
322{
323 assert(el < numberOfElements() && "[matioCpp::CellArray::operator[]] The required element is out of bounds.");
324 return getCellElement(el);
325}
326
CellArray is a particular type of Variable specialized for cell arrays.
Definition CellArray.h:18
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.
~CellArray()
Destructor.
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.
Definition CellArray.cpp:27
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/.
MultiDimensionalArray is a particular type of Variable specialized for multidimensional arrays of a g...
void resize(const std::vector< index_type > &newDimensions)
Resize the vector.
pointer data()
Direct access to the underlying array.
The matioCpp::Variable class is the equivalent of matvar_t in matio.
Definition Variable.h:23
bool isValid() const
Check if the variable is valid.
Definition Variable.cpp:546
matioCpp::VariableType variableType() const
Get the VariableType.
Definition Variable.cpp:510
bool initializeVariable(const std::string &name, const VariableType &variableType, const ValueType &valueType, matioCpp::Span< const size_t > dimensions, void *data)
Initialize the variable.
Definition Variable.cpp:16
matioCpp::ValueType valueType() const
Get the ValueType.
Definition Variable.cpp:515
T endl(T... args)
T move(T... args)
VariableType
Define the type of variable.
ValueType
The list of types for an element of a certain variable type.
T size(T... args)