Struct prefix_trie::map::PrefixMap

source ·
pub struct PrefixMap<P, T> { /* private fields */ }
Expand description

Prefix map implemented as a prefix tree.

Implementations§

source§

impl<P, T> PrefixMap<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).

let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::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.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),
    ]
);
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 arbitrary (and not in lexicographic order).

source

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

An iterator visiting all keys in lexicographic order. The iterator element type is &P.

let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::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.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()?,
    ]
);
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.

source

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

An iterator visiting all values in lexicographic order. The iterator element type is &P.

let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::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.values().collect::<Vec<_>>(), vec![&1, &2, &4, &3, &5]);
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.

source

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

Get a mutable iterator over all values. The order of this iterator is arbitrary (and not in lexicographic order).

source§

impl<P, T> PrefixMap<P, T>
where P: Prefix,

source

pub fn children(&self, prefix: &P) -> Iter<'_, 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).

let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::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),
    ]
);
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: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::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),
    ]
);
source§

impl<P, T> PrefixMap<P, T>
where P: Prefix,

source

pub fn new() -> Self

Create an empty prefix map.

source

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

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

let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.1.0/24".parse()?, 1);
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);
source

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

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

let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
let prefix = "192.168.1.0/24".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(&self, prefix: &P) -> Option<(&P, &T)>

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

let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
let prefix = "192.168.1.0/24".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<(&'a P, &'a T)>

Get a value of an element by using longest prefix matching

let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::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(&mut self, prefix: &P) -> Option<(&P, &mut T)>

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

let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::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: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::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: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::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<(&'a P, &'a T)>

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

let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::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: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::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.

let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::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));
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.

let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.0.0/23".parse()?, vec![1]);
pm.entry("192.168.0.0/23".parse()?).or_default().push(2);
pm.entry("192.168.0.0/24".parse()?).or_default().push(3);
assert_eq!(pm.get(&"192.168.0.0/23".parse()?), Some(&vec![1, 2]));
assert_eq!(pm.get(&"192.168.0.0/24".parse()?), Some(&vec![3]));
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: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::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: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::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: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::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: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.0.0/24".parse()?, 1);
pm.insert("192.168.1.0/24".parse()?, 2);
pm.clear();
assert_eq!(pm.get(&"192.168.0.0/24".parse()?), None);
assert_eq!(pm.get(&"192.168.1.0/24".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: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::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.retain(|_, t| *t % 2 == 0);
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));

Trait Implementations§

source§

impl<P: Clone, T: Clone> Clone for PrefixMap<P, T>

source§

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

Returns a copy 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: Debug, T: Debug> Debug for PrefixMap<P, T>

source§

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

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

impl<P, T> Default for PrefixMap<P, T>
where P: Prefix,

source§

fn default() -> Self

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

impl<P, T> FromIterator<(P, T)> for PrefixMap<P, T>
where P: Prefix,

source§

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

Creates a value from an iterator. Read more
source§

impl<'a, P, T> IntoIterator for &'a PrefixMap<P, T>

§

type Item = (&'a P, &'a T)

The type of the elements being iterated over.
§

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: Prefix, T> IntoIterator for PrefixMap<P, T>

§

type Item = (P, T)

The type of the elements being iterated over.
§

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, T> PartialEq for PrefixMap<P, T>
where P: Prefix + PartialEq, T: PartialEq,

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<P, T> Eq for PrefixMap<P, T>
where P: Prefix + Eq, T: Eq,

Auto Trait Implementations§

§

impl<P, T> RefUnwindSafe for PrefixMap<P, T>

§

impl<P, T> Send for PrefixMap<P, T>
where P: Send, T: Send,

§

impl<P, T> Sync for PrefixMap<P, T>
where P: Sync, T: Sync,

§

impl<P, T> Unpin for PrefixMap<P, T>
where P: Unpin, T: Unpin,

§

impl<P, T> UnwindSafe for PrefixMap<P, T>
where P: UnwindSafe, T: UnwindSafe,

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> 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> ToOwned for T
where T: Clone,

§

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>,

§

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>,

§

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.