29 #include <string_view>
30 #include <unordered_set>
39 return n ? a *
power(a, n - 1) :
static_cast<T>(1);
42 template <
typename T,
size_t... Indices>
45 std::index_sequence<Indices...>) {
46 return {
power(a, static_cast<T>(Indices))...};
49 template <
size_t... Indices>
52 std::index_sequence<Indices...>) {
53 return {(1.0 /
power(a, static_cast<double>(Indices)))...};
60 template <
typename K,
typename V,
typename comp>
62 auto find_it = map.find(key);
63 CHECK(find_it != map.end());
64 return find_it->second;
67 template <
typename K,
typename V,
typename comp>
68 const V&
get_from_map(
const std::map<K, V, comp>& map,
const K& key) {
69 auto find_it = map.find(key);
70 CHECK(find_it != map.end());
71 return find_it->second;
77 size_t append_move(std::vector<T>& destination, std::vector<T>&& source) {
80 }
else if (destination.empty()) {
81 destination = std::move(source);
82 return destination.size();
84 size_t const source_size = source.size();
85 destination.reserve(destination.size() + source_size);
86 std::move(std::begin(source), std::end(source), std::back_inserter(destination));
91 template <
typename... Ts,
typename T>
93 return (... || dynamic_cast<Ts const*>(ptr));
101 template <
typename CONTAINER>
106 template <
typename CONTAINER>
111 template <
typename CONTAINER>
113 template <
typename T,
typename A>
115 template <
typename T,
typename A>
117 template <
typename T,
typename A>
119 template <
typename T,
typename A>
121 template <
typename T,
typename A>
124 template <
typename OSTREAM,
typename CONTAINER>
125 OSTREAM& operator<<(OSTREAM& os, PrintContainer<CONTAINER> pc) {
126 if (pc.container.empty()) {
131 for (
auto& container : pc.container) {
135 for (
auto itr = pc.container.begin(); itr != pc.container.end(); ++itr) {
136 if constexpr (std::is_pointer_v<typename CONTAINER::value_type>) {
137 os << (itr == pc.container.begin() ?
'(' :
' ') << (
void const*)*itr;
139 os << (itr == pc.container.begin() ?
'(' :
' ') << *itr;
150 size_t formatDate(
char* buf,
size_t const max, int64_t
const unixtime);
158 int64_t
const timestamp,
160 bool use_iso_format =
false);
163 size_t formatHMS(
char* buf,
size_t const max, int64_t
const unixtime);
177 DivUMod div{num / den, num % den};
187 int64_t mod = num % den;
194 template <
typename T,
typename U>
195 inline bool contains(
const T& container,
const U& element) {
196 if (std::find(container.begin(), container.end(), element) == container.end()) {
204 template <
typename... COEFFICIENTS>
205 DEVICE constexpr
double horner(
double const x,
double const c0, COEFFICIENTS... c) {
206 if constexpr (
sizeof...(COEFFICIENTS) == 0) {
209 return horner(x, c...) * x + c0;
217 return x *
horner(x * x, 1, 1 / 3., 1 / 5., 1 / 7., 1 / 9., 1 / 11., 1 / 13., 1 / 15.);
224 return horner(x * x, 1, -1/2., 1/24., -1/720., 1/40320., -1/3628800.,
225 1/479001600., -1/87178291200., 1/20922789888000.);
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 x *
horner(x * x, 1, -1/6., 1/120., -1/5040., 1/362880.,
243 -1/39916800., 1/6227020800., -1/1307674368000.);
251 return x *
horner(x * x, 1, 1/6., 1/120., 1/5040., 1/362880.,
252 1/39916800., 1/6227020800., 1/1307674368000.);
257 template <
int... values,
typename T>
259 return (... || (values == value));
263 template <
typename T,
size_t N>
265 return powersOfImpl<T>(
a, std::make_index_sequence<N>{});
276 constexpr
unsigned N = 20;
277 constexpr
auto pow10 = powersOf<double, N>(10.0);
278 return x < N ? pow10[x] : (pow10[N - 1] * 10) *
power10(x - N);
283 constexpr
unsigned N = 20;
284 constexpr
auto pow10inv = inversePowersOf<N>(10.0);
285 return x < N ? pow10inv[x] : (pow10inv[N - 1] / 10) *
power10inv(x - N);
290 template <
typename TO,
typename FROM>
293 memcpy(&to, &from,
sizeof(TO) <
sizeof(FROM) ?
sizeof(TO) :
sizeof(FROM));
297 template <
typename TO,
typename FROM>
299 #if 202002L <= __cplusplus // C++20
300 if constexpr (
sizeof(TO) <=
sizeof(FROM)) {
304 memcpy(&to, &from,
sizeof(FROM));
309 memcpy(&to, &from,
sizeof(TO) <
sizeof(FROM) ?
sizeof(TO) :
sizeof(FROM));
314 template <
typename... STR>
316 return {std::forward<STR>(str)...};
319 template <
typename OUTPUT,
typename INPUT,
typename FUNC>
320 OUTPUT
transform(INPUT
const& input, FUNC
const& func) {
322 output.reserve(input.size());
323 for (
auto const& x : input) {
324 output.push_back(func(x));
329 inline unsigned ceil_div(
unsigned const dividend,
unsigned const divisor) {
330 return (dividend + (divisor - 1)) / divisor;
338 #if __cplusplus >= 202002L // C++20
344 using endian = std::endian;
350 #if defined(__GNUC__) || defined(__clang__) // compiler
355 little = __ORDER_LITTLE_ENDIAN__,
356 big = __ORDER_BIG_ENDIAN__,
357 native = __BYTE_ORDER__
362 #elif defined(_WIN32) // compiler
366 enum class endian { little = 0, big = 1, native = little };
372 #error "unexpected compiler"
376 #endif // __cplusplus >= 202002L
380 #if __cplusplus >= 202002L // C++20
382 #endif // __cplusplus >= 202002L
384 #if __cplusplus < 202002L || !defined(__cpp_lib_byteswap) // C++ standard
392 template <
class T, std::size_t...
N>
394 return ((((i >> (
N * CHAR_BIT)) & (
T)(
unsigned char)(-1))
395 << ((
sizeof(
T) - 1 -
N) * CHAR_BIT)) |
400 return bswap_impl<U>(i, std::make_index_sequence<sizeof(T)>{});
409 #else // C++ standard
419 #endif // C++ standard
426 return (shared::endian::native == shared::endian::big) ? h :
shared::byteswap(h);
430 return (shared::endian::native == shared::endian::big) ? h :
shared::byteswap(h);
434 return (shared::endian::native == shared::endian::big) ? h :
shared::byteswap(h);
438 return (shared::endian::native == shared::endian::big) ? n :
shared::byteswap(n);
442 return (shared::endian::native == shared::endian::big) ? n :
shared::byteswap(n);
446 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)
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)
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)
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)