bitpack_vec/
lib.rs

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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
#![doc = include_str!("../README.md")]

use std::fmt::Debug;

use deepsize::DeepSizeOf;
use serde::{Deserialize, Serialize};

/// A densely-packed vector of integers with fixed bit-length
#[derive(DeepSizeOf, Serialize, Deserialize)]
pub struct BitpackVec {
    data: Vec<u64>,
    len: usize,
    width: u8,
}

impl BitpackVec {
    /// Constructs a new `BitpackVec` with the given bit-width.
    ///
    /// ```rust
    /// use bitpack_vec::BitpackVec;
    /// let bv = BitpackVec::new(5);
    /// assert_eq!(bv.width(), 5);
    /// ```
    pub fn new(width: usize) -> BitpackVec {
        assert!(width > 0, "bitpack width must be greater than 0");
        assert!(
            width <= 64,
            "bitpack width must be less than or equal to 64"
        );

        BitpackVec {
            data: Vec::with_capacity(1),
            len: 0,
            width: width as u8,
        }
    }

    /// Construct a `BitpackVec` from a vector of `u64`s, interpreting the
    /// vector with the given bitwidth.
    ///
    /// ```rust
    /// use bitpack_vec::BitpackVec;
    ///
    /// let v = vec![6];
    /// let bv = BitpackVec::from_raw_vec(v, 5, 12);
    /// assert_eq!(bv.at(0), 6);
    /// assert_eq!(bv.at(1), 0);
    /// ```
    pub fn from_raw_vec(data: Vec<u64>, width: usize, len: usize) -> BitpackVec {
        assert!(
            data.len() * 64 > width * len,
            "data is not long enough to be valid"
        );
        BitpackVec {
            data,
            width: width as u8,
            len,
        }
    }

    /// Returns the internal vector representing the bitpacked data
    ///
    /// ```rust
    /// use bitpack_vec::BitpackVec;
    ///
    /// let bv = BitpackVec::from_slice(&[10, 15]);
    /// let v = bv.into_raw_vec();
    /// assert_eq!(v.len(), 1);
    /// assert_eq!(v[0], (15 << 4) | 10);
    /// ```
    pub fn into_raw_vec(self) -> Vec<u64> {
        self.data
    }

    /// Reference to the internal vector, see [into_raw_vec](Self::into_raw_vec)
    pub fn as_raw(&self) -> &[u64] {
        &self.data
    }

    /// Construct a `BitpackVec` from a slice of `u64`s. The smallest
    /// correct bitwidth will be computed.
    ///
    /// ```rust
    /// use bitpack_vec::BitpackVec;
    /// let bv = BitpackVec::from_slice(&[5, 12, 13]);
    /// assert_eq!(bv.width(), 4);
    /// assert_eq!(bv.at(2), 13);
    /// ```
    pub fn from_slice(x: &[u64]) -> BitpackVec {
        assert!(
            !x.is_empty(),
            "Cannot make bitpacked vector from empty slice"
        );

        // scan the data to figure out the fewest bits needed
        let max = x.iter().max().unwrap();
        let bits = 64 - max.leading_zeros();

        let mut bv = BitpackVec::new(bits as usize);

        for i in x {
            bv.push(*i);
        }

        bv
    }

    /// Returns the number of items in the bitpacked vector.
    /// ```rust
    /// use bitpack_vec::BitpackVec;
    /// let bv = BitpackVec::from_slice(&[5, 12, 13]);
    /// assert_eq!(bv.len(), 3);
    /// ```
    pub fn len(&self) -> usize {
        self.len
    }

    /// Returns the packing width of the vector (the size in bits of each
    /// element).
    ///
    /// ```rust
    /// use bitpack_vec::BitpackVec;
    /// let bv = BitpackVec::from_slice(&[5, 12, 13]);
    /// assert_eq!(bv.width(), 4);
    /// ```
    pub fn width(&self) -> usize {
        self.width as usize
    }

    /// Returns the number of items that can be inserted into the
    /// `BitpackVec` before the vector will grow.
    pub fn capacity(&self) -> usize {
        (self.data.capacity() * 64) / self.width()
    }

    /// Determines if `x` can fit inside this vector.
    ///
    /// ```rust
    /// use bitpack_vec::BitpackVec;
    ///
    /// let bv = BitpackVec::new(5);
    ///
    /// assert!(bv.fits(31));
    /// assert!(!bv.fits(32));
    /// ```
    pub fn fits(&self, x: u64) -> bool {
        x < 2_u64.pow(self.width as u32)
    }

