packmem 0.3.1

Packed, operation-aware columns for record collections: filter and aggregate in memory without decompressing.
Documentation
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
//! Integer columns: a small vocabulary of composable lightweight encodings,
//! chosen per column from a sample, each with operators that run on the encoded form.
use crate::buf::Buf;
use std::collections::HashSet;

/// What the caller will mostly do with the data. Steers the encoding choice.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Hint {
    Memory,
    Balanced,
    Speed,
    /// As `Memory`, and long text is compressed with Zstandard in small blocks: about half the size on prose, at the
    /// cost of decoding a block to read a value and of filters reading the text. Builds without Zstandard treat it
    /// as `Memory`.
    Compact,
}

fn width(max: u64) -> u8 {
    (64 - max.leading_zeros()) as u8
}

/// Exact-width bit-packed array (width <= 56), O(1) random access via one unaligned load.
pub struct BitPacked {
    data: Buf<u8>,
    w: u8,
    mask: u64,
}
impl BitPacked {
    fn new(vals: impl Iterator<Item = u64>, n: usize, w: u8) -> Self {
        let mut data = vec![0u8; (n * w as usize + 7) / 8 + 8];
        for (i, v) in vals.enumerate() {
            let bit = i * w as usize;
            let p = bit >> 3;
            let mut x = u64::from_le_bytes(data[p..p + 8].try_into().unwrap());
            x |= v << (bit & 7);
            data[p..p + 8].copy_from_slice(&x.to_le_bytes());
        }
        BitPacked {
            data: data.into(),
            w,
            mask: if w == 0 { 0 } else { (1u64 << w) - 1 },
        }
    }
    #[inline(always)]
    fn get(&self, i: usize) -> u64 {
        let bit = i * self.w as usize;
        let p = bit >> 3;
        let x = u64::from_le_bytes(self.data[p..p + 8].try_into().unwrap());
        (x >> (bit & 7)) & self.mask
    }
}

/// Physical storage of unsigned codes: byte-aligned (SIMD-friendly) or exact-width bits.
pub enum Store {
    A8(Buf<u8>),
    A16(Buf<u16>),
    A32(Buf<u32>),
    A64(Buf<u64>),
    Bits(BitPacked),
}

macro_rules! each {
    ($s:expr, $v:ident => $body:expr, $b:ident, $n:ident => $bits:expr) => {
        match $s {
            Store::A8($v) => $body,
            Store::A16($v) => $body,
            Store::A32($v) => $body,
            Store::A64($v) => $body,
            Store::Bits($b) => {
                let $n = 0usize;
                let _ = $n;
                $bits
            }
        }
    };
}

