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
/*
* SPDX-FileCopyrightText: 2025 Inria
* SPDX-FileCopyrightText: 2025 Sebastiano Vigna
*
* SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
*/
//! Fair chunk iteration over weighted integers.
use FusedIterator;
use crate;
/// An iterator returning fair chunks.
///
/// Given positive integer weights on the first `n` integers, this iterator
/// returns chunks of integers (in the form of ranges) such that the sum of the
/// weights in each chunk is approximately the same, and approximately equal to
/// a provided target weight.
///
/// To build an iterator, you need to provide the cumulative function of weights
/// (i.e., the sequence 0, `w₀`, `w₀ + w₁`, `w₀ + w₁ + w₂`, …), stored in a
/// structure that supports [(unchecked) successor queries].
///
/// [(unchecked) successor queries]: crate::traits::Succ
///
/// # Example
///
/// ```rust
/// # use sux::prelude::*;
/// # use sux::utils::FairChunks;
/// // The weights of our elements
/// let weights = [
/// 15u64, 27, 20, 26, 4, 22, 10, 25, 7, 13, 0, 11, 5, 28, 23,
/// 1, 12, 24, 3, 30, 8, 29, 17, 2, 14, 9, 16, 18, 21, 19,
/// ];
/// // Compute the cumulative weight function
/// let mut cwf = vec![0u64];
/// cwf.extend(weights.iter().scan(0u64, |acc, x| {
/// *acc += x;
/// Some(*acc)
/// }));
/// // Put the sequence in an Elias–Fano structure
/// let mut efb = EliasFanoBuilder::new(cwf.len(), *cwf.last().unwrap());
/// efb.extend(cwf);
/// let ef = efb.build_with_seq_and_dict();
/// // Create the iterator
/// let chunks = FairChunks::new(50, &ef);
/// // The weight of the chunks is balanced
/// assert_eq!(
/// chunks.collect::<Vec<_>>(),
/// vec![
/// 0..3, // weight 62
/// 3..6, // weight 52
/// 6..10, // weight 55
/// 10..15, // weight 67
/// 15..20, // weight 70
/// 20..23, // weight 54
/// 23..28, // weight 59
/// 28..30, // weight 40
/// ],
/// );
///
/// // To save memory, we can build a smaller Elias–Fano structure
/// // only supporting unchecked successor queries
/// let mut cwf = vec![0u64];
/// cwf.extend(weights.iter().scan(0u64, |acc, x| {
/// *acc += x;
/// Some(*acc)
/// }));
/// let last_cwf = *cwf.last().unwrap();
/// let mut efb = EliasFanoBuilder::new(cwf.len(), last_cwf);
/// efb.extend(cwf);
/// let ef = efb.build_with_dict();
/// let chunks = FairChunks::new_with(50, &ef, weights.len(), last_cwf);
/// assert_eq!(
/// chunks.collect::<Vec<_>>(),
/// vec![
/// 0..3, // weight 62
/// 3..6, // weight 52
/// 6..10, // weight 55
/// 10..15, // weight 67
/// 15..20, // weight 70
/// 20..23, // weight 54
/// 23..28, // weight 59
/// 28..30, // weight 40
/// ],
/// );
/// ```