    /// Appends an item to the back of the `BitpackVec`. Panics if the item
    /// is too large for the vector's bitwidth.
    ///
    /// ```rust
    /// use bitpack_vec::BitpackVec;
    /// let mut bv = BitpackVec::new(5);
    ///
    /// bv.push(4);
    /// bv.push(22);
    ///
    /// assert_eq!(bv.at(0), 4);
    /// assert_eq!(bv.at(1), 22);
    /// ```
    ///
    /// Adding items that are too large will cause a panic:
    /// ```should_panic
    /// use bitpack_vec::BitpackVec;
    /// let mut bv = BitpackVec::new(5);
    ///
    /// bv.push(90);  // panics
    /// ```
    pub fn push(&mut self, x: u64) {
        assert!(
            self.fits(x),
            "value {} too large to bitpack to width {}",
            x,
            self.width
        );

        // calculate position info
        let start_bit = self.len * self.width();
        let stop_bit = start_bit + self.width() - 1;

        let start_u64 = start_bit / 64;
        let stop_u64 = stop_bit / 64;

        // ensure we have enough u64s to store the number
        while self.data.len() <= stop_u64 {
            self.data.push(0);
        }

        let local_start_bit = start_bit % 64;
        if start_u64 == stop_u64 {
            // pack the whole value into the same u64
            self.data[start_u64] |= x << local_start_bit;
        } else {
            // we have to pack part of the number into one u64, and the
            // rest into the next
            let bits_in_first_cell = 64 - local_start_bit;
            self.data[start_u64] |= x << local_start_bit;
            self.data[stop_u64] |= x >> bits_in_first_cell;
        }

        self.len += 1;
    }

    /// Sets an existing element to a particular value. Panics if `idx` is
    /// out of range or if `x` does not fit within the bitwidth.
    ///
    /// ```rust
    /// use bitpack_vec::BitpackVec;
    /// let mut bv = BitpackVec::new(5);
    ///
    /// bv.push(5);
    ///
    /// assert_eq!(bv.at(0), 5);
    /// bv.set(0, 9);
    /// assert_eq!(bv.at(0), 9);
    /// ```
    pub fn set(&mut self, idx: usize, x: u64) {
        assert!(
            idx < self.len,
            "index {} out of bounds for len {}",
            idx,
            self.len
        );
        assert!(
            self.fits(x),
            "value {} too large to bitpack to width {}",
            x,
            self.width
        );

        let start_bit = idx * self.width();
        let stop_bit = start_bit + self.width() - 1;

        let start_u64 = start_bit / 64;
        let stop_u64 = stop_bit / 64;

        if start_u64 == stop_u64 {
            // all in the same u64
            let local_start_bit = start_bit % 64;
            let local_stop_bit = local_start_bit + self.width();
            let v = self.data[start_u64];

            // zero out all the data at index `idx`
            let mut mask = ((!0_u64) >> local_start_bit) << local_start_bit;
            mask = (mask << (64 - local_stop_bit)) >> (64 - local_stop_bit);
            mask = !mask;

            let v = v & mask;

            // now or the value into it
            let x = x << local_start_bit;
            let v = v | x;

            self.data[start_u64] = v;
        } else {
            // bits are split between two cells
            let local_start_bit = start_bit % 64;

            // clear the bits currently in the cell
            let bit_count_in_first = 64 - local_start_bit;
            let v = self.data[start_u64];
            let v = (v << bit_count_in_first) >> bit_count_in_first;
            let prefix_bits = x << (64 - bit_count_in_first);
            let v = v | prefix_bits;
            self.data[start_u64] = v;

            // clear the bits at the front of the next cell
            let remaining_bit_count = self.width() - bit_count_in_first;
            let v = self.data[stop_u64];
            let v = (v >> remaining_bit_count) << remaining_bit_count;
            let x = x >> bit_count_in_first;
            let v = v | x;

            self.data[stop_u64] = v;
        }
    }

