annotate liboctave/array/Range.cc @ 29569:29a1f8fd8ee6

move idx_vector classes inside octave namespace * idx-vector.h, idx-vector.cc: Move idx_vector classes inside octave namespace. Update all uses outside of octave namespace to use octave:: tag. * Sparse.h: Eliminate forward declaration of idx_vector.
author John W. Eaton <jwe@octave.org>
date Wed, 28 Apr 2021 13:46:02 -0400
parents 7854d5752dd2
children 4c88a452519c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ////////////////////////////////////////////////////////////////////////
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 //
29358
0a5b15007766 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
3 // Copyright (C) 1993-2021 The Octave Project Developers
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
4 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 // See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 // distribution or <https://octave.org/copyright/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
7 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
8 // This file is part of Octave.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
9 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
10 // Octave is free software: you can redistribute it and/or modify it
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
11 // under the terms of the GNU General Public License as published by
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
12 // the Free Software Foundation, either version 3 of the License, or
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
13 // (at your option) any later version.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
14 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
15 // Octave is distributed in the hope that it will be useful, but
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
18 // GNU General Public License for more details.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
19 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
20 // You should have received a copy of the GNU General Public License
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
21 // along with Octave; see the file COPYING. If not, see
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
22 // <https://www.gnu.org/licenses/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ////////////////////////////////////////////////////////////////////////
3
9a4c07481e61 [project @ 1993-08-08 01:20:23 by jwe]
jwe
parents:
diff changeset
25
21724
aba2e6293dd8 use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents: 21321
diff changeset
26 #if defined (HAVE_CONFIG_H)
21301
40de9f8f23a6 Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents: 21229
diff changeset
27 # include "config.h"
3
9a4c07481e61 [project @ 1993-08-08 01:20:23 by jwe]
jwe
parents:
diff changeset
28 #endif
9a4c07481e61 [project @ 1993-08-08 01:20:23 by jwe]
jwe
parents:
diff changeset
29
23443
3f1bf237908b maint: Eliminate <cfloat.h> header from liboctave files.
Rik <rik@octave.org>
parents: 23398
diff changeset
30 #include <cmath>
1367
9f9131a8d706 [project @ 1995-09-06 00:07:03 by jwe]
jwe
parents: 1360
diff changeset
31
25438
cb1606f78f6b prefer <istream>, <ostream>, or <iosfwd> to <iostream> where possible
John W. Eaton <jwe@octave.org>
parents: 25054
diff changeset
32 #include <istream>
6490
0ad7655cf2bc [project @ 2007-04-05 02:44:34 by jwe]
jwe
parents: 6457
diff changeset
33 #include <limits>
25438
cb1606f78f6b prefer <istream>, <ostream>, or <iosfwd> to <iostream> where possible
John W. Eaton <jwe@octave.org>
parents: 25054
diff changeset
34 #include <ostream>
3
9a4c07481e61 [project @ 1993-08-08 01:20:23 by jwe]
jwe
parents:
diff changeset
35
23443
3f1bf237908b maint: Eliminate <cfloat.h> header from liboctave files.
Rik <rik@octave.org>
parents: 23398
diff changeset
36 #include "Array-util.h"
3
9a4c07481e61 [project @ 1993-08-08 01:20:23 by jwe]
jwe
parents:
diff changeset
37 #include "Range.h"
7458
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
38 #include "lo-error.h"
2383
096529066838 [project @ 1996-10-12 17:50:47 by jwe]
jwe
parents: 1993
diff changeset
39 #include "lo-mappers.h"
096529066838 [project @ 1996-10-12 17:50:47 by jwe]
jwe
parents: 1993
diff changeset
40 #include "lo-utils.h"
096529066838 [project @ 1996-10-12 17:50:47 by jwe]
jwe
parents: 1993
diff changeset
41
28635
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
42 namespace octave
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
43 {
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
44 template <typename T>
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
45 T xtfloor (T x, T ct)
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
46 {
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
47 // C---------FLOOR(X) is the largest integer algebraically less than
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
48 // C or equal to X; that is, the unfuzzy FLOOR function.
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
49
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
50 // DINT (X) = X - DMOD (X, 1.0);
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
51 // FLOOR (X) = DINT (X) - DMOD (2.0 + DSIGN (1.0, X), 3.0);
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
52
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
53 // C---------Hagerty's FL5 function follows...
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
54
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
55 T q = 1;
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
56
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
57 if (x < 0)
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
58 q = 1 - ct;
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
59
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
60 T rmax = q / (2 - ct);
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
61
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
62 T t1 = 1 + std::floor (x);
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
63 t1 = (ct / q) * (t1 < 0 ? -t1 : t1);
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
64 t1 = (rmax < t1 ? rmax : t1);
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
65 t1 = (ct > t1 ? ct : t1);
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
66 t1 = std::floor (x + t1);
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
67
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
68 if (x <= 0 || (t1 - x) < rmax)
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
69 return t1;
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
70 else
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
71 return t1 - 1;
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
72 }
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
73
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
74 template <typename T>
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
75 bool xteq (T u, T v, T ct = 3 * std::numeric_limits<T>::epsilon ())
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
76 {
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
77 T tu = std::abs (u);
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
78 T tv = std::abs (v);
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
79
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
80 return std::abs (u - v) < ((tu > tv ? tu : tv) * ct);
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
81 }
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
82
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
83 template <typename T>
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
84 octave_idx_type xnumel_internal (T base, T limit, T inc)
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
85 {
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
86 octave_idx_type retval = -1;
28857
43ad651cf5a0 eliminate unnecessary uses of octave:: namespace qualifier
John W. Eaton <jwe@octave.org>
parents: 28636
diff changeset
87 if (! math::isfinite (base) || ! math::isfinite (inc)
43ad651cf5a0 eliminate unnecessary uses of octave:: namespace qualifier
John W. Eaton <jwe@octave.org>
parents: 28636
diff changeset
88 || math::isnan (limit))
28635
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
89 retval = -2;
28857
43ad651cf5a0 eliminate unnecessary uses of octave:: namespace qualifier
John W. Eaton <jwe@octave.org>
parents: 28636
diff changeset
90 else if (math::isinf (limit)
28635
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
91 && ((inc > 0 && limit > 0)
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
92 || (inc < 0 && limit < 0)))
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
93 retval = std::numeric_limits<octave_idx_type>::max () - 1;
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
94 else if (inc == 0
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
95 || (limit > base && inc < 0)
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
96 || (limit < base && inc > 0))
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
97 {
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
98 retval = 0;
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
99 }
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
100 else
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
101 {
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
102 T ct = 3 * std::numeric_limits<T>::epsilon ();
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
103
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
104 T tmp = xtfloor ((limit - base + inc) / inc, ct);
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
105
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
106 octave_idx_type n_elt
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
107 = (tmp > 0 ? static_cast<octave_idx_type> (tmp) : 0);
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
108
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
109 // If the final element that we would compute for the range is
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
110 // equal to the limit of the range, or is an adjacent floating
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
111 // point number, accept it. Otherwise, try a range with one
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
112 // fewer element. If that fails, try again with one more
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
113 // element.
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
114 //
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
115 // I'm not sure this is very good, but it seems to work better
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
116 // than just using tfloor as above. For example, without it,
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
117 // the expression 1.8:0.05:1.9 fails to produce the expected
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
118 // result of [1.8, 1.85, 1.9].
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
119
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
120 if (! xteq (base + (n_elt - 1) * inc, limit))
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
121 {
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
122 if (xteq (base + (n_elt - 2) * inc, limit))
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
123 n_elt--;
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
124 else if (xteq (base + n_elt * inc, limit))
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
125 n_elt++;
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
126 }
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
127
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
128 retval = (n_elt < std::numeric_limits<octave_idx_type>::max () - 1
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
129 ? n_elt : -1);
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
130 }
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
131
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
132 return retval;
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
133 }
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
134
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
135 template <typename T>
29353
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
136 bool xall_elements_are_ints (T base, T inc, T final_val, octave_idx_type nel)
28635
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
137 {
29353
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
138 // If the range is empty or NaN then there are no elements so there
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
139 // can be no int elements.
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
140 if (nel == 0 || math::isnan (final_val))
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
141 return false;
28635
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
142
29353
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
143 // If the base and increment are ints, all elements will be
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
144 // integers.
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
145 if (math::nint_big (base) == base && math::nint_big (inc) == inc)
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
146 return true;
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
147
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
148 // If the range has only one element, then the base needs to be an
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
149 // integer.
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
150 if (nel == 1 && math::nint_big (base))
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
151 return true;
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
152
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
153 return false;
28635
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
154 }
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
155
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
156 template <typename T>
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
157 T
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
158 xfinal_value (T base, T limit, T inc, octave_idx_type nel)
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
159 {
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
160 T retval = T (0);
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
161
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
162 if (nel <= 1)
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
163 return base;
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
164
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
165 // If increment is 0, then numel should also be zero.
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
166
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
167 retval = base + (nel - 1) * inc;
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
168
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
169 // On some machines (x86 with extended precision floating point
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
170 // arithmetic, for example) it is possible that we can overshoot
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
171 // the limit by approximately the machine precision even though
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
172 // we were very careful in our calculation of the number of
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
173 // elements. Therefore, we clip the result to the limit if it
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
174 // overshoots.
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
175
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
176 // NOTE: The test also includes equality (>= limit) to have
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
177 // expressions such as -5:1:-0 result in a -0 endpoint.
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
178
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
179 if ((inc > T (0) && retval >= limit) || (inc < T (0) && retval <= limit))
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
180 retval = limit;
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
181
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
182 // If all elements are integers, then ensure the final value is.
29353
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
183 // Note that we pass the preliminary computed final value to
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
184 // xall_elements_are_ints, but it only checks whether that value is
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
185 // NaN.
28635
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
186
29353
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
187 if (xall_elements_are_ints (base, inc, retval, nel))
28635
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
188 retval = std::round (retval);
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
189
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
190 return retval;
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
191 }
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
192
29352
03c283f73b9a specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents: 29351
diff changeset
193 template <typename T>
03c283f73b9a specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents: 29351
diff changeset
194 void
03c283f73b9a specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents: 29351
diff changeset
195 xinit (T base, T limit, T inc, T& final_val, octave_idx_type& nel)
03c283f73b9a specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents: 29351
diff changeset
196 {
29353
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
197 // Catch obvious NaN ranges.
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
198 if (math::isnan (base) || math::isnan (limit) || math::isnan (inc))
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
199 {
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
200 final_val = numeric_limits<T>::NaN ();
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
201 nel = 1;
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
202 return;
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
203 }
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
204
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
205 // Catch empty ranges.
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
206 if (inc == 0
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
207 || (limit < base && inc > 0)
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
208 || (limit > base && inc < 0))
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
209 {
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
210 nel = 0;
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
211 return;
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
212 }
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
213
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
214 // The following case also catches Inf values for increment when
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
215 // there will be only one element.
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
216
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
217 if ((limit <= base && base + inc < limit)
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
218 || (limit >= base && base + inc > limit))
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
219 {
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
220 final_val = base;
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
221 nel = 1;
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
222 return;
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
223 }
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
224
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
225 // Any other calculations with Inf will give us either a NaN range
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
226 // or an infinite nember of elements.
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
227
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
228 T dnel = (limit - base) / inc;
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
229 if (math::isnan (dnel))
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
230 {
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
231 nel = 1;
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
232 final_val = numeric_limits<T>::NaN ();
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
233 return;
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
234 }
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
235
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
236 if (dnel > 0 && math::isinf (dnel))
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
237 {
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
238 // FIXME: Should this be an immediate error?
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
239 nel = std::numeric_limits<octave_idx_type>::max ();
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
240
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
241 // FIXME: Will this do the right thing in all cases?
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
242 final_val = xfinal_value (base, limit, inc, nel);
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
243
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
244 return;
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
245 }
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
246
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
247 // Now that we have handled all the special cases, we can compute
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
248 // the number of elements and the final value in a way that attempts
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
249 // to avoid rounding errors as much as possible.
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
250
29352
03c283f73b9a specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents: 29351
diff changeset
251 nel = xnumel_internal (base, limit, inc);
03c283f73b9a specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents: 29351
diff changeset
252 final_val = xfinal_value (base, limit, inc, nel);
03c283f73b9a specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents: 29351
diff changeset
253 }
03c283f73b9a specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents: 29351
diff changeset
254
29353
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
255 template <typename T>
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
256 bool
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
257 xis_storable (T base, T limit, octave_idx_type nel)
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
258 {
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
259 return ! (nel > 1 && (math::isinf (base) || math::isinf (limit)));
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
260 }
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
261
28635
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
262 template <>
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
263 bool
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
264 range<double>::all_elements_are_ints (void) const
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
265 {
29353
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
266 return xall_elements_are_ints (m_base, m_increment, m_final, m_numel);
28635
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
267 }
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
268
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
269 template <>
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
270 bool
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
271 range<float>::all_elements_are_ints (void) const
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
272 {
29353
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
273 return xall_elements_are_ints (m_base, m_increment, m_final, m_numel);
28635
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
274 }
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
275
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
276 template <>
29352
03c283f73b9a specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents: 29351
diff changeset
277 void
03c283f73b9a specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents: 29351
diff changeset
278 range<double>::init (void)
28635
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
279 {
29352
03c283f73b9a specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents: 29351
diff changeset
280 return xinit (m_base, m_limit, m_increment, m_final, m_numel);
28635
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
281 }
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
282
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
283 template <>
29352
03c283f73b9a specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents: 29351
diff changeset
284 void
03c283f73b9a specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents: 29351
diff changeset
285 range<float>::init (void)
28635
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
286 {
29352
03c283f73b9a specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents: 29351
diff changeset
287 return xinit (m_base, m_limit, m_increment, m_final, m_numel);
28635
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
288 }
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
289
29353
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
290 template <>
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
291 bool
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
292 range<double>::is_storable (void) const
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
293 {
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
294 return xis_storable (m_base, m_limit, m_numel);
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
295 }
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
296
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
297 template <>
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
298 bool
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
299 range<float>::is_storable (void) const
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
300 {
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
301 return xis_storable (m_base, m_limit, m_numel);
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
302 }
715344f405f0 improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents: 29352
diff changeset
303
29351
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
304 template <typename T>
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
305 octave_idx_type
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
306 xnnz (T base, T limit, T inc, T final_val, octave_idx_type nel)
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
307 {
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
308 // Note that the order of the following checks matters.
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
309
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
310 // If there are no elements, there can be no non-zero elements.
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
311 if (nel == 0)
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
312 return 0;
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
313
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
314 // All elements have the same sign, hence there are no zeros.
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
315 if ((base > 0 && limit > 0) || (base < 0 && limit < 0))
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
316 return nel;
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
317
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
318 // All elements are equal (inc = 0) but we know from the previous
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
319 // condition that they are not positive or negative, therefore all
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
320 // elements are zero.
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
321 if (inc == 0)
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
322 return 0;
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
323
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
324 // Exactly one zero at beginning or end of range.
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
325 if (base == 0 || final_val == 0)
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
326 return nel - 1;
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
327
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
328 // Range crosses negative/positive without hitting zero.
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
329 // FIXME: Is this test sufficiently tolerant or do we need to be
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
330 // more careful?
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
331 if (math::mod (-base, inc) != 0)
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
332 return nel;
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
333
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
334 // Range crosses negative/positive and hits zero.
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
335 return nel - 1;
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
336 }
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
337
28635
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
338 template <>
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
339 octave_idx_type
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
340 range<double>::nnz (void) const
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
341 {
29351
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
342 return xnnz (m_base, m_limit, m_increment, m_final, m_numel);
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
343 }
28635
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
344
29351
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
345 template <>
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
346 octave_idx_type
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
347 range<float>::nnz (void) const
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
348 {
326a7ca62c43 also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents: 28857
diff changeset
349 return xnnz (m_base, m_limit, m_increment, m_final, m_numel);
28635
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
350 }
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
351 }
b6371946f106 new template for range objects
John W. Eaton <jwe@octave.org>
parents: 28628
diff changeset
352
2383
096529066838 [project @ 1996-10-12 17:50:47 by jwe]
jwe
parents: 1993
diff changeset
353 bool
096529066838 [project @ 1996-10-12 17:50:47 by jwe]
jwe
parents: 1993
diff changeset
354 Range::all_elements_are_ints (void) const
096529066838 [project @ 1996-10-12 17:50:47 by jwe]
jwe
parents: 1993
diff changeset
355 {
25812
fc74c8d2a584 Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents: 25808
diff changeset
356 // If the base and increment are ints, the final value in the range will also
fc74c8d2a584 Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents: 25808
diff changeset
357 // be an integer, even if the limit is not. If there is one or fewer
fc74c8d2a584 Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents: 25808
diff changeset
358 // elements only the base needs to be an integer.
2383
096529066838 [project @ 1996-10-12 17:50:47 by jwe]
jwe
parents: 1993
diff changeset
359
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
360 return (! (octave::math::isnan (m_base) || octave::math::isnan (m_inc))
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
361 && (octave::math::nint_big (m_base) == m_base || m_numel < 1)
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
362 && (octave::math::nint_big (m_inc) == m_inc || m_numel <= 1));
2383
096529066838 [project @ 1996-10-12 17:50:47 by jwe]
jwe
parents: 1993
diff changeset
363 }
645
21cb3c7dc9e3 [project @ 1994-08-24 06:19:54 by jwe]
jwe
parents: 461
diff changeset
364
24859
00ecff875f8a Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents: 24799
diff changeset
365 octave_idx_type
00ecff875f8a Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents: 24799
diff changeset
366 Range::nnz (void) const
00ecff875f8a Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents: 24799
diff changeset
367 {
00ecff875f8a Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents: 24799
diff changeset
368 octave_idx_type retval = 0;
00ecff875f8a Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents: 24799
diff changeset
369
00ecff875f8a Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents: 24799
diff changeset
370 if (! isempty ())
00ecff875f8a Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents: 24799
diff changeset
371 {
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
372 if ((m_base > 0.0 && m_limit > 0.0) || (m_base < 0.0 && m_limit < 0.0))
24859
00ecff875f8a Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents: 24799
diff changeset
373 {
00ecff875f8a Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents: 24799
diff changeset
374 // All elements have the same sign, hence there are no zeros.
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
375 retval = m_numel;
24859
00ecff875f8a Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents: 24799
diff changeset
376 }
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
377 else if (m_inc != 0.0)
24859
00ecff875f8a Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents: 24799
diff changeset
378 {
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
379 if (m_base == 0.0 || m_limit == 0.0)
25812
fc74c8d2a584 Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents: 25808
diff changeset
380 // Exactly one zero at beginning or end of range.
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
381 retval = m_numel - 1;
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
382 else if ((m_base / m_inc) != std::floor (m_base / m_inc))
25812
fc74c8d2a584 Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents: 25808
diff changeset
383 // Range crosses negative/positive without hitting zero.
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
384 retval = m_numel;
24859
00ecff875f8a Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents: 24799
diff changeset
385 else
25812
fc74c8d2a584 Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents: 25808
diff changeset
386 // Range crosses negative/positive and hits zero.
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
387 retval = m_numel - 1;
24859
00ecff875f8a Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents: 24799
diff changeset
388 }
00ecff875f8a Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents: 24799
diff changeset
389 else
00ecff875f8a Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents: 24799
diff changeset
390 {
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
391 // All elements are equal (m_inc = 0) but not positive or negative,
24859
00ecff875f8a Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents: 24799
diff changeset
392 // therefore all elements are zero.
00ecff875f8a Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents: 24799
diff changeset
393 retval = 0;
00ecff875f8a Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents: 24799
diff changeset
394 }
00ecff875f8a Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents: 24799
diff changeset
395 }
00ecff875f8a Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents: 24799
diff changeset
396
00ecff875f8a Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents: 24799
diff changeset
397 return retval;
00ecff875f8a Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents: 24799
diff changeset
398 }
00ecff875f8a Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents: 24799
diff changeset
399
645
21cb3c7dc9e3 [project @ 1994-08-24 06:19:54 by jwe]
jwe
parents: 461
diff changeset
400 Matrix
21cb3c7dc9e3 [project @ 1994-08-24 06:19:54 by jwe]
jwe
parents: 461
diff changeset
401 Range::matrix_value (void) const
21cb3c7dc9e3 [project @ 1994-08-24 06:19:54 by jwe]
jwe
parents: 461
diff changeset
402 {
28524
455fe4a6f22c deprecate arithmetic operators for ranges; eliminate use in Octave
John W. Eaton <jwe@octave.org>
parents: 28402
diff changeset
403 Matrix retval (1, m_numel);
20531
dcfbf4c1c3c8 eliminate trailing whitespace and tabs from sources
John W. Eaton <jwe@octave.org>
parents: 20513
diff changeset
404
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 28592
diff changeset
405 if (m_numel > 0)
645
21cb3c7dc9e3 [project @ 1994-08-24 06:19:54 by jwe]
jwe
parents: 461
diff changeset
406 {
20513
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
407 // The first element must always be *exactly* the base.
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
408 // E.g, -0 would otherwise become +0 in the loop (-0 + 0*increment).
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 28592
diff changeset
409 retval(0) = m_base;
20513
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
410
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 28592
diff changeset
411 double b = m_base;
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 28592
diff changeset
412 double increment = m_inc;
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 28592
diff changeset
413 for (octave_idx_type i = 1; i < m_numel - 1; i++)
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 28592
diff changeset
414 retval.xelem (i) = b + i * increment;
4791
62f2fb593455 [project @ 2004-02-20 18:02:59 by jwe]
jwe
parents: 4192
diff changeset
415
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 28592
diff changeset
416 retval.xelem (m_numel - 1) = m_limit;
645
21cb3c7dc9e3 [project @ 1994-08-24 06:19:54 by jwe]
jwe
parents: 461
diff changeset
417 }
21cb3c7dc9e3 [project @ 1994-08-24 06:19:54 by jwe]
jwe
parents: 461
diff changeset
418
28524
455fe4a6f22c deprecate arithmetic operators for ranges; eliminate use in Octave
John W. Eaton <jwe@octave.org>
parents: 28402
diff changeset
419 return retval;
645
21cb3c7dc9e3 [project @ 1994-08-24 06:19:54 by jwe]
jwe
parents: 461
diff changeset
420 }
3
9a4c07481e61 [project @ 1993-08-08 01:20:23 by jwe]
jwe
parents:
diff changeset
421
9986
672e1b49e01e optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents: 8971
diff changeset
422 double
672e1b49e01e optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents: 8971
diff changeset
423 Range::checkelem (octave_idx_type i) const
672e1b49e01e optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents: 8971
diff changeset
424 {
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
425 if (i < 0 || i >= m_numel)
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
426 octave::err_index_out_of_range (2, 2, i+1, m_numel, dims ());
9986
672e1b49e01e optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents: 8971
diff changeset
427
16169
0303fda3e929 Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents: 15271
diff changeset
428 if (i == 0)
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
429 return m_base;
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
430 else if (i < m_numel - 1)
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
431 return m_base + i * m_inc;
16169
0303fda3e929 Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents: 15271
diff changeset
432 else
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
433 return m_limit;
9986
672e1b49e01e optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents: 8971
diff changeset
434 }
672e1b49e01e optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents: 8971
diff changeset
435
16169
0303fda3e929 Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents: 15271
diff changeset
436 double
24780
0d21e2a1cdfc new element access operators for Range objects
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
437 Range::checkelem (octave_idx_type i, octave_idx_type j) const
0d21e2a1cdfc new element access operators for Range objects
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
438 {
25812
fc74c8d2a584 Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents: 25808
diff changeset
439 // Ranges are *always* row vectors.
24780
0d21e2a1cdfc new element access operators for Range objects
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
440 if (i != 0)
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
441 octave::err_index_out_of_range (1, 1, i+1, m_numel, dims ());
24780
0d21e2a1cdfc new element access operators for Range objects
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
442
0d21e2a1cdfc new element access operators for Range objects
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
443 return checkelem (j);
0d21e2a1cdfc new element access operators for Range objects
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
444 }
0d21e2a1cdfc new element access operators for Range objects
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
445
0d21e2a1cdfc new element access operators for Range objects
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
446 double
16169
0303fda3e929 Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents: 15271
diff changeset
447 Range::elem (octave_idx_type i) const
0303fda3e929 Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents: 15271
diff changeset
448 {
0303fda3e929 Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents: 15271
diff changeset
449 if (i == 0)
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
450 return m_base;
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
451 else if (i < m_numel - 1)
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
452 return m_base + i * m_inc;
16169
0303fda3e929 Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents: 15271
diff changeset
453 else
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
454 return m_limit;
16169
0303fda3e929 Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents: 15271
diff changeset
455 }
0303fda3e929 Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents: 15271
diff changeset
456
16186
b4a6895a9863 Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents: 16169
diff changeset
457 // 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
458 class __rangeidx_helper
9986
672e1b49e01e optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents: 8971
diff changeset
459 {
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
460 public:
16186
b4a6895a9863 Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents: 16169
diff changeset
461 __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
462 : 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
463
9986
672e1b49e01e optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents: 8971
diff changeset
464 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
465 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
466 if (i == 0)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
467 *array++ = base;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
468 else if (i < nmax)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
469 *array++ = base + i * inc;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
470 else
20513
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
471 *array++ = limit;
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
472 }
16186
b4a6895a9863 Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents: 16169
diff changeset
473
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
474 private:
16186
b4a6895a9863 Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents: 16169
diff changeset
475
b4a6895a9863 Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents: 16169
diff changeset
476 double *array, base, inc, limit;
b4a6895a9863 Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents: 16169
diff changeset
477 octave_idx_type nmax;
b4a6895a9863 Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents: 16169
diff changeset
478
9986
672e1b49e01e optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents: 8971
diff changeset
479 };
672e1b49e01e optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents: 8971
diff changeset
480
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
481 Array<double>
29569
29a1f8fd8ee6 move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
482 Range::index (const octave::idx_vector& i) const
9986
672e1b49e01e optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents: 8971
diff changeset
483 {
672e1b49e01e optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents: 8971
diff changeset
484 Array<double> retval;
672e1b49e01e optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents: 8971
diff changeset
485
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
486 octave_idx_type n = m_numel;
9986
672e1b49e01e optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents: 8971
diff changeset
487
672e1b49e01e optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents: 8971
diff changeset
488 if (i.is_colon ())
672e1b49e01e optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents: 8971
diff changeset
489 {
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
490 retval = matrix_value ().reshape (dim_vector (m_numel, 1));
9986
672e1b49e01e optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents: 8971
diff changeset
491 }
672e1b49e01e optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents: 8971
diff changeset
492 else
672e1b49e01e optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents: 8971
diff changeset
493 {
10366
e5ae13b8b2c2 improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents: 10314
diff changeset
494 if (i.extent (n) != n)
28004
403df0b32204 update out_of_range error messages
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
495 octave::err_index_out_of_range (1, 1, i.extent (n), n, dims ()); // throws
10366
e5ae13b8b2c2 improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents: 10314
diff changeset
496
9986
672e1b49e01e optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents: 8971
diff changeset
497 dim_vector rd = i.orig_dimensions ();
672e1b49e01e optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents: 8971
diff changeset
498 octave_idx_type il = i.length (n);
672e1b49e01e optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents: 8971
diff changeset
499
672e1b49e01e optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents: 8971
diff changeset
500 // taken from Array.cc.
23592
80e3bfb7bd5a maint: Deprecate is_vector and replace with isvector.
Rik <rik@octave.org>
parents: 23588
diff changeset
501 if (n != 1 && rd.isvector ())
9986
672e1b49e01e optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents: 8971
diff changeset
502 rd = dim_vector (1, il);
672e1b49e01e optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents: 8971
diff changeset
503
672e1b49e01e optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents: 8971
diff changeset
504 retval.clear (rd);
672e1b49e01e optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents: 8971
diff changeset
505
16186
b4a6895a9863 Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents: 16169
diff changeset
506 // idx_vector loop across all values in i,
b4a6895a9863 Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents: 16169
diff changeset
507 // executing __rangeidx_helper (i) for each i
b4a6895a9863 Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents: 16169
diff changeset
508 i.loop (n, __rangeidx_helper (retval.fortran_vec (),
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
509 m_base, m_inc, m_limit, m_numel));
9986
672e1b49e01e optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents: 8971
diff changeset
510 }
672e1b49e01e optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents: 8971
diff changeset
511
672e1b49e01e optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents: 8971
diff changeset
512 return retval;
672e1b49e01e optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents: 8971
diff changeset
513 }
4810
72a6d410a14a [project @ 2004-03-03 18:49:39 by jwe]
jwe
parents: 4791
diff changeset
514
20228
00cf2847355d Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents: 19697
diff changeset
515 // 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
516 // do_minmax_body() in max.cc avoids calling Range::min/max if numel == 0.
208
380cb9ccc889 [project @ 1993-11-12 10:35:05 by jwe]
jwe
parents: 3
diff changeset
517
380cb9ccc889 [project @ 1993-11-12 10:35:05 by jwe]
jwe
parents: 3
diff changeset
518 double
380cb9ccc889 [project @ 1993-11-12 10:35:05 by jwe]
jwe
parents: 3
diff changeset
519 Range::min (void) const
380cb9ccc889 [project @ 1993-11-12 10:35:05 by jwe]
jwe
parents: 3
diff changeset
520 {
380cb9ccc889 [project @ 1993-11-12 10:35:05 by jwe]
jwe
parents: 3
diff changeset
521 double retval = 0.0;
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
522 if (m_numel > 0)
208
380cb9ccc889 [project @ 1993-11-12 10:35:05 by jwe]
jwe
parents: 3
diff changeset
523 {
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
524 if (m_inc > 0)
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
525 retval = m_base;
208
380cb9ccc889 [project @ 1993-11-12 10:35:05 by jwe]
jwe
parents: 3
diff changeset
526 else
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
527 {
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
528 retval = m_base + (m_numel - 1) * m_inc;
4791
62f2fb593455 [project @ 2004-02-20 18:02:59 by jwe]
jwe
parents: 4192
diff changeset
529
20513
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
530 // Require '<=' test. See note in max ().
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
531 if (retval <= m_limit)
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
532 retval = m_limit;
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
533 }
4791
62f2fb593455 [project @ 2004-02-20 18:02:59 by jwe]
jwe
parents: 4192
diff changeset
534
208
380cb9ccc889 [project @ 1993-11-12 10:35:05 by jwe]
jwe
parents: 3
diff changeset
535 }
380cb9ccc889 [project @ 1993-11-12 10:35:05 by jwe]
jwe
parents: 3
diff changeset
536 return retval;
380cb9ccc889 [project @ 1993-11-12 10:35:05 by jwe]
jwe
parents: 3
diff changeset
537 }
380cb9ccc889 [project @ 1993-11-12 10:35:05 by jwe]
jwe
parents: 3
diff changeset
538
380cb9ccc889 [project @ 1993-11-12 10:35:05 by jwe]
jwe
parents: 3
diff changeset
539 double
380cb9ccc889 [project @ 1993-11-12 10:35:05 by jwe]
jwe
parents: 3
diff changeset
540 Range::max (void) const
380cb9ccc889 [project @ 1993-11-12 10:35:05 by jwe]
jwe
parents: 3
diff changeset
541 {
380cb9ccc889 [project @ 1993-11-12 10:35:05 by jwe]
jwe
parents: 3
diff changeset
542 double retval = 0.0;
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
543 if (m_numel > 0)
208
380cb9ccc889 [project @ 1993-11-12 10:35:05 by jwe]
jwe
parents: 3
diff changeset
544 {
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
545 if (m_inc > 0)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
546 {
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
547 retval = m_base + (m_numel - 1) * m_inc;
4791
62f2fb593455 [project @ 2004-02-20 18:02:59 by jwe]
jwe
parents: 4192
diff changeset
548
20513
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
549 // On some machines (x86 with extended precision floating point
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
550 // arithmetic, for example) it is possible that we can overshoot the
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
551 // limit by approximately the machine precision even though we were
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
552 // very careful in our calculation of the number of elements.
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
553 // Therefore, we clip the result to the limit if it overshoots.
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
554 // The test also includes equality (>= m_limit) to have expressions
20513
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
555 // such as -5:1:-0 result in a -0 endpoint.
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
556 if (retval >= m_limit)
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
557 retval = m_limit;
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
558 }
208
380cb9ccc889 [project @ 1993-11-12 10:35:05 by jwe]
jwe
parents: 3
diff changeset
559 else
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
560 retval = m_base;
208
380cb9ccc889 [project @ 1993-11-12 10:35:05 by jwe]
jwe
parents: 3
diff changeset
561 }
380cb9ccc889 [project @ 1993-11-12 10:35:05 by jwe]
jwe
parents: 3
diff changeset
562 return retval;
380cb9ccc889 [project @ 1993-11-12 10:35:05 by jwe]
jwe
parents: 3
diff changeset
563 }
380cb9ccc889 [project @ 1993-11-12 10:35:05 by jwe]
jwe
parents: 3
diff changeset
564
380cb9ccc889 [project @ 1993-11-12 10:35:05 by jwe]
jwe
parents: 3
diff changeset
565 void
7458
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
566 Range::sort_internal (bool ascending)
208
380cb9ccc889 [project @ 1993-11-12 10:35:05 by jwe]
jwe
parents: 3
diff changeset
567 {
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
568 if ((ascending && m_base > m_limit && m_inc < 0.0)
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
569 || (! ascending && m_base < m_limit && m_inc > 0.0))
208
380cb9ccc889 [project @ 1993-11-12 10:35:05 by jwe]
jwe
parents: 3
diff changeset
570 {
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
571 std::swap (m_base, m_limit);
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
572 m_inc = -m_inc;
7458
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
573 }
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
574 }
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
575
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
576 void
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
577 Range::sort_internal (Array<octave_idx_type>& sidx, bool ascending)
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
578 {
20228
00cf2847355d Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents: 19697
diff changeset
579 octave_idx_type nel = numel ();
7458
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
580
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
581 sidx.resize (dim_vector (1, nel));
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
582
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
583 octave_idx_type *psidx = sidx.fortran_vec ();
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
584
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
585 bool reverse = false;
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
586
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
587 if ((ascending && m_base > m_limit && m_inc < 0.0)
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
588 || (! ascending && m_base < m_limit && m_inc > 0.0))
7458
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
589 {
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
590 std::swap (m_base, m_limit);
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
591 m_inc = -m_inc;
7458
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
592 reverse = true;
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
593 }
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
594
23450
855122b993da maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents: 23443
diff changeset
595 octave_idx_type tmp = (reverse ? nel - 1 : 0);
855122b993da maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents: 23443
diff changeset
596 octave_idx_type stp = (reverse ? -1 : 1);
7458
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
597
7467
ba4214856f9f avoid shadow warning
John W. Eaton <jwe@octave.org>
parents: 7463
diff changeset
598 for (octave_idx_type i = 0; i < nel; i++, tmp += stp)
7458
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
599 psidx[i] = tmp;
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
600 }
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
601
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
602 Matrix
7620
36594d5bbe13 Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents: 7467
diff changeset
603 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
604 {
8971
967a692ddfe2 fix range arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
605 return matrix_value ().diag (k);
7620
36594d5bbe13 Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents: 7467
diff changeset
606 }
36594d5bbe13 Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents: 7467
diff changeset
607
7458
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
608 Range
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
609 Range::sort (octave_idx_type dim, sortmode mode) const
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
610 {
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
611 Range retval = *this;
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
612
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
613 if (dim == 1)
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
614 {
7463
2467639bd8c0 eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents: 7458
diff changeset
615 if (mode == ASCENDING)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
616 retval.sort_internal (true);
7463
2467639bd8c0 eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents: 7458
diff changeset
617 else if (mode == DESCENDING)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
618 retval.sort_internal (false);
7458
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
619 }
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
620 else if (dim != 0)
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
621 (*current_liboctave_error_handler) ("Range::sort: invalid dimension");
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
622
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
623 return retval;
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
624 }
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
625
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
626 Range
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
627 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
628 sortmode mode) const
7458
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
629 {
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
630 Range retval = *this;
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
631
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
632 if (dim == 1)
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
633 {
7463
2467639bd8c0 eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents: 7458
diff changeset
634 if (mode == ASCENDING)
16169
0303fda3e929 Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents: 15271
diff changeset
635 retval.sort_internal (sidx, true);
7463
2467639bd8c0 eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents: 7458
diff changeset
636 else if (mode == DESCENDING)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
637 retval.sort_internal (sidx, false);
7458
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
638 }
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
639 else if (dim != 0)
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
640 (*current_liboctave_error_handler) ("Range::sort: invalid dimension");
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
641
1032e24f199f make sort work for ranges
John W. Eaton <jwe@octave.org>
parents: 7231
diff changeset
642 return retval;
208
380cb9ccc889 [project @ 1993-11-12 10:35:05 by jwe]
jwe
parents: 3
diff changeset
643 }
380cb9ccc889 [project @ 1993-11-12 10:35:05 by jwe]
jwe
parents: 3
diff changeset
644
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8589
diff changeset
645 sortmode
23588
0549061d35b9 maint: Deprecate is_sorted and replace with issorted.
Rik <rik@octave.org>
parents: 23577
diff changeset
646 Range::issorted (sortmode mode) const
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8589
diff changeset
647 {
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
648 if (m_numel > 1 && m_inc > 0)
16169
0303fda3e929 Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents: 15271
diff changeset
649 mode = (mode == DESCENDING) ? UNSORTED : ASCENDING;
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
650 else if (m_numel > 1 && m_inc < 0)
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8589
diff changeset
651 mode = (mode == ASCENDING) ? UNSORTED : DESCENDING;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8589
diff changeset
652 else
26444
5c91daa02f87 Fix static analyzer detected issues with UNSORTED enum (bug #55347).
Rik <rik@octave.org>
parents: 26376
diff changeset
653 mode = (mode == UNSORTED) ? ASCENDING : mode;
8742
d2b06871afac add missing return statement
Jaroslav Hajek <highegg@gmail.com>
parents: 8721
diff changeset
654
d2b06871afac add missing return statement
Jaroslav Hajek <highegg@gmail.com>
parents: 8721
diff changeset
655 return mode;
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8589
diff changeset
656 }
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8589
diff changeset
657
20513
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
658 void
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
659 Range::set_base (double b)
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
660 {
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
661 if (m_base != b)
20513
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
662 {
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
663 m_base = b;
20513
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
664
21321
79a51b7e00b6 eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
665 init ();
20513
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
666 }
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
667 }
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
668
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
669 void
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
670 Range::set_limit (double l)
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
671 {
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
672 if (m_limit != l)
20513
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
673 {
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
674 m_limit = l;
20513
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
675
21321
79a51b7e00b6 eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
676 init ();
20513
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
677 }
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
678 }
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
679
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
680 void
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
681 Range::set_inc (double i)
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
682 {
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
683 if (m_inc != i)
20513
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
684 {
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
685 m_inc = i;
20513
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
686
21321
79a51b7e00b6 eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
687 init ();
20513
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
688 }
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
689 }
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
690
3504
5eef8a2294bd [project @ 2000-02-01 10:06:51 by jwe]
jwe
parents: 3503
diff changeset
691 std::ostream&
5eef8a2294bd [project @ 2000-02-01 10:06:51 by jwe]
jwe
parents: 3503
diff changeset
692 operator << (std::ostream& os, const Range& a)
3
9a4c07481e61 [project @ 1993-08-08 01:20:23 by jwe]
jwe
parents:
diff changeset
693 {
9a4c07481e61 [project @ 1993-08-08 01:20:23 by jwe]
jwe
parents:
diff changeset
694 double b = a.base ();
28636
a3db48e66ef8 use Range::increment instead of Range::inc
John W. Eaton <jwe@octave.org>
parents: 28635
diff changeset
695 double increment = a.increment ();
25812
fc74c8d2a584 Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents: 25808
diff changeset
696 octave_idx_type nel = a.numel ();
3
9a4c07481e61 [project @ 1993-08-08 01:20:23 by jwe]
jwe
parents:
diff changeset
697
25812
fc74c8d2a584 Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents: 25808
diff changeset
698 if (nel > 1)
16169
0303fda3e929 Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents: 15271
diff changeset
699 {
25812
fc74c8d2a584 Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents: 25808
diff changeset
700 // First element must be the base *exactly* (e.g., -0).
23807
336f89b6208b Use character literals 'c' rather than string literals "c" when possible.
Rik <rik@octave.org>
parents: 23662
diff changeset
701 os << b << ' ';
25812
fc74c8d2a584 Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents: 25808
diff changeset
702 for (octave_idx_type i = 1; i < nel-1; i++)
23807
336f89b6208b Use character literals 'c' rather than string literals "c" when possible.
Rik <rik@octave.org>
parents: 23662
diff changeset
703 os << b + i * increment << ' ';
16169
0303fda3e929 Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents: 15271
diff changeset
704 }
3
9a4c07481e61 [project @ 1993-08-08 01:20:23 by jwe]
jwe
parents:
diff changeset
705
25812
fc74c8d2a584 Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents: 25808
diff changeset
706 // Print out the last element exactly, rather than a calculated last element.
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
707 os << a.m_limit << "\n";
3
9a4c07481e61 [project @ 1993-08-08 01:20:23 by jwe]
jwe
parents:
diff changeset
708
9a4c07481e61 [project @ 1993-08-08 01:20:23 by jwe]
jwe
parents:
diff changeset
709 return os;
9a4c07481e61 [project @ 1993-08-08 01:20:23 by jwe]
jwe
parents:
diff changeset
710 }
9a4c07481e61 [project @ 1993-08-08 01:20:23 by jwe]
jwe
parents:
diff changeset
711
3504
5eef8a2294bd [project @ 2000-02-01 10:06:51 by jwe]
jwe
parents: 3503
diff changeset
712 std::istream&
5eef8a2294bd [project @ 2000-02-01 10:06:51 by jwe]
jwe
parents: 3503
diff changeset
713 operator >> (std::istream& is, Range& a)
3
9a4c07481e61 [project @ 1993-08-08 01:20:23 by jwe]
jwe
parents:
diff changeset
714 {
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
715 is >> a.m_base;
3
9a4c07481e61 [project @ 1993-08-08 01:20:23 by jwe]
jwe
parents:
diff changeset
716 if (is)
9a4c07481e61 [project @ 1993-08-08 01:20:23 by jwe]
jwe
parents:
diff changeset
717 {
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
718 double tmp_limit;
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
719 is >> tmp_limit;
20513
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
720
3
9a4c07481e61 [project @ 1993-08-08 01:20:23 by jwe]
jwe
parents:
diff changeset
721 if (is)
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
722 is >> a.m_inc;
20513
96153b16febe Overhaul Range object in liboctave.
Rik <rik@octave.org>
parents: 20457
diff changeset
723
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
724 // Clip the m_limit to the true limit, rebuild numel, clear cache
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
725 a.set_limit (tmp_limit);
3
9a4c07481e61 [project @ 1993-08-08 01:20:23 by jwe]
jwe
parents:
diff changeset
726 }
9a4c07481e61 [project @ 1993-08-08 01:20:23 by jwe]
jwe
parents:
diff changeset
727
9a4c07481e61 [project @ 1993-08-08 01:20:23 by jwe]
jwe
parents:
diff changeset
728 return is;
9a4c07481e61 [project @ 1993-08-08 01:20:23 by jwe]
jwe
parents:
diff changeset
729 }
9a4c07481e61 [project @ 1993-08-08 01:20:23 by jwe]
jwe
parents:
diff changeset
730
2599
9705ea40f0a2 [project @ 1996-12-19 04:49:09 by jwe]
jwe
parents: 2383
diff changeset
731 Range
9705ea40f0a2 [project @ 1996-12-19 04:49:09 by jwe]
jwe
parents: 2383
diff changeset
732 operator - (const Range& r)
9705ea40f0a2 [project @ 1996-12-19 04:49:09 by jwe]
jwe
parents: 2383
diff changeset
733 {
28636
a3db48e66ef8 use Range::increment instead of Range::inc
John W. Eaton <jwe@octave.org>
parents: 28635
diff changeset
734 return Range (-r.base (), -r.limit (), -r.increment (), r.numel ());
2599
9705ea40f0a2 [project @ 1996-12-19 04:49:09 by jwe]
jwe
parents: 2383
diff changeset
735 }
9705ea40f0a2 [project @ 1996-12-19 04:49:09 by jwe]
jwe
parents: 2383
diff changeset
736
8553
c7ff200e45f5 optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 7620
diff changeset
737 Range operator + (double x, const Range& r)
c7ff200e45f5 optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 7620
diff changeset
738 {
28636
a3db48e66ef8 use Range::increment instead of Range::inc
John W. Eaton <jwe@octave.org>
parents: 28635
diff changeset
739 return Range (x + r.base (), x + r.limit (), r.increment (), r.numel ());
8553
c7ff200e45f5 optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 7620
diff changeset
740 }
c7ff200e45f5 optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 7620
diff changeset
741
c7ff200e45f5 optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 7620
diff changeset
742 Range operator + (const Range& r, double x)
c7ff200e45f5 optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 7620
diff changeset
743 {
28636
a3db48e66ef8 use Range::increment instead of Range::inc
John W. Eaton <jwe@octave.org>
parents: 28635
diff changeset
744 return Range (r.base () + x, r.limit () + x, r.increment (), r.numel ());
8553
c7ff200e45f5 optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 7620
diff changeset
745 }
c7ff200e45f5 optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 7620
diff changeset
746
c7ff200e45f5 optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 7620
diff changeset
747 Range operator - (double x, const Range& r)
c7ff200e45f5 optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 7620
diff changeset
748 {
28636
a3db48e66ef8 use Range::increment instead of Range::inc
John W. Eaton <jwe@octave.org>
parents: 28635
diff changeset
749 return Range (x - r.base (), x - r.limit (), -r.increment (), r.numel ());
8553
c7ff200e45f5 optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 7620
diff changeset
750 }
c7ff200e45f5 optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 7620
diff changeset
751
c7ff200e45f5 optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 7620
diff changeset
752 Range operator - (const Range& r, double x)
c7ff200e45f5 optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 7620
diff changeset
753 {
28636
a3db48e66ef8 use Range::increment instead of Range::inc
John W. Eaton <jwe@octave.org>
parents: 28635
diff changeset
754 return Range (r.base () - x, r.limit () - x, r.increment (), r.numel ());
8553
c7ff200e45f5 optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 7620
diff changeset
755 }
c7ff200e45f5 optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 7620
diff changeset
756
c7ff200e45f5 optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 7620
diff changeset
757 Range operator * (double x, const Range& r)
c7ff200e45f5 optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 7620
diff changeset
758 {
28636
a3db48e66ef8 use Range::increment instead of Range::inc
John W. Eaton <jwe@octave.org>
parents: 28635
diff changeset
759 return Range (x * r.base (), x * r.limit (), x * r.increment (), r.numel ());
8553
c7ff200e45f5 optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 7620
diff changeset
760 }
c7ff200e45f5 optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 7620
diff changeset
761
c7ff200e45f5 optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 7620
diff changeset
762 Range operator * (const Range& r, double x)
c7ff200e45f5 optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 7620
diff changeset
763 {
28636
a3db48e66ef8 use Range::increment instead of Range::inc
John W. Eaton <jwe@octave.org>
parents: 28635
diff changeset
764 return Range (r.base () * x, r.limit () * x, r.increment () * x, r.numel ());
8553
c7ff200e45f5 optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 7620
diff changeset
765 }
c7ff200e45f5 optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 7620
diff changeset
766
1546
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
767 // C See Knuth, Art Of Computer Programming, Vol. 1, Problem 1.2.4-5.
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
768 // C
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
769 // C===Tolerant FLOOR function.
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
770 // C
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
771 // C X - is given as a Double Precision argument to be operated on.
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
772 // C It is assumed that X is represented with M mantissa bits.
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
773 // C CT - is given as a Comparison Tolerance such that
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
774 // C 0.LT.CT.LE.3-SQRT(5)/2. If the relative difference between
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
775 // C X and A whole number is less than CT, then TFLOOR is
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
776 // C returned as this whole number. By treating the
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
777 // C floating-point numbers as a finite ordered set note that
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
778 // C the heuristic EPS=2.**(-(M-1)) and CT=3*EPS causes
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
779 // C arguments of TFLOOR/TCEIL to be treated as whole numbers
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
780 // C if they are exactly whole numbers or are immediately
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
781 // C adjacent to whole number representations. Since EPS, the
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
782 // C "distance" between floating-point numbers on the unit
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
783 // C interval, and M, the number of bits in X'S mantissa, exist
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
784 // C on every floating-point computer, TFLOOR/TCEIL are
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
785 // C consistently definable on every floating-point computer.
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
786 // C
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
787 // C For more information see the following references:
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
788 // C (1) P. E. Hagerty, "More On Fuzzy Floor And Ceiling," APL QUOTE
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
789 // C QUAD 8(4):20-24, June 1978. Note that TFLOOR=FL5.
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
790 // C (2) L. M. Breed, "Definitions For Fuzzy Floor And Ceiling", APL
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
791 // C QUOTE QUAD 8(3):16-23, March 1978. This paper cites FL1 through
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
792 // C FL5, the history of five years of evolutionary development of
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
793 // C FL5 - the seven lines of code below - by open collaboration
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
794 // C and corroboration of the mathematical-computing community.
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
795 // C
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
796 // C Penn State University Center for Academic Computing
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
797 // C H. D. Knoble - August, 1978.
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
798
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
799 static inline double
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
800 tfloor (double x, double ct)
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
801 {
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
802 // C---------FLOOR(X) is the largest integer algebraically less than
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
803 // C or equal to X; that is, the unfuzzy FLOOR function.
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
804
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
805 // DINT (X) = X - DMOD (X, 1.0);
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
806 // FLOOR (X) = DINT (X) - DMOD (2.0 + DSIGN (1.0, X), 3.0);
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
807
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
808 // C---------Hagerty's FL5 function follows...
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
809
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
810 double q = 1.0;
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
811
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
812 if (x < 0.0)
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
813 q = 1.0 - ct;
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
814
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
815 double rmax = q / (2.0 - ct);
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
816
21942
aab79a1885cc limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
817 double t1 = 1.0 + std::floor (x);
1546
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
818 t1 = (ct / q) * (t1 < 0.0 ? -t1 : t1);
23450
855122b993da maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents: 23443
diff changeset
819 t1 = (rmax < t1 ? rmax : t1);
855122b993da maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents: 23443
diff changeset
820 t1 = (ct > t1 ? ct : t1);
21942
aab79a1885cc limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
821 t1 = std::floor (x + t1);
1546
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
822
1555
6935e817fd25 [project @ 1995-10-11 05:49:55 by jwe]
jwe
parents: 1548
diff changeset
823 if (x <= 0.0 || (t1 - x) < rmax)
1546
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
824 return t1;
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
825 else
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
826 return t1 - 1.0;
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
827 }
a272c4056bab [project @ 1995-10-07 03:38:40 by jwe]
jwe
parents: 1367
diff changeset
828
3753
f751e43de300 [project @ 2000-12-14 03:01:23 by jwe]
jwe
parents: 3751
diff changeset
829 static inline bool
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
830 teq (double u, double v,
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
831 double ct = 3.0 * std::numeric_limits<double>::epsilon ())
3753
f751e43de300 [project @ 2000-12-14 03:01:23 by jwe]
jwe
parents: 3751
diff changeset
832 {
25812
fc74c8d2a584 Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents: 25808
diff changeset
833 double tu = std::abs (u);
fc74c8d2a584 Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents: 25808
diff changeset
834 double tv = std::abs (v);
3753
f751e43de300 [project @ 2000-12-14 03:01:23 by jwe]
jwe
parents: 3751
diff changeset
835
25812
fc74c8d2a584 Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents: 25808
diff changeset
836 return std::abs (u - v) < ((tu > tv ? tu : tv) * ct);
3753
f751e43de300 [project @ 2000-12-14 03:01:23 by jwe]
jwe
parents: 3751
diff changeset
837 }
f751e43de300 [project @ 2000-12-14 03:01:23 by jwe]
jwe
parents: 3751
diff changeset
838
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5105
diff changeset
839 octave_idx_type
20228
00cf2847355d Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents: 19697
diff changeset
840 Range::numel_internal (void) const
1360
7eb93d12654c [project @ 1995-09-05 21:51:54 by jwe]
jwe
parents: 1315
diff changeset
841 {
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5105
diff changeset
842 octave_idx_type retval = -1;
3
9a4c07481e61 [project @ 1993-08-08 01:20:23 by jwe]
jwe
parents:
diff changeset
843
28592
e70c859c4bff allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents: 28524
diff changeset
844 if (! octave::math::isfinite (m_base) || ! octave::math::isfinite (m_inc)
e70c859c4bff allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents: 28524
diff changeset
845 || octave::math::isnan (m_limit))
e70c859c4bff allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents: 28524
diff changeset
846 retval = -2;
e70c859c4bff allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents: 28524
diff changeset
847 else if (octave::math::isinf (m_limit)
e70c859c4bff allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents: 28524
diff changeset
848 && ((m_inc > 0 && m_limit > 0)
e70c859c4bff allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents: 28524
diff changeset
849 || (m_inc < 0 && m_limit < 0)))
e70c859c4bff allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents: 28524
diff changeset
850 retval = std::numeric_limits<octave_idx_type>::max () - 1;
e70c859c4bff allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents: 28524
diff changeset
851 else if (m_inc == 0
e70c859c4bff allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents: 28524
diff changeset
852 || (m_limit > m_base && m_inc < 0)
e70c859c4bff allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents: 28524
diff changeset
853 || (m_limit < m_base && m_inc > 0))
3858
de05e6bdf897 [project @ 2001-11-08 19:34:22 by jwe]
jwe
parents: 3753
diff changeset
854 {
de05e6bdf897 [project @ 2001-11-08 19:34:22 by jwe]
jwe
parents: 3753
diff changeset
855 retval = 0;
de05e6bdf897 [project @ 2001-11-08 19:34:22 by jwe]
jwe
parents: 3753
diff changeset
856 }
de05e6bdf897 [project @ 2001-11-08 19:34:22 by jwe]
jwe
parents: 3753
diff changeset
857 else
de05e6bdf897 [project @ 2001-11-08 19:34:22 by jwe]
jwe
parents: 3753
diff changeset
858 {
15220
61822c866ba1 use std::numeric_limits<T>::epsilon in C++ code
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
859 double ct = 3.0 * std::numeric_limits<double>::epsilon ();
3
9a4c07481e61 [project @ 1993-08-08 01:20:23 by jwe]
jwe
parents:
diff changeset
860
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
861 double tmp = tfloor ((m_limit - m_base + m_inc) / m_inc, ct);
3858
de05e6bdf897 [project @ 2001-11-08 19:34:22 by jwe]
jwe
parents: 3753
diff changeset
862
28592
e70c859c4bff allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents: 28524
diff changeset
863 octave_idx_type n_elt = (tmp > 0.0
e70c859c4bff allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents: 28524
diff changeset
864 ? static_cast<octave_idx_type> (tmp) : 0);
398
167ea9ebd5a0 [project @ 1994-04-06 18:41:44 by jwe]
jwe
parents: 238
diff changeset
865
25812
fc74c8d2a584 Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents: 25808
diff changeset
866 // If the final element that we would compute for the range is equal to
fc74c8d2a584 Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents: 25808
diff changeset
867 // the limit of the range, or is an adjacent floating point number,
fc74c8d2a584 Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents: 25808
diff changeset
868 // accept it. Otherwise, try a range with one fewer element. If that
fc74c8d2a584 Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents: 25808
diff changeset
869 // fails, try again with one more element.
3858
de05e6bdf897 [project @ 2001-11-08 19:34:22 by jwe]
jwe
parents: 3753
diff changeset
870 //
25812
fc74c8d2a584 Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents: 25808
diff changeset
871 // I'm not sure this is very good, but it seems to work better than just
fc74c8d2a584 Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents: 25808
diff changeset
872 // using tfloor as above. For example, without it, the expression
fc74c8d2a584 Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents: 25808
diff changeset
873 // 1.8:0.05:1.9 fails to produce the expected result of [1.8, 1.85, 1.9].
3753
f751e43de300 [project @ 2000-12-14 03:01:23 by jwe]
jwe
parents: 3751
diff changeset
874
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
875 if (! teq (m_base + (n_elt - 1) * m_inc, m_limit))
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
876 {
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
877 if (teq (m_base + (n_elt - 2) * m_inc, m_limit))
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
878 n_elt--;
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
879 else if (teq (m_base + n_elt * m_inc, m_limit))
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
880 n_elt++;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
881 }
3858
de05e6bdf897 [project @ 2001-11-08 19:34:22 by jwe]
jwe
parents: 3753
diff changeset
882
28592
e70c859c4bff allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents: 28524
diff changeset
883 retval = ((n_elt < std::numeric_limits<octave_idx_type>::max ())
e70c859c4bff allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents: 28524
diff changeset
884 ? n_elt : -1);
3753
f751e43de300 [project @ 2000-12-14 03:01:23 by jwe]
jwe
parents: 3751
diff changeset
885 }
f751e43de300 [project @ 2000-12-14 03:01:23 by jwe]
jwe
parents: 3751
diff changeset
886
3858
de05e6bdf897 [project @ 2001-11-08 19:34:22 by jwe]
jwe
parents: 3753
diff changeset
887 return retval;
3
9a4c07481e61 [project @ 1993-08-08 01:20:23 by jwe]
jwe
parents:
diff changeset
888 }
21321
79a51b7e00b6 eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
889
79a51b7e00b6 eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
890 double
79a51b7e00b6 eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
891 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
892 {
28592
e70c859c4bff allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents: 28524
diff changeset
893 double new_limit = m_inc > 0 ? max () : min ();
21321
79a51b7e00b6 eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
894
25812
fc74c8d2a584 Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents: 25808
diff changeset
895 // If result must be an integer then force the new_limit to be one.
25808
dc47c9e48801 Guarantee an integer for max of Range when base and increment are ints (bug #46859).
Rik <rik@octave.org>
parents: 25438
diff changeset
896 if (all_elements_are_ints ())
25812
fc74c8d2a584 Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents: 25808
diff changeset
897 new_limit = std::round (new_limit);
25808
dc47c9e48801 Guarantee an integer for max of Range when base and increment are ints (bug #46859).
Rik <rik@octave.org>
parents: 25438
diff changeset
898
25812
fc74c8d2a584 Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents: 25808
diff changeset
899 return new_limit;
21321
79a51b7e00b6 eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
900 }
79a51b7e00b6 eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
901
79a51b7e00b6 eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
902 void
79a51b7e00b6 eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
903 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
904 {
28402
09a3c0e91e6e style update for Range class
John W. Eaton <jwe@octave.org>
parents: 28004
diff changeset
905 m_numel = numel_internal ();
21321
79a51b7e00b6 eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
906
28592
e70c859c4bff allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents: 28524
diff changeset
907 if (! octave::math::isinf (m_limit))
e70c859c4bff allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents: 28524
diff changeset
908 m_limit = limit_internal ();
21321
79a51b7e00b6 eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
909 }