GNU Octave  8.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
lo-utils.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2023 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_lo_utils_h)
27 #define octave_lo_utils_h 1
28 
29 #include "octave-config.h"
30 
31 #include <cstdio>
32 
33 #include <iosfwd>
34 #include <string>
35 
36 #include "oct-cmplx.h"
37 #include "quit.h"
38 
40 
41 // Generic any/all test functionality with arbitrary predicate.
42 
43 template <typename F, typename T, bool zero>
44 bool
45 any_all_test (F fcn, const T *m, octave_idx_type len)
46 {
48 
49  for (i = 0; i < len - 3; i += 4)
50  {
51  octave_quit ();
52 
53  if (fcn (m[i]) != zero
54  || fcn (m[i+1]) != zero
55  || fcn (m[i+2]) != zero
56  || fcn (m[i+3]) != zero)
57  return ! zero;
58  }
59 
60  octave_quit ();
61 
62  for (; i < len; i++)
63  if (fcn (m[i]) != zero)
64  return ! zero;
65 
66  return zero;
67 }
68 
69 extern OCTAVE_API bool xis_int_or_inf_or_nan (double x);
70 
71 template <typename T>
72 bool is_one_or_zero (const T& x)
73 {
74  return x == T (0) || x == T (1);
75 }
76 
77 template <typename T>
78 bool is_zero (const T& x)
79 {
80  return x == T (0);
81 }
82 
83 extern OCTAVE_API bool too_large_for_float (double x);
84 
85 extern OCTAVE_API bool too_large_for_float (const Complex& x);
86 
87 extern OCTAVE_API bool is_int_or_inf_or_nan (float x);
88 extern OCTAVE_API bool too_large_for_float (float x);
89 
90 extern OCTAVE_API char * strsave (const char *);
91 
92 extern OCTAVE_API std::string fgets (std::FILE *);
93 extern OCTAVE_API std::string fgetl (std::FILE *);
94 
95 extern OCTAVE_API std::string fgets (std::FILE *, bool& eof);
96 extern OCTAVE_API std::string fgetl (std::FILE *, bool& eof);
97 
98 template <typename T> OCTAVE_API T read_value (std::istream& is);
99 
100 template <> OCTAVE_API double read_value (std::istream& is);
101 template <> OCTAVE_API Complex read_value (std::istream& is);
102 template <> OCTAVE_API float read_value (std::istream& is);
103 template <> OCTAVE_API FloatComplex read_value (std::istream& is);
104 
105 template <typename T> OCTAVE_API void write_value (std::ostream& os, const T& value);
106 
107 template <> OCTAVE_API void write_value (std::ostream& os, const double& value);
108 template <> OCTAVE_API void write_value (std::ostream& os, const Complex& value);
109 template <> OCTAVE_API void write_value (std::ostream& os, const float& value);
110 template <> OCTAVE_API void write_value (std::ostream& os, const FloatComplex& value);
111 
113 
114 extern OCTAVE_API bool int_multiply_overflow (int a, int b, int *r);
115 
116 extern OCTAVE_API bool
117 int_multiply_overflow (long int a, long int b, long int *r);
118 
119 #if defined (OCTAVE_HAVE_LONG_LONG_INT)
120 extern OCTAVE_API bool
121 int_multiply_overflow (long long int a, long long int b, long long int *r);
122 #endif
123 
124 extern OCTAVE_API bool
125 int_multiply_overflow (unsigned int a, unsigned int b, unsigned int *r);
126 
127 extern OCTAVE_API bool
128 int_multiply_overflow (unsigned long int a, unsigned long int b,
129  unsigned long int *r);
130 
131 #if defined (OCTAVE_HAVE_UNSIGNED_LONG_LONG_INT)
132 extern OCTAVE_API bool
133 int_multiply_overflow (unsigned long long int a, unsigned long long int b,
134  unsigned long long int *r);
135 #endif
136 
139 
140 #if defined (OCTAVE_PROVIDE_DEPRECATED_SYMBOLS)
141 template <typename F, typename T, bool zero>
142 OCTAVE_DEPRECATED (7, "use 'octave::any_all_test' instead")
143 bool
144 any_all_test (F fcn, const T *m, octave_idx_type len)
145 {
146  return octave::any_all_test<F, T, zero> (fcn, m, len);
147 }
148 
149 OCTAVE_DEPRECATED (7, "use 'octave::is_int_or_inf_or_nan' instead")
150 inline bool xis_int_or_inf_or_nan (double x)
151 {
153 }
154 
155 template <typename T>
156 OCTAVE_DEPRECATED (7, "use 'octave::is_one_or_zero' instead")
157 bool
158 xis_one_or_zero (const T& x)
159 {
160  return octave::is_one_or_zero (x);
161 }
162 
163 template <typename T>
164 OCTAVE_DEPRECATED (7, "use 'octave::is_zero' instead")
165 bool
166 xis_zero (const T& x)
167 {
168  return octave::is_zero (x);
169 }
170 
171 OCTAVE_DEPRECATED (7, "use 'octave::' instead")
172 inline bool xtoo_large_for_float (double x)
173 {
175 }
176 
177 OCTAVE_DEPRECATED (7, "use 'octave::' instead")
178 inline bool xtoo_large_for_float (const Complex& x)
179 {
181 }
182 
183 OCTAVE_DEPRECATED (7, "use 'octave::' instead")
184 inline bool xis_int_or_inf_or_nan (float x)
185 {
187 }
188 
189 OCTAVE_DEPRECATED (7, "use 'octave::' instead")
190 inline bool xtoo_large_for_float (float x)
191 {
193 }
194 
195 OCTAVE_DEPRECATED (7, "use 'octave::strsave' instead")
196 inline char * strsave (const char *s)
197 {
198  return octave::strsave (s);
199 }
200 
201 OCTAVE_DEPRECATED (7, "use 'octave::fgets' instead")
202 inline std::string octave_fgets (std::FILE *f)
203 {
204  return octave::fgets (f);
205 }
206 
207 OCTAVE_DEPRECATED (7, "use 'octave::fgetl' instead")
208 inline std::string octave_fgetl (std::FILE *f)
209 {
210  return octave::fgetl (f);
211 }
212 
213 OCTAVE_DEPRECATED (7, "use 'octave::fgets' instead")
214 inline std::string octave_fgets (std::FILE *f, bool& eof)
215 {
216  return octave::fgets (f, eof);
217 }
218 
219 OCTAVE_DEPRECATED (7, "use 'octave::fgetl' instead")
220 inline std::string octave_fgetl (std::FILE *f, bool& eof)
221 {
222  return octave::fgetl (f, eof);
223 }
224 
225 OCTAVE_DEPRECATED (7, "use 'octave::read_value<T>' instead")
226 inline double
227 octave_read_double (std::istream& is)
228 {
229  return octave::read_value<double> (is);
230 }
231 
232 OCTAVE_DEPRECATED (7, "use 'octave::read_value<T>' instead")
233 inline Complex
234 octave_read_complex (std::istream& is)
235 {
236  return octave::read_value<Complex> (is);
237 }
238 
239 OCTAVE_DEPRECATED (7, "use 'octave::read_value<T>' instead")
240 inline float
241 octave_read_float (std::istream& is)
242 {
243  return octave::read_value<float> (is);
244 }
245 
246 OCTAVE_DEPRECATED (7, "use 'octave::read_value<T>' instead")
247 inline FloatComplex
248 octave_read_float_complex (std::istream& is)
249 {
250  return octave::read_value<FloatComplex> (is);
251 }
252 
253 OCTAVE_DEPRECATED (7, "use 'octave::write_value<T>' instead")
254 inline void
255 octave_write_double (std::ostream& os, double value)
256 {
257  octave::write_value<double> (os, value);
258 }
259 
260 OCTAVE_DEPRECATED (7, "use 'octave::write_value<T>' instead")
261 inline void
262 octave_write_complex (std::ostream& os, const Complex& value)
263 {
264  octave::write_value<Complex> (os, value);
265 }
266 
267 OCTAVE_DEPRECATED (7, "use 'octave::write_value<T>' instead")
268 inline void
269 octave_write_float (std::ostream& os, float value)
270 {
271  octave::write_value<float> (os, value);
272 }
273 
274 OCTAVE_DEPRECATED (7, "use 'octave::write_value<T>' instead")
275 inline void
276 octave_write_float_complex (std::ostream& os, const FloatComplex& value)
277 {
278  octave::write_value<FloatComplex> (os, value);
279 }
280 #endif
281 
282 #endif
OCTAVE_END_NAMESPACE(octave)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
F77_RET_T const F77_DBLE * x
F77_RET_T const F77_DBLE const F77_DBLE * f
bool is_one_or_zero(const T &x)
Definition: lo-utils.h:72
OCTAVE_API bool is_int_or_inf_or_nan(float x)
Definition: lo-utils.cc:67
OCTAVE_API std::string fgetl(std::FILE *)
bool is_zero(const T &x)
Definition: lo-utils.h:78
OCTAVE_API bool xis_int_or_inf_or_nan(double x)
OCTAVE_API std::string fgets(std::FILE *)
OCTAVE_API char * strsave(const char *)
Definition: lo-utils.cc:74
OCTAVE_API void write_value(std::ostream &os, const T &value)
Definition: lo-utils.cc:402
OCTAVE_API bool too_large_for_float(double x)
Definition: lo-utils.cc:55
OCTAVE_API bool int_multiply_overflow(int a, int b, int *r)
Definition: lo-utils.cc:480
bool any_all_test(F fcn, const T *m, octave_idx_type len)
Definition: lo-utils.h:45
OCTAVE_API T read_value(std::istream &is)
Definition: lo-utils.cc:183
#define OCTAVE_API
Definition: main.in.cc:55
T octave_idx_type m
Definition: mx-inlines.cc:773
T * r
Definition: mx-inlines.cc:773
std::complex< double > Complex
Definition: oct-cmplx.h:33
std::complex< float > FloatComplex
Definition: oct-cmplx.h:34
F77_RET_T len
Definition: xerbla.cc:61