diff options
author | Chris Dyer <cdyer@cs.cmu.edu> | 2011-09-03 17:14:18 +0100 |
---|---|---|
committer | Chris Dyer <cdyer@cs.cmu.edu> | 2011-09-03 17:14:18 +0100 |
commit | b6a2a068e552de6d3c1e2507213d0161e1c7e34b (patch) | |
tree | 392f06b2b03647fb3a4d33e61d57aefb5dbe0b57 | |
parent | 7607b0a7873f52d6e3ea387bf88c773cbb55f8ee (diff) |
fix sparse vector to work with boost serialization
-rw-r--r-- | utils/fast_sparse_vector.h | 46 | ||||
-rw-r--r-- | utils/sparse_vector.h | 38 |
2 files changed, 46 insertions, 38 deletions
diff --git a/utils/fast_sparse_vector.h b/utils/fast_sparse_vector.h index 9d72cb87..b3f9588d 100644 --- a/utils/fast_sparse_vector.h +++ b/utils/fast_sparse_vector.h @@ -7,6 +7,8 @@ // important: indexes are integers // important: iterators may return elements in any order +#include "config.h" + #include <cmath> #include <cstring> #include <climits> @@ -16,6 +18,13 @@ #include <boost/static_assert.hpp> +#if HAVE_BOOST_ARCHIVE_TEXT_OARCHIVE_HPP +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp> +#endif + +#include "fdict.h" + // this is architecture dependent, it should be // detected in some way but it's probably easiest (for me) // to just set it @@ -334,8 +343,45 @@ class FastSparseVector { } data_; unsigned char local_size_; bool is_remote_; + +#if HAVE_BOOST_ARCHIVE_TEXT_OARCHIVE_HPP + private: + friend class boost::serialization::access; + template<class Archive> + void save(Archive & ar, const unsigned int version) const { + (void) version; + int eff_size = size(); + const_iterator it = this->begin(); + if (eff_size > 0) { + // 0 index is reserved as empty + if (it->first == 0) { ++it; --eff_size; } + } + ar & eff_size; + while (it != this->end()) { + const std::pair<const std::string&, const T&> wire_pair(FD::Convert(it->first), it->second); + ar & wire_pair; + ++it; + } + } + template<class Archive> + void load(Archive & ar, const unsigned int version) { + (void) version; + this->clear(); + int sz; ar & sz; + for (int i = 0; i < sz; ++i) { + std::pair<std::string, T> wire_pair; + ar & wire_pair; + this->set_value(FD::Convert(wire_pair.first), wire_pair.second); + } + } + BOOST_SERIALIZATION_SPLIT_MEMBER() +#endif }; +#if HAVE_BOOST_ARCHIVE_TEXT_OARCHIVE_HPP +BOOST_CLASS_TRACKING(FastSparseVector<double>,track_never) +#endif + template <typename T> const FastSparseVector<T> operator+(const FastSparseVector<T>& x, const FastSparseVector<T>& y) { if (x.size() > y.size()) { diff --git a/utils/sparse_vector.h b/utils/sparse_vector.h index a55436fb..049151f7 100644 --- a/utils/sparse_vector.h +++ b/utils/sparse_vector.h @@ -1,44 +1,6 @@ #ifndef _SPARSE_VECTOR_H_ #define _SPARSE_VECTOR_H_ -#if 0 - -#if HAVE_BOOST_ARCHIVE_TEXT_OARCHIVE_HPP - friend class boost::serialization::access; - template<class Archive> - void save(Archive & ar, const unsigned int version) const { - (void) version; - int eff_size = values_.size(); - const_iterator it = this->begin(); - if (values_.find(0) != values_.end()) { ++it; --eff_size; } - ar & eff_size; - while (it != this->end()) { - const std::pair<const std::string&, const T&> wire_pair(FD::Convert(it->first), it->second); - ar & wire_pair; - ++it; - } - } - template<class Archive> - void load(Archive & ar, const unsigned int version) { - (void) version; - this->clear(); - int sz; ar & sz; - for (int i = 0; i < sz; ++i) { - std::pair<std::string, T> wire_pair; - ar & wire_pair; - this->set_value(FD::Convert(wire_pair.first), wire_pair.second); - } - } - BOOST_SERIALIZATION_SPLIT_MEMBER() -#endif -}; - -#if HAVE_BOOST_ARCHIVE_TEXT_OARCHIVE_HPP -BOOST_CLASS_TRACKING(SparseVector<double>,track_never) -#endif - -#endif /// FIX - #include "fast_sparse_vector.h" #define SparseVector FastSparseVector |