JointPrefixMap

Struct JointPrefixMap 

Source
pub struct JointPrefixMap<P, T>
where P: JointPrefix,
{ pub t1: PrefixMap<P::P1, T>, pub t2: PrefixMap<P::P2, T>, }
Expand description

A Joint prefix map, implemented as two separate prefix trees.

Fields§

§t1: PrefixMap<P::P1, T>

PrefixMap that corresponds to the first prefix type

§t2: PrefixMap<P::P2, T>

PrefixMap that corresponds to the second prefix type

Implementations§

Source§

impl<P: JointPrefix, T> JointPrefixMap<P, T>

Source

pub fn new() -> Self

Create an empty prefix map

Source

pub fn len(&self) -> usize

Returns the number of elements stored in self.

let mut map: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::default();
map.insert("192.168.1.0/24".parse()?, 1u32);
map.insert("192.168.1.0/25".parse()?, 2u32);
map.insert("2001::1:0:0/96".parse()?, 3u32);
assert_eq!(map.len(), 3);
Source

pub fn is_empty(&self) -> bool

Returns true if the map contains no elements.

let mut map: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::new();
assert!(map.is_empty());
map.insert("2001::1:0:0/96".parse()?, 1u32);
assert!(!map.is_empty());
Source

pub fn get<'a>(&'a self, prefix: &P) -> Option<&'a T>

Get the value of an element by matching exactly on the prefix.

let mut pm: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::new();
pm.insert("192.168.1.0/24".parse()?, 1);
pm.insert("2001::1:0:0/96".parse()?, 2);
assert_eq!(pm.get(&"192.168.1.0/24".parse()?), Some(&1));
assert_eq!(pm.get(&"192.168.2.0/24".parse()?), None);
assert_eq!(pm.get(&"192.168.0.0/23".parse()?), None);
assert_eq!(pm.get(&"192.168.1.128/25".parse()?), None);
assert_eq!(pm.get(&"2001::1:0:0/96".parse()?), Some(&2));
assert_eq!(pm.get(&"0ca8:1::/24".parse()?), None);
Source

pub fn get_mut<'a>(&'a mut self, prefix: &P) -> Option<&'a mut T>

Get a mutable reference to a value of an element by matching exactly on the prefix.

let mut pm: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::new();
let prefix = "2001::/32".parse()?;
pm.insert(prefix, 1);
assert_eq!(pm.get(&prefix), Some(&1));
*pm.get_mut(&prefix).unwrap() += 1;
assert_eq!(pm.get(&prefix), Some(&2));
Source

