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
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
66// build a string with the file name and line number
73#define POSITION_TRACE (std::string(__FILE__) + ":" + std::to_string(__LINE__))
74
75namespace Aleph {
83template <class T, class Compare = Aleph::less<T>>
84inline 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))
89 return &b; // a < b < c
90 if (cmp(a, c))
91 return &c; // a < c <= b
92 return &a; // c <= a < b
93 }
94 if (cmp(a, c))
95 return &a; // b <= a < c
96 if (cmp(b, c))
97 return &c; // b < c <= a
98 return &b; // c <= b <= a
99}
100
105inline bool is_even(const long n)
106{
107 return n % 2 == 0;
108}
109
114inline bool is_odd(const long n)
115{
116 return not is_even(n);
117}
118
125inline char nibble_to_char(const int i)
126{
127 assert(i >= 0 and i <= 15);
128
129 const char ret = i < 10 ? i + '0' : i - 10 + 'A';
130
131 assert((ret >= '0' and ret <= '9') or (ret >= 'A' and ret <= 'F'));
132
133 return ret;
134}
135
140inline int char_to_nibble(const char c)
141{
142 assert((c >= '0' and c <= '9') or (c >= 'A' and c <= 'F'));
143
144 const int ret = c < 'A' ? c - '0' : c - 'A' + 10;
145
146 assert(ret >= 0 and ret <= 15);
147
148 return ret;
149}
150
162#define DERIVATE_ITERATOR(container_name, base_it_name, it_name) \
163 struct it_name : public base_it_name \
164 { \
165 it_name() \
166 { /* empty */ \
167 } \
168 \
169 it_name(container_name &c) : base_it_name(c) \
170 { \
171 /* empty */ \
172 } \
173 \
174 it_name(const it_name &it) : base_it_name(it) \
175 { \
176 /* empty */ \
177 } \
178 \
179 it_name &operator = (const it_name &it) \
180 { \
181 return base_it_name::operator = (it); \
182 } \
183 };
184
192extern bool resize_process_stack(size_t new_size);
193
203inline size_t u_index(const size_t &i)
204{
205 return i >> 1; // divide i entre 2
206}
207
217inline size_t l_index(const size_t i)
218{
219 return i << 1;
220}
221
228inline bool is_power_of_2(unsigned long x)
229{
230 return x && !(x & (x - 1UL));
231}
232
238inline unsigned long next_power_of_2(unsigned long x)
239{
240 if (is_power_of_2(x))
241 return x;
242
243 unsigned long ret = 1;
244 while (ret < x)
245 ret <<= 1;
246
247 return ret;
248}
249
255inline std::string demangle(const char *name)
256{
257#ifdef _MSC_VER
258 // MSVC's typeid().name() already returns a human-readable name
259 // (e.g. "class Foo"); there is no cxxabi.h to demangle with.
260 return name;
261#else
262 int status = -4; // some arbitrary value to eliminate the compiler warning
263 // enable c++11 by passing the flag -std=c++11 to g++
264 std::unique_ptr<char, void (*)(void *)> res{abi::__cxa_demangle(name, nullptr, nullptr, &status),
265 std::free};
266 return (status == 0) ? res.get() : name;
267#endif
268}
269
274#define CLASSNAME_TO_STRING(class_ptr) ::Aleph::demangle(typeid(*class_ptr).name())
275
280inline void error_msg(const std::string &msg)
281{
282 std::cerr << msg << '\n';
283 abort();
284}
285
290inline bool exists_file(const std::string &name)
291{
292 struct stat buffer;
293 return (stat(name.c_str(), &buffer) == 0);
294}
295
301template <class C>
302inline std::string Rvector(const std::string &name, const C &c)
303{
304 std::ostringstream s;
305 s << name << " <- c(";
306 auto last_ptr = &c.get_last();
307 for (auto it = c.get_it(); it.has_curr(); it.next())
308 {
309 auto &v = it.get_curr();
310 s << v;
311 if (&v != last_ptr)
312 s << ", ";
313 }
314 s << ")";
315
316 return s.str();
317}
318
327template <class C>
328inline std::string Rvector(const C &c)
329{
330 std::ostringstream s;
331 s << c.get_first() << " <- c(";
332 auto last_ptr = &c.get_last();
333 for (auto it = c.get_it(1); it.has_curr(); it.next())
334 {
335 auto &v = it.get_curr();
336 s << v;
337 if (&v != last_ptr)
338 s << ", ";
339 }
340 s << ")";
341
342 return s.str();
343}
344
359inline double interpolate(const double x1,
360 const double x2,
361 const double y1,
362 const double y2,
363 const double x)
364{
365 assert(x2 > x1 and x <= x2 and x >= x1);
366 return y1 + (y2 - y1) * (x - x1) / (x2 - x1);
367}
368
383inline double extrapolate_left(const double x1,
384 const double x2,
385 const double y1,
386 const double y2,
387 const double x)
388{
389 assert(x2 > x1 and x < x1);
390 return y1 - (y2 - y1) * (x1 - x) / (x2 - x1);
391}
392
396inline double pow2(const double x)
397{
398 return x * x;
399}
400
404inline double pow3(const double x)
405{
406 return x * pow2(x);
407}
408
423inline double extrapolate_right(const double x1,
424 const double x2,
425 const double y1,
426 const double y2,
427 const double x)
428{
429 assert(x2 > x1 and x > x2);
430 return y2 + (y2 - y1) * (x - x2) / (x2 - x1);
431}
432
437inline double next_value(const double val)
438{
439 // Store limit in a local to avoid preprocessor misparse of <double>
440 // inside a macro argument (clang-cl on Windows treats < as arg separator).
441 constexpr double lim = std::numeric_limits<double>::max();
442 return std::nextafter(val, lim);
443}
444
450inline double prev_value(const double val)
451{
452 constexpr double lim = std::numeric_limits<double>::min();
453 return std::nextafter(val, lim);
454}
455
460[[nodiscard]] constexpr bool are_near(const double v1, const double v2, const double e) noexcept
461{
462 // std::fabs is not constexpr in MSVC's STL under C++20 (it only became
463 // constexpr in C++23), so compute the absolute difference by hand to keep
464 // this function usable in constant expressions across all compilers.
465 const double d = v1 - v2;
466 return (d < 0.0 ? -d : d) <= e;
467}
468
477inline void execute_R_script(const std::string &scr, const std::string &file_name = "tmp.R")
478{
479 std::ofstream tmp(file_name);
480 tmp << "#!/usr/bin/Rscript" << '\n'
481 << '\n'
482 << "X11()" << '\n'
483 << scr << '\n'
484 << "message(\"Press Return To Continue\")" << '\n'
485 << "invisible(readLines(\"stdin\", n=1))" << '\n';
486 const std::string cmd = "Rscript " + file_name;
487 system(cmd.c_str());
488}
489
501inline bool is_normal_number(const double n)
502{
503 if constexpr (std::numeric_limits<double>::is_iec559 && sizeof(double) == sizeof(std::uint64_t))
504 {
505 // IEEE-754 bit inspection avoids fast-math assuming finiteness.
506 std::uint64_t bits = 0;
507 std::memcpy(&bits, &n, sizeof(bits));
508
509 constexpr std::uint64_t exp_mask = 0x7ff0000000000000ULL;
510 constexpr std::uint64_t frac_mask = 0x000fffffffffffffULL;
511
512 const std::uint64_t exp = bits & exp_mask;
513 const std::uint64_t frac = bits & frac_mask;
514
515 if (exp == exp_mask)
516 return false;
517
518 if (exp == 0)
519 return frac == 0;
520
521 return true;
522 }
523
524 if (not std::isfinite(n))
525 return false;
526
527 const int status = std::fpclassify(n);
528 return (status == FP_NORMAL or status == FP_ZERO);
529}
530} // namespace Aleph
531
532#endif // AHUTILS_H
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:4114
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_frac_function > > frac(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4125
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_exp_function > > exp(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4077
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4129
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
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:217
double next_value(const double val)
Return the next representable floating-point value to val
Definition ahUtils.H:437
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:238
void error_msg(const std::string &msg)
Display message and abort program execution.
Definition ahUtils.H:280
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
and
Check uniqueness with explicit hash + equality functors.
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:460
double extrapolate_right(const double x1, const double x2, const double y1, const double y2, const double x)
Basic linear extrapolation.
Definition ahUtils.H:423
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:105
size_t u_index(const size_t &i)
Map a binary heap index to the index of its parent.
Definition ahUtils.H:203
double interpolate(const double x1, const double x2, const double y1, const double y2, const double x)
Basic linear interpolation.
Definition ahUtils.H:359
double prev_value(const double val)
Return the next representable floating-point value of val towards the smallest positive normal number...
Definition ahUtils.H:450
bool is_even(const long n)
Return true if n is even.
Definition ahUtils.H:105
double pow2(const double x)
Return x^2.
Definition ahUtils.H:396
char nibble_to_char(const int i)
Convert a 4-bit nibble stored in an int to its hex character.
Definition ahUtils.H:125
bool exists_file(const std::string &name)
Return true if it exists a file of name
Definition ahUtils.H:290
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:140
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:302
bool is_normal_number(const double n)
Return true if a floating-point number is normal or zero.
Definition ahUtils.H:501
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:255
double extrapolate_left(const double x1, const double x2, const double y1, const double y2, const double x)
Basic linear extrapolation.
Definition ahUtils.H:383
double pow3(const double x)
Return x^3.
Definition ahUtils.H:404
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:228
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:477
#define is_odd(x)
Definition ran_array.c:30
CmdLine cmd
Definition testHash.C:48