mirror of
https://github.com/sourcegraph/sourcegraph.git
synced 2026-02-06 19:21:50 +00:00
rockskip: ruler function is just bits.TrailingZeros (#62567)
I couldn't help but make this change after seeing the algorithm. Feel free to just close this if this makes it less clear, but this kinda feels clearer in my head but I am probably weird. Test Plan: TestRuler passes
This commit is contained in:
parent
68fe349b01
commit
1515512ec2
@ -3,6 +3,7 @@ package rockskip
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"math/bits"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@ -168,10 +169,11 @@ func getHops(ctx context.Context, tx dbutil.DB, commit int, tasklog *TaskLog) ([
|
||||
//
|
||||
// https://oeis.org/A007814
|
||||
func ruler(n int) int {
|
||||
height := 0
|
||||
for n > 0 && n%2 == 0 {
|
||||
height++
|
||||
n = n / 2
|
||||
if n <= 0 {
|
||||
return 0
|
||||
}
|
||||
return height
|
||||
// ruler(n) is equivalent to asking how many times can you divide n by 2
|
||||
// before you get an odd number. That is the number of 0's at the end of n
|
||||
// when n is written in base 2.
|
||||
return bits.TrailingZeros(uint(n))
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user