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
//! Covering-union set-operation view.
//!
//! [`CoveringUnionView`] yields every prefix present in either input view, like
//! [`UnionView`]. For prefixes present on only one side, the yielded item
//! also carries the longest prefix match from the opposite view, when one exists inside that
//! opposite input view.

use std::marker::PhantomData;

use crate::{
    prefix::mask_from_prefix_len,
    Prefix,
    {
        node::{child_bit as node_child_bit, data_lpm_mask},
        AsView,
    },
};

use super::{
    reconstruct_prefix,
    union::{UnionItem, UnionView},
    TrieView, ViewIter,
};

/// The item type yielded by iterating a [`CoveringUnionView`].
///
/// Prefixes present in both views yield [`Both`][CoveringUnionItem::Both]. Prefixes present on
/// only one side yield that side's value plus the longest prefix match from the opposite side,
/// if such a prefix exists inside the opposite input view.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CoveringUnionItem<P, L, R> {
    /// Present only in the left view.
    Left {
        /// The exact value from the left view.
        left: L,
        /// The longest prefix match from the right view.
        right_lpm: Option<(P, R)>,
    },
    /// Present only in the right view.
    Right {
        /// The longest prefix match from the left view.
        left_lpm: Option<(P, L)>,
        /// The exact value from the right view.
        right: R,
    },
    /// Present in both views at the same prefix.
    Both {
        /// The exact value from the left view.
        left: L,
        /// The exact value from the right view.
        right: R,
    },
}

/// An immutable view over the covering union of two [`TrieView`]s.
///
/// Returned by [`TrieView::covering_union`]. The exact union traversal is delegated to
/// [`UnionView`]. In parallel, each side keeps a cloneable LPM cursor for the opposite side. That
/// cursor is advanced as child views are produced, so LPM context is maintained along the traversal
/// path rather than recomputed from the root for every yielded item.
pub struct CoveringUnionView<'a, L, R>
where
    L: TrieView<'a>,
    R: TrieView<'a, P = L::P>,
{
    exact: UnionView<'a, L, R>,
    left_lpm: LpmState<'a, L>,
    right_lpm: LpmState<'a, R>,
    _phantom: PhantomData<&'a ()>,
}

impl<'a, L, R> Clone for CoveringUnionView<'a, L, R>
where
    L: TrieView<'a> + Clone,
    L::P: Clone,
    L::T: Clone,
    R: TrieView<'a, P = L::P> + Clone,
    R::T: Clone,
{
    fn clone(&self) -> Self {
        Self {
            exact: self.exact.clone(),
            left_lpm: self.left_lpm.clone(),
            right_lpm: self.right_lpm.clone(),
            _phantom: PhantomData,
        }
    }
}

impl<'a, L, R> CoveringUnionView<'a, L, R>
where
    L: TrieView<'a> + Clone,
    R: TrieView<'a, P = L::P> + Clone,
{
    /// Construct a `CoveringUnionView` from two views.
    pub(crate) fn new(left: L, right: R) -> Self {
        Self {
            exact: UnionView::new(left.clone(), right.clone()),
            left_lpm: LpmState::new(left),
            right_lpm: LpmState::new(right),
            _phantom: PhantomData,
        }
    }
}

