diff src/ov-re-diag.cc @ 8381:ad896677a2e2

implement binary saving of diag & perm matrices
author Jaroslav Hajek <highegg@gmail.com>
date Mon, 08 Dec 2008 12:31:57 +0100
parents 8b1a2555c4e2
children d95282fa0579
line wrap: on
line diff
--- a/src/ov-re-diag.cc	Sun Dec 07 10:29:34 2008 +0100
+++ b/src/ov-re-diag.cc	Mon Dec 08 12:31:57 2008 +0100
@@ -24,11 +24,14 @@
 #include <config.h>
 #endif
 
+#include "byte-swap.h"
+
 #include "ov-re-diag.h"
 #include "ov-flt-re-diag.h"
 #include "ov-base-diag.cc"
 #include "ov-scalar.h"
 #include "ov-re-mat.h"
+#include "ls-utils.h"
 
 template class octave_base_diag<DiagMatrix, Matrix>;
 
@@ -125,3 +128,64 @@
 {
   return DiagMatrix (matrix.rows (), matrix.cols ());
 }
+
+bool 
+octave_diag_matrix::save_binary (std::ostream& os, bool& save_as_floats)
+{
+
+  int32_t r = matrix.rows (), c = matrix.cols ();
+  os.write (reinterpret_cast<char *> (&r), 4);
+  os.write (reinterpret_cast<char *> (&c), 4);
+
+  Matrix m = Matrix (matrix.diag ());
+  save_type st = LS_DOUBLE;
+  if (save_as_floats)
+    {
+      if (m.too_large_for_float ())
+	{
+	  warning ("save: some values too large to save as floats --");
+	  warning ("save: saving as doubles instead");
+	}
+      else
+	st = LS_FLOAT;
+    }
+  else if (matrix.length () > 8192) // FIXME -- make this configurable.
+    {
+      double max_val, min_val;
+      if (m.all_integers (max_val, min_val))
+	st = get_save_type (max_val, min_val);
+    }
+
+  const double *mtmp = m.data ();
+  write_doubles (os, mtmp, st, m.numel ());
+
+  return true;
+}
+
+bool 
+octave_diag_matrix::load_binary (std::istream& is, bool swap,
+				 oct_mach_info::float_format fmt)
+{
+  int32_t r, c;
+  char tmp;
+  if (! (is.read (reinterpret_cast<char *> (&r), 4)
+         && is.read (reinterpret_cast<char *> (&c), 4)
+         && is.read (reinterpret_cast<char *> (&tmp), 1)))
+    return false;
+  if (swap)
+    {
+      swap_bytes<4> (&r);
+      swap_bytes<4> (&c);
+    }
+
+  DiagMatrix m (r, c);
+  double *re = m.fortran_vec ();
+  octave_idx_type len = m.length ();
+  read_doubles (is, re, static_cast<save_type> (tmp), len, swap, fmt);
+  if (error_state || ! is)
+    return false;
+  matrix = m;
+
+  return true;
+}
+