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!
-
-
Save LearningNerd/c2b375808c6915dc6b650c076ca99bca to your computer and use it in GitHub Desktop.
a brand new test gist!
This file contains hidden or 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
/** | |
* 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()] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment