-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMisc.h
93 lines (79 loc) · 2.45 KB
/
Misc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* Misc.h
*
* Created on: 11 Feb 2018
* Author: jeremy
*/
#ifndef SOURCE_UTIL_MISC_H_
#define SOURCE_UTIL_MISC_H_
#include <functional>
#include <sstream>
#include <numeric>
#include <utility>
#include <vector>
namespace Util
{
template <size_t I = 0, typename F, typename... T>
inline typename std::enable_if_t<I == sizeof...(T)> for_each_tuple(std::tuple<T...>&, F)
{
return;
}
template <size_t I = 0, typename F, typename... T>
inline typename std::enable_if_t<(I < sizeof...(T))> for_each_tuple(std::tuple<T...>& t, F f)
{
f(std::get<I>(t));
for_each_tuple<I + 1, F, T...>(t, f);
}
template <class T>
inline void StrToValue(const std::string& is, T& val)
{
std::stringstream s(is);
s >> val;
}
template <typename... T>
inline void VectorOfStrToTuple(const std::vector<std::string>& vec, std::tuple<T...>& tuple)
{
size_t i = 0;
for_each_tuple(tuple, [&](auto& t){ StrToValue(vec[i++], t); });
}
/**
* Converts every element of input and concatenates the results to a string, where elements are separated by separator.
* The last separator is removed.
* @param input
* @param separator
* @return
*/
template <typename T>
static std::string JoinToStr(const T& input, std::string separator = ",")
{
std::string str = std::accumulate(std::begin(input), std::end(input), std::string{}, [&](const auto& a, const auto& b) { return a + std::to_string(b) + separator; });
return str.substr(0, str.size() - separator.size());
}
/**
*
* @brief If v compares less than lo, returns lo; otherwise if hi compares less than v, returns hi; otherwise returns v. Uses comp to compare the values.
* @param v the value to clamp.
* @param lo the low boundary to clamp v to.
* @param hi the high boundary to clamp v to.
* @param comp Comparison function object.
* @return Clamped value.
*/
template <class T, class Compare>
inline constexpr const T& clamp( const T& v, const T& lo, const T& hi, Compare comp )
{
return comp(v, lo) ? lo : comp(hi, v) ? hi : v;
}
/**
* @brief If v compares less than lo, returns lo; otherwise if hi compares less than v, returns hi; otherwise returns v.
* @param v the value to clamp.
* @param lo the low boundary to clamp v to.
* @param hi the high boundary to clamp v to.
* @return Clamped value.
*/
template <class T>
inline constexpr const T& clamp( const T& v, const T& lo, const T& hi )
{
return Util::clamp( v, lo, hi, std::less<>() );
}
}
#endif /* SOURCE_UTIL_MISC_H_ */