diff liboctave/Array.cc @ 4870:2cbc6f37b0c2

[project @ 2004-04-21 17:30:51 by jwe]
author jwe
date Wed, 21 Apr 2004 17:30:52 +0000
parents 8f669cc5a901
children 05d464a13f43
line wrap: on
line diff
--- a/liboctave/Array.cc	Wed Apr 21 17:03:02 2004 +0000
+++ b/liboctave/Array.cc	Wed Apr 21 17:30:52 2004 +0000
@@ -560,8 +560,6 @@
   if (same_size)
     return;
 
-  int old_len = length ();
-
   typename Array<T>::ArrayRep *old_rep = rep;
   const T *old_data = data ();
 
@@ -569,16 +567,26 @@
 
   rep = new typename Array<T>::ArrayRep (ts);
 
+  dim_vector dv_old = dimensions;
+  int dv_old_orig_len = dv_old.length ();
   dimensions = dv;
 
-  if (ts > 0)
+  if (ts > 0 && dv_old_orig_len > 0)
     {
       Array<int> ra_idx (dimensions.length (), 0);
 
-      for (int i = 0; i < old_len; i++)
+      if (n > dv_old_orig_len)
 	{
-	  if (index_in_bounds (ra_idx, dimensions))
-	    xelem (ra_idx) = old_data[i];
+	  dv_old.resize (n);
+
+	  for (int i = dv_old_orig_len; i < n; i++)
+	    dv_old.elem (i) = 1;
+	}
+
+      for (int i = 0; i < ts; i++)
+	{
+	  if (index_in_bounds (ra_idx, dv_old))
+	    rep->elem (i) = old_data[get_scalar_idx (ra_idx, dv_old)];
 
 	  increment_index (ra_idx, dimensions);
 	}
@@ -894,47 +902,39 @@
   typename Array<T>::ArrayRep *old_rep = rep;
   const T *old_data = data ();
 
-  int old_len = length ();
-
   int len = get_size (dv);
 
   rep = new typename Array<T>::ArrayRep (len);
 
   dim_vector dv_old = dimensions;
-
   int dv_old_orig_len = dv_old.length ();
-
-  if (n > dv_old_orig_len)
-    {
-      dv_old.resize (n);
-
-      for (int i = dv_old_orig_len; i < n; i++)
-	dv_old.elem (i) = 1;
-    }
-
   dimensions = dv;
 
-  if (len > 0)
+  if (len > 0 && dv_old_orig_len > 0)
     {
       Array<int> ra_idx (dimensions.length (), 0);
-
-      // XXX FIXME XXX -- it is much simpler to fill the whole array
-      // first, but probably slower for large arrays, or if the assignment
-      // operator for the type T is expensive.  OTOH, the logic for
-      // deciding whether an element needs the copied value or the filled
-      // value might be more expensive.
+      
+      if (n > dv_old_orig_len)
+	{
+	  dv_old.resize (n);
+
+	  for (int i = dv_old_orig_len; i < n; i++)
+	    dv_old.elem (i) = 1;
+	}
 
       for (int i = 0; i < len; i++)
-	rep->elem (i) = val;
-
-      for (int i = 0; i < old_len; i++)
 	{
 	  if (index_in_bounds (ra_idx, dv_old))
-	    xelem (ra_idx) = old_data[get_scalar_idx (ra_idx, dv_old)];
-
-	  increment_index (ra_idx, dv_old);
+	    rep->elem (i) = old_data[get_scalar_idx (ra_idx, dv_old)];
+	  else
+	    rep->elem (i) = val;
+	  
+	  increment_index (ra_idx, dimensions);
 	}
     }
+  else
+    for (int i = 0; i < len; i++)
+      rep->elem (i) = val;
 
   if (--old_rep->count <= 0)
     delete old_rep;