changeset 20978:a5b500efca9f

maint: invert if/else/error instances. * ov-base-diag.cc, ov-base-int.cc, ov-base-mat.cc, ov-base-scalar.cc, ov-base-sparse.cc, ov-base.cc, ov.cc: Invert if/else/error instances.
author John W. Eaton <jwe@octave.org>
date Thu, 24 Dec 2015 12:06:27 -0500
parents c6a170510735
children 0963ed389012
files libinterp/octave-value/ov-base-diag.cc libinterp/octave-value/ov-base-int.cc libinterp/octave-value/ov-base-mat.cc libinterp/octave-value/ov-base-scalar.cc libinterp/octave-value/ov-base-sparse.cc libinterp/octave-value/ov-base.cc libinterp/octave-value/ov.cc
diffstat 7 files changed, 278 insertions(+), 307 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/octave-value/ov-base-diag.cc	Thu Dec 24 11:42:57 2015 -0500
+++ b/libinterp/octave-value/ov-base-diag.cc	Thu Dec 24 12:06:27 2015 -0500
@@ -155,102 +155,98 @@
     {
     case '(':
       {
-        if (type.length () == 1)
-          {
-            octave_value_list jdx = idx.front ();
-
-            // FIXME: Mostly repeated code for cases 1 and 2 could be
-            //        consolidated for DRY (Don't Repeat Yourself).
-            // Check for assignments to diagonal elements which should not
-            // destroy the diagonal property of the matrix.
-            // If D is a diagonal matrix then the assignment can be
-            // 1) linear, D(i) = x, where ind2sub results in case #2 below
-            // 2) subscript D(i,i) = x, where both indices are equal.
-            if (jdx.length () == 1 && jdx(0).is_scalar_type ())
-              {
-                typename DMT::element_type val;
-                int k = 0;
-                try
-                  {
-                    idx_vector ind = jdx(0).index_vector ();
-                    k = 1;
-                    dim_vector dv (matrix.rows (), matrix.cols ());
-                    Array<idx_vector> ivec = ind2sub (dv, ind);
-                    idx_vector i0 = ivec(0);
-                    idx_vector i1 = ivec(1);
-
-                    if (i0(0) == i1(0)
-                        && chk_valid_scalar (rhs, val))
-                      {
-                        matrix.dgelem (i0(0)) = val;
-                        retval = this;
-                        this->count++;
-                        // invalidate cache
-                        dense_cache = octave_value ();
-                      }
-                  }
-                catch (index_exception& e)
-                  {
-                    // Rethrow to allow more info to be reported later.
-                    e.set_pos_if_unset (2, k+1);
-                    throw;
-                  }
-              }
-            else if (jdx.length () == 2
-                     && jdx(0).is_scalar_type () && jdx(1).is_scalar_type ())
-              {
-                typename DMT::element_type val;
-                int k = 0;
-                try
-                  {
-                    idx_vector i0 = jdx(0).index_vector ();
-                    k = 1;
-                    idx_vector i1 = jdx(1).index_vector ();
-                    if (i0(0) == i1(0)
-                        && i0(0) < matrix.rows () && i1(0) < matrix.cols ()
-                        && chk_valid_scalar (rhs, val))
-                      {
-                        matrix.dgelem (i0(0)) = val;
-                        retval = this;
-                        this->count++;
-                        // invalidate cache
-                        dense_cache = octave_value ();
-                      }
-                  }
-                catch (index_exception& e)
-                  {
-                    // Rethrow to allow more info to be reported later.
-                    e.set_pos_if_unset (2, k+1);
-                    throw;
-                  }
-              }
-
-            if (! retval.is_defined ())
-              retval = numeric_assign (type, idx, rhs);
-          }
-        else
+        if (type.length () != 1)
           {
             std::string nm = type_name ();
             error ("in indexed assignment of %s, last lhs index must be ()",
                    nm.c_str ());
           }
+
+        octave_value_list jdx = idx.front ();
+
+        // FIXME: Mostly repeated code for cases 1 and 2 could be
+        //        consolidated for DRY (Don't Repeat Yourself).
+        // Check for assignments to diagonal elements which should not
+        // destroy the diagonal property of the matrix.
+        // If D is a diagonal matrix then the assignment can be
+        // 1) linear, D(i) = x, where ind2sub results in case #2 below
+        // 2) subscript D(i,i) = x, where both indices are equal.
+        if (jdx.length () == 1 && jdx(0).is_scalar_type ())
+          {
+            typename DMT::element_type val;
+            int k = 0;
+            try
+              {
+                idx_vector ind = jdx(0).index_vector ();
+                k = 1;
+                dim_vector dv (matrix.rows (), matrix.cols ());
+                Array<idx_vector> ivec = ind2sub (dv, ind);
+                idx_vector i0 = ivec(0);
+                idx_vector i1 = ivec(1);
+
+                if (i0(0) == i1(0)
+                    && chk_valid_scalar (rhs, val))
+                  {
+                    matrix.dgelem (i0(0)) = val;
+                    retval = this;
+                    this->count++;
+                    // invalidate cache
+                    dense_cache = octave_value ();
+                  }
+              }
+            catch (index_exception& e)
+              {
+                // Rethrow to allow more info to be reported later.
+                e.set_pos_if_unset (2, k+1);
+                throw;
+              }
+          }
+        else if (jdx.length () == 2
+                 && jdx(0).is_scalar_type () && jdx(1).is_scalar_type ())
+          {
+            typename DMT::element_type val;
+            int k = 0;
+            try
+              {
+                idx_vector i0 = jdx(0).index_vector ();
+                k = 1;
+                idx_vector i1 = jdx(1).index_vector ();
+                if (i0(0) == i1(0)
+                    && i0(0) < matrix.rows () && i1(0) < matrix.cols ()
+                    && chk_valid_scalar (rhs, val))
+                  {
+                    matrix.dgelem (i0(0)) = val;
+                    retval = this;
+                    this->count++;
+                    // invalidate cache
+                    dense_cache = octave_value ();
+                  }
+              }
+            catch (index_exception& e)
+              {
+                // Rethrow to allow more info to be reported later.
+                e.set_pos_if_unset (2, k+1);
+                throw;
+              }
+          }
+
+        if (! retval.is_defined ())
+          retval = numeric_assign (type, idx, rhs);
       }
       break;
 
     case '{':
     case '.':
       {
-        if (is_empty ())
-          {
-            octave_value tmp = octave_value::empty_conv (type, rhs);
-
-            retval = tmp.subsasgn (type, idx, rhs);
-          }
-        else
+        if (! is_empty ())
           {
             std::string nm = type_name ();
             error ("%s cannot be indexed with %c", nm.c_str (), type[0]);
           }
+
+        octave_value tmp = octave_value::empty_conv (type, rhs);
+
+        retval = tmp.subsasgn (type, idx, rhs);
       }
       break;
 
@@ -502,30 +498,26 @@
   octave_idx_type c = 0;
   bool success = true;
 
-  if (extract_keyword (is, "rows", r, true)
-      && extract_keyword (is, "columns", c, true))
-    {
-      octave_idx_type l = r < c ? r : c;
-      MT tmp (l, 1);
-      is >> tmp;
+  if (! extract_keyword (is, "rows", r, true)
+      || ! extract_keyword (is, "columns", c, true))
+    error ("load: failed to extract number of rows and columns");
+
+  octave_idx_type l = r < c ? r : c;
+  MT tmp (l, 1);
+  is >> tmp;
 
-      if (! is)
-        error ("load: failed to load diagonal matrix constant");
-      else
-        {
-          // This is a little tricky, as we have the Matrix type, but
-          // not ColumnVector type. We need to help the compiler get
-          // through the inheritance tree.
-          typedef typename DMT::element_type el_type;
-          matrix = DMT (MDiagArray2<el_type> (MArray<el_type> (tmp)));
-          matrix.resize (r, c);
+  if (! is)
+    error ("load: failed to load diagonal matrix constant");
 
-          // Invalidate cache. Probably not necessary, but safe.
-          dense_cache = octave_value ();
-        }
-    }
-  else
-    error ("load: failed to extract number of rows and columns");
+  // This is a little tricky, as we have the Matrix type, but
+  // not ColumnVector type. We need to help the compiler get
+  // through the inheritance tree.
+  typedef typename DMT::element_type el_type;
+  matrix = DMT (MDiagArray2<el_type> (MArray<el_type> (tmp)));
+  matrix.resize (r, c);
+
+  // Invalidate cache. Probably not necessary, but safe.
+  dense_cache = octave_value ();
 
   return success;
 }
