changeset 9739:13b57eec9440

a few handy methods for dim_vector
author Jaroslav Hajek <highegg@gmail.com>
date Mon, 19 Oct 2009 10:52:00 +0200
parents a141154ee825
children 78ac37d73557
files liboctave/ChangeLog liboctave/dim-vector.h
diffstat 2 files changed, 58 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/ChangeLog	Sun Oct 18 12:01:24 2009 -0400
+++ b/liboctave/ChangeLog	Mon Oct 19 10:52:00 2009 +0200
@@ -1,3 +1,9 @@
+2009-10-19  Jaroslav Hajek  <highegg@gmail.com>
+
+	* dim-vector.h (dim_vector::compute_index,
+	dim_vector::increment_index, dim_vector::cumulative,
+	dim_vector::cum_compute_index): New methods.
+
 2009-10-18  Carsten Clark  <tantumquantum+gnuoctave@gmail.com>
 
 	* Makefile.in: Remove ArrayN.cc.
--- a/liboctave/dim-vector.h	Sun Oct 18 12:01:24 2009 -0400
+++ b/liboctave/dim-vector.h	Mon Oct 19 10:52:00 2009 +0200
@@ -516,6 +516,58 @@
       return def;      
     }
 
+  // Computes a linear index from an index tuple.
+  octave_idx_type compute_index (const octave_idx_type *idx)
+    {
+      octave_idx_type k = 0;
+      for (int i = length () - 1; i >= 0; i++)
+        k = k * rep[i] + idx[i];
+
+      return k;
+    }
+
+  // Increments a multi-dimensional index tuple, optionally starting
+  // from an offset position. Returns the index of the last index
+  // position that was changed, or length () if just cycled over.
+  int increment_index (octave_idx_type *idx, int start = 0)
+    {
+      int i;
+      for (i = start; i < length (); i++)
+        {
+          if (++(*idx) == rep[i])
+            *idx = 0;
+          else
+            break;
+        }
+      return i;
+    }
+
+  // Returns cumulative dimensions.
+  dim_vector cumulative (void) const
+    {
+      int nd = length ();
+      dim_vector retval = alloc (nd);
+
+      octave_idx_type k = 1;
+      for (int i = 0; i < nd; i++)
+        retval.rep[i] = k *= rep[i];
+
+      return retval;
+    }
+
+  // Computes a linear index from an index tuple. Assumes that the dimensions
+  // are cumulative.
+  octave_idx_type cum_compute_index (const octave_idx_type *idx)
+    {
+      octave_idx_type k = idx[0];
+
+      for (int i = 1; i < length (); i++)
+        k += rep[i-1] * idx[i];
+
+      return k;
+    }
+
+
   friend bool operator == (const dim_vector& a, const dim_vector& b);
 };