pub fn get_key_value<'a>(&'a self, prefix: &P) -> Option<(P, &'a T)>

Get the value of an element by matching exactly on the prefix. Notice, that the returned prefix may differ from the one provided in the host-part of the address.

let mut pm: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::new();
let prefix = "2001::/32".parse()?;
pm.insert(prefix, 1);
assert_eq!(pm.get_key_value(&prefix), Some((prefix, &1)));
Source

pub fn get_lpm<'a>(&'a self, prefix: &P) -> Option<(P, &'a T)>

Get a value of an element by using longest prefix matching

let mut pm: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::new();
pm.insert("192.168.1.0/24".parse()?, 1);
pm.insert("192.168.0.0/23".parse()?, 2);
assert_eq!(pm.get_lpm(&"192.168.1.1/32".parse()?), Some(("192.168.1.0/24".parse()?, &1)));
assert_eq!(pm.get_lpm(&"192.168.1.0/24".parse()?), Some(("192.168.1.0/24".parse()?, &1)));
assert_eq!(pm.get_lpm(&"192.168.0.0/24".parse()?), Some(("192.168.0.0/23".parse()?, &2)));
assert_eq!(pm.get_lpm(&"192.168.2.0/24".parse()?), None);
Source

pub fn get_lpm_mut<'a>(&'a mut self, prefix: &P) -> Option<(P, &'a mut T)>

Get a mutable reference to a value of an element by using longest prefix matching

let mut pm: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::new();
pm.insert("192.168.1.0/24".parse()?, 1);
pm.insert("192.168.0.0/23".parse()?, 2);
assert_eq!(pm.get_lpm(&"192.168.1.1/32".parse()?), Some(("192.168.1.0/24".parse()?, &1)));
*pm.get_lpm_mut(&"192.168.1.64/26".parse()?).unwrap().1 += 1;
assert_eq!(pm.get_lpm(&"192.168.1.1/32".parse()?), Some(("192.168.1.0/24".parse()?, &2)));
Source

pub fn contains_key(&self, prefix: &P) -> bool

Check if a key is present in the datastructure.

let mut pm: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::new();
pm.insert("192.168.1.0/24".parse()?, 1);
assert!(pm.contains_key(&"192.168.1.0/24".parse()?));
assert!(!pm.contains_key(&"192.168.2.0/24".parse()?));
assert!(!pm.contains_key(&"192.168.0.0/23".parse()?));
assert!(!pm.contains_key(&"192.168.1.128/25".parse()?));
Source

pub fn get_lpm_prefix(&self, prefix: &P) -> Option<P>

Get the longest prefix in the datastructure that matches the given prefix.

let mut pm: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::new();
pm.insert("192.168.1.0/24".parse()?, 1);
pm.insert("192.168.0.0/23".parse()?, 2);
assert_eq!(pm.get_lpm_prefix(&"192.168.1.1/32".parse()?), Some("192.168.1.0/24".parse()?));
assert_eq!(pm.get_lpm_prefix(&"192.168.1.0/24".parse()?), Some("192.168.1.0/24".parse()?));
assert_eq!(pm.get_lpm_prefix(&"192.168.0.0/24".parse()?), Some("192.168.0.0/23".parse()?));
assert_eq!(pm.get_lpm_prefix(&"192.168.2.0/24".parse()?), None);
Source

pub fn get_spm<'a>(&'a self, prefix: &P) -> Option<(P, &'a T)>

Get a value of an element by using shortest prefix matching.

let mut pm: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::new();
pm.insert("192.168.1.0/24".parse()?, 1);
pm.insert("192.168.0.0/23".parse()?, 2);
assert_eq!(pm.get_spm(&"192.168.1.1/32".parse()?), Some(("192.168.0.0/23".parse()?, &2)));
assert_eq!(pm.get_spm(&"192.168.1.0/24".parse()?), Some(("192.168.0.0/23".parse()?, &2)));
assert_eq!(pm.get_spm(&"192.168.0.0/23".parse()?), Some(("192.168.0.0/23".parse()?, &2)));
assert_eq!(pm.get_spm(&"192.168.2.0/24".parse()?), None);
Source

pub fn get_spm_prefix(&self, prefix: &P) -> Option<P>

Get the shortest prefix in the datastructure that contains the given prefix.

let mut pm: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::new();
pm.insert("192.168.1.1/24".parse()?, 1);
pm.insert("192.168.0.0/23".parse()?, 2);
assert_eq!(pm.get_spm_prefix(&"192.168.1.1/32".parse()?), Some("192.168.0.0/23".parse()?));
assert_eq!(pm.get_spm_prefix(&"192.168.1.0/24".parse()?), Some("192.168.0.0/23".parse()?));
assert_eq!(pm.get_spm_prefix(&"192.168.0.0/23".parse()?), Some("192.168.0.0/23".parse()?));
assert_eq!(pm.get_spm_prefix(&"192.168.2.0/24".parse()?), None);
Source

pub fn insert(&mut self, prefix: P, value: T) -> Option<T>

Insert a new item into the prefix-map. This function may return any value that existed before.

In case the node already exists in the tree, its prefix will be replaced by the provided argument. This allows you to store additional information in the host part of the prefix.

let mut pm: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::new();
assert_eq!(pm.insert("192.168.0.0/23".parse()?, 1), None);
assert_eq!(pm.insert("192.168.1.0/24".parse()?, 2), None);
assert_eq!(pm.insert("192.168.1.0/24".parse()?, 3), Some(2));
assert_eq!(pm.insert("2001::1:0:0/96".parse()?, 4), None);
assert_eq!(pm.insert("2001::1:0:0/97".parse()?, 5), None);
assert_eq!(pm.insert("2001::1:0:0/97".parse()?, 6), Some(5));
Source

pub fn entry(&mut self, prefix: P) -> Entry<'_, P, T>

Gets the given key’s corresponding entry in the map for in-place manipulation. In case you eventually insert an element into the map, this operation will also replace the prefix in the node with the existing one. That is if you store additional information in the host part of the address (the one that is masked out).

let mut pm: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::new();
pm.insert("192.168.1.0/24".parse()?, vec![1]);
pm.entry("192.168.1.0/24".parse()?).or_default().push(2);
pm.entry("192.168.1.0/25".parse()?).or_default().push(3);
pm.entry("c0a8:1::/24".parse()?).or_default().push(4);
assert_eq!(pm.get(&"192.168.1.0/24".parse()?), Some(&vec![1, 2]));
assert_eq!(pm.get(&"192.168.1.0/25".parse()?), Some(&vec![3]));
assert_eq!(pm.get(&"c0a8:1::/24".parse()?), Some(&vec![4]));
Source

pub fn remove(&mut self, prefix: &P) -> Option<T>

Removes a key from the map, returning the value at the key if the key was previously in the map. In contrast to Self::remove_keep_tree, this operation will modify the tree structure. As a result, this operation takes longer than remove_keep_tree, as does inserting the same element again. However, future reads may be faster as less nodes need to be traversed. Further, it reduces the memory footprint to its minimum.

let mut pm: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::new();
let prefix = "192.168.1.0/24".parse()?;
pm.insert(prefix, 1);
assert_eq!(pm.get(&prefix), Some(&1));
assert_eq!(pm.remove(&prefix), Some(1));
assert_eq!(pm.get(&prefix), None);
Source

pub fn remove_keep_tree(&mut self, prefix: &P) -> Option<T>

Removes a key from the map, returning the value at the key if the key was previously in the map. In contrast to Self::remove, his operation will keep the tree structure as is, but only remove the element from it. This allows any future insert on the same prefix to be faster. However future reads from the tree might be a bit slower because they need to traverse more nodes.

let mut pm: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::new();
let prefix = "192.168.1.0/24".parse()?;
pm.insert(prefix, 1);
assert_eq!(pm.get(&prefix), Some(&1));
assert_eq!(pm.remove_keep_tree(&prefix), Some(1));
assert_eq!(pm.get(&prefix), None);

// future inserts of the same key are now faster!
pm.insert(prefix, 1);
Source

pub fn remove_children(&mut self, prefix: &P)

Remove all entries that are contained within prefix. This will change the tree structure. This operation is O(n), as the entries must be freed up one-by-one.

let mut pm: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::new();
pm.insert("192.168.0.0/22".parse()?, 1);
pm.insert("192.168.0.0/23".parse()?, 2);
pm.insert("192.168.0.0/24".parse()?, 3);
pm.insert("192.168.2.0/23".parse()?, 4);
pm.insert("192.168.2.0/24".parse()?, 5);
pm.remove_children(&"192.168.0.0/23".parse()?);
assert_eq!(pm.get(&"192.168.0.0/23".parse()?), None);
assert_eq!(pm.get(&"192.168.0.0/24".parse()?), None);
assert_eq!(pm.get(&"192.168.2.0/23".parse()?), Some(&4));
assert_eq!(pm.get(&"192.168.2.0/24".parse()?), Some(&5));
Source

pub fn clear(&mut self)

Clear the map but keep the allocated memory.

let mut pm: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::new();
pm.insert("192.168.0.0/24".parse()?, 1);
pm.insert("192.168.1.0/24".parse()?, 2);
pm.insert("2001::1:0:0/96".parse()?, 3);
pm.clear();
assert_eq!(pm.get(&"192.168.0.0/24".parse()?), None);
assert_eq!(pm.get(&"192.168.1.0/24".parse()?), None);
assert_eq!(pm.get(&"2001::1:0:0/96".parse()?), None);
Source

pub fn retain<F>(&mut self, f: F)
where F: FnMut(P, &T) -> bool,

Keep only the elements in the map that satisfy the given condition f.

let mut pm: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::new();
pm.insert("192.168.0.0/24".parse()?, 1);
pm.insert("192.168.1.0/24".parse()?, 2);
pm.insert("192.168.2.0/24".parse()?, 3);
pm.insert("192.168.2.0/25".parse()?, 4);
pm.insert("2001::1:0:0/24".parse()?, 5);
pm.insert("2001::1:0:0/25".parse()?, 6);
pm.retain(|_, t| *t % 2 == 0); // only keep the even values.
assert_eq!(pm.get(&"192.168.0.0/24".parse()?), None);
assert_eq!(pm.get(&"192.168.1.0/24".parse()?), Some(&2));
assert_eq!(pm.get(&"192.168.2.0/24".parse()?), None);
assert_eq!(pm.get(&"192.168.2.0/25".parse()?), Some(&4));
assert_eq!(pm.get(&"2001::1:0:0/24".parse()?), None);
assert_eq!(pm.get(&"2001::1:0:0/25".parse()?), Some(&6));
Source

pub fn cover<'a, 'p>(&'a self, prefix: &'p P) -> Cover<'a, 'p, P, T>

Iterate over all entries in the map that covers the given prefix (including prefix itself if that is present in the map). The returned iterator yields (&'a P, &'a T).

The iterator will always yield elements ordered by their prefix length, i.e., their depth in the tree.

let mut pm: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::new();
let p0 = "10.0.0.0/8".parse()?;
let p1 = "10.1.0.0/16".parse()?;
let p2 = "10.1.1.0/24".parse()?;
pm.insert(p0, 0);
pm.insert(p1, 1);
pm.insert(p2, 2);
pm.insert("10.1.2.0/24".parse()?, 3); // disjoint prefixes are not covered
pm.insert("10.1.1.0/25".parse()?, 4); // more specific prefixes are not covered
pm.insert("11.0.0.0/8".parse()?, 5);  // Branch points that don't contain values are skipped
assert_eq!(
    pm.cover(&p2).collect::<Vec<_>>(),
    vec![(p0, &0), (p1, &1), (p2, &2)]
);

This function also yields the root note if it is part of the map:

let mut pm: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::new();
let root = "0.0.0.0/0".parse()?;
pm.insert(root, 0);
assert_eq!(pm.cover(&"10.0.0.0/8".parse()?).collect::<Vec<_>>(), vec![(root, &0)]);
Source

pub fn cover_keys<'a, 'p>(&'a self, prefix: &'p P) -> CoverKeys<'a, 'p, P, T>

Iterate over all keys (prefixes) in the map that covers the given prefix (including prefix itself if that is present in the map). The returned iterator yields &'a P.

The iterator will always yield elements ordered by their prefix length, i.e., their depth in the tree.

let mut pm: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::new();
let p0 = "10.0.0.0/8".parse()?;
let p1 = "10.1.0.0/16".parse()?;
let p2 = "10.1.1.0/24".parse()?;
pm.insert(p0, 0);
pm.insert(p1, 1);
pm.insert(p2, 2);
pm.insert("10.1.2.0/24".parse()?, 3); // disjoint prefixes are not covered
pm.insert("10.1.1.0/25".parse()?, 4); // more specific prefixes are not covered
pm.insert("11.0.0.0/8".parse()?, 5);  // Branch points that don't contain values are skipped
assert_eq!(pm.cover_keys(&p2).collect::<Vec<_>>(), vec![p0, p1, p2]);
Source

pub fn cover_values<'a, 'p>( &'a self, prefix: &'p P, ) -> CoverValues<'a, 'p, P, T>

Iterate over all values in the map that covers the given prefix (including prefix itself if that is present in the map). The returned iterator yields &'a T.

The iterator will always yield elements ordered by their prefix length, i.e., their depth in the tree.

let mut pm: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::new();
let p0 = "10.0.0.0/8".parse()?;
let p1 = "10.1.0.0/16".parse()?;
let p2 = "10.1.1.0/24".parse()?;
pm.insert(p0, 0);
pm.insert(p1, 1);
pm.insert(p2, 2);
pm.insert("10.1.2.0/24".parse()?, 3); // disjoint prefixes are not covered
pm.insert("10.1.1.0/25".parse()?, 4); // more specific prefixes are not covered
pm.insert("11.0.0.0/8".parse()?, 5);  // Branch points that don't contain values are skipped
assert_eq!(pm.cover_values(&p2).collect::<Vec<_>>(), vec![&0, &1, &2]);
Source§

impl<P: JointPrefix, T> JointPrefixMap<P, T>

Source

pub fn iter(&self) -> Iter<'_, P, T>

An iterator visiting all key-value pairs in lexicographic order. The iterator element type is (&P, &T). Elements of the first prefix are yielded before those of the second prefix.

let mut pm: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::new();
pm.insert("2001::1:0:0/97".parse()?, 6);
pm.insert("2001::1:0:0/96".parse()?, 7);
pm.insert("192.168.0.0/22".parse()?, 1);
pm.insert("192.168.0.0/23".parse()?, 2);
pm.insert("192.168.2.0/23".parse()?, 3);
pm.insert("192.168.0.0/24".parse()?, 4);
pm.insert("192.168.2.0/24".parse()?, 5);
assert_eq!(
    pm.iter().collect::<Vec<_>>(),
    vec![
        ("192.168.0.0/22".parse()?, &1),
        ("192.168.0.0/23".parse()?, &2),
        ("192.168.0.0/24".parse()?, &4),
        ("192.168.2.0/23".parse()?, &3),
        ("192.168.2.0/24".parse()?, &5),
        ("2001::1:0:0/96".parse()?, &7),
        ("2001::1:0:0/97".parse()?, &6),
    ]
);
Source

pub fn iter_mut(&mut self) -> IterMut<'_, P, T>

Get a mutable iterator over all key-value pairs. The order of this iterator is lexicographic.

let mut pm: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::new();
pm.insert("2001::1:0:0/97".parse()?, 6);
pm.insert("2001::1:0:0/96".parse()?, 7);
pm.insert("192.168.0.0/22".parse()?, 1);
pm.insert("192.168.0.0/23".parse()?, 2);
pm.insert("192.168.2.0/23".parse()?, 3);
pm.insert("192.168.0.0/24".parse()?, 4);
pm.insert("192.168.2.0/24".parse()?, 5);
pm.iter_mut().for_each(|(_, v)| *v += 1);
assert_eq!(
    pm.iter().collect::<Vec<_>>(),
    vec![
        ("192.168.0.0/22".parse()?, &2),
        ("192.168.0.0/23".parse()?, &3),
        ("192.168.0.0/24".parse()?, &5),
        ("192.168.2.0/23".parse()?, &4),
        ("192.168.2.0/24".parse()?, &6),
        ("2001::1:0:0/96".parse()?, &8),
        ("2001::1:0:0/97".parse()?, &7),
    ]
);
Source

pub fn keys(&self) -> Keys<'_, P, T>

An iterator visiting all keys in lexicographic order. The iterator element type is &P. Elements of the first prefix are yielded before those of the second one.

let mut pm: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::new();
pm.insert("2001::1:0:0/97".parse()?, 6);
pm.insert("2001::1:0:0/96".parse()?, 7);
pm.insert("192.168.0.0/22".parse()?, 1);
pm.insert("192.168.0.0/23".parse()?, 2);
pm.insert("192.168.2.0/23".parse()?, 3);
pm.insert("192.168.0.0/24".parse()?, 4);
pm.insert("192.168.2.0/24".parse()?, 5);
assert_eq!(
    pm.keys().collect::<Vec<_>>(),
    vec![
        "192.168.0.0/22".parse()?,
        "192.168.0.0/23".parse()?,
        "192.168.0.0/24".parse()?,
        "192.168.2.0/23".parse()?,
        "192.168.2.0/24".parse()?,
        "2001::1:0:0/96".parse()?,
        "2001::1:0:0/97".parse()?,
    ]
);
Source

pub fn into_keys(self) -> IntoKeys<P, T>

Creates a consuming iterator visiting all keys in lexicographic order. The iterator element type is P.

let mut pm: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::new();
pm.insert("2001::1:0:0/97".parse()?, 6);
pm.insert("2001::1:0:0/96".parse()?, 7);
pm.insert("192.168.0.0/22".parse()?, 1);
pm.insert("192.168.0.0/23".parse()?, 2);
pm.insert("192.168.2.0/23".parse()?, 3);
pm.insert("192.168.0.0/24".parse()?, 4);
pm.insert("192.168.2.0/24".parse()?, 5);
assert_eq!(
    pm.into_keys().collect::<Vec<_>>(),
    vec![
        "192.168.0.0/22".parse()?,
        "192.168.0.0/23".parse()?,
        "192.168.0.0/24".parse()?,
        "192.168.2.0/23".parse()?,
        "192.168.2.0/24".parse()?,
        "2001::1:0:0/96".parse()?,
        "2001::1:0:0/97".parse()?,
    ]
);
Source

pub fn values(&self) -> Values<'_, P, T>

An iterator visiting all values in lexicographic order. The iterator element type is &P. Elements of the first prefix are yielded before those of the second one.

let mut pm: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::new();
pm.insert("2001::1:0:0/97".parse()?, 6);
pm.insert("2001::1:0:0/96".parse()?, 7);
pm.insert("192.168.0.0/22".parse()?, 1);
pm.insert("192.168.0.0/23".parse()?, 2);
pm.insert("192.168.2.0/23".parse()?, 3);
pm.insert("192.168.0.0/24".parse()?, 4);
pm.insert("192.168.2.0/24".parse()?, 5);
assert_eq!(pm.values().collect::<Vec<_>>(), vec![&1, &2, &4, &3, &5, &7, &6]);
Source

pub fn into_values(self) -> IntoValues<P, T>

Creates a consuming iterator visiting all values in lexicographic order. The iterator element type is P.

let mut pm: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::new();
pm.insert("2001::1:0:0/97".parse()?, 6);
pm.insert("2001::1:0:0/96".parse()?, 7);
pm.insert("192.168.0.0/22".parse()?, 1);
pm.insert("192.168.0.0/23".parse()?, 2);
pm.insert("192.168.2.0/23".parse()?, 3);
pm.insert("192.168.0.0/24".parse()?, 4);
pm.insert("192.168.2.0/24".parse()?, 5);
assert_eq!(pm.into_values().collect::<Vec<_>>(), vec![1, 2, 4, 3, 5, 7, 6]);
Source

pub fn values_mut(&mut self) -> ValuesMut<'_, P, T>

Get a mutable iterator over all values. The order of this iterator is lexicographic.

let mut pm: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::new();
pm.insert("2001::1:0:0/97".parse()?, 6);
pm.insert("2001::1:0:0/96".parse()?, 7);
pm.insert("192.168.0.0/22".parse()?, 1);
pm.insert("192.168.0.0/23".parse()?, 2);
pm.insert("192.168.2.0/23".parse()?, 3);
pm.insert("192.168.0.0/24".parse()?, 4);
pm.insert("192.168.2.0/24".parse()?, 5);

pm.values_mut().for_each(|v| *v += 1);
assert_eq!(pm.values().collect::<Vec<_>>(), vec![&2, &3, &5, &4, &6, &8, &7]);
Source

pub fn children<'a>(&'a self, prefix: &P) -> Iter<'a, P, T>

