diff --git a/roaring/roaring.go b/roaring/roaring.go index 5fc61f835..bff94ad26 100644 --- a/roaring/roaring.go +++ b/roaring/roaring.go @@ -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. diff --git a/roaring/roaring_test.go b/roaring/roaring_test.go index 07fd99d84..40f6c515d 100644 --- a/roaring/roaring_test.go +++ b/roaring/roaring_test.go @@ -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 {