replaced linear search with binary search in run container

This commit is contained in:
Todd Gruben 2017-06-14 16:48:54 -05:00 • committed by Matt Jaffee
parent ccef8be844
commit 6609120464
2 changed files with 15 additions and 12 deletions

View file

@ -1337,18 +1337,22 @@ func (c *container) bitmapContains(v uint32) bool {
return (c.bitmap[v/64] & (1 << uint64(v%64))) != 0
}
func (c *container) runContains(v uint32) bool {
// TODO binary search
for _, iv := range c.runs {
if v > iv.last {
continue
}
if v < iv.start {
return false
}
return true
//runBinSearch uses a binary search of the runs and returns the index of nearest
//run.
func runBinSearch(v uint32, a []interval32) (int, bool) {
i := sort.Search(len(a),
func(i int) bool { return a[i].last >= v })
if i < len(a) {
return i, (v >= a[i].start) && (v <= a[i].last)
}
return false
return i, false
}
//runContains determines if v is in the containers run set.
func (c *container) runContains(v uint32) bool {
_, found := runBinSearch(v, c.runs)
return found
}
// remove removes a value from the container.

View file

@ -360,7 +360,6 @@ func testBitmapQuick(t *testing.T, n int, min, max uint64) {
}
// Remove all values in random order.
fmt.Println(len(a))
for _, i := range rand.Perm(len(a)) {
removed, _ := bm.Remove(a[i])
if removed {