From 6c509d022012f749a0c7f9648f784b829dda286b Mon Sep 17 00:00:00 2001 From: Alexey Stepanov Date: Wed, 10 Aug 2016 18:33:24 +0300 Subject: [PATCH] translator: add copyOfRange, plus extensions for IntArray --- .gitignore | 5 ++++- kotstd/include/IntArray.kt | 40 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index fc5d90a0819..d16fad24508 100644 --- a/.gitignore +++ b/.gitignore @@ -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 \ No newline at end of file +proto/compiler/tests diff --git a/kotstd/include/IntArray.kt b/kotstd/include/IntArray.kt index c2a03b4940c..326a83a41ea 100644 --- a/kotstd/include/IntArray.kt +++ b/kotstd/include/IntArray.kt @@ -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 +} \ No newline at end of file