vnl_io_vector.hxx
Go to the documentation of this file.
1 // This is core/vnl/io/vnl_io_vector.hxx
2 #ifndef vnl_io_vector_hxx_
3 #define vnl_io_vector_hxx_
4 //:
5 // \file
6 
7 #include "vnl_io_vector.h"
8 #include <vsl/vsl_binary_io.h>
9 #include <vsl/vsl_b_read_block_old.h>
10 #include <vsl/vsl_block_binary.h>
11 
12 //=================================================================================
13 //: Binary save self to stream.
14 template<class T>
15 void vsl_b_write(vsl_b_ostream & os, const vnl_vector<T> & p)
16 {
17  constexpr short io_version_no = 2;
18  vsl_b_write(os, io_version_no);
19  vsl_b_write(os, p.size());
20  if (p.size())
21  vsl_block_binary_write(os, p.begin(), p.size());
22 }
23 
24 //=================================================================================
25 //: Binary load self from stream.
26 template<class T>
27 void vsl_b_read(vsl_b_istream &is, vnl_vector<T> & p)
28 {
29  if (!is) return;
30 
31  short ver;
32  unsigned n;
33  vsl_b_read(is, ver);
34  switch (ver)
35  {
36  case 1:
37  vsl_b_read(is, n);
38  p.set_size(n);
39  if (n)
40  vsl_b_read_block_old(is, p.data_block(), n);
41  break;
42 
43  case 2:
44  vsl_b_read(is, n);
45  p.set_size(n);
46  if (n)
47  vsl_block_binary_read(is, p.data_block(), n);
48  break;
49 
50  default:
51  std::cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vnl_vector<T>&)\n"
52  << " Unknown version number "<< ver << '\n';
53  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
54  return;
55  }
56 }
57 
58 //====================================================================================
59 //: Output a human readable summary to the stream
60 template<class T>
61 void vsl_print_summary(std::ostream & os,const vnl_vector<T> & p)
62 {
63  os<<"Len: "<<p.size()<<" (";
64  for ( unsigned int i =0; i < p.size() && i < 5; ++i )
65  os << p.operator()(i) <<' ';
66  if (p.size() > 5) os << " ...";
67  os << ')';
68 }
69 
70 #define VNL_IO_VECTOR_INSTANTIATE(T) \
71 template VNL_EXPORT void vsl_print_summary(std::ostream &, const vnl_vector<T > &); \
72 template VNL_EXPORT void vsl_b_read(vsl_b_istream &, vnl_vector<T > &); \
73 template VNL_EXPORT void vsl_b_write(vsl_b_ostream &, const vnl_vector<T > &)
74 
75 #endif // vnl_io_vector_hxx_
void vsl_print_summary(std::ostream &os, vnl_bignum const &b)
Print human readable summary of object to a stream.
bool set_size(size_t n)
Resize to n elements.
Definition: vnl_vector.hxx:250
size_t size() const
Return the length, number of elements, dimension of this vector.
Definition: vnl_vector.h:126
T const * data_block() const
Access the contiguous block storing the elements in the vector. O(1).
Definition: vnl_vector.h:230
iterator begin()
Iterator pointing to start of data.
Definition: vnl_vector.h:243
void vsl_b_read(vsl_b_istream &is, vnl_bignum &v)
Binary load vnl_bignum from stream.
void vsl_b_write(vsl_b_ostream &os, vnl_bignum const &v)
Binary save vnl_bignum to stream.
Mathematical vector class, templated by type of element.
Definition: vnl_fwd.h:16