From a15be295198e97152e5cc58d957b1d91b4d58a1d Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Tue, 21 Mar 2017 09:36:11 +0300 Subject: [PATCH] More of stdlib (#363) --- runtime/src/main/kotlin/kotlin/Arrays.kt | 457 ++++++++++++++++++ .../kotlin/kotlin/collections/Collections.kt | 113 +++++ 2 files changed, 570 insertions(+) diff --git a/runtime/src/main/kotlin/kotlin/Arrays.kt b/runtime/src/main/kotlin/kotlin/Arrays.kt index 9778063c0dd..f285aa9ebd0 100644 --- a/runtime/src/main/kotlin/kotlin/Arrays.kt +++ b/runtime/src/main/kotlin/kotlin/Arrays.kt @@ -316,6 +316,367 @@ private class BooleanIteratorImpl(val collection: BooleanArray) : BooleanIterato // This part is from generated _Arrays.kt. + +/** + * Returns 1st *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun Array.component1(): T { + return get(0) +} + +/** + * Returns 1st *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun ByteArray.component1(): Byte { + return get(0) +} + +/** + * Returns 1st *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun ShortArray.component1(): Short { + return get(0) +} + +/** + * Returns 1st *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun IntArray.component1(): Int { + return get(0) +} + +/** + * Returns 1st *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun LongArray.component1(): Long { + return get(0) +} + +/** + * Returns 1st *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun FloatArray.component1(): Float { + return get(0) +} + +/** + * Returns 1st *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun DoubleArray.component1(): Double { + return get(0) +} + +/** + * Returns 1st *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun BooleanArray.component1(): Boolean { + return get(0) +} + +/** + * Returns 1st *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun CharArray.component1(): Char { + return get(0) +} + +/** + * Returns 2nd *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun Array.component2(): T { + return get(1) +} + +/** + * Returns 2nd *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun ByteArray.component2(): Byte { + return get(1) +} + +/** + * Returns 2nd *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun ShortArray.component2(): Short { + return get(1) +} + +/** + * Returns 2nd *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun IntArray.component2(): Int { + return get(1) +} + +/** + * Returns 2nd *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun LongArray.component2(): Long { + return get(1) +} + +/** + * Returns 2nd *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun FloatArray.component2(): Float { + return get(1) +} + +/** + * Returns 2nd *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun DoubleArray.component2(): Double { + return get(1) +} + +/** + * Returns 2nd *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun BooleanArray.component2(): Boolean { + return get(1) +} + +/** + * Returns 2nd *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun CharArray.component2(): Char { + return get(1) +} + +/** + * Returns 3rd *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun Array.component3(): T { + return get(2) +} + +/** + * Returns 3rd *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun ByteArray.component3(): Byte { + return get(2) +} + +/** + * Returns 3rd *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun ShortArray.component3(): Short { + return get(2) +} + +/** + * Returns 3rd *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun IntArray.component3(): Int { + return get(2) +} + +/** + * Returns 3rd *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun LongArray.component3(): Long { + return get(2) +} + +/** + * Returns 3rd *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun FloatArray.component3(): Float { + return get(2) +} + +/** + * Returns 3rd *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun DoubleArray.component3(): Double { + return get(2) +} + +/** + * Returns 3rd *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun BooleanArray.component3(): Boolean { + return get(2) +} + +/** + * Returns 3rd *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun CharArray.component3(): Char { + return get(2) +} + +/** + * Returns 4th *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun Array.component4(): T { + return get(3) +} + +/** + * Returns 4th *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun ByteArray.component4(): Byte { + return get(3) +} + +/** + * Returns 4th *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun ShortArray.component4(): Short { + return get(3) +} + +/** + * Returns 4th *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun IntArray.component4(): Int { + return get(3) +} + +/** + * Returns 4th *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun LongArray.component4(): Long { + return get(3) +} + +/** + * Returns 4th *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun FloatArray.component4(): Float { + return get(3) +} + +/** + * Returns 4th *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun DoubleArray.component4(): Double { + return get(3) +} + +/** + * Returns 4th *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun BooleanArray.component4(): Boolean { + return get(3) +} + +/** + * Returns 4th *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun CharArray.component4(): Char { + return get(3) +} + +/** + * Returns 5th *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun Array.component5(): T { + return get(4) +} + +/** + * Returns 5th *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun ByteArray.component5(): Byte { + return get(4) +} + +/** + * Returns 5th *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun ShortArray.component5(): Short { + return get(4) +} + +/** + * Returns 5th *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun IntArray.component5(): Int { + return get(4) +} + +/** + * Returns 5th *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun LongArray.component5(): Long { + return get(4) +} + +/** + * Returns 5th *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun FloatArray.component5(): Float { + return get(4) +} + +/** + * Returns 5th *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun DoubleArray.component5(): Double { + return get(4) +} + +/** + * Returns 5th *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun BooleanArray.component5(): Boolean { + return get(4) +} + +/** + * Returns 5th *element* from the collection. + */ +@kotlin.internal.InlineOnly +public inline operator fun CharArray.component5(): Char { + return get(4) +} + /** * Returns `true` if all elements match the given [predicate]. */ @@ -1765,6 +2126,102 @@ public fun <@kotlin.internal.OnlyInputTypes T> Array.lastIndexOf(element: return -1 } +/** + * Returns last index of [element], or -1 if the array does not contain element. + */ +public fun ByteArray.lastIndexOf(element: Byte): Int { + for (index in indices.reversed()) { + if (element == this[index]) { + return index + } + } + return -1 +} + +/** + * Returns last index of [element], or -1 if the array does not contain element. + */ +public fun ShortArray.lastIndexOf(element: Short): Int { + for (index in indices.reversed()) { + if (element == this[index]) { + return index + } + } + return -1 +} + +/** + * Returns last index of [element], or -1 if the array does not contain element. + */ +public fun IntArray.lastIndexOf(element: Int): Int { + for (index in indices.reversed()) { + if (element == this[index]) { + return index + } + } + return -1 +} + +/** + * Returns last index of [element], or -1 if the array does not contain element. + */ +public fun LongArray.lastIndexOf(element: Long): Int { + for (index in indices.reversed()) { + if (element == this[index]) { + return index + } + } + return -1 +} + +/** + * Returns last index of [element], or -1 if the array does not contain element. + */ +public fun FloatArray.lastIndexOf(element: Float): Int { + for (index in indices.reversed()) { + if (element == this[index]) { + return index + } + } + return -1 +} + +/** + * Returns last index of [element], or -1 if the array does not contain element. + */ +public fun DoubleArray.lastIndexOf(element: Double): Int { + for (index in indices.reversed()) { + if (element == this[index]) { + return index + } + } + return -1 +} + +/** + * Returns last index of [element], or -1 if the array does not contain element. + */ +public fun BooleanArray.lastIndexOf(element: Boolean): Int { + for (index in indices.reversed()) { + if (element == this[index]) { + return index + } + } + return -1 +} + +/** + * Returns last index of [element], or -1 if the array does not contain element. + */ +public fun CharArray.lastIndexOf(element: Char): Int { + for (index in indices.reversed()) { + if (element == this[index]) { + return index + } + } + return -1 +} + /** * Appends all elements to the given [destination] collection. */ diff --git a/runtime/src/main/kotlin/kotlin/collections/Collections.kt b/runtime/src/main/kotlin/kotlin/collections/Collections.kt index 249f6e8f2cb..f2488bb8945 100644 --- a/runtime/src/main/kotlin/kotlin/collections/Collections.kt +++ b/runtime/src/main/kotlin/kotlin/collections/Collections.kt @@ -160,6 +160,119 @@ public fun Array.asList(): List { } } +/** + * Returns a [List] that wraps the original array. + */ +public fun ByteArray.asList(): List { + return object : AbstractList(), RandomAccess { + override val size: Int get() = this@asList.size + override fun isEmpty(): Boolean = this@asList.isEmpty() + override fun contains(element: Byte): Boolean = this@asList.contains(element) + override fun get(index: Int): Byte = this@asList[index] + override fun indexOf(element: Byte): Int = this@asList.indexOf(element) + override fun lastIndexOf(element: Byte): Int = this@asList.lastIndexOf(element) + } +} + +/** + * Returns a [List] that wraps the original array. + */ +public fun ShortArray.asList(): List { + return object : AbstractList(), RandomAccess { + override val size: Int get() = this@asList.size + override fun isEmpty(): Boolean = this@asList.isEmpty() + override fun contains(element: Short): Boolean = this@asList.contains(element) + override fun get(index: Int): Short = this@asList[index] + override fun indexOf(element: Short): Int = this@asList.indexOf(element) + override fun lastIndexOf(element: Short): Int = this@asList.lastIndexOf(element) + } +} + +/** + * Returns a [List] that wraps the original array. + */ +public fun IntArray.asList(): List { + return object : AbstractList(), RandomAccess { + override val size: Int get() = this@asList.size + override fun isEmpty(): Boolean = this@asList.isEmpty() + override fun contains(element: Int): Boolean = this@asList.contains(element) + override fun get(index: Int): Int = this@asList[index] + override fun indexOf(element: Int): Int = this@asList.indexOf(element) + override fun lastIndexOf(element: Int): Int = this@asList.lastIndexOf(element) + } +} + +/** + * Returns a [List] that wraps the original array. + */ +public fun LongArray.asList(): List { + return object : AbstractList(), RandomAccess { + override val size: Int get() = this@asList.size + override fun isEmpty(): Boolean = this@asList.isEmpty() + override fun contains(element: Long): Boolean = this@asList.contains(element) + override fun get(index: Int): Long = this@asList[index] + override fun indexOf(element: Long): Int = this@asList.indexOf(element) + override fun lastIndexOf(element: Long): Int = this@asList.lastIndexOf(element) + } +} + +/** + * Returns a [List] that wraps the original array. + */ +public fun FloatArray.asList(): List { + return object : AbstractList(), RandomAccess { + override val size: Int get() = this@asList.size + override fun isEmpty(): Boolean = this@asList.isEmpty() + override fun contains(element: Float): Boolean = this@asList.contains(element) + override fun get(index: Int): Float = this@asList[index] + override fun indexOf(element: Float): Int = this@asList.indexOf(element) + override fun lastIndexOf(element: Float): Int = this@asList.lastIndexOf(element) + } +} + +/** + * Returns a [List] that wraps the original array. + */ +public fun DoubleArray.asList(): List { + return object : AbstractList(), RandomAccess { + override val size: Int get() = this@asList.size + override fun isEmpty(): Boolean = this@asList.isEmpty() + override fun contains(element: Double): Boolean = this@asList.contains(element) + override fun get(index: Int): Double = this@asList[index] + override fun indexOf(element: Double): Int = this@asList.indexOf(element) + override fun lastIndexOf(element: Double): Int = this@asList.lastIndexOf(element) + } +} + +/** + * Returns a [List] that wraps the original array. + */ +public fun BooleanArray.asList(): List { + return object : AbstractList(), RandomAccess { + override val size: Int get() = this@asList.size + override fun isEmpty(): Boolean = this@asList.isEmpty() + override fun contains(element: Boolean): Boolean = this@asList.contains(element) + override fun get(index: Int): Boolean = this@asList[index] + override fun indexOf(element: Boolean): Int = this@asList.indexOf(element) + override fun lastIndexOf(element: Boolean): Int = this@asList.lastIndexOf(element) + } +} + +/** + * Returns a [List] that wraps the original array. + */ +public fun CharArray.asList(): List { + return object : AbstractList(), RandomAccess { + override val size: Int get() = this@asList.size + override fun isEmpty(): Boolean = this@asList.isEmpty() + override fun contains(element: Char): Boolean = this@asList.contains(element) + override fun get(index: Int): Char = this@asList[index] + override fun indexOf(element: Char): Int = this@asList.indexOf(element) + override fun lastIndexOf(element: Char): Int = this@asList.lastIndexOf(element) + } +} + + fun Array.toSet(): Set { val result = HashSet(this.size) for (e in this) {