GF2++
Loading...
Searching...
No Matches
BitRef.h
Go to the documentation of this file.
1#pragma once
2// SPDX-FileCopyrightText: 2025 Nessan Fitzmaurice <nzznfitz+gh@icloud.com>
3// SPDX-License-Identifier: MIT
4
8
9#include <gf2/store_word.h>
10#include <format>
11
12namespace gf2 {
13
14// Forward declaration of the `BitStore` class which generates `BitRef` instances from index operations.
15template<typename Store>
16class BitStore;
17
37template<typename Store>
38class BitRef {
39private:
40 // The bit-store we are referencing.
41 BitStore<Store>* m_store;
42
43 // The index of the bit of interest within that bit-store
44 usize m_index;
45
46public:
48 BitRef(BitStore<Store>* store, usize index) : m_store(store), m_index(index) {}
49
50 // The default compiler generator implementations for most "rule of 5" methods will be fine ...
51 constexpr BitRef() = default;
52 constexpr BitRef(const BitRef&) = default;
53 constexpr BitRef(BitRef&&) noexcept = default;
54 ~BitRef() = default;
55
57 constexpr operator bool() const { return m_store->get(m_index); }
58
60 constexpr BitRef& operator=(bool rhs) {
61 m_store->set(m_index, rhs);
62 return *this;
63 }
64
66 constexpr BitRef& operator=(const BitRef& rhs) {
67 m_store->set(m_index, static_cast<bool>(rhs));
68 return *this;
69 }
70
72 constexpr BitRef& flip() {
73 m_store->flip(m_index);
74 return *this;
75 }
76
78 constexpr BitRef& operator&=(bool rhs) {
79 if (!rhs) m_store->set(m_index, false);
80 return *this;
81 }
82
84 constexpr BitRef& operator&=(const BitRef& rhs) {
85 if (!static_cast<bool>(rhs)) m_store->set(m_index, false);
86 return *this;
87 }
88
90 constexpr BitRef& operator|=(bool rhs) {
91 if (rhs) m_store->set(m_index);
92 return *this;
93 }
94
96 constexpr BitRef& operator|=(const BitRef& rhs) {
97 if (static_cast<bool>(rhs)) m_store->set(m_index);
98 return *this;
99 }
100
102 constexpr BitRef& operator^=(bool rhs) {
103 if (rhs) m_store->flip(m_index);
104 return *this;
105 }
106
108 constexpr BitRef& operator^=(const BitRef& rhs) {
109 if (static_cast<bool>(rhs)) m_store->flip(m_index);
110 return *this;
111 }
112};
113
114} // namespace gf2
115
119template<typename Store>
120struct std::formatter<gf2::BitRef<Store>> : std::formatter<bool> {
121 template<typename FormatContext>
122 auto format(const gf2::BitRef<Store>& bit, FormatContext& ctx) const {
123 return std::formatter<bool>::format(static_cast<bool>(bit), ctx);
124 }
125};
A BitRef is a proxy class to reference a single bit in a bit-store.
Definition BitRef.h:38
constexpr BitRef & operator&=(const BitRef &rhs)
Performs an AND operation on the bit at index in the referenced bit-store with rhs.
Definition BitRef.h:84
BitRef(BitStore< Store > *store, usize index)
Constructs a reference to the bit at index in the given BitStore.
Definition BitRef.h:48
constexpr BitRef & operator|=(bool rhs)
Performs an OR operation on the bit at index in the referenced bit-store with rhs.
Definition BitRef.h:90
constexpr BitRef & operator^=(const BitRef &rhs)
Performs an XOR operation on the bit at index in the referenced bit-store with rhs.
Definition BitRef.h:108
constexpr BitRef & operator=(const BitRef &rhs)
Sets the bit at index in the referenced bit-store to the value of the given BitRef.
Definition BitRef.h:66
constexpr BitRef & operator^=(bool rhs)
Performs an XOR operation on the bit at index in the referenced bit-store with rhs.
Definition BitRef.h:102
constexpr BitRef & flip()
Flips the bit at index in the referenced bit-store.
Definition BitRef.h:72
constexpr BitRef & operator|=(const BitRef &rhs)
Performs an OR operation on the bit at index in the referenced bit-store with rhs.
Definition BitRef.h:96
constexpr BitRef & operator=(bool rhs)
Sets the bit at index in the referenced bit-store to the given value.
Definition BitRef.h:60
constexpr BitRef & operator&=(bool rhs)
Performs an AND operation on the bit at index in the referenced bit-store with rhs.
Definition BitRef.h:78
The base class for the vector-like types in the gf2 library: BitSet, BitVec, and BitSpan The templat...
Definition BitStore.h:35
The namespace for the gf2 library.
Definition assert.h:119
std::size_t usize
Word type alias for the platform's "native"-sized unsigned integer.
Definition unsigned_word.h:41