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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
use std::fmt;
use hex_slice::AsHex;
use rand::{random, Rng, Rand};
#[derive(Clone, Default, PartialEq)]
pub struct InfoPool {
data: Vec<u8>,
}
#[derive(Clone, Default)]
pub struct InfoTap<'a> {
data: &'a [u8],
off: usize,
}
impl fmt::Debug for InfoPool {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("InfoPool")
.field("data", &format_args!("{:x}", self.data.as_hex()))
.finish()
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum DataError {
PoolExhausted,
SkipItem,
}
pub type Maybe<T> = Result<T, DataError>;
impl InfoPool {
pub fn of_vec(data: Vec<u8>) -> Self {
InfoPool { data: data }
}
pub fn random_of_size(size: usize) -> Self {
Self::of_vec((0..size).map(|_| random()).collect::<Vec<u8>>())
}
pub fn from_random_of_size<R: Rng>(rng: &mut R, size: usize) -> Self {
Self::of_vec(
(0..size)
.map(|_| (u64::rand(rng) >> 56) as u8)
.collect::<Vec<u8>>(),
)
}
pub fn buffer(&self) -> &[u8] {
&*self.data
}
pub fn tap(&self) -> InfoTap {
InfoTap {
data: &*self.data,
off: 0,
}
}
}
impl<'a> InfoTap<'a> {
pub fn next_byte(&mut self) -> Maybe<u8> {
let res = self.data.get(self.off).cloned();
self.off += 1;
res.ok_or(DataError::PoolExhausted)
}
}
impl<'a> Iterator for InfoTap<'a> {
type Item = u8;
fn next(&mut self) -> Option<u8> {
self.next_byte().ok()
}
}
fn minimize_via_removal<F: Fn(InfoTap) -> bool>(
p: &InfoPool,
candidate: &mut InfoPool,
pred: &F,
) -> Option<InfoPool> {
trace!("minimizing by removal: {:?}", p);
let max_pow = 0usize.count_zeros();
let pow = max_pow - p.data.len().leading_zeros();
for granularity in 0..pow {
let width = p.data.len() >> granularity;
for chunk in 0..(1 << granularity) {
let start = chunk * width;
let end = start + width;
candidate.data.clear();
candidate.data.extend(&p.data[0..start]);
candidate.data.extend(&p.data[end..]);
let test = pred(candidate.tap());
trace!(
"removed {},{}: {:?}; test result {}",
start,
end,
candidate,
test
);
if test {
if let Some(res) = minimize(&candidate, pred) {
trace!("Returning shrunk: {:?}", res);
return Some(res);
} else {
trace!("Returning original: {:?}", candidate);
return Some(candidate.clone());
}
}
}
}
None
}
fn minimize_via_scalar_shrink<F: Fn(InfoTap) -> bool>(
p: &InfoPool,
candidate: &mut InfoPool,
pred: &F,
) -> Option<InfoPool> {
trace!("minimizing by scalar shrink: {:?}", p);
for i in 0..p.data.len() {
candidate.clone_from(&p);
for bitoff in 0..8 {
candidate.data[i] = p.data[i] - (p.data[i] >> bitoff);
trace!(
"shrunk item -(bitoff:{}) {} {}->{}: {:?}",
bitoff,
i,
p.data[i],
candidate.data[i],
candidate
);
if candidate.buffer() == p.buffer() {
trace!("No change");
continue;
}
let test = pred(candidate.tap());
trace!("test result {}", test);
if test {
if let Some(res) = minimize(&candidate, pred) {
trace!("Returning shrunk: {:?}", res);
return Some(res);
} else {
trace!("Returning original: {:?}", candidate);
return Some(candidate.clone());
}
}
}
}
None
}
pub fn minimize<F: Fn(InfoTap) -> bool>(p: &InfoPool, pred: &F) -> Option<InfoPool> {
let mut candidate = p.clone();
if let Some(res) = minimize_via_removal(p, &mut candidate, pred) {
return Some(res);
}
if let Some(res) = minimize_via_scalar_shrink(p, &mut candidate, pred) {
return Some(res);
}
trace!("Nothing smaller found than {:?}", p);
None
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn should_take_each_item_in_pool() {
let p = InfoPool::of_vec(vec![0, 1, 2, 3]);
let mut t = p.tap();
assert_eq!(t.next_byte(), Ok(0));
assert_eq!(t.next_byte(), Ok(1));
assert_eq!(t.next_byte(), Ok(2));
assert_eq!(t.next_byte(), Ok(3));
assert_eq!(t.next_byte(), Err(DataError::PoolExhausted));
}
#[test]
fn should_generate_random_data_of_size() {
let size = 100;
let p = InfoPool::random_of_size(size);
let mut t = p.tap();
for _ in 0..size {
assert!(t.next_byte().is_ok());
}
assert!(t.next_byte().is_err());
}
#[test]
fn should_allow_restarting_read() {
let p = InfoPool::random_of_size(4);
let mut t = p.tap();
let mut v0 = Vec::new();
while let Ok(val) = t.next_byte() {
v0.push(val)
}
let mut t = p.tap();
let mut v1 = Vec::new();
while let Ok(val) = t.next_byte() {
v1.push(val)
}
assert_eq!(v0, v1)
}
#[test]
fn should_allow_borrowing_buffer() {
let p = InfoPool::of_vec(vec![1]);
assert_eq!(p.buffer(), &[1]);
}
#[test]
fn tap_can_act_as_iterator() {
let buf = vec![4, 3, 2, 1];
let p = InfoPool::of_vec(buf.clone());
let _: &Iterator<Item = u8> = &p.tap();
assert_eq!(p.tap().collect::<Vec<_>>(), buf)
}
#[test]
fn minimiser_should_minimise_to_empty() {
let p = InfoPool::of_vec(vec![1]);
let min = minimize(&p, &|_| true);
assert_eq!(min.as_ref().map(|p| p.buffer()), Some([].as_ref()))
}
#[test]
fn minimiser_should_minimise_to_minimum_given_size() {
let p = InfoPool::of_vec(vec![0; 4]);
let min = minimize(&p, &|t| t.count() > 1).expect("some smaller pool");
assert_eq!(min.buffer(), &[0, 0])
}
#[test]
fn minimiser_should_minimise_scalar_values() {
let p = InfoPool::of_vec(vec![255; 3]);
let min = minimize(&p, &|mut t| t.any(|v| v >= 3)).expect("some smaller pool");
assert_eq!(min.buffer(), &[3])
}
#[test]
fn minimiser_should_minimise_scalar_values_to_zero() {
let p = InfoPool::of_vec(vec![255; 3]);
let min = minimize(&p, &|mut t| t.any(|_| true)).expect("some smaller pool");
assert_eq!(min.buffer(), &[0])
}
#[test]
fn minimiser_should_minimise_scalar_values_by_search() {
let p = InfoPool::of_vec(vec![255; 3]);
let min = minimize(&p, &|mut t| t.any(|v| v >= 13)).expect("some smaller pool");
assert_eq!(min.buffer(), &[13])
}
#[test]
fn minimiser_should_minimise_scalar_values_accounting_for_overflow() {
let p = InfoPool::of_vec(vec![255; 3]);
let min = minimize(&p, &|mut t| t.any(|v| v >= 251)).expect("some smaller pool");
assert_eq!(min.buffer(), &[251])
}
}