[PATCH] D33104: [BitVector] Implement find_[first/last]_[set/unset]_in
Zachary Turner via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu May 11 09:44:44 PDT 2017
zturner created this revision.
This effort started when I wanted a method that did the following:
Given a BitVector and indices N and M, find the largest index less than M but greater than or equal to N which contains a set bit.
At first I just used `find_prev(M)` which I had added in an earlier patch, but I quickly discovered the performance of this was poor. My `BitVector` had over 500,000 bits in it, and in the worst case this could cause an O(n) scan all the way to the beginning. But I already had a lower bound on the range I wanted to search in, so I figured I could just make a function `find_last_in(LowerBound, PriorTo)`.
But it quickly became apparent that if we already have this function, then `find_last()` is just `find_last_in(0, Size)` and `find_prev(PriorTo)` is just `find_last_in(0, PriorTo)`.
In fact, all of the methods are generalizable this way. For example, `find_first` and `find_next` reduce to `find_first_in(0, Size)` and `find_first_in(After, Size)`. The generalizations to the `find_xxx_unset` family of methods is obvious.
So I went all the way. There is now a `find_in` method for all combinations of `[first, last] x [set, unset]`, and all `find` methods delegate to these methods.
https://reviews.llvm.org/D33104
Files:
llvm/include/llvm/ADT/BitVector.h
llvm/unittests/ADT/BitVectorTest.cpp
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D33104.98647.patch
Type: text/x-patch
Size: 15129 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170511/65580b95/attachment.bin>
More information about the llvm-commits
mailing list