1 #ifndef Analysis_Tools_Utilities_h 2 #define Analysis_Tools_Utilities_h 18 template<
typename Type,
unsigned N,
unsigned Last>
21 static void print(std::ostream& out,
const Type& value) {
22 out << std::get<N>(value) <<
", ";
26 template<
typename Type,
unsigned N>
29 static void print(std::ostream& out,
const Type& value) {
30 out << std::get<N>(value);
34 template<
typename... Types>
35 std::ostream&
operator<<(std::ostream& out,
const std::tuple<Types...>& value) {
43 template <
size_t... n>
64 template <
size_t... indices,
typename Tuple>
66 -> decltype(std::make_tuple(std::get<indices>(tpl)...))
68 return std::make_tuple(std::get<indices>(tpl)...);
72 template <
typename Head,
typename... Tail>
73 std::tuple<Tail...>
tuple_tail(
const std::tuple<Head, Tail...>& tpl)
87 const uint32_t& b =
reinterpret_cast<const uint32_t&
>(std::get<0>(t));
100 const uint32_t& b =
reinterpret_cast<const uint32_t&
>(std::get<0>(t));
101 return static_cast<result_type
>(b);
105 template<
typename Head,
typename... ndims>
struct hash<
std::tuple<Head, ndims...> >
114 template<>
struct hash<
std::tuple<> >
127 void handleError(
const std::string& fClass,
const std::string& fMessage)
129 std::stringstream sserr;
130 sserr<<fClass<<
" ERROR: "<<fMessage;
131 throw std::runtime_error(sserr.str());
134 inline float getFloat(
const std::string& token)
137 float result = strtod (token.c_str(), &endptr);
138 if (endptr == token.c_str())
140 std::stringstream sserr;
141 sserr<<
"can't convert token "<<token<<
" to float value";
142 handleError(
"getFloat",sserr.str());
147 inline unsigned getUnsigned(
const std::string& token)
150 unsigned result = strtoul (token.c_str(), &endptr, 0);
151 if (endptr == token.c_str())
153 std::stringstream sserr;
154 sserr<<
"can't convert token "<<token<<
" to unsigned value";
155 handleError(
"getUnsigned",sserr.str());
159 inline long int getSigned(
const std::string& token)
162 unsigned result = strtol (token.c_str(), &endptr, 0);
163 if (endptr == token.c_str())
165 std::stringstream sserr;
166 sserr<<
"can't convert token "<<token<<
" to signed value";
167 handleError(
"getSigned",sserr.str());
172 inline std::string getSection(
const std::string& token)
174 size_t iFirst = token.find (
'[');
175 size_t iLast = token.find (
']');
176 if (iFirst != std::string::npos && iLast != std::string::npos && iFirst < iLast)
177 return std::string (token, iFirst+1, iLast-iFirst-1);
181 inline std::vector<std::string> getTokens(
const std::string& fLine)
183 std::vector<std::string> tokens;
184 std::string currentToken;
185 for (
unsigned ipos = 0; ipos < fLine.length (); ++ipos)
187 char c = fLine[ipos];
191 if (!currentToken.empty())
193 tokens.push_back(currentToken);
194 currentToken.clear();
200 if (!currentToken.empty()) tokens.push_back(currentToken);
204 inline std::string getDefinitions(
const std::string& token)
206 size_t iFirst = token.find (
'{');
207 size_t iLast = token.find (
'}');
208 if (iFirst != std::string::npos && iLast != std::string::npos && iFirst < iLast)
209 return std::string (token, iFirst+1, iLast-iFirst-1);
213 inline float quadraticInterpolation(
float fZ,
const float fX[3],
const float fY[3])
218 D[0] = fX[0]*fX[1]*(fX[0]-fX[1])+fX[1]*fX[2]*(fX[1]-fX[2])+fX[2]*fX[0]*(fX[2]-fX[0]);
219 D[3] = fY[0]*(fX[1]-fX[2])+fY[1]*(fX[2]-fX[0])+fY[2]*(fX[0]-fX[1]);
220 D[2] = fY[0]*(pow(fX[2],2)-pow(fX[1],2))+fY[1]*(pow(fX[0],2)-pow(fX[2],2))+fY[2]*(pow(fX[1],2)-pow(fX[0],2));
221 D[1] = fY[0]*fX[1]*fX[2]*(fX[1]-fX[2])+fY[1]*fX[0]*fX[2]*(fX[2]-fX[0])+fY[2]*fX[0]*fX[1]*(fX[0]-fX[1]);
234 float r = a[0]+fZ*(a[1]+fZ*a[2]);
241 template<
typename ,
typename >
245 template<
typename... LEFT,
typename... RIGHT>
246 struct join_tuples<std::tuple<LEFT...>, std::tuple<RIGHT...>>
248 typedef std::tuple<LEFT..., RIGHT...>
type;
250 template<
typename T,
unsigned N>
251 struct generate_tuple_type
253 typedef typename generate_tuple_type<
T, N/2>
::type left;
254 typedef typename generate_tuple_type<
T, N/2 + N%2>
::type right;
258 struct generate_tuple_type<T, 1>
260 typedef std::tuple<T>
type;
263 struct generate_tuple_type<T, 0>
265 typedef std::tuple<>
type;
270 template<
class T>
using Invoke =
typename T::type;
272 template<
unsigned...>
struct seq{
using type = seq; };
274 template<
class S1,
class S2>
struct concat;
276 template<
unsigned... I1,
unsigned... I2>
277 struct concat<seq<I1...>, seq<I2...>>
278 : seq<I1..., (sizeof...(I1)+I2)...>{};
280 template<
class S1,
class S2>
281 using Concat = Invoke<concat<S1, S2>>;
283 template<
unsigned N>
struct gen_seq;
284 template<
unsigned N>
using GenSeq = Invoke<gen_seq<N>>;
287 struct gen_seq : Concat<GenSeq<N/2>, GenSeq<N - N/2>>{};
289 template<>
struct gen_seq<0> : seq<>{};
290 template<>
struct gen_seq<1> : seq<0>{};
293 template <
typename F,
unsigned... Is>
294 auto gen_tuple_impl(F func, seq<Is...> )
295 -> decltype(std::make_tuple(func(Is)...))
297 return std::make_tuple(func(Is)...);
299 template <
unsigned N,
typename F>
300 auto gen_tuple(F func)
301 -> decltype(gen_tuple_impl(func, GenSeq<N>() ))
303 return gen_tuple_impl(func, GenSeq<N>() );
std::tuple< Head, ndims... > argument_type
std::tuple< Tail... > tuple_tail(const std::tuple< Head, Tail... > &tpl)
auto tuple_subset(const Tuple &tpl, ct_integers_list< indices... >) -> decltype(std::make_tuple(std::get< indices >(tpl)...))
ct_iota_1< max-1 >::type::template push_back< max >::type type
result_type operator()(const argument_type &t) const
result_type operator()(const argument_type &t) const
static void print(std::ostream &out, const Type &value)
std::tuple< Head, ndims... > argument_type
std::ostream & operator<<(std::ostream &out, const std::tuple< Types... > &value)
std::tuple< float > argument_type
result_type operator()(const argument_type &t) const
static void print(std::ostream &out, const Type &value)
result_type operator()(const argument_type &t) const
ct_integers_list< n..., m > type