Created
December 5, 2017 18:28
-
-
Save MasseGuillaume/b245fb9d049e04d115a6a7802fc39b34 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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