Caffe2 - C++ API
A deep learning, cross platform ML framework
functional.h
1 #pragma once
2 
3 #include <vector>
4 #include <c10/util/ArrayRef.h>
5 
6 namespace c10 {
7 
8 // The passed in function must take T by value (T), or by
9 // const reference (const T&); taking T by non-const reference
10 // will result in an error like:
11 //
12 // error: no type named 'type' in 'class std::result_of<foobar::__lambda(T)>'
13 //
14 // No explicit template parameters are required.
15 
16 // Overload for explicit function and ArrayRef
17 template<typename F, typename T>
18 inline auto fmap(const T& inputs, const F& fn) -> std::vector<decltype(fn(*inputs.begin()))> {
19  std::vector<decltype(fn(*inputs.begin()))> r;
20  r.reserve(inputs.size());
21  for(const auto & input : inputs)
22  r.push_back(fn(input));
23  return r;
24 }
25 
26 template<typename F, typename T>
27 inline auto fmap(T& inputs, const F& fn) -> std::vector<decltype(fn(*inputs.begin()))> {
28  std::vector<decltype(fn(*inputs.begin()))> r;
29  r.reserve(inputs.size());
30  for(auto & input : inputs)
31  r.push_back(fn(input));
32  return r;
33 }
34 
35 // C++ forbids taking an address of a constructor, so here's a workaround...
36 // Overload for constructor (R) application
37 template<typename R, typename T>
38 inline std::vector<R> fmap(const T& inputs) {
39  std::vector<R> r;
40  r.reserve(inputs.size());
41  for(auto & input : inputs)
42  r.push_back(R(input));
43  return r;
44 }
45 
46 template<typename F, typename T>
47 inline std::vector<T> filter(at::ArrayRef<T> inputs, const F& fn) {
48  std::vector<T> r;
49  r.reserve(inputs.size());
50  for(auto & input : inputs) {
51  if (fn(input)) {
52  r.push_back(input);
53  }
54  }
55  return r;
56 }
57 
58 template<typename F, typename T>
59 inline std::vector<T> filter(const std::vector<T>& inputs, const F& fn) {
60  return filter<F, T>(static_cast<at::ArrayRef<T>>(inputs), fn);
61 }
62 
63 } // namespace c10
constexpr size_t size() const
size - Get the array size.
Definition: ArrayRef.h:138
To register your own kernel for an operator, do in one (!) cpp file: C10_REGISTER_KERNEL(OperatorHand...
Definition: alias_info.h:7
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:41