Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
functional.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * thrill/common/functional.hpp
3  *
4  * Part of Project Thrill - http://project-thrill.org
5  *
6  * Copyright (C) 2015 Timo Bingmann <[email protected]>
7  * Copyright (C) 2015 Alexander Noe <[email protected]>
8  *
9  * All rights reserved. Published under the BSD-2 license in the LICENSE file.
10  ******************************************************************************/
11 
12 #pragma once
13 #ifndef THRILL_COMMON_FUNCTIONAL_HEADER
14 #define THRILL_COMMON_FUNCTIONAL_HEADER
15 
16 #include <algorithm>
17 #include <array>
18 #include <cassert>
19 #include <functional>
20 #include <tuple>
21 #include <type_traits>
22 #include <utility>
23 #include <vector>
24 
25 namespace thrill {
26 namespace common {
27 
28 //! Identity functor, very useful for default parameters.
29 struct Identity {
30  template <typename Type>
31  constexpr auto operator () (Type&& v) const noexcept
32  ->decltype(std::forward<Type>(v)) {
33  return std::forward<Type>(v);
34  }
35 };
36 
37 //! The noop functor, which takes any arguments and does nothing. This is a good
38 //! default argument for lambda function parameters.
39 template <typename ReturnType>
40 struct NoOperation {
41  ReturnType return_value_;
42 
43  explicit NoOperation(ReturnType return_value = ReturnType())
44  : return_value_(return_value) { }
45 
46  ReturnType operator () (...) const noexcept {
47  return return_value_;
48  }
49 };
50 
51 //! Specialized noop functor which returns a void.
52 template <>
53 struct NoOperation<void>{
54  void operator () (...) const noexcept { }
55 };
56 
57 //! template for constexpr min, because std::min is not good enough.
58 template <typename T>
59 constexpr static inline const T& min(const T& a, const T& b) {
60  return a < b ? a : b;
61 }
62 
63 //! template for constexpr max, because std::max is not good enough.
64 template <typename T>
65 constexpr static inline const T& max(const T& a, const T& b) {
66  return a > b ? a : b;
67 }
68 
69 /******************************************************************************/
70 
71 //! Compute the maximum of two values. This is a class, while std::max is a
72 //! function.
73 template <typename T>
74 class maximum : public std::binary_function<T, T, T>
75 {
76 public:
77  const T& operator () (const T& x, const T& y) const {
78  return std::max<T>(x, y);
79  }
80 };
81 
82 //! Compute the minimum of two values. This is a class, while std::min is a
83 //! function.
84 template <typename T>
85 class minimum : public std::binary_function<T, T, T>
86 {
87 public:
88  const T& operator () (const T& x, const T& y) const {
89  return std::min<T>(x, y);
90  }
91 };
92 
93 /******************************************************************************/
94 
95 //! apply the Functor to each item in a std::vector<T> and return a new
96 //! std::vector<U> with different type.
97 template <typename Type, typename Functor>
98 inline auto MapVector(const std::vector<Type>& input, const Functor& f)
99 ->std::vector<typename std::result_of<Functor(Type)>::type>{
100  std::vector<typename std::result_of<Functor(Type)>::type> output;
101  output.reserve(input.size());
102  for (typename std::vector<Type>::const_iterator it = input.begin();
103  it != input.end(); ++it) {
104  output.emplace_back(f(*it));
105  }
106  return output;
107 }
108 
109 /******************************************************************************/
110 
111 //! template for computing the component-wise sum of std::array or std::vector.
112 template <typename ArrayType,
113  typename Operation = std::plus<typename ArrayType::value_type> >
115 
116 //! Compute the component-wise sum of two std::array<T,N> of same sizes.
117 template <typename Type, size_t N, typename Operation>
118 class ComponentSum<std::array<Type, N>, Operation>
119  : public std::binary_function<
120  std::array<Type, N>, std::array<Type, N>, std::array<Type, N> >
121 {
122 public:
123  using ArrayType = std::array<Type, N>;
124  explicit ComponentSum(const Operation& op = Operation()) : op_(op) { }
125  ArrayType operator () (const ArrayType& a, const ArrayType& b) const {
126  ArrayType out;
127  for (size_t i = 0; i < N; ++i) out[i] = op_(a[i], b[i]);
128  return out;
129  }
130 
131 private:
132  Operation op_;
133 };
134 
135 //! Compute the component-wise sum of two std::vector<T> of same sizes.
136 template <typename Type, typename Operation>
137 class ComponentSum<std::vector<Type>, Operation>
138  : public std::binary_function<
139  std::vector<Type>, std::vector<Type>, std::vector<Type> >
140 {
141 public:
142  using VectorType = std::vector<Type>;
143  explicit ComponentSum(const Operation& op = Operation()) : op_(op) { }
144  VectorType operator () (const VectorType& a, const VectorType& b) const {
145  assert(a.size() == b.size());
146  VectorType out;
147  out.reserve(std::min(a.size(), b.size()));
148  for (size_t i = 0; i < min(a.size(), b.size()); ++i)
149  out.emplace_back(op_(a[i], b[i]));
150  return out;
151  }
152 
153 private:
154  Operation op_;
155 };
156 
157 //! Compute the concatenation of two std::vector<T>s.
158 template <typename Type>
160  : public std::binary_function<
161  std::vector<Type>, std::vector<Type>, std::vector<Type> >
162 {
163 public:
164  using VectorType = std::vector<Type>;
165  VectorType operator () (const VectorType& a, const VectorType& b) const {
166  VectorType out;
167  out.reserve(a.size() + b.size());
168  out.insert(out.end(), a.begin(), a.end());
169  out.insert(out.end(), b.begin(), b.end());
170  return out;
171  }
172 };
173 
174 } // namespace common
175 } // namespace thrill
176 
177 #endif // !THRILL_COMMON_FUNCTIONAL_HEADER
178 
179 /******************************************************************************/
ReturnType operator()(...) const noexcept
Definition: functional.hpp:46
const T & operator()(const T &x, const T &y) const
Definition: functional.hpp:77
double T
Identity functor, very useful for default parameters.
Definition: functional.hpp:29
Type
VFS object type.
Definition: file_io.hpp:52
std::vector< Type > VectorType
Definition: functional.hpp:164
const T & operator()(const T &x, const T &y) const
Definition: functional.hpp:88
Compute the concatenation of two std::vector<T>s.
Definition: functional.hpp:159
auto MapVector(const std::vector< Type > &input, const Functor &f) -> std::vector< typename std::result_of< Functor(Type)>::type >
Definition: functional.hpp:98
template for computing the component-wise sum of std::array or std::vector.
Definition: functional.hpp:114
int N
Definition: gen_data.py:15
VectorType operator()(const VectorType &a, const VectorType &b) const
Definition: functional.hpp:165
list x
Definition: gen_data.py:39
std::vector< T, Allocator< T > > vector
vector with Manager tracking
Definition: allocator.hpp:228
constexpr auto operator()(Type &&v) const noexcept-> decltype(std::forward< Type >(v))
Definition: functional.hpp:31
static uint_pair min()
return an uint_pair instance containing the smallest value possible
Definition: uint_types.hpp:217
static constexpr const T & min(const T &a, const T &b)
template for constexpr min, because std::min is not good enough.
Definition: functional.hpp:59
NoOperation(ReturnType return_value=ReturnType())
Definition: functional.hpp:43
static constexpr const T & max(const T &a, const T &b)
template for constexpr max, because std::max is not good enough.
Definition: functional.hpp:65