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
/* Collector traits for grouping and aggregating entities.
Collectors aggregate entities within groups during `group_by()` operations.
They maintain incremental state for O(1) insert/retract operations.
*/
/* A collector that aggregates entities of type `A` into a result of type `R`.
Collectors are used in `group_by()` operations to reduce groups of entities
into summary values.
# Zero-Erasure Design
The collector owns any mapping functions and provides `extract()` to convert
entities to values. The accumulator only works with extracted values, avoiding
the need to clone mapping functions into each accumulator.
# Incremental Protocol
Collectors support incremental updates:
1. `create_accumulator()` creates a fresh accumulator
2. `extract(entity)` converts entity to accumulator value
3. `accumulate(value)` adds value to accumulator
4. `retract(value)` removes value from accumulator
5. `finish()` produces the final result
This enables O(1) score updates when entities are added/removed from groups.
*/
/* An accumulator that incrementally collects values.
Values are extracted by the collector's `extract()` method before
being passed to the accumulator by reference.
*/