Get an iterator over the node itself and all children. All elements returned have a prefix that is contained within prefix itself (or are the same). The iterator yields references to both keys and values, i.e., type (&'a P, &'a T). The iterator yields elements in lexicographic order.

Note: Consider using crate::AsView::view_at as an alternative.

let mut pm: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::new();
pm.insert("192.168.0.0/22".parse()?, 1);
pm.insert("192.168.0.0/23".parse()?, 2);
pm.insert("192.168.2.0/23".parse()?, 3);
pm.insert("192.168.0.0/24".parse()?, 4);
pm.insert("192.168.2.0/24".parse()?, 5);
assert_eq!(
    pm.children(&"192.168.0.0/23".parse()?).collect::<Vec<_>>(),
    vec![
        ("192.168.0.0/23".parse()?, &2),
        ("192.168.0.0/24".parse()?, &4),
    ]
);

If the prefix is not present in the tree, and there are no children, the iterator will be empty:

let mut pm: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::new();
pm.insert("2001::/32".parse()?, 1);
pm.insert("2001::/48".parse()?, 2);
assert_eq!(
    pm.children(&"2001::/24".parse()?).collect::<Vec<_>>(),
    vec![
        ("2001::/32".parse()?, &1),
        ("2001::/48".parse()?, &2),
    ]
);
assert_eq!(pm.children(&"2001::/96".parse()?).collect::<Vec<_>>(), vec![]);
assert_eq!(pm.children(&"1111::/24".parse()?).collect::<Vec<_>>(), vec![]);
Source

