Mercurial > octave
comparison liboctave/array/Array-util.cc @ 30902:972959edc3ff
Allow sub2ind() to accept indices outside the size of the input subscripts (bug #62184)
* NEWS.8.md: Announce change in Matlab Compatibility section.
* sub2ind.cc: Add '#include "utility"' for access to std::swap.
* sub2ind.cc (Fsub2ind): Check nargout to figure out ndims of output.
For special case of vector (1-dimension), put in code to guarantee a
row vector output. Add BIST tests for bug #62184. Remove input
validation BIST which no longer applies.
* Array-util.cc (ind2sub): Remove input validation requiring index to
be within range of subscript size. Adjust code to put all remaining
elements in the final output dimension.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 05 Apr 2022 15:12:34 -0700 |
parents | 796f54d4ddbf |
children | 597f3ee61a48 |
comparison
equal
deleted
inserted
replaced
30901:f6c74e01e294 | 30902:972959edc3ff |
---|---|
621 octave_idx_type len = idx.length (0); | 621 octave_idx_type len = idx.length (0); |
622 octave_idx_type n = dv.ndims (); | 622 octave_idx_type n = dv.ndims (); |
623 Array<octave::idx_vector> retval (dim_vector (n, 1)); | 623 Array<octave::idx_vector> retval (dim_vector (n, 1)); |
624 octave_idx_type numel = dv.numel (); | 624 octave_idx_type numel = dv.numel (); |
625 | 625 |
626 if (idx.extent (numel) > numel) | |
627 (*current_liboctave_error_handler) ("ind2sub: index out of range"); | |
628 | |
629 if (idx.is_scalar ()) | 626 if (idx.is_scalar ()) |
630 { | 627 { |
631 octave_idx_type k = idx(0); | 628 octave_idx_type k = idx(0); |
632 for (octave_idx_type j = 0; j < n; j++) | 629 for (octave_idx_type j = 0; j < n-1; j++) |
633 { | 630 { |
634 retval(j) = k % dv(j); | 631 retval(j) = k % dv(j); |
635 k /= dv(j); | 632 k /= dv(j); |
636 } | 633 } |
634 | |
635 retval(n-1) = idx(0) < numel ? k % dv(n-1) : k; | |
637 } | 636 } |
638 else | 637 else |
639 { | 638 { |
640 OCTAVE_LOCAL_BUFFER (Array<octave_idx_type>, rdata, n); | 639 OCTAVE_LOCAL_BUFFER (Array<octave_idx_type>, rdata, n); |
641 | 640 |
644 rdata[j] = Array<octave_idx_type> (odv); | 643 rdata[j] = Array<octave_idx_type> (odv); |
645 | 644 |
646 for (octave_idx_type i = 0; i < len; i++) | 645 for (octave_idx_type i = 0; i < len; i++) |
647 { | 646 { |
648 octave_idx_type k = idx(i); | 647 octave_idx_type k = idx(i); |
649 for (octave_idx_type j = 0; j < n; j++) | 648 for (octave_idx_type j = 0; j < n-1; j++) |
650 { | 649 { |
651 rdata[j](i) = k % dv(j); | 650 rdata[j](i) = k % dv(j); |
652 k /= dv(j); | 651 k /= dv(j); |
653 } | 652 } |
653 | |
654 rdata[n-1](i) = idx(i) < numel ? k % dv(n-1) : k; | |
654 } | 655 } |
655 | 656 |
656 for (octave_idx_type j = 0; j < n; j++) | 657 for (octave_idx_type j = 0; j < n; j++) |
657 retval(j) = rdata[j]; | 658 retval(j) = rdata[j]; |
658 } | 659 } |