Mercurial > jwe > octave
annotate liboctave/array/Array.cc @ 21131:54527108599a
store more --enable-FEATURE info in octave_config_info
* configure.ac: (ENABLE_64, ENABLE_BOUNDS_CHECK, ENABLE_DOCS,
ENABLE_FLOAT_TRUNCATE, ENABLE_JIT, ENABLE_OPENMP): New variables.
macros. Handle consistently and AC_DEFINE them.
* common.mk, toplev.cc, oct-conf.in.h: Substitute new variables and
store them in octave_config_info.
* oct-conf-post.in.h: Provide old macro names for compatibility.
* Array.cc, Array.h, DiagArray2.h, PermMatrix.h, Range.cc, Sparse.h,
dim-vector.h, idx-vector.h, oct-refcount.h: Fix uses of macros.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 21 Jan 2016 18:18:16 -0500 |
parents | 499b851fbfae |
children | 7cac4e7458f2 |
rev | line source |
---|---|
1993 | 1 // Template array classes |
237 | 2 /* |
3 | |
19697
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19596
diff
changeset
|
4 Copyright (C) 1993-2015 John W. Eaton |
11523 | 5 Copyright (C) 2008-2009 Jaroslav Hajek |
9601
a9b37bae1802
add a couple of missing copyright statements
Jaroslav Hajek <highegg@gmail.com>
parents:
9556
diff
changeset
|
6 Copyright (C) 2009 VZLU Prague |
237 | 7 |
8 This file is part of Octave. | |
9 | |
10 Octave is free software; you can redistribute it and/or modify it | |
11 under the terms of the GNU General Public License as published by the | |
7016 | 12 Free Software Foundation; either version 3 of the License, or (at your |
13 option) any later version. | |
237 | 14 |
15 Octave is distributed in the hope that it will be useful, but WITHOUT | |
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
18 for more details. | |
19 | |
20 You should have received a copy of the GNU General Public License | |
7016 | 21 along with Octave; see the file COPYING. If not, see |
22 <http://www.gnu.org/licenses/>. | |
237 | 23 |
24 */ | |
25 | |
26 #ifdef HAVE_CONFIG_H | |
1192 | 27 #include <config.h> |
237 | 28 #endif |
29 | |
1367 | 30 #include <cassert> |
449 | 31 |
3503 | 32 #include <iostream> |
5765 | 33 #include <sstream> |
5607 | 34 #include <vector> |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
35 #include <algorithm> |
6674 | 36 #include <new> |
1560 | 37 |
237 | 38 #include "Array.h" |
4588 | 39 #include "Array-util.h" |
1560 | 40 #include "idx-vector.h" |
41 #include "lo-error.h" | |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
8333
diff
changeset
|
42 #include "oct-locbuf.h" |
1560 | 43 |
1360 | 44 // One dimensional array class. Handles the reference counting for |
45 // all the derived classes. | |
237 | 46 |
47 template <class T> | |
4834 | 48 Array<T>::Array (const Array<T>& a, const dim_vector& dv) |
11507
c3ad80f4ce36
Array.h, Array.cc: more constructor fixes
John W. Eaton <jwe@octave.org>
parents:
11242
diff
changeset
|
49 : dimensions (dv), rep (a.rep), |
8523
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
50 slice_data (a.slice_data), slice_len (a.slice_len) |
4834 | 51 { |
10352 | 52 if (dimensions.safe_numel () != a.numel ()) |
9731 | 53 { |
10352 | 54 std::string dimensions_str = a.dimensions.str (); |
55 std::string new_dims_str = dimensions.str (); | |
56 | |
57 (*current_liboctave_error_handler) | |
58 ("reshape: can't reshape %s array to %s array", | |
59 dimensions_str.c_str (), new_dims_str.c_str ()); | |
60 } | |
61 | |
62 // This goes here because if an exception is thrown by the above, | |
63 // destructor will be never called. | |
64 rep->count++; | |
65 dimensions.chop_trailing_singletons (); | |
66 } | |
67 | |
68 template <class T> | |
8524
937921654627
clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
69 void |
937921654627
clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
70 Array<T>::fill (const T& val) |
937921654627
clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
71 { |
937921654627
clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
72 if (rep->count > 1) |
937921654627
clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
73 { |
937921654627
clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
74 --rep->count; |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20228
diff
changeset
|
75 rep = new ArrayRep (numel (), val); |
8524
937921654627
clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
76 slice_data = rep->data; |
937921654627
clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
77 } |
937921654627
clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
78 else |
19381
af41e41ad28e
replace oct-mem.h inline indirections by standard function calls.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18146
diff
changeset
|
79 std::fill_n (slice_data, slice_len, val); |
8524
937921654627
clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
80 } |
937921654627
clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
81 |
937921654627
clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
82 template <class T> |
9546
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
9507
diff
changeset
|
83 void |
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
9507
diff
changeset
|
84 Array<T>::clear (void) |
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
9507
diff
changeset
|
85 { |
13985
43cc49c7abd1
Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents:
13141
diff
changeset
|
86 if (--rep->count == 0) |
9546
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
9507
diff
changeset
|
87 delete rep; |
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
9507
diff
changeset
|
88 |
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
9507
diff
changeset
|
89 rep = nil_rep (); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
90 rep->count++; |
9546
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
9507
diff
changeset
|
91 slice_data = rep->data; |
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
9507
diff
changeset
|
92 slice_len = rep->len; |
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
9507
diff
changeset
|
93 |
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
9507
diff
changeset
|
94 dimensions = dim_vector (); |
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
9507
diff
changeset
|
95 } |
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
9507
diff
changeset
|
96 |
1beb23d2b892
optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents:
9507
diff
changeset
|
97 template <class T> |
9624
3fc7272937ce
implement Array<T>::clear overloads
Jaroslav Hajek <highegg@gmail.com>
parents:
9601
diff
changeset
|
98 void |
3fc7272937ce
implement Array<T>::clear overloads
Jaroslav Hajek <highegg@gmail.com>
parents:
9601
diff
changeset
|
99 Array<T>::clear (const dim_vector& dv) |
3fc7272937ce
implement Array<T>::clear overloads
Jaroslav Hajek <highegg@gmail.com>
parents:
9601
diff
changeset
|
100 { |
13985
43cc49c7abd1
Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents:
13141
diff
changeset
|
101 if (--rep->count == 0) |
9624
3fc7272937ce
implement Array<T>::clear overloads
Jaroslav Hajek <highegg@gmail.com>
parents:
9601
diff
changeset
|
102 delete rep; |
3fc7272937ce
implement Array<T>::clear overloads
Jaroslav Hajek <highegg@gmail.com>
parents:
9601
diff
changeset
|
103 |
9840
c0b54271904b
improve safe numel() calculation for arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9839
diff
changeset
|
104 rep = new ArrayRep (dv.safe_numel ()); |
9624
3fc7272937ce
implement Array<T>::clear overloads
Jaroslav Hajek <highegg@gmail.com>
parents:
9601
diff
changeset
|
105 slice_data = rep->data; |
3fc7272937ce
implement Array<T>::clear overloads
Jaroslav Hajek <highegg@gmail.com>
parents:
9601
diff
changeset
|
106 slice_len = rep->len; |
3fc7272937ce
implement Array<T>::clear overloads
Jaroslav Hajek <highegg@gmail.com>
parents:
9601
diff
changeset
|
107 |
3fc7272937ce
implement Array<T>::clear overloads
Jaroslav Hajek <highegg@gmail.com>
parents:
9601
diff
changeset
|
108 dimensions = dv; |
10095
eb8ac0eed9f1
always chop dimension vector when constructing Arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
10075
diff
changeset
|
109 dimensions.chop_trailing_singletons (); |
9624
3fc7272937ce
implement Array<T>::clear overloads
Jaroslav Hajek <highegg@gmail.com>
parents:
9601
diff
changeset
|
110 } |
3fc7272937ce
implement Array<T>::clear overloads
Jaroslav Hajek <highegg@gmail.com>
parents:
9601
diff
changeset
|
111 |
3fc7272937ce
implement Array<T>::clear overloads
Jaroslav Hajek <highegg@gmail.com>
parents:
9601
diff
changeset
|
112 template <class T> |
4532 | 113 Array<T> |
114 Array<T>::squeeze (void) const | |
115 { | |
116 Array<T> retval = *this; | |
117 | |
4929 | 118 if (ndims () > 2) |
4532 | 119 { |
4929 | 120 bool dims_changed = false; |
121 | |
122 dim_vector new_dimensions = dimensions; | |
123 | |
124 int k = 0; | |
125 | |
126 for (int i = 0; i < ndims (); i++) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
127 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
128 if (dimensions(i) == 1) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
129 dims_changed = true; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
130 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
131 new_dimensions(k++) = dimensions(i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
132 } |
4929 | 133 |
134 if (dims_changed) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
135 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
136 switch (k) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
137 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
138 case 0: |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
139 new_dimensions = dim_vector (1, 1); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
140 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
141 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
142 case 1: |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
143 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
144 octave_idx_type tmp = new_dimensions(0); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
145 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
146 new_dimensions.resize (2); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
147 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
148 new_dimensions(0) = tmp; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
149 new_dimensions(1) = 1; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
150 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
151 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
152 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
153 default: |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
154 new_dimensions.resize (k); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
155 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
156 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
157 } |
4532 | 158 |
8524
937921654627
clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
159 retval = Array<T> (*this, new_dimensions); |
4532 | 160 } |
161 | |
162 return retval; | |
163 } | |
164 | |
237 | 165 template <class T> |
5275 | 166 octave_idx_type |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10636
diff
changeset
|
167 Array<T>::compute_index (octave_idx_type i, octave_idx_type j) const |
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10636
diff
changeset
|
168 { |
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10636
diff
changeset
|
169 return ::compute_index (i, j, dimensions); |
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10636
diff
changeset
|
170 } |
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10636
diff
changeset
|
171 |
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10636
diff
changeset
|
172 template <class T> |
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10636
diff
changeset
|
173 octave_idx_type |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
174 Array<T>::compute_index (octave_idx_type i, octave_idx_type j, |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
175 octave_idx_type k) const |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10636
diff
changeset
|
176 { |
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10636
diff
changeset
|
177 return ::compute_index (i, j, k, dimensions); |
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10636
diff
changeset
|
178 } |
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10636
diff
changeset
|
179 |
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10636
diff
changeset
|
180 template <class T> |
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10636
diff
changeset
|
181 octave_idx_type |
5275 | 182 Array<T>::compute_index (const Array<octave_idx_type>& ra_idx) const |
237 | 183 { |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10636
diff
changeset
|
184 return ::compute_index (ra_idx, dimensions); |
237 | 185 } |
186 | |
2049 | 187 template <class T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
188 T& |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
189 Array<T>::checkelem (octave_idx_type n) |
2049 | 190 { |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10636
diff
changeset
|
191 // Do checks directly to avoid recomputing slice_len. |
10370
9c4daf174387
implement IDs for common liboctave exceptions
Jaroslav Hajek <highegg@gmail.com>
parents:
10366
diff
changeset
|
192 if (n < 0) |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
21098
diff
changeset
|
193 err_invalid_index (n); |
10370
9c4daf174387
implement IDs for common liboctave exceptions
Jaroslav Hajek <highegg@gmail.com>
parents:
10366
diff
changeset
|
194 if (n >= slice_len) |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
21098
diff
changeset
|
195 err_index_out_of_range (1, 1, n+1, slice_len, dimensions); |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
196 |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
197 return elem (n); |
2049 | 198 } |
199 | |
3933 | 200 template <class T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
201 T& |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
202 Array<T>::checkelem (octave_idx_type i, octave_idx_type j) |
4513 | 203 { |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10636
diff
changeset
|
204 return elem (compute_index (i, j)); |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
205 } |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
206 |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
207 template <class T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
208 T& |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
209 Array<T>::checkelem (octave_idx_type i, octave_idx_type j, octave_idx_type k) |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
210 { |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10636
diff
changeset
|
211 return elem (compute_index (i, j, k)); |
4513 | 212 } |
213 | |
214 template <class T> | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
215 T& |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
216 Array<T>::checkelem (const Array<octave_idx_type>& ra_idx) |
4513 | 217 { |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10636
diff
changeset
|
218 return elem (compute_index (ra_idx)); |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
219 } |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
220 |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
221 template <class T> |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
222 typename Array<T>::crefT |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
223 Array<T>::checkelem (octave_idx_type n) const |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
224 { |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10636
diff
changeset
|
225 // Do checks directly to avoid recomputing slice_len. |
10370
9c4daf174387
implement IDs for common liboctave exceptions
Jaroslav Hajek <highegg@gmail.com>
parents:
10366
diff
changeset
|
226 if (n < 0) |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
21098
diff
changeset
|
227 err_invalid_index (n); |
10370
9c4daf174387
implement IDs for common liboctave exceptions
Jaroslav Hajek <highegg@gmail.com>
parents:
10366
diff
changeset
|
228 if (n >= slice_len) |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
21098
diff
changeset
|
229 err_index_out_of_range (1, 1, n+1, slice_len, dimensions); |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
230 |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
231 return elem (n); |
4513 | 232 } |
233 | |
234 template <class T> | |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
235 typename Array<T>::crefT |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
236 Array<T>::checkelem (octave_idx_type i, octave_idx_type j) const |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
237 { |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10636
diff
changeset
|
238 return elem (compute_index (i, j)); |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
239 } |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
240 |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
241 template <class T> |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
242 typename Array<T>::crefT |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
243 Array<T>::checkelem (octave_idx_type i, octave_idx_type j, |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
244 octave_idx_type k) const |
4513 | 245 { |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10636
diff
changeset
|
246 return elem (compute_index (i, j, k)); |
4513 | 247 } |
248 | |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
249 template <class T> |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
250 typename Array<T>::crefT |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
251 Array<T>::checkelem (const Array<octave_idx_type>& ra_idx) const |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
252 { |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10636
diff
changeset
|
253 return elem (compute_index (ra_idx)); |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
254 } |
9731 | 255 |
256 template <class T> | |
257 Array<T> | |
258 Array<T>::column (octave_idx_type k) const | |
259 { | |
260 octave_idx_type r = dimensions(0); | |
21131
54527108599a
store more --enable-FEATURE info in octave_config_info
John W. Eaton <jwe@octave.org>
parents:
21120
diff
changeset
|
261 #ifdef ENABLE_BOUNDS_CHECK |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
262 if (k < 0 || k > dimensions.numel (1)) |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
21098
diff
changeset
|
263 err_index_out_of_range (2, 2, k+1, dimensions.numel (1), dimensions); |
9731 | 264 #endif |
265 | |
266 return Array<T> (*this, dim_vector (r, 1), k*r, k*r + r); | |
267 } | |
268 | |
4513 | 269 template <class T> |
4567 | 270 Array<T> |
9731 | 271 Array<T>::page (octave_idx_type k) const |
272 { | |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
273 octave_idx_type r = dimensions(0); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
274 octave_idx_type c = dimensions(1); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
275 octave_idx_type p = r*c; |
21131
54527108599a
store more --enable-FEATURE info in octave_config_info
John W. Eaton <jwe@octave.org>
parents:
21120
diff
changeset
|
276 #ifdef ENABLE_BOUNDS_CHECK |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
277 if (k < 0 || k > dimensions.numel (2)) |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
21098
diff
changeset
|
278 err_index_out_of_range (3, 3, k+1, dimensions.numel (2), dimensions); |
9731 | 279 #endif |
280 | |
281 return Array<T> (*this, dim_vector (r, c), k*p, k*p + p); | |
282 } | |
283 | |
284 template <class T> | |
285 Array<T> | |
286 Array<T>::linear_slice (octave_idx_type lo, octave_idx_type up) const | |
287 { | |
21131
54527108599a
store more --enable-FEATURE info in octave_config_info
John W. Eaton <jwe@octave.org>
parents:
21120
diff
changeset
|
288 #ifdef ENABLE_BOUNDS_CHECK |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
289 if (lo < 0) |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
21098
diff
changeset
|
290 err_index_out_of_range (1, 1, lo+1, numel (), dimensions); |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
291 if (up > numel ()) |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
21098
diff
changeset
|
292 err_index_out_of_range (1, 1, up, numel (), dimensions); |
9731 | 293 #endif |
294 if (up < lo) up = lo; | |
295 return Array<T> (*this, dim_vector (up - lo, 1), lo, up); | |
4567 | 296 } |
297 | |
8410 | 298 // Helper class for multi-d dimension permuting (generalized transpose). |
299 class rec_permute_helper | |
300 { | |
11507
c3ad80f4ce36
Array.h, Array.cc: more constructor fixes
John W. Eaton <jwe@octave.org>
parents:
11242
diff
changeset
|
301 // STRIDE occupies the last half of the space allocated for dim to |
c3ad80f4ce36
Array.h, Array.cc: more constructor fixes
John W. Eaton <jwe@octave.org>
parents:
11242
diff
changeset
|
302 // avoid a double allocation. |
c3ad80f4ce36
Array.h, Array.cc: more constructor fixes
John W. Eaton <jwe@octave.org>
parents:
11242
diff
changeset
|
303 |
c3ad80f4ce36
Array.h, Array.cc: more constructor fixes
John W. Eaton <jwe@octave.org>
parents:
11242
diff
changeset
|
304 int n; |
c3ad80f4ce36
Array.h, Array.cc: more constructor fixes
John W. Eaton <jwe@octave.org>
parents:
11242
diff
changeset
|
305 int top; |
c3ad80f4ce36
Array.h, Array.cc: more constructor fixes
John W. Eaton <jwe@octave.org>
parents:
11242
diff
changeset
|
306 octave_idx_type *dim; |
c3ad80f4ce36
Array.h, Array.cc: more constructor fixes
John W. Eaton <jwe@octave.org>
parents:
11242
diff
changeset
|
307 octave_idx_type *stride; |
9012
9f5e095555fc
smarter algorithm for permute
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
308 bool use_blk; |
8410 | 309 |
310 public: | |
311 rec_permute_helper (const dim_vector& dv, const Array<octave_idx_type>& perm) | |
11507
c3ad80f4ce36
Array.h, Array.cc: more constructor fixes
John W. Eaton <jwe@octave.org>
parents:
11242
diff
changeset
|
312 |
c3ad80f4ce36
Array.h, Array.cc: more constructor fixes
John W. Eaton <jwe@octave.org>
parents:
11242
diff
changeset
|
313 : n (dv.length ()), top (0), dim (new octave_idx_type [2*n]), |
c3ad80f4ce36
Array.h, Array.cc: more constructor fixes
John W. Eaton <jwe@octave.org>
parents:
11242
diff
changeset
|
314 stride (dim + n), use_blk (false) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
315 { |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20228
diff
changeset
|
316 assert (n == perm.numel ()); |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
317 |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
318 // Get cumulative dimensions. |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
319 OCTAVE_LOCAL_BUFFER (octave_idx_type, cdim, n+1); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
320 cdim[0] = 1; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
321 for (int i = 1; i < n+1; i++) cdim[i] = cdim[i-1] * dv(i-1); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
322 |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
323 // Setup the permuted strides. |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
324 for (int k = 0; k < n; k++) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
325 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
326 int kk = perm(k); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
327 dim[k] = dv(kk); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
328 stride[k] = cdim[kk]; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
329 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
330 |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
331 // Reduce contiguous runs. |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
332 for (int k = 1; k < n; k++) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
333 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
334 if (stride[k] == stride[top]*dim[top]) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
335 dim[top] *= dim[k]; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
336 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
337 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
338 top++; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
339 dim[top] = dim[k]; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
340 stride[top] = stride[k]; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
341 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
342 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
343 |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
344 // Determine whether we can use block transposes. |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
345 use_blk = top >= 1 && stride[1] == 1 && stride[0] == dim[1]; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
346 |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
347 } |
8410 | 348 |
349 ~rec_permute_helper (void) { delete [] dim; } | |
350 | |
9012
9f5e095555fc
smarter algorithm for permute
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
351 // Helper method for fast blocked transpose. |
9f5e095555fc
smarter algorithm for permute
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
352 template <class T> |
9121
bb62bc406ea7
reuse fast blocked transpose implementation from rec_permute_helper in Array<T>::transpose
Jaroslav Hajek <highegg@gmail.com>
parents:
9100
diff
changeset
|
353 static T * |
bb62bc406ea7
reuse fast blocked transpose implementation from rec_permute_helper in Array<T>::transpose
Jaroslav Hajek <highegg@gmail.com>
parents:
9100
diff
changeset
|
354 blk_trans (const T *src, T *dest, octave_idx_type nr, octave_idx_type nc) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
355 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
356 static const octave_idx_type m = 8; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
357 OCTAVE_LOCAL_BUFFER (T, blk, m*m); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
358 for (octave_idx_type kr = 0; kr < nr; kr += m) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
359 for (octave_idx_type kc = 0; kc < nc; kc += m) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
360 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
361 octave_idx_type lr = std::min (m, nr - kr); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
362 octave_idx_type lc = std::min (m, nc - kc); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
363 if (lr == m && lc == m) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
364 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
365 const T *ss = src + kc * nr + kr; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
366 for (octave_idx_type j = 0; j < m; j++) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
367 for (octave_idx_type i = 0; i < m; i++) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
368 blk[j*m+i] = ss[j*nr + i]; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
369 T *dd = dest + kr * nc + kc; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
370 for (octave_idx_type j = 0; j < m; j++) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
371 for (octave_idx_type i = 0; i < m; i++) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
372 dd[j*nc+i] = blk[i*m+j]; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
373 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
374 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
375 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
376 const T *ss = src + kc * nr + kr; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
377 for (octave_idx_type j = 0; j < lc; j++) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
378 for (octave_idx_type i = 0; i < lr; i++) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
379 blk[j*m+i] = ss[j*nr + i]; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
380 T *dd = dest + kr * nc + kc; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
381 for (octave_idx_type j = 0; j < lr; j++) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
382 for (octave_idx_type i = 0; i < lc; i++) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
383 dd[j*nc+i] = blk[i*m+j]; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
384 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
385 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
386 |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
387 return dest + nr*nc; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
388 } |
12153
e0e50f48df37
Explicitly disallow copying in some classes
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
11590
diff
changeset
|
389 |
8410 | 390 private: |
391 | |
21014
b9d4c3615e89
maint: Capitalize D for dimension in messages and comments.
Rik <rik@octave.org>
parents:
20974
diff
changeset
|
392 // Recursive N-D generalized transpose |
8410 | 393 template <class T> |
394 T *do_permute (const T *src, T *dest, int lev) const | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
395 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
396 if (lev == 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
397 { |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
398 octave_idx_type step = stride[0]; |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
399 octave_idx_type len = dim[0]; |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
400 if (step == 1) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
401 { |
19381
af41e41ad28e
replace oct-mem.h inline indirections by standard function calls.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18146
diff
changeset
|
402 std::copy (src, src + len, dest); |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
403 dest += len; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
404 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
405 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
406 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
407 for (octave_idx_type i = 0, j = 0; i < len; i++, j += step) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
408 dest[i] = src[j]; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
409 |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
410 dest += len; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
411 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
412 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
413 else if (use_blk && lev == 1) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
414 dest = blk_trans (src, dest, dim[1], dim[0]); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
415 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
416 { |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
417 octave_idx_type step = stride[lev]; |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
418 octave_idx_type len = dim[lev]; |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
419 for (octave_idx_type i = 0, j = 0; i < len; i++, j+= step) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
420 dest = do_permute (src + i * step, dest, lev-1); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
421 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
422 |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
423 return dest; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
424 } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
425 |
12153
e0e50f48df37
Explicitly disallow copying in some classes
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
11590
diff
changeset
|
426 // No copying! |
e0e50f48df37
Explicitly disallow copying in some classes
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
11590
diff
changeset
|
427 |
e0e50f48df37
Explicitly disallow copying in some classes
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
11590
diff
changeset
|
428 rec_permute_helper (const rec_permute_helper&); |
e0e50f48df37
Explicitly disallow copying in some classes
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
11590
diff
changeset
|
429 |
e0e50f48df37
Explicitly disallow copying in some classes
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
11590
diff
changeset
|
430 rec_permute_helper& operator = (const rec_permute_helper&); |
e0e50f48df37
Explicitly disallow copying in some classes
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
11590
diff
changeset
|
431 |
8410 | 432 public: |
433 | |
434 template <class T> | |
435 void permute (const T *src, T *dest) const { do_permute (src, dest, top); } | |
436 }; | |
7241 | 437 |
5607 | 438 |
4567 | 439 template <class T> |
4593 | 440 Array<T> |
5607 | 441 Array<T>::permute (const Array<octave_idx_type>& perm_vec_arg, bool inv) const |
4593 | 442 { |
443 Array<T> retval; | |
444 | |
5607 | 445 Array<octave_idx_type> perm_vec = perm_vec_arg; |
446 | |
4593 | 447 dim_vector dv = dims (); |
448 | |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20228
diff
changeset
|
449 int perm_vec_len = perm_vec_arg.numel (); |
5148 | 450 |
451 if (perm_vec_len < dv.length ()) | |
452 (*current_liboctave_error_handler) | |
453 ("%s: invalid permutation vector", inv ? "ipermute" : "permute"); | |
454 | |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9341
diff
changeset
|
455 dim_vector dv_new = dim_vector::alloc (perm_vec_len); |
5148 | 456 |
457 // Append singleton dimensions as needed. | |
458 dv.resize (perm_vec_len, 1); | |
459 | |
4593 | 460 // Need this array to check for identical elements in permutation array. |
8410 | 461 OCTAVE_LOCAL_BUFFER_INIT (bool, checked, perm_vec_len, false); |
4593 | 462 |
9678
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9624
diff
changeset
|
463 bool identity = true; |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9624
diff
changeset
|
464 |
4593 | 465 // Find dimension vector of permuted array. |
5148 | 466 for (int i = 0; i < perm_vec_len; i++) |
4593 | 467 { |
5275 | 468 octave_idx_type perm_elt = perm_vec.elem (i); |
5148 | 469 if (perm_elt >= perm_vec_len || perm_elt < 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
470 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
471 (*current_liboctave_error_handler) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
472 ("%s: permutation vector contains an invalid element", |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
473 inv ? "ipermute" : "permute"); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
474 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
475 return retval; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
476 } |
4593 | 477 |
8410 | 478 if (checked[perm_elt]) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
479 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
480 (*current_liboctave_error_handler) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
481 ("%s: permutation vector cannot contain identical elements", |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
482 inv ? "ipermute" : "permute"); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
483 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
484 return retval; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
485 } |
4593 | 486 else |
9678
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9624
diff
changeset
|
487 { |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9624
diff
changeset
|
488 checked[perm_elt] = true; |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9624
diff
changeset
|
489 identity = identity && perm_elt == i; |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9624
diff
changeset
|
490 } |
4593 | 491 } |
492 | |
9678
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9624
diff
changeset
|
493 if (identity) |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9624
diff
changeset
|
494 return *this; |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9624
diff
changeset
|
495 |
5607 | 496 if (inv) |
497 { | |
8410 | 498 for (int i = 0; i < perm_vec_len; i++) |
499 perm_vec(perm_vec_arg(i)) = i; | |
5607 | 500 } |
501 | |
10270 | 502 for (int i = 0; i < perm_vec_len; i++) |
503 dv_new(i) = dv(perm_vec(i)); | |
504 | |
8410 | 505 retval = Array<T> (dv_new); |
4593 | 506 |
5940 | 507 if (numel () > 0) |
5607 | 508 { |
8410 | 509 rec_permute_helper rh (dv, perm_vec); |
510 rh.permute (data (), retval.fortran_vec ()); | |
4593 | 511 } |
512 | |
513 return retval; | |
514 } | |
515 | |
11507
c3ad80f4ce36
Array.h, Array.cc: more constructor fixes
John W. Eaton <jwe@octave.org>
parents:
11242
diff
changeset
|
516 // Helper class for multi-d index reduction and recursive |
c3ad80f4ce36
Array.h, Array.cc: more constructor fixes
John W. Eaton <jwe@octave.org>
parents:
11242
diff
changeset
|
517 // indexing/indexed assignment. Rationale: we could avoid recursion |
c3ad80f4ce36
Array.h, Array.cc: more constructor fixes
John W. Eaton <jwe@octave.org>
parents:
11242
diff
changeset
|
518 // using a state machine instead. However, using recursion is much |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
519 // more amenable to possible parallelization in the future. |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
520 // Also, the recursion solution is cleaner and more understandable. |
11507
c3ad80f4ce36
Array.h, Array.cc: more constructor fixes
John W. Eaton <jwe@octave.org>
parents:
11242
diff
changeset
|
521 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
522 class rec_index_helper |
4513 | 523 { |
11507
c3ad80f4ce36
Array.h, Array.cc: more constructor fixes
John W. Eaton <jwe@octave.org>
parents:
11242
diff
changeset
|
524 // CDIM occupies the last half of the space allocated for dim to |
c3ad80f4ce36
Array.h, Array.cc: more constructor fixes
John W. Eaton <jwe@octave.org>
parents:
11242
diff
changeset
|
525 // avoid a double allocation. |
c3ad80f4ce36
Array.h, Array.cc: more constructor fixes
John W. Eaton <jwe@octave.org>
parents:
11242
diff
changeset
|
526 |
c3ad80f4ce36
Array.h, Array.cc: more constructor fixes
John W. Eaton <jwe@octave.org>
parents:
11242
diff
changeset
|
527 int n; |
c3ad80f4ce36
Array.h, Array.cc: more constructor fixes
John W. Eaton <jwe@octave.org>
parents:
11242
diff
changeset
|
528 int top; |
c3ad80f4ce36
Array.h, Array.cc: more constructor fixes
John W. Eaton <jwe@octave.org>
parents:
11242
diff
changeset
|
529 octave_idx_type *dim; |
c3ad80f4ce36
Array.h, Array.cc: more constructor fixes
John W. Eaton <jwe@octave.org>
parents:
11242
diff
changeset
|
530 octave_idx_type *cdim; |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
531 idx_vector *idx; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
532 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
533 public: |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
534 rec_index_helper (const dim_vector& dv, const Array<idx_vector>& ia) |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20228
diff
changeset
|
535 : n (ia.numel ()), top (0), dim (new octave_idx_type [2*n]), |
11507
c3ad80f4ce36
Array.h, Array.cc: more constructor fixes
John W. Eaton <jwe@octave.org>
parents:
11242
diff
changeset
|
536 cdim (dim + n), idx (new idx_vector [n]) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
537 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
538 assert (n > 0 && (dv.length () == std::max (n, 2))); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
539 |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
540 dim[0] = dv(0); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
541 cdim[0] = 1; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
542 idx[0] = ia(0); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
543 |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
544 for (int i = 1; i < n; i++) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
545 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
546 // Try reduction... |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
547 if (idx[top].maybe_reduce (dim[top], ia(i), dv(i))) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
548 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
549 // Reduction successful, fold dimensions. |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
550 dim[top] *= dv(i); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
551 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
552 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
553 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
554 // Unsuccessful, store index & cumulative dim. |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
555 top++; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
556 idx[top] = ia(i); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
557 dim[top] = dv(i); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
558 cdim[top] = cdim[top-1] * dim[top-1]; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
559 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
560 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
561 } |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
562 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
563 ~rec_index_helper (void) { delete [] idx; delete [] dim; } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
564 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
565 private: |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
566 |
21014
b9d4c3615e89
maint: Capitalize D for dimension in messages and comments.
Rik <rik@octave.org>
parents:
20974
diff
changeset
|
567 // Recursive N-D indexing |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
568 template <class T> |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
569 T *do_index (const T *src, T *dest, int lev) const |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
570 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
571 if (lev == 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
572 dest += idx[0].index (src, dim[0], dest); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
573 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
574 { |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
575 octave_idx_type nn = idx[lev].length (dim[lev]); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
576 octave_idx_type d = cdim[lev]; |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
577 for (octave_idx_type i = 0; i < nn; i++) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
578 dest = do_index (src + d*idx[lev].xelem (i), dest, lev-1); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
579 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
580 |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
581 return dest; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
582 } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
583 |
21014
b9d4c3615e89
maint: Capitalize D for dimension in messages and comments.
Rik <rik@octave.org>
parents:
20974
diff
changeset
|
584 // Recursive N-D indexed assignment |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
585 template <class T> |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
586 const T *do_assign (const T *src, T *dest, int lev) const |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
587 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
588 if (lev == 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
589 src += idx[0].assign (src, dim[0], dest); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
590 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
591 { |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
592 octave_idx_type nn = idx[lev].length (dim[lev]); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
593 octave_idx_type d = cdim[lev]; |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
594 for (octave_idx_type i = 0; i < nn; i++) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
595 src = do_assign (src, dest + d*idx[lev].xelem (i), lev-1); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
596 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
597 |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
598 return src; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
599 } |
4513 | 600 |
21014
b9d4c3615e89
maint: Capitalize D for dimension in messages and comments.
Rik <rik@octave.org>
parents:
20974
diff
changeset
|
601 // Recursive N-D indexed assignment |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
602 template <class T> |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
603 void do_fill (const T& val, T *dest, int lev) const |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
604 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
605 if (lev == 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
606 idx[0].fill (val, dim[0], dest); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
607 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
608 { |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
609 octave_idx_type nn = idx[lev].length (dim[lev]); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
610 octave_idx_type d = cdim[lev]; |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
611 for (octave_idx_type i = 0; i < nn; i++) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
612 do_fill (val, dest + d*idx[lev].xelem (i), lev-1); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
613 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
614 } |
4513 | 615 |
12153
e0e50f48df37
Explicitly disallow copying in some classes
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
11590
diff
changeset
|
616 // No copying! |
e0e50f48df37
Explicitly disallow copying in some classes
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
11590
diff
changeset
|
617 |
e0e50f48df37
Explicitly disallow copying in some classes
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
11590
diff
changeset
|
618 rec_index_helper (const rec_index_helper&); |
e0e50f48df37
Explicitly disallow copying in some classes
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
11590
diff
changeset
|
619 |
e0e50f48df37
Explicitly disallow copying in some classes
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
11590
diff
changeset
|
620 rec_index_helper& operator = (const rec_index_helper&); |
e0e50f48df37
Explicitly disallow copying in some classes
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
11590
diff
changeset
|
621 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
622 public: |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
623 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
624 template <class T> |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
625 void index (const T *src, T *dest) const { do_index (src, dest, top); } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
626 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
627 template <class T> |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
628 void assign (const T *src, T *dest) const { do_assign (src, dest, top); } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
629 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
630 template <class T> |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
631 void fill (const T& val, T *dest) const { do_fill (val, dest, top); } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
632 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
633 bool is_cont_range (octave_idx_type& l, |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
634 octave_idx_type& u) const |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
635 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
636 return top == 0 && idx[0].is_cont_range (dim[0], l, u); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
637 } |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
638 }; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
639 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
640 // Helper class for multi-d recursive resizing |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
641 // This handles resize () in an efficient manner, touching memory only |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
642 // once (apart from reinitialization) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
643 class rec_resize_helper |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
644 { |
11507
c3ad80f4ce36
Array.h, Array.cc: more constructor fixes
John W. Eaton <jwe@octave.org>
parents:
11242
diff
changeset
|
645 octave_idx_type *cext; |
c3ad80f4ce36
Array.h, Array.cc: more constructor fixes
John W. Eaton <jwe@octave.org>
parents:
11242
diff
changeset
|
646 octave_idx_type *sext; |
c3ad80f4ce36
Array.h, Array.cc: more constructor fixes
John W. Eaton <jwe@octave.org>
parents:
11242
diff
changeset
|
647 octave_idx_type *dext; |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
648 int n; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
649 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
650 public: |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
651 rec_resize_helper (const dim_vector& ndv, const dim_vector& odv) |
11507
c3ad80f4ce36
Array.h, Array.cc: more constructor fixes
John W. Eaton <jwe@octave.org>
parents:
11242
diff
changeset
|
652 : cext (0), sext (0), dext (0), n (0) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
653 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
654 int l = ndv.length (); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
655 assert (odv.length () == l); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
656 octave_idx_type ld = 1; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
657 int i = 0; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
658 for (; i < l-1 && ndv(i) == odv(i); i++) ld *= ndv(i); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
659 n = l - i; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
660 cext = new octave_idx_type [3*n]; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
661 // Trick to avoid three allocations |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
662 sext = cext + n; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
663 dext = sext + n; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
664 |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
665 octave_idx_type sld = ld; |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
666 octave_idx_type dld = ld; |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
667 for (int j = 0; j < n; j++) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
668 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
669 cext[j] = std::min (ndv(i+j), odv(i+j)); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
670 sext[j] = sld *= odv(i+j); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
671 dext[j] = dld *= ndv(i+j); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
672 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
673 cext[0] *= ld; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
674 } |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
675 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
676 ~rec_resize_helper (void) { delete [] cext; } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
677 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
678 private: |
12153
e0e50f48df37
Explicitly disallow copying in some classes
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
11590
diff
changeset
|
679 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
680 // recursive resizing |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
681 template <class T> |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
682 void do_resize_fill (const T* src, T *dest, const T& rfv, int lev) const |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
683 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
684 if (lev == 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
685 { |
19381
af41e41ad28e
replace oct-mem.h inline indirections by standard function calls.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18146
diff
changeset
|
686 std::copy (src, src+cext[0], dest); |
af41e41ad28e
replace oct-mem.h inline indirections by standard function calls.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18146
diff
changeset
|
687 std::fill_n (dest + cext[0], dext[0] - cext[0], rfv); |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
688 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
689 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
690 { |
18099
6c706a83070f
Tweak cset 8e056300994b defining 1 var per line in liboctave.
Rik <rik@octave.org>
parents:
18084
diff
changeset
|
691 octave_idx_type sd, dd, k; |
6c706a83070f
Tweak cset 8e056300994b defining 1 var per line in liboctave.
Rik <rik@octave.org>
parents:
18084
diff
changeset
|
692 sd = sext[lev-1]; |
6c706a83070f
Tweak cset 8e056300994b defining 1 var per line in liboctave.
Rik <rik@octave.org>
parents:
18084
diff
changeset
|
693 dd = dext[lev-1]; |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
694 for (k = 0; k < cext[lev]; k++) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
695 do_resize_fill (src + k * sd, dest + k * dd, rfv, lev - 1); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
696 |
19381
af41e41ad28e
replace oct-mem.h inline indirections by standard function calls.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18146
diff
changeset
|
697 std::fill_n (dest + k * dd, dext[lev] - k * dd, rfv); |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
698 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
699 } |
12153
e0e50f48df37
Explicitly disallow copying in some classes
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
11590
diff
changeset
|
700 |
e0e50f48df37
Explicitly disallow copying in some classes
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
11590
diff
changeset
|
701 // No copying! |
e0e50f48df37
Explicitly disallow copying in some classes
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
11590
diff
changeset
|
702 |
e0e50f48df37
Explicitly disallow copying in some classes
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
11590
diff
changeset
|
703 rec_resize_helper (const rec_resize_helper&); |
e0e50f48df37
Explicitly disallow copying in some classes
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
11590
diff
changeset
|
704 |
e0e50f48df37
Explicitly disallow copying in some classes
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
11590
diff
changeset
|
705 rec_resize_helper& operator = (const rec_resize_helper&); |
e0e50f48df37
Explicitly disallow copying in some classes
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
11590
diff
changeset
|
706 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
707 public: |
12153
e0e50f48df37
Explicitly disallow copying in some classes
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
11590
diff
changeset
|
708 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
709 template <class T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
710 void resize_fill (const T* src, T *dest, const T& rfv) const |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
711 { do_resize_fill (src, dest, rfv, n-1); } |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
712 }; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
713 |
4513 | 714 template <class T> |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
715 Array<T> |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
716 Array<T>::index (const idx_vector& i) const |
4513 | 717 { |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
718 octave_idx_type n = numel (); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
719 Array<T> retval; |
5275 | 720 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
721 if (i.is_colon ()) |
4513 | 722 { |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
723 // A(:) produces a shallow copy as a column vector. |
8523
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
724 retval = Array<T> (*this, dim_vector (n, 1)); |
4513 | 725 } |
4548 | 726 else |
4513 | 727 { |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
728 if (i.extent (n) != n) |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
21098
diff
changeset
|
729 err_index_out_of_range (1, 1, i.extent (n), n, dimensions); // throws |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
730 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
731 // FIXME: this is the only place where orig_dimensions are used. |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
732 dim_vector rd = i.orig_dimensions (); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
733 octave_idx_type il = i.length (n); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
734 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
735 // FIXME: this is for Matlab compatibility. Matlab 2007 given |
8333 | 736 // |
15018
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14846
diff
changeset
|
737 // b = ones (3,1) |
8333 | 738 // |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
739 // yields the following: |
8333 | 740 // |
15018
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14846
diff
changeset
|
741 // b(zeros (0,0)) gives [] |
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14846
diff
changeset
|
742 // b(zeros (1,0)) gives zeros (0,1) |
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14846
diff
changeset
|
743 // b(zeros (0,1)) gives zeros (0,1) |
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14846
diff
changeset
|
744 // b(zeros (0,m)) gives zeros (0,m) |
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14846
diff
changeset
|
745 // b(zeros (m,0)) gives zeros (m,0) |
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14846
diff
changeset
|
746 // b(1:2) gives ones (2,1) |
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14846
diff
changeset
|
747 // b(ones (2)) gives ones (2) etc. |
8333 | 748 // |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
749 // As you can see, the behaviour is weird, but the tests end up pretty |
8333 | 750 // simple. Nah, I don't want to suggest that this is ad hoc :) |
751 | |
9987
bb30843c4929
fix subscripting of vectors by N-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9921
diff
changeset
|
752 if (ndims () == 2 && n != 1 && rd.is_vector ()) |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
753 { |
9987
bb30843c4929
fix subscripting of vectors by N-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9921
diff
changeset
|
754 if (columns () == 1) |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
755 rd = dim_vector (il, 1); |
9987
bb30843c4929
fix subscripting of vectors by N-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9921
diff
changeset
|
756 else if (rows () == 1) |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
757 rd = dim_vector (1, il); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
758 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
759 |
8523
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
760 octave_idx_type l, u; |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
761 if (il != 0 && i.is_cont_range (n, l, u)) |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
762 // If suitable, produce a shallow slice. |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
763 retval = Array<T> (*this, rd, l, u); |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
764 else |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
765 { |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
766 // Don't use resize here to avoid useless initialization for POD |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
767 // types. |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
768 retval = Array<T> (rd); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
769 |
8523
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
770 if (il != 0) |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
771 i.index (data (), n, retval.fortran_vec ()); |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
772 } |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
773 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
774 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
775 return retval; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
776 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
777 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
778 template <class T> |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
779 Array<T> |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
780 Array<T>::index (const idx_vector& i, const idx_vector& j) const |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
781 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
782 // Get dimensions, allowing Fortran indexing in the 2nd dim. |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
783 dim_vector dv = dimensions.redim (2); |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
784 octave_idx_type r = dv(0); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
785 octave_idx_type c = dv(1); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
786 Array<T> retval; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
787 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
788 if (i.is_colon () && j.is_colon ()) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
789 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
790 // A(:,:) produces a shallow copy. |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
791 retval = Array<T> (*this, dv); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
792 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
793 else |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
794 { |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
795 if (i.extent (r) != r) |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
21098
diff
changeset
|
796 err_index_out_of_range (2, 1, i.extent (r), r, dimensions); // throws |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
797 if (j.extent (c) != c) |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
21098
diff
changeset
|
798 err_index_out_of_range (2, 2, j.extent (c), c, dimensions); // throws |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
799 |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
800 octave_idx_type n = numel (); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
801 octave_idx_type il = i.length (r); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
802 octave_idx_type jl = j.length (c); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
803 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
804 idx_vector ii (i); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
805 |
8523
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
806 if (ii.maybe_reduce (r, j, c)) |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
807 { |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
808 octave_idx_type l, u; |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
809 if (ii.length () > 0 && ii.is_cont_range (n, l, u)) |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
810 // If suitable, produce a shallow slice. |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
811 retval = Array<T> (*this, dim_vector (il, jl), l, u); |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
812 else |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
813 { |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
814 // Don't use resize to avoid useless initialization for POD types. |
8523
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
815 retval = Array<T> (dim_vector (il, jl)); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
816 |
8523
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
817 ii.index (data (), n, retval.fortran_vec ()); |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
818 } |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
819 } |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
820 else |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
821 { |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
822 // Don't use resize to avoid useless initialization for POD types. |
8523
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
823 retval = Array<T> (dim_vector (il, jl)); |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
824 |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
825 const T* src = data (); |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
826 T *dest = retval.fortran_vec (); |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
827 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
828 for (octave_idx_type k = 0; k < jl; k++) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
829 dest += i.index (src + r * j.xelem (k), r, dest); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
830 } |
4513 | 831 } |
832 | |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
833 return retval; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
834 } |
4747 | 835 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
836 template <class T> |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
837 Array<T> |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
838 Array<T>::index (const Array<idx_vector>& ia) const |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
839 { |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20228
diff
changeset
|
840 int ial = ia.numel (); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
841 Array<T> retval; |
4587 | 842 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
843 // FIXME: is this dispatching necessary? |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
844 if (ial == 1) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
845 retval = index (ia(0)); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
846 else if (ial == 2) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
847 retval = index (ia(0), ia(1)); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
848 else if (ial > 0) |
4513 | 849 { |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
850 // Get dimensions, allowing Fortran indexing in the last dim. |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
851 dim_vector dv = dimensions.redim (ial); |
4747 | 852 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
853 // Check for out of bounds conditions. |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
854 bool all_colons = true; |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
855 for (int i = 0; i < ial; i++) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
856 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
857 if (ia(i).extent (dv(i)) != dv(i)) |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
21098
diff
changeset
|
858 err_index_out_of_range (ial, i+1, ia(i).extent (dv(i)), dv(i), dimensions); // throws |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
859 |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
860 all_colons = all_colons && ia(i).is_colon (); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
861 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
862 |
4870 | 863 |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
864 if (all_colons) |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
865 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
866 // A(:,:,...,:) produces a shallow copy. |
10095
eb8ac0eed9f1
always chop dimension vector when constructing Arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
10075
diff
changeset
|
867 dv.chop_trailing_singletons (); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
868 retval = Array<T> (*this, dv); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
869 } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
870 else |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
871 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
872 // Form result dimensions. |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9341
diff
changeset
|
873 dim_vector rdv = dim_vector::alloc (ial); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
874 for (int i = 0; i < ial; i++) rdv(i) = ia(i).length (dv(i)); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
875 rdv.chop_trailing_singletons (); |
4870 | 876 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
877 // Prepare for recursive indexing |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
878 rec_index_helper rh (dv, ia); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
879 |
8523
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
880 octave_idx_type l, u; |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
881 if (rh.is_cont_range (l, u)) |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
882 // If suitable, produce a shallow slice. |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
883 retval = Array<T> (*this, rdv, l, u); |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
884 else |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
885 { |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
886 // Don't use resize to avoid useless initialization for POD types. |
8523
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
887 retval = Array<T> (rdv); |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
888 |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
889 // Do it. |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
890 rh.index (data (), retval.fortran_vec ()); |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
891 } |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
892 } |
4513 | 893 } |
894 | |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
895 return retval; |
4513 | 896 } |
897 | |
8333 | 898 // The default fill value. Override if you want a different one. |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
899 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
900 template <class T> |
14616
13cc11418393
improve handling of default resize fill value for arrays
John W. Eaton <jwe@octave.org>
parents:
14591
diff
changeset
|
901 T |
13cc11418393
improve handling of default resize fill value for arrays
John W. Eaton <jwe@octave.org>
parents:
14591
diff
changeset
|
902 Array<T>::resize_fill_value (void) const |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
903 { |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
904 static T zero = T (); |
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
905 return zero; |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
906 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
907 |
8333 | 908 // Yes, we could do resize using index & assign. However, that would |
909 // possibly involve a lot more memory traffic than we actually need. | |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
910 |
4513 | 911 template <class T> |
912 void | |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
913 Array<T>::resize1 (octave_idx_type n, const T& rfv) |
4513 | 914 { |
21118
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
915 if (n < 0 || ndims () != 2) |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
916 err_invalid_resize (); |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
917 |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
918 dim_vector dv; |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
919 // This is driven by Matlab's behaviour of giving a *row* vector |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
920 // on some out-of-bounds assignments. Specifically, Matlab |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
921 // allows a(i) with out-of-bouds i when a is either of 0x0, 1x0, |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
922 // 1x1, 0xN, and gives a row vector in all cases (yes, even the |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
923 // last one, search me why). Giving a column vector would make |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
924 // much more sense (given the way trailing singleton dims are |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
925 // treated). |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
926 bool invalid = false; |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
927 if (rows () == 0 || rows () == 1) |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
928 dv = dim_vector (1, n); |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
929 else if (columns () == 1) |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
930 dv = dim_vector (n, 1); |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
931 else |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
932 invalid = true; |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
933 |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
934 if (invalid) |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
935 err_invalid_resize (); |
21120
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
936 |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
937 octave_idx_type nx = numel (); |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
938 if (n == nx - 1 && n > 0) |
4513 | 939 { |
21120
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
940 // Stack "pop" operation. |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
941 if (rep->count == 1) |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
942 slice_data[slice_len-1] = T (); |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
943 slice_len--; |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
944 dimensions = dv; |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
945 } |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
946 else if (n == nx + 1 && nx > 0) |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
947 { |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
948 // Stack "push" operation. |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
949 if (rep->count == 1 |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
950 && slice_data + slice_len < rep->data + rep->len) |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
951 { |
21120
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
952 slice_data[slice_len++] = rfv; |
21118
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
953 dimensions = dv; |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
954 } |
21120
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
955 else |
21118
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
956 { |
21120
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
957 static const octave_idx_type max_stack_chunk = 1024; |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
958 octave_idx_type nn = n + std::min (nx, max_stack_chunk); |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
959 Array<T> tmp (Array<T> (dim_vector (nn, 1)), dv, 0, n); |
21118
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
960 T *dest = tmp.fortran_vec (); |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
961 |
21120
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
962 std::copy (data (), data () + nx, dest); |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
963 dest[nx] = rfv; |
21118
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
964 |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
965 *this = tmp; |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
966 } |
4513 | 967 } |
21120
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
968 else if (n != nx) |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
969 { |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
970 Array<T> tmp = Array<T> (dv); |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
971 T *dest = tmp.fortran_vec (); |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
972 |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
973 octave_idx_type n0 = std::min (n, nx); |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
974 octave_idx_type n1 = n - n0; |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
975 std::copy (data (), data () + n0, dest); |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
976 std::fill_n (dest + n0, n1, rfv); |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
977 |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
978 *this = tmp; |
499b851fbfae
Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents:
21118
diff
changeset
|
979 } |
4513 | 980 } |
981 | |
982 template <class T> | |
983 void | |
11574
a83bad07f7e3
attempt better backward compatibility for Array resize functions
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
984 Array<T>::resize2 (octave_idx_type r, octave_idx_type c, const T& rfv) |
4513 | 985 { |
21118
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
986 if (r < 0 || c < 0 || ndims () != 2) |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
987 err_invalid_resize (); |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
988 |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
989 octave_idx_type rx = rows (); |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
990 octave_idx_type cx = columns (); |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
991 if (r != rx || c != cx) |
4513 | 992 { |
21118
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
993 Array<T> tmp = Array<T> (dim_vector (r, c)); |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
994 T *dest = tmp.fortran_vec (); |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
995 |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
996 octave_idx_type r0 = std::min (r, rx); |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
997 octave_idx_type r1 = r - r0; |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
998 octave_idx_type c0 = std::min (c, cx); |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
999 octave_idx_type c1 = c - c0; |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1000 const T *src = data (); |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1001 if (r == rx) |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1002 { |
21118
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1003 std::copy (src, src + r * c0, dest); |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1004 dest += r * c0; |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1005 } |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1006 else |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1007 { |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1008 for (octave_idx_type k = 0; k < c0; k++) |
9773
01f897d8a130
optimize memory manipulation by arrays & indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
9732
diff
changeset
|
1009 { |
21118
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1010 std::copy (src, src + r0, dest); |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1011 src += rx; |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1012 dest += r0; |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1013 std::fill_n (dest, r1, rfv); |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1014 dest += r1; |
9773
01f897d8a130
optimize memory manipulation by arrays & indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
9732
diff
changeset
|
1015 } |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1016 } |
21118
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1017 |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1018 std::fill_n (dest, r * c1, rfv); |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1019 |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1020 *this = tmp; |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1021 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1022 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1023 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1024 template<class T> |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1025 void |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
1026 Array<T>::resize (const dim_vector& dv, const T& rfv) |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1027 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1028 int dvl = dv.length (); |
8784
374cb30311a8
remove dead branch in Array.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
8760
diff
changeset
|
1029 if (dvl == 2) |
11574
a83bad07f7e3
attempt better backward compatibility for Array resize functions
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
1030 resize2 (dv(0), dv(1), rfv); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1031 else if (dimensions != dv) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1032 { |
21118
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1033 if (dimensions.length () > dvl || dv.any_neg ()) |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
21098
diff
changeset
|
1034 err_invalid_resize (); |
21118
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1035 |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1036 Array<T> tmp (dv); |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1037 // Prepare for recursive resizing. |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1038 rec_resize_helper rh (dv, dimensions.redim (dvl)); |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1039 |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1040 // Do it. |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1041 rh.resize_fill (data (), tmp.fortran_vec (), rfv); |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1042 *this = tmp; |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1043 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1044 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1045 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1046 template <class T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1047 Array<T> |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1048 Array<T>::index (const idx_vector& i, bool resize_ok, const T& rfv) const |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1049 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1050 Array<T> tmp = *this; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1051 if (resize_ok) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1052 { |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
1053 octave_idx_type n = numel (); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
1054 octave_idx_type nx = i.extent (n); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1055 if (n != nx) |
8563
3a3421a9f0bb
optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
1056 { |
3a3421a9f0bb
optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
1057 if (i.is_scalar ()) |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
1058 return Array<T> (dim_vector (1, 1), rfv); |
8563
3a3421a9f0bb
optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
1059 else |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
1060 tmp.resize1 (nx, rfv); |
8563
3a3421a9f0bb
optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
1061 } |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1062 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1063 if (tmp.numel () != nx) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1064 return Array<T> (); |
4513 | 1065 } |
1066 | |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1067 return tmp.index (i); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1068 } |
4513 | 1069 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1070 template <class T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1071 Array<T> |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1072 Array<T>::index (const idx_vector& i, const idx_vector& j, |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1073 bool resize_ok, const T& rfv) const |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1074 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1075 Array<T> tmp = *this; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1076 if (resize_ok) |
4513 | 1077 { |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1078 dim_vector dv = dimensions.redim (2); |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
1079 octave_idx_type r = dv(0); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
1080 octave_idx_type c = dv(1); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
1081 octave_idx_type rx = i.extent (r); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
1082 octave_idx_type cx = j.extent (c); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1083 if (r != rx || c != cx) |
8563
3a3421a9f0bb
optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
1084 { |
3a3421a9f0bb
optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
1085 if (i.is_scalar () && j.is_scalar ()) |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
1086 return Array<T> (dim_vector (1, 1), rfv); |
8563
3a3421a9f0bb
optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
1087 else |
11574
a83bad07f7e3
attempt better backward compatibility for Array resize functions
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
1088 tmp.resize2 (rx, cx, rfv); |
8563
3a3421a9f0bb
optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
1089 } |
4747 | 1090 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1091 if (tmp.rows () != rx || tmp.columns () != cx) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1092 return Array<T> (); |
4513 | 1093 } |
1094 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1095 return tmp.index (i, j); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1096 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1097 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1098 template <class T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1099 Array<T> |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1100 Array<T>::index (const Array<idx_vector>& ia, |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1101 bool resize_ok, const T& rfv) const |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1102 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1103 Array<T> tmp = *this; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1104 if (resize_ok) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1105 { |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20228
diff
changeset
|
1106 int ial = ia.numel (); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1107 dim_vector dv = dimensions.redim (ial); |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9341
diff
changeset
|
1108 dim_vector dvx = dim_vector::alloc (ial); |
21098
99d373870017
Fix assigning to trailing singletons for structs and cell (bug #39789, bug #35841)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
21014
diff
changeset
|
1109 for (int i = 0; i < ial; i++) |
99d373870017
Fix assigning to trailing singletons for structs and cell (bug #39789, bug #35841)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
21014
diff
changeset
|
1110 dvx(i) = ia(i).extent (dv(i)); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1111 if (! (dvx == dv)) |
8563
3a3421a9f0bb
optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
1112 { |
3a3421a9f0bb
optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
1113 bool all_scalars = true; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1114 for (int i = 0; i < ial; i++) |
8563
3a3421a9f0bb
optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
1115 all_scalars = all_scalars && ia(i).is_scalar (); |
3a3421a9f0bb
optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
1116 if (all_scalars) |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
1117 return Array<T> (dim_vector (1, 1), rfv); |
8563
3a3421a9f0bb
optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
1118 else |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
1119 tmp.resize (dvx, rfv); |
21098
99d373870017
Fix assigning to trailing singletons for structs and cell (bug #39789, bug #35841)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
21014
diff
changeset
|
1120 |
99d373870017
Fix assigning to trailing singletons for structs and cell (bug #39789, bug #35841)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
21014
diff
changeset
|
1121 if (tmp.dimensions != dvx) |
99d373870017
Fix assigning to trailing singletons for structs and cell (bug #39789, bug #35841)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
21014
diff
changeset
|
1122 return Array<T> (); |
8563
3a3421a9f0bb
optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
1123 } |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1124 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1125 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1126 return tmp.index (ia); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1127 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1128 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1129 |
4513 | 1130 template <class T> |
1131 void | |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1132 Array<T>::assign (const idx_vector& i, const Array<T>& rhs, const T& rfv) |
4513 | 1133 { |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
1134 octave_idx_type n = numel (); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
1135 octave_idx_type rhl = rhs.numel (); |
4548 | 1136 |
21118
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1137 if (rhl != 1 && i.length (n) != rhl) |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1138 err_nonconformant ("=", dim_vector(i.length(n),1), rhs.dims()); |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1139 |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1140 octave_idx_type nx = i.extent (n); |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1141 bool colon = i.is_colon_equiv (nx); |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1142 // Try to resize first if necessary. |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1143 if (nx != n) |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1144 { |
21118
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1145 // Optimize case A = []; A(1:n) = X with A empty. |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1146 if (dimensions.zero_by_zero () && colon) |
9058
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1147 { |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1148 if (rhl == 1) |
21118
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1149 *this = Array<T> (dim_vector (1, nx), rhs(0)); |
9058
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1150 else |
21118
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1151 *this = Array<T> (rhs, dim_vector (1, nx)); |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1152 return; |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1153 } |
21118
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1154 |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1155 resize1 (nx, rfv); |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1156 n = numel (); |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1157 } |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1158 |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1159 if (colon) |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1160 { |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1161 // A(:) = X makes a full fill or a shallow copy. |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1162 if (rhl == 1) |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1163 fill (rhs(0)); |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1164 else |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1165 *this = rhs.reshape (dimensions); |
4513 | 1166 } |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1167 else |
21118
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1168 { |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1169 if (rhl == 1) |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1170 i.fill (rhs(0), n, fortran_vec ()); |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1171 else |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1172 i.assign (rhs.data (), n, fortran_vec ()); |
3ac9f47fb04b
Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1173 } |
4513 | 1174 } |
1175 | |
20390
41d19a6ef55a
Allow assignment of an empty variable to an empty indexing slice (bug #45467).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
1176 // Assignment to a 2-dimensional array |
4513 | 1177 template <class T> |
1178 void | |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1179 Array<T>::assign (const idx_vector& i, const idx_vector& j, |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1180 const Array<T>& rhs, const T& rfv) |
4513 | 1181 { |
14624
edf9ca8a92a8
when redimensioning, always pad dim_vector objects with 1 (bug #33216)
John W. Eaton <jwe@octave.org>
parents:
14440
diff
changeset
|
1182 bool initial_dims_all_zero = dimensions.all_zero (); |
edf9ca8a92a8
when redimensioning, always pad dim_vector objects with 1 (bug #33216)
John W. Eaton <jwe@octave.org>
parents:
14440
diff
changeset
|
1183 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1184 // Get RHS extents, discarding singletons. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1185 dim_vector rhdv = rhs.dims (); |
14624
edf9ca8a92a8
when redimensioning, always pad dim_vector objects with 1 (bug #33216)
John W. Eaton <jwe@octave.org>
parents:
14440
diff
changeset
|
1186 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1187 // Get LHS extents, allowing Fortran indexing in the second dim. |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1188 dim_vector dv = dimensions.redim (2); |
14624
edf9ca8a92a8
when redimensioning, always pad dim_vector objects with 1 (bug #33216)
John W. Eaton <jwe@octave.org>
parents:
14440
diff
changeset
|
1189 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1190 // Check for out-of-bounds and form resizing dimensions. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1191 dim_vector rdv; |
14624
edf9ca8a92a8
when redimensioning, always pad dim_vector objects with 1 (bug #33216)
John W. Eaton <jwe@octave.org>
parents:
14440
diff
changeset
|
1192 |
8333 | 1193 // In the special when all dimensions are zero, colons are allowed |
1194 // to inquire the shape of RHS. The rules are more obscure, so we | |
1195 // solve that elsewhere. | |
14624
edf9ca8a92a8
when redimensioning, always pad dim_vector objects with 1 (bug #33216)
John W. Eaton <jwe@octave.org>
parents:
14440
diff
changeset
|
1196 if (initial_dims_all_zero) |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1197 rdv = zero_dims_inquire (i, j, rhdv); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1198 else |
4513 | 1199 { |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1200 rdv(0) = i.extent (dv(0)); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1201 rdv(1) = j.extent (dv(1)); |
4513 | 1202 } |
1203 | |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1204 bool isfill = rhs.numel () == 1; |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
1205 octave_idx_type il = i.length (rdv(0)); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
1206 octave_idx_type jl = j.length (rdv(1)); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1207 rhdv.chop_all_singletons (); |
8333 | 1208 bool match = (isfill |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1209 || (rhdv.length () == 2 && il == rhdv(0) && jl == rhdv(1))); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1210 match = match || (il == 1 && jl == rhdv(0) && rhdv(1) == 1); |
4548 | 1211 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1212 if (match) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1213 { |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1214 bool all_colons = (i.is_colon_equiv (rdv(0)) |
9886
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9878
diff
changeset
|
1215 && j.is_colon_equiv (rdv(1))); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1216 // Resize if requested. |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1217 if (rdv != dv) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1218 { |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8563
diff
changeset
|
1219 // Optimize case A = []; A(1:m, 1:n) = X |
9886
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9878
diff
changeset
|
1220 if (dv.zero_by_zero () && all_colons) |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8563
diff
changeset
|
1221 { |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8563
diff
changeset
|
1222 if (isfill) |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8563
diff
changeset
|
1223 *this = Array<T> (rdv, rhs(0)); |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8563
diff
changeset
|
1224 else |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8563
diff
changeset
|
1225 *this = Array<T> (rhs, rdv); |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8563
diff
changeset
|
1226 return; |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8563
diff
changeset
|
1227 } |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8563
diff
changeset
|
1228 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1229 resize (rdv, rfv); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1230 dv = dimensions; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1231 } |
4513 | 1232 |
9886
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9878
diff
changeset
|
1233 if (all_colons) |
9058
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1234 { |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1235 // A(:,:) = X makes a full fill or a shallow copy |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1236 if (isfill) |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1237 fill (rhs(0)); |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1238 else |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1239 *this = rhs.reshape (dimensions); |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1240 } |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1241 else |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1242 { |
9058
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1243 // The actual work. |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
1244 octave_idx_type n = numel (); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
1245 octave_idx_type r = dv(0); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
1246 octave_idx_type c = dv(1); |
9058
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1247 idx_vector ii (i); |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1248 |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1249 const T* src = rhs.data (); |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1250 T *dest = fortran_vec (); |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1251 |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1252 // Try reduction first. |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1253 if (ii.maybe_reduce (r, j, c)) |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1254 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1255 if (isfill) |
9058
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1256 ii.fill (*src, n, dest); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1257 else |
9058
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1258 ii.assign (src, n, dest); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1259 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1260 else |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1261 { |
9058
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1262 if (isfill) |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1263 { |
9058
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1264 for (octave_idx_type k = 0; k < jl; k++) |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1265 i.fill (*src, r, dest + r * j.xelem (k)); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1266 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1267 else |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1268 { |
9058
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1269 for (octave_idx_type k = 0; k < jl; k++) |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1270 src += i.assign (src, r, dest + r * j.xelem (k)); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1271 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1272 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1273 } |
4747 | 1274 } |
20390
41d19a6ef55a
Allow assignment of an empty variable to an empty indexing slice (bug #45467).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
1275 // any empty RHS can be assigned to an empty LHS |
41d19a6ef55a
Allow assignment of an empty variable to an empty indexing slice (bug #45467).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
1276 else if ((il != 0 && jl != 0) || (rhdv(0) != 0 && rhdv(1) != 0)) |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
21098
diff
changeset
|
1277 err_nonconformant ("=", il, jl, rhs.dim1 (), rhs.dim2 ()); |
4513 | 1278 } |
1279 | |
20390
41d19a6ef55a
Allow assignment of an empty variable to an empty indexing slice (bug #45467).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
1280 // Assignment to a multi-dimensional array |
4513 | 1281 template <class T> |
1282 void | |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1283 Array<T>::assign (const Array<idx_vector>& ia, |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1284 const Array<T>& rhs, const T& rfv) |
4513 | 1285 { |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20228
diff
changeset
|
1286 int ial = ia.numel (); |
5275 | 1287 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1288 // FIXME: is this dispatching necessary / desirable? |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1289 if (ial == 1) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1290 assign (ia(0), rhs, rfv); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1291 else if (ial == 2) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1292 assign (ia(0), ia(1), rhs, rfv); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1293 else if (ial > 0) |
4513 | 1294 { |
14624
edf9ca8a92a8
when redimensioning, always pad dim_vector objects with 1 (bug #33216)
John W. Eaton <jwe@octave.org>
parents:
14440
diff
changeset
|
1295 bool initial_dims_all_zero = dimensions.all_zero (); |
edf9ca8a92a8
when redimensioning, always pad dim_vector objects with 1 (bug #33216)
John W. Eaton <jwe@octave.org>
parents:
14440
diff
changeset
|
1296 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1297 // Get RHS extents, discarding singletons. |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1298 dim_vector rhdv = rhs.dims (); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1299 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1300 // Get LHS extents, allowing Fortran indexing in the second dim. |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1301 dim_vector dv = dimensions.redim (ial); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1302 |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1303 // Get the extents forced by indexing. |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1304 dim_vector rdv; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1305 |
8333 | 1306 // In the special when all dimensions are zero, colons are |
1307 // allowed to inquire the shape of RHS. The rules are more | |
1308 // obscure, so we solve that elsewhere. | |
14624
edf9ca8a92a8
when redimensioning, always pad dim_vector objects with 1 (bug #33216)
John W. Eaton <jwe@octave.org>
parents:
14440
diff
changeset
|
1309 if (initial_dims_all_zero) |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1310 rdv = zero_dims_inquire (ia, rhdv); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1311 else |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1312 { |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9341
diff
changeset
|
1313 rdv = dim_vector::alloc (ial); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1314 for (int i = 0; i < ial; i++) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1315 rdv(i) = ia(i).extent (dv(i)); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1316 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1317 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1318 // Check whether LHS and RHS match, up to singleton dims. |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
1319 bool match = true; |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
1320 bool all_colons = true; |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
1321 bool isfill = rhs.numel () == 1; |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1322 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1323 rhdv.chop_all_singletons (); |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
1324 int j = 0; |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
1325 int rhdvl = rhdv.length (); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1326 for (int i = 0; i < ial; i++) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1327 { |
9886
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9878
diff
changeset
|
1328 all_colons = all_colons && ia(i).is_colon_equiv (rdv(i)); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1329 octave_idx_type l = ia(i).length (rdv(i)); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1330 if (l == 1) continue; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1331 match = match && j < rhdvl && l == rhdv(j++); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1332 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1333 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1334 match = match && (j == rhdvl || rhdv(j) == 1); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1335 match = match || isfill; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1336 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1337 if (match) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1338 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1339 // Resize first if necessary. |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1340 if (rdv != dv) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1341 { |
9886
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9878
diff
changeset
|
1342 // Optimize case A = []; A(1:m, 1:n) = X |
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9878
diff
changeset
|
1343 if (dv.zero_by_zero () && all_colons) |
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9878
diff
changeset
|
1344 { |
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9878
diff
changeset
|
1345 rdv.chop_trailing_singletons (); |
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9878
diff
changeset
|
1346 if (isfill) |
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9878
diff
changeset
|
1347 *this = Array<T> (rdv, rhs(0)); |
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9878
diff
changeset
|
1348 else |
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9878
diff
changeset
|
1349 *this = Array<T> (rhs, rdv); |
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9878
diff
changeset
|
1350 return; |
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9878
diff
changeset
|
1351 } |
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9878
diff
changeset
|
1352 |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
1353 resize (rdv, rfv); |
10095
eb8ac0eed9f1
always chop dimension vector when constructing Arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
10075
diff
changeset
|
1354 dv = rdv; |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1355 } |
4513 | 1356 |
9058
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1357 if (all_colons) |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1358 { |
9058
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1359 // A(:,:,...,:) = X makes a full fill or a shallow copy. |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1360 if (isfill) |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1361 fill (rhs(0)); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1362 else |
9058
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1363 *this = rhs.reshape (dimensions); |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1364 } |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1365 else |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1366 { |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1367 // Do the actual work. |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1368 |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1369 // Prepare for recursive indexing |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1370 rec_index_helper rh (dv, ia); |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1371 |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1372 // Do it. |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1373 if (isfill) |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1374 rh.fill (rhs(0), fortran_vec ()); |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1375 else |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1376 rh.assign (rhs.data (), fortran_vec ()); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1377 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1378 } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1379 else |
20390
41d19a6ef55a
Allow assignment of an empty variable to an empty indexing slice (bug #45467).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
1380 { |
41d19a6ef55a
Allow assignment of an empty variable to an empty indexing slice (bug #45467).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
1381 // dimension mismatch, unless LHS and RHS both empty |
41d19a6ef55a
Allow assignment of an empty variable to an empty indexing slice (bug #45467).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
1382 bool lhsempty, rhsempty; |
41d19a6ef55a
Allow assignment of an empty variable to an empty indexing slice (bug #45467).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
1383 lhsempty = rhsempty = false; |
41d19a6ef55a
Allow assignment of an empty variable to an empty indexing slice (bug #45467).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
1384 for (int i = 0; i < ial; i++) |
41d19a6ef55a
Allow assignment of an empty variable to an empty indexing slice (bug #45467).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
1385 { |
41d19a6ef55a
Allow assignment of an empty variable to an empty indexing slice (bug #45467).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
1386 octave_idx_type l = ia(i).length (rdv(i)); |
41d19a6ef55a
Allow assignment of an empty variable to an empty indexing slice (bug #45467).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
1387 lhsempty = lhsempty || (l == 0); |
41d19a6ef55a
Allow assignment of an empty variable to an empty indexing slice (bug #45467).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
1388 rhsempty = rhsempty || (rhdv(j++) == 0); |
41d19a6ef55a
Allow assignment of an empty variable to an empty indexing slice (bug #45467).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
1389 } |
41d19a6ef55a
Allow assignment of an empty variable to an empty indexing slice (bug #45467).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
1390 if (! lhsempty || ! rhsempty) |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
21098
diff
changeset
|
1391 err_nonconformant ("=", dv, rhdv); |
20390
41d19a6ef55a
Allow assignment of an empty variable to an empty indexing slice (bug #45467).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
1392 } |
4553 | 1393 } |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1394 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1395 |
20542
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20390
diff
changeset
|
1396 /* |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20390
diff
changeset
|
1397 %!shared a |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20390
diff
changeset
|
1398 %! a = [1 2; 3 4]; |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20390
diff
changeset
|
1399 %!error <op1 is 1x2, op2 is 1x3> a(1,:) = [1 2 3] |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20390
diff
changeset
|
1400 %!error <op1 is 2x1, op2 is 1x3> a(:,1) = [1 2 3] |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20390
diff
changeset
|
1401 %!error <op1 is 2x2, op2 is 2x1> a(1:2,2:3) = [1;2] |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20390
diff
changeset
|
1402 */ |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20390
diff
changeset
|
1403 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1404 template <class T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1405 void |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1406 Array<T>::delete_elements (const idx_vector& i) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1407 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1408 octave_idx_type n = numel (); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1409 if (i.is_colon ()) |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1410 { |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1411 *this = Array<T> (); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1412 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1413 else if (i.length (n) != 0) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1414 { |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
1415 if (i.extent (n) != n) |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
21098
diff
changeset
|
1416 err_del_index_out_of_range (true, i.extent (n), n); |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
1417 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1418 octave_idx_type l, u; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1419 bool col_vec = ndims () == 2 && columns () == 1 && rows () != 1; |
10610
f5f6bde82e19
fix null assignment bug #29785
Jaroslav Hajek <highegg@gmail.com>
parents:
10535
diff
changeset
|
1420 if (i.is_scalar () && i(0) == n-1 && dimensions.is_vector ()) |
9100
1a8bbfb2f7cf
optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9058
diff
changeset
|
1421 { |
1a8bbfb2f7cf
optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9058
diff
changeset
|
1422 // Stack "pop" operation. |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
1423 resize1 (n-1); |
9100
1a8bbfb2f7cf
optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9058
diff
changeset
|
1424 } |
1a8bbfb2f7cf
optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9058
diff
changeset
|
1425 else if (i.is_cont_range (n, l, u)) |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1426 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1427 // Special case deleting a contiguous range. |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1428 octave_idx_type m = n + l - u; |
20955
77f5591878bf
maint: Use '! expr' rather than '!expr' to conform to coding guidelines.
Rik <rik@octave.org>
parents:
20542
diff
changeset
|
1429 Array<T> tmp (dim_vector (col_vec ? m : 1, ! col_vec ? m : 1)); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1430 const T *src = data (); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1431 T *dest = tmp.fortran_vec (); |
19381
af41e41ad28e
replace oct-mem.h inline indirections by standard function calls.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18146
diff
changeset
|
1432 std::copy (src, src + l, dest); |
af41e41ad28e
replace oct-mem.h inline indirections by standard function calls.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18146
diff
changeset
|
1433 std::copy (src + u, src + n, dest + l); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1434 *this = tmp; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1435 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1436 else |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1437 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1438 // Use index. |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1439 *this = index (i.complement (n)); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1440 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1441 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1442 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1443 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1444 template <class T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1445 void |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1446 Array<T>::delete_elements (int dim, const idx_vector& i) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1447 { |
8302
f2e050b62199
fix dim check in Array<T>::delete_elements
Jaroslav Hajek <highegg@gmail.com>
parents:
8290
diff
changeset
|
1448 if (dim < 0 || dim >= ndims ()) |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1449 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1450 (*current_liboctave_error_handler) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1451 ("invalid dimension in delete_elements"); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1452 return; |
4513 | 1453 } |
1454 | |
20218
b2100e1659ac
maint: Use cuddled parentheses when indexing dimension_vectors.
Rik <rik@octave.org>
parents:
19861
diff
changeset
|
1455 octave_idx_type n = dimensions(dim); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1456 if (i.is_colon ()) |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1457 { |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1458 *this = Array<T> (); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1459 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1460 else if (i.length (n) != 0) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1461 { |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
1462 if (i.extent (n) != n) |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
21098
diff
changeset
|
1463 err_del_index_out_of_range (false, i.extent (n), n); |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
1464 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1465 octave_idx_type l, u; |
4513 | 1466 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1467 if (i.is_cont_range (n, l, u)) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1468 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1469 // Special case deleting a contiguous range. |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
1470 octave_idx_type nd = n + l - u; |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
1471 octave_idx_type dl = 1; |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
1472 octave_idx_type du = 1; |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1473 dim_vector rdv = dimensions; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1474 rdv(dim) = nd; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1475 for (int k = 0; k < dim; k++) dl *= dimensions(k); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1476 for (int k = dim + 1; k < ndims (); k++) du *= dimensions(k); |
4513 | 1477 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1478 // Special case deleting a contiguous range. |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1479 Array<T> tmp = Array<T> (rdv); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1480 const T *src = data (); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1481 T *dest = tmp.fortran_vec (); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1482 l *= dl; u *= dl; n *= dl; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1483 for (octave_idx_type k = 0; k < du; k++) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1484 { |
19381
af41e41ad28e
replace oct-mem.h inline indirections by standard function calls.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18146
diff
changeset
|
1485 std::copy (src, src + l, dest); |
9773
01f897d8a130
optimize memory manipulation by arrays & indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
9732
diff
changeset
|
1486 dest += l; |
19381
af41e41ad28e
replace oct-mem.h inline indirections by standard function calls.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18146
diff
changeset
|
1487 std::copy (src + u, src + n, dest); |
9773
01f897d8a130
optimize memory manipulation by arrays & indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
9732
diff
changeset
|
1488 dest += n - u; |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1489 src += n; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1490 } |
4870 | 1491 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1492 *this = tmp; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1493 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1494 else |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1495 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1496 // Use index. |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
1497 Array<idx_vector> ia (dim_vector (ndims (), 1), idx_vector::colon); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1498 ia (dim) = i.complement (n); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1499 *this = index (ia); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1500 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1501 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1502 } |
4747 | 1503 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1504 template <class T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1505 void |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1506 Array<T>::delete_elements (const Array<idx_vector>& ia) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1507 { |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20228
diff
changeset
|
1508 int ial = ia.numel (); |
17642
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1509 |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1510 if (ial == 1) |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1511 delete_elements (ia(0)); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1512 else |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1513 { |
17642
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1514 int k, dim = -1; |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1515 for (k = 0; k < ial; k++) |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1516 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1517 if (! ia(k).is_colon ()) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1518 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1519 if (dim < 0) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1520 dim = k; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1521 else |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1522 break; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1523 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1524 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1525 if (dim < 0) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1526 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1527 dim_vector dv = dimensions; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1528 dv(0) = 0; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1529 *this = Array<T> (dv); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1530 } |
17642
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1531 else if (k == ial) |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1532 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1533 delete_elements (dim, ia(dim)); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1534 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1535 else |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1536 { |
17642
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1537 // Allow the null assignment to succeed if it won't change |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1538 // anything because the indices reference an empty slice, |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1539 // provided that there is at most one non-colon (or |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1540 // equivalent) index. So, we still have the requirement of |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1541 // deleting a slice, but it is OK if the slice is empty. |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1542 |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1543 // For compatibility with Matlab, stop checking once we see |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1544 // more than one non-colon index or an empty index. Matlab |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1545 // considers "[]" to be an empty index but not "false". We |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1546 // accept both. |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1547 |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1548 bool empty_assignment = false; |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1549 |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1550 int num_non_colon_indices = 0; |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1551 |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1552 int nd = ndims (); |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1553 |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1554 for (int i = 0; i < ial; i++) |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1555 { |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1556 octave_idx_type dim_len = i >= nd ? 1 : dimensions(i); |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1557 |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1558 if (ia(i).length (dim_len) == 0) |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1559 { |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1560 empty_assignment = true; |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1561 break; |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1562 } |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1563 |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1564 if (! ia(i).is_colon_equiv (dim_len)) |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1565 { |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1566 num_non_colon_indices++; |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1567 |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1568 if (num_non_colon_indices == 2) |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1569 break; |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1570 } |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1571 } |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1572 |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1573 if (! empty_assignment) |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1574 (*current_liboctave_error_handler) |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1575 ("a null assignment can only have one non-colon index"); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1576 } |
4513 | 1577 } |
1578 | |
1579 } | |
1580 | |
1581 template <class T> | |
1582 Array<T>& | |
5275 | 1583 Array<T>::insert (const Array<T>& a, octave_idx_type r, octave_idx_type c) |
4513 | 1584 { |
10115
ed49cef7e005
simplify Array::insert methods
Jaroslav Hajek <highegg@gmail.com>
parents:
10113
diff
changeset
|
1585 idx_vector i (r, r + a.rows ()); |
ed49cef7e005
simplify Array::insert methods
Jaroslav Hajek <highegg@gmail.com>
parents:
10113
diff
changeset
|
1586 idx_vector j (c, c + a.columns ()); |
4786 | 1587 if (ndims () == 2 && a.ndims () == 2) |
10115
ed49cef7e005
simplify Array::insert methods
Jaroslav Hajek <highegg@gmail.com>
parents:
10113
diff
changeset
|
1588 assign (i, j, a); |
4786 | 1589 else |
4513 | 1590 { |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
1591 Array<idx_vector> idx (dim_vector (a.ndims (), 1)); |
10115
ed49cef7e005
simplify Array::insert methods
Jaroslav Hajek <highegg@gmail.com>
parents:
10113
diff
changeset
|
1592 idx(0) = i; |
ed49cef7e005
simplify Array::insert methods
Jaroslav Hajek <highegg@gmail.com>
parents:
10113
diff
changeset
|
1593 idx(1) = j; |
12884
73e75ff9c31b
Fix incorrect loop (bug #32683)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12550
diff
changeset
|
1594 for (int k = 2; k < a.ndims (); k++) |
10115
ed49cef7e005
simplify Array::insert methods
Jaroslav Hajek <highegg@gmail.com>
parents:
10113
diff
changeset
|
1595 idx(k) = idx_vector (0, a.dimensions(k)); |
ed49cef7e005
simplify Array::insert methods
Jaroslav Hajek <highegg@gmail.com>
parents:
10113
diff
changeset
|
1596 assign (idx, a); |
4513 | 1597 } |
1598 | |
1599 return *this; | |
1600 } | |
1601 | |
1602 template <class T> | |
1603 Array<T>& | |
5275 | 1604 Array<T>::insert (const Array<T>& a, const Array<octave_idx_type>& ra_idx) |
4513 | 1605 { |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20228
diff
changeset
|
1606 octave_idx_type n = ra_idx.numel (); |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
1607 Array<idx_vector> idx (dim_vector (n, 1)); |
10115
ed49cef7e005
simplify Array::insert methods
Jaroslav Hajek <highegg@gmail.com>
parents:
10113
diff
changeset
|
1608 const dim_vector dva = a.dims ().redim (n); |
ed49cef7e005
simplify Array::insert methods
Jaroslav Hajek <highegg@gmail.com>
parents:
10113
diff
changeset
|
1609 for (octave_idx_type k = 0; k < n; k++) |
20218
b2100e1659ac
maint: Use cuddled parentheses when indexing dimension_vectors.
Rik <rik@octave.org>
parents:
19861
diff
changeset
|
1610 idx(k) = idx_vector (ra_idx(k), ra_idx(k) + dva(k)); |
10115
ed49cef7e005
simplify Array::insert methods
Jaroslav Hajek <highegg@gmail.com>
parents:
10113
diff
changeset
|
1611 |
ed49cef7e005
simplify Array::insert methods
Jaroslav Hajek <highegg@gmail.com>
parents:
10113
diff
changeset
|
1612 assign (idx, a); |
4513 | 1613 |
1614 return *this; | |
1615 } | |
1616 | |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1617 |
4513 | 1618 template <class T> |
1619 Array<T> | |
1620 Array<T>::transpose (void) const | |
1621 { | |
4548 | 1622 assert (ndims () == 2); |
1623 | |
5275 | 1624 octave_idx_type nr = dim1 (); |
1625 octave_idx_type nc = dim2 (); | |
4513 | 1626 |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1627 if (nr >= 8 && nc >= 8) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1628 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1629 Array<T> result (dim_vector (nc, nr)); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1630 |
9121
bb62bc406ea7
reuse fast blocked transpose implementation from rec_permute_helper in Array<T>::transpose
Jaroslav Hajek <highegg@gmail.com>
parents:
9100
diff
changeset
|
1631 // Reuse the implementation used for permuting. |
bb62bc406ea7
reuse fast blocked transpose implementation from rec_permute_helper in Array<T>::transpose
Jaroslav Hajek <highegg@gmail.com>
parents:
9100
diff
changeset
|
1632 |
bb62bc406ea7
reuse fast blocked transpose implementation from rec_permute_helper in Array<T>::transpose
Jaroslav Hajek <highegg@gmail.com>
parents:
9100
diff
changeset
|
1633 rec_permute_helper::blk_trans (data (), result.fortran_vec (), nr, nc); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1634 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1635 return result; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1636 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1637 else if (nr > 1 && nc > 1) |
4513 | 1638 { |
1639 Array<T> result (dim_vector (nc, nr)); | |
1640 | |
5275 | 1641 for (octave_idx_type j = 0; j < nc; j++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1642 for (octave_idx_type i = 0; i < nr; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1643 result.xelem (j, i) = xelem (i, j); |
4513 | 1644 |
1645 return result; | |
1646 } | |
1647 else | |
1648 { | |
8333 | 1649 // Fast transpose for vectors and empty matrices. |
4513 | 1650 return Array<T> (*this, dim_vector (nc, nr)); |
1651 } | |
1652 } | |
1653 | |
1654 template <class T> | |
8028
f0fbf47c914c
avoid null pointer call in Array<T>::hermitian
Jaroslav Hajek <highegg@gmail.com>
parents:
7796
diff
changeset
|
1655 static T |
f0fbf47c914c
avoid null pointer call in Array<T>::hermitian
Jaroslav Hajek <highegg@gmail.com>
parents:
7796
diff
changeset
|
1656 no_op_fcn (const T& x) |
f0fbf47c914c
avoid null pointer call in Array<T>::hermitian
Jaroslav Hajek <highegg@gmail.com>
parents:
7796
diff
changeset
|
1657 { |
f0fbf47c914c
avoid null pointer call in Array<T>::hermitian
Jaroslav Hajek <highegg@gmail.com>
parents:
7796
diff
changeset
|
1658 return x; |
f0fbf47c914c
avoid null pointer call in Array<T>::hermitian
Jaroslav Hajek <highegg@gmail.com>
parents:
7796
diff
changeset
|
1659 } |
f0fbf47c914c
avoid null pointer call in Array<T>::hermitian
Jaroslav Hajek <highegg@gmail.com>
parents:
7796
diff
changeset
|
1660 |
f0fbf47c914c
avoid null pointer call in Array<T>::hermitian
Jaroslav Hajek <highegg@gmail.com>
parents:
7796
diff
changeset
|
1661 template <class T> |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1662 Array<T> |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1663 Array<T>::hermitian (T (*fcn) (const T&)) const |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1664 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1665 assert (ndims () == 2); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1666 |
8028
f0fbf47c914c
avoid null pointer call in Array<T>::hermitian
Jaroslav Hajek <highegg@gmail.com>
parents:
7796
diff
changeset
|
1667 if (! fcn) |
f0fbf47c914c
avoid null pointer call in Array<T>::hermitian
Jaroslav Hajek <highegg@gmail.com>
parents:
7796
diff
changeset
|
1668 fcn = no_op_fcn<T>; |
f0fbf47c914c
avoid null pointer call in Array<T>::hermitian
Jaroslav Hajek <highegg@gmail.com>
parents:
7796
diff
changeset
|
1669 |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1670 octave_idx_type nr = dim1 (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1671 octave_idx_type nc = dim2 (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1672 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1673 if (nr >= 8 && nc >= 8) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1674 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1675 Array<T> result (dim_vector (nc, nr)); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1676 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1677 // Blocked transpose to attempt to avoid cache misses. |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1678 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1679 // Don't use OCTAVE_LOCAL_BUFFER here as it doesn't work with bool |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1680 // on some compilers. |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1681 T buf[64]; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1682 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1683 octave_idx_type ii = 0, jj; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1684 for (jj = 0; jj < (nc - 8 + 1); jj += 8) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1685 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1686 for (ii = 0; ii < (nr - 8 + 1); ii += 8) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1687 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1688 // Copy to buffer |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1689 for (octave_idx_type j = jj, k = 0, idxj = jj * nr; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1690 j < jj + 8; j++, idxj += nr) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1691 for (octave_idx_type i = ii; i < ii + 8; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1692 buf[k++] = xelem (i + idxj); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1693 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1694 // Copy from buffer |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1695 for (octave_idx_type i = ii, idxi = ii * nc; i < ii + 8; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1696 i++, idxi += nc) |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1697 for (octave_idx_type j = jj, k = i - ii; j < jj + 8; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1698 j++, k+=8) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1699 result.xelem (j + idxi) = fcn (buf[k]); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1700 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1701 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1702 if (ii < nr) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1703 for (octave_idx_type j = jj; j < jj + 8; j++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1704 for (octave_idx_type i = ii; i < nr; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1705 result.xelem (j, i) = fcn (xelem (i, j)); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1706 } |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1707 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1708 for (octave_idx_type j = jj; j < nc; j++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1709 for (octave_idx_type i = 0; i < nr; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1710 result.xelem (j, i) = fcn (xelem (i, j)); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1711 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1712 return result; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1713 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1714 else |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1715 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1716 Array<T> result (dim_vector (nc, nr)); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1717 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1718 for (octave_idx_type j = 0; j < nc; j++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1719 for (octave_idx_type i = 0; i < nr; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1720 result.xelem (j, i) = fcn (xelem (i, j)); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1721 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1722 return result; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1723 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1724 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1725 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1726 /* |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1727 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1728 %% Tranpose tests for matrices of the tile size and plus or minus a row |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1729 %% and with four tiles. |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1730 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1731 %!shared m7, mt7, m8, mt8, m9, mt9 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1732 %! m7 = reshape (1 : 7*8, 8, 7); |
7796
762801c50b21
Fix tests for transpose in Array.cc
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
1733 %! mt7 = [1:8; 9:16; 17:24; 25:32; 33:40; 41:48; 49:56]; |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1734 %! m8 = reshape (1 : 8*8, 8, 8); |
7796
762801c50b21
Fix tests for transpose in Array.cc
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
1735 %! mt8 = mt8 = [mt7; 57:64]; |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1736 %! m9 = reshape (1 : 9*8, 8, 9); |
7796
762801c50b21
Fix tests for transpose in Array.cc
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
1737 %! mt9 = [mt8; 65:72]; |
762801c50b21
Fix tests for transpose in Array.cc
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
1738 |
14427
d07e989686b0
Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14158
diff
changeset
|
1739 %!assert (m7', mt7) |
d07e989686b0
Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14158
diff
changeset
|
1740 %!assert ((1i*m7).', 1i * mt7) |
d07e989686b0
Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14158
diff
changeset
|
1741 %!assert ((1i*m7)', conj (1i * mt7)) |
d07e989686b0
Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14158
diff
changeset
|
1742 %!assert (m8', mt8) |
d07e989686b0
Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14158
diff
changeset
|
1743 %!assert ((1i*m8).', 1i * mt8) |
d07e989686b0
Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14158
diff
changeset
|
1744 %!assert ((1i*m8)', conj (1i * mt8)) |
d07e989686b0
Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14158
diff
changeset
|
1745 %!assert (m9', mt9) |
d07e989686b0
Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14158
diff
changeset
|
1746 %!assert ((1i*m9).', 1i * mt9) |
d07e989686b0
Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14158
diff
changeset
|
1747 %!assert ((1i*m9)', conj (1i * mt9)) |
d07e989686b0
Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14158
diff
changeset
|
1748 %!assert ([m7, m8; m7, m8]', [mt7, mt7; mt8, mt8]) |
d07e989686b0
Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14158
diff
changeset
|
1749 %!assert ((1i*[m7, m8; m7, m8]).', 1i * [mt7, mt7; mt8, mt8]) |
d07e989686b0
Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14158
diff
changeset
|
1750 %!assert ((1i*[m7, m8; m7, m8])', conj (1i * [mt7, mt7; mt8, mt8])) |
d07e989686b0
Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14158
diff
changeset
|
1751 %!assert ([m8, m8; m8, m8]', [mt8, mt8; mt8, mt8]) |
d07e989686b0
Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14158
diff
changeset
|
1752 %!assert ((1i*[m8, m8; m8, m8]).', 1i * [mt8, mt8; mt8, mt8]) |
d07e989686b0
Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14158
diff
changeset
|
1753 %!assert ((1i*[m8, m8; m8, m8])', conj (1i * [mt8, mt8; mt8, mt8])) |
d07e989686b0
Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14158
diff
changeset
|
1754 %!assert ([m9, m8; m9, m8]', [mt9, mt9; mt8, mt8]) |
d07e989686b0
Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14158
diff
changeset
|
1755 %!assert ((1i*[m9, m8; m9, m8]).', 1i * [mt9, mt9; mt8, mt8]) |
d07e989686b0
Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14158
diff
changeset
|
1756 %!assert ((1i*[m9, m8; m9, m8])', conj (1i * [mt9, mt9; mt8, mt8])) |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1757 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1758 */ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1759 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1760 template <class T> |
4513 | 1761 T * |
1762 Array<T>::fortran_vec (void) | |
1763 { | |
6881 | 1764 make_unique (); |
1765 | |
8523
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
1766 return slice_data; |
4513 | 1767 } |
1768 | |
8700 | 1769 // Non-real types don't have NaNs. |
4517 | 1770 template <class T> |
8725 | 1771 inline bool |
1772 sort_isnan (typename ref_param<T>::type) | |
7433 | 1773 { |
8700 | 1774 return false; |
7433 | 1775 } |
1776 | |
1777 template <class T> | |
1778 Array<T> | |
9725 | 1779 Array<T>::sort (int dim, sortmode mode) const |
7433 | 1780 { |
10703
5eb420d92307
fix sort and nth_element when trailing singleton dim is specified
Jaroslav Hajek <highegg@gmail.com>
parents:
10681
diff
changeset
|
1781 if (dim < 0) |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
1782 { |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
1783 (*current_liboctave_error_handler) |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
1784 ("sort: invalid dimension"); |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
1785 return Array<T> (); |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
1786 } |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
1787 |
8651
ea8e65ca234f
reduce memory usage in sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
1788 Array<T> m (dims ()); |
7433 | 1789 |
1790 dim_vector dv = m.dims (); | |
1791 | |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20228
diff
changeset
|
1792 if (m.numel () < 1) |
7433 | 1793 return m; |
1794 | |
10703
5eb420d92307
fix sort and nth_element when trailing singleton dim is specified
Jaroslav Hajek <highegg@gmail.com>
parents:
10681
diff
changeset
|
1795 if (dim >= dv.length ()) |
5eb420d92307
fix sort and nth_element when trailing singleton dim is specified
Jaroslav Hajek <highegg@gmail.com>
parents:
10681
diff
changeset
|
1796 dv.resize (dim+1, 1); |
5eb420d92307
fix sort and nth_element when trailing singleton dim is specified
Jaroslav Hajek <highegg@gmail.com>
parents:
10681
diff
changeset
|
1797 |
7433 | 1798 octave_idx_type ns = dv(dim); |
1799 octave_idx_type iter = dv.numel () / ns; | |
1800 octave_idx_type stride = 1; | |
8505 | 1801 |
7433 | 1802 for (int i = 0; i < dim; i++) |
1803 stride *= dv(i); | |
1804 | |
1805 T *v = m.fortran_vec (); | |
8651
ea8e65ca234f
reduce memory usage in sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
1806 const T *ov = data (); |
ea8e65ca234f
reduce memory usage in sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
1807 |
7433 | 1808 octave_sort<T> lsort; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1809 |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1810 if (mode != UNSORTED) |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
1811 lsort.set_compare (mode); |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7443
diff
changeset
|
1812 else |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
1813 return m; |
7433 | 1814 |
1815 if (stride == 1) | |
1816 { | |
1817 for (octave_idx_type j = 0; j < iter; j++) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1818 { |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1819 // copy and partition out NaNs. |
8700 | 1820 // FIXME: impact on integer types noticeable? |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
1821 octave_idx_type kl = 0; |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
1822 octave_idx_type ku = ns; |
8700 | 1823 for (octave_idx_type i = 0; i < ns; i++) |
1824 { | |
1825 T tmp = ov[i]; | |
8725 | 1826 if (sort_isnan<T> (tmp)) |
8700 | 1827 v[--ku] = tmp; |
1828 else | |
1829 v[kl++] = tmp; | |
1830 } | |
1831 | |
1832 // sort. | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1833 lsort.sort (v, kl); |
8700 | 1834 |
1835 if (ku < ns) | |
1836 { | |
1837 // NaNs are in reverse order | |
1838 std::reverse (v + ku, v + ns); | |
1839 if (mode == DESCENDING) | |
1840 std::rotate (v, v + ku, v + ns); | |
1841 } | |
1842 | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1843 v += ns; |
8651
ea8e65ca234f
reduce memory usage in sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
1844 ov += ns; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1845 } |
7433 | 1846 } |
1847 else | |
1848 { | |
8700 | 1849 OCTAVE_LOCAL_BUFFER (T, buf, ns); |
7433 | 1850 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1851 for (octave_idx_type j = 0; j < iter; j++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1852 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1853 octave_idx_type offset = j; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1854 octave_idx_type offset2 = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1855 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1856 while (offset >= stride) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1857 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1858 offset -= stride; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1859 offset2++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1860 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1861 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1862 offset += offset2 * stride * ns; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1863 |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1864 // gather and partition out NaNs. |
8700 | 1865 // FIXME: impact on integer types noticeable? |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
1866 octave_idx_type kl = 0; |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
1867 octave_idx_type ku = ns; |
8700 | 1868 for (octave_idx_type i = 0; i < ns; i++) |
1869 { | |
1870 T tmp = ov[i*stride + offset]; | |
8725 | 1871 if (sort_isnan<T> (tmp)) |
8700 | 1872 buf[--ku] = tmp; |
1873 else | |
1874 buf[kl++] = tmp; | |
1875 } | |
7433 | 1876 |
8700 | 1877 // sort. |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1878 lsort.sort (buf, kl); |
8700 | 1879 |
1880 if (ku < ns) | |
1881 { | |
1882 // NaNs are in reverse order | |
1883 std::reverse (buf + ku, buf + ns); | |
1884 if (mode == DESCENDING) | |
1885 std::rotate (buf, buf + ku, buf + ns); | |
1886 } | |
1887 | |
1888 // scatter. | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1889 for (octave_idx_type i = 0; i < ns; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1890 v[i*stride + offset] = buf[i]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1891 } |
7433 | 1892 } |
1893 | |
1894 return m; | |
1895 } | |
1896 | |
1897 template <class T> | |
1898 Array<T> | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1899 Array<T>::sort (Array<octave_idx_type> &sidx, int dim, |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1900 sortmode mode) const |
7433 | 1901 { |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
1902 if (dim < 0 || dim >= ndims ()) |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
1903 { |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
1904 (*current_liboctave_error_handler) |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
1905 ("sort: invalid dimension"); |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
1906 return Array<T> (); |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
1907 } |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
1908 |
8651
ea8e65ca234f
reduce memory usage in sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
1909 Array<T> m (dims ()); |
7433 | 1910 |
1911 dim_vector dv = m.dims (); | |
1912 | |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20228
diff
changeset
|
1913 if (m.numel () < 1) |
7433 | 1914 { |
1915 sidx = Array<octave_idx_type> (dv); | |
1916 return m; | |
1917 } | |
1918 | |
1919 octave_idx_type ns = dv(dim); | |
1920 octave_idx_type iter = dv.numel () / ns; | |
1921 octave_idx_type stride = 1; | |
8505 | 1922 |
7433 | 1923 for (int i = 0; i < dim; i++) |
1924 stride *= dv(i); | |
1925 | |
1926 T *v = m.fortran_vec (); | |
8651
ea8e65ca234f
reduce memory usage in sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
1927 const T *ov = data (); |
ea8e65ca234f
reduce memory usage in sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
1928 |
8700 | 1929 octave_sort<T> lsort; |
7433 | 1930 |
8700 | 1931 sidx = Array<octave_idx_type> (dv); |
1932 octave_idx_type *vi = sidx.fortran_vec (); | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1933 |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1934 if (mode != UNSORTED) |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
1935 lsort.set_compare (mode); |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7443
diff
changeset
|
1936 else |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
1937 return m; |
7433 | 1938 |
1939 if (stride == 1) | |
1940 { | |
1941 for (octave_idx_type j = 0; j < iter; j++) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1942 { |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1943 // copy and partition out NaNs. |
8700 | 1944 // FIXME: impact on integer types noticeable? |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
1945 octave_idx_type kl = 0; |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
1946 octave_idx_type ku = ns; |
8700 | 1947 for (octave_idx_type i = 0; i < ns; i++) |
1948 { | |
1949 T tmp = ov[i]; | |
8725 | 1950 if (sort_isnan<T> (tmp)) |
8700 | 1951 { |
1952 --ku; | |
1953 v[ku] = tmp; | |
1954 vi[ku] = i; | |
1955 } | |
1956 else | |
1957 { | |
1958 v[kl] = tmp; | |
1959 vi[kl] = i; | |
1960 kl++; | |
1961 } | |
1962 } | |
7433 | 1963 |
8700 | 1964 // sort. |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1965 lsort.sort (v, vi, kl); |
8700 | 1966 |
1967 if (ku < ns) | |
1968 { | |
1969 // NaNs are in reverse order | |
1970 std::reverse (v + ku, v + ns); | |
1971 std::reverse (vi + ku, vi + ns); | |
1972 if (mode == DESCENDING) | |
1973 { | |
1974 std::rotate (v, v + ku, v + ns); | |
1975 std::rotate (vi, vi + ku, vi + ns); | |
1976 } | |
1977 } | |
1978 | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1979 v += ns; |
8700 | 1980 vi += ns; |
8651
ea8e65ca234f
reduce memory usage in sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
1981 ov += ns; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1982 } |
7433 | 1983 } |
1984 else | |
1985 { | |
8700 | 1986 OCTAVE_LOCAL_BUFFER (T, buf, ns); |
1987 OCTAVE_LOCAL_BUFFER (octave_idx_type, bufi, ns); | |
8651
ea8e65ca234f
reduce memory usage in sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
1988 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1989 for (octave_idx_type j = 0; j < iter; j++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1990 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1991 octave_idx_type offset = j; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1992 octave_idx_type offset2 = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1993 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1994 while (offset >= stride) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1995 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1996 offset -= stride; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1997 offset2++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1998 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1999 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2000 offset += offset2 * stride * ns; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
2001 |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
2002 // gather and partition out NaNs. |
8700 | 2003 // FIXME: impact on integer types noticeable? |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
2004 octave_idx_type kl = 0; |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
2005 octave_idx_type ku = ns; |
8700 | 2006 for (octave_idx_type i = 0; i < ns; i++) |
2007 { | |
2008 T tmp = ov[i*stride + offset]; | |
8725 | 2009 if (sort_isnan<T> (tmp)) |
8700 | 2010 { |
2011 --ku; | |
2012 buf[ku] = tmp; | |
2013 bufi[ku] = i; | |
2014 } | |
2015 else | |
2016 { | |
2017 buf[kl] = tmp; | |
2018 bufi[kl] = i; | |
2019 kl++; | |
2020 } | |
2021 } | |
7433 | 2022 |
8700 | 2023 // sort. |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2024 lsort.sort (buf, bufi, kl); |
8700 | 2025 |
2026 if (ku < ns) | |
2027 { | |
2028 // NaNs are in reverse order | |
2029 std::reverse (buf + ku, buf + ns); | |
2030 std::reverse (bufi + ku, bufi + ns); | |
2031 if (mode == DESCENDING) | |
2032 { | |
2033 std::rotate (buf, buf + ku, buf + ns); | |
2034 std::rotate (bufi, bufi + ku, bufi + ns); | |
2035 } | |
2036 } | |
2037 | |
2038 // scatter. | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2039 for (octave_idx_type i = 0; i < ns; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2040 v[i*stride + offset] = buf[i]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2041 for (octave_idx_type i = 0; i < ns; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2042 vi[i*stride + offset] = bufi[i]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2043 } |
7433 | 2044 } |
2045 | |
2046 return m; | |
2047 } | |
2048 | |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2049 template <class T> |
8725 | 2050 typename Array<T>::compare_fcn_type |
9920
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2051 safe_comparator (sortmode mode, const Array<T>& /* a */, |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2052 bool /* allow_chk */) |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2053 { |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2054 if (mode == ASCENDING) |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2055 return octave_sort<T>::ascending_compare; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2056 else if (mode == DESCENDING) |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2057 return octave_sort<T>::descending_compare; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2058 else |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2059 return 0; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2060 } |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2061 |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2062 template <class T> |
9920
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2063 sortmode |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2064 Array<T>::is_sorted (sortmode mode) const |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2065 { |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2066 octave_sort<T> lsort; |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2067 |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2068 octave_idx_type n = numel (); |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2069 |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2070 if (n <= 1) |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2071 return mode ? mode : ASCENDING; |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2072 |
11210
b79924abf776
Array.cc: use comparisons to sortmode values instead of testing mode directly
John W. Eaton <jwe@octave.org>
parents:
10716
diff
changeset
|
2073 if (mode == UNSORTED) |
9920
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2074 { |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2075 // Auto-detect mode. |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2076 compare_fcn_type compare |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2077 = safe_comparator (ASCENDING, *this, false); |
9920
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2078 |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2079 if (compare (elem (n-1), elem (0))) |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2080 mode = DESCENDING; |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2081 else |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2082 mode = ASCENDING; |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2083 } |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2084 |
11210
b79924abf776
Array.cc: use comparisons to sortmode values instead of testing mode directly
John W. Eaton <jwe@octave.org>
parents:
10716
diff
changeset
|
2085 if (mode != UNSORTED) |
9920
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2086 { |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2087 lsort.set_compare (safe_comparator (mode, *this, false)); |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2088 |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2089 if (! lsort.is_sorted (data (), n)) |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2090 mode = UNSORTED; |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2091 } |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2092 |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2093 return mode; |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2094 |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2095 } |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2096 |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2097 template <class T> |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2098 Array<octave_idx_type> |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2099 Array<T>::sort_rows_idx (sortmode mode) const |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2100 { |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2101 Array<octave_idx_type> idx; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2102 |
9921
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2103 octave_sort<T> lsort (safe_comparator (mode, *this, true)); |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2104 |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
2105 octave_idx_type r = rows (); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
2106 octave_idx_type c = cols (); |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2107 |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
2108 idx = Array<octave_idx_type> (dim_vector (r, 1)); |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2109 |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2110 lsort.sort_rows (data (), idx.fortran_vec (), r, c); |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2111 |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2112 return idx; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2113 } |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2114 |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2115 |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2116 template <class T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
2117 sortmode |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2118 Array<T>::is_sorted_rows (sortmode mode) const |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2119 { |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2120 octave_sort<T> lsort; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2121 |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
2122 octave_idx_type r = rows (); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
2123 octave_idx_type c = cols (); |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2124 |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2125 if (r <= 1 || c == 0) |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2126 return mode ? mode : ASCENDING; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2127 |
11210
b79924abf776
Array.cc: use comparisons to sortmode values instead of testing mode directly
John W. Eaton <jwe@octave.org>
parents:
10716
diff
changeset
|
2128 if (mode == UNSORTED) |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2129 { |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2130 // Auto-detect mode. |
8725 | 2131 compare_fcn_type compare |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2132 = safe_comparator (ASCENDING, *this, false); |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2133 |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2134 octave_idx_type i; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2135 for (i = 0; i < cols (); i++) |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2136 { |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
2137 T l = elem (0, i); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
2138 T u = elem (rows () - 1, i); |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2139 if (compare (l, u)) |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2140 { |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2141 if (mode == DESCENDING) |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2142 { |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2143 mode = UNSORTED; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2144 break; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2145 } |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2146 else |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2147 mode = ASCENDING; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2148 } |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2149 else if (compare (u, l)) |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2150 { |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2151 if (mode == ASCENDING) |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2152 { |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2153 mode = UNSORTED; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2154 break; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2155 } |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2156 else |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2157 mode = DESCENDING; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2158 } |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2159 } |
11210
b79924abf776
Array.cc: use comparisons to sortmode values instead of testing mode directly
John W. Eaton <jwe@octave.org>
parents:
10716
diff
changeset
|
2160 if (mode == UNSORTED && i == cols ()) |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2161 mode = ASCENDING; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2162 } |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2163 |
11210
b79924abf776
Array.cc: use comparisons to sortmode values instead of testing mode directly
John W. Eaton <jwe@octave.org>
parents:
10716
diff
changeset
|
2164 if (mode != UNSORTED) |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2165 { |
9920
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2166 lsort.set_compare (safe_comparator (mode, *this, false)); |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2167 |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2168 if (! lsort.is_sorted_rows (data (), r, c)) |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2169 mode = UNSORTED; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2170 } |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2171 |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2172 return mode; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2173 |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2174 } |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2175 |
8814
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2176 // Do a binary lookup in a sorted array. |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2177 template <class T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
2178 octave_idx_type |
8814
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2179 Array<T>::lookup (const T& value, sortmode mode) const |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2180 { |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2181 octave_idx_type n = numel (); |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2182 octave_sort<T> lsort; |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2183 |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2184 if (mode == UNSORTED) |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2185 { |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2186 // auto-detect mode |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2187 if (n > 1 && lsort.descending_compare (elem (0), elem (n-1))) |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2188 mode = DESCENDING; |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2189 else |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2190 mode = ASCENDING; |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2191 } |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2192 |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2193 lsort.set_compare (mode); |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2194 |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2195 return lsort.lookup (data (), n, value); |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2196 } |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2197 |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2198 template <class T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
2199 Array<octave_idx_type> |
9921
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2200 Array<T>::lookup (const Array<T>& values, sortmode mode) const |
8814
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2201 { |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
2202 octave_idx_type n = numel (); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
2203 octave_idx_type nval = values.numel (); |
8814
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2204 octave_sort<T> lsort; |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2205 Array<octave_idx_type> idx (values.dims ()); |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2206 |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2207 if (mode == UNSORTED) |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2208 { |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2209 // auto-detect mode |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2210 if (n > 1 && lsort.descending_compare (elem (0), elem (n-1))) |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2211 mode = DESCENDING; |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2212 else |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2213 mode = ASCENDING; |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2214 } |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2215 |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2216 lsort.set_compare (mode); |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2217 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
2218 // This determines the split ratio between the O(M*log2(N)) and O(M+N) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
2219 // algorithms. |
9921
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2220 static const double ratio = 1.0; |
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2221 sortmode vmode = UNSORTED; |
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2222 |
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2223 // Attempt the O(M+N) algorithm if M is large enough. |
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2224 if (nval > ratio * n / xlog2 (n + 1.0)) |
8814
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2225 { |
9921
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2226 vmode = values.is_sorted (); |
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2227 // The table must not contain a NaN. |
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2228 if ((vmode == ASCENDING && sort_isnan<T> (values(nval-1))) |
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2229 || (vmode == DESCENDING && sort_isnan<T> (values(0)))) |
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2230 vmode = UNSORTED; |
8814
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2231 } |
9921
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2232 |
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2233 if (vmode != UNSORTED) |
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2234 lsort.lookup_sorted (data (), n, values.data (), nval, |
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2235 idx.fortran_vec (), vmode != mode); |
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2236 else |
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2237 lsort.lookup (data (), n, values.data (), nval, idx.fortran_vec ()); |
8814
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2238 |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2239 return idx; |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2240 } |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2241 |
9025 | 2242 template <class T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
2243 octave_idx_type |
9878
ead4f9c82a9a
implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
2244 Array<T>::nnz (void) const |
ead4f9c82a9a
implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
2245 { |
ead4f9c82a9a
implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
2246 const T *src = data (); |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20218
diff
changeset
|
2247 octave_idx_type nel = numel (); |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
2248 octave_idx_type retval = 0; |
9878
ead4f9c82a9a
implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
2249 const T zero = T (); |
ead4f9c82a9a
implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
2250 for (octave_idx_type i = 0; i < nel; i++) |
ead4f9c82a9a
implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
2251 if (src[i] != zero) |
ead4f9c82a9a
implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
2252 retval++; |
ead4f9c82a9a
implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
2253 |
ead4f9c82a9a
implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
2254 return retval; |
ead4f9c82a9a
implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
2255 } |
ead4f9c82a9a
implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
2256 |
ead4f9c82a9a
implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
2257 template <class T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
2258 Array<octave_idx_type> |
9025 | 2259 Array<T>::find (octave_idx_type n, bool backward) const |
2260 { | |
2261 Array<octave_idx_type> retval; | |
2262 const T *src = data (); | |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20218
diff
changeset
|
2263 octave_idx_type nel = numel (); |
9025 | 2264 const T zero = T (); |
9310 | 2265 if (n < 0 || n >= nel) |
9025 | 2266 { |
2267 // We want all elements, which means we'll almost surely need | |
2268 // to resize. So count first, then allocate array of exact size. | |
2269 octave_idx_type cnt = 0; | |
2270 for (octave_idx_type i = 0; i < nel; i++) | |
2271 cnt += src[i] != zero; | |
2272 | |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
2273 retval.clear (cnt, 1); |
9025 | 2274 octave_idx_type *dest = retval.fortran_vec (); |
2275 for (octave_idx_type i = 0; i < nel; i++) | |
2276 if (src[i] != zero) *dest++ = i; | |
2277 } | |
2278 else | |
2279 { | |
2280 // We want a fixed max number of elements, usually small. So be | |
2281 // optimistic, alloc the array in advance, and then resize if | |
2282 // needed. | |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
2283 retval.clear (n, 1); |
9025 | 2284 if (backward) |
2285 { | |
2286 // Do the search as a series of successive single-element searches. | |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
2287 octave_idx_type k = 0; |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
2288 octave_idx_type l = nel - 1; |
9025 | 2289 for (; k < n; k++) |
2290 { | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
2291 for (; l >= 0 && src[l] == zero; l--) ; |
9025 | 2292 if (l >= 0) |
2293 retval(k) = l--; | |
2294 else | |
2295 break; | |
2296 } | |
2297 if (k < n) | |
11574
a83bad07f7e3
attempt better backward compatibility for Array resize functions
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
2298 retval.resize2 (k, 1); |
9310 | 2299 octave_idx_type *rdata = retval.fortran_vec (); |
2300 std::reverse (rdata, rdata + k); | |
9025 | 2301 } |
2302 else | |
2303 { | |
2304 // Do the search as a series of successive single-element searches. | |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
2305 octave_idx_type k = 0; |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
2306 octave_idx_type l = 0; |
9025 | 2307 for (; k < n; k++) |
2308 { | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
2309 for (; l != nel && src[l] == zero; l++) ; |
9029
2df28ad88b0e
small fix in Array::find
Jaroslav Hajek <highegg@gmail.com>
parents:
9025
diff
changeset
|
2310 if (l != nel) |
9025 | 2311 retval(k) = l++; |
2312 else | |
2313 break; | |
2314 } | |
2315 if (k < n) | |
11574
a83bad07f7e3
attempt better backward compatibility for Array resize functions
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
2316 retval.resize2 (k, 1); |
9025 | 2317 } |
2318 } | |
2319 | |
9046
88bf56bbccca
make Array::find already return Matlab-compatible dimensions
Jaroslav Hajek <highegg@gmail.com>
parents:
9029
diff
changeset
|
2320 // Fixup return dimensions, for Matlab compatibility. |
15018
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14846
diff
changeset
|
2321 // find (zeros (0,0)) -> zeros (0,0) |
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14846
diff
changeset
|
2322 // find (zeros (1,0)) -> zeros (1,0) |
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14846
diff
changeset
|
2323 // find (zeros (0,1)) -> zeros (0,1) |
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14846
diff
changeset
|
2324 // find (zeros (0,X)) -> zeros (0,1) |
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14846
diff
changeset
|
2325 // find (zeros (1,1)) -> zeros (0,0) !!!! WHY? |
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14846
diff
changeset
|
2326 // find (zeros (0,1,0)) -> zeros (0,0) |
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14846
diff
changeset
|
2327 // find (zeros (0,1,0,1)) -> zeros (0,0) etc |
9046
88bf56bbccca
make Array::find already return Matlab-compatible dimensions
Jaroslav Hajek <highegg@gmail.com>
parents:
9029
diff
changeset
|
2328 |
88bf56bbccca
make Array::find already return Matlab-compatible dimensions
Jaroslav Hajek <highegg@gmail.com>
parents:
9029
diff
changeset
|
2329 if ((numel () == 1 && retval.is_empty ()) |
88bf56bbccca
make Array::find already return Matlab-compatible dimensions
Jaroslav Hajek <highegg@gmail.com>
parents:
9029
diff
changeset
|
2330 || (rows () == 0 && dims ().numel (1) == 0)) |
88bf56bbccca
make Array::find already return Matlab-compatible dimensions
Jaroslav Hajek <highegg@gmail.com>
parents:
9029
diff
changeset
|
2331 retval.dimensions = dim_vector (); |
88bf56bbccca
make Array::find already return Matlab-compatible dimensions
Jaroslav Hajek <highegg@gmail.com>
parents:
9029
diff
changeset
|
2332 else if (rows () == 1 && ndims () == 2) |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20228
diff
changeset
|
2333 retval.dimensions = dim_vector (1, retval.numel ()); |
9046
88bf56bbccca
make Array::find already return Matlab-compatible dimensions
Jaroslav Hajek <highegg@gmail.com>
parents:
9029
diff
changeset
|
2334 |
9025 | 2335 return retval; |
2336 } | |
2337 | |
9725 | 2338 template <class T> |
2339 Array<T> | |
2340 Array<T>::nth_element (const idx_vector& n, int dim) const | |
2341 { | |
10703
5eb420d92307
fix sort and nth_element when trailing singleton dim is specified
Jaroslav Hajek <highegg@gmail.com>
parents:
10681
diff
changeset
|
2342 if (dim < 0) |
9725 | 2343 { |
2344 (*current_liboctave_error_handler) | |
2345 ("nth_element: invalid dimension"); | |
2346 return Array<T> (); | |
2347 } | |
2348 | |
2349 dim_vector dv = dims (); | |
10703
5eb420d92307
fix sort and nth_element when trailing singleton dim is specified
Jaroslav Hajek <highegg@gmail.com>
parents:
10681
diff
changeset
|
2350 if (dim >= dv.length ()) |
5eb420d92307
fix sort and nth_element when trailing singleton dim is specified
Jaroslav Hajek <highegg@gmail.com>
parents:
10681
diff
changeset
|
2351 dv.resize (dim+1, 1); |
5eb420d92307
fix sort and nth_element when trailing singleton dim is specified
Jaroslav Hajek <highegg@gmail.com>
parents:
10681
diff
changeset
|
2352 |
9725 | 2353 octave_idx_type ns = dv(dim); |
2354 | |
2355 octave_idx_type nn = n.length (ns); | |
2356 | |
2357 dv(dim) = std::min (nn, ns); | |
2358 dv.chop_trailing_singletons (); | |
14440
3d4bea9accd7
Fix segfault on multidimensional median call (bug #35679).
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14158
diff
changeset
|
2359 dim = std::min (dv.length (), dim); |
9725 | 2360 |
2361 Array<T> m (dv); | |
2362 | |
20974
1edf15793cac
maint: Use is_empty rather than "numel () == 0" for clarity.
Rik <rik@octave.org>
parents:
20955
diff
changeset
|
2363 if (m.is_empty ()) |
9725 | 2364 return m; |
2365 | |
2366 sortmode mode = UNSORTED; | |
2367 octave_idx_type lo = 0; | |
2368 | |
2369 switch (n.idx_class ()) | |
2370 { | |
2371 case idx_vector::class_scalar: | |
2372 mode = ASCENDING; | |
2373 lo = n(0); | |
2374 break; | |
2375 case idx_vector::class_range: | |
2376 { | |
2377 octave_idx_type inc = n.increment (); | |
2378 if (inc == 1) | |
2379 { | |
2380 mode = ASCENDING; | |
2381 lo = n(0); | |
2382 } | |
2383 else if (inc == -1) | |
2384 { | |
2385 mode = DESCENDING; | |
2386 lo = ns - 1 - n(0); | |
2387 } | |
2388 } | |
2389 default: | |
2390 break; | |
2391 } | |
2392 | |
2393 if (mode == UNSORTED) | |
2394 { | |
2395 (*current_liboctave_error_handler) | |
2396 ("nth_element: n must be a scalar or a contiguous range"); | |
2397 return Array<T> (); | |
2398 } | |
2399 | |
2400 octave_idx_type up = lo + nn; | |
2401 | |
2402 if (lo < 0 || up > ns) | |
2403 { | |
2404 (*current_liboctave_error_handler) | |
2405 ("nth_element: invalid element index"); | |
2406 return Array<T> (); | |
2407 } | |
2408 | |
2409 octave_idx_type iter = numel () / ns; | |
2410 octave_idx_type stride = 1; | |
2411 | |
2412 for (int i = 0; i < dim; i++) | |
2413 stride *= dv(i); | |
2414 | |
2415 T *v = m.fortran_vec (); | |
2416 const T *ov = data (); | |
2417 | |
2418 OCTAVE_LOCAL_BUFFER (T, buf, ns); | |
2419 | |
2420 octave_sort<T> lsort; | |
2421 lsort.set_compare (mode); | |
2422 | |
2423 for (octave_idx_type j = 0; j < iter; j++) | |
2424 { | |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
2425 octave_idx_type kl = 0; |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
2426 octave_idx_type ku = ns; |
9725 | 2427 |
2428 if (stride == 1) | |
2429 { | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
2430 // copy without NaNs. |
9725 | 2431 // FIXME: impact on integer types noticeable? |
2432 for (octave_idx_type i = 0; i < ns; i++) | |
2433 { | |
2434 T tmp = ov[i]; | |
2435 if (sort_isnan<T> (tmp)) | |
2436 buf[--ku] = tmp; | |
2437 else | |
2438 buf[kl++] = tmp; | |
2439 } | |
2440 | |
2441 ov += ns; | |
2442 } | |
2443 else | |
2444 { | |
2445 octave_idx_type offset = j % stride; | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
2446 // copy without NaNs. |
9725 | 2447 // FIXME: impact on integer types noticeable? |
2448 for (octave_idx_type i = 0; i < ns; i++) | |
2449 { | |
2450 T tmp = ov[offset + i*stride]; | |
2451 if (sort_isnan<T> (tmp)) | |
2452 buf[--ku] = tmp; | |
2453 else | |
2454 buf[kl++] = tmp; | |
2455 } | |
2456 | |
2457 if (offset == stride-1) | |
2458 ov += ns*stride; | |
2459 } | |
2460 | |
2461 if (ku == ns) | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
2462 lsort.nth_element (buf, ns, lo, up); |
9725 | 2463 else if (mode == ASCENDING) |
2464 lsort.nth_element (buf, ku, lo, std::min (ku, up)); | |
2465 else | |
2466 { | |
2467 octave_idx_type nnan = ns - ku; | |
10258 | 2468 octave_idx_type zero = 0; |
2469 lsort.nth_element (buf, ku, std::max (lo - nnan, zero), | |
2470 std::max (up - nnan, zero)); | |
9725 | 2471 std::rotate (buf, buf + ku, buf + ns); |
2472 } | |
2473 | |
2474 if (stride == 1) | |
2475 { | |
2476 for (octave_idx_type i = 0; i < nn; i++) | |
2477 v[i] = buf[lo + i]; | |
2478 | |
2479 v += nn; | |
2480 } | |
2481 else | |
2482 { | |
2483 octave_idx_type offset = j % stride; | |
2484 for (octave_idx_type i = 0; i < nn; i++) | |
2485 v[offset + stride * i] = buf[lo + i]; | |
2486 if (offset == stride-1) | |
2487 v += nn*stride; | |
2488 } | |
2489 } | |
2490 | |
2491 return m; | |
2492 } | |
2493 | |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2494 #define NO_INSTANTIATE_ARRAY_SORT(T) \ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2495 \ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2496 template <> Array<T> \ |
9725 | 2497 Array<T>::sort (int, sortmode) const { return *this; } \ |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2498 \ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2499 template <> Array<T> \ |
9725 | 2500 Array<T>::sort (Array<octave_idx_type> &sidx, int, sortmode) const \ |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2501 { sidx = Array<octave_idx_type> (); return *this; } \ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2502 \ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2503 template <> sortmode \ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2504 Array<T>::is_sorted (sortmode) const \ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2505 { return UNSORTED; } \ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2506 \ |
8725 | 2507 Array<T>::compare_fcn_type \ |
9920
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2508 safe_comparator (sortmode, const Array<T>&, bool) \ |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2509 { return 0; } \ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2510 \ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2511 template <> Array<octave_idx_type> \ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2512 Array<T>::sort_rows_idx (sortmode) const \ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2513 { return Array<octave_idx_type> (); } \ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2514 \ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2515 template <> sortmode \ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2516 Array<T>::is_sorted_rows (sortmode) const \ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2517 { return UNSORTED; } \ |
8814
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2518 \ |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2519 template <> octave_idx_type \ |
9222
7bd406e12e4d
instantiate Array<void *> in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9201
diff
changeset
|
2520 Array<T>::lookup (T const &, sortmode) const \ |
8814
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2521 { return 0; } \ |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2522 template <> Array<octave_idx_type> \ |
9921
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2523 Array<T>::lookup (const Array<T>&, sortmode) const \ |
8814
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2524 { return Array<octave_idx_type> (); } \ |
9878
ead4f9c82a9a
implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
2525 \ |
ead4f9c82a9a
implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
2526 template <> octave_idx_type \ |
ead4f9c82a9a
implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
2527 Array<T>::nnz (void) const\ |
ead4f9c82a9a
implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
2528 { return 0; } \ |
9025 | 2529 template <> Array<octave_idx_type> \ |
2530 Array<T>::find (octave_idx_type, bool) const\ | |
2531 { return Array<octave_idx_type> (); } \ | |
9725 | 2532 \ |
2533 template <> Array<T> \ | |
19861
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
2534 Array<T>::nth_element (const idx_vector&, int) const { return Array<T> (); } |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17769
diff
changeset
|
2535 |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2536 |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2537 template <class T> |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2538 Array<T> |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2539 Array<T>::diag (octave_idx_type k) const |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2540 { |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2541 dim_vector dv = dims (); |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2542 octave_idx_type nd = dv.length (); |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2543 Array<T> d; |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2544 |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2545 if (nd > 2) |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
2546 (*current_liboctave_error_handler) ("Matrix must be 2-dimensional"); |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2547 else |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2548 { |
20218
b2100e1659ac
maint: Use cuddled parentheses when indexing dimension_vectors.
Rik <rik@octave.org>
parents:
19861
diff
changeset
|
2549 octave_idx_type nnr = dv(0); |
b2100e1659ac
maint: Use cuddled parentheses when indexing dimension_vectors.
Rik <rik@octave.org>
parents:
19861
diff
changeset
|
2550 octave_idx_type nnc = dv(1); |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2551 |
12550
ea3a4bf39048
Treat empty vector (1x0 or 0x1) as valid input to diag().
Rik <octave@nomad.inbox5.com>
parents:
12153
diff
changeset
|
2552 if (nnr == 0 && nnc == 0) |
ea3a4bf39048
Treat empty vector (1x0 or 0x1) as valid input to diag().
Rik <octave@nomad.inbox5.com>
parents:
12153
diff
changeset
|
2553 ; // do nothing for empty matrix |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2554 else if (nnr != 1 && nnc != 1) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2555 { |
12550
ea3a4bf39048
Treat empty vector (1x0 or 0x1) as valid input to diag().
Rik <octave@nomad.inbox5.com>
parents:
12153
diff
changeset
|
2556 // Extract diag from matrix |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2557 if (k > 0) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2558 nnc -= k; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2559 else if (k < 0) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2560 nnr += k; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2561 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2562 if (nnr > 0 && nnc > 0) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2563 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2564 octave_idx_type ndiag = (nnr < nnc) ? nnr : nnc; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2565 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2566 d.resize (dim_vector (ndiag, 1)); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2567 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2568 if (k > 0) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2569 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2570 for (octave_idx_type i = 0; i < ndiag; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2571 d.xelem (i) = elem (i, i+k); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2572 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2573 else if (k < 0) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2574 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2575 for (octave_idx_type i = 0; i < ndiag; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2576 d.xelem (i) = elem (i-k, i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2577 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2578 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2579 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2580 for (octave_idx_type i = 0; i < ndiag; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2581 d.xelem (i) = elem (i, i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2582 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2583 } |
18146
a86d608c413c
Return empty matrix rather than issuing error when requested diagonal is out of range.
Marco Caliari <marco.caliari@univr.it>
parents:
18099
diff
changeset
|
2584 else // Matlab returns [] 0x1 for out-of-range diagonal |
a86d608c413c
Return empty matrix rather than issuing error when requested diagonal is out of range.
Marco Caliari <marco.caliari@univr.it>
parents:
18099
diff
changeset
|
2585 d.resize (dim_vector (0, 1)); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2586 } |
12550
ea3a4bf39048
Treat empty vector (1x0 or 0x1) as valid input to diag().
Rik <octave@nomad.inbox5.com>
parents:
12153
diff
changeset
|
2587 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2588 { |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12884
diff
changeset
|
2589 // Create diag matrix from vector |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2590 octave_idx_type roff = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2591 octave_idx_type coff = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2592 if (k > 0) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2593 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2594 roff = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2595 coff = k; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2596 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2597 else if (k < 0) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2598 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2599 roff = -k; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2600 coff = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2601 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2602 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2603 if (nnr == 1) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2604 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2605 octave_idx_type n = nnc + std::abs (k); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2606 d = Array<T> (dim_vector (n, n), resize_fill_value ()); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2607 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2608 for (octave_idx_type i = 0; i < nnc; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2609 d.xelem (i+roff, i+coff) = elem (0, i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2610 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2611 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2612 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2613 octave_idx_type n = nnr + std::abs (k); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2614 d = Array<T> (dim_vector (n, n), resize_fill_value ()); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2615 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2616 for (octave_idx_type i = 0; i < nnr; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2617 d.xelem (i+roff, i+coff) = elem (i, 0); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2618 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2619 } |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2620 } |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2621 |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2622 return d; |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2623 } |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2624 |
4517 | 2625 template <class T> |
10531
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2626 Array<T> |
14557
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14446
diff
changeset
|
2627 Array<T>::diag (octave_idx_type m, octave_idx_type n) const |
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14446
diff
changeset
|
2628 { |
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14446
diff
changeset
|
2629 Array<T> retval; |
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14446
diff
changeset
|
2630 |
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14446
diff
changeset
|
2631 if (ndims () == 2 && (rows () == 1 || cols () == 1)) |
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14446
diff
changeset
|
2632 { |
14591
6b7e9724912a
Silence warning about deprecated resize() call
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14557
diff
changeset
|
2633 retval.resize (dim_vector (m, n), resize_fill_value ()); |
14557
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14446
diff
changeset
|
2634 |
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14446
diff
changeset
|
2635 for (octave_idx_type i = 0; i < numel (); i++) |
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14446
diff
changeset
|
2636 retval.xelem (i, i) = xelem (i); |
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14446
diff
changeset
|
2637 } |
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14446
diff
changeset
|
2638 else |
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14446
diff
changeset
|
2639 (*current_liboctave_error_handler) |
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14446
diff
changeset
|
2640 ("cat: invalid dimension"); |
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14446
diff
changeset
|
2641 |
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14446
diff
changeset
|
2642 return retval; |
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14446
diff
changeset
|
2643 } |
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14446
diff
changeset
|
2644 |
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14446
diff
changeset
|
2645 template <class T> |
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14446
diff
changeset
|
2646 Array<T> |
10531
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2647 Array<T>::cat (int dim, octave_idx_type n, const Array<T> *array_list) |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2648 { |
10716
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
2649 // Default concatenation. |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
2650 bool (dim_vector::*concat_rule) (const dim_vector&, int) = &dim_vector::concat; |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
2651 |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
2652 if (dim == -1 || dim == -2) |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
2653 { |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
2654 concat_rule = &dim_vector::hvcat; |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
2655 dim = -dim - 1; |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
2656 } |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
2657 else if (dim < 0) |
10531
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2658 (*current_liboctave_error_handler) |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2659 ("cat: invalid dimension"); |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2660 |
10535
3f973f6c841c
improve sparse concatenation operator
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
2661 if (n == 1) |
3f973f6c841c
improve sparse concatenation operator
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
2662 return array_list[0]; |
10681
0ba9bd294421
make cat() (hopefully) more matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10674
diff
changeset
|
2663 else if (n == 0) |
0ba9bd294421
make cat() (hopefully) more matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10674
diff
changeset
|
2664 return Array<T> (); |
0ba9bd294421
make cat() (hopefully) more matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10674
diff
changeset
|
2665 |
11241
80e01d79cf80
special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11210
diff
changeset
|
2666 // Special case: |
80e01d79cf80
special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11210
diff
changeset
|
2667 // |
11242
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2668 // cat (dim, [], ..., [], A, ...) |
11241
80e01d79cf80
special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11210
diff
changeset
|
2669 // |
80e01d79cf80
special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11210
diff
changeset
|
2670 // with dim > 2, A not 0x0, and at least three arguments to |
11242
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2671 // concatenate is equivalent to |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2672 // |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2673 // cat (dim, A, ...) |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2674 // |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2675 // Note that this check must be performed here because for full-on |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2676 // braindead Matlab compatibility, we need to have things like |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2677 // |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2678 // cat (3, [], [], A) |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2679 // |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2680 // succeed, but to have things like |
11241
80e01d79cf80
special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11210
diff
changeset
|
2681 // |
80e01d79cf80
special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11210
diff
changeset
|
2682 // cat (3, cat (3, [], []), A) |
80e01d79cf80
special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11210
diff
changeset
|
2683 // cat (3, zeros (0, 0, 2), A) |
80e01d79cf80
special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11210
diff
changeset
|
2684 // |
11242
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2685 // fail. See also bug report #31615. |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2686 |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2687 octave_idx_type istart = 0; |
11241
80e01d79cf80
special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11210
diff
changeset
|
2688 |
80e01d79cf80
special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11210
diff
changeset
|
2689 if (n > 2 && dim > 1) |
80e01d79cf80
special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11210
diff
changeset
|
2690 { |
11242
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2691 for (octave_idx_type i = 0; i < n; i++) |
11241
80e01d79cf80
special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11210
diff
changeset
|
2692 { |
11242
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2693 dim_vector dv = array_list[i].dims (); |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2694 |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2695 if (dv.zero_by_zero ()) |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2696 istart++; |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2697 else |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2698 break; |
11241
80e01d79cf80
special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11210
diff
changeset
|
2699 } |
11242
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2700 |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2701 // Don't skip any initial aguments if they are all empty. |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2702 if (istart >= n) |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2703 istart = 0; |
11241
80e01d79cf80
special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11210
diff
changeset
|
2704 } |
80e01d79cf80
special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11210
diff
changeset
|
2705 |
11242
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2706 dim_vector dv = array_list[istart++].dims (); |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2707 |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2708 for (octave_idx_type i = istart; i < n; i++) |
10716
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
2709 if (! (dv.*concat_rule) (array_list[i].dims (), dim)) |
10531
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2710 (*current_liboctave_error_handler) |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2711 ("cat: dimension mismatch"); |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2712 |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2713 Array<T> retval (dv); |
10716
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
2714 |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
2715 if (retval.is_empty ()) |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
2716 return retval; |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
2717 |
10533
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10531
diff
changeset
|
2718 int nidx = std::max (dv.length (), dim + 1); |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
2719 Array<idx_vector> idxa (dim_vector (nidx, 1), idx_vector::colon); |
10531
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2720 octave_idx_type l = 0; |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2721 |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2722 for (octave_idx_type i = 0; i < n; i++) |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2723 { |
10716
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
2724 // NOTE: This takes some thinking, but no matter what the above rules |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
2725 // are, an empty array can always be skipped at this point, because |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
2726 // the result dimensions are already determined, and there is no way |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
2727 // an empty array may contribute a nonzero piece along the dimension |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
2728 // at this point, unless an empty array can be promoted to a non-empty |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
2729 // one (which makes no sense). I repeat, *no way*, think about it. |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
2730 if (array_list[i].is_empty ()) |
10533
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10531
diff
changeset
|
2731 continue; |
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10531
diff
changeset
|
2732 |
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10531
diff
changeset
|
2733 octave_quit (); |
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10531
diff
changeset
|
2734 |
10531
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2735 octave_idx_type u; |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2736 if (dim < array_list[i].ndims ()) |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2737 u = l + array_list[i].dims ()(dim); |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2738 else |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2739 u = l + 1; |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2740 |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2741 idxa(dim) = idx_vector (l, u); |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2742 |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2743 retval.assign (idxa, array_list[i]); |
10533
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10531
diff
changeset
|
2744 |
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10531
diff
changeset
|
2745 l = u; |
10531
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2746 } |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2747 |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2748 return retval; |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2749 } |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2750 |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2751 template <class T> |
4517 | 2752 void |
3933 | 2753 Array<T>::print_info (std::ostream& os, const std::string& prefix) const |
2754 { | |
8523
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
2755 os << prefix << "rep address: " << rep << '\n' |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
2756 << prefix << "rep->len: " << rep->len << '\n' |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
2757 << prefix << "rep->data: " << static_cast<void *> (rep->data) << '\n' |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
2758 << prefix << "rep->count: " << rep->count << '\n' |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
2759 << prefix << "slice_data: " << static_cast<void *> (slice_data) << '\n' |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
2760 << prefix << "slice_len: " << slice_len << '\n'; |
4513 | 2761 |
2762 // 2D info: | |
2763 // | |
4657 | 2764 // << pefix << "rows: " << rows () << "\n" |
4513 | 2765 // << prefix << "cols: " << cols () << "\n"; |
3933 | 2766 } |
2767 | |
9201
472f0e22aa60
guard against implicit instantiation
Jaroslav Hajek <highegg@gmail.com>
parents:
9121
diff
changeset
|
2768 template <class T> |
10674
e3064439d6b4
new Array method for internal use
Jaroslav Hajek <highegg@gmail.com>
parents:
10673
diff
changeset
|
2769 bool Array<T>::optimize_dimensions (const dim_vector& dv) |
e3064439d6b4
new Array method for internal use
Jaroslav Hajek <highegg@gmail.com>
parents:
10673
diff
changeset
|
2770 { |
e3064439d6b4
new Array method for internal use
Jaroslav Hajek <highegg@gmail.com>
parents:
10673
diff
changeset
|
2771 bool retval = dimensions == dv; |
e3064439d6b4
new Array method for internal use
Jaroslav Hajek <highegg@gmail.com>
parents:
10673
diff
changeset
|
2772 if (retval) |
e3064439d6b4
new Array method for internal use
Jaroslav Hajek <highegg@gmail.com>
parents:
10673
diff
changeset
|
2773 dimensions = dv; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
2774 |
10674
e3064439d6b4
new Array method for internal use
Jaroslav Hajek <highegg@gmail.com>
parents:
10673
diff
changeset
|
2775 return retval; |
e3064439d6b4
new Array method for internal use
Jaroslav Hajek <highegg@gmail.com>
parents:
10673
diff
changeset
|
2776 } |
e3064439d6b4
new Array method for internal use
Jaroslav Hajek <highegg@gmail.com>
parents:
10673
diff
changeset
|
2777 |
e3064439d6b4
new Array method for internal use
Jaroslav Hajek <highegg@gmail.com>
parents:
10673
diff
changeset
|
2778 template <class T> |
9201
472f0e22aa60
guard against implicit instantiation
Jaroslav Hajek <highegg@gmail.com>
parents:
9121
diff
changeset
|
2779 void Array<T>::instantiation_guard () |
472f0e22aa60
guard against implicit instantiation
Jaroslav Hajek <highegg@gmail.com>
parents:
9121
diff
changeset
|
2780 { |
472f0e22aa60
guard against implicit instantiation
Jaroslav Hajek <highegg@gmail.com>
parents:
9121
diff
changeset
|
2781 // This guards against accidental implicit instantiations. |
472f0e22aa60
guard against implicit instantiation
Jaroslav Hajek <highegg@gmail.com>
parents:
9121
diff
changeset
|
2782 // Array<T> instances should always be explicit and use INSTANTIATE_ARRAY. |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14625
diff
changeset
|
2783 T::__xXxXx__ (); |
9201
472f0e22aa60
guard against implicit instantiation
Jaroslav Hajek <highegg@gmail.com>
parents:
9121
diff
changeset
|
2784 } |
472f0e22aa60
guard against implicit instantiation
Jaroslav Hajek <highegg@gmail.com>
parents:
9121
diff
changeset
|
2785 |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2786 #define INSTANTIATE_ARRAY(T, API) \ |
9201
472f0e22aa60
guard against implicit instantiation
Jaroslav Hajek <highegg@gmail.com>
parents:
9121
diff
changeset
|
2787 template <> void Array<T>::instantiation_guard () { } \ |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2788 template class API Array<T> |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2789 |
9732
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2790 // FIXME: is this used? |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2791 |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2792 template <class T> |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2793 std::ostream& |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2794 operator << (std::ostream& os, const Array<T>& a) |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2795 { |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2796 dim_vector a_dims = a.dims (); |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2797 |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2798 int n_dims = a_dims.length (); |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2799 |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2800 os << n_dims << "-dimensional array"; |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2801 |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2802 if (n_dims) |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2803 os << " (" << a_dims.str () << ")"; |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2804 |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2805 os <<"\n\n"; |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2806 |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2807 if (n_dims) |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2808 { |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2809 os << "data:"; |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2810 |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
2811 Array<octave_idx_type> ra_idx (dim_vector (n_dims, 1), 0); |
9732
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2812 |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2813 // Number of times the first 2d-array is to be displayed. |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2814 |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2815 octave_idx_type m = 1; |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2816 for (int i = 2; i < n_dims; i++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2817 m *= a_dims(i); |
9732
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2818 |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2819 if (m == 1) |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2820 { |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2821 octave_idx_type rows = 0; |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2822 octave_idx_type cols = 0; |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2823 |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2824 switch (n_dims) |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2825 { |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2826 case 2: |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2827 rows = a_dims(0); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2828 cols = a_dims(1); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2829 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2830 for (octave_idx_type j = 0; j < rows; j++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2831 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2832 ra_idx(0) = j; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2833 for (octave_idx_type k = 0; k < cols; k++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2834 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2835 ra_idx(1) = k; |
15018
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14846
diff
changeset
|
2836 os << " " << a.elem (ra_idx); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2837 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2838 os << "\n"; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2839 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2840 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2841 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2842 default: |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2843 rows = a_dims(0); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2844 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2845 for (octave_idx_type k = 0; k < rows; k++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2846 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2847 ra_idx(0) = k; |
15018
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14846
diff
changeset
|
2848 os << " " << a.elem (ra_idx); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2849 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2850 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2851 } |
9732
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2852 |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2853 os << "\n"; |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2854 } |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2855 else |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2856 { |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2857 octave_idx_type rows = a_dims(0); |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2858 octave_idx_type cols = a_dims(1); |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2859 |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2860 for (int i = 0; i < m; i++) |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2861 { |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2862 os << "\n(:,:,"; |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2863 |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2864 for (int j = 2; j < n_dims - 1; j++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2865 os << ra_idx(j) + 1 << ","; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2866 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2867 os << ra_idx(n_dims - 1) + 1 << ") = \n"; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2868 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2869 for (octave_idx_type j = 0; j < rows; j++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2870 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2871 ra_idx(0) = j; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2872 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2873 for (octave_idx_type k = 0; k < cols; k++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2874 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2875 ra_idx(1) = k; |
15018
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14846
diff
changeset
|
2876 os << " " << a.elem (ra_idx); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2877 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2878 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2879 os << "\n"; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2880 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2881 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2882 os << "\n"; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2883 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2884 if (i != m - 1) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2885 increment_index (ra_idx, a_dims, 2); |
9732
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2886 } |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2887 } |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2888 } |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2889 |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2890 return os; |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2891 } |