Struct prefix_trie::set::PrefixSet

source ·
pub struct PrefixSet<P>(_);
Expand description

Set of prefixes, organized in a tree. This strucutre gives efficient access to the longest prefix in the set that contains another prefix.

Implementations§

Create a new, empty prefixset.

Check wether some prefix is present in the set, without using longest prefix match.

let mut set: PrefixSet<Ipv4Net> = PrefixSet::new();
set.insert("192.168.1.0/24".parse()?);
assert!(set.contains(&"192.168.1.0/24".parse()?));
assert!(!set.contains(&"192.168.2.0/24".parse()?));
assert!(!set.contains(&"192.168.0.0/23".parse()?));
assert!(!set.contains(&"192.168.1.128/25".parse()?));

Get the longest prefix in the set that contains the given preifx.

let mut set: PrefixSet<Ipv4Net> = PrefixSet::new();
set.insert("192.168.1.0/24".parse()?);
set.insert("192.168.0.0/23".parse()?);
assert_eq!(set.get_lpm(&"192.168.1.1/32".parse()?), Some(&"192.168.1.0/24".parse()?));
assert_eq!(set.get_lpm(&"192.168.1.0/24".parse()?), Some(&"192.168.1.0/24".parse()?));
assert_eq!(set.get_lpm(&"192.168.0.0/24".parse()?), Some(&"192.168.0.0/23".parse()?));
assert_eq!(set.get_lpm(&"192.168.2.0/24".parse()?), None);

Adds a value to the set.

Returns whether the value was newly inserted. That is:

  • If the set did not previously contain this value, true is returned.
  • If the set already contained this value, false is returned.
let mut set: PrefixSet<Ipv4Net> = PrefixSet::new();
assert!(set.insert("192.168.0.0/23".parse()?));
assert!(set.insert("192.168.1.0/24".parse()?));
assert!(!set.insert("192.168.1.0/24".parse()?));

Removes a value from the set. Returns whether the value was present in the set.

let mut set: PrefixSet<Ipv4Net> = PrefixSet::new();
let prefix = "192.168.1.0/24".parse()?;
set.insert(prefix);
assert!(set.contains(&prefix));
assert!(set.remove(&prefix));
assert!(!set.contains(&prefix));

Removes a prefix from the set, returning wether the prefix was present or not. 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 set: PrefixSet<Ipv4Net> = PrefixSet::new();
let prefix = "192.168.1.0/24".parse()?;
set.insert(prefix);
assert!(set.contains(&prefix));
assert!(set.remove_keep_tree(&prefix));
assert!(!set.contains(&prefix));

// future inserts of the same key are now faster!
set.insert(prefix);

Remove all elements 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 set: PrefixSet<Ipv4Net> = PrefixSet::new();
set.insert("192.168.0.0/22".parse()?);
set.insert("192.168.0.0/23".parse()?);
set.insert("192.168.0.0/24".parse()?);
set.insert("192.168.2.0/23".parse()?);
set.insert("192.168.2.0/24".parse()?);
set.remove_children(&"192.168.0.0/23".parse()?);
assert!(!set.contains(&"192.168.0.0/23".parse()?));
assert!(!set.contains(&"192.168.0.0/24".parse()?));
assert!(set.contains(&"192.168.2.0/23".parse()?));
assert!(set.contains(&"192.168.2.0/24".parse()?));

Clear the set but keep the allocated memory.

let mut set: PrefixSet<Ipv4Net> = PrefixSet::new();
set.insert("192.168.0.0/24".parse()?);
set.insert("192.168.1.0/24".parse()?);
set.clear();
assert!(!set.contains(&"192.168.0.0/24".parse()?));
assert!(!set.contains(&"192.168.1.0/24".parse()?));

Iterate over all prefixes in the set

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

let mut set: PrefixSet<Ipv4Net> = PrefixSet::new();
set.insert("192.168.0.0/24".parse()?);
set.insert("192.168.1.0/24".parse()?);
set.insert("192.168.2.0/24".parse()?);
set.insert("192.168.2.0/25".parse()?);
set.retain(|p| p.prefix_len() == 24);
assert!(set.contains(&"192.168.0.0/24".parse()?));
assert!(set.contains(&"192.168.1.0/24".parse()?));
assert!(set.contains(&"192.168.2.0/24".parse()?));
assert!(!set.contains(&"192.168.2.0/25".parse()?));

Return an iterator that traverses both trees simultaneously and yields the union of both sets in lexicographic order.

let mut set_a: PrefixSet<Ipv4Net> = PrefixSet::from_iter([
    "192.168.0.0/22".parse()?,
    "192.168.0.0/24".parse()?,
    "192.168.2.0/23".parse()?,
]);
let mut set_b: PrefixSet<Ipv4Net> = PrefixSet::from_iter([
    "192.168.0.0/22".parse()?,
    "192.168.0.0/23".parse()?,
    "192.168.2.0/24".parse()?,
]);
assert_eq!(
    set_a.union(&set_b).copied().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()?,
    ]
);

Return an iterator that traverses both trees simultaneously and yields the intersection of both sets in lexicographic order.

let mut set_a: PrefixSet<Ipv4Net> = PrefixSet::from_iter([
    "192.168.0.0/22".parse()?,
    "192.168.0.0/24".parse()?,
    "192.168.2.0/23".parse()?,
]);
let mut set_b: PrefixSet<Ipv4Net> = PrefixSet::from_iter([
    "192.168.0.0/22".parse()?,
    "192.168.0.0/24".parse()?,
    "192.168.2.0/24".parse()?,
]);
assert_eq!(
    set_a.intersection(&set_b).copied().collect::<Vec<_>>(),
    vec!["192.168.0.0/22".parse()?, "192.168.0.0/24".parse()?]
);

Return an iterator that traverses both trees simultaneously and yields the difference of both sets in lexicographic order.

let mut set_a: PrefixSet<Ipv4Net> = PrefixSet::from_iter([
    "192.168.0.0/22".parse()?,
    "192.168.0.0/24".parse()?,
    "192.168.2.0/23".parse()?,
]);
let mut set_b: PrefixSet<Ipv4Net> = PrefixSet::from_iter([
    "192.168.0.0/22".parse()?,
    "192.168.0.0/24".parse()?,
    "192.168.2.0/24".parse()?,
]);
assert_eq!(
    set_a.difference(&set_b).copied().collect::<Vec<_>>(),
    vec!["192.168.2.0/23".parse()?]
);

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Creates a value from an iterator. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.