Mercurial > hg > octave-nkf
annotate liboctave/array/Array.cc @ 19898:4197fc428c7d
maint: Update copyright notices for 2015.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 11 Feb 2015 14:19:08 -0500 |
parents | 0e1f5a750d00 |
children | 19755f4fc851 |
rev | line source |
---|---|
1993 | 1 // Template array classes |
237 | 2 /* |
3 | |
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19793
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; |
937921654627
clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
75 rep = new ArrayRep (length (), val); |
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 |
19576
af41e41ad28e
replace oct-mem.h inline indirections by standard function calls.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18145
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) |
9c4daf174387
implement IDs for common liboctave exceptions
Jaroslav Hajek <highegg@gmail.com>
parents:
10366
diff
changeset
|
193 gripe_invalid_index (); |
9c4daf174387
implement IDs for common liboctave exceptions
Jaroslav Hajek <highegg@gmail.com>
parents:
10366
diff
changeset
|
194 if (n >= slice_len) |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
195 gripe_index_out_of_range (1, 1, n+1, slice_len); |
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) |
9c4daf174387
implement IDs for common liboctave exceptions
Jaroslav Hajek <highegg@gmail.com>
parents:
10366
diff
changeset
|
227 gripe_invalid_index (); |
9c4daf174387
implement IDs for common liboctave exceptions
Jaroslav Hajek <highegg@gmail.com>
parents:
10366
diff
changeset
|
228 if (n >= slice_len) |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
229 gripe_index_out_of_range (1, 1, n+1, slice_len); |
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); | |
261 #ifdef BOUNDS_CHECKING | |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
262 if (k < 0 || k > dimensions.numel (1)) |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
263 gripe_index_out_of_range (2, 2, k+1, dimensions.numel (1)); |
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; |
9731 | 276 #ifdef BOUNDS_CHECKING |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
277 if (k < 0 || k > dimensions.numel (2)) |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
278 gripe_index_out_of_range (3, 3, k+1, dimensions.numel (2)); |
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 { | |
288 #ifdef BOUNDS_CHECKING | |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
289 if (lo < 0) |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
290 gripe_index_out_of_range (1, 1, lo+1, numel ()); |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
291 if (up > numel ()) |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
292 gripe_index_out_of_range (1, 1, up, numel ()); |
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 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
316 assert (n == perm.length ()); |
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 | |
392 // Recursive N-d generalized transpose | |
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 { |
19576
af41e41ad28e
replace oct-mem.h inline indirections by standard function calls.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18145
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 | |
8410 | 449 int perm_vec_len = perm_vec_arg.length (); |
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) |
11507
c3ad80f4ce36
Array.h, Array.cc: more constructor fixes
John W. Eaton <jwe@octave.org>
parents:
11242
diff
changeset
|
535 : n (ia.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
|
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 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
567 // Recursive N-d indexing |
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 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
584 // Recursive N-d indexed assignment |
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 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
601 // Recursive N-d indexed assignment |
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 { |
19576
af41e41ad28e
replace oct-mem.h inline indirections by standard function calls.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18145
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:
18145
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 { |
18098
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 |
19576
af41e41ad28e
replace oct-mem.h inline indirections by standard function calls.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18145
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) |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
729 gripe_index_out_of_range (1, 1, i.extent (n), n); // throws |
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) |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
796 gripe_index_out_of_range (2, 1, i.extent (r), r); // throws |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
797 if (j.extent (c) != c) |
14158
727504fd26f0
Fix erroneous indexing error message.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
798 gripe_index_out_of_range (2, 2, j.extent (c), c); // 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 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
840 int ial = ia.length (); |
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)) |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
858 gripe_index_out_of_range (ial, i+1, ia(i).extent (dv(i)), dv(i)); // throws |
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 { |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
915 if (n >= 0 && ndims () == 2) |
4513 | 916 { |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
917 dim_vector dv; |
8333 | 918 // This is driven by Matlab's behaviour of giving a *row* vector |
919 // on some out-of-bounds assignments. Specifically, Matlab | |
920 // allows a(i) with out-of-bouds i when a is either of 0x0, 1x0, | |
921 // 1x1, 0xN, and gives a row vector in all cases (yes, even the | |
922 // last one, search me why). Giving a column vector would make | |
923 // much more sense (given the way trailing singleton dims are | |
924 // treated). | |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
925 bool invalid = false; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
926 if (rows () == 0 || rows () == 1) |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
927 dv = dim_vector (1, n); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
928 else if (columns () == 1) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
929 dv = dim_vector (n, 1); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
930 else |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
931 invalid = true; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
932 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
933 if (invalid) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
934 gripe_invalid_resize (); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
935 else |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
936 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
937 octave_idx_type nx = numel (); |
9100
1a8bbfb2f7cf
optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9058
diff
changeset
|
938 if (n == nx - 1 && n > 0) |
1a8bbfb2f7cf
optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9058
diff
changeset
|
939 { |
1a8bbfb2f7cf
optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9058
diff
changeset
|
940 // Stack "pop" operation. |
1a8bbfb2f7cf
optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9058
diff
changeset
|
941 if (rep->count == 1) |
1a8bbfb2f7cf
optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9058
diff
changeset
|
942 slice_data[slice_len-1] = T (); |
1a8bbfb2f7cf
optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9058
diff
changeset
|
943 slice_len--; |
1a8bbfb2f7cf
optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9058
diff
changeset
|
944 dimensions = dv; |
1a8bbfb2f7cf
optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9058
diff
changeset
|
945 } |
1a8bbfb2f7cf
optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9058
diff
changeset
|
946 else if (n == nx + 1 && nx > 0) |
1a8bbfb2f7cf
optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9058
diff
changeset
|
947 { |
1a8bbfb2f7cf
optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9058
diff
changeset
|
948 // Stack "push" operation. |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
949 if (rep->count == 1 |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
950 && slice_data + slice_len < rep->data + rep->len) |
9100
1a8bbfb2f7cf
optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9058
diff
changeset
|
951 { |
1a8bbfb2f7cf
optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9058
diff
changeset
|
952 slice_data[slice_len++] = rfv; |
1a8bbfb2f7cf
optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9058
diff
changeset
|
953 dimensions = dv; |
1a8bbfb2f7cf
optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9058
diff
changeset
|
954 } |
1a8bbfb2f7cf
optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9058
diff
changeset
|
955 else |
1a8bbfb2f7cf
optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9058
diff
changeset
|
956 { |
1a8bbfb2f7cf
optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9058
diff
changeset
|
957 static const octave_idx_type max_stack_chunk = 1024; |
1a8bbfb2f7cf
optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9058
diff
changeset
|
958 octave_idx_type nn = n + std::min (nx, max_stack_chunk); |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
959 Array<T> tmp (Array<T> (dim_vector (nn, 1)), dv, 0, n); |
9100
1a8bbfb2f7cf
optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9058
diff
changeset
|
960 T *dest = tmp.fortran_vec (); |
1a8bbfb2f7cf
optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9058
diff
changeset
|
961 |
19576
af41e41ad28e
replace oct-mem.h inline indirections by standard function calls.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18145
diff
changeset
|
962 std::copy (data (), data () + nx, dest); |
9100
1a8bbfb2f7cf
optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9058
diff
changeset
|
963 dest[nx] = rfv; |
1a8bbfb2f7cf
optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9058
diff
changeset
|
964 |
1a8bbfb2f7cf
optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9058
diff
changeset
|
965 *this = tmp; |
1a8bbfb2f7cf
optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9058
diff
changeset
|
966 } |
1a8bbfb2f7cf
optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9058
diff
changeset
|
967 } |
1a8bbfb2f7cf
optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9058
diff
changeset
|
968 else if (n != nx) |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
969 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
970 Array<T> tmp = Array<T> (dv); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
971 T *dest = tmp.fortran_vec (); |
4513 | 972 |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
973 octave_idx_type n0 = std::min (n, nx); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
974 octave_idx_type n1 = n - n0; |
19576
af41e41ad28e
replace oct-mem.h inline indirections by standard function calls.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18145
diff
changeset
|
975 std::copy (data (), data () + n0, dest); |
af41e41ad28e
replace oct-mem.h inline indirections by standard function calls.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18145
diff
changeset
|
976 std::fill_n (dest + n0, n1, rfv); |
4513 | 977 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
978 *this = tmp; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
979 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
980 } |
4513 | 981 } |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
982 else |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
983 gripe_invalid_resize (); |
4513 | 984 } |
985 | |
986 template <class T> | |
987 void | |
11574
a83bad07f7e3
attempt better backward compatibility for Array resize functions
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
988 Array<T>::resize2 (octave_idx_type r, octave_idx_type c, const T& rfv) |
4513 | 989 { |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
990 if (r >= 0 && c >= 0 && ndims () == 2) |
4513 | 991 { |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
992 octave_idx_type rx = rows (); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
993 octave_idx_type cx = columns (); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
994 if (r != rx || c != cx) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
995 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
996 Array<T> tmp = Array<T> (dim_vector (r, c)); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
997 T *dest = tmp.fortran_vec (); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
998 |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
999 octave_idx_type r0 = std::min (r, rx); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
1000 octave_idx_type r1 = r - r0; |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
1001 octave_idx_type c0 = std::min (c, cx); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
1002 octave_idx_type c1 = c - c0; |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1003 const T *src = data (); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1004 if (r == rx) |
9773
01f897d8a130
optimize memory manipulation by arrays & indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
9732
diff
changeset
|
1005 { |
19576
af41e41ad28e
replace oct-mem.h inline indirections by standard function calls.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18145
diff
changeset
|
1006 std::copy (src, src + r * c0, dest); |
9773
01f897d8a130
optimize memory manipulation by arrays & indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
9732
diff
changeset
|
1007 dest += r * c0; |
01f897d8a130
optimize memory manipulation by arrays & indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
9732
diff
changeset
|
1008 } |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1009 else |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1010 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1011 for (octave_idx_type k = 0; k < c0; k++) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1012 { |
19576
af41e41ad28e
replace oct-mem.h inline indirections by standard function calls.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18145
diff
changeset
|
1013 std::copy (src, src + r0, dest); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1014 src += rx; |
9773
01f897d8a130
optimize memory manipulation by arrays & indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
9732
diff
changeset
|
1015 dest += r0; |
19576
af41e41ad28e
replace oct-mem.h inline indirections by standard function calls.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18145
diff
changeset
|
1016 std::fill_n (dest, r1, rfv); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1017 dest += r1; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1018 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1019 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1020 |
19576
af41e41ad28e
replace oct-mem.h inline indirections by standard function calls.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18145
diff
changeset
|
1021 std::fill_n (dest, r * c1, rfv); |
8290
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 *this = tmp; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1024 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1025 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1026 else |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1027 gripe_invalid_resize (); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1028 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1029 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1030 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1031 template<class T> |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1032 void |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
1033 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
|
1034 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1035 int dvl = dv.length (); |
8784
374cb30311a8
remove dead branch in Array.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
8760
diff
changeset
|
1036 if (dvl == 2) |
11574
a83bad07f7e3
attempt better backward compatibility for Array resize functions
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
1037 resize2 (dv(0), dv(1), rfv); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1038 else if (dimensions != dv) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1039 { |
8799
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8784
diff
changeset
|
1040 if (dimensions.length () <= dvl && ! dv.any_neg ()) |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1041 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1042 Array<T> tmp (dv); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1043 // Prepare for recursive resizing. |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1044 rec_resize_helper rh (dv, dimensions.redim (dvl)); |
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 // Do it. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1047 rh.resize_fill (data (), tmp.fortran_vec (), rfv); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1048 *this = tmp; |
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 else |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1051 gripe_invalid_resize (); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1052 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1053 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1054 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1055 template <class T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1056 Array<T> |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1057 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
|
1058 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1059 Array<T> tmp = *this; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1060 if (resize_ok) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1061 { |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
1062 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
|
1063 octave_idx_type nx = i.extent (n); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1064 if (n != nx) |
8563
3a3421a9f0bb
optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
1065 { |
3a3421a9f0bb
optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
1066 if (i.is_scalar ()) |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
1067 return Array<T> (dim_vector (1, 1), rfv); |
8563
3a3421a9f0bb
optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
1068 else |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
1069 tmp.resize1 (nx, rfv); |
8563
3a3421a9f0bb
optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
1070 } |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1071 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1072 if (tmp.numel () != nx) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1073 return Array<T> (); |
4513 | 1074 } |
1075 | |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1076 return tmp.index (i); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1077 } |
4513 | 1078 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1079 template <class T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1080 Array<T> |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1081 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
|
1082 bool resize_ok, const T& rfv) const |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1083 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1084 Array<T> tmp = *this; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1085 if (resize_ok) |
4513 | 1086 { |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1087 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
|
1088 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
|
1089 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
|
1090 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
|
1091 octave_idx_type cx = j.extent (c); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1092 if (r != rx || c != cx) |
8563
3a3421a9f0bb
optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
1093 { |
3a3421a9f0bb
optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
1094 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
|
1095 return Array<T> (dim_vector (1, 1), rfv); |
8563
3a3421a9f0bb
optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
1096 else |
11574
a83bad07f7e3
attempt better backward compatibility for Array resize functions
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
1097 tmp.resize2 (rx, cx, rfv); |
8563
3a3421a9f0bb
optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
1098 } |
4747 | 1099 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1100 if (tmp.rows () != rx || tmp.columns () != cx) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1101 return Array<T> (); |
4513 | 1102 } |
1103 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1104 return tmp.index (i, j); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1105 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1106 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1107 template <class T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1108 Array<T> |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1109 Array<T>::index (const Array<idx_vector>& ia, |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1110 bool resize_ok, const T& rfv) const |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1111 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1112 Array<T> tmp = *this; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1113 if (resize_ok) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1114 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1115 int ial = ia.length (); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1116 dim_vector dv = dimensions.redim (ial); |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9341
diff
changeset
|
1117 dim_vector dvx = dim_vector::alloc (ial); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1118 for (int i = 0; i < ial; i++) dvx(i) = ia(i).extent (dv (i)); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1119 if (! (dvx == dv)) |
8563
3a3421a9f0bb
optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
1120 { |
3a3421a9f0bb
optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
1121 bool all_scalars = true; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1122 for (int i = 0; i < ial; i++) |
8563
3a3421a9f0bb
optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
1123 all_scalars = all_scalars && ia(i).is_scalar (); |
3a3421a9f0bb
optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
1124 if (all_scalars) |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
1125 return Array<T> (dim_vector (1, 1), rfv); |
8563
3a3421a9f0bb
optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
1126 else |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
1127 tmp.resize (dvx, rfv); |
8563
3a3421a9f0bb
optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
1128 } |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1129 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1130 if (tmp.dimensions != dvx) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1131 return Array<T> (); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1132 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1133 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1134 return tmp.index (ia); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1135 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1136 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1137 |
4513 | 1138 template <class T> |
1139 void | |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1140 Array<T>::assign (const idx_vector& i, const Array<T>& rhs, const T& rfv) |
4513 | 1141 { |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
1142 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
|
1143 octave_idx_type rhl = rhs.numel (); |
4548 | 1144 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1145 if (rhl == 1 || i.length (n) == rhl) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1146 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1147 octave_idx_type nx = i.extent (n); |
9886
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9878
diff
changeset
|
1148 bool colon = i.is_colon_equiv (nx); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1149 // Try to resize first if necessary. |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1150 if (nx != n) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1151 { |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1152 // Optimize case A = []; A(1:n) = X with A empty. |
9886
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9878
diff
changeset
|
1153 if (dimensions.zero_by_zero () && colon) |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8563
diff
changeset
|
1154 { |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8563
diff
changeset
|
1155 if (rhl == 1) |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8563
diff
changeset
|
1156 *this = Array<T> (dim_vector (1, nx), rhs(0)); |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8563
diff
changeset
|
1157 else |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8563
diff
changeset
|
1158 *this = Array<T> (rhs, dim_vector (1, nx)); |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8563
diff
changeset
|
1159 return; |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8563
diff
changeset
|
1160 } |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8563
diff
changeset
|
1161 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1162 resize1 (nx, rfv); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1163 n = numel (); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1164 } |
4747 | 1165 |
9886
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9878
diff
changeset
|
1166 if (colon) |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1167 { |
9058
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1168 // 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
|
1169 if (rhl == 1) |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1170 fill (rhs(0)); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1171 else |
9058
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1172 *this = rhs.reshape (dimensions); |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1173 } |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1174 else |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1175 { |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1176 if (rhl == 1) |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1177 i.fill (rhs(0), n, fortran_vec ()); |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1178 else |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1179 i.assign (rhs.data (), n, fortran_vec ()); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1180 } |
4513 | 1181 } |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1182 else |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1183 gripe_invalid_assignment_size (); |
4513 | 1184 } |
1185 | |
1186 template <class T> | |
1187 void | |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1188 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
|
1189 const Array<T>& rhs, const T& rfv) |
4513 | 1190 { |
14624
edf9ca8a92a8
when redimensioning, always pad dim_vector objects with 1 (bug #33216)
John W. Eaton <jwe@octave.org>
parents:
14440
diff
changeset
|
1191 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
|
1192 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1193 // Get RHS extents, discarding singletons. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1194 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
|
1195 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1196 // Get LHS extents, allowing Fortran indexing in the second dim. |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1197 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
|
1198 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1199 // 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
|
1200 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
|
1201 |
8333 | 1202 // In the special when all dimensions are zero, colons are allowed |
1203 // to inquire the shape of RHS. The rules are more obscure, so we | |
1204 // 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
|
1205 if (initial_dims_all_zero) |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1206 rdv = zero_dims_inquire (i, j, rhdv); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1207 else |
4513 | 1208 { |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1209 rdv(0) = i.extent (dv(0)); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1210 rdv(1) = j.extent (dv(1)); |
4513 | 1211 } |
1212 | |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1213 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
|
1214 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
|
1215 octave_idx_type jl = j.length (rdv(1)); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1216 rhdv.chop_all_singletons (); |
8333 | 1217 bool match = (isfill |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1218 || (rhdv.length () == 2 && il == rhdv(0) && jl == rhdv(1))); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1219 match = match || (il == 1 && jl == rhdv(0) && rhdv(1) == 1); |
4548 | 1220 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1221 if (match) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1222 { |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1223 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
|
1224 && j.is_colon_equiv (rdv(1))); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1225 // Resize if requested. |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1226 if (rdv != dv) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1227 { |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8563
diff
changeset
|
1228 // 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
|
1229 if (dv.zero_by_zero () && all_colons) |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8563
diff
changeset
|
1230 { |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8563
diff
changeset
|
1231 if (isfill) |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8563
diff
changeset
|
1232 *this = Array<T> (rdv, rhs(0)); |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8563
diff
changeset
|
1233 else |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8563
diff
changeset
|
1234 *this = Array<T> (rhs, rdv); |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8563
diff
changeset
|
1235 return; |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8563
diff
changeset
|
1236 } |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8563
diff
changeset
|
1237 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1238 resize (rdv, rfv); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1239 dv = dimensions; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1240 } |
4513 | 1241 |
9886
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9878
diff
changeset
|
1242 if (all_colons) |
9058
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1243 { |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1244 // 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
|
1245 if (isfill) |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1246 fill (rhs(0)); |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1247 else |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1248 *this = rhs.reshape (dimensions); |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1249 } |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1250 else |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1251 { |
9058
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1252 // 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
|
1253 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
|
1254 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
|
1255 octave_idx_type c = dv(1); |
9058
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1256 idx_vector ii (i); |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1257 |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1258 const T* src = rhs.data (); |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1259 T *dest = fortran_vec (); |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1260 |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1261 // Try reduction first. |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1262 if (ii.maybe_reduce (r, j, c)) |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1263 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1264 if (isfill) |
9058
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1265 ii.fill (*src, n, dest); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1266 else |
9058
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1267 ii.assign (src, n, dest); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1268 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1269 else |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1270 { |
9058
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1271 if (isfill) |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1272 { |
9058
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1273 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
|
1274 i.fill (*src, r, dest + r * j.xelem (k)); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1275 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1276 else |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1277 { |
9058
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1278 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
|
1279 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
|
1280 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1281 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1282 } |
4747 | 1283 } |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1284 else |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1285 gripe_assignment_dimension_mismatch (); |
4513 | 1286 } |
1287 | |
1288 template <class T> | |
1289 void | |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1290 Array<T>::assign (const Array<idx_vector>& ia, |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1291 const Array<T>& rhs, const T& rfv) |
4513 | 1292 { |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1293 int ial = ia.length (); |
5275 | 1294 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1295 // FIXME: is this dispatching necessary / desirable? |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1296 if (ial == 1) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1297 assign (ia(0), rhs, rfv); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1298 else if (ial == 2) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1299 assign (ia(0), ia(1), rhs, rfv); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1300 else if (ial > 0) |
4513 | 1301 { |
14624
edf9ca8a92a8
when redimensioning, always pad dim_vector objects with 1 (bug #33216)
John W. Eaton <jwe@octave.org>
parents:
14440
diff
changeset
|
1302 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
|
1303 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1304 // Get RHS extents, discarding singletons. |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1305 dim_vector rhdv = rhs.dims (); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1306 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1307 // Get LHS extents, allowing Fortran indexing in the second dim. |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1308 dim_vector dv = dimensions.redim (ial); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1309 |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1310 // Get the extents forced by indexing. |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1311 dim_vector rdv; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1312 |
8333 | 1313 // In the special when all dimensions are zero, colons are |
1314 // allowed to inquire the shape of RHS. The rules are more | |
1315 // 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
|
1316 if (initial_dims_all_zero) |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1317 rdv = zero_dims_inquire (ia, rhdv); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1318 else |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1319 { |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9341
diff
changeset
|
1320 rdv = dim_vector::alloc (ial); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1321 for (int i = 0; i < ial; i++) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1322 rdv(i) = ia(i).extent (dv(i)); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1323 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1324 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1325 // 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
|
1326 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
|
1327 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
|
1328 bool isfill = rhs.numel () == 1; |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1329 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1330 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
|
1331 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
|
1332 int rhdvl = rhdv.length (); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1333 for (int i = 0; i < ial; i++) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1334 { |
9886
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9878
diff
changeset
|
1335 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
|
1336 octave_idx_type l = ia(i).length (rdv(i)); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1337 if (l == 1) continue; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1338 match = match && j < rhdvl && l == rhdv(j++); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1339 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1340 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1341 match = match && (j == rhdvl || rhdv(j) == 1); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1342 match = match || isfill; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1343 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1344 if (match) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1345 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1346 // Resize first if necessary. |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1347 if (rdv != dv) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1348 { |
9886
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9878
diff
changeset
|
1349 // 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
|
1350 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
|
1351 { |
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9878
diff
changeset
|
1352 rdv.chop_trailing_singletons (); |
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9878
diff
changeset
|
1353 if (isfill) |
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9878
diff
changeset
|
1354 *this = Array<T> (rdv, rhs(0)); |
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9878
diff
changeset
|
1355 else |
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9878
diff
changeset
|
1356 *this = Array<T> (rhs, rdv); |
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9878
diff
changeset
|
1357 return; |
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9878
diff
changeset
|
1358 } |
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9878
diff
changeset
|
1359 |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
1360 resize (rdv, rfv); |
10095
eb8ac0eed9f1
always chop dimension vector when constructing Arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
10075
diff
changeset
|
1361 dv = rdv; |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1362 } |
4513 | 1363 |
9058
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1364 if (all_colons) |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1365 { |
9058
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1366 // 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
|
1367 if (isfill) |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1368 fill (rhs(0)); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1369 else |
9058
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1370 *this = rhs.reshape (dimensions); |
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 else |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1373 { |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1374 // Do the actual work. |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1375 |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1376 // Prepare for recursive indexing |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1377 rec_index_helper rh (dv, ia); |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1378 |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1379 // Do it. |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1380 if (isfill) |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1381 rh.fill (rhs(0), fortran_vec ()); |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1382 else |
2da105bf2507
remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents:
9046
diff
changeset
|
1383 rh.assign (rhs.data (), fortran_vec ()); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1384 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1385 } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1386 else |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1387 gripe_assignment_dimension_mismatch (); |
4553 | 1388 } |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1389 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1390 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1391 template <class T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1392 void |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1393 Array<T>::delete_elements (const idx_vector& i) |
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 octave_idx_type n = numel (); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1396 if (i.is_colon ()) |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1397 { |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1398 *this = Array<T> (); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1399 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1400 else if (i.length (n) != 0) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1401 { |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
1402 if (i.extent (n) != n) |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
1403 gripe_del_index_out_of_range (true, i.extent (n), n); |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
1404 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1405 octave_idx_type l, u; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1406 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
|
1407 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
|
1408 { |
1a8bbfb2f7cf
optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9058
diff
changeset
|
1409 // Stack "pop" operation. |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
1410 resize1 (n-1); |
9100
1a8bbfb2f7cf
optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9058
diff
changeset
|
1411 } |
1a8bbfb2f7cf
optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9058
diff
changeset
|
1412 else if (i.is_cont_range (n, l, u)) |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1413 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1414 // Special case deleting a contiguous range. |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1415 octave_idx_type m = n + l - u; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1416 Array<T> tmp (dim_vector (col_vec ? m : 1, !col_vec ? m : 1)); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1417 const T *src = data (); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1418 T *dest = tmp.fortran_vec (); |
19576
af41e41ad28e
replace oct-mem.h inline indirections by standard function calls.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18145
diff
changeset
|
1419 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:
18145
diff
changeset
|
1420 std::copy (src + u, src + n, dest + l); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1421 *this = tmp; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1422 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1423 else |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1424 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1425 // Use index. |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1426 *this = index (i.complement (n)); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1427 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1428 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1429 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1430 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1431 template <class T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1432 void |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1433 Array<T>::delete_elements (int dim, const idx_vector& i) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1434 { |
8302
f2e050b62199
fix dim check in Array<T>::delete_elements
Jaroslav Hajek <highegg@gmail.com>
parents:
8290
diff
changeset
|
1435 if (dim < 0 || dim >= ndims ()) |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1436 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1437 (*current_liboctave_error_handler) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1438 ("invalid dimension in delete_elements"); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1439 return; |
4513 | 1440 } |
1441 | |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1442 octave_idx_type n = dimensions (dim); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1443 if (i.is_colon ()) |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1444 { |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1445 *this = Array<T> (); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1446 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1447 else if (i.length (n) != 0) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1448 { |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
1449 if (i.extent (n) != n) |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
1450 gripe_del_index_out_of_range (false, i.extent (n), n); |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
1451 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1452 octave_idx_type l, u; |
4513 | 1453 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1454 if (i.is_cont_range (n, l, u)) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1455 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1456 // 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
|
1457 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
|
1458 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
|
1459 octave_idx_type du = 1; |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1460 dim_vector rdv = dimensions; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1461 rdv(dim) = nd; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1462 for (int k = 0; k < dim; k++) dl *= dimensions(k); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1463 for (int k = dim + 1; k < ndims (); k++) du *= dimensions(k); |
4513 | 1464 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1465 // Special case deleting a contiguous range. |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1466 Array<T> tmp = Array<T> (rdv); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1467 const T *src = data (); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1468 T *dest = tmp.fortran_vec (); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1469 l *= dl; u *= dl; n *= dl; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1470 for (octave_idx_type k = 0; k < du; k++) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1471 { |
19576
af41e41ad28e
replace oct-mem.h inline indirections by standard function calls.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18145
diff
changeset
|
1472 std::copy (src, src + l, dest); |
9773
01f897d8a130
optimize memory manipulation by arrays & indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
9732
diff
changeset
|
1473 dest += l; |
19576
af41e41ad28e
replace oct-mem.h inline indirections by standard function calls.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
18145
diff
changeset
|
1474 std::copy (src + u, src + n, dest); |
9773
01f897d8a130
optimize memory manipulation by arrays & indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
9732
diff
changeset
|
1475 dest += n - u; |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1476 src += n; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1477 } |
4870 | 1478 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1479 *this = tmp; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1480 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1481 else |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1482 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1483 // Use index. |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
1484 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
|
1485 ia (dim) = i.complement (n); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1486 *this = index (ia); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1487 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1488 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1489 } |
4747 | 1490 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1491 template <class T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1492 void |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1493 Array<T>::delete_elements (const Array<idx_vector>& ia) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1494 { |
17642
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1495 int ial = ia.length (); |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1496 |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1497 if (ial == 1) |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1498 delete_elements (ia(0)); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1499 else |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1500 { |
17642
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1501 int k, dim = -1; |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1502 for (k = 0; k < ial; k++) |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1503 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1504 if (! ia(k).is_colon ()) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1505 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1506 if (dim < 0) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1507 dim = k; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1508 else |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1509 break; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1510 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1511 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1512 if (dim < 0) |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1513 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1514 dim_vector dv = dimensions; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1515 dv(0) = 0; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1516 *this = Array<T> (dv); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1517 } |
17642
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1518 else if (k == ial) |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1519 { |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1520 delete_elements (dim, ia(dim)); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1521 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1522 else |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1523 { |
17642
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1524 // 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
|
1525 // 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
|
1526 // 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
|
1527 // 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
|
1528 // 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
|
1529 |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1530 // 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
|
1531 // 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
|
1532 // 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
|
1533 // accept both. |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1534 |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1535 bool empty_assignment = false; |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1536 |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1537 int num_non_colon_indices = 0; |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1538 |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1539 int nd = ndims (); |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1540 |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1541 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
|
1542 { |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1543 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
|
1544 |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1545 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
|
1546 { |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1547 empty_assignment = true; |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1548 break; |
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 |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1551 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
|
1552 { |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1553 num_non_colon_indices++; |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1554 |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1555 if (num_non_colon_indices == 2) |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1556 break; |
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 } |
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 if (! empty_assignment) |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1561 (*current_liboctave_error_handler) |
7ed397c8ca68
improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
1562 ("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
|
1563 } |
4513 | 1564 } |
1565 | |
1566 } | |
1567 | |
1568 template <class T> | |
1569 Array<T>& | |
5275 | 1570 Array<T>::insert (const Array<T>& a, octave_idx_type r, octave_idx_type c) |
4513 | 1571 { |
10115
ed49cef7e005
simplify Array::insert methods
Jaroslav Hajek <highegg@gmail.com>
parents:
10113
diff
changeset
|
1572 idx_vector i (r, r + a.rows ()); |
ed49cef7e005
simplify Array::insert methods
Jaroslav Hajek <highegg@gmail.com>
parents:
10113
diff
changeset
|
1573 idx_vector j (c, c + a.columns ()); |
4786 | 1574 if (ndims () == 2 && a.ndims () == 2) |
10115
ed49cef7e005
simplify Array::insert methods
Jaroslav Hajek <highegg@gmail.com>
parents:
10113
diff
changeset
|
1575 assign (i, j, a); |
4786 | 1576 else |
4513 | 1577 { |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
1578 Array<idx_vector> idx (dim_vector (a.ndims (), 1)); |
10115
ed49cef7e005
simplify Array::insert methods
Jaroslav Hajek <highegg@gmail.com>
parents:
10113
diff
changeset
|
1579 idx(0) = i; |
ed49cef7e005
simplify Array::insert methods
Jaroslav Hajek <highegg@gmail.com>
parents:
10113
diff
changeset
|
1580 idx(1) = j; |
12884
73e75ff9c31b
Fix incorrect loop (bug #32683)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12550
diff
changeset
|
1581 for (int k = 2; k < a.ndims (); k++) |
10115
ed49cef7e005
simplify Array::insert methods
Jaroslav Hajek <highegg@gmail.com>
parents:
10113
diff
changeset
|
1582 idx(k) = idx_vector (0, a.dimensions(k)); |
ed49cef7e005
simplify Array::insert methods
Jaroslav Hajek <highegg@gmail.com>
parents:
10113
diff
changeset
|
1583 assign (idx, a); |
4513 | 1584 } |
1585 | |
1586 return *this; | |
1587 } | |
1588 | |
1589 template <class T> | |
1590 Array<T>& | |
5275 | 1591 Array<T>::insert (const Array<T>& a, const Array<octave_idx_type>& ra_idx) |
4513 | 1592 { |
5275 | 1593 octave_idx_type n = ra_idx.length (); |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
1594 Array<idx_vector> idx (dim_vector (n, 1)); |
10115
ed49cef7e005
simplify Array::insert methods
Jaroslav Hajek <highegg@gmail.com>
parents:
10113
diff
changeset
|
1595 const dim_vector dva = a.dims ().redim (n); |
ed49cef7e005
simplify Array::insert methods
Jaroslav Hajek <highegg@gmail.com>
parents:
10113
diff
changeset
|
1596 for (octave_idx_type k = 0; k < n; k++) |
ed49cef7e005
simplify Array::insert methods
Jaroslav Hajek <highegg@gmail.com>
parents:
10113
diff
changeset
|
1597 idx(k) = idx_vector (ra_idx (k), ra_idx (k) + dva(k)); |
ed49cef7e005
simplify Array::insert methods
Jaroslav Hajek <highegg@gmail.com>
parents:
10113
diff
changeset
|
1598 |
ed49cef7e005
simplify Array::insert methods
Jaroslav Hajek <highegg@gmail.com>
parents:
10113
diff
changeset
|
1599 assign (idx, a); |
4513 | 1600 |
1601 return *this; | |
1602 } | |
1603 | |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
1604 |
4513 | 1605 template <class T> |
1606 Array<T> | |
1607 Array<T>::transpose (void) const | |
1608 { | |
4548 | 1609 assert (ndims () == 2); |
1610 | |
5275 | 1611 octave_idx_type nr = dim1 (); |
1612 octave_idx_type nc = dim2 (); | |
4513 | 1613 |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1614 if (nr >= 8 && nc >= 8) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1615 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1616 Array<T> result (dim_vector (nc, nr)); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1617 |
9121
bb62bc406ea7
reuse fast blocked transpose implementation from rec_permute_helper in Array<T>::transpose
Jaroslav Hajek <highegg@gmail.com>
parents:
9100
diff
changeset
|
1618 // 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
|
1619 |
bb62bc406ea7
reuse fast blocked transpose implementation from rec_permute_helper in Array<T>::transpose
Jaroslav Hajek <highegg@gmail.com>
parents:
9100
diff
changeset
|
1620 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
|
1621 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1622 return result; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1623 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1624 else if (nr > 1 && nc > 1) |
4513 | 1625 { |
1626 Array<T> result (dim_vector (nc, nr)); | |
1627 | |
5275 | 1628 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
|
1629 for (octave_idx_type i = 0; i < nr; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1630 result.xelem (j, i) = xelem (i, j); |
4513 | 1631 |
1632 return result; | |
1633 } | |
1634 else | |
1635 { | |
8333 | 1636 // Fast transpose for vectors and empty matrices. |
4513 | 1637 return Array<T> (*this, dim_vector (nc, nr)); |
1638 } | |
1639 } | |
1640 | |
1641 template <class T> | |
8028
f0fbf47c914c
avoid null pointer call in Array<T>::hermitian
Jaroslav Hajek <highegg@gmail.com>
parents:
7796
diff
changeset
|
1642 static T |
f0fbf47c914c
avoid null pointer call in Array<T>::hermitian
Jaroslav Hajek <highegg@gmail.com>
parents:
7796
diff
changeset
|
1643 no_op_fcn (const T& x) |
f0fbf47c914c
avoid null pointer call in Array<T>::hermitian
Jaroslav Hajek <highegg@gmail.com>
parents:
7796
diff
changeset
|
1644 { |
f0fbf47c914c
avoid null pointer call in Array<T>::hermitian
Jaroslav Hajek <highegg@gmail.com>
parents:
7796
diff
changeset
|
1645 return x; |
f0fbf47c914c
avoid null pointer call in Array<T>::hermitian
Jaroslav Hajek <highegg@gmail.com>
parents:
7796
diff
changeset
|
1646 } |
f0fbf47c914c
avoid null pointer call in Array<T>::hermitian
Jaroslav Hajek <highegg@gmail.com>
parents:
7796
diff
changeset
|
1647 |
f0fbf47c914c
avoid null pointer call in Array<T>::hermitian
Jaroslav Hajek <highegg@gmail.com>
parents:
7796
diff
changeset
|
1648 template <class T> |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1649 Array<T> |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1650 Array<T>::hermitian (T (*fcn) (const T&)) const |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1651 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1652 assert (ndims () == 2); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1653 |
8028
f0fbf47c914c
avoid null pointer call in Array<T>::hermitian
Jaroslav Hajek <highegg@gmail.com>
parents:
7796
diff
changeset
|
1654 if (! fcn) |
f0fbf47c914c
avoid null pointer call in Array<T>::hermitian
Jaroslav Hajek <highegg@gmail.com>
parents:
7796
diff
changeset
|
1655 fcn = no_op_fcn<T>; |
f0fbf47c914c
avoid null pointer call in Array<T>::hermitian
Jaroslav Hajek <highegg@gmail.com>
parents:
7796
diff
changeset
|
1656 |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1657 octave_idx_type nr = dim1 (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1658 octave_idx_type nc = dim2 (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1659 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1660 if (nr >= 8 && nc >= 8) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1661 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1662 Array<T> result (dim_vector (nc, nr)); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1663 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1664 // Blocked transpose to attempt to avoid cache misses. |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1665 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1666 // 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
|
1667 // on some compilers. |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1668 T buf[64]; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1669 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1670 octave_idx_type ii = 0, jj; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1671 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
|
1672 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1673 for (ii = 0; ii < (nr - 8 + 1); ii += 8) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1674 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1675 // Copy to buffer |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1676 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
|
1677 j < jj + 8; j++, idxj += nr) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1678 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
|
1679 buf[k++] = xelem (i + idxj); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1680 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1681 // Copy from buffer |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1682 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
|
1683 i++, idxi += nc) |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1684 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
|
1685 j++, k+=8) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1686 result.xelem (j + idxi) = fcn (buf[k]); |
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 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1689 if (ii < nr) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1690 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
|
1691 for (octave_idx_type i = ii; i < nr; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1692 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
|
1693 } |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1694 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1695 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
|
1696 for (octave_idx_type i = 0; i < nr; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1697 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
|
1698 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1699 return result; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1700 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1701 else |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1702 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1703 Array<T> result (dim_vector (nc, nr)); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1704 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1705 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
|
1706 for (octave_idx_type i = 0; i < nr; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1707 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
|
1708 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1709 return result; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1710 } |
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 |
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 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1715 %% 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
|
1716 %% and with four tiles. |
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 %!shared m7, mt7, m8, mt8, m9, mt9 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1719 %! 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
|
1720 %! 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
|
1721 %! 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
|
1722 %! mt8 = mt8 = [mt7; 57:64]; |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1723 %! 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
|
1724 %! mt9 = [mt8; 65:72]; |
762801c50b21
Fix tests for transpose in Array.cc
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
1725 |
14427
d07e989686b0
Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14158
diff
changeset
|
1726 %!assert (m7', mt7) |
d07e989686b0
Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14158
diff
changeset
|
1727 %!assert ((1i*m7).', 1i * mt7) |
d07e989686b0
Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14158
diff
changeset
|
1728 %!assert ((1i*m7)', conj (1i * mt7)) |
d07e989686b0
Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14158
diff
changeset
|
1729 %!assert (m8', mt8) |
d07e989686b0
Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14158
diff
changeset
|
1730 %!assert ((1i*m8).', 1i * mt8) |
d07e989686b0
Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14158
diff
changeset
|
1731 %!assert ((1i*m8)', conj (1i * mt8)) |
d07e989686b0
Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14158
diff
changeset
|
1732 %!assert (m9', mt9) |
d07e989686b0
Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14158
diff
changeset
|
1733 %!assert ((1i*m9).', 1i * mt9) |
d07e989686b0
Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14158
diff
changeset
|
1734 %!assert ((1i*m9)', conj (1i * mt9)) |
d07e989686b0
Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14158
diff
changeset
|
1735 %!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
|
1736 %!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
|
1737 %!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
|
1738 %!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
|
1739 %!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
|
1740 %!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
|
1741 %!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
|
1742 %!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
|
1743 %!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
|
1744 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1745 */ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1746 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7717
diff
changeset
|
1747 template <class T> |
4513 | 1748 T * |
1749 Array<T>::fortran_vec (void) | |
1750 { | |
6881 | 1751 make_unique (); |
1752 | |
8523
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
1753 return slice_data; |
4513 | 1754 } |
1755 | |
8700 | 1756 // Non-real types don't have NaNs. |
4517 | 1757 template <class T> |
8725 | 1758 inline bool |
1759 sort_isnan (typename ref_param<T>::type) | |
7433 | 1760 { |
8700 | 1761 return false; |
7433 | 1762 } |
1763 | |
1764 template <class T> | |
1765 Array<T> | |
9725 | 1766 Array<T>::sort (int dim, sortmode mode) const |
7433 | 1767 { |
10703
5eb420d92307
fix sort and nth_element when trailing singleton dim is specified
Jaroslav Hajek <highegg@gmail.com>
parents:
10681
diff
changeset
|
1768 if (dim < 0) |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
1769 { |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
1770 (*current_liboctave_error_handler) |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
1771 ("sort: invalid dimension"); |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
1772 return Array<T> (); |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
1773 } |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
1774 |
8651
ea8e65ca234f
reduce memory usage in sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
1775 Array<T> m (dims ()); |
7433 | 1776 |
1777 dim_vector dv = m.dims (); | |
1778 | |
1779 if (m.length () < 1) | |
1780 return m; | |
1781 | |
10703
5eb420d92307
fix sort and nth_element when trailing singleton dim is specified
Jaroslav Hajek <highegg@gmail.com>
parents:
10681
diff
changeset
|
1782 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
|
1783 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
|
1784 |
7433 | 1785 octave_idx_type ns = dv(dim); |
1786 octave_idx_type iter = dv.numel () / ns; | |
1787 octave_idx_type stride = 1; | |
8505 | 1788 |
7433 | 1789 for (int i = 0; i < dim; i++) |
1790 stride *= dv(i); | |
1791 | |
1792 T *v = m.fortran_vec (); | |
8651
ea8e65ca234f
reduce memory usage in sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
1793 const T *ov = data (); |
ea8e65ca234f
reduce memory usage in sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
1794 |
7433 | 1795 octave_sort<T> lsort; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1796 |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1797 if (mode != UNSORTED) |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
1798 lsort.set_compare (mode); |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7443
diff
changeset
|
1799 else |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
1800 return m; |
7433 | 1801 |
1802 if (stride == 1) | |
1803 { | |
1804 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
|
1805 { |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1806 // copy and partition out NaNs. |
8700 | 1807 // 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
|
1808 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
|
1809 octave_idx_type ku = ns; |
8700 | 1810 for (octave_idx_type i = 0; i < ns; i++) |
1811 { | |
1812 T tmp = ov[i]; | |
8725 | 1813 if (sort_isnan<T> (tmp)) |
8700 | 1814 v[--ku] = tmp; |
1815 else | |
1816 v[kl++] = tmp; | |
1817 } | |
1818 | |
1819 // sort. | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1820 lsort.sort (v, kl); |
8700 | 1821 |
1822 if (ku < ns) | |
1823 { | |
1824 // NaNs are in reverse order | |
1825 std::reverse (v + ku, v + ns); | |
1826 if (mode == DESCENDING) | |
1827 std::rotate (v, v + ku, v + ns); | |
1828 } | |
1829 | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1830 v += ns; |
8651
ea8e65ca234f
reduce memory usage in sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
1831 ov += ns; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1832 } |
7433 | 1833 } |
1834 else | |
1835 { | |
8700 | 1836 OCTAVE_LOCAL_BUFFER (T, buf, ns); |
7433 | 1837 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1838 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
|
1839 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1840 octave_idx_type offset = j; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1841 octave_idx_type offset2 = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1842 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1843 while (offset >= stride) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1844 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1845 offset -= stride; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1846 offset2++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1847 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1848 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1849 offset += offset2 * stride * ns; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1850 |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1851 // gather and partition out NaNs. |
8700 | 1852 // 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
|
1853 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
|
1854 octave_idx_type ku = ns; |
8700 | 1855 for (octave_idx_type i = 0; i < ns; i++) |
1856 { | |
1857 T tmp = ov[i*stride + offset]; | |
8725 | 1858 if (sort_isnan<T> (tmp)) |
8700 | 1859 buf[--ku] = tmp; |
1860 else | |
1861 buf[kl++] = tmp; | |
1862 } | |
7433 | 1863 |
8700 | 1864 // sort. |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1865 lsort.sort (buf, kl); |
8700 | 1866 |
1867 if (ku < ns) | |
1868 { | |
1869 // NaNs are in reverse order | |
1870 std::reverse (buf + ku, buf + ns); | |
1871 if (mode == DESCENDING) | |
1872 std::rotate (buf, buf + ku, buf + ns); | |
1873 } | |
1874 | |
1875 // scatter. | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1876 for (octave_idx_type i = 0; i < ns; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1877 v[i*stride + offset] = buf[i]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1878 } |
7433 | 1879 } |
1880 | |
1881 return m; | |
1882 } | |
1883 | |
1884 template <class T> | |
1885 Array<T> | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1886 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
|
1887 sortmode mode) const |
7433 | 1888 { |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
1889 if (dim < 0 || dim >= ndims ()) |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
1890 { |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
1891 (*current_liboctave_error_handler) |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
1892 ("sort: invalid dimension"); |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
1893 return Array<T> (); |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
1894 } |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
1895 |
8651
ea8e65ca234f
reduce memory usage in sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
1896 Array<T> m (dims ()); |
7433 | 1897 |
1898 dim_vector dv = m.dims (); | |
1899 | |
1900 if (m.length () < 1) | |
1901 { | |
1902 sidx = Array<octave_idx_type> (dv); | |
1903 return m; | |
1904 } | |
1905 | |
1906 octave_idx_type ns = dv(dim); | |
1907 octave_idx_type iter = dv.numel () / ns; | |
1908 octave_idx_type stride = 1; | |
8505 | 1909 |
7433 | 1910 for (int i = 0; i < dim; i++) |
1911 stride *= dv(i); | |
1912 | |
1913 T *v = m.fortran_vec (); | |
8651
ea8e65ca234f
reduce memory usage in sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
1914 const T *ov = data (); |
ea8e65ca234f
reduce memory usage in sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
1915 |
8700 | 1916 octave_sort<T> lsort; |
7433 | 1917 |
8700 | 1918 sidx = Array<octave_idx_type> (dv); |
1919 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
|
1920 |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1921 if (mode != UNSORTED) |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
1922 lsort.set_compare (mode); |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7443
diff
changeset
|
1923 else |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
1924 return m; |
7433 | 1925 |
1926 if (stride == 1) | |
1927 { | |
1928 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
|
1929 { |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1930 // copy and partition out NaNs. |
8700 | 1931 // 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
|
1932 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
|
1933 octave_idx_type ku = ns; |
8700 | 1934 for (octave_idx_type i = 0; i < ns; i++) |
1935 { | |
1936 T tmp = ov[i]; | |
8725 | 1937 if (sort_isnan<T> (tmp)) |
8700 | 1938 { |
1939 --ku; | |
1940 v[ku] = tmp; | |
1941 vi[ku] = i; | |
1942 } | |
1943 else | |
1944 { | |
1945 v[kl] = tmp; | |
1946 vi[kl] = i; | |
1947 kl++; | |
1948 } | |
1949 } | |
7433 | 1950 |
8700 | 1951 // sort. |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1952 lsort.sort (v, vi, kl); |
8700 | 1953 |
1954 if (ku < ns) | |
1955 { | |
1956 // NaNs are in reverse order | |
1957 std::reverse (v + ku, v + ns); | |
1958 std::reverse (vi + ku, vi + ns); | |
1959 if (mode == DESCENDING) | |
1960 { | |
1961 std::rotate (v, v + ku, v + ns); | |
1962 std::rotate (vi, vi + ku, vi + ns); | |
1963 } | |
1964 } | |
1965 | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1966 v += ns; |
8700 | 1967 vi += ns; |
8651
ea8e65ca234f
reduce memory usage in sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
1968 ov += ns; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1969 } |
7433 | 1970 } |
1971 else | |
1972 { | |
8700 | 1973 OCTAVE_LOCAL_BUFFER (T, buf, ns); |
1974 OCTAVE_LOCAL_BUFFER (octave_idx_type, bufi, ns); | |
8651
ea8e65ca234f
reduce memory usage in sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
1975 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1976 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
|
1977 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1978 octave_idx_type offset = j; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1979 octave_idx_type offset2 = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1980 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1981 while (offset >= stride) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1982 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1983 offset -= stride; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1984 offset2++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1985 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1986 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
1987 offset += offset2 * stride * ns; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1988 |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1989 // gather and partition out NaNs. |
8700 | 1990 // 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
|
1991 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
|
1992 octave_idx_type ku = ns; |
8700 | 1993 for (octave_idx_type i = 0; i < ns; i++) |
1994 { | |
1995 T tmp = ov[i*stride + offset]; | |
8725 | 1996 if (sort_isnan<T> (tmp)) |
8700 | 1997 { |
1998 --ku; | |
1999 buf[ku] = tmp; | |
2000 bufi[ku] = i; | |
2001 } | |
2002 else | |
2003 { | |
2004 buf[kl] = tmp; | |
2005 bufi[kl] = i; | |
2006 kl++; | |
2007 } | |
2008 } | |
7433 | 2009 |
8700 | 2010 // sort. |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2011 lsort.sort (buf, bufi, kl); |
8700 | 2012 |
2013 if (ku < ns) | |
2014 { | |
2015 // NaNs are in reverse order | |
2016 std::reverse (buf + ku, buf + ns); | |
2017 std::reverse (bufi + ku, bufi + ns); | |
2018 if (mode == DESCENDING) | |
2019 { | |
2020 std::rotate (buf, buf + ku, buf + ns); | |
2021 std::rotate (bufi, bufi + ku, bufi + ns); | |
2022 } | |
2023 } | |
2024 | |
2025 // scatter. | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2026 for (octave_idx_type i = 0; i < ns; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2027 v[i*stride + offset] = buf[i]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2028 for (octave_idx_type i = 0; i < ns; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2029 vi[i*stride + offset] = bufi[i]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2030 } |
7433 | 2031 } |
2032 | |
2033 return m; | |
2034 } | |
2035 | |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2036 template <class T> |
8725 | 2037 typename Array<T>::compare_fcn_type |
9920
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2038 safe_comparator (sortmode mode, const Array<T>& /* a */, |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2039 bool /* allow_chk */) |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2040 { |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2041 if (mode == ASCENDING) |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2042 return octave_sort<T>::ascending_compare; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2043 else if (mode == DESCENDING) |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2044 return octave_sort<T>::descending_compare; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2045 else |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2046 return 0; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2047 } |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2048 |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2049 template <class T> |
9920
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2050 sortmode |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2051 Array<T>::is_sorted (sortmode mode) const |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2052 { |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2053 octave_sort<T> lsort; |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2054 |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2055 octave_idx_type n = numel (); |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2056 |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2057 if (n <= 1) |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2058 return mode ? mode : ASCENDING; |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2059 |
11210
b79924abf776
Array.cc: use comparisons to sortmode values instead of testing mode directly
John W. Eaton <jwe@octave.org>
parents:
10716
diff
changeset
|
2060 if (mode == UNSORTED) |
9920
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2061 { |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2062 // Auto-detect mode. |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2063 compare_fcn_type compare |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2064 = safe_comparator (ASCENDING, *this, false); |
9920
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 if (compare (elem (n-1), elem (0))) |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2067 mode = DESCENDING; |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2068 else |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2069 mode = ASCENDING; |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2070 } |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2071 |
11210
b79924abf776
Array.cc: use comparisons to sortmode values instead of testing mode directly
John W. Eaton <jwe@octave.org>
parents:
10716
diff
changeset
|
2072 if (mode != UNSORTED) |
9920
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2073 { |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2074 lsort.set_compare (safe_comparator (mode, *this, false)); |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2075 |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2076 if (! lsort.is_sorted (data (), n)) |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2077 mode = UNSORTED; |
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 |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2080 return mode; |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2081 |
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2082 } |
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 template <class T> |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2085 Array<octave_idx_type> |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2086 Array<T>::sort_rows_idx (sortmode mode) const |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2087 { |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2088 Array<octave_idx_type> idx; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2089 |
9921
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2090 octave_sort<T> lsort (safe_comparator (mode, *this, true)); |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2091 |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
2092 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
|
2093 octave_idx_type c = cols (); |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2094 |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
2095 idx = Array<octave_idx_type> (dim_vector (r, 1)); |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2096 |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2097 lsort.sort_rows (data (), idx.fortran_vec (), r, c); |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2098 |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2099 return idx; |
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 |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2102 |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2103 template <class T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
2104 sortmode |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2105 Array<T>::is_sorted_rows (sortmode mode) const |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2106 { |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2107 octave_sort<T> lsort; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2108 |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
2109 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
|
2110 octave_idx_type c = cols (); |
8721
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 if (r <= 1 || c == 0) |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2113 return mode ? mode : ASCENDING; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2114 |
11210
b79924abf776
Array.cc: use comparisons to sortmode values instead of testing mode directly
John W. Eaton <jwe@octave.org>
parents:
10716
diff
changeset
|
2115 if (mode == UNSORTED) |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2116 { |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2117 // Auto-detect mode. |
8725 | 2118 compare_fcn_type compare |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2119 = safe_comparator (ASCENDING, *this, false); |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2120 |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2121 octave_idx_type i; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2122 for (i = 0; i < cols (); i++) |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2123 { |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
2124 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
|
2125 T u = elem (rows () - 1, i); |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2126 if (compare (l, u)) |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2127 { |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2128 if (mode == DESCENDING) |
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 mode = UNSORTED; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2131 break; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2132 } |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2133 else |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2134 mode = ASCENDING; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2135 } |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2136 else if (compare (u, l)) |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2137 { |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2138 if (mode == ASCENDING) |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2139 { |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2140 mode = UNSORTED; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2141 break; |
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 else |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2144 mode = DESCENDING; |
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 } |
11210
b79924abf776
Array.cc: use comparisons to sortmode values instead of testing mode directly
John W. Eaton <jwe@octave.org>
parents:
10716
diff
changeset
|
2147 if (mode == UNSORTED && i == cols ()) |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2148 mode = ASCENDING; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2149 } |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2150 |
11210
b79924abf776
Array.cc: use comparisons to sortmode values instead of testing mode directly
John W. Eaton <jwe@octave.org>
parents:
10716
diff
changeset
|
2151 if (mode != UNSORTED) |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2152 { |
9920
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2153 lsort.set_compare (safe_comparator (mode, *this, false)); |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2154 |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2155 if (! lsort.is_sorted_rows (data (), r, c)) |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2156 mode = UNSORTED; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2157 } |
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 return mode; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2160 |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2161 } |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2162 |
8814
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2163 // Do a binary lookup in a sorted array. |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2164 template <class T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
2165 octave_idx_type |
8814
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2166 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
|
2167 { |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2168 octave_idx_type n = numel (); |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2169 octave_sort<T> lsort; |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2170 |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2171 if (mode == UNSORTED) |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2172 { |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2173 // auto-detect mode |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2174 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
|
2175 mode = DESCENDING; |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2176 else |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2177 mode = ASCENDING; |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2178 } |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2179 |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2180 lsort.set_compare (mode); |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2181 |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2182 return lsort.lookup (data (), n, value); |
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 |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2185 template <class T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
2186 Array<octave_idx_type> |
9921
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2187 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
|
2188 { |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
2189 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
|
2190 octave_idx_type nval = values.numel (); |
8814
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2191 octave_sort<T> lsort; |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2192 Array<octave_idx_type> idx (values.dims ()); |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2193 |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2194 if (mode == UNSORTED) |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2195 { |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2196 // auto-detect mode |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2197 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
|
2198 mode = DESCENDING; |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2199 else |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2200 mode = ASCENDING; |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2201 } |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2202 |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2203 lsort.set_compare (mode); |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2204 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
2205 // 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
|
2206 // algorithms. |
9921
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2207 static const double ratio = 1.0; |
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2208 sortmode vmode = UNSORTED; |
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2209 |
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2210 // 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
|
2211 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
|
2212 { |
9921
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2213 vmode = values.is_sorted (); |
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2214 // The table must not contain a NaN. |
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2215 if ((vmode == ASCENDING && sort_isnan<T> (values(nval-1))) |
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2216 || (vmode == DESCENDING && sort_isnan<T> (values(0)))) |
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2217 vmode = UNSORTED; |
8814
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2218 } |
9921
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2219 |
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2220 if (vmode != UNSORTED) |
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2221 lsort.lookup_sorted (data (), n, values.data (), nval, |
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2222 idx.fortran_vec (), vmode != mode); |
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2223 else |
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2224 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
|
2225 |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2226 return idx; |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2227 } |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2228 |
9025 | 2229 template <class T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
2230 octave_idx_type |
9878
ead4f9c82a9a
implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
2231 Array<T>::nnz (void) const |
ead4f9c82a9a
implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
2232 { |
ead4f9c82a9a
implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
2233 const T *src = data (); |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
2234 octave_idx_type nel = nelem (); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
2235 octave_idx_type retval = 0; |
9878
ead4f9c82a9a
implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
2236 const T zero = T (); |
ead4f9c82a9a
implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
2237 for (octave_idx_type i = 0; i < nel; i++) |
ead4f9c82a9a
implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
2238 if (src[i] != zero) |
ead4f9c82a9a
implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
2239 retval++; |
ead4f9c82a9a
implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
2240 |
ead4f9c82a9a
implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
2241 return retval; |
ead4f9c82a9a
implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
2242 } |
ead4f9c82a9a
implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
2243 |
ead4f9c82a9a
implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
2244 template <class T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
2245 Array<octave_idx_type> |
9025 | 2246 Array<T>::find (octave_idx_type n, bool backward) const |
2247 { | |
2248 Array<octave_idx_type> retval; | |
2249 const T *src = data (); | |
2250 octave_idx_type nel = nelem (); | |
2251 const T zero = T (); | |
9310 | 2252 if (n < 0 || n >= nel) |
9025 | 2253 { |
2254 // We want all elements, which means we'll almost surely need | |
2255 // to resize. So count first, then allocate array of exact size. | |
2256 octave_idx_type cnt = 0; | |
2257 for (octave_idx_type i = 0; i < nel; i++) | |
2258 cnt += src[i] != zero; | |
2259 | |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
2260 retval.clear (cnt, 1); |
9025 | 2261 octave_idx_type *dest = retval.fortran_vec (); |
2262 for (octave_idx_type i = 0; i < nel; i++) | |
2263 if (src[i] != zero) *dest++ = i; | |
2264 } | |
2265 else | |
2266 { | |
2267 // We want a fixed max number of elements, usually small. So be | |
2268 // optimistic, alloc the array in advance, and then resize if | |
2269 // needed. | |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
2270 retval.clear (n, 1); |
9025 | 2271 if (backward) |
2272 { | |
2273 // 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
|
2274 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
|
2275 octave_idx_type l = nel - 1; |
9025 | 2276 for (; k < n; k++) |
2277 { | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
2278 for (; l >= 0 && src[l] == zero; l--) ; |
9025 | 2279 if (l >= 0) |
2280 retval(k) = l--; | |
2281 else | |
2282 break; | |
2283 } | |
2284 if (k < n) | |
11574
a83bad07f7e3
attempt better backward compatibility for Array resize functions
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
2285 retval.resize2 (k, 1); |
9310 | 2286 octave_idx_type *rdata = retval.fortran_vec (); |
2287 std::reverse (rdata, rdata + k); | |
9025 | 2288 } |
2289 else | |
2290 { | |
2291 // 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
|
2292 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
|
2293 octave_idx_type l = 0; |
9025 | 2294 for (; k < n; k++) |
2295 { | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
2296 for (; l != nel && src[l] == zero; l++) ; |
9029
2df28ad88b0e
small fix in Array::find
Jaroslav Hajek <highegg@gmail.com>
parents:
9025
diff
changeset
|
2297 if (l != nel) |
9025 | 2298 retval(k) = l++; |
2299 else | |
2300 break; | |
2301 } | |
2302 if (k < n) | |
11574
a83bad07f7e3
attempt better backward compatibility for Array resize functions
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
2303 retval.resize2 (k, 1); |
9025 | 2304 } |
2305 } | |
2306 | |
9046
88bf56bbccca
make Array::find already return Matlab-compatible dimensions
Jaroslav Hajek <highegg@gmail.com>
parents:
9029
diff
changeset
|
2307 // 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
|
2308 // 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
|
2309 // 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
|
2310 // 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
|
2311 // 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
|
2312 // 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
|
2313 // 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
|
2314 // 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
|
2315 |
88bf56bbccca
make Array::find already return Matlab-compatible dimensions
Jaroslav Hajek <highegg@gmail.com>
parents:
9029
diff
changeset
|
2316 if ((numel () == 1 && retval.is_empty ()) |
88bf56bbccca
make Array::find already return Matlab-compatible dimensions
Jaroslav Hajek <highegg@gmail.com>
parents:
9029
diff
changeset
|
2317 || (rows () == 0 && dims ().numel (1) == 0)) |
88bf56bbccca
make Array::find already return Matlab-compatible dimensions
Jaroslav Hajek <highegg@gmail.com>
parents:
9029
diff
changeset
|
2318 retval.dimensions = dim_vector (); |
88bf56bbccca
make Array::find already return Matlab-compatible dimensions
Jaroslav Hajek <highegg@gmail.com>
parents:
9029
diff
changeset
|
2319 else if (rows () == 1 && ndims () == 2) |
88bf56bbccca
make Array::find already return Matlab-compatible dimensions
Jaroslav Hajek <highegg@gmail.com>
parents:
9029
diff
changeset
|
2320 retval.dimensions = dim_vector (1, retval.length ()); |
88bf56bbccca
make Array::find already return Matlab-compatible dimensions
Jaroslav Hajek <highegg@gmail.com>
parents:
9029
diff
changeset
|
2321 |
9025 | 2322 return retval; |
2323 } | |
2324 | |
9725 | 2325 template <class T> |
2326 Array<T> | |
2327 Array<T>::nth_element (const idx_vector& n, int dim) const | |
2328 { | |
10703
5eb420d92307
fix sort and nth_element when trailing singleton dim is specified
Jaroslav Hajek <highegg@gmail.com>
parents:
10681
diff
changeset
|
2329 if (dim < 0) |
9725 | 2330 { |
2331 (*current_liboctave_error_handler) | |
2332 ("nth_element: invalid dimension"); | |
2333 return Array<T> (); | |
2334 } | |
2335 | |
2336 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
|
2337 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
|
2338 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
|
2339 |
9725 | 2340 octave_idx_type ns = dv(dim); |
2341 | |
2342 octave_idx_type nn = n.length (ns); | |
2343 | |
2344 dv(dim) = std::min (nn, ns); | |
2345 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
|
2346 dim = std::min (dv.length (), dim); |
9725 | 2347 |
2348 Array<T> m (dv); | |
2349 | |
2350 if (m.numel () == 0) | |
2351 return m; | |
2352 | |
2353 sortmode mode = UNSORTED; | |
2354 octave_idx_type lo = 0; | |
2355 | |
2356 switch (n.idx_class ()) | |
2357 { | |
2358 case idx_vector::class_scalar: | |
2359 mode = ASCENDING; | |
2360 lo = n(0); | |
2361 break; | |
2362 case idx_vector::class_range: | |
2363 { | |
2364 octave_idx_type inc = n.increment (); | |
2365 if (inc == 1) | |
2366 { | |
2367 mode = ASCENDING; | |
2368 lo = n(0); | |
2369 } | |
2370 else if (inc == -1) | |
2371 { | |
2372 mode = DESCENDING; | |
2373 lo = ns - 1 - n(0); | |
2374 } | |
2375 } | |
2376 default: | |
2377 break; | |
2378 } | |
2379 | |
2380 if (mode == UNSORTED) | |
2381 { | |
2382 (*current_liboctave_error_handler) | |
2383 ("nth_element: n must be a scalar or a contiguous range"); | |
2384 return Array<T> (); | |
2385 } | |
2386 | |
2387 octave_idx_type up = lo + nn; | |
2388 | |
2389 if (lo < 0 || up > ns) | |
2390 { | |
2391 (*current_liboctave_error_handler) | |
2392 ("nth_element: invalid element index"); | |
2393 return Array<T> (); | |
2394 } | |
2395 | |
2396 octave_idx_type iter = numel () / ns; | |
2397 octave_idx_type stride = 1; | |
2398 | |
2399 for (int i = 0; i < dim; i++) | |
2400 stride *= dv(i); | |
2401 | |
2402 T *v = m.fortran_vec (); | |
2403 const T *ov = data (); | |
2404 | |
2405 OCTAVE_LOCAL_BUFFER (T, buf, ns); | |
2406 | |
2407 octave_sort<T> lsort; | |
2408 lsort.set_compare (mode); | |
2409 | |
2410 for (octave_idx_type j = 0; j < iter; j++) | |
2411 { | |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
2412 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
|
2413 octave_idx_type ku = ns; |
9725 | 2414 |
2415 if (stride == 1) | |
2416 { | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
2417 // copy without NaNs. |
9725 | 2418 // FIXME: impact on integer types noticeable? |
2419 for (octave_idx_type i = 0; i < ns; i++) | |
2420 { | |
2421 T tmp = ov[i]; | |
2422 if (sort_isnan<T> (tmp)) | |
2423 buf[--ku] = tmp; | |
2424 else | |
2425 buf[kl++] = tmp; | |
2426 } | |
2427 | |
2428 ov += ns; | |
2429 } | |
2430 else | |
2431 { | |
2432 octave_idx_type offset = j % stride; | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
2433 // copy without NaNs. |
9725 | 2434 // FIXME: impact on integer types noticeable? |
2435 for (octave_idx_type i = 0; i < ns; i++) | |
2436 { | |
2437 T tmp = ov[offset + i*stride]; | |
2438 if (sort_isnan<T> (tmp)) | |
2439 buf[--ku] = tmp; | |
2440 else | |
2441 buf[kl++] = tmp; | |
2442 } | |
2443 | |
2444 if (offset == stride-1) | |
2445 ov += ns*stride; | |
2446 } | |
2447 | |
2448 if (ku == ns) | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
2449 lsort.nth_element (buf, ns, lo, up); |
9725 | 2450 else if (mode == ASCENDING) |
2451 lsort.nth_element (buf, ku, lo, std::min (ku, up)); | |
2452 else | |
2453 { | |
2454 octave_idx_type nnan = ns - ku; | |
10258 | 2455 octave_idx_type zero = 0; |
2456 lsort.nth_element (buf, ku, std::max (lo - nnan, zero), | |
2457 std::max (up - nnan, zero)); | |
9725 | 2458 std::rotate (buf, buf + ku, buf + ns); |
2459 } | |
2460 | |
2461 if (stride == 1) | |
2462 { | |
2463 for (octave_idx_type i = 0; i < nn; i++) | |
2464 v[i] = buf[lo + i]; | |
2465 | |
2466 v += nn; | |
2467 } | |
2468 else | |
2469 { | |
2470 octave_idx_type offset = j % stride; | |
2471 for (octave_idx_type i = 0; i < nn; i++) | |
2472 v[offset + stride * i] = buf[lo + i]; | |
2473 if (offset == stride-1) | |
2474 v += nn*stride; | |
2475 } | |
2476 } | |
2477 | |
2478 return m; | |
2479 } | |
2480 | |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2481 #define NO_INSTANTIATE_ARRAY_SORT(T) \ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2482 \ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2483 template <> Array<T> \ |
9725 | 2484 Array<T>::sort (int, sortmode) const { return *this; } \ |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2485 \ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2486 template <> Array<T> \ |
9725 | 2487 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
|
2488 { sidx = Array<octave_idx_type> (); return *this; } \ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2489 \ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2490 template <> sortmode \ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2491 Array<T>::is_sorted (sortmode) const \ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2492 { return UNSORTED; } \ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2493 \ |
8725 | 2494 Array<T>::compare_fcn_type \ |
9920
56fbe170d354
fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents:
9886
diff
changeset
|
2495 safe_comparator (sortmode, const Array<T>&, bool) \ |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2496 { return 0; } \ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2497 \ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2498 template <> Array<octave_idx_type> \ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2499 Array<T>::sort_rows_idx (sortmode) const \ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2500 { return Array<octave_idx_type> (); } \ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2501 \ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2502 template <> sortmode \ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2503 Array<T>::is_sorted_rows (sortmode) const \ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2504 { return UNSORTED; } \ |
8814
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2505 \ |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2506 template <> octave_idx_type \ |
9222
7bd406e12e4d
instantiate Array<void *> in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9201
diff
changeset
|
2507 Array<T>::lookup (T const &, sortmode) const \ |
8814
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2508 { return 0; } \ |
de16ebeef93d
improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8799
diff
changeset
|
2509 template <> Array<octave_idx_type> \ |
9921
7c8392a034e6
fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents:
9920
diff
changeset
|
2510 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
|
2511 { return Array<octave_idx_type> (); } \ |
9878
ead4f9c82a9a
implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
2512 \ |
ead4f9c82a9a
implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
2513 template <> octave_idx_type \ |
ead4f9c82a9a
implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
2514 Array<T>::nnz (void) const\ |
ead4f9c82a9a
implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
2515 { return 0; } \ |
9025 | 2516 template <> Array<octave_idx_type> \ |
2517 Array<T>::find (octave_idx_type, bool) const\ | |
2518 { return Array<octave_idx_type> (); } \ | |
9725 | 2519 \ |
2520 template <> Array<T> \ | |
2521 Array<T>::nth_element (const idx_vector&, int) const { return Array<T> (); } \ | |
19790
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17769
diff
changeset
|
2522 |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2523 |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2524 template <class T> |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2525 Array<T> |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2526 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
|
2527 { |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2528 dim_vector dv = dims (); |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2529 octave_idx_type nd = dv.length (); |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2530 Array<T> d; |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2531 |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2532 if (nd > 2) |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
2533 (*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
|
2534 else |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2535 { |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2536 octave_idx_type nnr = dv (0); |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2537 octave_idx_type nnc = dv (1); |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2538 |
12550
ea3a4bf39048
Treat empty vector (1x0 or 0x1) as valid input to diag().
Rik <octave@nomad.inbox5.com>
parents:
12153
diff
changeset
|
2539 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
|
2540 ; // do nothing for empty matrix |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2541 else if (nnr != 1 && nnc != 1) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2542 { |
12550
ea3a4bf39048
Treat empty vector (1x0 or 0x1) as valid input to diag().
Rik <octave@nomad.inbox5.com>
parents:
12153
diff
changeset
|
2543 // Extract diag from matrix |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2544 if (k > 0) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2545 nnc -= k; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2546 else if (k < 0) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2547 nnr += k; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2548 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2549 if (nnr > 0 && nnc > 0) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2550 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2551 octave_idx_type ndiag = (nnr < nnc) ? nnr : nnc; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2552 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2553 d.resize (dim_vector (ndiag, 1)); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2554 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2555 if (k > 0) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2556 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2557 for (octave_idx_type i = 0; i < ndiag; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2558 d.xelem (i) = elem (i, i+k); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2559 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2560 else if (k < 0) |
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 for (octave_idx_type i = 0; i < ndiag; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2563 d.xelem (i) = elem (i-k, i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2564 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2565 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2566 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2567 for (octave_idx_type i = 0; i < ndiag; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2568 d.xelem (i) = elem (i, i); |
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 } |
18145
a86d608c413c
Return empty matrix rather than issuing error when requested diagonal is out of range.
Marco Caliari <marco.caliari@univr.it>
parents:
18098
diff
changeset
|
2571 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:
18098
diff
changeset
|
2572 d.resize (dim_vector (0, 1)); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2573 } |
12550
ea3a4bf39048
Treat empty vector (1x0 or 0x1) as valid input to diag().
Rik <octave@nomad.inbox5.com>
parents:
12153
diff
changeset
|
2574 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2575 { |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12884
diff
changeset
|
2576 // Create diag matrix from vector |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2577 octave_idx_type roff = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2578 octave_idx_type coff = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2579 if (k > 0) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2580 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2581 roff = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2582 coff = k; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2583 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2584 else if (k < 0) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2585 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2586 roff = -k; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2587 coff = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2588 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2589 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2590 if (nnr == 1) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2591 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2592 octave_idx_type n = nnc + std::abs (k); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2593 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
|
2594 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2595 for (octave_idx_type i = 0; i < nnc; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2596 d.xelem (i+roff, i+coff) = elem (0, i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2597 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2598 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2599 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2600 octave_idx_type n = nnr + std::abs (k); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2601 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
|
2602 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2603 for (octave_idx_type i = 0; i < nnr; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2604 d.xelem (i+roff, i+coff) = elem (i, 0); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2605 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2606 } |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2607 } |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2608 |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2609 return d; |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2610 } |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7605
diff
changeset
|
2611 |
4517 | 2612 template <class T> |
10531
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2613 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
|
2614 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
|
2615 { |
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14446
diff
changeset
|
2616 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
|
2617 |
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14446
diff
changeset
|
2618 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
|
2619 { |
14591
6b7e9724912a
Silence warning about deprecated resize() call
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14557
diff
changeset
|
2620 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
|
2621 |
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14446
diff
changeset
|
2622 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
|
2623 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
|
2624 } |
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14446
diff
changeset
|
2625 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
|
2626 (*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
|
2627 ("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
|
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 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
|
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 |
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 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
|
2633 Array<T> |
10531
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2634 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
|
2635 { |
10716
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
2636 // 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
|
2637 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
|
2638 |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
2639 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
|
2640 { |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
2641 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
|
2642 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
|
2643 } |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
2644 else if (dim < 0) |
10531
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2645 (*current_liboctave_error_handler) |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2646 ("cat: invalid dimension"); |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2647 |
10535
3f973f6c841c
improve sparse concatenation operator
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
2648 if (n == 1) |
3f973f6c841c
improve sparse concatenation operator
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
2649 return array_list[0]; |
10681
0ba9bd294421
make cat() (hopefully) more matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10674
diff
changeset
|
2650 else if (n == 0) |
0ba9bd294421
make cat() (hopefully) more matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10674
diff
changeset
|
2651 return Array<T> (); |
0ba9bd294421
make cat() (hopefully) more matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10674
diff
changeset
|
2652 |
11241
80e01d79cf80
special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11210
diff
changeset
|
2653 // Special case: |
80e01d79cf80
special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11210
diff
changeset
|
2654 // |
11242
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2655 // cat (dim, [], ..., [], A, ...) |
11241
80e01d79cf80
special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11210
diff
changeset
|
2656 // |
80e01d79cf80
special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11210
diff
changeset
|
2657 // 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
|
2658 // concatenate is equivalent to |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2659 // |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2660 // cat (dim, A, ...) |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2661 // |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2662 // 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
|
2663 // 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
|
2664 // |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2665 // cat (3, [], [], A) |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2666 // |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2667 // 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
|
2668 // |
80e01d79cf80
special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11210
diff
changeset
|
2669 // cat (3, cat (3, [], []), A) |
80e01d79cf80
special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11210
diff
changeset
|
2670 // 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
|
2671 // |
11242
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2672 // 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
|
2673 |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2674 octave_idx_type istart = 0; |
11241
80e01d79cf80
special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11210
diff
changeset
|
2675 |
80e01d79cf80
special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11210
diff
changeset
|
2676 if (n > 2 && dim > 1) |
80e01d79cf80
special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11210
diff
changeset
|
2677 { |
11242
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2678 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
|
2679 { |
11242
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2680 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
|
2681 |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2682 if (dv.zero_by_zero ()) |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2683 istart++; |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2684 else |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2685 break; |
11241
80e01d79cf80
special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11210
diff
changeset
|
2686 } |
11242
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2687 |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2688 // 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
|
2689 if (istart >= n) |
0090bb47d0b5
simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11241
diff
changeset
|
2690 istart = 0; |
11241
80e01d79cf80
special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents:
11210
diff
changeset
|
2691 } |
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[istart++].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 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
|
2696 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
|
2697 (*current_liboctave_error_handler) |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2698 ("cat: dimension mismatch"); |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2699 |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2700 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
|
2701 |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
2702 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
|
2703 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
|
2704 |
10533
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10531
diff
changeset
|
2705 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
|
2706 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
|
2707 octave_idx_type l = 0; |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2708 |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2709 for (octave_idx_type i = 0; i < n; i++) |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2710 { |
10716
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
2711 // 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
|
2712 // 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
|
2713 // 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
|
2714 // 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
|
2715 // 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
|
2716 // 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
|
2717 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
|
2718 continue; |
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10531
diff
changeset
|
2719 |
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10531
diff
changeset
|
2720 octave_quit (); |
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10531
diff
changeset
|
2721 |
10531
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2722 octave_idx_type u; |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2723 if (dim < array_list[i].ndims ()) |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2724 u = l + array_list[i].dims ()(dim); |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2725 else |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2726 u = l + 1; |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2727 |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2728 idxa(dim) = idx_vector (l, u); |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2729 |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2730 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
|
2731 |
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10531
diff
changeset
|
2732 l = u; |
10531
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2733 } |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2734 |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2735 return retval; |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2736 } |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2737 |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
2738 template <class T> |
4517 | 2739 void |
3933 | 2740 Array<T>::print_info (std::ostream& os, const std::string& prefix) const |
2741 { | |
8523
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
2742 os << prefix << "rep address: " << rep << '\n' |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
2743 << prefix << "rep->len: " << rep->len << '\n' |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
2744 << 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
|
2745 << prefix << "rep->count: " << rep->count << '\n' |
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8505
diff
changeset
|
2746 << 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
|
2747 << prefix << "slice_len: " << slice_len << '\n'; |
4513 | 2748 |
2749 // 2D info: | |
2750 // | |
4657 | 2751 // << pefix << "rows: " << rows () << "\n" |
4513 | 2752 // << prefix << "cols: " << cols () << "\n"; |
3933 | 2753 } |
2754 | |
9201
472f0e22aa60
guard against implicit instantiation
Jaroslav Hajek <highegg@gmail.com>
parents:
9121
diff
changeset
|
2755 template <class T> |
10674
e3064439d6b4
new Array method for internal use
Jaroslav Hajek <highegg@gmail.com>
parents:
10673
diff
changeset
|
2756 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
|
2757 { |
e3064439d6b4
new Array method for internal use
Jaroslav Hajek <highegg@gmail.com>
parents:
10673
diff
changeset
|
2758 bool retval = dimensions == dv; |
e3064439d6b4
new Array method for internal use
Jaroslav Hajek <highegg@gmail.com>
parents:
10673
diff
changeset
|
2759 if (retval) |
e3064439d6b4
new Array method for internal use
Jaroslav Hajek <highegg@gmail.com>
parents:
10673
diff
changeset
|
2760 dimensions = dv; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
2761 |
10674
e3064439d6b4
new Array method for internal use
Jaroslav Hajek <highegg@gmail.com>
parents:
10673
diff
changeset
|
2762 return retval; |
e3064439d6b4
new Array method for internal use
Jaroslav Hajek <highegg@gmail.com>
parents:
10673
diff
changeset
|
2763 } |
e3064439d6b4
new Array method for internal use
Jaroslav Hajek <highegg@gmail.com>
parents:
10673
diff
changeset
|
2764 |
e3064439d6b4
new Array method for internal use
Jaroslav Hajek <highegg@gmail.com>
parents:
10673
diff
changeset
|
2765 template <class T> |
9201
472f0e22aa60
guard against implicit instantiation
Jaroslav Hajek <highegg@gmail.com>
parents:
9121
diff
changeset
|
2766 void Array<T>::instantiation_guard () |
472f0e22aa60
guard against implicit instantiation
Jaroslav Hajek <highegg@gmail.com>
parents:
9121
diff
changeset
|
2767 { |
472f0e22aa60
guard against implicit instantiation
Jaroslav Hajek <highegg@gmail.com>
parents:
9121
diff
changeset
|
2768 // This guards against accidental implicit instantiations. |
472f0e22aa60
guard against implicit instantiation
Jaroslav Hajek <highegg@gmail.com>
parents:
9121
diff
changeset
|
2769 // 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
|
2770 T::__xXxXx__ (); |
9201
472f0e22aa60
guard against implicit instantiation
Jaroslav Hajek <highegg@gmail.com>
parents:
9121
diff
changeset
|
2771 } |
472f0e22aa60
guard against implicit instantiation
Jaroslav Hajek <highegg@gmail.com>
parents:
9121
diff
changeset
|
2772 |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2773 #define INSTANTIATE_ARRAY(T, API) \ |
9201
472f0e22aa60
guard against implicit instantiation
Jaroslav Hajek <highegg@gmail.com>
parents:
9121
diff
changeset
|
2774 template <> void Array<T>::instantiation_guard () { } \ |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2775 template class API Array<T> |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8700
diff
changeset
|
2776 |
9732
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2777 // FIXME: is this used? |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2778 |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2779 template <class T> |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2780 std::ostream& |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2781 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
|
2782 { |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2783 dim_vector a_dims = a.dims (); |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2784 |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2785 int n_dims = a_dims.length (); |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2786 |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2787 os << n_dims << "-dimensional array"; |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2788 |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2789 if (n_dims) |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2790 os << " (" << a_dims.str () << ")"; |
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 os <<"\n\n"; |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2793 |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2794 if (n_dims) |
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 os << "data:"; |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2797 |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
2798 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
|
2799 |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2800 // 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
|
2801 |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2802 octave_idx_type m = 1; |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2803 for (int i = 2; i < n_dims; i++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2804 m *= a_dims(i); |
9732
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2805 |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2806 if (m == 1) |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2807 { |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2808 octave_idx_type rows = 0; |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2809 octave_idx_type cols = 0; |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2810 |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2811 switch (n_dims) |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2812 { |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2813 case 2: |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2814 rows = a_dims(0); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2815 cols = a_dims(1); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2816 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2817 for (octave_idx_type j = 0; j < rows; j++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2818 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2819 ra_idx(0) = j; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2820 for (octave_idx_type k = 0; k < cols; k++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2821 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2822 ra_idx(1) = k; |
15018
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14846
diff
changeset
|
2823 os << " " << a.elem (ra_idx); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2824 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2825 os << "\n"; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2826 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2827 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2828 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2829 default: |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2830 rows = a_dims(0); |
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 for (octave_idx_type k = 0; k < rows; k++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2833 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2834 ra_idx(0) = k; |
15018
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14846
diff
changeset
|
2835 os << " " << a.elem (ra_idx); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2836 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2837 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2838 } |
9732
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2839 |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2840 os << "\n"; |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2841 } |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2842 else |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2843 { |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2844 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
|
2845 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
|
2846 |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2847 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
|
2848 { |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2849 os << "\n(:,:,"; |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2850 |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2851 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
|
2852 os << ra_idx(j) + 1 << ","; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2853 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2854 os << ra_idx(n_dims - 1) + 1 << ") = \n"; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2855 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2856 for (octave_idx_type j = 0; j < rows; j++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2857 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2858 ra_idx(0) = j; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2859 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2860 for (octave_idx_type k = 0; k < cols; k++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2861 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2862 ra_idx(1) = k; |
15018
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14846
diff
changeset
|
2863 os << " " << a.elem (ra_idx); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2864 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2865 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2866 os << "\n"; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2867 } |
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 os << "\n"; |
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 if (i != m - 1) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10270
diff
changeset
|
2872 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
|
2873 } |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2874 } |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2875 } |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2876 |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2877 return os; |
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2878 } |