Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
container.hpp
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3#include <cstddef>
4#include <string>
5#include <vector>
6
7namespace bb {
8
9template <typename C> C slice(C const& container, size_t start)
10{
11 auto b = container.begin();
12 auto e = container.end();
13 std::advance(b, start);
14 return C(b, e);
15}
16
17template <typename C> C slice(C const& container, size_t start, size_t end)
18{
19 auto b = container.begin();
20 auto e = container.begin();
21 std::advance(b, start);
22 std::advance(e, end);
23 return C(b, e);
24}
25
26template <typename C> C join(std::initializer_list<C> to_join)
27{
28 C result;
29 for (auto& e : to_join) {
30 result.insert(result.end(), e.begin(), e.end());
31 }
32 return result;
33}
34
35inline std::string join(std::vector<std::string> const& to_join, std::string const& with = ",")
36{
37 auto it = to_join.begin();
38 std::string result(*it++);
39 for (; it != to_join.end(); ++it) {
40 result += with;
41 result += *it;
42 }
43 return result;
44}
45
46template <template <typename, typename...> typename Cont, typename InnerCont, typename... Args>
47InnerCont flatten(Cont<InnerCont, Args...> const& in)
48{
49 InnerCont result;
50 for (auto& e : in) {
51 result.insert(result.end(), e.begin(), e.end());
52 }
53 return result;
54}
55
56// Return the first index at which a given item can be found in the vector.
57// Only safe for vectors with length less than the size_t overflow size.
58template <typename T> int64_t index_of(std::vector<T> const& vec, T const& item)
59{
60 auto const& begin = vec.begin();
61 auto const& end = vec.end();
62
63 auto const& itr = std::find(begin, end, item);
64
65 return itr == end ? -1 : std::distance(begin, itr);
66}
67
68// A simple sum meant for small containers (i.e. doesn't use threading)
69template <template <typename, typename...> typename Cont, typename Inner, typename... Args>
70Inner sum(Cont<Inner, Args...> const& in)
71{
72 Inner result{};
73 for (auto& e : in) {
74 result += e;
75 }
76 return result;
77}
78
79// A simple sum meant for small containers (i.e. doesn't use threading)
80template <template <typename, typename...> typename Cont, typename Left, typename Right, typename... Args>
82{
83 std::pair<Left, Right> result{ {}, {} };
84 for (auto& e : in) {
85 result.first += e.first;
86 result.second += e.second;
87 }
88 return result;
89}
90
91} // namespace bb
FF b
bb::avm2::Column C
Entry point for Barretenberg command-line interface.
std::pair< Left, Right > sum_pairs(Cont< std::pair< Left, Right >, Args... > const &in)
Definition container.hpp:81
InnerCont flatten(Cont< InnerCont, Args... > const &in)
Definition container.hpp:47
C slice(C const &container, size_t start)
Definition container.hpp:9
int64_t index_of(std::vector< T > const &vec, T const &item)
Definition container.hpp:58
Inner sum(Cont< Inner, Args... > const &in)
Definition container.hpp:70
C join(std::initializer_list< C > to_join)
Definition container.hpp:26
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13