core/vbl/io/vbl_io_sparse_array_base.txx
Go to the documentation of this file.
00001 // This is core/vbl/io/vbl_io_sparse_array_base.txx
00002 #ifndef vbl_io_sparse_array_base_txx_
00003 #define vbl_io_sparse_array_base_txx_
00004 //:
00005 // \file
00006 
00007 #include "vbl_io_sparse_array_base.h"
00008 #include <vsl/vsl_pair_io.h>
00009 #include <vsl/vsl_binary_io.h>
00010 
00011 //============================================================================
00012 //: Binary save self to stream.
00013 template<class T, class Index>
00014 void vsl_b_write(vsl_b_ostream &os, const vbl_sparse_array_base<T, Index> & p)
00015 {
00016   const short io_version_no = 1;
00017   vsl_b_write(os, io_version_no);
00018 
00019   vsl_b_write(os, p.count_nonempty());
00020   for (typename vbl_sparse_array_base<T, Index>::const_iterator s = p.begin(); s != p.end(); ++s){
00021     // the value_type of a map<Key, T> is "pair<Key const, T>", not "pair<Key, T>".
00022     vcl_pair<Index, T> tt((*s).first, (*s).second);
00023     vsl_b_write(os, tt);
00024   }
00025 }
00026 
00027 //===========================================================================
00028 //: Binary load self from stream.
00029 template<class T, class Index>
00030 void vsl_b_read(vsl_b_istream &is, vbl_sparse_array_base<T, Index> & p)
00031 {
00032   if (!is) return;
00033 
00034   p.clear();
00035   short v;
00036   vsl_b_read(is, v);
00037 
00038   switch (v)
00039   {
00040    case 1: {
00041     unsigned int size;
00042     vsl_b_read(is, size);
00043 
00044 #ifdef VCL_SUNPRO_CC_50
00045     // SunPro 5.0 (CC -g -c) generates wrong code (duplicate symbols).
00046     Index value_first;
00047     T     value_second;
00048     for (unsigned i=0; i<size; i++){
00049       vsl_b_read(is, value_first);
00050       vsl_b_read(is, value_second);
00051       p(value_first) = value_second;
00052     }
00053 #else
00054     vcl_pair<Index, T> value;
00055     for (unsigned i=0; i<size; i++){
00056       vsl_b_read(is, value);
00057       p(value.first)=value.second;
00058     }
00059 #endif
00060     break;
00061    }
00062 
00063    default:
00064     vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vbl_sparse_array_base<T, Index> &)\n"
00065              << "           Unknown version number "<< v << '\n';
00066     is.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00067     return;
00068   }
00069 }
00070 
00071 
00072 //==========================================================================
00073 //: Output a human readable summary to the stream
00074 template<class T, class Index>
00075 void vsl_print_summary(vcl_ostream& os,const vbl_sparse_array_base<T, Index> & p)
00076 {
00077   os<<"nonempty elements: "<< p.count_nonempty() << '\n';
00078   int k=0;
00079 
00080   for (typename vbl_sparse_array_base<T, Index>::const_iterator s = p.begin();
00081        s != p.end() && k<5; ++s)
00082   {
00083     k++;
00084     os << ' ';
00085     vsl_print_summary(os, (*s).first);
00086     os << ": ";
00087     vsl_print_summary(os, (*s).second);
00088     os << '\n';
00089   }
00090   if (p.count_nonempty() > 5)
00091     os << " ...\n";
00092 }
00093 
00094 #define VBL_IO_SPARSE_ARRAY_BASE_INSTANTIATE(T, I) \
00095   template void vsl_print_summary(vcl_ostream &, const vbl_sparse_array_base<T , I > &); \
00096   template void vsl_b_read(vsl_b_istream &, vbl_sparse_array_base<T , I > &); \
00097   template void vsl_b_write(vsl_b_ostream &, const vbl_sparse_array_base<T , I > &)
00098 
00099 #endif // vbl_io_sparse_array_base_txx_