|
| 1 | +/* |
| 2 | + * Scala (https://www.scala-lang.org) |
| 3 | + * |
| 4 | + * Copyright EPFL and Lightbend, Inc. |
| 5 | + * |
| 6 | + * Licensed under Apache License 2.0 |
| 7 | + * (http://www.apache.org/licenses/LICENSE-2.0). |
| 8 | + * |
| 9 | + * See the NOTICE file distributed with this work for |
| 10 | + * additional information regarding copyright ownership. |
| 11 | + */ |
| 12 | + |
| 13 | +package scala |
| 14 | +package collection |
| 15 | + |
| 16 | +import java.io.{ObjectInputStream, ObjectOutputStream} |
| 17 | + |
| 18 | +import scala.annotation.nowarn |
| 19 | +import scala.collection.Stepper.EfficientSplit |
| 20 | +import scala.collection.mutable.Builder |
| 21 | + |
| 22 | + |
| 23 | +/** Base type of bitsets. |
| 24 | + * |
| 25 | + * This trait provides most of the operations of a `BitSet` independently of its representation. |
| 26 | + * It is inherited by all concrete implementations of bitsets. |
| 27 | + * |
| 28 | + * @define bitsetinfo |
| 29 | + * Bitsets are sets of non-negative integers which are represented as |
| 30 | + * variable-size arrays of bits packed into 64-bit words. The lower bound of memory footprint of a bitset is |
| 31 | + * determined by the largest number stored in it. |
| 32 | + * @define coll bitset |
| 33 | + * @define Coll `BitSet` |
| 34 | + */ |
| 35 | +trait BitSet extends SortedSet[Int] with BitSetOps[BitSet] { |
| 36 | + override protected def fromSpecific(coll: IterableOnce[Int]): BitSet = bitSetFactory.fromSpecific(coll) |
| 37 | + override protected def newSpecificBuilder: Builder[Int, BitSet] = bitSetFactory.newBuilder |
| 38 | + override def empty: BitSet = bitSetFactory.empty |
| 39 | + @nowarn("""cat=deprecation&origin=scala\.collection\.Iterable\.stringPrefix""") |
| 40 | + override protected[this] def stringPrefix = "BitSet" |
| 41 | + override def unsorted: Set[Int] = this |
| 42 | +} |
| 43 | + |
| 44 | +@SerialVersionUID(3L) |
| 45 | +object BitSet extends SpecificIterableFactory[Int, BitSet] { |
| 46 | + private[collection] final val ordMsg = "No implicit Ordering[${B}] found to build a SortedSet[${B}]. You may want to upcast to a Set[Int] first by calling `unsorted`." |
| 47 | + private[collection] final val zipOrdMsg = "No implicit Ordering[${B}] found to build a SortedSet[(Int, ${B})]. You may want to upcast to a Set[Int] first by calling `unsorted`." |
| 48 | + |
| 49 | + def empty: BitSet = immutable.BitSet.empty |
| 50 | + def newBuilder: Builder[Int, BitSet] = immutable.BitSet.newBuilder |
| 51 | + def fromSpecific(it: IterableOnce[Int]): BitSet = immutable.BitSet.fromSpecific(it) |
| 52 | + |
| 53 | + @SerialVersionUID(3L) |
| 54 | + private[collection] abstract class SerializationProxy(@transient protected val coll: BitSet) extends Serializable { |
| 55 | + |
| 56 | + @transient protected var elems: Array[Long] = _ |
| 57 | + |
| 58 | + private[this] def writeObject(out: ObjectOutputStream): Unit = { |
| 59 | + out.defaultWriteObject() |
| 60 | + val nwords = coll.nwords |
| 61 | + out.writeInt(nwords) |
| 62 | + var i = 0 |
| 63 | + while(i < nwords) { |
| 64 | + out.writeLong(coll.word(i)) |
| 65 | + i += 1 |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private[this] def readObject(in: ObjectInputStream): Unit = { |
| 70 | + in.defaultReadObject() |
| 71 | + val nwords = in.readInt() |
| 72 | + elems = new Array[Long](nwords) |
| 73 | + var i = 0 |
| 74 | + while(i < nwords) { |
| 75 | + elems(i) = in.readLong() |
| 76 | + i += 1 |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + protected[this] def readResolve(): Any |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +/** Base implementation type of bitsets */ |
| 85 | +trait BitSetOps[+C <: BitSet with BitSetOps[C]] |
| 86 | + extends SortedSetOps[Int, SortedSet, C] { self => |
| 87 | + import BitSetOps._ |
| 88 | + |
| 89 | + def bitSetFactory: SpecificIterableFactory[Int, C] |
| 90 | + |
| 91 | + def unsorted: Set[Int] |
| 92 | + |
| 93 | + final def ordering: Ordering[Int] = Ordering.Int |
| 94 | + |
| 95 | + /** The number of words (each with 64 bits) making up the set */ |
| 96 | + protected[collection] def nwords: Int |
| 97 | + |
| 98 | + /** The words at index `idx`, or 0L if outside the range of the set |
| 99 | + * '''Note:''' requires `idx >= 0` |
| 100 | + */ |
| 101 | + protected[collection] def word(idx: Int): Long |
| 102 | + |
| 103 | + /** Creates a new set of this kind from an array of longs |
| 104 | + */ |
| 105 | + protected[collection] def fromBitMaskNoCopy(elems: Array[Long]): C |
| 106 | + |
| 107 | + def contains(elem: Int): Boolean = |
| 108 | + 0 <= elem && (word(elem >> LogWL) & (1L << elem)) != 0L |
| 109 | + |
| 110 | + def iterator: Iterator[Int] = iteratorFrom(0) |
| 111 | + |
| 112 | + def iteratorFrom(start: Int): Iterator[Int] = new AbstractIterator[Int] { |
| 113 | + private[this] var currentPos = if (start > 0) start >> LogWL else 0 |
| 114 | + private[this] var currentWord = if (start > 0) word(currentPos) & (-1L << (start & (WordLength - 1))) else word(0) |
| 115 | + final override def hasNext: Boolean = { |
| 116 | + while (currentWord == 0) { |
| 117 | + if (currentPos + 1 >= nwords) return false |
| 118 | + currentPos += 1 |
| 119 | + currentWord = word(currentPos) |
| 120 | + } |
| 121 | + true |
| 122 | + } |
| 123 | + final override def next(): Int = { |
| 124 | + if (hasNext) { |
| 125 | + val bitPos = java.lang.Long.numberOfTrailingZeros(currentWord) |
| 126 | + currentWord &= currentWord - 1 |
| 127 | + (currentPos << LogWL) + bitPos |
| 128 | + } else Iterator.empty.next() |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + override def stepper[S <: Stepper[_]](implicit shape: StepperShape[Int, S]): S with EfficientSplit = { |
| 133 | + val st = scala.collection.convert.impl.BitSetStepper.from(this) |
| 134 | + val r = |
| 135 | + if (shape.shape == StepperShape.IntShape) st |
| 136 | + else { |
| 137 | + assert(shape.shape == StepperShape.ReferenceShape, s"unexpected StepperShape: $shape") |
| 138 | + AnyStepper.ofParIntStepper(st) |
| 139 | + } |
| 140 | + r.asInstanceOf[S with EfficientSplit] |
| 141 | + } |
| 142 | + |
| 143 | + override def size: Int = { |
| 144 | + var s = 0 |
| 145 | + var i = nwords |
| 146 | + while (i > 0) { |
| 147 | + i -= 1 |
| 148 | + s += java.lang.Long.bitCount(word(i)) |
| 149 | + } |
| 150 | + s |
| 151 | + } |
| 152 | + |
| 153 | + override def isEmpty: Boolean = 0 until nwords forall (i => word(i) == 0) |
| 154 | + |
| 155 | + @inline private[this] def smallestInt: Int = { |
| 156 | + val thisnwords = nwords |
| 157 | + var i = 0 |
| 158 | + while(i < thisnwords) { |
| 159 | + val currentWord = word(i) |
| 160 | + if (currentWord != 0L) { |
| 161 | + return java.lang.Long.numberOfTrailingZeros(currentWord) + (i * WordLength) |
| 162 | + } |
| 163 | + i += 1 |
| 164 | + } |
| 165 | + throw new UnsupportedOperationException("empty.smallestInt") |
| 166 | + } |
| 167 | + |
| 168 | + @inline private[this] def largestInt: Int = { |
| 169 | + var i = nwords - 1 |
| 170 | + while(i >= 0) { |
| 171 | + val currentWord = word(i) |
| 172 | + if (currentWord != 0L) { |
| 173 | + return ((i + 1) * WordLength) - java.lang.Long.numberOfLeadingZeros(currentWord) - 1 |
| 174 | + } |
| 175 | + i -= 1 |
| 176 | + } |
| 177 | + throw new UnsupportedOperationException("empty.largestInt") |
| 178 | + } |
| 179 | + |
| 180 | + override def max[B >: Int](implicit ord: Ordering[B]): Int = |
| 181 | + if (Ordering.Int eq ord) largestInt |
| 182 | + else if (Ordering.Int isReverseOf ord) smallestInt |
| 183 | + else super.max(ord) |
| 184 | + |
| 185 | + |
| 186 | + override def min[B >: Int](implicit ord: Ordering[B]): Int = |
| 187 | + if (Ordering.Int eq ord) smallestInt |
| 188 | + else if (Ordering.Int isReverseOf ord) largestInt |
| 189 | + else super.min(ord) |
| 190 | + |
| 191 | + override def foreach[U](f: Int => U): Unit = { |
| 192 | + /* NOTE: while loops are significantly faster as of 2.11 and |
| 193 | + one major use case of bitsets is performance. Also, there |
| 194 | + is nothing to do when all bits are clear, so use that as |
| 195 | + the inner loop condition. */ |
| 196 | + var i = 0 |
| 197 | + while (i < nwords) { |
| 198 | + var w = word(i) |
| 199 | + var j = i * WordLength |
| 200 | + while (w != 0L) { |
| 201 | + if ((w&1L) == 1L) f(j) |
| 202 | + w = w >>> 1 |
| 203 | + j += 1 |
| 204 | + } |
| 205 | + i += 1 |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + /** Creates a bit mask for this set as a new array of longs |
| 210 | + */ |
| 211 | + def toBitMask: Array[Long] = { |
| 212 | + val a = new Array[Long](nwords) |
| 213 | + var i = a.length |
| 214 | + while(i > 0) { |
| 215 | + i -= 1 |
| 216 | + a(i) = word(i) |
| 217 | + } |
| 218 | + a |
| 219 | + } |
| 220 | + |
| 221 | + def rangeImpl(from: Option[Int], until: Option[Int]): C = { |
| 222 | + val a = coll.toBitMask |
| 223 | + val len = a.length |
| 224 | + if (from.isDefined) { |
| 225 | + val f = from.get |
| 226 | + val w = f >> LogWL |
| 227 | + val b = f & (WordLength - 1) |
| 228 | + if (w >= 0) { |
| 229 | + java.util.Arrays.fill(a, 0, math.min(w, len), 0) |
| 230 | + if (b > 0 && w < len) a(w) &= ~((1L << b) - 1) |
| 231 | + } |
| 232 | + } |
| 233 | + if (until.isDefined) { |
| 234 | + val u = until.get |
| 235 | + val w = u >> LogWL |
| 236 | + val b = u & (WordLength - 1) |
| 237 | + if (w < len) { |
| 238 | + java.util.Arrays.fill(a, math.max(w + 1, 0), len, 0) |
| 239 | + if (w >= 0) a(w) &= (1L << b) - 1 |
| 240 | + } |
| 241 | + } |
| 242 | + coll.fromBitMaskNoCopy(a) |
| 243 | + } |
| 244 | + |
| 245 | + override def concat(other: collection.IterableOnce[Int]): C = other match { |
| 246 | + case otherBitset: BitSet => |
| 247 | + val len = coll.nwords max otherBitset.nwords |
| 248 | + val words = new Array[Long](len) |
| 249 | + for (idx <- 0 until len) |
| 250 | + words(idx) = this.word(idx) | otherBitset.word(idx) |
| 251 | + fromBitMaskNoCopy(words) |
| 252 | + case _ => super.concat(other) |
| 253 | + } |
| 254 | + |
| 255 | + override def intersect(other: Set[Int]): C = other match { |
| 256 | + case otherBitset: BitSet => |
| 257 | + val len = coll.nwords min otherBitset.nwords |
| 258 | + val words = new Array[Long](len) |
| 259 | + for (idx <- 0 until len) |
| 260 | + words(idx) = this.word(idx) & otherBitset.word(idx) |
| 261 | + fromBitMaskNoCopy(words) |
| 262 | + case _ => super.intersect(other) |
| 263 | + } |
| 264 | + |
| 265 | + abstract override def diff(other: Set[Int]): C = other match { |
| 266 | + case otherBitset: BitSet => |
| 267 | + val len = coll.nwords |
| 268 | + val words = new Array[Long](len) |
| 269 | + for (idx <- 0 until len) |
| 270 | + words(idx) = this.word(idx) & ~otherBitset.word(idx) |
| 271 | + fromBitMaskNoCopy(words) |
| 272 | + case _ => super.diff(other) |
| 273 | + } |
| 274 | + |
| 275 | + /** Computes the symmetric difference of this bitset and another bitset by performing |
| 276 | + * a bitwise "exclusive-or". |
| 277 | + * |
| 278 | + * @param other the other bitset to take part in the symmetric difference. |
| 279 | + * @return a bitset containing those bits of this |
| 280 | + * bitset or the other bitset that are not contained in both bitsets. |
| 281 | + */ |
| 282 | + def xor(other: BitSet): C = { |
| 283 | + val len = coll.nwords max other.nwords |
| 284 | + val words = new Array[Long](len) |
| 285 | + for (idx <- 0 until len) |
| 286 | + words(idx) = coll.word(idx) ^ other.word(idx) |
| 287 | + coll.fromBitMaskNoCopy(words) |
| 288 | + } |
| 289 | + |
| 290 | + @`inline` final def ^ (other: BitSet): C = xor(other) |
| 291 | + |
| 292 | + /** |
| 293 | + * Builds a new bitset by applying a function to all elements of this bitset |
| 294 | + * @param f the function to apply to each element. |
| 295 | + * @return a new bitset resulting from applying the given function ''f'' to |
| 296 | + * each element of this bitset and collecting the results |
| 297 | + */ |
| 298 | + def map(f: Int => Int): C = fromSpecific(new View.Map(this, f)) |
| 299 | + |
| 300 | + def flatMap(f: Int => IterableOnce[Int]): C = fromSpecific(new View.FlatMap(this, f)) |
| 301 | + |
| 302 | + def collect(pf: PartialFunction[Int, Int]): C = fromSpecific(super[SortedSetOps].collect(pf)) |
| 303 | + |
| 304 | + override def partition(p: Int => Boolean): (C, C) = { |
| 305 | + val left = filter(p) |
| 306 | + (left, this &~ left) |
| 307 | + } |
| 308 | +} |
| 309 | + |
| 310 | +object BitSetOps { |
| 311 | + |
| 312 | + /* Final vals can sometimes be inlined as constants (faster) */ |
| 313 | + private[collection] final val LogWL = 6 |
| 314 | + private[collection] final val WordLength = 64 |
| 315 | + private[collection] final val MaxSize = (Int.MaxValue >> LogWL) + 1 |
| 316 | + |
| 317 | + private[collection] def updateArray(elems: Array[Long], idx: Int, w: Long): Array[Long] = { |
| 318 | + var len = elems.length |
| 319 | + while (len > 0 && (elems(len - 1) == 0L || w == 0L && idx == len - 1)) len -= 1 |
| 320 | + var newlen = len |
| 321 | + if (idx >= newlen && w != 0L) newlen = idx + 1 |
| 322 | + val newelems = new Array[Long](newlen) |
| 323 | + Array.copy(elems, 0, newelems, 0, len) |
| 324 | + if (idx < newlen) newelems(idx) = w |
| 325 | + else assert(w == 0L) |
| 326 | + newelems |
| 327 | + } |
| 328 | + |
| 329 | + private[collection] def computeWordForFilter(pred: Int => Boolean, isFlipped: Boolean, oldWord: Long, wordIndex: Int): Long = |
| 330 | + if (oldWord == 0L) 0L else { |
| 331 | + var w = oldWord |
| 332 | + val trailingZeroes = java.lang.Long.numberOfTrailingZeros(w) |
| 333 | + var jmask = 1L << trailingZeroes |
| 334 | + var j = wordIndex * BitSetOps.WordLength + trailingZeroes |
| 335 | + val maxJ = (wordIndex + 1) * BitSetOps.WordLength - java.lang.Long.numberOfLeadingZeros(w) |
| 336 | + while (j != maxJ) { |
| 337 | + if ((w & jmask) != 0L) { |
| 338 | + if (pred(j) == isFlipped) { |
| 339 | + // j did not pass the filter here |
| 340 | + w = w & ~jmask |
| 341 | + } |
| 342 | + } |
| 343 | + jmask = jmask << 1 |
| 344 | + j += 1 |
| 345 | + } |
| 346 | + w |
| 347 | + } |
| 348 | +} |
0 commit comments