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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#![allow(dead_code)]
use core::mem::MaybeUninit;
use crate::prelude::*;
use crate::v2::consts::headers;
use alloc::vec::Vec;
pub use queue::*;
///
/// A dynamic vector of nibbles.
///
/// Pushing is fast. There is no pop.
/// All bits are pushed together during finish.
///
#[derive(Debug)]
pub struct HalfVec {
words: Vec<HalfWord>,
len: usize,
}
///
/// Bits collected into a single word as one byte or two bytes.
///
#[derive(Debug)]
pub enum HalfWord {
/// The bottom bits of the word are used.
/// 0b0000_1111
Half(u8),
/// The top and bottom bits of the word are used.
/// 0b1111_1111
Byte(u8),
/// All bits of the word are used.
/// 0xffff_ffff
Full(u32),
}
impl HalfWord {
fn len(&self) -> usize {
match self {
HalfWord::Half(_) => 1,
HalfWord::Byte(_) => 2,
HalfWord::Full(_) => 8,
}
}
}
impl HalfVec {
///
/// Creates an empty vector.
///
pub fn new(capacity: usize) -> Self {
Self {
words: Vec::with_capacity(capacity),
len: 0,
}
}
///
/// Returns the number of elements in the queue.
///
pub const fn len(&self) -> usize {
self.len
}
///
/// Returns true if the queue if there is
/// nothing to pop from the queue.
///
pub const fn is_empty(&self) -> bool {
self.len == 0
}
///
/// Clears the queue, removing all values.
/// The queue will be empty after this call completes, but it may not be zero-capacity.
///
pub fn clear(&mut self) {
self.len = 0;
self.words.clear();
}
///
/// Pushes a value into the queue,
/// overwriting the oldest value if the queue is full.
///
#[inline(always)]
pub fn push(&mut self, value: HalfWord) {
self.len += value.len();
self.words.push(value);
}
///
/// Flattens the queue into a single vector of bytes.
///
pub fn finish<'a, I>(out: &mut Vec<u8>, word_lists: I)
where
I: Iterator<Item = &'a HalfVec> + Clone,
{
// We will be writing directly into the buffer since we know the capacity
unsafe {
// 2 nibbles per byte and len is in nibbles
// Reserve enough space for the output
let len = word_lists.clone().map(|w| w.len).sum::<usize>() / 2;
let reserve_len = len + 1;
let avail = out.capacity() - out.len();
if avail < reserve_len {
out.reserve_exact(reserve_len - avail);
}
let bytes = out.spare_capacity_mut();
let mut idx = 0;
// Keep track of whether we are on the upper or lower nibble across word lists
let mut upper = true;
let mut byte = 0u8;
// Iterate over all the words in all the word lists
for word in word_lists.flat_map(|w| w.words.iter()) {
if upper {
match word {
HalfWord::Half(value) => {
// Shift the value into the upper nibble
byte = value << 4;
// We are now on the lower nibble
upper = false;
}
HalfWord::Byte(value) => {
// Use both nibbles from the byte
known_append(bytes, &mut idx, *value);
}
HalfWord::Full(value) => {
// Use both nibbles from the top of the full
known_append(bytes, &mut idx, (value >> 24) as u8);
// Use both nibbles from the top middle of the full
known_append(bytes, &mut idx, (value >> 16) as u8);
// Use both nibbles from the bottom middle of the full
known_append(bytes, &mut idx, (value >> 8) as u8);
// Use both nibbles from the bottom of the full
known_append(bytes, &mut idx, *value as u8);
}
}
} else {
match word {
HalfWord::Half(value) => {
// Fill the lower nibble, the upper nibble is already filled
byte |= value & 0x0F;
known_append(bytes, &mut idx, byte);
// We are now on the upper nibble
upper = true;
}
HalfWord::Byte(value) => {
// Fill the lower nibble with the upper nibble of the value
byte |= value >> 4;
known_append(bytes, &mut idx, byte);
// Use the lower nibble from the value as the upper nibble
byte = value << 4;
// We are still on the lower nibble
}
HalfWord::Full(value) => {
// Fill the lower nibble with the upper nibble of the value
byte |= (value >> 28) as u8;
known_append(bytes, &mut idx, byte);
// Bits 28-20
byte = (value >> 20) as u8;
known_append(bytes, &mut idx, byte);
// Bits 20-12
byte = (value >> 12) as u8;
known_append(bytes, &mut idx, byte);
// Bits 12-4
byte = (value >> 4) as u8;
known_append(bytes, &mut idx, byte);
// Use the lower nibble from the full as the upper nibble
byte = (value << 4) as u8;
// We are still on the lower nibble
}
}
}
}
if !upper {
// We are on the lower nibble, so fill the upper nibble with headers::START_OF_COLUMN
byte |= headers::START_OF_COLUMN;
known_append(bytes, &mut idx, byte);
}
out.set_len(out.len() + idx);
}
}
}
/// Appends a value to a vector without checking the capacity.
#[inline(always)]
unsafe fn known_append(buf: &mut [MaybeUninit<u8>], idx: &mut usize, value: u8) {
// SAFETY: We allocate at least one vector in the constructor and never remove it.
(*buf.as_mut_ptr().add(*idx)).write(value);
*idx += 1;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn can_init() {
let queue = HalfVec::new(128);
assert_eq!(queue.len(), 0);
assert_eq!(queue.is_empty(), true);
}
#[test]
fn can_push() {
let mut queue = HalfVec::new(128);
assert_eq!(queue.len(), 0);
assert_eq!(queue.is_empty(), true);
for i in 0..128 {
queue.push(HalfWord::Half(i as u8));
assert_eq!(queue.len(), i + 1);
assert_eq!(queue.is_empty(), false);
}
for i in 0..128 {
queue.push(HalfWord::Full(i as u32));
assert_eq!(queue.len(), 128 + (i + 1) * 8);
assert_eq!(queue.is_empty(), false);
}
queue.push(HalfWord::Half(15));
assert!(queue.len() % 2 == 1);
// End on the byte
queue.push(HalfWord::Half(0));
// Now every nibble is pushed together
let mut bytes = Vec::new();
HalfVec::finish(&mut bytes, [&queue].into_iter());
assert_eq!(bytes.len(), (128 + 8 * 128 + 1 + 1) / 2);
}
#[test]
fn can_push_with_header() {
let mut queue = HalfVec::new(128);
assert_eq!(queue.len(), 0);
assert_eq!(queue.is_empty(), true);
for i in 0..128 {
queue.push(HalfWord::Half(i as u8));
assert_eq!(queue.len(), i + 1);
assert_eq!(queue.is_empty(), false);
}
for i in 0..128 {
queue.push(HalfWord::Full(i as u32));
assert_eq!(queue.len(), 128 + (i + 1) * 8);
assert_eq!(queue.is_empty(), false);
}
queue.push(HalfWord::Half(15));
assert!(queue.len() % 2 == 1);
// End on the byte
queue.push(HalfWord::Half(0));
// Now every nibble is pushed together
let mut bytes = Vec::new();
bytes.push(0xDE);
bytes.push(0xAD);
bytes.push(0xBE);
bytes.push(0xEF);
HalfVec::finish(&mut bytes, [&queue].into_iter());
assert_eq!(bytes.len(), 4 + ((128 + 8 * 128 + 1 + 1) / 2));
assert_eq!(bytes[0], 0xDE);
assert_eq!(bytes[1], 0xAD);
assert_eq!(bytes[2], 0xBE);
assert_eq!(bytes[3], 0xEF);
}
}
// Delta Tests
#[cfg(test)]
mod tests_emit_delta {
use crate::prelude::halfvec::{HalfVec, HalfWord};
use super::*;
use bitvec::bits;
use rand::Rng;
/// Compares two HalfVec for equality.
impl PartialEq for HalfVec {
fn eq(&self, other: &Self) -> bool {
self.words == other.words && self.len() == other.len()
}
}
/// Compares two HalfWord for equality.
impl PartialEq for HalfWord {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(HalfWord::Half(value1), HalfWord::Half(value2)) => value1 == value2,
(HalfWord::Byte(value1), HalfWord::Byte(value2)) => value1 == value2,
(HalfWord::Full(value1), HalfWord::Full(value2)) => value1 == value2,
_ => false,
}
}
}
// Helped function
fn _emit_delta_i32(values: Vec<i32>) -> HalfVec {
// Create queue
let mut queue: CompressionQueue<10> = CompressionQueue::new();
// Push values into queue
for value in &values {
queue.push(*value);
}
// Initialize bit buffer
let mut bits = HalfVec::new(8);
// Encode
queue.emit_delta_bits(&mut bits);
bits
}
#[test]
fn test_emit_delta_i32_sanity1() {
// Case 7: Encode 10 samples between [-4, 3] in 3 bits
let values = vec![-3, 2, 0, 1, 2, -3, -1, -2, -4, -3];
let encoded_halfvec = _emit_delta_i32(values);
// Initialize expected bit buffer
let mut expected_halfvec = HalfVec::new(8);
// Expecting 10 samples of 3 bits
expected_halfvec.push(HalfWord::Half(headers::THREE_BITS_TEN_SAMPLES));
// Values: [-3, 2, 0, 1, 2, -3, -1, -2, -4, -3]
// Zigzag values: [5, 4, 0, 2, 4, 5, 1, 3, 7, 5]
// Binary of zigzag values: 00 101 100 000 010 100 101 001 011 111 101
let full_value = 0x2C0A_52FD;
expected_halfvec.push(HalfWord::Full(full_value));
// Assert equality
assert_eq!(encoded_halfvec, expected_halfvec);
}
#[test]
fn test_emit_delta_i32_sanity2() {
// Case 6 and 7: Encode 5 samples between [-32, 31] in 6 bits
let values = vec![-32, 31, 16, 1, 2];
let encoded_halfvec = _emit_delta_i32(values);
// Initialize expected bit buffer
let mut expected_halfvec = HalfVec::new(8);
// Expecting 10 samples of 3 bits
expected_halfvec.push(HalfWord::Half(headers::SIX_BITS_FIVE_SAMPLES));
// Values: [-32, 31, 16, 1, 2]
// Zigzag values: [63, 62, 32, 2, 4]
// Binary of zigzag values: 111111 111110 100000 000010 000100
let full_value = 0x3FFA_0084;
expected_halfvec.push(HalfWord::Full(full_value));
// Expected length
assert_eq!(encoded_halfvec, expected_halfvec);
}
#[test]
fn test_emit_delta_i32_sanity3() {
// Case 5, 6 and 7: Encode 4 samples between [-128, 127] in 8 bits
let values = vec![-128, 127, 64, 1];
let encoded_halfvec = _emit_delta_i32(values);
// Initialize expected bit buffer
let mut expected_halfvec = HalfVec::new(8);
// Expecting 10 samples of 3 bits
expected_halfvec.push(HalfWord::Half(headers::EIGHT_BITS_FOUR_SAMPLES));
// Values: [-128, 127, 64, 1]
// Zigzag values: [255, 254, 128, 2]
// Binary of zigzag values: 11111111 11111110 10000000 00000010
let full_value = 0xFFFE_8002;
expected_halfvec.push(HalfWord::Full(full_value));
// Expected length
assert_eq!(encoded_halfvec, expected_halfvec);
}
#[test]
fn test_emit_delta_i32_sanity4() {
// Case 4, 5, 6 and 7: Encode 3 samples between [-512, 511] in 10 bits
let values = vec![-512, 511, 256];
let encoded_halfvec = _emit_delta_i32(values);
// Initialize expected bit buffer
let mut expected_halfvec = HalfVec::new(8);
// Expecting 10 samples of 3 bits
expected_halfvec.push(HalfWord::Half(headers::TEN_BITS_THREE_SAMPLES));
// Values: [-512, 511, 256]
// Zigzag values: [1023, 1022, 512]
// Binary of zigzag values: 1111111111 1111111110 1000000000
let full_value = 0x3FFF_FA00;
expected_halfvec.push(HalfWord::Full(full_value));
// Expected length
assert_eq!(encoded_halfvec, expected_halfvec);
}
#[test]
fn test_emit_delta_i32_sanity5() {
// Case 3, 4, 5, 6 and 7: Encode 2 samples between [-32768, 32767] in 16 bits
let values = vec![-32768, 32767];
let encoded_halfvec = _emit_delta_i32(values);
// Initialize expected bit buffer
let mut expected_halfvec = HalfVec::new(8);
// Expecting 10 samples of 3 bits
expected_halfvec.push(HalfWord::Half(headers::SIXTEEN_BITS_TWO_SAMPLES));
// Values: [-32768, 32767]
// Zigzag values: [65535, 65534]
// Binary of zigzag values: 1111111111111111 1111111111111110
let full_value = 0xFFFF_FFFE;
expected_halfvec.push(HalfWord::Full(full_value));
// Expected length
assert_eq!(encoded_halfvec, expected_halfvec);
}
#[test]
fn test_emit_delta_i32_sanity6() {
// Case 2, 3, 4, 5, 6 and 7: Encode 2 samples between [-32768, 32767] in 16 bits
let values = vec![i32::MIN / 4, 1, 1, 1];
let encoded_halfvec = _emit_delta_i32(values);
// Initialize expected bit buffer
let mut expected_halfvec = HalfVec::new(8);
// Expecting 10 samples of 3 bits
expected_halfvec.push(HalfWord::Half(headers::THIRTY_TWO_BITS_ONE_SAMPLE));
// Values: vec![i32::MIN / 4]
// Zigzag values: [-1 * i32::MIN / 2]
// Binary of zigzag values:
let full_value = 0x3FFFFFFF;
expected_halfvec.push(HalfWord::Full(full_value));
// Expected length
assert_eq!(encoded_halfvec, expected_halfvec);
}
}