prefix-trie 0.9.0

Prefix trie (tree) datastructure (both a set and a map) that provides exact and longest-prefix matches.
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
//! The inner node implementation.

use array_const_fn_init::array_const_fn_init;
use num_traits::{CheckedShr, One, PrimInt, Unsigned, Zero};

use super::table::{DataIdx, K, NUM_CHILDREN, NUM_DATA};
use crate::{
    allocator::{AllocIdx, Loc},
    prefix::mask_from_prefix_len,
};

pub(crate) trait Key: Unsigned + PrimInt + Zero + One + CheckedShr {}
impl<R: Unsigned + PrimInt + Zero + One + CheckedShr> Key for R {}

/// A single node for the dense TreeBitMap
#[derive(Clone, Copy, Default)]
pub(crate) struct MultiBitNode {
    pub(super) data_bitmap: u32,
    pub(super) child_bitmap: u32,
    // The index at which all children are located in the table.
    pub(super) children_idx: AllocIdx,
    pub(super) data_idx: AllocIdx,
}

impl std::fmt::Debug for MultiBitNode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MultiBitNode")
            .field(
                "data_bitmap",
                &format!("  {:#0width$b}", self.data_bitmap, width = NUM_DATA),
            )
            .field(
                "child_bitmap",
                &format!("{:#0width$b}", self.child_bitmap, width = NUM_CHILDREN),
            )
            .field("children_idx", &self.children_idx)
            .field("cell_idx", &self.data_idx)
            .finish()
    }
}

/// Iterate over set bits in `filter`, yielding `Loc`s with slots computed from `bitmap`.
///
/// `bitmap` is the full bitmap (used for POPCNT slot computation).
/// `filter` is the subset of bits to yield (must be a subset of `bitmap`).
/// `len` is the number of bit positions to scan (NUM_DATA or NUM_CHILDREN).
#[inline(always)]
fn bitmap_locs(
    idx: AllocIdx,
    bitmap: u32,
    filter: u32,
    len: u32,
) -> impl DoubleEndedIterator<Item = Loc> + 'static {
    (0..len)
        .filter(move |&raw| filter & (1 << raw) != 0)
        .map(move |raw| Loc::new(idx, raw, bitmap))
}

impl MultiBitNode {
    /****** DATA HANDLING *******/

    #[inline(always)]
    pub(crate) fn has_data_bit(&self, bit: u32) -> bool {
        self.data_bitmap & (1 << bit) != 0
    }

    #[inline(always)]
    pub(crate) fn data_bitmap(&self) -> u32 {
        self.data_bitmap
    }

    #[inline(always)]
    pub(crate) fn child_bitmap(&self) -> u32 {
        self.child_bitmap
    }

    #[inline(always)]
    pub(crate) fn set_data_bit(&mut self, bit: u32) {
        self.data_bitmap |= 1 << bit;
    }

    #[inline(always)]
    pub(crate) fn unset_data_bit(&mut self, bit: u32) {
        self.data_bitmap &= !(1 << bit);
    }

