2
0
mirror of https://github.com/boostorg/ublas.git synced 2026-02-21 15:32:12 +00:00

- added serialization support to most matrix and vector classes

- touched files: matrix.hpp matrix_sparse.hpp storage.hpp
  storage_sparse.hpp vector.hpp vector_of_vector.hpp vector_sparse.hpp
- still no regression tests for this :-(


[SVN r37970]
This commit is contained in:
Gunter Winkler
2007-06-11 20:29:08 +00:00
parent 352abadbd8
commit dba05c4dd6
7 changed files with 388 additions and 4 deletions

View File

@@ -1,6 +1,6 @@
//
// Copyright (c) 2000-2002
// Joerg Walter, Mathias Koch
// Copyright (c) 2000-2007
// Joerg Walter, Mathias Koch, Gunter Winkler
//
// Permission to use, copy, modify, distribute and sell this software
// and its documentation for any purpose is hereby granted without fee,
@@ -20,6 +20,8 @@
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/matrix_expression.hpp>
#include <boost/numeric/ublas/detail/matrix_assign.hpp>
#include <boost/serialization/collection_size_type.hpp>
#include <boost/serialization/nvp.hpp>
// Iterators based on ideas of Jeremy Siek
@@ -945,6 +947,27 @@ namespace boost { namespace numeric { namespace ublas {
return reverse_iterator2 (begin2 ());
}
// Serialization
template<class Archive>
void serialize(Archive & ar, const unsigned int /* file_version */){
// we need to copy to a collection_size_type to get a portable
// and efficient serialization
serialization::collection_size_type s1 (size1_);
serialization::collection_size_type s2 (size2_);
// serialize the sizes
ar & serialization::make_nvp("size1",s1)
& serialization::make_nvp("size2",s2);
// copy the values back if loading
if (Archive::is_loading::value) {
size1_ = s1;
size2_ = s2;
}
ar & serialization::make_nvp("data",data_);
}
private:
size_type size1_;
size_type size2_;
@@ -1964,6 +1987,27 @@ namespace boost { namespace numeric { namespace ublas {
return reverse_iterator2 (begin2 ());
}
// Serialization
template<class Archive>
void serialize(Archive & ar, const unsigned int /* file_version */){
// we need to copy to a collection_size_type to get a portable
// and efficient serialization
serialization::collection_size_type s1 (size1_);
serialization::collection_size_type s2 (size2_);
// serialize the sizes
ar & serialization::make_nvp("size1",s1)
& serialization::make_nvp("size2",s2);
// copy the values back if loading
if (Archive::is_loading::value) {
size1_ = s1;
size2_ = s2;
}
ar & serialization::make_nvp("data",data_);
}
private:
size_type size1_;
size_type size2_;
@@ -2321,6 +2365,26 @@ namespace boost { namespace numeric { namespace ublas {
return const_reverse_iterator2 (begin2 ());
}
// Serialization
template<class Archive>
void serialize(Archive & ar, const unsigned int /* file_version */){
// we need to copy to a collection_size_type to get a portable
// and efficient serialization
serialization::collection_size_type s1 (size1_);
serialization::collection_size_type s2 (size2_);
// serialize the sizes
ar & serialization::make_nvp("size1",s1)
& serialization::make_nvp("size2",s2);
// copy the values back if loading
if (Archive::is_loading::value) {
size1_ = s1;
size2_ = s2;
}
}
private:
size_type size1_;
size_type size2_;
@@ -2701,6 +2765,27 @@ namespace boost { namespace numeric { namespace ublas {
return const_reverse_iterator2 (begin2 ());
}
// Serialization
template<class Archive>
void serialize(Archive & ar, const unsigned int /* file_version */){
// we need to copy to a collection_size_type to get a portable
// and efficient serialization
serialization::collection_size_type s1 (size1_);
serialization::collection_size_type s2 (size2_);
// serialize the sizes
ar & serialization::make_nvp("size1",s1)
& serialization::make_nvp("size2",s2);
// copy the values back if loading
if (Archive::is_loading::value) {
size1_ = s1;
size2_ = s2;
size_common_ = ((std::min)(size1_, size2_));
}
}
private:
size_type size1_;
size_type size2_;
@@ -3140,6 +3225,28 @@ namespace boost { namespace numeric { namespace ublas {
return const_reverse_iterator2 (begin2 ());
}
// Serialization
template<class Archive>
void serialize(Archive & ar, const unsigned int /* file_version */){
// we need to copy to a collection_size_type to get a portable
// and efficient serialization
serialization::collection_size_type s1 (size1_);
serialization::collection_size_type s2 (size2_);
// serialize the sizes
ar & serialization::make_nvp("size1",s1)
& serialization::make_nvp("size2",s2);
// copy the values back if loading
if (Archive::is_loading::value) {
size1_ = s1;
size2_ = s2;
}
ar & serialization::make_nvp("value", value_);
}
private:
size_type size1_;
size_type size2_;
@@ -4043,6 +4150,28 @@ namespace boost { namespace numeric { namespace ublas {
return reverse_iterator2 (begin2 ());
}
// Serialization
template<class Archive>
void serialize(Archive & ar, const unsigned int /* file_version */){
// we need to copy to a collection_size_type to get a portable
// and efficient serialization
serialization::collection_size_type s1 (size1_);
serialization::collection_size_type s2 (size2_);
// serialize the sizes
ar & serialization::make_nvp("size1",s1)
& serialization::make_nvp("size2",s2);
// copy the values back if loading
if (Archive::is_loading::value) {
size1_ = s1;
size2_ = s2;
}
// could probably use make_array( &(data[0][0]), N*M )
ar & serialization::make_array(data_, N);
}
private:
size_type size1_;
size_type size2_;

View File

@@ -1,6 +1,6 @@
//
// Copyright (c) 2000-2002
// Joerg Walter, Mathias Koch
// Copyright (c) 2000-2007
// Joerg Walter, Mathias Koch, Gunter Winkler
//
// Permission to use, copy, modify, distribute and sell this software
// and its documentation for any purpose is hereby granted without fee,
@@ -1295,6 +1295,20 @@ namespace boost { namespace numeric { namespace ublas {
return reverse_iterator2 (begin2 ());
}
// Serialization
template<class Archive>
void serialize(Archive & ar, const unsigned int /* file_version */){
serialization::collection_size_type s1 (size1_);
serialization::collection_size_type s2 (size2_);
ar & serialization::make_nvp("size1",s1);
ar & serialization::make_nvp("size2",s2);
if (Archive::is_loading::value) {
size1_ = s1;
size2_ = s2;
}
ar & serialization::make_nvp("data", data_);
}
private:
size_type size1_;
size_type size2_;
@@ -2482,6 +2496,20 @@ namespace boost { namespace numeric { namespace ublas {
return reverse_iterator2 (begin2 ());
}
// Serialization
template<class Archive>
void serialize(Archive & ar, const unsigned int /* file_version */){
serialization::collection_size_type s1 (size1_);
serialization::collection_size_type s2 (size2_);
ar & serialization::make_nvp("size1",s1);
ar & serialization::make_nvp("size2",s2);
if (Archive::is_loading::value) {
size1_ = s1;
size2_ = s2;
}
ar & serialization::make_nvp("data", data_);
}
private:
size_type size1_;
size_type size2_;
@@ -3815,6 +3843,26 @@ namespace boost { namespace numeric { namespace ublas {
return reverse_iterator2 (begin2 ());
}
// Serialization
template<class Archive>
void serialize(Archive & ar, const unsigned int /* file_version */){
serialization::collection_size_type s1 (size1_);
serialization::collection_size_type s2 (size2_);
ar & serialization::make_nvp("size1",s1);
ar & serialization::make_nvp("size2",s2);
if (Archive::is_loading::value) {
size1_ = s1;
size2_ = s2;
}
ar & serialization::make_nvp("capacity", capacity_);
ar & serialization::make_nvp("filled1", filled1_);
ar & serialization::make_nvp("filled2", filled2_);
ar & serialization::make_nvp("index1_data", index1_data_);
ar & serialization::make_nvp("index2_data", index2_data_);
ar & serialization::make_nvp("value_data", value_data_);
storage_invariants();
}
private:
void storage_invariants () const {
BOOST_UBLAS_CHECK (layout_type::size_M (size1_, size2_) + 1 == index1_data_.size (), internal_logic ());
@@ -5159,6 +5207,27 @@ namespace boost { namespace numeric { namespace ublas {
return reverse_iterator2 (begin2 ());
}
// Serialization
template<class Archive>
void serialize(Archive & ar, const unsigned int /* file_version */){
serialization::collection_size_type s1 (size1_);
serialization::collection_size_type s2 (size2_);
ar & serialization::make_nvp("size1",s1);
ar & serialization::make_nvp("size2",s2);
if (Archive::is_loading::value) {
size1_ = s1;
size2_ = s2;
}
ar & serialization::make_nvp("capacity", capacity_);
ar & serialization::make_nvp("filled", filled_);
ar & serialization::make_nvp("sorted_filled", sorted_filled_);
ar & serialization::make_nvp("sorted", sorted_);
ar & serialization::make_nvp("index1_data", index1_data_);
ar & serialization::make_nvp("index2_data", index2_data_);
ar & serialization::make_nvp("value_data", value_data_);
storage_invariants();
}
private:
void storage_invariants () const
{

View File

@@ -22,6 +22,10 @@
#include <boost/shared_array.hpp>
#endif
#include <boost/serialization/array.hpp>
#include <boost/serialization/collection_size_type.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/numeric/ublas/exception.hpp>
#include <boost/numeric/ublas/traits.hpp>
#include <boost/numeric/ublas/detail/iterator.hpp>
@@ -87,6 +91,7 @@ namespace boost { namespace numeric { namespace ublas {
}
BOOST_UBLAS_INLINE
unbounded_array (const unbounded_array &c):
storage_array<unbounded_array<T, ALLOC> >(),
alloc_ (c.alloc_), size_ (c.size_) {
if (size_) {
data_ = alloc_.allocate (size_);
@@ -267,6 +272,21 @@ namespace boost { namespace numeric { namespace ublas {
return alloc_;
}
private:
friend class boost::serialization::access;
// Serialization
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
serialization::collection_size_type s(size_);
ar & serialization::make_nvp("size",s);
if ( Archive::is_loading::value ) {
resize(s);
}
ar & serialization::make_array(data_, s);
}
private:
// Handle explict destroy on a (possibly indexed) iterator
BOOST_UBLAS_INLINE
@@ -430,6 +450,22 @@ namespace boost { namespace numeric { namespace ublas {
return reverse_iterator (begin ());
}
private:
// Serialization
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
serialization::collection_size_type s(size_);
ar & serialization::make_nvp("size", s);
if ( Archive::is_loading::value ) {
if (s > N) bad_size("too large size in bounded_array::load()\n").raise();
resize(s);
}
ar & serialization::make_array(data_, s);
}
private:
size_type size_;
BOOST_UBLAS_BOUNDED_ARRAY_ALIGN value_type data_ [N];

View File

@@ -18,6 +18,11 @@
#define _BOOST_UBLAS_STORAGE_SPARSE_
#include <map>
#include <boost/serialization/collection_size_type.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/array.hpp>
#include <boost/serialization/map.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/numeric/ublas/storage.hpp>
@@ -197,8 +202,16 @@ namespace boost { namespace numeric { namespace ublas {
// FIXME should use ALLOC for map but std::allocator of std::pair<const I, T> and std::pair<I,T> fail to compile
template<class I, class T, class ALLOC>
class map_std : public std::map<I, T /*, ALLOC */> {
public:
// Serialization
template<class Archive>
void serialize(Archive & ar, const unsigned int /* file_version */){
ar & serialization::make_nvp("base", boost::serialization::base_object< std::map<I, T /*, ALLOC */> >(*this));
}
};
// Map array
// Implementation requires pair<I, T> allocator definition (without const)
@@ -478,6 +491,17 @@ namespace boost { namespace numeric { namespace ublas {
return alloc_;
}
// Serialization
template<class Archive>
void serialize(Archive & ar, const unsigned int /* file_version */){
serialization::collection_size_type s (size_);
ar & serialization::make_nvp("size",s);
if (Archive::is_loading::value) {
resize(s);
}
ar & serialization::make_array(data_, s);
}
private:
// Provide destroy as a non member function
BOOST_UBLAS_INLINE

View File

@@ -20,6 +20,8 @@
#include <boost/numeric/ublas/storage.hpp>
#include <boost/numeric/ublas/vector_expression.hpp>
#include <boost/numeric/ublas/detail/vector_assign.hpp>
#include <boost/serialization/collection_size_type.hpp>
#include <boost/serialization/nvp.hpp>
// Iterators based on ideas of Jeremy Siek
@@ -509,6 +511,12 @@ namespace boost { namespace numeric { namespace ublas {
return reverse_iterator (begin ());
}
// Serialization
template<class Archive>
void serialize(Archive & ar, const unsigned int /* file_version */){
ar & serialization::make_nvp("data",data_);
}
private:
array_type data_;
};
@@ -752,6 +760,16 @@ namespace boost { namespace numeric { namespace ublas {
return const_reverse_iterator (begin ());
}
// Serialization
template<class Archive>
void serialize(Archive & ar, const unsigned int /* file_version */){
serialization::collection_size_type s (size_);
ar & serialization::make_nvp("size",s);
if (Archive::is_loading::value) {
size_ = s;
}
}
private:
size_type size_;
typedef const value_type const_value_type;
@@ -962,6 +980,17 @@ namespace boost { namespace numeric { namespace ublas {
return const_reverse_iterator (begin ());
}
// Serialization
template<class Archive>
void serialize(Archive & ar, const unsigned int /* file_version */){
serialization::collection_size_type s (size_);
ar & serialization::make_nvp("size",s);
if (Archive::is_loading::value) {
size_ = s;
}
ar & serialization::make_nvp("index", index_);
}
private:
size_type size_;
size_type index_;
@@ -1196,6 +1225,17 @@ namespace boost { namespace numeric { namespace ublas {
return const_reverse_iterator (begin ());
}
// Serialization
template<class Archive>
void serialize(Archive & ar, const unsigned int /* file_version */){
serialization::collection_size_type s (size_);
ar & serialization::make_nvp("size",s);
if (Archive::is_loading::value) {
size_ = s;
}
ar & serialization::make_nvp("value", value_);
}
private:
size_type size_;
value_type value_;
@@ -1678,6 +1718,21 @@ namespace boost { namespace numeric { namespace ublas {
return reverse_iterator (begin ());
}
// Serialization
template<class Archive>
void serialize(Archive & ar, const unsigned int /* file_version */){
serialization::collection_size_type s (size_);
ar & serialization::make_nvp("size",s);
// copy the value back if loading
if (Archive::is_loading::value) {
if (s > N) bad_size("too large size in bounded_vector::load()\n").raise();
size_ = s;
}
// ISSUE: this writes the full array
ar & serialization::make_nvp("data",data_);
}
private:
size_type size_;
array_type data_;

View File

@@ -1217,6 +1217,30 @@ namespace boost { namespace numeric { namespace ublas {
return reverse_iterator2 (begin2 ());
}
// Serialization
template<class Archive>
void serialize(Archive & ar, const unsigned int /* file_version */){
// we need to copy to a collection_size_type to get a portable
// and efficient serialization
serialization::collection_size_type s1 (size1_);
serialization::collection_size_type s2 (size2_);
// serialize the sizes
ar & serialization::make_nvp("size1",s1)
& serialization::make_nvp("size2",s2);
// copy the values back if loading
if (Archive::is_loading::value) {
size1_ = s1;
size2_ = s2;
}
ar & serialization::make_nvp("data", data_);
storage_invariants();
}
private:
void storage_invariants () const
{

View File

@@ -738,6 +738,17 @@ namespace boost { namespace numeric { namespace ublas {
return reverse_iterator (begin ());
}
// Serialization
template<class Archive>
void serialize(Archive & ar, const unsigned int /* file_version */){
serialization::collection_size_type s (size_);
ar & serialization::make_nvp("size",s);
if (Archive::is_loading::value) {
size_ = s;
}
ar & serialization::make_nvp("data", data_);
}
private:
size_type size_;
array_type data_;
@@ -1318,6 +1329,23 @@ namespace boost { namespace numeric { namespace ublas {
return reverse_iterator (begin ());
}
// Serialization
template<class Archive>
void serialize(Archive & ar, const unsigned int /* file_version */){
serialization::collection_size_type s (size_);
ar & serialization::make_nvp("size",s);
if (Archive::is_loading::value) {
size_ = s;
}
// ISSUE: filled may be much less than capacity
// ISSUE: index_data_ and value_data_ are undefined between filled and capacity (trouble with 'nan'-values)
ar & serialization::make_nvp("capacity", capacity_);
ar & serialization::make_nvp("filled", filled_);
ar & serialization::make_nvp("index_data", index_data_);
ar & serialization::make_nvp("value_data", value_data_);
storage_invariants();
}
private:
void storage_invariants () const
{
@@ -1981,6 +2009,25 @@ namespace boost { namespace numeric { namespace ublas {
return reverse_iterator (begin ());
}
// Serialization
template<class Archive>
void serialize(Archive & ar, const unsigned int /* file_version */){
serialization::collection_size_type s (size_);
ar & serialization::make_nvp("size",s);
if (Archive::is_loading::value) {
size_ = s;
}
// ISSUE: filled may be much less than capacity
// ISSUE: index_data_ and value_data_ are undefined between filled and capacity (trouble with 'nan'-values)
ar & serialization::make_nvp("capacity", capacity_);
ar & serialization::make_nvp("filled", filled_);
ar & serialization::make_nvp("sorted_filled", sorted_filled_);
ar & serialization::make_nvp("sorted", sorted_);
ar & serialization::make_nvp("index_data", index_data_);
ar & serialization::make_nvp("value_data", value_data_);
storage_invariants();
}
private:
void storage_invariants () const
{