30 #include <string_view>
31 #include <unordered_set>
40 return n ? a *
power(a, n - 1) :
static_cast<T>(1);
43 template <
typename T,
size_t... Indices>
46 std::index_sequence<Indices...>) {
47 return {
power(a, static_cast<T>(Indices))...};
50 template <
size_t... Indices>
53 std::index_sequence<Indices...>) {
54 return {(1.0 /
power(a, static_cast<double>(Indices)))...};
61 template <
typename K,
typename V,
typename comp>
63 auto find_it = map.find(key);
64 CHECK(find_it != map.end());
65 return find_it->second;
68 template <
typename K,
typename V,
typename comp>
69 const V&
get_from_map(
const std::map<K, V, comp>& map,
const K& key) {
70 auto find_it = map.find(key);
71 CHECK(find_it != map.end());
72 return find_it->second;
78 size_t append_move(std::vector<T>& destination, std::vector<T>&& source) {
81 }
else if (destination.empty()) {
82 destination = std::move(source);
83 return destination.size();
85 size_t const source_size = source.size();
86 destination.reserve(destination.size() + source_size);
87 std::move(std::begin(source), std::end(source), std::back_inserter(destination));
92 template <
typename... Ts,
typename T>
94 return (... || dynamic_cast<Ts const*>(ptr));
102 template <
typename CONTAINER>
107 template <
typename CONTAINER>
112 template <
typename CONTAINER>
114 template <
typename T,
typename A>
116 template <
typename T,
typename A>
118 template <
typename T,
typename A>
120 template <
typename T,
typename A>
122 template <
typename T,
typename A>
125 template <
typename OSTREAM,
typename CONTAINER>
126 OSTREAM& operator<<(OSTREAM& os, PrintContainer<CONTAINER> pc) {
127 if (pc.container.empty()) {
132 for (
auto& container : pc.container) {
136 for (
auto itr = pc.container.begin(); itr != pc.container.end(); ++itr) {
137 if constexpr (std::is_pointer_v<typename CONTAINER::value_type>) {
138 os << (itr == pc.container.begin() ?
'(' :
' ') << (
void const*)*itr;
140 os << (itr == pc.container.begin() ?
'(' :
' ') << *itr;
152 void quoteAndPrint(std::ostream&)
const;
159 size_t formatDate(
char* buf,
size_t const max, int64_t
const unixtime);
167 int64_t
const timestamp,
169 bool use_iso_format =
false);
172 size_t formatHMS(
char* buf,
size_t const max, int64_t
const unixtime);
186 DivUMod div{num / den, num % den};
196 int64_t mod = num % den;
203 template <
typename T,
typename U>
204 inline bool contains(
const T& container,
const U& element) {
205 if (std::find(container.begin(), container.end(), element) == container.end()) {
213 template <
typename... COEFFICIENTS>
214 DEVICE constexpr
double horner(
double const x,
double const c0, COEFFICIENTS... c) {
215 if constexpr (
sizeof...(COEFFICIENTS) == 0) {
218 return horner(x, c...) * x + c0;
226 return x *
horner(x * x, 1, 1 / 3., 1 / 5., 1 / 7., 1 / 9., 1 / 11., 1 / 13., 1 / 15.);
233 return horner(x * x, 1, -1/2., 1/24., -1/720., 1/40320., -1/3628800.,
234 1/479001600., -1/87178291200., 1/20922789888000.);
242 return horner(x * x, 1, 1/2., 1/24., 1/720., 1/40320., 1/3628800.,
243 1/479001600., 1/87178291200., 1/20922789888000.);
251 return x *
horner(x * x, 1, -1/6., 1/120., -1/5040., 1/362880.,
252 -1/39916800., 1/6227020800., -1/1307674368000.);
260 return x *
horner(x * x, 1, 1/6., 1/120., 1/5040., 1/362880.,
261 1/39916800., 1/6227020800., 1/1307674368000.);
266 template <
int... values,
typename T>
268 return (... || (values == value));
272 template <
typename T,
size_t N>
274 return powersOfImpl<T>(
a, std::make_index_sequence<N>{});
285 constexpr
unsigned N = 20;
286 constexpr
auto pow10 = powersOf<double, N>(10.0);
287 return x < N ? pow10[x] : (pow10[N - 1] * 10) *
power10(x - N);
292 constexpr
unsigned N = 20;
293 constexpr
auto pow10inv = inversePowersOf<N>(10.0);
294 return x < N ? pow10inv[x] : (pow10inv[N - 1] / 10) *
power10inv(x - N);
299 template <
typename TO,
typename FROM>
302 memcpy(&to, &from,
sizeof(TO) <
sizeof(FROM) ?
sizeof(TO) :
sizeof(FROM));
306 template <
typename TO,
typename FROM>
308 #if 202002L <= __cplusplus // C++20
309 if constexpr (
sizeof(TO) <=
sizeof(FROM)) {
313 memcpy(&to, &from,
sizeof(FROM));
318 memcpy(&to, &from,
sizeof(TO) <
sizeof(FROM) ?
sizeof(TO) :
sizeof(FROM));
323 template <
typename... STR>
325 return {std::forward<STR>(str)...};
328 template <
typename OUTPUT,
typename INPUT,
typename FUNC>
329 OUTPUT
transform(INPUT
const& input, FUNC
const& func) {
331 output.reserve(input.size());
332 for (
auto const& x : input) {
333 output.push_back(func(x));
338 inline unsigned ceil_div(
unsigned const dividend,
unsigned const divisor) {
339 return (dividend + (divisor - 1)) / divisor;
344 template <
typename T>
346 std::unordered_set<T>
const& s) {
347 auto const& c_r = r.size() < s.size() ? r : s;
348 auto const& c_s = r.size() < s.size() ? s : r;
349 for (
auto const& v : c_r) {
357 template <
typename T>
359 std::unordered_set<T>
const& r,
360 std::unordered_set<T>
const& s) {
361 auto const& c_r = r.size() < s.size() ? r : s;
362 auto const& c_s = r.size() < s.size() ? s : r;
363 for (
auto const& v : c_r) {
374 #if __cplusplus >= 202002L // C++20
380 using endian = std::endian;
386 #if defined(__GNUC__) || defined(__clang__) // compiler
391 little = __ORDER_LITTLE_ENDIAN__,
392 big = __ORDER_BIG_ENDIAN__,
393 native = __BYTE_ORDER__
398 #elif defined(_WIN32) // compiler
402 enum class endian { little = 0, big = 1, native = little };
408 #error "unexpected compiler"
412 #endif // __cplusplus >= 202002L
416 #if __cplusplus >= 202002L // C++20
418 #endif // __cplusplus >= 202002L
420 #if __cplusplus < 202002L || !defined(__cpp_lib_byteswap) // C++ standard
428 template <
class T, std::size_t...
N>
430 return ((((i >> (
N * CHAR_BIT)) & (
T)(
unsigned char)(-1))
431 << ((
sizeof(
T) - 1 -
N) * CHAR_BIT)) |
436 return bswap_impl<U>(i, std::make_index_sequence<sizeof(T)>{});
445 #else // C++ standard
455 #endif // C++ standard
462 return (shared::endian::native == shared::endian::big) ? h :
shared::byteswap(h);
466 return (shared::endian::native == shared::endian::big) ? h :
shared::byteswap(h);
470 return (shared::endian::native == shared::endian::big) ? h :
shared::byteswap(h);
474 return (shared::endian::native == shared::endian::big) ? n :
shared::byteswap(n);
478 return (shared::endian::native == shared::endian::big) ? n :
shared::byteswap(n);
482 return (shared::endian::native == shared::endian::big) ? n :
shared::byteswap(n);
bool contains(const T &container, const U &element)
double power10(unsigned const x)
DEVICE constexpr double horner(double const x, double const c0, COEFFICIENTS...c)
TO reinterpret_bits(FROM const from)
unsigned ceil_div(unsigned const dividend, unsigned const divisor)
DEVICE double fastSin(double const x)
std::string convert_temporal_to_iso_format(const SQLTypeInfo &type_info, int64_t unix_time)
constexpr std::array< double, N > inversePowersOf(double const a)
std::ostream & operator<<(std::ostream &os, const SessionInfo &session_info)
void compute_unordered_set_intersection(std::unordered_set< T > *const dest, std::unordered_set< T > const &r, std::unordered_set< T > const &s)
EXTENSION_NOINLINE double power(const double x, const double y)
DEVICE double fastCos(double const x)
constexpr auto heavyai_ntohll(std::uint64_t n)
constexpr std::array< T, sizeof...(Indices)> powersOfImpl(T const a, std::index_sequence< Indices...>)
constexpr auto heavyai_ntohs(std::uint16_t n)
size_t formatHMS(char *buf, size_t const max, int64_t const unixtime)
constexpr auto heavyai_htonl(std::uint32_t h)
char const *const filename
size_t append_move(std::vector< T > &destination, std::vector< T > &&source)
constexpr std::array< double, sizeof...(Indices)> inversePowersOfImpl(double const a, std::index_sequence< Indices...>)
constexpr auto heavyai_htonll(std::uint64_t h)
OUTPUT transform(INPUT const &input, FUNC const &func)
double power10inv(unsigned const x)
constexpr std::array< std::string_view, sizeof...(STR)> string_view_array(STR &&...str)
size_t formatDate(char *buf, size_t const max, int64_t const unixtime)
constexpr T bswap_impl(T i, std::index_sequence< N...>)
V & get_from_map(std::map< K, V, comp > &map, const K &key)
DEVICE double fastAtanh(double const x)
uint64_t unsignedMod(int64_t num, int64_t den)
size_t formatDateTime(char *buf, size_t const max, int64_t const timestamp, int const dimension, bool use_iso_format)
bool is_unordered_set_intersection_empty(std::unordered_set< T > const &r, std::unordered_set< T > const &s)
constexpr auto heavyai_ntohl(std::uint32_t n)
constexpr T byteswap(T n) noexcept
bool dynamic_castable_to_any(T const *ptr)
PrintContainer< CONTAINER > printContainer(CONTAINER &container)
size_t compute_hash(int32_t item_1, int32_t item_2)
constexpr auto heavyai_htons(std::uint16_t h)
DEVICE double fastCosh(double const x)
DEVICE double fastSinh(double const x)
constexpr std::array< T, N > powersOf(T const a)
DivUMod divUMod(int64_t num, int64_t den)