impl<'a, L, R> TrieView<'a> for CoveringUnionView<'a, L, R>
where
    L: TrieView<'a> + Clone,
    L::P: Clone + 'a,
    L::T: Clone,
    R: TrieView<'a, P = L::P> + Clone,
    R::T: Clone,
{
    type P = L::P;
    type T = CoveringUnionItem<L::P, L::T, R::T>;

    #[inline]
    fn depth(&self) -> u32 {
        self.exact.depth()
    }

    #[inline]
    fn key(&self) -> <L::P as Prefix>::R {
        self.exact.key()
    }

    #[inline]
    fn prefix_len(&self) -> u32 {
        self.exact.prefix_len()
    }

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

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

    #[inline]
    unsafe fn get_data(&mut self, data_bit: u32) -> Self::T {
        let prefix = reconstruct_prefix::<L::P>(self.exact.depth(), self.exact.key(), data_bit);
        match unsafe { self.exact.get_data(data_bit) } {
            UnionItem::Left(left) => CoveringUnionItem::Left {
                left,
                right_lpm: self.right_lpm.lpm_for_prefix(&prefix),
            },
            UnionItem::Right(right) => CoveringUnionItem::Right {
                left_lpm: self.left_lpm.lpm_for_prefix(&prefix),
                right,
            },
            UnionItem::Both(left, right) => CoveringUnionItem::Both { left, right },
        }
    }

    unsafe fn get_child(&mut self, child_bit: u32) -> Self {
        let exact = unsafe { self.exact.get_child(child_bit) };
        let prefix = exact.prefix();
        let mut left_lpm = self.left_lpm.clone();
        let mut right_lpm = self.right_lpm.clone();
        left_lpm.advance_to_prefix(&prefix);
        right_lpm.advance_to_prefix(&prefix);
        Self {
            exact,
            left_lpm,
            right_lpm,
            _phantom: PhantomData,
        }
    }

    unsafe fn reposition(&mut self, key: <L::P as Prefix>::R, prefix_len: u32) {
        unsafe { self.exact.reposition(key, prefix_len) };
        let prefix = self.exact.prefix();
        self.left_lpm.advance_to_prefix(&prefix);
        self.right_lpm.advance_to_prefix(&prefix);
    }
}

impl<'a, L, R> IntoIterator for CoveringUnionView<'a, L, R>
where
    L: TrieView<'a> + Clone,
    L::P: Clone + 'a,
    L::T: Clone,
    R: TrieView<'a, P = L::P> + Clone,
    R::T: Clone,
{
    type Item = (L::P, CoveringUnionItem<L::P, L::T, R::T>);
    type IntoIter = ViewIter<'a, CoveringUnionView<'a, L, R>>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl<'a, L, R> AsView<'a> for CoveringUnionView<'a, L, R>
where
    L: TrieView<'a> + Clone,
    L::P: Clone + 'a,
    L::T: Clone,
    R: TrieView<'a, P = L::P> + Clone,
    R::T: Clone,
{
    type P = L::P;
    type View = Self;

    fn view(self) -> Self {
        self
    }
}

struct LpmState<'a, V>
where
    V: TrieView<'a>,
{
    lpm_view: Option<V>,
    best: Option<(V::P, V::T)>,
}

impl<'a, V> Clone for LpmState<'a, V>
where
    V: TrieView<'a> + Clone,
    V::P: Clone,
    V::T: Clone,
{
    fn clone(&self) -> Self {
        Self {
            lpm_view: self.lpm_view.clone(),
            best: self.best.clone(),
        }
    }
}

impl<'a, V> LpmState<'a, V>
where
    V: TrieView<'a>,
{
    fn new(view: V) -> Self {
        Self {
            lpm_view: Some(view),
            best: None,
        }
    }
}

impl<'a, V> LpmState<'a, V>
where
    V: TrieView<'a> + Clone,
    V::P: Clone,
    V::T: Clone,
{
    fn advance_to_prefix(&mut self, prefix: &V::P) {
        let target_key = prefix.mask();
        let target_len = prefix.prefix_len() as u32;
        let Some(view) = self.lpm_view.take() else {
            return;
        };
        self.lpm_view = self.advance_view(view, target_key, target_len);
    }

    fn lpm_for_prefix(&self, prefix: &V::P) -> Option<(V::P, V::T)> {
        let target_key = prefix.mask();
        let target_len = prefix.prefix_len() as u32;
        let mut best = self
            .best
            .as_ref()
            .filter(|(best_prefix, _)| best_prefix.contains(prefix))
            .cloned();

        if let Some(view) = self.lpm_view.as_ref() {
            if let Some(candidate) = lpm_in_view(view, target_key, target_len) {
                remember_best(&mut best, candidate);
            }
        }

        best
    }

    fn advance_view(
        &mut self,
        mut view: V,
        target_key: <V::P as Prefix>::R,
        target_len: u32,
    ) -> Option<V> {
        if !same_path::<V::P>(view.key(), view.prefix_len(), target_key, target_len) {
            return None;
        }

        if view.prefix_len() > target_len {
            return Some(view);
        }

        loop {
            if let Some(candidate) = lpm_in_view(&view, target_key, target_len) {
                remember_best(&mut self.best, candidate);
            }

            if target_len < view.depth() + crate::table::K {
                return Some(view);
            }

            let child_bit = node_child_bit(view.depth(), target_key);
            if (view.child_bitmap() >> child_bit) & 1 == 0 {
                return None;
            }

            view = unsafe { view.get_child(child_bit) };
        }
    }
}

fn lpm_in_view<'a, V>(
    view: &V,
    target_key: <V::P as Prefix>::R,
    target_len: u32,
) -> Option<(V::P, V::T)>
where
    V: TrieView<'a> + Clone,
{
    if view.prefix_len() > target_len {
        return None;
    }
    if !same_path::<V::P>(view.key(), view.prefix_len(), target_key, target_len) {
        return None;
    }

    let data_bits = view.data_bitmap() & data_lpm_mask(view.depth(), target_key, target_len);
    if data_bits == 0 {
        return None;
    }

    let data_bit = u32::BITS - 1 - data_bits.leading_zeros();
    let prefix = reconstruct_prefix::<V::P>(view.depth(), view.key(), data_bit);
    let mut cloned = view.clone();
    Some((prefix, unsafe { cloned.get_data(data_bit) }))
}

