24 namespace polyhedral {
25 namespace functional {
28 void App(std::function<
void(I)> fun,
const std::vector<I>& vec) {
35 void App(std::function<
void(I)> fun,
const std::vector<I>&& vec) {
40 void App(std::function<
void(I,
size_t)> fun,
const std::vector<I>& vec) {
48 void App(std::function<
void(I,
size_t)> fun,
const std::vector<I>&& vec) {
52 template <
typename R,
typename I>
53 std::vector<R> Map(std::function<R(I)> fun,
const std::vector<I>& vec) {
55 res.reserve(vec.size());
57 res.push_back(fun(i));
62 template <
typename R,
typename I>
63 std::vector<R> Map(std::function<R(I)> fun, std::vector<I>&& vec) {
64 return Map<R, I>(fun, vec);
69 std::function<I(
const I&,
const I&)> red,
71 const std::vector<I>& vec) {
73 for (
int i = 0; i < vec.size(); ++i) {
74 res = red(res, vec.at(i));
80 I Reduce(std::function<I(
const I&,
const I&)> red,
const std::vector<I>& vec) {
81 const std::vector<I> v(vec.begin() + 1, vec.end());
82 return Reduce(red, vec.at(0), v);
86 I Reduce(std::function<I(I&&, I&&)> red, I&& initVal, std::vector<I>&& vec) {
87 I res = std::move(initVal);
88 for (
int i = 0; i < vec.size(); ++i) {
89 res = red(std::move(res), std::move(vec.at(i)));
95 I Reduce(std::function<I(I&&, I&&)> red, std::vector<I>&& vec) {
96 I res = std::move(vec.at(0));
97 for (
int i = 1; i < vec.size(); ++i) {
98 res = red(std::move(res), std::move(vec.at(i)));
114 template <
typename Func,
typename T>
115 std::vector<T> Filter(Func f,
const std::vector<T>& input) {
117 std::is_same<
typename function_traits<Func>::result_type,
bool>::value,
118 "Filtering function must return bool");
120 function_traits<Func>::n_args == 1,
121 "Filtering function must take one argument");
123 std::is_same<
typename function_traits<Func>::template arg<0>::type, T>::
125 "The argument of the filtering function must have the same type "
126 "as the element type of the collection being filtered");
129 res.reserve(input.size());
130 for (
const auto& i : input) {
139 template <
typename R,
typename I>
140 R MapReduce(std::function<R(R, I,
bool)> fun,
const std::vector<I>& vec) {
141 R res = fun(R(), vec.at(0),
true);
142 for (
int i = 1; i < vec.size(); ++i) {
143 res = fun(res, vec.at(i),
false);