Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
ref_array.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <array>
5#include <cstddef>
6#include <initializer_list>
7#include <iterator>
8#include <span>
9#include <stdexcept>
10#include <type_traits>
11
12namespace bb {
23template <typename T, std::size_t N> class RefArray {
24 public:
25 RefArray() = default;
26 RefArray(const std::array<T*, N>& ptr_array)
27 {
28 for (std::size_t i = 0; i < N; ++i) {
29 storage[i] = ptr_array[i];
30 }
31 }
33 {
34 for (std::size_t i = 0; i < N; ++i) {
35 storage[i] = &arr[i];
36 }
37 }
38 template <typename... Ts>
39 RefArray(T& first, Ts&... refs)
40 : storage{ &first, &refs... }
41 {}
43 {
44 for (std::size_t i = 0; i < N; ++i) {
45 storage[i] = &sp[i];
46 }
47 }
49 requires std::is_const_v<T>
50 {
51 for (std::size_t i = 0; i < N; ++i) {
52 storage[i] = &sp[i];
53 }
54 }
55
56 T& operator[](std::size_t idx) const
57 {
58 // GCC has a bug where it has trouble analyzing zip_view
59 // this is likely due to this bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104165
60 // We disable this - if GCC was right, we would have caught this at runtime
61#if !defined(__clang__) && defined(__GNUC__)
62#pragma GCC diagnostic push
63#pragma GCC diagnostic ignored "-Warray-bounds"
64#endif
65 BB_ASSERT_DEBUG(idx < N);
66 return *storage[idx];
67#if !defined(__clang__) && defined(__GNUC__)
68#pragma GCC diagnostic pop
69#endif
70 }
71
78 {
80 for (size_t idx = 0; idx < N; idx++) {
81 data[idx] = *storage[idx];
82 }
83
84 return data;
85 }
86
91 class iterator {
92 public:
100 : array(array)
101 , pos(pos)
102 {}
103
104 T& operator*() const
105 {
107 return (*array)[pos];
108 }
109
111 {
112 pos++;
113 return *this;
114 }
115
117 {
118 iterator temp = *this;
119 ++(*this);
120 return temp;
121 }
122
123 bool operator==(iterator const& other) const { return pos == other.pos; }
124 bool operator!=(iterator const& other) const { return pos != other.pos; }
125
126 private:
129 };
130
131 constexpr std::size_t size() const { return N; }
137 iterator begin() const { return iterator(this, 0); }
143 iterator end() const { return iterator(this, N); }
144
145 T** get_storage() { return storage; }
146 T* const* get_storage() const { return storage; }
147
148 private:
149 // We are making a high-level array, for simplicity having a C array as backing makes sense.
150 // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays)
152};
153
158template <typename T, typename... Ts> RefArray(T&, Ts&...) -> RefArray<T, 1 + sizeof...(Ts)>;
159
171template <typename T, std::size_t... Ns>
172RefArray<T, (Ns + ...)> constexpr concatenate(const RefArray<T, Ns>&... ref_arrays)
173{
174 // Fold expression to calculate the total size of the new array using fold expression
175 constexpr std::size_t TotalSize = (Ns + ...);
176 RefArray<T, TotalSize> concatenated;
177
179 // Copies elements from a given RefArray to the concatenated array
180 auto copy_into = [&](const auto& ref_array, std::size_t& offset) {
181 for (std::size_t i = 0; i < ref_array.size(); ++i) {
182 concatenated.get_storage()[offset + i] = &ref_array[i];
183 }
184 offset += ref_array.size();
185 };
186
187 // Fold expression to copy elements from each input RefArray to the concatenated array
188 (..., copy_into(ref_arrays, offset));
189
190 return concatenated;
191}
192
199template <typename T, std::size_t N1, std::size_t N2>
204
205template <typename T, std::size_t N1, std::size_t N2>
210
211template <typename T, std::size_t N1, std::size_t N2>
216} // namespace bb
constexpr size_t N
#define BB_ASSERT_DEBUG(expression,...)
Definition assert.hpp:55
#define BB_ASSERT_LT_NO_WASM(left, right,...)
Definition assert.hpp:182
Nested iterator class for RefArray, based on indexing into the pointer array. Provides semantics simi...
Definition ref_array.hpp:91
bool operator==(iterator const &other) const
iterator(RefArray const *array, std::size_t pos)
Constructs an iterator for a given RefArray object.
Definition ref_array.hpp:99
bool operator!=(iterator const &other) const
iterator & operator++()
RefArray const * array
iterator operator++(int)
A template class for a reference array. Behaves as if std::array<T&, N> was possible.
Definition ref_array.hpp:23
RefArray(std::span< std::remove_const_t< T >, N > sp)
Definition ref_array.hpp:48
T ** get_storage()
T *const * get_storage() const
iterator end() const
Returns an iterator to the end of the RefArray.
iterator begin() const
Returns an iterator to the beginning of the RefArray.
RefArray(const std::array< T *, N > &ptr_array)
Definition ref_array.hpp:26
RefArray(std::array< T, N > &arr)
Definition ref_array.hpp:32
std::array< T, N > get_copy()
Get a copy of the underlying data. Use carefully, as it allocates new data for the data pointed to by...
Definition ref_array.hpp:77
RefArray(T &first, Ts &... refs)
Definition ref_array.hpp:39
constexpr std::size_t size() const
RefArray(std::span< T, N > sp)
Definition ref_array.hpp:42
RefArray()=default
T & operator[](std::size_t idx) const
Definition ref_array.hpp:56
FF a
FF b
ssize_t offset
Definition engine.cpp:62
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
RefArray< T,(Ns+...)> constexpr concatenate(const RefArray< T, Ns > &... ref_arrays)
Concatenates multiple RefArray objects into a single RefArray.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::byte * data