gwr_components/arbiter/policy/
round_robin.rs1use std::rc::Rc;
6
7use gwr_engine::traits::SimObject;
8use gwr_track::entity::Entity;
9
10use crate::arbiter::Arbitrate;
11
12pub struct RoundRobin {
13 candidate: usize,
14}
15
16impl RoundRobin {
17 #[must_use]
18 pub fn new() -> Self {
19 Self { candidate: 0 }
20 }
21}
22
23impl Default for RoundRobin {
24 fn default() -> Self {
25 Self::new()
26 }
27}
28
29impl<T> Arbitrate<T> for RoundRobin
30where
31 T: SimObject,
32{
33 fn arbitrate(
34 &mut self,
35 _entity: &Rc<Entity>,
36 input_values: &mut [Option<T>],
37 ) -> Option<(usize, T)> {
38 let num_inputs = input_values.len();
39 for i in 0..num_inputs {
40 let index = (i + self.candidate) % num_inputs;
41 if let Some(value) = input_values[index].take() {
42 self.candidate = (index + 1) % num_inputs;
43 return Some((index, value));
44 }
45 }
46 None
47 }
48}