1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//! Counting quiet rounds for local settle detection; see
//! [`cbba::Agent::is_locally_settled`](crate::cbba::Agent::is_locally_settled) for the
//! heuristic and its limits.
use alloc::vec::Vec;
/// How many bundle phases in a row an agent's winners and bids stayed the same.
#[derive(Clone, Debug)]
pub(crate) struct Quiet<T> {
/// The winners and bids at the last bundle phase that changed them.
last: Vec<T>,
/// Bundle phases since then.
rounds: u64,
}
impl<T> Quiet<T> {
/// A count that has seen no bundle phase yet, starting from the agent's initial `view`,
/// so that a first phase that changes nothing already counts as quiet.
pub(crate) fn new(view: impl Iterator<Item = T>) -> Self {
Self {
last: view.collect(),
rounds: 0,
}
}
/// Bundle phases since the winners or bids last changed.
pub(crate) const fn rounds(&self) -> u64 {
self.rounds
}
}
impl<T: Clone + PartialEq> Quiet<T> {
/// Notes one bundle phase, ending with `view`: the agent's winners and bids per task.
/// Counts it as quiet when `view` matches the last one, and restarts the count
/// otherwise. Does not allocate once the task count stopped growing.
pub(crate) fn observe(&mut self, view: impl ExactSizeIterator<Item = T> + Clone) {
if self.last.len() == view.len() && self.last.iter().cloned().eq(view.clone()) {
self.rounds = self.rounds.saturating_add(1);
return;
}
self.last.clear();
self.last.extend(view);
self.rounds = 0;
}
}
/// Unit tests for [`Quiet`].
#[cfg(test)]
mod tests;