    /// Get an iterator over all internal offsets that are set.
    /// Yields Loc structs with bit (bitmap position) and computed slot for data access.
    #[inline(always)]
    pub(crate) fn data_locs(&self) -> impl DoubleEndedIterator<Item = Loc> + 'static {
        bitmap_locs(
            self.data_idx,
            self.data_bitmap,
            self.data_bitmap,
            NUM_DATA as u32,
        )
    }

    /****** CHILD HANDLING *******/

    #[inline(always)]
    pub(crate) fn has_child_bit(&self, bit: u32) -> bool {
        self.child_bitmap & (1 << bit) != 0
    }

    #[inline(always)]
    pub(crate) fn set_child_bit(&mut self, bit: u32) {
        self.child_bitmap |= 1 << bit;
    }

    #[inline(always)]
    pub(crate) fn unset_child_bit(&mut self, bit: u32) {
        self.child_bitmap &= !(1 << bit);
    }

    /// Get an iterator over all children.
    /// Yields Loc with bit set to the child bitmap position (for operations like unset_child_bit).
    #[inline(always)]
    pub(crate) fn child_locs(&self) -> impl DoubleEndedIterator<Item = Loc> + 'static {
        bitmap_locs(
            self.children_idx,
            self.child_bitmap,
            self.child_bitmap,
            NUM_CHILDREN as u32,
        )
    }

    /****** COVER STUFF *******/

    /// Iterator over all data locations that are covered by the prefix. This includes the
    /// prefix itself. Each Loc includes the bit (bitmap position) and computed slot.
    ///
    /// Safety: This iterator only yields indixes that are set in the internal bitmap.
    #[inline(always)]
    pub(crate) fn data_cover_locs<R: Key>(
        &self,
        depth: u32,
        key: R,
        prefix_len: u32,
    ) -> impl DoubleEndedIterator<Item = Loc> + 'static {
        let filter = data_cover_mask(depth, key, prefix_len) & self.data_bitmap;
        bitmap_locs(self.data_idx, self.data_bitmap, filter, NUM_DATA as u32)
    }

    /// Iterator over the indices of all children that of that node that are covered within the
    /// prefix itself. Important: The function returns no entries if the prefix length is not part
    /// of this node.
    ///
    /// The function does check if these nodes actually exist, and only returns those indixes
    #[inline(always)]
    pub(crate) fn child_cover_locs<R: Key>(
        &self,
        depth: u32,
        key: R,
        prefix_len: u32,
    ) -> impl DoubleEndedIterator<Item = Loc> + 'static {
        let filter = child_cover_mask(depth, key, prefix_len) & self.child_bitmap;
        bitmap_locs(
            self.children_idx,
            self.child_bitmap,
            filter,
            NUM_CHILDREN as u32,
        )
    }

    /****** DATA LPM *******/

    /// Iterator over all internal node indices that cover a given prefix
    ///
    /// Safety: This iterator only yields indixes that are set in the internal bitmap.
    #[inline(always)]
    pub(crate) fn data_lpm_locs<R: Key>(
        &self,
        depth: u32,
        key: R,
        prefix_len: u32,
    ) -> impl DoubleEndedIterator<Item = Loc> + 'static {
        let filter = data_lpm_mask(depth, key, prefix_len) & self.data_bitmap;
        bitmap_locs(self.data_idx, self.data_bitmap, filter, NUM_DATA as u32)
    }

    /// Get the data loc of the longest prefix match in this node (if it exists).
    /// Returns Loc with bit (bitmap position) and computed slot.
    pub(crate) fn data_lpm_loc<R: Key>(&self, depth: u32, key: R, prefix_len: u32) -> Option<Loc> {
        let nodes_present = self.data_bitmap & data_lpm_mask(depth, key, prefix_len);
        if nodes_present == 0 {
            return None;
        }
        let msb_raw = u32::BITS - 1 - nodes_present.leading_zeros();
        Some(Loc::new(self.data_idx, msb_raw, self.data_bitmap))
    }

    /// Get the data loc of the shortest prefix match in this node (if it exists).
    /// Returns Loc with bit (bitmap position) and computed slot.
    pub(crate) fn data_spm_loc<R: Key>(&self, depth: u32, key: R, prefix_len: u32) -> Option<Loc> {
        let nodes_present = self.data_bitmap & data_lpm_mask(depth, key, prefix_len);
        if nodes_present == 0 {
            return None;
        }
        let lsb_raw = nodes_present.trailing_zeros();
        Some(Loc::new(self.data_idx, lsb_raw, self.data_bitmap))
    }
}

#[inline(always)]
pub(crate) fn data_bit<R: Key>(key: R, prefix_len: u32) -> u32 {
    // prefix must be in this node.
    let depth = (prefix_len / K) * K;
    let num_bits = R::zero().count_zeros();
    let mask = R::one().unsigned_shl(K).sub(R::one());
    // Extract the K-bit block starting at `depth` (from MSB).  When `depth + K > num_bits`
    // the window extends past the key width, so we left-shift instead of right-shifting to
    // avoid u32 underflow (e.g. depth=30, K=5, num_bits=32 -> shift would be −3).
    let pattern = if depth + K <= num_bits {
        key.unsigned_shr(num_bits - depth - K).bitand(mask)
    } else {
        key.unsigned_shl(depth + K - num_bits).bitand(mask)
    };
    let level = prefix_len - depth;
    let offset = (1u32 << level) - 1;
    pattern.unsigned_shr(K - level).to_u32().unwrap() + offset
}

