fn p_insert_better(k: u32, a: &mut [(u32,u32)]) -> Inserted {
    let n = a.len();
    let poverty_limit = n;
    for pov in 0..poverty_limit {
        let ii = ((k + pov as u32) % n as u32) as usize;
        let ki = a[ii].0;
        let pov_ki = p_poverty(ki, ii, n);
        if a[ii] == (0,0) {
            return Inserted::EmptySpot(ii);
        } else if ki == k {
            // println!("already got a spot");
            return Inserted::Present(ii);
        } else if pov_ki < pov {
            // println!("need to steal from {} < {} at spot {}", pov_ki, pov, ii);
            // need to steal
            let stolen = ii;
            let mut displaced = a[ii];
            // println!("displaced value is {}", displaced);
            let mut pov_displaced = pov_ki;
            a[stolen] = (0,0);

            for j in 1..poverty_limit {
                pov_displaced += 1;
                let jj = (stolen + j) % n as usize;
                let kj = a[jj].0;
                let pov_kj = p_poverty(kj, jj, n);
                if a[jj] == (0,0) {
                    // We finally found an unoccupied spot!
                    // println!("put the displaced at {}", jj);
                    a[jj] = displaced;
                    return Inserted::EmptySpot(stolen);
                }
                if pov_kj < pov_displaced {
                    // need to steal again!
                    std::mem::swap(&mut a[jj], &mut displaced);
                    pov_displaced = pov_kj;
                }
            }
            return Inserted::NoRoom;
        }
    }
    Inserted::NoRoom
}
/// This inserts k into the array, and requires that there be room for
/// one more element.  Otherwise, things will be sad.
This inserts k into the array, and requires that there be room for
one more element.  Otherwise, things will be sad.
This inserts k into the array and requires that there be room for one more element . Otherwise things will be sad .
fn p_insert_better(k: u32, a: &mut [(u32,u32)]) -> Inserted {
    let n = a.len();
    let poverty_limit = n;
    for pov in 0..poverty_limit {
        let ii = ((k + pov as u32) % n as u32) as usize;
        let ki = a[ii].0;
        let pov_ki = p_poverty(ki, ii, n);
        if a[ii] == (0,0) {
            return Inserted::EmptySpot(ii);
        } else if ki == k {
            // println!("already got a spot");
            return Inserted::Present(ii);
        } else if pov_ki < pov {
            // println!("need to steal from {} < {} at spot {}", pov_ki, pov, ii);
            // need to steal
            let stolen = ii;
            let mut displaced = a[ii];
            // println!("displaced value is {}", displaced);
            let mut pov_displaced = pov_ki;
            a[stolen] = (0,0);
            for j in 1..poverty_limit {
                pov_displaced += 1;
                let jj = (stolen + j) % n as usize;
                let kj = a[jj].0;
                let pov_kj = p_poverty(kj, jj, n);
                if a[jj] == (0,0) {
                    // We finally found an unoccupied spot!
                    // println!("put the displaced at {}", jj);
                    a[jj] = displaced;
                    return Inserted::EmptySpot(stolen);
                }
                if pov_kj < pov_displaced {
                    // need to steal again!
                    std::mem::swap(&mut a[jj], &mut displaced);
                    pov_displaced = pov_kj;
                }
            }
            return Inserted::NoRoom;
        }
    }
    Inserted::NoRoom
}
fn p_insert_better ( k : u32 , a : & mut [ ( u32 , u32 ) ] ) -> Inserted { let n = a . len ( ) ; let poverty_limit = n ; for pov in 0 .. poverty_limit { let ii = ( ( k + pov as u32 ) % n as u32 ) as usize ; let ki = a [ ii ] . 0 ; let pov_ki = p_poverty ( ki , ii , n ) ; if a [ ii ] == ( 0 , 0 ) { return Inserted :: EmptySpot ( ii ) ; } else if ki == k { // println!("already got a spot"); return Inserted :: Present ( ii ) ; } else if pov_ki < pov { // println!("need to steal from {} < {} at spot {}", pov_ki, pov, ii); // need to steal let stolen = ii ; let mut displaced = a [ ii ] ; // println!("displaced value is {}", displaced); let mut pov_displaced = pov_ki ; a [ stolen ] = ( 0 , 0 ) ; for j in 1 .. poverty_limit { pov_displaced += 1 ; let jj = ( stolen + j ) % n as usize ; let kj = a [ jj ] . 0 ; let pov_kj = p_poverty ( kj , jj , n ) ; if a [ jj ] == ( 0 , 0 ) { // We finally found an unoccupied spot! // println!("put the displaced at {}", jj); a [ jj ] = displaced ; return Inserted :: EmptySpot ( stolen ) ; } if pov_kj < pov_displaced { // need to steal again! std :: mem :: swap ( & mut a [ jj ] , & mut displaced ) ; pov_displaced = pov_kj ; } } return Inserted :: NoRoom ; } } Inserted :: NoRoom }
This inserts k into the array, and requires that there be room for
one more element.
This inserts k into the array and requires that there be room for one more element .
k
u32
a
&mut [(u32,u32)]
k
u32
a
&mut [(u32,u32)]