changeset 4548:820323598f4f

[project @ 2003-10-26 03:16:42 by jwe]
author jwe
date Sun, 26 Oct 2003 03:16:42 +0000
parents 308feb0e5646
children 2d393bead213
files liboctave/Array.cc liboctave/Array.h liboctave/ChangeLog liboctave/DASPK-opts.in liboctave/DASRT.cc liboctave/MArray.h liboctave/MArray2.h liboctave/ODESSA.cc liboctave/dim-vector.h src/Cell.h src/ChangeLog src/oct-map.cc src/oct-obj.h src/ov-typeinfo.cc src/pt-idx.cc
diffstat 15 files changed, 287 insertions(+), 127 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/Array.cc	Sat Oct 25 01:59:15 2003 +0000
+++ b/liboctave/Array.cc	Sun Oct 26 03:16:42 2003 +0000
@@ -387,18 +387,25 @@
 	}
     }
 
-  bool no_change = true;
-
-  for (int i = 0; i < n; i++)
+  bool same_size = true;
+
+  if (dimensions.length () != n)
+    {
+      same_size = false;
+    }
+  else
     {
-      if (dims(i) != dimensions(i))
+      for (int i = 0; i < n; i++)
 	{
-	  no_change = false;
-	  break;
+	  if (dims(i) != dimensions(i))
+	    {
+	      same_size = false;
+	      break;
+	    }
 	}
     }
 
-  if (no_change)
+  if (same_size)
     return;
 
   int old_len = length ();
@@ -435,6 +442,13 @@
       return;
     }
 
+  int n = ndims ();
+
+  if (n == 0)
+    dimensions = dim_vector (0, 0);
+
+  assert (ndims () == 2);
+
   if (r == dim1 () && c == dim2 ())
     return;
 
@@ -474,6 +488,13 @@
       return;
     }
 
+  int n = ndims ();
+
+  if (n == 0)
+    dimensions = dim_vector (0, 0, 0);
+
+  assert (ndims () == 3);
+
   if (r == dim1 () && c == dim2 () && p == dim3 ())
     return;
 
@@ -555,6 +576,11 @@
       return;
     }
 
+  if (ndims () == 0)
+    dimensions = dim_vector (0, 0);
+
+  assert (ndims () == 2);
+
   if (r == dim1 () && c == dim2 ())
     return;
 
@@ -602,6 +628,11 @@
       return;
     }
 
+  if (ndims () == 0)
+    dimensions = dim_vector (0, 0, 0);
+
+  assert (ndims () == 3);
+
   if (r == dim1 () && c == dim2 () && p == dim3 ())
     return;
 
