Mercurial > octave-nkf
annotate libinterp/octave-value/ov-range.cc @ 20574:dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
* lo-array-gripes.h, lo-array-gripes.cc (index_exception):
New base class for indexing errors.
(invalid_index, out_of_range): New classes.
(gripe_index_out_of_range): New overloaded function.
(gripe_invalid_index): New overloaded functions.
Delete version with no arguments.
(gripe_invalid_assignment_size, gripe_assignment_dimension_mismatch):
Delete.
Change uses of gripe functions as needed.
* Cell.cc (Cell::index, Cell::assign, Cell::delete_elements): Use
exceptions to collect error info about and handle indexing errors.
* data.cc (Fnth_element, do_accumarray_sum, F__accumarray_sum__,
do_accumarray_minmax, do_accumarray_minmax_fun, F__accumdim_sum__):
Likewise.
* oct-map.cc (octave_map::index, octave_map::assign,
octave_map::delete_elements): Likewise.
* sparse.cc (Fsparse): Likewise.
* sub2ind.cc (Fsub2ind, Find2sub): Likewise. New tests.
* utils.cc (dims_to_numel): Likewise.
* ov-base-diag.cc (octave_base_diag<DMT, MT>::do_index_op,
octave_base_diag<DMT, MT>::subsasgn): Likewise.
* ov-base-mat.cc (octave_base_matrix<MT>::subsref,
octave_base_matrix<MT>::assign): Likewise.
* ov-base-sparse.cc (octave_base_sparse<T>::do_index_op,
octave_base_sparse<T>::assign,
octave_base_sparse<MT>::delete_elements): Likewise.
* ov-classdef.cc (cdef_object_array::subsref,
cdef_object_array::subsasgn): Likewise.
* ov-java.cc (make_java_index): Likewise.
* ov-perm.cc (octave_perm_matrix::do_index_op): Likewise.
* ov-range.cc (octave_range::do_index_op): Likewise.
* ov-re-diag.cc (octave_diag_matrix::do_index_op): Likewise.
* ov-str-mat.cc (octave_char_matrix_str::do_index_op_internal): Likewise.
* pt-assign.cc (tree_simple_assignment::rvalue1): Likewise.
* pt-idx.cc (tree_index_expression::rvalue,
tree_index_expression::lvalue): Likewise.
* Array-util.cc (sub2ind): Likewise.
* toplev.cc (main_loop): Also catch unhandled index_exception
exceptions.
* ov-base.cc (octave_base_value::index_vector): Improve error message.
* ov-re-sparse.cc (octave_sparse_matrix::index_vector): Likewise.
* ov-complex.cc (complex_index): New class.
(gripe_complex_index): New function.
(octave_complex::index_vector): Use it.
* pt-id.h, pt-id.cc (tree_identifier::is_variable,
tree_black_hole::is_variable): Now const.
* pt-idx.cc (final_index_error): New static function.
(tree_index_expression::rvalue, tree_index_expression::lvalue):
Use it.
* index.tst: New tests.
author | Lachlan Andrew <lachlanbis@gmail.com> |
---|---|
date | Fri, 02 Oct 2015 15:07:37 -0400 |
parents | 00cf2847355d |
children | f90c8372b7ba |
rev | line source |
---|---|
2376 | 1 /* |
2 | |
19731
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19632
diff
changeset
|
3 Copyright (C) 1996-2015 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" |
19897
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19895
diff
changeset
|
38 #include "oct-hdf5.h" |
2376 | 39 #include "ov-range.h" |
40 #include "ov-re-mat.h" | |
2410 | 41 #include "ov-scalar.h" |
2376 | 42 #include "pr-output.h" |
43 | |
4687 | 44 #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
|
45 #include "ls-ascii-helper.h" |
4687 | 46 #include "ls-hdf5.h" |
47 #include "ls-utils.h" | |
48 | |
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
|
49 // If TRUE, allow ranges with non-integer elements as array indices. |
18129
e473c4853afc
enable non-integer ranges as indices by default and deprecate preference
John W. Eaton <jwe@octave.org>
parents:
17870
diff
changeset
|
50 static bool Vallow_noninteger_range_as_index = true; |
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
|
51 |
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 |
20263
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20198
diff
changeset
|
75 switch (range.numel ()) |
2410 | 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. |
20574
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20263
diff
changeset
|
131 |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20263
diff
changeset
|
132 try |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
9892
diff
changeset
|
133 { |
20574
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20263
diff
changeset
|
134 idx_vector i = idx(0).index_vector (); |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20263
diff
changeset
|
135 |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20263
diff
changeset
|
136 if (! error_state) |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20263
diff
changeset
|
137 { |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20263
diff
changeset
|
138 if (i.is_scalar () && i(0) < range.numel ()) |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20263
diff
changeset
|
139 retval = range.elem (i(0)); |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20263
diff
changeset
|
140 else |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20263
diff
changeset
|
141 retval = range.index (i); |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20263
diff
changeset
|
142 } |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20263
diff
changeset
|
143 } |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20263
diff
changeset
|
144 catch (index_exception& e) |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20263
diff
changeset
|
145 { |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20263
diff
changeset
|
146 // More info may be added later before displaying error. |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20263
diff
changeset
|
147 |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20263
diff
changeset
|
148 e.set_pos_if_unset (1, 1); |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20263
diff
changeset
|
149 throw; |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
9892
diff
changeset
|
150 } |
2436 | 151 |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
9892
diff
changeset
|
152 return retval; |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
9892
diff
changeset
|
153 } |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
9892
diff
changeset
|
154 else |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
9892
diff
changeset
|
155 { |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
9892
diff
changeset
|
156 octave_value tmp (new octave_matrix (range.matrix_value ())); |
2436 | 157 |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
9892
diff
changeset
|
158 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
|
159 } |
2436 | 160 } |
161 | |
10605
1834132fb50b
allow non-integer ranges as indices conditionally
John W. Eaton <jwe@octave.org>
parents:
10315
diff
changeset
|
162 idx_vector |
18129
e473c4853afc
enable non-integer ranges as indices by default and deprecate preference
John W. Eaton <jwe@octave.org>
parents:
17870
diff
changeset
|
163 octave_range::index_vector (bool require_integers) const |
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 if (idx_cache) |
1834132fb50b
allow non-integer ranges as indices conditionally
John W. Eaton <jwe@octave.org>
parents:
10315
diff
changeset
|
166 return *idx_cache; |
1834132fb50b
allow non-integer ranges as indices conditionally
John W. Eaton <jwe@octave.org>
parents:
10315
diff
changeset
|
167 else |
1834132fb50b
allow non-integer ranges as indices conditionally
John W. Eaton <jwe@octave.org>
parents:
10315
diff
changeset
|
168 { |
18129
e473c4853afc
enable non-integer ranges as indices by default and deprecate preference
John W. Eaton <jwe@octave.org>
parents:
17870
diff
changeset
|
169 if (require_integers |
e473c4853afc
enable non-integer ranges as indices by default and deprecate preference
John W. Eaton <jwe@octave.org>
parents:
17870
diff
changeset
|
170 || ! Vallow_noninteger_range_as_index |
10616
d61caf612f1e
optimize order of conditions in octave_range::index_vector
Jaroslav Hajek <highegg@gmail.com>
parents:
10613
diff
changeset
|
171 || range.all_elements_are_ints ()) |
10605
1834132fb50b
allow non-integer ranges as indices conditionally
John W. Eaton <jwe@octave.org>
parents:
10315
diff
changeset
|
172 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
|
173 else |
1834132fb50b
allow non-integer ranges as indices conditionally
John W. Eaton <jwe@octave.org>
parents:
10315
diff
changeset
|
174 { |
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
|
175 warning_with_id ("Octave:noninteger-range-as-index", |
10609
58bcda68ac11
improve warning/error message
John W. Eaton <jwe@octave.org>
parents:
10605
diff
changeset
|
176 "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
|
177 |
1834132fb50b
allow non-integer ranges as indices conditionally
John W. Eaton <jwe@octave.org>
parents:
10315
diff
changeset
|
178 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
|
179 } |
1834132fb50b
allow non-integer ranges as indices conditionally
John W. Eaton <jwe@octave.org>
parents:
10315
diff
changeset
|
180 } |
1834132fb50b
allow non-integer ranges as indices conditionally
John W. Eaton <jwe@octave.org>
parents:
10315
diff
changeset
|
181 } |
1834132fb50b
allow non-integer ranges as indices conditionally
John W. Eaton <jwe@octave.org>
parents:
10315
diff
changeset
|
182 |
2376 | 183 double |
184 octave_range::double_value (bool) const | |
185 { | |
4102 | 186 double retval = lo_ieee_nan_value (); |
2376 | 187 |
20263
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20198
diff
changeset
|
188 octave_idx_type nel = range.numel (); |
2376 | 189 |
4455 | 190 if (nel > 0) |
191 { | |
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
|
192 gripe_implicit_conversion ("Octave:array-to-scalar", |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
193 "range", "real scalar"); |
4455 | 194 |
195 retval = range.base (); | |
196 } | |
2376 | 197 else |
198 gripe_invalid_conversion ("range", "real scalar"); | |
199 | |
200 return retval; | |
201 } | |
202 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
203 float |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
204 octave_range::float_value (bool) const |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
205 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
206 float retval = lo_ieee_float_nan_value (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
207 |
20263
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20198
diff
changeset
|
208 octave_idx_type nel = range.numel (); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
209 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
210 if (nel > 0) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
211 { |
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
|
212 gripe_implicit_conversion ("Octave:array-to-scalar", |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
213 "range", "real scalar"); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
214 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
215 retval = range.base (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
216 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
217 else |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
218 gripe_invalid_conversion ("range", "real scalar"); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
219 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
220 return retval; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
221 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
222 |
9146
a48c500e48e1
support range->string conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
223 charNDArray |
a48c500e48e1
support range->string conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
224 octave_range::char_array_value (bool) const |
a48c500e48e1
support range->string conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
225 { |
a48c500e48e1
support range->string conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
226 const Matrix matrix = range.matrix_value (); |
a48c500e48e1
support range->string conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
227 charNDArray retval (dims ()); |
a48c500e48e1
support range->string conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
228 |
a48c500e48e1
support range->string conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
229 octave_idx_type nel = numel (); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
230 |
9146
a48c500e48e1
support range->string conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
231 for (octave_idx_type i = 0; i < nel; i++) |
a48c500e48e1
support range->string conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
232 retval.elem (i) = static_cast<char>(matrix.elem (i)); |
a48c500e48e1
support range->string conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
233 |
a48c500e48e1
support range->string conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
234 return retval; |
a48c500e48e1
support range->string conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
235 } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
236 |
2376 | 237 octave_value |
4017 | 238 octave_range::all (int dim) const |
2376 | 239 { |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
240 // FIXME: this is a potential waste of memory. |
2436 | 241 |
242 Matrix m = range.matrix_value (); | |
243 | |
4017 | 244 return m.all (dim); |
2376 | 245 } |
246 | |
247 octave_value | |
4017 | 248 octave_range::any (int dim) const |
2376 | 249 { |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
250 // FIXME: this is a potential waste of memory. |
4017 | 251 |
252 Matrix m = range.matrix_value (); | |
253 | |
254 return m.any (dim); | |
2376 | 255 } |
256 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
257 octave_value |
8366
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8345
diff
changeset
|
258 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
|
259 { |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
260 return |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
261 (k == 0 |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
262 ? octave_value (DiagMatrix (DiagArray2<double> (range.matrix_value ()))) |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
263 : octave_value (range.diag (k))); |
8366
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8345
diff
changeset
|
264 } |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8345
diff
changeset
|
265 |
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
|
266 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
|
267 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
|
268 { |
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14469
diff
changeset
|
269 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
|
270 |
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14469
diff
changeset
|
271 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
|
272 } |
8366
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8345
diff
changeset
|
273 |
2376 | 274 bool |
275 octave_range::is_true (void) const | |
276 { | |
277 bool retval = false; | |
2436 | 278 |
20263
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20198
diff
changeset
|
279 if (range.numel () != 0) |
2436 | 280 { |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
281 // FIXME: this is a potential waste of memory. |
2436 | 282 |
283 Matrix m ((range.matrix_value () . all ()) . all ()); | |
284 | |
285 retval = (m.rows () == 1 && m.columns () == 1 && m (0, 0) != 0.0); | |
286 } | |
287 | |
2376 | 288 return retval; |
289 } | |
290 | |
291 Complex | |
292 octave_range::complex_value (bool) const | |
293 { | |
4102 | 294 double tmp = lo_ieee_nan_value (); |
295 | |
296 Complex retval (tmp, tmp); | |
2376 | 297 |
20263
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20198
diff
changeset
|
298 octave_idx_type nel = range.numel (); |
2376 | 299 |
4455 | 300 if (nel > 0) |
301 { | |
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
|
302 gripe_implicit_conversion ("Octave:array-to-scalar", |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
303 "range", "complex scalar"); |
4455 | 304 |
305 retval = range.base (); | |
306 } | |
2376 | 307 else |
308 gripe_invalid_conversion ("range", "complex scalar"); | |
309 | |
310 return retval; | |
311 } | |
312 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
313 FloatComplex |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
314 octave_range::float_complex_value (bool) const |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
315 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
316 float tmp = lo_ieee_float_nan_value (); |
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 FloatComplex retval (tmp, tmp); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
319 |
20263
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20198
diff
changeset
|
320 octave_idx_type nel = range.numel (); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
321 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
322 if (nel > 0) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
323 { |
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
|
324 gripe_implicit_conversion ("Octave:array-to-scalar", |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
325 "range", "complex scalar"); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
326 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
327 retval = range.base (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
328 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
329 else |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
330 gripe_invalid_conversion ("range", "complex scalar"); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
331 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
332 return retval; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
333 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
334 |
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
|
335 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
|
336 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
|
337 { |
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
|
338 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
|
339 |
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
|
340 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
|
341 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
|
342 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
|
343 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
|
344 |
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
|
345 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
|
346 } |
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
|
347 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
348 octave_value |
5731 | 349 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
|
350 { |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
351 NDArray retval = array_value (); |
5731 | 352 if (fill) |
14616
13cc11418393
improve handling of default resize fill value for arrays
John W. Eaton <jwe@octave.org>
parents:
14557
diff
changeset
|
353 retval.resize (dv, 0); |
5731 | 354 else |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
355 retval.resize (dv); |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
356 return retval; |
5731 | 357 } |
358 | |
2376 | 359 octave_value |
5279 | 360 octave_range::convert_to_str_internal (bool pad, bool force, char type) const |
2449 | 361 { |
362 octave_value tmp (range.matrix_value ()); | |
5279 | 363 return tmp.convert_to_str (pad, force, type); |
2449 | 364 } |
365 | |
2376 | 366 void |
18416
bcd71a2531d3
Support disp/display overloading in classdef
Michael Goffioul <michael.goffioul@gmail.com>
parents:
18131
diff
changeset
|
367 octave_range::print (std::ostream& os, bool pr_as_read_syntax) |
2901 | 368 { |
369 print_raw (os, pr_as_read_syntax); | |
370 newline (os); | |
371 } | |
372 | |
373 void | |
3523 | 374 octave_range::print_raw (std::ostream& os, bool pr_as_read_syntax) const |
2901 | 375 { |
376 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
|
377 current_print_indent_level ()); |
2901 | 378 } |
379 | |
380 bool | |
3523 | 381 octave_range::print_name_tag (std::ostream& os, const std::string& name) const |
2376 | 382 { |
2901 | 383 bool retval = false; |
384 | |
20263
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20198
diff
changeset
|
385 octave_idx_type n = range.numel (); |
2901 | 386 |
387 indent (os); | |
388 | |
389 if (n == 0 || n == 1) | |
390 os << name << " = "; | |
391 else | |
392 { | |
393 os << name << " ="; | |
394 newline (os); | |
13112
969ed305dde5
Remove all blank lines with "format compact"
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12863
diff
changeset
|
395 if (! Vcompact_format) |
969ed305dde5
Remove all blank lines with "format compact"
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12863
diff
changeset
|
396 newline (os); |
969ed305dde5
Remove all blank lines with "format compact"
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12863
diff
changeset
|
397 |
2901 | 398 retval = true; |
399 } | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
400 |
2901 | 401 return retval; |
2376 | 402 } |
403 | |
17870 | 404 void |
405 octave_range::short_disp (std::ostream& os) const | |
16468
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
406 { |
20263
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20198
diff
changeset
|
407 octave_idx_type len = range.numel (); |
16468
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
408 |
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
409 if (len == 0) |
17870 | 410 os << "[]"; |
16468
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
411 else |
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
412 { |
17870 | 413 os << range.base () << ":"; |
16468
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
414 |
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
415 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
|
416 { |
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
417 if (range.inc () != 1) |
17870 | 418 os << range.inc () << ":"; |
16468
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
419 |
17870 | 420 os << range.limit (); |
16468
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
421 } |
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
422 } |
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
423 } |
0f143f68078d
use signal/slot for updating workspace instead of using event listener
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
424 |
4687 | 425 // Skip white space and comments on stream IS. |
426 | |
427 static void | |
428 skip_comments (std::istream& is) | |
429 { | |
430 char c = '\0'; | |
431 while (is.get (c)) | |
432 { | |
433 if (c == ' ' || c == '\t' || c == '\n') | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
434 ; // Skip whitespace on way to beginning of next line. |
4687 | 435 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
436 break; |
4687 | 437 } |
438 | |
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
|
439 skip_until_newline (is, false); |
4687 | 440 } |
441 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
442 bool |
6974 | 443 octave_range::save_ascii (std::ostream& os) |
4687 | 444 { |
445 Range r = range_value (); | |
446 double base = r.base (); | |
447 double limit = r.limit (); | |
448 double inc = r.inc (); | |
20263
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20198
diff
changeset
|
449 octave_idx_type len = r.numel (); |
4687 | 450 |
10735
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
451 if (inc != 0) |
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
452 os << "# base, limit, increment\n"; |
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
453 else |
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
454 os << "# base, length, increment\n"; |
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
455 |
4687 | 456 octave_write_double (os, base); |
457 os << " "; | |
10735
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
458 if (inc != 0) |
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
459 octave_write_double (os, limit); |
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
460 else |
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
461 os << len; |
4687 | 462 os << " "; |
463 octave_write_double (os, inc); | |
464 os << "\n"; | |
465 | |
466 return true; | |
467 } | |
468 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
469 bool |
4687 | 470 octave_range::load_ascii (std::istream& is) |
471 { | |
472 // # base, limit, range comment added by save (). | |
473 skip_comments (is); | |
474 | |
10735
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
475 double base, limit, inc; |
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
476 is >> base >> limit >> inc; |
4687 | 477 |
478 if (!is) | |
479 { | |
480 error ("load: failed to load range constant"); | |
481 return false; | |
482 } | |
483 | |
10735
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
484 if (inc != 0) |
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
485 range = Range (base, limit, inc); |
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
486 else |
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
487 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
|
488 |
4687 | 489 return true; |
490 } | |
491 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
492 bool |
4687 | 493 octave_range::save_binary (std::ostream& os, bool& /* save_as_floats */) |
494 { | |
5760 | 495 char tmp = LS_DOUBLE; |
496 os.write (reinterpret_cast<char *> (&tmp), 1); | |
4687 | 497 Range r = range_value (); |
498 double bas = r.base (); | |
499 double lim = r.limit (); | |
500 double inc = r.inc (); | |
10735
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
501 if (inc == 0) |
20263
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20198
diff
changeset
|
502 lim = r.numel (); |
10735
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
503 |
5760 | 504 os.write (reinterpret_cast<char *> (&bas), 8); |
505 os.write (reinterpret_cast<char *> (&lim), 8); | |
506 os.write (reinterpret_cast<char *> (&inc), 8); | |
4687 | 507 |
508 return true; | |
509 } | |
510 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
511 bool |
4687 | 512 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
|
513 oct_mach_info::float_format /* fmt */) |
4687 | 514 { |
515 char tmp; | |
5760 | 516 if (! is.read (reinterpret_cast<char *> (&tmp), 1)) |
4687 | 517 return false; |
518 double bas, lim, inc; | |
5760 | 519 if (! is.read (reinterpret_cast<char *> (&bas), 8)) |
4687 | 520 return false; |
521 if (swap) | |
4944 | 522 swap_bytes<8> (&bas); |
5760 | 523 if (! is.read (reinterpret_cast<char *> (&lim), 8)) |
4687 | 524 return false; |
525 if (swap) | |
4944 | 526 swap_bytes<8> (&lim); |
5760 | 527 if (! is.read (reinterpret_cast<char *> (&inc), 8)) |
4687 | 528 return false; |
529 if (swap) | |
4944 | 530 swap_bytes<8> (&inc); |
10735
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
531 if (inc != 0) |
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
532 range = Range (bas, lim, inc); |
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
533 else |
d899b2ee6a37
fix saving/loading of constant ranges (bug #30289)
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
534 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
|
535 |
4687 | 536 return true; |
537 } | |
538 | |
539 #if defined (HAVE_HDF5) | |
4944 | 540 |
4687 | 541 // 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
|
542 // 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
|
543 // 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
|
544 // H5T_NATIVE_DOUBLE to save as 'double'). Note that any necessary |
4687 | 545 // conversions are handled automatically by HDF5. |
546 | |
547 static hid_t | |
548 hdf5_make_range_type (hid_t num_type) | |
549 { | |
550 hid_t type_id = H5Tcreate (H5T_COMPOUND, sizeof (double) * 3); | |
551 | |
552 H5Tinsert (type_id, "base", 0 * sizeof (double), num_type); | |
553 H5Tinsert (type_id, "limit", 1 * sizeof (double), num_type); | |
554 H5Tinsert (type_id, "increment", 2 * sizeof (double), num_type); | |
555 | |
556 return type_id; | |
557 } | |
558 | |
19897
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19895
diff
changeset
|
559 #endif |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19895
diff
changeset
|
560 |
4687 | 561 bool |
19897
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19895
diff
changeset
|
562 octave_range::save_hdf5 (octave_hdf5_id loc_id, const char *name, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
563 bool /* save_as_floats */) |
4687 | 564 { |
19897
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19895
diff
changeset
|
565 bool retval = false; |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19895
diff
changeset
|
566 |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19895
diff
changeset
|
567 #if defined (HAVE_HDF5) |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19895
diff
changeset
|
568 |
4792 | 569 hsize_t dimens[3]; |
18099
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17870
diff
changeset
|
570 hid_t space_hid, type_hid, data_hid; |
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17870
diff
changeset
|
571 space_hid = type_hid = data_hid = -1; |
4687 | 572 |
4815 | 573 space_hid = H5Screate_simple (0, dimens, 0); |
4687 | 574 if (space_hid < 0) return false; |
575 | |
576 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
|
577 if (type_hid < 0) |
4687 | 578 { |
579 H5Sclose (space_hid); | |
580 return false; | |
581 } | |
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
|
582 #if HAVE_HDF5_18 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
583 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
|
584 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
|
585 #else |
4687 | 586 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
|
587 #endif |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
588 if (data_hid < 0) |
4687 | 589 { |
590 H5Sclose (space_hid); | |
591 H5Tclose (type_hid); | |
592 return false; | |
593 } | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
594 |
4687 | 595 Range r = range_value (); |
596 double range_vals[3]; | |
597 range_vals[0] = r.base (); | |
20263
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20198
diff
changeset
|
598 range_vals[1] = r.inc () != 0 ? r.limit () : r.numel (); |
4687 | 599 range_vals[2] = r.inc (); |
600 | |
11176
2271261f088a
Address precision issue in ranges saved to HDF5 files
David Bateman <dbateman@free.fr>
parents:
11129
diff
changeset
|
601 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
|
602 range_vals) >= 0) |
2271261f088a
Address precision issue in ranges saved to HDF5 files
David Bateman <dbateman@free.fr>
parents:
11129
diff
changeset
|
603 { |
20263
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20198
diff
changeset
|
604 octave_idx_type nel = r.numel (); |
11176
2271261f088a
Address precision issue in ranges saved to HDF5 files
David Bateman <dbateman@free.fr>
parents:
11129
diff
changeset
|
605 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
|
606 "OCTAVE_RANGE_NELEM", &nel) >= 0; |
2271261f088a
Address precision issue in ranges saved to HDF5 files
David Bateman <dbateman@free.fr>
parents:
11129
diff
changeset
|
607 } |
2271261f088a
Address precision issue in ranges saved to HDF5 files
David Bateman <dbateman@free.fr>
parents:
11129
diff
changeset
|
608 else |
2271261f088a
Address precision issue in ranges saved to HDF5 files
David Bateman <dbateman@free.fr>
parents:
11129
diff
changeset
|
609 retval = false; |
4687 | 610 |
611 H5Dclose (data_hid); | |
612 H5Tclose (type_hid); | |
613 H5Sclose (space_hid); | |
4837 | 614 |
19897
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19895
diff
changeset
|
615 #else |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19895
diff
changeset
|
616 gripe_save ("hdf5"); |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19895
diff
changeset
|
617 #endif |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19895
diff
changeset
|
618 |
4687 | 619 return retval; |
620 } | |
621 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
622 bool |
19897
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19895
diff
changeset
|
623 octave_range::load_hdf5 (octave_hdf5_id loc_id, const char *name) |
4687 | 624 { |
625 bool retval = false; | |
4837 | 626 |
19897
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19895
diff
changeset
|
627 #if defined (HAVE_HDF5) |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19895
diff
changeset
|
628 |
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
|
629 #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
|
630 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
|
631 #else |
4687 | 632 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
|
633 #endif |
4687 | 634 hid_t type_hid = H5Dget_type (data_hid); |
635 | |
636 hid_t range_type = hdf5_make_range_type (H5T_NATIVE_DOUBLE); | |
637 | |
638 if (! hdf5_types_compatible (type_hid, range_type)) | |
639 { | |
4837 | 640 H5Tclose (range_type); |
4687 | 641 H5Dclose (data_hid); |
642 return false; | |
643 } | |
644 | |
645 hid_t space_hid = H5Dget_space (data_hid); | |
646 hsize_t rank = H5Sget_simple_extent_ndims (space_hid); | |
647 | |
648 if (rank != 0) | |
649 { | |
4837 | 650 H5Tclose (range_type); |
4687 | 651 H5Sclose (space_hid); |
652 H5Dclose (data_hid); | |
653 return false; | |
654 } | |
655 | |
656 double rangevals[3]; | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
657 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
|
658 rangevals) >= 0) |
4687 | 659 { |
660 retval = true; | |
11176
2271261f088a
Address precision issue in ranges saved to HDF5 files
David Bateman <dbateman@free.fr>
parents:
11129
diff
changeset
|
661 octave_idx_type nel; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
662 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
|
663 "OCTAVE_RANGE_NELEM", &nel)) |
2271261f088a
Address precision issue in ranges saved to HDF5 files
David Bateman <dbateman@free.fr>
parents:
11129
diff
changeset
|
664 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
|
665 else |
11176
2271261f088a
Address precision issue in ranges saved to HDF5 files
David Bateman <dbateman@free.fr>
parents:
11129
diff
changeset
|
666 { |
2271261f088a
Address precision issue in ranges saved to HDF5 files
David Bateman <dbateman@free.fr>
parents:
11129
diff
changeset
|
667 if (rangevals[2] != 0) |
2271261f088a
Address precision issue in ranges saved to HDF5 files
David Bateman <dbateman@free.fr>
parents:
11129
diff
changeset
|
668 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
|
669 else |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
670 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
|
671 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
|
672 } |
4687 | 673 } |
674 | |
4837 | 675 H5Tclose (range_type); |
4687 | 676 H5Sclose (space_hid); |
677 H5Dclose (data_hid); | |
4837 | 678 |
19897
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19895
diff
changeset
|
679 #else |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19895
diff
changeset
|
680 gripe_load ("hdf5"); |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19895
diff
changeset
|
681 #endif |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19895
diff
changeset
|
682 |
4687 | 683 return retval; |
684 } | |
4944 | 685 |
5900 | 686 mxArray * |
687 octave_range::as_mxArray (void) const | |
688 { | |
689 mxArray *retval = new mxArray (mxDOUBLE_CLASS, dims (), mxREAL); | |
690 | |
691 double *pr = static_cast<double *> (retval->get_data ()); | |
692 | |
6686 | 693 mwSize nel = numel (); |
5900 | 694 |
695 Matrix m = matrix_value (); | |
696 | |
697 const double *p = m.data (); | |
698 | |
6686 | 699 for (mwSize i = 0; i < nel; i++) |
5900 | 700 pr[i] = p[i]; |
701 | |
702 return retval; | |
703 } | |
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
|
704 |
18684
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18416
diff
changeset
|
705 octave_value |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18416
diff
changeset
|
706 octave_range::fast_elem_extract (octave_idx_type n) const |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18416
diff
changeset
|
707 { |
20263
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20198
diff
changeset
|
708 return (n < range.numel ()) ? octave_value (range.elem (n)) |
19895
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
709 : octave_value (); |
18684
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18416
diff
changeset
|
710 } |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18416
diff
changeset
|
711 |
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
|
712 DEFUN (allow_noninteger_range_as_index, args, nargout, |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
713 "-*- texinfo -*-\n\ |
10840 | 714 @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
|
715 @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
|
716 @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
|
717 Query or set the internal variable that controls whether non-integer\n\ |
20198
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19897
diff
changeset
|
718 ranges are allowed as indices.\n\ |
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19897
diff
changeset
|
719 \n\ |
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19897
diff
changeset
|
720 This might be useful for @sc{matlab} compatibility; however, it is still not\n\ |
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19897
diff
changeset
|
721 entirely compatible because @sc{matlab} treats the range expression\n\ |
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19897
diff
changeset
|
722 differently in different contexts.\n\ |
13951
79aa00a94e9e
doc: Document "local" option for configuration variables.
Rik <octave@nomad.inbox5.com>
parents:
13846
diff
changeset
|
723 \n\ |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16468
diff
changeset
|
724 When called from inside a function with the @qcode{\"local\"} option, the\n\ |
20198
075a5e2e1ba5
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19897
diff
changeset
|
725 variable is changed locally for the function and any subroutines it calls.\n\ |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
16468
diff
changeset
|
726 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
|
727 @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
|
728 { |
18129
e473c4853afc
enable non-integer ranges as indices by default and deprecate preference
John W. Eaton <jwe@octave.org>
parents:
17870
diff
changeset
|
729 static bool warned = false; |
e473c4853afc
enable non-integer ranges as indices by default and deprecate preference
John W. Eaton <jwe@octave.org>
parents:
17870
diff
changeset
|
730 if (! warned) |
e473c4853afc
enable non-integer ranges as indices by default and deprecate preference
John W. Eaton <jwe@octave.org>
parents:
17870
diff
changeset
|
731 { |
e473c4853afc
enable non-integer ranges as indices by default and deprecate preference
John W. Eaton <jwe@octave.org>
parents:
17870
diff
changeset
|
732 warned = true; |
e473c4853afc
enable non-integer ranges as indices by default and deprecate preference
John W. Eaton <jwe@octave.org>
parents:
17870
diff
changeset
|
733 warning_with_id ("Octave:deprecated-function", |
e473c4853afc
enable non-integer ranges as indices by default and deprecate preference
John W. Eaton <jwe@octave.org>
parents:
17870
diff
changeset
|
734 "allow_noninteger_range_as_index is obsolete and will be removed from a future version of Octave"); |
e473c4853afc
enable non-integer ranges as indices by default and deprecate preference
John W. Eaton <jwe@octave.org>
parents:
17870
diff
changeset
|
735 } |
e473c4853afc
enable non-integer ranges as indices by default and deprecate preference
John W. Eaton <jwe@octave.org>
parents:
17870
diff
changeset
|
736 |
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
|
737 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
|
738 } |
12863
8f9702abe3e3
test: Add validation for allow_noninteger_range_as_index()
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
739 |
8f9702abe3e3
test: Add validation for allow_noninteger_range_as_index()
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
740 /* |
8f9702abe3e3
test: Add validation for allow_noninteger_range_as_index()
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
741 %!test |
8f9702abe3e3
test: Add validation for allow_noninteger_range_as_index()
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
742 %! 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
|
743 %! 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
|
744 %! 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
|
745 %! unwind_protect |
14040
a64f8b6f63e9
test: simplify test for allow_noninteger_range_as_index
Rik <octave@nomad.inbox5.com>
parents:
13951
diff
changeset
|
746 %! 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
|
747 %! 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
|
748 %! 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
|
749 %! 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
|
750 %! 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
|
751 %! 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
|
752 %! 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
|
753 %! 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
|
754 %! 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
|
755 %! 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
|
756 %! 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
|
757 %! end_unwind_protect |
12863
8f9702abe3e3
test: Add validation for allow_noninteger_range_as_index()
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
758 */ |