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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
use super::encode::Bits;
///
/// A statically sized ring-buffer queue used
/// while compressing a column.
///
/// The absolute max size of this buffer is 16 elements.
///
#[derive(Debug)]
pub struct CompressionQueue<const N: usize> {
zigzag: [usize; 16],
bitcount: [usize; 16],
front: usize,
len: usize,
}
impl<const N: usize> CompressionQueue<N> {
///
/// Creates an empty queue.
///
pub const fn new() -> Self {
assert!(N <= 16);
CompressionQueue {
zigzag: [0; 16],
bitcount: [0; 16],
front: 0,
len: 0,
}
}
///
/// Returns the number of elements in the queue.
///
pub const fn len(&self) -> usize {
self.len
}
///
/// Returns true if the queue if the queue would
/// have to overwrite an element to push a new value.
///
pub fn is_full(&self) -> bool {
self.len >= N
}
///
/// Returns true if the queue if there is
/// nothing to pop from the queue.
///
pub fn is_empty(&self) -> bool {
self.len == 0
}
///
/// Pushes a value into the queue,
/// overwriting the oldest value if the queue is full.
///
pub fn push<T: Bits + Sized>(&mut self, value: T) {
let index = (self.front + self.len) % 16;
unsafe { self.write(index, value) };
if self.len < 16 {
self.len += 1;
} else {
self.front = (self.front + 1) % 16;
}
}
///
/// Pops the oldest value from the queue,
/// returning None if the queue is empty.
///
pub fn pop(&mut self) -> Option<usize> {
if self.is_empty() {
return None;
}
let value = unsafe { self.value_at(self.front) };
self.front = (self.front + 1) % 16;
self.len -= 1;
Some(value)
}
///
/// Pop N values from the queue at once,
/// values may not be meaningful if the queue is
/// not of length N.
///
#[inline(always)]
pub fn pop_n<const M: usize>(&mut self) -> [usize; M] {
let mut values: [usize; M] = [0; M];
for i in 0..M {
let index = (self.front + i) % 16;
unsafe {
*values.get_unchecked_mut(i) = self.value_at(index);
}
}
self.front = (self.front + M) % 16;
self.len -= M;
values
}
///
/// Peak N values from the queue at once,
/// values may not be meaningful if the queue is
/// not of length N.
///
#[inline(always)]
pub fn peak_bitcounts<const M: usize>(&mut self) -> [usize; M] {
let mut values: [usize; M] = [0; M];
for i in 0..M {
let index = (self.front + i) % 16;
unsafe {
*values.get_unchecked_mut(i) = self.count_at(index);
}
}
values
}
///
/// Internal use accessor to an initialized value.
///
/// # Safety
/// This function is unsafe because it assumes that
/// the index is inbounds and initialized.
///
unsafe fn value_at(&self, index: usize) -> usize {
*self.zigzag.get_unchecked(index)
}
///
/// Internal use accessor to an initialized value.
///
/// # Safety
/// This function is unsafe because it assumes that
/// the index is inbounds and initialized.
///
unsafe fn count_at(&self, index: usize) -> usize {
*self.bitcount.get_unchecked(index)
}
///
/// Internal use mutable accessor to an initialized value.
///
/// # Safety
/// This function is unsafe because it assumes that
/// the index is inbounds. It does *not* drop the value
/// at the index if it was initialized; therefore, T must
/// be trivially dropable.
///
unsafe fn write<T: Bits + Sized>(&mut self, index: usize, t: T) {
let (zbits, zcount) = t.zigzag_bits();
*self.zigzag.get_unchecked_mut(index) = zbits;
*self.bitcount.get_unchecked_mut(index) = zcount;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn can_init() {
let queue: CompressionQueue<10> = CompressionQueue::new();
assert_eq!(queue.len(), 0);
assert_eq!(queue.is_full(), false);
assert_eq!(queue.is_empty(), true);
}
#[test]
fn is_empty_or_full() {
let mut queue: CompressionQueue<4> = CompressionQueue::new();
assert_eq!(queue.len(), 0);
assert_eq!(queue.is_empty(), true);
assert_eq!(queue.is_full(), false);
// push 4 values, queue should be full
for i in 0..4 {
queue.push(i as i8);
assert_eq!(queue.len(), i + 1);
assert_eq!(queue.is_empty(), false);
assert_eq!(queue.is_full(), i == 3);
}
// pop 4 values, queue should be empty
for i in 0..4 {
assert_eq!(queue.pop(), Some((i as i8).zigzag()));
assert_eq!(queue.len(), 3 - i);
assert_eq!(queue.is_empty(), (i == 3));
assert_eq!(queue.is_full(), false);
}
}
#[test]
fn can_overwrite() {
let mut queue: CompressionQueue<4> = CompressionQueue::new();
// push 4 values, queue should be full
for i in 0..4 {
queue.push(i as i8);
assert_eq!(queue.len(), i + 1);
assert_eq!(queue.is_empty(), false);
assert_eq!(queue.is_full(), i == 3);
}
// keep pushing, queue should still be full
for i in 4..8 {
queue.push(i as i8);
assert_eq!(queue.len(), i + 1);
assert_eq!(queue.is_empty(), false);
assert_eq!(queue.is_full(), true);
}
// keep pushing, queue should still be full and start overwriting
for i in 8..20 {
queue.push(i as i8);
assert_eq!(queue.len(), (i + 1).min(16));
assert_eq!(queue.is_empty(), false);
assert_eq!(queue.is_full(), true);
}
// pop the values, they should be 16..20
for j in 0..4 {
assert_eq!(queue.pop(), Some(((j + 4) as i8).zigzag()));
assert_eq!(queue.len(), 15 - j);
assert_eq!(queue.is_empty(), false);
assert_eq!(queue.is_full(), true);
}
// pop another 8 values, then the queue will start to empty
queue.pop_n::<8>();
assert_eq!(queue.len(), 4);
assert_eq!(queue.is_empty(), false);
assert_eq!(queue.is_full(), true);
// pop the remaining 4 values, then the queue will be empty
for j in 0..4 {
assert_eq!(queue.pop(), Some(((j + 16) as i8).zigzag()));
assert_eq!(queue.len(), 3 - j);
assert_eq!(queue.is_empty(), (j == 3));
assert_eq!(queue.is_full(), false);
}
}
#[test]
fn fuzz() {
use alloc::collections::VecDeque;
use rand::Rng;
let mut rng = rand::thread_rng();
let mut std_queue: VecDeque<usize> = VecDeque::new();
let mut queue: CompressionQueue<10> = CompressionQueue::new();
for _ in 0..10000 {
let value = rng.gen::<i32>();
let zig_zag_value = value.zigzag();
if rng.gen::<bool>() {
std_queue.push_back(zig_zag_value as usize);
if queue.len() == 16 {
assert_eq!(std_queue.pop_front(), queue.pop());
}
queue.push(value);
} else {
assert_eq!(std_queue.pop_front(), queue.pop());
}
assert_eq!(std_queue.len(), queue.len());
for _ in 0..queue.len() {
assert_eq!(std_queue.pop_front(), queue.pop());
}
}
}
}