@@ -777,6 +808,8 @@
 Array<T>
 Array<T>::transpose (void) const
 {
+  assert (ndims () == 2);
+
   int nr = dim1 ();
   int nc = dim2 ();
 
@@ -963,6 +996,8 @@
 void
 Array<T>::maybe_delete_elements_2 (idx_vector& idx_arg)
 {
+  assert (ndims () == 2);
+
   int nr = dim1 ();
   int nc = dim2 ();
 
@@ -1062,6 +1097,8 @@
 void
 Array<T>::maybe_delete_elements (idx_vector& idx_i, idx_vector& idx_j)
 {
+  assert (ndims () == 2);
+
   int nr = dim1 ();
   int nc = dim2 ();
 
@@ -1517,7 +1554,7 @@
 	       && idx_arg.one_zero_only ()
 	       && idx_arg.ones_count () == n)
 	{
-	  retval.resize (n, elem (0));
+	  retval.resize_and_fill (n, elem (0));
 	}
       else
 	{
@@ -1545,6 +1582,8 @@
 {
   Array<T> retval;
 
+  assert (ndims () == 2);
+
   int nr = dim1 ();
   int nc = dim2 ();
 
@@ -1666,18 +1705,16 @@
 
   dim_vector idx_orig_dims;
 
-  idx_orig_dims.resize(idx_orig_dimsXXX.length());
-
-  for (int i = 0; i < idx_orig_dimsXXX.length(); i++)
+  idx_orig_dims.resize (idx_orig_dimsXXX.length ());
+
+  for (int i = 0; i < idx_orig_dimsXXX.length (); i++)
     idx_orig_dims(i) = idx_orig_dimsXXX(i);
 
-
   if (ra_idx.is_colon ())
     {
-      dim_vector idx(orig_len);
+      dim_vector idx (orig_len);
 
       retval = Array<T> (*this, idx);
-
     }
   else if (length () == 1)
     {
@@ -1688,10 +1725,7 @@
       if (tmp.length () != 0)
 	retval = Array<T> (tmp, idx_orig_dims);
       else
-	{
-	  dim_vector d;
-	  retval = Array<T> (tmp, d);
-	}
+	retval = Array<T> (tmp, dim_vector (0));
     }
   else if (vector_equivalent (dims ()))
     { 
@@ -1808,7 +1842,6 @@
           Array<int> idx = get_ra_idx (r_idx, dims ());
 
           dim_vector new_dims (1);
-	  new_dims(0)=1;
 
 	  // This shouldn't be needed.
 
@@ -1837,6 +1870,8 @@
 {
   Array<T> retval;
 
+  assert (ndims () == 2);
+
   int nr = dim1 ();
   int nc = dim2 ();
 
@@ -1910,10 +1945,7 @@
 	    }
 	  else if (any_zero_len (frozen_lengths))
 	    {
-	      dim_vector new_size = 
-		get_zero_len_size (frozen_lengths, dimensions);
-
-	      retval.resize (new_size);
+	      retval.resize (get_zero_len_size (frozen_lengths, dimensions));
 	    }
 	  else if (all_colon_equiv (ra_idx, dimensions) 
 		    && frozen_lengths.length () == n_dims)
@@ -2029,7 +2061,7 @@
 	{
 	  int max_idx = lhs_idx.max () + 1;
 	  if (max_idx > lhs_len)
-	    lhs.resize (max_idx, rfv);
+	    lhs.resize_and_fill (max_idx, rfv);
 	}
 
       if (rhs_len == n)
@@ -2383,13 +2415,13 @@
       if (n_idx >= lhs_dims.length () && ! rhs_is_empty) \
 	{ \
 	  Array<int> max_idx (n_idx); \
-	  dim_vector new_idx; \
-          new_idx.resize (n_idx); \
+	  dim_vector new_dims; \
+          new_dims.resize (n_idx); \
  \
 	  for (int i = 0; i < n_idx; i++) \
 	    { \
 	      if (lhs_dims.length () == 0 || i >= lhs_dims.length ()) \
-		new_idx(i) = idx(i).max () + 1; \
+		new_dims(i) = idx(i).max () + 1; \
 	      else \
 		{ \
 		  if (i < rhs_dims.length ()) \
@@ -2397,11 +2429,11 @@
 		  else \
 		    max_idx(i) = idx(i).max () + 1; \
  \
-		  new_idx(i) = max_idx(i) > lhs_dims(i) ? max_idx(i) : lhs_dims(i); \
+		  new_dims(i) = max_idx(i) > lhs_dims(i) ? max_idx(i) : lhs_dims(i); \
 		} \
             } \
  \
-	  lhs.resize (new_idx, rfv); \
+	  lhs.resize_and_fill (new_dims, rfv); \
 	  lhs_dims = lhs.dims ();  \
         } \
     } \
@@ -2661,7 +2693,7 @@
 	      Array<int> result_rhs_idx (rhs_dims.length (), 0);
 
 	      dim_vector frozen_rhs;
-	      frozen_rhs.resize (rhs_dims.length());
+	      frozen_rhs.resize (rhs_dims.length ());
 
 	      for (int i = 0; i < rhs_dims.length (); i++)
 		frozen_rhs(i) = rhs_dims(i);
--- a/liboctave/Array.h	Sat Oct 25 01:59:15 2003 +0000
+++ b/liboctave/Array.h	Sun Oct 26 03:16:42 2003 +0000
@@ -404,6 +404,9 @@
   T operator () (const Array<int>& ra_idx) const { return elem (ra_idx); }
 #endif
 
+  void resize_no_fill (int n);
+  void resize_and_fill (int n, const T& val);
+
   // !!! WARNING !!! -- the following resize_no_fill and
   // resize_and_fill functions are public because template friends
   // don't work properly with versions of gcc earlier than 3.3.  You
@@ -412,20 +415,13 @@
 
   // protected:
 
-  void resize_no_fill (int n);
-
   void resize_no_fill (int r, int c);
+  void resize_and_fill (int r, int c, const T& val);
 
   void resize_no_fill (int r, int c, int p);
+  void resize_and_fill (int r, int c, int p, const T& val);
 
   void resize_no_fill (const dim_vector& dims);
-
-  void resize_and_fill (int n, const T& val);
-
-  void resize_and_fill (int r, int c, const T& val);
-
-  void resize_and_fill (int r, int c, int p, const T& val);
-
   void resize_and_fill (const dim_vector& dims, const T& val);
 
 public:
--- a/liboctave/ChangeLog	Sat Oct 25 01:59:15 2003 +0000
+++ b/liboctave/ChangeLog	Sun Oct 26 03:16:42 2003 +0000
@@ -1,3 +1,35 @@
+2003-10-25  John W. Eaton  <jwe@bevo.che.wisc.edu>
+
+	* Array.cc (Array<T>::resize_no_fill (const dim_vector&)):
+	Allow number of dimensions to change.
+	(Array<T>::resize_no_fill (int, int)): Require ndims to be 0 or 2.
+	(Array<T>::resize_and_fill (int, int, const T&)): Likewise.
+	(Array<T>::resize_no_fill (int, int, int)): Require ndims to be 0 or 3.
+	(Array<T>::resize_and_fill (int, int, int, const T&)): Likewise.
+	(Array<T>::transpose): Require ndims to be 2.
+	(Array<T>::index2): Likewise.
+	(Array<T>::index (idx_vector&, idx_vector&, int, const T&)): Likewise.
+	(Array<T>::maybe_delete_elements_2): Likewise.
+	(Array<T>::maybe_delete_elements (idx_vector&, idx_vector&)): Likewise.
+	(Array<T>::index1): Use resize_and_fill.
+	(MAYBE_RESIZE_ND_DIMS): Likewise.
+
+	* ODESSA.cc (ODESSA::integrate): Use resize_and_fill for x_s_out.
+
+	* MArray2.h (MArray2<T>::resize (int, int)): New function.
+	(MArray2<T>::resize (int, int, const T&)): New function.
+
+	* MArray.h (MArray<T>::resize (int)): New function.
+	(MArray<T>::resize (int, const T&)): New function.
+
+	* DASRT.cc (DASRT::integrate): Use resize_and_fill for jroot.
+
+	* DASPK-opts.in: Use single-arg resize for initial condition
+	heuristics.
+
+	* dim-vector.h (class dim_vector): Now reference counted.
+	(dim_vector_rep::elem): Use assert to check that index is in bounds.
+
 2003-10-23  John W. Eaton  <jwe@bevo.che.wisc.edu>
 
 	* Array.cc (Array<T>::squeeze): Delete redundant retval decl.
--- a/liboctave/DASPK-opts.in	Sat Oct 25 01:59:15 2003 +0000
+++ b/liboctave/DASPK-opts.in	Sun Oct 26 03:16:42 2003 +0000
@@ -135,7 +135,7 @@
   TYPE = "Array<double>"
   SET_ARG_TYPE = "const $TYPE&"
   INIT_BODY
-    $OPTVAR.resize (6, 0.0);
+    $OPTVAR.resize (6);
     $OPTVAR(0) = 5.0;
     $OPTVAR(1) = 6.0;
     $OPTVAR(2) = 5.0;
--- a/liboctave/DASRT.cc	Sat Oct 25 01:59:15 2003 +0000
+++ b/liboctave/DASRT.cc	Sun Oct 26 03:16:42 2003 +0000
@@ -253,7 +253,7 @@
 
       DAEFunc::reset = false;
 
-      jroot.resize (ng, 1);
+      jroot.resize_and_fill (ng, 1);
 
       pjroot = jroot.fortran_vec ();
 
--- a/liboctave/MArray.h	Sat Oct 25 01:59:15 2003 +0000
+++ b/liboctave/MArray.h	Sun Oct 26 03:16:42 2003 +0000
@@ -66,6 +66,10 @@
       return *this;
     }
 
+  void resize (int n) { Array<T>::resize_no_fill (n); }
+
+  void resize (int n, const T& val) { Array<T>::resize_and_fill (n, val); }
+
   // Currently, the OPS functions don't need to be friends, but that
   // may change.
 
--- a/liboctave/MArray2.h	Sat Oct 25 01:59:15 2003 +0000
+++ b/liboctave/MArray2.h	Sun Oct 26 03:16:42 2003 +0000
@@ -66,6 +66,13 @@
       return *this;
     }
 
+  void resize (int r, int c) { Array2<T>::resize_no_fill (r, c); }
+
+  void resize (int r, int c, const T& val)
+  {
+    Array<T>::resize_and_fill (r, c, val);
+  }
+
   MArray2<T>& insert (const Array2<T>& a, int r, int c)
   {
     Array2<T>::insert (a, r, c);
--- a/liboctave/ODESSA.cc	Sat Oct 25 01:59:15 2003 +0000
+++ b/liboctave/ODESSA.cc	Sun Oct 26 03:16:42 2003 +0000
@@ -578,7 +578,7 @@
     {
       x_out.resize (n_out, n);
 
-      x_s_out.resize (npar, Matrix (n_out, n, 0.0));
+      x_s_out.resize_and_fill (npar, Matrix (n_out, n, 0.0));
 
       for (int j = 0; j < n_out; j++)
 	{
@@ -622,7 +622,7 @@
     {
       x_out.resize (n_out, n);
 
-      x_s_out.resize (npar, Matrix (n_out, n, 0.0));
+      x_s_out.resize_and_fill (npar, Matrix (n_out, n, 0.0));
 
       int n_crit = tcrit.capacity ();
 
--- a/liboctave/dim-vector.h	Sat Oct 25 01:59:15 2003 +0000
+++ b/liboctave/dim-vector.h	Sun Oct 26 03:16:42 2003 +0000
@@ -35,131 +35,206 @@
 class
 dim_vector
 {
-public:
+protected:
 
-  dim_vector (void) : ndims (0), dims (0) { }
+  class dim_vector_rep
+  {
+  public:
 
-  dim_vector (int n) : ndims (1), dims (new int [1]) { dims[0] = n; }
+    int *dims;
+    int ndims;
+    int count;
 
-  dim_vector (int r, int c)
-    : ndims (2), dims (new int [2]) { dims[0] = r; dims[1] = c; }
+    dim_vector_rep (void) : dims (0), ndims (0), count (1) { }
+
+    dim_vector_rep (int n) : dims (new int [1]), ndims (1), count (1)
+    {
+      dims[0] = n;
+    }
 
-  dim_vector (int r, int c, int p)
-    : ndims (3), dims (new int [3]) { dims[0] = r; dims[1] = c; dims[2] = p; }
+    dim_vector_rep (int r, int c) : dims (new int [2]), ndims (2), count (1)
+    {
+      dims[0] = r;
+      dims[1] = c;
+    }
 
-  dim_vector (const dim_vector& dv)
-    : ndims (dv.ndims)
+    dim_vector_rep (int r, int c, int p)
+      : dims (new int [3]), ndims (3), count (1)
     {
-      if (dv.dims)
+      dims[0] = r;
+      dims[1] = c;
+      dims[2] = p;
+    }
+
+    dim_vector_rep (const dim_vector_rep& dv)
+      : dims (dv.ndims > 0 ? new int [dv.ndims] : 0),
+	ndims (dv.ndims > 0 ? dv.ndims : 0), count (1)
+    {
+      if (dims)
 	{
-	  dims = new int [ndims];
-
 	  for (int i = 0; i < ndims; i++)
 	    dims[i] = dv.dims[i];
 	}
-      else
-	dims = 0;
     }
 
-  dim_vector& operator = (const dim_vector& dv)
+    dim_vector_rep (int n, const dim_vector_rep *dv)
+      : dims ((dv && n > 0) ? new int [n] : 0),
+	ndims (n > 0 ? n : 0), count (1)
     {
-      if (&dv != this)
+      if (dims)
 	{
-	  ndims = dv.ndims;
+	  int dv_ndims = dv ? dv->ndims : 0;
 
-	  if (dv.dims)
-	    {
-	      dims = new int [ndims];
+	  for (int i = 0; i < dv_ndims; i++)
+	    dims[i] = dv->dims[i];
 
-	      for (int i = 0; i < ndims; i++)
-		dims[i] = dv.dims[i];
-	    }
+	  for (int i = dv_ndims; i < n; i++)
+	    dims[i] = 0;
 	}
-
-      return *this;
     }
 
-  ~dim_vector (void) { delete [] dims; }
+    ~dim_vector_rep (void) { delete [] dims; }
 
-  int length (void) const { return ndims; }
+    int length (void) const { return ndims; }
 
-  int& elem (int i)
+    int& elem (int i)
     {
-      if (i >= ndims)
-	resize (i+1);
+      assert (i >= 0 && i < ndims);
+      return dims[i];
+    }
 
+    int elem (int i) const
+    {
+      assert (i >= 0 && i < ndims);
       return dims[i];
     }
 
-  int elem (int i) const { return i < ndims ? dims[i] : -1; }
+  private:
+
+    // No assignment!
+
+    dim_vector_rep& operator = (const dim_vector_rep& dv);
+  };
+
+  dim_vector_rep *rep;
+
+  void make_unique (void)
+  {
+    if (rep->count > 1)
+      {
+	--rep->count;
+	rep = new dim_vector_rep (*rep);
+      }
+  }
+
+private:
+
+  dim_vector_rep *nil_rep (void) const
+  {
+    static dim_vector_rep *nr = new dim_vector_rep ();
+
+    return nr;
+  }
+
+public:
+
+  explicit dim_vector (void)
+    : rep (nil_rep ()) { rep->count++; }
+
+  explicit dim_vector (int n)
+    : rep (new dim_vector_rep (n)) { }
+
+  explicit dim_vector (int r, int c)
+    : rep (new dim_vector_rep (r, c)) { }
+
+  explicit dim_vector (int r, int c, int p)
+    : rep (new dim_vector_rep (r, c, p)) { }
+
+  dim_vector (const dim_vector& dv)
+    : rep (dv.rep) { rep->count++; }
+
+  dim_vector& operator = (const dim_vector& dv)
+  {
+    if (&dv != this)
+      {
+	if (--rep->count <= 0)
+	  delete rep;
+
+	rep = dv.rep;
+	rep->count++;
+      }
+
+    return *this;
+  }
+
+  ~dim_vector (void)
+  {
+    if (--rep->count <= 0)
+      delete rep;
+  }
+
+  int length (void) const { return rep->length (); }
+
+  int& elem (int i) { make_unique (); return rep->elem (i); }
+
+  int elem (int i) const { return rep->elem (i); }
 
   int& operator () (int i) { return elem (i); }
 
   int operator () (int i) const { return elem (i); }
 
   void resize (int n)
-    {
-      if (n > ndims)
-	{
-	  int *new_dims = new int [n];
+  {
+    int len = length ();
 
-	  for (int i = 0; i < ndims; i++)
-	    new_dims[i] = dims[i];
+    if (n != len)
+      {
+	dim_vector_rep *old_rep = rep;
 
-	  for (int i = ndims; i < n; i++)
-	    new_dims[i] = 0;
-
-	  delete [] dims;
+	rep = new dim_vector_rep (n, old_rep);
 
-	  dims = new_dims;
+	if (--old_rep->count <= 0)
+	  delete old_rep;
+      }
+  }
 
-	  ndims = n;
-	}
-      else
-	ndims = n;
-    }
 
   std::string str (void) const
-    {
-      OSSTREAM buf;
+  {
+    OSSTREAM buf;
 
-      for (int i = 0; i < ndims; i++)
-	{
-	  buf << dims[i];
+    for (int i = 0; i < length (); i++)
+      {
+	buf << elem (i);
 
-	  if (i < ndims - 1)
-	    buf << "x";
-	}
+	if (i < length () - 1)
+	  buf << "x";
+      }
 
-      buf << OSSTREAM_ENDS;
+    buf << OSSTREAM_ENDS;
 
-      std::string retval = OSSTREAM_STR (buf);
+    std::string retval = OSSTREAM_STR (buf);
 
-      OSSTREAM_FREEZE (buf);
+    OSSTREAM_FREEZE (buf);
 
-      return retval;
-    }
+    return retval;
+  }
 
   bool all_zero (void) const
-    {
-      bool retval = true;
+  {
+    bool retval = true;
 
-      for (int i = 0; i < ndims; i++)
-	{
-	  if (dims[i] != 0)
-	    {
-	      retval = false;
-	      break;
-	    }
-	}
+    for (int i = 0; i < length (); i++)
+      {
+	if (elem (i) != 0)
+	  {
+	    retval = false;
+	    break;
+	  }
+      }
 
-      return retval;
-    }
-
-private:
-
-  int ndims;
-  int *dims;
+    return retval;
+  }
 };
 
 static inline bool
--- a/src/Cell.h	Sat Oct 25 01:59:15 2003 +0000
+++ b/src/Cell.h	Sun Oct 26 03:16:42 2003 +0000
@@ -41,7 +41,7 @@
 public:
 
   Cell (void)
-    : ArrayN<octave_value> () { }
+    : ArrayN<octave_value> (dim_vector (0, 0)) { }
 
   Cell (const octave_value& val)
     : ArrayN<octave_value> (dim_vector (1, 1), val) { }
--- a/src/ChangeLog	Sat Oct 25 01:59:15 2003 +0000
+++ b/src/ChangeLog	Sun Oct 26 03:16:42 2003 +0000
@@ -1,3 +1,17 @@
+2003-10-25  John W. Eaton  <jwe@bevo.che.wisc.edu>
+
+	* pt-idx.cc (make_subs_cell): Pass dim_vector object to resize.
+
+	* ov-typeinfo.cc (octave_value_typeinfo::do_register_type):
+	Use resize_and_fill for types.
+
+	* oct-obj.h (octave_value_list::maybe_resize): Use resize_and_fill.
+	(octave_value_list::resize (int, const octave_value&)): Likewise.
+
+	* oct-map.cc (Octave_map::assign): Use resize_and_fill as needed.
+
+	* Cell.h (Cell (void)): Default size is now 0x0.
+
 2003-10-23  John W. Eaton  <jwe@bevo.che.wisc.edu>
 
 	* OPERATORS/op-cm-m.cc, OPERATORS/op-cm-s.cc,
--- a/src/oct-map.cc	Sat Oct 25 01:59:15 2003 +0000
+++ b/src/oct-map.cc	Sun Oct 26 03:16:42 2003 +0000
@@ -138,12 +138,12 @@
 
       if (rhs_len < len)
 	{
-	  tmp.resize (len, fill_value);
+	  tmp.resize_and_fill (len, fill_value);
 	}
       else if (rhs_len > len)
 	{
 	  for (iterator p = begin (); p != end (); p++)
-	    contents(p).resize (rhs_len, fill_value);
+	    contents(p).resize_and_fill (rhs_len, fill_value);
 
 	  array_len = rhs_len;
 	}
--- a/src/oct-obj.h	Sat Oct 25 01:59:15 2003 +0000
+++ b/src/oct-obj.h	Sun Oct 26 03:16:42 2003 +0000
@@ -99,7 +99,7 @@
 
   void resize (int n) { data.resize (n); }
 
-  void resize (int n, const octave_value& val) { data.resize (n, val); }
+  void resize (int n, const octave_value& val) { data.resize_and_fill (n, val); }
 
   octave_value_list& prepend (const octave_value& val);
 
@@ -158,7 +158,7 @@
   void maybe_resize (int n)
     {
       if (n >= length ())
-	data.resize (n + 1, Matrix ());
+	data.resize_and_fill (n + 1, Matrix ());
     }
 
   octave_value& elem (int n)
--- a/src/ov-typeinfo.cc	Sat Oct 25 01:59:15 2003 +0000
+++ b/src/ov-typeinfo.cc	Sun Oct 26 03:16:42 2003 +0000
@@ -161,7 +161,7 @@
     {
       len *= 2;
 
-      types.resize (len, std::string ());
+      types.resize_and_fill (len, std::string ());
 
       unary_ops.resize (static_cast<int> (octave_value::num_unary_ops),
 			len, static_cast<unary_op_fcn> (0));
--- a/src/pt-idx.cc	Sat Oct 25 01:59:15 2003 +0000
+++ b/src/pt-idx.cc	Sun Oct 26 03:16:42 2003 +0000
@@ -137,7 +137,7 @@
 	{
 	  arg_values.stash_name_tags (arg_nm);
 
-	  retval.resize (1, n);
+	  retval.resize (dim_vector (1, n));
 
 	  for (int i = 0; i < n; i++)
 	    retval(0,i) = arg_values(i);