Mercurial > octave
annotate 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 |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 //////////////////////////////////////////////////////////////////////// |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 // |
30564
796f54d4ddbf
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
30190
diff
changeset
|
3 // Copyright (C) 2003-2022 The Octave Project Developers |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
4 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 // See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 // distribution or <https://octave.org/copyright/>. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
7 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
8 // This file is part of Octave. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
9 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
10 // Octave is free software: you can redistribute it and/or modify it |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
11 // under the terms of the GNU General Public License as published by |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
12 // the Free Software Foundation, either version 3 of the License, or |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
13 // (at your option) any later version. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
14 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
15 // Octave is distributed in the hope that it will be useful, but |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
16 // WITHOUT ANY WARRANTY; without even the implied warranty of |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
18 // GNU General Public License for more details. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
19 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
20 // You should have received a copy of the GNU General Public License |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
21 // along with Octave; see the file COPYING. If not, see |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
22 // <https://www.gnu.org/licenses/>. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 //////////////////////////////////////////////////////////////////////// |
4588 | 25 |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21660
diff
changeset
|
26 #if defined (HAVE_CONFIG_H) |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21202
diff
changeset
|
27 # include "config.h" |
4588 | 28 #endif |
29 | |
30 #include "Array-util.h" | |
4669 | 31 #include "lo-error.h" |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
9192
diff
changeset
|
32 #include "oct-locbuf.h" |
4588 | 33 |
34 bool | |
8333 | 35 index_in_bounds (const Array<octave_idx_type>& ra_idx, |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
36 const dim_vector& dimensions) |
4588 | 37 { |
38 bool retval = true; | |
39 | |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20230
diff
changeset
|
40 int n = ra_idx.numel (); |
4588 | 41 |
21660
53728df3e4c9
maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents:
21470
diff
changeset
|
42 if (n == dimensions.ndims ()) |
4588 | 43 { |
44 for (int i = 0; i < n; i++) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
45 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
46 if (ra_idx(i) < 0 || ra_idx(i) >= dimensions(i)) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
47 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
48 retval = false; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
49 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
50 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
51 } |
4588 | 52 } |
53 else | |
54 retval = false; | |
55 | |
56 return retval; | |
57 } | |
58 | |
59 void | |
5275 | 60 increment_index (Array<octave_idx_type>& ra_idx, const dim_vector& dimensions, |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
61 int start_dimension) |
4588 | 62 { |
63 ra_idx(start_dimension)++; | |
64 | |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20230
diff
changeset
|
65 int n = ra_idx.numel () - 1; |
21660
53728df3e4c9
maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents:
21470
diff
changeset
|
66 int nda = dimensions.ndims (); |
4588 | 67 |
68 for (int i = start_dimension; i < n; i++) | |
69 { | |
6868 | 70 if (ra_idx(i) < (i < nda ? dimensions(i) : 1)) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
71 break; |
4588 | 72 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
73 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
74 ra_idx(i) = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
75 ra_idx(i+1)++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
76 } |
4588 | 77 } |
78 } | |
79 | |
5275 | 80 octave_idx_type |
81 get_scalar_idx (Array<octave_idx_type>& idx, dim_vector& dims) | |
4588 | 82 { |
5275 | 83 octave_idx_type retval (-1); |
4588 | 84 |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20230
diff
changeset
|
85 int n = idx.numel (); |
4588 | 86 |
87 if (n > 0) | |
88 { | |
89 retval = idx(--n); | |
90 | |
91 while (--n >= 0) | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
92 { |
20218
b2100e1659ac
maint: Use cuddled parentheses when indexing dimension_vectors.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
93 retval *= dims(n); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
94 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
95 retval += idx(n); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
96 } |
4588 | 97 } |
98 return retval; | |
99 } | |
100 | |
5275 | 101 octave_idx_type |
102 num_ones (const Array<octave_idx_type>& ra_idx) | |
4588 | 103 { |
5275 | 104 octave_idx_type retval = 0; |
4635 | 105 |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20230
diff
changeset
|
106 for (octave_idx_type i = 0; i < ra_idx.numel (); i++) |
4588 | 107 { |
20218
b2100e1659ac
maint: Use cuddled parentheses when indexing dimension_vectors.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
108 if (ra_idx(i) == 1) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
109 retval++; |
4588 | 110 } |
4635 | 111 |
4588 | 112 return retval; |
113 } | |
114 | |
115 bool | |
116 is_scalar (const dim_vector& dim) | |
117 { | |
118 bool retval = true; | |
119 | |
21660
53728df3e4c9
maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents:
21470
diff
changeset
|
120 int n = dim.ndims (); |
4588 | 121 |
122 if (n == 0) | |
21660
53728df3e4c9
maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents:
21470
diff
changeset
|
123 retval = false; |
4588 | 124 else |
125 { | |
20714
7b6d8c19dab0
Cuddle increment (++) and decrement (--) operators with variable in question.
Rik <rik@octave.org>
parents:
20652
diff
changeset
|
126 for (int i = 0; i < n; i++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
127 { |
20218
b2100e1659ac
maint: Use cuddled parentheses when indexing dimension_vectors.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
128 if (dim(i) != 1) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
129 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
130 retval = false; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
131 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
132 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
133 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
134 } |
4588 | 135 } |
136 return retval; | |
137 } | |
138 | |
139 bool | |
23592
80e3bfb7bd5a
maint: Deprecate is_vector and replace with isvector.
Rik <rik@octave.org>
parents:
23450
diff
changeset
|
140 isvector (const dim_vector& dim) |
7642
9a4541c622b5
refactor Array::assignN dimensioning code for empty initial matrices
David Bateman <dbateman@free.fr>
parents:
7241
diff
changeset
|
141 { |
9a4541c622b5
refactor Array::assignN dimensioning code for empty initial matrices
David Bateman <dbateman@free.fr>
parents:
7241
diff
changeset
|
142 int m = 0; |
21660
53728df3e4c9
maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents:
21470
diff
changeset
|
143 int n = dim.ndims (); |
7642
9a4541c622b5
refactor Array::assignN dimensioning code for empty initial matrices
David Bateman <dbateman@free.fr>
parents:
7241
diff
changeset
|
144 |
9a4541c622b5
refactor Array::assignN dimensioning code for empty initial matrices
David Bateman <dbateman@free.fr>
parents:
7241
diff
changeset
|
145 if (n == 0) |
9a4541c622b5
refactor Array::assignN dimensioning code for empty initial matrices
David Bateman <dbateman@free.fr>
parents:
7241
diff
changeset
|
146 m = 2; |
9a4541c622b5
refactor Array::assignN dimensioning code for empty initial matrices
David Bateman <dbateman@free.fr>
parents:
7241
diff
changeset
|
147 else |
9a4541c622b5
refactor Array::assignN dimensioning code for empty initial matrices
David Bateman <dbateman@free.fr>
parents:
7241
diff
changeset
|
148 { |
20714
7b6d8c19dab0
Cuddle increment (++) and decrement (--) operators with variable in question.
Rik <rik@octave.org>
parents:
20652
diff
changeset
|
149 for (int i = 0; i < n; i++) |
20218
b2100e1659ac
maint: Use cuddled parentheses when indexing dimension_vectors.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
150 if (dim(i) > 1) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
151 m++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
152 else if (dim(i) < 1) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
153 m += 2; |
7642
9a4541c622b5
refactor Array::assignN dimensioning code for empty initial matrices
David Bateman <dbateman@free.fr>
parents:
7241
diff
changeset
|
154 } |
7924 | 155 |
7642
9a4541c622b5
refactor Array::assignN dimensioning code for empty initial matrices
David Bateman <dbateman@free.fr>
parents:
7241
diff
changeset
|
156 return (m < 2); |
9a4541c622b5
refactor Array::assignN dimensioning code for empty initial matrices
David Bateman <dbateman@free.fr>
parents:
7241
diff
changeset
|
157 } |
9a4541c622b5
refactor Array::assignN dimensioning code for empty initial matrices
David Bateman <dbateman@free.fr>
parents:
7241
diff
changeset
|
158 |
9a4541c622b5
refactor Array::assignN dimensioning code for empty initial matrices
David Bateman <dbateman@free.fr>
parents:
7241
diff
changeset
|
159 bool |
5275 | 160 any_ones (const Array<octave_idx_type>& arr) |
4588 | 161 { |
162 bool retval = false; | |
163 | |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20230
diff
changeset
|
164 for (octave_idx_type i = 0; i < arr.numel (); i++) |
4588 | 165 { |
166 if (arr (i) == 1) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
167 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
168 retval = true; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
169 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
170 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
171 } |
4588 | 172 } |
173 return retval; | |
174 } | |
175 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
176 octave_idx_type |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
177 compute_index (octave_idx_type n, const dim_vector& dims) |
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
178 { |
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
179 if (n < 0) |
22327
d0562b3159c7
move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22323
diff
changeset
|
180 octave::err_invalid_index (n, 1, 1); |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
181 if (n >= dims.numel ()) |
22327
d0562b3159c7
move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22323
diff
changeset
|
182 octave::err_index_out_of_range (1, 1, n+1, dims.numel (), dims); |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
183 |
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
184 return n; |
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
185 } |
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
186 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
187 octave_idx_type |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
188 compute_index (octave_idx_type i, octave_idx_type j, const dim_vector& dims) |
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
189 { |
20542
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
190 if (i < 0) |
22327
d0562b3159c7
move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22323
diff
changeset
|
191 octave::err_invalid_index (i, 2, 1); |
21118
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
192 if (j < 0) |
22327
d0562b3159c7
move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22323
diff
changeset
|
193 octave::err_invalid_index (j, 2, 2); |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
194 if (i >= dims(0)) |
22327
d0562b3159c7
move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22323
diff
changeset
|
195 octave::err_index_out_of_range (2, 1, i+1, dims(0), dims); |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
196 if (j >= dims.numel (1)) |
22327
d0562b3159c7
move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22323
diff
changeset
|
197 octave::err_index_out_of_range (2, 2, j+1, dims.numel (1), dims); |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
198 |
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
199 return j*dims(0) + i; |
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
200 } |
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
201 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
202 octave_idx_type |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
203 compute_index (octave_idx_type i, octave_idx_type j, octave_idx_type k, |
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
204 const dim_vector& dims) |
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
205 { |
20542
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
206 if (i < 0) |
22327
d0562b3159c7
move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22323
diff
changeset
|
207 octave::err_invalid_index (i, 3, 1); |
21118
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
208 if (j < 0) |
22327
d0562b3159c7
move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22323
diff
changeset
|
209 octave::err_invalid_index (j, 3, 2); |
21118
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
210 if (k < 0) |
22327
d0562b3159c7
move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22323
diff
changeset
|
211 octave::err_invalid_index (k, 3, 3); |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
212 if (i >= dims(0)) |
22327
d0562b3159c7
move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22323
diff
changeset
|
213 octave::err_index_out_of_range (3, 1, i+1, dims(0), dims); |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
214 if (j >= dims(1)) |
22327
d0562b3159c7
move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22323
diff
changeset
|
215 octave::err_index_out_of_range (3, 2, j+1, dims(1), dims); |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
216 if (k >= dims.numel (2)) |
22327
d0562b3159c7
move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22323
diff
changeset
|
217 octave::err_index_out_of_range (3, 3, k+1, dims.numel (2), dims); |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
218 |
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
219 return (k*dims(1) + j)*dims(0) + i; |
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
220 } |
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
221 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
222 octave_idx_type |
5275 | 223 compute_index (const Array<octave_idx_type>& ra_idx, const dim_vector& dims) |
4588 | 224 { |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20230
diff
changeset
|
225 int nd = ra_idx.numel (); |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
226 const dim_vector dv = dims.redim (nd); |
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
227 for (int d = 0; d < nd; d++) |
4588 | 228 { |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
229 if (ra_idx(d) < 0) |
22327
d0562b3159c7
move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22323
diff
changeset
|
230 octave::err_invalid_index (ra_idx(d), nd, d+1); |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
231 if (ra_idx(d) >= dv(d)) |
22327
d0562b3159c7
move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22323
diff
changeset
|
232 octave::err_index_out_of_range (nd, d+1, ra_idx(d)+1, dv(d), dims); |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
233 } |
4588 | 234 |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
235 return dv.compute_index (ra_idx.data ()); |
4588 | 236 } |
237 | |
5275 | 238 Array<octave_idx_type> |
29569
29a1f8fd8ee6
move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
239 conv_to_int_array (const Array<octave::idx_vector>& a) |
4588 | 240 { |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
241 Array<octave_idx_type> retval (a.dims ()); |
4588 | 242 |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20230
diff
changeset
|
243 for (octave_idx_type i = 0; i < a.numel (); i++) |
14844
5bc9b9cb4362
maint: Use Octave coding conventions for cuddled parenthesis in retval assignments.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
244 retval(i) = a(i).elem (0); |
4588 | 245 |
246 return retval; | |
247 } | |
248 | |
29569
29a1f8fd8ee6
move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
249 Array<octave::idx_vector> |
29a1f8fd8ee6
move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
250 conv_to_array (const octave::idx_vector *tmp, const octave_idx_type len) |
4588 | 251 { |
29569
29a1f8fd8ee6
move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
252 Array<octave::idx_vector> retval (dim_vector (len, 1)); |
4588 | 253 |
5275 | 254 for (octave_idx_type i = 0; i < len; i++) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
255 retval(i) = tmp[i]; |
4588 | 256 |
257 return retval; | |
258 } | |
259 | |
260 dim_vector | |
29569
29a1f8fd8ee6
move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
261 freeze (Array<octave::idx_vector>& ra_idx, const dim_vector& dimensions, int resize_ok) |
4588 | 262 { |
263 dim_vector retval; | |
264 | |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20230
diff
changeset
|
265 int n = ra_idx.numel (); |
4588 | 266 |
21660
53728df3e4c9
maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents:
21470
diff
changeset
|
267 assert (n == dimensions.ndims ()); |
4588 | 268 |
269 retval.resize (n); | |
270 | |
23795
980f39c3ab90
Use C++11 nullptr rather than 0 in code (bug #51565).
Rik <rik@octave.org>
parents:
23592
diff
changeset
|
271 static const char *tag[3] = { "row", "column", nullptr }; |
5136 | 272 |
4588 | 273 for (int i = 0; i < n; i++) |
21470
bbf3814049e2
* Array-util.cc (freeze): Fix out-of-bound indexing (bug #47423)
Mike Miller <mtmiller@octave.org>
parents:
21301
diff
changeset
|
274 retval(i) = ra_idx(i).freeze (dimensions(i), tag[i < 2 ? i : 2], |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
275 resize_ok); |
4588 | 276 |
277 return retval; | |
278 } | |
279 | |
280 bool | |
5519 | 281 vector_equivalent (const dim_vector& dv) |
4588 | 282 { |
21660
53728df3e4c9
maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents:
21470
diff
changeset
|
283 int n = dv.ndims (); |
4588 | 284 |
285 bool found_first = false; | |
286 | |
5519 | 287 for (int i = 0; i < n; i++) |
4588 | 288 { |
5519 | 289 if (dv(i) != 1) |
4588 | 290 { |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
291 if (! found_first) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
292 found_first = true; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
293 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
294 return false; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
295 } |
4588 | 296 } |
297 | |
298 return true; | |
299 } | |
300 | |
301 bool | |
29569
29a1f8fd8ee6
move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
302 all_ok (const Array<octave::idx_vector>& ra_idx) |
4588 | 303 { |
304 bool retval = true; | |
305 | |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20230
diff
changeset
|
306 octave_idx_type n = ra_idx.numel (); |
4588 | 307 |
5275 | 308 for (octave_idx_type i = 0; i < n; i++) |
4588 | 309 { |
310 if (! ra_idx(i)) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
311 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
312 retval = false; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
313 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
314 } |
4588 | 315 } |
316 | |
317 return retval; | |
318 } | |
319 | |
320 bool | |
29569
29a1f8fd8ee6
move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
321 any_orig_empty (const Array<octave::idx_vector>& ra_idx) |
4588 | 322 { |
323 bool retval = false; | |
324 | |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20230
diff
changeset
|
325 octave_idx_type n = ra_idx.numel (); |
4588 | 326 |
5275 | 327 for (octave_idx_type i = 0; i < n; i++) |
4588 | 328 { |
329 if (ra_idx(i).orig_empty ()) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
330 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
331 retval = true; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
332 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
333 } |
4588 | 334 } |
335 | |
336 return retval; | |
337 } | |
338 | |
339 bool | |
29569
29a1f8fd8ee6
move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
340 all_colon_equiv (const Array<octave::idx_vector>& ra_idx, |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
341 const dim_vector& frozen_lengths) |
4588 | 342 { |
343 bool retval = true; | |
344 | |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20230
diff
changeset
|
345 octave_idx_type idx_n = ra_idx.numel (); |
4588 | 346 |
21660
53728df3e4c9
maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents:
21470
diff
changeset
|
347 int n = frozen_lengths.ndims (); |
4588 | 348 |
349 assert (idx_n == n); | |
350 | |
5275 | 351 for (octave_idx_type i = 0; i < n; i++) |
4588 | 352 { |
353 if (! ra_idx(i).is_colon_equiv (frozen_lengths(i))) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
354 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
355 retval = false; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
356 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
357 } |
4588 | 358 } |
359 | |
360 return retval; | |
361 } | |
362 | |
363 bool | |
5275 | 364 all_ones (const Array<octave_idx_type>& arr) |
4588 | 365 { |
366 bool retval = true; | |
367 | |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20230
diff
changeset
|
368 for (octave_idx_type i = 0; i < arr.numel (); i++) |
4588 | 369 { |
370 if (arr(i) != 1) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
371 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
372 retval = false; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
373 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
374 } |
4588 | 375 } |
376 | |
377 return retval; | |
378 } | |
379 | |
5275 | 380 Array<octave_idx_type> |
29569
29a1f8fd8ee6
move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
381 get_elt_idx (const Array<octave::idx_vector>& ra_idx, |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
382 const Array<octave_idx_type>& result_idx) |
4588 | 383 { |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20230
diff
changeset
|
384 octave_idx_type n = ra_idx.numel (); |
4588 | 385 |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
386 Array<octave_idx_type> retval (dim_vector (n, 1)); |
4588 | 387 |
5275 | 388 for (octave_idx_type i = 0; i < n; i++) |
4588 | 389 retval(i) = ra_idx(i).elem (result_idx(i)); |
390 | |
391 return retval; | |
392 } | |
393 | |
5275 | 394 Array<octave_idx_type> |
395 get_ra_idx (octave_idx_type idx, const dim_vector& dims) | |
4588 | 396 { |
5275 | 397 Array<octave_idx_type> retval; |
4588 | 398 |
21660
53728df3e4c9
maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents:
21470
diff
changeset
|
399 int n_dims = dims.ndims (); |
4588 | 400 |
11574
a83bad07f7e3
attempt better backward compatibility for Array resize functions
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
401 retval.resize (dim_vector (n_dims, 1)); |
4588 | 402 |
403 for (int i = 0; i < n_dims; i++) | |
404 retval(i) = 0; | |
405 | |
4747 | 406 assert (idx > 0 || idx < dims.numel ()); |
4588 | 407 |
5275 | 408 for (octave_idx_type i = 0; i < idx; i++) |
4588 | 409 increment_index (retval, dims); |
410 | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
411 // FIXME: the solution using increment_index is not efficient. |
4588 | 412 |
413 #if 0 | |
5275 | 414 octave_idx_type var = 1; |
4588 | 415 for (int i = 0; i < n_dims; i++) |
416 { | |
8333 | 417 std::cout << "idx: " << idx << ", var: " << var |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
418 << ", dims(" << i << "): " << dims(i) <<"\n"; |
4588 | 419 retval(i) = ((int)floor(((idx) / (double)var))) % dims(i); |
420 idx -= var * retval(i); | |
421 var = dims(i); | |
422 } | |
423 #endif | |
424 | |
425 return retval; | |
426 } | |
427 | |
8677
095ae5e0a831
eliminte some compiler warnings
John W. Eaton <jwe@octave.org>
parents:
8333
diff
changeset
|
428 dim_vector |
29569
29a1f8fd8ee6
move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
429 zero_dims_inquire (const Array<octave::idx_vector>& ia, const dim_vector& rhdv) |
4588 | 430 { |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20230
diff
changeset
|
431 int ial = ia.numel (); |
21660
53728df3e4c9
maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents:
21470
diff
changeset
|
432 int rhdvl = rhdv.ndims (); |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9479
diff
changeset
|
433 dim_vector rdv = dim_vector::alloc (ial); |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
434 bool *scalar = new bool [ial]; |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
435 bool *colon = new bool [ial]; |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
436 // Mark scalars and colons, count non-scalar indices. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
437 int nonsc = 0; |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
438 bool all_colons = true; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
439 for (int i = 0; i < ial; i++) |
4588 | 440 { |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
441 // FIXME: should we check for length() instead? |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
442 scalar[i] = ia(i).is_scalar (); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
443 colon[i] = ia(i).is_colon (); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
444 if (! scalar[i]) nonsc++; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
445 if (! colon[i]) rdv(i) = ia(i).extent (0); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
446 all_colons = all_colons && colon[i]; |
4588 | 447 } |
448 | |
8333 | 449 // If the number of nonscalar indices matches the dimensionality of |
450 // RHS, we try an exact match, inquiring even singleton dimensions. | |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
451 if (all_colons) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
452 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
453 rdv = rhdv; |
15018
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14844
diff
changeset
|
454 rdv.resize (ial, 1); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
455 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
456 else if (nonsc == rhdvl) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
457 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
458 for (int i = 0, j = 0; i < ial; i++) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
459 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
460 if (scalar[i]) continue; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
461 if (colon[i]) |
9192
864805896876
fix zero-dims assignment bug
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
462 rdv(i) = rhdv(j); |
864805896876
fix zero-dims assignment bug
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
463 j++; |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
464 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
465 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
466 else |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
467 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
468 dim_vector rhdv0 = rhdv; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
469 rhdv0.chop_all_singletons (); |
21660
53728df3e4c9
maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents:
21470
diff
changeset
|
470 int rhdv0l = rhdv0.ndims (); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
471 for (int i = 0, j = 0; i < ial; i++) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
472 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
473 if (scalar[i]) continue; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
474 if (colon[i]) |
15018
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14844
diff
changeset
|
475 rdv(i) = (j < rhdv0l) ? rhdv0(j++) : 1; |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
476 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
477 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
478 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
479 delete [] scalar; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
480 delete [] colon; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
481 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
482 return rdv; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
483 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
484 |
8677
095ae5e0a831
eliminte some compiler warnings
John W. Eaton <jwe@octave.org>
parents:
8333
diff
changeset
|
485 dim_vector |
29569
29a1f8fd8ee6
move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
486 zero_dims_inquire (const octave::idx_vector& i, const octave::idx_vector& j, |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
487 const dim_vector& rhdv) |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
488 { |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
489 bool icol = i.is_colon (); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
490 bool jcol = j.is_colon (); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
491 dim_vector rdv; |
21660
53728df3e4c9
maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents:
21470
diff
changeset
|
492 if (icol && jcol && rhdv.ndims () == 2) |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
493 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
494 rdv(0) = rhdv(0); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
495 rdv(1) = rhdv(1); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
496 } |
21660
53728df3e4c9
maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents:
21470
diff
changeset
|
497 else if (rhdv.ndims () == 2 |
9549
ed34b1da0e26
zero matrix assignment fix
Jaroslav Hajek <highegg@gmail.com>
parents:
9507
diff
changeset
|
498 && ! i.is_scalar () && ! j.is_scalar ()) |
ed34b1da0e26
zero matrix assignment fix
Jaroslav Hajek <highegg@gmail.com>
parents:
9507
diff
changeset
|
499 { |
23450
855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents:
23443
diff
changeset
|
500 rdv(0) = (icol ? rhdv(0) : i.extent (0)); |
855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents:
23443
diff
changeset
|
501 rdv(1) = (jcol ? rhdv(1) : j.extent (0)); |
9549
ed34b1da0e26
zero matrix assignment fix
Jaroslav Hajek <highegg@gmail.com>
parents:
9507
diff
changeset
|
502 } |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
503 else |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
504 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
505 dim_vector rhdv0 = rhdv; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
506 rhdv0.chop_all_singletons (); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
507 int k = 0; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
508 rdv(0) = i.extent (0); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
509 if (icol) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
510 rdv(0) = rhdv0(k++); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
511 else if (! i.is_scalar ()) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
512 k++; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
513 rdv(1) = j.extent (0); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
514 if (jcol) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
515 rdv(1) = rhdv0(k++); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
516 else if (! j.is_scalar ()) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
517 k++; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
518 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
519 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
520 return rdv; |
4588 | 521 } |
522 | |
29569
29a1f8fd8ee6
move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
523 octave::idx_vector |
29a1f8fd8ee6
move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
524 sub2ind (const dim_vector& dv, const Array<octave::idx_vector>& idxa) |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
9192
diff
changeset
|
525 { |
29569
29a1f8fd8ee6
move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
526 octave::idx_vector retval; |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20230
diff
changeset
|
527 octave_idx_type len = idxa.numel (); |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
9192
diff
changeset
|
528 |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
529 if (len == 0) |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
530 (*current_liboctave_error_handler) ("sub2ind: needs at least 2 indices"); |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
531 |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
532 const dim_vector dvx = dv.redim (len); |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
533 bool all_ranges = true; |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
534 octave_idx_type clen = -1; |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
9192
diff
changeset
|
535 |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
536 for (octave_idx_type i = 0; i < len; i++) |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
537 { |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
538 try |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
9192
diff
changeset
|
539 { |
29569
29a1f8fd8ee6
move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
540 octave::idx_vector idx = idxa(i); |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
541 octave_idx_type n = dvx(i); |
20542
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
542 |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
543 all_ranges = all_ranges && idx.is_range (); |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
544 if (clen < 0) |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
545 clen = idx.length (n); |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
546 else if (clen != idx.length (n)) |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
547 (*current_liboctave_error_handler) |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
548 ("sub2ind: lengths of indices must match"); |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
9192
diff
changeset
|
549 |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
550 if (idx.extent (n) > n) |
28004
403df0b32204
update out_of_range error messages
John W. Eaton <jwe@octave.org>
parents:
27923
diff
changeset
|
551 octave::err_index_out_of_range (len, i+1, idx.extent (n), n, dv); |
20542
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
552 } |
29163
8f67ad8b3103
maint: Updating naming conventions for exceptions and use const where possible.
Rik <rik@octave.org>
parents:
28004
diff
changeset
|
553 catch (octave::index_exception& ie) |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
554 { |
29163
8f67ad8b3103
maint: Updating naming conventions for exceptions and use const where possible.
Rik <rik@octave.org>
parents:
28004
diff
changeset
|
555 ie.set_pos_if_unset (len, i+1); |
8f67ad8b3103
maint: Updating naming conventions for exceptions and use const where possible.
Rik <rik@octave.org>
parents:
28004
diff
changeset
|
556 ie.set_var (); |
8f67ad8b3103
maint: Updating naming conventions for exceptions and use const where possible.
Rik <rik@octave.org>
parents:
28004
diff
changeset
|
557 std::string msg = ie.message (); |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
558 (*current_liboctave_error_with_id_handler) |
29163
8f67ad8b3103
maint: Updating naming conventions for exceptions and use const where possible.
Rik <rik@octave.org>
parents:
28004
diff
changeset
|
559 (ie.err_id (), "%s", msg.c_str ()); |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
560 } |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
561 } |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
562 // idxa known to be valid. |
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
563 // Shouldn't need to catch index_exception below here. |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
9192
diff
changeset
|
564 |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
565 if (len == 1) |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
566 retval = idxa(0); |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
567 else if (clen == 1) |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
568 { |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
569 // All scalars case - the result is a scalar. |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
570 octave_idx_type idx = idxa(len-1)(0); |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
571 for (octave_idx_type i = len - 2; i >= 0; i--) |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
572 idx = dvx(i) * idx + idxa(i)(0); |
29569
29a1f8fd8ee6
move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
573 retval = octave::idx_vector (idx); |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
574 } |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
575 else if (all_ranges && clen != 0) |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
576 { |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
577 // All ranges case - the result is a range. |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
578 octave_idx_type start = 0; |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
579 octave_idx_type step = 0; |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
580 for (octave_idx_type i = len - 1; i >= 0; i--) |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
9192
diff
changeset
|
581 { |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
582 octave_idx_type xstart = idxa(i)(0); |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
583 octave_idx_type xstep = idxa(i)(1) - xstart; |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
584 start = dvx(i) * start + xstart; |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
585 step = dvx(i) * step + xstep; |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
9192
diff
changeset
|
586 } |
29569
29a1f8fd8ee6
move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
587 retval = octave::idx_vector::make_range (start, step, clen); |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
9192
diff
changeset
|
588 } |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
9192
diff
changeset
|
589 else |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
590 { |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
591 Array<octave_idx_type> idx (idxa(0).orig_dimensions ()); |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
592 |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
593 for (octave_idx_type i = len - 1; i >= 0; i--) |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
594 { |
30190
2429561a1415
replace sub2ind helper classes with lambda expression
John W. Eaton <jwe@octave.org>
parents:
30161
diff
changeset
|
595 // Initialized inside the loop so that each call to |
2429561a1415
replace sub2ind helper classes with lambda expression
John W. Eaton <jwe@octave.org>
parents:
30161
diff
changeset
|
596 // idx_vector::loop operates from the beginning of IDX_VEC. |
2429561a1415
replace sub2ind helper classes with lambda expression
John W. Eaton <jwe@octave.org>
parents:
30161
diff
changeset
|
597 |
2429561a1415
replace sub2ind helper classes with lambda expression
John W. Eaton <jwe@octave.org>
parents:
30161
diff
changeset
|
598 octave_idx_type *idx_vec = idx.fortran_vec (); |
2429561a1415
replace sub2ind helper classes with lambda expression
John W. Eaton <jwe@octave.org>
parents:
30161
diff
changeset
|
599 |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
600 if (i < len - 1) |
30190
2429561a1415
replace sub2ind helper classes with lambda expression
John W. Eaton <jwe@octave.org>
parents:
30161
diff
changeset
|
601 { |
2429561a1415
replace sub2ind helper classes with lambda expression
John W. Eaton <jwe@octave.org>
parents:
30161
diff
changeset
|
602 octave_idx_type n = dvx(i); |
2429561a1415
replace sub2ind helper classes with lambda expression
John W. Eaton <jwe@octave.org>
parents:
30161
diff
changeset
|
603 |
2429561a1415
replace sub2ind helper classes with lambda expression
John W. Eaton <jwe@octave.org>
parents:
30161
diff
changeset
|
604 idxa(i).loop (clen, [=, &idx_vec] (octave_idx_type k) { |
2429561a1415
replace sub2ind helper classes with lambda expression
John W. Eaton <jwe@octave.org>
parents:
30161
diff
changeset
|
605 (*idx_vec++ *= n) += k; |
2429561a1415
replace sub2ind helper classes with lambda expression
John W. Eaton <jwe@octave.org>
parents:
30161
diff
changeset
|
606 }); |
2429561a1415
replace sub2ind helper classes with lambda expression
John W. Eaton <jwe@octave.org>
parents:
30161
diff
changeset
|
607 } |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
608 else |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
609 idxa(i).copy_data (idx_vec); |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
610 } |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
611 |
29569
29a1f8fd8ee6
move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
612 retval = octave::idx_vector (idx); |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
613 } |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
9192
diff
changeset
|
614 |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
9192
diff
changeset
|
615 return retval; |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
9192
diff
changeset
|
616 } |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
9192
diff
changeset
|
617 |
29569
29a1f8fd8ee6
move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
618 Array<octave::idx_vector> |
29a1f8fd8ee6
move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
619 ind2sub (const dim_vector& dv, const octave::idx_vector& idx) |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
9192
diff
changeset
|
620 { |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
621 octave_idx_type len = idx.length (0); |
21660
53728df3e4c9
maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents:
21470
diff
changeset
|
622 octave_idx_type n = dv.ndims (); |
29569
29a1f8fd8ee6
move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
623 Array<octave::idx_vector> retval (dim_vector (n, 1)); |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
9192
diff
changeset
|
624 octave_idx_type numel = dv.numel (); |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
9192
diff
changeset
|
625 |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
626 if (idx.is_scalar ()) |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
627 { |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
628 octave_idx_type k = idx(0); |
30902
972959edc3ff
Allow sub2ind() to accept indices outside the size of the input subscripts (bug #62184)
John W. Eaton <jwe@octave.org>
parents:
30564
diff
changeset
|
629 for (octave_idx_type j = 0; j < n-1; j++) |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
630 { |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
631 retval(j) = k % dv(j); |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
632 k /= dv(j); |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
633 } |
30902
972959edc3ff
Allow sub2ind() to accept indices outside the size of the input subscripts (bug #62184)
John W. Eaton <jwe@octave.org>
parents:
30564
diff
changeset
|
634 |
972959edc3ff
Allow sub2ind() to accept indices outside the size of the input subscripts (bug #62184)
John W. Eaton <jwe@octave.org>
parents:
30564
diff
changeset
|
635 retval(n-1) = idx(0) < numel ? k % dv(n-1) : k; |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
636 } |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
9192
diff
changeset
|
637 else |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
9192
diff
changeset
|
638 { |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
639 OCTAVE_LOCAL_BUFFER (Array<octave_idx_type>, rdata, n); |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
640 |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
641 dim_vector odv = idx.orig_dimensions (); |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
642 for (octave_idx_type j = 0; j < n; j++) |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
643 rdata[j] = Array<octave_idx_type> (odv); |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
644 |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
645 for (octave_idx_type i = 0; i < len; i++) |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
9192
diff
changeset
|
646 { |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
647 octave_idx_type k = idx(i); |
30902
972959edc3ff
Allow sub2ind() to accept indices outside the size of the input subscripts (bug #62184)
John W. Eaton <jwe@octave.org>
parents:
30564
diff
changeset
|
648 for (octave_idx_type j = 0; j < n-1; j++) |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
9192
diff
changeset
|
649 { |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
650 rdata[j](i) = k % dv(j); |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
9192
diff
changeset
|
651 k /= dv(j); |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
9192
diff
changeset
|
652 } |
30902
972959edc3ff
Allow sub2ind() to accept indices outside the size of the input subscripts (bug #62184)
John W. Eaton <jwe@octave.org>
parents:
30564
diff
changeset
|
653 |
972959edc3ff
Allow sub2ind() to accept indices outside the size of the input subscripts (bug #62184)
John W. Eaton <jwe@octave.org>
parents:
30564
diff
changeset
|
654 rdata[n-1](i) = idx(i) < numel ? k % dv(n-1) : k; |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
9192
diff
changeset
|
655 } |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
9192
diff
changeset
|
656 |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
657 for (octave_idx_type j = 0; j < n; j++) |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
658 retval(j) = rdata[j]; |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
659 } |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
9192
diff
changeset
|
660 |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
9192
diff
changeset
|
661 return retval; |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
9192
diff
changeset
|
662 } |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
9192
diff
changeset
|
663 |
7241 | 664 int |
665 permute_vector_compare (const void *a, const void *b) | |
666 { | |
667 const permute_vector *pva = static_cast<const permute_vector *> (a); | |
668 const permute_vector *pvb = static_cast<const permute_vector *> (b); | |
669 | |
670 return pva->pidx > pvb->pidx; | |
671 } |