translator: add copyOfRange, plus extensions for IntArray

This commit is contained in:
Alexey Stepanov
2016-08-10 18:33:24 +03:00
parent 15cde9a2a9
commit 6c509d0220
2 changed files with 44 additions and 1 deletions
+4 -1
View File
@@ -12,6 +12,9 @@ workspace.xml
build
translator/.gradle/2.9/taskArtifacts
kotstd/kotstd.iml
# Ignore Gradle GUI config
gradle-app.setting
@@ -25,4 +28,4 @@ gradle-app.setting
lib/
.idea/
proto/compiler/protoc-artifacts
proto/compiler/tests
proto/compiler/tests
+40
View File
@@ -37,3 +37,43 @@ class IntArray(var size: Int) {
}
}
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
}