Skip to content

Commit

Permalink
adding xor and and-not to BitSet; unit tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
Petko Nikolov committed Mar 18, 2014
1 parent af7f2f1 commit 4e1df43
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class BitSet(numBits: Int) extends Serializable {
}

/**
* Compute the bit-wise OR of the two sets returning the
* Compute the bit-wise OR of the two sets returning the
* result.
*/
def |(other: BitSet): BitSet = {
Expand All @@ -88,6 +88,53 @@ class BitSet(numBits: Int) extends Serializable {
newBS
}


/**
* Compute the symmetric difference by performing bit-wise XOR of the two sets returning the
* result.
*/
def ^(other: BitSet): BitSet = {
val newBS = new BitSet(math.max(capacity, other.capacity))
assert(newBS.numWords >= numWords)
assert(newBS.numWords >= other.numWords)
val smaller = math.min(numWords, other.numWords)
var ind = 0
while( ind < smaller ) {
newBS.words(ind) = words(ind) ^ other.words(ind)
ind += 1
}
while( ind < numWords ) {
newBS.words(ind) = words(ind)
ind += 1
}
while( ind < other.numWords ) {
newBS.words(ind) = other.words(ind)
ind += 1
}
newBS
}

/**
* Compute the difference of the two sets by performing bit-wise AND-NOT returning the
* result.
*/
def &~(other: BitSet): BitSet = {
val newBS = new BitSet(math.max(capacity, other.capacity))
assert(newBS.numWords >= numWords)
assert(newBS.numWords >= other.numWords)
val smaller = math.min(numWords, other.numWords)
var ind = 0
while( ind < smaller ) {
newBS.words(ind) = words(ind) & ~other.words(ind)
ind += 1
}
while( ind < numWords ) {
newBS.words(ind) = words(ind)
ind += 1
}
newBS
}

/**
* Sets the bit at the specified index to true.
* @param index the bit index
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,45 @@ class BitSetSuite extends FunSuite {
assert(bitset.nextSetBit(96) === 96)
assert(bitset.nextSetBit(97) === -1)
}

test( "xor" ) {
val setBitsX = Seq( 0, 2, 3, 37, 41 )
val setBitsY = Seq( 0, 1, 3, 37, 38, 41, 85)
val bitsetX = new BitSet(60)
setBitsX.foreach( i => bitsetX.set(i))
val bitsetY = new BitSet(100)
setBitsY.foreach( i => bitsetY.set(i))

val bitsetXor = bitsetX ^ bitsetY

assert(bitsetXor.nextSetBit(0) === 1)
assert(bitsetXor.nextSetBit(1) === 1)
assert(bitsetXor.nextSetBit(2) === 2)
assert(bitsetXor.nextSetBit(3) === 38)
assert(bitsetXor.nextSetBit(38) === 38)
assert(bitsetXor.nextSetBit(39) === 85)
assert(bitsetXor.nextSetBit(42) === 85)
assert(bitsetXor.nextSetBit(85) === 85)
assert(bitsetXor.nextSetBit(86) === -1)

}

test( "and-not" ) {
val setBitsX = Seq( 0, 2, 3, 37, 41, 48 )
val setBitsY = Seq( 0, 1, 3, 37, 38, 41, 85)
val bitsetX = new BitSet(60)
setBitsX.foreach( i => bitsetX.set(i))
val bitsetY = new BitSet(100)
setBitsY.foreach( i => bitsetY.set(i))

val bitsetDiff = bitsetX &~ bitsetY

assert(bitsetDiff.nextSetBit(0) === 2)
assert(bitsetDiff.nextSetBit(1) === 2)
assert(bitsetDiff.nextSetBit(2) === 2)
assert(bitsetDiff.nextSetBit(3) === 48)
assert(bitsetDiff.nextSetBit(48) === 48)
assert(bitsetDiff.nextSetBit(49) === -1)
assert(bitsetDiff.nextSetBit(65) === -1)
}
}

0 comments on commit 4e1df43

Please sign in to comment.