--- a/libinterp/octave-value/ov-base-int.cc	Thu Dec 24 11:42:57 2015 -0500
+++ b/libinterp/octave-value/ov-base-int.cc	Thu Dec 24 12:06:27 2015 -0500
@@ -210,25 +210,23 @@
   if (! extract_keyword (is, "ndims", mdims, true))
     error ("load: failed to extract number of dimensions");
 
-  if (mdims >= 0)
-    {
-      dim_vector dv;
-      dv.resize (mdims);
+  if (mdims < 0)
+    error ("load: failed to extract number of rows and columns");
 
-      for (int i = 0; i < mdims; i++)
-        is >> dv(i);
+  dim_vector dv;
+  dv.resize (mdims);
 
-      T tmp(dv);
+  for (int i = 0; i < mdims; i++)
+    is >> dv(i);
 
-      is >> tmp;
+  T tmp(dv);
 
-      if (! is)
-        error ("load: failed to load matrix constant");
+  is >> tmp;
 
-      this->matrix = tmp;
-    }
-  else
-    error ("load: failed to extract number of rows and columns");
+  if (! is)
+    error ("load: failed to load matrix constant");
+
+  this->matrix = tmp;
 
   return success;
 }
--- a/libinterp/octave-value/ov-base-mat.cc	Thu Dec 24 11:42:57 2015 -0500
+++ b/libinterp/octave-value/ov-base-mat.cc	Thu Dec 24 12:06:27 2015 -0500
@@ -86,14 +86,12 @@
             //
             //  x = []; x(i).f = rhs
 