impl Store {
    /// The field width `pack` will use for codes up to `max` (aligned, or exact bits), and whether it is bit-packed.
    fn layout(max: u64, hint: Hint) -> (u8, bool) {
        let w = width(max);
        let aligned = match w {
            0..=8 => 8,
            9..=16 => 16,
            17..=32 => 32,
            _ => 64,
        };
        let use_bits = w <= 56
            && w < aligned
            && match hint {
                Hint::Memory | Hint::Compact => true,
                Hint::Balanced => aligned as f64 / w.max(1) as f64 >= 1.34,
                Hint::Speed => false,
            };
        (if use_bits { w } else { aligned }, use_bits)
    }
    pub fn pack(vals: impl Iterator<Item = u64> + Clone, n: usize, max: u64, hint: Hint) -> Store {
        let w = width(max);
        let (aligned, use_bits) = match Self::layout(max, hint) {
            (_, true) => (0, true),
            (a, false) => (a, false),
        };
        if use_bits {
            return Store::Bits(BitPacked::new(vals, n, w));
        }
        match aligned {
            8 => Store::A8(vals.map(|v| v as u8).collect()),
            16 => Store::A16(vals.map(|v| v as u16).collect()),
            32 => Store::A32(vals.map(|v| v as u32).collect()),
            _ => Store::A64(vals.collect()),
        }
    }
    pub fn bytes(&self) -> usize {
        match self {
            Store::A8(v) => v.len(),
            Store::A16(v) => v.len() * 2,
            Store::A32(v) => v.len() * 4,
            Store::A64(v) => v.len() * 8,
            Store::Bits(b) => b.data.len(),
        }
    }
    pub fn kind(&self) -> String {
        match self {
            Store::A8(_) => "u8".into(),
            Store::A16(_) => "u16".into(),
            Store::A32(_) => "u32".into(),
            Store::A64(_) => "u64".into(),
            Store::Bits(b) => format!("{}bit", b.w),
        }
    }
    #[inline(always)]
    pub fn get(&self, i: usize) -> u64 {
        match self {
            Store::A8(v) => v[i] as u64,
            Store::A16(v) => v[i] as u64,
            Store::A32(v) => v[i] as u64,
            Store::A64(v) => v[i],
            Store::Bits(b) => b.get(i),
        }
    }
    pub fn sum(&self, n: usize) -> u64 {
        // wraps on purpose: `PackedInts::sum` is a wrapping 64-bit sum, and the table only uses it for small values
        each!(self, v => v.iter().fold(0u64, |a, &x| a.wrapping_add(x as u64)), b, _z => (0..n).fold(0u64, |a, i| a.wrapping_add(b.get(i))))
    }
    pub fn count_range(&self, n: usize, lo: u64, hi: u64) -> usize {
        each!(self, v => v.iter().filter(|&&x| (x as u64) >= lo && (x as u64) <= hi).count(), b, _z => (0..n).filter(|&i| { let x = b.get(i); x >= lo && x <= hi }).count())
    }
    pub fn select_range(&self, n: usize, lo: u64, hi: u64, out: &mut Vec<u32>) {
        each!(self, v => for (i, &x) in v.iter().enumerate() { if (x as u64) >= lo && (x as u64) <= hi { out.push(i as u32) } },
              b, _z => for i in 0..n { let x = b.get(i); if x >= lo && x <= hi { out.push(i as u32) } })
    }
    pub fn for_each(&self, n: usize, mut f: impl FnMut(u64)) {
        each!(self, v => for &x in v.iter() { f(x as u64) }, b, _z => for i in 0..n { f(b.get(i)) })
    }
}

const BLOCK: usize = 1024;

pub enum PackedInts {
    /// Every value identical.
    Const { v: i64, len: usize },
    /// Frame of reference: value = base + code.
    For { base: i64, s: Store, len: usize },
    /// Sorted dictionary + codes. Order-preserving, so range predicates work on codes.
    Dict {
        dict: Buf<i64>,
        codes: Store,
        len: usize,
    },
    /// Run-length: (value, exclusive end index) per run.
    Rle {
        vals: Buf<i64>,
        ends: Buf<u32>,
        len: usize,
    },
    /// Per-1024-block frame of reference with per-block width (good for sorted/clustered data).
    BlockFor {
        bases: Buf<i64>,
        widths: Buf<u8>,
        offs: Buf<u32>,
        data: Buf<u8>,
        len: usize,
        sorted: bool,
    },
}

pub struct IntStats {
    pub n: usize,
    pub min: i64,
    pub max: i64,
    pub sample_n: usize,
    pub sample_distinct: usize,
    pub avg_run: f64,
    pub block_bits: f64,
    pub sorted: bool,
}

