serialize functions for Eigen::SparseVector
							parent
							
								
									73f98d8cf3
								
							
						
					
					
						commit
						0302075280
					
				|  | @ -24,6 +24,7 @@ | ||||||
| 
 | 
 | ||||||
| #include <gtsam/base/Matrix.h> | #include <gtsam/base/Matrix.h> | ||||||
| 
 | 
 | ||||||
|  | #include <Eigen/Sparse> | ||||||
| #include <boost/serialization/array.hpp> | #include <boost/serialization/array.hpp> | ||||||
| #include <boost/serialization/nvp.hpp> | #include <boost/serialization/nvp.hpp> | ||||||
| #include <boost/serialization/split_free.hpp> | #include <boost/serialization/split_free.hpp> | ||||||
|  | @ -87,6 +88,45 @@ void serialize(Archive& ar, gtsam::Matrix& m, const unsigned int version) { | ||||||
|   split_free(ar, m, version); |   split_free(ar, m, version); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | /******************************************************************************/ | ||||||
|  | /// Customized functions for serializing Eigen::SparseVector
 | ||||||
|  | template <class Archive, typename _Scalar, int _Options, typename _Index> | ||||||
|  | void save(Archive& ar, const Eigen::SparseVector<_Scalar, _Options, _Index>& m, | ||||||
|  |           const unsigned int /*version*/) { | ||||||
|  |   _Index size = m.size(); | ||||||
|  | 
 | ||||||
|  |   std::vector<std::pair<Eigen::Index, _Scalar>> data; | ||||||
|  |   for (typename Eigen::SparseVector<_Scalar, _Options, _Index>::InnerIterator | ||||||
|  |            it(m); | ||||||
|  |        it; ++it) | ||||||
|  |     data.push_back({it.index(), it.value()}); | ||||||
|  | 
 | ||||||
|  |   ar << BOOST_SERIALIZATION_NVP(size); | ||||||
|  |   ar << BOOST_SERIALIZATION_NVP(data); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | template <class Archive, typename _Scalar, int _Options, typename _Index> | ||||||
|  | void load(Archive& ar, Eigen::SparseVector<_Scalar, _Options, _Index>& m, | ||||||
|  |           const unsigned int /*version*/) { | ||||||
|  |   _Index size; | ||||||
|  |   ar >> BOOST_SERIALIZATION_NVP(size); | ||||||
|  |   m.resize(size); | ||||||
|  | 
 | ||||||
|  |   std::vector<std::pair<Eigen::Index, _Scalar>> data; | ||||||
|  |   ar >> BOOST_SERIALIZATION_NVP(data); | ||||||
|  | 
 | ||||||
|  |   for (auto&& d : data) { | ||||||
|  |     m.coeffRef(d.first) = d.second; | ||||||
|  |   } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | template <class Archive, typename _Scalar, int _Options, typename _Index> | ||||||
|  | void serialize(Archive& ar, Eigen::SparseVector<_Scalar, _Options, _Index>& m, | ||||||
|  |                const unsigned int version) { | ||||||
|  |   split_free(ar, m, version); | ||||||
|  | } | ||||||
|  | /******************************************************************************/ | ||||||
|  | 
 | ||||||
| }  // namespace serialization
 | }  // namespace serialization
 | ||||||
| }  // namespace boost
 | }  // namespace boost
 | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue