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
//! Reduction primitives for global ECS observations.
//!
//! This module defines **pure, thread-local accumulator types** intended for
//! use with the ECS reduction APIs (e.g. [`crate::ECSReference::reduce_abstraction`]
//! and [`crate::ECSReference::reduce_read`]).
//!
//! ## Purpose
//! Reductions compute **summary statistics** over large populations of entities
//! without mutating ECS state. Common use cases include:
//!
//! * population counts,
//! * totals and aggregates,
//! * minima / maxima,
//! * means and variances,
//! * convergence and equilibrium checks,
//! * diagnostics and model instrumentation.
//!
//! ## Execution model
//! A reduction proceeds in two phases:
//!
//! 1. **Parallel accumulation**
//! * Each worker thread processes a disjoint subset of archetype chunks.
//! * Each thread maintains its own accumulator value.
//!
//! 2. **Deterministic combination**
//! * Thread-local accumulators are merged using an associative `combine`
//! operation.
//! * Combination order is deterministic and independent of thread count.
//!
//! This model scales efficiently on multicore CPUs and maps directly to
//! GPU-style block reductions.
//!
//! ## Design principles
//! The accumulator types in this module are intentionally:
//!
//! * **Plain data containers** - no ECS references or side effects.
//! * **Copy / Clone** - easy to move between threads.
//! * **Execution-agnostic** - usable on CPU, GPU, or in offline analysis.
//!
//! They do not depend on the scheduler, query system, or ECS internals.
//!
//! ## Provided accumulators
//!
//! * [`Count`] - counts entities.
//! * [`Sum`] - accumulates floating-point totals.
//! * [`MinMax`] - tracks minimum and maximum values.
//! * [`Welford`] - computes mean and variance using a numerically stable
//! online algorithm. Supports parallel combination via [`Welford::combine`].
//!
//!
//! ## Usage example
//!
//! ```text
//! // `ecs` is an ECSReference obtained from ECSManager::world_ref().
//! let query = ecs.query()?.read::<Wealth>()?.build()?;
//!
//! let total = ecs.reduce_read::<Wealth, Sum>(
//! query,
//! Sum::default,
//! |acc, w| acc.0 += w.amount,
//! |a, b| a.0 += b.0,
//! )?;
//! ```
//!
//! ## Safety
//! Accumulators are used exclusively within the ECS reduction APIs, which
//! enforce:
//!
//! * read-only phase discipline,
//! * iteration scope tracking,
//! * runtime borrow checking,
//! * disjoint chunk processing.
/// Accumulator that counts the number of entities processed.
///
/// ## Semantics
/// Each call to the reduction fold function typically increments the internal
/// counter by one, yielding the total number of entities matching a query.
///
/// ## Typical use cases
/// * Population size
/// * Cardinality of a query
/// * Participation counts
;
/// Accumulator that computes a floating-point sum.
///
/// ## Semantics
/// Values are accumulated using standard floating-point addition. For large
/// populations or numerically sensitive models, users may prefer more stable
/// accumulation strategies (e.g. pairwise summation or Welford-based methods).
///
/// ## Typical use cases
/// * Total wealth
/// * Aggregate production
/// * Market volume
;
/// Accumulator that tracks minimum and maximum values.
///
/// ## Semantics
/// The accumulator maintains the smallest and largest values observed during
/// reduction. The default initializer sets:
/// * `min` to positive infinity
/// * `max` to negative infinity
///
/// allowing the first observed value to establish both bounds.
///
/// ## Typical use cases
/// * Price ranges
/// * Income bounds
/// Accumulator implementing Welford's online algorithm for mean and variance.
///
/// ## Semantics
/// This accumulator computes the mean and (sample) variance of a stream of
/// values in a numerically stable, single-pass manner.
///
/// It supports:
/// * incremental updates via [`Welford::push`],
/// * parallel combination of partial accumulators via [`Welford::combine`],
/// * stable results independent of iteration order.
///
/// ## Typical use cases
/// * Average income or wealth
/// * Inequality and dispersion metrics
/// * Convergence diagnostics
///
/// ## References
/// * Welford, B. P. (1962). *Note on a method for calculating corrected sums of
/// squares and products*.
/// * Chan, T. F., Golub, G. H., LeVeque, R. J. (1979). *Updating formulae and a
/// pairwise algorithm for computing sample variances*.