Created
March 31, 2012 23:42
-
-
Save non/2269758 to your computer and use it in GitHub Desktop.
Value classes and unboxed tuples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Util { | |
def unsigned(n:Byte) = (n + 0x100) & 0xFF | |
def unsigned(n:Short) = (n + 0x10000) & 0xFFFF | |
def b2i(a:Byte, b:Byte): Int = | |
(a<<8) + unsigned(b) | |
def b3i(a:Byte, b:Byte, c:Byte): Int = | |
(a<<16) + (b<<8) + unsigned(c) | |
def b4i(a:Byte, b:Byte, c:Byte, d:Byte): Int = | |
(a<<24) + (b<<16) + (c<<8) + unsigned(d) | |
def b5l(a:Byte, b:Byte, c:Byte, d:Byte, e:Byte): Long = | |
(a<<32L) + (b<<24L) + (c<<16L) + (d<<8L) + unsigned(e) | |
def b6l(a:Byte, b:Byte, c:Byte, d:Byte, e:Byte, f:Byte): Long = | |
(a<<40L) + (b<<32L) + (c<<24L) + (d<<16L) + (e<<8L) + unsigned(f) | |
def b7l(a:Byte, b:Byte, c:Byte, d:Byte, e:Byte, f:Byte, g:Byte): Long = | |
(a<<48L) + (b<<40L) + (c<<32L) + (d<<24L) + (e<<16L) + (f<<8L) + unsigned(g) | |
def b8l(a:Byte, b:Byte, c:Byte, d:Byte, e:Byte, f:Byte, g:Byte, h:Byte): Long = | |
(a<<56L) + (b<<48L) + (c<<40L) + (d<<32L) + (e<<24L) + (f<<16L) + (g<<8L) + unsigned(h) | |
def s2i(a:Short, b:Short): Int = (a<<16) + unsigned(b) | |
def s3l(a:Short, b:Short, c:Short): Int = (a<<32) + (b<<16) + unsigned(c) | |
def s4l(a:Short, b:Short, c:Short, d:Short): Int = (a<<48) + (b<<32) + (c<<16) + unsigned(d) | |
def i2l(a:Int, b:Int): Long = (a.toLong << 32) + b | |
} | |
import Util._ | |
object ByteTuple2 { | |
def apply(a:Byte, b:Byte) = new ByteTuple2(b2i(a, b)) | |
def unapply(t:ByteTuple2) = Option(t.tpl) | |
} | |
class ByteTuple2(val u:Int) extends AnyVal { | |
def _1 = (u >> 8).toByte | |
def _2 = u.toByte | |
def tpl = (_1, _2) | |
} | |
object ByteTuple3 { | |
def apply(a:Byte, b:Byte, c:Byte) = new ByteTuple3(b3i(a, b, c)) | |
def unapply(t:ByteTuple3) = Option(t.tpl) | |
} | |
class ByteTuple3(val u:Int) extends AnyVal { | |
def _1 = (u >> 16).toByte | |
def _2 = (u >> 8).toByte | |
def _3 = u.toByte | |
def tpl = (_1, _2, _3) | |
} | |
object ByteTuple4 { | |
def apply(a:Byte, b:Byte, c:Byte, d:Byte) = new ByteTuple4(b4i(a, b, c, d)) | |
def unapply(t:ByteTuple4) = Option(t.tpl) | |
} | |
class ByteTuple4(val u:Int) extends AnyVal { | |
def _1 = (u >> 24).toByte | |
def _2 = (u >> 16).toByte | |
def _3 = (u >> 8).toByte | |
def _4 = u.toByte | |
def tpl = (_1, _2, _3, _4) | |
} | |
object ByteTuple5 { | |
def apply(a:Byte, b:Byte, c:Byte, d:Byte, e:Byte) = | |
new ByteTuple5(b5l(a, b, c, d, e)) | |
def unapply(t:ByteTuple5) = Option(t.tpl) | |
} | |
class ByteTuple5(val u:Long) extends AnyVal { | |
def _1 = (u >> 40).toByte | |
def _2 = (u >> 32).toByte | |
def _3 = (u >> 16).toByte | |
def _4 = (u >> 8).toByte | |
def _5 = u.toByte | |
def tpl = (_1, _2, _3, _4, _5) | |
} | |
object ByteTuple6 { | |
def apply(a:Byte, b:Byte, c:Byte, d:Byte, e:Byte, f:Byte) = | |
new ByteTuple6(b6l(a, b, c, d, e, f)) | |
def unapply(t:ByteTuple6) = Option(t.tpl) | |
} | |
class ByteTuple6(val u:Long) extends AnyVal { | |
def _1 = (u >> 40).toByte | |
def _2 = (u >> 32).toByte | |
def _3 = (u >> 24).toByte | |
def _4 = (u >> 16).toByte | |
def _5 = (u >> 8).toByte | |
def _6 = u.toByte | |
def tpl = (_1, _2, _3, _4, _5, _6) | |
} | |
object ByteTuple7 { | |
def apply(a:Byte, b:Byte, c:Byte, d:Byte, e:Byte, f:Byte, g:Byte) = | |
new ByteTuple7(b7l(a, b, c, d, e, f, g)) | |
def unapply(t:ByteTuple7) = Option(t.tpl) | |
} | |
class ByteTuple7(val u:Long) extends AnyVal { | |
def _1 = (u >> 48).toByte | |
def _2 = (u >> 40).toByte | |
def _3 = (u >> 32).toByte | |
def _4 = (u >> 24).toByte | |
def _5 = (u >> 16).toByte | |
def _6 = (u >> 8).toByte | |
def _7 = u.toByte | |
def tpl = (_1, _2, _3, _4, _5, _6, _7) | |
} | |
object ByteTuple8 { | |
def apply(a:Byte, b:Byte, c:Byte, d:Byte, e:Byte, f:Byte, g:Byte, h:Byte) = | |
new ByteTuple8(b8l(a, b, c, d, e, f, g, h)) | |
def unapply(t:ByteTuple8) = Option(t.tpl) | |
} | |
class ByteTuple8(val u:Long) extends AnyVal { | |
def _1 = (u >> 54).toByte | |
def _2 = (u >> 48).toByte | |
def _3 = (u >> 40).toByte | |
def _4 = (u >> 32).toByte | |
def _5 = (u >> 24).toByte | |
def _6 = (u >> 16).toByte | |
def _7 = (u >> 8).toByte | |
def _8 = u.toByte | |
def tpl = (_1, _2, _3, _4, _5, _6, _7, _8) | |
} | |
object ShortTuple2 { | |
def apply(a:Short, b:Short) = new ShortTuple2(s2i(a, b)) | |
def unapply(t:ShortTuple2) = Option(t.tpl) | |
} | |
class ShortTuple2(val u:Int) extends AnyVal { | |
def _1 = (u >> 16).toShort | |
def _2 = u.toShort | |
def tpl = (_1, _2) | |
} | |
object ShortTuple3 { | |
def apply(a:Short, b:Short, c:Short) = new ShortTuple3(s3l(a, b, c)) | |
def unapply(t:ShortTuple3) = Option(t.tpl) | |
} | |
class ShortTuple3(val u:Long) extends AnyVal { | |
def _1 = (u >> 32).toShort | |
def _2 = (u >> 16).toShort | |
def _3 = u.toShort | |
def tpl = (_1, _2, _3) | |
} | |
object ShortTuple4 { | |
def apply(a:Short, b:Short, c:Short, d:Short) = new ShortTuple4(s4l(a, b, c, d)) | |
def unapply(t:ShortTuple4) = Option(t.tpl) | |
} | |
class ShortTuple4(val u:Long) extends AnyVal { | |
def _1 = (u >> 48).toShort | |
def _2 = (u >> 32).toShort | |
def _3 = (u >> 16).toShort | |
def _4 = u.toShort | |
def tpl = (_1, _2, _3, _4) | |
} | |
object IntTuple2 { | |
def apply(a:Int, b:Int) = new IntTuple2(i2l(a, b)) | |
def unapply(t:IntTuple2) = Option(t.tpl) | |
} | |
class IntTuple2(val u:Long) extends AnyVal { | |
def _1 = (u >> 32).toInt | |
def _2 = u.toInt | |
def tpl = (_1, _2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment