Mercurial > hg > octave-terminal
changeset 10491:077fef5da460
optimize null assignment with bool masks
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Tue, 06 Apr 2010 15:38:56 +0200 |
parents | fdccd69d26bd |
children | a6b64a7a3769 |
files | liboctave/ChangeLog liboctave/idx-vector.cc |
diffstat | 2 files changed, 27 insertions(+), 20 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,7 @@ +2010-04-06 Jaroslav Hajek <highegg@gmail.com> + + * idx-vector.cc (idx_vector::complement): Rewrite. + 2010-04-06 Jaroslav Hajek <highegg@gmail.com> * Sparse.cc (Sparse<T>::maybe_delete_elements): Rename to
--- a/liboctave/idx-vector.cc +++ b/liboctave/idx-vector.cc @@ -1096,29 +1096,32 @@ idx_vector idx_vector::complement (octave_idx_type n) const { - OCTAVE_LOCAL_BUFFER_INIT (bool, left, n, true); - - octave_idx_type cnt = n; + idx_vector retval; + if (extent (n) > n) + (*current_liboctave_error_handler) + ("internal error: out of range complement index requested"); - for (octave_idx_type i = 0, len = length (); i < len; i++) - { - octave_idx_type k = xelem (i); - if (k < n && left[k]) - { - left[k] = false; - cnt--; - } + if (idx_class () == class_mask) + { + idx_mask_rep * r = dynamic_cast<idx_mask_rep *> (rep); + octave_idx_type nz = r->length (0), ext = r->extent (0); + Array<bool> mask (n, 1); + const bool *data = r->get_data (); + 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; + retval = new idx_mask_rep (mask, n - nz); + } + else + { + Array<bool> mask (n, 1, true); + fill (false, length (n), mask.fortran_vec ()); + retval = idx_vector (mask); } - octave_idx_type len = cnt, *data = new octave_idx_type[len]; - - for (octave_idx_type i = 0, j = 0; i < n; i++) - if (left[i]) - data[j++] = i; - - return new idx_vector_rep (data, len, - len ? data[len-1]+1 : 0, - dim_vector (1, len), DIRECT); + return retval; } bool