OmniSciDB  fe05a0c208
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
shared Namespace Reference

Classes

struct  PrintContainer
 
struct  is_std_container
 
struct  is_std_container< std::deque< T, A > >
 
struct  is_std_container< std::list< T, A > >
 
struct  is_std_container< std::set< T, A > >
 
struct  is_std_container< std::unordered_set< T, A > >
 
struct  is_std_container< std::vector< T, A > >
 
struct  DivUMod
 

Functions

std::string decode_base64_uri (const std::string &data, bool trim_nulls)
 
std::string decode_base64 (const std::string &val, bool trim_nulls)
 
std::string decode_base64 (const std::string &val)
 
static std::string encode_base64 (const std::string &val)
 
bool isPowOfTwo (unsigned n)
 
unsigned getExpOfTwo (unsigned n)
 
size_t formatDate (char *buf, size_t const max, int64_t const unixtime)
 
size_t formatDateTime (char *buf, size_t const max, int64_t const timestamp, int const dimension)
 
size_t formatHMS (char *buf, size_t const max, int64_t const unixtime)
 
template<typename T >
size_t appendMove (std::vector< T > &destination, std::vector< T > &source)
 
template<typename... Ts, typename T >
bool dynamic_castable_to_any (T const *ptr)
 
template<typename CONTAINER >
PrintContainer< CONTAINER > printContainer (CONTAINER &container)
 
template<typename OSTREAM , typename CONTAINER >
OSTREAM & operator<< (OSTREAM &os, PrintContainer< CONTAINER > pc)
 
DivUMod divUMod (int64_t num, int64_t den)
 
uint64_t unsignedMod (int64_t num, int64_t den)
 
template<typename T , typename U >
bool contains (const T &container, const U &element)
 
template<typename T , size_t N>
constexpr std::array< T, N > powersOf (T const a)
 
template<typename TO , typename FROM >
TO reinterpretBits (FROM const from)
 

Function Documentation

template<typename T >
size_t shared::appendMove ( std::vector< T > &  destination,
std::vector< T > &  source 
)

Definition at line 49 of file misc.h.

49  {
50  if (source.empty()) {
51  return 0;
52  } else if (destination.empty()) {
53  destination = std::move(source);
54  return destination.size();
55  } else {
56  size_t const source_size = source.size();
57  destination.reserve(destination.size() + source_size);
58  std::move(std::begin(source), std::end(source), std::back_inserter(destination));
59  source.clear();
60  return source_size;
61  }
62 }
template<typename T , typename U >
bool shared::contains ( const T &  container,
const U &  element 
)
inline

Definition at line 163 of file misc.h.

Referenced by TableOptimizer::getFragmentIndexes(), TableOptimizer::vacuumFragments(), and foreign_storage::AbstractFileStorageDataWrapper::validateServerOptions().

163  {
164  if (std::find(container.begin(), container.end(), element) == container.end()) {
165  return false;
166  } else {
167  return true;
168  }
169 }

+ Here is the caller graph for this function:

std::string shared::decode_base64 ( const std::string &  val,
bool  trim_nulls 
)
inline

Definition at line 27 of file base64.h.

Referenced by decode_base64(), and decode_base64_uri().

27  {
28  // For some strings, signatature particualary it is important no to trim
29  // '\0' characters
30  using namespace boost::archive::iterators;
31  using It = transform_width<binary_from_base64<std::string::const_iterator>, 8, 6>;
32 
33  if (!trim_nulls) {
34  return std::string(It(val.begin()), It(val.end()));
35  }
36  return boost::algorithm::trim_right_copy_if(
37  std::string(It(std::begin(val)), It(std::end(val))),
38  [](char c) { return c == '\0'; });
39 }

+ Here is the caller graph for this function:

std::string shared::decode_base64 ( const std::string &  val)
inline

Definition at line 41 of file base64.h.

References decode_base64().

41  {
42  return decode_base64(val, true);
43 }
std::string decode_base64(const std::string &val, bool trim_nulls)
Definition: base64.h:27

+ Here is the call graph for this function:

std::string shared::decode_base64_uri ( const std::string &  data,
bool  trim_nulls 
)

Definition at line 21 of file base64.cpp.

References decode_base64().

