changeset 23408:0af9a1ae0912

maint: Use std::copy_n and std::fill_n for dim_vectors and idx_vectors. * Sparse.h (SparseRep constructors): Use C++ value-initialization rather than explicit loops to initialize arrays to 0. * Sparse.h: Use std::copy_n rather than std::copy where it seems to make more intuitive sense. * dim-vector.cc (chop_all_singletons): Rename local variable l to nd for clarity. * dim-vector.cc (redim): Replace for loops with std::copy_n and std::fill_n. * dim-vector.h (clonerep): Rename local variable l to nd for clarity. Replace for loop with std::copy_n. * dim-vector.h (resizerep): Rename local variable l to nd for clarity. Replace for loops with std::copy_n and std::fill_n. * dim-vector.h (chop_trailing_singletons):Rename local variable l to nd for clarity. * idx-vector.cc (idx_vector_rep::print, idx_mask_rep::print): Rename local variable ii to just i. * idx-vector.cc (complement): Replace for loop with std::fill_n.
author Rik <rik@octave.org>
date Tue, 18 Apr 2017 11:59:24 -0700
parents e265ae9e7a6c
children 907f8c3e1c8d
files liboctave/array/Sparse.h liboctave/array/dim-vector.cc liboctave/array/dim-vector.h liboctave/array/idx-vector.cc
diffstat 4 files changed, 43 insertions(+), 62 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/array/Sparse.h	Tue Apr 18 12:04:30 2017 +0200
+++ b/liboctave/array/Sparse.h	Tue Apr 18 11:59:24 2017 -0700
@@ -31,14 +31,13 @@
 #include <cassert>
 #include <cstddef>
 
+#include <algorithm>
 #include <iosfwd>
-#include <algorithm>
 
 #include "Array.h"
 #include "dim-vector.h"
 #include "lo-error.h"
 #include "lo-utils.h"
-
 #include "oct-sort.h"
 
 class idx_vector;
@@ -73,35 +72,21 @@
     octave::refcount<int> count;
 
     SparseRep (void)
-      : d (0), r (0), c (new octave_idx_type [1]), nzmx (0), nrows (0),
+      : d (0), r (0), c (new octave_idx_type [1] {}), nzmx (0), nrows (0),
         ncols (0), count (1)
-    {
-      c[0] = 0;
-    }
+    { }
 
     SparseRep (octave_idx_type n)
-      : d (0), r (0), c (new octave_idx_type [n+1]), nzmx (0), nrows (n),
+      : d (0), r (0), c (new octave_idx_type [n+1] {}), nzmx (0), nrows (n),
         ncols (n), count (1)
-    {
-      for (octave_idx_type i = 0; i < n + 1; i++)
-        c[i] = 0;
-    }
+    { }
 
     SparseRep (octave_idx_type nr, octave_idx_type nc, octave_idx_type nz = 0)
-      : d (nz > 0 ? new T [nz] : 0),
-        r (nz > 0 ? new octave_idx_type [nz] : 0),
-        c (new octave_idx_type [nc+1]), nzmx (nz), nrows (nr),
+      : d (nz > 0 ? new T [nz] {} : 0),
+        r (nz > 0 ? new octave_idx_type [nz] {} : 0),
+        c (new octave_idx_type [nc+1] {}), nzmx (nz), nrows (nr),
         ncols (nc), count (1)
-    {
-      for (octave_idx_type i = 0; i < nz; i++)
-        d[i] = T ();
-
-      for (octave_idx_type i = 0; i < nz; i++)
-        r[i] = 0;
-
-      for (octave_idx_type i = 0; i < nc + 1; i++)
-        c[i] = 0;
-    }
+    { }
 
     SparseRep (const SparseRep& a)
       : d (new T [a.nzmx]), r (new octave_idx_type [a.nzmx]),
@@ -109,9 +94,9 @@
         nzmx (a.nzmx), nrows (a.nrows), ncols (a.ncols), count (1)
     {
       octave_idx_type nz = a.nnz ();
-      std::copy (a.d, a.d + nz, d);
-      std::copy (a.r, a.r + nz, r);
-      std::copy (a.c, a.c + ncols + 1, c);
+      std::copy_n (a.d, nz, d);
+      std::copy_n (a.r, nz, r);
+      std::copy_n (a.c, ncols + 1, c);
     }
 
     ~SparseRep (void) { delete [] d; delete [] r; delete [] c; }
--- a/liboctave/array/dim-vector.cc	Tue Apr 18 12:04:30 2017 +0200
+++ b/liboctave/array/dim-vector.cc	Tue Apr 18 11:59:24 2017 -0700
@@ -55,9 +55,9 @@
   make_unique ();
 
   int j = 0;