pub fn children_mut<'a>(&'a mut self, prefix: &P) -> IterMut<'a, P, T>

Get an iterator of mutable references of the node itself and all its children. All elements returned have a prefix that is contained within prefix itself (or are the same). The iterator yields references to the keys, and mutable references to the values, i.e., type (&'a P, &'a mut T). The iterator yields elements in lexicographic order.

Note: Consider using crate::AsViewMut::view_mut_at as an alternative.

let mut pm: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::new();
pm.insert("192.168.0.0/22".parse()?, 1);
pm.insert("192.168.0.0/23".parse()?, 2);
pm.insert("192.168.0.0/24".parse()?, 3);
pm.insert("192.168.2.0/23".parse()?, 4);
pm.insert("192.168.2.0/24".parse()?, 5);
pm.children_mut(&"192.168.0.0/23".parse()?).for_each(|(_, x)| *x *= 10);
assert_eq!(
    pm.into_iter().collect::<Vec<_>>(),
    vec![
        ("192.168.0.0/22".parse()?, 1),
        ("192.168.0.0/23".parse()?, 20),
        ("192.168.0.0/24".parse()?, 30),
        ("192.168.2.0/23".parse()?, 4),
        ("192.168.2.0/24".parse()?, 5),
    ]
);

If the prefix is not present in the tree, and there are no children, the iterator will be empty:

let mut pm: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::new();
pm.insert("2001::/32".parse()?, 1);
pm.insert("2001::/48".parse()?, 2);
assert_eq!(pm.children_mut(&"2001::/96".parse()?).collect::<Vec<_>>(), vec![]);
assert_eq!(pm.children_mut(&"1111::/24".parse()?).collect::<Vec<_>>(), vec![]);
Source

pub fn into_children(self, prefix: &P) -> IntoIter<P, T>

Get an iterator over the node itself and all children with a value. All elements returned have a prefix that is contained within prefix itself (or are the same). This function will consume self, returning an iterator over all owned children.

let mut pm: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::new();
pm.insert("192.168.0.0/22".parse()?, 1);
pm.insert("192.168.0.0/23".parse()?, 2);
pm.insert("192.168.2.0/23".parse()?, 3);
pm.insert("192.168.0.0/24".parse()?, 4);
pm.insert("192.168.2.0/24".parse()?, 5);
assert_eq!(
    pm.into_children(&"192.168.0.0/23".parse()?).collect::<Vec<_>>(),
    vec![
        ("192.168.0.0/23".parse()?, 2),
        ("192.168.0.0/24".parse()?, 4),
    ]
);

If the prefix is not present in the tree, and there are no children, the iterator will be empty:

let mut pm: JointPrefixMap<ipnet::IpNet, _> = JointPrefixMap::new();
pm.insert("2001::/32".parse()?, 1);
pm.insert("2001::/48".parse()?, 2);
assert_eq!(
    pm.clone().into_children(&"2001::/24".parse()?).collect::<Vec<_>>(),
    vec![
        ("2001::/32".parse()?, 1),
        ("2001::/48".parse()?, 2),
    ]
);
assert_eq!(pm.clone().into_children(&"2001::/96".parse()?).collect::<Vec<_>>(), vec![]);
assert_eq!(pm.clone().into_children(&"1111::/24".parse()?).collect::<Vec<_>>(), vec![]);
Source

