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
//! The ascending id-list algebra (LABS-803): union and intersection as
//! two-pointer merges over the ascending, deduplicated id lists every
//! serve and lookup produces — iterating the lists directly instead of
//! re-materializing them into a per-statement `HashSet<NodeId>`.
//!
//! Home: the lowest module both consumers import — this overlay's own
//! scratch merge (`seek.rs`) and the pattern-walk tier's candidate
//! folds (which sit above the overlay and read it downward), so the one
//! spelling serves both without an upward edge.
use std::cmp::Ordering;
use relate_core::NodeId;
/// The union of two ascending, deduplicated id lists — a two-pointer
/// merge whose answer keeps the same ascending, deduplicated contract,
/// so every later fold — and every membership probe, a binary search on
/// the sorted slice — stays correct.
pub(crate) fn union_ids(a: Vec<NodeId>, b: Vec<NodeId>) -> Vec<NodeId> {
let mut ids = Vec::with_capacity(a.len() + b.len());
let (mut left, mut right) = (0, 0);
while left < a.len() && right < b.len() {
match a[left].cmp(&b[right]) {
Ordering::Less => {
ids.push(a[left]);
left += 1;
}
Ordering::Greater => {
ids.push(b[right]);
right += 1;
}
Ordering::Equal => {
ids.push(a[left]);
left += 1;
right += 1;
}
}
}
ids.extend_from_slice(&a[left..]);
ids.extend_from_slice(&b[right..]);
ids
}
/// The intersection of two ascending, deduplicated id lists — the
/// two-pointer twin of [`union_ids`], answer ascending and deduplicated.
pub(crate) fn intersect_ids(a: Vec<NodeId>, b: Vec<NodeId>) -> Vec<NodeId> {
let mut ids = Vec::with_capacity(a.len().min(b.len()));
let (mut left, mut right) = (0, 0);
while left < a.len() && right < b.len() {
match a[left].cmp(&b[right]) {
Ordering::Less => left += 1,
Ordering::Greater => right += 1,
Ordering::Equal => {
ids.push(a[left]);
left += 1;
right += 1;
}
}
}
ids
}
#[cfg(test)]
mod tests {
use super::*;
/// Union merges two ascending lists without duplicating the shared
/// ids — the two-pointer algebra every OR composite and the overlay's
/// scratch merge read.
#[test]
fn union_merges_sorted_lists_without_duplicates() {
assert_eq!(
union_ids(vec![1, 3, 5, 7], vec![3, 4, 7, 9]),
vec![1, 3, 4, 5, 7, 9]
);
// One side subsuming the other, and an empty side, keep the answer
// the subsumed list alone would give.
assert_eq!(union_ids(vec![2, 4, 6], vec![4, 6]), vec![2, 4, 6]);
assert_eq!(union_ids(Vec::new(), vec![1, 2]), vec![1, 2]);
assert_eq!(union_ids(vec![1, 2], Vec::new()), vec![1, 2]);
}
/// Intersection keeps the shared ids of two ascending lists, ascending
/// — the two-pointer algebra every AND composite and the far probe's
/// fold read.
#[test]
fn intersection_keeps_the_shared_ids_ascending() {
assert_eq!(
intersect_ids(vec![1, 3, 5, 7], vec![3, 4, 7, 9]),
vec![3, 7]
);
// Disjoint lists intersect to nothing, an empty side to nothing.
assert_eq!(intersect_ids(vec![1, 2], vec![3, 4]), Vec::<NodeId>::new());
assert_eq!(intersect_ids(Vec::new(), vec![1, 2]), Vec::<NodeId>::new());
assert_eq!(intersect_ids(vec![1, 2], Vec::new()), Vec::<NodeId>::new());
}
}