pub fn stats(vals: &[i64]) -> IntStats {
    let n = vals.len();
    let (mut min, mut max, mut sorted) = (i64::MAX, i64::MIN, true);
    let mut prev = i64::MIN;
    for &v in vals {
        if v < min {
            min = v
        }
        if v > max {
            max = v
        }
        if v < prev {
            sorted = false
        }
        prev = v;
    }
    // sample: up to 64 runs of 256 consecutive values, evenly spaced (keeps run/cluster structure visible)
    let run = 256usize;
    let nruns = if n <= 64 * run {
        (n + run - 1) / run
    } else {
        64
    };
    let mut distinct: HashSet<i64> = HashSet::new();
    let (mut sample_n, mut runs, mut bits) = (0usize, 0usize, 0f64);
    for r in 0..nruns {
        let start = if n <= 64 * run {
            r * run
        } else {
            r * (n - run) / (nruns - 1).max(1)
        };
        let s = &vals[start..(start + run).min(n)];
        let (mut lo, mut hi) = (i64::MAX, i64::MIN);
        for (i, &v) in s.iter().enumerate() {
            distinct.insert(v);
            if v < lo {
                lo = v
            }
            if v > hi {
                hi = v
            }
            if i == 0 || v != s[i - 1] {
                runs += 1
            }
        }
        sample_n += s.len();
        bits += width((hi as i128 - lo as i128) as u64) as f64 * s.len() as f64;
    }
    IntStats {
        n,
        min,
        max,
        sample_n,
        sample_distinct: distinct.len(),
        avg_run: sample_n as f64 / runs.max(1) as f64,
        block_bits: bits / sample_n.max(1) as f64 + 13.0 * 8.0 / BLOCK as f64,
        sorted,
    }
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Scheme {
    Const,
    For,
    Dict,
    Rle,
    BlockFor,
}

/// Estimated bits/value for every applicable scheme (from the sample), before operation weighting.
pub fn estimates(st: &IntStats) -> Vec<(Scheme, f64)> {
    let mut c = vec![];
    if st.n == 0 || st.min == st.max {
        c.push((Scheme::Const, 0.0));
        return c;
    }
    let range = (st.max as i128 - st.min as i128) as u128;
    let w = if range > u64::MAX as u128 {
        64
    } else {
        width(range as u64)
    };
    c.push((Scheme::For, w as f64));
    if st.sample_distinct <= 4096 && st.sample_distinct * 4 < st.sample_n {
        c.push((
            Scheme::Dict,
            width(st.sample_distinct as u64 - 1).max(1) as f64
                + st.sample_distinct as f64 * 64.0 / st.n as f64,
        ));
    }
    if st.avg_run >= 2.0 {
        c.push((Scheme::Rle, 96.0 / st.avg_run));
    }
    c.push((Scheme::BlockFor, st.block_bits));
    c
}

/// Operation-aware choice: bits/value scaled by a per-scheme access-cost penalty for the hint.
pub fn choose(st: &IntStats, hint: Hint) -> Scheme {
    let pen = |s: Scheme| -> f64 {
        match (hint, s) {
            (Hint::Memory | Hint::Compact, _) => 1.0,
            (Hint::Balanced, Scheme::Dict) => 1.1,
            (Hint::Balanced, Scheme::BlockFor) => 1.25,
            (Hint::Balanced, Scheme::Rle) => 1.5,
            (Hint::Speed, Scheme::Dict) => 1.3,
            (Hint::Speed, Scheme::BlockFor) => 2.0,
            (Hint::Speed, Scheme::Rle) => 4.0,
            _ => 1.0,
        }
    };
    // +1 bit floor so that near-zero estimates don't make penalties meaningless
    estimates(st)
        .into_iter()
        .min_by(|a, b| {
            ((a.1 + 0.5) * pen(a.0))
                .partial_cmp(&((b.1 + 0.5) * pen(b.0)))
                .unwrap()
        })
        .unwrap()
        .0
}

impl PackedInts {
    pub fn encode(vals: &[i64], hint: Hint) -> Self {
        let st = stats(vals);
        Self::encode_as(vals, choose(&st, hint), hint, &st)
    }
    pub fn encode_as(vals: &[i64], scheme: Scheme, hint: Hint, st: &IntStats) -> Self {
        let n = vals.len();
        match scheme {
            Scheme::Const => PackedInts::Const {
                v: if n > 0 { st.min } else { 0 },
                len: n,
            },
            Scheme::For => {
                let base = st.min;
                let range = (st.max as i128 - base as i128) as u64;
                PackedInts::For {
                    base,
                    s: Store::pack(
                        vals.iter().map(move |&v| v.wrapping_sub(base) as u64),
                        n,
                        range,
                        hint,
                    ),
                    len: n,
                }
            }
            Scheme::Dict => {
                let mut set: HashSet<i64> = HashSet::new();
                for &v in vals {
                    set.insert(v);
                    if set.len() > 65536 {
                        return Self::encode_as(vals, Scheme::For, hint, st);
                    }
                }
                // The choice was made from a sample, which undercounts distinct values when the tail is long (token ids,
                // Zipf-like keys). Now that the real dictionary is known, keep it only if it is actually smaller.
                let range = (st.max as i128 - st.min as i128) as u128;
                if range <= u64::MAX as u128 {
                    let dict_bits = Store::layout(set.len() as u64 - 1, hint).0 as u128 * n as u128
                        + set.len() as u128 * 64;
                    let for_bits = Store::layout(range as u64, hint).0 as u128 * n as u128;
                    if dict_bits >= for_bits {
                        return Self::encode_as(vals, Scheme::For, hint, st);
                    }
                }
                let mut dict: Vec<i64> = set.into_iter().collect();
                dict.sort_unstable();
                let d = &dict;
                let codes = Store::pack(
                    vals.iter().map(move |v| d.binary_search(v).unwrap() as u64),
                    n,
                    dict.len() as u64 - 1,
                    hint,
                );
                PackedInts::Dict {
                    dict: dict.into(),
                    codes,
                    len: n,
                }
            }
            Scheme::Rle => {
                let (mut rv, mut ends) = (vec![], vec![]);
                for (i, &v) in vals.iter().enumerate() {
                    if i == 0 || v != vals[i - 1] {
                        if i > 0 {
                            ends.push(i as u32)
                        }
                        rv.push(v)
                    }
                }
                if n > 0 {
                    ends.push(n as u32)
                }
                rv.shrink_to_fit();
                ends.shrink_to_fit();
                PackedInts::Rle {
                    vals: rv.into(),
                    ends: ends.into(),
                    len: n,
                }
            }
            Scheme::BlockFor => {
                let (mut bases, mut widths, mut offs, mut data) =
                    (vec![], vec![], vec![], Vec::<u8>::new());
                for blk in vals.chunks(BLOCK) {
                    let lo = *blk.iter().min().unwrap();
                    let hi = *blk.iter().max().unwrap();
                    let range = (hi as i128 - lo as i128) as u128;
                    let w = if range > (1u128 << 56) - 1 {
                        64
                    } else {
                        width(range as u64)
                    };
                    bases.push(lo);
                    widths.push(w);
                    offs.push(data.len() as u32);
                    if w == 64 {
                        for &v in blk {
                            data.extend_from_slice(&(v.wrapping_sub(lo) as u64).to_le_bytes())
                        }
                    } else {
                        let bp = BitPacked::new(blk.iter().map(|&v| (v - lo) as u64), blk.len(), w);
                        data.extend_from_slice(&bp.data[..bp.data.len() - 8]);
                    }
                }
                data.extend_from_slice(&[0u8; 8]);
                data.shrink_to_fit();
                bases.shrink_to_fit();
                widths.shrink_to_fit();
                offs.shrink_to_fit();
                PackedInts::BlockFor {
                    bases: bases.into(),
                    widths: widths.into(),
                    offs: offs.into(),
                    data: data.into(),
                    len: n,
                    sorted: st.sorted,
                }
            }
        }
    }
    pub fn len(&self) -> usize {
        match self {
            PackedInts::Const { len, .. }
            | PackedInts::For { len, .. }
            | PackedInts::Dict { len, .. }
            | PackedInts::Rle { len, .. }
            | PackedInts::BlockFor { len, .. } => *len,
        }
    }
    pub fn bytes(&self) -> usize {
        match self {
            PackedInts::Const { .. } => 16,
            PackedInts::For { s, .. } => s.bytes() + 8,
            PackedInts::Dict { dict, codes, .. } => dict.len() * 8 + codes.bytes(),
            PackedInts::Rle { vals, ends, .. } => vals.len() * 8 + ends.len() * 4,
            PackedInts::BlockFor {
                bases,
                widths,
                offs,
                data,
                ..
            } => bases.len() * 8 + widths.len() + offs.len() * 4 + data.len(),
        }
    }
    pub fn describe(&self) -> String {
        match self {
            PackedInts::Const { .. } => "const".into(),
            PackedInts::For { s, .. } => format!("for/{}", s.kind()),
            PackedInts::Dict { dict, codes, .. } => {
                format!("dict({})/{}", dict.len(), codes.kind())
            }
            PackedInts::Rle { vals, .. } => format!("rle({} runs)", vals.len()),
            PackedInts::BlockFor { sorted, .. } => {
                format!("blockfor{}", if *sorted { "/sorted" } else { "" })
            }
        }
    }
    #[inline]
    fn blk_get(data: &[u8], off: usize, w: u8, i: usize) -> u64 {
        if w == 64 {
            let p = off + i * 8;
            return u64::from_le_bytes(data[p..p + 8].try_into().unwrap());
        }
        if w == 0 {
            return 0;
        }
        let bit = i * w as usize;
        let p = off + (bit >> 3);
        (u64::from_le_bytes(data[p..p + 8].try_into().unwrap()) >> (bit & 7)) & ((1u64 << w) - 1)
    }
    /// O(1) random access (O(log runs) for RLE) without decoding neighbours.
    #[inline]
    pub fn get(&self, i: usize) -> i64 {
        match self {
            PackedInts::Const { v, .. } => *v,
            PackedInts::For { base, s, .. } => base.wrapping_add(s.get(i) as i64),
            PackedInts::Dict { dict, codes, .. } => dict[codes.get(i) as usize],
            PackedInts::Rle { vals, ends, .. } => vals[ends.partition_point(|&e| e as usize <= i)],
            PackedInts::BlockFor {
                bases,
                widths,
                offs,
                data,
                ..
            } => {
                let b = i / BLOCK;
                bases[b].wrapping_add(Self::blk_get(data, offs[b] as usize, widths[b], i % BLOCK) as i64)
            }
        }
    }
    fn blk_len(&self, b: usize) -> usize {
        (self.len() - b * BLOCK).min(BLOCK)
    }
    /// Sum computed on the encoded form (e.g. RLE: value*run; Dict: histogram of codes).
    pub fn sum(&self) -> i64 {
        match self {
            PackedInts::Const { v, len } => v.wrapping_mul(*len as i64),
            PackedInts::For { base, s, len } => base
                .wrapping_mul(*len as i64)
                .wrapping_add(s.sum(*len) as i64),
            PackedInts::Dict { dict, codes, len } => {
                let mut h = vec![0u64; dict.len()];
                codes.for_each(*len, |c| h[c as usize] += 1);
                h.iter()
                    .zip(dict)
                    .fold(0i64, |a, (&c, &v)| a.wrapping_add(v.wrapping_mul(c as i64)))
            }
            PackedInts::Rle { vals, ends, .. } => {
                let mut p = 0u32;
                let mut s = 0i64;
                for (v, &e) in vals.iter().zip(ends) {
                    s = s.wrapping_add(v.wrapping_mul((e - p) as i64));
                    p = e
                }
                s
            }
            PackedInts::BlockFor {
                bases,
                widths,
                offs,
                data,
                ..
            } => {
                let mut s = 0i64;
                for b in 0..bases.len() {
                    let m = self.blk_len(b);
                    s = s.wrapping_add(bases[b].wrapping_mul(m as i64));
                    let (o, w) = (offs[b] as usize, widths[b]);
                    let mut t = 0u64;
                    for i in 0..m {
                        t = t.wrapping_add(Self::blk_get(data, o, w, i))
                    }
                    s = s.wrapping_add(t as i64)
                }
                s
            }
        }
    }
    /// Count of values in [lo, hi], evaluated on codes.
    pub fn count_range(&self, lo: i64, hi: i64) -> usize {
        if lo > hi {
            return 0;
        }
        match self {
            PackedInts::Const { v, len } => {
                if *v >= lo && *v <= hi {
                    *len
                } else {
                    0
                }
            }
            PackedInts::For { base, s, len } => {
                if hi < *base {
                    return 0;
                }
                let l = if lo <= *base {
                    0
                } else {
                    lo.wrapping_sub(*base) as u64
                };
                s.count_range(*len, l, hi.wrapping_sub(*base) as u64)
            }
            PackedInts::Dict { dict, codes, len } => {
                let a = dict.partition_point(|&d| d < lo);
                let b = dict.partition_point(|&d| d <= hi);
                if a >= b {
                    0
                } else {
                    codes.count_range(*len, a as u64, b as u64 - 1)
                }
            }
            PackedInts::Rle { vals, ends, .. } => {
                let mut p = 0u32;
                let mut c = 0usize;
                for (v, &e) in vals.iter().zip(ends) {
                    if *v >= lo && *v <= hi {
                        c += (e - p) as usize
                    }
                    p = e
                }
                c
            }
            PackedInts::BlockFor { bases, sorted, .. } => {
                if *sorted {
                    // binary search on block bases, then inside two blocks only
                    let lb = |x: i64| -> usize {
                        // first index with value >= x
                        let b = bases.partition_point(|&v| v < x);
                        let b0 = b.saturating_sub(1);
                        if bases.is_empty() {
                            return 0;
                        }
                        let m = self.blk_len(b0);
                        let (mut l, mut r) = (0, m);
                        while l < r {
                            let mid = (l + r) / 2;
                            if self.get(b0 * BLOCK + mid) < x {
                                l = mid + 1
                            } else {
                                r = mid
                            }
                        }
                        b0 * BLOCK + l
                    };
                    let a = lb(lo);
                    let b = if hi == i64::MAX {
                        self.len()
                    } else {
                        lb(hi + 1)
                    };
                    b.saturating_sub(a)
                } else {
                    (0..self.len())
                        .filter(|&i| {
                            let v = self.get(i);
                            v >= lo && v <= hi
                        })
                        .count()
                }
            }
        }
    }
    pub fn count_eq(&self, v: i64) -> usize {
        self.count_range(v, v)
    }
    /// Row ids whose value lies in [lo, hi].
    pub fn select_range(&self, lo: i64, hi: i64, out: &mut Vec<u32>) {
        match self {
            PackedInts::For { base, s, len } => {
                if hi < *base {
                    return;
                }
                let l = if lo <= *base {
                    0
                } else {
                    lo.wrapping_sub(*base) as u64
                };
                s.select_range(*len, l, hi.wrapping_sub(*base) as u64, out)
            }
            PackedInts::Dict { dict, codes, len } => {
                let a = dict.partition_point(|&d| d < lo);
                let b = dict.partition_point(|&d| d <= hi);
                if a < b {
                    codes.select_range(*len, a as u64, b as u64 - 1, out)
                }
            }
            PackedInts::Rle { vals, ends, .. } => {
                let mut p = 0u32;
                for (v, &e) in vals.iter().zip(ends) {
                    if *v >= lo && *v <= hi {
                        out.extend(p..e)
                    }
                    p = e
                }
            }
            _ => {
                for i in 0..self.len() {
                    let v = self.get(i);
                    if v >= lo && v <= hi {
                        out.push(i as u32)
                    }
                }
            }
        }
    }
    pub fn sum_at(&self, sel: &[u32]) -> i64 {
        sel.iter()
            .fold(0i64, |a, &i| a.wrapping_add(self.get(i as usize)))
    }
    pub fn to_vec(&self) -> Vec<i64> {
        (0..self.len()).map(|i| self.get(i)).collect()
    }
}

use crate::wire::{R, Res, W, bad};

impl Store {
    pub(crate) fn write(&self, w: &mut W) {
        match self {
            Store::A8(v) => {
                w.u8(0);
                w.v_u8(v)
            }
            Store::A16(v) => {
                w.u8(1);
                w.v_u16(v)
            }
            Store::A32(v) => {
                w.u8(2);
                w.v_u32(v)
            }
            Store::A64(v) => {
                w.u8(3);
                w.v_u64(v)
            }
            Store::Bits(b) => {
                w.u8(4);
                w.u8(b.w);
                w.v_u8(&b.data)
            }
        }
    }
    pub(crate) fn read(r: &mut R) -> Res<Store> {
        Ok(match r.get_u8()? {
            0 => Store::A8(r.get_v_u8()?),
            1 => Store::A16(r.get_v_u16()?),
            2 => Store::A32(r.get_v_u32()?),
            3 => Store::A64(r.get_v_u64()?),
            4 => {
                let w = r.get_u8()?;
                if w > 56 {
                    return bad("bit width");
                }
                Store::Bits(BitPacked {
                    data: r.get_v_u8()?,
                    w,
                    mask: if w == 0 { 0 } else { (1u64 << w) - 1 },
                })
            }
            _ => return bad("store kind"),
        })
    }
}

impl PackedInts {
    pub(crate) fn write(&self, w: &mut W) {
        match self {
            PackedInts::Const { v, len } => {
                w.u8(0);
                w.i64(*v);
                w.u64(*len as u64)
            }
            PackedInts::For { base, s, len } => {
                w.u8(1);
                w.i64(*base);
                s.write(w);
                w.u64(*len as u64)
            }
            PackedInts::Dict { dict, codes, len } => {
                w.u8(2);
                w.v_i64(dict);
                codes.write(w);
                w.u64(*len as u64)
            }
            PackedInts::Rle { vals, ends, len } => {
                w.u8(3);
                w.v_i64(vals);
                w.v_u32(ends);
                w.u64(*len as u64)
            }
            PackedInts::BlockFor {
                bases,
                widths,
                offs,
                data,
                len,
                sorted,
            } => {
                w.u8(4);
                w.v_i64(bases);
                w.v_u8(widths);
                w.v_u32(offs);
                w.v_u8(data);
                w.u64(*len as u64);
                w.bool(*sorted)
            }
        }
    }
    pub(crate) fn read(r: &mut R) -> Res<PackedInts> {
        Ok(match r.get_u8()? {
            0 => PackedInts::Const {
                v: r.get_i64()?,
                len: r.get_u64()? as usize,
            },
            1 => PackedInts::For {
                base: r.get_i64()?,
                s: Store::read(r)?,
                len: r.get_u64()? as usize,
            },
            2 => PackedInts::Dict {
                dict: r.get_v_i64()?,
                codes: Store::read(r)?,
                len: r.get_u64()? as usize,
            },
            3 => PackedInts::Rle {
                vals: r.get_v_i64()?,
                ends: r.get_v_u32()?,
                len: r.get_u64()? as usize,
            },
            4 => PackedInts::BlockFor {
                bases: r.get_v_i64()?,
                widths: r.get_v_u8()?,
                offs: r.get_v_u32()?,
                data: r.get_v_u8()?,
                len: r.get_u64()? as usize,
                sorted: r.get_bool()?,
            },
            _ => return bad("integer encoding"),
        })
    }
}