Skip to content

Commit 8227356

Browse files
committed
refactor least_satisfying to remove start/end bounds checks
1 parent 85610b4 commit 8227356

File tree

1 file changed

+24
-36
lines changed

1 file changed

+24
-36
lines changed

src/least_satisfying.rs

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,21 @@
88
use std::collections::BTreeMap;
99
use std::fmt;
1010

11-
pub fn least_satisfying<T, P>(slice: &[T], start_check: bool, mut predicate: P) -> usize
11+
pub fn least_satisfying<T, P>(slice: &[T], mut predicate: P) -> usize
1212
where
1313
T: fmt::Display + fmt::Debug,
1414
P: FnMut(&T) -> Satisfies,
1515
{
1616
let mut cache = BTreeMap::new();
1717
let mut predicate = |idx: usize| *cache.entry(idx).or_insert_with(|| predicate(&slice[idx]));
1818
let mut unknown_ranges: Vec<(usize, usize)> = Vec::new();
19-
let mut rm_no = 0; // presume that the slice starts with a no
19+
// presume that the slice starts with a no
20+
// this should be tested before call
21+
let mut rm_no = 0;
2022

21-
if start_check {
22-
eprintln!("verifying the start of the range does not reproduce the regression");
23-
match predicate(rm_no) {
24-
Satisfies::No => {
25-
eprintln!("confirmed the start of the range does not reproduce the regression")
26-
}
27-
_ => panic!("the start of the range to test must not reproduce the regression"),
28-
}
29-
}
30-
31-
let mut lm_yes = slice.len() - 1; // presume that the slice ends with a yes
32-
33-
eprintln!("verifying the end of the range reproduces the regression");
34-
match predicate(lm_yes) {
35-
Satisfies::Yes => eprintln!("confirmed the end of the range reproduces the regression"),
36-
_ => panic!("the end of the range to test must reproduce the regression"),
37-
}
23+
// presume that the slice ends with a yes
24+
// this should be tested before the call
25+
let mut lm_yes = slice.len() - 1;
3826

3927
let mut next = (rm_no + lm_yes) / 2;
4028

@@ -105,111 +93,111 @@ mod tests {
10593
}
10694
}
10795

108-
let res = least_satisfying(&satisfies_v, true, |i| *i);
96+
let res = least_satisfying(&satisfies_v, |i| *i);
10997
let exp = first_yes.unwrap();
11098
TestResult::from_bool(res == exp)
11199
}
112100

113101
#[test]
114102
fn least_satisfying_1() {
115103
assert_eq!(
116-
least_satisfying(&[No, Unknown, Unknown, No, Yes], true,|i| *i),
104+
least_satisfying(&[No, Unknown, Unknown, No, Yes],|i| *i),
117105
4
118106
);
119107
}
120108

121109
#[test]
122110
fn least_satisfying_1f() {
123111
assert_eq!(
124-
least_satisfying(&[No, Unknown, Unknown, No, Yes], false,|i| *i),
112+
least_satisfying(&[No, Unknown, Unknown, No, Yes],|i| *i),
125113
4
126114
);
127115
}
128116

129117
#[test]
130118
fn least_satisfying_2() {
131119
assert_eq!(
132-
least_satisfying(&[No, Unknown, Yes, Unknown, Yes], true, |i| *i),
120+
least_satisfying(&[No, Unknown, Yes, Unknown, Yes], |i| *i),
133121
2
134122
);
135123
}
136124

137125
#[test]
138126
fn least_satisfying_2f() {
139127
assert_eq!(
140-
least_satisfying(&[No, Unknown, Yes, Unknown, Yes], false, |i| *i),
128+
least_satisfying(&[No, Unknown, Yes, Unknown, Yes], |i| *i),
141129
2
142130
);
143131
}
144132

