scalaのBitSet

Bit配列を持ちたいので調べた
http://www.scala-lang.org/api/current/scala/collection/BitSet.html

object TryBitSet {
  import scala.collection.mutable.BitSet
  def main(args:Array[String]) {
    var bits = new BitSet() // empty状態

    // bits += n で n番目をtrueにする
    bits += 1                    // BitSet(1)    -> 0b10
    bits += 3                    // BitSet(1, 3) -> 0b1010
    intValue(bits)               // 10

    // bits -= n で n番目をfalseにする
    bits -= 1                    // BitSet(3) -> 0b1000
    intValue(BitSet(0, 1, 2, 3)) // -> 0b1111 -> 15
  }

  def intValue(bits:BitSet):Int = {
    var v:Int = 0
    for (n <- 31 to 0 by -1) {
      v |= (if (bits(n)) 1 else 0) << n
    }
    return v
  }
}

BitSetの方がBoolの配列よりパフォーマンスは概ね良好(サイズ数百万になると、また別)らしい