aboutsummaryrefslogblamecommitdiff
path: root/compiler/src/dotty/tools/dotc/util/SixteenNibbles.scala
blob: 93817604e6b0ae92cc0431a527a948cc56ced835 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14













                                                                        
                                             










                                                        
                                  
 
package dotty.tools.dotc.util

/** An efficient implementation of sequences of 16 indexed elements with
 *  values 0..15 in a single Long.
 *
 */
class SixteenNibbles(val bits: Long) extends AnyVal {
  import SixteenNibbles._

  def apply(idx: Int): Int =
    (bits >>> (idx * Width)).toInt & Mask

  def updated(idx: Int, value: Int): SixteenNibbles =
    new SixteenNibbles(
      (bits & ~(LongMask << (idx * Width))) |
      ((value & Mask).toLong << (idx * Width)))

  def elements: IndexedSeq[Int] = (0 until 16) map apply

  override def toString =
    s"SixteenNibbles(${elements.mkString(", ")})"
}

object SixteenNibbles {
  final val Width = 4
  final val Mask = (1 << Width) - 1
  final val LongMask = Mask.toLong
}