fn same_path<P: Prefix>(left_key: P::R, left_len: u32, right_key: P::R, right_len: u32) -> bool {
    let prefix_len = left_len.min(right_len);
    let mask = mask_from_prefix_len(prefix_len as u8);
    left_key & mask == right_key & mask
}

fn remember_best<P: Prefix, T>(best: &mut Option<(P, T)>, candidate: (P, T)) {
    if match best.as_ref() {
        None => true,
        Some((prefix, _)) => candidate.0.prefix_len() >= prefix.prefix_len(),
    } {
        *best = Some(candidate);
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        Prefix,
        {
            trieview::{AsView, TrieView},
            PrefixMap,
        },
    };

    use super::CoveringUnionItem;

    type P = (u32, u8);

    #[derive(Debug, PartialEq, Eq)]
    enum Got {
        Left(P, i32, Option<(P, i32)>),
        Right(P, Option<(P, i32)>, i32),
        Both(P, i32, i32),
    }

    fn p(repr: u32, len: u8) -> P {
        P::from_repr_len(repr, len)
    }

    fn map_from(entries: &[(u32, u8, i32)]) -> PrefixMap<P, i32> {
        let mut m = PrefixMap::new();
        for &(repr, len, val) in entries {
            m.insert(p(repr, len), val);
        }
        m
    }

    fn collect<'a>(
        iter: impl Iterator<Item = (P, CoveringUnionItem<P, &'a i32, &'a i32>)>,
    ) -> Vec<Got> {
        iter.map(|(prefix, item)| match item {
            CoveringUnionItem::Left { left, right_lpm } => {
                Got::Left(prefix, *left, right_lpm.map(|(p, r)| (p, *r)))
            }
            CoveringUnionItem::Right { left_lpm, right } => {
                Got::Right(prefix, left_lpm.map(|(p, l)| (p, *l)), *right)
            }
            CoveringUnionItem::Both { left, right } => Got::Both(prefix, *left, *right),
        })
        .collect()
    }

    #[test]
    fn covering_union_adds_opposite_lpm_context() {
        let a = map_from(&[(0x0a000000, 8, 1), (0x0a010000, 16, 2), (0x0b000000, 8, 3)]);
        let b = map_from(&[
            (0x0a000000, 8, 10),
            (0x0a010100, 24, 30),
            (0x0c000000, 8, 40),
        ]);

        let got = collect(a.view().covering_union(b.view()).into_iter());
        assert_eq!(
            got,
            vec![
                Got::Both(p(0x0a000000, 8), 1, 10),
                Got::Left(p(0x0a010000, 16), 2, Some((p(0x0a000000, 8), 10))),
                Got::Right(p(0x0a010100, 24), Some((p(0x0a010000, 16), 2)), 30),
                Got::Left(p(0x0b000000, 8), 3, None),
                Got::Right(p(0x0c000000, 8), None, 40),
            ]
        );
    }

    #[test]
    fn covering_union_lpm_context_is_limited_to_input_views() {
        let a = map_from(&[(0x0a000000, 8, 1), (0x0a010000, 16, 2), (0x0a010100, 24, 3)]);
        let b = map_from(&[(0x0a000000, 8, 10), (0x0a010200, 24, 20)]);
        let a_sub = a.view_at(&p(0x0a010000, 16)).unwrap();
        let b_sub = b.view_at(&p(0x0a010000, 16)).unwrap();

        let got = collect(a_sub.covering_union(b_sub).into_iter());
        assert_eq!(
            got,
            vec![
                Got::Left(p(0x0a010000, 16), 2, None),
                Got::Left(p(0x0a010100, 24), 3, None),
                Got::Right(p(0x0a010200, 24), Some((p(0x0a010000, 16), 2)), 20),
            ]
        );
    }

    #[test]
    fn covering_union_right_deeper_than_left_view() {
        let a = map_from(&[
            (0x09000000, 8, 9),
            (0x0a000000, 8, 1),
            (0x0a010000, 16, 2),
            (0x0a010100, 24, 3),
            (0x0b000000, 8, 11),
        ]);
        let b = map_from(&[(0x0a010000, 16, 20), (0x0a010200, 24, 30)]);
        let b_sub = b.view_at(&p(0x0a010000, 16)).unwrap();

        let got = collect(a.view().covering_union(b_sub).into_iter());
        assert_eq!(
            got,
            vec![
                Got::Left(p(0x09000000, 8), 9, None),
                Got::Left(p(0x0a000000, 8), 1, None),
                Got::Both(p(0x0a010000, 16), 2, 20),
                Got::Left(p(0x0a010100, 24), 3, Some((p(0x0a010000, 16), 20))),
                Got::Right(p(0x0a010200, 24), Some((p(0x0a010000, 16), 2)), 30),
                Got::Left(p(0x0b000000, 8), 11, None),
            ]
        );
    }

    #[test]
    fn covering_union_left_deep_right_shallow_keeps_best_after_child_missing() {
        let a = map_from(&[(0x0a010100, 24, 1), (0x0a010180, 25, 2)]);
        let b = map_from(&[(0x0a000000, 8, 10)]);
        let a_sub = a.view_at(&p(0x0a010100, 24)).unwrap();

        let got = collect(a_sub.covering_union(b.view()).into_iter());
        assert_eq!(
            got,
            vec![
                Got::Right(p(0x0a000000, 8), None, 10),
                Got::Left(p(0x0a010100, 24), 1, Some((p(0x0a000000, 8), 10))),
                Got::Left(p(0x0a010180, 25), 2, Some((p(0x0a000000, 8), 10))),
            ]
        );
    }

    #[test]
    fn covering_union_different_depth_views_prefer_most_specific_lpm() {
        let a = map_from(&[(0x0a000000, 8, 1), (0x0a010000, 16, 2), (0x0a010100, 24, 3)]);
        let b = map_from(&[
            (0x0a000000, 8, 10),
            (0x0a010000, 16, 20),
            (0x0a010180, 25, 30),
        ]);
        let a_sub = a.view_at(&p(0x0a010000, 16)).unwrap();

        let got = collect(a_sub.covering_union(b.view()).into_iter());
        assert_eq!(
            got,
            vec![
                Got::Right(p(0x0a000000, 8), None, 10),
                Got::Both(p(0x0a010000, 16), 2, 20),
                Got::Left(p(0x0a010100, 24), 3, Some((p(0x0a010000, 16), 20))),
                Got::Right(p(0x0a010180, 25), Some((p(0x0a010100, 24), 3)), 30),
            ]
        );
    }

    #[test]
    fn covering_union_find_lpm_value_on_composed_view() {
        let a = map_from(&[(0x0a000000, 8, 1), (0x0a010000, 16, 2)]);
        let b = map_from(&[(0x0a010100, 24, 30)]);

        let got = a
            .view()
            .covering_union(b.view())
            .find_lpm_value(&p(0x0a010180, 25))
            .map(|(prefix, item)| match item {
                CoveringUnionItem::Right { left_lpm, right } => {
                    (prefix, left_lpm.map(|(p, l)| (p, *l)), *right)
                }
                other => panic!("expected right-only item, got {other:?}"),
            });

        assert_eq!(
            got,
            Some((p(0x0a010100, 24), Some((p(0x0a010000, 16), 2)), 30))
        );
    }
}