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
//! Helper functions for common fuzzy matching patterns.
//!
//! This module provides optimized implementations for hierarchical scope
//! filtering and other value-based query patterns.
/// Checks if two sorted vectors have any common elements.
///
/// This uses a two-pointer scan with early termination, making it
/// optimal for hierarchical scope intersection checks.
///
/// # Performance
///
/// - Time complexity: O(n + m) worst case, O(1) best case with early termination
/// - Benchmarks show 4.7% faster than HashSet for typical scope sets
/// - Works with unlimited scope IDs
///
/// # Requirements
///
/// Both input slices must be sorted in ascending order. If they are not sorted,
/// the behavior is undefined (may return incorrect results).
///
/// # Examples
///
/// ```
/// use liblevenshtein::transducer::helpers::sorted_vec_intersection;
///
/// let term_scopes = vec![1, 5, 10, 15];
/// let visible_scopes = vec![0, 5, 12];
///
/// assert!(sorted_vec_intersection(&term_scopes, &visible_scopes)); // 5 is common
///
/// let no_overlap_a = vec![1, 3, 5];
/// let no_overlap_b = vec![2, 4, 6];
/// assert!(!sorted_vec_intersection(&no_overlap_a, &no_overlap_b));
/// ```
///
/// # Use Case: Code Completion with Hierarchical Scopes
///
/// ```ignore
/// // Note: This example requires the "pathmap-backend" feature
/// use liblevenshtein::prelude::*;
/// use liblevenshtein::transducer::helpers::sorted_vec_intersection;
///
/// // Create dictionary mapping terms to their visible scopes
/// let terms = vec![
/// ("global_var".to_string(), vec![0]), // Only in global scope
/// ("outer_var".to_string(), vec![0, 1]), // Global + outer
/// ("inner_var".to_string(), vec![0, 1, 2]), // All scopes
/// ];
/// let dict = PathMapDictionary::from_terms_with_values(terms);
/// let transducer = Transducer::new(dict, Algorithm::Standard);
///
/// // Query from within inner scope (visible scopes: {0, 1, 2})
/// let visible_scopes = vec![0, 1, 2];
/// let results: Vec<_> = transducer
/// .query_filtered("var", 1, |term_scopes| {
/// sorted_vec_intersection(term_scopes, &visible_scopes)
/// })
/// .map(|c| c.term)
/// .collect();
///
/// assert_eq!(results.len(), 3); // All three visible
/// ```
/// Checks if two bitmasks have any common bits set.
///
/// This is the fastest intersection check for scope IDs < 64.
///
/// # Performance
///
/// - Time complexity: O(1) - single bitwise AND operation
/// - Benchmarks show 7.9% faster than HashSet for small scope sets
/// - 3.4% faster than sorted vector intersection
///
/// # Limitations
///
/// Only works for scope IDs in range 0-63. Use [`sorted_vec_intersection`]
/// for larger scope IDs.
///
/// # Examples
///
/// ```
/// use liblevenshtein::transducer::helpers::bitmask_intersection;
///
/// let term_mask = 0b101010; // scopes {1, 3, 5}
/// let visible_mask = 0b001100; // scopes {2, 3}
///
/// assert!(bitmask_intersection(term_mask, visible_mask)); // 3 is common
///
/// let no_overlap_a = 0b000001; // scope {0}
/// let no_overlap_b = 0b000010; // scope {1}
/// assert!(!bitmask_intersection(no_overlap_a, no_overlap_b));
/// ```
///
/// # Creating Bitmasks from Scope Sets
///
/// ```
/// use std::collections::HashSet;
///
/// fn scopes_to_bitmask(scopes: &HashSet<u32>) -> u64 {
/// let mut mask = 0u64;
/// for &scope_id in scopes {
/// if scope_id < 64 {
/// mask |= 1u64 << scope_id;
/// }
/// }
/// mask
/// }
///
/// let mut scopes = HashSet::new();
/// scopes.insert(1);
/// scopes.insert(3);
/// scopes.insert(5);
///
/// let mask = scopes_to_bitmask(&scopes);
/// assert_eq!(mask, 0b101010);
/// ```
///
/// # Use Case: Fast Code Completion (≤64 scopes)
///
/// ```ignore
/// // Note: This example requires the "pathmap-backend" feature
/// use liblevenshtein::prelude::*;
/// use liblevenshtein::transducer::helpers::bitmask_intersection;
///
/// // Create dictionary with bitmask values
/// let terms = vec![
/// ("global_var".to_string(), 0b0001u64), // scope 0
/// ("outer_var".to_string(), 0b0011u64), // scopes 0, 1
/// ("inner_var".to_string(), 0b0111u64), // scopes 0, 1, 2
/// ];
/// let dict = PathMapDictionary::from_terms_with_values(terms);
/// let transducer = Transducer::new(dict, Algorithm::Standard);
///
/// // Query from within inner scope (bitmask for scopes {0, 1, 2})
/// let visible_mask = 0b0111u64;
/// let results: Vec<_> = transducer
/// .query_filtered("var", 1, |term_mask| {
/// bitmask_intersection(*term_mask, visible_mask)
/// })
/// .map(|c| c.term)
/// .collect();
///
/// assert_eq!(results.len(), 3); // All three visible
/// ```