Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
map.hpp
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3#include <array>
4#include <type_traits>
5
6namespace bb::transform {
7/*
8 * Generic map function for mapping a containers element to another type.
9 */
10template <template <typename, typename...> typename Cont,
11 typename InElem,
12 typename... Args,
13 typename F,
14 typename OutElem = typename std::invoke_result<F, InElem const&>::type>
15Cont<OutElem> map(Cont<InElem, Args...> const& in, F&& op)
16{
17 Cont<OutElem> result;
18 std::transform(in.begin(), in.end(), std::back_inserter(result), op);
19 return result;
20}
21
22/*
23 * Generic map function for mapping a std::array's elements to another type.
24 * TODO: this has only been added because I (Mike) couldn't get the above to work
25 * with an array.
26 */
27template <std::size_t SIZE,
28 typename InElem,
29 typename F,
30 typename OutElem = typename std::invoke_result<F, InElem const&>::type>
32{
34 std::transform(in.begin(), in.end(), result.begin(), op);
35 return result;
36}
37
38/*
39 * Generic map function for mapping a containers element to another type.
40 * This version passes the element index as a second argument to the operator function.
41 */
42template <template <typename, typename...> typename Cont,
43 typename InElem,
44 typename... Args,
45 typename F,
46 typename OutElem = typename std::invoke_result<F, InElem const&, size_t>::type>
47Cont<OutElem> mapi(Cont<InElem, Args...> const& in, F op)
48{
49 Cont<OutElem> result;
50 for (size_t i = 0; i < in.size(); ++i) {
51 result.push_back(op(in[i], i));
52 }
53 return result;
54}
55
56/*
57 * Generic filter function for containers.
58 */
59template <typename Cont, typename F> Cont filter(Cont const& in, F op)
60{
61 Cont copy(in);
62 std::remove_if(copy.begin(), copy.end(), op);
63 return copy;
64}
65} // namespace bb::transform
Cont< OutElem > map(Cont< InElem, Args... > const &in, F &&op)
Definition map.hpp:15
Cont filter(Cont const &in, F op)
Definition map.hpp:59
Cont< OutElem > mapi(Cont< InElem, Args... > const &in, F op)
Definition map.hpp:47
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13