Skip to content

Instantly share code, notes, and snippets.

@hilltracer
Last active November 22, 2021 09:03
Show Gist options
  • Save hilltracer/104a9a0b3fb760b4249f0c642a93c7b3 to your computer and use it in GitHub Desktop.
Save hilltracer/104a9a0b3fb760b4249f0c642a93c7b3 to your computer and use it in GitHub Desktop.
val size = loop(0, 0)
//put in the buffer
val buf = ByteBuffer.allocate(size)
node.zipWithIndex foreach {
case (EmptyChild, _) => Unit
case (Leaf(leafPrefix, value, varLength), idx) =>
buf.put(idx.toByte) //first byte - number of children
if (varLength) {
//01000000b | (size & 00011111b)
buf.put((0x40 | (leafPrefix.size & 0x1F)).toByte) //second byte - type and size of prefix
buf.put(leafPrefix.toArray) //prefix sequence
buf.put(value.size.toByte) //size of value
} else {
//size & 00011111b
buf.put((leafPrefix.size & 0x1F).toByte) //second byte - type and size of prefix
buf.put(leafPrefix.toArray) //prefix sequence
}
buf.put(value.toArray) //value sequence
case (NodePtr(ptrPrefix, ptr), idx) =>
buf.put(idx.toByte) //first byte - number of children
//10000000b | (size & 00011111b)
buf.put((0x80 | (ptrPrefix.size & 0x1F)).toByte) //second byte - type and size of prefix
buf.put(ptrPrefix.toArray) //prefix sequence
buf.put(ptr.toArray) //size of value
}
buf.rewind
val arr = new Array[Byte](buf.remaining())
buf.get(arr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment