Merge branch 'master' of github.com:JetBrains/kotlin
This commit is contained in:
@@ -88,6 +88,7 @@ public inline fun FloatArray.copyOf(newLength: Int = this.size) : FloatArray
|
||||
public inline fun DoubleArray.copyOf(newLength: Int = this.size) : DoubleArray = Arrays.copyOf(this, newLength).sure()
|
||||
public inline fun CharArray.copyOf(newLength: Int = this.size) : CharArray = Arrays.copyOf(this, newLength).sure()
|
||||
|
||||
// TODO: resuling array may contain nulls even if T is non-nullable
|
||||
public inline fun <T> Array<T>.copyOf(newLength: Int = this.size) : Array<T> = Arrays.copyOf(this as Array<T?>, newLength) as Array<T>
|
||||
|
||||
public inline fun BooleanArray.copyOfRange(from: Int, to: Int) : BooleanArray = Arrays.copyOfRange(this, from, to).sure()
|
||||
@@ -99,6 +100,7 @@ public inline fun FloatArray.copyOfRange(from: Int, to: Int) : FloatArray =
|
||||
public inline fun DoubleArray.copyOfRange(from: Int, to: Int) : DoubleArray = Arrays.copyOfRange(this, from, to).sure()
|
||||
public inline fun CharArray.copyOfRange(from: Int, to: Int) : CharArray = Arrays.copyOfRange(this, from, to).sure()
|
||||
|
||||
// TODO: resuling array may contain nulls even if T is non-nullable
|
||||
public inline fun <T> Array<T>.copyOfRange(from: Int, to: Int) : Array<T> = Arrays.copyOfRange(this as Array<T?>, from, to) as Array<T>
|
||||
|
||||
public inline val ByteArray.inputStream : ByteArrayInputStream
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
package kotlin
|
||||
|
||||
import java.util.List
|
||||
import java.util.AbstractList
|
||||
|
||||
private class ImmutableArrayList<T>(
|
||||
private val array: Array<T>,
|
||||
private val offset: Int,
|
||||
private val length: Int
|
||||
) : AbstractList<T>() {
|
||||
{
|
||||
// impossible
|
||||
if (offset < 0) {
|
||||
throw IllegalArgumentException("negative offset ($offset)")
|
||||
}
|
||||
// impossible
|
||||
if (length < 0) {
|
||||
throw IllegalArgumentException("negative length ($length)")
|
||||
}
|
||||
// possible when builder is used from different threads
|
||||
if (offset + length > array.size) {
|
||||
throw IllegalArgumentException("offset ($offset) + length ($length) > array.length (${array.size})")
|
||||
}
|
||||
}
|
||||
|
||||
protected fun indexInArray(index: Int): Int {
|
||||
if (index < 0) {
|
||||
throw IndexOutOfBoundsException("negative index ($index)")
|
||||
}
|
||||
if (index >= length) {
|
||||
throw IndexOutOfBoundsException("index ($index) >= length ($length)")
|
||||
}
|
||||
return index + offset
|
||||
}
|
||||
|
||||
public override fun get(index: Int): T = array[indexInArray(index)] as T
|
||||
|
||||
public override fun size() : Int = length
|
||||
|
||||
public override fun subList(fromIndex: Int, toIndex: Int) : List<T> {
|
||||
if (fromIndex < 0) {
|
||||
throw IndexOutOfBoundsException("negative from index ($fromIndex)")
|
||||
}
|
||||
if (toIndex < fromIndex) {
|
||||
throw IndexOutOfBoundsException("toIndex ($toIndex) < fromIndex ($fromIndex)")
|
||||
}
|
||||
if (toIndex > length) {
|
||||
throw IndexOutOfBoundsException("fromIndex ($fromIndex) + toIndex ($toIndex) > length ($length)")
|
||||
}
|
||||
if (fromIndex == toIndex) {
|
||||
return emptyImmutableArrayList as List<T>
|
||||
}
|
||||
if (fromIndex == 0 && toIndex == length) {
|
||||
return this
|
||||
}
|
||||
return ImmutableArrayList(array, offset + fromIndex, toIndex - fromIndex)
|
||||
}
|
||||
|
||||
// TODO: efficiently implement iterator and other stuff
|
||||
}
|
||||
|
||||
// TODO: make private val, see http://youtrack.jetbrains.com/issue/KT-2028
|
||||
internal val emptyArray = arrayOfNulls<Any?>(0)
|
||||
internal val emptyImmutableArrayList = ImmutableArrayList<Any?>(emptyArray, 0, 0)
|
||||
|
||||
public class ImmutableArrayListBuilder<T>() {
|
||||
|
||||
private var array = emptyArray
|
||||
private var length = 0
|
||||
|
||||
public fun build(): List<T> {
|
||||
if (length == 0) {
|
||||
return emptyImmutableArrayList as List<T>
|
||||
}
|
||||
else {
|
||||
val r = ImmutableArrayList<T>(array as Array<T>, 0, length)
|
||||
array = emptyArray
|
||||
length = 0
|
||||
return r
|
||||
}
|
||||
}
|
||||
|
||||
public fun ensureCapacity(capacity: Int) {
|
||||
if (array.size < capacity) {
|
||||
val newSize = Math.max(capacity, Math.max(array.size * 2, 11))
|
||||
array = array.copyOf(newSize)
|
||||
}
|
||||
}
|
||||
|
||||
public fun add(item: T) {
|
||||
ensureCapacity(length + 1)
|
||||
array[length] = item
|
||||
++length
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// default list builder
|
||||
public fun <T> listBuilder(): ImmutableArrayListBuilder<T> = ImmutableArrayListBuilder<T>()
|
||||
Reference in New Issue
Block a user