forked from haptork/easyLambda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reduces.hpp
75 lines (66 loc) · 1.89 KB
/
reduces.hpp
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
/*!
* @file
* Function objects for basic piecemean/streaming reductions
*
* This file is a part of easyLambda(ezl) project for parallel data
* processing with modern C++ and MPI.
*
* @copyright Utkarsh Bhardwaj <[email protected]> 2015-2016
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying LICENSE.md or copy at * http://boost.org/LICENSE_1_0.txt)
* */
#ifndef REDUCES_EZL_ALGO_H
#define REDUCES_EZL_ALGO_H
#include <tuple>
namespace ezl {
/*!
* @ingroup algorithms
* function object for returning count of rows irrespective of key/value types.
*
* Example usage:
* @code
* load<string>("*.txt").colSeparator("").rowSeparator('s')
* .reduce<1>(ezl::count()).build()
* @endcode
*
* */
class count {
public:
template<class K, class V, class R>
auto operator () (const R& res, const K&, const V&) {
return 1 + res; //std::get<0>(res) + 1;
}
};
/*!
* @ingroup algorithms
* function object for summation of value columns.
*
* Example usage:
* @code
* load<string>("*.txt").colSeparator("").rowSeparator('s')
* .reduce<1>(ezl::count()).build()
* @endcode
*
* */
class sum {
public:
template<class... R, class... K, class V>
auto operator () (const std::tuple<R...>& res, const std::tuple<K...>&, const V& val) {
return _sum(
res, val, std::make_index_sequence<std::tuple_size<V>::value>{});
};
template<class K, class V, class... Vs, class R>
typename std::enable_if<!detail::meta::isTuple<R>{}, R>::type
operator () (const R& res, const K&, const std::tuple<const V&,
const Vs&...>& val) {
return res + std::get<0>(val);
};
private:
template <typename R, typename V, std::size_t... index>
inline R _sum(const R &tup1, const V &tup2,
std::index_sequence<index...>) {
return std::make_tuple(std::get<index>(tup1) + (std::get<index>(tup2))...);
}
};
} // namespace ezl
#endif // !REDUCES_EZL_ALGO_H