-            if (type[1] == '.')
-              {
-                octave_value tmp = octave_value::empty_conv (type, rhs);
+            if (type[1] != '.')
+              error ("invalid assignment expression");
 
-                retval = tmp.subsasgn (type, idx, rhs);
-              }
-            else
-              error ("invalid assignment expression");
+            octave_value tmp = octave_value::empty_conv (type, rhs);
+
+            retval = tmp.subsasgn (type, idx, rhs);
           }
         else
           {
@@ -107,17 +105,15 @@
     case '{':
     case '.':
       {
-        if (is_empty ())
-          {
-            octave_value tmp = octave_value::empty_conv (type, rhs);
-
-            retval = tmp.subsasgn (type, idx, rhs);
-          }
-        else
+        if (! is_empty ())
           {
             std::string nm = type_name ();
             error ("%s cannot be indexed with %c", nm.c_str (), type[0]);
           }
+
+        octave_value tmp = octave_value::empty_conv (type, rhs);
+
+        retval = tmp.subsasgn (type, idx, rhs);
       }
       break;
 
--- a/libinterp/octave-value/ov-base-scalar.cc	Thu Dec 24 11:42:57 2015 -0500
+++ b/libinterp/octave-value/ov-base-scalar.cc	Thu Dec 24 12:06:27 2015 -0500
@@ -73,14 +73,14 @@
     {
     case '(':
       {
-        if (type.length () == 1)
-          retval = numeric_assign (type, idx, rhs);
-        else
+        if (type.length () != 1)
           {
             std::string nm = type_name ();
             error ("in indexed assignment of %s, last rhs index must be ()",
                    nm.c_str ());
           }
+
+        retval = numeric_assign (type, idx, rhs);
       }
       break;
 
--- a/libinterp/octave-value/ov-base-sparse.cc	Thu Dec 24 11:42:57 2015 -0500
+++ b/libinterp/octave-value/ov-base-sparse.cc	Thu Dec 24 12:06:27 2015 -0500
@@ -88,6 +88,7 @@
             retval = octave_value (matrix.index (i, j, resize_ok));
           }
           break;
+
         default:
           error ("sparse indexing needs 1 or 2 indices");
         }
