pub struct RTrieSet<P: IpPrefix>(_);Expand description
A set of Ip prefixes based on a radix binary trie
Implementations§
source§impl<P: IpPrefix> RTrieSet<P>
impl<P: IpPrefix> RTrieSet<P>
sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a new set with an initial capacity.
The returned set already contains the root prefix.
sourcepub fn len(&self) -> NonZeroUsize
pub fn len(&self) -> NonZeroUsize
Returns the size of the set.
Notice that it never equals zero since the top prefix is always present in the set.
sourcepub fn compress(self) -> LCTrieSet<P>
pub fn compress(self) -> LCTrieSet<P>
Compress this Patricia trie in a LC-Trie.
For lookup algorithms, a Patricia trie performs unit bit checking and LC-Trie performs multi bits checking. So the last one is more performant but it cannot be modified (no insertion or removal operations are provided).
sourcepub fn insert(&mut self, k: P) -> bool
pub fn insert(&mut self, k: P) -> bool
Inserts a new element in the set.
If the specified element already exists in the set, false is returned.
Example
use std::net::Ipv4Addr;
let addr = Ipv4Addr::new(1,1,1,1);
let mut trie = Ipv4RTrieSet::new();
let ip20 = Ipv4Prefix::new(addr, 20).unwrap();
let ip22 = Ipv4Prefix::new(addr, 22).unwrap();
assert_eq!( trie.insert(ip20), true);
assert_eq!( trie.insert(ip22), true);
assert_eq!( trie.insert(ip20), false);sourcepub fn contains<Q>(&self, k: &Q) -> boolwhere
Q: IpPrefix<Addr = P::Addr>,
P: IpPrefixCovering<Q>,
pub fn contains<Q>(&self, k: &Q) -> boolwhere Q: IpPrefix<Addr = P::Addr>, P: IpPrefixCovering<Q>,
Checks if an element is present (exact match).
Example
use std::net::Ipv4Addr;
let addr = Ipv4Addr::new(1,1,1,1);
let ip20 = Ipv4Prefix::new(addr, 20).unwrap();
let ip22 = Ipv4Prefix::new(addr, 22).unwrap();
let ip24 = Ipv4Prefix::new(addr, 24).unwrap();
let trie = Ipv4RTrieSet::from_iter([ip20,ip24]);
assert_eq!( trie.contains(&ip20), true);
assert_eq!( trie.contains(&ip22), false);
assert_eq!( trie.contains(&ip24), true);sourcepub fn remove<Q>(&mut self, k: &Q) -> boolwhere
Q: IpPrefix<Addr = P::Addr>,
P: IpPrefixCovering<Q>,
pub fn remove<Q>(&mut self, k: &Q) -> boolwhere Q: IpPrefix<Addr = P::Addr>, P: IpPrefixCovering<Q>,
Removes a previously inserted prefix (exact match).
Returns false is the element was not present in the set
and true if the removal is effective.
Example
use std::net::Ipv4Addr;
let addr = Ipv4Addr::new(1,1,1,1);
let ip20 = Ipv4Prefix::new(addr, 20).unwrap();
let ip22 = Ipv4Prefix::new(addr, 22).unwrap();
let mut trie = Ipv4RTrieSet::from_iter([ip20,ip22]);
assert_eq!( trie.contains(&ip20), true);
assert_eq!(trie.remove(&ip20), true);
assert_eq!(trie.remove(&ip20), false);
assert_eq!( trie.contains(&ip22), true);
assert_eq!( trie.contains(&ip20), false);sourcepub fn replace(&mut self, k: P) -> Option<P>
pub fn replace(&mut self, k: P) -> Option<P>
Adds a prefix to the set, replacing the existing one, if any (exact match performed). Returns the replaced value.
Example
use std::net::Ipv4Addr;
use ipnet::Ipv4Net;
let mut trie = RTrieSet::new();
let addr1 = Ipv4Addr::new(1,1,1,1);;
let addr2 = Ipv4Addr::new(1,1,1,2);;
let ip20 = Ipv4Net::new(addr1, 20).unwrap();
let ip20b = Ipv4Net::new(addr2, 20).unwrap();
trie.insert(ip20);
assert_eq!(trie.get(&ip20).unwrap().to_string(), "1.1.1.1/20".to_string());
assert_eq!(trie.insert(ip20b), false);
assert_eq!(trie.get(&ip20).unwrap().to_string(), "1.1.1.1/20".to_string());
assert_eq!(trie.replace(ip20b), Some(ip20));
assert_eq!(trie.get(&ip20).unwrap().to_string(), "1.1.1.2/20".to_string());sourcepub fn get<Q>(&self, k: &Q) -> Option<&P>where
Q: IpPrefix<Addr = P::Addr>,
P: IpPrefixCovering<Q>,
pub fn get<Q>(&self, k: &Q) -> Option<&P>where Q: IpPrefix<Addr = P::Addr>, P: IpPrefixCovering<Q>,
Gets the value associated with an exact match of the key.
To access to the longest prefix match, use Self::lookup.
To get a mutable access to a value, use [Self::get_mut].
Example
use std::net::Ipv4Addr;
use ipnet::Ipv4Net;
let mut trie = RTrieSet::new();
let addr1 = Ipv4Addr::new(1,1,1,1);;
let addr2 = Ipv4Addr::new(1,1,1,2);;
let ip20 = Ipv4Net::new(addr1, 20).unwrap();
let ip20b = Ipv4Net::new(addr2, 20).unwrap();
trie.insert(ip20);
assert_eq!(trie.get(&ip20).unwrap().to_string(), "1.1.1.1/20".to_string());
trie.insert(ip20b);
assert_eq!(trie.get(&ip20).unwrap().to_string(), "1.1.1.1/20".to_string());sourcepub fn lookup<Q>(&self, k: &Q) -> &Pwhere
Q: IpPrefix<Addr = P::Addr>,
P: IpPrefixCovering<Q>,
pub fn lookup<Q>(&self, k: &Q) -> &Pwhere Q: IpPrefix<Addr = P::Addr>, P: IpPrefixCovering<Q>,
Gets the longest prefix which matches the given key.
As the top prefix always matches, it never fails.
To access to the exact prefix match, use Self::get.
Example
use std::net::Ipv4Addr;
let mut trie = Ipv4RTrieSet::new();
let addr = Ipv4Addr::new(1,1,1,1);;
let ip20 = Ipv4Prefix::new(addr, 20).unwrap();
let ip22 = Ipv4Prefix::new(addr, 22).unwrap();
let ip24 = Ipv4Prefix::new(addr, 24).unwrap();
trie.insert(ip20);
trie.insert(ip24);
assert_eq!( trie.lookup(&ip20), &ip20);
assert_eq!( trie.lookup(&ip22), &ip20);
assert_eq!( trie.lookup(&ip24), &ip24);
assert_eq!( trie.lookup(&addr), &ip24);Trait Implementations§
source§impl<P: IpPrefix> Extend<P> for RTrieSet<P>
impl<P: IpPrefix> Extend<P> for RTrieSet<P>
source§fn extend<I: IntoIterator<Item = P>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = P>>(&mut self, iter: I)
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)