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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//! Early stopping and time slicing: the [`Budget`] trait over a crate's own statistics type,
//! the never-exhausted budget [`Unlimited`], and the [`Status`] a sliced search reports.
/// Decides when a search must stop before it finishes, given its counters so far.
///
/// `T` is the statistics type of the algorithm, such as a struct with `expanded` and
/// `generated` counters. Each search crate defines its own statistics and limits (the
/// counters differ between graph search and MDP solvers) and implements `Budget<Stats>` for
/// them; this trait lets budget adapters, such as a wall-clock deadline in a `std` crate, be
/// written once for every algorithm.
///
/// Algorithms ask the budget before each unit of work, so a limit of `n` stops after exactly
/// `n` units. In a time-sliced search the counters start from zero in each start or resume
/// call, so a budget is the allowance of one slice (see [`Status`]). Budgets are taken by
/// value; to keep a struct budget's state across slices, pass a closure that borrows it:
/// `&mut |s: &T| limits.exhausted(s)`.
///
/// Implemented for every `FnMut(&T) -> bool`; annotate the closure argument
/// (`|stats: &Stats| ...`), since the type cannot be inferred through the blanket impl.
///
/// # Examples
///
/// ```
/// use search_core::Budget;
///
/// /// The counters of some search.
/// struct Stats {
/// expanded: u64,
/// }
///
/// /// A search crate's limits.
/// struct Limits {
/// max_expansions: Option<u64>,
/// }
///
/// impl Budget<Stats> for Limits {
/// fn exhausted(&mut self, stats: &Stats) -> bool {
/// self.max_expansions.is_some_and(|max| stats.expanded >= max)
/// }
/// }
///
/// fn stops(mut budget: impl Budget<Stats>, expanded: u64) -> bool {
/// budget.exhausted(&Stats { expanded })
/// }
///
/// assert!(stops(Limits { max_expansions: Some(3) }, 3));
/// assert!(!stops(Limits { max_expansions: None }, 3));
/// assert!(stops(|stats: &Stats| stats.expanded > 1, 2));
/// ```
/// A budget that is never exhausted: the search runs until it finishes.
///
/// # Examples
///
/// ```
/// use search_core::{Budget, Unlimited};
///
/// assert!(!Unlimited.exhausted(&u64::MAX));
/// ```
;
/// How one call of a time-sliced search ended: it finished, or its [`Budget`] stopped it.
///
/// A search that can be spread over several frames returns this from its start and resume
/// calls. After [`Status::OutOfBudget`], the search keeps its state in its workspace, and
/// the crate's resume call continues where it stopped. The resume call itself is
/// crate-specific, since what a search needs to continue differs between algorithms.
///
/// Non-exhaustive, so that searches can gain further outcomes without a breaking change;
/// matches outside this crate need a `_` arm.
///
/// # Examples
///
/// A countdown sliced over two frames, then run to the end:
///
/// ```
/// use search_core::{Budget, Status, Unlimited};
///
/// /// A resumable search: counts `left` down to zero, one unit of work per step.
/// struct Countdown {
/// left: u32,
/// }
///
/// impl Countdown {
/// /// Continues the run. The budget sees the units done in this call only.
/// fn resume(&mut self, mut budget: impl Budget<u32>) -> Status {
/// let mut done = 0;
/// while self.left > 0 {
/// if budget.exhausted(&done) {
/// return Status::OutOfBudget;
/// }
///
/// self.left -= 1;
/// done += 1;
/// }
///
/// Status::Finished
/// }
/// }
///
/// let mut search = Countdown { left: 5 };
/// let mut frames = 0;
/// let mut per_frame = |done: &u32| {
/// frames += 1;
/// *done >= 2
/// };
///
/// assert_eq!(search.resume(&mut per_frame), Status::OutOfBudget);
/// assert_eq!(search.left, 3);
/// assert_eq!(search.resume(&mut per_frame), Status::OutOfBudget);
/// assert_eq!(search.left, 1);
/// assert_eq!(search.resume(Unlimited), Status::Finished);
/// assert_eq!(frames, 6);
/// ```