pub fn union<'a, R>( &'a self, other: &'a JointPrefixMap<P, R>, ) -> Union<'a, P, T, R>

Iterate over the union of two joint prefix maps. This is roughly equivalent to calling self.t1.view().union(&other.t1).chain(self.t2.view().union(&other.t2)).

If a prefix is present in both trees, the iterator will yield both elements. Otherwise, the iterator will yield the element of one map together with the longest prefix match in the other map. Elements are of type UnionItem.

macro_rules! net { ($x:literal) => {$x.parse::<ipnet::IpNet>().unwrap()}; }

let mut map_a: JointPrefixMap<ipnet::IpNet, usize> = JointPrefixMap::from_iter([
    (net!("2001::1:0:0/96"), 1),
    (net!("192.168.0.0/22"), 2),
    (net!("192.168.0.0/24"), 3),
]);
let mut map_b: JointPrefixMap<ipnet::IpNet, &'static str> = JointPrefixMap::from_iter([
    (net!("192.168.0.0/22"), "a"),
    (net!("192.168.0.0/23"), "b"),
]);
assert_eq!(
    map_a.union(&map_b).collect::<Vec<_>>(),
    vec![
        UnionItem::Both{
            prefix: net!("192.168.0.0/22"),
            left: &2,
            right: &"a",
        },
        UnionItem::Right{
            prefix: net!("192.168.0.0/23"),
            left: Some((net!("192.168.0.0/22"), &2)),
            right: &"b",
        },
        UnionItem::Left{
            prefix: net!("192.168.0.0/24"),
            left: &3,
            right: Some((net!("192.168.0.0/23"), &"b")),
        },
        UnionItem::Left{
            prefix: net!("2001::1:0:0/96"),
            left: &1,
            right: None,
        },
    ]
);
Source

