Mercurial > hg > octave-lyh
changeset 6388:4109a2386346
[project @ 2007-03-07 08:47:36 by jwe]
author | jwe |
---|---|
date | Wed, 07 Mar 2007 08:47:36 +0000 |
parents | 24666a9192f2 |
children | f427b33aeb4c |
files | liboctave/Array.cc liboctave/ChangeLog |
diffstat | 2 files changed, 134 insertions(+), 75 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/Array.cc +++ b/liboctave/Array.cc @@ -1679,6 +1679,8 @@ dim_vector lhs_dims = dims (); + int n_lhs_dims = lhs_dims.length (); + if (lhs_dims.all_zero ()) return; @@ -1688,7 +1690,24 @@ return; } - int n_lhs_dims = lhs_dims.length (); + if (n_idx > n_lhs_dims) + { + for (int i = n_idx; i < n_lhs_dims; i++) + { + // Ensure that extra indices are either colon or 1. + + if (! ra_idx(i).is_colon_equiv (1, 1)) + { + (*current_liboctave_error_handler) + ("index exceeds array dimensions"); + return; + } + } + + ra_idx.resize (n_lhs_dims); + + n_idx = n_lhs_dims; + } Array<int> idx_is_colon (n_idx, 0); @@ -2466,47 +2485,18 @@ int assign (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv) { - int retval = 0; - - switch (lhs.ndims ()) - { - case 0: - { - if (lhs.index_count () < 3) - { - // kluge... - lhs.resize_no_fill (0, 0); - retval = assign2 (lhs, rhs, rfv); - } - else - retval = assignN (lhs, rhs, rfv); - } - break; - - case 1: - { - if (lhs.index_count () > 1) - retval = assignN (lhs, rhs, rfv); - else - retval = assign1 (lhs, rhs, rfv); - } - break; - - case 2: - { - if (lhs.index_count () > 2) - retval = assignN (lhs, rhs, rfv); - else - retval = assign2 (lhs, rhs, rfv); - } - break; - - default: - retval = assignN (lhs, rhs, rfv); - break; - } - - return retval; + int n_idx = lhs.index_count (); + + // kluge... + if (lhs.ndims () == 0) + lhs.resize_no_fill (0, 0); + + return (lhs.ndims () == 2 + && (n_idx == 1 + || (n_idx < 3 + && rhs.ndims () == 2 + && rhs.rows () == 0 && rhs.columns () == 0))) + ? assign2 (lhs, rhs, rfv) : assignN (lhs, rhs, rfv); } template <class LT, class RT> @@ -3067,16 +3057,17 @@ octave_idx_type nelem = idx(i).capacity (); if (nelem >= 1 - && k < rhs_dims.length () && nelem == rhs_dims(k)) + && ((k < rhs_dims.length () && nelem == rhs_dims(k)) + || rhs_is_scalar)) k++; - else if (nelem != 1) + else if (! (nelem == 1 || rhs_is_scalar)) { (*current_liboctave_error_handler) ("A(IDX-LIST) = RHS: mismatched index and RHS dimension"); return retval; } - new_dims(i) = idx(i).max () + 1; + new_dims(i) = idx(i).orig_empty () ? 0 : idx(i).max () + 1; } } } @@ -3154,19 +3145,53 @@ if (! final_lhs_dims.any_zero ()) { - octave_idx_type n = Array<LT>::get_size (frozen_len); - - Array<octave_idx_type> result_idx (lhs_dims_len, 0); - RT scalar = rhs.elem (0); - for (octave_idx_type i = 0; i < n; i++) + if (n_idx == 1) + { + idx_vector iidx = idx(0); + + octave_idx_type len = frozen_len(0); + + for (octave_idx_type i = 0; i < len; i++) + { + octave_idx_type ii = iidx.elem (i); + + lhs.elem (ii) = scalar; + } + } + else if (lhs_dims_len == 2 && n_idx == 2) { - Array<octave_idx_type> elt_idx = get_elt_idx (idx, result_idx); - - lhs.elem (elt_idx) = scalar; - - increment_index (result_idx, frozen_len); + idx_vector idx_i = idx(0); + idx_vector idx_j = idx(1); + + octave_idx_type i_len = frozen_len(0); + octave_idx_type j_len = frozen_len(1); + + for (octave_idx_type j = 0; j < j_len; j++) + { + octave_idx_type jj = idx_j.elem (j); + for (octave_idx_type i = 0; i < i_len; i++) + { + octave_idx_type ii = idx_i.elem (i); + lhs.elem (ii, jj) = scalar; + } + } + } + else + { + octave_idx_type n = Array<LT>::get_size (frozen_len); + + Array<octave_idx_type> result_idx (lhs_dims_len, 0); + + for (octave_idx_type i = 0; i < n; i++) + { + Array<octave_idx_type> elt_idx = get_elt_idx (idx, result_idx); + + lhs.elem (elt_idx) = scalar; + + increment_index (result_idx, frozen_len); + } } } } @@ -3192,17 +3217,52 @@ if (! final_lhs_dims.any_zero ()) { - n = Array<LT>::get_size (frozen_len); - - Array<octave_idx_type> result_idx (lhs_dims_len, 0); - - for (octave_idx_type i = 0; i < n; i++) + if (n_idx == 1) + { + idx_vector iidx = idx(0); + + octave_idx_type len = frozen_len(0); + + for (octave_idx_type i = 0; i < len; i++) + { + octave_idx_type ii = iidx.elem (i); + + lhs.elem (ii) = rhs.elem (i); + } + } + else if (lhs_dims_len == 2 && n_idx == 2) { - Array<octave_idx_type> elt_idx = get_elt_idx (idx, result_idx); - - lhs.elem (elt_idx) = rhs.elem (i); - - increment_index (result_idx, frozen_len); + idx_vector idx_i = idx(0); + idx_vector idx_j = idx(1); + + octave_idx_type i_len = frozen_len(0); + octave_idx_type j_len = frozen_len(1); + octave_idx_type k = 0; + + for (octave_idx_type j = 0; j < j_len; j++) + { + octave_idx_type jj = idx_j.elem (j); + for (octave_idx_type i = 0; i < i_len; i++) + { + octave_idx_type ii = idx_i.elem (i); + lhs.elem (ii, jj) = rhs.elem (k++); + } + } + } + else + { + n = Array<LT>::get_size (frozen_len); + + Array<octave_idx_type> result_idx (lhs_dims_len, 0); + + for (octave_idx_type i = 0; i < n; i++) + { + Array<octave_idx_type> elt_idx = get_elt_idx (idx, result_idx); + + lhs.elem (elt_idx) = rhs.elem (i); + + increment_index (result_idx, frozen_len); + } } } }
--- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,13 +1,12 @@ -2007-03-06 John W. Eaton <jwe@octave.org> - - * Array.cc (assign1, assign2): For assignments like x(:,1) = 1 or - x(:) = 1, only resize if X is 0x0. - (assign2): Use single loop for x(:) = nonempty_rhs case. - If single index and rhs is 0x0, always call maybe_delete_elements - on lhs. - (Array<T>::maybe_delete_elements (Array<idx_vector>&, const T&)): - Quick return of [] if doing x(:) = []. - +2007-03-07 John W. Eaton <jwe@octave.org> + + * Array.cc (Array<T>::maybe_delete_elements (Array<idx_vector>&, + const T&)): Handle case of more indices than dimensions. + (assign (Array<LT>&, const Array<RT>&, const LT&)): Allow more + cases to be handled by assignN. + (assignN (Array<LT>&, const Array<RT>&, const LT&)): + Special cases for 2-d array assignments for speed. + Improve handling of scalar RHS and empty indices. 2007-03-05 David Bateman <dbateman@free.fr>