Skip to content

Commit 3c679f1

Browse files
committed
refactor: use usize::ilog2 instead of handwritten
1 parent 2ab9574 commit 3c679f1

File tree

1 file changed

+3
-42
lines changed

1 file changed

+3
-42
lines changed

src/least_satisfying.rs

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ where
88
{
99
let mut cache = BTreeMap::new();
1010
let mut predicate = |idx: usize, rm_no, lm_yes| {
11-
let range = lm_yes - rm_no + 1;
11+
let range: usize = lm_yes - rm_no + 1;
1212
// FIXME: This does not consider unknown_ranges.
1313
let remaining = range / 2;
14-
let estimate = estimate_steps(range);
14+
let estimate = if range < 3 { 0 } else { range.ilog2() as usize };
1515
*cache
1616
.entry(idx)
1717
.or_insert_with(|| predicate(&slice[idx], remaining, estimate))
@@ -76,33 +76,10 @@ where
7676
}
7777
}
7878

79-
fn estimate_steps(range: usize) -> usize {
80-
// Replace with int_log when it is stabilized.
81-
let log2 = |mut n| {
82-
let mut r = 0;
83-
while n > 1 {
84-
r += 1;
85-
n >>= 1;
86-
}
87-
r
88-
};
89-
if range < 3 {
90-
return 0;
91-
}
92-
let n = log2(range);
93-
let e = 1 << n;
94-
let x = range - e;
95-
if e < 3 * x {
96-
n
97-
} else {
98-
n - 1
99-
}
100-
}
101-
10279
#[cfg(test)]
10380
mod tests {
10481
use super::Satisfies::{No, Unknown, Yes};
105-
use super::{estimate_steps, least_satisfying, Satisfies};
82+
use super::{least_satisfying, Satisfies};
10683
use quickcheck::{QuickCheck, TestResult};
10784

10885
fn prop(xs: Vec<Option<bool>>) -> TestResult {
@@ -186,22 +163,6 @@ mod tests {
186163
fn qc_prop() {
187164
QuickCheck::new().quickcheck(prop as fn(_) -> _);
188165
}
189-
190-
#[test]
191-
fn estimates() {
192-
for (n, expect) in &[
193-
(0, 0),
194-
(1, 0),
195-
(2, 0),
196-
(3, 1),
197-
(4, 1),
198-
(5, 1),
199-
(6, 2),
200-
(150, 6),
201-
] {
202-
assert_eq!(estimate_steps(*n), *expect);
203-
}
204-
}
205166
}
206167

207168
#[derive(Copy, Clone, Debug, PartialEq, Eq)]

0 commit comments

Comments
 (0)