21  {
22  // Allocate a string large enough to hold exta '=' as padding
23  std::string uri_dec;
24  size_t data_len = data.length();
25  // base64_uri encoding removes '=' padding at the end of the string.
26  size_t padding = 4 - (data_len % 4);
27  if (padding == 4) {
28  padding = 0;
29  }
30  uri_dec.resize(data_len + padding);
31 
32  // base64_uri encoding replaces all '+' and '/' with '-' and '_' respectively.
33  std::transform(
34  data.begin(), data.end(), uri_dec.begin(), [](unsigned char c) -> unsigned char {
35  switch (c) {
36  case '-':
37  return '+';
38  case '_':
39  return '/';
40  default:
41  return c;
42  }
43  });
44  if (padding != 0) {
45  uri_dec.replace(uri_dec.begin() + data_len, uri_dec.end(), padding, '=');
46  }
47  // in the case of a signature from a JWT trim_nulls should be false
48  return decode_base64(uri_dec, trim_nulls);
49 }
std::string decode_base64(const std::string &val, bool trim_nulls)
Definition: base64.h:27

+ Here is the call graph for this function:

DivUMod shared::divUMod ( int64_t  num,
int64_t  den 
)
inline

Definition at line 144 of file misc.h.

References shared::DivUMod::quot.

Referenced by formatDate(), and formatDateTime().

144  {
145  DivUMod div{num / den, num % den};
146  if (div.rem < 0) {
147  --div.quot;
148  div.rem += den;
149  }
150  return div;
151 }

+ Here is the caller graph for this function:

template<typename... Ts, typename T >
bool shared::dynamic_castable_to_any ( T const ptr)

Definition at line 65 of file misc.h.

65  {
66  return (... || dynamic_cast<Ts const*>(ptr));
67 }
static std::string shared::encode_base64 ( const std::string &  val)
inlinestatic

Definition at line 45 of file base64.h.

45  {
46  using namespace boost::archive::iterators;
47  using It = base64_from_binary<transform_width<std::string::const_iterator, 6, 8>>;
48  auto tmp = std::string(It(std::begin(val)), It(std::end(val)));
49  return tmp.append((3 - val.size() % 3) % 3, '=');
50 }
size_t shared::formatDate ( char *  buf,
size_t const  max,
int64_t const  unixtime 
)

Definition at line 25 of file misc.cpp.

References test_fsi::d, divUMod(), shared::DivUMod::quot, and shared::DivUMod::rem.

Referenced by DatumToString().

25  {
26  DivUMod const div_day = divUMod(unixtime, 24 * 60 * 60);
27  DivUMod const div_era = divUMod(div_day.quot - 11017, 146097);
28  unsigned const doe = static_cast<unsigned>(div_era.rem);
29  unsigned const yoe = (doe - doe / 1460 + doe / 36524 - (doe == 146096)) / 365;
30  unsigned const doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
31  unsigned const moy = (5 * doy + 2) / 153;
32  static_assert(8 <= sizeof(long long)); // long long needed for snprintf()
33  long long const y = 2000 + div_era.quot * 400 + yoe + (9 < moy);
34  unsigned const m = moy + (9 < moy ? -9 : 3);
35  unsigned const d = doy - (153 * moy + 2) / 5 + 1;
36  int const len = snprintf(buf, max, "%04lld-%02u-%02u", y, m, d);
37  if (0 <= len && static_cast<size_t>(len) < max) {
38  return static_cast<size_t>(len);
39  }
40  return 0;
41 }
tuple d
Definition: test_fsi.py:9
DivUMod divUMod(int64_t num, int64_t den)
Definition: misc.h:144

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

size_t shared::formatDateTime ( char *  buf,
size_t const  max,
int64_t const  timestamp,
int const  dimension 
)

Definition at line 43 of file misc.cpp.

References test_fsi::d, divUMod(), shared::DivUMod::quot, and shared::DivUMod::rem.

Referenced by DatumToString().

46  {
47  constexpr int pow10[10]{
48  1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
49  DivUMod const div_hip = divUMod(timestamp, pow10[dimension]);
50  DivUMod const div_day = divUMod(div_hip.quot, 24 * 60 * 60);
51  DivUMod const div_era = divUMod(div_day.quot - 11017, 146097);
52  unsigned const doe = static_cast<unsigned>(div_era.rem);
53  unsigned const yoe = (doe - doe / 1460 + doe / 36524 - (doe == 146096)) / 365;
54  unsigned const doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
55  unsigned const moy = (5 * doy + 2) / 153;
56  static_assert(8 <= sizeof(long long)); // long long needed for snprintf()
57  long long const y = 2000 + div_era.quot * 400 + yoe + (9 < moy);
58  unsigned const m = moy + (9 < moy ? -9 : 3);
59  unsigned const d = doy - (153 * moy + 2) / 5 + 1;
60  unsigned const minutes = static_cast<unsigned>(div_day.rem) / 60;
61  unsigned const ss = div_day.rem % 60;
62  unsigned const hh = minutes / 60;
63  unsigned const mm = minutes % 60;
64  int const len =
65  snprintf(buf, max, "%04lld-%02u-%02u %02u:%02u:%02u", y, m, d, hh, mm, ss);
66  if (0 <= len && static_cast<size_t>(len) < max) {
67  if (dimension) {
68  int const len_frac = snprintf(
69  buf + len, max - len, ".%0*d", dimension, static_cast<int>(div_hip.rem));
70  if (0 <= len_frac && static_cast<size_t>(len + len_frac) < max) {
71  return static_cast<size_t>(len + len_frac);
72  }
73  } else {
74  return static_cast<size_t>(len);
75  }
76  }
77  return 0;
78 }
tuple d
Definition: test_fsi.py:9
DivUMod divUMod(int64_t num, int64_t den)
Definition: misc.h:144

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

size_t shared::formatHMS ( char *  buf,
size_t const  max,
int64_t const  unixtime 
)

Definition at line 80 of file misc.cpp.

References unsignedMod().

Referenced by DatumToString(), import_export::QueryExporterCSV::exportResults(), import_export::anonymous_namespace{QueryExporterGDAL.cpp}::insert_array_column(), and import_export::anonymous_namespace{QueryExporterGDAL.cpp}::insert_scalar_column().

80  {
81  unsigned const seconds = static_cast<unsigned>(unsignedMod(unixtime, 24 * 60 * 60));
82  unsigned const minutes = seconds / 60;
83  unsigned const ss = seconds % 60;
84  unsigned const hh = minutes / 60;
85  unsigned const mm = minutes % 60;
86  int const len = snprintf(buf, max, "%02u:%02u:%02u", hh, mm, ss);
87  if (0 <= len && static_cast<size_t>(len) < max) {
88  return static_cast<size_t>(len);
89  }
90  return 0;
91 }
uint64_t unsignedMod(int64_t num, int64_t den)
Definition: misc.h:154

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

unsigned shared::getExpOfTwo ( unsigned  n)

Definition at line 23 of file MathUtils.cpp.

References i.

23  {
24  unsigned i = 0;
25 
26  while ((n = n >> 1)) {
27  ++i;
28  }
29 
30  return i;
31 }
bool shared::isPowOfTwo ( unsigned  n)

Definition at line 19 of file MathUtils.cpp.

19  {
20  return (n & (n - 1)) == 0;
21 }
template<typename OSTREAM , typename CONTAINER >
OSTREAM& shared::operator<< ( OSTREAM &  os,
PrintContainer< CONTAINER >  pc 
)

Definition at line 98 of file misc.h.

References printContainer().

98  {
99  if (pc.container.empty()) {
100  return os << "()";
101  } else {
102  if constexpr (is_std_container<typename CONTAINER::value_type>::value) {
103  os << '(';
104  for (auto& container : pc.container) {
105  os << printContainer(container);
106  }
107  } else {
108  for (auto itr = pc.container.begin(); itr != pc.container.end(); ++itr) {
109  if constexpr (std::is_pointer_v<typename CONTAINER::value_type>) {
110  os << (itr == pc.container.begin() ? '(' : ' ') << (void const*)*itr;
111  } else {
112  os << (itr == pc.container.begin() ? '(' : ' ') << *itr;
113  }
114  }
115  }
116  return os << ')';
117  }
118 }
PrintContainer< CONTAINER > printContainer(CONTAINER &container)
Definition: misc.h:80

+ Here is the call graph for this function:

template<typename T , size_t N>
constexpr std::array<T, N> shared::powersOf ( T const  a)

Definition at line 173 of file misc.h.

173  {
174  return powersOfImpl<T>(a, std::make_index_sequence<N>{});
175 }
template<typename CONTAINER >
PrintContainer<CONTAINER> shared::printContainer ( CONTAINER &  container)
template<typename TO , typename FROM >
TO shared::reinterpretBits ( FROM const  from)

Definition at line 179 of file misc.h.

References FROM, TO, and to.

179  {
180  union {
181  FROM const from;
182  TO const to;
183  } const u{.from = from};
184  return u.to;
185 }
#define TO
#define FROM
char * to
uint64_t shared::unsignedMod ( int64_t  num,
int64_t  den 
)
inline

Definition at line 154 of file misc.h.

Referenced by formatHMS().

154  {
155  int64_t mod = num % den;
156  if (mod < 0) {
157  mod += den;
158  }
159  return mod;
160 }

+ Here is the caller graph for this function: