|
| 1 | +package scala.collection.decorators |
| 2 | + |
| 3 | +import scala.collection.{BitSet, BitSetOps} |
| 4 | + |
| 5 | +class BitSetDecorator[+C <: BitSet with BitSetOps[C]](protected val bs: C) { |
| 6 | + |
| 7 | + import BitSetDecorator._ |
| 8 | + import BitSetOps._ |
| 9 | + |
| 10 | + /** |
| 11 | + * Bitwise left shift of this BitSet by given the 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 a new BitSet whose value is a bitwise shift left of this BitSet by given shift distance (`shiftBy`) |
| 15 | + */ |
| 16 | + def <<(shiftBy: Int): C = { |
| 17 | + |
| 18 | + val shiftedBits = if (bs.nwords == 0 || bs.nwords == 1 && bs.word(0) == 0) Array.emptyLongArray |
| 19 | + else if (shiftBy > 0) shiftLeft(shiftBy) |
| 20 | + else if (shiftBy == 0) bs.toBitMask |
| 21 | + else shiftRight(-shiftBy) |
| 22 | + |
| 23 | + bs.fromBitMaskNoCopy(shiftedBits) |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Bitwise right shift of this BitSet by the given shift distance. |
| 28 | + * The shift distance may be negative, in which case this method performs a left shift. |
| 29 | + * @param shiftBy shift distance, in bits |
| 30 | + * @return a new BitSet whose value is a bitwise shift right of this BitSet by given shift distance (`shiftBy`) |
| 31 | + */ |
| 32 | + def >>(shiftBy: Int): C = { |
| 33 | + |
| 34 | + val shiftedBits = if (bs.nwords == 0 || bs.nwords == 1 && bs.word(0) == 0) Array.emptyLongArray |
| 35 | + else if (shiftBy > 0) shiftRight(shiftBy) |
| 36 | + else if (shiftBy == 0) bs.toBitMask |
| 37 | + else shiftLeft(-shiftBy) |
| 38 | + |
| 39 | + bs.fromBitMaskNoCopy(shiftedBits) |
| 40 | + } |
| 41 | + |
| 42 | + private def shiftLeft(shiftBy: Int): Array[Long] = { |
| 43 | + |
| 44 | + val bitOffset = shiftBy & WordMask |
| 45 | + val wordOffset = shiftBy >>> LogWL |
| 46 | + |
| 47 | + if (bitOffset == 0) { |
| 48 | + val newSize = bs.nwords + wordOffset |
| 49 | + require(newSize <= MaxSize) |
| 50 | + val newBits = Array.ofDim[Long](newSize) |
| 51 | + var i = wordOffset |
| 52 | + while (i < newSize) { |
| 53 | + newBits(i) = bs.word(i - wordOffset) |
| 54 | + i += 1 |
| 55 | + } |
| 56 | + newBits |
| 57 | + } else { |
| 58 | + val revBitOffset = WordLength - bitOffset |
| 59 | + val extraBits = bs.word(bs.nwords - 1) >>> revBitOffset |
| 60 | + val extraWordCount = if (extraBits == 0) 0 else 1 |
| 61 | + val newSize = bs.nwords + wordOffset + extraWordCount |
| 62 | + require(newSize <= MaxSize) |
| 63 | + val newBits = Array.ofDim[Long](newSize) |
| 64 | + var previous = 0L |
| 65 | + var i = 0 |
| 66 | + while (i < bs.nwords) { |
| 67 | + val current = bs.word(i) |
| 68 | + newBits(i + wordOffset) = (previous >>> revBitOffset) | (current << bitOffset) |
| 69 | + previous = current |
| 70 | + i += 1 |
| 71 | + } |
| 72 | + if (extraWordCount != 0) newBits(newSize - 1) = extraBits |
| 73 | + newBits |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private def shiftRight(shiftBy: Int): Array[Long] = { |
| 78 | + |
| 79 | + val bitOffset = shiftBy & WordMask |
| 80 | + |
| 81 | + if (bitOffset == 0) { |
| 82 | + val wordOffset = shiftBy >>> LogWL |
| 83 | + val newSize = bs.nwords - wordOffset |
| 84 | + if (newSize > 0) { |
| 85 | + val newBits = Array.ofDim[Long](newSize) |
| 86 | + var i = 0 |
| 87 | + while (i < newSize) { |
| 88 | + newBits(i) = bs.word(i + wordOffset) |
| 89 | + i += 1 |
| 90 | + } |
| 91 | + newBits |
| 92 | + } else Array.emptyLongArray |
| 93 | + } else { |
| 94 | + val wordOffset = (shiftBy >>> LogWL) + 1 |
| 95 | + val extraBits = bs.word(bs.nwords - 1) >>> bitOffset |
| 96 | + val extraWordCount = if (extraBits == 0) 0 else 1 |
| 97 | + val newSize = bs.nwords - wordOffset + extraWordCount |
| 98 | + if (newSize > 0) { |
| 99 | + val revBitOffset = WordLength - bitOffset |
| 100 | + val newBits = Array.ofDim[Long](newSize) |
| 101 | + var previous = bs.word(wordOffset - 1) |
| 102 | + var i = wordOffset |
| 103 | + while (i < bs.nwords) { |
| 104 | + val current = bs.word(i) |
| 105 | + newBits(i - wordOffset) = (previous >>> bitOffset) | (current << revBitOffset) |
| 106 | + previous = current |
| 107 | + i += 1 |
| 108 | + } |
| 109 | + if (extraWordCount != 0) newBits(newSize - 1) = extraBits |
| 110 | + newBits |
| 111 | + } else Array.emptyLongArray |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | +} |
| 116 | + |
| 117 | +object BitSetDecorator { |
| 118 | + private[collection] final val WordMask = BitSetOps.WordLength - 1 |
| 119 | +} |
0 commit comments