Mercurial > hg > octave-lyh
annotate libinterp/octave-value/ov-range.cc @ 17402:b6867a09d7cf
Return correct status code for copyfile, movefile.
* scripts/miscellaneous/copyfile.m, scripts/miscellaneous/movefile.m:
Return correct status (inversion of system function). Put input validation
first. Use single quotes to simplify expressions. Redo docstrings.
author | Rik <rik@octave.org> |
---|---|
date | Sun, 08 Sep 2013 21:11:45 -0700 |
parents | bc924baa2c4e |
children |
rev | line source |
---|---|
2376 | 1 /* |
2 | |
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
14040
diff
changeset
|
3 Copyright (C) 1996-2012 John W. Eaton |
2376 | 4 |
5 This file is part of Octave. | |
6 | |
7 Octave is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
2376 | 11 |
12 Octave is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
2376 | 20 |
21 */ | |
22 | |
23 #ifdef HAVE_CONFIG_H | |
24 #include <config.h> | |
25 #endif | |
26 | |
3503 | 27 #include <iostream> |
2901 | 28 |
2376 | 29 #include "lo-ieee.h" |
30 #include "lo-utils.h" | |
31 | |
10605
1834132fb50b
allow non-integer ranges as indices conditionally
John W. Eaton <jwe@octave.org>
parents:
10315
diff
changeset
|
32 #include "defun.h" |
1834132fb50b
allow non-integer ranges as indices conditionally
John W. Eaton <jwe@octave.org>
parents:
10315
diff
changeset
|
33 #include "variables.h" |
2376 | 34 #include "gripes.h" |
15149
62a35ae7d6a2
use forward decls for mxArray in ov.h and ov-base.h
John W. Eaton <jwe@octave.org>
parents:
15057
diff
changeset
|
35 #include "mxarray.h" |
2376 | 36 #include "ops.h" |
3933 | 37 #include "oct-obj.h" |
2376 | 38 #include "ov-range.h" |
39 #include "ov-re-mat.h" | |
2410 | 40 #include "ov-scalar.h" |
2376 | 41 #include "pr-output.h" |
42 | |
4687 | 43 #include "byte-swap.h" |
8946
e7e928088e90
fix CRLF issues with text-mode reading in windows when loading ascii data
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
8920
diff
changeset
|
44 #include "ls-ascii-helper.h" |
4687 | 45 #include "ls-hdf5.h" |
46 #include "ls-utils.h" | |
47 | |
10613
e103fb2182ce
use internal variable instead of warning state to control whether to allow non-integer ranges as indices
John W. Eaton <jwe@octave.org>
parents:
10609
diff
changeset
|
48 // If TRUE, allow ranges with non-integer elements as array indices. |
e103fb2182ce
use internal variable instead of warning state to control whether to allow non-integer ranges as indices
John W. Eaton <jwe@octave.org>
parents:
10609
diff
changeset
|
49 bool Vallow_noninteger_range_as_index = false; |
e103fb2182ce
use internal variable instead of warning state to control whether to allow non-integer ranges as indices
John W. Eaton <jwe@octave.org>
parents:
10609
diff
changeset
|
50 |
3219 | 51 DEFINE_OCTAVE_ALLOCATOR (octave_range); |
2376 | 52 |
4612 | 53 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_range, "range", "double"); |
2376 | 54 |
5759 | 55 static octave_base_value * |
56 default_numeric_conversion_function (const octave_base_value& a) | |
2376 | 57 { |
58 CAST_CONV_ARG (const octave_range&); | |
59 | |
60 return new octave_matrix (v.matrix_value ()); | |
61 } | |
62 | |
8345
c777f3ce02d8
smarter conversion lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
63 octave_base_value::type_conv_info |
2376 | 64 octave_range::numeric_conversion_function (void) const |
65 { | |
8345
c777f3ce02d8
smarter conversion lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
66 return octave_base_value::type_conv_info (default_numeric_conversion_function, |
c777f3ce02d8
smarter conversion lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
67 octave_matrix::static_type_id ()); |
2376 | 68 } |
69 | |
5759 | 70 octave_base_value * |
2410 | 71 octave_range::try_narrowing_conversion (void) |
72 { | |
5759 | 73 octave_base_value *retval = 0; |
2410 | 74 |
75 switch (range.nelem ()) | |
76 { | |
77 case 1: | |
78 retval = new octave_scalar (range.base ()); | |
79 break; | |
80 | |
81 case 0: | |
4417 | 82 retval = new octave_matrix (Matrix (1, 0)); |
2410 | 83 break; |
84 | |
8971 | 85 case -2: |
86 retval = new octave_matrix (range.matrix_value ()); | |
87 break; | |
88 | |
2410 | 89 default: |
90 break; | |
91 } | |
92 | |
93 return retval; | |
94 } | |
95 | |
2436 | 96 octave_value |
4247 | 97 octave_range::subsref (const std::string& type, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
98 const std::list<octave_value_list>& idx) |
3933 | 99 { |
100 octave_value retval; | |
101 | |
102 switch (type[0]) | |
103 { | |
104 case '(': | |
105 retval = do_index_op (idx.front ()); | |
106 break; | |
107 | |
108 case '{': | |
109 case '.': | |
110 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
111 std::string nm = type_name (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
112 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
3933 | 113 } |
114 break; | |
115 | |
116 default: | |
117 panic_impossible (); | |
118 } | |
119 | |
120 return retval.next_subsref (type, idx); | |
121 } | |
122 | |
123 octave_value | |
5885 | 124 octave_range::do_index_op (const octave_value_list& idx, bool resize_ok) |
2436 | 125 { |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
9892
diff
changeset
|
126 if (idx.length () == 1 && ! resize_ok) |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
9892
diff
changeset
|
127 { |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
9892
diff
changeset
|
128 octave_value retval; |
2436 | 129 |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
9892
diff
changeset
|
130 // The range can handle a single subscript. |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
9892
diff
changeset
|
131 idx_vector i = idx(0).index_vector (); |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
9892
diff
changeset
|
132 if (! error_state) |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
9892
diff
changeset
|
133 { |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
9892
diff
changeset
|
134 if (i.is_scalar () && i(0) < range.nelem ()) |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
9892
diff
changeset
|
135 retval = range.elem (i(0)); |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
9892
diff
changeset
|
136 else |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
9892
diff
changeset
|
137 retval = range.index (i); |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
9892
diff
changeset
|
138 } |
2436 | 139 |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
9892
diff
changeset
|
140 return retval; |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
9892
diff
changeset
|
141 } |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
9892
diff
changeset
|
142 else |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
9892
diff
changeset
|
143 { |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
9892
diff
changeset
|
144 octave_value tmp (new octave_matrix (range.matrix_value ())); |
2436 | 145 |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
9892
diff
changeset
|
146 return tmp.do_index_op (idx, resize_ok); |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
9892
diff
changeset
|
147 } |
2436 | 148 } |
149 | |
10605
1834132fb50b
allow non-integer ranges as indices conditionally
John W. Eaton <jwe@octave.org>
parents:
10315
diff
changeset
|
150 idx_vector |
1834132fb50b
allow non-integer ranges as indices conditionally
John W. Eaton <jwe@octave.org>
parents:
10315
diff
changeset
|
151 octave_range::index_vector (void) const |
1834132fb50b
allow non-integer ranges as indices conditionally
John W. Eaton <jwe@octave.org>
parents:
10315
diff
changeset
|
152 { |
1834132fb50b
allow non-integer ranges as indices conditionally
John W. Eaton <jwe@octave.org>
parents:
10315
diff
changeset
|
153 if (idx_cache) |
1834132fb50b
allow non-integer ranges as indices conditionally
John W. Eaton <jwe@octave.org>
parents:
10315
diff
changeset
|
154 return *idx_cache; |
1834132fb50b
allow non-integer ranges as indices conditionally
John W. Eaton <jwe@octave.org>
parents:
10315
diff
changeset
|
155 else |
1834132fb50b
allow non-integer ranges as indices conditionally
John W. Eaton <jwe@octave.org>
parents:
10315
diff
changeset
|
156 { |
10616
d61caf612f1e
optimize order of conditions in octave_range::index_vector
Jaroslav Hajek <highegg@gmail.com>
parents:
10613
diff
changeset
|
157 if (! Vallow_noninteger_range_as_index |
d61caf612f1e
optimize order of conditions in octave_range::index_vector
Jaroslav Hajek <highegg@gmail.com>
parents:
10613
diff
changeset
|
158 || range.all_elements_are_ints ()) |
10605
1834132fb50b
allow non-integer ranges as indices conditionally
John W. Eaton <jwe@octave.org>
parents:
10315
diff
changeset
|
159 return set_idx_cache (idx_vector (range)); |
1834132fb50b
allow non-integer ranges as indices conditionally
John W. Eaton <jwe@octave.org>
parents:
10315
diff
changeset
|
160 else |
1834132fb50b
allow non-integer ranges as indices conditionally
John W. Eaton <jwe@octave.org>
parents:
10315
diff
changeset
|
161 { |
10613
e103fb2182ce
use internal variable instead of warning state to control whether to allow non-integer ranges as indices
John W. Eaton <jwe@octave.org>
parents:
10609
diff
changeset
|
162 warning_with_id ("Octave:noninteger-range-as-index", |
10609
58bcda68ac11
improve warning/error message
John W. Eaton <jwe@octave.org>
parents:
10605
diff
changeset
|
163 "non-integer range used as index"); |
10605
1834132fb50b
allow non-integer ranges as indices conditionally
John W. Eaton <jwe@octave.org>
parents:
10315
diff
changeset
|
164 |
1834132fb50b
allow non-integer ranges as indices conditionally
John W. Eaton <jwe@octave.org>
parents:
10315
diff
changeset
|
165 return octave_value (matrix_value ()).round ().index_vector (); |
1834132fb50b
allow non-integer ranges as indices conditionally
John W. Eaton <jwe@octave.org>
parents:
10315
diff
changeset
|
166 } |
1834132fb50b
allow non-integer ranges as indices conditionally
John W. Eaton <jwe@octave.org>
parents:
10315
diff
changeset
|
167 } |
1834132fb50b
allow non-integer ranges as indices conditionally
John W. Eaton <jwe@octave.org>
parents:
10315
diff
changeset
|
168 } |
1834132fb50b
allow non-integer ranges as indices conditionally
John W. Eaton <jwe@octave.org>
parents:
10315
diff
changeset
|
169 |
2376 | 170 double |
171 octave_range::double_value (bool) const | |
172 { | |
4102 | 173 double retval = lo_ieee_nan_value (); |
2376 | 174 |
5275 | 175 octave_idx_type nel = range.nelem (); |
2376 | 176 |
4455 | 177 if (nel > 0) |
178 { | |
14469
29aabe9b37a2
Rename array-as-vector, array-as-scalar warning IDs to match documentation (bug #35838)
Rik <octave@nomad.inbox5.com>
parents:
14429
diff
changeset
|
179 gripe_implicit_conversion ("Octave:array-to-scalar", |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
180 "range", "real scalar"); |
4455 | 181 |
182 retval = range.base (); | |
183 } | |
2376 | 184 else |
185 gripe_invalid_conversion ("range", "real scalar"); | |
186 | |
187 return retval; | |
188 } | |
189 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
190 float |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
191 octave_range::float_value (bool) const |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
192 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
193 float retval = lo_ieee_float_nan_value (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
194 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
195 octave_idx_type nel = range.nelem (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
196 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
197 if (nel > 0) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
198 { |
14469
29aabe9b37a2
Rename array-as-vector, array-as-scalar warning IDs to match documentation (bug #35838)
Rik <octave@nomad.inbox5.com>
parents:
14429
diff
changeset
|
199 gripe_implicit_conversion ("Octave:array-to-scalar", |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
200 "range", "real scalar"); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
201 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
202 retval = range.base (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
203 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
204 else |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
205 gripe_invalid_conversion ("range", "real scalar"); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
206 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
207 return retval; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
208 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
209 |
9146
a48c500e48e1
support range->string conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
210 charNDArray |
a48c500e48e1
support range->string conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
211 octave_range::char_array_value (bool) const |
a48c500e48e1
support range->string conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
212 { |
a48c500e48e1
support range->string conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
213 const Matrix matrix = range.matrix_value (); |
a48c500e48e1
support range->string conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
214 charNDArray retval (dims ()); |
a48c500e48e1
support range->string conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
215 |
a48c500e48e1
support range->string conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
216 octave_idx_type nel = numel (); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
217 |
9146
a48c500e48e1
support range->string conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
218 for (octave_idx_type i = 0; i < nel; i++) |
a48c500e48e1
support range->string conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
219 retval.elem (i) = static_cast<char>(matrix.elem (i)); |
a48c500e48e1
support range->string conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
220 |
a48c500e48e1
support range->string conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
221 return retval; |
a48c500e48e1
support range->string conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
222 } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
223 |
2376 | 224 octave_value |
4017 | 225 octave_range::all (int dim) const |
2376 | 226 { |
5775 | 227 // FIXME -- this is a potential waste of memory. |
2436 | 228 |
229 Matrix m = range.matrix_value (); | |
230 | |
4017 | 231 return m.all (dim); |
2376 | 232 } |
233 | |
234 octave_value | |
4017 | 235 octave_range::any (int dim) const |
2376 | 236 { |
5775 | 237 // FIXME -- this is a potential waste of memory. |
4017 | 238 |
239 Matrix m = range.matrix_value (); | |
240 | |
241 return m.any (dim); | |
2376 | 242 } |
243 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
244 octave_value |
8366
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8345
diff
changeset
|
245 octave_range::diag (octave_idx_type k) const |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
246 { |
8366
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8345
diff
changeset
|
247 return (k == 0 |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8345
diff
changeset
|
248 ? octave_value (DiagMatrix (DiagArray2<double> (range.matrix_value ()))) |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
249 : octave_value (range.diag (k))); |
8366
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8345
diff
changeset
|
250 } |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8345
diff
changeset
|
251 |
14557
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14469
diff
changeset
|
252 octave_value |
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14469
diff
changeset
|
253 octave_range::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:
14469
diff
changeset
|
254 { |
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14469
diff
changeset
|
255 Matrix mat = range.matrix_value (); |
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14469
diff
changeset
|
256 |
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14469
diff
changeset
|
257 return mat.diag (m, n); |
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14469
diff
changeset
|
258 } |
8366
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8345
diff
changeset
|
259 |
2376 | 260 bool |
261 octave_range::is_true (void) const | |
262 { | |
263 bool retval = false; | |
2436 | 264 |
4478 | 265 if (range.nelem () != 0) |
2436 | 266 { |
5775 | 267 // FIXME -- this is a potential waste of memory. |
2436 | 268 |
269 Matrix m ((range.matrix_value () . all ()) . all ()); | |
270 | |
271 retval = (m.rows () == 1 && m.columns () == 1 && m (0, 0) != 0.0); | |
272 } | |
273 | |
2376 | 274 return retval; |
275 } | |
276 | |
277 Complex | |
278 octave_range::complex_value (bool) const | |
279 { | |
4102 | 280 double tmp = lo_ieee_nan_value (); |
281 | |
282 Complex retval (tmp, tmp); | |
2376 | 283 |
5275 | 284 octave_idx_type nel = range.nelem (); |
2376 | 285 |
4455 | 286 if (nel > 0) |
287 { | |
14469
29aabe9b37a2
Rename array-as-vector, array-as-scalar warning IDs to match documentation (bug #35838)
Rik <octave@nomad.inbox5.com>
parents:
14429
diff
changeset
|
288 gripe_implicit_conversion ("Octave:array-to-scalar", |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
289 "range", "complex scalar"); |
4455 | 290 |
291 retval = range.base (); | |
292 } | |
2376 | 293 else |
294 gripe_invalid_conversion ("range", "complex scalar"); | |
295 | |
296 return retval; | |
297 } | |
298 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
299 FloatComplex |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
300 octave_range::float_complex_value (bool) const |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
301 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
302 float tmp = lo_ieee_float_nan_value (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
303 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
304 FloatComplex retval (tmp, tmp); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
305 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
306 octave_idx_type nel = range.nelem (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
307 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
308 if (nel > 0) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
309 { |
14469
29aabe9b37a2
Rename array-as-vector, array-as-scalar warning IDs to match documentation (bug #35838)
Rik <octave@nomad.inbox5.com>
parents:
14429
diff
changeset
|
310 gripe_implicit_conversion ("Octave:array-to-scalar", |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
311 "range", "complex scalar"); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
312 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
313 retval = range.base (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
314 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
315 else |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
316 gripe_invalid_conversion ("range", "complex scalar"); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
317 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
318 return retval; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
319 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
320 |
10613
e103fb2182ce
use internal variable instead of warning state to control whether to allow non-integer ranges as indices
John W. Eaton <jwe@octave.org>
parents:
10609
diff
changeset
|
321 boolNDArray |
e103fb2182ce
use internal variable instead of warning state to control whether to allow non-integer ranges as indices
John W. Eaton <jwe@octave.org>
parents:
10609
diff
changeset
|
322 octave_range::bool_array_value (bool warn) const |
e103fb2182ce
use internal variable instead of warning state to control whether to allow non-integer ranges as indices
John W. Eaton <jwe@octave.org>
parents:
10609
diff
changeset
|
323 { |
e103fb2182ce
use internal variable instead of warning state to control whether to allow non-integer ranges as indices
John W. Eaton <jwe@octave.org>
parents:
10609
diff
changeset
|
324 Matrix m = range.matrix_value (); |
e103fb2182ce
use internal variable instead of warning state to control whether to allow non-integer ranges as indices
John W. Eaton <jwe@octave.org>
parents:
10609
diff
changeset
|
325 |
e103fb2182ce
use internal variable instead of warning state to control whether to allow non-integer ranges as indices
John W. Eaton <jwe@octave.org>
parents:
10609
diff
changeset
|
326 if (m.any_element_is_nan ()) |
11129
0de5cc44e690
use gripe functions for NaN to logical and NaN to character conversions more consistently
John W. Eaton <jwe@octave.org>
parents:
10840
diff
changeset
|
327 gripe_nan_to_logical_conversion (); |
10613
e103fb2182ce
use internal variable instead of warning state to control whether to allow non-integer ranges as indices
John W. Eaton <jwe@octave.org>
parents:
10609
diff
changeset
|
328 else if (warn && m.any_element_not_one_or_zero ()) |
e103fb2182ce
use internal variable instead of warning state to control whether to allow non-integer ranges as indices
John W. Eaton <jwe@octave.org>
parents:
10609
diff
changeset
|
329 gripe_logical_conversion (); |
e103fb2182ce
use internal variable instead of warning state to control whether to allow non-integer ranges as indices
John W. Eaton <jwe@octave.org>
parents:
10609
diff
changeset
|
330 |
e103fb2182ce
use internal variable instead of warning state to control whether to allow non-integer ranges as indices
John W. Eaton <jwe@octave.org>
parents:
10609
diff
changeset
|
331 return boolNDArray (m); |
e103fb2182ce
use internal variable instead of warning state to control whether to allow non-integer ranges as indices
John W. Eaton <jwe@octave.org>
parents:
10609
diff
changeset
|
332 } |
e103fb2182ce
use internal variable instead of warning state to control whether to allow non-integer ranges as indices
John W. Eaton <jwe@octave.org>
parents:
10609
diff
changeset
|
333 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
334 octave_value |
5731 | 335 octave_range::resize (const dim_vector& dv, bool fill) const |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
336 { |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
337 NDArray retval = array_value (); |
5731 | 338 if (fill) |
14616
13cc11418393
improve handling of default resize fill value for arrays
John W. Eaton <jwe@octave.org>
parents:
14557
diff
changeset
|
339 retval.resize (dv, 0); |
5731 | 340 else |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
341 retval.resize (dv); |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
342 return retval; |
5731 | 343 } |
344 | |
2376 | 345 octave_value |
5279 | 346 octave_range::convert_to_str_internal (bool pad, bool force, char type) const |
2449 | 347 { |
348 octave_value tmp (range.matrix_value ()); | |
5279 | 349 return tmp.convert_to_str (pad, force, type); |
2449 | 350 } |
351 | |
2376 | 352 void |
3523 | 353 octave_range::print (std::ostream& os, bool pr_as_read_syntax) const |
2901 | 354 { |
355 print_raw (os, pr_as_read_syntax); | |
356 newline (os); | |
357 } | |
358 | |
359 void | |
3523 | 360 octave_range::print_raw (std::ostream& os, bool pr_as_read_syntax) const |
2901 | 361 { |
362 octave_print_internal (os, range, pr_as_read_syntax, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
363 current_print_indent_level ()); |
2901 | 364 } |
365 | |
366 bool | |
3523 | 367 octave_range::print_name_tag (std::ostream& os, const std::string& name) const |
2376 | 368 { |
2901 | 369 bool retval = false; |
370 | |
5275 | 371 octave_idx_type n = range.nelem (); |
2901 | 372 |
373 indent (os); | |
374 | |
375 if (n == 0 || n == 1) | |
376 os << name << " = "; | |
377 else | |
378 { | |
379 os << name << " ="; | |
380 newline (os); | |
13112
969ed305dde5
Remove all blank lines with "format compact"
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12863
diff
changeset
|
381 if (! Vcompact_format) |
969ed305dde5
Remove all blank lines with "format compact"
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12863
diff
changeset
|
382 newline (os); |
969ed305dde5
Remove all blank lines with "format compact"
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12863
diff
changeset
|
383 |
2901 | 384 retval = true; |
385 } | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
386 |
2901 | 387 return retval; |
2376 | 388 } |
389 | |
16468
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
390 std::string |
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
391 octave_range::short_disp (void) const |
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
392 { |
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
393 std::ostringstream buf; |
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
394 |
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
395 octave_idx_type len = range.nelem (); |
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
396 |
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
397 if (len == 0) |
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
398 buf << "[]"; |
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
399 else |
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
400 { |
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
401 buf << range.base () << ":"; |
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
402 |
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
403 if (len > 1) |
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
404 { |
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
405 if (range.inc () != 1) |
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
406 buf << range.inc () << ":"; |
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
407 |
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
408 buf << range.limit (); |
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
409 } |
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
410 } |
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
411 |
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
412 return buf.str (); |
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
413 } |
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
414 |
4687 | 415 // Skip white space and comments on stream IS. |
416 | |
417 static void | |
418 skip_comments (std::istream& is) | |
419 { | |
420 char c = '\0'; | |
421 while (is.get (c)) | |
422 { | |
423 if (c == ' ' || c == '\t' || c == '\n') | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
424 ; // Skip whitespace on way to beginning of next line. |
4687 | 425 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
426 break; |
4687 | 427 } |
428 | |
8946
e7e928088e90
fix CRLF issues with text-mode reading in windows when loading ascii data
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
8920
diff
changeset
|
429 skip_until_newline (is, false); |
4687 | 430 } |
431 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
432 bool |
6974 | 433 octave_range::save_ascii (std::ostream& os) |
4687 | 434 { |
435 Range r = range_value (); | |
436 double base = r.base (); | |
437 double limit = r.limit (); | |
438 double inc = r.inc (); | |
10735
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
439 octave_idx_type len = r.nelem (); |
4687 | 440 |
10735
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
441 if (inc != 0) |
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
442 os << "# base, limit, increment\n"; |
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
443 else |
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
444 os << "# base, length, increment\n"; |
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
445 |
4687 | 446 octave_write_double (os, base); |
447 os << " "; | |
10735
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
448 if (inc != 0) |
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
449 octave_write_double (os, limit); |
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
450 else |
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
451 os << len; |
4687 | 452 os << " "; |
453 octave_write_double (os, inc); | |
454 os << "\n"; | |
455 | |
456 return true; | |
457 } | |
458 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
459 bool |
4687 | 460 octave_range::load_ascii (std::istream& is) |
461 { | |
462 // # base, limit, range comment added by save (). | |
463 skip_comments (is); | |
464 | |
10735
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
465 double base, limit, inc; |
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
466 is >> base >> limit >> inc; |
4687 | 467 |
468 if (!is) | |
469 { | |
470 error ("load: failed to load range constant"); | |
471 return false; | |
472 } | |
473 | |
10735
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
474 if (inc != 0) |
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
475 range = Range (base, limit, inc); |
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
476 else |
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
477 range = Range (base, inc, static_cast<octave_idx_type> (limit)); |
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
478 |
4687 | 479 return true; |
480 } | |
481 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
482 bool |
4687 | 483 octave_range::save_binary (std::ostream& os, bool& /* save_as_floats */) |
484 { | |
5760 | 485 char tmp = LS_DOUBLE; |
486 os.write (reinterpret_cast<char *> (&tmp), 1); | |
4687 | 487 Range r = range_value (); |
488 double bas = r.base (); | |
489 double lim = r.limit (); | |
490 double inc = r.inc (); | |
10735
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
491 if (inc == 0) |
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
492 lim = r.nelem (); |
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
493 |
5760 | 494 os.write (reinterpret_cast<char *> (&bas), 8); |
495 os.write (reinterpret_cast<char *> (&lim), 8); | |
496 os.write (reinterpret_cast<char *> (&inc), 8); | |
4687 | 497 |
498 return true; | |
499 } | |
500 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
501 bool |
4687 | 502 octave_range::load_binary (std::istream& is, bool swap, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
503 oct_mach_info::float_format /* fmt */) |
4687 | 504 { |
505 char tmp; | |
5760 | 506 if (! is.read (reinterpret_cast<char *> (&tmp), 1)) |
4687 | 507 return false; |
508 double bas, lim, inc; | |
5760 | 509 if (! is.read (reinterpret_cast<char *> (&bas), 8)) |
4687 | 510 return false; |
511 if (swap) | |
4944 | 512 swap_bytes<8> (&bas); |
5760 | 513 if (! is.read (reinterpret_cast<char *> (&lim), 8)) |
4687 | 514 return false; |
515 if (swap) | |
4944 | 516 swap_bytes<8> (&lim); |
5760 | 517 if (! is.read (reinterpret_cast<char *> (&inc), 8)) |
4687 | 518 return false; |
519 if (swap) | |
4944 | 520 swap_bytes<8> (&inc); |
10735
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
521 if (inc != 0) |
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
522 range = Range (bas, lim, inc); |
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
523 else |
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
524 range = Range (bas, inc, static_cast<octave_idx_type> (lim)); |
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
525 |
4687 | 526 return true; |
527 } | |
528 | |
529 #if defined (HAVE_HDF5) | |
4944 | 530 |
4687 | 531 // The following subroutines creates an HDF5 representation of the way |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
532 // we will store Octave range types (triplets of floating-point numbers). |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
533 // NUM_TYPE is the HDF5 numeric type to use for storage (e.g. |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
534 // H5T_NATIVE_DOUBLE to save as 'double'). Note that any necessary |
4687 | 535 // conversions are handled automatically by HDF5. |
536 | |
537 static hid_t | |
538 hdf5_make_range_type (hid_t num_type) | |
539 { | |
540 hid_t type_id = H5Tcreate (H5T_COMPOUND, sizeof (double) * 3); | |
541 | |
542 H5Tinsert (type_id, "base", 0 * sizeof (double), num_type); | |
543 H5Tinsert (type_id, "limit", 1 * sizeof (double), num_type); | |
544 H5Tinsert (type_id, "increment", 2 * sizeof (double), num_type); | |
545 | |
546 return type_id; | |
547 } | |
548 | |
549 bool | |
550 octave_range::save_hdf5 (hid_t loc_id, const char *name, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
551 bool /* save_as_floats */) |
4687 | 552 { |
4792 | 553 hsize_t dimens[3]; |
4687 | 554 hid_t space_hid = -1, type_hid = -1, data_hid = -1; |
555 bool retval = true; | |
556 | |
4815 | 557 space_hid = H5Screate_simple (0, dimens, 0); |
4687 | 558 if (space_hid < 0) return false; |
559 | |
560 type_hid = hdf5_make_range_type (H5T_NATIVE_DOUBLE); | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
561 if (type_hid < 0) |
4687 | 562 { |
563 H5Sclose (space_hid); | |
564 return false; | |
565 } | |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
566 #if HAVE_HDF5_18 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
567 data_hid = H5Dcreate (loc_id, name, type_hid, space_hid, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
568 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
569 #else |
4687 | 570 data_hid = H5Dcreate (loc_id, name, type_hid, space_hid, H5P_DEFAULT); |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
571 #endif |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
572 if (data_hid < 0) |
4687 | 573 { |
574 H5Sclose (space_hid); | |
575 H5Tclose (type_hid); | |
576 return false; | |
577 } | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
578 |
4687 | 579 Range r = range_value (); |
580 double range_vals[3]; | |
581 range_vals[0] = r.base (); | |
10735
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
582 range_vals[1] = r.inc () != 0 ? r.limit () : r.nelem (); |
4687 | 583 range_vals[2] = r.inc (); |
584 | |
11176
2271261f088a
Address precision issue in ranges saved to HDF5 files
David Bateman <dbateman@free.fr>
parents:
11129
diff
changeset
|
585 if (H5Dwrite (data_hid, type_hid, H5S_ALL, H5S_ALL, H5P_DEFAULT, |
2271261f088a
Address precision issue in ranges saved to HDF5 files
David Bateman <dbateman@free.fr>
parents:
11129
diff
changeset
|
586 range_vals) >= 0) |
2271261f088a
Address precision issue in ranges saved to HDF5 files
David Bateman <dbateman@free.fr>
parents:
11129
diff
changeset
|
587 { |
2271261f088a
Address precision issue in ranges saved to HDF5 files
David Bateman <dbateman@free.fr>
parents:
11129
diff
changeset
|
588 octave_idx_type nel = r.nelem (); |
2271261f088a
Address precision issue in ranges saved to HDF5 files
David Bateman <dbateman@free.fr>
parents:
11129
diff
changeset
|
589 retval = hdf5_add_scalar_attr (data_hid, H5T_NATIVE_IDX, |
2271261f088a
Address precision issue in ranges saved to HDF5 files
David Bateman <dbateman@free.fr>
parents:
11129
diff
changeset
|
590 "OCTAVE_RANGE_NELEM", &nel) >= 0; |
2271261f088a
Address precision issue in ranges saved to HDF5 files
David Bateman <dbateman@free.fr>
parents:
11129
diff
changeset
|
591 } |
2271261f088a
Address precision issue in ranges saved to HDF5 files
David Bateman <dbateman@free.fr>
parents:
11129
diff
changeset
|
592 else |
2271261f088a
Address precision issue in ranges saved to HDF5 files
David Bateman <dbateman@free.fr>
parents:
11129
diff
changeset
|
593 retval = false; |
4687 | 594 |
595 H5Dclose (data_hid); | |
596 H5Tclose (type_hid); | |
597 H5Sclose (space_hid); | |
4837 | 598 |
4687 | 599 return retval; |
600 } | |
601 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
602 bool |
9881
b3089dba88bf
Remove HDF5 cruft for older versions of HDF5
Kacper Kowalik
parents:
9146
diff
changeset
|
603 octave_range::load_hdf5 (hid_t loc_id, const char *name) |
4687 | 604 { |
605 bool retval = false; | |
4837 | 606 |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
607 #if HAVE_HDF5_18 |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
608 hid_t data_hid = H5Dopen (loc_id, name, H5P_DEFAULT); |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
609 #else |
4687 | 610 hid_t data_hid = H5Dopen (loc_id, name); |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
611 #endif |
4687 | 612 hid_t type_hid = H5Dget_type (data_hid); |
613 | |
614 hid_t range_type = hdf5_make_range_type (H5T_NATIVE_DOUBLE); | |
615 | |
616 if (! hdf5_types_compatible (type_hid, range_type)) | |
617 { | |
4837 | 618 H5Tclose (range_type); |
4687 | 619 H5Dclose (data_hid); |
620 return false; | |
621 } | |
622 | |
623 hid_t space_hid = H5Dget_space (data_hid); | |
624 hsize_t rank = H5Sget_simple_extent_ndims (space_hid); | |
625 | |
626 if (rank != 0) | |
627 { | |
4837 | 628 H5Tclose (range_type); |
4687 | 629 H5Sclose (space_hid); |
630 H5Dclose (data_hid); | |
631 return false; | |
632 } | |
633 | |
634 double rangevals[3]; | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
635 if (H5Dread (data_hid, range_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
636 rangevals) >= 0) |
4687 | 637 { |
638 retval = true; | |
11176
2271261f088a
Address precision issue in ranges saved to HDF5 files
David Bateman <dbateman@free.fr>
parents:
11129
diff
changeset
|
639 octave_idx_type nel; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
640 if (hdf5_get_scalar_attr (data_hid, H5T_NATIVE_IDX, |
11176
2271261f088a
Address precision issue in ranges saved to HDF5 files
David Bateman <dbateman@free.fr>
parents:
11129
diff
changeset
|
641 "OCTAVE_RANGE_NELEM", &nel)) |
2271261f088a
Address precision issue in ranges saved to HDF5 files
David Bateman <dbateman@free.fr>
parents:
11129
diff
changeset
|
642 range = Range (rangevals[0], rangevals[2], nel); |
10735
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
643 else |
11176
2271261f088a
Address precision issue in ranges saved to HDF5 files
David Bateman <dbateman@free.fr>
parents:
11129
diff
changeset
|
644 { |
2271261f088a
Address precision issue in ranges saved to HDF5 files
David Bateman <dbateman@free.fr>
parents:
11129
diff
changeset
|
645 if (rangevals[2] != 0) |
2271261f088a
Address precision issue in ranges saved to HDF5 files
David Bateman <dbateman@free.fr>
parents:
11129
diff
changeset
|
646 range = Range (rangevals[0], rangevals[1], rangevals[2]); |
2271261f088a
Address precision issue in ranges saved to HDF5 files
David Bateman <dbateman@free.fr>
parents:
11129
diff
changeset
|
647 else |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
648 range = Range (rangevals[0], rangevals[2], |
11176
2271261f088a
Address precision issue in ranges saved to HDF5 files
David Bateman <dbateman@free.fr>
parents:
11129
diff
changeset
|
649 static_cast<octave_idx_type> (rangevals[1])); |
2271261f088a
Address precision issue in ranges saved to HDF5 files
David Bateman <dbateman@free.fr>
parents:
11129
diff
changeset
|
650 } |
4687 | 651 } |
652 | |
4837 | 653 H5Tclose (range_type); |
4687 | 654 H5Sclose (space_hid); |
655 H5Dclose (data_hid); | |
4837 | 656 |
4687 | 657 return retval; |
658 } | |
4944 | 659 |
4687 | 660 #endif |
661 | |
5900 | 662 mxArray * |
663 octave_range::as_mxArray (void) const | |
664 { | |
665 mxArray *retval = new mxArray (mxDOUBLE_CLASS, dims (), mxREAL); | |
666 | |
667 double *pr = static_cast<double *> (retval->get_data ()); | |
668 | |
6686 | 669 mwSize nel = numel (); |
5900 | 670 |
671 Matrix m = matrix_value (); | |
672 | |
673 const double *p = m.data (); | |
674 | |
6686 | 675 for (mwSize i = 0; i < nel; i++) |
5900 | 676 pr[i] = p[i]; |
677 | |
678 return retval; | |
679 } | |
10613
e103fb2182ce
use internal variable instead of warning state to control whether to allow non-integer ranges as indices
John W. Eaton <jwe@octave.org>
parents:
10609
diff
changeset
|
680 |
e103fb2182ce
use internal variable instead of warning state to control whether to allow non-integer ranges as indices
John W. Eaton <jwe@octave.org>
parents:
10609
diff
changeset
|
681 DEFUN (allow_noninteger_range_as_index, args, nargout, |
e103fb2182ce
use internal variable instead of warning state to control whether to allow non-integer ranges as indices
John W. Eaton <jwe@octave.org>
parents:
10609
diff
changeset
|
682 "-*- texinfo -*-\n\ |
10840 | 683 @deftypefn {Built-in Function} {@var{val} =} allow_noninteger_range_as_index ()\n\ |
10613
e103fb2182ce
use internal variable instead of warning state to control whether to allow non-integer ranges as indices
John W. Eaton <jwe@octave.org>
parents:
10609
diff
changeset
|
684 @deftypefnx {Built-in Function} {@var{old_val} =} allow_noninteger_range_as_index (@var{new_val})\n\ |
13951
79aa00a94e9e
doc: Document "local" option for configuration variables.
Rik <octave@nomad.inbox5.com>
parents:
13846
diff
changeset
|
685 @deftypefnx {Built-in Function} {} allow_noninteger_range_as_index (@var{new_val}, \"local\")\n\ |
10613
e103fb2182ce
use internal variable instead of warning state to control whether to allow non-integer ranges as indices
John W. Eaton <jwe@octave.org>
parents:
10609
diff
changeset
|
686 Query or set the internal variable that controls whether non-integer\n\ |
10711
fbd7843974fa
Periodic grammar check of documentation files to ensure common format.
Rik <octave@nomad.inbox5.com>
parents:
10616
diff
changeset
|
687 ranges are allowed as indices. This might be useful for @sc{matlab}\n\ |
10613
e103fb2182ce
use internal variable instead of warning state to control whether to allow non-integer ranges as indices
John W. Eaton <jwe@octave.org>
parents:
10609
diff
changeset
|
688 compatibility; however, it is still not entirely compatible because\n\ |
10711
fbd7843974fa
Periodic grammar check of documentation files to ensure common format.
Rik <octave@nomad.inbox5.com>
parents:
10616
diff
changeset
|
689 @sc{matlab} treats the range expression differently in different contexts.\n\ |
13951
79aa00a94e9e
doc: Document "local" option for configuration variables.
Rik <octave@nomad.inbox5.com>
parents:
13846
diff
changeset
|
690 \n\ |
17289
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16468
diff
changeset
|
691 When called from inside a function with the @qcode{\"local\"} option, the\n\ |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16468
diff
changeset
|
692 variable is changed locally for the function and any subroutines it calls. \n\ |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16468
diff
changeset
|
693 The original variable value is restored when exiting the function.\n\ |
10613
e103fb2182ce
use internal variable instead of warning state to control whether to allow non-integer ranges as indices
John W. Eaton <jwe@octave.org>
parents:
10609
diff
changeset
|
694 @end deftypefn") |
e103fb2182ce
use internal variable instead of warning state to control whether to allow non-integer ranges as indices
John W. Eaton <jwe@octave.org>
parents:
10609
diff
changeset
|
695 { |
e103fb2182ce
use internal variable instead of warning state to control whether to allow non-integer ranges as indices
John W. Eaton <jwe@octave.org>
parents:
10609
diff
changeset
|
696 return SET_INTERNAL_VARIABLE (allow_noninteger_range_as_index); |
e103fb2182ce
use internal variable instead of warning state to control whether to allow non-integer ranges as indices
John W. Eaton <jwe@octave.org>
parents:
10609
diff
changeset
|
697 } |
12863
8f9702abe3e3
test: Add validation for allow_noninteger_range_as_index()
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
698 |
8f9702abe3e3
test: Add validation for allow_noninteger_range_as_index()
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
699 /* |
8f9702abe3e3
test: Add validation for allow_noninteger_range_as_index()
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
700 %!test |
8f9702abe3e3
test: Add validation for allow_noninteger_range_as_index()
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
701 %! x = 0:10; |
13846
10a5c8155756
* ov-range.cc: Also disable warning for noninteger index in test.
John W. Eaton <jwe@octave.org>
parents:
13112
diff
changeset
|
702 %! save = allow_noninteger_range_as_index (); |
10a5c8155756
* ov-range.cc: Also disable warning for noninteger index in test.
John W. Eaton <jwe@octave.org>
parents:
13112
diff
changeset
|
703 %! warn_state = warning ("query", "Octave:noninteger-range-as-index"); |
10a5c8155756
* ov-range.cc: Also disable warning for noninteger index in test.
John W. Eaton <jwe@octave.org>
parents:
13112
diff
changeset
|
704 %! unwind_protect |
14040
a64f8b6f63e9
test: simplify test for allow_noninteger_range_as_index
Rik <octave@nomad.inbox5.com>
parents:
13951
diff
changeset
|
705 %! allow_noninteger_range_as_index (false); |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
706 %! fail ("x(2.1:5)"); |
13846
10a5c8155756
* ov-range.cc: Also disable warning for noninteger index in test.
John W. Eaton <jwe@octave.org>
parents:
13112
diff
changeset
|
707 %! assert (x(2:5), 1:4); |
10a5c8155756
* ov-range.cc: Also disable warning for noninteger index in test.
John W. Eaton <jwe@octave.org>
parents:
13112
diff
changeset
|
708 %! allow_noninteger_range_as_index (true); |
10a5c8155756
* ov-range.cc: Also disable warning for noninteger index in test.
John W. Eaton <jwe@octave.org>
parents:
13112
diff
changeset
|
709 %! warning ("off", "Octave:noninteger-range-as-index"); |
10a5c8155756
* ov-range.cc: Also disable warning for noninteger index in test.
John W. Eaton <jwe@octave.org>
parents:
13112
diff
changeset
|
710 %! assert (x(2.49:5), 1:3); |
10a5c8155756
* ov-range.cc: Also disable warning for noninteger index in test.
John W. Eaton <jwe@octave.org>
parents:
13112
diff
changeset
|
711 %! assert (x(2.5:5), 2:4); |
10a5c8155756
* ov-range.cc: Also disable warning for noninteger index in test.
John W. Eaton <jwe@octave.org>
parents:
13112
diff
changeset
|
712 %! assert (x(2.51:5), 2:4); |
10a5c8155756
* ov-range.cc: Also disable warning for noninteger index in test.
John W. Eaton <jwe@octave.org>
parents:
13112
diff
changeset
|
713 %! unwind_protect_cleanup |
10a5c8155756
* ov-range.cc: Also disable warning for noninteger index in test.
John W. Eaton <jwe@octave.org>
parents:
13112
diff
changeset
|
714 %! allow_noninteger_range_as_index (save); |
10a5c8155756
* ov-range.cc: Also disable warning for noninteger index in test.
John W. Eaton <jwe@octave.org>
parents:
13112
diff
changeset
|
715 %! warning (warn_state.state, warn_state.identifier); |
10a5c8155756
* ov-range.cc: Also disable warning for noninteger index in test.
John W. Eaton <jwe@octave.org>
parents:
13112
diff
changeset
|
716 %! end_unwind_protect |
12863
8f9702abe3e3
test: Add validation for allow_noninteger_range_as_index()
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
717 */ |