My Project
Functional.h
Go to the documentation of this file.
1 //===- Functional.h - Helpers for functional-style Combinators --*- C++ -*-===//
2 //
3 // Part of the MLIR Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef MLIR_SUPPORT_FUNCTIONAL_H_
10 #define MLIR_SUPPORT_FUNCTIONAL_H_
11 
12 #include "mlir/Support/LLVM.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/Support/Casting.h"
16 
22 
23 namespace mlir {
24 namespace functional {
25 
27 template <typename Fn, typename IterType>
28 auto map(Fn fun, IterType begin, IterType end)
30  using R = typename std::result_of<Fn(decltype(*begin))>::type;
32  // auto i works with both pointer types and value types with an operator*.
33  // auto *i only works for pointer types.
34  for (auto i = begin; i != end; ++i) {
35  res.push_back(fun(*i));
36  }
37  return res;
38 }
39 
41 template <typename Fn, typename ContainerType>
42 auto map(Fn fun, ContainerType input)
43  -> decltype(map(fun, std::begin(input), std::end(input))) {
44  return map(fun, std::begin(input), std::end(input));
45 }
46 
50 template <typename Fn, typename ContainerType1, typename ContainerType2>
51 auto zipMap(Fn fun, ContainerType1 input1, ContainerType2 input2)
52  -> SmallVector<typename std::result_of<Fn(decltype(*input1.begin()),
53  decltype(*input2.begin()))>::type,
54  8> {
55  using R = typename std::result_of<Fn(decltype(*input1.begin()),
56  decltype(*input2.begin()))>::type;
58  auto zipIter = llvm::zip(input1, input2);
59  for (auto it : zipIter) {
60  res.push_back(fun(std::get<0>(it), std::get<1>(it)));
61  }
62  return res;
63 }
64 
66 template <typename Fn, typename IterType>
67 void apply(Fn fun, IterType begin, IterType end) {
68  // auto i works with both pointer types and value types with an operator*.
69  // auto *i only works for pointer types.
70  for (auto i = begin; i != end; ++i) {
71  fun(*i);
72  }
73 }
74 
76 template <typename Fn, typename ContainerType>
77 void apply(Fn fun, ContainerType input) {
78  return apply(fun, std::begin(input), std::end(input));
79 }
80 
84 template <typename Fn, typename ContainerType1, typename ContainerType2>
85 void zipApply(Fn fun, ContainerType1 input1, ContainerType2 input2) {
86  auto zipIter = llvm::zip(input1, input2);
87  for (auto it : zipIter) {
88  fun(std::get<0>(it), std::get<1>(it));
89  }
90 }
91 
95 template <typename T, typename ToType = T>
96 inline std::function<ToType *(T *)> makePtrDynCaster() {
97  return [](T *val) { return dyn_cast<ToType>(val); };
98 }
99 
101 struct ScopeGuard {
102  explicit ScopeGuard(std::function<void(void)> destruct)
103  : destruct(destruct) {}
104  ~ScopeGuard() { destruct(); }
105 
106 private:
107  std::function<void(void)> destruct;
108 };
109 
110 } // namespace functional
111 } // namespace mlir
112 
113 #endif // MLIR_SUPPORT_FUNCTIONAL_H_
Definition: InferTypeOpInterface.cpp:20
void zipApply(Fn fun, ContainerType1 input1, ContainerType2 input2)
Definition: Functional.h:85
IterType
Definition: Builders.h:27
~ScopeGuard()
Definition: Functional.h:104
ScopeGuard(std::function< void(void)> destruct)
Definition: Functional.h:102
auto map(Fn fun, IterType begin, IterType end) -> SmallVector< typename std::result_of< Fn(decltype(*begin))>::type, 8 >
Map with iterators.
Definition: Functional.h:28
auto zipMap(Fn fun, ContainerType1 input1, ContainerType2 input2) -> SmallVector< typename std::result_of< Fn(decltype(*input1.begin()), decltype(*input2.begin()))>::type, 8 >
Definition: Functional.h:51
Definition: LLVM.h:35
void apply(Fn fun, IterType begin, IterType end)
Apply with iterators.
Definition: Functional.h:67
Simple ScopeGuard.
Definition: Functional.h:101
std::function< ToType *(T *)> makePtrDynCaster()
Definition: Functional.h:96