1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#![allow(clippy::len_without_is_empty)]
use super::{
Bits256,
Index256,
};
#[derive(Debug)]
pub struct BitRefMut<'a> {
bits: &'a mut Bits256,
at: u8,
}
impl<'a> PartialEq for BitRefMut<'a> {
fn eq(&self, other: &Self) -> bool {
self.get() == other.get()
}
}
impl<'a> Eq for BitRefMut<'a> {}
impl<'a> BitRefMut<'a> {
pub(super) fn new(bits: &'a mut Bits256, at: Index256) -> Self {
Self { bits, at }
}
pub fn get(&self) -> bool {
self.bits.get(self.at)
}
pub fn set_to(&mut self, new_value: bool) {
self.bits.set_to(self.at, new_value)
}
pub fn set(&mut self) {
self.bits.set(self.at)
}
pub fn reset(&mut self) {
self.bits.reset(self.at)
}
pub fn flip(&mut self) {
self.bits.flip(self.at)
}
pub fn xor(&mut self, rhs: bool) {
self.bits.xor(self.at, rhs)
}
pub fn and(&mut self, rhs: bool) {
self.bits.and(self.at, rhs)
}
pub fn or(&mut self, rhs: bool) {
self.bits.or(self.at, rhs)
}
}
#[cfg(test)]
mod tests {
use super::BitRefMut;
use crate::collections::bitvec::Bits256;
fn is_populated_bit_set(index: u8) -> bool {
(index % 5) == 0 || (index % 13) == 0
}
fn populated_bits256() -> Bits256 {
let mut bits256 = Bits256::default();
for i in 0..256 {
let i = i as u8;
bits256.set_to(i, is_populated_bit_set(i));
}
bits256
}
#[test]
fn get_set_works() {
let mut bits256 = populated_bits256();
for i in 0..=255 {
let mut bitref = BitRefMut::new(&mut bits256, i);
let expected = is_populated_bit_set(i);
assert_eq!(bitref.get(), expected);
bitref.set_to(i % 2 == 0);
}
for i in 0..=255 {
assert_eq!(bits256.get(i), i % 2 == 0);
}
}
#[test]
fn flip_works() {
let mut bits256 = populated_bits256();
for i in 0..=255 {
let mut bitref = BitRefMut::new(&mut bits256, i);
bitref.flip();
}
for i in 0..=255 {
assert_eq!(bits256.get(i), !is_populated_bit_set(i));
}
}
#[test]
fn set_and_reset_works() {
let mut bits256 = populated_bits256();
for i in 0..=255 {
let mut bitref = BitRefMut::new(&mut bits256, i);
if i % 2 == 0 {
bitref.set();
} else {
bitref.reset();
}
}
for i in 0..=255 {
assert_eq!(bits256.get(i), i % 2 == 0);
}
}
#[test]
fn bitops_works() {
let mut bits256 = populated_bits256();
for i in 0..=255 {
let mut bitref = BitRefMut::new(&mut bits256, i);
let expected = is_populated_bit_set(i);
fn test_xor(bitref: &mut BitRefMut, expected: bool) {
fn test_xor_for(bitref: &mut BitRefMut, expected: bool, input: bool) {
assert_eq!(bitref.get(), expected);
bitref.xor(input);
assert_eq!(bitref.get(), expected ^ input);
bitref.set_to(expected);
}
test_xor_for(bitref, expected, false);
test_xor_for(bitref, expected, true);
}
test_xor(&mut bitref, expected);
fn test_and(bitref: &mut BitRefMut, expected: bool) {
fn test_and_for(bitref: &mut BitRefMut, expected: bool, input: bool) {
assert_eq!(bitref.get(), expected);
bitref.and(input);
assert_eq!(bitref.get(), expected & input);
bitref.set_to(expected);
}
test_and_for(bitref, expected, false);
test_and_for(bitref, expected, true);
}
test_and(&mut bitref, expected);
fn test_or(bitref: &mut BitRefMut, expected: bool) {
fn test_or_for(bitref: &mut BitRefMut, expected: bool, input: bool) {
assert_eq!(bitref.get(), expected);
bitref.or(input);
assert_eq!(bitref.get(), expected | input);
bitref.set_to(expected);
}
test_or_for(bitref, expected, false);
test_or_for(bitref, expected, true);
}
test_or(&mut bitref, expected);
}
}
}