Mercurial > octave
annotate liboctave/array/Range.cc @ 23398:d4ef04757ead
eliminate some compiler warnings
* oct-stream.cc (textscan::numeric_delim): Delete unused data member.
* pr-output.cc (set_format): Delete unused functions.
* xdiv.cc (result_ok): Delete unused function.
* oct-parse.in.yy: Tag token with octave:: as needed.
* pt-eval.cc: Tag tree_break_command with octave:: as needed.
* pt-loop.h (tree_while_command::compiled): Only define data member if
HAVE_LLVM is defined.
* Range.cc (tceil): Delete unused function.
* quit.cc (octave_throw_exit_exception, octave_rethrow_exception):
Avoid deprecated declaration warning.
* quit.h (octave_throw_exit_exception): Deprecate.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 13 Apr 2017 14:25:16 -0400 |
parents | 092078913d54 |
children | 3f1bf237908b |
rev | line source |
---|---|
3 | 1 /* |
2 | |
23219
3ac9f9ecfae5
maint: Update copyright dates.
John W. Eaton <jwe@octave.org>
parents:
23083
diff
changeset
|
3 Copyright (C) 1993-2017 John W. Eaton |
3 | 4 |
5 This file is part of Octave. | |
6 | |
7 Octave is free software; you can redistribute it and/or modify it | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
8 under the terms of the GNU General Public License as published by |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
9 the Free Software Foundation; either version 3 of the License, or |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
10 (at your option) any later version. |
3 | 11 |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
12 Octave is distributed in the hope that it will be useful, but |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
13 WITHOUT ANY WARRANTY; without even the implied warranty of |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
15 GNU General Public License for more details. |
3 | 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/>. | |
3 | 20 |
21 */ | |
22 | |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21321
diff
changeset
|
23 #if defined (HAVE_CONFIG_H) |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21229
diff
changeset
|
24 # include "config.h" |
3 | 25 #endif |
26 | |
1546 | 27 #include <cfloat> |
1367 | 28 |
3503 | 29 #include <iostream> |
6490 | 30 #include <limits> |
3 | 31 |
32 #include "Range.h" | |
7458 | 33 #include "lo-error.h" |
2383 | 34 #include "lo-mappers.h" |
7231 | 35 #include "lo-math.h" |
2383 | 36 #include "lo-utils.h" |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
37 #include "Array-util.h" |
2383 | 38 |
39 bool | |
40 Range::all_elements_are_ints (void) const | |
41 { | |
42 // If the base and increment are ints, the final value in the range | |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
43 // will also be an integer, even if the limit is not. If there is one |
6457 | 44 // or fewer elements only the base needs to be an integer |
2383 | 45 |
21782
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
46 return (! (octave::math::isnan (rng_base) || octave::math::isnan (rng_inc)) |
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
47 && (octave::math::nint_big (rng_base) == rng_base || rng_numel < 1) |
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
48 && (octave::math::nint_big (rng_inc) == rng_inc || rng_numel <= 1)); |
2383 | 49 } |
645 | 50 |
51 Matrix | |
52 Range::matrix_value (void) const | |
53 { | |
20974
1edf15793cac
maint: Use is_empty rather than "numel () == 0" for clarity.
Rik <rik@octave.org>
parents:
20531
diff
changeset
|
54 if (rng_numel > 0 && cache.is_empty ()) |
645 | 55 { |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
56 cache.resize (1, rng_numel); |
20531
dcfbf4c1c3c8
eliminate trailing whitespace and tabs from sources
John W. Eaton <jwe@octave.org>
parents:
20513
diff
changeset
|
57 |
20513 | 58 // The first element must always be *exactly* the base. |
59 // E.g, -0 would otherwise become +0 in the loop (-0 + 0*increment). | |
60 cache(0) = rng_base; | |
61 | |
645 | 62 double b = rng_base; |
63 double increment = rng_inc; | |
20513 | 64 for (octave_idx_type i = 1; i < rng_numel - 1; i++) |
65 cache(i) = b + i * increment; | |
4791 | 66 |
20513 | 67 cache(rng_numel - 1) = rng_limit; |
645 | 68 } |
69 | |
4810 | 70 return cache; |
645 | 71 } |
3 | 72 |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
73 double |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
74 Range::checkelem (octave_idx_type i) const |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
75 { |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
76 if (i < 0 || i >= rng_numel) |
22327
d0562b3159c7
move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22323
diff
changeset
|
77 octave::err_index_out_of_range (1, 1, i+1, rng_numel); |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
78 |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
79 if (i == 0) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
80 return rng_base; |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
81 else if (i < rng_numel - 1) |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
82 return rng_base + i * rng_inc; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
83 else |
20513 | 84 return rng_limit; |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
85 } |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
86 |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
87 double |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
88 Range::elem (octave_idx_type i) const |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
89 { |
21229
a83e7a384ee0
create and install a subset of config.h in octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21202
diff
changeset
|
90 #if defined (OCTAVE_ENABLE_BOUNDS_CHECK) |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
91 return checkelem (i); |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
92 #else |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
93 if (i == 0) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
94 return rng_base; |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
95 else if (i < rng_numel - 1) |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
96 return rng_base + i * rng_inc; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
97 else |
20513 | 98 return rng_limit; |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
99 #endif |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
100 } |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
101 |
16186
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
102 // Helper class used solely for idx_vector.loop () function call |
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
103 class __rangeidx_helper |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
104 { |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
105 public: |
16186
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
106 __rangeidx_helper (double *a, double b, double i, double l, octave_idx_type n) |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
107 : array (a), base (b), inc (i), limit (l), nmax (n-1) { } |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
108 |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
109 void operator () (octave_idx_type i) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
110 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
111 if (i == 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
112 *array++ = base; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
113 else if (i < nmax) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
114 *array++ = base + i * inc; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
115 else |
20513 | 116 *array++ = limit; |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
117 } |
16186
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
118 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
119 private: |
16186
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
120 |
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
121 double *array, base, inc, limit; |
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
122 octave_idx_type nmax; |
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
123 |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
124 }; |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
125 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
126 Array<double> |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
127 Range::index (const idx_vector& i) const |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
128 { |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
129 Array<double> retval; |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
130 |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
131 octave_idx_type n = rng_numel; |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
132 |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
133 if (i.is_colon ()) |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
134 { |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
135 retval = matrix_value ().reshape (dim_vector (rng_numel, 1)); |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
136 } |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
137 else |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
138 { |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
139 if (i.extent (n) != n) |
22327
d0562b3159c7
move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22323
diff
changeset
|
140 octave::err_index_out_of_range (1, 1, i.extent (n), n); // throws |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
141 |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
142 dim_vector rd = i.orig_dimensions (); |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
143 octave_idx_type il = i.length (n); |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
144 |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
145 // taken from Array.cc. |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
146 if (n != 1 && rd.is_vector ()) |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
147 rd = dim_vector (1, il); |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
148 |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
149 retval.clear (rd); |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
150 |
16186
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
151 // idx_vector loop across all values in i, |
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
152 // executing __rangeidx_helper (i) for each i |
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
153 i.loop (n, __rangeidx_helper (retval.fortran_vec (), |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
154 rng_base, rng_inc, rng_limit, rng_numel)); |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
155 } |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
156 |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
157 return retval; |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
158 } |
4810 | 159 |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
160 // NOTE: max and min only return useful values if numel > 0. |
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
161 // do_minmax_body() in max.cc avoids calling Range::min/max if numel == 0. |
208 | 162 |
163 double | |
164 Range::min (void) const | |
165 { | |
166 double retval = 0.0; | |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
167 if (rng_numel > 0) |
208 | 168 { |
169 if (rng_inc > 0) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
170 retval = rng_base; |
208 | 171 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
172 { |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
173 retval = rng_base + (rng_numel - 1) * rng_inc; |
4791 | 174 |
20513 | 175 // Require '<=' test. See note in max (). |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
176 if (retval <= rng_limit) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
177 retval = rng_limit; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
178 } |
4791 | 179 |
208 | 180 } |
181 return retval; | |
182 } | |
183 | |
184 double | |
185 Range::max (void) const | |
186 { | |
187 double retval = 0.0; | |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
188 if (rng_numel > 0) |
208 | 189 { |
190 if (rng_inc > 0) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
191 { |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
192 retval = rng_base + (rng_numel - 1) * rng_inc; |
4791 | 193 |
20513 | 194 // On some machines (x86 with extended precision floating point |
195 // arithmetic, for example) it is possible that we can overshoot the | |
196 // limit by approximately the machine precision even though we were | |
197 // very careful in our calculation of the number of elements. | |
198 // Therefore, we clip the result to the limit if it overshoots. | |
199 // The test also includes equality (>= rng_limit) to have expressions | |
200 // such as -5:1:-0 result in a -0 endpoint. | |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
201 if (retval >= rng_limit) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
202 retval = rng_limit; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
203 } |
208 | 204 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
205 retval = rng_base; |
208 | 206 } |
207 return retval; | |
208 } | |
209 | |
210 void | |
7458 | 211 Range::sort_internal (bool ascending) |
208 | 212 { |
20513 | 213 if ((ascending && rng_base > rng_limit && rng_inc < 0.0) |
214 || (! ascending && rng_base < rng_limit && rng_inc > 0.0)) | |
208 | 215 { |
20513 | 216 std::swap (rng_base, rng_limit); |
7458 | 217 rng_inc = -rng_inc; |
218 clear_cache (); | |
219 } | |
220 } | |
221 | |
222 void | |
223 Range::sort_internal (Array<octave_idx_type>& sidx, bool ascending) | |
224 { | |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
225 octave_idx_type nel = numel (); |
7458 | 226 |
227 sidx.resize (dim_vector (1, nel)); | |
228 | |
229 octave_idx_type *psidx = sidx.fortran_vec (); | |
230 | |
231 bool reverse = false; | |
232 | |
20513 | 233 if ((ascending && rng_base > rng_limit && rng_inc < 0.0) |
234 || (! ascending && rng_base < rng_limit && rng_inc > 0.0)) | |
7458 | 235 { |
20513 | 236 std::swap (rng_base, rng_limit); |
7458 | 237 rng_inc = -rng_inc; |
238 clear_cache (); | |
239 reverse = true; | |
240 } | |
241 | |
242 octave_idx_type tmp = reverse ? nel - 1 : 0; | |
7467 | 243 octave_idx_type stp = reverse ? -1 : 1; |
7458 | 244 |
7467 | 245 for (octave_idx_type i = 0; i < nel; i++, tmp += stp) |
7458 | 246 psidx[i] = tmp; |
247 | |
248 } | |
249 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
250 Matrix |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7467
diff
changeset
|
251 Range::diag (octave_idx_type k) const |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7467
diff
changeset
|
252 { |
8971 | 253 return matrix_value ().diag (k); |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7467
diff
changeset
|
254 } |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7467
diff
changeset
|
255 |
7458 | 256 Range |
257 Range::sort (octave_idx_type dim, sortmode mode) const | |
258 { | |
259 Range retval = *this; | |
260 | |
261 if (dim == 1) | |
262 { | |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7458
diff
changeset
|
263 if (mode == ASCENDING) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
264 retval.sort_internal (true); |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7458
diff
changeset
|
265 else if (mode == DESCENDING) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
266 retval.sort_internal (false); |
7458 | 267 } |
268 else if (dim != 0) | |
269 (*current_liboctave_error_handler) ("Range::sort: invalid dimension"); | |
270 | |
271 return retval; | |
272 } | |
273 | |
274 Range | |
275 Range::sort (Array<octave_idx_type>& sidx, octave_idx_type dim, | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
276 sortmode mode) const |
7458 | 277 { |
278 Range retval = *this; | |
279 | |
280 if (dim == 1) | |
281 { | |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7458
diff
changeset
|
282 if (mode == ASCENDING) |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
283 retval.sort_internal (sidx, true); |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7458
diff
changeset
|
284 else if (mode == DESCENDING) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
285 retval.sort_internal (sidx, false); |
7458 | 286 } |
287 else if (dim != 0) | |
288 (*current_liboctave_error_handler) ("Range::sort: invalid dimension"); | |
289 | |
290 return retval; | |
208 | 291 } |
292 | |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
293 sortmode |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
294 Range::is_sorted (sortmode mode) const |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
295 { |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
296 if (rng_numel > 1 && rng_inc > 0) |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
297 mode = (mode == DESCENDING) ? UNSORTED : ASCENDING; |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
298 else if (rng_numel > 1 && rng_inc < 0) |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
299 mode = (mode == ASCENDING) ? UNSORTED : DESCENDING; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
300 else |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
301 mode = mode ? mode : ASCENDING; |
8742
d2b06871afac
add missing return statement
Jaroslav Hajek <highegg@gmail.com>
parents:
8721
diff
changeset
|
302 |
d2b06871afac
add missing return statement
Jaroslav Hajek <highegg@gmail.com>
parents:
8721
diff
changeset
|
303 return mode; |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
304 } |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
305 |
20513 | 306 void |
307 Range::set_base (double b) | |
308 { | |
309 if (rng_base != b) | |
310 { | |
311 rng_base = b; | |
312 | |
21321
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
313 init (); |
20513 | 314 } |
315 } | |
316 | |
317 void | |
318 Range::set_limit (double l) | |
319 { | |
320 if (rng_limit != l) | |
321 { | |
322 rng_limit = l; | |
323 | |
21321
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
324 init (); |
20513 | 325 } |
326 } | |
327 | |
328 void | |
329 Range::set_inc (double i) | |
330 { | |
331 if (rng_inc != i) | |
332 { | |
333 rng_inc = i; | |
334 | |
21321
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
335 init (); |
20513 | 336 } |
337 } | |
338 | |
3504 | 339 std::ostream& |
340 operator << (std::ostream& os, const Range& a) | |
3 | 341 { |
342 double b = a.base (); | |
343 double increment = a.inc (); | |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
344 octave_idx_type num_elem = a.numel (); |
3 | 345 |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
346 if (num_elem > 1) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
347 { |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
348 // First element must be the base *exactly* (-0). |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
349 os << b << " "; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
350 for (octave_idx_type i = 1; i < num_elem-1; i++) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
351 os << b + i * increment << " "; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
352 } |
3 | 353 |
20513 | 354 // Print out exactly the last element, rather than a calculated last element. |
355 os << a.rng_limit << "\n"; | |
3 | 356 |
357 return os; | |
358 } | |
359 | |
3504 | 360 std::istream& |
361 operator >> (std::istream& is, Range& a) | |
3 | 362 { |
208 | 363 is >> a.rng_base; |
3 | 364 if (is) |
365 { | |
20513 | 366 double tmp_rng_limit; |
367 is >> tmp_rng_limit; | |
368 | |
3 | 369 if (is) |
20513 | 370 is >> a.rng_inc; |
371 | |
372 // Clip the rng_limit to the true limit and rebuild numel, clear cache | |
373 a.set_limit (tmp_rng_limit); | |
3 | 374 } |
375 | |
376 return is; | |
377 } | |
378 | |
2599 | 379 Range |
380 operator - (const Range& r) | |
381 { | |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
382 return Range (-r.base (), -r.limit (), -r.inc (), r.numel ()); |
2599 | 383 } |
384 | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
385 Range operator + (double x, const Range& r) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
386 { |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
387 Range result (x + r.base (), x + r.limit (), r.inc (), r.numel ()); |
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
388 if (result.rng_numel < 0) |
8971 | 389 result.cache = x + r.matrix_value (); |
390 | |
391 return result; | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
392 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
393 |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
394 Range operator + (const Range& r, double x) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
395 { |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
396 Range result (r.base () + x, r.limit () + x, r.inc (), r.numel ()); |
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
397 if (result.rng_numel < 0) |
8971 | 398 result.cache = r.matrix_value () + x; |
399 | |
400 return result; | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
401 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
402 |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
403 Range operator - (double x, const Range& r) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
404 { |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
405 Range result (x - r.base (), x - r.limit (), -r.inc (), r.numel ()); |
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
406 if (result.rng_numel < 0) |
8971 | 407 result.cache = x - r.matrix_value (); |
408 | |
409 return result; | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
410 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
411 |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
412 Range operator - (const Range& r, double x) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
413 { |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
414 Range result (r.base () - x, r.limit () - x, r.inc (), r.numel ()); |
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
415 if (result.rng_numel < 0) |
8971 | 416 result.cache = r.matrix_value () - x; |
417 | |
418 return result; | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
419 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
420 |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
421 Range operator * (double x, const Range& r) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
422 { |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
423 Range result (x * r.base (), x * r.limit (), x * r.inc (), r.numel ()); |
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
424 if (result.rng_numel < 0) |
8971 | 425 result.cache = x * r.matrix_value (); |
426 | |
427 return result; | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
428 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
429 |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
430 Range operator * (const Range& r, double x) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
431 { |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
432 Range result (r.base () * x, r.limit () * x, r.inc () * x, r.numel ()); |
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
433 if (result.rng_numel < 0) |
8971 | 434 result.cache = r.matrix_value () * x; |
435 | |
436 return result; | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
437 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
438 |
1546 | 439 // C See Knuth, Art Of Computer Programming, Vol. 1, Problem 1.2.4-5. |
440 // C | |
441 // C===Tolerant FLOOR function. | |
442 // C | |
443 // C X - is given as a Double Precision argument to be operated on. | |
444 // C It is assumed that X is represented with M mantissa bits. | |
445 // C CT - is given as a Comparison Tolerance such that | |
446 // C 0.LT.CT.LE.3-SQRT(5)/2. If the relative difference between | |
447 // C X and A whole number is less than CT, then TFLOOR is | |
448 // C returned as this whole number. By treating the | |
449 // C floating-point numbers as a finite ordered set note that | |
450 // C the heuristic EPS=2.**(-(M-1)) and CT=3*EPS causes | |
451 // C arguments of TFLOOR/TCEIL to be treated as whole numbers | |
452 // C if they are exactly whole numbers or are immediately | |
453 // C adjacent to whole number representations. Since EPS, the | |
454 // C "distance" between floating-point numbers on the unit | |
455 // C interval, and M, the number of bits in X'S mantissa, exist | |
456 // C on every floating-point computer, TFLOOR/TCEIL are | |
457 // C consistently definable on every floating-point computer. | |
458 // C | |
459 // C For more information see the following references: | |
460 // C (1) P. E. Hagerty, "More On Fuzzy Floor And Ceiling," APL QUOTE | |
461 // C QUAD 8(4):20-24, June 1978. Note that TFLOOR=FL5. | |
462 // C (2) L. M. Breed, "Definitions For Fuzzy Floor And Ceiling", APL | |
463 // C QUOTE QUAD 8(3):16-23, March 1978. This paper cites FL1 through | |
464 // C FL5, the history of five years of evolutionary development of | |
465 // C FL5 - the seven lines of code below - by open collaboration | |
466 // C and corroboration of the mathematical-computing community. | |
467 // C | |
468 // C Penn State University Center for Academic Computing | |
469 // C H. D. Knoble - August, 1978. | |
470 | |
471 static inline double | |
472 tfloor (double x, double ct) | |
473 { | |
474 // C---------FLOOR(X) is the largest integer algebraically less than | |
475 // C or equal to X; that is, the unfuzzy FLOOR function. | |
476 | |
477 // DINT (X) = X - DMOD (X, 1.0); | |
478 // FLOOR (X) = DINT (X) - DMOD (2.0 + DSIGN (1.0, X), 3.0); | |
479 | |
480 // C---------Hagerty's FL5 function follows... | |
481 | |
482 double q = 1.0; | |
483 | |
484 if (x < 0.0) | |
485 q = 1.0 - ct; | |
486 | |
487 double rmax = q / (2.0 - ct); | |
488 | |
21942
aab79a1885cc
limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
489 double t1 = 1.0 + std::floor (x); |
1546 | 490 t1 = (ct / q) * (t1 < 0.0 ? -t1 : t1); |
491 t1 = rmax < t1 ? rmax : t1; | |
492 t1 = ct > t1 ? ct : t1; | |
21942
aab79a1885cc
limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
493 t1 = std::floor (x + t1); |
1546 | 494 |
1555 | 495 if (x <= 0.0 || (t1 - x) < rmax) |
1546 | 496 return t1; |
497 else | |
498 return t1 - 1.0; | |
499 } | |
500 | |
3753 | 501 static inline bool |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
502 teq (double u, double v, |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
503 double ct = 3.0 * std::numeric_limits<double>::epsilon ()) |
3753 | 504 { |
505 double tu = fabs (u); | |
506 double tv = fabs (v); | |
507 | |
508 return fabs (u - v) < ((tu > tv ? tu : tv) * ct); | |
509 } | |
510 | |
5275 | 511 octave_idx_type |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
512 Range::numel_internal (void) const |
1360 | 513 { |
5275 | 514 octave_idx_type retval = -1; |
3 | 515 |
3858 | 516 if (rng_inc == 0 |
517 || (rng_limit > rng_base && rng_inc < 0) | |
518 || (rng_limit < rng_base && rng_inc > 0)) | |
519 { | |
520 retval = 0; | |
521 } | |
522 else | |
523 { | |
15220
61822c866ba1
use std::numeric_limits<T>::epsilon in C++ code
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
524 double ct = 3.0 * std::numeric_limits<double>::epsilon (); |
3 | 525 |
3858 | 526 double tmp = tfloor ((rng_limit - rng_base + rng_inc) / rng_inc, ct); |
527 | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
528 octave_idx_type n_elt = (tmp > 0.0 ? static_cast<octave_idx_type> (tmp) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
529 : 0); |
398 | 530 |
3858 | 531 // If the final element that we would compute for the range is |
532 // equal to the limit of the range, or is an adjacent floating | |
533 // point number, accept it. Otherwise, try a range with one | |
534 // fewer element. If that fails, try again with one more | |
535 // element. | |
536 // | |
537 // I'm not sure this is very good, but it seems to work better than | |
538 // just using tfloor as above. For example, without it, the | |
539 // expression 1.8:0.05:1.9 fails to produce the expected result of | |
540 // [1.8, 1.85, 1.9]. | |
3753 | 541 |
3858 | 542 if (! teq (rng_base + (n_elt - 1) * rng_inc, rng_limit)) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
543 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
544 if (teq (rng_base + (n_elt - 2) * rng_inc, rng_limit)) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
545 n_elt--; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
546 else if (teq (rng_base + n_elt * rng_inc, rng_limit)) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
547 n_elt++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
548 } |
3858 | 549 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
550 retval = (n_elt < std::numeric_limits<octave_idx_type>::max () - 1) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
551 ? n_elt : -1; |
3753 | 552 } |
553 | |
3858 | 554 return retval; |
3 | 555 } |
21321
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
556 |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
557 double |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
558 Range::limit_internal (void) const |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
559 { |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
560 double tmp_limit = rng_limit; |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
561 |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
562 if (rng_inc > 0) |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
563 tmp_limit = max (); |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
564 else |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
565 tmp_limit = min (); |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
566 |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
567 return (tmp_limit != rng_limit) ? tmp_limit : rng_limit; |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
568 } |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
569 |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
570 void |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
571 Range::init (void) |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
572 { |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
573 rng_numel = numel_internal (); |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
574 rng_limit = limit_internal (); |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
575 |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
576 clear_cache (); |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
577 } |