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