barretenberg
Loading...
Searching...
No Matches
ref_array.hpp
1#include "barretenberg/common/assert.hpp"
2#include <array>
3#include <cstddef>
4#include <initializer_list>
5#include <iterator>
6#include <stdexcept>
7
8// TODO(https://github.com/AztecProtocol/barretenberg/issues/794) namespace this once convenient
19template <typename T, std::size_t N> class RefArray {
20 public:
21 RefArray(const std::array<T*, N>& ptr_array)
22 {
23 std::size_t i = 0;
24 for (T& elem : ptr_array) {
25 storage[i++] = &elem;
26 }
27 }
28 template <typename... Ts> RefArray(T& ref, Ts&... rest)
29 {
30 storage[0] = &ref;
31 int i = 1;
32 ((storage[i++] = &rest), ...);
33 }
34
35 T& operator[](std::size_t idx) const
36 {
37 ASSERT(idx < N);
38 return *storage[idx];
39 }
40
45 class iterator {
46 public:
53 iterator(RefArray const* array, std::size_t pos)
54 : array(array)
55 , pos(pos)
56 {}
57
58 T& operator*() const { return (*array)[pos]; }
59
60 iterator& operator++()
61 {
62 pos++;
63 return *this;
64 }
65
66 iterator operator++(int)
67 {
68 iterator temp = *this;
69 ++(*this);
70 return temp;
71 }
72
73 bool operator==(iterator const& other) const { return pos == other.pos; }
74 bool operator!=(iterator const& other) const { return pos != other.pos; }
75
76 private:
77 RefArray const* array;
78 std::size_t pos;
79 };
80
81 constexpr std::size_t size() const { return N; }
87 iterator begin() const { return iterator(this, 0); }
93 iterator end() const { return iterator(this, N); }
94
95 private:
96 // We are making a high-level array, for simplicity having a C array as backing makes sense.
97 // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays)
98 T* storage[N];
99};
100
105template <typename T, typename... Ts> RefArray(T&, Ts&...) -> RefArray<T, 1 + sizeof...(Ts)>;
106
118template <typename T, std::size_t... Ns> RefArray<T, (Ns + ...)> concatenate(const RefArray<T, Ns>&... ref_arrays)
119{
120 // Fold expression to calculate the total size of the new array using fold expression
121 constexpr std::size_t TotalSize = (Ns + ...);
122 std::array<T*, TotalSize> concatenated;
123
124 std::size_t offset = 0;
125 // Copies elements from a given RefArray to the concatenated array
126 auto copy_into = [&](const auto& ref_array, std::size_t& offset) {
127 for (std::size_t i = 0; i < ref_array.size(); ++i) {
128 concatenated[offset + i] = &ref_array[i];
129 }
130 offset += ref_array.size();
131 };
132
133 // Fold expression to copy elements from each input RefArray to the concatenated array
134 (..., copy_into(ref_arrays, offset));
135
136 return RefArray<T, TotalSize>{ concatenated };
137}
Nested iterator class for RefArray, based on indexing into the pointer array. Provides semantics simi...
Definition: ref_array.hpp:45
iterator(RefArray const *array, std::size_t pos)
Constructs an iterator for a given RefArray object.
Definition: ref_array.hpp:53
A template class for a reference array. Behaves as if std::array<T&, N> was possible.
Definition: ref_array.hpp:19
iterator begin() const
Returns an iterator to the beginning of the RefArray.
Definition: ref_array.hpp:87
iterator end() const
Returns an iterator to the end of the RefArray.
Definition: ref_array.hpp:93