@@ -142,31 +143,29 @@
     {
     case '(':
       {
-        if (type.length () == 1)
-          retval = numeric_assign (type, idx, rhs);
-        else
+        if (type.length () != 1)
           {
             std::string nm = type_name ();
             error ("in indexed assignment of %s, last lhs index must be ()",
                    nm.c_str ());
           }
+
+        retval = numeric_assign (type, idx, rhs);
       }
       break;
 
     case '{':
     case '.':
       {
-        if (is_empty ())
-          {
-            octave_value tmp = octave_value::empty_conv (type, rhs);
-
-            retval = tmp.subsasgn (type, idx, rhs);
-          }
-        else
+        if (! is_empty ())
           {
             std::string nm = type_name ();
             error ("%s cannot be indexed with %c", nm.c_str (), type[0]);
           }
+
+        octave_value tmp = octave_value::empty_conv (type, rhs);
+
+        retval = tmp.subsasgn (type, idx, rhs);
       }
       break;
 
@@ -447,21 +446,19 @@
   octave_idx_type nc = 0;
   bool success = true;
 
-  if (extract_keyword (is, "nnz", nz, true)
-      && extract_keyword (is, "rows", nr, true)
-      && extract_keyword (is, "columns", nc, true))
-    {
-      T tmp (nr, nc, nz);
+  if (! extract_keyword (is, "nnz", nz, true)
+      || ! extract_keyword (is, "rows", nr, true)
+      || ! extract_keyword (is, "columns", nc, true))
+    error ("load: failed to extract number of rows and columns");
 
-      is >> tmp;
+  T tmp (nr, nc, nz);
 
-      if (! is)
-        error ("load: failed to load matrix constant");
+  is >> tmp;
 
-      matrix = tmp;
-    }
-  else
-    error ("load: failed to extract number of rows and columns");
+  if (! is)
+    error ("load: failed to load matrix constant");
+
+  matrix = tmp;
 
   return success;
 }
--- a/libinterp/octave-value/ov-base.cc	Thu Dec 24 11:42:57 2015 -0500
+++ b/libinterp/octave-value/ov-base.cc	Thu Dec 24 12:06:27 2015 -0500
@@ -227,51 +227,49 @@
 
   if (is_defined ())
     {
-      if (is_numeric_type ())
-        {
-          switch (type[0])
-            {
-            case '(':
-              {
-                if (type.length () == 1)
-                  retval = numeric_assign (type, idx, rhs);
-                else if (is_empty ())
-                  {
-                    // Allow conversion of empty matrix to some other
-                    // type in cases like
-                    //
-                    //  x = []; x(i).f = rhs
-
-                    octave_value tmp = octave_value::empty_conv (type, rhs);
-
-                    retval = tmp.subsasgn (type, idx, rhs);
-                  }
-                else
-                  {
-                    std::string nm = type_name ();
-                    error ("in indexed assignment of %s, last rhs index must be ()",
-                           nm.c_str ());
-                  }
-              }
-              break;
-
-            case '{':
-            case '.':
-              {
-                std::string nm = type_name ();
-                error ("%s cannot be indexed with %c", nm.c_str (), type[0]);
-              }
-              break;
-
-            default:
-              panic_impossible ();
-            }
-        }
-      else
+      if (! is_numeric_type ())
         {
           std::string nm = type_name ();
           error ("can't perform indexed assignment for %s type", nm.c_str ());
         }
+
+      switch (type[0])
+        {
+        case '(':
+          {
+            if (type.length () == 1)
+              retval = numeric_assign (type, idx, rhs);
+            else if (is_empty ())
+              {
+                // Allow conversion of empty matrix to some other
+                // type in cases like
+                //
+                //  x = []; x(i).f = rhs
+
+                octave_value tmp = octave_value::empty_conv (type, rhs);
+
+                retval = tmp.subsasgn (type, idx, rhs);
+              }
+            else
+              {
+                std::string nm = type_name ();
+                error ("in indexed assignment of %s, last rhs index must be ()",
+                       nm.c_str ());
+              }
+          }
+          break;
+
+        case '{':
+        case '.':
+          {
+            std::string nm = type_name ();
+            error ("%s cannot be indexed with %c", nm.c_str (), type[0]);
+          }
+          break;
+
+        default:
+          panic_impossible ();
+        }
     }
   else
     {
@@ -1568,70 +1566,62 @@
                const std::list<octave_value_list>& idx,
                const std::string& who)
 {
-  octave_value retval;
-
   size_t len = type.length ();
 
-  if (len == idx.size ())
-    {
-      Cell type_field (1, len);
-      Cell subs_field (1, len);
-
-      std::list<octave_value_list>::const_iterator p = idx.begin ();
-
-      for (size_t i = 0; i < len; i++)
-        {
-          char t = type[i];
-
-          switch (t)
-            {
-            case '(':
-              type_field(i) = "()";
-              subs_field(i) = Cell (sanitize (*p++));
-              break;
-
-            case '{':
-              type_field(i) = "{}";
-              subs_field(i) = Cell (sanitize (*p++));
-              break;
-
-            case '.':
-              {
-                type_field(i) = ".";
-
-                octave_value_list vlist = *p++;
-
-                if (vlist.length () == 1)
-                  {
-                    octave_value val = vlist(0);
-
-                    if (val.is_string ())
-                      subs_field(i) = val;
-                    else
-                      error ("string argument required for '.' index");
-                  }
-                else
-                  error ("only single argument permitted for '.' index");
-              }
-              break;
-
-            default:
-              panic_impossible ();
-              break;
-            }
-        }
-
-      octave_map m;
-
-      m.assign ("type", type_field);
-      m.assign ("subs", subs_field);
-
-      retval = m;
-    }
-  else
+  if (len != idx.size ())
     error ("invalid index for %s", who.c_str ());
 
-  return retval;
+  Cell type_field (1, len);
+  Cell subs_field (1, len);
+
+  std::list<octave_value_list>::const_iterator p = idx.begin ();
+
+  for (size_t i = 0; i < len; i++)
+    {
+      char t = type[i];
+
+      switch (t)
+        {
+        case '(':
+          type_field(i) = "()";
+          subs_field(i) = Cell (sanitize (*p++));
+          break;
+
+        case '{':
+          type_field(i) = "{}";
+          subs_field(i) = Cell (sanitize (*p++));
+          break;
+
+        case '.':
+          {
+            type_field(i) = ".";
+
+            octave_value_list vlist = *p++;
+
+            if (vlist.length () != 1)
+              error ("only single argument permitted for '.' index");
+
+            octave_value val = vlist(0);
+
+            if (! val.is_string ())
+              error ("string argument required for '.' index");
+
+            subs_field(i) = val;
+          }
+          break;
+
+        default:
+          panic_impossible ();
+          break;
+        }
+    }
+
+  octave_map m;
+
+  m.assign ("type", type_field);
+  m.assign ("subs", subs_field);
+
+  return m;
 }
 
 bool
