Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
shared_shifted_virtual_zeroes_array.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: not started, auditors: [], date: YYYY-MM-DD }
3// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
5// =====================
6
7#pragma once
8
12#include <cstddef>
13#include <memory>
14
29template <typename T> struct SharedShiftedVirtualZeroesArray {
30
38 void set(size_t index, const T& value)
39 {
40 BB_ASSERT_GTE(index, start_);
41 BB_ASSERT_LT(index, end_);
42 data()[index - start_] = value;
43 }
44
56 const T& get(size_t index, size_t virtual_padding = 0) const
57 {
58 static const T zero{};
59 if (index >= virtual_size_ + virtual_padding) {
60 info("BAD GET(): index = ",
61 index,
62 ", virtual_size_ = ",
64 ", virtual_padding = ",
66 }
68 if (index >= start_ && index < end_) {
69 return data()[index - start_];
70 }
71 return zero; // Return default element when index is out of the actual filled size
72 }
73
80 T* data() { return backing_memory_ ? backing_memory_->raw_data() : nullptr; }
81 const T* data() const { return backing_memory_ ? backing_memory_->raw_data() : nullptr; }
82 // Our size is end_ - start_. Note that we need to offset end_ when doing a shift to
83 // correctly maintain the size.
84 size_t size() const { return end_ - start_; }
85 // Getter for consistency with size();
86 size_t virtual_size() const { return virtual_size_; }
87
89 {
90 BB_ASSERT_GTE(new_virtual_size, virtual_size_); // shrinking is not allowed
92 }
93
94 T& operator[](size_t index)
95 {
96 BB_ASSERT_GTE(index, start_);
97 BB_ASSERT_LT(index, end_);
98 return data()[index - start_];
99 }
100 // get() is more useful, but for completeness with the non-const operator[]
101 const T& operator[](size_t index) const
102 {
103 BB_ASSERT_GTE(index, start_);
104 BB_ASSERT_LT(index, end_);
105 return data()[index - start_];
106 }
107
108 // MEMBERS:
118 size_t start_ = 0;
119
127 size_t end_ = 0;
128
135 size_t virtual_size_ = 0;
136
144};
#define BB_ASSERT_GTE(left, right,...)
Definition assert.hpp:101
#define BB_ASSERT_LT(left, right,...)
Definition assert.hpp:115
void info(Args... args)
Definition log.hpp:70
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
A shared pointer array template that represents a virtual array filled with zeros up to virtual_size_...
size_t start_
The starting index of the memory-backed range.
void increase_virtual_size(const size_t new_virtual_size)
T * data()
Returns a pointer to the underlying memory array. NOTE: This should be used with care,...
const T & get(size_t index, size_t virtual_padding=0) const
Retrieves the value at the specified index, or 'zero'. Optimizes for e.g. 256-bit fields by storing a...
std::shared_ptr< BackingMemory< T > > backing_memory_
Shared pointer to the underlying memory array.
void set(size_t index, const T &value)
Sets the value at the specified index.
size_t virtual_size_
The total logical size of the array.
size_t end_
The ending index of the memory-backed range.