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;
151 size_t formatDate(
char* buf,
size_t const max, int64_t
const unixtime);
159 int64_t
const timestamp,
161 bool use_iso_format =
false);
164 size_t formatHMS(
char* buf,
size_t const max, int64_t
const unixtime);
178 DivUMod div{num / den, num % den};
188 int64_t mod = num % den;
195 template <
typename T,
typename U>
196 inline bool contains(
const T& container,
const U& element) {
197 if (std::find(container.begin(), container.end(), element) == container.end()) {
205 template <
typename... COEFFICIENTS>
206 DEVICE constexpr
double horner(
double const x,
double const c0, COEFFICIENTS... c) {
207 if constexpr (
sizeof...(COEFFICIENTS) == 0) {
210 return horner(x, c...) * x + c0;
218 return x *
horner(x * x, 1, 1 / 3., 1 / 5., 1 / 7., 1 / 9., 1 / 11., 1 / 13., 1 / 15.);
225 return horner(x * x, 1, -1/2., 1/24., -1/720., 1/40320., -1/3628800.,
226 1/479001600., -1/87178291200., 1/20922789888000.);
234 return horner(x * x, 1, 1/2., 1/24., 1/720., 1/40320., 1/3628800.,
235 1/479001600., 1/87178291200., 1/20922789888000.);
243 return x *
horner(x * x, 1, -1/6., 1/120., -1/5040., 1/362880.,
244 -1/39916800., 1/6227020800., -1/1307674368000.);
252 return x *
horner(x * x, 1, 1/6., 1/120., 1/5040., 1/362880.,
253 1/39916800., 1/6227020800., 1/1307674368000.);
258 template <
typename T,
size_t N>
260 return powersOfImpl<T>(
a, std::make_index_sequence<N>{});
271 constexpr
unsigned N = 20;
272 constexpr
auto pow10 = powersOf<double, N>(10.0);
273 return x < N ? pow10[x] : (pow10[N - 1] * 10) *
power10(x - N);
278 constexpr
unsigned N = 20;
279 constexpr
auto pow10inv = inversePowersOf<N>(10.0);
280 return x < N ? pow10inv[x] : (pow10inv[N - 1] / 10) *
power10inv(x - N);
284 template <
typename TO,
typename FROM>
287 memcpy(&to, &from,
sizeof(TO) <
sizeof(FROM) ?
sizeof(TO) :
sizeof(FROM));
291 template <
typename... STR>
293 return {std::forward<STR>(str)...};
296 template <
typename OUTPUT,
typename INPUT,
typename FUNC>
297 OUTPUT
transform(INPUT
const& input, FUNC
const& func) {
299 output.reserve(input.size());
300 for (
auto const& x : input) {
301 output.push_back(func(x));
308 #endif // SHARED_MISC_H
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)
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 std::array< T, sizeof...(Indices)> powersOfImpl(T const a, std::index_sequence< Indices...>)
size_t formatHMS(char *buf, size_t const max, int64_t const unixtime)
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...>)
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)
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 dynamic_castable_to_any(T const *ptr)
PrintContainer< CONTAINER > printContainer(CONTAINER &container)
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)