Aleph-w 7.10.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
43#ifndef AHUTILS_H
44#define AHUTILS_H
45
46#include <sys/stat.h>
47#include <cstdlib>
48#include <cassert>
49#include <cmath>
50#include <cstdint>
51#include <cstring>
52#include <memory>
53#include <limits>
54#ifndef _MSC_VER
55#include <cxxabi.h> // Itanium ABI demangling (GCC/Clang); absent on MSVC
56#endif
57#include <string>
58#include <sstream>
59#include <iostream>
60#include <fstream>
61#include <typeinfo>
62#include <algorithm>
63
64#include <ahFunction.H>
65#include <ah-errors.H>
66
67// build a string with the file name and line number
74#define POSITION_TRACE (std::string(__FILE__) + ":" + std::to_string(__LINE__))
75
76namespace Aleph {
84template <class T, class Compare = Aleph::less<T>>
85inline const T *median(const T &a, const T &b, const T &c, const Compare &cmp = Compare())
86{
87 if (cmp(a, b))
88 {
89 if (cmp(b, c))
90 return &b; // a < b < c
91 if (cmp(a, c))
92 return &c; // a < c <= b
93 return &a; // c <= a < b
94 }
95 if (cmp(a, c))
96 return &a; // b <= a < c
97 if (cmp(b, c))
98 return &c; // b < c <= a
99 return &b; // c <= b <= a
100}
101
106inline bool is_even(const long n)
107{
108 return n % 2 == 0;
109}
110
115inline bool is_odd(const long n)
116{
117 return not is_even(n);
118}
119
126inline char nibble_to_char(const int i)
127{
128 assert(i >= 0 and i <= 15);
129
130 const char ret = i < 10 ? i + '0' : i - 10 + 'A';
131
132 assert((ret >= '0' and ret <= '9') or (ret >= 'A' and ret <= 'F'));
133
134 return ret;
135}
136
141inline int char_to_nibble(const char c)
142{
143 assert((c >= '0' and c <= '9') or (c >= 'A' and c <= 'F'));
144
145 const int ret = c < 'A' ? c - '0' : c - 'A' + 10;
146
147 assert(ret >= 0 and ret <= 15);
148
149 return ret;
150}
151
163#define DERIVATE_ITERATOR(container_name, base_it_name, it_name) \
164 struct it_name : public base_it_name \
165 { \
166 it_name() \
167 { /* empty */ \
168 } \
169 \
170 it_name(container_name &c) : base_it_name(c) \
171 { \
172 /* empty */ \
173 } \
174 \
175 it_name(const it_name &it) : base_it_name(it) \
176 { \
177 /* empty */ \
178 } \
179 \
180 it_name &operator = (const it_name &it) \
181 { \
182 return base_it_name::operator = (it); \
183 } \
184 };
185
193extern bool resize_process_stack(size_t new_size);
194
204inline constexpr size_t u_index(const size_t &i)
205{
206 return i >> 1; // divide i entre 2
207}
208
218inline constexpr size_t l_index(const size_t i)
219{
220 return i << 1;
221}
222
229inline bool is_power_of_2(size_t x)
230{
231 return x && !(x & (x - 1));
232}
233
243inline size_t next_power_of_2(size_t x)
244{
245 if (is_power_of_2(x))
246 return x;
247
248 ah_overflow_error_if(x > (std::numeric_limits<size_t>::max() >> 1))
249 << "next_power_of_2: no power of 2 that a size_t can hold is >= " << x;
250
251 size_t ret = 1;
252 while (ret < x)
253 ret <<= 1;
254
255 return ret;
256}
257
263inline std::string demangle(const char *name)
264{
265#ifdef _MSC_VER
266 // MSVC's typeid().name() already returns a human-readable name
267 // (e.g. "class Foo"); there is no cxxabi.h to demangle with.
268 return name;
269#else
270 int status = -4; // some arbitrary value to eliminate the compiler warning
271 // enable c++11 by passing the flag -std=c++11 to g++
272 std::unique_ptr<char, void (*)(void *)> res{abi::__cxa_demangle(name, nullptr, nullptr, &status),
273 std::free};
274 return (status == 0) ? res.get() : name;
275#endif
276}
277
282#define CLASSNAME_TO_STRING(class_ptr) ::Aleph::demangle(typeid(*class_ptr).name())
283
288inline void error_msg(const std::string &msg)
289{
290 std::cerr << msg << '\n';
291 abort();
292}
293
298inline bool exists_file(const std::string &name)
299{
300 struct stat buffer;
301 return (stat(name.c_str(), &buffer) == 0);
302}
303
309template <class C>
310inline std::string Rvector(const std::string &name, const C &c)
311{
312 std::ostringstream s;
313 s << name << " <- c(";
314 auto last_ptr = &c.get_last();
315 for (auto it = c.get_it(); it.has_curr(); it.next())
316 {
317 auto &v = it.get_curr();
318 s << v;
319 if (&v != last_ptr)
320 s << ", ";
321 }
322 s << ")";
323
324 return s.str();
325}
326
335template <class C>
336inline std::string Rvector(const C &c)
337{
338 std::ostringstream s;
339 s << c.get_first() << " <- c(";
340 auto last_ptr = &c.get_last();
341 for (auto it = c.get_it(1); it.has_curr(); it.next())
342 {
343 auto &v = it.get_curr();
344 s << v;
345 if (&v != last_ptr)
346 s << ", ";
347 }
348 s << ")";
349
350 return s.str();
351}
352
367inline double interpolate(const double x1,
368 const double x2,
369 const double y1,
370 const double y2,
371 const double x)
372{
373 assert(x2 > x1 and x <= x2 and x >= x1);
374 return y1 + (y2 - y1) * (x - x1) / (x2 - x1);
375}
376
391inline double extrapolate_left(const double x1,
392 const double x2,
393 const double y1,
394 const double y2,
395 const double x)
396{
397 assert(x2 > x1 and x < x1);
398 return y1 - (y2 - y1) * (x1 - x) / (x2 - x1);
399}
400
404inline double pow2(const double x)
405{
406 return x * x;
407}
408
412inline double pow3(const double x)
413{
414 return x * pow2(x);
415}
416
431inline double extrapolate_right(const double x1,
432 const double x2,
433 const double y1,
434 const double y2,
435 const double x)
436{
437 assert(x2 > x1 and x > x2);
438 return y2 + (y2 - y1) * (x - x2) / (x2 - x1);
439}
440
445inline double next_value(const double val)
446{
447 // Store limit in a local to avoid preprocessor misparse of <double>
448 // inside a macro argument (clang-cl on Windows treats < as arg separator).
449 constexpr double lim = std::numeric_limits<double>::max();
450 return std::nextafter(val, lim);
451}
452
458inline double prev_value(const double val)
459{
460 constexpr double lim = std::numeric_limits<double>::min();
461 return std::nextafter(val, lim);
462}
463
468[[nodiscard]] constexpr bool are_near(const double v1, const double v2, const double e) noexcept
469{
470 // std::fabs is not constexpr in MSVC's STL under C++20 (it only became
471 // constexpr in C++23), so compute the absolute difference by hand to keep
472 // this function usable in constant expressions across all compilers.
473 const double d = v1 - v2;
474 return (d < 0.0 ? -d : d) <= e;
475}
476
485inline void execute_R_script(const std::string &scr, const std::string &file_name = "tmp.R")
486{
487 std::ofstream tmp(file_name);
488 tmp << "#!/usr/bin/Rscript" << '\n'
489 << '\n'
490 << "X11()" << '\n'
491 << scr << '\n'
492 << "message(\"Press Return To Continue\")" << '\n'
493 << "invisible(readLines(\"stdin\", n=1))" << '\n';
494 const std::string cmd = "Rscript " + file_name;
495 system(cmd.c_str());
496}
497
509inline bool is_normal_number(const double n)
510{
511 if constexpr (std::numeric_limits<double>::is_iec559 && sizeof(double) == sizeof(std::uint64_t))
512 {
513 // IEEE-754 bit inspection avoids fast-math assuming finiteness.
514 std::uint64_t bits = 0;
515 std::memcpy(&bits, &n, sizeof(bits));
516
517 constexpr std::uint64_t exp_mask = 0x7ff0000000000000ULL;
518 constexpr std::uint64_t frac_mask = 0x000fffffffffffffULL;
519
520 const std::uint64_t exp = bits & exp_mask;
521 const std::uint64_t frac = bits & frac_mask;
522
523 if (exp == exp_mask)
524 return false;
525
526 if (exp == 0)
527 return frac == 0;
528
529 return true;
530 }
531
532 if (not std::isfinite(n))
533 return false;
534
535 const int status = std::fpclassify(n);
536 return (status == FP_NORMAL or status == FP_ZERO);
537}
538} // namespace Aleph
539
540#endif // AHUTILS_H
Exception handling system with formatted messages for Aleph-w.
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
Definition ah-errors.H:611
Standard functor implementations and comparison objects.
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_y1_function > > y1(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4181
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_frac_function > > frac(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4192
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_exp_function > > exp(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4144
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4196
Main namespace for Aleph-w library functions.
double next_value(const double val)
Return the next representable floating-point value to val
Definition ahUtils.H:445
size_t next_power_of_2(size_t x)
Return the smallest power of 2 that is not less than x.
Definition ahUtils.H:243
void error_msg(const std::string &msg)
Display message and abort program execution.
Definition ahUtils.H:288
bool is_power_of_2(size_t x)
Taken from http://stackoverflow.com/questions/3638431/determine-if-an-int-is-a-power-of-2-or-not-in-a...
Definition ahUtils.H:229
constexpr Bit_Column bits(const unsigned n)
Column of n bits without sign: the integers of [0, 2^n - 1].
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:85
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:468
double extrapolate_right(const double x1, const double x2, const double y1, const double y2, const double x)
Basic linear extrapolation.
Definition ahUtils.H:431
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
double interpolate(const double x1, const double x2, const double y1, const double y2, const double x)
Basic linear interpolation.
Definition ahUtils.H:367
constexpr size_t l_index(const size_t i)
Map a binary heap index to the index of its left child.
Definition ahUtils.H:218
and
Check uniqueness with explicit hash + equality functors.
constexpr size_t u_index(const size_t &i)
Map a binary heap index to the index of its parent.
Definition ahUtils.H:204
double prev_value(const double val)
Return the next representable floating-point value of val towards the smallest positive normal number...
Definition ahUtils.H:458
bool is_even(const long n)
Return true if n is even.
Definition ahUtils.H:106
double pow2(const double x)
Return x^2.
Definition ahUtils.H:404
char nibble_to_char(const int i)
Convert a 4-bit nibble stored in an int to its hex character.
Definition ahUtils.H:126
bool exists_file(const std::string &name)
Return true if it exists a file of name
Definition ahUtils.H:298
bool resize_process_stack(size_t new_size)
Resize the process stack to new_size.
Definition ahUtils.C:52
int char_to_nibble(const char c)
Convert a hex character in 0..9A..F to its 4-bit nibble value.
Definition ahUtils.H:141
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:310
bool is_normal_number(const double n)
Return true if a floating-point number is normal or zero.
Definition ahUtils.H:509
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:263
double extrapolate_left(const double x1, const double x2, const double y1, const double y2, const double x)
Basic linear extrapolation.
Definition ahUtils.H:391
double pow3(const double x)
Return x^3.
Definition ahUtils.H:412
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:485
#define is_odd(x)
Definition ran_array.c:30
CmdLine cmd
Definition testHash.C:48