Class | Ferret::Utils::BitVector |
In: |
ext/r_utils.c
|
Parent: | Object |
A BitVector is pretty easy to implement in Ruby using Ruby‘s BigNum class. This BitVector however allows you to count the set bits with the +count+ method (or unset bits of flipped bit vectors) and also to quickly scan the set bits.
BitVector handles four boolean operations;
bv1 = BitVector.new bv2 = BitVector.new bv3 = BitVector.new bv4 = (bv1 & bv2) | ~bv3
You can also do the operations in-place;
Perhaps the most useful functionality in BitVector is the ability to quickly scan for set bits. To print all set bits;
bv.each {|bit| puts bit }
Alternatively you could use the lower level next or next_unset methods. Note that the each method will automatically scan unset bits if the BitVector has been flipped (using not).
Returns the next set bit in the bit vector scanning from low order to high order. You should call +reset_scan+ before calling this method if you want to scan from the beginning. It is automatically reset when you first create the bit vector.
Returns the next unset bit in the bit vector scanning from low order to high order. This method should only be called on bit vectors which have been flipped (negated). You should call +reset_scan+ before calling this method if you want to scan from the beginning. It is automatically reset when you first create the bit vector.
Iterate through all the set bits in the bit vector adding the index of each set bit to an array. This is useful if you want to perform array methods on the bit vector. If you want to convert an array to a bit_vector simply do this;
bv = [1, 12, 45, 367, 455].inject(BitVector.new) {|bv, i| bv.set(i)}