2014-11-04 64 views
1

在Java中,你可以flip一個BitSet翻轉Scala的位集合

val javaBitSet = new java.util.BitSet(5) 
javaBitSet.set(0) 
javaBitSet.set(1) 
javaBitSet.set(3) 
javaBitSet.flip(0, 5) 
javaBitSet: java.util.BitSet = {2, 4} 

你怎麼慣用用Scala的BitSet做到這一點? (爲什麼不是這個方法?)

回答

3
scala> import collection.immutable.BitSet 
import collection.immutable.BitSet 

你刪除例如翻轉第2位,但葉位5折:

scala> BitSet(1,2,3) &~ BitSet(2,5) 
res3: scala.collection.immutable.BitSet = BitSet(1, 3) 

但邏輯XOR是你想要什麼:

scala> BitSet(1,2,3)^BitSet(2,5) 
res4: scala.collection.immutable.BitSet = BitSet(1, 3, 5) 

或慣用

scala> implicit class `flipper of bits`(val bs: BitSet) extends AnyVal { 
    | def flip(lo: Int, hi: Int): BitSet = BitSet(lo until hi: _*)^bs 
    | } 
defined class flipper$u0020of$u0020bits 

scala> BitSet(2,5) flip (1,4) 
res5: scala.collection.immutable.BitSet = BitSet(1, 3, 5) 

爲「擴展方法」。從範圍構建BitSet效率不高,因爲它會調用+ n次。好一點的是從數組中創建一個位集的子範圍,如下所示。根據您的要求,這些箍筋建議您可以擁有對遠程操作的內置支持。

更新:

對於非平凡的範圍內,最好不要創建從RangeBitSet

scala> import collection.immutable.BitSet 
import collection.immutable.BitSet 

scala> val bs = BitSet(1, 69, 188) 
bs: scala.collection.immutable.BitSet = BitSet(1, 69, 188) 

scala> val lower = 32 
lower: Int = 32 

scala> val upper = 190 
upper: Int = 190 

scala> val n = (bs.last max upper)/64 + 1 
n: Int = 3 

scala> val arr = Array.tabulate(n)(_ => -1L) 
arr: Array[Long] = Array(-1, -1, -1) 

scala> val mask = BitSet.fromBitMaskNoCopy(arr).range(lower, upper) 
mask: scala.collection.immutable.BitSet = BitSet(32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189) 

scala> val res = bs^mask 
res: scala.collection.immutable.BitSet = BitSet(1, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 189) 

scala> res(69) 
res0: Boolean = false 
+0

該文本似乎都是錯誤的,但方法是正確的。 – yonran 2017-10-24 19:45:09

+0

@yonran「你的榜樣」是OP的第一個答案,現在已被刪除,但用特殊眼鏡可見。這個方法很醜,因爲'BitSet(range)'很昂貴,我猜想是一個循環添加到空BitSet。也許最好使用'fromBitMaskNoCopy(array).range(from,to)'或者只是'toBitMask'和手動提取位。爲了避免額外的數組拷貝,可能需要內置的OP支持。 – 2017-10-24 21:00:39