const LEVEL_SIZE: usize = 1 << (K - 1);
const PATTERN_SIZE: usize = (K as usize) * LEVEL_SIZE;
type Pattern = [u32; PATTERN_SIZE];

const DATA_LPM_PATTERN: Pattern = array_const_fn_init![const_data_lpm_pattern; 80];
const fn const_data_lpm_pattern(i: usize) -> u32 {
    let lvl = i / LEVEL_SIZE;
    let pos = i % LEVEL_SIZE;
    let mut result: u32 = 1;
    let mut j: u32 = 1;
    while j <= lvl as u32 {
        let field_offset = (1usize << j) - 1;
        let top_j = pos >> (K - 1 - j);
        result |= 1u32 << (top_j + field_offset);
        j += 1;
    }
    result
}

const DATA_COVER_PATTERN: Pattern = array_const_fn_init![const_data_cover_pattern; 80];
const fn const_data_cover_pattern(i: usize) -> u32 {
    let i = i as u32;
    let lvl = i / LEVEL_SIZE as u32;
    let pos = i % LEVEL_SIZE as u32;

    if lvl == 0 {
        return (1u32 << ((1usize << K) - 1)) - 1;
    }

    // Which node at this level does `pos` belong to?
    let node = pos >> (K - 1 - lvl);

    let mut result: u32 = 0;
    let mut j = lvl;
    while j < K {
        let field_offset = (1usize << j) - 1;
        let start = node << (j - lvl);
        let block_size = 1usize << (j - lvl);
        let mask = ((1u32 << block_size) - 1) << start;
        result |= mask << field_offset;
        j += 1;
    }

    result
}

const CHILD_COVER_PATTERN: Pattern = array_const_fn_init![const_child_cover_pattern; 80];
const fn const_child_cover_pattern(i: usize) -> u32 {
    let k = K as usize;
    let lvl = i / LEVEL_SIZE;
    let pos = i % LEVEL_SIZE;

    let node = pos >> (k - 1 - lvl);
    let block_size = k - lvl; // log2 of actual block size
    let start = node << block_size;
    let mask = ((1usize << (1usize << block_size)) - 1) << start;

    mask as u32
}

#[inline(always)]
fn get_mask<R: Key>(depth: u32, key: R, prefix_len: u32, patterns: &Pattern) -> u32 {
    // prefix length must be at least as large as the current depth of the node.
    debug_assert!(prefix_len >= depth);
    let num_bits = R::zero().count_zeros();
    // Extract the (K-1)-bit block starting at `depth` (from MSB).  When `depth + K > num_bits + 1`
    // the window extends past the key width, so we left-shift to avoid u32 underflow.
    let mask = R::one().unsigned_shl(K - 1).sub(R::one());
    let pattern = if depth + K <= num_bits + 1 {
        key.unsigned_shr(num_bits - depth - K + 1).bitand(mask)
    } else {
        key.unsigned_shl(depth + K - num_bits - 1).bitand(mask)
    }
    .to_usize()
    .unwrap();
    let max_level = (prefix_len - depth).min(K - 1) as usize;
    let pattern_idx = pattern + max_level * (1 << (K - 1));
    // SAFETY: pattern_idx is always within the max pattern, because we mask the pattern (and then offset it).
    debug_assert!(pattern_idx < patterns.len());
    unsafe { *patterns.get_unchecked(pattern_idx) }
}

#[inline(always)]
pub(crate) fn data_lpm_mask<R: Key>(depth: u32, key: R, prefix_len: u32) -> u32 {
    get_mask(depth, key, prefix_len, &DATA_LPM_PATTERN)
}

#[inline(always)]
pub(crate) fn data_cover_mask<R: Key>(depth: u32, key: R, prefix_len: u32) -> u32 {
    get_mask(depth, key, prefix_len, &DATA_COVER_PATTERN)
}

#[inline(always)]
pub(crate) fn child_cover_mask<R: Key>(depth: u32, key: R, prefix_len: u32) -> u32 {
    get_mask(depth, key, prefix_len, &CHILD_COVER_PATTERN)
}

