matio-cpp  v0.2.5
A C++ wrapper of the matio library, with memory ownership handling, to read and write .mat files.
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 
8 #include <matioCpp/CellArray.h>
9 
10 bool 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",
32  matioCpp::ValueType::VARIABLE, emptyDimensions,
33  nullptr);
34 }
35 
37 {
38  size_t emptyDimensions[] = {0, 0};
39  initializeVariable(name,
41  matioCpp::ValueType::VARIABLE, emptyDimensions,
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 {
64  matioCpp::CellArray::index_type totalElements = 1;
65  for (matioCpp::CellArray::index_type dim : dimensions)
66  {
67  totalElements *= dim;
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  }
75  std::vector<matvar_t*> vectorOfPointers(totalElements, nullptr);
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  }
83  vectorOfPointers[i] = matioCpp::MatvarHandler::GetMatvarDuplicate(elements[i].toMatio());
84  }
85 
86  initializeVariable(name,
89  vectorOfPointers.data());
90 }
91 
93 {
94  fromOther(other);
95 }
96 
98 {
99  fromOther(std::forward<CellArray>(other));
100 }
101 
103  : matioCpp::Variable(handler)
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",
111  matioCpp::ValueType::VARIABLE, emptyDimensions,
112  nullptr);
113  }
114 }
115 
117 {
118 
119 }
120 
122 {
123  fromOther(other);
124  return *this;
125 }
126 
128 {
129  fromOther(std::forward<CellArray>(other));
130  return *this;
131 }
132 
134 {
135  matioCpp::CellArray::index_type totalElements = 1;
136  for (matioCpp::CellArray::index_type dim : dimensions)
137  {
138  totalElements *= dim;
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  }
146  std::vector<matvar_t*> vectorOfPointers(totalElements, nullptr);
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  }
154  vectorOfPointers[i] = matioCpp::MatvarHandler::GetMatvarDuplicate(elements[i].toMatio());
155  }
156 
157  return initializeVariable(name(),
159  matioCpp::ValueType::VARIABLE, dimensions,
160  vectorOfPointers.data());
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;
170  typename matioCpp::CellArray::index_type previousDimensionsFactorial = 1;
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  {
198  el[i - 1] = previousDimensionsFactorial;
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 
220 {
221  matioCpp::CellArray newArray(name(), newDimensions);
222  fromOther(std::move(newArray));
223 }
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.
Definition: CellArray.cpp:305
index_type rawIndexFromIndices(const std::vector< index_type > &el) const
Get the linear index corresponding to the provided indices.
Definition: CellArray.cpp:163
bool fromVectorOfVariables(const std::vector< index_type > &dimensions, std::vector< Variable > &elements)
Set from a vector of Variables.
Definition: CellArray.cpp:133
~CellArray()
Destructor.
Definition: CellArray.cpp:116
bool setElement(const std::vector< index_type > &el, const Variable &newValue)
Set the element at the specified position.
Definition: CellArray.cpp:235
index_type numberOfElements() const
Get the total number of elements in the array.
Definition: CellArray.cpp:230
matioCpp::Variable operator()(const std::vector< index_type > &el)
Access specified element.
Definition: CellArray.cpp:283
bool setName(const std::string &newName)
Change the name of the Variable.
Definition: CellArray.cpp:214
CellArray & operator=(const CellArray &other)
Assignement operator (copy) from another CellArray.
Definition: CellArray.cpp:121
CellArray()
The type used for indices.
Definition: CellArray.cpp:27
void clear()
Clear the cell array.
Definition: CellArray.cpp:225
bool indicesFromRawIndex(size_t rawIndex, std::vector< index_type > &el) const
Get the indices given the raw index.
Definition: CellArray.cpp:182
void resize(const std::vector< index_type > &newDimensions)
Resize the cell array.
Definition: CellArray.cpp:219
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.
Definition: Variable.h:23
bool isValid() const
Check if the variable is valid.
Definition: Variable.cpp:542
matioCpp::VariableType variableType() const
Get the VariableType.
Definition: Variable.cpp:506
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
T data(T... args)
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 resize(T... args)
T size(T... args)