145133
#[test]
146134
fn least_satisfying_3() {
147-
assert_eq!(least_satisfying(&[No, No, No, No, Yes], true, |i| *i), 4);
135+
assert_eq!(least_satisfying(&[No, No, No, No, Yes], |i| *i), 4);
148136
}
149137

150138
#[test]
151139
fn least_satisfying_3f() {
152-
assert_eq!(least_satisfying(&[No, No, No, No, Yes], false, |i| *i), 4);
140+
assert_eq!(least_satisfying(&[No, No, No, No, Yes], |i| *i), 4);
153141
}
154142

155143
#[test]
156144
fn least_satisfying_4() {
157-
assert_eq!(least_satisfying(&[No, No, Yes, Yes, Yes], true, |i| *i), 2);
145+
assert_eq!(least_satisfying(&[No, No, Yes, Yes, Yes], |i| *i), 2);
158146
}
159147

160148
#[test]
161149
fn least_satisfying_4f() {
162-
assert_eq!(least_satisfying(&[No, No, Yes, Yes, Yes], false, |i| *i), 2);
150+
assert_eq!(least_satisfying(&[No, No, Yes, Yes, Yes], |i| *i), 2);
163151
}
164152

165153
#[test]
166154
fn least_satisfying_5() {
167-
assert_eq!(least_satisfying(&[No, Yes, Yes, Yes, Yes], true, |i| *i), 1);
155+
assert_eq!(least_satisfying(&[No, Yes, Yes, Yes, Yes], |i| *i), 1);
168156
}
169157

170158
#[test]
171159
fn least_satisfying_5f() {
172-
assert_eq!(least_satisfying(&[No, Yes, Yes, Yes, Yes], false, |i| *i), 1);
160+
assert_eq!(least_satisfying(&[No, Yes, Yes, Yes, Yes], |i| *i), 1);
173161
}
174162

175163
#[test]
176164
fn least_satisfying_6() {
177165
assert_eq!(
178-
least_satisfying(&[No, Yes, Yes, Unknown, Unknown, Yes, Unknown, Yes], true, |i| *i),
166+
least_satisfying(&[No, Yes, Yes, Unknown, Unknown, Yes, Unknown, Yes], |i| *i),
179167
1
180168
);
181169
}
182170

183171
#[test]
184172
fn least_satisfying_6f() {
185173
assert_eq!(
186-
least_satisfying(&[No, Yes, Yes, Unknown, Unknown, Yes, Unknown, Yes], false, |i| *i),
174+
least_satisfying(&[No, Yes, Yes, Unknown, Unknown, Yes, Unknown, Yes], |i| *i),
187175
1
188176
);
189177
}
190178

191179
#[test]
192180
fn least_satisfying_7() {
193-
assert_eq!(least_satisfying(&[No, Yes, Unknown, Yes], true, |i| *i), 1);
181+
assert_eq!(least_satisfying(&[No, Yes, Unknown, Yes], |i| *i), 1);
194182
}
195183

196184
#[test]
197185
fn least_satisfying_7f() {
198-
assert_eq!(least_satisfying(&[No, Yes, Unknown, Yes], false, |i| *i), 1);
186+
assert_eq!(least_satisfying(&[No, Yes, Unknown, Yes], |i| *i), 1);
199187
}
200188

201189
#[test]
202190
fn least_satisfying_8() {
203191
assert_eq!(
204-
least_satisfying(&[No, Unknown, No, No, Unknown, Yes, Yes], true,|i| *i),
192+
least_satisfying(&[No, Unknown, No, No, Unknown, Yes, Yes],|i| *i),
205193
5
206194
);
207195
}
208196

209197
#[test]
210198
fn least_satisfying_8f() {
211199
assert_eq!(
212-
least_satisfying(&[No, Unknown, No, No, Unknown, Yes, Yes], false,|i| *i),
200+
least_satisfying(&[No, Unknown, No, No, Unknown, Yes, Yes],|i| *i),
213201
5
214202
);
215203
}

0 commit comments

Comments
 (0)