vbl_io_array_2d.hxx
Go to the documentation of this file.
1 // This is core/vbl/io/vbl_io_array_2d.hxx
2 #ifndef vbl_io_array_2d_hxx_
3 #define vbl_io_array_2d_hxx_
4 //:
5 // \file
6 // \brief binary IO functions for vbl_array_2d<T>
7 // \author K.Y.McGaul
8 #include <iostream>
9 #include "vbl_io_array_2d.h"
10 #include <vsl/vsl_binary_io.h>
11 #include <vbl/vbl_array_2d.h>
12 
13 //=======================================================================
14 //: Binary save self to stream.
15 template<class T>
16 void vsl_b_write(vsl_b_ostream & os, const vbl_array_2d<T> &p)
17 {
18  constexpr short io_version_no = 1;
19  vsl_b_write(os, io_version_no);
20 
21  typedef typename vbl_array_2d<T>::size_type size_type;
22  size_type array_rows = p.rows();
23  size_type array_cols = p.cols();
24  vsl_b_write(os, array_rows);
25  vsl_b_write(os, array_cols);
26  for (size_type i=0; i<array_rows; i++)
27  {
28  for (size_type j=0; j<array_cols; j++)
29  vsl_b_write(os, p(i,j));
30  }
31 }
32 
33 //=======================================================================
34 //: Binary load self from stream.
35 template<class T>
36 void vsl_b_read(vsl_b_istream &is, vbl_array_2d<T> &p)
37 {
38  if (!is) return;
39 
40  short ver;
41  typedef typename vbl_array_2d<T>::size_type size_type;
42  size_type array_rows, array_cols;
43  vsl_b_read(is, ver);
44  switch (ver)
45  {
46  case 1:
47  vsl_b_read(is, array_rows);
48  vsl_b_read(is, array_cols);
49  p.resize(array_rows, array_cols);
50  for (size_type i=0; i<array_rows; i++)
51  {
52  for (size_type j=0; j<array_cols; j++)
53  vsl_b_read(is, p(i,j));
54  }
55  break;
56 
57  default:
58  std::cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vbl_array_2d<T>&)\n"
59  << " Unknown version number "<< ver << '\n';
60  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
61  return;
62  }
63 }
64 
65 //=======================================================================
66 //: Output a human readable summary to the stream
67 template<class T>
68 void vsl_print_summary(std::ostream & os,const vbl_array_2d<T> & p)
69 {
70  os << "Rows: " << p.rows() << std::endl
71  << "Columns: " << p.cols() << std::endl;
72  typedef typename vbl_array_2d<T>::size_type size_type;
73  for (size_type i =0; i<p.rows() && i<5; i++)
74  {
75  for (size_type j=0; j<p.cols() && j<5; j++)
76  {
77  os << ' ';
78  vsl_print_summary(os, p(i,j));
79  }
80  if (p.cols() > 5)
81  os << "...";
82  os << std::endl;
83  }
84  if (p.rows() > 5)
85  os << " ...\n";
86 }
87 
88 #define VBL_IO_ARRAY_2D_INSTANTIATE(T) \
89 template void vsl_print_summary(std::ostream &, const vbl_array_2d<T > &); \
90 template void vsl_b_read(vsl_b_istream &, vbl_array_2d<T > &); \
91 template void vsl_b_write(vsl_b_ostream &, const vbl_array_2d<T > &)
92 
93 #endif // vbl_io_array_2d_hxx_
simple 2D array.
Definition: vbl_array_2d.h:25
void vsl_b_read(vsl_b_istream &is, vbl_array_2d< T > &p)
Binary load self from stream.
std::size_t size_type
Definition: vbl_array_2d.h:28
void vsl_b_write(vsl_b_ostream &os, const vbl_array_2d< T > &p)
Binary save self to stream.
size_type rows() const
Return number of rows.
Definition: vbl_array_2d.h:122
size_type cols() const
Return number of columns.
Definition: vbl_array_2d.h:125
void vsl_print_summary(std::ostream &os, const vbl_array_2d< T > &p)
Output a human readable summary to the stream.
void resize(size_type m, size_type n)
change size.
Definition: vbl_array_2d.h:95
Contains class for a templated 2d array.