Files
kotlin-fork/kotstd/include/IntArray.kt
T
2016-08-10 18:33:35 +03:00

79 lines
2.0 KiB
Kotlin

external fun kotlinclib_get_int(src: Int, index: Int): Int
external fun kotlinclib_set_int(src: Int, index: Int, value: Int)
class IntArray(var size: Int) {
val data: Int
/** Returns the number of elements in the array. */
//size: Int
init {
this.data = malloc_array(4 * this.size)
}
/** Returns the array element at the given [index]. This method can be called using the index operator. */
operator fun get(index: Int): Int {
return kotlinclib_get_int(this.data, index)
}
/** Sets the element at the given [index] to the given [value]. This method can be called using the index operator. */
operator fun set(index: Int, value: Int) {
kotlinclib_set_int(this.data, index, value)
}
fun clone(): IntArray {
val newInstance = IntArray(this.size)
var index = 0
while (index < this.size) {
val value = this.get(index)
newInstance.set(index, value)
index = index + 1
}
return newInstance
}
}
public fun IntArray.copyOf(newSize: Int): IntArray {
val newInstance = IntArray(newSize)
var index = 0
var end = this.size
if (newSize < this.size) {
end = newSize
}
while (index < end) {
val value = this.get(index)
newInstance.set(index, value)
index = index + 1
}
while (index < newSize) {
newInstance.set(index, 0)
index = index + 1
}
return newInstance
}
public fun IntArray.copyOfRange(fromIndex: Int, toIndex: Int): IntArray {
val newInstance = IntArray(toIndex - fromIndex + 1)
var index = fromIndex
while (index <= toIndex) {
val value = this.get(index)
newInstance.set(index - fromIndex, value)
index = index + 1
}
return newInstance
}
public operator fun IntArray.plus(element: Int): IntArray {
val index = size
val result = this.copyOf(index + 1)
result[index] = element
return result
}