10 #include <unordered_map>
16 namespace Belle2::group_helper {
19 using _Remove_cvref_t = std::remove_cv_t<std::remove_reference_t<_Ty>>;
38 #define AXIS_NAME(name_, type_) \
40 struct name_ : axis_name_t<type_> { \
41 name_(type_ val) : axis_name_t<type_>(val) {} \
42 template <typename... T1>\
43 name_(const std::tuple <T1...> & val) :name_( std::get<name_>(val)) {} \
45 template <typename T1> \
46 auto operator()(const T1& val){ \
47 m_value = std::get<name_>(val); \
50 template <typename T1>\
51 auto operator==(const T1& rhs ) const -> decltype( std::get<name_>(rhs) == m_value ){\
52 return std::get<name_>(rhs) == m_value ;\
59 template <
typename T,
typename... FUNC_T>
60 auto fill_vector(T& klmDigits, FUNC_T&& ... func)
62 std::vector<std::tuple< decltype(func(klmDigits[0]))... >> hits;
63 for (
int i = 0; i < klmDigits.getEntries(); ++i) {
64 hits.emplace_back(func(klmDigits[i])...);
70 template <
typename T1,
typename T2>
76 __range__impl(
const T1& b,
const T2& e) : m_begin(b), m_end(e) {}
93 auto operator[](
size_t i)
const
95 return *(m_begin + i);
99 return m_end - m_begin;
107 template <
typename T1,
typename T2>
108 auto __range__(T1&& b, T2&& e)
114 template <
typename... T>
116 template <
typename VEC_T,
typename T1,
typename... T_rest>
117 static auto __isEequal(
const VEC_T& vecA,
const VEC_T& vecB) -> decltype(std::enable_if_t< (
bool)
sizeof...(T_rest),
bool> {})
119 if (std::get<T1>(vecA) != std::get<T1>(vecB)) {
122 return __isEequal< VEC_T, T_rest...>(vecA, vecB);
126 template <
typename VEC_T,
typename T1>
127 static bool __isEequal(
const VEC_T& vecA,
const VEC_T& vecB)
129 return std::get<T1>(vecA) == std::get<T1>(vecB);
132 template <
typename VEC_T,
typename... FUNC_T>
133 static auto apply(
const std::vector<VEC_T>& vec, FUNC_T&& ... fun)
136 std::vector< std::tuple<T..., decltype(fun(__range__(std::begin(vec), std::end(vec))))... >> ret;
140 auto tail = std::begin(vec);
142 for (
auto head = std::begin(vec); head != std::end(vec); ++head) {
145 ret.emplace_back(std::get<T>(*tail)..., fun(__range__(tail, head))...);
151 ret.emplace_back(std::get<T>(*tail)..., fun(__range__(tail, std::end(vec)))...);
155 template <
typename A1 ,
typename... ARGGS>
158 template<
class ARRAY_T>
159 static constexpr
auto has_safe_access(ARRAY_T&& arr) -> decltype(arr.at(0), std::true_type());
160 static constexpr
auto has_safe_access(...)->std::false_type;
162 template <
class ARRAY_T>
163 static auto& get_safe_if_possible(ARRAY_T& arr,
size_t i)
165 if constexpr(decltype(has_safe_access(arr))::value) {
174 template <
typename T1,
typename T2>
175 static auto& get(
const T1& e, T2& out_array)
177 if constexpr(
sizeof...(ARGGS)) {
179 get_safe_if_possible(out_array, std::get<A1>(e))
182 return get_safe_if_possible(out_array, std::get<A1>(e));
188 template <
typename T1,
typename T2,
typename T3,
typename FUNC_T>
189 static auto to_array(
const T2& in_array, T3& out_array, FUNC_T&& fun)
191 for (
const auto& e : in_array) {
193 x = fun(x, std::get<T1>(e));
196 template <
typename T1,
typename T2,
typename T3>
197 static auto to_array(
const T2& in_array, T3& out_array)
199 return to_array<T1>(in_array, out_array, std::plus());
204 template <
typename CONTAINER_T,
typename FUNC_T>
205 void erase_remove_if(CONTAINER_T& container, FUNC_T&& fun)
208 std::remove_if(container.begin(), container.end(),
209 std::forward<FUNC_T>(fun)),
214 template <
typename CONTAINER_T>
215 void sort(CONTAINER_T& container)
217 std::sort(container.begin(), container.end());
220 template <
typename CONTAINER_T>
221 void drop_duplicates(CONTAINER_T& container)
223 container.erase(std::unique(container.begin(), container.end()), container.end());
227 template<
typename T,
typename U>
228 constexpr
auto operator()(T&& t, U&& u)
const -> decltype((T&&) t + (U&&) u)
230 return (T&&) t + (U&&) u;
234 template<
typename T1 =
int>
236 explicit greater(T1&& data) : m_data(data) {}
237 explicit greater(
const T1& data) : m_data(data) {}
241 constexpr
auto operator()(
const T1& u)
const
247 template<
typename T1>
253 constexpr
auto operator()(U&& u)
const
255 return m_data <= (U&&) u;
260 constexpr T&& operator()(T&& t)
const noexcept
267 template <
typename CONTAINER_T,
typename INIT_T,
typename OP_T = plus,
typename PROJECTION_T =
identity>
268 auto accumulate(
const CONTAINER_T& container, INIT_T init, OP_T op =
plus {} , PROJECTION_T proj = identity{})
270 for (
const auto& e : container)
271 init = op(init, proj(e));
275 template <
typename CONTAINER_T,
typename OP_T = group_helper::greater<
int>,
typename PROJECTION_T =
identity>
276 auto count_if(
const CONTAINER_T& container, OP_T op = group_helper::greater<int> {0} , PROJECTION_T proj = identity{})
279 for (
const auto& e : container)
285 template <
typename CONTAINER_T,
typename CONDITION_T,
typename DEFAULT_T =
int,
typename PROJECTION_T =
identity>
286 auto first_or_default(
const CONTAINER_T& container,
const CONDITION_T& con, DEFAULT_T default__ = 0,
287 PROJECTION_T proj = identity {})
289 decltype(proj(container[0])) ret = default__;
290 for (
const auto& e : container) {