core/vsl/vsl_map_io.txx
Go to the documentation of this file.
00001 // This is core/vsl/vsl_map_io.txx
00002 #ifndef vsl_map_io_txx_
00003 #define vsl_map_io_txx_
00004 //:
00005 // \file
00006 // \brief Implementation binary IO functions for vcl_(multi)map<Key, T, Compare>
00007 // \author K.Y.McGaul
00008 //
00009 // \verbatim
00010 //  Modifications
00011 //   IMS - 22 June 2001 - Added IO for multimap
00012 // \endverbatim
00013 
00014 #include "vsl_map_io.h"
00015 #include <vsl/vsl_binary_io.h>
00016 #include <vsl/vsl_indent.h>
00017 #include <vsl/vsl_pair_io.h>
00018 
00019 //====================================================================================
00020 //: Write map to binary stream
00021 template <class Key, class T, class Compare>
00022 void vsl_b_write(vsl_b_ostream& s, const vcl_map<Key, T, Compare>& v)
00023 {
00024   const short version_no = 1;
00025   vsl_b_write(s, version_no);
00026   vsl_b_write(s, v.size());
00027   for (typename vcl_map<Key, T, Compare>::const_iterator iter = v.begin(); iter != v.end(); iter++)
00028   {
00029     vsl_b_write(s,(*iter).first);
00030     vsl_b_write(s,(*iter).second);
00031   }
00032 }
00033 
00034 //====================================================================================
00035 //: Read map from binary stream
00036 template <class Key, class T, class Compare>
00037 void vsl_b_read(vsl_b_istream& is, vcl_map<Key, T, Compare>& v)
00038 {
00039   if (!is) return;
00040 
00041   v.clear();
00042   unsigned map_size;
00043   short ver;
00044   vsl_b_read(is, ver);
00045   switch (ver)
00046   {
00047   case 1:
00048     vsl_b_read(is, map_size);
00049     for (unsigned i=0; i<map_size; i++)
00050     {
00051       Key first_val;
00052       T second_val;
00053       vsl_b_read(is, first_val);
00054       vsl_b_read(is, second_val);
00055       v[first_val] = second_val;
00056     }
00057     break;
00058   default:
00059     vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vcl_map<K, T>&)\n"
00060              << "           Unknown version number "<< ver << '\n';
00061     is.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00062     return;
00063   }
00064 }
00065 
00066 //====================================================================================
00067 //: Output a human readable summary to the stream
00068 template <class Key, class T, class Compare>
00069 void vsl_print_summary(vcl_ostream& os, const vcl_map<Key, T, Compare> &v)
00070 {
00071   os << "Map size: " << v.size() << '\n';
00072   unsigned i=0;
00073   for (typename vcl_map<Key, T, Compare>::const_iterator iter = v.begin();
00074        iter != v.end() && i< 5; ++iter,++i)
00075   {
00076     os << vsl_indent() << ' ' << i << ": ";
00077     vsl_print_summary(os, (*iter).first);
00078     os << ", ";
00079     vsl_indent_inc(os);
00080     vsl_print_summary(os, (*iter).second);
00081     os << '\n';
00082     vsl_indent_dec(os);
00083  }
00084   if (v.size() > 5)
00085     os << " ...\n";
00086 }
00087 
00088 
00089 #define VSL_MAP_IO_INSTANTIATE(Key, T, Compare) \
00090 template void vsl_print_summary(vcl_ostream&, const vcl_map<Key, T, Compare >&); \
00091 template void vsl_b_write(vsl_b_ostream& s, const vcl_map<Key, T, Compare >& v); \
00092 template void vsl_b_read(vsl_b_istream& s, vcl_map<Key, T, Compare >& v)
00093 
00094 //====================================================================================
00095 //: Write multimap to binary stream
00096 template <class Key, class T, class Compare>
00097 void vsl_b_write(vsl_b_ostream& s, const vcl_multimap<Key, T, Compare>& v)
00098 {
00099   const short version_no = 1;
00100   vsl_b_write(s, version_no);
00101   vsl_b_write(s, v.size());
00102   for (typename vcl_multimap<Key, T, Compare>::const_iterator iter = v.begin();
00103        iter != v.end(); iter++)
00104   {
00105     vsl_b_write(s,(*iter).first);
00106     vsl_b_write(s,(*iter).second);
00107   }
00108 }
00109 
00110 //====================================================================================
00111 //: Read multimap from binary stream
00112 template <class Key, class T, class Compare>
00113 void vsl_b_read(vsl_b_istream& is, vcl_multimap<Key, T, Compare>& v)
00114 {
00115   if (!is) return;
00116 
00117   v.clear();
00118   unsigned multimap_size;
00119   short ver;
00120   vsl_b_read(is, ver);
00121   switch (ver)
00122   {
00123   case 1:
00124     vsl_b_read(is, multimap_size);
00125     for (unsigned i=0; i<multimap_size; i++)
00126     {
00127       Key first_val;
00128       T second_val;
00129       vsl_b_read(is, first_val);
00130       vsl_b_read(is, second_val);
00131       v.insert( vcl_make_pair(first_val, second_val));
00132     }
00133     break;
00134   default:
00135     vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vcl_multimap<K, T>&)\n"
00136              << "           Unknown version number "<< ver << '\n';
00137     is.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00138     return;
00139   }
00140 }
00141 
00142 //====================================================================================
00143 //: Output a human readable summary to the stream
00144 template <class Key, class T, class Compare>
00145 void vsl_print_summary(vcl_ostream& os, const vcl_multimap<Key, T, Compare> &v)
00146 {
00147   os << "multimap size: " << v.size() << '\n';
00148   unsigned i=0;
00149   for (typename vcl_multimap<Key, T, Compare>::const_iterator iter = v.begin();
00150        iter != v.end() && i< 5; ++iter,++i)
00151   {
00152     os << ' ' << i << ": ";
00153     vsl_print_summary(os, (*iter).first);
00154     os << ", ";
00155     vsl_print_summary(os, (*iter).second);
00156     os << '\n';
00157   }
00158   if (v.size() > 5)
00159     os << " ...\n";
00160 }
00161 
00162 
00163 #define VSL_MULTIMAP_IO_INSTANTIATE(Key, T, Compare) \
00164 template void vsl_print_summary(vcl_ostream&, const vcl_multimap<Key, T, Compare >&); \
00165 template void vsl_b_write(vsl_b_ostream& s, const vcl_multimap<Key, T, Compare >& v); \
00166 template void vsl_b_read(vsl_b_istream& s, vcl_multimap<Key, T, Compare >& v)
00167 
00168 #endif // vsl_map_io_txx_