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