    /// Returns the value at the specified index. Panics if `idx` is out of
    /// range.
    ///
    /// ```rust
    /// use bitpack_vec::BitpackVec;
    /// let mut bv = BitpackVec::new(5);
    ///
    /// bv.push(5);
    ///
    /// assert_eq!(bv.at(0), 5);
    /// ```
    pub fn at(&self, idx: usize) -> u64 {
        assert!(
            idx < self.len,
            "index {} out of bounds for len {}",
            idx,
            self.len
        );
        let start_bit = idx * self.width();
        let stop_bit = start_bit + self.width() - 1;

        let start_u64 = start_bit / 64;
        let stop_u64 = stop_bit / 64;

        if start_u64 == stop_u64 {
            // all in the same u64
            let local_start_bit = start_bit % 64;
            let local_stop_bit = local_start_bit + self.width();
            let v = self.data[start_u64];

            let v = v << (64 - local_stop_bit);
            v >> (64 - self.width)
        } else {
            // bits are split between two cells
            let mut x = 0;
            let local_start_bit = start_bit % 64;
            x |= self.data[start_u64] >> local_start_bit;
            x |= self.data[stop_u64] << (64 - local_start_bit);
            x = (x << (64 - self.width)) >> (64 - self.width);
            x
        }
    }

    /// Determines if the vector's length is 0 (empty)
    ///
    /// ```rust
    /// use bitpack_vec::BitpackVec;
    /// let mut bv = BitpackVec::new(5);
    ///
    /// assert!(bv.is_empty());
    /// bv.push(5);
    /// assert!(!bv.is_empty());
    /// ```
    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    /// Removes and returns the last element in the vector. Returns `None`
    /// is the vector is empty.
    /// ```rust
    /// use bitpack_vec::BitpackVec;
    /// let mut bv = BitpackVec::new(5);
    ///
    /// bv.push(5);
    /// bv.push(6);
    ///
    /// assert_eq!(bv.pop(), Some(6));
    /// assert_eq!(bv.pop(), Some(5));
    /// assert_eq!(bv.pop(), None);
    /// ```
    pub fn pop(&mut self) -> Option<u64> {
        if self.is_empty() {
            return None;
        }

        let idx = self.len() - 1;
        let last = self.at(idx);

        // zero out the value so our xor works later
        let start_bit = idx * self.width();
        let stop_bit = start_bit + self.width() - 1;

        let start_u64 = start_bit / 64;
        let stop_u64 = stop_bit / 64;

        let local_start_bit = start_bit % 64;

        if local_start_bit == 0 {
            // this is the only value in the cell
            self.data.pop();
        } else {
            // clear the data in the cell containing the start of the number
            self.data[start_u64] =
                (self.data[start_u64] << (64 - local_start_bit)) >> (64 - local_start_bit);

            if start_u64 != stop_u64 {
                // the last cell is now clear
                self.data.pop();
            }
        }

        self.len -= 1;

        Some(last)
    }

    /// Truncates the vector to the given length.
    /// ```rust
    /// use bitpack_vec::BitpackVec;
    /// let mut bv = BitpackVec::new(5);
    ///
    /// bv.push(5);
    /// bv.push(6);
    /// bv.push(7);
    ///
    /// assert_eq!(bv.len(), 3);
    /// bv.truncate(1);
    /// assert_eq!(bv.len(), 1);
    /// assert_eq!(bv.at(0), 5);
    /// ```
    pub fn truncate(&mut self, len: usize) {
        // TODO this should compute which entire cells can be dropped, and
        // do that first
        while self.len() > len {
            self.pop();
        }
    }

    /// Split the vector into two parts, so that `self` will contain all
    /// elements from 0 to `idx` (exclusive), and the returned value will
    /// contain all elements from `idx` to the end of the vector.
    /// ```rust
    /// use bitpack_vec::BitpackVec;
    /// let mut bv = BitpackVec::new(5);
    ///
    /// bv.push(5);
    /// bv.push(6);
    /// bv.push(7);
    /// bv.push(8);
    ///
    /// let bv_rest = bv.split_off(2);
    ///
    /// assert_eq!(bv.to_vec(), &[5, 6]);
    /// assert_eq!(bv_rest.to_vec(), &[7, 8]);
    /// ```
    pub fn split_off(&mut self, idx: usize) -> BitpackVec {
        assert!(
            idx <= self.len(),
            "split index ({}) should be <= len ({})",
            idx,
            self.len()
        );
        let mut rest = BitpackVec::new(self.width());

        for i in idx..self.len() {
            rest.push(self.at(i));
        }

        self.truncate(idx);
        rest
    }

