/// Populate the lookup table based on the current backend settings.
///
/// ```
/// use std::net::Ipv4Addr;
/// use std::str::FromStr;
///
/// use rusty_rail::consistenthash;
///
/// let mut c = consistenthash::ConsistentHash::new();
/// let tgt = Ipv4Addr::from_str("1.2.3.4").unwrap();
/// c.backends.push(consistenthash::Backend::new("server-1", tgt));
/// c.backends.push(consistenthash::Backend::new("server-2", tgt));
/// c.backends.push(consistenthash::Backend::new("server-3", tgt));
/// c.backends.push(consistenthash::Backend::new("server-4", tgt));
/// c.backends[3].live = false;
/// c.populate();
/// assert_eq!(c.lookup,
/// vec!
/// [2, 1, 1, 0, 1, 2, 0, 1, 2, 1, 2, 0, 2, 2, 0, 2, 1, 0, 2, 1, 2, 2, 0, 2, 0, 0, 1, 0, 1, 2,
/// 0, 1, 0, 0, 2, 0, 2, 2, 1, 2, 1, 0, 2, 1, 2, 2, 0, 2, 0, 0, 2, 0, 1, 0, 0, 1, 0, 0, 2, 0, 0,
/// 1, 1, 2, 1, 0, 2, 1, 0, 1, 0, 2, 0, 0, 2, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 2, 1, 0, 1, 1,
/// 0, 1, 0, 2, 0, 0, 1, 0, 0, 2, 0, 0, 0, 1, 1, 1, 2, 1, 1, 2, 1, 2, 1, 1, 2, 1, 2, 0, 2, 2, 1,
/// 2, 2, 2, 2, 1, 2, 2, 1, 1, 2, 1, 1, 0, 1, 2, 1, 1, 2, 1, 2, 0, 2, 2, 0, 2, 0, 2, 2, 1, 2, 2,
/// 0, 2, 1, 2, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 2, 0, 2, 0, 2, 2, 1, 2, 2, 0, 2, 1, 0, 2, 0, 2,
/// 0, 0, 1, 0, 0, 2, 0, 1, 0, 0, 2, 1, 0, 2, 1, 2, 1, 0, 2, 1, 0, 2, 0, 2, 0, 0, 1, 0, 0, 0, 0,
/// 1, 1, 1, 2, 1, 0, 1, 1, 0, 1, 0, 2, 1, 0, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1,
/// 1, 0, 1, 0, 1, 1, 2, 1, 2, 2, 2, 2, 1, 2, 2, 1, 2, 1, 1, 1, 0, 1, 2, 1, 1, 2, 1, 2, 0, 1, 2,
/// 1, 2, 0, 2, 2, 0, 2, 2, 2, 2, 1, 2, 2, 0, 1]);
/// ```
Populate the lookup table based on the current backend settings.




Populate the lookup table based on the current backend settings .
pub fn populate(&mut self) {
        // This is 'approximately' 100x the number of backends; could look for the next higher
        // prime in future to ensure that.
        let lookup_size = self.backends.iter().filter(|b| b.live).count() * 100;
        let p = primes::primes(lookup_size);
        let lookup_size = p[p.len() - 1] as u32;
        for backend in &mut self.backends {
            if backend.permutation.len() != lookup_size as usize {
                backend.permutation = permute_backend(&backend.name, lookup_size)
            }
        }
        let mut next = vec![0; self.backends.len()];
        self.lookup = vec![u32::max_value();lookup_size as usize];
        let mut allocated = 0;
        loop {
            for (i, backend) in self.backends.iter().enumerate() {
                if !backend.live {
                    continue;
                }
                let mut candidate = backend.permutation[next[i]];
                while self.lookup[candidate as usize] != u32::max_value() {
                    // Find next unallocated position from backend.
                    next[i] += 1;
                    candidate = backend.permutation[next[i]];
                }
                self.lookup[candidate as usize] = i as u32;
                next[i] += 1;
                allocated += 1;
                if allocated == lookup_size {
                    return;
                }
            }
        }
    }
pub fn populate ( & mut self ) { // This is 'approximately' 100x the number of backends; could look for the next higher // prime in future to ensure that. let lookup_size = self . backends . iter ( ) . filter ( | b | b . live ) . count ( ) * 100 ; let p = primes :: primes ( lookup_size ) ; let lookup_size = p [ p . len ( ) - 1 ] as u32 ; for backend in & mut self . backends { if backend . permutation . len ( ) != lookup_size as usize { backend . permutation = permute_backend ( & backend . name , lookup_size ) } } let mut next = vec ! [ 0 self backends len ( ) ] ; self . lookup = vec ! [ u32 max_value ( ) lookup_size as usize ] ; let mut allocated = 0 ; loop { for ( i , backend ) in self . backends . iter ( ) . enumerate ( ) { if ! backend . live { continue ; } let mut candidate = backend . permutation [ next [ i ] ] ; while self . lookup [ candidate as usize ] != u32 :: max_value ( ) { // Find next unallocated position from backend. next [ i ] += 1 ; candidate = backend . permutation [ next [ i ] ] ; } self . lookup [ candidate as usize ] = i as u32 ; next [ i ] += 1 ; allocated += 1 ; if allocated == lookup_size { return ; } } } }
Populate the lookup table based on the current backend settings.
Populate the lookup table based on the current backend settings .
&mut self