aboutsummaryrefslogtreecommitdiff
path: root/src/dotty/tools/dotc/util/SixteenNibbles.scala
diff options
context:
space:
mode:
Diffstat (limited to 'src/dotty/tools/dotc/util/SixteenNibbles.scala')
-rw-r--r--src/dotty/tools/dotc/util/SixteenNibbles.scala27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/dotty/tools/dotc/util/SixteenNibbles.scala b/src/dotty/tools/dotc/util/SixteenNibbles.scala
new file mode 100644
index 000000000..58d4a1182
--- /dev/null
+++ b/src/dotty/tools/dotc/util/SixteenNibbles.scala
@@ -0,0 +1,27 @@
+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 & ~(Mask << (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
+} \ No newline at end of file