pub fn intersection<'a, R>( &'a self, other: &'a JointPrefixMap<P, R>, ) -> Intersection<'a, P, T, R>

Iterate over the intersection of two joint prefix maps. This is roughly equivalent to calling self.t1.view().intersection(&other.t1).chain(self.t2.view().intersection(&other.t2)).

macro_rules! net { ($x:literal) => {$x.parse::<ipnet::IpNet>().unwrap()}; }

let mut map_a: JointPrefixMap<ipnet::IpNet, usize> = JointPrefixMap::from_iter([
    (net!("192.168.0.0/20"), 1),
    (net!("192.168.0.0/22"), 2),
    (net!("192.168.0.0/24"), 3),
    (net!("192.168.2.0/23"), 4),
    (net!("2001::1:0:0/96"), 5),
    (net!("2001::1:0:0/97"), 6),
]);
let mut map_b: JointPrefixMap<ipnet::IpNet, &'static str> = JointPrefixMap::from_iter([
    (net!("192.168.0.0/20"), "a"),
    (net!("192.168.0.0/22"), "b"),
    (net!("192.168.0.0/23"), "c"),
    (net!("192.168.0.0/24"), "d"),
    (net!("192.168.2.0/24"), "e"),
    (net!("2001::1:0:0/96"), "f"),
    (net!("2001::0:0:0/97"), "g"),
]);
assert_eq!(
    map_a.intersection(&map_b).collect::<Vec<_>>(),
    vec![
        (net!("192.168.0.0/20"), &1, &"a"),
        (net!("192.168.0.0/22"), &2, &"b"),
        (net!("192.168.0.0/24"), &3, &"d"),
        (net!("2001::1:0:0/96"), &5, &"f"),
    ]
);
Source

pub fn difference<'a, R>( &'a self, other: &'a JointPrefixMap<P, R>, ) -> Difference<'a, P, T, R>

Iterate over the all elements in self that are not present in other. Each item will return a reference to the prefix and value in self, as well as the longest prefix match of other.

macro_rules! net { ($x:literal) => {$x.parse::<ipnet::IpNet>().unwrap()}; }

