Struct rendezvous_hash::HeterogeneousRendezvousNodes
[−]
[src]
pub struct HeterogeneousRendezvousNodes<N, H> { /* fields omitted */ }
A heterogeneous candidate node set of a rendezvous for clients that are requiring the same item.
"heterogeneous" means that each node can have a different capacity.
Examples
use std::collections::HashMap; use rendezvous_hash::Capacity; use rendezvous_hash::HeterogeneousRendezvousNodes; let mut nodes = HeterogeneousRendezvousNodes::default(); nodes.insert("foo", Capacity::new(70).unwrap()); nodes.insert("bar", Capacity::new(20).unwrap()); nodes.insert("baz", Capacity::new(9).unwrap()); nodes.insert("qux", Capacity::new(1).unwrap()); let mut counts = HashMap::new(); for item in 0..10000 { let node = nodes.calc_candidates(&item).nth(0).unwrap(); *counts.entry(node.0.to_string()).or_insert(0) += 1; } assert_eq!(((counts["foo"] as f64) / 100.0).round(), 70.0); assert_eq!(((counts["bar"] as f64) / 100.0).round(), 20.0); assert_eq!(((counts["baz"] as f64) / 100.0).round(), 9.0); assert_eq!(((counts["qux"] as f64) / 100.0).round(), 1.0);
Methods
impl<N, H> HeterogeneousRendezvousNodes<N, H> where N: PartialEq + Ord + Hash,
H: for<'a> NodeHasher<(&'a N, u16)>
[src]
H: for<'a> NodeHasher<(&'a N, u16)>
fn new(hasher: H) -> Self
Makes a new HeterogeneousRendezvousNodes
instance.
fn calc_candidates<T: Hash>(&mut self, item: &T) -> Candidates<(N, Capacity)>
Returns the candidate nodes for item
.
The higher priority node is located in front of the returned candidate sequence.
Note that this method takes O(n * m + n log n)
steps
(where n
is the return value of self.len()
and
m
is the maximum value among the capacities of the nodes.)
impl<N, H> HeterogeneousRendezvousNodes<N, H> where N: PartialEq + Hash
[src]
fn insert(&mut self, node: N, capacity: Capacity) -> Option<(N, Capacity)>
Inserts a new candidate node which has the capacity capacity
.
If a node which equals to node
exists,
it will be removed and returned as Some((N, Capacity))
.
fn remove<M>(&mut self, node: &M) -> Option<(N, Capacity)> where N: Borrow<M>, M: PartialEq
Removes the specified node from the candidates.
If the node does not exist, this method will return None
.
fn contains<M>(&self, node: &M) -> bool where N: Borrow<M>, M: PartialEq
Returns true
if the specified node exists in this candidate set, otherwise false
.
impl<N, H> HeterogeneousRendezvousNodes<N, H>
[src]
fn len(&self) -> usize
Returns the count of the candidate nodes.
fn iter(&self) -> Iter<(N, Capacity)>
Returns an iterator over the nodes of this candidate set.
Trait Implementations
impl<N: Debug, H: Debug> Debug for HeterogeneousRendezvousNodes<N, H>
[src]
impl<N: Clone, H: Clone> Clone for HeterogeneousRendezvousNodes<N, H>
[src]
fn clone(&self) -> HeterogeneousRendezvousNodes<N, H>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more
impl<N> Default for HeterogeneousRendezvousNodes<N, DefaultNodeHasher> where N: PartialEq + Ord + Hash
[src]
impl<N, H> IntoIterator for HeterogeneousRendezvousNodes<N, H>
[src]
type Item = (N, Capacity)
The type of the elements being iterated over.
type IntoIter = IntoIter<(N, Capacity)>
Which kind of iterator are we turning this into?
fn into_iter(self) -> Self::IntoIter
Creates an iterator from a value. Read more