in algebird-core/src/main/scala/com/twitter/algebird/immutable/BitSet.scala [657:701]
def ^(rhs: BitSet): BitSet =
if (this eq rhs) {
// TODO: it is unclear why BitSet.Empty isn't okay here.
// Tests pass if we do it, but it seems a pretty minor optimization
// If we need some invariant, we should have a test for it.
// newEmpty(offset)
//
// a motivation to use Empty here is to avoid always returning
// aligned offsets, which make some of the branches below unreachable
BitSet.Empty
} else if (height > rhs.height) {
if (rhs.offset < offset || limit <= rhs.offset) {
this | rhs
} else {
// this branch contains rhs, so find its index
val i = index(rhs.offset)
val c0 = children(i)
if (c0 != null) {
val cc = c0 ^ rhs
if (c0 eq cc) this else replace(i, cc)
} else {
var cc = rhs
while (cc.height < height - 1) cc = BitSet.parentFor(cc)
replace(i, cc)
}
}
} else if (height < rhs.height) {
// use commuativity to handle this in previous case
rhs ^ this
} else if (offset != rhs.offset) {
// same height, but non-overlapping
this | rhs
} else {
// height == rhs.height, so we know rhs is a Branch.
val Branch(_, _, rcs) = rhs
val cs = new Array[BitSet](32)
var i = 0
while (i < 32) {
val c0 = children(i)
val c1 = rcs(i)
cs(i) = if (c1 == null) c0 else if (c0 == null) c1 else c0 ^ c1
i += 1
}
Branch(offset, height, cs)
}