Boost logo

Boost :

From: Robert Ramey (ramey_at_[hidden])
Date: 2005-08-26 15:31:42


Hmm - this raises a couple of questions in my mind. I would guess this is
because I don't kow what the meserenne twister does.

First a slight cosmetic change just to avoid a confusing/ugly cast;

 template<class Archive>
 inline void save (Archive &ar, const unsigned int) const {
    for (int j = 0; j < state_size; ++j) {
        const UIntType x = compute (j);
        ar << boost::serialization::make_nvp("item", x);
    }
}

But I'm intrigued about one thing. It seems to me that if you have an
instance of the class meserenne twister and serialize it to a file then
load it back to another instance - the two wont be equal unless compute(j)
== j ?

What am I missing here?

Neal Becker wrote:
> I think this is OK now. Here is addition to mersenne_twister.hpp:
> friend class boost::serialization::access;
>
> template<class Archive>
> inline void save (Archive &ar, const unsigned int) const {
> for (int j = 0; j < state_size; ++j) {
> UIntType x = compute (j);
> ar << boost::serialization::make_nvp("item", const_cast<const
> UIntType&>(x));
> }
> }
>
> template<class Archive>
> inline void load (Archive &ar, const unsigned int) {
> for (int j = 0; j < state_size; ++j) {
> ar >> boost::serialization::make_nvp ("item", x[j]);
> }
> i = state_size;
> }
>
> template<class Archive>
> void serialize(Archive & ar, const unsigned int file_version) {
> boost::serialization::split_member(ar, *this, file_version);
> }
>
> Also:
> #include <boost/serialization/split_member.hpp>
> #include <boost/serialization/nvp.hpp>
>
> Here is a simple test that passes:
>
> #include <boost/archive/text_oarchive.hpp>
> #include <boost/archive/text_iarchive.hpp>
> #include <boost/random.hpp>
>
> typedef boost::mt19937 rng_t;
>
>
> int main() {
> rng_t rng1;
>
> std::ostringstream os;
> boost::archive::text_oarchive oa(os);
> oa << const_cast<const rng_t&>(rng1);
>
> std::string st (os.str());
> std::istringstream is (st);
> boost::archive::text_iarchive ia (is);
> rng_t rng2;
> ia >> rng2;
>
> // std::cout << rng2;
> assert (rng1 == rng2);
> }
>
>
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost


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