octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

x(idx)=y(idx) for empty idx


From: John W. Eaton
Subject: x(idx)=y(idx) for empty idx
Date: Fri, 5 Mar 2004 10:19:01 -0600

On  3-Mar-2004, Paul Kienzle <address@hidden> wrote:

| For dimension != 2, the simple code seg faults (but only
| for empty idx):
| 
|    octave:1> y = rand(10,10,10);
|    octave:2> idx = y>1;
|    octave:3> y(idx) = y(idx)-1;
|    panic: Segmentation fault -- stopping myself...

Please try the following patch.

Thanks,

jwe


liboctave/ChangeLog:

2004-03-05  John W. Eaton  <address@hidden>

        * Array.cc (Array<T>::maybe_delete_elements): Return immediately
        if all LHS dimensions are zero.  For one index case, freeze and
        sort idx_vec before checking length, and do nothing if
        num_to_delete is zero.



Index: liboctave/Array.cc
===================================================================
RCS file: /usr/local/cvsroot/octave/liboctave/Array.cc,v
retrieving revision 1.107
diff -u -r1.107 Array.cc
--- liboctave/Array.cc  2 Mar 2004 05:12:32 -0000       1.107
+++ liboctave/Array.cc  5 Mar 2004 16:16:52 -0000
@@ -1540,6 +1540,9 @@
 
   dim_vector lhs_dims = dims ();
 
+  if (lhs_dims.all_zero ())
+    return;
+
   int n_lhs_dims = lhs_dims.length ();
 
   Array<int> idx_is_colon (n_idx, 0);
@@ -1791,60 +1794,68 @@
        }
       else if (n_idx == 1)
        {
-         // This handle cases where we only have one index (not colon).
-         // The index denotes which elements we should delete in the array
-         // which can be of any dimension. We return a column vector, except
-         // for the case where we are operating on a row column. The elements
-         // are numerated columns by column.
+         // This handle cases where we only have one index (not
+         // colon).  The index denotes which elements we should
+         // delete in the array which can be of any dimension. We
+         // return a column vector, except for the case where we are
+         // operating on a row vector. The elements are numerated
+         // column by column.
          //
          // A(3,3,3)=2;
          // A(3:5) = []; A(6)=[]
-         //
+
+         int lhs_numel = numel ();
+
          idx_vector idx_vec = ra_idx(0);
 
-         int num_to_delete = idx_vec.capacity ();
+         idx_vec.freeze (lhs_numel, 0, true, liboctave_wrore_flag);
+      
+         idx_vec.sort (true);
 
-         int lhs_numel = numel ();
+         int num_to_delete = idx_vec.length (lhs_numel);
 
-         int new_numel = lhs_numel - num_to_delete;
+         if (num_to_delete > 0)
+           {
+             int new_numel = lhs_numel - num_to_delete;
 
-         T *new_data = new T[new_numel];
+             T *new_data = new T[new_numel];
 
-         Array<int> lhs_ra_idx (ndims (), 0);
+             Array<int> lhs_ra_idx (ndims (), 0);
 
-         int ii = 0;
-         int iidx = 0;
+             int ii = 0;
+             int iidx = 0;
 
-         for (int i = 0; i < lhs_numel; i++)
-           {
-             if (iidx < num_to_delete && i == idx_vec.elem (iidx))
-               {
-                 iidx++;
-               }
-             else
+             for (int i = 0; i < lhs_numel; i++)
                {
-                 new_data[ii++] = elem (lhs_ra_idx);
-               }
+                 if (iidx < num_to_delete && i == idx_vec.elem (iidx))
+                   {
+                     iidx++;
+                   }
+                 else
+                   {
+                     new_data[ii++] = elem (lhs_ra_idx);
+                   }
 
-             increment_index (lhs_ra_idx, lhs_dims);
-           }
+                 increment_index (lhs_ra_idx, lhs_dims);
+               }
 
-         if (--(Array<T>::rep)->count <= 0)
-           delete Array<T>::rep;
+             if (--(Array<T>::rep)->count <= 0)
+               delete Array<T>::rep;
 
-         Array<T>::rep = new typename Array<T>::ArrayRep (new_data, new_numel);
+             Array<T>::rep = new typename Array<T>::ArrayRep (new_data, 
new_numel);
 
-         dimensions.resize (2);
+             dimensions.resize (2);
 
-         if (lhs_dims.length () == 2 && lhs_dims(1) == 1)
-           {
-             dimensions(0) = new_numel;
-             dimensions(1) = 1;
-           }
-         else
-           {
-             dimensions(0) = 1;
-             dimensions(1) = new_numel;
+             if (lhs_dims.length () == 2 && lhs_dims(1) == 1)
+               {
+                 dimensions(0) = new_numel;
+                 dimensions(1) = 1;
+               }
+             else
+               {
+                 dimensions(0) = 1;
+                 dimensions(1) = new_numel;
+               }
            }
        }
       else if (num_ones (idx_is_colon) < n_idx)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]