Created
November 22, 2021 09:05
-
-
Save hilltracer/62c589d02c2e999a4cd81527526fbb1f to your computer and use it in GitHub Desktop.
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
val arr = new Array[Byte](size) | |
@tailrec | |
def loopFill(numChild: Int, idx0: Int): Array[Byte] = | |
if (idx0 == size) arr | |
else { | |
node(numChild) match { | |
case EmptyChild => loopFill(numChild + 1, idx0) | |
case Leaf(prefix, value, varLength) => | |
arr(idx0) = numChild.toByte | |
val idxSecondByte = idx0 + 1 | |
val idxPrefixStart = idxSecondByte + 1 | |
val prefixSize: Int = prefix.size.toInt | |
if (varLength) { | |
//01000000b | (size & 00011111b) | |
arr(idxSecondByte) = (0x40 | (prefixSize & 0x1F)).toByte //second byte - type and size of prefix | |
for (i <- 0 until prefixSize) arr(idxPrefixStart + i) = prefix(i.toLong) | |
val idxValueSize = idxPrefixStart + prefixSize | |
val valueSize = value.size.toInt | |
arr(idxValueSize) = valueSize.toByte | |
val idxValueStart = idxValueSize + 1 | |
for (i <- 0 until valueSize) arr(idxValueStart + i) = value(i.toLong) | |
loopFill(numChild + 1, idxValueStart + valueSize) | |
} else { | |
//size & 00011111b | |
arr(idxSecondByte) = (prefixSize & 0x1F).toByte //second byte - type and size of prefix | |
for (i <- 0 until prefixSize) arr(idxPrefixStart + i) = prefix(i.toLong) | |
val idxValueStart = idxPrefixStart + prefixSize | |
for (i <- 0 until 32) arr(idxValueStart + i) = value(i.toLong) | |
loopFill(numChild + 1, idxValueStart + 32) | |
} | |
case NodePtr(prefix, ptr) => | |
arr(idx0) = numChild.toByte | |
val idxSecondByte = idx0 + 1 | |
val prefixSize: Int = prefix.size.toInt | |
//10000000b | (size & 00011111b) | |
arr(idxSecondByte) = (0x80 | (prefixSize & 0x1F)).toByte //second byte - type and size of prefix | |
val idxPrefixStart = idxSecondByte + 1 | |
for (i <- 0 until prefixSize) arr(idxPrefixStart + i) = prefix(i.toLong) | |
val idxPtrStart = idxPrefixStart + prefixSize | |
for (i <- 0 until 32) arr(idxPtrStart + i) = ptr(i.toLong) | |
loopFill(numChild + 1, idxPtrStart + 32) | |
} | |
} | |
loopFill(0, 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment