Skip to content

Instantly share code, notes, and snippets.

@LearningNerd
Forked from curioustorvald/ByteArray64.kt
Last active April 11, 2017 16:11
Show Gist options
  • Save LearningNerd/c2b375808c6915dc6b650c076ca99bca to your computer and use it in GitHub Desktop.
Save LearningNerd/c2b375808c6915dc6b650c076ca99bca to your computer and use it in GitHub Desktop.
a brand new test gist!
/**
* ByteArray that can hold larger than 4 GiB of Data.
*
* Works kind of like Bank Switching of old game console's cartridges which does same thing.
*
* Created by Minjaesong on 2017-04-12.
*/
class ByteArray64(val size: Long) {
private val bankSize: Int = 1 shr 30 // 2^30 Bytes, or 1 GiB
private val data: Array<ByteArray>
init {
if (size <= 0)
throw IllegalArgumentException("Invalid array size!")
val requiredChunks = Math.ceil(size.toDouble() / bankSize).toInt()
data = Array<ByteArray>(requiredChunks, { kotlin.ByteArray(bankSize, { 0.toByte() }) })
}
private fun Long.toBankNumber(): Int = (this / bankSize).toInt()
private fun Long.toBankOffset(): Int = (this % bankSize).toInt()
operator fun set(index: Long, value: Byte) {
if (index < 0 || index >= size)
throw ArrayIndexOutOfBoundsException()
data[index.toBankNumber()][index.toBankOffset()] = value
}
operator fun get(index: Long): Byte {
if (index < 0 || index >= size)
throw ArrayIndexOutOfBoundsException()
return data[index.toBankNumber()][index.toBankOffset()]
}
}

Here is some updated content added to the beginning of this gist. This is ANOTHER test gist made with the GitHub Gists API via a client-side POST request using AJAX!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment