Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add iterator starting at a given value #54

Merged
merged 1 commit into from
Nov 20, 2019
Merged

Add iterator starting at a given value #54

merged 1 commit into from
Nov 20, 2019

Conversation

michaelmior
Copy link
Contributor

@michaelmior michaelmior commented Nov 20, 2019

This is a follow up to #53 which implements next_set_biton top of an iterator for equal or larger. I'm not sure I'm happy with the naming and I expect the implementation may need some tweaks, but it works :)

@Ezibenroc
Copy link
Owner

Thank you, great PR!

Correct me if I am wrong, but I think that you can already achieve a similar thing with the rank() method and slices:

bm = BitMap(range(0, 100, 3))
for i in range(1, 100):
    assert list(bm[bm.rank(i-1):]) == list(bm.iter_equal_or_larger(i))
    assert bm[bm.rank(i-1)] == bm.next_set_bit(i)

So, the main benefits of these new methods are:

  • Only one search in the bitmap instead of two
  • Lazy evaluation with the iterator for iter_equal_or_larger, so more efficient if we do not need all the values (but if we do need all the values as a bitmap, the rank() solution is probably much better).

In my first (quick) tests, it seems great for next_set_bit:

bm = BitMap(range(0, 100000000, 3))

%timeit bm[bm.rank(1234567-1)]
4.88 µs ± 43.7 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

%timeit bm.next_set_bit(1234567)
482 ns ± 2.67 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

For iter_equal_or_larger, I do not think there is a fair comparison possible.


Before I merge, could you add some documentation for iter_equal_or_larger?

@michaelmior
Copy link
Contributor Author

@Ezibenroc Good point that this could be done with rank and thanks for the benchmark! I added the extra documentation.

@Ezibenroc Ezibenroc merged commit 9a8bf41 into Ezibenroc:master Nov 20, 2019
@Ezibenroc
Copy link
Owner

Thanks!

@michaelmior
Copy link
Contributor Author

Thanks for the merge!

@michaelmior michaelmior deleted the next-set-bit branch November 20, 2019 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants