Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[derive(Debug)]
- struct Up {
- weight: f64,
- cap: usize,
- conns: usize,
- }
- fn main() {
- let mut ups = [
- Up {
- weight: 0.9,
- cap: 300,
- conns: 0,
- },
- Up {
- weight: 0.1,
- cap: 300,
- conns: 0,
- },
- ];
- for i in 0..100 {
- let r: Option<usize> = {
- use std::cmp::Ordering::Equal;
- let r = ups
- .iter()
- .enumerate()
- .filter(|(i, e)| e.conns < e.cap)
- .min_by(|(i, a), (j, b)| {
- let a = a.conns as f64 / a.weight;
- let b = b.conns as f64 / b.weight;
- a.partial_cmp(&b).unwrap_or(Equal)
- })
- .map(|(i, ..)| i);
- r
- };
- if let Some(idx) = r {
- ups[idx].conns += 1;
- }
- let arr = [
- ups[0].conns,
- ups[1].conns,
- ];
- println!("ups: {:?}", arr);
- }
- }
Add Comment
Please, Sign In to add comment