Skip to content

Commit

Permalink
translator: add copyOfRange, plus extensions for IntArray
Browse files Browse the repository at this point in the history
  • Loading branch information
alejes committed Aug 10, 2016
1 parent 15cde9a commit 6c509d0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ workspace.xml
build
translator/.gradle/2.9/taskArtifacts

kotstd/kotstd.iml


# Ignore Gradle GUI config
gradle-app.setting

Expand All @@ -25,4 +28,4 @@ gradle-app.setting
lib/
.idea/
proto/compiler/protoc-artifacts
proto/compiler/tests
proto/compiler/tests
40 changes: 40 additions & 0 deletions kotstd/include/IntArray.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 6c509d0

Please sign in to comment.