|
@@ -16,22 +16,14 @@ mod private {
|
|
use std::io;
|
|
use std::io;
|
|
use strum::EnumDiscriminants;
|
|
use strum::EnumDiscriminants;
|
|
|
|
|
|
- /// Returns the base 2 logarithm of the given number. This function will return -1 when given 0, and
|
|
|
|
- /// this is the only input for which a negative value is returned.
|
|
|
|
- pub(super) fn log2(mut n: usize) -> isize {
|
|
|
|
- // Is there a better implementation of this in std? I wasn't able to find an integer log2
|
|
|
|
- // function in std, so I wrote this naive implementation.
|
|
|
|
|
|
+ /// Returns the base 2 logarithm of the given number. This function will return -1 when given 0,
|
|
|
|
+ /// and this is the only input for which a negative value is returned.
|
|
|
|
+ pub(super) fn log2(n: usize) -> isize {
|
|
if 0 == n {
|
|
if 0 == n {
|
|
- return -1;
|
|
|
|
- }
|
|
|
|
- let num_bits = usize::BITS.try_into().unwrap();
|
|
|
|
- for k in 0..num_bits {
|
|
|
|
- n >>= 1;
|
|
|
|
- if 0 == n {
|
|
|
|
- return k;
|
|
|
|
- }
|
|
|
|
|
|
+ -1
|
|
|
|
+ } else {
|
|
|
|
+ n.ilog2().try_into().unwrap()
|
|
}
|
|
}
|
|
- num_bits
|
|
|
|
}
|
|
}
|
|
|
|
|
|
/// Returns 2^x. Note that 0 is returned for any negative input.
|
|
/// Returns 2^x. Note that 0 is returned for any negative input.
|