Class: Ferret::Utils::BitVector

Inherits:
Object
  • Object
show all
Defined in:
ext/r_utils.c

Overview

Summary

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.

Boolean Operations

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;

  • and!

  • or!

  • xor!

  • not!

    bv4.and!(bv5).not!
    

Set Bit Scanning

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).