-  int l = ndims ();
+  int nd = ndims ();
 
-  for (int i = 0; i < l; i++)
+  for (int i = 0; i < nd; i++)
     {
       if (rep[i] != 1)
         rep[j++] = rep[i];
@@ -282,31 +282,31 @@
     {
       dim_vector retval = alloc (n);
 
-      for (int i = 0; i < n_dims; i++)
-        retval.rep[i] = rep[i];
-
-      for (int i = n_dims; i < n; i++)
-        retval.rep[i] = 1;
+      std::copy_n (rep, n_dims, retval.rep);
+      std::fill_n (retval.rep + n_dims, n - n_dims, 1);
 
       return retval;
     }
   else
     {
-      if (n < 1) n = 1;
+      if (n < 1)
+        n = 1;
 
       dim_vector retval = alloc (n);
 
-      retval.rep[1] = 1;
+      std::copy_n (rep, n-1, retval.rep);
 
-      for (int i = 0; i < n-1; i++)
-        retval.rep[i] = rep[i];
-
+      // Accumulate overflow dimensions into last remaining dimension
       int k = rep[n-1];
       for (int i = n; i < n_dims; i++)
         k *= rep[i];
 
       retval.rep[n-1] = k;
 
+      // All dim_vectors are at least 2-D.  Make Nx1 if necessary.
+      if (n == 1)
+        retval.rep[1] = 1;
+
       return retval;
     }
 }
--- a/liboctave/array/dim-vector.h	Tue Apr 18 12:04:30 2017 +0200
+++ b/liboctave/array/dim-vector.h	Tue Apr 18 11:59:24 2017 -0700
@@ -28,6 +28,7 @@
 
 #include <cassert>
 
+#include <algorithm>
 #include <initializer_list>
 #include <string>
 
@@ -109,12 +110,11 @@
 
   octave_idx_type *clonerep (void)
   {
-    int l = ndims ();
+    int nd = ndims ();
 
-    octave_idx_type* r = newrep (l);
+    octave_idx_type* r = newrep (nd);
 
-    for (int i = 0; i < l; i++)
-      r[i] = rep[i];
+    std::copy_n (rep, nd, r);
 
     return r;
   }
@@ -123,21 +123,18 @@
 
   octave_idx_type *resizerep (int n, octave_idx_type fill_value)
   {
-    int l = ndims ();
+    int nd = ndims ();
 
     if (n < 2)
       n = 2;
 
     octave_idx_type* r = newrep (n);
 
-    if (l > n)
-      l = n;
+    if (nd > n)
+      nd = n;
 
-    int j = 0;
-    for (; j < l; j++)
-      r[j] = rep[j];
-    for (; j < n; j++)
-      r[j] = fill_value;
+    std::copy_n (rep, nd, r);
+    std::fill_n (r + nd, n - nd, fill_value);
 
     return r;
   }
@@ -238,14 +235,14 @@
 
   void chop_trailing_singletons (void)
   {
-    int l = ndims ();
-    if (l > 2 && rep[l-1] == 1)
+    int nd = ndims ();
+    if (nd > 2 && rep[nd-1] == 1)
       {
         make_unique ();
         do
-          l--;
-        while (l > 2 && rep[l-1] == 1);
-        rep[-1] = l;
+          nd--;
+        while (nd > 2 && rep[nd-1] == 1);
+        rep[-1] = nd;
       }
   }
 
--- a/liboctave/array/idx-vector.cc	Tue Apr 18 12:04:30 2017 +0200
+++ b/liboctave/array/idx-vector.cc	Tue Apr 18 11:59:24 2017 -0700
@@ -596,8 +596,8 @@
 {
   os << '[';
 
-  for (octave_idx_type ii = 0; ii < len - 1; ii++)
-    os << data[ii] << ',' << ' ';
+  for (octave_idx_type i = 0; i < len - 1; i++)
+    os << data[i] << ',' << ' ';
 
   if (len > 0)
     os << data[len-1];
@@ -713,8 +713,8 @@
 {
   os << '[';
 
-  for (octave_idx_type ii = 0; ii < ext - 1; ii++)
-    os << data[ii] << ',' << ' ';
+  for (octave_idx_type i = 0; i < ext - 1; i++)
+    os << data[i] << ',' << ' ';
 
   if (ext > 0)
     os << data[ext-1];
@@ -1121,8 +1121,7 @@
       bool *ndata = mask.fortran_vec ();
       for (octave_idx_type i = 0; i < ext; i++)
         ndata[i] = ! data[i];
-      for (octave_idx_type i = ext; i < n; i++)
-        ndata[i] = true;
+      std::fill_n (ndata + ext, n - ext, true);
       retval = new idx_mask_rep (mask, n - nz);
     }
   else