|
| 1 | +package scala.collection.decorators |
| 2 | + |
| 3 | +import scala.collection.{BitSetOps, mutable} |
| 4 | + |
| 5 | +class MutableBitSetDecorator(protected val bs: mutable.BitSet) { |
| 6 | + |
| 7 | + import BitSetDecorator._ |
| 8 | + import BitSetOps._ |
| 9 | + |
| 10 | + /** |
| 11 | + * Updates this BitSet to the left shift of itself by the given shift distance. |
| 12 | + * The shift distance may be negative, in which case this method performs a right shift. |
| 13 | + * @param shiftBy shift distance, in bits |
| 14 | + * @return the BitSet itself |
| 15 | + */ |
| 16 | + def <<=(shiftBy: Int): mutable.BitSet = { |
| 17 | + |
| 18 | + if (bs.nwords == 0 || bs.nwords == 1 && bs.word(0) == 0) () |
| 19 | + else if (shiftBy > 0) shiftLeftInPlace(shiftBy) |
| 20 | + else if (shiftBy < 0) shiftRightInPlace(-shiftBy) |
| 21 | + |
| 22 | + bs |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Updates this BitSet to the right shift of itself by the given shift distance. |
| 27 | + * The shift distance may be negative, in which case this method performs a left shift. |
| 28 | + * @param shiftBy shift distance, in bits |
| 29 | + * @return the BitSet itself |
| 30 | + */ |
| 31 | + def >>=(shiftBy: Int): mutable.BitSet = { |
| 32 | + |
| 33 | + if (bs.nwords == 0 || bs.nwords == 1 && bs.word(0) == 0) () |
| 34 | + else if (shiftBy > 0) shiftRightInPlace(shiftBy) |
| 35 | + else if (shiftBy < 0) shiftLeftInPlace(-shiftBy) |
| 36 | + |
| 37 | + bs |
| 38 | + } |
| 39 | + |
| 40 | + private def shiftLeftInPlace(shiftBy: Int): Unit = { |
| 41 | + |
| 42 | + val bitOffset = shiftBy & WordMask |
| 43 | + val wordOffset = shiftBy >>> LogWL |
| 44 | + |
| 45 | + var significantWordCount = bs.nwords |
| 46 | + while (significantWordCount > 0 && bs.word(significantWordCount - 1) == 0) { |
| 47 | + significantWordCount -= 1 |
| 48 | + } |
| 49 | + |
| 50 | + if (bitOffset == 0) { |
| 51 | + val newSize = significantWordCount + wordOffset |
| 52 | + require(newSize <= MaxSize) |
| 53 | + ensureCapacity(newSize) |
| 54 | + System.arraycopy(bs.elems, 0, bs.elems, wordOffset, significantWordCount) |
| 55 | + } else { |
| 56 | + val revBitOffset = WordLength - bitOffset |
| 57 | + val extraBits = bs.elems(significantWordCount - 1) >>> revBitOffset |
| 58 | + val extraWordCount = if (extraBits == 0) 0 else 1 |
| 59 | + val newSize = significantWordCount + wordOffset + extraWordCount |
| 60 | + require(newSize <= MaxSize) |
| 61 | + ensureCapacity(newSize) |
| 62 | + var i = significantWordCount - 1 |
| 63 | + var previous = bs.elems(i) |
| 64 | + while (i > 0) { |
| 65 | + val current = bs.elems(i - 1) |
| 66 | + bs.elems(i + wordOffset) = (current >>> revBitOffset) | (previous << bitOffset) |
| 67 | + previous = current |
| 68 | + i -= 1 |
| 69 | + } |
| 70 | + bs.elems(wordOffset) = previous << bitOffset |
| 71 | + if (extraWordCount != 0) bs.elems(newSize - 1) = extraBits |
| 72 | + } |
| 73 | + java.util.Arrays.fill(bs.elems, 0, wordOffset, 0) |
| 74 | + } |
| 75 | + |
| 76 | + private def shiftRightInPlace(shiftBy: Int): Unit = { |
| 77 | + |
| 78 | + val bitOffset = shiftBy & WordMask |
| 79 | + |
| 80 | + if (bitOffset == 0) { |
| 81 | + val wordOffset = shiftBy >>> LogWL |
| 82 | + val newSize = bs.nwords - wordOffset |
| 83 | + if (newSize > 0) { |
| 84 | + System.arraycopy(bs.elems, wordOffset, bs.elems, 0, newSize) |
| 85 | + java.util.Arrays.fill(bs.elems, newSize, bs.nwords, 0) |
| 86 | + } else bs.clear() |
| 87 | + } else { |
| 88 | + val wordOffset = (shiftBy >>> LogWL) + 1 |
| 89 | + val extraBits = bs.elems(bs.nwords - 1) >>> bitOffset |
| 90 | + val extraWordCount = if (extraBits == 0) 0 else 1 |
| 91 | + val newSize = bs.nwords - wordOffset + extraWordCount |
| 92 | + if (newSize > 0) { |
| 93 | + val revBitOffset = WordLength - bitOffset |
| 94 | + var previous = bs.elems(wordOffset - 1) |
| 95 | + var i = wordOffset |
| 96 | + while (i < bs.nwords) { |
| 97 | + val current = bs.elems(i) |
| 98 | + bs.elems(i - wordOffset) = (previous >>> bitOffset) | (current << revBitOffset) |
| 99 | + previous = current |
| 100 | + i += 1 |
| 101 | + } |
| 102 | + if (extraWordCount != 0) bs.elems(newSize - 1) = extraBits |
| 103 | + java.util.Arrays.fill(bs.elems, newSize, bs.nwords, 0) |
| 104 | + } else bs.clear() |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + protected final def ensureCapacity(idx: Int): Unit = { |
| 109 | + // Copied from mutable.BitSet.ensureCapacity (which is inaccessible from here). |
| 110 | + require(idx < MaxSize) |
| 111 | + if (idx >= bs.nwords) { |
| 112 | + var newlen = bs.nwords |
| 113 | + while (idx >= newlen) newlen = math.min(newlen * 2, MaxSize) |
| 114 | + val elems1 = new Array[Long](newlen) |
| 115 | + Array.copy(bs.elems, 0, elems1, 0, bs.nwords) |
| 116 | + bs.elems = elems1 |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | +} |
0 commit comments