diff src/utils.cc @ 10033:f349847c4541

optimize cellfun (@subsref, args, {subs}, uniformoutput, true) case
author Jaroslav Hajek <highegg@gmail.com>
date Sun, 27 Dec 2009 21:56:53 +0100
parents 10519b4d6507
children 2cd940306a06
line wrap: on
line diff
--- a/src/utils.cc
+++ b/src/utils.cc
@@ -1045,6 +1045,77 @@
   return retval;
 }
 
+void
+decode_subscripts (const char* name, const octave_value& arg,
+		   std::string& type_string,
+		   std::list<octave_value_list>& idx)
+{
+  Octave_map m = arg.map_value ();
+
+  if (! error_state
+      && m.nfields () == 2 && m.contains ("type") && m.contains ("subs"))
+    {
+      Cell& type = m.contents ("type");
+      Cell& subs = m.contents ("subs");
+
+      type_string = std::string (type.length(), '\0');
+
+      for (int k = 0; k < type.length (); k++)
+	{
+	  std::string item = type(k).string_value ();
+
+	  if (! error_state)
+	    {
+	      if (item == "{}")
+		type_string[k] = '{';
+	      else if (item == "()")
+		type_string[k] = '(';
+	      else if (item == ".")
+		type_string[k] = '.';
+	      else
+		{
+		  error("%s: invalid indexing type `%s'", name, item.c_str ());
+		  return;
+		}
+	    }
+	  else
+	    {
+	      error ("%s: expecting type(%d) to be a character string",
+		     name, k+1);
+	      return;
+	    }
+
+	  octave_value_list idx_item;
+
+	  if (subs(k).is_string ())
+	    idx_item(0) = subs(k);
+	  else if (subs(k).is_cell ())
+	    {
+	      Cell subs_cell = subs(k).cell_value ();
+
+	      for (int n = 0; n < subs_cell.length (); n++)
+		{
+		  if (subs_cell(n).is_string ()
+		      && subs_cell(n).string_value () == ":")
+		    idx_item(n) = octave_value(octave_value::magic_colon_t);
+		  else
+		    idx_item(n) = subs_cell(n);
+		}
+	    }
+	  else
+	    {
+	      error ("%s: expecting subs(%d) to be a character string or cell array",
+		     name, k+1);
+	      return;
+	    }
+
+	  idx.push_back (idx_item);
+	}
+    }
+  else
+    error ("%s: second argument must be a structure with fields `type' and `subs'", name);
+}
+
 Matrix
 identity_matrix (octave_idx_type nr, octave_idx_type nc)
 {