Fleet  0.0.9
Inference in the LOT
Miscellaneous.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 #include <chrono>
5 #include <array>
6 
7 #include "Numerics.h"
8 
9 
10 // From https://stackoverflow.com/questions/478898/how-to-execute-a-command-and-get-output-of-command-within-c-using-posix
11 std::string system_exec(const char* cmd) {
18  std::array<char, 1024> buffer;
19  std::string result;
20  std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
21  if (!pipe) {
22  throw std::runtime_error("popen() failed!");
23  }
24  while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
25  result += buffer.data();
26  }
27  return result;
28 }
29 
30 
32 // Python-like pass statements
34 
35 #define pass ((void)0)
36 
37 template <typename T>
38 void UNUSED(const T& x) {}
39 
43 
44 // Hash combination from https://stackoverflow.com/questions/2590677/how-do-i-combine-hash-values-in-c0x
45 inline void hash_combine(std::size_t& seed) { }
46 
52 template <typename T, typename... Rest>
53 inline void hash_combine(std::size_t& seed, const T& v, Rest... rest) {
54 
55  std::hash<T> hasher;
56  seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
57  hash_combine(seed, rest...);
58 }
59 
63 
71 template<typename M>
72 typename M::mapped_type get(const M& m, const typename M::key_type& key, const typename M::mapped_type& def) {
73  if(m.contains(key)) return m.at(key);
74  else return def;
75 }
76 
77 
81 
82 //https://stackoverflow.com/questions/13830158/check-if-a-variable-is-iterable
83 template <typename T, typename = void>
84 struct is_iterable : std::false_type {};
85 
86 // this gets used only when we can call std::begin() and std::end() on that type
87 template <typename T>
88 struct is_iterable<T, std::void_t<decltype(std::begin(std::declval<T>())),
89  decltype(std::end(std::declval<T>()))
90  >
91  > : std::true_type {};
92 
93 // Here is a helper:
94 template <typename T>
95 constexpr bool is_iterable_v = is_iterable<T>::value;
96 
97 
101 
103 // from https://stackoverflow.com/questions/42258608/c-constexpr-values-for-types
104 template <class T, class Tuple>
105 struct TypeIndex;
106 
107 template <class T, class... Types>
108 struct TypeIndex<T, std::tuple<T, Types...>> {
109  static const size_t value = 0;
110 };
111 
112 template <class T, class U, class... Types>
113 struct TypeIndex<T, std::tuple<U, Types...>> {
114  static const size_t value = 1 + TypeIndex<T, std::tuple<Types...>>::value;
115 };
116 
120 
122 // * @brief Check if a type is contained in parameter pack
123 // * @return
124 // */
125 template<typename X, typename... Ts>
126 constexpr bool contains_type() {
127  return std::disjunction<std::is_same<X, Ts>...>::value;
128 }
129 
133 
134 
135 //https://stackoverflow.com/questions/16337610/how-to-know-if-a-type-is-a-specialization-of-stdvector
136 template<typename Test, template<typename...> class Ref>
137 struct is_specialization : std::false_type {};
138 
139 template<template<typename...> class Ref, typename... Args>
140 struct is_specialization<Ref<Args...>, Ref>: std::true_type {};
141 
145 
146 
147 template<typename T>
148 T mymin(T a) {
149  return a; // if std::isnan(a) then we also return that
150 }
151 
152 template<typename T, typename... Args>
153 T mymin(T a, Args... args) {
154  // if we are a nan, then remove
155  if(std::isnan(a)) {
156  return mymin(args...);
157  }
158  else {
159  return std::min(a, mymin(args...));
160  }
161 }
void UNUSED(const T &x)
Definition: Miscellaneous.h:38
std::string system_exec(const char *cmd)
Definition: Miscellaneous.h:11
const word U
Definition: Main.cpp:12
Check if a type is contained in parameter pack // *.
Definition: Miscellaneous.h:137
Helpers to Find the numerical index (as a nonterminal_t) in a tuple of a given type.
Definition: Miscellaneous.h:105