Skip to content

Commit 97d7dfb

Browse files
committed
New method IpLookupTable::matches_mut
1 parent cdb7190 commit 97d7dfb

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,14 @@ where
236236
pub fn matches(&self, ip: A) -> impl Iterator<Item = (A, u32, &T)> {
237237
self.inner
238238
.matches(ip.nibbles().as_ref())
239-
.into_iter()
239+
.map(move |(bits_matched, value)| (ip.mask(bits_matched), bits_matched, value))
240+
}
241+
242+
/// Perform match lookup of `ip` and return the all matching
243+
/// prefixes, designated by ip, masklen, along with its mutable value.
244+
pub fn matches_mut(&mut self, ip: A) -> impl Iterator<Item = (A, u32, &mut T)> {
245+
self.inner
246+
.matches_mut(ip.nibbles().as_ref())
240247
.map(move |(bits_matched, value)| (ip.mask(bits_matched), bits_matched, value))
241248
}
242249

src/tree_bitmap/mod.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,11 @@ impl<T: Sized> TreeBitmap<T> {
167167
}
168168

169169
/// All matches lookup of ```nibbles```. Returns of Vec of tuples, each containing bits matched as u32 and a reference to T.
170-
pub fn matches(&self, nibbles: &[u8]) -> Vec<(u32, &T)> {
170+
fn matches_internal(&self, nibbles: &[u8]) -> Vec<(u32, AllocatorHandle, u32)> {
171171
let mut cur_hdl = self.root_handle();
172172
let mut cur_index = 0;
173173
let mut bits_searched = 0;
174-
let mut matches: Vec<(u32, &T)> = Vec::new();
174+
let mut matches = Vec::new();
175175

176176
for nibble in nibbles {
177177
let cur_node = *self.trienodes.get(&cur_hdl, cur_index);
@@ -183,7 +183,7 @@ impl<T: Sized> TreeBitmap<T> {
183183
cur_node.match_internal(bitmap)
184184
{
185185
let bits_matched = bits_searched + (i as u32);
186-
matches.push((bits_matched, self.results.get(&result_hdl, result_index)));
186+
matches.push((bits_matched, result_hdl, result_index));
187187
}
188188
}
189189

@@ -208,6 +208,21 @@ impl<T: Sized> TreeBitmap<T> {
208208
matches
209209
}
210210

211+
/// All matches lookup of ```nibbles```. Returns of Vec of tuples, each containing bits matched as u32 and a reference to T.
212+
pub fn matches(&self, nibbles: &[u8]) -> impl Iterator<Item = (u32, &T)> {
213+
self.matches_internal(nibbles).into_iter().map(
214+
move |(bits_matched, result_hdl, result_index)| {
215+
(bits_matched, self.results.get(&result_hdl, result_index))
216+
},
217+
)
218+
}
219+
220+
/// All matches lookup of ```nibbles```. Returns of Vec of tuples, each containing bits matched as u32 and a reference to T.
221+
pub fn matches_mut(&mut self, nibbles: &[u8]) -> MatchesMut<T> {
222+
let path = self.matches_internal(nibbles).into_iter();
223+
MatchesMut { inner: self, path }
224+
}
225+
211226
pub fn insert(&mut self, nibbles: &[u8], masklen: u32, value: T) -> Option<T> {
212227
let mut cur_hdl = self.root_handle();
213228
let mut cur_index = 0;
@@ -585,6 +600,26 @@ impl<T> Drop for IntoIter<T> {
585600
}
586601
}
587602

603+
pub struct MatchesMut<'a, T: 'a> {
604+
inner: &'a mut TreeBitmap<T>,
605+
path: std::vec::IntoIter<(u32, AllocatorHandle, u32)>,
606+
}
607+
608+
impl<'a, T: 'a> Iterator for MatchesMut<'a, T> {
609+
type Item = (u32, &'a mut T);
610+
611+
fn next(&mut self) -> Option<Self::Item> {
612+
match self.path.next() {
613+
Some((bits_matched, hdl, index)) => unsafe {
614+
let ptr: *mut T = self.inner.results.get_mut(&hdl, index);
615+
let val_ref = &mut *ptr;
616+
Some((bits_matched, val_ref))
617+
},
618+
None => None,
619+
}
620+
}
621+
}
622+
588623
impl<T> Drop for TreeBitmap<T> {
589624
fn drop(&mut self) {
590625
if self.should_drop {

0 commit comments

Comments
 (0)