vbl_io_sparse_array_base.hxx
Go to the documentation of this file.
1 // This is core/vbl/io/vbl_io_sparse_array_base.hxx
2 #ifndef vbl_io_sparse_array_base_hxx_
3 #define vbl_io_sparse_array_base_hxx_
4 //:
5 // \file
6 
7 #include <iostream>
9 #include <vsl/vsl_pair_io.h>
10 #include <vsl/vsl_binary_io.h>
11 
12 //============================================================================
13 //: Binary save self to stream.
14 template<class T, class Index>
15 void vsl_b_write(vsl_b_ostream &os, const vbl_sparse_array_base<T, Index> & p)
16 {
17  constexpr short io_version_no = 1;
18  vsl_b_write(os, io_version_no);
19 
20  vsl_b_write(os, p.count_nonempty());
21  for (typename vbl_sparse_array_base<T, Index>::const_iterator s = p.begin(); s != p.end(); ++s){
22  // the value_type of a map<Key, T> is "pair<Key const, T>", not "pair<Key, T>".
23  std::pair<Index, T> tt((*s).first, (*s).second);
24  vsl_b_write(os, tt);
25  }
26 }
27 
28 //===========================================================================
29 //: Binary load self from stream.
30 template<class T, class Index>
31 void vsl_b_read(vsl_b_istream &is, vbl_sparse_array_base<T, Index> & p)
32 {
33  if (!is) return;
34 
35  p.clear();
36  short v;
37  vsl_b_read(is, v);
38 
39  switch (v)
40  {
41  case 1: {
42  unsigned int size;
43  vsl_b_read(is, size);
44 
45  std::pair<Index, T> value;
46  for (unsigned i=0; i<size; i++){
47  vsl_b_read(is, value);
48  p(value.first)=value.second;
49  }
50  break;
51  }
52 
53  default:
54  std::cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vbl_sparse_array_base<T, Index> &)\n"
55  << " Unknown version number "<< v << '\n';
56  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
57  return;
58  }
59 }
60 
61 
62 //==========================================================================
63 //: Output a human readable summary to the stream
64 template<class T, class Index>
65 void vsl_print_summary(std::ostream& os,const vbl_sparse_array_base<T, Index> & p)
66 {
67  os<<"nonempty elements: "<< p.count_nonempty() << '\n';
68  int k=0;
69 
71  s != p.end() && k<5; ++s)
72  {
73  k++;
74  os << ' ';
75  vsl_print_summary(os, (*s).first);
76  os << ": ";
77  vsl_print_summary(os, (*s).second);
78  os << '\n';
79  }
80  if (p.count_nonempty() > 5)
81  os << " ...\n";
82 }
83 
84 #define VBL_IO_SPARSE_ARRAY_BASE_INSTANTIATE(T, I) \
85  template void vsl_print_summary(std::ostream &, const vbl_sparse_array_base<T , I > &); \
86  template void vsl_b_read(vsl_b_istream &, vbl_sparse_array_base<T , I > &); \
87  template void vsl_b_write(vsl_b_ostream &, const vbl_sparse_array_base<T , I > &)
88 
89 #endif // vbl_io_sparse_array_base_hxx_
Map::const_iterator const_iterator
The type of iterators into the efficient storage.
size_type count_nonempty() const
Return number of locations that have been assigned a value using "put".
const_iterator end() const
A bidirectional iterator pointing just beyond last non-empty element.
const_iterator begin() const
A bidirectional iterator pointing at the first non-empty element.
void vsl_b_write(vsl_b_ostream &os, const vbl_sparse_array_base< T, Index > &p)
Binary save self to stream.
void clear()
Empty the sparse matrix.
void vsl_b_read(vsl_b_istream &is, vbl_sparse_array_base< T, Index > &p)
Binary load self from stream.
A fully featured sparse array which devolves indexing to its templated type.
void vsl_print_summary(std::ostream &os, const vbl_sparse_array_base< T, Index > &p)
Output a human readable summary to the stream.