/// Given a data bit `b`, return the mask of all data bits in the same node covered by it.
///
/// This includes `b` itself and all its heap descendants (`2b+1`, `2b+2`, etc.).
/// Equivalent to `data_cover_mask(depth, key_for_b, depth + level_b)` but without
/// requiring key reconstruction.
#[inline(always)]
pub(crate) fn data_cover_mask_for_bit(b: u32) -> u32 {
    let (offset, level) = DATA_BIT_TO_PREFIX[b as usize];
    DATA_COVER_PATTERN[offset as usize + level as usize * LEVEL_SIZE]
}

/// Given a data bit `b`, return the mask of child bits covered by it.
///
/// Equivalent to `child_cover_mask(depth, key_for_b, depth + level_b)` but without
/// requiring key reconstruction.
#[inline(always)]
pub(crate) fn child_cover_mask_for_bit(b: u32) -> u32 {
    let (offset, level) = DATA_BIT_TO_PREFIX[b as usize];
    CHILD_COVER_PATTERN[offset as usize + level as usize * LEVEL_SIZE]
}

#[inline(always)]
pub(crate) fn child_bit<R: Key>(depth: u32, key: R) -> u32 {
    let mask = R::one().unsigned_shl(K).sub(R::one());
    let pattern = key
        .unsigned_shr(R::zero().count_zeros() - depth - K)
        .bitand(mask);
    pattern.to_u32().unwrap()
}

#[derive(Clone, Copy)]
pub(crate) enum LexIterElem<R> {
    Data(DataIdx),
    Child(Loc, u32, R),
}

#[repr(transparent)]
#[derive(Debug, Clone, Copy)]
struct LexElem(u8);

const CHILD_FLAG: u8 = 1 << 7;

impl LexElem {
    const fn data(x: u8) -> Self {
        Self(x)
    }

    const fn child(x: u8) -> Self {
        Self(x + CHILD_FLAG)
    }
    /// Decodes the element.
    /// Returns `Ok(data_bit)` if this is a data slot, or `Err(child_bit)` if this is a child slot.
    const fn decode(self) -> Result<u32, u32> {
        if self.0 >= CHILD_FLAG {
            Err((self.0 - CHILD_FLAG) as u32)
        } else {
            Ok(self.0 as u32)
        }
    }
}

const LEX_ORDER: [LexElem; NUM_DATA + NUM_CHILDREN] = array_const_fn_init![const_lex_iter; 63];
const fn const_lex_iter(i: usize) -> LexElem {
    let mut i = i as u32;
    let mut node: u32 = 0;
    let mut depth: u32 = 0;

    loop {
        if depth == K {
            return LexElem::child((node - ((1 << K) - 1)) as u8);
        }

        // This position in the traversal is the current node
        if i == 0 {
            return LexElem::data(node as u8);
        }

        // Skip current node, descend into children
        i -= 1;

        // Size of each child's subtree in the traversal
        let child_subtree_size = (1 << (K - depth)) - 1;

        if i < child_subtree_size {
            // Go left
            node = 2 * node + 1;
        } else {
            // Skip left subtree, go right
            i -= child_subtree_size;
            node = 2 * node + 2;
        }
        depth += 1;
    }
}

#[derive(Clone)]
pub(crate) struct MaskedLexIter<R> {
    iter: std::slice::Iter<'static, LexElem>,
    loc: Loc,
    depth: u32,
    key: R,
    // Original (unmasked) node: kept for correct POPCNT slot computation.
    node: MultiBitNode,
    // Separate filter fields: apply_*_mask modifies these, not the node bitmaps.
    data_filter: u32,
    child_filter: u32,
}

impl<R: Key> Default for MaskedLexIter<R> {
    fn default() -> Self {
        Self {
            iter: Default::default(),
            loc: Loc::root(),
            depth: Default::default(),
            key: R::zero(),
            node: Default::default(),
            data_filter: u32::MAX,
            child_filter: u32::MAX,
        }
    }
}

impl<R> MaskedLexIter<R> {
    pub(crate) fn new(loc: Loc, depth: u32, key: R, node: MultiBitNode) -> Self {
        Self {
            iter: LEX_ORDER.iter(),
            loc,
            depth,
            key,
            node,
            data_filter: u32::MAX,
            child_filter: u32::MAX,
        }
    }

