diff liboctave/array/chNDArray.cc @ 19508:6c9ea5be96bf

Change charMatrix to subclass charNDArray rather than be another Array<char>. * chMatrix.h: both charMatrix and charNDArray are Array<char>, the first being simply 2 dimensional. We change this so that charMatrix inherits from charNDArray instead. * chMatrix.cc: remove all constructors which are now inherited from charNDArray. * chNDArray.h, chNDArray.cc: implement all constructors here rather than calling charMatrix. Remove matrix_value() since a charMatrix constructor is now enough. * pr-output.cc, octave-value/ov-ch-mat.h, octave-value/ov-str-mat.cc: replace calls to charNDArray::matrix_value () with the charMatrix constructor.
author Carnë Draug <carandraug@octave.org>
date Fri, 24 Oct 2014 01:31:53 +0100
parents 49a5a4be04a1
children 1f4455ff2329
line wrap: on
line diff
--- a/liboctave/array/chNDArray.cc
+++ b/liboctave/array/chNDArray.cc
@@ -25,15 +25,67 @@
 #include <config.h>
 #endif
 
+#include <string>
+
 #include "Array-util.h"
 #include "chNDArray.h"
 #include "mx-base.h"
 #include "lo-ieee.h"
 #include "lo-mappers.h"
 #include "mx-op-defs.h"
+#include "str-vec.h"
 
 #include "bsxfun-defs.cc"
 
+charNDArray::charNDArray (char c)
+  : Array<char> ()
+{
+  octave_idx_type nc = 1;
+  octave_idx_type nr = 1;
+
+  resize (nr, nc);
+
+  elem (0, 0) = c;
+}
+
+charNDArray::charNDArray (const char *s)
+  : Array<char> ()
+{
+  octave_idx_type nc = s ? strlen (s) : 0;
+  octave_idx_type nr = s && nc > 0 ? 1 : 0;
+
+  resize (nr, nc);
+
+  for (octave_idx_type i = 0; i < nc; i++)
+    elem (0, i) = s[i];
+}
+
+charNDArray::charNDArray (const std::string& s)
+  : Array<char> ()
+{
+  octave_idx_type nc = s.length ();
+  octave_idx_type nr = nc > 0 ? 1 : 0;
+
+  resize (nr, nc);
+
+  for (octave_idx_type i = 0; i < nc; i++)
+    elem (0, i) = s[i];
+}
+
+charNDArray::charNDArray (const string_vector& s, char fill_value)
+  : Array<char> (dim_vector (s.length (), s.max_length ()), fill_value)
+{
+  octave_idx_type nr = rows ();
+
+  for (octave_idx_type i = 0; i < nr; i++)
+    {
+      const std::string si = s(i);
+      octave_idx_type nc = si.length ();
+      for (octave_idx_type j = 0; j < nc; j++)
+        elem (i, j) = si[j];
+    }
+}
+
 // FIXME: this is not quite the right thing.
 
 boolNDArray
@@ -130,12 +182,6 @@
   return *this;
 }
 
-charMatrix
-charNDArray::matrix_value (void) const
-{
-  return *this;
-}
-
 void
 charNDArray::increment_index (Array<octave_idx_type>& ra_idx,
                               const dim_vector& dimensions,