@@ -29,7 +29,12 @@ template <class Archive, typename _Scalar, int _Rows, int _Cols>
2929inline void save (Archive &archive,
3030 const Eigen::Matrix<_Scalar, _Rows, _Cols> &v) {
3131 size_type rows = static_cast <size_type>(v.rows ());
32+ size_type cols = static_cast <size_type>(v.cols ());
3233 archive (cereal::make_size_tag (rows));
34+ // Storing the rows and columns is redundant information but
35+ // makes loading a lot easier.
36+ archive (rows);
37+ archive (cols);
3338 for (size_type i = 0 ; i < rows; i++) {
3439 Eigen::Matrix<_Scalar, _Cols, 1 > row = v.row (i);
3540 archive (row);
@@ -38,28 +43,21 @@ inline void save(Archive &archive,
3843
3944template <class Archive , typename _Scalar, int _Rows, int _Cols>
4045inline void load (Archive &archive, Eigen::Matrix<_Scalar, _Rows, _Cols> &v) {
41- size_type rows;
42- archive (cereal::make_size_tag (rows));
46+ size_type rows_plus_two, rows, cols;
47+ archive (cereal::make_size_tag (rows_plus_two));
48+ archive (rows);
49+ archive (cols);
50+ assert (rows == rows_plus_two - 2 );
4351 /*
4452 * In order to determine the size of a matrix, we have to first determine
4553 * how many rows, then inspect the size of the first row to get the
4654 * number of columns.
4755 */
48- if (rows > 0 ) {
49- Eigen::Matrix<_Scalar, _Rows, 1 > first;
50- archive (first);
51- size_type cols = first.rows ();
52- v.resize (rows, cols);
53- v.row (0 ) = first;
54-
55- for (size_type i = 1 ; i < rows; i++) {
56- Eigen::Matrix<_Scalar, _Cols, 1 > row;
57- archive (row);
58- v.row (i) = row;
59- }
60- } else {
61- // Serialized matrix is empty.
62- v.resize (0 , 0 );
56+ v.resize (rows, cols);
57+ for (size_type i = 0 ; i < rows; i++) {
58+ Eigen::Matrix<_Scalar, _Cols, 1 > row;
59+ archive (row);
60+ v.row (i) = row;
6361 }
6462};
6563
0 commit comments