    pub(crate) fn key(&self) -> &R {
        &self.key
    }

    pub(crate) fn apply_data_mask(&mut self, mask: u32) {
        // Only reduce the set of offsets to yield; keep node.data_bitmap intact for POPCNT.
        self.data_filter &= mask;
    }

    pub(crate) fn apply_child_mask(&mut self, mask: u32) {
        self.child_filter &= mask;
    }
}

impl<R: Key> Iterator for MaskedLexIter<R> {
    type Item = LexIterElem<R>;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            let next = *self.iter.next()?;
            match next.decode() {
                Ok(data_bit) => {
                    // Check original bitmap (for existence) AND filter (for masking).
                    if self.node.has_data_bit(data_bit) && (self.data_filter & (1 << data_bit)) != 0
                    {
                        return Some(LexIterElem::Data(DataIdx {
                            node: self.loc,
                            bit: data_bit,
                            depth: self.depth,
                        }));
                    }
                }
                Err(child_bit) => {
                    if self.node.has_child_bit(child_bit)
                        && (self.child_filter & (1 << child_bit)) != 0
                    {
                        return Some(LexIterElem::Child(
                            Loc::new(self.node.children_idx, child_bit, self.node.child_bitmap),
                            self.depth + K,
                            extend_repr(self.key, self.depth, child_bit),
                        ));
                    }
                }
            }
        }
    }
}

/// Iterate over all data and child slot positions in lexicographic (DFS pre-order) order.
/// Yields `Ok(data_bit)` for data positions and `Err(child_bit)` for child positions.
/// Supports reverse iteration, which is needed to push stack entries in reverse lex order.
pub(crate) fn lex_order() -> impl DoubleEndedIterator<Item = Result<u32, u32>> {
    LEX_ORDER.iter().copied().map(LexElem::decode)
}

pub(crate) fn extend_repr<R: Key>(key: R, depth: u32, offset: u32) -> R {
    let mask = mask_from_prefix_len(depth as u8);
    let root = key & mask;

    let offset = <R as num_traits::cast::NumCast>::from(offset).unwrap();
    let offset_bits = K;
    let total_width = R::zero().count_zeros();
    let shifted_offset = if total_width > depth + offset_bits {
        offset << (total_width - (depth + offset_bits)) as usize
    } else {
        offset >> (depth + offset_bits - total_width) as usize
    };

    root | shifted_offset
}

#[rustfmt::skip]
pub(crate) const DATA_BIT_TO_PREFIX: [(u8, u8); NUM_DATA] = [
    (0b0000, 0),
    (0b0000, 1),                                     (0b1000, 1),
    (0b0000, 2), (0b0100, 2),                         (0b1000, 2),             (0b1100, 2),
    (0b0000, 3), (0b0010, 3), (0b0100, 3), (0b0110, 3), (0b1000, 3), (0b1010, 3), (0b1100, 3), (0b1110, 3),
    (0b0000, 4), (0b0001, 4), (0b0010, 4), (0b0011, 4), (0b0100, 4), (0b0101, 4), (0b0110, 4), (0b0111, 4), (0b1000, 4), (0b1001, 4), (0b1010, 4), (0b1011, 4), (0b1100, 4), (0b1101, 4), (0b1110, 4), (0b1111, 4),
];

