Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ahUtils.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
44# ifndef AHUTILS_H
45# define AHUTILS_H
46
47# include <sys/stat.h>
48# include <cstdlib>
49# include <cassert>
50# include <cmath>
51# include <cstdint>
52# include <cstring>
53# include <memory>
54# include <limits>
55# include <cxxabi.h>
56# include <string>
57# include <sstream>
58# include <iostream>
59# include <fstream>
60# include <typeinfo>
61# include <algorithm>
62
63# include <ahFunction.H>
64
65// build a string with the file name and line number
72# define POSITION_TRACE (std::string(__FILE__) + ":" + std::to_string(__LINE__))
73
74namespace Aleph
75{
83 template <class T, class Compare = Aleph::less<T>>
84 inline const T * median(const T & a, const T & b, const T & c, const Compare & cmp = Compare())
85 {
86 if (cmp(a, b))
87 {
88 if (cmp(b, c)) return &b; // a < b < c
89 if (cmp(a, c)) return &c; // a < c <= b
90 return &a; // c <= a < b
91 }
92 if (cmp(a, c)) return &a; // b <= a < c
93 if (cmp(b, c)) return &c; // b < c <= a
94 return &b; // c <= b <= a
95 }
96
97
102 inline bool is_even(const long n)
103 {
104 return n % 2 == 0;
105 }
106
111 inline bool is_odd(const long n)
112 {
113 return not is_even(n);
114 }
115
122 inline char nibble_to_char(const int i)
123 {
124 assert(i >= 0 and i <= 15);
125
126 const char ret = i < 10 ? i + '0' : i - 10 + 'A';
127
128 assert((ret >= '0' and ret <= '9') or (ret >= 'A' and ret <= 'F'));
129
130 return ret;
131 }
132
137 inline int char_to_nibble(const char c)
138 {
139 assert((c >= '0' and c <= '9') or (c >= 'A' and c <= 'F'));
140
141 const int ret = c < 'A' ? c - '0' : c - 'A' + 10;
142
143 assert(ret >= 0 and ret <= 15);
144
145 return ret;
146 }
147
159# define DERIVATE_ITERATOR(container_name, base_it_name, it_name) \
160 struct it_name : public base_it_name \
161 { \
162 it_name() { /* empty */ } \
163 \
164 it_name(container_name & c) : base_it_name(c) \
165 { \
166 /* empty */ \
167 } \
168 \
169 it_name(const it_name & it) : base_it_name(it) \
170 { \
171 /* empty */ \
172 } \
173 \
174 it_name & operator = (const it_name & it) \
175 { \
176 return base_it_name::operator = (it); \
177 } \
178 };
179
180
188 extern bool resize_process_stack(size_t new_size);
189
199 inline size_t u_index(const size_t & i)
200 {
201 return i >> 1; // divide i entre 2
202 }
203
213 inline size_t l_index(const size_t i)
214 {
215 return i << 1;
216 }
217
224 inline bool is_power_of_2(unsigned long x)
225 {
226 return x && ! (x & (x - 1UL));
227 }
228
234 inline unsigned long next_power_of_2(unsigned long x)
235 {
236 if (is_power_of_2(x))
237 return x;
238
239 unsigned long ret = 1;
240 while (ret < x)
241 ret <<= 1;
242
243 return ret;
244 }
245
251 inline std::string demangle(const char *name)
252 {
253 int status = -4; // some arbitrary value to eliminate the compiler warning
254 // enable c++11 by passing the flag -std=c++11 to g++
255 std::unique_ptr<char, void(*)(void *)> res
256 {abi::__cxa_demangle(name, nullptr, nullptr, &status), std::free};
257 return (status == 0) ? res.get() : name;
258 }
259
264# define CLASSNAME_TO_STRING(class_ptr) ::Aleph::demangle(typeid(*class_ptr).name())
265
270 inline void error_msg(const std::string & msg)
271 {
272 std::cerr << msg << '\n';
273 abort();
274 }
275
280 inline bool exists_file(const std::string & name)
281 {
282 struct stat buffer;
283 return (stat(name.c_str(), &buffer) == 0);
284 }
285
291 template <class C>
292 inline std::string Rvector(const std::string & name, const C & c)
293 {
294 std::ostringstream s;
295 s << name << " <- c(";
296 auto last_ptr = &c.get_last();
297 for (auto it = c.get_it(); it.has_curr(); it.next())
298 {
299 auto & v = it.get_curr();
300 s << v;
301 if (&v != last_ptr)
302 s << ", ";
303 }
304 s << ")";
305
306 return s.str();
307 }
308
317 template <class C>
318 inline std::string Rvector(const C & c)
319 {
320 std::ostringstream s;
321 s << c.get_first() << " <- c(";
322 auto last_ptr = &c.get_last();
323 for (auto it = c.get_it(1); it.has_curr(); it.next())
324 {
325 auto & v = it.get_curr();
326 s << v;
327 if (&v != last_ptr)
328 s << ", ";
329 }
330 s << ")";
331
332 return s.str();
333 }
334
349 inline
350 double interpolate(const double x1, const double x2,
351 const double y1, const double y2, const double x)
352 {
353 assert(x2 > x1 and x <= x2 and x >= x1);
354 return y1 + (y2 - y1) * (x - x1) / (x2 - x1);
355 }
356
371 inline
372 double extrapolate_left(const double x1, const double x2,
373 const double y1, const double y2, const double x)
374 {
375 assert(x2 > x1 and x < x1);
376 return y1 - (y2 - y1) * (x1 - x) / (x2 - x1);
377 }
378
382 inline double pow2(const double x) { return x * x; }
383
387 inline double pow3(const double x) { return x * pow2(x); }
388
403 inline
404 double extrapolate_right(const double x1, const double x2,
405 const double y1, const double y2, const double x)
406 {
407 assert(x2 > x1 and x > x2);
408 return y2 + (y2 - y1) * (x - x2) / (x2 - x1);
409 }
410
415 inline double next_value(const double val)
416 {
417 return nextafter(val, std::numeric_limits<double>::max());
418 }
419
425 inline double prev_value(const double val)
426 {
427 return nextafter(val, std::numeric_limits<double>::min());
428 }
429
434 [[nodiscard]] constexpr bool are_near(const double v1, const double v2, const double e) noexcept { return std::fabs(v1 - v2) <= e; }
435
444 inline
445 void execute_R_script(const std::string & scr, const std::string & file_name = "tmp.R")
446 {
447 std::ofstream tmp(file_name);
448 tmp << "#!/usr/bin/Rscript" << '\n'
449 << '\n'
450 << "X11()" << '\n'
451 << scr << '\n'
452 << "message(\"Press Return To Continue\")" << '\n'
453 << "invisible(readLines(\"stdin\", n=1))" << '\n';
454 const std::string cmd = "Rscript " + file_name;
455 system(cmd.c_str());
456 }
457
469 inline bool is_normal_number(const double n)
470 {
471 if constexpr (std::numeric_limits<double>::is_iec559 &&
472 sizeof(double) == sizeof(std::uint64_t))
473 {
474 // IEEE-754 bit inspection avoids fast-math assuming finiteness.
475 std::uint64_t bits = 0;
476 std::memcpy(&bits, &n, sizeof(bits));
477
478 constexpr std::uint64_t exp_mask = 0x7ff0000000000000ULL;
479 constexpr std::uint64_t frac_mask = 0x000fffffffffffffULL;
480
481 const std::uint64_t exp = bits & exp_mask;
482 const std::uint64_t frac = bits & frac_mask;
483
484 if (exp == exp_mask)
485 return false;
486
487 if (exp == 0)
488 return frac == 0;
489
490 return true;
491 }
492
493 if (not std::isfinite(n))
494 return false;
495
496 const int status = std::fpclassify(n);
497 return (status == FP_NORMAL or status == FP_ZERO);
498 }
499}
500
501
502# endif // AHUTILS_H
Standard functor implementations and comparison objects.
T & get_last() const
Return the last item of the list.
Definition htlist.H:1663
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_y1_function > > y1(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4103
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_frac_function > > frac(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4114
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_exp_function > > exp(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4066
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4118
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
size_t l_index(const size_t i)
Map a binary heap index to the index of its left child.
Definition ahUtils.H:213
double next_value(const double val)
Return the next representable floating-point value to val
Definition ahUtils.H:415
unsigned long next_power_of_2(unsigned long x)
In x is not exact power of 2, it returns the next power of 2.
Definition ahUtils.H:234
void error_msg(const std::string &msg)
Display message and abort program execution.
Definition ahUtils.H:270
const T * median(const T &a, const T &b, const T &c, const Compare &cmp=Compare())
Return a pointer to the median value among three elements.
Definition ahUtils.H:84
constexpr bool are_near(const double v1, const double v2, const double e) noexcept
Return true if v1 is within absolute distance e of v2.
Definition ahUtils.H:434
double extrapolate_right(const double x1, const double x2, const double y1, const double y2, const double x)
Basic linear extrapolation.
Definition ahUtils.H:404
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:107
size_t u_index(const size_t &i)
Map a binary heap index to the index of its parent.
Definition ahUtils.H:199
double interpolate(const double x1, const double x2, const double y1, const double y2, const double x)
Basic linear interpolation.
Definition ahUtils.H:350
double prev_value(const double val)
Return the next representable floating-point value of val towards the smallest positive normal number...
Definition ahUtils.H:425
bool is_even(const long n)
Return true if n is even.
Definition ahUtils.H:102
double pow2(const double x)
Return x^2.
Definition ahUtils.H:382
char nibble_to_char(const int i)
Convert a 4-bit nibble stored in an int to its hex character.
Definition ahUtils.H:122
bool exists_file(const std::string &name)
Return true if it exists a file of name
Definition ahUtils.H:280
bool resize_process_stack(size_t new_size)
Resize the process stack to new_size.
Definition ahUtils.C:39
int char_to_nibble(const char c)
Convert a hex character in 0..9A..F to its 4-bit nibble value.
Definition ahUtils.H:137
std::string Rvector(const std::string &name, const C &c)
Return a string with R specification of a vector with name and data stored in container c
Definition ahUtils.H:292
bool is_normal_number(const double n)
Return true if a floating-point number is normal or zero.
Definition ahUtils.H:469
std::string demangle(const char *name)
Given a linker symbol name generated by a c++ compiler, this functions decodes it into a user level n...
Definition ahUtils.H:251
double extrapolate_left(const double x1, const double x2, const double y1, const double y2, const double x)
Basic linear extrapolation.
Definition ahUtils.H:372
double pow3(const double x)
Return x^3.
Definition ahUtils.H:387
bool is_power_of_2(unsigned long x)
Taken from http://stackoverflow.com/questions/3638431/determine-if-an-int-is-a-power-of-2-or-not-in-a...
Definition ahUtils.H:224
DynList< T > maps(const C &c, Op op)
Classic map operation.
void execute_R_script(const std::string &scr, const std::string &file_name="tmp.R")
Generate and execute a R script from a string (containing the script)
Definition ahUtils.H:445
#define is_odd(x)
Definition ran_array.c:30