Boost logo

Boost :

From: Neal Becker (ndbecker2_at_[hidden])
Date: 2006-02-27 11:20:18


Janek Kozicki wrote:

> hi
>
> serialization of ublas matrix and vector is missing in boost 1.33,
> therefore I've written missing functions:
>
> /usr/include/boost/serialization/ublas_matrix.hpp
> /usr/include/boost/serialization/ublas_vector.hpp
>
> files are in the attachment.
>
> I'm afraid that the idea of converting matrix to
> std::vector<std::vector<U> > is not optimal, perhaps someone can make it
> better?
>
> Please include those files in next boost version. File are very short,
> so review should not take too long. I bet that some people out there need
> them.
>
> best regards

Here is the code I use to serialize ublas vector:

#include <sstream>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/split_free.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/level.hpp>
#include <boost/serialization/tracking.hpp>

namespace boost {
namespace serialization {

template<class T>
struct implementation_level<std::complex<T> >
{
  typedef mpl::integral_c_tag tag;
  // typedef mpl::int_<primitive_type> type;
  typedef mpl::int_<object_serializable> type;
  BOOST_STATIC_CONSTANT(
                        int,
                        value = implementation_level::type::value
                        );
};

template<class T>
struct tracking_level<std::complex<T> >
{
  typedef mpl::integral_c_tag tag;
  typedef mpl::int_<track_never> type;
  BOOST_STATIC_CONSTANT(
                        int,
                        value = tracking_level::type::value
                        );

};

}
}

// template<typename Archive, typename T>
// inline void save (Archive &ar, const std::complex<T>& z, const unsigned int) {
// // ar << boost::serialization::make_nvp ("real", const_cast<const T&>(real (z)));
// // ar << boost::serialization::make_nvp ("imag", const_cast<const T&>(imag (z)));
// ar << boost::serialization::make_nvp ("real", real (z));
// ar << boost::serialization::make_nvp ("imag", imag (z));
// }

// template<typename Archive, typename T>
// inline void load (Archive &ar, std::complex<T>& z, const unsigned int) {
// // T i, r;
// // ar >> boost::serialization::make_nvp ("real", r);
// // ar >> boost::serialization::make_nvp ("imag", i);
// // z = std::complex<T> (r, i);
// ar >> boost::serialization::make_nvp ("real", real(z));
// ar >> boost::serialization::make_nvp ("imag", imag(z));
// }

// namespace boost { namespace serialization {
// template<class Archive, class T>
// inline void serialize (Archive &ar, std::complex<T>& z, const unsigned int file_version) {
// boost::serialization::split_free (ar, z, file_version);
// }
// }
// }
namespace boost { namespace serialization {
template<class Archive, class T>
inline void serialize (Archive &ar, std::complex<T>& z, const
unsigned int file_version) {
  ar & boost::serialization::make_nvp ("real", real(z));
  ar & boost::serialization::make_nvp ("imag", imag(z));
  // ar & real(z);
  // ar & imag(z);
}
}
}

namespace boost {
namespace serialization {

template<class T>
struct implementation_level<ublas::vector<T> >
{
  typedef mpl::integral_c_tag tag;
  // typedef mpl::int_<primitive_type> type;
  typedef mpl::int_<object_serializable> type;
  BOOST_STATIC_CONSTANT(
                        int,
                        value = implementation_level::type::value
                        );
};

}
}

template<class Archive, class U>
inline void save (Archive &ar, const ublas::vector<U> &v, const unsigned int) {
  unsigned int count = v.size();
  ar << BOOST_SERIALIZATION_NVP (count);
  typename ublas::vector<U>::const_iterator it = v.begin();
  while (count-- > 0) {
    ar << boost::serialization::make_nvp ("item", *it++);
  }
}

template<class Archive, class U>
inline void load (Archive &ar, ublas::vector<U> &v, const unsigned int) {
  unsigned int count;
  ar >> BOOST_SERIALIZATION_NVP (count);
  v.resize (count);
  typename ublas::vector<U>::iterator it = v.begin();
  while (count-- > 0) {
    ar >> boost::serialization::make_nvp ("item", *it++);
  }
}

namespace boost { namespace serialization {
template<class Archive, class U>
inline void serialize (Archive &ar, ublas::vector<U>& v, const unsigned int file_version) {
  boost::serialization::split_free (ar, v, file_version);
}
}
}

namespace python = boost::python;
template <typename MatrixType>
struct vector_pickle_suite : pickle_suite
{
  static
  tuple
  getinitargs(const MatrixType &m)
  {
    return make_tuple(m.size());
  }

  static python::object getstate (const MatrixType& v) {
    std::ostringstream os;
    boost::archive::binary_oarchive oa(os);
    oa << v;
    return python::str (os.str());
  }

  static void
  setstate(MatrixType& v, python::object entries) {
    python::str s = python::extract<python::str> (entries)();
    std::string st = python::extract<std::string> (s)();
    std::istringstream is (st);
    
    boost::archive::binary_iarchive ia (is);
    ia >> v;
  }
// static
// object
// getstate(const MatrixType &m)
// {
// typename MatrixType::const_iterator
// first = m.begin(),
// last = m.end();
    
// list result;
// while (first != last)
// result.append(*first++);
    
// return result;
// }
  
// static void
// setstate(MatrixType &m, object entries)
// {
// typename MatrixType::iterator
// first = m.begin();
    
// unsigned len = extract<unsigned>(entries.attr("__len__")());
// for (unsigned i = 0; i < len; i++)
// *first++ = (typename MatrixType::value_type)
// extract<typename MatrixType::value_type>(entries[i]);
// }
};


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk