changeset 8732:a669df7beb73

[mq]: x
author John W. Eaton <jwe@octave.org>
date Thu, 12 Feb 2009 14:37:43 -0500
parents 5abe5ae55465
children 3ef774603887
files src/Cell.cc src/Cell.h src/ChangeLog src/TEMPLATE-INST/Array-tc.cc src/ov-base.cc src/ov-base.h src/ov-cell.cc src/ov-cell.h src/ov.h
diffstat 9 files changed, 123 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/src/Cell.cc	Thu Feb 12 10:00:43 2009 +0100
+++ b/src/Cell.cc	Thu Feb 12 14:37:43 2009 -0500
@@ -61,6 +61,18 @@
     }
 }
 
+Cell::Cell (const Array<std::string>& sa)
+  : ArrayN<octave_value> (sa.dims ())
+{
+  octave_idx_type n = sa.numel ();
+
+  octave_value *dst = fortran_vec ();
+  const std::string *src = sa.data ();
+
+  for (octave_idx_type i = 0; i < n; i++)
+    dst[i] = src[i];
+}
+
 // Set size to DV, filling with [].  Then fill with as many elements of
 // SV as possible.
 
--- a/src/Cell.h	Thu Feb 12 10:00:43 2009 +0100
+++ b/src/Cell.h	Thu Feb 12 14:37:43 2009 -0500
@@ -64,6 +64,8 @@
 
   Cell (const string_vector& sv, bool trim = false);
 
+  Cell (const Array<std::string>& sa);
+
   Cell (const dim_vector& dv, const string_vector& sv, bool trim = false);
 
   Cell (const Cell& c)
--- a/src/ChangeLog	Thu Feb 12 10:00:43 2009 +0100
+++ b/src/ChangeLog	Thu Feb 12 14:37:43 2009 -0500
@@ -1,5 +1,17 @@
 2009-02-12  John W. Eaton  <jwe@octave.org>
 
+	* TEMPLATE-INST/Array-tc.cc: Don't instantiate sort functions for
+	Arrays of octave_value objects.
+	(octave_sort<octave_value>::ascending_compare,
+	octave_sort<octave_value>::descending_compare): Delete.
+
+	* ov.h (octave_value::cellstr_value): New function.
+	* ov-base.cc, ov-base.h (octave_base_value::cellstr_value):
+	New function.
+	* ov-cell.h (octave_cell::cellstr_value, octave_cell::sort,
+	octave_cell::sortrows_idx): New functions
+	* Cell.h, Cell.cc (Cell::Cell (Array<std::string>)): New constructor.
+
 	* TEMPLATE-INST/Array-tc.cc: Undo previous change.
 	(octave_sort<octave_value>::ascending_compare,
 	octave_sort<octave_value>::descending_compare):
--- a/src/TEMPLATE-INST/Array-tc.cc	Thu Feb 12 10:00:43 2009 +0100
+++ b/src/TEMPLATE-INST/Array-tc.cc	Thu Feb 12 14:37:43 2009 -0500
@@ -35,31 +35,11 @@
 #include "ArrayN.h"
 #include "ArrayN.cc"
 
-#include "DiagArray2.h"
-#include "DiagArray2.cc"
-
 #include "oct-obj.h"
 
 #include "oct-sort.cc"
 
-// FIXME -- these comparisons don't look right.  Where do we sort
-// octave_value objects and expect them to be character strings?
-
-template <>
-bool
-octave_sort<octave_value>::ascending_compare (const octave_value& a, const octave_value& b)
-{
-  return (a.string_value () < b.string_value ());
-}
-
-template <>
-bool
-octave_sort<octave_value>::descending_compare (const octave_value& a, const octave_value& b)
-{
-  return (a.string_value () > b.string_value ());
-}
-
-INSTANTIATE_ARRAY_SORT (octave_value);
+NO_INSTANTIATE_ARRAY_SORT (octave_value);
 
 INSTANTIATE_ARRAY (octave_value, OCTINTERP_API);
 
--- a/src/ov-base.cc	Thu Feb 12 10:00:43 2009 +0100
+++ b/src/ov-base.cc	Thu Feb 12 14:37:43 2009 -0500
@@ -778,6 +778,15 @@
   return retval;
 }
 
+Array<std::string>
+octave_base_value::cellstr_value (void) const
+{
+  Array<std::string> retval;
+  gripe_wrong_type_arg ("octave_base_value::cellstry_value()",
+			type_name ());
+  return retval;
+}
+
 Range
 octave_base_value::range_value (void) const
 {
--- a/src/ov-base.h	Thu Feb 12 10:00:43 2009 +0100
+++ b/src/ov-base.h	Thu Feb 12 14:37:43 2009 -0500
@@ -432,6 +432,8 @@
 
   virtual std::string string_value (bool force = false) const;
 
+  virtual Array<std::string> cellstr_value (void) const;
+
   virtual Range range_value (void) const;
 
   virtual Octave_map map_value (void) const;
--- a/src/ov-cell.cc	Thu Feb 12 10:00:43 2009 +0100
+++ b/src/ov-cell.cc	Thu Feb 12 14:37:43 2009 -0500
@@ -387,6 +387,58 @@
   return retval;
 }
 
+octave_value
+octave_cell::sort (octave_idx_type dim, sortmode mode) const
+{
+  octave_value retval;
+
+  if (is_cellstr ())
+    {
+      Array<std::string> tmp = cellstr_value ();
+
+      retval = Cell (tmp.sort (dim, mode));
+    }
+  else
+    error ("sort: only cell arrays of character strings may be sorted");
+  
+  return retval;
+}
+
+octave_value
+octave_cell::sort (Array<octave_idx_type> &sidx, octave_idx_type dim,
+		   sortmode mode) const
+{
+  octave_value retval;
+
+  if (is_cellstr ())
+    {
+      Array<std::string> tmp = cellstr_value ();
+
+      retval = Cell (tmp.sort (sidx, dim, mode));
+    }
+  else
+    error ("sort: only cell arrays of character strings may be sorted");
+  
+  return retval;
+}
+
+Array<octave_idx_type>
+octave_cell::sortrows_idx (sortmode mode) const
+{
+  Array<octave_idx_type> retval;
+
+  if (is_cellstr ())
+    {
+      Array<std::string> tmp = cellstr_value ();
+
+      retval = tmp.sort_rows_idx (mode);
+    }
+  else
+    error ("sortrows: only cell arrays of character strings may be sorted");
+  
+  return retval;
+}
+
 bool
 octave_cell::is_true (void) const
 {
@@ -460,6 +512,27 @@
   return retval;
 }
 
+Array<std::string>
+octave_cell::cellstr_value (void) const
+{
+  Array<std::string> retval (dims ());
+
+  if (is_cellstr ())
+    {
+      octave_idx_type n = numel ();
+
+      std::string *dst = retval.fortran_vec ();
+      const octave_value *src = matrix.data ();
+
+      for (octave_idx_type i = 0; i < n; i++)
+	dst[i] = src[i].string_value ();
+    }
+  else
+    error ("invalid conversion from cell array to Array<std::string>");
+
+  return retval;
+}
+
 bool
 octave_cell::print_as_scalar (void) const
 {
--- a/src/ov-cell.h	Thu Feb 12 10:00:43 2009 +0100
+++ b/src/ov-cell.h	Thu Feb 12 14:37:43 2009 -0500
@@ -88,6 +88,13 @@
 
   size_t byte_size (void) const;
 
+  octave_value sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const;
+
+  octave_value sort (Array<octave_idx_type> &sidx, octave_idx_type dim = 0,
+		     sortmode mode = ASCENDING) const;
+
+  Array<octave_idx_type> sortrows_idx (sortmode mode = ASCENDING) const;
+
   bool is_matrix_type (void) const { return false; }
 
   bool is_numeric_type (void) const { return false; }
@@ -111,6 +118,8 @@
 
   string_vector all_strings (bool pad = false) const;
 
+  Array<std::string> cellstr_value (void) const;
+
   bool print_as_scalar (void) const;
 
   void print (std::ostream& os, bool pr_as_read_syntax = false) const;
--- a/src/ov.h	Thu Feb 12 10:00:43 2009 +0100
+++ b/src/ov.h	Thu Feb 12 14:37:43 2009 -0500
@@ -794,6 +794,9 @@
   std::string string_value (bool force = false) const
     { return rep->string_value (force); }
 
+  Array<std::string> cellstr_value (void) const
+    { return rep->cellstr_value (); }
+
   Range range_value (void) const
     { return rep->range_value (); }