core/vnl/io/vnl_io_sym_matrix.txx
Go to the documentation of this file.
00001 // This is core/vnl/io/vnl_io_sym_matrix.txx
00002 #ifndef vnl_io_sym_matrix_txx_
00003 #define vnl_io_sym_matrix_txx_
00004 //:
00005 // \file
00006 
00007 #include "vnl_io_sym_matrix.h"
00008 #include <vnl/vnl_sym_matrix.h>
00009 #include <vsl/vsl_b_read_block_old.h>
00010 #include <vsl/vsl_block_binary.h>
00011 #include <vsl/vsl_indent.h>
00012 
00013 //=================================================================================
00014 //: Binary save self to stream.
00015 template<class T>
00016 void vsl_b_write(vsl_b_ostream & os, const vnl_sym_matrix<T> & p)
00017 {
00018   const short version_no = 2;
00019   vsl_b_write(os, version_no);
00020   vsl_b_write(os, p.rows());
00021 
00022   // Calling p.begin() on empty matrix causes segfault
00023   if (p.size()>0)
00024     vsl_block_binary_write(os, p.data_block(), p.size());
00025 }
00026 
00027 //=================================================================================
00028 //: Binary load self from stream.
00029 template<class T>
00030 void vsl_b_read(vsl_b_istream &is, vnl_sym_matrix<T> & p)
00031 {
00032   if (!is) return;
00033 
00034   short v;
00035   unsigned n;
00036   vsl_b_read(is, v);
00037   switch (v)
00038   {
00039    case 1:
00040     vsl_b_read(is, n);
00041     p.set_size(n);
00042     // Calling begin() on empty matrix causes segfault
00043     if (n>0)
00044       vsl_b_read_block_old(is, p.data_block(), p.size());
00045     break;
00046 
00047    case 2:
00048     vsl_b_read(is, n);
00049     p.set_size(n);
00050     // Calling begin() on empty matrix causes segfault
00051     if (n>0)
00052       vsl_block_binary_read(is, p.data_block(), p.size());
00053     break;
00054 
00055    default:
00056     vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vnl_sym_matrix<T>&)\n"
00057              << "           Unknown version number "<< v << '\n';
00058     is.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00059     return;
00060   }
00061 }
00062 
00063 //====================================================================================
00064 //: Output a human readable summary to the stream
00065 template<class T>
00066 void vsl_print_summary(vcl_ostream & os,const vnl_sym_matrix<T> & p)
00067 {
00068   os<<"Size: "<<p.rows()<<" x "<<p.cols()<<vcl_endl;
00069 
00070   unsigned int n = 5;
00071 
00072 
00073   if (n>p.cols()) n=p.cols();
00074 
00075   vsl_indent_inc(os);
00076   for (unsigned int i=0;i<n;i++)
00077   {
00078     os<<vsl_indent()<<" (";
00079 
00080     for ( unsigned int j=0; j<=i; j++)
00081       os<<p(i,j)<<' ';
00082     os << vcl_endl;
00083   }
00084   if (p.rows()>n) os <<vsl_indent()<<" (...\n";
00085   vsl_indent_dec(os);
00086 }
00087 
00088 
00089 #define VNL_IO_SYM_MATRIX_INSTANTIATE(T) \
00090 template void vsl_print_summary(vcl_ostream &, const vnl_sym_matrix<T > &); \
00091 template void vsl_b_read(vsl_b_istream &, vnl_sym_matrix<T > &); \
00092 template void vsl_b_write(vsl_b_ostream &, const vnl_sym_matrix<T > &)
00093 
00094 #endif // vnl_io_sym_matrix_txx_