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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* Selection order configuration for selectors.
Defines the order in which elements are selected from a selector.
*/
/* Defines the order in which elements are selected from a selector.
This enum controls how entities, values, or moves are ordered when
iterating through a selector.
*/
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum SelectionOrder {
/* Inherit the selection order from the parent configuration.
If the parent is cached, defaults to `Original`.
If there is no parent, defaults to `Random`.
*/
#[default]
Inherit,
/* Select elements in their original order.
Elements are returned in the order they appear in the underlying
collection. This is deterministic and reproducible.
*/
Original,
/* Select elements in random order without shuffling.
Elements are selected randomly from the pool on each call to next().
The same element may be selected multiple times.
This scales well because it does not require caching.
*/
Random,
/* Select elements in random order by shuffling.
Elements are shuffled when a selection iterator is created.
Each element will be selected exactly once (if all elements are consumed).
Requires caching (at least step-level).
*/
Shuffled,
/* Select elements in sorted order.
Elements are sorted according to a sorter before iteration.
Each element will be selected exactly once (if all elements are consumed).
Requires caching (at least step-level).
*/
Sorted,
/* Select elements based on probability weights.
Elements with higher probability have a greater chance of being selected.
The same element may be selected multiple times.
Requires caching (at least step-level).
*/
Probabilistic,
}
impl SelectionOrder {
/// Resolves the selection order by inheriting from a parent if necessary.
///
/// # Arguments
///
/// * `inherited` - The selection order to inherit from if this is `Inherit`
///
/// # Returns
///
/// The resolved selection order (never `Inherit`)
pub fn resolve(self, inherited: SelectionOrder) -> SelectionOrder {
match self {
SelectionOrder::Inherit => {
if inherited == SelectionOrder::Inherit {
SelectionOrder::Random
} else {
inherited
}
}
other => other,
}
}
/// Returns `true` if this selection order implies random selection.
///
/// This is used to determine whether a selector should use random iteration
/// or deterministic iteration.
pub fn is_random(&self) -> bool {
matches!(
self,
SelectionOrder::Random | SelectionOrder::Shuffled | SelectionOrder::Probabilistic
)
}
/// Returns `true` if this selection order requires caching.
///
/// Some selection orders need to collect all elements before iteration
/// can begin (e.g., Shuffled, Sorted, Probabilistic).
pub fn requires_caching(&self) -> bool {
matches!(
self,
SelectionOrder::Shuffled | SelectionOrder::Sorted | SelectionOrder::Probabilistic
)
}
/// Converts from a boolean random selection flag.
///
/// # Arguments
///
/// * `random` - `true` for `Random`, `false` for `Original`
pub fn from_random_selection(random: bool) -> Self {
if random {
SelectionOrder::Random
} else {
SelectionOrder::Original
}
}
/// Converts to a boolean random selection flag.
///
/// # Panics
///
/// Panics if this is not `Random` or `Original`.
pub fn to_random_selection(&self) -> bool {
match self {
SelectionOrder::Random => true,
SelectionOrder::Original => false,
_ => panic!(
"Selection order {:?} cannot be converted to a random selection boolean",
self
),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_resolve_inherit_from_random() {
let order = SelectionOrder::Inherit;
assert_eq!(
order.resolve(SelectionOrder::Random),
SelectionOrder::Random
);
}
#[test]
fn test_resolve_inherit_from_original() {
let order = SelectionOrder::Inherit;
assert_eq!(
order.resolve(SelectionOrder::Original),
SelectionOrder::Original
);
}
#[test]
fn test_resolve_inherit_from_inherit() {
let order = SelectionOrder::Inherit;
// When inheriting from Inherit, default to Random
assert_eq!(
order.resolve(SelectionOrder::Inherit),
SelectionOrder::Random
);
}
#[test]
fn test_resolve_non_inherit() {
let order = SelectionOrder::Original;
// Non-inherit should not change
assert_eq!(
order.resolve(SelectionOrder::Random),
SelectionOrder::Original
);
}
#[test]
fn test_is_random() {
assert!(SelectionOrder::Random.is_random());
assert!(SelectionOrder::Shuffled.is_random());
assert!(SelectionOrder::Probabilistic.is_random());
assert!(!SelectionOrder::Original.is_random());
assert!(!SelectionOrder::Sorted.is_random());
assert!(!SelectionOrder::Inherit.is_random());
}
#[test]
fn test_requires_caching() {
assert!(SelectionOrder::Shuffled.requires_caching());
assert!(SelectionOrder::Sorted.requires_caching());
assert!(SelectionOrder::Probabilistic.requires_caching());
assert!(!SelectionOrder::Original.requires_caching());
assert!(!SelectionOrder::Random.requires_caching());
assert!(!SelectionOrder::Inherit.requires_caching());
}
#[test]
fn test_from_random_selection() {
assert_eq!(
SelectionOrder::from_random_selection(true),
SelectionOrder::Random
);
assert_eq!(
SelectionOrder::from_random_selection(false),
SelectionOrder::Original
);
}
#[test]
fn test_to_random_selection() {
assert!(SelectionOrder::Random.to_random_selection());
assert!(!SelectionOrder::Original.to_random_selection());
}
#[test]
#[should_panic(expected = "cannot be converted")]
fn test_to_random_selection_panics_on_shuffled() {
SelectionOrder::Shuffled.to_random_selection();
}
#[test]
fn test_default() {
assert_eq!(SelectionOrder::default(), SelectionOrder::Inherit);
}
}