-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatio_cpp.h
1529 lines (1346 loc) · 47.5 KB
/
matio_cpp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2016 Felix Lelchuk
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
/**
* @file matio_cpp.h
* @brief C++11 interface to MAT I/O
* @author Felix Lelchuk
* @version 20/11/2016
*/
#ifndef MATIO_CPP_H
#define MATIO_CPP_H
#include <matio.h>
#include <cassert>
#include <cinttypes>
#include <string>
#include <array>
#include <algorithm>
#include <numeric>
#include <memory>
/** @brief Namespace containing the C++ 11 interface to MAT I/O */
namespace MatioCPP
{
using SizeType = size_t;
/**
* @brief Base class for n-dimensional array memory ordering.
* @tparam N Number of dimensions
*
* This class pre-calculates strides for the addressing of a multi-dimensional
* array's memory. The resulting strides a stores inside the class.
*/
template <size_t N>
class MemOrder
{
public:
static const size_t Dims = N; /**< Holds the dimension template parameter N. */
/**
* @brief Calculates the strides for the given dimensions.
* @param dims std::array of the sizes in each dimension.
*/
virtual void calcStrides(const std::array<SizeType, N>& dims) = 0;
/**
* @brief Returns the calculated strides.
* @return const_iterator to the head of the stride list.
* @pre calcStrides() was called.
*/
inline typename std::array<SizeType, N>::const_iterator enumStride() const
{
return _strides.cbegin();
}
protected:
std::array<SizeType, N> _strides; /**< Stide list */
};
/**
* @brief Class implementing row-major memory ordering.
* @tparam N Number of dimensions
*
* This class pre-calculates strides for row-major addressing and stores them.
* Row-major ordering is the default memory layout for C/C++ an many other languages.
*/
template <size_t N>
class RowMajor : public MemOrder<N>
{
public:
/**
* @brief Calculates the strides for the given dimensions in a row-major fashion.
* @param dims std::array of the sizes in each dimension.
*/
virtual void calcStrides(const std::array<SizeType, N>& dims)
{
_strides.back() = 1;
if (N > 1)
std::partial_sum(dims.cbegin() + 1, dims.cend(), _strides.rbegin() + 1, std::multiplies<SizeType>());
}
};
/**
* @brief Class implementing column-major memory ordering.
* @tparam N Number of dimensions
*
* This class pre-calculates strides for column-major addressing and stores them.
* Color-major ordering is the default memory layout for MATLAB and Fortran.
*/
template <size_t N>
class ColumnMajor : public MemOrder<N>
{
public:
/**
* @brief Calculates the strides for the given dimensions in a column-major fashion.
* @param dims std::array of the sizes in each dimension.
*/
virtual void calcStrides(const std::array<SizeType, N>& dims)
{
if (N > 1)
std::partial_sum(dims.cbegin(), dims.cend() - 1, this->_strides.begin() + 1, std::multiplies<SizeType>());
_strides.front() = 1;
}
};
/**
* @brief Index into a multi-dimensional array
* @tparam N Number of dimensions
*/
template <size_t N>
class Index;
template <>
class Index<1>
{
public:
static const size_t Dims = 1;
inline Index(int last) : _pos(last)
{
}
template <typename StrideList>
inline int toLinear(StrideList strideList) const
{
return _pos * (*strideList);
}
template <typename DimList>
inline void checkBounds(DimList dims) const
{
assert(_pos >= 0);
assert(_pos < *dims);
}
private:
int _pos;
};
template <size_t N>
class Index
{
public:
static const size_t Dims = N;
template <typename... Rest>
Index(int first, Rest... rest) :
_inner(rest...),
_pos(first)
{
}
template <typename StrideList>
inline int toLinear(StrideList strideList) const
{
return _pos * (*strideList) + _inner.toLinear(++strideList);
}
template <typename DimList>
inline void checkBounds(DimList dims) const
{
assert(_pos >= 0);
assert(_pos < *dims);
_inner.checkBounds(++dims);
}
private:
Index<N - 1> _inner;
int _pos;
};
/**
* @brief Order-independent base class of MultiArray.
* @tparam T Element type.
* @tparam N Number of dimensions.
*/
template <typename T, size_t N>
class ArrayBase
{
public:
/**
* @brief Returns a pointer the contiguous memory backing the array.
* @return Pointer to the first element.
*/
inline const T* data() const
{
return _data;
}
/**
* @brief Returns a pointer the contiguous memory backing the array.
* @return Pointer to the first element.
*/
inline T* data()
{
return _data;
}
/**
* @brief Returns the total number of elements, in all dimensions.
* @return Overall number of elements.
*/
inline SizeType size() const
{
return _size;
}
/**
* @brief Returns the number of elements in a certain dimension.
* @param n Zero-based number of the dimension.
* @return Number of elements in the dimension of interest.
*/
inline SizeType dim(SizeType n) const
{
return _dims.at(n);
}
/**
* @brief Returns a N-element array containing the number of elements in each dimension.
* @return std::array with the number of elements for each dimension.
*/
inline const std::array<SizeType, N>& dims() const
{
return _dims;
}
/**
* @brief Sanity-check if the linear index is in range.
* @param index Index to check.
*
* This method uses C's assert() to report errors.
*/
void checkBounds(int index) const
{
assert(index >= 0);
assert(index < size());
}
/**
* @brief Return an immutable reference to an array element by its linear index.
* @param Linear index of the element.
* @return Constant element reference.
*/
inline const T& linear(int index) const
{
//checkBounds(index);
return data()[index];
}
/**
* @brief Return a mutable reference to an array element by its linear index.
* @param Linear index of the element.
* @return Mutable element reference.
*/
inline T& linear(int index)
{
//checkBounds(index);
return data()[index];
}
/**
* @brief Return an immutable reference to the first array element.
* @param Linear index of the element.
* @return Constant element reference.
*/
inline const T& value() const
{
return linear(0);
}
/**
* @brief Return a mutable reference to the first array element.
* @param Linear index of the element.
* @return Constant element reference.
*/
inline T& value()
{
return linear(0);
}
protected:
/**
* @internal
* @brief Constructs an object for the given dimensions.
* @param dims Number of elements in each of the N dimensions.
*
* Internal to the implementation. Does not allocate memory.
* This is left to the MultiArray.
*/
ArrayBase(const std::array<SizeType, N>& dims) :
_data(nullptr),
_dims(dims),
_ownData(true)
{
_size = std::accumulate(dims.cbegin(), dims.cend(), SizeType(1), std::multiplies<SizeType>());
}
/**
* @brief Destroys the array, freeing owned memory through setData().
*/
~ArrayBase()
{
setData(nullptr);
}
protected:
/**
* @brief Assign a new backing memory buffer to this array.
* @param data New memory to use.
* @postcondition The old memory is freed (if previously owned).
* @postcondition The newly assigned data is not owned.
*
* Frees the old memory if owned. Assigns new memory which is NOT
* owned by this object.
*/
inline void setData(T *data)
{
if (data == _data)
return;
if (_data && _ownData)
{
delete[] _data;
}
_ownData = false;
_data = data;
}
/**
* @brief Creates a new memory region for backing and claims ownership thereof.
* @param numElements Overall number of elements.
* @postcondition setData() was called, see its postconditions!
* @postcondition This object holds ownership of the newly allocated memory.
*/
inline void createData(SizeType numElements)
{
T* t = new T[numElements]();
setData(t);
_ownData = true;
}
protected:
T *_data; /**< Pointer to the backing memory. */
SizeType _size; /**< Overall number of elements in the array (all dimension). */
std::array<SizeType, N> _dims; /**< Numbers of elements in each of the N dimensions. */
bool _ownData; /**< Free backing memory when no longer used? */
};
/**
* @brief Multi-dimensional array with custom memory ordering.
* @tparam T Element type.
* @tparam N Number of dimensions.
* @tparam Order The Order class to use.
*/
template <typename T, size_t N, template <size_t> class Order>
class MultiArray : public ArrayBase<T, N>
{
public:
/**
* @brief Constructs a multi-dimensional array with the given number of elements in each dimension.
* @param first Number of elements in the first dimension.
* @param rest Number of elements in the following dimensions.
*
* This constructor allows to supply the element numbers as arguments rather than using std::array.
*/
template <typename... Rest>
MultiArray(SizeType first, Rest... rest)
: ArrayBase<T, N>({ first, rest... })
{
init();
}
/**
* @brief Creates a copy of the supplies MultiArray.
* @param other Original array.
*
* The newly created copy has the same dimensions as the original and contains an independent
* copy of the original array data.
*/
MultiArray(const MultiArray<T, N, Order>& other)
: ArrayBase<T, N>(other._dims)
{
init();
std::copy(other._data, other._data + other.size(), _data);
}
/**
* @brief Constructs a multi-dimensional array with the given number of elements in each dimension.
* @param dims std::array of the element numbers per dimension.
*/
MultiArray(const std::array<SizeType, N>& dims) : ArrayBase<T, N>(dims)
{
init();
}
/**
* @brief Constructs a MultiArray with the given number of elements in each dimension reusing existing memory.
* @param dims std::array of the element numbers per dimension.
* @param data Pointer to the exisiting memory.
* @postcondition The memory pointed to by the data argument may must persist until destruction of this object.
* @postcondition The memory is NOT owned by this object and must be freed elsewhere.
*
* Create a MultiArray based on existing memory. E.g. allows severals MultiArrays to share the same memory.
*/
MultiArray(const std::array<SizeType, N>& dims, T* data) : ArrayBase<T, N>(dims)
{
init(data);
}
/**
* @brief Assigns the other MultiArray object to this object.
* @param other Original array.
* @return Immutable reference to this object.
* @postcondition This object's size is adjusted to the argument's.
* @postcondition This object holds an independent copy of the argument's data.
*/
const MultiArray<T, N, Order>& operator=(const MultiArray<T, N, Order>& other)
{
if (&other == this)
return;
init();
std::copy(other._data, other._data + other.size(), _data);
return *this;
}
/**
* @brief Returns a constant reference to the element at the given Index.
* @param index N-dimensional Index of the element.
* @return Constant reference to the element.
* @precondition The index argument must be within the bounds of the array.
*/
inline const T& at(const Index<N> &index) const
{
//index.checkBounds(_dims.cbegin());
return linear(index.toLinear(_order.enumStride()));
}
/**
* @brief Returns a mutable reference to the element at the given Index.
* @param index N-dimensional Index of the element.
* @return Mutable reference to the element.
* @precondition The index argument must be within the bounds of the array.
*/
inline T& at(const Index<N> &index)
{
//index.checkBounds(_dims.cbegin());
return linear(index.toLinear(_order.enumStride()));
}
/**
* @brief Returns a constant reference to the element at the given Index.
* @param first Index into the first dimension.
* @param rest Index into the following dimensions.
* @return Constant reference to the element.
* @precondition The index must be within the bounds of the array.
*
* This version of the at() method allows to pass the
* sub-indices as parameters, rather than using an Index object.
*/
template <typename... Rest>
inline const T& at(int first, Rest... rest) const
{
return at(Index<N>(first, rest...));
}
/**
* @brief Returns a mutable reference to the element at the given Index.
* @param first Index into the first dimension.
* @param rest Index into the following dimensions.
* @return Mutable reference to the element.
* @precondition The index must be within the bounds of the array.
*
* This version of the at() method allows to pass the
* sub-indices as parameters, rather than using an Index object.
*/
template <typename... Rest>
inline T& at(int first, Rest... rest)
{
return at(Index<N>(first, rest...));
}
private:
/**
* @internal
* @brief Allocates memory for the array and calculates strides.
* @postcondition Postconditions of createData() apply.
* @postcondition The array received its very own backing memory buffer.
* @postcondition The strides were calculated according to the Order class.
*/
inline void init()
{
SizeType sz = size();
createData(sz);
_order.calcStrides(_dims);
}
/**
* @internal
* @brief Assigns existing memory for the array and calculates strides.
* @postcondition Postconditions of setData() apply.
* @postcondition The array is configured to use (but not own) the given memory.
* @postcondition The strides were calculated according to the Order class.
*/
inline void init(T *data)
{
setData(data);
_order.calcStrides(_dims);
}
private:
Order<N> _order; /**< Instance of the Order class. Contains stides for addressing. */
};
/** @internal */
template <typename T>
struct MatioCompat
{
static const matio_classes cls = MAT_C_EMPTY;
static const matio_types typ = MAT_T_UNKNOWN;
};
template <>
struct MatioCompat<uint8_t>
{
static const matio_classes cls = MAT_C_UINT8;
static const matio_types typ = MAT_T_UINT8;
};
template <>
struct MatioCompat<int8_t>
{
static const matio_classes cls = MAT_C_INT8;
static const matio_types typ = MAT_T_INT8;
};
template <>
struct MatioCompat<uint16_t>
{
static const matio_classes cls = MAT_C_UINT16;
static const matio_types typ = MAT_T_UINT16;
};
template <>
struct MatioCompat<int16_t>
{
static const matio_classes cls = MAT_C_INT16;
static const matio_types typ = MAT_T_INT16;
};
template <>
struct MatioCompat<uint32_t>
{
static const matio_classes cls = MAT_C_UINT32;
static const matio_types typ = MAT_T_UINT32;
};
template <>
struct MatioCompat<int32_t>
{
static const matio_classes cls = MAT_C_INT32;
static const matio_types typ = MAT_T_INT32;
};
template <>
struct MatioCompat<uint64_t>
{
static const matio_classes cls = MAT_C_UINT64;
static const matio_types typ = MAT_T_UINT64;
};
template <>
struct MatioCompat<int64_t>
{
static const matio_classes cls = MAT_C_INT64;
static const matio_types typ = MAT_T_INT64;
};
template <>
struct MatioCompat<float>
{
static const matio_classes cls = MAT_C_SINGLE;
static const matio_types typ = MAT_T_SINGLE;
};
template <>
struct MatioCompat<double>
{
static const matio_classes cls = MAT_C_DOUBLE;
static const matio_types typ = MAT_T_DOUBLE;
};
/**
* @brief Copy-on-write smart pointer.
*
* The held object can be pointed to by several ImplicitlySharedDataPtr.
* However, each write access (non-const dereferencing) of the smart pointer
* will cause the object to be duplicated, if more than one
* ImplicitlySharedDataPtr holds the object.
*/
template <class T>
class ImplicitlySharedDataPtr
{
public:
typedef std::shared_ptr<T> RefPtr; /**< Implementation of the shared_ptr. */
private:
RefPtr _ref; /**< Contained shared_ptr. */
/**
* @brief Method to duplicate the held object, if required.
* @postcondition The newly held object may now be a copy of the previously held.
*/
void detach()
{
T* tmp = _ref.get();
if (tmp != nullptr && !_ref.unique())
{
_ref = RefPtr(new T(*tmp));
}
}
public:
/**
* @brief Constructs the ImplicitlySharedDataPtr to hold the object pointed to by the argument.
* @param t Object to hold and take ownership of.
* @postcondition Takes ownership of the given object.
*/
ImplicitlySharedDataPtr(T* t) :
_ref(t)
{
}
/**
* @brief Constructs the ImplicitlySharedDataPtr to hold the object pointed to by the argument.
* @param t Object to hold.
*/
ImplicitlySharedDataPtr(const RefPtr& refptr) :
_ref(refptr)
{
}
/**
* @brief Check if any object is being held.
* @return false if the object is nullptr, true otherwise.
*/
inline operator bool() const
{
return (bool)_ref;
}
/**
* @brief Dereference immutably.
* @return Constant reference to the held object.
* @precondition Must hold an object, not nullptr.
* @see operator bool()
*/
inline const T& operator*() const
{
return *_ref;
}
/**
* @brief Dereference mutably.
* @return Mutable reference to the held object.
* @precondition Must hold an object, not nullptr.
* @see operator bool()
*/
inline T& operator*()
{
detach();
return *_ref;
}
/**
* @brief Immutable pointer access.
* @return Constant pointer to the held object.
* @see operator bool()
*/
inline const T* operator->() const
{
return _ref.operator->();
}
/**
* @brief Mutable pointer access.
* @return Mutable pointer to the held object.
* @see operator bool()
*/
inline T* operator->()
{
detach();
return _ref.operator->();
}
};
/** @brief Base class for MATLAB variables. */
template <typename T, size_t N, matio_classes MC = MatioCompat<T>::cls, matio_types MT = MatioCompat<T>::typ>
class MatVarBase
{
public:
using ArrayType = MultiArray<T, N, ColumnMajor>; /**< Type of the underlying MultiArray template class. */
protected:
/** @internal */
struct PrivateData
{
PrivateData() :
var(nullptr)
{
}
PrivateData(matvar_t *variable) :
var(variable)
{
}
virtual ~PrivateData()
{
if (var) Mat_VarFree(var);
}
matvar_t *var;
};
public:
/**
* @brief Constructs an invalid object.
* @postcondition isValid() returns false on this object.
*/
MatVarBase() :
_private(nullptr)
{
}
/**
* @brief Check if the object is valid.
* @return true if this is valid, false otherwise.
*/
inline bool isValid() const
{
return (bool)_private;
}
/**
* @brief Returns the variable's name.
* @return std::string containing the variable name.
* @precondition This object is valid.
* @see isValid()
*/
inline std::string name() const
{
if (_private->var->name)
return _private->var->name;
else
return std::string();
}
/**
* @brief Returns the variable's name.
* @param name std::string containing the variable name.
* @precondition This object is valid.
* @postcondition The string holding the old name was freed.
* @postcondition The name was changed.
* @see isValid()
*/
inline void setName(const std::string& name)
{
char* &n = _private->var->name;
if (n)
{
free(n);
}
n = malloc(name.length() + 1);
size_t copied = name.copy(n, name.length());
n[copied] = '\0';
}
/**
* Stores this variable to a matio file.
* @param file The matio file to write to.
* @param compress matio compression flag. Defaults to no compression.
* @precondition This object is valid.
* @postcondition The variable was written to the given matio file.
* @see isValid()
*/
inline void write(mat_t *file, matio_compression compress = MAT_COMPRESSION_NONE) const
{
Mat_VarWrite(file, _private->var, compress);
}
protected:
/** @internal */
MatVarBase(const ImplicitlySharedDataPtr<PrivateData> &priv) :
_private(priv)
{
}
static matvar_t* readBasic(mat_t *file, const std::string& name)
{
matvar_t *var = Mat_VarReadInfo(file, name.c_str());
if (!var) goto leave;
// Check if the variable we just read from the file is compatible with this array.
if (var->rank != N) goto del;
if (var->class_type != MC) goto del;
if (var->data_type != MT) goto del;
return var;
del:
Mat_VarFree(var);
leave:
return nullptr;
}
static bool readData(mat_t *file, matvar_t *var)
{
return Mat_VarReadDataAll(file, var) == 0;
}
protected:
ImplicitlySharedDataPtr<PrivateData> _private; /**< Smart pointer to private data structure. */
};
/**
* @brief Multidimensional non-complex MATLAB variable.
* @tparam T Element type.
* @tparam N Number of dimensions.
* @tparam MC matio_classes enum value for given T.
* @tparam MT matio_types enum value for given T.
*/
template <typename T, size_t N, matio_classes MC = MatioCompat<T>::cls, matio_types MT = MatioCompat<T>::typ>
class MatVar : public MatVarBase<T, N, MC, MT>
{
protected:
using Base = MatVarBase<T, N, MC, MT>; /**< Parameterized type of the MatVarBase base class. */
/** @internal */
struct PrivateData : Base::PrivateData
{
PrivateData(const std::string& name, const std::array<SizeType, N> &dims) :
arr(new ArrayType(dims))
{
var = Mat_VarCreate(name.c_str(), MC, MT, N, const_cast<SizeType *>(dims.data()), arr->data(), MAT_F_DONT_COPY_DATA);
assert(var != nullptr);
}
// NOTE: The PrivateData takes ownership of the variable.
PrivateData(const std::array<SizeType, N> &dims, matvar_t *variable) :
Base::PrivateData(variable),
arr(new ArrayType(dims, static_cast<T*>(variable->data)))
{
// Here, the variable reserved the memory.
// Have the Array use it (NOT own it).
}
PrivateData(const PrivateData& other)
{
if (other.arr)
{
// Duplicate the array and the variable.
// The variable will access (NOT own) the Array's data.
arr.swap(std::unique_ptr<ArrayType>(new ArrayType(*other.arr)));
var = Mat_VarDuplicate(other.var, 0); // 0 = don't copy data
assert(var != nullptr);
var->data = arr->data(); // replace the variable's data pointer but...
var->mem_conserve = 1; // ...tell the variable that memory is NOT owned
}
}
std::unique_ptr<ArrayType> arr;
};
public:
/**
* @brief Constructs an invalid MatVar.
*/
MatVar()
{
}
/**
* @brief Constructs a MatVar with given name and dimensional extents.
* @param name Name to assign to the variable.
* @param dims Number of elements in each of the N dimensions.
*/
MatVar(const std::string &name, std::array<SizeType, N> dims) :
Base(new PrivateData(name, dims))
{
}
/**
* @brief Constructs a MatVar with given name and dimensional extents.
* @param name Name to assign to the variable.
* @param dim1 Number of elements in the first dimension.
* @param rest Number of elements in the other N-1 dimensions.
*/
template <typename... Rest>
MatVar(const std::string &name, SizeType dim1, Rest... rest) :
MatVar(name, { dim1, rest... })
{
}
/**
* @brief Reads a MatVar with given name from the given matio file.
* @param file The matio file to read from.
* @param name Name of the variable to read.
* @postcondition The MatVar is invalid if the variable is not found in the matio file.
* @postcondition The MatVar is invalid if the variable's rank is not N.
* @postcondition The MatVar is invalid if the variable's matio_classes class is not MC.
* @postcondition The MatVar is invalid if the variable's matio_types type is not MT.
* @postcondition The MatVar is invalid if the variable is complex.
* @postcondition The MatVar's dimensions are according to the variable stored in the matio file.
* @postcondition The MatVar contains the data from the matio file.
*/
MatVar(mat_t *file, const std::string &name)
{
std::array<SizeType, N> dims;
// Read basic variable info from the file.
matvar_t *var = Base::readBasic(file, name);
if (!var) return;
// Check additional constraints
if (var->isComplex) goto del;
// Extract dimensions from the variable.
std::copy(var->dims, var->dims + N, dims.begin());
if (!Base::readData(file, var)) goto del;
_private = new PrivateData(dims, var);
return;
del:
Mat_VarFree(var); // This will leave _private uninitialized and thus the object invalid.
}
/**
* @brief Returns a constant reference to the underlying MultiArray.
* @precondition This object is valid.
* @see isValid()
*/
inline const ArrayType& array() const
{
return *static_cast<const PrivateData&>(*_private).arr;
}
/**
* @brief Returns a mutable reference to the underlying MultiArray.
* @precondition This object is valid.
* @see isValid()
*/
inline ArrayType& array()
{
return *static_cast<PrivateData&>(*_private).arr;
}
/**
* @brief Returns the number of elements in the given dimension.
* @param n Zero-based dimension number.
* @return Number of elements in the n'th dimension.
* @precondition This object is valid.
* @precondition n is in bounds (0 <= n < N)
* @see isValid()
*/
inline SizeType dim(SizeType n) const
{
return array().dim(n);
}
/**