Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove redundant node depth mark #9

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/routing_table/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl<V> Ipv4CidrRoutingTable<V> {
#[inline]
pub fn new() -> Self {
Self {
bitmap: TreeBitmap::new(Ipv4Cidr::MAX_BITS as usize),
bitmap: TreeBitmap::new(),
}
}

Expand Down Expand Up @@ -206,7 +206,7 @@ impl<V> Ipv6CidrRoutingTable<V> {
#[inline]
pub fn new() -> Self {
Self {
bitmap: TreeBitmap::new(Ipv6Cidr::MAX_BITS as usize),
bitmap: TreeBitmap::new(),
}
}

Expand Down
56 changes: 18 additions & 38 deletions src/routing_table/tree_bitmap/bitmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,29 @@ where
K: Copy + Into<Nibbles<N>>,
{
root: NonNull<Node<(K, V)>>,
depth: usize,
}

impl<const N: usize, K, V> TreeBitmap<N, K, V>
where
K: Copy + Into<Nibbles<N>>,
{
/// Creates a new tree bitmap.
///
/// # Arguments
///
/// * `max_bits` - The maximum number of bits in the key. 32 for IPv4 and 128 for IPv6.
pub fn new(max_bits: usize) -> Self {
pub fn new() -> Self {
let root = Node::internal();
Self {
root,
depth: max_bits / 4,
}
Self { root }
}

pub fn list_matched<I>(&self, key: I) -> Vec<(K, &V)>
where
I: Into<Nibbles<N>>,
{
let mut node = self.root;
let mut depth = 0;
let mut rv = Vec::new();
let mut is_end = false;
for nibble in key.into() {
depth += 1;
let p = unsafe { node.as_ref() };
rv.extend(p.list_values(nibble));
debug_assert!((depth == self.depth) == p.is_end());
if nibble.bits < 4 || self.depth == depth {
if nibble.bits < 4 || p.is_end() {
is_end = true;
break;
}
Expand All @@ -69,13 +58,10 @@ where
I: Into<Nibbles<N>>,
{
let mut node = self.root;
let mut depth = 0;
for nibble in key.into() {
depth += 1;
let p = unsafe { node.as_ref() };

debug_assert!((depth == self.depth) == p.is_end());
if nibble.bits < 4 || depth == self.depth {
if nibble.bits < 4 || p.is_end() {
return p.get_exact_match_value(nibble).map(|(_, v)| v);
}
node = p.get_child(nibble.byte)?;
Expand All @@ -90,17 +76,15 @@ where
I: Into<Nibbles<N>>,
{
let mut node = self.root;
let mut depth = 0;
let mut rv = None;
for nibble in key.into() {
depth += 1;
let p = unsafe { node.as_ref() };

if let Some((k, v)) = p.get_longest_match_value(nibble) {
rv = Some((k, v));
}

if nibble.bits < 4 || depth == self.depth {
if nibble.bits < 4 || p.is_end() {
break;
}
node = if let Some(next) = p.get_child(nibble.byte) {
Expand All @@ -118,13 +102,10 @@ where

pub fn insert(&mut self, key: K, value: V) -> Option<V> {
let mut node = self.root;
let mut depth = 0;
for nibble in key.into() {
depth += 1;

for (i, nibble) in key.into().enumerate() {
let p = unsafe { node.as_mut() };

if nibble.bits < 4 || depth == self.depth {
if nibble.bits < 4 || p.is_end() {
let rv = p.remove_value(nibble).map(|(_, v)| v);
p.set_value(nibble, (key, value));
return rv;
Expand All @@ -135,7 +116,7 @@ where
continue;
}
// create new node
let next = if self.depth - depth == 1 {
let next = if (i + 1) * 4 == N * 8 {
Node::end()
} else {
Node::internal()
Expand All @@ -152,19 +133,18 @@ where

pub fn remove(&mut self, key: K) -> Option<V> {
let mut node = self.root;
let mut depth = 0;
for nibble in key.into() {
depth += 1;
let p = unsafe { node.as_mut() };
if nibble.bits == 4 && depth < self.depth {
if let Some(next) = p.get_child(nibble.byte) {
node = next;
} else {
return None;
}
} else {

if nibble.bits < 4 || p.is_end() {
return p.remove_value(nibble).map(|(_, v)| v);
}

if let Some(next) = p.get_child(nibble.byte) {
node = next;
} else {
return None;
}
}
let p = unsafe { node.as_mut() };
p.remove_value(Nibble::nil()).map(|(_, v)| v)
Expand Down Expand Up @@ -203,7 +183,7 @@ mod tests {
];

for ip in tests {
let mut map = TreeBitmap::new(32);
let mut map = TreeBitmap::new();

let k = ip.parse::<Ipv4Cidr>().unwrap();
let v = ip.to_owned();
Expand All @@ -214,7 +194,7 @@ mod tests {

#[test]
fn test_ipv4_tree_bitmap_list_matched() {
let mut map = TreeBitmap::new(32);
let mut map = TreeBitmap::new();
let cidrs = parse_ipv4_cidrs(&[
"192.168.122.1/32",
"192.168.122.0/31",
Expand Down
2 changes: 1 addition & 1 deletion src/routing_table/tree_bitmap/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn drop_node() {

#[test]
fn drop_tree_bitmap() {
let mut tree = TreeBitmap::new(32);
let mut tree = TreeBitmap::new();
let (d1, v1) = Dropped::new();
tree.insert(Ipv4Cidr::new([1, 1, 1, 1], 32).unwrap(), v1);

Expand Down