    /// Copies the bitpacked vector into a standard, non-packed vector of
    /// `u64`s.
    /// ```rust
    /// use bitpack_vec::BitpackVec;
    /// let mut bv = BitpackVec::new(5);
    ///
    /// bv.push(5);
    /// bv.push(6);
    /// bv.push(7);
    /// bv.push(8);
    ///
    /// let v = bv.to_vec();
    ///
    /// assert_eq!(v, vec![5, 6, 7, 8]);
    /// ```
    pub fn to_vec(&self) -> Vec<u64> {
        let mut r = Vec::with_capacity(self.len());

        for i in 0..self.len() {
            r.push(self.at(i))
        }

        r
    }

    /// Changes the width of the vector via copying (`O(n)`). Panics if any
    /// element in the current vector will not fit in `new_width` bits.
    ///
    /// ```rust
    /// use bitpack_vec::BitpackVec;
    /// let mut bv = BitpackVec::new(5);
    ///
    ///
    /// assert!(!bv.fits(34)); // doesn't fit
    /// bv.change_width(6);
    /// assert!(bv.fits(34)); // fits now
    /// ```
    pub fn change_width(&mut self, new_width: usize) {
        if new_width == self.width() {
            return;
        }

        let mut nv = BitpackVec::new(new_width);
        for item in self.iter() {
            nv.push(item);
        }
        *self = nv;
    }

    /// Allows iteration over the values in the bit vector.
    /// ```rust
    /// use bitpack_vec::BitpackVec;
    /// let mut bv = BitpackVec::new(5);
    ///
    /// bv.push(5);
    /// bv.push(6);
    /// bv.push(7);
    /// bv.push(8);
    ///
    /// let v: Vec<u64> = bv.iter().filter(|x| x % 2 == 0).collect();
    /// assert_eq!(v, vec![6, 8]);
    /// ```
    pub fn iter(&self) -> BitpackIter {
        BitpackIter { bv: self, curr: 0 }
    }
}

/// Iterator over a `BitpackVec`
pub struct BitpackIter<'a> {
    bv: &'a BitpackVec,
    curr: usize,
}

impl Iterator for BitpackIter<'_> {
    type Item = u64;

    fn next(&mut self) -> Option<Self::Item> {
        if self.curr >= self.bv.len() {
            return None;
        }

        let v = self.bv.at(self.curr);
        self.curr += 1;

        Some(v)
    }
}

impl PartialEq for BitpackVec {
    fn eq(&self, other: &Self) -> bool {
        if self.width != other.width {
            return false;
        }

        if self.len != other.len {
            return false;
        }

        // TODO could use a faster, bitwise check here, but we can't just
        // compare the underlying vecs because an element may have been
        // removed from one, but the data left and unzero'd
        for i in 0..self.len() {
            if self.at(i) != other.at(i) {
                return false;
            }
        }

        true
    }
}

impl Debug for BitpackVec {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&format!("BitpackVec (width: {}) [ ", self.width()))?;

        for i in self.iter() {
            f.write_str(&format!("{} ", i))?;
        }

        f.write_str("]\n")
    }
}