--- a/libinterp/octave-value/ov.cc	Thu Dec 24 11:42:57 2015 -0500
+++ b/libinterp/octave-value/ov.cc	Thu Dec 24 12:06:27 2015 -0500
@@ -2942,58 +2942,56 @@
 {
   const octave_map m = arg.xmap_value ("%s: second argument must be a structure with fields 'type' and 'subs'", name);
 
-  if (m.nfields () == 2 && m.contains ("type") && m.contains ("subs"))
-    {
-      octave_idx_type nel = m.numel ();
-
-      type_string = std::string (nel, '\0');
-      idx = std::list<octave_value_list> ();
-
-      if (nel == 0)
-        return;
-
-      const Cell type = m.contents ("type");
-      const Cell subs = m.contents ("subs");
-
-      for (int k = 0; k < nel; k++)
-        {
-          std::string item = type(k).xstring_value ("%s: type(%d) must be a string", name, k+1);
-
-          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 ());
-
-          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.numel (); 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: subs(%d) must be a string or cell array", name, k+1);
-
-          idx.push_back (idx_item);
-        }
-    }
-  else
+  if (m.nfields () != 2 || ! m.contains ("type") || ! m.contains ("subs"))
     error ("%s: second argument must be a structure with fields 'type' and 'subs'",
            name);
+
+  octave_idx_type nel = m.numel ();
+
+  type_string = std::string (nel, '\0');
+  idx = std::list<octave_value_list> ();
+
+  if (nel == 0)
+    return;
+
+  const Cell type = m.contents ("type");
+  const Cell subs = m.contents ("subs");
+
+  for (int k = 0; k < nel; k++)
+    {
+      std::string item = type(k).xstring_value ("%s: type(%d) must be a string", name, k+1);
+
+      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 ());
+
+      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.numel (); 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: subs(%d) must be a string or cell array", name, k+1);
+
+      idx.push_back (idx_item);
+    }
 }
 
 DEFUN (subsref, args, nargout,