Skip to content

Instantly share code, notes, and snippets.

@MasseGuillaume
Created December 5, 2017 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MasseGuillaume/b245fb9d049e04d115a6a7802fc39b34 to your computer and use it in GitHub Desktop.
Save MasseGuillaume/b245fb9d049e04d115a6a7802fc39b34 to your computer and use it in GitHub Desktop.
import scala.collection.immutable.BitSet
def fromRange(xs: List[(Int, Int)]): BitSet = {
val range = BitSet.newBuilder
xs.foreach {
case (start, end) =>
range ++= BitSet((start to end): _*)
}
range.result
}
val t1 = List((14, 20), (60, 80), (180, 270))
val r1 = fromRange(t1)
println(r1)
def fromRange2(xs: List[(Int, Int)]): BitSet = {
val n = 64
val max = xs.maxBy(_._2)._2
val mask = Array.ofDim[Long](max / n + 1)
xs.foreach {
case (start, end) => {
var i = start
while(i <= end) {
val idx = i / n
mask.update(
idx,
mask(idx) | (1L << (i - (idx * n)))
)
i += 1
}
}
}
BitSet.fromBitMaskNoCopy(mask)
}
val r2 = fromRange2(t1)
println(r2)
r1 == r2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment