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
11namespace bb {
22template <typename T, std::size_t N> class RefArray {
23 public:
24 RefArray() = default;
25 RefArray(const std::array<T*, N>& ptr_array)
26 {
27 for (std::size_t i = 0; i < N; ++i) {
28 storage[i] = ptr_array[i];
29 }
30 }
32 {
33 for (std::size_t i = 0; i < N; ++i) {
34 storage[i] = &arr[i];
35 }
36 }
37 template <typename... Ts>
38 RefArray(T& first, Ts&... refs)
39 : storage{ &first, &refs... }
40 {}
41
42 T& operator[](std::size_t idx) const
43 {
44 // GCC has a bug where it has trouble analyzing zip_view
45 // this is likely due to this bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104165
46 // We disable this - if GCC was right, we would have caught this at runtime
47#if !defined(__clang__) && defined(__GNUC__)
48#pragma GCC diagnostic push
49#pragma GCC diagnostic ignored "-Warray-bounds"
50#endif
51 BB_ASSERT_LT(idx, N);
52 return *storage[idx];
53#if !defined(__clang__) && defined(__GNUC__)
54#pragma GCC diagnostic pop
55#endif
56 }
57
64 {
66 for (size_t idx = 0; idx < N; idx++) {
67 data[idx] = *storage[idx];
68 }
69
70 return data;
71 }
72
77 class iterator {
78 public:
86 : array(array)
87 , pos(pos)
88 {}
89
90 T& operator*() const
91 {
92 BB_ASSERT_LT(pos, N);
93 return (*array)[pos];
94 }
95
97 {
98 pos++;
99 return *this;
100 }
101
103 {
104 iterator temp = *this;
105 ++(*this);
106 return temp;
107 }
108
109 bool operator==(iterator const& other) const { return pos == other.pos; }
110 bool operator!=(iterator const& other) const { return pos != other.pos; }
111
112 private:
115 };
116
117 constexpr std::size_t size() const { return N; }
123 iterator begin() const { return iterator(this, 0); }
129 iterator end() const { return iterator(this, N); }
130
131 T** get_storage() { return storage; }
132 T* const* get_storage() const { return storage; }
133
134 private:
135 // We are making a high-level array, for simplicity having a C array as backing makes sense.
136 // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays)
137 T* storage[N];
138};
139
144template <typename T, typename... Ts> RefArray(T&, Ts&...) -> RefArray<T, 1 + sizeof...(Ts)>;
145
157template <typename T, std::size_t... Ns>
158RefArray<T, (Ns + ...)> constexpr concatenate(const RefArray<T, Ns>&... ref_arrays)
159{
160 // Fold expression to calculate the total size of the new array using fold expression
161 constexpr std::size_t TotalSize = (Ns + ...);
162 RefArray<T, TotalSize> concatenated;
163
165 // Copies elements from a given RefArray to the concatenated array
166 auto copy_into = [&](const auto& ref_array, std::size_t& offset) {
167 for (std::size_t i = 0; i < ref_array.size(); ++i) {
168 concatenated.get_storage()[offset + i] = &ref_array[i];
169 }
170 offset += ref_array.size();
171 };
172
173 // Fold expression to copy elements from each input RefArray to the concatenated array
174 (..., copy_into(ref_arrays, offset));
175
176 return concatenated;
177}
178} // namespace bb
#define BB_ASSERT_LT(left, right,...)
Definition assert.hpp:115
Nested iterator class for RefArray, based on indexing into the pointer array. Provides semantics simi...
Definition ref_array.hpp:77
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:85
bool operator!=(iterator const &other) const
iterator & operator++()
Definition ref_array.hpp:96
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:22
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:25
RefArray(std::array< T, N > &arr)
Definition ref_array.hpp:31
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:63
RefArray(T &first, Ts &... refs)
Definition ref_array.hpp:38
constexpr std::size_t size() const
RefArray()=default
T & operator[](std::size_t idx) const
Definition ref_array.hpp:42
const std::vector< FF > data
ssize_t offset
Definition engine.cpp:36
Entry point for Barretenberg command-line interface.
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