Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
bitvector.hpp
Go to the documentation of this file.
1#pragma once
2#include <cassert>
3#include <cstdint>
4#include <cstring> // For memset (optional)
5#include <vector>
6
9
15class BitVector {
16 public:
17 BitVector(size_t num_bits)
18 : num_bits_(num_bits)
19 , data_((num_bits + 63) / 64, 0)
20 {}
21
22 BB_INLINE void set(size_t index, bool value) noexcept
23 {
24 BB_ASSERT_LT(index, num_bits_);
25 const size_t word = index >> 6;
26 const size_t bit = index & 63;
27
28 // from bit twiddling hacks
29 // http://graphics.stanford.edu/~seander/bithacks.html#ConditionalSetOrClearBitsWithoutBranching
30 const uint64_t mask = static_cast<uint64_t>(1) << bit;
31 const uint64_t f = -static_cast<uint64_t>(value);
32 const uint64_t limb = data_[word];
33 data_[word] = (limb & ~mask) | (f & mask);
34 }
35
36 BB_INLINE bool get(size_t index) const noexcept
37 {
38 BB_ASSERT_LT(index, num_bits_);
39 const uint64_t word = index >> 6;
40 const uint64_t bit = index & 63;
41 return ((data_[static_cast<size_t>(word)] >> bit) & 1) == 1;
42 }
43
44 void clear()
45 {
46 // std::fill(data_.begin(), data_.end(), 0);
47 // or use raw memset for faster clearing
48 std::memset(data_.data(), 0, data_.size() * sizeof(uint64_t));
49 }
50
51 size_t size() const { return num_bits_; }
52
53 // Optional: access raw pointer for performance
54 uint64_t* raw_data() { return data_.data(); }
55 const uint64_t* raw_data() const { return data_.data(); }
56
57 private:
58 size_t num_bits_;
59 std::vector<uint64_t> data_;
60};
#define BB_ASSERT_LT(left, right,...)
Definition assert.hpp:115
Custom class to handle packed vectors of bits.
Definition bitvector.hpp:15
BB_INLINE bool get(size_t index) const noexcept
Definition bitvector.hpp:36
const uint64_t * raw_data() const
Definition bitvector.hpp:55
BB_INLINE void set(size_t index, bool value) noexcept
Definition bitvector.hpp:22
BitVector(size_t num_bits)
Definition bitvector.hpp:17
size_t num_bits_
Definition bitvector.hpp:58
void clear()
Definition bitvector.hpp:44
size_t size() const
Definition bitvector.hpp:51
uint64_t * raw_data()
Definition bitvector.hpp:54
std::vector< uint64_t > data_
Definition bitvector.hpp:59
#define BB_INLINE
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13