#[cfg(test)]
mod test {
    // all these are to visually align the content.
    #![allow(
        clippy::zero_prefixed_literal,
        clippy::unusual_byte_groupings,
        clippy::identity_op
    )]

    use super::*;
    #[test]
    fn internal_idx_test() {
        // Test cases updated for K=5
        let test_cases: [(u32, u32, u32); _] = [
            // prefix_len 0: level=0, offset=0, max_idx=0
            (0xdeadbeef, 0, 0),
            (0xffffffff, 0, 0),
            (0x00000000, 0, 0),
            // prefix_len 1-4: level=1-4, offset=1,3,7,15
            (0x7eadbeef, 1, 1),
            (0xaeadbeef, 1, 2),
            (0x3eadbeef, 2, 3),
            (0x4eadbeef, 2, 4),
            (0xbeadbeef, 2, 5),
            (0xfeadbeef, 2, 6),
            (0x0eadbeef, 3, 7),
            (0x2eadbeef, 3, 8),
            (0x4eadbeef, 3, 9),
            (0x6eadbeef, 3, 10),
            (0x8eadbeef, 3, 11),
            (0xaeadbeef, 3, 12),
            (0xceadbeef, 3, 13),
            (0xeeadbeef, 3, 14),
            // prefix_len 4: level=4, offset=15
            (0xddadbeef, 4, 28),
            // prefix_len 5+: moves to next tier
            (0xd7adbeef, 5, 0),
            (0xdaadbeef, 5, 0),
            (0xd3adbeef, 6, 1),
            (0xd4adbeef, 6, 2),
            (0xdbadbeef, 6, 1),
            (0xdfadbeef, 6, 2),
            (0xd0adbeef, 7, 3),
            (0xd2adbeef, 7, 4),
            (0xd4adbeef, 7, 5),
            (0xd6adbeef, 7, 6),
            (0xd8adbeef, 7, 3),
            (0xdaadbeef, 7, 4),
        ];
        for (key, prefix_len, want) in test_cases {
            assert_eq!(
                data_bit::<u32>(key, prefix_len),
                want,
                "data_bit mismatch for key={:#x}, prefix_len={}",
                key,
                prefix_len
            )
        }
    }

    #[test]
    fn lpm_pattern() {
        assert_eq!(DATA_LPM_PATTERN[0 + 00], 0b1);
        assert_eq!(DATA_LPM_PATTERN[0 + 08], 0b1);
        assert_eq!(DATA_LPM_PATTERN[0 + 15], 0b1);

        assert_eq!(DATA_LPM_PATTERN[16 + 00], 0b01_1);
        assert_eq!(DATA_LPM_PATTERN[16 + 07], 0b01_1);
        assert_eq!(DATA_LPM_PATTERN[16 + 08], 0b10_1);
        assert_eq!(DATA_LPM_PATTERN[16 + 15], 0b10_1);

        assert_eq!(DATA_LPM_PATTERN[32 + 00], 0b0001_01_1);
        assert_eq!(DATA_LPM_PATTERN[32 + 03], 0b0001_01_1);
        assert_eq!(DATA_LPM_PATTERN[32 + 04], 0b0010_01_1);
        assert_eq!(DATA_LPM_PATTERN[32 + 07], 0b0010_01_1);
        assert_eq!(DATA_LPM_PATTERN[32 + 08], 0b0100_10_1);
        assert_eq!(DATA_LPM_PATTERN[32 + 11], 0b0100_10_1);
        assert_eq!(DATA_LPM_PATTERN[32 + 12], 0b1000_10_1);
        assert_eq!(DATA_LPM_PATTERN[32 + 15], 0b1000_10_1);

        assert_eq!(DATA_LPM_PATTERN[48 + 00], 0b00000001_0001_01_1);
        assert_eq!(DATA_LPM_PATTERN[48 + 03], 0b00000010_0001_01_1);
        assert_eq!(DATA_LPM_PATTERN[48 + 04], 0b00000100_0010_01_1);
        assert_eq!(DATA_LPM_PATTERN[48 + 07], 0b00001000_0010_01_1);
        assert_eq!(DATA_LPM_PATTERN[48 + 08], 0b00010000_0100_10_1);
        assert_eq!(DATA_LPM_PATTERN[48 + 11], 0b00100000_0100_10_1);
        assert_eq!(DATA_LPM_PATTERN[48 + 12], 0b01000000_1000_10_1);
        assert_eq!(DATA_LPM_PATTERN[48 + 15], 0b10000000_1000_10_1);

        assert_eq!(
            DATA_LPM_PATTERN[64 + 00],
            0b0000000000000001_00000001_0001_01_1
        );
        assert_eq!(
            DATA_LPM_PATTERN[64 + 03],
            0b0000000000001000_00000010_0001_01_1
        );
        assert_eq!(
            DATA_LPM_PATTERN[64 + 04],
            0b0000000000010000_00000100_0010_01_1
        );
        assert_eq!(
            DATA_LPM_PATTERN[64 + 07],
            0b0000000010000000_00001000_0010_01_1
        );
        assert_eq!(
            DATA_LPM_PATTERN[64 + 08],
            0b0000000100000000_00010000_0100_10_1
        );
        assert_eq!(
            DATA_LPM_PATTERN[64 + 11],
            0b0000100000000000_00100000_0100_10_1
        );
        assert_eq!(
            DATA_LPM_PATTERN[64 + 12],
            0b0001000000000000_01000000_1000_10_1
        );
        assert_eq!(
            DATA_LPM_PATTERN[64 + 15],
            0b1000000000000000_10000000_1000_10_1
        );
    }

    #[test]
    #[rustfmt::skip]
    fn internal_cover_pattern() {
        assert_eq!(DATA_COVER_PATTERN[0 + 00], 0b1111111111111111_11111111_1111_11_1);
        assert_eq!(DATA_COVER_PATTERN[0 + 08], 0b1111111111111111_11111111_1111_11_1);
        assert_eq!(DATA_COVER_PATTERN[0 + 15], 0b1111111111111111_11111111_1111_11_1);

        assert_eq!(DATA_COVER_PATTERN[16 + 00], 0b0000000011111111_00001111_0011_01_0);
        assert_eq!(DATA_COVER_PATTERN[16 + 07], 0b0000000011111111_00001111_0011_01_0);
        assert_eq!(DATA_COVER_PATTERN[16 + 08], 0b1111111100000000_11110000_1100_10_0);
        assert_eq!(DATA_COVER_PATTERN[16 + 15], 0b1111111100000000_11110000_1100_10_0);

        assert_eq!(DATA_COVER_PATTERN[32 + 00], 0b0000000000001111_00000011_0001_00_0);
        assert_eq!(DATA_COVER_PATTERN[32 + 03], 0b0000000000001111_00000011_0001_00_0);
        assert_eq!(DATA_COVER_PATTERN[32 + 04], 0b0000000011110000_00001100_0010_00_0);
        assert_eq!(DATA_COVER_PATTERN[32 + 07], 0b0000000011110000_00001100_0010_00_0);
        assert_eq!(DATA_COVER_PATTERN[32 + 08], 0b0000111100000000_00110000_0100_00_0);
        assert_eq!(DATA_COVER_PATTERN[32 + 11], 0b0000111100000000_00110000_0100_00_0);
        assert_eq!(DATA_COVER_PATTERN[32 + 12], 0b1111000000000000_11000000_1000_00_0);
        assert_eq!(DATA_COVER_PATTERN[32 + 15], 0b1111000000000000_11000000_1000_00_0);

        assert_eq!(DATA_COVER_PATTERN[48 + 00], 0b0000000000000011_00000001_0000_00_0);
        assert_eq!(DATA_COVER_PATTERN[48 + 03], 0b0000000000001100_00000010_0000_00_0);
        assert_eq!(DATA_COVER_PATTERN[48 + 04], 0b0000000000110000_00000100_0000_00_0);
        assert_eq!(DATA_COVER_PATTERN[48 + 07], 0b0000000011000000_00001000_0000_00_0);
        assert_eq!(DATA_COVER_PATTERN[48 + 08], 0b0000001100000000_00010000_0000_00_0);
        assert_eq!(DATA_COVER_PATTERN[48 + 11], 0b0000110000000000_00100000_0000_00_0);
        assert_eq!(DATA_COVER_PATTERN[48 + 12], 0b0011000000000000_01000000_0000_00_0);
        assert_eq!(DATA_COVER_PATTERN[48 + 15], 0b1100000000000000_10000000_0000_00_0);

        assert_eq!(DATA_COVER_PATTERN[64 + 00], 0b0000000000000001_00000000_0000_00_0);
        assert_eq!(DATA_COVER_PATTERN[64 + 03], 0b0000000000001000_00000000_0000_00_0);
        assert_eq!(DATA_COVER_PATTERN[64 + 04], 0b0000000000010000_00000000_0000_00_0);
        assert_eq!(DATA_COVER_PATTERN[64 + 07], 0b0000000010000000_00000000_0000_00_0);
        assert_eq!(DATA_COVER_PATTERN[64 + 08], 0b0000000100000000_00000000_0000_00_0);
        assert_eq!(DATA_COVER_PATTERN[64 + 11], 0b0000100000000000_00000000_0000_00_0);
        assert_eq!(DATA_COVER_PATTERN[64 + 12], 0b0001000000000000_00000000_0000_00_0);
        assert_eq!(DATA_COVER_PATTERN[64 + 15], 0b1000000000000000_00000000_0000_00_0);
    }

    #[test]
    #[rustfmt::skip]
    fn external_cover_pattern() {
        assert_eq!(CHILD_COVER_PATTERN[00 + 00], 0b11111111_11111111__11111111_11111111);
        assert_eq!(CHILD_COVER_PATTERN[00 + 08], 0b11111111_11111111__11111111_11111111);
        assert_eq!(CHILD_COVER_PATTERN[00 + 15], 0b11111111_11111111__11111111_11111111);

        assert_eq!(CHILD_COVER_PATTERN[16 + 00], 0b00000000_00000000__11111111_11111111);
        assert_eq!(CHILD_COVER_PATTERN[16 + 07], 0b00000000_00000000__11111111_11111111);
        assert_eq!(CHILD_COVER_PATTERN[16 + 08], 0b11111111_11111111__00000000_00000000);
        assert_eq!(CHILD_COVER_PATTERN[16 + 15], 0b11111111_11111111__00000000_00000000);

        assert_eq!(CHILD_COVER_PATTERN[32 + 00], 0b00000000_00000000__00000000_11111111);
        assert_eq!(CHILD_COVER_PATTERN[32 + 03], 0b00000000_00000000__00000000_11111111);
        assert_eq!(CHILD_COVER_PATTERN[32 + 04], 0b00000000_00000000__11111111_00000000);
        assert_eq!(CHILD_COVER_PATTERN[32 + 07], 0b00000000_00000000__11111111_00000000);
        assert_eq!(CHILD_COVER_PATTERN[32 + 08], 0b00000000_11111111__00000000_00000000);
        assert_eq!(CHILD_COVER_PATTERN[32 + 11], 0b00000000_11111111__00000000_00000000);
        assert_eq!(CHILD_COVER_PATTERN[32 + 12], 0b11111111_00000000__00000000_00000000);
        assert_eq!(CHILD_COVER_PATTERN[32 + 15], 0b11111111_00000000__00000000_00000000);

        assert_eq!(CHILD_COVER_PATTERN[48 + 00], 0b00000000_00000000__00000000_00001111);
        assert_eq!(CHILD_COVER_PATTERN[48 + 03], 0b00000000_00000000__00000000_11110000);
        assert_eq!(CHILD_COVER_PATTERN[48 + 04], 0b00000000_00000000__00001111_00000000);
        assert_eq!(CHILD_COVER_PATTERN[48 + 07], 0b00000000_00000000__11110000_00000000);
        assert_eq!(CHILD_COVER_PATTERN[48 + 08], 0b00000000_00001111__00000000_00000000);
        assert_eq!(CHILD_COVER_PATTERN[48 + 11], 0b00000000_11110000__00000000_00000000);
        assert_eq!(CHILD_COVER_PATTERN[48 + 12], 0b00001111_00000000__00000000_00000000);
        assert_eq!(CHILD_COVER_PATTERN[48 + 15], 0b11110000_00000000__00000000_00000000);

        assert_eq!(CHILD_COVER_PATTERN[64 + 00], 0b00000000_00000000__00000000_00000011);
        assert_eq!(CHILD_COVER_PATTERN[64 + 03], 0b00000000_00000000__00000000_11000000);
        assert_eq!(CHILD_COVER_PATTERN[64 + 04], 0b00000000_00000000__00000011_00000000);
        assert_eq!(CHILD_COVER_PATTERN[64 + 07], 0b00000000_00000000__11000000_00000000);
        assert_eq!(CHILD_COVER_PATTERN[64 + 08], 0b00000000_00000011__00000000_00000000);
        assert_eq!(CHILD_COVER_PATTERN[64 + 11], 0b00000000_11000000__00000000_00000000);
        assert_eq!(CHILD_COVER_PATTERN[64 + 12], 0b00000011_00000000__00000000_00000000);
        assert_eq!(CHILD_COVER_PATTERN[64 + 15], 0b11000000_00000000__00000000_00000000);
    }
}