質問

I am trying to write a backtracking algorithm that keeps state using mutable BitSets, it works fine but I want it to go faster!

The crux is given two mutable.BitSet alpha and beta I need to calculate if any of the bits of alpha are set in beta, i.e. bitwise AND. I do not need the resulting set just need to know if the intersection isNonEmpty

(alpha intersect beta).nonEmpty

or

(alpha & beta).nonEmpty

but both of these construct a set which is then tested for size... I really just need a boolean and would like to avoid the cost of constructing the intermediate set.

Is there a better way?

TIA Nivag

役に立ちましたか?

解決

Referring to the API docs, you may use find and contains method.

alpha find (beta.contains) isDefined

OR

Even better, use exists method.

alpha exists (beta.contains)

OR

Even shorter and better, use apply method of BitSet which is equivalent to its contains method.

alpha exists beta
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top