impl Extend<u64> for BitpackVec {
    fn extend<T: IntoIterator<Item = u64>>(&mut self, iter: T) {
        for i in iter {
            self.push(i);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    pub fn test_simple_6bit() {
        let mut v = BitpackVec::new(6);

        for i in 0..64 {
            v.push(i)
        }

        for i in 0..64 {
            assert_eq!(v.at(i as usize), i);
        }

        assert_eq!(v.len(), 64);
    }

    #[test]
    pub fn test_simple_5bit() {
        let mut v = BitpackVec::new(5);

        for i in 0..32 {
            v.push(i)
        }

        for i in 0..32 {
            assert_eq!(v.at(i as usize), i);
        }

        assert_eq!(v.len(), 32);
    }

    #[test]
    pub fn test_perfect_pack_8bit() {
        let mut v = BitpackVec::new(8);

        v.push(10);
        v.push(11);
        v.push(12);
        v.push(13);
        v.push(14);
        v.push(15);
        v.push(16);
        v.push(17);

        assert_eq!(v.at(0), 10);
        assert_eq!(v.at(1), 11);
        assert_eq!(v.at(2), 12);
        assert_eq!(v.at(3), 13);
        assert_eq!(v.at(4), 14);
        assert_eq!(v.at(5), 15);
        assert_eq!(v.at(6), 16);
        assert_eq!(v.at(7), 17);

        assert_eq!(v.capacity(), 8);
    }

    #[test]
    pub fn test_immut_n_bit() {
        for bits in 1..13 {
            let mut v = BitpackVec::new(bits);

            for x in 0..2_u64.pow(bits as u32) {
                v.push(x);
            }

            for i in 0..2_u64.pow(bits as u32) {
                assert_eq!(v.at(i as usize), i);
            }

            assert_eq!(v.len(), 2_u64.pow(bits as u32) as usize);
        }
    }

    #[test]
    pub fn test_set() {
        let mut v = BitpackVec::new(5);

        for i in 0..16 {
            v.push(i);
        }

        assert_eq!(v.at(5), 5);
        v.set(5, 20);
        assert_eq!(v.at(5), 20);
        v.set(5, 5);
        assert_eq!(v.at(5), 5);

        v.set(12, 20);
        assert_eq!(v.at(12), 20);

        for i in 0..16 {
            v.set(i as usize, i + 5);
        }

        for i in 0..16 {
            assert_eq!(v.at(i as usize), i + 5);
        }
    }

    #[test]
    pub fn test_pop() {
        let mut v = BitpackVec::new(11);
        let mut rv = Vec::new();

        for i in 0..2048 {
            v.push(i);
            rv.push(i);
        }

        while let Some(i) = rv.pop() {
            assert_eq!(i, v.pop().unwrap());
        }

        assert!(v.pop().is_none());
    }

    #[test]
    pub fn test_push_pop() {
        let mut v = BitpackVec::new(11);
        let mut rv = Vec::new();

        for i in 0..2048 {
            v.push(i);
            rv.push(i);
        }

        for _ in 0..100 {
            assert_eq!(v.pop(), rv.pop());
        }

        for i in 0..10_000 {
            v.push(i % 2048);
            rv.push(i % 2048);
        }

        while let Some(i) = rv.pop() {
            assert_eq!(i, v.pop().unwrap());
        }

        assert!(v.pop().is_none());
    }

    #[test]
    pub fn test_split_off() {
        let mut v = Vec::new();
        let mut bv = BitpackVec::new(7);

        for i in 0..10_000 {
            v.push(i % 128);
            bv.push(i % 128);
        }

        assert_eq!(v, bv.to_vec());

        let rv = v.split_off(500);
        let rbv = bv.split_off(500);

        assert_eq!(v, bv.to_vec());
        assert_eq!(rv, rbv.to_vec());
    }

    #[test]
    pub fn test_raw() {
        let mut v = BitpackVec::new(7);

        for i in 0..10_000 {
            v.push(i % 128);
        }

        let data = v.into_raw_vec();

        let nv = BitpackVec::from_raw_vec(data, 7, 10_000);

        for i in 0..10_000 {
            assert_eq!(nv.at(i), i as u64 % 128);
        }
    }

    #[test]
    pub fn test_iter() {
        let mut v = BitpackVec::new(7);

        for i in 0..10_000 {
            v.push(i % 128);
        }

        let from_iter: Vec<u64> = v.iter().collect();
        assert_eq!(v.to_vec(), from_iter);
    }

    #[test]
    pub fn test_eq() {
        let mut v = BitpackVec::new(7);
        let mut v2 = BitpackVec::new(7);

        for i in 0..10_000 {
            v.push(i % 128);
            v2.push(i % 128);
        }

        assert_eq!(v, v2);

        v.push(7);
        assert_ne!(v, v2);

        v.pop();
        assert_eq!(v, v2);

        v.push(8);
        v2.push(8);
        assert_eq!(v, v2);
    }

    #[test]
    pub fn test_from_slice() {
        let v = vec![4, 19, 184, 18314, 62];
        let bv = BitpackVec::from_slice(&v);

        assert_eq!(bv.width(), 15);
        assert_eq!(bv.to_vec(), v);
    }

    #[test]
    pub fn test_change_width() {
        let mut v = vec![1, 2, 3, 4];
        let mut bv = BitpackVec::from_slice(&v);

        v.push(66);

        assert!(!bv.fits(66));
        bv.change_width(7);
        assert!(bv.fits(66));
        bv.push(66);

        assert_eq!(bv.to_vec(), v);
    }

    #[test]
    #[should_panic]
    fn test_does_not_fit_push() {
        let mut bv = BitpackVec::new(5);
        bv.push(32);
    }

    #[test]
    #[should_panic]
    fn test_does_not_fit_set() {
        let mut bv = BitpackVec::new(5);
        bv.push(18);
        bv.set(0, 32);
    }
}