Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
ref_span.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <iterator>
5
6#include "ref_array.hpp"
7#include "ref_vector.hpp"
8
9namespace bb {
10
11template <typename T> class RefSpan {
12 public:
13 // Default constructor
15 : storage(nullptr)
16 , array_size(0)
17 {}
18
19 template <std::size_t Size>
20 RefSpan(const RefArray<T, Size>& ref_array)
21 : storage(ref_array.get_storage())
22 , array_size(Size)
23 {}
24 RefSpan(const RefVector<T>& ref_vector)
25 : storage(&ref_vector.get_storage()[0])
26 , array_size(ref_vector.size())
27 {}
28
29 // Constructor from an array of pointers and size
30 RefSpan(T* const* ptr_array, std::size_t size)
31 : storage(ptr_array)
33 {}
34
35 // Copy constructor
36 RefSpan(const RefSpan& other) = default;
37
38 // Move constructor
39 RefSpan(RefSpan&& other) noexcept = default;
40
41 // Destructor
42 ~RefSpan() = default;
43
44 // Copy assignment operator
45 RefSpan& operator=(const RefSpan& other) = default;
46
47 // Move assignment operator
48 RefSpan& operator=(RefSpan&& other) noexcept = default;
49
50 // Access element at index
51 T& operator[](std::size_t idx) const
52 {
53 // Assuming the caller ensures idx is within bounds.
54 return *storage[idx];
55 }
56
57 // Get size of the RefSpan
58 constexpr std::size_t size() const { return array_size; }
59
61 {
62 // NOTE: like std::span, assumes the caller ensures offset and count are within bounds.
63 return RefSpan(storage + offset, count);
64 }
65
67 {
68 // NOTE: like std::span, assumes the caller ensures offset and count are within bounds.
70 }
71
72 // Iterator implementation
73 class iterator {
74 public:
76 : array(array)
77 , pos(pos)
78 {}
79
80 T& operator*() const { return *(array[pos]); }
81
83 {
84 ++pos;
85 return *this;
86 }
87
89 {
90 iterator temp = *this;
91 ++(*this);
92 return temp;
93 }
94
95 bool operator==(const iterator& other) const { return pos == other.pos; }
96 bool operator!=(const iterator& other) const { return pos != other.pos; }
97
98 private:
99 T* const* array;
101 };
102
103 // Begin and end for iterator support
104 iterator begin() const { return iterator(storage, 0); }
105 iterator end() const { return iterator(storage, array_size); }
106
107 private:
108 T* const* storage;
110};
111
112} // namespace bb
A template class for a reference array. Behaves as if std::array<T&, N> was possible.
Definition ref_array.hpp:22
iterator & operator++()
Definition ref_span.hpp:82
bool operator!=(const iterator &other) const
Definition ref_span.hpp:96
iterator(T *const *array, std::size_t pos)
Definition ref_span.hpp:75
iterator operator++(int)
Definition ref_span.hpp:88
bool operator==(const iterator &other) const
Definition ref_span.hpp:95
T & operator*() const
Definition ref_span.hpp:80
~RefSpan()=default
std::size_t array_size
Definition ref_span.hpp:109
T & operator[](std::size_t idx) const
Definition ref_span.hpp:51
RefSpan subspan(std::size_t offset, std::size_t count)
Definition ref_span.hpp:60
iterator end() const
Definition ref_span.hpp:105
iterator begin() const
Definition ref_span.hpp:104
T *const * storage
Definition ref_span.hpp:108
RefSpan(const RefVector< T > &ref_vector)
Definition ref_span.hpp:24
RefSpan & operator=(const RefSpan &other)=default
RefSpan(const RefArray< T, Size > &ref_array)
Definition ref_span.hpp:20
RefSpan & operator=(RefSpan &&other) noexcept=default
constexpr std::size_t size() const
Definition ref_span.hpp:58
RefSpan(T *const *ptr_array, std::size_t size)
Definition ref_span.hpp:30
RefSpan(RefSpan &&other) noexcept=default
RefSpan(const RefSpan &other)=default
RefSpan subspan(std::size_t offset)
Definition ref_span.hpp:66
A template class for a reference vector. Behaves as if std::vector<T&> was possible.
ssize_t offset
Definition engine.cpp:36
Entry point for Barretenberg command-line interface.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13