let mut map_a: JointPrefixMap<ipnet::IpNet, usize> = JointPrefixMap::from_iter([
    (net!("192.168.0.0/20"), 1),
    (net!("192.168.0.0/22"), 2),
    (net!("192.168.0.0/24"), 3),
    (net!("192.168.2.0/23"), 4),
    (net!("2001::1:0:0/96"), 5),
    (net!("2001::1:0:0/97"), 6),
]);
let mut map_b: JointPrefixMap<ipnet::IpNet, &'static str> = JointPrefixMap::from_iter([
    (net!("192.168.0.0/20"), "a"),
    (net!("192.168.0.0/22"), "b"),
    (net!("192.168.0.0/23"), "c"),
    (net!("192.168.2.0/24"), "d"),
    (net!("2001::1:0:0/96"), "e"),
]);
assert_eq!(
    map_a.difference(&map_b).collect::<Vec<_>>(),
    vec![
        DifferenceItem { prefix: net!("192.168.0.0/24"), value: &3, right: Some((net!("192.168.0.0/23"), &"c"))},
        DifferenceItem { prefix: net!("192.168.2.0/23"), value: &4, right: Some((net!("192.168.0.0/22"), &"b"))},
        DifferenceItem { prefix: net!("2001::1:0:0/97"), value: &6, right: Some((net!("2001::1:0:0/96"), &"e"))},
    ]
);
Source

pub fn covering_difference<'a, R>( &'a self, other: &'a JointPrefixMap<P, R>, ) -> CoveringDifference<'a, P, T, R>

Iterate over the all elements in self that are not covered by other.

macro_rules! net { ($x:literal) => {$x.parse::<ipnet::IpNet>().unwrap()}; }

let mut map_a: JointPrefixMap<ipnet::IpNet, usize> = JointPrefixMap::from_iter([
    (net!("192.168.0.0/20"), 1),
    (net!("192.168.0.0/22"), 2),
    (net!("192.168.0.0/24"), 3),
    (net!("192.168.2.0/23"), 4),
    (net!("2001::0:0:0/95"), 5),
    (net!("2001::1:0:0/96"), 6),
    (net!("2001::1:0:0/97"), 7),
]);
let mut map_b: JointPrefixMap<ipnet::IpNet, &'static str> = JointPrefixMap::from_iter([
    (net!("192.168.0.0/21"), "a"),
    (net!("192.168.0.0/22"), "b"),
    (net!("192.168.0.0/23"), "c"),
    (net!("192.168.2.0/24"), "d"),
    (net!("2001::1:0:0/96"), "e"),
]);
assert_eq!(
    map_a.covering_difference(&map_b).collect::<Vec<_>>(),
    vec![(net!("192.168.0.0/20"), &1), (net!("2001::0:0:0/95"), &5)]
);

Trait Implementations§

Source§

impl<P, T: Clone> Clone for JointPrefixMap<P, T>
where P: JointPrefix + Clone, P::P1: Clone, P::P2: Clone,

Source§

fn clone(&self) -> JointPrefixMap<P, T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<P, T: Debug> Debug for JointPrefixMap<P, T>
where P: JointPrefix + Debug, P::P1: Debug, P::P2: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<P, T> Default for JointPrefixMap<P, T>
where P: JointPrefix,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<P: JointPrefix, T> FromIterator<(P, T)> for JointPrefixMap<P, T>

Source§

fn from_iter<I: IntoIterator<Item = (P, T)>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<'a, P: JointPrefix, T> IntoIterator for &'a JointPrefixMap<P, T>

Source§

type Item = (P, &'a T)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, P, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<P: JointPrefix, T> IntoIterator for JointPrefixMap<P, T>

Source§

type Item = (P, T)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<P, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<P, L, R> PartialEq<JointPrefixMap<P, R>> for JointPrefixMap<P, L>
where P: JointPrefix + PartialEq, L: PartialEq<R>,

Source§

fn eq(&self, other: &JointPrefixMap<P, R>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<P, T> Eq for JointPrefixMap<P, T>
where P: JointPrefix + Eq, T: Eq,

Auto Trait Implementations§

§

impl<P, T> !Freeze for JointPrefixMap<P, T>

§

impl<P, T> !RefUnwindSafe for JointPrefixMap<P, T>

§

impl<P, T> Send for JointPrefixMap<P, T>
where <P as JointPrefix>::P1: Send, T: Send, <P as JointPrefix>::P2: Send,

§

impl<P, T> Sync for JointPrefixMap<P, T>
where <P as JointPrefix>::P1: Sync, T: Sync, <P as JointPrefix>::P2: Sync,

§

impl<P, T> Unpin for JointPrefixMap<P, T>
where <P as JointPrefix>::P1: Unpin, <P as JointPrefix>::P2: Unpin, T: Unpin,

§

impl<P, T> UnwindSafe for JointPrefixMap<P, T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.