barretenberg
Loading...
Searching...
No Matches
container.hpp
1#pragma once
2#include <algorithm>
3#include <cstddef>
4#include <string>
5#include <vector>
6
7template <typename C> C slice(C const& container, size_t start)
8{
9 auto b = container.begin();
10 auto e = container.end();
11 std::advance(b, start);
12 return C(b, e);
13}
14
15template <typename C> C slice(C const& container, size_t start, size_t end)
16{
17 auto b = container.begin();
18 auto e = container.begin();
19 std::advance(b, start);
20 std::advance(e, end);
21 return C(b, e);
22}
23
24template <typename C> C join(std::initializer_list<C> to_join)
25{
26 C result;
27 for (auto& e : to_join) {
28 result.insert(result.end(), e.begin(), e.end());
29 }
30 return result;
31}
32
33inline std::string join(std::vector<std::string> const& to_join, std::string const& with = ",")
34{
35 auto it = to_join.begin();
36 std::string result(*it++);
37 for (; it != to_join.end(); ++it) {
38 result += with;
39 result += *it;
40 }
41 return result;
42}
43
44template <template <typename, typename...> typename Cont, typename InnerCont, typename... Args>
45InnerCont flatten(Cont<InnerCont, Args...> const& in)
46{
47 InnerCont result;
48 for (auto& e : in) {
49 result.insert(result.end(), e.begin(), e.end());
50 }
51 return result;
52}
53
54// Return the first index at which a given item can be found in the vector.
55// Only safe for vectors with length less than the size_t overflow size.
56template <typename T> int64_t index_of(std::vector<T> const& vec, T const& item)
57{
58 auto const& begin = vec.begin();
59 auto const& end = vec.end();
60
61 auto const& itr = std::find(begin, end, item);
62
63 return itr == end ? -1 : std::distance(begin, itr);
64}