Thrill  0.1
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 //! template for constexpr min, because std::min is not good enough.
38 template <typename T>
39 constexpr static inline const T& min(const T& a, const T& b) {
40  return a < b ? a : b;
41 }
42 
43 //! template for constexpr max, because std::max is not good enough.
44 template <typename T>
45 constexpr static inline const T& max(const T& a, const T& b) {
46  return a > b ? a : b;
47 }
48 
49 /******************************************************************************/
50 
51 //! Compute the maximum of two values. This is a class, while std::max is a
52 //! function.
53 template <typename T>
54 class maximum : public std::binary_function<T, T, T>
55 {
56 public:
57  const T& operator () (const T& x, const T& y) const {
58  return std::max<T>(x, y);
59  }
60 };
61 
62 //! Compute the minimum of two values. This is a class, while std::min is a
63 //! function.
64 template <typename T>
65 class minimum : public std::binary_function<T, T, T>
66 {
67 public:
68  const T& operator () (const T& x, const T& y) const {
69  return std::min<T>(x, y);
70  }
71 };
72 
73 /******************************************************************************/
74 
75 //! apply the Functor to each item in a std::vector<T> and return a new
76 //! std::vector<U> with different type.
77 template <typename Type, typename Functor>
78 inline auto MapVector(const std::vector<Type>& input, const Functor& f)
79 ->std::vector<typename std::result_of<Functor(Type)>::type> {
80  std::vector<typename std::result_of<Functor(Type)>::type> output;
81  output.reserve(input.size());
82  for (typename std::vector<Type>::const_iterator it = input.begin();
83  it != input.end(); ++it) {
84  output.emplace_back(f(*it));
85  }
86  return output;
87 }
88 
89 /******************************************************************************/
90 
91 //! template for computing the component-wise sum of std::array or std::vector.
92 template <typename ArrayType,
93  typename Operation = std::plus<typename ArrayType::value_type> >
95 
96 //! Compute the component-wise sum of two std::array<T,N> of same sizes.
97 template <typename Type, size_t N, typename Operation>
98 class ComponentSum<std::array<Type, N>, Operation>
99  : public std::binary_function<
100  std::array<Type, N>, std::array<Type, N>, std::array<Type, N> >
101 {
102 public:
103  using ArrayType = std::array<Type, N>;
104  explicit ComponentSum(const Operation& op = Operation()) : op_(op) { }
105  ArrayType operator () (const ArrayType& a, const ArrayType& b) const {
106  ArrayType out;
107  for (size_t i = 0; i < N; ++i) out[i] = op_(a[i], b[i]);
108  return out;
109  }
110 
111 private:
112  Operation op_;
113 };
114 
115 //! Compute the component-wise sum of two std::vector<T> of same sizes.
116 template <typename Type, typename Operation>
117 class ComponentSum<std::vector<Type>, Operation>
118  : public std::binary_function<
119  std::vector<Type>, std::vector<Type>, std::vector<Type> >
120 {
121 public:
122  using VectorType = std::vector<Type>;
123  explicit ComponentSum(const Operation& op = Operation()) : op_(op) { }
124  VectorType operator () (const VectorType& a, const VectorType& b) const {
125  assert(a.size() == b.size());
126  VectorType out;
127  out.reserve(std::min(a.size(), b.size()));
128  for (size_t i = 0; i < min(a.size(), b.size()); ++i)
129  out.emplace_back(op_(a[i], b[i]));
130  return out;
131  }
132 
133 private:
134  Operation op_;
135 };
136 
137 //! Compute the concatenation of two std::vector<T>s.
138 template <typename Type>
140  : public std::binary_function<
141  std::vector<Type>, std::vector<Type>, std::vector<Type> >
142 {
143 public:
144  using VectorType = std::vector<Type>;
145  VectorType operator () (const VectorType& a, const VectorType& b) const {
146  VectorType out;
147  out.reserve(a.size() + b.size());
148  out.insert(out.end(), a.begin(), a.end());
149  out.insert(out.end(), b.begin(), b.end());
150  return out;
151  }
152 };
153 
154 } // namespace common
155 } // namespace thrill
156 
157 #endif // !THRILL_COMMON_FUNCTIONAL_HEADER
158 
159 /******************************************************************************/
double T
constexpr auto operator()(Type &&v) const noexcept -> decltype(std::forward< Type >(v))
Definition: functional.hpp:31
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:144
STL namespace.
Compute the concatenation of two std::vector<T>s.
Definition: functional.hpp:139
auto MapVector(const std::vector< Type > &input, const Functor &f) -> std::vector< typename std::result_of< Functor(Type)>::type >
Definition: functional.hpp:78
template for computing the component-wise sum of std::array or std::vector.
Definition: functional.hpp:94
int N
Definition: gen_data.py:15
list x
Definition: gen_data.py:39
std::vector< T, Allocator< T > > vector
vector with Manager tracking
Definition: allocator.hpp:228
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:39
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:45