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
// use std::{ops::Index, fmt::{Debug, Write}};
// use super::{GrowingBitmap, Bitmap, ResizableBitmap};
// #[derive(Copy, Clone, PartialEq, Eq, Hash, Default)]
// pub struct TinyBitmap56 {
// data: [u8; 7],
// len: u8,
// }
// impl TryFrom<&GrowingBitmap> for TinyBitmap56 {
// type Error = ();
// fn try_from(value: &GrowingBitmap) -> Result<Self, Self::Error> {
// if value.is_empty() {
// Ok(Self::default())
// } else if value.len() <= 56 {
// Ok(TinyBitmap56 {
// data: value.data()[0].to_le_bytes()[..7].try_into().unwrap(),
// len: value.len() as u8,
// })
// } else {
// Err(())
// }
// }
// }
// impl TinyBitmap56 {
// #[inline]
// fn index(x: usize) -> (usize, usize) {
// (x / 8, x % 8)
// }
// fn zeroize_past_end(&mut self) {
// let last_index = self.len as usize / 8;
// let rest = if self.len % 8 != 0 {
// self.data[last_index] &= (1 << (self.len % 8)) - 1;
// &mut self.data[last_index + 1..]
// } else {
// &mut self.data[last_index..]
// };
// for b in rest.iter_mut() {
// *b = 0;
// }
// }
// }
// impl Bitmap for TinyBitmap56 {
// fn get(&self, index: usize) -> bool {
// let (index, shift) = Self::index(index);
// (self.data.get(index).unwrap_or(&0) >> shift) & 1 != 0
// }
// fn set(&mut self, index: usize) -> bool {
// if index >= 56 {
// panic!("Index {index} is out of bounds [0, 56) for TinyBitmap56");
// }
// if index as u8 > self.len {
// self.len = index as u8 + 1;
// }
// let (index, offset) = Self::index(index);
// let mask = 1 << offset;
// let changed = self.data[index] & mask == 0;
// self.data[index] |= mask;
// changed
// }
// fn clear(&mut self, index: usize) -> bool {
// if index >= 56 {
// panic!("Index {index} is out of bounds [0, 56) for TinyBitmap56");
// }
// if index as u8 > self.len {
// self.len = index as u8 + 1;
// }
// let (index, offset) = Self::index(index);
// let mask = 1 << offset;
// let changed = self.data[index] & mask != 0;
// self.data[index] &= !mask;
// changed
// }
// fn is_all_zeros(&self) -> bool {
// self.data == [0; 7]
// }
// fn is_all_ones(&self) -> bool {
// let (full_u8s, ok) = if self.len % 8 == 0 {
// (&self.data[..self.len as usize / 8], true)
// } else if !self.data.is_empty() {
// let v = self.data[self.data.len() - 1];
// let last_ok = v == (1 << (self.len % 8)) - 1;
// (&self.data[..self.data.len() - 1], last_ok)
// } else {
// (&[][..], true)
// };
// ok && full_u8s.iter().all(|&n| n == u8::MAX)
// }
// fn and_with(&mut self, other: &GrowingBitmap) -> bool {
// if other.len() < self.len as usize {
// self.len = other.len() as u8;
// }
// let mut changed = false;
// for (lhs, rhs) in self.data.iter_mut().zip(other.data().iter().flat_map(|d| d.to_le_bytes())) {
// let original = *lhs;
// *lhs &= rhs;
// changed |= original != *lhs;
// }
// changed
// }
// }
// impl ResizableBitmap for TinyBitmap56 {
// fn resize(&mut self, new_size: usize) {
// if new_size > 56 {
// panic!("Cannot resize a TinyBitmap56 to {new_size} bits")
// }
// self.len = new_size as u8;
// self.zeroize_past_end();
// }
// }
// impl Index<usize> for TinyBitmap56 {
// type Output = bool;
// fn index(&self, index: usize) -> &Self::Output {
// if self.get(index) {
// &true
// } else {
// &false
// }
// }
// }
// impl Debug for TinyBitmap56 {
// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// for index in 0..self.len {
// f.write_char(if self.get(index as usize) {
// '1'
// } else {
// '0'
// })?;
// }
// Ok(())
// }
// }