From 69ebb3bfb0e7ac53b29f88eaaba2e61e1293685d Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Wed, 22 Nov 2017 07:14:33 +0300 Subject: [PATCH] Switch to the new stdlib source generation scheme Remove old generated sources for stdlib-jvm and stdlib-js --- .../src/core/generated/_ArraysJs.kt | 13887 --------------- .../src/core/generated/_CollectionsJs.kt | 2379 --- .../src/core/generated/_ComparisonsJs.kt | 299 - js/js.libraries/src/core/generated/_MapsJs.kt | 218 - .../src/core/generated/_RangesJs.kt | 961 -- .../src/core/generated/_SequencesJs.kt | 1849 -- js/js.libraries/src/core/generated/_SetsJs.kt | 134 - .../src/core/generated/_StringsJs.kt | 1380 -- .../stdlib/common/src/generated/_Arrays.kt | 7781 +++++++-- .../common/src/generated/_Collections.kt | 1463 +- .../common/src/generated/_Comparisons.kt | 40 +- .../stdlib/common/src/generated/_Maps.kt | 117 +- .../stdlib/common/src/generated/_Ranges.kt | 505 +- .../stdlib/common/src/generated/_Sequences.kt | 929 +- .../stdlib/common/src/generated/_Sets.kt | 65 +- .../stdlib/common/src/generated/_Strings.kt | 752 +- .../stdlib/js/src/generated/_ArraysJs.kt | 1022 ++ .../stdlib/js/src/generated/_CollectionsJs.kt | 30 + .../stdlib/js/src/generated/_ComparisonsJs.kt | 213 + .../stdlib/jvm/src/generated/_ArraysJvm.kt | 1458 ++ .../jvm/src/generated/_CollectionsJvm.kt | 54 + .../jvm/src/generated/_ComparisonsJvm.kt | 213 + .../stdlib/jvm/src/generated/_SequencesJvm.kt | 57 + .../stdlib/jvm/src/generated/_StringsJvm.kt | 22 + libraries/stdlib/src/generated/_Arrays.kt | 14323 ---------------- .../stdlib/src/generated/_Collections.kt | 2403 --- .../stdlib/src/generated/_Comparisons.kt | 299 - libraries/stdlib/src/generated/_Maps.kt | 218 - libraries/stdlib/src/generated/_Ranges.kt | 961 -- libraries/stdlib/src/generated/_Sequences.kt | 1890 -- libraries/stdlib/src/generated/_Sets.kt | 134 - libraries/stdlib/src/generated/_Strings.kt | 1387 -- .../src/generators/GenerateStandardLib.kt | 6 +- .../src/templates/dsl/MemberBuilder.kt | 2 +- 34 files changed, 12985 insertions(+), 44466 deletions(-) delete mode 100644 js/js.libraries/src/core/generated/_ArraysJs.kt delete mode 100644 js/js.libraries/src/core/generated/_CollectionsJs.kt delete mode 100644 js/js.libraries/src/core/generated/_ComparisonsJs.kt delete mode 100644 js/js.libraries/src/core/generated/_MapsJs.kt delete mode 100644 js/js.libraries/src/core/generated/_RangesJs.kt delete mode 100644 js/js.libraries/src/core/generated/_SequencesJs.kt delete mode 100644 js/js.libraries/src/core/generated/_SetsJs.kt delete mode 100644 js/js.libraries/src/core/generated/_StringsJs.kt create mode 100644 libraries/stdlib/js/src/generated/_ArraysJs.kt create mode 100644 libraries/stdlib/js/src/generated/_CollectionsJs.kt create mode 100644 libraries/stdlib/js/src/generated/_ComparisonsJs.kt create mode 100644 libraries/stdlib/jvm/src/generated/_ArraysJvm.kt create mode 100644 libraries/stdlib/jvm/src/generated/_CollectionsJvm.kt create mode 100644 libraries/stdlib/jvm/src/generated/_ComparisonsJvm.kt create mode 100644 libraries/stdlib/jvm/src/generated/_SequencesJvm.kt create mode 100644 libraries/stdlib/jvm/src/generated/_StringsJvm.kt delete mode 100644 libraries/stdlib/src/generated/_Arrays.kt delete mode 100644 libraries/stdlib/src/generated/_Collections.kt delete mode 100644 libraries/stdlib/src/generated/_Comparisons.kt delete mode 100644 libraries/stdlib/src/generated/_Maps.kt delete mode 100644 libraries/stdlib/src/generated/_Ranges.kt delete mode 100644 libraries/stdlib/src/generated/_Sequences.kt delete mode 100644 libraries/stdlib/src/generated/_Sets.kt delete mode 100644 libraries/stdlib/src/generated/_Strings.kt diff --git a/js/js.libraries/src/core/generated/_ArraysJs.kt b/js/js.libraries/src/core/generated/_ArraysJs.kt deleted file mode 100644 index f82b998abb5..00000000000 --- a/js/js.libraries/src/core/generated/_ArraysJs.kt +++ /dev/null @@ -1,13887 +0,0 @@ -@file:kotlin.jvm.JvmMultifileClass -@file:kotlin.jvm.JvmName("ArraysKt") - -package kotlin.collections - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// - -import kotlin.js.* -import primitiveArrayConcat -import withType -import kotlin.* -import kotlin.text.* -import kotlin.comparisons.* - -/** - * 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 [element] is found in the array. - */ -public operator fun <@kotlin.internal.OnlyInputTypes T> Array.contains(element: T): Boolean { - return indexOf(element) >= 0 -} - -/** - * Returns `true` if [element] is found in the array. - */ -public operator fun ByteArray.contains(element: Byte): Boolean { - return indexOf(element) >= 0 -} - -/** - * Returns `true` if [element] is found in the array. - */ -public operator fun ShortArray.contains(element: Short): Boolean { - return indexOf(element) >= 0 -} - -/** - * Returns `true` if [element] is found in the array. - */ -public operator fun IntArray.contains(element: Int): Boolean { - return indexOf(element) >= 0 -} - -/** - * Returns `true` if [element] is found in the array. - */ -public operator fun LongArray.contains(element: Long): Boolean { - return indexOf(element) >= 0 -} - -/** - * Returns `true` if [element] is found in the array. - */ -public operator fun FloatArray.contains(element: Float): Boolean { - return indexOf(element) >= 0 -} - -/** - * Returns `true` if [element] is found in the array. - */ -public operator fun DoubleArray.contains(element: Double): Boolean { - return indexOf(element) >= 0 -} - -/** - * Returns `true` if [element] is found in the array. - */ -public operator fun BooleanArray.contains(element: Boolean): Boolean { - return indexOf(element) >= 0 -} - -/** - * Returns `true` if [element] is found in the array. - */ -public operator fun CharArray.contains(element: Char): Boolean { - return indexOf(element) >= 0 -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun Array.elementAt(index: Int): T { - return get(index) -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun ByteArray.elementAt(index: Int): Byte { - return get(index) -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun ShortArray.elementAt(index: Int): Short { - return get(index) -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun IntArray.elementAt(index: Int): Int { - return get(index) -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun LongArray.elementAt(index: Int): Long { - return get(index) -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun FloatArray.elementAt(index: Int): Float { - return get(index) -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun DoubleArray.elementAt(index: Int): Double { - return get(index) -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun BooleanArray.elementAt(index: Int): Boolean { - return get(index) -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun CharArray.elementAt(index: Int): Char { - return get(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun Array.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun ByteArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Byte): Byte { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun ShortArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Short): Short { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun IntArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Int): Int { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun LongArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Long): Long { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun FloatArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Float): Float { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun DoubleArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Double): Double { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun BooleanArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Boolean): Boolean { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun CharArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Char): Char { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun Array.elementAtOrNull(index: Int): T? { - return this.getOrNull(index) -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun ByteArray.elementAtOrNull(index: Int): Byte? { - return this.getOrNull(index) -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun ShortArray.elementAtOrNull(index: Int): Short? { - return this.getOrNull(index) -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun IntArray.elementAtOrNull(index: Int): Int? { - return this.getOrNull(index) -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun LongArray.elementAtOrNull(index: Int): Long? { - return this.getOrNull(index) -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun FloatArray.elementAtOrNull(index: Int): Float? { - return this.getOrNull(index) -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun DoubleArray.elementAtOrNull(index: Int): Double? { - return this.getOrNull(index) -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun BooleanArray.elementAtOrNull(index: Int): Boolean? { - return this.getOrNull(index) -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun CharArray.elementAtOrNull(index: Int): Char? { - return this.getOrNull(index) -} - -/** - * Returns the first element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun Array.find(predicate: (T) -> Boolean): T? { - return firstOrNull(predicate) -} - -/** - * Returns the first element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun ByteArray.find(predicate: (Byte) -> Boolean): Byte? { - return firstOrNull(predicate) -} - -/** - * Returns the first element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun ShortArray.find(predicate: (Short) -> Boolean): Short? { - return firstOrNull(predicate) -} - -/** - * Returns the first element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun IntArray.find(predicate: (Int) -> Boolean): Int? { - return firstOrNull(predicate) -} - -/** - * Returns the first element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun LongArray.find(predicate: (Long) -> Boolean): Long? { - return firstOrNull(predicate) -} - -/** - * Returns the first element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun FloatArray.find(predicate: (Float) -> Boolean): Float? { - return firstOrNull(predicate) -} - -/** - * Returns the first element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun DoubleArray.find(predicate: (Double) -> Boolean): Double? { - return firstOrNull(predicate) -} - -/** - * Returns the first element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun BooleanArray.find(predicate: (Boolean) -> Boolean): Boolean? { - return firstOrNull(predicate) -} - -/** - * Returns the first element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun CharArray.find(predicate: (Char) -> Boolean): Char? { - return firstOrNull(predicate) -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun Array.findLast(predicate: (T) -> Boolean): T? { - return lastOrNull(predicate) -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun ByteArray.findLast(predicate: (Byte) -> Boolean): Byte? { - return lastOrNull(predicate) -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun ShortArray.findLast(predicate: (Short) -> Boolean): Short? { - return lastOrNull(predicate) -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun IntArray.findLast(predicate: (Int) -> Boolean): Int? { - return lastOrNull(predicate) -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun LongArray.findLast(predicate: (Long) -> Boolean): Long? { - return lastOrNull(predicate) -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun FloatArray.findLast(predicate: (Float) -> Boolean): Float? { - return lastOrNull(predicate) -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun DoubleArray.findLast(predicate: (Double) -> Boolean): Double? { - return lastOrNull(predicate) -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun BooleanArray.findLast(predicate: (Boolean) -> Boolean): Boolean? { - return lastOrNull(predicate) -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun CharArray.findLast(predicate: (Char) -> Boolean): Char? { - return lastOrNull(predicate) -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun Array.first(): T { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[0] -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun ByteArray.first(): Byte { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[0] -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun ShortArray.first(): Short { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[0] -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun IntArray.first(): Int { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[0] -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun LongArray.first(): Long { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[0] -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun FloatArray.first(): Float { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[0] -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun DoubleArray.first(): Double { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[0] -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun BooleanArray.first(): Boolean { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[0] -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun CharArray.first(): Char { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[0] -} - -/** - * Returns the first element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun Array.first(predicate: (T) -> Boolean): T { - for (element in this) if (predicate(element)) return element - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns the first element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun ByteArray.first(predicate: (Byte) -> Boolean): Byte { - for (element in this) if (predicate(element)) return element - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns the first element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun ShortArray.first(predicate: (Short) -> Boolean): Short { - for (element in this) if (predicate(element)) return element - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns the first element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun IntArray.first(predicate: (Int) -> Boolean): Int { - for (element in this) if (predicate(element)) return element - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns the first element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun LongArray.first(predicate: (Long) -> Boolean): Long { - for (element in this) if (predicate(element)) return element - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns the first element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun FloatArray.first(predicate: (Float) -> Boolean): Float { - for (element in this) if (predicate(element)) return element - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns the first element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun DoubleArray.first(predicate: (Double) -> Boolean): Double { - for (element in this) if (predicate(element)) return element - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns the first element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun BooleanArray.first(predicate: (Boolean) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return element - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns the first element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun CharArray.first(predicate: (Char) -> Boolean): Char { - for (element in this) if (predicate(element)) return element - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns the first element, or `null` if the array is empty. - */ -public fun Array.firstOrNull(): T? { - return if (isEmpty()) null else this[0] -} - -/** - * Returns the first element, or `null` if the array is empty. - */ -public fun ByteArray.firstOrNull(): Byte? { - return if (isEmpty()) null else this[0] -} - -/** - * Returns the first element, or `null` if the array is empty. - */ -public fun ShortArray.firstOrNull(): Short? { - return if (isEmpty()) null else this[0] -} - -/** - * Returns the first element, or `null` if the array is empty. - */ -public fun IntArray.firstOrNull(): Int? { - return if (isEmpty()) null else this[0] -} - -/** - * Returns the first element, or `null` if the array is empty. - */ -public fun LongArray.firstOrNull(): Long? { - return if (isEmpty()) null else this[0] -} - -/** - * Returns the first element, or `null` if the array is empty. - */ -public fun FloatArray.firstOrNull(): Float? { - return if (isEmpty()) null else this[0] -} - -/** - * Returns the first element, or `null` if the array is empty. - */ -public fun DoubleArray.firstOrNull(): Double? { - return if (isEmpty()) null else this[0] -} - -/** - * Returns the first element, or `null` if the array is empty. - */ -public fun BooleanArray.firstOrNull(): Boolean? { - return if (isEmpty()) null else this[0] -} - -/** - * Returns the first element, or `null` if the array is empty. - */ -public fun CharArray.firstOrNull(): Char? { - return if (isEmpty()) null else this[0] -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - */ -public inline fun Array.firstOrNull(predicate: (T) -> Boolean): T? { - for (element in this) if (predicate(element)) return element - return null -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - */ -public inline fun ByteArray.firstOrNull(predicate: (Byte) -> Boolean): Byte? { - for (element in this) if (predicate(element)) return element - return null -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - */ -public inline fun ShortArray.firstOrNull(predicate: (Short) -> Boolean): Short? { - for (element in this) if (predicate(element)) return element - return null -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - */ -public inline fun IntArray.firstOrNull(predicate: (Int) -> Boolean): Int? { - for (element in this) if (predicate(element)) return element - return null -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - */ -public inline fun LongArray.firstOrNull(predicate: (Long) -> Boolean): Long? { - for (element in this) if (predicate(element)) return element - return null -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - */ -public inline fun FloatArray.firstOrNull(predicate: (Float) -> Boolean): Float? { - for (element in this) if (predicate(element)) return element - return null -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - */ -public inline fun DoubleArray.firstOrNull(predicate: (Double) -> Boolean): Double? { - for (element in this) if (predicate(element)) return element - return null -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - */ -public inline fun BooleanArray.firstOrNull(predicate: (Boolean) -> Boolean): Boolean? { - for (element in this) if (predicate(element)) return element - return null -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - */ -public inline fun CharArray.firstOrNull(predicate: (Char) -> Boolean): Char? { - for (element in this) if (predicate(element)) return element - return null -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun Array.getOrElse(index: Int, defaultValue: (Int) -> T): T { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun ByteArray.getOrElse(index: Int, defaultValue: (Int) -> Byte): Byte { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun ShortArray.getOrElse(index: Int, defaultValue: (Int) -> Short): Short { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun IntArray.getOrElse(index: Int, defaultValue: (Int) -> Int): Int { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun LongArray.getOrElse(index: Int, defaultValue: (Int) -> Long): Long { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun FloatArray.getOrElse(index: Int, defaultValue: (Int) -> Float): Float { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun DoubleArray.getOrElse(index: Int, defaultValue: (Int) -> Double): Double { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun BooleanArray.getOrElse(index: Int, defaultValue: (Int) -> Boolean): Boolean { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun CharArray.getOrElse(index: Int, defaultValue: (Int) -> Char): Char { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -public fun Array.getOrNull(index: Int): T? { - return if (index >= 0 && index <= lastIndex) get(index) else null -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -public fun ByteArray.getOrNull(index: Int): Byte? { - return if (index >= 0 && index <= lastIndex) get(index) else null -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -public fun ShortArray.getOrNull(index: Int): Short? { - return if (index >= 0 && index <= lastIndex) get(index) else null -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -public fun IntArray.getOrNull(index: Int): Int? { - return if (index >= 0 && index <= lastIndex) get(index) else null -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -public fun LongArray.getOrNull(index: Int): Long? { - return if (index >= 0 && index <= lastIndex) get(index) else null -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -public fun FloatArray.getOrNull(index: Int): Float? { - return if (index >= 0 && index <= lastIndex) get(index) else null -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -public fun DoubleArray.getOrNull(index: Int): Double? { - return if (index >= 0 && index <= lastIndex) get(index) else null -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -public fun BooleanArray.getOrNull(index: Int): Boolean? { - return if (index >= 0 && index <= lastIndex) get(index) else null -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -public fun CharArray.getOrNull(index: Int): Char? { - return if (index >= 0 && index <= lastIndex) get(index) else null -} - -/** - * Returns first index of [element], or -1 if the array does not contain element. - */ -public fun <@kotlin.internal.OnlyInputTypes T> Array.indexOf(element: T): Int { - if (element == null) { - for (index in indices) { - if (this[index] == null) { - return index - } - } - } else { - for (index in indices) { - if (element == this[index]) { - return index - } - } - } - return -1 -} - -/** - * Returns first index of [element], or -1 if the array does not contain element. - */ -public fun ByteArray.indexOf(element: Byte): Int { - for (index in indices) { - if (element == this[index]) { - return index - } - } - return -1 -} - -/** - * Returns first index of [element], or -1 if the array does not contain element. - */ -public fun ShortArray.indexOf(element: Short): Int { - for (index in indices) { - if (element == this[index]) { - return index - } - } - return -1 -} - -/** - * Returns first index of [element], or -1 if the array does not contain element. - */ -public fun IntArray.indexOf(element: Int): Int { - for (index in indices) { - if (element == this[index]) { - return index - } - } - return -1 -} - -/** - * Returns first index of [element], or -1 if the array does not contain element. - */ -public fun LongArray.indexOf(element: Long): Int { - for (index in indices) { - if (element == this[index]) { - return index - } - } - return -1 -} - -/** - * Returns first index of [element], or -1 if the array does not contain element. - */ -public fun FloatArray.indexOf(element: Float): Int { - for (index in indices) { - if (element == this[index]) { - return index - } - } - return -1 -} - -/** - * Returns first index of [element], or -1 if the array does not contain element. - */ -public fun DoubleArray.indexOf(element: Double): Int { - for (index in indices) { - if (element == this[index]) { - return index - } - } - return -1 -} - -/** - * Returns first index of [element], or -1 if the array does not contain element. - */ -public fun BooleanArray.indexOf(element: Boolean): Int { - for (index in indices) { - if (element == this[index]) { - return index - } - } - return -1 -} - -/** - * Returns first index of [element], or -1 if the array does not contain element. - */ -public fun CharArray.indexOf(element: Char): Int { - for (index in indices) { - if (element == this[index]) { - return index - } - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun Array.indexOfFirst(predicate: (T) -> Boolean): Int { - for (index in indices) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun ByteArray.indexOfFirst(predicate: (Byte) -> Boolean): Int { - for (index in indices) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun ShortArray.indexOfFirst(predicate: (Short) -> Boolean): Int { - for (index in indices) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun IntArray.indexOfFirst(predicate: (Int) -> Boolean): Int { - for (index in indices) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun LongArray.indexOfFirst(predicate: (Long) -> Boolean): Int { - for (index in indices) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun FloatArray.indexOfFirst(predicate: (Float) -> Boolean): Int { - for (index in indices) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun DoubleArray.indexOfFirst(predicate: (Double) -> Boolean): Int { - for (index in indices) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun BooleanArray.indexOfFirst(predicate: (Boolean) -> Boolean): Int { - for (index in indices) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun CharArray.indexOfFirst(predicate: (Char) -> Boolean): Int { - for (index in indices) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun Array.indexOfLast(predicate: (T) -> Boolean): Int { - for (index in indices.reversed()) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun ByteArray.indexOfLast(predicate: (Byte) -> Boolean): Int { - for (index in indices.reversed()) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun ShortArray.indexOfLast(predicate: (Short) -> Boolean): Int { - for (index in indices.reversed()) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun IntArray.indexOfLast(predicate: (Int) -> Boolean): Int { - for (index in indices.reversed()) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun LongArray.indexOfLast(predicate: (Long) -> Boolean): Int { - for (index in indices.reversed()) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun FloatArray.indexOfLast(predicate: (Float) -> Boolean): Int { - for (index in indices.reversed()) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun DoubleArray.indexOfLast(predicate: (Double) -> Boolean): Int { - for (index in indices.reversed()) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun BooleanArray.indexOfLast(predicate: (Boolean) -> Boolean): Int { - for (index in indices.reversed()) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun CharArray.indexOfLast(predicate: (Char) -> Boolean): Int { - for (index in indices.reversed()) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun Array.last(): T { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[lastIndex] -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun ByteArray.last(): Byte { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[lastIndex] -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun ShortArray.last(): Short { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[lastIndex] -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun IntArray.last(): Int { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[lastIndex] -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun LongArray.last(): Long { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[lastIndex] -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun FloatArray.last(): Float { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[lastIndex] -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun DoubleArray.last(): Double { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[lastIndex] -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun BooleanArray.last(): Boolean { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[lastIndex] -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun CharArray.last(): Char { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[lastIndex] -} - -/** - * Returns the last element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun Array.last(predicate: (T) -> Boolean): T { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns the last element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun ByteArray.last(predicate: (Byte) -> Boolean): Byte { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns the last element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun ShortArray.last(predicate: (Short) -> Boolean): Short { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns the last element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun IntArray.last(predicate: (Int) -> Boolean): Int { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns the last element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun LongArray.last(predicate: (Long) -> Boolean): Long { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns the last element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun FloatArray.last(predicate: (Float) -> Boolean): Float { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns the last element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun DoubleArray.last(predicate: (Double) -> Boolean): Double { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns the last element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun BooleanArray.last(predicate: (Boolean) -> Boolean): Boolean { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns the last element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun CharArray.last(predicate: (Char) -> Boolean): Char { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns last index of [element], or -1 if the array does not contain element. - */ -public fun <@kotlin.internal.OnlyInputTypes T> Array.lastIndexOf(element: T): Int { - if (element == null) { - for (index in indices.reversed()) { - if (this[index] == null) { - return index - } - } - } else { - 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 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 -} - -/** - * Returns the last element, or `null` if the array is empty. - */ -public fun Array.lastOrNull(): T? { - return if (isEmpty()) null else this[size - 1] -} - -/** - * Returns the last element, or `null` if the array is empty. - */ -public fun ByteArray.lastOrNull(): Byte? { - return if (isEmpty()) null else this[size - 1] -} - -/** - * Returns the last element, or `null` if the array is empty. - */ -public fun ShortArray.lastOrNull(): Short? { - return if (isEmpty()) null else this[size - 1] -} - -/** - * Returns the last element, or `null` if the array is empty. - */ -public fun IntArray.lastOrNull(): Int? { - return if (isEmpty()) null else this[size - 1] -} - -/** - * Returns the last element, or `null` if the array is empty. - */ -public fun LongArray.lastOrNull(): Long? { - return if (isEmpty()) null else this[size - 1] -} - -/** - * Returns the last element, or `null` if the array is empty. - */ -public fun FloatArray.lastOrNull(): Float? { - return if (isEmpty()) null else this[size - 1] -} - -/** - * Returns the last element, or `null` if the array is empty. - */ -public fun DoubleArray.lastOrNull(): Double? { - return if (isEmpty()) null else this[size - 1] -} - -/** - * Returns the last element, or `null` if the array is empty. - */ -public fun BooleanArray.lastOrNull(): Boolean? { - return if (isEmpty()) null else this[size - 1] -} - -/** - * Returns the last element, or `null` if the array is empty. - */ -public fun CharArray.lastOrNull(): Char? { - return if (isEmpty()) null else this[size - 1] -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -public inline fun Array.lastOrNull(predicate: (T) -> Boolean): T? { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - return null -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -public inline fun ByteArray.lastOrNull(predicate: (Byte) -> Boolean): Byte? { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - return null -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -public inline fun ShortArray.lastOrNull(predicate: (Short) -> Boolean): Short? { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - return null -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -public inline fun IntArray.lastOrNull(predicate: (Int) -> Boolean): Int? { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - return null -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -public inline fun LongArray.lastOrNull(predicate: (Long) -> Boolean): Long? { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - return null -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -public inline fun FloatArray.lastOrNull(predicate: (Float) -> Boolean): Float? { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - return null -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -public inline fun DoubleArray.lastOrNull(predicate: (Double) -> Boolean): Double? { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - return null -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -public inline fun BooleanArray.lastOrNull(predicate: (Boolean) -> Boolean): Boolean? { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - return null -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -public inline fun CharArray.lastOrNull(predicate: (Char) -> Boolean): Char? { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - return null -} - -/** - * Returns the single element, or throws an exception if the array is empty or has more than one element. - */ -public fun Array.single(): T { - return when (size) { - 0 -> throw NoSuchElementException("Array is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Array has more than one element.") - } -} - -/** - * Returns the single element, or throws an exception if the array is empty or has more than one element. - */ -public fun ByteArray.single(): Byte { - return when (size) { - 0 -> throw NoSuchElementException("Array is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Array has more than one element.") - } -} - -/** - * Returns the single element, or throws an exception if the array is empty or has more than one element. - */ -public fun ShortArray.single(): Short { - return when (size) { - 0 -> throw NoSuchElementException("Array is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Array has more than one element.") - } -} - -/** - * Returns the single element, or throws an exception if the array is empty or has more than one element. - */ -public fun IntArray.single(): Int { - return when (size) { - 0 -> throw NoSuchElementException("Array is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Array has more than one element.") - } -} - -/** - * Returns the single element, or throws an exception if the array is empty or has more than one element. - */ -public fun LongArray.single(): Long { - return when (size) { - 0 -> throw NoSuchElementException("Array is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Array has more than one element.") - } -} - -/** - * Returns the single element, or throws an exception if the array is empty or has more than one element. - */ -public fun FloatArray.single(): Float { - return when (size) { - 0 -> throw NoSuchElementException("Array is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Array has more than one element.") - } -} - -/** - * Returns the single element, or throws an exception if the array is empty or has more than one element. - */ -public fun DoubleArray.single(): Double { - return when (size) { - 0 -> throw NoSuchElementException("Array is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Array has more than one element.") - } -} - -/** - * Returns the single element, or throws an exception if the array is empty or has more than one element. - */ -public fun BooleanArray.single(): Boolean { - return when (size) { - 0 -> throw NoSuchElementException("Array is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Array has more than one element.") - } -} - -/** - * Returns the single element, or throws an exception if the array is empty or has more than one element. - */ -public fun CharArray.single(): Char { - return when (size) { - 0 -> throw NoSuchElementException("Array is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Array has more than one element.") - } -} - -/** - * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. - */ -public inline fun Array.single(predicate: (T) -> Boolean): T { - var single: T? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Array contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") - @Suppress("UNCHECKED_CAST") - return single as T -} - -/** - * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. - */ -public inline fun ByteArray.single(predicate: (Byte) -> Boolean): Byte { - var single: Byte? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Array contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") - @Suppress("UNCHECKED_CAST") - return single as Byte -} - -/** - * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. - */ -public inline fun ShortArray.single(predicate: (Short) -> Boolean): Short { - var single: Short? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Array contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") - @Suppress("UNCHECKED_CAST") - return single as Short -} - -/** - * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. - */ -public inline fun IntArray.single(predicate: (Int) -> Boolean): Int { - var single: Int? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Array contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") - @Suppress("UNCHECKED_CAST") - return single as Int -} - -/** - * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. - */ -public inline fun LongArray.single(predicate: (Long) -> Boolean): Long { - var single: Long? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Array contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") - @Suppress("UNCHECKED_CAST") - return single as Long -} - -/** - * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. - */ -public inline fun FloatArray.single(predicate: (Float) -> Boolean): Float { - var single: Float? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Array contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") - @Suppress("UNCHECKED_CAST") - return single as Float -} - -/** - * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. - */ -public inline fun DoubleArray.single(predicate: (Double) -> Boolean): Double { - var single: Double? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Array contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") - @Suppress("UNCHECKED_CAST") - return single as Double -} - -/** - * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. - */ -public inline fun BooleanArray.single(predicate: (Boolean) -> Boolean): Boolean { - var single: Boolean? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Array contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") - @Suppress("UNCHECKED_CAST") - return single as Boolean -} - -/** - * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. - */ -public inline fun CharArray.single(predicate: (Char) -> Boolean): Char { - var single: Char? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Array contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") - @Suppress("UNCHECKED_CAST") - return single as Char -} - -/** - * Returns single element, or `null` if the array is empty or has more than one element. - */ -public fun Array.singleOrNull(): T? { - return if (size == 1) this[0] else null -} - -/** - * Returns single element, or `null` if the array is empty or has more than one element. - */ -public fun ByteArray.singleOrNull(): Byte? { - return if (size == 1) this[0] else null -} - -/** - * Returns single element, or `null` if the array is empty or has more than one element. - */ -public fun ShortArray.singleOrNull(): Short? { - return if (size == 1) this[0] else null -} - -/** - * Returns single element, or `null` if the array is empty or has more than one element. - */ -public fun IntArray.singleOrNull(): Int? { - return if (size == 1) this[0] else null -} - -/** - * Returns single element, or `null` if the array is empty or has more than one element. - */ -public fun LongArray.singleOrNull(): Long? { - return if (size == 1) this[0] else null -} - -/** - * Returns single element, or `null` if the array is empty or has more than one element. - */ -public fun FloatArray.singleOrNull(): Float? { - return if (size == 1) this[0] else null -} - -/** - * Returns single element, or `null` if the array is empty or has more than one element. - */ -public fun DoubleArray.singleOrNull(): Double? { - return if (size == 1) this[0] else null -} - -/** - * Returns single element, or `null` if the array is empty or has more than one element. - */ -public fun BooleanArray.singleOrNull(): Boolean? { - return if (size == 1) this[0] else null -} - -/** - * Returns single element, or `null` if the array is empty or has more than one element. - */ -public fun CharArray.singleOrNull(): Char? { - return if (size == 1) this[0] else null -} - -/** - * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. - */ -public inline fun Array.singleOrNull(predicate: (T) -> Boolean): T? { - var single: T? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) return null - single = element - found = true - } - } - if (!found) return null - return single -} - -/** - * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. - */ -public inline fun ByteArray.singleOrNull(predicate: (Byte) -> Boolean): Byte? { - var single: Byte? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) return null - single = element - found = true - } - } - if (!found) return null - return single -} - -/** - * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. - */ -public inline fun ShortArray.singleOrNull(predicate: (Short) -> Boolean): Short? { - var single: Short? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) return null - single = element - found = true - } - } - if (!found) return null - return single -} - -/** - * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. - */ -public inline fun IntArray.singleOrNull(predicate: (Int) -> Boolean): Int? { - var single: Int? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) return null - single = element - found = true - } - } - if (!found) return null - return single -} - -/** - * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. - */ -public inline fun LongArray.singleOrNull(predicate: (Long) -> Boolean): Long? { - var single: Long? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) return null - single = element - found = true - } - } - if (!found) return null - return single -} - -/** - * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. - */ -public inline fun FloatArray.singleOrNull(predicate: (Float) -> Boolean): Float? { - var single: Float? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) return null - single = element - found = true - } - } - if (!found) return null - return single -} - -/** - * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. - */ -public inline fun DoubleArray.singleOrNull(predicate: (Double) -> Boolean): Double? { - var single: Double? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) return null - single = element - found = true - } - } - if (!found) return null - return single -} - -/** - * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. - */ -public inline fun BooleanArray.singleOrNull(predicate: (Boolean) -> Boolean): Boolean? { - var single: Boolean? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) return null - single = element - found = true - } - } - if (!found) return null - return single -} - -/** - * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. - */ -public inline fun CharArray.singleOrNull(predicate: (Char) -> Boolean): Char? { - var single: Char? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) return null - single = element - found = true - } - } - if (!found) return null - return single -} - -/** - * Returns a list containing all elements except first [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun Array.drop(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return takeLast((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except first [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun ByteArray.drop(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return takeLast((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except first [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun ShortArray.drop(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return takeLast((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except first [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun IntArray.drop(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return takeLast((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except first [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun LongArray.drop(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return takeLast((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except first [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun FloatArray.drop(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return takeLast((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except first [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun DoubleArray.drop(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return takeLast((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except first [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun BooleanArray.drop(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return takeLast((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except first [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun CharArray.drop(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return takeLast((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except last [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun Array.dropLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return take((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except last [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun ByteArray.dropLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return take((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except last [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun ShortArray.dropLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return take((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except last [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun IntArray.dropLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return take((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except last [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun LongArray.dropLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return take((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except last [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun FloatArray.dropLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return take((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except last [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun DoubleArray.dropLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return take((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except last [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun BooleanArray.dropLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return take((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except last [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun CharArray.dropLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return take((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except last elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun Array.dropLastWhile(predicate: (T) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return take(index + 1) - } - } - return emptyList() -} - -/** - * Returns a list containing all elements except last elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun ByteArray.dropLastWhile(predicate: (Byte) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return take(index + 1) - } - } - return emptyList() -} - -/** - * Returns a list containing all elements except last elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun ShortArray.dropLastWhile(predicate: (Short) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return take(index + 1) - } - } - return emptyList() -} - -/** - * Returns a list containing all elements except last elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun IntArray.dropLastWhile(predicate: (Int) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return take(index + 1) - } - } - return emptyList() -} - -/** - * Returns a list containing all elements except last elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun LongArray.dropLastWhile(predicate: (Long) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return take(index + 1) - } - } - return emptyList() -} - -/** - * Returns a list containing all elements except last elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun FloatArray.dropLastWhile(predicate: (Float) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return take(index + 1) - } - } - return emptyList() -} - -/** - * Returns a list containing all elements except last elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun DoubleArray.dropLastWhile(predicate: (Double) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return take(index + 1) - } - } - return emptyList() -} - -/** - * Returns a list containing all elements except last elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun BooleanArray.dropLastWhile(predicate: (Boolean) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return take(index + 1) - } - } - return emptyList() -} - -/** - * Returns a list containing all elements except last elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun CharArray.dropLastWhile(predicate: (Char) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return take(index + 1) - } - } - return emptyList() -} - -/** - * Returns a list containing all elements except first elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun Array.dropWhile(predicate: (T) -> Boolean): List { - var yielding = false - val list = ArrayList() - for (item in this) - if (yielding) - list.add(item) - else if (!predicate(item)) { - list.add(item) - yielding = true - } - return list -} - -/** - * Returns a list containing all elements except first elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun ByteArray.dropWhile(predicate: (Byte) -> Boolean): List { - var yielding = false - val list = ArrayList() - for (item in this) - if (yielding) - list.add(item) - else if (!predicate(item)) { - list.add(item) - yielding = true - } - return list -} - -/** - * Returns a list containing all elements except first elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun ShortArray.dropWhile(predicate: (Short) -> Boolean): List { - var yielding = false - val list = ArrayList() - for (item in this) - if (yielding) - list.add(item) - else if (!predicate(item)) { - list.add(item) - yielding = true - } - return list -} - -/** - * Returns a list containing all elements except first elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun IntArray.dropWhile(predicate: (Int) -> Boolean): List { - var yielding = false - val list = ArrayList() - for (item in this) - if (yielding) - list.add(item) - else if (!predicate(item)) { - list.add(item) - yielding = true - } - return list -} - -/** - * Returns a list containing all elements except first elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun LongArray.dropWhile(predicate: (Long) -> Boolean): List { - var yielding = false - val list = ArrayList() - for (item in this) - if (yielding) - list.add(item) - else if (!predicate(item)) { - list.add(item) - yielding = true - } - return list -} - -/** - * Returns a list containing all elements except first elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun FloatArray.dropWhile(predicate: (Float) -> Boolean): List { - var yielding = false - val list = ArrayList() - for (item in this) - if (yielding) - list.add(item) - else if (!predicate(item)) { - list.add(item) - yielding = true - } - return list -} - -/** - * Returns a list containing all elements except first elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun DoubleArray.dropWhile(predicate: (Double) -> Boolean): List { - var yielding = false - val list = ArrayList() - for (item in this) - if (yielding) - list.add(item) - else if (!predicate(item)) { - list.add(item) - yielding = true - } - return list -} - -/** - * Returns a list containing all elements except first elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun BooleanArray.dropWhile(predicate: (Boolean) -> Boolean): List { - var yielding = false - val list = ArrayList() - for (item in this) - if (yielding) - list.add(item) - else if (!predicate(item)) { - list.add(item) - yielding = true - } - return list -} - -/** - * Returns a list containing all elements except first elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun CharArray.dropWhile(predicate: (Char) -> Boolean): List { - var yielding = false - val list = ArrayList() - for (item in this) - if (yielding) - list.add(item) - else if (!predicate(item)) { - list.add(item) - yielding = true - } - return list -} - -/** - * Returns a list containing only elements matching the given [predicate]. - */ -public inline fun Array.filter(predicate: (T) -> Boolean): List { - return filterTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - */ -public inline fun ByteArray.filter(predicate: (Byte) -> Boolean): List { - return filterTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - */ -public inline fun ShortArray.filter(predicate: (Short) -> Boolean): List { - return filterTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - */ -public inline fun IntArray.filter(predicate: (Int) -> Boolean): List { - return filterTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - */ -public inline fun LongArray.filter(predicate: (Long) -> Boolean): List { - return filterTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - */ -public inline fun FloatArray.filter(predicate: (Float) -> Boolean): List { - return filterTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - */ -public inline fun DoubleArray.filter(predicate: (Double) -> Boolean): List { - return filterTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - */ -public inline fun BooleanArray.filter(predicate: (Boolean) -> Boolean): List { - return filterTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - */ -public inline fun CharArray.filter(predicate: (Char) -> Boolean): List { - return filterTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun Array.filterIndexed(predicate: (index: Int, T) -> Boolean): List { - return filterIndexedTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun ByteArray.filterIndexed(predicate: (index: Int, Byte) -> Boolean): List { - return filterIndexedTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun ShortArray.filterIndexed(predicate: (index: Int, Short) -> Boolean): List { - return filterIndexedTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun IntArray.filterIndexed(predicate: (index: Int, Int) -> Boolean): List { - return filterIndexedTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun LongArray.filterIndexed(predicate: (index: Int, Long) -> Boolean): List { - return filterIndexedTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun FloatArray.filterIndexed(predicate: (index: Int, Float) -> Boolean): List { - return filterIndexedTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun DoubleArray.filterIndexed(predicate: (index: Int, Double) -> Boolean): List { - return filterIndexedTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun BooleanArray.filterIndexed(predicate: (index: Int, Boolean) -> Boolean): List { - return filterIndexedTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun CharArray.filterIndexed(predicate: (index: Int, Char) -> Boolean): List { - return filterIndexedTo(ArrayList(), predicate) -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun > Array.filterIndexedTo(destination: C, predicate: (index: Int, T) -> Boolean): C { - forEachIndexed { index, element -> - if (predicate(index, element)) destination.add(element) - } - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun > ByteArray.filterIndexedTo(destination: C, predicate: (index: Int, Byte) -> Boolean): C { - forEachIndexed { index, element -> - if (predicate(index, element)) destination.add(element) - } - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun > ShortArray.filterIndexedTo(destination: C, predicate: (index: Int, Short) -> Boolean): C { - forEachIndexed { index, element -> - if (predicate(index, element)) destination.add(element) - } - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun > IntArray.filterIndexedTo(destination: C, predicate: (index: Int, Int) -> Boolean): C { - forEachIndexed { index, element -> - if (predicate(index, element)) destination.add(element) - } - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun > LongArray.filterIndexedTo(destination: C, predicate: (index: Int, Long) -> Boolean): C { - forEachIndexed { index, element -> - if (predicate(index, element)) destination.add(element) - } - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun > FloatArray.filterIndexedTo(destination: C, predicate: (index: Int, Float) -> Boolean): C { - forEachIndexed { index, element -> - if (predicate(index, element)) destination.add(element) - } - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun > DoubleArray.filterIndexedTo(destination: C, predicate: (index: Int, Double) -> Boolean): C { - forEachIndexed { index, element -> - if (predicate(index, element)) destination.add(element) - } - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun > BooleanArray.filterIndexedTo(destination: C, predicate: (index: Int, Boolean) -> Boolean): C { - forEachIndexed { index, element -> - if (predicate(index, element)) destination.add(element) - } - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun > CharArray.filterIndexedTo(destination: C, predicate: (index: Int, Char) -> Boolean): C { - forEachIndexed { index, element -> - if (predicate(index, element)) destination.add(element) - } - return destination -} - -/** - * Returns a list containing all elements that are instances of specified type parameter R. - */ -public inline fun Array<*>.filterIsInstance(): List<@kotlin.internal.NoInfer R> { - return filterIsInstanceTo(ArrayList()) -} - -/** - * Appends all elements that are instances of specified type parameter R to the given [destination]. - */ -public inline fun > Array<*>.filterIsInstanceTo(destination: C): C { - for (element in this) if (element is R) destination.add(element) - return destination -} - -/** - * Returns a list containing all elements not matching the given [predicate]. - */ -public inline fun Array.filterNot(predicate: (T) -> Boolean): List { - return filterNotTo(ArrayList(), predicate) -} - -/** - * Returns a list containing all elements not matching the given [predicate]. - */ -public inline fun ByteArray.filterNot(predicate: (Byte) -> Boolean): List { - return filterNotTo(ArrayList(), predicate) -} - -/** - * Returns a list containing all elements not matching the given [predicate]. - */ -public inline fun ShortArray.filterNot(predicate: (Short) -> Boolean): List { - return filterNotTo(ArrayList(), predicate) -} - -/** - * Returns a list containing all elements not matching the given [predicate]. - */ -public inline fun IntArray.filterNot(predicate: (Int) -> Boolean): List { - return filterNotTo(ArrayList(), predicate) -} - -/** - * Returns a list containing all elements not matching the given [predicate]. - */ -public inline fun LongArray.filterNot(predicate: (Long) -> Boolean): List { - return filterNotTo(ArrayList(), predicate) -} - -/** - * Returns a list containing all elements not matching the given [predicate]. - */ -public inline fun FloatArray.filterNot(predicate: (Float) -> Boolean): List { - return filterNotTo(ArrayList(), predicate) -} - -/** - * Returns a list containing all elements not matching the given [predicate]. - */ -public inline fun DoubleArray.filterNot(predicate: (Double) -> Boolean): List { - return filterNotTo(ArrayList(), predicate) -} - -/** - * Returns a list containing all elements not matching the given [predicate]. - */ -public inline fun BooleanArray.filterNot(predicate: (Boolean) -> Boolean): List { - return filterNotTo(ArrayList(), predicate) -} - -/** - * Returns a list containing all elements not matching the given [predicate]. - */ -public inline fun CharArray.filterNot(predicate: (Char) -> Boolean): List { - return filterNotTo(ArrayList(), predicate) -} - -/** - * Returns a list containing all elements that are not `null`. - */ -public fun Array.filterNotNull(): List { - return filterNotNullTo(ArrayList()) -} - -/** - * Appends all elements that are not `null` to the given [destination]. - */ -public fun , T : Any> Array.filterNotNullTo(destination: C): C { - for (element in this) if (element != null) destination.add(element) - return destination -} - -/** - * Appends all elements not matching the given [predicate] to the given [destination]. - */ -public inline fun > Array.filterNotTo(destination: C, predicate: (T) -> Boolean): C { - for (element in this) if (!predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements not matching the given [predicate] to the given [destination]. - */ -public inline fun > ByteArray.filterNotTo(destination: C, predicate: (Byte) -> Boolean): C { - for (element in this) if (!predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements not matching the given [predicate] to the given [destination]. - */ -public inline fun > ShortArray.filterNotTo(destination: C, predicate: (Short) -> Boolean): C { - for (element in this) if (!predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements not matching the given [predicate] to the given [destination]. - */ -public inline fun > IntArray.filterNotTo(destination: C, predicate: (Int) -> Boolean): C { - for (element in this) if (!predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements not matching the given [predicate] to the given [destination]. - */ -public inline fun > LongArray.filterNotTo(destination: C, predicate: (Long) -> Boolean): C { - for (element in this) if (!predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements not matching the given [predicate] to the given [destination]. - */ -public inline fun > FloatArray.filterNotTo(destination: C, predicate: (Float) -> Boolean): C { - for (element in this) if (!predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements not matching the given [predicate] to the given [destination]. - */ -public inline fun > DoubleArray.filterNotTo(destination: C, predicate: (Double) -> Boolean): C { - for (element in this) if (!predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements not matching the given [predicate] to the given [destination]. - */ -public inline fun > BooleanArray.filterNotTo(destination: C, predicate: (Boolean) -> Boolean): C { - for (element in this) if (!predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements not matching the given [predicate] to the given [destination]. - */ -public inline fun > CharArray.filterNotTo(destination: C, predicate: (Char) -> Boolean): C { - for (element in this) if (!predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - */ -public inline fun > Array.filterTo(destination: C, predicate: (T) -> Boolean): C { - for (element in this) if (predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - */ -public inline fun > ByteArray.filterTo(destination: C, predicate: (Byte) -> Boolean): C { - for (element in this) if (predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - */ -public inline fun > ShortArray.filterTo(destination: C, predicate: (Short) -> Boolean): C { - for (element in this) if (predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - */ -public inline fun > IntArray.filterTo(destination: C, predicate: (Int) -> Boolean): C { - for (element in this) if (predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - */ -public inline fun > LongArray.filterTo(destination: C, predicate: (Long) -> Boolean): C { - for (element in this) if (predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - */ -public inline fun > FloatArray.filterTo(destination: C, predicate: (Float) -> Boolean): C { - for (element in this) if (predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - */ -public inline fun > DoubleArray.filterTo(destination: C, predicate: (Double) -> Boolean): C { - for (element in this) if (predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - */ -public inline fun > BooleanArray.filterTo(destination: C, predicate: (Boolean) -> Boolean): C { - for (element in this) if (predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - */ -public inline fun > CharArray.filterTo(destination: C, predicate: (Char) -> Boolean): C { - for (element in this) if (predicate(element)) destination.add(element) - return destination -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun Array.slice(indices: IntRange): List { - if (indices.isEmpty()) return listOf() - return copyOfRange(indices.start, indices.endInclusive + 1).asList() -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun ByteArray.slice(indices: IntRange): List { - if (indices.isEmpty()) return listOf() - return copyOfRange(indices.start, indices.endInclusive + 1).asList() -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun ShortArray.slice(indices: IntRange): List { - if (indices.isEmpty()) return listOf() - return copyOfRange(indices.start, indices.endInclusive + 1).asList() -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun IntArray.slice(indices: IntRange): List { - if (indices.isEmpty()) return listOf() - return copyOfRange(indices.start, indices.endInclusive + 1).asList() -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun LongArray.slice(indices: IntRange): List { - if (indices.isEmpty()) return listOf() - return copyOfRange(indices.start, indices.endInclusive + 1).asList() -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun FloatArray.slice(indices: IntRange): List { - if (indices.isEmpty()) return listOf() - return copyOfRange(indices.start, indices.endInclusive + 1).asList() -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun DoubleArray.slice(indices: IntRange): List { - if (indices.isEmpty()) return listOf() - return copyOfRange(indices.start, indices.endInclusive + 1).asList() -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun BooleanArray.slice(indices: IntRange): List { - if (indices.isEmpty()) return listOf() - return copyOfRange(indices.start, indices.endInclusive + 1).asList() -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun CharArray.slice(indices: IntRange): List { - if (indices.isEmpty()) return listOf() - return copyOfRange(indices.start, indices.endInclusive + 1).asList() -} - -/** - * Returns a list containing elements at specified [indices]. - */ -public fun Array.slice(indices: Iterable): List { - val size = indices.collectionSizeOrDefault(10) - if (size == 0) return emptyList() - val list = ArrayList(size) - for (index in indices) { - list.add(get(index)) - } - return list -} - -/** - * Returns a list containing elements at specified [indices]. - */ -public fun ByteArray.slice(indices: Iterable): List { - val size = indices.collectionSizeOrDefault(10) - if (size == 0) return emptyList() - val list = ArrayList(size) - for (index in indices) { - list.add(get(index)) - } - return list -} - -/** - * Returns a list containing elements at specified [indices]. - */ -public fun ShortArray.slice(indices: Iterable): List { - val size = indices.collectionSizeOrDefault(10) - if (size == 0) return emptyList() - val list = ArrayList(size) - for (index in indices) { - list.add(get(index)) - } - return list -} - -/** - * Returns a list containing elements at specified [indices]. - */ -public fun IntArray.slice(indices: Iterable): List { - val size = indices.collectionSizeOrDefault(10) - if (size == 0) return emptyList() - val list = ArrayList(size) - for (index in indices) { - list.add(get(index)) - } - return list -} - -/** - * Returns a list containing elements at specified [indices]. - */ -public fun LongArray.slice(indices: Iterable): List { - val size = indices.collectionSizeOrDefault(10) - if (size == 0) return emptyList() - val list = ArrayList(size) - for (index in indices) { - list.add(get(index)) - } - return list -} - -/** - * Returns a list containing elements at specified [indices]. - */ -public fun FloatArray.slice(indices: Iterable): List { - val size = indices.collectionSizeOrDefault(10) - if (size == 0) return emptyList() - val list = ArrayList(size) - for (index in indices) { - list.add(get(index)) - } - return list -} - -/** - * Returns a list containing elements at specified [indices]. - */ -public fun DoubleArray.slice(indices: Iterable): List { - val size = indices.collectionSizeOrDefault(10) - if (size == 0) return emptyList() - val list = ArrayList(size) - for (index in indices) { - list.add(get(index)) - } - return list -} - -/** - * Returns a list containing elements at specified [indices]. - */ -public fun BooleanArray.slice(indices: Iterable): List { - val size = indices.collectionSizeOrDefault(10) - if (size == 0) return emptyList() - val list = ArrayList(size) - for (index in indices) { - list.add(get(index)) - } - return list -} - -/** - * Returns a list containing elements at specified [indices]. - */ -public fun CharArray.slice(indices: Iterable): List { - val size = indices.collectionSizeOrDefault(10) - if (size == 0) return emptyList() - val list = ArrayList(size) - for (index in indices) { - list.add(get(index)) - } - return list -} - -/** - * Returns an array containing elements of this array at specified [indices]. - */ -public fun Array.sliceArray(indices: Collection): Array { - val result = arrayOfNulls(this, indices.size) - var targetIndex = 0 - for (sourceIndex in indices) { - result[targetIndex++] = this[sourceIndex] - } - return result -} - -/** - * Returns an array containing elements of this array at specified [indices]. - */ -public fun ByteArray.sliceArray(indices: Collection): ByteArray { - val result = ByteArray(indices.size) - var targetIndex = 0 - for (sourceIndex in indices) { - result[targetIndex++] = this[sourceIndex] - } - return result -} - -/** - * Returns an array containing elements of this array at specified [indices]. - */ -public fun ShortArray.sliceArray(indices: Collection): ShortArray { - val result = ShortArray(indices.size) - var targetIndex = 0 - for (sourceIndex in indices) { - result[targetIndex++] = this[sourceIndex] - } - return result -} - -/** - * Returns an array containing elements of this array at specified [indices]. - */ -public fun IntArray.sliceArray(indices: Collection): IntArray { - val result = IntArray(indices.size) - var targetIndex = 0 - for (sourceIndex in indices) { - result[targetIndex++] = this[sourceIndex] - } - return result -} - -/** - * Returns an array containing elements of this array at specified [indices]. - */ -public fun LongArray.sliceArray(indices: Collection): LongArray { - val result = LongArray(indices.size) - var targetIndex = 0 - for (sourceIndex in indices) { - result[targetIndex++] = this[sourceIndex] - } - return result -} - -/** - * Returns an array containing elements of this array at specified [indices]. - */ -public fun FloatArray.sliceArray(indices: Collection): FloatArray { - val result = FloatArray(indices.size) - var targetIndex = 0 - for (sourceIndex in indices) { - result[targetIndex++] = this[sourceIndex] - } - return result -} - -/** - * Returns an array containing elements of this array at specified [indices]. - */ -public fun DoubleArray.sliceArray(indices: Collection): DoubleArray { - val result = DoubleArray(indices.size) - var targetIndex = 0 - for (sourceIndex in indices) { - result[targetIndex++] = this[sourceIndex] - } - return result -} - -/** - * Returns an array containing elements of this array at specified [indices]. - */ -public fun BooleanArray.sliceArray(indices: Collection): BooleanArray { - val result = BooleanArray(indices.size) - var targetIndex = 0 - for (sourceIndex in indices) { - result[targetIndex++] = this[sourceIndex] - } - return result -} - -/** - * Returns an array containing elements of this array at specified [indices]. - */ -public fun CharArray.sliceArray(indices: Collection): CharArray { - val result = CharArray(indices.size) - var targetIndex = 0 - for (sourceIndex in indices) { - result[targetIndex++] = this[sourceIndex] - } - return result -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun Array.sliceArray(indices: IntRange): Array { - if (indices.isEmpty()) return copyOfRange(0, 0) - return copyOfRange(indices.start, indices.endInclusive + 1) -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun ByteArray.sliceArray(indices: IntRange): ByteArray { - if (indices.isEmpty()) return ByteArray(0) - return copyOfRange(indices.start, indices.endInclusive + 1) -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun ShortArray.sliceArray(indices: IntRange): ShortArray { - if (indices.isEmpty()) return ShortArray(0) - return copyOfRange(indices.start, indices.endInclusive + 1) -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun IntArray.sliceArray(indices: IntRange): IntArray { - if (indices.isEmpty()) return IntArray(0) - return copyOfRange(indices.start, indices.endInclusive + 1) -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun LongArray.sliceArray(indices: IntRange): LongArray { - if (indices.isEmpty()) return LongArray(0) - return copyOfRange(indices.start, indices.endInclusive + 1) -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun FloatArray.sliceArray(indices: IntRange): FloatArray { - if (indices.isEmpty()) return FloatArray(0) - return copyOfRange(indices.start, indices.endInclusive + 1) -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun DoubleArray.sliceArray(indices: IntRange): DoubleArray { - if (indices.isEmpty()) return DoubleArray(0) - return copyOfRange(indices.start, indices.endInclusive + 1) -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun BooleanArray.sliceArray(indices: IntRange): BooleanArray { - if (indices.isEmpty()) return BooleanArray(0) - return copyOfRange(indices.start, indices.endInclusive + 1) -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun CharArray.sliceArray(indices: IntRange): CharArray { - if (indices.isEmpty()) return CharArray(0) - return copyOfRange(indices.start, indices.endInclusive + 1) -} - -/** - * Returns a list containing first [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun Array.take(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - if (n >= size) return toList() - if (n == 1) return listOf(this[0]) - var count = 0 - val list = ArrayList(n) - for (item in this) { - if (count++ == n) - break - list.add(item) - } - return list -} - -/** - * Returns a list containing first [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun ByteArray.take(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - if (n >= size) return toList() - if (n == 1) return listOf(this[0]) - var count = 0 - val list = ArrayList(n) - for (item in this) { - if (count++ == n) - break - list.add(item) - } - return list -} - -/** - * Returns a list containing first [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun ShortArray.take(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - if (n >= size) return toList() - if (n == 1) return listOf(this[0]) - var count = 0 - val list = ArrayList(n) - for (item in this) { - if (count++ == n) - break - list.add(item) - } - return list -} - -/** - * Returns a list containing first [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun IntArray.take(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - if (n >= size) return toList() - if (n == 1) return listOf(this[0]) - var count = 0 - val list = ArrayList(n) - for (item in this) { - if (count++ == n) - break - list.add(item) - } - return list -} - -/** - * Returns a list containing first [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun LongArray.take(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - if (n >= size) return toList() - if (n == 1) return listOf(this[0]) - var count = 0 - val list = ArrayList(n) - for (item in this) { - if (count++ == n) - break - list.add(item) - } - return list -} - -/** - * Returns a list containing first [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun FloatArray.take(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - if (n >= size) return toList() - if (n == 1) return listOf(this[0]) - var count = 0 - val list = ArrayList(n) - for (item in this) { - if (count++ == n) - break - list.add(item) - } - return list -} - -/** - * Returns a list containing first [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun DoubleArray.take(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - if (n >= size) return toList() - if (n == 1) return listOf(this[0]) - var count = 0 - val list = ArrayList(n) - for (item in this) { - if (count++ == n) - break - list.add(item) - } - return list -} - -/** - * Returns a list containing first [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun BooleanArray.take(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - if (n >= size) return toList() - if (n == 1) return listOf(this[0]) - var count = 0 - val list = ArrayList(n) - for (item in this) { - if (count++ == n) - break - list.add(item) - } - return list -} - -/** - * Returns a list containing first [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun CharArray.take(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - if (n >= size) return toList() - if (n == 1) return listOf(this[0]) - var count = 0 - val list = ArrayList(n) - for (item in this) { - if (count++ == n) - break - list.add(item) - } - return list -} - -/** - * Returns a list containing last [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun Array.takeLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - val size = size - if (n >= size) return toList() - if (n == 1) return listOf(this[size - 1]) - val list = ArrayList(n) - for (index in size - n until size) - list.add(this[index]) - return list -} - -/** - * Returns a list containing last [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun ByteArray.takeLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - val size = size - if (n >= size) return toList() - if (n == 1) return listOf(this[size - 1]) - val list = ArrayList(n) - for (index in size - n until size) - list.add(this[index]) - return list -} - -/** - * Returns a list containing last [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun ShortArray.takeLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - val size = size - if (n >= size) return toList() - if (n == 1) return listOf(this[size - 1]) - val list = ArrayList(n) - for (index in size - n until size) - list.add(this[index]) - return list -} - -/** - * Returns a list containing last [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun IntArray.takeLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - val size = size - if (n >= size) return toList() - if (n == 1) return listOf(this[size - 1]) - val list = ArrayList(n) - for (index in size - n until size) - list.add(this[index]) - return list -} - -/** - * Returns a list containing last [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun LongArray.takeLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - val size = size - if (n >= size) return toList() - if (n == 1) return listOf(this[size - 1]) - val list = ArrayList(n) - for (index in size - n until size) - list.add(this[index]) - return list -} - -/** - * Returns a list containing last [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun FloatArray.takeLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - val size = size - if (n >= size) return toList() - if (n == 1) return listOf(this[size - 1]) - val list = ArrayList(n) - for (index in size - n until size) - list.add(this[index]) - return list -} - -/** - * Returns a list containing last [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun DoubleArray.takeLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - val size = size - if (n >= size) return toList() - if (n == 1) return listOf(this[size - 1]) - val list = ArrayList(n) - for (index in size - n until size) - list.add(this[index]) - return list -} - -/** - * Returns a list containing last [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun BooleanArray.takeLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - val size = size - if (n >= size) return toList() - if (n == 1) return listOf(this[size - 1]) - val list = ArrayList(n) - for (index in size - n until size) - list.add(this[index]) - return list -} - -/** - * Returns a list containing last [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun CharArray.takeLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - val size = size - if (n >= size) return toList() - if (n == 1) return listOf(this[size - 1]) - val list = ArrayList(n) - for (index in size - n until size) - list.add(this[index]) - return list -} - -/** - * Returns a list containing last elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun Array.takeLastWhile(predicate: (T) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return drop(index + 1) - } - } - return toList() -} - -/** - * Returns a list containing last elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun ByteArray.takeLastWhile(predicate: (Byte) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return drop(index + 1) - } - } - return toList() -} - -/** - * Returns a list containing last elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun ShortArray.takeLastWhile(predicate: (Short) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return drop(index + 1) - } - } - return toList() -} - -/** - * Returns a list containing last elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun IntArray.takeLastWhile(predicate: (Int) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return drop(index + 1) - } - } - return toList() -} - -/** - * Returns a list containing last elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun LongArray.takeLastWhile(predicate: (Long) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return drop(index + 1) - } - } - return toList() -} - -/** - * Returns a list containing last elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun FloatArray.takeLastWhile(predicate: (Float) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return drop(index + 1) - } - } - return toList() -} - -/** - * Returns a list containing last elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun DoubleArray.takeLastWhile(predicate: (Double) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return drop(index + 1) - } - } - return toList() -} - -/** - * Returns a list containing last elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun BooleanArray.takeLastWhile(predicate: (Boolean) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return drop(index + 1) - } - } - return toList() -} - -/** - * Returns a list containing last elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun CharArray.takeLastWhile(predicate: (Char) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return drop(index + 1) - } - } - return toList() -} - -/** - * Returns a list containing first elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun Array.takeWhile(predicate: (T) -> Boolean): List { - val list = ArrayList() - for (item in this) { - if (!predicate(item)) - break - list.add(item) - } - return list -} - -/** - * Returns a list containing first elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun ByteArray.takeWhile(predicate: (Byte) -> Boolean): List { - val list = ArrayList() - for (item in this) { - if (!predicate(item)) - break - list.add(item) - } - return list -} - -/** - * Returns a list containing first elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun ShortArray.takeWhile(predicate: (Short) -> Boolean): List { - val list = ArrayList() - for (item in this) { - if (!predicate(item)) - break - list.add(item) - } - return list -} - -/** - * Returns a list containing first elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun IntArray.takeWhile(predicate: (Int) -> Boolean): List { - val list = ArrayList() - for (item in this) { - if (!predicate(item)) - break - list.add(item) - } - return list -} - -/** - * Returns a list containing first elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun LongArray.takeWhile(predicate: (Long) -> Boolean): List { - val list = ArrayList() - for (item in this) { - if (!predicate(item)) - break - list.add(item) - } - return list -} - -/** - * Returns a list containing first elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun FloatArray.takeWhile(predicate: (Float) -> Boolean): List { - val list = ArrayList() - for (item in this) { - if (!predicate(item)) - break - list.add(item) - } - return list -} - -/** - * Returns a list containing first elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun DoubleArray.takeWhile(predicate: (Double) -> Boolean): List { - val list = ArrayList() - for (item in this) { - if (!predicate(item)) - break - list.add(item) - } - return list -} - -/** - * Returns a list containing first elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun BooleanArray.takeWhile(predicate: (Boolean) -> Boolean): List { - val list = ArrayList() - for (item in this) { - if (!predicate(item)) - break - list.add(item) - } - return list -} - -/** - * Returns a list containing first elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun CharArray.takeWhile(predicate: (Char) -> Boolean): List { - val list = ArrayList() - for (item in this) { - if (!predicate(item)) - break - list.add(item) - } - return list -} - -/** - * Reverses elements in the array in-place. - */ -public fun Array.reverse(): Unit { - val midPoint = (size / 2) - 1 - if (midPoint < 0) return - var reverseIndex = lastIndex - for (index in 0..midPoint) { - val tmp = this[index] - this[index] = this[reverseIndex] - this[reverseIndex] = tmp - reverseIndex-- - } -} - -/** - * Reverses elements in the array in-place. - */ -public fun ByteArray.reverse(): Unit { - val midPoint = (size / 2) - 1 - if (midPoint < 0) return - var reverseIndex = lastIndex - for (index in 0..midPoint) { - val tmp = this[index] - this[index] = this[reverseIndex] - this[reverseIndex] = tmp - reverseIndex-- - } -} - -/** - * Reverses elements in the array in-place. - */ -public fun ShortArray.reverse(): Unit { - val midPoint = (size / 2) - 1 - if (midPoint < 0) return - var reverseIndex = lastIndex - for (index in 0..midPoint) { - val tmp = this[index] - this[index] = this[reverseIndex] - this[reverseIndex] = tmp - reverseIndex-- - } -} - -/** - * Reverses elements in the array in-place. - */ -public fun IntArray.reverse(): Unit { - val midPoint = (size / 2) - 1 - if (midPoint < 0) return - var reverseIndex = lastIndex - for (index in 0..midPoint) { - val tmp = this[index] - this[index] = this[reverseIndex] - this[reverseIndex] = tmp - reverseIndex-- - } -} - -/** - * Reverses elements in the array in-place. - */ -public fun LongArray.reverse(): Unit { - val midPoint = (size / 2) - 1 - if (midPoint < 0) return - var reverseIndex = lastIndex - for (index in 0..midPoint) { - val tmp = this[index] - this[index] = this[reverseIndex] - this[reverseIndex] = tmp - reverseIndex-- - } -} - -/** - * Reverses elements in the array in-place. - */ -public fun FloatArray.reverse(): Unit { - val midPoint = (size / 2) - 1 - if (midPoint < 0) return - var reverseIndex = lastIndex - for (index in 0..midPoint) { - val tmp = this[index] - this[index] = this[reverseIndex] - this[reverseIndex] = tmp - reverseIndex-- - } -} - -/** - * Reverses elements in the array in-place. - */ -public fun DoubleArray.reverse(): Unit { - val midPoint = (size / 2) - 1 - if (midPoint < 0) return - var reverseIndex = lastIndex - for (index in 0..midPoint) { - val tmp = this[index] - this[index] = this[reverseIndex] - this[reverseIndex] = tmp - reverseIndex-- - } -} - -/** - * Reverses elements in the array in-place. - */ -public fun BooleanArray.reverse(): Unit { - val midPoint = (size / 2) - 1 - if (midPoint < 0) return - var reverseIndex = lastIndex - for (index in 0..midPoint) { - val tmp = this[index] - this[index] = this[reverseIndex] - this[reverseIndex] = tmp - reverseIndex-- - } -} - -/** - * Reverses elements in the array in-place. - */ -public fun CharArray.reverse(): Unit { - val midPoint = (size / 2) - 1 - if (midPoint < 0) return - var reverseIndex = lastIndex - for (index in 0..midPoint) { - val tmp = this[index] - this[index] = this[reverseIndex] - this[reverseIndex] = tmp - reverseIndex-- - } -} - -/** - * Returns a list with elements in reversed order. - */ -public fun Array.reversed(): List { - if (isEmpty()) return emptyList() - val list = toMutableList() - list.reverse() - return list -} - -/** - * Returns a list with elements in reversed order. - */ -public fun ByteArray.reversed(): List { - if (isEmpty()) return emptyList() - val list = toMutableList() - list.reverse() - return list -} - -/** - * Returns a list with elements in reversed order. - */ -public fun ShortArray.reversed(): List { - if (isEmpty()) return emptyList() - val list = toMutableList() - list.reverse() - return list -} - -/** - * Returns a list with elements in reversed order. - */ -public fun IntArray.reversed(): List { - if (isEmpty()) return emptyList() - val list = toMutableList() - list.reverse() - return list -} - -/** - * Returns a list with elements in reversed order. - */ -public fun LongArray.reversed(): List { - if (isEmpty()) return emptyList() - val list = toMutableList() - list.reverse() - return list -} - -/** - * Returns a list with elements in reversed order. - */ -public fun FloatArray.reversed(): List { - if (isEmpty()) return emptyList() - val list = toMutableList() - list.reverse() - return list -} - -/** - * Returns a list with elements in reversed order. - */ -public fun DoubleArray.reversed(): List { - if (isEmpty()) return emptyList() - val list = toMutableList() - list.reverse() - return list -} - -/** - * Returns a list with elements in reversed order. - */ -public fun BooleanArray.reversed(): List { - if (isEmpty()) return emptyList() - val list = toMutableList() - list.reverse() - return list -} - -/** - * Returns a list with elements in reversed order. - */ -public fun CharArray.reversed(): List { - if (isEmpty()) return emptyList() - val list = toMutableList() - list.reverse() - return list -} - -/** - * Returns an array with elements of this array in reversed order. - */ -public fun Array.reversedArray(): Array { - if (isEmpty()) return this - val result = arrayOfNulls(this, size) - val lastIndex = lastIndex - for (i in 0..lastIndex) - result[lastIndex - i] = this[i] - return result -} - -/** - * Returns an array with elements of this array in reversed order. - */ -public fun ByteArray.reversedArray(): ByteArray { - if (isEmpty()) return this - val result = ByteArray(size) - val lastIndex = lastIndex - for (i in 0..lastIndex) - result[lastIndex - i] = this[i] - return result -} - -/** - * Returns an array with elements of this array in reversed order. - */ -public fun ShortArray.reversedArray(): ShortArray { - if (isEmpty()) return this - val result = ShortArray(size) - val lastIndex = lastIndex - for (i in 0..lastIndex) - result[lastIndex - i] = this[i] - return result -} - -/** - * Returns an array with elements of this array in reversed order. - */ -public fun IntArray.reversedArray(): IntArray { - if (isEmpty()) return this - val result = IntArray(size) - val lastIndex = lastIndex - for (i in 0..lastIndex) - result[lastIndex - i] = this[i] - return result -} - -/** - * Returns an array with elements of this array in reversed order. - */ -public fun LongArray.reversedArray(): LongArray { - if (isEmpty()) return this - val result = LongArray(size) - val lastIndex = lastIndex - for (i in 0..lastIndex) - result[lastIndex - i] = this[i] - return result -} - -/** - * Returns an array with elements of this array in reversed order. - */ -public fun FloatArray.reversedArray(): FloatArray { - if (isEmpty()) return this - val result = FloatArray(size) - val lastIndex = lastIndex - for (i in 0..lastIndex) - result[lastIndex - i] = this[i] - return result -} - -/** - * Returns an array with elements of this array in reversed order. - */ -public fun DoubleArray.reversedArray(): DoubleArray { - if (isEmpty()) return this - val result = DoubleArray(size) - val lastIndex = lastIndex - for (i in 0..lastIndex) - result[lastIndex - i] = this[i] - return result -} - -/** - * Returns an array with elements of this array in reversed order. - */ -public fun BooleanArray.reversedArray(): BooleanArray { - if (isEmpty()) return this - val result = BooleanArray(size) - val lastIndex = lastIndex - for (i in 0..lastIndex) - result[lastIndex - i] = this[i] - return result -} - -/** - * Returns an array with elements of this array in reversed order. - */ -public fun CharArray.reversedArray(): CharArray { - if (isEmpty()) return this - val result = CharArray(size) - val lastIndex = lastIndex - for (i in 0..lastIndex) - result[lastIndex - i] = this[i] - return result -} - -/** - * Sorts elements in the array in-place according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > Array.sortBy(crossinline selector: (T) -> R?): Unit { - if (size > 1) sortWith(compareBy(selector)) -} - -/** - * Sorts elements in the array in-place descending according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > Array.sortByDescending(crossinline selector: (T) -> R?): Unit { - if (size > 1) sortWith(compareByDescending(selector)) -} - -/** - * Sorts elements in the array in-place descending according to their natural sort order. - */ -public fun > Array.sortDescending(): Unit { - sortWith(reverseOrder()) -} - -/** - * Sorts elements in the array in-place descending according to their natural sort order. - */ -public fun ByteArray.sortDescending(): Unit { - if (size > 1) { - sort() - reverse() - } -} - -/** - * Sorts elements in the array in-place descending according to their natural sort order. - */ -public fun ShortArray.sortDescending(): Unit { - if (size > 1) { - sort() - reverse() - } -} - -/** - * Sorts elements in the array in-place descending according to their natural sort order. - */ -public fun IntArray.sortDescending(): Unit { - if (size > 1) { - sort() - reverse() - } -} - -/** - * Sorts elements in the array in-place descending according to their natural sort order. - */ -public fun LongArray.sortDescending(): Unit { - if (size > 1) { - sort() - reverse() - } -} - -/** - * Sorts elements in the array in-place descending according to their natural sort order. - */ -public fun FloatArray.sortDescending(): Unit { - if (size > 1) { - sort() - reverse() - } -} - -/** - * Sorts elements in the array in-place descending according to their natural sort order. - */ -public fun DoubleArray.sortDescending(): Unit { - if (size > 1) { - sort() - reverse() - } -} - -/** - * Sorts elements in the array in-place descending according to their natural sort order. - */ -public fun CharArray.sortDescending(): Unit { - if (size > 1) { - sort() - reverse() - } -} - -/** - * Returns a list of all elements sorted according to their natural sort order. - */ -public fun > Array.sorted(): List { - return sortedArray().asList() -} - -/** - * Returns a list of all elements sorted according to their natural sort order. - */ -public fun ByteArray.sorted(): List { - return toTypedArray().apply { sort() }.asList() -} - -/** - * Returns a list of all elements sorted according to their natural sort order. - */ -public fun ShortArray.sorted(): List { - return toTypedArray().apply { sort() }.asList() -} - -/** - * Returns a list of all elements sorted according to their natural sort order. - */ -public fun IntArray.sorted(): List { - return toTypedArray().apply { sort() }.asList() -} - -/** - * Returns a list of all elements sorted according to their natural sort order. - */ -public fun LongArray.sorted(): List { - return toTypedArray().apply { sort() }.asList() -} - -/** - * Returns a list of all elements sorted according to their natural sort order. - */ -public fun FloatArray.sorted(): List { - return toTypedArray().apply { sort() }.asList() -} - -/** - * Returns a list of all elements sorted according to their natural sort order. - */ -public fun DoubleArray.sorted(): List { - return toTypedArray().apply { sort() }.asList() -} - -/** - * Returns a list of all elements sorted according to their natural sort order. - */ -public fun CharArray.sorted(): List { - return toTypedArray().apply { sort() }.asList() -} - -/** - * Returns an array with all elements of this array sorted according to their natural sort order. - */ -public fun > Array.sortedArray(): Array { - if (isEmpty()) return this - return this.copyOf().apply { sort() } -} - -/** - * Returns an array with all elements of this array sorted according to their natural sort order. - */ -public fun ByteArray.sortedArray(): ByteArray { - if (isEmpty()) return this - return this.copyOf().apply { sort() } -} - -/** - * Returns an array with all elements of this array sorted according to their natural sort order. - */ -public fun ShortArray.sortedArray(): ShortArray { - if (isEmpty()) return this - return this.copyOf().apply { sort() } -} - -/** - * Returns an array with all elements of this array sorted according to their natural sort order. - */ -public fun IntArray.sortedArray(): IntArray { - if (isEmpty()) return this - return this.copyOf().apply { sort() } -} - -/** - * Returns an array with all elements of this array sorted according to their natural sort order. - */ -public fun LongArray.sortedArray(): LongArray { - if (isEmpty()) return this - return this.copyOf().apply { sort() } -} - -/** - * Returns an array with all elements of this array sorted according to their natural sort order. - */ -public fun FloatArray.sortedArray(): FloatArray { - if (isEmpty()) return this - return this.copyOf().apply { sort() } -} - -/** - * Returns an array with all elements of this array sorted according to their natural sort order. - */ -public fun DoubleArray.sortedArray(): DoubleArray { - if (isEmpty()) return this - return this.copyOf().apply { sort() } -} - -/** - * Returns an array with all elements of this array sorted according to their natural sort order. - */ -public fun CharArray.sortedArray(): CharArray { - if (isEmpty()) return this - return this.copyOf().apply { sort() } -} - -/** - * Returns an array with all elements of this array sorted descending according to their natural sort order. - */ -public fun > Array.sortedArrayDescending(): Array { - if (isEmpty()) return this - return this.copyOf().apply { sortWith(reverseOrder()) } -} - -/** - * Returns an array with all elements of this array sorted descending according to their natural sort order. - */ -public fun ByteArray.sortedArrayDescending(): ByteArray { - if (isEmpty()) return this - return this.copyOf().apply { sortDescending() } -} - -/** - * Returns an array with all elements of this array sorted descending according to their natural sort order. - */ -public fun ShortArray.sortedArrayDescending(): ShortArray { - if (isEmpty()) return this - return this.copyOf().apply { sortDescending() } -} - -/** - * Returns an array with all elements of this array sorted descending according to their natural sort order. - */ -public fun IntArray.sortedArrayDescending(): IntArray { - if (isEmpty()) return this - return this.copyOf().apply { sortDescending() } -} - -/** - * Returns an array with all elements of this array sorted descending according to their natural sort order. - */ -public fun LongArray.sortedArrayDescending(): LongArray { - if (isEmpty()) return this - return this.copyOf().apply { sortDescending() } -} - -/** - * Returns an array with all elements of this array sorted descending according to their natural sort order. - */ -public fun FloatArray.sortedArrayDescending(): FloatArray { - if (isEmpty()) return this - return this.copyOf().apply { sortDescending() } -} - -/** - * Returns an array with all elements of this array sorted descending according to their natural sort order. - */ -public fun DoubleArray.sortedArrayDescending(): DoubleArray { - if (isEmpty()) return this - return this.copyOf().apply { sortDescending() } -} - -/** - * Returns an array with all elements of this array sorted descending according to their natural sort order. - */ -public fun CharArray.sortedArrayDescending(): CharArray { - if (isEmpty()) return this - return this.copyOf().apply { sortDescending() } -} - -/** - * Returns an array with all elements of this array sorted according the specified [comparator]. - */ -public fun Array.sortedArrayWith(comparator: Comparator): Array { - if (isEmpty()) return this - return this.copyOf().apply { sortWith(comparator) } -} - -/** - * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > Array.sortedBy(crossinline selector: (T) -> R?): List { - return sortedWith(compareBy(selector)) -} - -/** - * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > ByteArray.sortedBy(crossinline selector: (Byte) -> R?): List { - return sortedWith(compareBy(selector)) -} - -/** - * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > ShortArray.sortedBy(crossinline selector: (Short) -> R?): List { - return sortedWith(compareBy(selector)) -} - -/** - * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > IntArray.sortedBy(crossinline selector: (Int) -> R?): List { - return sortedWith(compareBy(selector)) -} - -/** - * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > LongArray.sortedBy(crossinline selector: (Long) -> R?): List { - return sortedWith(compareBy(selector)) -} - -/** - * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > FloatArray.sortedBy(crossinline selector: (Float) -> R?): List { - return sortedWith(compareBy(selector)) -} - -/** - * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > DoubleArray.sortedBy(crossinline selector: (Double) -> R?): List { - return sortedWith(compareBy(selector)) -} - -/** - * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > BooleanArray.sortedBy(crossinline selector: (Boolean) -> R?): List { - return sortedWith(compareBy(selector)) -} - -/** - * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > CharArray.sortedBy(crossinline selector: (Char) -> R?): List { - return sortedWith(compareBy(selector)) -} - -/** - * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > Array.sortedByDescending(crossinline selector: (T) -> R?): List { - return sortedWith(compareByDescending(selector)) -} - -/** - * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > ByteArray.sortedByDescending(crossinline selector: (Byte) -> R?): List { - return sortedWith(compareByDescending(selector)) -} - -/** - * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > ShortArray.sortedByDescending(crossinline selector: (Short) -> R?): List { - return sortedWith(compareByDescending(selector)) -} - -/** - * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > IntArray.sortedByDescending(crossinline selector: (Int) -> R?): List { - return sortedWith(compareByDescending(selector)) -} - -/** - * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > LongArray.sortedByDescending(crossinline selector: (Long) -> R?): List { - return sortedWith(compareByDescending(selector)) -} - -/** - * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > FloatArray.sortedByDescending(crossinline selector: (Float) -> R?): List { - return sortedWith(compareByDescending(selector)) -} - -/** - * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > DoubleArray.sortedByDescending(crossinline selector: (Double) -> R?): List { - return sortedWith(compareByDescending(selector)) -} - -/** - * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > BooleanArray.sortedByDescending(crossinline selector: (Boolean) -> R?): List { - return sortedWith(compareByDescending(selector)) -} - -/** - * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > CharArray.sortedByDescending(crossinline selector: (Char) -> R?): List { - return sortedWith(compareByDescending(selector)) -} - -/** - * Returns a list of all elements sorted descending according to their natural sort order. - */ -public fun > Array.sortedDescending(): List { - return sortedWith(reverseOrder()) -} - -/** - * Returns a list of all elements sorted descending according to their natural sort order. - */ -public fun ByteArray.sortedDescending(): List { - return copyOf().apply { sort() }.reversed() -} - -/** - * Returns a list of all elements sorted descending according to their natural sort order. - */ -public fun ShortArray.sortedDescending(): List { - return copyOf().apply { sort() }.reversed() -} - -/** - * Returns a list of all elements sorted descending according to their natural sort order. - */ -public fun IntArray.sortedDescending(): List { - return copyOf().apply { sort() }.reversed() -} - -/** - * Returns a list of all elements sorted descending according to their natural sort order. - */ -public fun LongArray.sortedDescending(): List { - return copyOf().apply { sort() }.reversed() -} - -/** - * Returns a list of all elements sorted descending according to their natural sort order. - */ -public fun FloatArray.sortedDescending(): List { - return copyOf().apply { sort() }.reversed() -} - -/** - * Returns a list of all elements sorted descending according to their natural sort order. - */ -public fun DoubleArray.sortedDescending(): List { - return copyOf().apply { sort() }.reversed() -} - -/** - * Returns a list of all elements sorted descending according to their natural sort order. - */ -public fun CharArray.sortedDescending(): List { - return copyOf().apply { sort() }.reversed() -} - -/** - * Returns a list of all elements sorted according to the specified [comparator]. - */ -public fun Array.sortedWith(comparator: Comparator): List { - return sortedArrayWith(comparator).asList() -} - -/** - * Returns a list of all elements sorted according to the specified [comparator]. - */ -public fun ByteArray.sortedWith(comparator: Comparator): List { - return toTypedArray().apply { sortWith(comparator) }.asList() -} - -/** - * Returns a list of all elements sorted according to the specified [comparator]. - */ -public fun ShortArray.sortedWith(comparator: Comparator): List { - return toTypedArray().apply { sortWith(comparator) }.asList() -} - -/** - * Returns a list of all elements sorted according to the specified [comparator]. - */ -public fun IntArray.sortedWith(comparator: Comparator): List { - return toTypedArray().apply { sortWith(comparator) }.asList() -} - -/** - * Returns a list of all elements sorted according to the specified [comparator]. - */ -public fun LongArray.sortedWith(comparator: Comparator): List { - return toTypedArray().apply { sortWith(comparator) }.asList() -} - -/** - * Returns a list of all elements sorted according to the specified [comparator]. - */ -public fun FloatArray.sortedWith(comparator: Comparator): List { - return toTypedArray().apply { sortWith(comparator) }.asList() -} - -/** - * Returns a list of all elements sorted according to the specified [comparator]. - */ -public fun DoubleArray.sortedWith(comparator: Comparator): List { - return toTypedArray().apply { sortWith(comparator) }.asList() -} - -/** - * Returns a list of all elements sorted according to the specified [comparator]. - */ -public fun BooleanArray.sortedWith(comparator: Comparator): List { - return toTypedArray().apply { sortWith(comparator) }.asList() -} - -/** - * Returns a list of all elements sorted according to the specified [comparator]. - */ -public fun CharArray.sortedWith(comparator: Comparator): List { - return toTypedArray().apply { sortWith(comparator) }.asList() -} - -/** - * Returns a [List] that wraps the original array. - */ -public fun Array.asList(): List { - return ArrayList(this.unsafeCast>()) -} - -/** - * Returns a [List] that wraps the original array. - */ -@kotlin.internal.InlineOnly -public inline fun ByteArray.asList(): List { - return this.unsafeCast>().asList() -} - -/** - * Returns a [List] that wraps the original array. - */ -@kotlin.internal.InlineOnly -public inline fun ShortArray.asList(): List { - return this.unsafeCast>().asList() -} - -/** - * Returns a [List] that wraps the original array. - */ -@kotlin.internal.InlineOnly -public inline fun IntArray.asList(): List { - return this.unsafeCast>().asList() -} - -/** - * Returns a [List] that wraps the original array. - */ -@kotlin.internal.InlineOnly -public inline fun LongArray.asList(): List { - return this.unsafeCast>().asList() -} - -/** - * Returns a [List] that wraps the original array. - */ -@kotlin.internal.InlineOnly -public inline fun FloatArray.asList(): List { - return this.unsafeCast>().asList() -} - -/** - * Returns a [List] that wraps the original array. - */ -@kotlin.internal.InlineOnly -public inline fun DoubleArray.asList(): List { - return this.unsafeCast>().asList() -} - -/** - * Returns a [List] that wraps the original array. - */ -@kotlin.internal.InlineOnly -public inline fun BooleanArray.asList(): List { - return this.unsafeCast>().asList() -} - -/** - * 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) - } -} - -/** - * Returns `true` if the two specified arrays are *deeply* equal to one another, - * i.e. contain the same number of the same elements in the same order. - * - * If two corresponding elements are nested arrays, they are also compared deeply. - * If any of arrays contains itself on any nesting level the behavior is undefined. - */ -@SinceKotlin("1.1") -@library("arrayDeepEquals") -@Suppress("UNUSED_PARAMETER") -public infix fun Array.contentDeepEquals(other: Array): Boolean { - definedExternally -} - -/** - * Returns a hash code based on the contents of this array as if it is [List]. - * Nested arrays are treated as lists too. - * - * If any of arrays contains itself on any nesting level the behavior is undefined. - */ -@SinceKotlin("1.1") -@library("arrayDeepHashCode") -public fun Array.contentDeepHashCode(): Int { - definedExternally -} - -/** - * Returns a string representation of the contents of this array as if it is a [List]. - * Nested arrays are treated as lists too. - * - * If any of arrays contains itself on any nesting level that reference - * is rendered as `"[...]"` to prevent recursion. - * - * @sample samples.collections.Arrays.ContentOperations.contentDeepToString - */ -@SinceKotlin("1.1") -@library("arrayDeepToString") -public fun Array.contentDeepToString(): String { - definedExternally -} - -/** - * Returns `true` if the two specified arrays are *structurally* equal to one another, - * i.e. contain the same number of the same elements in the same order. - */ -@SinceKotlin("1.1") -@library("arrayEquals") -@Suppress("UNUSED_PARAMETER") -public infix fun Array.contentEquals(other: Array): Boolean { - definedExternally -} - -/** - * Returns `true` if the two specified arrays are *structurally* equal to one another, - * i.e. contain the same number of the same elements in the same order. - */ -@SinceKotlin("1.1") -@library("arrayEquals") -@Suppress("UNUSED_PARAMETER") -public infix fun ByteArray.contentEquals(other: ByteArray): Boolean { - definedExternally -} - -/** - * Returns `true` if the two specified arrays are *structurally* equal to one another, - * i.e. contain the same number of the same elements in the same order. - */ -@SinceKotlin("1.1") -@library("arrayEquals") -@Suppress("UNUSED_PARAMETER") -public infix fun ShortArray.contentEquals(other: ShortArray): Boolean { - definedExternally -} - -/** - * Returns `true` if the two specified arrays are *structurally* equal to one another, - * i.e. contain the same number of the same elements in the same order. - */ -@SinceKotlin("1.1") -@library("arrayEquals") -@Suppress("UNUSED_PARAMETER") -public infix fun IntArray.contentEquals(other: IntArray): Boolean { - definedExternally -} - -/** - * Returns `true` if the two specified arrays are *structurally* equal to one another, - * i.e. contain the same number of the same elements in the same order. - */ -@SinceKotlin("1.1") -@library("arrayEquals") -@Suppress("UNUSED_PARAMETER") -public infix fun LongArray.contentEquals(other: LongArray): Boolean { - definedExternally -} - -/** - * Returns `true` if the two specified arrays are *structurally* equal to one another, - * i.e. contain the same number of the same elements in the same order. - */ -@SinceKotlin("1.1") -@library("arrayEquals") -@Suppress("UNUSED_PARAMETER") -public infix fun FloatArray.contentEquals(other: FloatArray): Boolean { - definedExternally -} - -/** - * Returns `true` if the two specified arrays are *structurally* equal to one another, - * i.e. contain the same number of the same elements in the same order. - */ -@SinceKotlin("1.1") -@library("arrayEquals") -@Suppress("UNUSED_PARAMETER") -public infix fun DoubleArray.contentEquals(other: DoubleArray): Boolean { - definedExternally -} - -/** - * Returns `true` if the two specified arrays are *structurally* equal to one another, - * i.e. contain the same number of the same elements in the same order. - */ -@SinceKotlin("1.1") -@library("arrayEquals") -@Suppress("UNUSED_PARAMETER") -public infix fun BooleanArray.contentEquals(other: BooleanArray): Boolean { - definedExternally -} - -/** - * Returns `true` if the two specified arrays are *structurally* equal to one another, - * i.e. contain the same number of the same elements in the same order. - */ -@SinceKotlin("1.1") -@library("arrayEquals") -@Suppress("UNUSED_PARAMETER") -public infix fun CharArray.contentEquals(other: CharArray): Boolean { - definedExternally -} - -/** - * Returns a hash code based on the contents of this array as if it is [List]. - */ -@SinceKotlin("1.1") -@library("arrayHashCode") -public fun Array.contentHashCode(): Int { - definedExternally -} - -/** - * Returns a hash code based on the contents of this array as if it is [List]. - */ -@SinceKotlin("1.1") -@library("arrayHashCode") -public fun ByteArray.contentHashCode(): Int { - definedExternally -} - -/** - * Returns a hash code based on the contents of this array as if it is [List]. - */ -@SinceKotlin("1.1") -@library("arrayHashCode") -public fun ShortArray.contentHashCode(): Int { - definedExternally -} - -/** - * Returns a hash code based on the contents of this array as if it is [List]. - */ -@SinceKotlin("1.1") -@library("arrayHashCode") -public fun IntArray.contentHashCode(): Int { - definedExternally -} - -/** - * Returns a hash code based on the contents of this array as if it is [List]. - */ -@SinceKotlin("1.1") -@library("arrayHashCode") -public fun LongArray.contentHashCode(): Int { - definedExternally -} - -/** - * Returns a hash code based on the contents of this array as if it is [List]. - */ -@SinceKotlin("1.1") -@library("arrayHashCode") -public fun FloatArray.contentHashCode(): Int { - definedExternally -} - -/** - * Returns a hash code based on the contents of this array as if it is [List]. - */ -@SinceKotlin("1.1") -@library("arrayHashCode") -public fun DoubleArray.contentHashCode(): Int { - definedExternally -} - -/** - * Returns a hash code based on the contents of this array as if it is [List]. - */ -@SinceKotlin("1.1") -@library("arrayHashCode") -public fun BooleanArray.contentHashCode(): Int { - definedExternally -} - -/** - * Returns a hash code based on the contents of this array as if it is [List]. - */ -@SinceKotlin("1.1") -@library("arrayHashCode") -public fun CharArray.contentHashCode(): Int { - definedExternally -} - -/** - * Returns a string representation of the contents of the specified array as if it is [List]. - * - * @sample samples.collections.Arrays.ContentOperations.contentToString - */ -@SinceKotlin("1.1") -@library("arrayToString") -public fun Array.contentToString(): String { - definedExternally -} - -/** - * Returns a string representation of the contents of the specified array as if it is [List]. - * - * @sample samples.collections.Arrays.ContentOperations.contentToString - */ -@SinceKotlin("1.1") -@library("arrayToString") -public fun ByteArray.contentToString(): String { - definedExternally -} - -/** - * Returns a string representation of the contents of the specified array as if it is [List]. - * - * @sample samples.collections.Arrays.ContentOperations.contentToString - */ -@SinceKotlin("1.1") -@library("arrayToString") -public fun ShortArray.contentToString(): String { - definedExternally -} - -/** - * Returns a string representation of the contents of the specified array as if it is [List]. - * - * @sample samples.collections.Arrays.ContentOperations.contentToString - */ -@SinceKotlin("1.1") -@library("arrayToString") -public fun IntArray.contentToString(): String { - definedExternally -} - -/** - * Returns a string representation of the contents of the specified array as if it is [List]. - * - * @sample samples.collections.Arrays.ContentOperations.contentToString - */ -@SinceKotlin("1.1") -@library("arrayToString") -public fun LongArray.contentToString(): String { - definedExternally -} - -/** - * Returns a string representation of the contents of the specified array as if it is [List]. - * - * @sample samples.collections.Arrays.ContentOperations.contentToString - */ -@SinceKotlin("1.1") -@library("arrayToString") -public fun FloatArray.contentToString(): String { - definedExternally -} - -/** - * Returns a string representation of the contents of the specified array as if it is [List]. - * - * @sample samples.collections.Arrays.ContentOperations.contentToString - */ -@SinceKotlin("1.1") -@library("arrayToString") -public fun DoubleArray.contentToString(): String { - definedExternally -} - -/** - * Returns a string representation of the contents of the specified array as if it is [List]. - * - * @sample samples.collections.Arrays.ContentOperations.contentToString - */ -@SinceKotlin("1.1") -@library("arrayToString") -public fun BooleanArray.contentToString(): String { - definedExternally -} - -/** - * Returns a string representation of the contents of the specified array as if it is [List]. - * - * @sample samples.collections.Arrays.ContentOperations.contentToString - */ -@SinceKotlin("1.1") -@library("arrayToString") -public fun CharArray.contentToString(): String { - definedExternally -} - -/** - * Returns new array which is a copy of the original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun Array.copyOf(): Array { - return this.asDynamic().slice() -} - -/** - * Returns new array which is a copy of the original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun ByteArray.copyOf(): ByteArray { - return this.asDynamic().slice() -} - -/** - * Returns new array which is a copy of the original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun ShortArray.copyOf(): ShortArray { - return this.asDynamic().slice() -} - -/** - * Returns new array which is a copy of the original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun IntArray.copyOf(): IntArray { - return this.asDynamic().slice() -} - -/** - * Returns new array which is a copy of the original array. - */ -public fun LongArray.copyOf(): LongArray { - return withType("LongArray", this.asDynamic().slice()) -} - -/** - * Returns new array which is a copy of the original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun FloatArray.copyOf(): FloatArray { - return this.asDynamic().slice() -} - -/** - * Returns new array which is a copy of the original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun DoubleArray.copyOf(): DoubleArray { - return this.asDynamic().slice() -} - -/** - * Returns new array which is a copy of the original array. - */ -public fun BooleanArray.copyOf(): BooleanArray { - return withType("BooleanArray", this.asDynamic().slice()) -} - -/** - * Returns new array which is a copy of the original array. - */ -public fun CharArray.copyOf(): CharArray { - return withType("CharArray", this.asDynamic().slice()) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -public fun ByteArray.copyOf(newSize: Int): ByteArray { - return fillFrom(this, ByteArray(newSize)) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -public fun ShortArray.copyOf(newSize: Int): ShortArray { - return fillFrom(this, ShortArray(newSize)) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -public fun IntArray.copyOf(newSize: Int): IntArray { - return fillFrom(this, IntArray(newSize)) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -public fun LongArray.copyOf(newSize: Int): LongArray { - return withType("LongArray", arrayCopyResize(this, newSize, 0L)) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -public fun FloatArray.copyOf(newSize: Int): FloatArray { - return fillFrom(this, FloatArray(newSize)) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -public fun DoubleArray.copyOf(newSize: Int): DoubleArray { - return fillFrom(this, DoubleArray(newSize)) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -public fun BooleanArray.copyOf(newSize: Int): BooleanArray { - return withType("BooleanArray", arrayCopyResize(this, newSize, false)) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -public fun CharArray.copyOf(newSize: Int): CharArray { - return withType("CharArray", fillFrom(this, CharArray(newSize))) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -public fun Array.copyOf(newSize: Int): Array { - return arrayCopyResize(this, newSize, null) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun Array.copyOfRange(fromIndex: Int, toIndex: Int): Array { - return this.asDynamic().slice(fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun ByteArray.copyOfRange(fromIndex: Int, toIndex: Int): ByteArray { - return this.asDynamic().slice(fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun ShortArray.copyOfRange(fromIndex: Int, toIndex: Int): ShortArray { - return this.asDynamic().slice(fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun IntArray.copyOfRange(fromIndex: Int, toIndex: Int): IntArray { - return this.asDynamic().slice(fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -public fun LongArray.copyOfRange(fromIndex: Int, toIndex: Int): LongArray { - return withType("LongArray", this.asDynamic().slice(fromIndex, toIndex)) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun FloatArray.copyOfRange(fromIndex: Int, toIndex: Int): FloatArray { - return this.asDynamic().slice(fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun DoubleArray.copyOfRange(fromIndex: Int, toIndex: Int): DoubleArray { - return this.asDynamic().slice(fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -public fun BooleanArray.copyOfRange(fromIndex: Int, toIndex: Int): BooleanArray { - return withType("BooleanArray", this.asDynamic().slice(fromIndex, toIndex)) -} - -/** - * Returns new array which is a copy of range of original array. - */ -public fun CharArray.copyOfRange(fromIndex: Int, toIndex: Int): CharArray { - return withType("CharArray", this.asDynamic().slice(fromIndex, toIndex)) -} - -/** - * Returns the range of valid indices for the array. - */ -public val Array.indices: IntRange - get() = IntRange(0, lastIndex) - -/** - * Returns the range of valid indices for the array. - */ -public val ByteArray.indices: IntRange - get() = IntRange(0, lastIndex) - -/** - * Returns the range of valid indices for the array. - */ -public val ShortArray.indices: IntRange - get() = IntRange(0, lastIndex) - -/** - * Returns the range of valid indices for the array. - */ -public val IntArray.indices: IntRange - get() = IntRange(0, lastIndex) - -/** - * Returns the range of valid indices for the array. - */ -public val LongArray.indices: IntRange - get() = IntRange(0, lastIndex) - -/** - * Returns the range of valid indices for the array. - */ -public val FloatArray.indices: IntRange - get() = IntRange(0, lastIndex) - -/** - * Returns the range of valid indices for the array. - */ -public val DoubleArray.indices: IntRange - get() = IntRange(0, lastIndex) - -/** - * Returns the range of valid indices for the array. - */ -public val BooleanArray.indices: IntRange - get() = IntRange(0, lastIndex) - -/** - * Returns the range of valid indices for the array. - */ -public val CharArray.indices: IntRange - get() = IntRange(0, lastIndex) - -/** - * Returns `true` if the array is empty. - */ -@kotlin.internal.InlineOnly -public inline fun Array.isEmpty(): Boolean { - return size == 0 -} - -/** - * Returns `true` if the array is empty. - */ -@kotlin.internal.InlineOnly -public inline fun ByteArray.isEmpty(): Boolean { - return size == 0 -} - -/** - * Returns `true` if the array is empty. - */ -@kotlin.internal.InlineOnly -public inline fun ShortArray.isEmpty(): Boolean { - return size == 0 -} - -/** - * Returns `true` if the array is empty. - */ -@kotlin.internal.InlineOnly -public inline fun IntArray.isEmpty(): Boolean { - return size == 0 -} - -/** - * Returns `true` if the array is empty. - */ -@kotlin.internal.InlineOnly -public inline fun LongArray.isEmpty(): Boolean { - return size == 0 -} - -/** - * Returns `true` if the array is empty. - */ -@kotlin.internal.InlineOnly -public inline fun FloatArray.isEmpty(): Boolean { - return size == 0 -} - -/** - * Returns `true` if the array is empty. - */ -@kotlin.internal.InlineOnly -public inline fun DoubleArray.isEmpty(): Boolean { - return size == 0 -} - -/** - * Returns `true` if the array is empty. - */ -@kotlin.internal.InlineOnly -public inline fun BooleanArray.isEmpty(): Boolean { - return size == 0 -} - -/** - * Returns `true` if the array is empty. - */ -@kotlin.internal.InlineOnly -public inline fun CharArray.isEmpty(): Boolean { - return size == 0 -} - -/** - * Returns `true` if the array is not empty. - */ -@kotlin.internal.InlineOnly -public inline fun Array.isNotEmpty(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if the array is not empty. - */ -@kotlin.internal.InlineOnly -public inline fun ByteArray.isNotEmpty(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if the array is not empty. - */ -@kotlin.internal.InlineOnly -public inline fun ShortArray.isNotEmpty(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if the array is not empty. - */ -@kotlin.internal.InlineOnly -public inline fun IntArray.isNotEmpty(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if the array is not empty. - */ -@kotlin.internal.InlineOnly -public inline fun LongArray.isNotEmpty(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if the array is not empty. - */ -@kotlin.internal.InlineOnly -public inline fun FloatArray.isNotEmpty(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if the array is not empty. - */ -@kotlin.internal.InlineOnly -public inline fun DoubleArray.isNotEmpty(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if the array is not empty. - */ -@kotlin.internal.InlineOnly -public inline fun BooleanArray.isNotEmpty(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if the array is not empty. - */ -@kotlin.internal.InlineOnly -public inline fun CharArray.isNotEmpty(): Boolean { - return !isEmpty() -} - -/** - * Returns the last valid index for the array. - */ -public val Array.lastIndex: Int - get() = size - 1 - -/** - * Returns the last valid index for the array. - */ -public val ByteArray.lastIndex: Int - get() = size - 1 - -/** - * Returns the last valid index for the array. - */ -public val ShortArray.lastIndex: Int - get() = size - 1 - -/** - * Returns the last valid index for the array. - */ -public val IntArray.lastIndex: Int - get() = size - 1 - -/** - * Returns the last valid index for the array. - */ -public val LongArray.lastIndex: Int - get() = size - 1 - -/** - * Returns the last valid index for the array. - */ -public val FloatArray.lastIndex: Int - get() = size - 1 - -/** - * Returns the last valid index for the array. - */ -public val DoubleArray.lastIndex: Int - get() = size - 1 - -/** - * Returns the last valid index for the array. - */ -public val BooleanArray.lastIndex: Int - get() = size - 1 - -/** - * Returns the last valid index for the array. - */ -public val CharArray.lastIndex: Int - get() = size - 1 - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun Array.plus(element: T): Array { - return this.asDynamic().concat(arrayOf(element)) -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun ByteArray.plus(element: Byte): ByteArray { - return plus(byteArrayOf(element)) -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun ShortArray.plus(element: Short): ShortArray { - return plus(shortArrayOf(element)) -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun IntArray.plus(element: Int): IntArray { - return plus(intArrayOf(element)) -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun LongArray.plus(element: Long): LongArray { - return plus(longArrayOf(element)) -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun FloatArray.plus(element: Float): FloatArray { - return plus(floatArrayOf(element)) -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun DoubleArray.plus(element: Double): DoubleArray { - return plus(doubleArrayOf(element)) -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun BooleanArray.plus(element: Boolean): BooleanArray { - return plus(booleanArrayOf(element)) -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun CharArray.plus(element: Char): CharArray { - return plus(charArrayOf(element)) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun Array.plus(elements: Collection): Array { - return arrayPlusCollection(this, elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun ByteArray.plus(elements: Collection): ByteArray { - return fillFromCollection(this.copyOf(size + elements.size), this.size, elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun ShortArray.plus(elements: Collection): ShortArray { - return fillFromCollection(this.copyOf(size + elements.size), this.size, elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun IntArray.plus(elements: Collection): IntArray { - return fillFromCollection(this.copyOf(size + elements.size), this.size, elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun LongArray.plus(elements: Collection): LongArray { - return arrayPlusCollection(this, elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun FloatArray.plus(elements: Collection): FloatArray { - return fillFromCollection(this.copyOf(size + elements.size), this.size, elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun DoubleArray.plus(elements: Collection): DoubleArray { - return fillFromCollection(this.copyOf(size + elements.size), this.size, elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun BooleanArray.plus(elements: Collection): BooleanArray { - return arrayPlusCollection(this, elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun CharArray.plus(elements: Collection): CharArray { - return fillFromCollection(this.copyOf(size + elements.size), this.size, elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun Array.plus(elements: Array): Array { - return this.asDynamic().concat(elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun ByteArray.plus(elements: ByteArray): ByteArray { - return primitiveArrayConcat(this, elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun ShortArray.plus(elements: ShortArray): ShortArray { - return primitiveArrayConcat(this, elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun IntArray.plus(elements: IntArray): IntArray { - return primitiveArrayConcat(this, elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun LongArray.plus(elements: LongArray): LongArray { - return primitiveArrayConcat(this, elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun FloatArray.plus(elements: FloatArray): FloatArray { - return primitiveArrayConcat(this, elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun DoubleArray.plus(elements: DoubleArray): DoubleArray { - return primitiveArrayConcat(this, elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun BooleanArray.plus(elements: BooleanArray): BooleanArray { - return primitiveArrayConcat(this, elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun CharArray.plus(elements: CharArray): CharArray { - return primitiveArrayConcat(this, elements) -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun Array.plusElement(element: T): Array { - return this.asDynamic().concat(arrayOf(element)) -} - -/** - * Sorts the array in-place. - */ -@library("primitiveArraySort") -public fun IntArray.sort(): Unit { - definedExternally -} - -/** - * Sorts the array in-place. - */ -public fun LongArray.sort(): Unit { - if (size > 1) - sort { a: Long, b: Long -> a.compareTo(b) } -} - -/** - * Sorts the array in-place. - */ -@library("primitiveArraySort") -public fun ByteArray.sort(): Unit { - definedExternally -} - -/** - * Sorts the array in-place. - */ -@library("primitiveArraySort") -public fun ShortArray.sort(): Unit { - definedExternally -} - -/** - * Sorts the array in-place. - */ -@library("primitiveArraySort") -public fun DoubleArray.sort(): Unit { - definedExternally -} - -/** - * Sorts the array in-place. - */ -@library("primitiveArraySort") -public fun FloatArray.sort(): Unit { - definedExternally -} - -/** - * Sorts the array in-place. - */ -@library("primitiveArraySort") -public fun CharArray.sort(): Unit { - definedExternally -} - -/** - * Sorts the array in-place according to the natural order of its elements. - */ -public fun > Array.sort(): Unit { - if (size > 1) - sort { a: T, b: T -> a.compareTo(b) } -} - -/** - * Sorts the array in-place according to the order specified by the given [comparison] function. - */ -@kotlin.internal.InlineOnly -public inline fun Array.sort(noinline comparison: (a: T, b: T) -> Int): Unit { - asDynamic().sort(comparison) -} - -/** - * Sorts the array in-place according to the order specified by the given [comparison] function. - */ -@kotlin.internal.InlineOnly -public inline fun ByteArray.sort(noinline comparison: (a: Byte, b: Byte) -> Int): Unit { - asDynamic().sort(comparison) -} - -/** - * Sorts the array in-place according to the order specified by the given [comparison] function. - */ -@kotlin.internal.InlineOnly -public inline fun ShortArray.sort(noinline comparison: (a: Short, b: Short) -> Int): Unit { - asDynamic().sort(comparison) -} - -/** - * Sorts the array in-place according to the order specified by the given [comparison] function. - */ -@kotlin.internal.InlineOnly -public inline fun IntArray.sort(noinline comparison: (a: Int, b: Int) -> Int): Unit { - asDynamic().sort(comparison) -} - -/** - * Sorts the array in-place according to the order specified by the given [comparison] function. - */ -@kotlin.internal.InlineOnly -public inline fun LongArray.sort(noinline comparison: (a: Long, b: Long) -> Int): Unit { - asDynamic().sort(comparison) -} - -/** - * Sorts the array in-place according to the order specified by the given [comparison] function. - */ -@kotlin.internal.InlineOnly -public inline fun FloatArray.sort(noinline comparison: (a: Float, b: Float) -> Int): Unit { - asDynamic().sort(comparison) -} - -/** - * Sorts the array in-place according to the order specified by the given [comparison] function. - */ -@kotlin.internal.InlineOnly -public inline fun DoubleArray.sort(noinline comparison: (a: Double, b: Double) -> Int): Unit { - asDynamic().sort(comparison) -} - -/** - * Sorts the array in-place according to the order specified by the given [comparison] function. - */ -@kotlin.internal.InlineOnly -public inline fun CharArray.sort(noinline comparison: (a: Char, b: Char) -> Int): Unit { - asDynamic().sort(comparison) -} - -/** - * Sorts the array in-place according to the order specified by the given [comparator]. - */ -public fun Array.sortWith(comparator: Comparator): Unit { - if (size > 1) - sort { a, b -> comparator.compare(a, b) } -} - -/** - * Returns an array of Boolean containing all of the elements of this generic array. - */ -public fun Array.toBooleanArray(): BooleanArray { - val result = BooleanArray(size) - for (index in indices) - result[index] = this[index] - return result -} - -/** - * Returns an array of Byte containing all of the elements of this generic array. - */ -public fun Array.toByteArray(): ByteArray { - val result = ByteArray(size) - for (index in indices) - result[index] = this[index] - return result -} - -/** - * Returns an array of Char containing all of the elements of this generic array. - */ -public fun Array.toCharArray(): CharArray { - val result = CharArray(size) - for (index in indices) - result[index] = this[index] - return result -} - -/** - * Returns an array of Double containing all of the elements of this generic array. - */ -public fun Array.toDoubleArray(): DoubleArray { - val result = DoubleArray(size) - for (index in indices) - result[index] = this[index] - return result -} - -/** - * Returns an array of Float containing all of the elements of this generic array. - */ -public fun Array.toFloatArray(): FloatArray { - val result = FloatArray(size) - for (index in indices) - result[index] = this[index] - return result -} - -/** - * Returns an array of Int containing all of the elements of this generic array. - */ -public fun Array.toIntArray(): IntArray { - val result = IntArray(size) - for (index in indices) - result[index] = this[index] - return result -} - -/** - * Returns an array of Long containing all of the elements of this generic array. - */ -public fun Array.toLongArray(): LongArray { - val result = LongArray(size) - for (index in indices) - result[index] = this[index] - return result -} - -/** - * Returns an array of Short containing all of the elements of this generic array. - */ -public fun Array.toShortArray(): ShortArray { - val result = ShortArray(size) - for (index in indices) - result[index] = this[index] - return result -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun ByteArray.toTypedArray(): Array { - return js("[]").slice.call(this) -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun ShortArray.toTypedArray(): Array { - return js("[]").slice.call(this) -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun IntArray.toTypedArray(): Array { - return js("[]").slice.call(this) -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun LongArray.toTypedArray(): Array { - return copyOf().unsafeCast>() -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun FloatArray.toTypedArray(): Array { - return js("[]").slice.call(this) -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun DoubleArray.toTypedArray(): Array { - return js("[]").slice.call(this) -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun BooleanArray.toTypedArray(): Array { - return copyOf().unsafeCast>() -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun CharArray.toTypedArray(): Array { - return Array(size, { i -> this[i] }) -} - -/** - * Returns a [Map] containing key-value pairs provided by [transform] function - * applied to elements of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun Array.associate(transform: (T) -> Pair): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateTo(LinkedHashMap(capacity), transform) -} - -/** - * Returns a [Map] containing key-value pairs provided by [transform] function - * applied to elements of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun ByteArray.associate(transform: (Byte) -> Pair): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateTo(LinkedHashMap(capacity), transform) -} - -/** - * Returns a [Map] containing key-value pairs provided by [transform] function - * applied to elements of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun ShortArray.associate(transform: (Short) -> Pair): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateTo(LinkedHashMap(capacity), transform) -} - -/** - * Returns a [Map] containing key-value pairs provided by [transform] function - * applied to elements of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun IntArray.associate(transform: (Int) -> Pair): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateTo(LinkedHashMap(capacity), transform) -} - -/** - * Returns a [Map] containing key-value pairs provided by [transform] function - * applied to elements of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun LongArray.associate(transform: (Long) -> Pair): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateTo(LinkedHashMap(capacity), transform) -} - -/** - * Returns a [Map] containing key-value pairs provided by [transform] function - * applied to elements of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun FloatArray.associate(transform: (Float) -> Pair): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateTo(LinkedHashMap(capacity), transform) -} - -/** - * Returns a [Map] containing key-value pairs provided by [transform] function - * applied to elements of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun DoubleArray.associate(transform: (Double) -> Pair): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateTo(LinkedHashMap(capacity), transform) -} - -/** - * Returns a [Map] containing key-value pairs provided by [transform] function - * applied to elements of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun BooleanArray.associate(transform: (Boolean) -> Pair): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateTo(LinkedHashMap(capacity), transform) -} - -/** - * Returns a [Map] containing key-value pairs provided by [transform] function - * applied to elements of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun CharArray.associate(transform: (Char) -> Pair): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateTo(LinkedHashMap(capacity), transform) -} - -/** - * Returns a [Map] containing the elements from the given array indexed by the key - * returned from [keySelector] function applied to each element. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun Array.associateBy(keySelector: (T) -> K): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector) -} - -/** - * Returns a [Map] containing the elements from the given array indexed by the key - * returned from [keySelector] function applied to each element. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun ByteArray.associateBy(keySelector: (Byte) -> K): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector) -} - -/** - * Returns a [Map] containing the elements from the given array indexed by the key - * returned from [keySelector] function applied to each element. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun ShortArray.associateBy(keySelector: (Short) -> K): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector) -} - -/** - * Returns a [Map] containing the elements from the given array indexed by the key - * returned from [keySelector] function applied to each element. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun IntArray.associateBy(keySelector: (Int) -> K): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector) -} - -/** - * Returns a [Map] containing the elements from the given array indexed by the key - * returned from [keySelector] function applied to each element. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun LongArray.associateBy(keySelector: (Long) -> K): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector) -} - -/** - * Returns a [Map] containing the elements from the given array indexed by the key - * returned from [keySelector] function applied to each element. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun FloatArray.associateBy(keySelector: (Float) -> K): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector) -} - -/** - * Returns a [Map] containing the elements from the given array indexed by the key - * returned from [keySelector] function applied to each element. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun DoubleArray.associateBy(keySelector: (Double) -> K): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector) -} - -/** - * Returns a [Map] containing the elements from the given array indexed by the key - * returned from [keySelector] function applied to each element. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun BooleanArray.associateBy(keySelector: (Boolean) -> K): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector) -} - -/** - * Returns a [Map] containing the elements from the given array indexed by the key - * returned from [keySelector] function applied to each element. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun CharArray.associateBy(keySelector: (Char) -> K): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector) -} - -/** - * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun Array.associateBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) -} - -/** - * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun ByteArray.associateBy(keySelector: (Byte) -> K, valueTransform: (Byte) -> V): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) -} - -/** - * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun ShortArray.associateBy(keySelector: (Short) -> K, valueTransform: (Short) -> V): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) -} - -/** - * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun IntArray.associateBy(keySelector: (Int) -> K, valueTransform: (Int) -> V): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) -} - -/** - * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun LongArray.associateBy(keySelector: (Long) -> K, valueTransform: (Long) -> V): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) -} - -/** - * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun FloatArray.associateBy(keySelector: (Float) -> K, valueTransform: (Float) -> V): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) -} - -/** - * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun DoubleArray.associateBy(keySelector: (Double) -> K, valueTransform: (Double) -> V): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) -} - -/** - * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun BooleanArray.associateBy(keySelector: (Boolean) -> K, valueTransform: (Boolean) -> V): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) -} - -/** - * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun CharArray.associateBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function applied to each element of the given array - * and value is the element itself. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > Array.associateByTo(destination: M, keySelector: (T) -> K): M { - for (element in this) { - destination.put(keySelector(element), element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function applied to each element of the given array - * and value is the element itself. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > ByteArray.associateByTo(destination: M, keySelector: (Byte) -> K): M { - for (element in this) { - destination.put(keySelector(element), element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function applied to each element of the given array - * and value is the element itself. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > ShortArray.associateByTo(destination: M, keySelector: (Short) -> K): M { - for (element in this) { - destination.put(keySelector(element), element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function applied to each element of the given array - * and value is the element itself. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > IntArray.associateByTo(destination: M, keySelector: (Int) -> K): M { - for (element in this) { - destination.put(keySelector(element), element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function applied to each element of the given array - * and value is the element itself. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > LongArray.associateByTo(destination: M, keySelector: (Long) -> K): M { - for (element in this) { - destination.put(keySelector(element), element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function applied to each element of the given array - * and value is the element itself. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > FloatArray.associateByTo(destination: M, keySelector: (Float) -> K): M { - for (element in this) { - destination.put(keySelector(element), element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function applied to each element of the given array - * and value is the element itself. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > DoubleArray.associateByTo(destination: M, keySelector: (Double) -> K): M { - for (element in this) { - destination.put(keySelector(element), element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function applied to each element of the given array - * and value is the element itself. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > BooleanArray.associateByTo(destination: M, keySelector: (Boolean) -> K): M { - for (element in this) { - destination.put(keySelector(element), element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function applied to each element of the given array - * and value is the element itself. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > CharArray.associateByTo(destination: M, keySelector: (Char) -> K): M { - for (element in this) { - destination.put(keySelector(element), element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function and - * and value is provided by the [valueTransform] function applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > Array.associateByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M { - for (element in this) { - destination.put(keySelector(element), valueTransform(element)) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function and - * and value is provided by the [valueTransform] function applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > ByteArray.associateByTo(destination: M, keySelector: (Byte) -> K, valueTransform: (Byte) -> V): M { - for (element in this) { - destination.put(keySelector(element), valueTransform(element)) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function and - * and value is provided by the [valueTransform] function applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > ShortArray.associateByTo(destination: M, keySelector: (Short) -> K, valueTransform: (Short) -> V): M { - for (element in this) { - destination.put(keySelector(element), valueTransform(element)) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function and - * and value is provided by the [valueTransform] function applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > IntArray.associateByTo(destination: M, keySelector: (Int) -> K, valueTransform: (Int) -> V): M { - for (element in this) { - destination.put(keySelector(element), valueTransform(element)) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function and - * and value is provided by the [valueTransform] function applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > LongArray.associateByTo(destination: M, keySelector: (Long) -> K, valueTransform: (Long) -> V): M { - for (element in this) { - destination.put(keySelector(element), valueTransform(element)) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function and - * and value is provided by the [valueTransform] function applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > FloatArray.associateByTo(destination: M, keySelector: (Float) -> K, valueTransform: (Float) -> V): M { - for (element in this) { - destination.put(keySelector(element), valueTransform(element)) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function and - * and value is provided by the [valueTransform] function applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > DoubleArray.associateByTo(destination: M, keySelector: (Double) -> K, valueTransform: (Double) -> V): M { - for (element in this) { - destination.put(keySelector(element), valueTransform(element)) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function and - * and value is provided by the [valueTransform] function applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > BooleanArray.associateByTo(destination: M, keySelector: (Boolean) -> K, valueTransform: (Boolean) -> V): M { - for (element in this) { - destination.put(keySelector(element), valueTransform(element)) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function and - * and value is provided by the [valueTransform] function applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > CharArray.associateByTo(destination: M, keySelector: (Char) -> K, valueTransform: (Char) -> V): M { - for (element in this) { - destination.put(keySelector(element), valueTransform(element)) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs - * provided by [transform] function applied to each element of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - */ -public inline fun > Array.associateTo(destination: M, transform: (T) -> Pair): M { - for (element in this) { - destination += transform(element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs - * provided by [transform] function applied to each element of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - */ -public inline fun > ByteArray.associateTo(destination: M, transform: (Byte) -> Pair): M { - for (element in this) { - destination += transform(element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs - * provided by [transform] function applied to each element of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - */ -public inline fun > ShortArray.associateTo(destination: M, transform: (Short) -> Pair): M { - for (element in this) { - destination += transform(element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs - * provided by [transform] function applied to each element of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - */ -public inline fun > IntArray.associateTo(destination: M, transform: (Int) -> Pair): M { - for (element in this) { - destination += transform(element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs - * provided by [transform] function applied to each element of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - */ -public inline fun > LongArray.associateTo(destination: M, transform: (Long) -> Pair): M { - for (element in this) { - destination += transform(element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs - * provided by [transform] function applied to each element of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - */ -public inline fun > FloatArray.associateTo(destination: M, transform: (Float) -> Pair): M { - for (element in this) { - destination += transform(element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs - * provided by [transform] function applied to each element of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - */ -public inline fun > DoubleArray.associateTo(destination: M, transform: (Double) -> Pair): M { - for (element in this) { - destination += transform(element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs - * provided by [transform] function applied to each element of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - */ -public inline fun > BooleanArray.associateTo(destination: M, transform: (Boolean) -> Pair): M { - for (element in this) { - destination += transform(element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs - * provided by [transform] function applied to each element of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - */ -public inline fun > CharArray.associateTo(destination: M, transform: (Char) -> Pair): M { - for (element in this) { - destination += transform(element) - } - return destination -} - -/** - * Appends all elements to the given [destination] collection. - */ -public fun > Array.toCollection(destination: C): C { - for (item in this) { - destination.add(item) - } - return destination -} - -/** - * Appends all elements to the given [destination] collection. - */ -public fun > ByteArray.toCollection(destination: C): C { - for (item in this) { - destination.add(item) - } - return destination -} - -/** - * Appends all elements to the given [destination] collection. - */ -public fun > ShortArray.toCollection(destination: C): C { - for (item in this) { - destination.add(item) - } - return destination -} - -/** - * Appends all elements to the given [destination] collection. - */ -public fun > IntArray.toCollection(destination: C): C { - for (item in this) { - destination.add(item) - } - return destination -} - -/** - * Appends all elements to the given [destination] collection. - */ -public fun > LongArray.toCollection(destination: C): C { - for (item in this) { - destination.add(item) - } - return destination -} - -/** - * Appends all elements to the given [destination] collection. - */ -public fun > FloatArray.toCollection(destination: C): C { - for (item in this) { - destination.add(item) - } - return destination -} - -/** - * Appends all elements to the given [destination] collection. - */ -public fun > DoubleArray.toCollection(destination: C): C { - for (item in this) { - destination.add(item) - } - return destination -} - -/** - * Appends all elements to the given [destination] collection. - */ -public fun > BooleanArray.toCollection(destination: C): C { - for (item in this) { - destination.add(item) - } - return destination -} - -/** - * Appends all elements to the given [destination] collection. - */ -public fun > CharArray.toCollection(destination: C): C { - for (item in this) { - destination.add(item) - } - return destination -} - -/** - * Returns a [HashSet] of all elements. - */ -public fun Array.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(size))) -} - -/** - * Returns a [HashSet] of all elements. - */ -public fun ByteArray.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(size))) -} - -/** - * Returns a [HashSet] of all elements. - */ -public fun ShortArray.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(size))) -} - -/** - * Returns a [HashSet] of all elements. - */ -public fun IntArray.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(size))) -} - -/** - * Returns a [HashSet] of all elements. - */ -public fun LongArray.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(size))) -} - -/** - * Returns a [HashSet] of all elements. - */ -public fun FloatArray.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(size))) -} - -/** - * Returns a [HashSet] of all elements. - */ -public fun DoubleArray.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(size))) -} - -/** - * Returns a [HashSet] of all elements. - */ -public fun BooleanArray.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(size))) -} - -/** - * Returns a [HashSet] of all elements. - */ -public fun CharArray.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(size))) -} - -/** - * Returns a [List] containing all elements. - */ -public fun Array.toList(): List { - return when (size) { - 0 -> emptyList() - 1 -> listOf(this[0]) - else -> this.toMutableList() - } -} - -/** - * Returns a [List] containing all elements. - */ -public fun ByteArray.toList(): List { - return when (size) { - 0 -> emptyList() - 1 -> listOf(this[0]) - else -> this.toMutableList() - } -} - -/** - * Returns a [List] containing all elements. - */ -public fun ShortArray.toList(): List { - return when (size) { - 0 -> emptyList() - 1 -> listOf(this[0]) - else -> this.toMutableList() - } -} - -/** - * Returns a [List] containing all elements. - */ -public fun IntArray.toList(): List { - return when (size) { - 0 -> emptyList() - 1 -> listOf(this[0]) - else -> this.toMutableList() - } -} - -/** - * Returns a [List] containing all elements. - */ -public fun LongArray.toList(): List { - return when (size) { - 0 -> emptyList() - 1 -> listOf(this[0]) - else -> this.toMutableList() - } -} - -/** - * Returns a [List] containing all elements. - */ -public fun FloatArray.toList(): List { - return when (size) { - 0 -> emptyList() - 1 -> listOf(this[0]) - else -> this.toMutableList() - } -} - -/** - * Returns a [List] containing all elements. - */ -public fun DoubleArray.toList(): List { - return when (size) { - 0 -> emptyList() - 1 -> listOf(this[0]) - else -> this.toMutableList() - } -} - -/** - * Returns a [List] containing all elements. - */ -public fun BooleanArray.toList(): List { - return when (size) { - 0 -> emptyList() - 1 -> listOf(this[0]) - else -> this.toMutableList() - } -} - -/** - * Returns a [List] containing all elements. - */ -public fun CharArray.toList(): List { - return when (size) { - 0 -> emptyList() - 1 -> listOf(this[0]) - else -> this.toMutableList() - } -} - -/** - * Returns a [MutableList] filled with all elements of this array. - */ -public fun Array.toMutableList(): MutableList { - return ArrayList(this.asCollection()) -} - -/** - * Returns a [MutableList] filled with all elements of this array. - */ -public fun ByteArray.toMutableList(): MutableList { - val list = ArrayList(size) - for (item in this) list.add(item) - return list -} - -/** - * Returns a [MutableList] filled with all elements of this array. - */ -public fun ShortArray.toMutableList(): MutableList { - val list = ArrayList(size) - for (item in this) list.add(item) - return list -} - -/** - * Returns a [MutableList] filled with all elements of this array. - */ -public fun IntArray.toMutableList(): MutableList { - val list = ArrayList(size) - for (item in this) list.add(item) - return list -} - -/** - * Returns a [MutableList] filled with all elements of this array. - */ -public fun LongArray.toMutableList(): MutableList { - val list = ArrayList(size) - for (item in this) list.add(item) - return list -} - -/** - * Returns a [MutableList] filled with all elements of this array. - */ -public fun FloatArray.toMutableList(): MutableList { - val list = ArrayList(size) - for (item in this) list.add(item) - return list -} - -/** - * Returns a [MutableList] filled with all elements of this array. - */ -public fun DoubleArray.toMutableList(): MutableList { - val list = ArrayList(size) - for (item in this) list.add(item) - return list -} - -/** - * Returns a [MutableList] filled with all elements of this array. - */ -public fun BooleanArray.toMutableList(): MutableList { - val list = ArrayList(size) - for (item in this) list.add(item) - return list -} - -/** - * Returns a [MutableList] filled with all elements of this array. - */ -public fun CharArray.toMutableList(): MutableList { - val list = ArrayList(size) - for (item in this) list.add(item) - return list -} - -/** - * Returns a [Set] of all elements. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun Array.toSet(): Set { - return when (size) { - 0 -> emptySet() - 1 -> setOf(this[0]) - else -> toCollection(LinkedHashSet(mapCapacity(size))) - } -} - -/** - * Returns a [Set] of all elements. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun ByteArray.toSet(): Set { - return when (size) { - 0 -> emptySet() - 1 -> setOf(this[0]) - else -> toCollection(LinkedHashSet(mapCapacity(size))) - } -} - -/** - * Returns a [Set] of all elements. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun ShortArray.toSet(): Set { - return when (size) { - 0 -> emptySet() - 1 -> setOf(this[0]) - else -> toCollection(LinkedHashSet(mapCapacity(size))) - } -} - -/** - * Returns a [Set] of all elements. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun IntArray.toSet(): Set { - return when (size) { - 0 -> emptySet() - 1 -> setOf(this[0]) - else -> toCollection(LinkedHashSet(mapCapacity(size))) - } -} - -/** - * Returns a [Set] of all elements. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun LongArray.toSet(): Set { - return when (size) { - 0 -> emptySet() - 1 -> setOf(this[0]) - else -> toCollection(LinkedHashSet(mapCapacity(size))) - } -} - -/** - * Returns a [Set] of all elements. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun FloatArray.toSet(): Set { - return when (size) { - 0 -> emptySet() - 1 -> setOf(this[0]) - else -> toCollection(LinkedHashSet(mapCapacity(size))) - } -} - -/** - * Returns a [Set] of all elements. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun DoubleArray.toSet(): Set { - return when (size) { - 0 -> emptySet() - 1 -> setOf(this[0]) - else -> toCollection(LinkedHashSet(mapCapacity(size))) - } -} - -/** - * Returns a [Set] of all elements. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun BooleanArray.toSet(): Set { - return when (size) { - 0 -> emptySet() - 1 -> setOf(this[0]) - else -> toCollection(LinkedHashSet(mapCapacity(size))) - } -} - -/** - * Returns a [Set] of all elements. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun CharArray.toSet(): Set { - return when (size) { - 0 -> emptySet() - 1 -> setOf(this[0]) - else -> toCollection(LinkedHashSet(mapCapacity(size))) - } -} - -/** - * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array. - */ -public inline fun Array.flatMap(transform: (T) -> Iterable): List { - return flatMapTo(ArrayList(), transform) -} - -/** - * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array. - */ -public inline fun ByteArray.flatMap(transform: (Byte) -> Iterable): List { - return flatMapTo(ArrayList(), transform) -} - -/** - * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array. - */ -public inline fun ShortArray.flatMap(transform: (Short) -> Iterable): List { - return flatMapTo(ArrayList(), transform) -} - -/** - * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array. - */ -public inline fun IntArray.flatMap(transform: (Int) -> Iterable): List { - return flatMapTo(ArrayList(), transform) -} - -/** - * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array. - */ -public inline fun LongArray.flatMap(transform: (Long) -> Iterable): List { - return flatMapTo(ArrayList(), transform) -} - -/** - * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array. - */ -public inline fun FloatArray.flatMap(transform: (Float) -> Iterable): List { - return flatMapTo(ArrayList(), transform) -} - -/** - * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array. - */ -public inline fun DoubleArray.flatMap(transform: (Double) -> Iterable): List { - return flatMapTo(ArrayList(), transform) -} - -/** - * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array. - */ -public inline fun BooleanArray.flatMap(transform: (Boolean) -> Iterable): List { - return flatMapTo(ArrayList(), transform) -} - -/** - * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array. - */ -public inline fun CharArray.flatMap(transform: (Char) -> Iterable): List { - return flatMapTo(ArrayList(), transform) -} - -/** - * Appends all elements yielded from results of [transform] function being invoked on each element of original array, to the given [destination]. - */ -public inline fun > Array.flatMapTo(destination: C, transform: (T) -> Iterable): C { - for (element in this) { - val list = transform(element) - destination.addAll(list) - } - return destination -} - -/** - * Appends all elements yielded from results of [transform] function being invoked on each element of original array, to the given [destination]. - */ -public inline fun > ByteArray.flatMapTo(destination: C, transform: (Byte) -> Iterable): C { - for (element in this) { - val list = transform(element) - destination.addAll(list) - } - return destination -} - -/** - * Appends all elements yielded from results of [transform] function being invoked on each element of original array, to the given [destination]. - */ -public inline fun > ShortArray.flatMapTo(destination: C, transform: (Short) -> Iterable): C { - for (element in this) { - val list = transform(element) - destination.addAll(list) - } - return destination -} - -/** - * Appends all elements yielded from results of [transform] function being invoked on each element of original array, to the given [destination]. - */ -public inline fun > IntArray.flatMapTo(destination: C, transform: (Int) -> Iterable): C { - for (element in this) { - val list = transform(element) - destination.addAll(list) - } - return destination -} - -/** - * Appends all elements yielded from results of [transform] function being invoked on each element of original array, to the given [destination]. - */ -public inline fun > LongArray.flatMapTo(destination: C, transform: (Long) -> Iterable): C { - for (element in this) { - val list = transform(element) - destination.addAll(list) - } - return destination -} - -/** - * Appends all elements yielded from results of [transform] function being invoked on each element of original array, to the given [destination]. - */ -public inline fun > FloatArray.flatMapTo(destination: C, transform: (Float) -> Iterable): C { - for (element in this) { - val list = transform(element) - destination.addAll(list) - } - return destination -} - -/** - * Appends all elements yielded from results of [transform] function being invoked on each element of original array, to the given [destination]. - */ -public inline fun > DoubleArray.flatMapTo(destination: C, transform: (Double) -> Iterable): C { - for (element in this) { - val list = transform(element) - destination.addAll(list) - } - return destination -} - -/** - * Appends all elements yielded from results of [transform] function being invoked on each element of original array, to the given [destination]. - */ -public inline fun > BooleanArray.flatMapTo(destination: C, transform: (Boolean) -> Iterable): C { - for (element in this) { - val list = transform(element) - destination.addAll(list) - } - return destination -} - -/** - * Appends all elements yielded from results of [transform] function being invoked on each element of original array, to the given [destination]. - */ -public inline fun > CharArray.flatMapTo(destination: C, transform: (Char) -> Iterable): C { - for (element in this) { - val list = transform(element) - destination.addAll(list) - } - return destination -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and returns a map where each group key is associated with a list of corresponding elements. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun Array.groupBy(keySelector: (T) -> K): Map> { - return groupByTo(LinkedHashMap>(), keySelector) -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and returns a map where each group key is associated with a list of corresponding elements. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun ByteArray.groupBy(keySelector: (Byte) -> K): Map> { - return groupByTo(LinkedHashMap>(), keySelector) -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and returns a map where each group key is associated with a list of corresponding elements. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun ShortArray.groupBy(keySelector: (Short) -> K): Map> { - return groupByTo(LinkedHashMap>(), keySelector) -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and returns a map where each group key is associated with a list of corresponding elements. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun IntArray.groupBy(keySelector: (Int) -> K): Map> { - return groupByTo(LinkedHashMap>(), keySelector) -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and returns a map where each group key is associated with a list of corresponding elements. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun LongArray.groupBy(keySelector: (Long) -> K): Map> { - return groupByTo(LinkedHashMap>(), keySelector) -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and returns a map where each group key is associated with a list of corresponding elements. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun FloatArray.groupBy(keySelector: (Float) -> K): Map> { - return groupByTo(LinkedHashMap>(), keySelector) -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and returns a map where each group key is associated with a list of corresponding elements. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun DoubleArray.groupBy(keySelector: (Double) -> K): Map> { - return groupByTo(LinkedHashMap>(), keySelector) -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and returns a map where each group key is associated with a list of corresponding elements. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun BooleanArray.groupBy(keySelector: (Boolean) -> K): Map> { - return groupByTo(LinkedHashMap>(), keySelector) -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and returns a map where each group key is associated with a list of corresponding elements. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun CharArray.groupBy(keySelector: (Char) -> K): Map> { - return groupByTo(LinkedHashMap>(), keySelector) -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and returns a map where each group key is associated with a list of corresponding values. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun Array.groupBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map> { - return groupByTo(LinkedHashMap>(), keySelector, valueTransform) -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and returns a map where each group key is associated with a list of corresponding values. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun ByteArray.groupBy(keySelector: (Byte) -> K, valueTransform: (Byte) -> V): Map> { - return groupByTo(LinkedHashMap>(), keySelector, valueTransform) -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and returns a map where each group key is associated with a list of corresponding values. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun ShortArray.groupBy(keySelector: (Short) -> K, valueTransform: (Short) -> V): Map> { - return groupByTo(LinkedHashMap>(), keySelector, valueTransform) -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and returns a map where each group key is associated with a list of corresponding values. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun IntArray.groupBy(keySelector: (Int) -> K, valueTransform: (Int) -> V): Map> { - return groupByTo(LinkedHashMap>(), keySelector, valueTransform) -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and returns a map where each group key is associated with a list of corresponding values. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun LongArray.groupBy(keySelector: (Long) -> K, valueTransform: (Long) -> V): Map> { - return groupByTo(LinkedHashMap>(), keySelector, valueTransform) -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and returns a map where each group key is associated with a list of corresponding values. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun FloatArray.groupBy(keySelector: (Float) -> K, valueTransform: (Float) -> V): Map> { - return groupByTo(LinkedHashMap>(), keySelector, valueTransform) -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and returns a map where each group key is associated with a list of corresponding values. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun DoubleArray.groupBy(keySelector: (Double) -> K, valueTransform: (Double) -> V): Map> { - return groupByTo(LinkedHashMap>(), keySelector, valueTransform) -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and returns a map where each group key is associated with a list of corresponding values. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun BooleanArray.groupBy(keySelector: (Boolean) -> K, valueTransform: (Boolean) -> V): Map> { - return groupByTo(LinkedHashMap>(), keySelector, valueTransform) -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and returns a map where each group key is associated with a list of corresponding values. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun CharArray.groupBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map> { - return groupByTo(LinkedHashMap>(), keySelector, valueTransform) -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun >> Array.groupByTo(destination: M, keySelector: (T) -> K): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(element) - } - return destination -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun >> ByteArray.groupByTo(destination: M, keySelector: (Byte) -> K): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(element) - } - return destination -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun >> ShortArray.groupByTo(destination: M, keySelector: (Short) -> K): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(element) - } - return destination -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun >> IntArray.groupByTo(destination: M, keySelector: (Int) -> K): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(element) - } - return destination -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun >> LongArray.groupByTo(destination: M, keySelector: (Long) -> K): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(element) - } - return destination -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun >> FloatArray.groupByTo(destination: M, keySelector: (Float) -> K): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(element) - } - return destination -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun >> DoubleArray.groupByTo(destination: M, keySelector: (Double) -> K): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(element) - } - return destination -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun >> BooleanArray.groupByTo(destination: M, keySelector: (Boolean) -> K): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(element) - } - return destination -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun >> CharArray.groupByTo(destination: M, keySelector: (Char) -> K): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(element) - } - return destination -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and puts to the [destination] map each group key associated with a list of corresponding values. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun >> Array.groupByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(valueTransform(element)) - } - return destination -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and puts to the [destination] map each group key associated with a list of corresponding values. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun >> ByteArray.groupByTo(destination: M, keySelector: (Byte) -> K, valueTransform: (Byte) -> V): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(valueTransform(element)) - } - return destination -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and puts to the [destination] map each group key associated with a list of corresponding values. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun >> ShortArray.groupByTo(destination: M, keySelector: (Short) -> K, valueTransform: (Short) -> V): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(valueTransform(element)) - } - return destination -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and puts to the [destination] map each group key associated with a list of corresponding values. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun >> IntArray.groupByTo(destination: M, keySelector: (Int) -> K, valueTransform: (Int) -> V): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(valueTransform(element)) - } - return destination -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and puts to the [destination] map each group key associated with a list of corresponding values. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun >> LongArray.groupByTo(destination: M, keySelector: (Long) -> K, valueTransform: (Long) -> V): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(valueTransform(element)) - } - return destination -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and puts to the [destination] map each group key associated with a list of corresponding values. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun >> FloatArray.groupByTo(destination: M, keySelector: (Float) -> K, valueTransform: (Float) -> V): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(valueTransform(element)) - } - return destination -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and puts to the [destination] map each group key associated with a list of corresponding values. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun >> DoubleArray.groupByTo(destination: M, keySelector: (Double) -> K, valueTransform: (Double) -> V): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(valueTransform(element)) - } - return destination -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and puts to the [destination] map each group key associated with a list of corresponding values. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun >> BooleanArray.groupByTo(destination: M, keySelector: (Boolean) -> K, valueTransform: (Boolean) -> V): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(valueTransform(element)) - } - return destination -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and puts to the [destination] map each group key associated with a list of corresponding values. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun >> CharArray.groupByTo(destination: M, keySelector: (Char) -> K, valueTransform: (Char) -> V): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(valueTransform(element)) - } - return destination -} - -/** - * Creates a [Grouping] source from an array to be used later with one of group-and-fold operations - * using the specified [keySelector] function to extract a key from each element. - * - * @sample samples.collections.Collections.Transformations.groupingByEachCount - */ -@SinceKotlin("1.1") -public inline fun Array.groupingBy(crossinline keySelector: (T) -> K): Grouping { - return object : Grouping { - override fun sourceIterator(): Iterator = this@groupingBy.iterator() - override fun keyOf(element: T): K = keySelector(element) - } -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element in the original array. - */ -public inline fun Array.map(transform: (T) -> R): List { - return mapTo(ArrayList(size), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element in the original array. - */ -public inline fun ByteArray.map(transform: (Byte) -> R): List { - return mapTo(ArrayList(size), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element in the original array. - */ -public inline fun ShortArray.map(transform: (Short) -> R): List { - return mapTo(ArrayList(size), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element in the original array. - */ -public inline fun IntArray.map(transform: (Int) -> R): List { - return mapTo(ArrayList(size), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element in the original array. - */ -public inline fun LongArray.map(transform: (Long) -> R): List { - return mapTo(ArrayList(size), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element in the original array. - */ -public inline fun FloatArray.map(transform: (Float) -> R): List { - return mapTo(ArrayList(size), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element in the original array. - */ -public inline fun DoubleArray.map(transform: (Double) -> R): List { - return mapTo(ArrayList(size), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element in the original array. - */ -public inline fun BooleanArray.map(transform: (Boolean) -> R): List { - return mapTo(ArrayList(size), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element in the original array. - */ -public inline fun CharArray.map(transform: (Char) -> R): List { - return mapTo(ArrayList(size), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element and its index in the original array. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun Array.mapIndexed(transform: (index: Int, T) -> R): List { - return mapIndexedTo(ArrayList(size), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element and its index in the original array. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun ByteArray.mapIndexed(transform: (index: Int, Byte) -> R): List { - return mapIndexedTo(ArrayList(size), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element and its index in the original array. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun ShortArray.mapIndexed(transform: (index: Int, Short) -> R): List { - return mapIndexedTo(ArrayList(size), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element and its index in the original array. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun IntArray.mapIndexed(transform: (index: Int, Int) -> R): List { - return mapIndexedTo(ArrayList(size), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element and its index in the original array. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun LongArray.mapIndexed(transform: (index: Int, Long) -> R): List { - return mapIndexedTo(ArrayList(size), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element and its index in the original array. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun FloatArray.mapIndexed(transform: (index: Int, Float) -> R): List { - return mapIndexedTo(ArrayList(size), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element and its index in the original array. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun DoubleArray.mapIndexed(transform: (index: Int, Double) -> R): List { - return mapIndexedTo(ArrayList(size), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element and its index in the original array. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun BooleanArray.mapIndexed(transform: (index: Int, Boolean) -> R): List { - return mapIndexedTo(ArrayList(size), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element and its index in the original array. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun CharArray.mapIndexed(transform: (index: Int, Char) -> R): List { - return mapIndexedTo(ArrayList(size), transform) -} - -/** - * Returns a list containing only the non-null results of applying the given [transform] function - * to each element and its index in the original array. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun Array.mapIndexedNotNull(transform: (index: Int, T) -> R?): List { - return mapIndexedNotNullTo(ArrayList(), transform) -} - -/** - * Applies the given [transform] function to each element and its index in the original array - * and appends only the non-null results to the given [destination]. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun > Array.mapIndexedNotNullTo(destination: C, transform: (index: Int, T) -> R?): C { - forEachIndexed { index, element -> transform(index, element)?.let { destination.add(it) } } - return destination -} - -/** - * Applies the given [transform] function to each element and its index in the original array - * and appends the results to the given [destination]. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun > Array.mapIndexedTo(destination: C, transform: (index: Int, T) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Applies the given [transform] function to each element and its index in the original array - * and appends the results to the given [destination]. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun > ByteArray.mapIndexedTo(destination: C, transform: (index: Int, Byte) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Applies the given [transform] function to each element and its index in the original array - * and appends the results to the given [destination]. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun > ShortArray.mapIndexedTo(destination: C, transform: (index: Int, Short) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Applies the given [transform] function to each element and its index in the original array - * and appends the results to the given [destination]. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun > IntArray.mapIndexedTo(destination: C, transform: (index: Int, Int) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Applies the given [transform] function to each element and its index in the original array - * and appends the results to the given [destination]. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun > LongArray.mapIndexedTo(destination: C, transform: (index: Int, Long) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Applies the given [transform] function to each element and its index in the original array - * and appends the results to the given [destination]. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun > FloatArray.mapIndexedTo(destination: C, transform: (index: Int, Float) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Applies the given [transform] function to each element and its index in the original array - * and appends the results to the given [destination]. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun > DoubleArray.mapIndexedTo(destination: C, transform: (index: Int, Double) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Applies the given [transform] function to each element and its index in the original array - * and appends the results to the given [destination]. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun > BooleanArray.mapIndexedTo(destination: C, transform: (index: Int, Boolean) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Applies the given [transform] function to each element and its index in the original array - * and appends the results to the given [destination]. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun > CharArray.mapIndexedTo(destination: C, transform: (index: Int, Char) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Returns a list containing only the non-null results of applying the given [transform] function - * to each element in the original array. - */ -public inline fun Array.mapNotNull(transform: (T) -> R?): List { - return mapNotNullTo(ArrayList(), transform) -} - -/** - * Applies the given [transform] function to each element in the original array - * and appends only the non-null results to the given [destination]. - */ -public inline fun > Array.mapNotNullTo(destination: C, transform: (T) -> R?): C { - forEach { element -> transform(element)?.let { destination.add(it) } } - return destination -} - -/** - * Applies the given [transform] function to each element of the original array - * and appends the results to the given [destination]. - */ -public inline fun > Array.mapTo(destination: C, transform: (T) -> R): C { - for (item in this) - destination.add(transform(item)) - return destination -} - -/** - * Applies the given [transform] function to each element of the original array - * and appends the results to the given [destination]. - */ -public inline fun > ByteArray.mapTo(destination: C, transform: (Byte) -> R): C { - for (item in this) - destination.add(transform(item)) - return destination -} - -/** - * Applies the given [transform] function to each element of the original array - * and appends the results to the given [destination]. - */ -public inline fun > ShortArray.mapTo(destination: C, transform: (Short) -> R): C { - for (item in this) - destination.add(transform(item)) - return destination -} - -/** - * Applies the given [transform] function to each element of the original array - * and appends the results to the given [destination]. - */ -public inline fun > IntArray.mapTo(destination: C, transform: (Int) -> R): C { - for (item in this) - destination.add(transform(item)) - return destination -} - -/** - * Applies the given [transform] function to each element of the original array - * and appends the results to the given [destination]. - */ -public inline fun > LongArray.mapTo(destination: C, transform: (Long) -> R): C { - for (item in this) - destination.add(transform(item)) - return destination -} - -/** - * Applies the given [transform] function to each element of the original array - * and appends the results to the given [destination]. - */ -public inline fun > FloatArray.mapTo(destination: C, transform: (Float) -> R): C { - for (item in this) - destination.add(transform(item)) - return destination -} - -/** - * Applies the given [transform] function to each element of the original array - * and appends the results to the given [destination]. - */ -public inline fun > DoubleArray.mapTo(destination: C, transform: (Double) -> R): C { - for (item in this) - destination.add(transform(item)) - return destination -} - -/** - * Applies the given [transform] function to each element of the original array - * and appends the results to the given [destination]. - */ -public inline fun > BooleanArray.mapTo(destination: C, transform: (Boolean) -> R): C { - for (item in this) - destination.add(transform(item)) - return destination -} - -/** - * Applies the given [transform] function to each element of the original array - * and appends the results to the given [destination]. - */ -public inline fun > CharArray.mapTo(destination: C, transform: (Char) -> R): C { - for (item in this) - destination.add(transform(item)) - return destination -} - -/** - * Returns a lazy [Iterable] of [IndexedValue] for each element of the original array. - */ -public fun Array.withIndex(): Iterable> { - return IndexingIterable { iterator() } -} - -/** - * Returns a lazy [Iterable] of [IndexedValue] for each element of the original array. - */ -public fun ByteArray.withIndex(): Iterable> { - return IndexingIterable { iterator() } -} - -/** - * Returns a lazy [Iterable] of [IndexedValue] for each element of the original array. - */ -public fun ShortArray.withIndex(): Iterable> { - return IndexingIterable { iterator() } -} - -/** - * Returns a lazy [Iterable] of [IndexedValue] for each element of the original array. - */ -public fun IntArray.withIndex(): Iterable> { - return IndexingIterable { iterator() } -} - -/** - * Returns a lazy [Iterable] of [IndexedValue] for each element of the original array. - */ -public fun LongArray.withIndex(): Iterable> { - return IndexingIterable { iterator() } -} - -/** - * Returns a lazy [Iterable] of [IndexedValue] for each element of the original array. - */ -public fun FloatArray.withIndex(): Iterable> { - return IndexingIterable { iterator() } -} - -/** - * Returns a lazy [Iterable] of [IndexedValue] for each element of the original array. - */ -public fun DoubleArray.withIndex(): Iterable> { - return IndexingIterable { iterator() } -} - -/** - * Returns a lazy [Iterable] of [IndexedValue] for each element of the original array. - */ -public fun BooleanArray.withIndex(): Iterable> { - return IndexingIterable { iterator() } -} - -/** - * Returns a lazy [Iterable] of [IndexedValue] for each element of the original array. - */ -public fun CharArray.withIndex(): Iterable> { - return IndexingIterable { iterator() } -} - -/** - * Returns a list containing only distinct elements from the given array. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public fun Array.distinct(): List { - return this.toMutableSet().toList() -} - -/** - * Returns a list containing only distinct elements from the given array. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public fun ByteArray.distinct(): List { - return this.toMutableSet().toList() -} - -/** - * Returns a list containing only distinct elements from the given array. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public fun ShortArray.distinct(): List { - return this.toMutableSet().toList() -} - -/** - * Returns a list containing only distinct elements from the given array. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public fun IntArray.distinct(): List { - return this.toMutableSet().toList() -} - -/** - * Returns a list containing only distinct elements from the given array. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public fun LongArray.distinct(): List { - return this.toMutableSet().toList() -} - -/** - * Returns a list containing only distinct elements from the given array. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public fun FloatArray.distinct(): List { - return this.toMutableSet().toList() -} - -/** - * Returns a list containing only distinct elements from the given array. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public fun DoubleArray.distinct(): List { - return this.toMutableSet().toList() -} - -/** - * Returns a list containing only distinct elements from the given array. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public fun BooleanArray.distinct(): List { - return this.toMutableSet().toList() -} - -/** - * Returns a list containing only distinct elements from the given array. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public fun CharArray.distinct(): List { - return this.toMutableSet().toList() -} - -/** - * Returns a list containing only elements from the given array - * having distinct keys returned by the given [selector] function. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public inline fun Array.distinctBy(selector: (T) -> K): List { - val set = HashSet() - val list = ArrayList() - for (e in this) { - val key = selector(e) - if (set.add(key)) - list.add(e) - } - return list -} - -/** - * Returns a list containing only elements from the given array - * having distinct keys returned by the given [selector] function. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public inline fun ByteArray.distinctBy(selector: (Byte) -> K): List { - val set = HashSet() - val list = ArrayList() - for (e in this) { - val key = selector(e) - if (set.add(key)) - list.add(e) - } - return list -} - -/** - * Returns a list containing only elements from the given array - * having distinct keys returned by the given [selector] function. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public inline fun ShortArray.distinctBy(selector: (Short) -> K): List { - val set = HashSet() - val list = ArrayList() - for (e in this) { - val key = selector(e) - if (set.add(key)) - list.add(e) - } - return list -} - -/** - * Returns a list containing only elements from the given array - * having distinct keys returned by the given [selector] function. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public inline fun IntArray.distinctBy(selector: (Int) -> K): List { - val set = HashSet() - val list = ArrayList() - for (e in this) { - val key = selector(e) - if (set.add(key)) - list.add(e) - } - return list -} - -/** - * Returns a list containing only elements from the given array - * having distinct keys returned by the given [selector] function. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public inline fun LongArray.distinctBy(selector: (Long) -> K): List { - val set = HashSet() - val list = ArrayList() - for (e in this) { - val key = selector(e) - if (set.add(key)) - list.add(e) - } - return list -} - -/** - * Returns a list containing only elements from the given array - * having distinct keys returned by the given [selector] function. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public inline fun FloatArray.distinctBy(selector: (Float) -> K): List { - val set = HashSet() - val list = ArrayList() - for (e in this) { - val key = selector(e) - if (set.add(key)) - list.add(e) - } - return list -} - -/** - * Returns a list containing only elements from the given array - * having distinct keys returned by the given [selector] function. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public inline fun DoubleArray.distinctBy(selector: (Double) -> K): List { - val set = HashSet() - val list = ArrayList() - for (e in this) { - val key = selector(e) - if (set.add(key)) - list.add(e) - } - return list -} - -/** - * Returns a list containing only elements from the given array - * having distinct keys returned by the given [selector] function. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public inline fun BooleanArray.distinctBy(selector: (Boolean) -> K): List { - val set = HashSet() - val list = ArrayList() - for (e in this) { - val key = selector(e) - if (set.add(key)) - list.add(e) - } - return list -} - -/** - * Returns a list containing only elements from the given array - * having distinct keys returned by the given [selector] function. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public inline fun CharArray.distinctBy(selector: (Char) -> K): List { - val set = HashSet() - val list = ArrayList() - for (e in this) { - val key = selector(e) - if (set.add(key)) - list.add(e) - } - return list -} - -/** - * Returns a set containing all elements that are contained by both this set and the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun Array.intersect(other: Iterable): Set { - val set = this.toMutableSet() - set.retainAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by both this set and the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun ByteArray.intersect(other: Iterable): Set { - val set = this.toMutableSet() - set.retainAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by both this set and the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun ShortArray.intersect(other: Iterable): Set { - val set = this.toMutableSet() - set.retainAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by both this set and the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun IntArray.intersect(other: Iterable): Set { - val set = this.toMutableSet() - set.retainAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by both this set and the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun LongArray.intersect(other: Iterable): Set { - val set = this.toMutableSet() - set.retainAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by both this set and the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun FloatArray.intersect(other: Iterable): Set { - val set = this.toMutableSet() - set.retainAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by both this set and the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun DoubleArray.intersect(other: Iterable): Set { - val set = this.toMutableSet() - set.retainAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by both this set and the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun BooleanArray.intersect(other: Iterable): Set { - val set = this.toMutableSet() - set.retainAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by both this set and the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun CharArray.intersect(other: Iterable): Set { - val set = this.toMutableSet() - set.retainAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by this array and not contained by the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun Array.subtract(other: Iterable): Set { - val set = this.toMutableSet() - set.removeAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by this array and not contained by the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun ByteArray.subtract(other: Iterable): Set { - val set = this.toMutableSet() - set.removeAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by this array and not contained by the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun ShortArray.subtract(other: Iterable): Set { - val set = this.toMutableSet() - set.removeAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by this array and not contained by the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun IntArray.subtract(other: Iterable): Set { - val set = this.toMutableSet() - set.removeAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by this array and not contained by the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun LongArray.subtract(other: Iterable): Set { - val set = this.toMutableSet() - set.removeAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by this array and not contained by the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun FloatArray.subtract(other: Iterable): Set { - val set = this.toMutableSet() - set.removeAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by this array and not contained by the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun DoubleArray.subtract(other: Iterable): Set { - val set = this.toMutableSet() - set.removeAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by this array and not contained by the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun BooleanArray.subtract(other: Iterable): Set { - val set = this.toMutableSet() - set.removeAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by this array and not contained by the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun CharArray.subtract(other: Iterable): Set { - val set = this.toMutableSet() - set.removeAll(other) - return set -} - -/** - * Returns a mutable set containing all distinct elements from the given array. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun Array.toMutableSet(): MutableSet { - val set = LinkedHashSet(mapCapacity(size)) - for (item in this) set.add(item) - return set -} - -/** - * Returns a mutable set containing all distinct elements from the given array. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun ByteArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(mapCapacity(size)) - for (item in this) set.add(item) - return set -} - -/** - * Returns a mutable set containing all distinct elements from the given array. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun ShortArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(mapCapacity(size)) - for (item in this) set.add(item) - return set -} - -/** - * Returns a mutable set containing all distinct elements from the given array. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun IntArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(mapCapacity(size)) - for (item in this) set.add(item) - return set -} - -/** - * Returns a mutable set containing all distinct elements from the given array. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun LongArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(mapCapacity(size)) - for (item in this) set.add(item) - return set -} - -/** - * Returns a mutable set containing all distinct elements from the given array. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun FloatArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(mapCapacity(size)) - for (item in this) set.add(item) - return set -} - -/** - * Returns a mutable set containing all distinct elements from the given array. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun DoubleArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(mapCapacity(size)) - for (item in this) set.add(item) - return set -} - -/** - * Returns a mutable set containing all distinct elements from the given array. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun BooleanArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(mapCapacity(size)) - for (item in this) set.add(item) - return set -} - -/** - * Returns a mutable set containing all distinct elements from the given array. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun CharArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(mapCapacity(size)) - for (item in this) set.add(item) - return set -} - -/** - * Returns a set containing all distinct elements from both collections. - * - * The returned set preserves the element iteration order of the original array. - * Those elements of the [other] collection that are unique are iterated in the end - * in the order of the [other] collection. - */ -public infix fun Array.union(other: Iterable): Set { - val set = this.toMutableSet() - set.addAll(other) - return set -} - -/** - * Returns a set containing all distinct elements from both collections. - * - * The returned set preserves the element iteration order of the original array. - * Those elements of the [other] collection that are unique are iterated in the end - * in the order of the [other] collection. - */ -public infix fun ByteArray.union(other: Iterable): Set { - val set = this.toMutableSet() - set.addAll(other) - return set -} - -/** - * Returns a set containing all distinct elements from both collections. - * - * The returned set preserves the element iteration order of the original array. - * Those elements of the [other] collection that are unique are iterated in the end - * in the order of the [other] collection. - */ -public infix fun ShortArray.union(other: Iterable): Set { - val set = this.toMutableSet() - set.addAll(other) - return set -} - -/** - * Returns a set containing all distinct elements from both collections. - * - * The returned set preserves the element iteration order of the original array. - * Those elements of the [other] collection that are unique are iterated in the end - * in the order of the [other] collection. - */ -public infix fun IntArray.union(other: Iterable): Set { - val set = this.toMutableSet() - set.addAll(other) - return set -} - -/** - * Returns a set containing all distinct elements from both collections. - * - * The returned set preserves the element iteration order of the original array. - * Those elements of the [other] collection that are unique are iterated in the end - * in the order of the [other] collection. - */ -public infix fun LongArray.union(other: Iterable): Set { - val set = this.toMutableSet() - set.addAll(other) - return set -} - -/** - * Returns a set containing all distinct elements from both collections. - * - * The returned set preserves the element iteration order of the original array. - * Those elements of the [other] collection that are unique are iterated in the end - * in the order of the [other] collection. - */ -public infix fun FloatArray.union(other: Iterable): Set { - val set = this.toMutableSet() - set.addAll(other) - return set -} - -/** - * Returns a set containing all distinct elements from both collections. - * - * The returned set preserves the element iteration order of the original array. - * Those elements of the [other] collection that are unique are iterated in the end - * in the order of the [other] collection. - */ -public infix fun DoubleArray.union(other: Iterable): Set { - val set = this.toMutableSet() - set.addAll(other) - return set -} - -/** - * Returns a set containing all distinct elements from both collections. - * - * The returned set preserves the element iteration order of the original array. - * Those elements of the [other] collection that are unique are iterated in the end - * in the order of the [other] collection. - */ -public infix fun BooleanArray.union(other: Iterable): Set { - val set = this.toMutableSet() - set.addAll(other) - return set -} - -/** - * Returns a set containing all distinct elements from both collections. - * - * The returned set preserves the element iteration order of the original array. - * Those elements of the [other] collection that are unique are iterated in the end - * in the order of the [other] collection. - */ -public infix fun CharArray.union(other: Iterable): Set { - val set = this.toMutableSet() - set.addAll(other) - return set -} - -/** - * Returns `true` if all elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.all - */ -public inline fun Array.all(predicate: (T) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if all elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.all - */ -public inline fun ByteArray.all(predicate: (Byte) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if all elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.all - */ -public inline fun ShortArray.all(predicate: (Short) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if all elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.all - */ -public inline fun IntArray.all(predicate: (Int) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if all elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.all - */ -public inline fun LongArray.all(predicate: (Long) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if all elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.all - */ -public inline fun FloatArray.all(predicate: (Float) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if all elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.all - */ -public inline fun DoubleArray.all(predicate: (Double) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if all elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.all - */ -public inline fun BooleanArray.all(predicate: (Boolean) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if all elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.all - */ -public inline fun CharArray.all(predicate: (Char) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if array has at least one element. - * - * @sample samples.collections.Collections.Aggregates.any - */ -public fun Array.any(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if array has at least one element. - * - * @sample samples.collections.Collections.Aggregates.any - */ -public fun ByteArray.any(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if array has at least one element. - * - * @sample samples.collections.Collections.Aggregates.any - */ -public fun ShortArray.any(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if array has at least one element. - * - * @sample samples.collections.Collections.Aggregates.any - */ -public fun IntArray.any(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if array has at least one element. - * - * @sample samples.collections.Collections.Aggregates.any - */ -public fun LongArray.any(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if array has at least one element. - * - * @sample samples.collections.Collections.Aggregates.any - */ -public fun FloatArray.any(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if array has at least one element. - * - * @sample samples.collections.Collections.Aggregates.any - */ -public fun DoubleArray.any(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if array has at least one element. - * - * @sample samples.collections.Collections.Aggregates.any - */ -public fun BooleanArray.any(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if array has at least one element. - * - * @sample samples.collections.Collections.Aggregates.any - */ -public fun CharArray.any(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if at least one element matches the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.anyWithPredicate - */ -public inline fun Array.any(predicate: (T) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return true - return false -} - -/** - * Returns `true` if at least one element matches the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.anyWithPredicate - */ -public inline fun ByteArray.any(predicate: (Byte) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return true - return false -} - -/** - * Returns `true` if at least one element matches the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.anyWithPredicate - */ -public inline fun ShortArray.any(predicate: (Short) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return true - return false -} - -/** - * Returns `true` if at least one element matches the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.anyWithPredicate - */ -public inline fun IntArray.any(predicate: (Int) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return true - return false -} - -/** - * Returns `true` if at least one element matches the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.anyWithPredicate - */ -public inline fun LongArray.any(predicate: (Long) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return true - return false -} - -/** - * Returns `true` if at least one element matches the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.anyWithPredicate - */ -public inline fun FloatArray.any(predicate: (Float) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return true - return false -} - -/** - * Returns `true` if at least one element matches the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.anyWithPredicate - */ -public inline fun DoubleArray.any(predicate: (Double) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return true - return false -} - -/** - * Returns `true` if at least one element matches the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.anyWithPredicate - */ -public inline fun BooleanArray.any(predicate: (Boolean) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return true - return false -} - -/** - * Returns `true` if at least one element matches the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.anyWithPredicate - */ -public inline fun CharArray.any(predicate: (Char) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return true - return false -} - -/** - * Returns the number of elements in this array. - */ -@kotlin.internal.InlineOnly -public inline fun Array.count(): Int { - return size -} - -/** - * Returns the number of elements in this array. - */ -@kotlin.internal.InlineOnly -public inline fun ByteArray.count(): Int { - return size -} - -/** - * Returns the number of elements in this array. - */ -@kotlin.internal.InlineOnly -public inline fun ShortArray.count(): Int { - return size -} - -/** - * Returns the number of elements in this array. - */ -@kotlin.internal.InlineOnly -public inline fun IntArray.count(): Int { - return size -} - -/** - * Returns the number of elements in this array. - */ -@kotlin.internal.InlineOnly -public inline fun LongArray.count(): Int { - return size -} - -/** - * Returns the number of elements in this array. - */ -@kotlin.internal.InlineOnly -public inline fun FloatArray.count(): Int { - return size -} - -/** - * Returns the number of elements in this array. - */ -@kotlin.internal.InlineOnly -public inline fun DoubleArray.count(): Int { - return size -} - -/** - * Returns the number of elements in this array. - */ -@kotlin.internal.InlineOnly -public inline fun BooleanArray.count(): Int { - return size -} - -/** - * Returns the number of elements in this array. - */ -@kotlin.internal.InlineOnly -public inline fun CharArray.count(): Int { - return size -} - -/** - * Returns the number of elements matching the given [predicate]. - */ -public inline fun Array.count(predicate: (T) -> Boolean): Int { - var count = 0 - for (element in this) if (predicate(element)) count++ - return count -} - -/** - * Returns the number of elements matching the given [predicate]. - */ -public inline fun ByteArray.count(predicate: (Byte) -> Boolean): Int { - var count = 0 - for (element in this) if (predicate(element)) count++ - return count -} - -/** - * Returns the number of elements matching the given [predicate]. - */ -public inline fun ShortArray.count(predicate: (Short) -> Boolean): Int { - var count = 0 - for (element in this) if (predicate(element)) count++ - return count -} - -/** - * Returns the number of elements matching the given [predicate]. - */ -public inline fun IntArray.count(predicate: (Int) -> Boolean): Int { - var count = 0 - for (element in this) if (predicate(element)) count++ - return count -} - -/** - * Returns the number of elements matching the given [predicate]. - */ -public inline fun LongArray.count(predicate: (Long) -> Boolean): Int { - var count = 0 - for (element in this) if (predicate(element)) count++ - return count -} - -/** - * Returns the number of elements matching the given [predicate]. - */ -public inline fun FloatArray.count(predicate: (Float) -> Boolean): Int { - var count = 0 - for (element in this) if (predicate(element)) count++ - return count -} - -/** - * Returns the number of elements matching the given [predicate]. - */ -public inline fun DoubleArray.count(predicate: (Double) -> Boolean): Int { - var count = 0 - for (element in this) if (predicate(element)) count++ - return count -} - -/** - * Returns the number of elements matching the given [predicate]. - */ -public inline fun BooleanArray.count(predicate: (Boolean) -> Boolean): Int { - var count = 0 - for (element in this) if (predicate(element)) count++ - return count -} - -/** - * Returns the number of elements matching the given [predicate]. - */ -public inline fun CharArray.count(predicate: (Char) -> Boolean): Int { - var count = 0 - for (element in this) if (predicate(element)) count++ - return count -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun Array.fold(initial: R, operation: (acc: R, T) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun ByteArray.fold(initial: R, operation: (acc: R, Byte) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun ShortArray.fold(initial: R, operation: (acc: R, Short) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun IntArray.fold(initial: R, operation: (acc: R, Int) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun LongArray.fold(initial: R, operation: (acc: R, Long) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun FloatArray.fold(initial: R, operation: (acc: R, Float) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun DoubleArray.fold(initial: R, operation: (acc: R, Double) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun BooleanArray.fold(initial: R, operation: (acc: R, Boolean) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun CharArray.fold(initial: R, operation: (acc: R, Char) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself, and calculates the next accumulator value. - */ -public inline fun Array.foldIndexed(initial: R, operation: (index: Int, acc: R, T) -> R): R { - var index = 0 - var accumulator = initial - for (element in this) accumulator = operation(index++, accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself, and calculates the next accumulator value. - */ -public inline fun ByteArray.foldIndexed(initial: R, operation: (index: Int, acc: R, Byte) -> R): R { - var index = 0 - var accumulator = initial - for (element in this) accumulator = operation(index++, accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself, and calculates the next accumulator value. - */ -public inline fun ShortArray.foldIndexed(initial: R, operation: (index: Int, acc: R, Short) -> R): R { - var index = 0 - var accumulator = initial - for (element in this) accumulator = operation(index++, accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself, and calculates the next accumulator value. - */ -public inline fun IntArray.foldIndexed(initial: R, operation: (index: Int, acc: R, Int) -> R): R { - var index = 0 - var accumulator = initial - for (element in this) accumulator = operation(index++, accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself, and calculates the next accumulator value. - */ -public inline fun LongArray.foldIndexed(initial: R, operation: (index: Int, acc: R, Long) -> R): R { - var index = 0 - var accumulator = initial - for (element in this) accumulator = operation(index++, accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself, and calculates the next accumulator value. - */ -public inline fun FloatArray.foldIndexed(initial: R, operation: (index: Int, acc: R, Float) -> R): R { - var index = 0 - var accumulator = initial - for (element in this) accumulator = operation(index++, accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself, and calculates the next accumulator value. - */ -public inline fun DoubleArray.foldIndexed(initial: R, operation: (index: Int, acc: R, Double) -> R): R { - var index = 0 - var accumulator = initial - for (element in this) accumulator = operation(index++, accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself, and calculates the next accumulator value. - */ -public inline fun BooleanArray.foldIndexed(initial: R, operation: (index: Int, acc: R, Boolean) -> R): R { - var index = 0 - var accumulator = initial - for (element in this) accumulator = operation(index++, accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself, and calculates the next accumulator value. - */ -public inline fun CharArray.foldIndexed(initial: R, operation: (index: Int, acc: R, Char) -> R): R { - var index = 0 - var accumulator = initial - for (element in this) accumulator = operation(index++, accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun Array.foldRight(initial: R, operation: (T, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun ByteArray.foldRight(initial: R, operation: (Byte, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun ShortArray.foldRight(initial: R, operation: (Short, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun IntArray.foldRight(initial: R, operation: (Int, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun LongArray.foldRight(initial: R, operation: (Long, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun FloatArray.foldRight(initial: R, operation: (Float, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun DoubleArray.foldRight(initial: R, operation: (Double, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun BooleanArray.foldRight(initial: R, operation: (Boolean, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun CharArray.foldRight(initial: R, operation: (Char, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun Array.foldRightIndexed(initial: R, operation: (index: Int, T, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun ByteArray.foldRightIndexed(initial: R, operation: (index: Int, Byte, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun ShortArray.foldRightIndexed(initial: R, operation: (index: Int, Short, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun IntArray.foldRightIndexed(initial: R, operation: (index: Int, Int, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun LongArray.foldRightIndexed(initial: R, operation: (index: Int, Long, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun FloatArray.foldRightIndexed(initial: R, operation: (index: Int, Float, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun DoubleArray.foldRightIndexed(initial: R, operation: (index: Int, Double, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun BooleanArray.foldRightIndexed(initial: R, operation: (index: Int, Boolean, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun CharArray.foldRightIndexed(initial: R, operation: (index: Int, Char, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Performs the given [action] on each element. - */ -public inline fun Array.forEach(action: (T) -> Unit): Unit { - for (element in this) action(element) -} - -/** - * Performs the given [action] on each element. - */ -public inline fun ByteArray.forEach(action: (Byte) -> Unit): Unit { - for (element in this) action(element) -} - -/** - * Performs the given [action] on each element. - */ -public inline fun ShortArray.forEach(action: (Short) -> Unit): Unit { - for (element in this) action(element) -} - -/** - * Performs the given [action] on each element. - */ -public inline fun IntArray.forEach(action: (Int) -> Unit): Unit { - for (element in this) action(element) -} - -/** - * Performs the given [action] on each element. - */ -public inline fun LongArray.forEach(action: (Long) -> Unit): Unit { - for (element in this) action(element) -} - -/** - * Performs the given [action] on each element. - */ -public inline fun FloatArray.forEach(action: (Float) -> Unit): Unit { - for (element in this) action(element) -} - -/** - * Performs the given [action] on each element. - */ -public inline fun DoubleArray.forEach(action: (Double) -> Unit): Unit { - for (element in this) action(element) -} - -/** - * Performs the given [action] on each element. - */ -public inline fun BooleanArray.forEach(action: (Boolean) -> Unit): Unit { - for (element in this) action(element) -} - -/** - * Performs the given [action] on each element. - */ -public inline fun CharArray.forEach(action: (Char) -> Unit): Unit { - for (element in this) action(element) -} - -/** - * Performs the given [action] on each element, providing sequential index with the element. - * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. - */ -public inline fun Array.forEachIndexed(action: (index: Int, T) -> Unit): Unit { - var index = 0 - for (item in this) action(index++, item) -} - -/** - * Performs the given [action] on each element, providing sequential index with the element. - * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. - */ -public inline fun ByteArray.forEachIndexed(action: (index: Int, Byte) -> Unit): Unit { - var index = 0 - for (item in this) action(index++, item) -} - -/** - * Performs the given [action] on each element, providing sequential index with the element. - * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. - */ -public inline fun ShortArray.forEachIndexed(action: (index: Int, Short) -> Unit): Unit { - var index = 0 - for (item in this) action(index++, item) -} - -/** - * Performs the given [action] on each element, providing sequential index with the element. - * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. - */ -public inline fun IntArray.forEachIndexed(action: (index: Int, Int) -> Unit): Unit { - var index = 0 - for (item in this) action(index++, item) -} - -/** - * Performs the given [action] on each element, providing sequential index with the element. - * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. - */ -public inline fun LongArray.forEachIndexed(action: (index: Int, Long) -> Unit): Unit { - var index = 0 - for (item in this) action(index++, item) -} - -/** - * Performs the given [action] on each element, providing sequential index with the element. - * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. - */ -public inline fun FloatArray.forEachIndexed(action: (index: Int, Float) -> Unit): Unit { - var index = 0 - for (item in this) action(index++, item) -} - -/** - * Performs the given [action] on each element, providing sequential index with the element. - * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. - */ -public inline fun DoubleArray.forEachIndexed(action: (index: Int, Double) -> Unit): Unit { - var index = 0 - for (item in this) action(index++, item) -} - -/** - * Performs the given [action] on each element, providing sequential index with the element. - * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. - */ -public inline fun BooleanArray.forEachIndexed(action: (index: Int, Boolean) -> Unit): Unit { - var index = 0 - for (item in this) action(index++, item) -} - -/** - * Performs the given [action] on each element, providing sequential index with the element. - * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. - */ -public inline fun CharArray.forEachIndexed(action: (index: Int, Char) -> Unit): Unit { - var index = 0 - for (item in this) action(index++, item) -} - -/** - * Returns the largest element or `null` if there are no elements. - * - * If any of elements is `NaN` returns `NaN`. - */ -@SinceKotlin("1.1") -public fun Array.max(): Double? { - if (isEmpty()) return null - var max = this[0] - if (max.isNaN()) return max - for (i in 1..lastIndex) { - val e = this[i] - if (e.isNaN()) return e - if (max < e) max = e - } - return max -} - -/** - * Returns the largest element or `null` if there are no elements. - * - * If any of elements is `NaN` returns `NaN`. - */ -@SinceKotlin("1.1") -public fun Array.max(): Float? { - if (isEmpty()) return null - var max = this[0] - if (max.isNaN()) return max - for (i in 1..lastIndex) { - val e = this[i] - if (e.isNaN()) return e - if (max < e) max = e - } - return max -} - -/** - * Returns the largest element or `null` if there are no elements. - */ -public fun > Array.max(): T? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (max < e) max = e - } - return max -} - -/** - * Returns the largest element or `null` if there are no elements. - */ -public fun ByteArray.max(): Byte? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (max < e) max = e - } - return max -} - -/** - * Returns the largest element or `null` if there are no elements. - */ -public fun ShortArray.max(): Short? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (max < e) max = e - } - return max -} - -/** - * Returns the largest element or `null` if there are no elements. - */ -public fun IntArray.max(): Int? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (max < e) max = e - } - return max -} - -/** - * Returns the largest element or `null` if there are no elements. - */ -public fun LongArray.max(): Long? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (max < e) max = e - } - return max -} - -/** - * Returns the largest element or `null` if there are no elements. - * - * If any of elements is `NaN` returns `NaN`. - */ -public fun FloatArray.max(): Float? { - if (isEmpty()) return null - var max = this[0] - if (max.isNaN()) return max - for (i in 1..lastIndex) { - val e = this[i] - if (e.isNaN()) return e - if (max < e) max = e - } - return max -} - -/** - * Returns the largest element or `null` if there are no elements. - * - * If any of elements is `NaN` returns `NaN`. - */ -public fun DoubleArray.max(): Double? { - if (isEmpty()) return null - var max = this[0] - if (max.isNaN()) return max - for (i in 1..lastIndex) { - val e = this[i] - if (e.isNaN()) return e - if (max < e) max = e - } - return max -} - -/** - * Returns the largest element or `null` if there are no elements. - */ -public fun CharArray.max(): Char? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (max < e) max = e - } - return max -} - -/** - * Returns the first element yielding the largest value of the given function or `null` if there are no elements. - */ -public inline fun > Array.maxBy(selector: (T) -> R): T? { - if (isEmpty()) return null - var maxElem = this[0] - var maxValue = selector(maxElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (maxValue < v) { - maxElem = e - maxValue = v - } - } - return maxElem -} - -/** - * Returns the first element yielding the largest value of the given function or `null` if there are no elements. - */ -public inline fun > ByteArray.maxBy(selector: (Byte) -> R): Byte? { - if (isEmpty()) return null - var maxElem = this[0] - var maxValue = selector(maxElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (maxValue < v) { - maxElem = e - maxValue = v - } - } - return maxElem -} - -/** - * Returns the first element yielding the largest value of the given function or `null` if there are no elements. - */ -public inline fun > ShortArray.maxBy(selector: (Short) -> R): Short? { - if (isEmpty()) return null - var maxElem = this[0] - var maxValue = selector(maxElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (maxValue < v) { - maxElem = e - maxValue = v - } - } - return maxElem -} - -/** - * Returns the first element yielding the largest value of the given function or `null` if there are no elements. - */ -public inline fun > IntArray.maxBy(selector: (Int) -> R): Int? { - if (isEmpty()) return null - var maxElem = this[0] - var maxValue = selector(maxElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (maxValue < v) { - maxElem = e - maxValue = v - } - } - return maxElem -} - -/** - * Returns the first element yielding the largest value of the given function or `null` if there are no elements. - */ -public inline fun > LongArray.maxBy(selector: (Long) -> R): Long? { - if (isEmpty()) return null - var maxElem = this[0] - var maxValue = selector(maxElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (maxValue < v) { - maxElem = e - maxValue = v - } - } - return maxElem -} - -/** - * Returns the first element yielding the largest value of the given function or `null` if there are no elements. - */ -public inline fun > FloatArray.maxBy(selector: (Float) -> R): Float? { - if (isEmpty()) return null - var maxElem = this[0] - var maxValue = selector(maxElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (maxValue < v) { - maxElem = e - maxValue = v - } - } - return maxElem -} - -/** - * Returns the first element yielding the largest value of the given function or `null` if there are no elements. - */ -public inline fun > DoubleArray.maxBy(selector: (Double) -> R): Double? { - if (isEmpty()) return null - var maxElem = this[0] - var maxValue = selector(maxElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (maxValue < v) { - maxElem = e - maxValue = v - } - } - return maxElem -} - -/** - * Returns the first element yielding the largest value of the given function or `null` if there are no elements. - */ -public inline fun > BooleanArray.maxBy(selector: (Boolean) -> R): Boolean? { - if (isEmpty()) return null - var maxElem = this[0] - var maxValue = selector(maxElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (maxValue < v) { - maxElem = e - maxValue = v - } - } - return maxElem -} - -/** - * Returns the first element yielding the largest value of the given function or `null` if there are no elements. - */ -public inline fun > CharArray.maxBy(selector: (Char) -> R): Char? { - if (isEmpty()) return null - var maxElem = this[0] - var maxValue = selector(maxElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (maxValue < v) { - maxElem = e - maxValue = v - } - } - return maxElem -} - -/** - * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun Array.maxWith(comparator: Comparator): T? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(max, e) < 0) max = e - } - return max -} - -/** - * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun ByteArray.maxWith(comparator: Comparator): Byte? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(max, e) < 0) max = e - } - return max -} - -/** - * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun ShortArray.maxWith(comparator: Comparator): Short? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(max, e) < 0) max = e - } - return max -} - -/** - * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun IntArray.maxWith(comparator: Comparator): Int? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(max, e) < 0) max = e - } - return max -} - -/** - * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun LongArray.maxWith(comparator: Comparator): Long? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(max, e) < 0) max = e - } - return max -} - -/** - * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun FloatArray.maxWith(comparator: Comparator): Float? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(max, e) < 0) max = e - } - return max -} - -/** - * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun DoubleArray.maxWith(comparator: Comparator): Double? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(max, e) < 0) max = e - } - return max -} - -/** - * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun BooleanArray.maxWith(comparator: Comparator): Boolean? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(max, e) < 0) max = e - } - return max -} - -/** - * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun CharArray.maxWith(comparator: Comparator): Char? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(max, e) < 0) max = e - } - return max -} - -/** - * Returns the smallest element or `null` if there are no elements. - * - * If any of elements is `NaN` returns `NaN`. - */ -@SinceKotlin("1.1") -public fun Array.min(): Double? { - if (isEmpty()) return null - var min = this[0] - if (min.isNaN()) return min - for (i in 1..lastIndex) { - val e = this[i] - if (e.isNaN()) return e - if (min > e) min = e - } - return min -} - -/** - * Returns the smallest element or `null` if there are no elements. - * - * If any of elements is `NaN` returns `NaN`. - */ -@SinceKotlin("1.1") -public fun Array.min(): Float? { - if (isEmpty()) return null - var min = this[0] - if (min.isNaN()) return min - for (i in 1..lastIndex) { - val e = this[i] - if (e.isNaN()) return e - if (min > e) min = e - } - return min -} - -/** - * Returns the smallest element or `null` if there are no elements. - */ -public fun > Array.min(): T? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (min > e) min = e - } - return min -} - -/** - * Returns the smallest element or `null` if there are no elements. - */ -public fun ByteArray.min(): Byte? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (min > e) min = e - } - return min -} - -/** - * Returns the smallest element or `null` if there are no elements. - */ -public fun ShortArray.min(): Short? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (min > e) min = e - } - return min -} - -/** - * Returns the smallest element or `null` if there are no elements. - */ -public fun IntArray.min(): Int? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (min > e) min = e - } - return min -} - -/** - * Returns the smallest element or `null` if there are no elements. - */ -public fun LongArray.min(): Long? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (min > e) min = e - } - return min -} - -/** - * Returns the smallest element or `null` if there are no elements. - * - * If any of elements is `NaN` returns `NaN`. - */ -public fun FloatArray.min(): Float? { - if (isEmpty()) return null - var min = this[0] - if (min.isNaN()) return min - for (i in 1..lastIndex) { - val e = this[i] - if (e.isNaN()) return e - if (min > e) min = e - } - return min -} - -/** - * Returns the smallest element or `null` if there are no elements. - * - * If any of elements is `NaN` returns `NaN`. - */ -public fun DoubleArray.min(): Double? { - if (isEmpty()) return null - var min = this[0] - if (min.isNaN()) return min - for (i in 1..lastIndex) { - val e = this[i] - if (e.isNaN()) return e - if (min > e) min = e - } - return min -} - -/** - * Returns the smallest element or `null` if there are no elements. - */ -public fun CharArray.min(): Char? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (min > e) min = e - } - return min -} - -/** - * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. - */ -public inline fun > Array.minBy(selector: (T) -> R): T? { - if (isEmpty()) return null - var minElem = this[0] - var minValue = selector(minElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (minValue > v) { - minElem = e - minValue = v - } - } - return minElem -} - -/** - * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. - */ -public inline fun > ByteArray.minBy(selector: (Byte) -> R): Byte? { - if (isEmpty()) return null - var minElem = this[0] - var minValue = selector(minElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (minValue > v) { - minElem = e - minValue = v - } - } - return minElem -} - -/** - * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. - */ -public inline fun > ShortArray.minBy(selector: (Short) -> R): Short? { - if (isEmpty()) return null - var minElem = this[0] - var minValue = selector(minElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (minValue > v) { - minElem = e - minValue = v - } - } - return minElem -} - -/** - * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. - */ -public inline fun > IntArray.minBy(selector: (Int) -> R): Int? { - if (isEmpty()) return null - var minElem = this[0] - var minValue = selector(minElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (minValue > v) { - minElem = e - minValue = v - } - } - return minElem -} - -/** - * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. - */ -public inline fun > LongArray.minBy(selector: (Long) -> R): Long? { - if (isEmpty()) return null - var minElem = this[0] - var minValue = selector(minElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (minValue > v) { - minElem = e - minValue = v - } - } - return minElem -} - -/** - * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. - */ -public inline fun > FloatArray.minBy(selector: (Float) -> R): Float? { - if (isEmpty()) return null - var minElem = this[0] - var minValue = selector(minElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (minValue > v) { - minElem = e - minValue = v - } - } - return minElem -} - -/** - * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. - */ -public inline fun > DoubleArray.minBy(selector: (Double) -> R): Double? { - if (isEmpty()) return null - var minElem = this[0] - var minValue = selector(minElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (minValue > v) { - minElem = e - minValue = v - } - } - return minElem -} - -/** - * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. - */ -public inline fun > BooleanArray.minBy(selector: (Boolean) -> R): Boolean? { - if (isEmpty()) return null - var minElem = this[0] - var minValue = selector(minElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (minValue > v) { - minElem = e - minValue = v - } - } - return minElem -} - -/** - * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. - */ -public inline fun > CharArray.minBy(selector: (Char) -> R): Char? { - if (isEmpty()) return null - var minElem = this[0] - var minValue = selector(minElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (minValue > v) { - minElem = e - minValue = v - } - } - return minElem -} - -/** - * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun Array.minWith(comparator: Comparator): T? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(min, e) > 0) min = e - } - return min -} - -/** - * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun ByteArray.minWith(comparator: Comparator): Byte? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(min, e) > 0) min = e - } - return min -} - -/** - * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun ShortArray.minWith(comparator: Comparator): Short? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(min, e) > 0) min = e - } - return min -} - -/** - * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun IntArray.minWith(comparator: Comparator): Int? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(min, e) > 0) min = e - } - return min -} - -/** - * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun LongArray.minWith(comparator: Comparator): Long? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(min, e) > 0) min = e - } - return min -} - -/** - * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun FloatArray.minWith(comparator: Comparator): Float? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(min, e) > 0) min = e - } - return min -} - -/** - * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun DoubleArray.minWith(comparator: Comparator): Double? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(min, e) > 0) min = e - } - return min -} - -/** - * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun BooleanArray.minWith(comparator: Comparator): Boolean? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(min, e) > 0) min = e - } - return min -} - -/** - * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun CharArray.minWith(comparator: Comparator): Char? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(min, e) > 0) min = e - } - return min -} - -/** - * Returns `true` if the array has no elements. - * - * @sample samples.collections.Collections.Aggregates.none - */ -public fun Array.none(): Boolean { - return isEmpty() -} - -/** - * Returns `true` if the array has no elements. - * - * @sample samples.collections.Collections.Aggregates.none - */ -public fun ByteArray.none(): Boolean { - return isEmpty() -} - -/** - * Returns `true` if the array has no elements. - * - * @sample samples.collections.Collections.Aggregates.none - */ -public fun ShortArray.none(): Boolean { - return isEmpty() -} - -/** - * Returns `true` if the array has no elements. - * - * @sample samples.collections.Collections.Aggregates.none - */ -public fun IntArray.none(): Boolean { - return isEmpty() -} - -/** - * Returns `true` if the array has no elements. - * - * @sample samples.collections.Collections.Aggregates.none - */ -public fun LongArray.none(): Boolean { - return isEmpty() -} - -/** - * Returns `true` if the array has no elements. - * - * @sample samples.collections.Collections.Aggregates.none - */ -public fun FloatArray.none(): Boolean { - return isEmpty() -} - -/** - * Returns `true` if the array has no elements. - * - * @sample samples.collections.Collections.Aggregates.none - */ -public fun DoubleArray.none(): Boolean { - return isEmpty() -} - -/** - * Returns `true` if the array has no elements. - * - * @sample samples.collections.Collections.Aggregates.none - */ -public fun BooleanArray.none(): Boolean { - return isEmpty() -} - -/** - * Returns `true` if the array has no elements. - * - * @sample samples.collections.Collections.Aggregates.none - */ -public fun CharArray.none(): Boolean { - return isEmpty() -} - -/** - * Returns `true` if no elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.noneWithPredicate - */ -public inline fun Array.none(predicate: (T) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return false - return true -} - -/** - * Returns `true` if no elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.noneWithPredicate - */ -public inline fun ByteArray.none(predicate: (Byte) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return false - return true -} - -/** - * Returns `true` if no elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.noneWithPredicate - */ -public inline fun ShortArray.none(predicate: (Short) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return false - return true -} - -/** - * Returns `true` if no elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.noneWithPredicate - */ -public inline fun IntArray.none(predicate: (Int) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return false - return true -} - -/** - * Returns `true` if no elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.noneWithPredicate - */ -public inline fun LongArray.none(predicate: (Long) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return false - return true -} - -/** - * Returns `true` if no elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.noneWithPredicate - */ -public inline fun FloatArray.none(predicate: (Float) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return false - return true -} - -/** - * Returns `true` if no elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.noneWithPredicate - */ -public inline fun DoubleArray.none(predicate: (Double) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return false - return true -} - -/** - * Returns `true` if no elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.noneWithPredicate - */ -public inline fun BooleanArray.none(predicate: (Boolean) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return false - return true -} - -/** - * Returns `true` if no elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.noneWithPredicate - */ -public inline fun CharArray.none(predicate: (Char) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return false - return true -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun Array.reduce(operation: (acc: S, T) -> S): S { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator: S = this[0] - for (index in 1..lastIndex) { - accumulator = operation(accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun ByteArray.reduce(operation: (acc: Byte, Byte) -> Byte): Byte { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun ShortArray.reduce(operation: (acc: Short, Short) -> Short): Short { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun IntArray.reduce(operation: (acc: Int, Int) -> Int): Int { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun LongArray.reduce(operation: (acc: Long, Long) -> Long): Long { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun FloatArray.reduce(operation: (acc: Float, Float) -> Float): Float { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun DoubleArray.reduce(operation: (acc: Double, Double) -> Double): Double { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun BooleanArray.reduce(operation: (acc: Boolean, Boolean) -> Boolean): Boolean { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun CharArray.reduce(operation: (acc: Char, Char) -> Char): Char { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself and calculates the next accumulator value. - */ -public inline fun Array.reduceIndexed(operation: (index: Int, acc: S, T) -> S): S { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator: S = this[0] - for (index in 1..lastIndex) { - accumulator = operation(index, accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself and calculates the next accumulator value. - */ -public inline fun ByteArray.reduceIndexed(operation: (index: Int, acc: Byte, Byte) -> Byte): Byte { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(index, accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself and calculates the next accumulator value. - */ -public inline fun ShortArray.reduceIndexed(operation: (index: Int, acc: Short, Short) -> Short): Short { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(index, accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself and calculates the next accumulator value. - */ -public inline fun IntArray.reduceIndexed(operation: (index: Int, acc: Int, Int) -> Int): Int { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(index, accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself and calculates the next accumulator value. - */ -public inline fun LongArray.reduceIndexed(operation: (index: Int, acc: Long, Long) -> Long): Long { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(index, accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself and calculates the next accumulator value. - */ -public inline fun FloatArray.reduceIndexed(operation: (index: Int, acc: Float, Float) -> Float): Float { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(index, accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself and calculates the next accumulator value. - */ -public inline fun DoubleArray.reduceIndexed(operation: (index: Int, acc: Double, Double) -> Double): Double { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(index, accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself and calculates the next accumulator value. - */ -public inline fun BooleanArray.reduceIndexed(operation: (index: Int, acc: Boolean, Boolean) -> Boolean): Boolean { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(index, accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself and calculates the next accumulator value. - */ -public inline fun CharArray.reduceIndexed(operation: (index: Int, acc: Char, Char) -> Char): Char { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(index, accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun Array.reduceRight(operation: (T, acc: S) -> S): S { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator: S = get(index--) - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun ByteArray.reduceRight(operation: (Byte, acc: Byte) -> Byte): Byte { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun ShortArray.reduceRight(operation: (Short, acc: Short) -> Short): Short { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun IntArray.reduceRight(operation: (Int, acc: Int) -> Int): Int { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun LongArray.reduceRight(operation: (Long, acc: Long) -> Long): Long { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun FloatArray.reduceRight(operation: (Float, acc: Float) -> Float): Float { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun DoubleArray.reduceRight(operation: (Double, acc: Double) -> Double): Double { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun BooleanArray.reduceRight(operation: (Boolean, acc: Boolean) -> Boolean): Boolean { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun CharArray.reduceRight(operation: (Char, acc: Char) -> Char): Char { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun Array.reduceRightIndexed(operation: (index: Int, T, acc: S) -> S): S { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator: S = get(index--) - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun ByteArray.reduceRightIndexed(operation: (index: Int, Byte, acc: Byte) -> Byte): Byte { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun ShortArray.reduceRightIndexed(operation: (index: Int, Short, acc: Short) -> Short): Short { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun IntArray.reduceRightIndexed(operation: (index: Int, Int, acc: Int) -> Int): Int { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun LongArray.reduceRightIndexed(operation: (index: Int, Long, acc: Long) -> Long): Long { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun FloatArray.reduceRightIndexed(operation: (index: Int, Float, acc: Float) -> Float): Float { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun DoubleArray.reduceRightIndexed(operation: (index: Int, Double, acc: Double) -> Double): Double { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun BooleanArray.reduceRightIndexed(operation: (index: Int, Boolean, acc: Boolean) -> Boolean): Boolean { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun CharArray.reduceRightIndexed(operation: (index: Int, Char, acc: Char) -> Char): Char { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun Array.sumBy(selector: (T) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun ByteArray.sumBy(selector: (Byte) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun ShortArray.sumBy(selector: (Short) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun IntArray.sumBy(selector: (Int) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun LongArray.sumBy(selector: (Long) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun FloatArray.sumBy(selector: (Float) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun DoubleArray.sumBy(selector: (Double) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun BooleanArray.sumBy(selector: (Boolean) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun CharArray.sumBy(selector: (Char) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun Array.sumByDouble(selector: (T) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun ByteArray.sumByDouble(selector: (Byte) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun ShortArray.sumByDouble(selector: (Short) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun IntArray.sumByDouble(selector: (Int) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun LongArray.sumByDouble(selector: (Long) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun FloatArray.sumByDouble(selector: (Float) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun DoubleArray.sumByDouble(selector: (Double) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun BooleanArray.sumByDouble(selector: (Boolean) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun CharArray.sumByDouble(selector: (Char) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns an original collection containing all the non-`null` elements, throwing an [IllegalArgumentException] if there are any `null` elements. - */ -public fun Array.requireNoNulls(): Array { - for (element in this) { - if (element == null) { - throw IllegalArgumentException("null element found in $this.") - } - } - @Suppress("UNCHECKED_CAST") - return this as Array -} - -/** - * Splits the original array into pair of lists, - * where *first* list contains elements for which [predicate] yielded `true`, - * while *second* list contains elements for which [predicate] yielded `false`. - */ -public inline fun Array.partition(predicate: (T) -> Boolean): Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Splits the original array into pair of lists, - * where *first* list contains elements for which [predicate] yielded `true`, - * while *second* list contains elements for which [predicate] yielded `false`. - */ -public inline fun ByteArray.partition(predicate: (Byte) -> Boolean): Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Splits the original array into pair of lists, - * where *first* list contains elements for which [predicate] yielded `true`, - * while *second* list contains elements for which [predicate] yielded `false`. - */ -public inline fun ShortArray.partition(predicate: (Short) -> Boolean): Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Splits the original array into pair of lists, - * where *first* list contains elements for which [predicate] yielded `true`, - * while *second* list contains elements for which [predicate] yielded `false`. - */ -public inline fun IntArray.partition(predicate: (Int) -> Boolean): Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Splits the original array into pair of lists, - * where *first* list contains elements for which [predicate] yielded `true`, - * while *second* list contains elements for which [predicate] yielded `false`. - */ -public inline fun LongArray.partition(predicate: (Long) -> Boolean): Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Splits the original array into pair of lists, - * where *first* list contains elements for which [predicate] yielded `true`, - * while *second* list contains elements for which [predicate] yielded `false`. - */ -public inline fun FloatArray.partition(predicate: (Float) -> Boolean): Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Splits the original array into pair of lists, - * where *first* list contains elements for which [predicate] yielded `true`, - * while *second* list contains elements for which [predicate] yielded `false`. - */ -public inline fun DoubleArray.partition(predicate: (Double) -> Boolean): Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Splits the original array into pair of lists, - * where *first* list contains elements for which [predicate] yielded `true`, - * while *second* list contains elements for which [predicate] yielded `false`. - */ -public inline fun BooleanArray.partition(predicate: (Boolean) -> Boolean): Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Splits the original array into pair of lists, - * where *first* list contains elements for which [predicate] yielded `true`, - * while *second* list contains elements for which [predicate] yielded `false`. - */ -public inline fun CharArray.partition(predicate: (Char) -> Boolean): Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun Array.zip(other: Array): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun ByteArray.zip(other: Array): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun ShortArray.zip(other: Array): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun IntArray.zip(other: Array): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun LongArray.zip(other: Array): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun FloatArray.zip(other: Array): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun DoubleArray.zip(other: Array): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun BooleanArray.zip(other: Array): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun CharArray.zip(other: Array): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun Array.zip(other: Array, transform: (a: T, b: R) -> V): List { - val size = minOf(size, other.size) - val list = ArrayList(size) - for (i in 0 until size) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun ByteArray.zip(other: Array, transform: (a: Byte, b: R) -> V): List { - val size = minOf(size, other.size) - val list = ArrayList(size) - for (i in 0 until size) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun ShortArray.zip(other: Array, transform: (a: Short, b: R) -> V): List { - val size = minOf(size, other.size) - val list = ArrayList(size) - for (i in 0 until size) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun IntArray.zip(other: Array, transform: (a: Int, b: R) -> V): List { - val size = minOf(size, other.size) - val list = ArrayList(size) - for (i in 0 until size) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun LongArray.zip(other: Array, transform: (a: Long, b: R) -> V): List { - val size = minOf(size, other.size) - val list = ArrayList(size) - for (i in 0 until size) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun FloatArray.zip(other: Array, transform: (a: Float, b: R) -> V): List { - val size = minOf(size, other.size) - val list = ArrayList(size) - for (i in 0 until size) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun DoubleArray.zip(other: Array, transform: (a: Double, b: R) -> V): List { - val size = minOf(size, other.size) - val list = ArrayList(size) - for (i in 0 until size) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun BooleanArray.zip(other: Array, transform: (a: Boolean, b: R) -> V): List { - val size = minOf(size, other.size) - val list = ArrayList(size) - for (i in 0 until size) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun CharArray.zip(other: Array, transform: (a: Char, b: R) -> V): List { - val size = minOf(size, other.size) - val list = ArrayList(size) - for (i in 0 until size) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun Array.zip(other: Iterable): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun ByteArray.zip(other: Iterable): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun ShortArray.zip(other: Iterable): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun IntArray.zip(other: Iterable): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun LongArray.zip(other: Iterable): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun FloatArray.zip(other: Iterable): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun DoubleArray.zip(other: Iterable): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun BooleanArray.zip(other: Iterable): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun CharArray.zip(other: Iterable): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun Array.zip(other: Iterable, transform: (a: T, b: R) -> V): List { - val arraySize = size - val list = ArrayList(minOf(other.collectionSizeOrDefault(10), arraySize)) - var i = 0 - for (element in other) { - if (i >= arraySize) break - list.add(transform(this[i++], element)) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun ByteArray.zip(other: Iterable, transform: (a: Byte, b: R) -> V): List { - val arraySize = size - val list = ArrayList(minOf(other.collectionSizeOrDefault(10), arraySize)) - var i = 0 - for (element in other) { - if (i >= arraySize) break - list.add(transform(this[i++], element)) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun ShortArray.zip(other: Iterable, transform: (a: Short, b: R) -> V): List { - val arraySize = size - val list = ArrayList(minOf(other.collectionSizeOrDefault(10), arraySize)) - var i = 0 - for (element in other) { - if (i >= arraySize) break - list.add(transform(this[i++], element)) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun IntArray.zip(other: Iterable, transform: (a: Int, b: R) -> V): List { - val arraySize = size - val list = ArrayList(minOf(other.collectionSizeOrDefault(10), arraySize)) - var i = 0 - for (element in other) { - if (i >= arraySize) break - list.add(transform(this[i++], element)) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun LongArray.zip(other: Iterable, transform: (a: Long, b: R) -> V): List { - val arraySize = size - val list = ArrayList(minOf(other.collectionSizeOrDefault(10), arraySize)) - var i = 0 - for (element in other) { - if (i >= arraySize) break - list.add(transform(this[i++], element)) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun FloatArray.zip(other: Iterable, transform: (a: Float, b: R) -> V): List { - val arraySize = size - val list = ArrayList(minOf(other.collectionSizeOrDefault(10), arraySize)) - var i = 0 - for (element in other) { - if (i >= arraySize) break - list.add(transform(this[i++], element)) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun DoubleArray.zip(other: Iterable, transform: (a: Double, b: R) -> V): List { - val arraySize = size - val list = ArrayList(minOf(other.collectionSizeOrDefault(10), arraySize)) - var i = 0 - for (element in other) { - if (i >= arraySize) break - list.add(transform(this[i++], element)) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun BooleanArray.zip(other: Iterable, transform: (a: Boolean, b: R) -> V): List { - val arraySize = size - val list = ArrayList(minOf(other.collectionSizeOrDefault(10), arraySize)) - var i = 0 - for (element in other) { - if (i >= arraySize) break - list.add(transform(this[i++], element)) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun CharArray.zip(other: Iterable, transform: (a: Char, b: R) -> V): List { - val arraySize = size - val list = ArrayList(minOf(other.collectionSizeOrDefault(10), arraySize)) - var i = 0 - for (element in other) { - if (i >= arraySize) break - list.add(transform(this[i++], element)) - } - return list -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun ByteArray.zip(other: ByteArray): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun ShortArray.zip(other: ShortArray): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun IntArray.zip(other: IntArray): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun LongArray.zip(other: LongArray): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun FloatArray.zip(other: FloatArray): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun DoubleArray.zip(other: DoubleArray): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun BooleanArray.zip(other: BooleanArray): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun CharArray.zip(other: CharArray): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun ByteArray.zip(other: ByteArray, transform: (a: Byte, b: Byte) -> V): List { - val size = minOf(size, other.size) - val list = ArrayList(size) - for (i in 0 until size) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun ShortArray.zip(other: ShortArray, transform: (a: Short, b: Short) -> V): List { - val size = minOf(size, other.size) - val list = ArrayList(size) - for (i in 0 until size) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun IntArray.zip(other: IntArray, transform: (a: Int, b: Int) -> V): List { - val size = minOf(size, other.size) - val list = ArrayList(size) - for (i in 0 until size) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun LongArray.zip(other: LongArray, transform: (a: Long, b: Long) -> V): List { - val size = minOf(size, other.size) - val list = ArrayList(size) - for (i in 0 until size) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun FloatArray.zip(other: FloatArray, transform: (a: Float, b: Float) -> V): List { - val size = minOf(size, other.size) - val list = ArrayList(size) - for (i in 0 until size) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun DoubleArray.zip(other: DoubleArray, transform: (a: Double, b: Double) -> V): List { - val size = minOf(size, other.size) - val list = ArrayList(size) - for (i in 0 until size) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun BooleanArray.zip(other: BooleanArray, transform: (a: Boolean, b: Boolean) -> V): List { - val size = minOf(size, other.size) - val list = ArrayList(size) - for (i in 0 until size) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun CharArray.zip(other: CharArray, transform: (a: Char, b: Char) -> V): List { - val size = minOf(size, other.size) - val list = ArrayList(size) - for (i in 0 until size) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinTo - */ -public fun Array.joinTo(buffer: A, separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((T) -> CharSequence)? = null): A { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - buffer.appendElement(element, transform) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) - return buffer -} - -/** - * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinTo - */ -public fun ByteArray.joinTo(buffer: A, separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Byte) -> CharSequence)? = null): A { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - if (transform != null) - buffer.append(transform(element)) - else - buffer.append(element.toString()) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) - return buffer -} - -/** - * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinTo - */ -public fun ShortArray.joinTo(buffer: A, separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Short) -> CharSequence)? = null): A { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - if (transform != null) - buffer.append(transform(element)) - else - buffer.append(element.toString()) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) - return buffer -} - -/** - * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinTo - */ -public fun IntArray.joinTo(buffer: A, separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Int) -> CharSequence)? = null): A { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - if (transform != null) - buffer.append(transform(element)) - else - buffer.append(element.toString()) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) - return buffer -} - -/** - * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinTo - */ -public fun LongArray.joinTo(buffer: A, separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Long) -> CharSequence)? = null): A { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - if (transform != null) - buffer.append(transform(element)) - else - buffer.append(element.toString()) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) - return buffer -} - -/** - * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinTo - */ -public fun FloatArray.joinTo(buffer: A, separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Float) -> CharSequence)? = null): A { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - if (transform != null) - buffer.append(transform(element)) - else - buffer.append(element.toString()) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) - return buffer -} - -/** - * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinTo - */ -public fun DoubleArray.joinTo(buffer: A, separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Double) -> CharSequence)? = null): A { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - if (transform != null) - buffer.append(transform(element)) - else - buffer.append(element.toString()) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) - return buffer -} - -/** - * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinTo - */ -public fun BooleanArray.joinTo(buffer: A, separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Boolean) -> CharSequence)? = null): A { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - if (transform != null) - buffer.append(transform(element)) - else - buffer.append(element.toString()) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) - return buffer -} - -/** - * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinTo - */ -public fun CharArray.joinTo(buffer: A, separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Char) -> CharSequence)? = null): A { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - if (transform != null) - buffer.append(transform(element)) - else - buffer.append(element) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) - return buffer -} - -/** - * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinToString - */ -public fun Array.joinToString(separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((T) -> CharSequence)? = null): String { - return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() -} - -/** - * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinToString - */ -public fun ByteArray.joinToString(separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Byte) -> CharSequence)? = null): String { - return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() -} - -/** - * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinToString - */ -public fun ShortArray.joinToString(separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Short) -> CharSequence)? = null): String { - return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() -} - -/** - * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinToString - */ -public fun IntArray.joinToString(separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Int) -> CharSequence)? = null): String { - return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() -} - -/** - * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinToString - */ -public fun LongArray.joinToString(separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Long) -> CharSequence)? = null): String { - return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() -} - -/** - * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinToString - */ -public fun FloatArray.joinToString(separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Float) -> CharSequence)? = null): String { - return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() -} - -/** - * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinToString - */ -public fun DoubleArray.joinToString(separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Double) -> CharSequence)? = null): String { - return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() -} - -/** - * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinToString - */ -public fun BooleanArray.joinToString(separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Boolean) -> CharSequence)? = null): String { - return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() -} - -/** - * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinToString - */ -public fun CharArray.joinToString(separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Char) -> CharSequence)? = null): String { - return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() -} - -/** - * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. - */ -public fun Array.asIterable(): Iterable { - if (isEmpty()) return emptyList() - return Iterable { this.iterator() } -} - -/** - * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. - */ -public fun ByteArray.asIterable(): Iterable { - if (isEmpty()) return emptyList() - return Iterable { this.iterator() } -} - -/** - * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. - */ -public fun ShortArray.asIterable(): Iterable { - if (isEmpty()) return emptyList() - return Iterable { this.iterator() } -} - -/** - * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. - */ -public fun IntArray.asIterable(): Iterable { - if (isEmpty()) return emptyList() - return Iterable { this.iterator() } -} - -/** - * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. - */ -public fun LongArray.asIterable(): Iterable { - if (isEmpty()) return emptyList() - return Iterable { this.iterator() } -} - -/** - * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. - */ -public fun FloatArray.asIterable(): Iterable { - if (isEmpty()) return emptyList() - return Iterable { this.iterator() } -} - -/** - * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. - */ -public fun DoubleArray.asIterable(): Iterable { - if (isEmpty()) return emptyList() - return Iterable { this.iterator() } -} - -/** - * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. - */ -public fun BooleanArray.asIterable(): Iterable { - if (isEmpty()) return emptyList() - return Iterable { this.iterator() } -} - -/** - * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. - */ -public fun CharArray.asIterable(): Iterable { - if (isEmpty()) return emptyList() - return Iterable { this.iterator() } -} - -/** - * Creates a [Sequence] instance that wraps the original array returning its elements when being iterated. - * - * @sample samples.collections.Sequences.Building.sequenceFromArray - */ -public fun Array.asSequence(): Sequence { - if (isEmpty()) return emptySequence() - return Sequence { this.iterator() } -} - -/** - * Creates a [Sequence] instance that wraps the original array returning its elements when being iterated. - * - * @sample samples.collections.Sequences.Building.sequenceFromArray - */ -public fun ByteArray.asSequence(): Sequence { - if (isEmpty()) return emptySequence() - return Sequence { this.iterator() } -} - -/** - * Creates a [Sequence] instance that wraps the original array returning its elements when being iterated. - * - * @sample samples.collections.Sequences.Building.sequenceFromArray - */ -public fun ShortArray.asSequence(): Sequence { - if (isEmpty()) return emptySequence() - return Sequence { this.iterator() } -} - -/** - * Creates a [Sequence] instance that wraps the original array returning its elements when being iterated. - * - * @sample samples.collections.Sequences.Building.sequenceFromArray - */ -public fun IntArray.asSequence(): Sequence { - if (isEmpty()) return emptySequence() - return Sequence { this.iterator() } -} - -/** - * Creates a [Sequence] instance that wraps the original array returning its elements when being iterated. - * - * @sample samples.collections.Sequences.Building.sequenceFromArray - */ -public fun LongArray.asSequence(): Sequence { - if (isEmpty()) return emptySequence() - return Sequence { this.iterator() } -} - -/** - * Creates a [Sequence] instance that wraps the original array returning its elements when being iterated. - * - * @sample samples.collections.Sequences.Building.sequenceFromArray - */ -public fun FloatArray.asSequence(): Sequence { - if (isEmpty()) return emptySequence() - return Sequence { this.iterator() } -} - -/** - * Creates a [Sequence] instance that wraps the original array returning its elements when being iterated. - * - * @sample samples.collections.Sequences.Building.sequenceFromArray - */ -public fun DoubleArray.asSequence(): Sequence { - if (isEmpty()) return emptySequence() - return Sequence { this.iterator() } -} - -/** - * Creates a [Sequence] instance that wraps the original array returning its elements when being iterated. - * - * @sample samples.collections.Sequences.Building.sequenceFromArray - */ -public fun BooleanArray.asSequence(): Sequence { - if (isEmpty()) return emptySequence() - return Sequence { this.iterator() } -} - -/** - * Creates a [Sequence] instance that wraps the original array returning its elements when being iterated. - * - * @sample samples.collections.Sequences.Building.sequenceFromArray - */ -public fun CharArray.asSequence(): Sequence { - if (isEmpty()) return emptySequence() - return Sequence { this.iterator() } -} - -/** - * Returns an average value of elements in the array. - */ -@kotlin.jvm.JvmName("averageOfByte") -public fun Array.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the array. - */ -@kotlin.jvm.JvmName("averageOfShort") -public fun Array.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the array. - */ -@kotlin.jvm.JvmName("averageOfInt") -public fun Array.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the array. - */ -@kotlin.jvm.JvmName("averageOfLong") -public fun Array.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the array. - */ -@kotlin.jvm.JvmName("averageOfFloat") -public fun Array.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the array. - */ -@kotlin.jvm.JvmName("averageOfDouble") -public fun Array.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the array. - */ -public fun ByteArray.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the array. - */ -public fun ShortArray.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the array. - */ -public fun IntArray.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the array. - */ -public fun LongArray.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the array. - */ -public fun FloatArray.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the array. - */ -public fun DoubleArray.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns the sum of all elements in the array. - */ -@kotlin.jvm.JvmName("sumOfByte") -public fun Array.sum(): Int { - var sum: Int = 0 - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the array. - */ -@kotlin.jvm.JvmName("sumOfShort") -public fun Array.sum(): Int { - var sum: Int = 0 - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the array. - */ -@kotlin.jvm.JvmName("sumOfInt") -public fun Array.sum(): Int { - var sum: Int = 0 - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the array. - */ -@kotlin.jvm.JvmName("sumOfLong") -public fun Array.sum(): Long { - var sum: Long = 0L - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the array. - */ -@kotlin.jvm.JvmName("sumOfFloat") -public fun Array.sum(): Float { - var sum: Float = 0.0f - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the array. - */ -@kotlin.jvm.JvmName("sumOfDouble") -public fun Array.sum(): Double { - var sum: Double = 0.0 - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the array. - */ -public fun ByteArray.sum(): Int { - var sum: Int = 0 - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the array. - */ -public fun ShortArray.sum(): Int { - var sum: Int = 0 - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the array. - */ -public fun IntArray.sum(): Int { - var sum: Int = 0 - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the array. - */ -public fun LongArray.sum(): Long { - var sum: Long = 0L - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the array. - */ -public fun FloatArray.sum(): Float { - var sum: Float = 0.0f - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the array. - */ -public fun DoubleArray.sum(): Double { - var sum: Double = 0.0 - for (element in this) { - sum += element - } - return sum -} - diff --git a/js/js.libraries/src/core/generated/_CollectionsJs.kt b/js/js.libraries/src/core/generated/_CollectionsJs.kt deleted file mode 100644 index 4e07c7c7462..00000000000 --- a/js/js.libraries/src/core/generated/_CollectionsJs.kt +++ /dev/null @@ -1,2379 +0,0 @@ -@file:kotlin.jvm.JvmMultifileClass -@file:kotlin.jvm.JvmName("CollectionsKt") - -package kotlin.collections - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// - -import kotlin.js.* -import kotlin.* -import kotlin.text.* -import kotlin.comparisons.* - -/** - * Returns 1st *element* from the collection. - */ -@kotlin.internal.InlineOnly -public inline operator fun List.component1(): T { - return get(0) -} - -/** - * Returns 2nd *element* from the collection. - */ -@kotlin.internal.InlineOnly -public inline operator fun List.component2(): T { - return get(1) -} - -/** - * Returns 3rd *element* from the collection. - */ -@kotlin.internal.InlineOnly -public inline operator fun List.component3(): T { - return get(2) -} - -/** - * Returns 4th *element* from the collection. - */ -@kotlin.internal.InlineOnly -public inline operator fun List.component4(): T { - return get(3) -} - -/** - * Returns 5th *element* from the collection. - */ -@kotlin.internal.InlineOnly -public inline operator fun List.component5(): T { - return get(4) -} - -/** - * Returns `true` if [element] is found in the collection. - */ -public operator fun <@kotlin.internal.OnlyInputTypes T> Iterable.contains(element: T): Boolean { - if (this is Collection) - return contains(element) - return indexOf(element) >= 0 -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. - */ -public fun Iterable.elementAt(index: Int): T { - if (this is List) - return get(index) - return elementAtOrElse(index) { throw IndexOutOfBoundsException("Collection doesn't contain element at index $index.") } -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this list. - */ -@kotlin.internal.InlineOnly -public inline fun List.elementAt(index: Int): T { - return get(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. - */ -public fun Iterable.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T { - if (this is List) - return this.getOrElse(index, defaultValue) - if (index < 0) - return defaultValue(index) - val iterator = iterator() - var count = 0 - while (iterator.hasNext()) { - val element = iterator.next() - if (index == count++) - return element - } - return defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this list. - */ -@kotlin.internal.InlineOnly -public inline fun List.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. - */ -public fun Iterable.elementAtOrNull(index: Int): T? { - if (this is List) - return this.getOrNull(index) - if (index < 0) - return null - val iterator = iterator() - var count = 0 - while (iterator.hasNext()) { - val element = iterator.next() - if (index == count++) - return element - } - return null -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this list. - */ -@kotlin.internal.InlineOnly -public inline fun List.elementAtOrNull(index: Int): T? { - return this.getOrNull(index) -} - -/** - * Returns the first element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun Iterable.find(predicate: (T) -> Boolean): T? { - return firstOrNull(predicate) -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun Iterable.findLast(predicate: (T) -> Boolean): T? { - return lastOrNull(predicate) -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun List.findLast(predicate: (T) -> Boolean): T? { - return lastOrNull(predicate) -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the collection is empty. - */ -public fun Iterable.first(): T { - when (this) { - is List -> return this.first() - else -> { - val iterator = iterator() - if (!iterator.hasNext()) - throw NoSuchElementException("Collection is empty.") - return iterator.next() - } - } -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the list is empty. - */ -public fun List.first(): T { - if (isEmpty()) - throw NoSuchElementException("List is empty.") - return this[0] -} - -/** - * Returns the first element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun Iterable.first(predicate: (T) -> Boolean): T { - for (element in this) if (predicate(element)) return element - throw NoSuchElementException("Collection contains no element matching the predicate.") -} - -/** - * Returns the first element, or `null` if the collection is empty. - */ -public fun Iterable.firstOrNull(): T? { - when (this) { - is List -> { - if (isEmpty()) - return null - else - return this[0] - } - else -> { - val iterator = iterator() - if (!iterator.hasNext()) - return null - return iterator.next() - } - } -} - -/** - * Returns the first element, or `null` if the list is empty. - */ -public fun List.firstOrNull(): T? { - return if (isEmpty()) null else this[0] -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - */ -public inline fun Iterable.firstOrNull(predicate: (T) -> Boolean): T? { - for (element in this) if (predicate(element)) return element - return null -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this list. - */ -@kotlin.internal.InlineOnly -public inline fun List.getOrElse(index: Int, defaultValue: (Int) -> T): T { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this list. - */ -public fun List.getOrNull(index: Int): T? { - return if (index >= 0 && index <= lastIndex) get(index) else null -} - -/** - * Returns first index of [element], or -1 if the collection does not contain element. - */ -public fun <@kotlin.internal.OnlyInputTypes T> Iterable.indexOf(element: T): Int { - if (this is List) return this.indexOf(element) - var index = 0 - for (item in this) { - if (element == item) - return index - index++ - } - return -1 -} - -/** - * Returns first index of [element], or -1 if the list does not contain element. - */ -@Suppress("EXTENSION_SHADOWED_BY_MEMBER") // false warning, extension takes precedence in some cases -public fun <@kotlin.internal.OnlyInputTypes T> List.indexOf(element: T): Int { - return indexOf(element) -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element. - */ -public inline fun Iterable.indexOfFirst(predicate: (T) -> Boolean): Int { - var index = 0 - for (item in this) { - if (predicate(item)) - return index - index++ - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the list does not contain such element. - */ -public inline fun List.indexOfFirst(predicate: (T) -> Boolean): Int { - var index = 0 - for (item in this) { - if (predicate(item)) - return index - index++ - } - return -1 -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element. - */ -public inline fun Iterable.indexOfLast(predicate: (T) -> Boolean): Int { - var lastIndex = -1 - var index = 0 - for (item in this) { - if (predicate(item)) - lastIndex = index - index++ - } - return lastIndex -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the list does not contain such element. - */ -public inline fun List.indexOfLast(predicate: (T) -> Boolean): Int { - val iterator = this.listIterator(size) - while (iterator.hasPrevious()) { - if (predicate(iterator.previous())) { - return iterator.nextIndex() - } - } - return -1 -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the collection is empty. - */ -public fun Iterable.last(): T { - when (this) { - is List -> return this.last() - else -> { - val iterator = iterator() - if (!iterator.hasNext()) - throw NoSuchElementException("Collection is empty.") - var last = iterator.next() - while (iterator.hasNext()) - last = iterator.next() - return last - } - } -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the list is empty. - */ -public fun List.last(): T { - if (isEmpty()) - throw NoSuchElementException("List is empty.") - return this[lastIndex] -} - -/** - * Returns the last element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun Iterable.last(predicate: (T) -> Boolean): T { - var last: T? = null - var found = false - for (element in this) { - if (predicate(element)) { - last = element - found = true - } - } - if (!found) throw NoSuchElementException("Collection contains no element matching the predicate.") - @Suppress("UNCHECKED_CAST") - return last as T -} - -/** - * Returns the last element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun List.last(predicate: (T) -> Boolean): T { - val iterator = this.listIterator(size) - while (iterator.hasPrevious()) { - val element = iterator.previous() - if (predicate(element)) return element - } - throw NoSuchElementException("List contains no element matching the predicate.") -} - -/** - * Returns last index of [element], or -1 if the collection does not contain element. - */ -public fun <@kotlin.internal.OnlyInputTypes T> Iterable.lastIndexOf(element: T): Int { - if (this is List) return this.lastIndexOf(element) - var lastIndex = -1 - var index = 0 - for (item in this) { - if (element == item) - lastIndex = index - index++ - } - return lastIndex -} - -/** - * Returns last index of [element], or -1 if the list does not contain element. - */ -@Suppress("EXTENSION_SHADOWED_BY_MEMBER") // false warning, extension takes precedence in some cases -public fun <@kotlin.internal.OnlyInputTypes T> List.lastIndexOf(element: T): Int { - return lastIndexOf(element) -} - -/** - * Returns the last element, or `null` if the collection is empty. - */ -public fun Iterable.lastOrNull(): T? { - when (this) { - is List -> return if (isEmpty()) null else this[size - 1] - else -> { - val iterator = iterator() - if (!iterator.hasNext()) - return null - var last = iterator.next() - while (iterator.hasNext()) - last = iterator.next() - return last - } - } -} - -/** - * Returns the last element, or `null` if the list is empty. - */ -public fun List.lastOrNull(): T? { - return if (isEmpty()) null else this[size - 1] -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -public inline fun Iterable.lastOrNull(predicate: (T) -> Boolean): T? { - var last: T? = null - for (element in this) { - if (predicate(element)) { - last = element - } - } - return last -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -public inline fun List.lastOrNull(predicate: (T) -> Boolean): T? { - val iterator = this.listIterator(size) - while (iterator.hasPrevious()) { - val element = iterator.previous() - if (predicate(element)) return element - } - return null -} - -/** - * Returns the single element, or throws an exception if the collection is empty or has more than one element. - */ -public fun Iterable.single(): T { - when (this) { - is List -> return this.single() - else -> { - val iterator = iterator() - if (!iterator.hasNext()) - throw NoSuchElementException("Collection is empty.") - val single = iterator.next() - if (iterator.hasNext()) - throw IllegalArgumentException("Collection has more than one element.") - return single - } - } -} - -/** - * Returns the single element, or throws an exception if the list is empty or has more than one element. - */ -public fun List.single(): T { - return when (size) { - 0 -> throw NoSuchElementException("List is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("List has more than one element.") - } -} - -/** - * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. - */ -public inline fun Iterable.single(predicate: (T) -> Boolean): T { - var single: T? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Collection contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Collection contains no element matching the predicate.") - @Suppress("UNCHECKED_CAST") - return single as T -} - -/** - * Returns single element, or `null` if the collection is empty or has more than one element. - */ -public fun Iterable.singleOrNull(): T? { - when (this) { - is List -> return if (size == 1) this[0] else null - else -> { - val iterator = iterator() - if (!iterator.hasNext()) - return null - val single = iterator.next() - if (iterator.hasNext()) - return null - return single - } - } -} - -/** - * Returns single element, or `null` if the list is empty or has more than one element. - */ -public fun List.singleOrNull(): T? { - return if (size == 1) this[0] else null -} - -/** - * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. - */ -public inline fun Iterable.singleOrNull(predicate: (T) -> Boolean): T? { - var single: T? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) return null - single = element - found = true - } - } - if (!found) return null - return single -} - -/** - * Returns a list containing all elements except first [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun Iterable.drop(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return toList() - val list: ArrayList - if (this is Collection<*>) { - val resultSize = size - n - if (resultSize <= 0) - return emptyList() - if (resultSize == 1) - return listOf(last()) - list = ArrayList(resultSize) - if (this is List) { - if (this is RandomAccess) { - for (index in n until size) - list.add(this[index]) - } else { - for (item in listIterator(n)) - list.add(item) - } - return list - } - } - else { - list = ArrayList() - } - var count = 0 - for (item in this) { - if (count++ >= n) list.add(item) - } - return list.optimizeReadOnlyList() -} - -/** - * Returns a list containing all elements except last [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun List.dropLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return take((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except last elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun List.dropLastWhile(predicate: (T) -> Boolean): List { - if (!isEmpty()) { - val iterator = listIterator(size) - while (iterator.hasPrevious()) { - if (!predicate(iterator.previous())) { - return take(iterator.nextIndex() + 1) - } - } - } - return emptyList() -} - -/** - * Returns a list containing all elements except first elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun Iterable.dropWhile(predicate: (T) -> Boolean): List { - var yielding = false - val list = ArrayList() - for (item in this) - if (yielding) - list.add(item) - else if (!predicate(item)) { - list.add(item) - yielding = true - } - return list -} - -/** - * Returns a list containing only elements matching the given [predicate]. - */ -public inline fun Iterable.filter(predicate: (T) -> Boolean): List { - return filterTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun Iterable.filterIndexed(predicate: (index: Int, T) -> Boolean): List { - return filterIndexedTo(ArrayList(), predicate) -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun > Iterable.filterIndexedTo(destination: C, predicate: (index: Int, T) -> Boolean): C { - forEachIndexed { index, element -> - if (predicate(index, element)) destination.add(element) - } - return destination -} - -/** - * Returns a list containing all elements that are instances of specified type parameter R. - */ -public inline fun Iterable<*>.filterIsInstance(): List<@kotlin.internal.NoInfer R> { - return filterIsInstanceTo(ArrayList()) -} - -/** - * Appends all elements that are instances of specified type parameter R to the given [destination]. - */ -public inline fun > Iterable<*>.filterIsInstanceTo(destination: C): C { - for (element in this) if (element is R) destination.add(element) - return destination -} - -/** - * Returns a list containing all elements not matching the given [predicate]. - */ -public inline fun Iterable.filterNot(predicate: (T) -> Boolean): List { - return filterNotTo(ArrayList(), predicate) -} - -/** - * Returns a list containing all elements that are not `null`. - */ -public fun Iterable.filterNotNull(): List { - return filterNotNullTo(ArrayList()) -} - -/** - * Appends all elements that are not `null` to the given [destination]. - */ -public fun , T : Any> Iterable.filterNotNullTo(destination: C): C { - for (element in this) if (element != null) destination.add(element) - return destination -} - -/** - * Appends all elements not matching the given [predicate] to the given [destination]. - */ -public inline fun > Iterable.filterNotTo(destination: C, predicate: (T) -> Boolean): C { - for (element in this) if (!predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - */ -public inline fun > Iterable.filterTo(destination: C, predicate: (T) -> Boolean): C { - for (element in this) if (predicate(element)) destination.add(element) - return destination -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun List.slice(indices: IntRange): List { - if (indices.isEmpty()) return listOf() - return this.subList(indices.start, indices.endInclusive + 1).toList() -} - -/** - * Returns a list containing elements at specified [indices]. - */ -public fun List.slice(indices: Iterable): List { - val size = indices.collectionSizeOrDefault(10) - if (size == 0) return emptyList() - val list = ArrayList(size) - for (index in indices) { - list.add(get(index)) - } - return list -} - -/** - * Returns a list containing first [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun Iterable.take(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - if (this is Collection) { - if (n >= size) return toList() - if (n == 1) return listOf(first()) - } - var count = 0 - val list = ArrayList(n) - for (item in this) { - if (count++ == n) - break - list.add(item) - } - return list.optimizeReadOnlyList() -} - -/** - * Returns a list containing last [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun List.takeLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - val size = size - if (n >= size) return toList() - if (n == 1) return listOf(last()) - val list = ArrayList(n) - if (this is RandomAccess) { - for (index in size - n until size) - list.add(this[index]) - } else { - for (item in listIterator(size - n)) - list.add(item) - } - return list -} - -/** - * Returns a list containing last elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun List.takeLastWhile(predicate: (T) -> Boolean): List { - if (isEmpty()) - return emptyList() - val iterator = listIterator(size) - while (iterator.hasPrevious()) { - if (!predicate(iterator.previous())) { - iterator.next() - val expectedSize = size - iterator.nextIndex() - if (expectedSize == 0) return emptyList() - return ArrayList(expectedSize).apply { - while (iterator.hasNext()) - add(iterator.next()) - } - } - } - return toList() -} - -/** - * Returns a list containing first elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun Iterable.takeWhile(predicate: (T) -> Boolean): List { - val list = ArrayList() - for (item in this) { - if (!predicate(item)) - break - list.add(item) - } - return list -} - -/** - * Reverses elements in the list in-place. - */ -public fun MutableList.reverse(): Unit { - val midPoint = (size / 2) - 1 - if (midPoint < 0) return - var reverseIndex = lastIndex - for (index in 0..midPoint) { - val tmp = this[index] - this[index] = this[reverseIndex] - this[reverseIndex] = tmp - reverseIndex-- - } -} - -/** - * Returns a list with elements in reversed order. - */ -public fun Iterable.reversed(): List { - if (this is Collection && size <= 1) return toList() - val list = toMutableList() - list.reverse() - return list -} - -/** - * Sorts elements in the list in-place according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > MutableList.sortBy(crossinline selector: (T) -> R?): Unit { - if (size > 1) sortWith(compareBy(selector)) -} - -/** - * Sorts elements in the list in-place descending according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > MutableList.sortByDescending(crossinline selector: (T) -> R?): Unit { - if (size > 1) sortWith(compareByDescending(selector)) -} - -/** - * Sorts elements in the list in-place descending according to their natural sort order. - */ -public fun > MutableList.sortDescending(): Unit { - sortWith(reverseOrder()) -} - -/** - * Returns a list of all elements sorted according to their natural sort order. - */ -public fun > Iterable.sorted(): List { - if (this is Collection) { - if (size <= 1) return this.toList() - @Suppress("UNCHECKED_CAST") - return (toTypedArray>() as Array).apply { sort() }.asList() - } - return toMutableList().apply { sort() } -} - -/** - * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > Iterable.sortedBy(crossinline selector: (T) -> R?): List { - return sortedWith(compareBy(selector)) -} - -/** - * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > Iterable.sortedByDescending(crossinline selector: (T) -> R?): List { - return sortedWith(compareByDescending(selector)) -} - -/** - * Returns a list of all elements sorted descending according to their natural sort order. - */ -public fun > Iterable.sortedDescending(): List { - return sortedWith(reverseOrder()) -} - -/** - * Returns a list of all elements sorted according to the specified [comparator]. - */ -public fun Iterable.sortedWith(comparator: Comparator): List { - if (this is Collection) { - if (size <= 1) return this.toList() - @Suppress("UNCHECKED_CAST") - return (toTypedArray() as Array).apply { sortWith(comparator) }.asList() - } - return toMutableList().apply { sortWith(comparator) } -} - -/** - * Returns an array of Boolean containing all of the elements of this collection. - */ -public fun Collection.toBooleanArray(): BooleanArray { - val result = BooleanArray(size) - var index = 0 - for (element in this) - result[index++] = element - return result -} - -/** - * Returns an array of Byte containing all of the elements of this collection. - */ -public fun Collection.toByteArray(): ByteArray { - val result = ByteArray(size) - var index = 0 - for (element in this) - result[index++] = element - return result -} - -/** - * Returns an array of Char containing all of the elements of this collection. - */ -public fun Collection.toCharArray(): CharArray { - val result = CharArray(size) - var index = 0 - for (element in this) - result[index++] = element - return result -} - -/** - * Returns an array of Double containing all of the elements of this collection. - */ -public fun Collection.toDoubleArray(): DoubleArray { - val result = DoubleArray(size) - var index = 0 - for (element in this) - result[index++] = element - return result -} - -/** - * Returns an array of Float containing all of the elements of this collection. - */ -public fun Collection.toFloatArray(): FloatArray { - val result = FloatArray(size) - var index = 0 - for (element in this) - result[index++] = element - return result -} - -/** - * Returns an array of Int containing all of the elements of this collection. - */ -public fun Collection.toIntArray(): IntArray { - val result = IntArray(size) - var index = 0 - for (element in this) - result[index++] = element - return result -} - -/** - * Returns an array of Long containing all of the elements of this collection. - */ -public fun Collection.toLongArray(): LongArray { - val result = LongArray(size) - var index = 0 - for (element in this) - result[index++] = element - return result -} - -/** - * Returns an array of Short containing all of the elements of this collection. - */ -public fun Collection.toShortArray(): ShortArray { - val result = ShortArray(size) - var index = 0 - for (element in this) - result[index++] = element - return result -} - -/** - * Returns a [Map] containing key-value pairs provided by [transform] function - * applied to elements of the given collection. - * - * If any of two pairs would have the same key the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original collection. - */ -public inline fun Iterable.associate(transform: (T) -> Pair): Map { - val capacity = mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16) - return associateTo(LinkedHashMap(capacity), transform) -} - -/** - * Returns a [Map] containing the elements from the given collection indexed by the key - * returned from [keySelector] function applied to each element. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original collection. - */ -public inline fun Iterable.associateBy(keySelector: (T) -> K): Map { - val capacity = mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector) -} - -/** - * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given collection. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original collection. - */ -public inline fun Iterable.associateBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map { - val capacity = mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function applied to each element of the given collection - * and value is the element itself. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > Iterable.associateByTo(destination: M, keySelector: (T) -> K): M { - for (element in this) { - destination.put(keySelector(element), element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function and - * and value is provided by the [valueTransform] function applied to elements of the given collection. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > Iterable.associateByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M { - for (element in this) { - destination.put(keySelector(element), valueTransform(element)) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs - * provided by [transform] function applied to each element of the given collection. - * - * If any of two pairs would have the same key the last one gets added to the map. - */ -public inline fun > Iterable.associateTo(destination: M, transform: (T) -> Pair): M { - for (element in this) { - destination += transform(element) - } - return destination -} - -/** - * Appends all elements to the given [destination] collection. - */ -public fun > Iterable.toCollection(destination: C): C { - for (item in this) { - destination.add(item) - } - return destination -} - -/** - * Returns a [HashSet] of all elements. - */ -public fun Iterable.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(collectionSizeOrDefault(12)))) -} - -/** - * Returns a [List] containing all elements. - */ -public fun Iterable.toList(): List { - if (this is Collection) { - return when (size) { - 0 -> emptyList() - 1 -> listOf(if (this is List) get(0) else iterator().next()) - else -> this.toMutableList() - } - } - return this.toMutableList().optimizeReadOnlyList() -} - -/** - * Returns a [MutableList] filled with all elements of this collection. - */ -public fun Iterable.toMutableList(): MutableList { - if (this is Collection) - return this.toMutableList() - return toCollection(ArrayList()) -} - -/** - * Returns a [MutableList] filled with all elements of this collection. - */ -public fun Collection.toMutableList(): MutableList { - return ArrayList(this) -} - -/** - * Returns a [Set] of all elements. - * - * The returned set preserves the element iteration order of the original collection. - */ -public fun Iterable.toSet(): Set { - if (this is Collection) { - return when (size) { - 0 -> emptySet() - 1 -> setOf(if (this is List) this[0] else iterator().next()) - else -> toCollection(LinkedHashSet(mapCapacity(size))) - } - } - return toCollection(LinkedHashSet()).optimizeReadOnlySet() -} - -/** - * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original collection. - */ -public inline fun Iterable.flatMap(transform: (T) -> Iterable): List { - return flatMapTo(ArrayList(), transform) -} - -/** - * Appends all elements yielded from results of [transform] function being invoked on each element of original collection, to the given [destination]. - */ -public inline fun > Iterable.flatMapTo(destination: C, transform: (T) -> Iterable): C { - for (element in this) { - val list = transform(element) - destination.addAll(list) - } - return destination -} - -/** - * Groups elements of the original collection by the key returned by the given [keySelector] function - * applied to each element and returns a map where each group key is associated with a list of corresponding elements. - * - * The returned map preserves the entry iteration order of the keys produced from the original collection. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun Iterable.groupBy(keySelector: (T) -> K): Map> { - return groupByTo(LinkedHashMap>(), keySelector) -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original collection - * by the key returned by the given [keySelector] function applied to the element - * and returns a map where each group key is associated with a list of corresponding values. - * - * The returned map preserves the entry iteration order of the keys produced from the original collection. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun Iterable.groupBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map> { - return groupByTo(LinkedHashMap>(), keySelector, valueTransform) -} - -/** - * Groups elements of the original collection by the key returned by the given [keySelector] function - * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun >> Iterable.groupByTo(destination: M, keySelector: (T) -> K): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(element) - } - return destination -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original collection - * by the key returned by the given [keySelector] function applied to the element - * and puts to the [destination] map each group key associated with a list of corresponding values. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun >> Iterable.groupByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(valueTransform(element)) - } - return destination -} - -/** - * Creates a [Grouping] source from a collection to be used later with one of group-and-fold operations - * using the specified [keySelector] function to extract a key from each element. - * - * @sample samples.collections.Collections.Transformations.groupingByEachCount - */ -@SinceKotlin("1.1") -public inline fun Iterable.groupingBy(crossinline keySelector: (T) -> K): Grouping { - return object : Grouping { - override fun sourceIterator(): Iterator = this@groupingBy.iterator() - override fun keyOf(element: T): K = keySelector(element) - } -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element in the original collection. - */ -public inline fun Iterable.map(transform: (T) -> R): List { - return mapTo(ArrayList(collectionSizeOrDefault(10)), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element and its index in the original collection. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun Iterable.mapIndexed(transform: (index: Int, T) -> R): List { - return mapIndexedTo(ArrayList(collectionSizeOrDefault(10)), transform) -} - -/** - * Returns a list containing only the non-null results of applying the given [transform] function - * to each element and its index in the original collection. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun Iterable.mapIndexedNotNull(transform: (index: Int, T) -> R?): List { - return mapIndexedNotNullTo(ArrayList(), transform) -} - -/** - * Applies the given [transform] function to each element and its index in the original collection - * and appends only the non-null results to the given [destination]. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun > Iterable.mapIndexedNotNullTo(destination: C, transform: (index: Int, T) -> R?): C { - forEachIndexed { index, element -> transform(index, element)?.let { destination.add(it) } } - return destination -} - -/** - * Applies the given [transform] function to each element and its index in the original collection - * and appends the results to the given [destination]. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun > Iterable.mapIndexedTo(destination: C, transform: (index: Int, T) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Returns a list containing only the non-null results of applying the given [transform] function - * to each element in the original collection. - */ -public inline fun Iterable.mapNotNull(transform: (T) -> R?): List { - return mapNotNullTo(ArrayList(), transform) -} - -/** - * Applies the given [transform] function to each element in the original collection - * and appends only the non-null results to the given [destination]. - */ -public inline fun > Iterable.mapNotNullTo(destination: C, transform: (T) -> R?): C { - forEach { element -> transform(element)?.let { destination.add(it) } } - return destination -} - -/** - * Applies the given [transform] function to each element of the original collection - * and appends the results to the given [destination]. - */ -public inline fun > Iterable.mapTo(destination: C, transform: (T) -> R): C { - for (item in this) - destination.add(transform(item)) - return destination -} - -/** - * Returns a lazy [Iterable] of [IndexedValue] for each element of the original collection. - */ -public fun Iterable.withIndex(): Iterable> { - return IndexingIterable { iterator() } -} - -/** - * Returns a list containing only distinct elements from the given collection. - * - * The elements in the resulting list are in the same order as they were in the source collection. - */ -public fun Iterable.distinct(): List { - return this.toMutableSet().toList() -} - -/** - * Returns a list containing only elements from the given collection - * having distinct keys returned by the given [selector] function. - * - * The elements in the resulting list are in the same order as they were in the source collection. - */ -public inline fun Iterable.distinctBy(selector: (T) -> K): List { - val set = HashSet() - val list = ArrayList() - for (e in this) { - val key = selector(e) - if (set.add(key)) - list.add(e) - } - return list -} - -/** - * Returns a set containing all elements that are contained by both this set and the specified collection. - * - * The returned set preserves the element iteration order of the original collection. - */ -public infix fun Iterable.intersect(other: Iterable): Set { - val set = this.toMutableSet() - set.retainAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by this collection and not contained by the specified collection. - * - * The returned set preserves the element iteration order of the original collection. - */ -public infix fun Iterable.subtract(other: Iterable): Set { - val set = this.toMutableSet() - set.removeAll(other) - return set -} - -/** - * Returns a mutable set containing all distinct elements from the given collection. - * - * The returned set preserves the element iteration order of the original collection. - */ -public fun Iterable.toMutableSet(): MutableSet { - return when (this) { - is Collection -> LinkedHashSet(this) - else -> toCollection(LinkedHashSet()) - } -} - -/** - * Returns a set containing all distinct elements from both collections. - * - * The returned set preserves the element iteration order of the original collection. - * Those elements of the [other] collection that are unique are iterated in the end - * in the order of the [other] collection. - */ -public infix fun Iterable.union(other: Iterable): Set { - val set = this.toMutableSet() - set.addAll(other) - return set -} - -/** - * Returns `true` if all elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.all - */ -public inline fun Iterable.all(predicate: (T) -> Boolean): Boolean { - if (this is Collection && isEmpty()) return true - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if collection has at least one element. - * - * @sample samples.collections.Collections.Aggregates.any - */ -public fun Iterable.any(): Boolean { - if (this is Collection) return !isEmpty() - return iterator().hasNext() -} - -/** - * Returns `true` if at least one element matches the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.anyWithPredicate - */ -public inline fun Iterable.any(predicate: (T) -> Boolean): Boolean { - if (this is Collection && isEmpty()) return false - for (element in this) if (predicate(element)) return true - return false -} - -/** - * Returns the number of elements in this collection. - */ -public fun Iterable.count(): Int { - if (this is Collection) return size - var count = 0 - for (element in this) count++ - return count -} - -/** - * Returns the number of elements in this collection. - */ -@kotlin.internal.InlineOnly -public inline fun Collection.count(): Int { - return size -} - -/** - * Returns the number of elements matching the given [predicate]. - */ -public inline fun Iterable.count(predicate: (T) -> Boolean): Int { - if (this is Collection && isEmpty()) return 0 - var count = 0 - for (element in this) if (predicate(element)) count++ - return count -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun Iterable.fold(initial: R, operation: (acc: R, T) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right - * to current accumulator value and each element with its index in the original collection. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself, and calculates the next accumulator value. - */ -public inline fun Iterable.foldIndexed(initial: R, operation: (index: Int, acc: R, T) -> R): R { - var index = 0 - var accumulator = initial - for (element in this) accumulator = operation(index++, accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun List.foldRight(initial: R, operation: (T, acc: R) -> R): R { - var accumulator = initial - if (!isEmpty()) { - val iterator = listIterator(size) - while (iterator.hasPrevious()) { - accumulator = operation(iterator.previous(), accumulator) - } - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left - * to each element with its index in the original list and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun List.foldRightIndexed(initial: R, operation: (index: Int, T, acc: R) -> R): R { - var accumulator = initial - if (!isEmpty()) { - val iterator = listIterator(size) - while (iterator.hasPrevious()) { - val index = iterator.previousIndex() - accumulator = operation(index, iterator.previous(), accumulator) - } - } - return accumulator -} - -/** - * Performs the given [action] on each element. - */ -@kotlin.internal.HidesMembers -public inline fun Iterable.forEach(action: (T) -> Unit): Unit { - for (element in this) action(element) -} - -/** - * Performs the given [action] on each element, providing sequential index with the element. - * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. - */ -public inline fun Iterable.forEachIndexed(action: (index: Int, T) -> Unit): Unit { - var index = 0 - for (item in this) action(index++, item) -} - -/** - * Returns the largest element or `null` if there are no elements. - * - * If any of elements is `NaN` returns `NaN`. - */ -@SinceKotlin("1.1") -public fun Iterable.max(): Double? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var max = iterator.next() - if (max.isNaN()) return max - while (iterator.hasNext()) { - val e = iterator.next() - if (e.isNaN()) return e - if (max < e) max = e - } - return max -} - -/** - * Returns the largest element or `null` if there are no elements. - * - * If any of elements is `NaN` returns `NaN`. - */ -@SinceKotlin("1.1") -public fun Iterable.max(): Float? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var max = iterator.next() - if (max.isNaN()) return max - while (iterator.hasNext()) { - val e = iterator.next() - if (e.isNaN()) return e - if (max < e) max = e - } - return max -} - -/** - * Returns the largest element or `null` if there are no elements. - */ -public fun > Iterable.max(): T? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var max = iterator.next() - while (iterator.hasNext()) { - val e = iterator.next() - if (max < e) max = e - } - return max -} - -/** - * Returns the first element yielding the largest value of the given function or `null` if there are no elements. - */ -public inline fun > Iterable.maxBy(selector: (T) -> R): T? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var maxElem = iterator.next() - var maxValue = selector(maxElem) - while (iterator.hasNext()) { - val e = iterator.next() - val v = selector(e) - if (maxValue < v) { - maxElem = e - maxValue = v - } - } - return maxElem -} - -/** - * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun Iterable.maxWith(comparator: Comparator): T? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var max = iterator.next() - while (iterator.hasNext()) { - val e = iterator.next() - if (comparator.compare(max, e) < 0) max = e - } - return max -} - -/** - * Returns the smallest element or `null` if there are no elements. - * - * If any of elements is `NaN` returns `NaN`. - */ -@SinceKotlin("1.1") -public fun Iterable.min(): Double? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var min = iterator.next() - if (min.isNaN()) return min - while (iterator.hasNext()) { - val e = iterator.next() - if (e.isNaN()) return e - if (min > e) min = e - } - return min -} - -/** - * Returns the smallest element or `null` if there are no elements. - * - * If any of elements is `NaN` returns `NaN`. - */ -@SinceKotlin("1.1") -public fun Iterable.min(): Float? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var min = iterator.next() - if (min.isNaN()) return min - while (iterator.hasNext()) { - val e = iterator.next() - if (e.isNaN()) return e - if (min > e) min = e - } - return min -} - -/** - * Returns the smallest element or `null` if there are no elements. - */ -public fun > Iterable.min(): T? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var min = iterator.next() - while (iterator.hasNext()) { - val e = iterator.next() - if (min > e) min = e - } - return min -} - -/** - * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. - */ -public inline fun > Iterable.minBy(selector: (T) -> R): T? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var minElem = iterator.next() - var minValue = selector(minElem) - while (iterator.hasNext()) { - val e = iterator.next() - val v = selector(e) - if (minValue > v) { - minElem = e - minValue = v - } - } - return minElem -} - -/** - * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun Iterable.minWith(comparator: Comparator): T? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var min = iterator.next() - while (iterator.hasNext()) { - val e = iterator.next() - if (comparator.compare(min, e) > 0) min = e - } - return min -} - -/** - * Returns `true` if the collection has no elements. - * - * @sample samples.collections.Collections.Aggregates.none - */ -public fun Iterable.none(): Boolean { - if (this is Collection) return isEmpty() - return !iterator().hasNext() -} - -/** - * Returns `true` if no elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.noneWithPredicate - */ -public inline fun Iterable.none(predicate: (T) -> Boolean): Boolean { - if (this is Collection && isEmpty()) return true - for (element in this) if (predicate(element)) return false - return true -} - -/** - * Performs the given [action] on each element and returns the collection itself afterwards. - */ -@SinceKotlin("1.1") -public inline fun > C.onEach(action: (T) -> Unit): C { - return apply { for (element in this) action(element) } -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun Iterable.reduce(operation: (acc: S, T) -> S): S { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty collection can't be reduced.") - var accumulator: S = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(accumulator, iterator.next()) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right - * to current accumulator value and each element with its index in the original collection. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself and calculates the next accumulator value. - */ -public inline fun Iterable.reduceIndexed(operation: (index: Int, acc: S, T) -> S): S { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty collection can't be reduced.") - var index = 1 - var accumulator: S = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(index++, accumulator, iterator.next()) - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun List.reduceRight(operation: (T, acc: S) -> S): S { - val iterator = listIterator(size) - if (!iterator.hasPrevious()) - throw UnsupportedOperationException("Empty list can't be reduced.") - var accumulator: S = iterator.previous() - while (iterator.hasPrevious()) { - accumulator = operation(iterator.previous(), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left - * to each element with its index in the original list and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun List.reduceRightIndexed(operation: (index: Int, T, acc: S) -> S): S { - val iterator = listIterator(size) - if (!iterator.hasPrevious()) - throw UnsupportedOperationException("Empty list can't be reduced.") - var accumulator: S = iterator.previous() - while (iterator.hasPrevious()) { - val index = iterator.previousIndex() - accumulator = operation(index, iterator.previous(), accumulator) - } - return accumulator -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the collection. - */ -public inline fun Iterable.sumBy(selector: (T) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the collection. - */ -public inline fun Iterable.sumByDouble(selector: (T) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns an original collection containing all the non-`null` elements, throwing an [IllegalArgumentException] if there are any `null` elements. - */ -public fun Iterable.requireNoNulls(): Iterable { - for (element in this) { - if (element == null) { - throw IllegalArgumentException("null element found in $this.") - } - } - @Suppress("UNCHECKED_CAST") - return this as Iterable -} - -/** - * Returns an original collection containing all the non-`null` elements, throwing an [IllegalArgumentException] if there are any `null` elements. - */ -public fun List.requireNoNulls(): List { - for (element in this) { - if (element == null) { - throw IllegalArgumentException("null element found in $this.") - } - } - @Suppress("UNCHECKED_CAST") - return this as List -} - -/** - * Splits this collection into a list of lists each not exceeding the given [size]. - * - * The last list in the resulting list may have less elements than the given [size]. - * - * @param size the number of elements to take in each list, must be positive and can be greater than the number of elements in this collection. - * - * @sample samples.collections.Collections.Transformations.chunked - */ -@SinceKotlin("1.2") -public fun Iterable.chunked(size: Int): List> { - return windowed(size, size, partialWindows = true) -} - -/** - * Splits this collection into several lists each not exceeding the given [size] - * and applies the given [transform] function to an each. - * - * @return list of results of the [transform] applied to an each list. - * - * Note that the list passed to the [transform] function is ephemeral and is valid only inside that function. - * You should not store it or allow it to escape in some way, unless you made a snapshot of it. - * The last list may have less elements than the given [size]. - * - * @param size the number of elements to take in each list, must be positive and can be greater than the number of elements in this collection. - * - * @sample samples.text.Strings.chunkedTransform - */ -@SinceKotlin("1.2") -public fun Iterable.chunked(size: Int, transform: (List) -> R): List { - return windowed(size, size, partialWindows = true, transform = transform) -} - -/** - * Returns a list containing all elements of the original collection without the first occurrence of the given [element]. - */ -public operator fun Iterable.minus(element: T): List { - val result = ArrayList(collectionSizeOrDefault(10)) - var removed = false - return this.filterTo(result) { if (!removed && it == element) { removed = true; false } else true } -} - -/** - * Returns a list containing all elements of the original collection except the elements contained in the given [elements] array. - */ -public operator fun Iterable.minus(elements: Array): List { - if (elements.isEmpty()) return this.toList() - val other = elements.toHashSet() - return this.filterNot { it in other } -} - -/** - * Returns a list containing all elements of the original collection except the elements contained in the given [elements] collection. - */ -public operator fun Iterable.minus(elements: Iterable): List { - val other = elements.convertToSetForSetOperationWith(this) - if (other.isEmpty()) - return this.toList() - return this.filterNot { it in other } -} - -/** - * Returns a list containing all elements of the original collection except the elements contained in the given [elements] sequence. - */ -public operator fun Iterable.minus(elements: Sequence): List { - val other = elements.toHashSet() - if (other.isEmpty()) - return this.toList() - return this.filterNot { it in other } -} - -/** - * Returns a list containing all elements of the original collection without the first occurrence of the given [element]. - */ -@kotlin.internal.InlineOnly -public inline fun Iterable.minusElement(element: T): List { - return minus(element) -} - -/** - * Splits the original collection into pair of lists, - * where *first* list contains elements for which [predicate] yielded `true`, - * while *second* list contains elements for which [predicate] yielded `false`. - */ -public inline fun Iterable.partition(predicate: (T) -> Boolean): Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Returns a list containing all elements of the original collection and then the given [element]. - */ -public operator fun Iterable.plus(element: T): List { - if (this is Collection) return this.plus(element) - val result = ArrayList() - result.addAll(this) - result.add(element) - return result -} - -/** - * Returns a list containing all elements of the original collection and then the given [element]. - */ -public operator fun Collection.plus(element: T): List { - val result = ArrayList(size + 1) - result.addAll(this) - result.add(element) - return result -} - -/** - * Returns a list containing all elements of the original collection and then all elements of the given [elements] array. - */ -public operator fun Iterable.plus(elements: Array): List { - if (this is Collection) return this.plus(elements) - val result = ArrayList() - result.addAll(this) - result.addAll(elements) - return result -} - -/** - * Returns a list containing all elements of the original collection and then all elements of the given [elements] array. - */ -public operator fun Collection.plus(elements: Array): List { - val result = ArrayList(this.size + elements.size) - result.addAll(this) - result.addAll(elements) - return result -} - -/** - * Returns a list containing all elements of the original collection and then all elements of the given [elements] collection. - */ -public operator fun Iterable.plus(elements: Iterable): List { - if (this is Collection) return this.plus(elements) - val result = ArrayList() - result.addAll(this) - result.addAll(elements) - return result -} - -/** - * Returns a list containing all elements of the original collection and then all elements of the given [elements] collection. - */ -public operator fun Collection.plus(elements: Iterable): List { - if (elements is Collection) { - val result = ArrayList(this.size + elements.size) - result.addAll(this) - result.addAll(elements) - return result - } else { - val result = ArrayList(this) - result.addAll(elements) - return result - } -} - -/** - * Returns a list containing all elements of the original collection and then all elements of the given [elements] sequence. - */ -public operator fun Iterable.plus(elements: Sequence): List { - val result = ArrayList() - result.addAll(this) - result.addAll(elements) - return result -} - -/** - * Returns a list containing all elements of the original collection and then all elements of the given [elements] sequence. - */ -public operator fun Collection.plus(elements: Sequence): List { - val result = ArrayList(this.size + 10) - result.addAll(this) - result.addAll(elements) - return result -} - -/** - * Returns a list containing all elements of the original collection and then the given [element]. - */ -@kotlin.internal.InlineOnly -public inline fun Iterable.plusElement(element: T): List { - return plus(element) -} - -/** - * Returns a list containing all elements of the original collection and then the given [element]. - */ -@kotlin.internal.InlineOnly -public inline fun Collection.plusElement(element: T): List { - return plus(element) -} - -/** - * Returns a list of snapshots of the window of the given [size] - * sliding along this collection with the given [step], where each - * snapshot is a list. - * - * Several last lists may have less elements than the given [size]. - * - * Both [size] and [step] must be positive and can be greater than the number of elements in this collection. - * @param size the number of elements to take in each window - * @param step the number of elements to move the window forward by on an each step, by default 1 - * @param partialWindows controls whether or not to keep partial windows in the end if any, - * by default `false` which means partial windows won't be preserved - * - * @sample samples.collections.Sequences.Transformations.takeWindows - */ -@SinceKotlin("1.2") -public fun Iterable.windowed(size: Int, step: Int = 1, partialWindows: Boolean = false): List> { - checkWindowSizeStep(size, step) - if (this is RandomAccess && this is List) { - val thisSize = this.size - val result = ArrayList>((thisSize + step - 1) / step) - var index = 0 - while (index < thisSize) { - val windowSize = size.coerceAtMost(thisSize - index) - if (windowSize < size && !partialWindows) break - result.add(List(windowSize) { this[it + index] }) - index += step - } - return result - } - val result = ArrayList>() - windowedIterator(iterator(), size, step, partialWindows, reuseBuffer = false).forEach { - result.add(it) - } - return result -} - -/** - * Returns a list of results of applying the given [transform] function to - * an each list representing a view over the window of the given [size] - * sliding along this collection with the given [step]. - * - * Note that the list passed to the [transform] function is ephemeral and is valid only inside that function. - * You should not store it or allow it to escape in some way, unless you made a snapshot of it. - * Several last lists may have less elements than the given [size]. - * - * Both [size] and [step] must be positive and can be greater than the number of elements in this collection. - * @param size the number of elements to take in each window - * @param step the number of elements to move the window forward by on an each step, by default 1 - * @param partialWindows controls whether or not to keep partial windows in the end if any, - * by default `false` which means partial windows won't be preserved - * - * @sample samples.collections.Sequences.Transformations.averageWindows - */ -@SinceKotlin("1.2") -public fun Iterable.windowed(size: Int, step: Int = 1, partialWindows: Boolean = false, transform: (List) -> R): List { - checkWindowSizeStep(size, step) - if (this is RandomAccess && this is List) { - val thisSize = this.size - val result = ArrayList((thisSize + step - 1) / step) - val window = MovingSubList(this) - var index = 0 - while (index < thisSize) { - window.move(index, (index + size).coerceAtMost(thisSize)) - if (!partialWindows && window.size < size) break - result.add(transform(window)) - index += step - } - return result - } - val result = ArrayList() - windowedIterator(iterator(), size, step, partialWindows, reuseBuffer = true).forEach { - result.add(transform(it)) - } - return result -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun Iterable.zip(other: Array): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun Iterable.zip(other: Array, transform: (a: T, b: R) -> V): List { - val arraySize = other.size - val list = ArrayList(minOf(collectionSizeOrDefault(10), arraySize)) - var i = 0 - for (element in this) { - if (i >= arraySize) break - list.add(transform(element, other[i++])) - } - return list -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun Iterable.zip(other: Iterable): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun Iterable.zip(other: Iterable, transform: (a: T, b: R) -> V): List { - val first = iterator() - val second = other.iterator() - val list = ArrayList(minOf(collectionSizeOrDefault(10), other.collectionSizeOrDefault(10))) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) - } - return list -} - -/** - * Returns a list of pairs of each two adjacent elements in this collection. - * - * The returned list is empty if this collection contains less than two elements. - * - * @sample samples.collections.Collections.Transformations.zipWithNext - */ -@SinceKotlin("1.2") -public fun Iterable.zipWithNext(): List> { - return zipWithNext { a, b -> a to b } -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to an each pair of two adjacent elements in this collection. - * - * The returned list is empty if this collection contains less than two elements. - * - * @sample samples.collections.Collections.Transformations.zipWithNextToFindDeltas - */ -@SinceKotlin("1.2") -public inline fun Iterable.zipWithNext(transform: (a: T, b: T) -> R): List { - val iterator = iterator() - if (!iterator.hasNext()) return emptyList() - val result = mutableListOf() - var current = iterator.next() - while (iterator.hasNext()) { - val next = iterator.next() - result.add(transform(current, next)) - current = next - } - return result -} - -/** - * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinTo - */ -public fun Iterable.joinTo(buffer: A, separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((T) -> CharSequence)? = null): A { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - buffer.appendElement(element, transform) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) - return buffer -} - -/** - * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinToString - */ -public fun Iterable.joinToString(separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((T) -> CharSequence)? = null): String { - return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() -} - -/** - * Returns this collection as an [Iterable]. - */ -@kotlin.internal.InlineOnly -public inline fun Iterable.asIterable(): Iterable { - return this -} - -/** - * Creates a [Sequence] instance that wraps the original collection returning its elements when being iterated. - * - * @sample samples.collections.Sequences.Building.sequenceFromCollection - */ -public fun Iterable.asSequence(): Sequence { - return Sequence { this.iterator() } -} - -/** - * Returns an average value of elements in the collection. - */ -@kotlin.jvm.JvmName("averageOfByte") -public fun Iterable.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the collection. - */ -@kotlin.jvm.JvmName("averageOfShort") -public fun Iterable.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the collection. - */ -@kotlin.jvm.JvmName("averageOfInt") -public fun Iterable.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the collection. - */ -@kotlin.jvm.JvmName("averageOfLong") -public fun Iterable.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the collection. - */ -@kotlin.jvm.JvmName("averageOfFloat") -public fun Iterable.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the collection. - */ -@kotlin.jvm.JvmName("averageOfDouble") -public fun Iterable.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns the sum of all elements in the collection. - */ -@kotlin.jvm.JvmName("sumOfByte") -public fun Iterable.sum(): Int { - var sum: Int = 0 - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the collection. - */ -@kotlin.jvm.JvmName("sumOfShort") -public fun Iterable.sum(): Int { - var sum: Int = 0 - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the collection. - */ -@kotlin.jvm.JvmName("sumOfInt") -public fun Iterable.sum(): Int { - var sum: Int = 0 - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the collection. - */ -@kotlin.jvm.JvmName("sumOfLong") -public fun Iterable.sum(): Long { - var sum: Long = 0L - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the collection. - */ -@kotlin.jvm.JvmName("sumOfFloat") -public fun Iterable.sum(): Float { - var sum: Float = 0.0f - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the collection. - */ -@kotlin.jvm.JvmName("sumOfDouble") -public fun Iterable.sum(): Double { - var sum: Double = 0.0 - for (element in this) { - sum += element - } - return sum -} - diff --git a/js/js.libraries/src/core/generated/_ComparisonsJs.kt b/js/js.libraries/src/core/generated/_ComparisonsJs.kt deleted file mode 100644 index 7f729bff685..00000000000 --- a/js/js.libraries/src/core/generated/_ComparisonsJs.kt +++ /dev/null @@ -1,299 +0,0 @@ -@file:kotlin.jvm.JvmMultifileClass -@file:kotlin.jvm.JvmName("ComparisonsKt") - -package kotlin.comparisons - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// - -import kotlin.js.* -import kotlin.* -import kotlin.text.* -import kotlin.comparisons.* - -/** - * Returns the greater of two values. - * If values are equal, returns the first one. - */ -@SinceKotlin("1.1") -public fun > maxOf(a: T, b: T): T { - return if (a >= b) a else b -} - -/** - * Returns the greater of two values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun maxOf(a: Byte, b: Byte): Byte { - return Math.max(a.toInt(), b.toInt()).unsafeCast() -} - -/** - * Returns the greater of two values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun maxOf(a: Short, b: Short): Short { - return Math.max(a.toInt(), b.toInt()).unsafeCast() -} - -/** - * Returns the greater of two values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun maxOf(a: Int, b: Int): Int { - return Math.max(a, b) -} - -/** - * Returns the greater of two values. - */ -@SinceKotlin("1.1") -@Suppress("NOTHING_TO_INLINE") -public inline fun maxOf(a: Long, b: Long): Long { - return if (a >= b) a else b -} - -/** - * Returns the greater of two values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun maxOf(a: Float, b: Float): Float { - return Math.max(a, b) -} - -/** - * Returns the greater of two values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun maxOf(a: Double, b: Double): Double { - return Math.max(a, b) -} - -/** - * Returns the greater of three values. - */ -@SinceKotlin("1.1") -public fun > maxOf(a: T, b: T, c: T): T { - return maxOf(a, maxOf(b, c)) -} - -/** - * Returns the greater of three values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun maxOf(a: Byte, b: Byte, c: Byte): Byte { - return Math.max(a.toInt(), b.toInt(), c.toInt()).unsafeCast() -} - -/** - * Returns the greater of three values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun maxOf(a: Short, b: Short, c: Short): Short { - return Math.max(a.toInt(), b.toInt(), c.toInt()).unsafeCast() -} - -/** - * Returns the greater of three values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun maxOf(a: Int, b: Int, c: Int): Int { - return Math.max(a, b, c) -} - -/** - * Returns the greater of three values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun maxOf(a: Long, b: Long, c: Long): Long { - return maxOf(a, maxOf(b, c)) -} - -/** - * Returns the greater of three values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun maxOf(a: Float, b: Float, c: Float): Float { - return Math.max(a, b, c) -} - -/** - * Returns the greater of three values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun maxOf(a: Double, b: Double, c: Double): Double { - return Math.max(a, b, c) -} - -/** - * Returns the greater of three values according to the order specified by the given [comparator]. - */ -@SinceKotlin("1.1") -public fun maxOf(a: T, b: T, c: T, comparator: Comparator): T { - return maxOf(a, maxOf(b, c, comparator), comparator) -} - -/** - * Returns the greater of two values according to the order specified by the given [comparator]. - * If values are equal, returns the first one. - */ -@SinceKotlin("1.1") -public fun maxOf(a: T, b: T, comparator: Comparator): T { - return if (comparator.compare(a, b) >= 0) a else b -} - -/** - * Returns the smaller of two values. - * If values are equal, returns the first one. - */ -@SinceKotlin("1.1") -public fun > minOf(a: T, b: T): T { - return if (a <= b) a else b -} - -/** - * Returns the smaller of two values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun minOf(a: Byte, b: Byte): Byte { - return Math.min(a.toInt(), b.toInt()).unsafeCast() -} - -/** - * Returns the smaller of two values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun minOf(a: Short, b: Short): Short { - return Math.min(a.toInt(), b.toInt()).unsafeCast() -} - -/** - * Returns the smaller of two values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun minOf(a: Int, b: Int): Int { - return Math.min(a, b) -} - -/** - * Returns the smaller of two values. - */ -@SinceKotlin("1.1") -@Suppress("NOTHING_TO_INLINE") -public inline fun minOf(a: Long, b: Long): Long { - return if (a <= b) a else b -} - -/** - * Returns the smaller of two values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun minOf(a: Float, b: Float): Float { - return Math.min(a, b) -} - -/** - * Returns the smaller of two values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun minOf(a: Double, b: Double): Double { - return Math.min(a, b) -} - -/** - * Returns the smaller of three values. - */ -@SinceKotlin("1.1") -public fun > minOf(a: T, b: T, c: T): T { - return minOf(a, minOf(b, c)) -} - -/** - * Returns the smaller of three values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun minOf(a: Byte, b: Byte, c: Byte): Byte { - return Math.min(a.toInt(), b.toInt(), c.toInt()).unsafeCast() -} - -/** - * Returns the smaller of three values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun minOf(a: Short, b: Short, c: Short): Short { - return Math.min(a.toInt(), b.toInt(), c.toInt()).unsafeCast() -} - -/** - * Returns the smaller of three values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun minOf(a: Int, b: Int, c: Int): Int { - return Math.min(a, b, c) -} - -/** - * Returns the smaller of three values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun minOf(a: Long, b: Long, c: Long): Long { - return minOf(a, minOf(b, c)) -} - -/** - * Returns the smaller of three values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun minOf(a: Float, b: Float, c: Float): Float { - return Math.min(a, b, c) -} - -/** - * Returns the smaller of three values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun minOf(a: Double, b: Double, c: Double): Double { - return Math.min(a, b, c) -} - -/** - * Returns the smaller of three values according to the order specified by the given [comparator]. - */ -@SinceKotlin("1.1") -public fun minOf(a: T, b: T, c: T, comparator: Comparator): T { - return minOf(a, minOf(b, c, comparator), comparator) -} - -/** - * Returns the smaller of two values according to the order specified by the given [comparator]. - * If values are equal, returns the first one. - */ -@SinceKotlin("1.1") -public fun minOf(a: T, b: T, comparator: Comparator): T { - return if (comparator.compare(a, b) <= 0) a else b -} - diff --git a/js/js.libraries/src/core/generated/_MapsJs.kt b/js/js.libraries/src/core/generated/_MapsJs.kt deleted file mode 100644 index 97c8749fd64..00000000000 --- a/js/js.libraries/src/core/generated/_MapsJs.kt +++ /dev/null @@ -1,218 +0,0 @@ -@file:kotlin.jvm.JvmMultifileClass -@file:kotlin.jvm.JvmName("MapsKt") - -package kotlin.collections - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// - -import kotlin.js.* -import kotlin.* -import kotlin.text.* -import kotlin.comparisons.* - -/** - * Returns a [List] containing all key-value pairs. - */ -public fun Map.toList(): List> { - if (size == 0) - return emptyList() - val iterator = entries.iterator() - if (!iterator.hasNext()) - return emptyList() - val first = iterator.next() - if (!iterator.hasNext()) - return listOf(first.toPair()) - val result = ArrayList>(size) - result.add(first.toPair()) - do { - result.add(iterator.next().toPair()) - } while (iterator.hasNext()) - return result -} - -/** - * Returns a single list of all elements yielded from results of [transform] function being invoked on each entry of original map. - */ -public inline fun Map.flatMap(transform: (Map.Entry) -> Iterable): List { - return flatMapTo(ArrayList(), transform) -} - -/** - * Appends all elements yielded from results of [transform] function being invoked on each entry of original map, to the given [destination]. - */ -public inline fun > Map.flatMapTo(destination: C, transform: (Map.Entry) -> Iterable): C { - for (element in this) { - val list = transform(element) - destination.addAll(list) - } - return destination -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each entry in the original map. - */ -public inline fun Map.map(transform: (Map.Entry) -> R): List { - return mapTo(ArrayList(size), transform) -} - -/** - * Returns a list containing only the non-null results of applying the given [transform] function - * to each entry in the original map. - */ -public inline fun Map.mapNotNull(transform: (Map.Entry) -> R?): List { - return mapNotNullTo(ArrayList(), transform) -} - -/** - * Applies the given [transform] function to each entry in the original map - * and appends only the non-null results to the given [destination]. - */ -public inline fun > Map.mapNotNullTo(destination: C, transform: (Map.Entry) -> R?): C { - forEach { element -> transform(element)?.let { destination.add(it) } } - return destination -} - -/** - * Applies the given [transform] function to each entry of the original map - * and appends the results to the given [destination]. - */ -public inline fun > Map.mapTo(destination: C, transform: (Map.Entry) -> R): C { - for (item in this) - destination.add(transform(item)) - return destination -} - -/** - * Returns `true` if all entries match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.all - */ -public inline fun Map.all(predicate: (Map.Entry) -> Boolean): Boolean { - if (isEmpty()) return true - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if map has at least one entry. - * - * @sample samples.collections.Collections.Aggregates.any - */ -public fun Map.any(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if at least one entry matches the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.anyWithPredicate - */ -public inline fun Map.any(predicate: (Map.Entry) -> Boolean): Boolean { - if (isEmpty()) return false - for (element in this) if (predicate(element)) return true - return false -} - -/** - * Returns the number of entries in this map. - */ -@kotlin.internal.InlineOnly -public inline fun Map.count(): Int { - return size -} - -/** - * Returns the number of entries matching the given [predicate]. - */ -public inline fun Map.count(predicate: (Map.Entry) -> Boolean): Int { - if (isEmpty()) return 0 - var count = 0 - for (element in this) if (predicate(element)) count++ - return count -} - -/** - * Performs the given [action] on each entry. - */ -@kotlin.internal.HidesMembers -public inline fun Map.forEach(action: (Map.Entry) -> Unit): Unit { - for (element in this) action(element) -} - -/** - * Returns the first entry yielding the largest value of the given function or `null` if there are no entries. - */ -@kotlin.internal.InlineOnly -public inline fun > Map.maxBy(selector: (Map.Entry) -> R): Map.Entry? { - return entries.maxBy(selector) -} - -/** - * Returns the first entry having the largest value according to the provided [comparator] or `null` if there are no entries. - */ -@kotlin.internal.InlineOnly -public inline fun Map.maxWith(comparator: Comparator>): Map.Entry? { - return entries.maxWith(comparator) -} - -/** - * Returns the first entry yielding the smallest value of the given function or `null` if there are no entries. - */ -public inline fun > Map.minBy(selector: (Map.Entry) -> R): Map.Entry? { - return entries.minBy(selector) -} - -/** - * Returns the first entry having the smallest value according to the provided [comparator] or `null` if there are no entries. - */ -public fun Map.minWith(comparator: Comparator>): Map.Entry? { - return entries.minWith(comparator) -} - -/** - * Returns `true` if the map has no entries. - * - * @sample samples.collections.Collections.Aggregates.none - */ -public fun Map.none(): Boolean { - return isEmpty() -} - -/** - * Returns `true` if no entries match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.noneWithPredicate - */ -public inline fun Map.none(predicate: (Map.Entry) -> Boolean): Boolean { - if (isEmpty()) return true - for (element in this) if (predicate(element)) return false - return true -} - -/** - * Performs the given [action] on each entry and returns the map itself afterwards. - */ -@SinceKotlin("1.1") -public inline fun > M.onEach(action: (Map.Entry) -> Unit): M { - return apply { for (element in this) action(element) } -} - -/** - * Creates an [Iterable] instance that wraps the original map returning its entries when being iterated. - */ -@kotlin.internal.InlineOnly -public inline fun Map.asIterable(): Iterable> { - return entries -} - -/** - * Creates a [Sequence] instance that wraps the original map returning its entries when being iterated. - */ -public fun Map.asSequence(): Sequence> { - return entries.asSequence() -} - diff --git a/js/js.libraries/src/core/generated/_RangesJs.kt b/js/js.libraries/src/core/generated/_RangesJs.kt deleted file mode 100644 index 34129003aa1..00000000000 --- a/js/js.libraries/src/core/generated/_RangesJs.kt +++ /dev/null @@ -1,961 +0,0 @@ -@file:kotlin.jvm.JvmMultifileClass -@file:kotlin.jvm.JvmName("RangesKt") - -package kotlin.ranges - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// - -import kotlin.js.* -import kotlin.* -import kotlin.text.* -import kotlin.comparisons.* - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("intRangeContains") -public operator fun ClosedRange.contains(value: Byte): Boolean { - return contains(value.toInt()) -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("longRangeContains") -public operator fun ClosedRange.contains(value: Byte): Boolean { - return contains(value.toLong()) -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("shortRangeContains") -public operator fun ClosedRange.contains(value: Byte): Boolean { - return contains(value.toShort()) -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("doubleRangeContains") -public operator fun ClosedRange.contains(value: Byte): Boolean { - return contains(value.toDouble()) -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("floatRangeContains") -public operator fun ClosedRange.contains(value: Byte): Boolean { - return contains(value.toFloat()) -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("intRangeContains") -public operator fun ClosedRange.contains(value: Double): Boolean { - return value.toIntExactOrNull().let { if (it != null) contains(it) else false } -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("longRangeContains") -public operator fun ClosedRange.contains(value: Double): Boolean { - return value.toLongExactOrNull().let { if (it != null) contains(it) else false } -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("byteRangeContains") -public operator fun ClosedRange.contains(value: Double): Boolean { - return value.toByteExactOrNull().let { if (it != null) contains(it) else false } -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("shortRangeContains") -public operator fun ClosedRange.contains(value: Double): Boolean { - return value.toShortExactOrNull().let { if (it != null) contains(it) else false } -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("floatRangeContains") -public operator fun ClosedRange.contains(value: Double): Boolean { - return contains(value.toFloat()) -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("intRangeContains") -public operator fun ClosedRange.contains(value: Float): Boolean { - return value.toIntExactOrNull().let { if (it != null) contains(it) else false } -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("longRangeContains") -public operator fun ClosedRange.contains(value: Float): Boolean { - return value.toLongExactOrNull().let { if (it != null) contains(it) else false } -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("byteRangeContains") -public operator fun ClosedRange.contains(value: Float): Boolean { - return value.toByteExactOrNull().let { if (it != null) contains(it) else false } -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("shortRangeContains") -public operator fun ClosedRange.contains(value: Float): Boolean { - return value.toShortExactOrNull().let { if (it != null) contains(it) else false } -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("doubleRangeContains") -public operator fun ClosedRange.contains(value: Float): Boolean { - return contains(value.toDouble()) -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("longRangeContains") -public operator fun ClosedRange.contains(value: Int): Boolean { - return contains(value.toLong()) -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("byteRangeContains") -public operator fun ClosedRange.contains(value: Int): Boolean { - return value.toByteExactOrNull().let { if (it != null) contains(it) else false } -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("shortRangeContains") -public operator fun ClosedRange.contains(value: Int): Boolean { - return value.toShortExactOrNull().let { if (it != null) contains(it) else false } -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("doubleRangeContains") -public operator fun ClosedRange.contains(value: Int): Boolean { - return contains(value.toDouble()) -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("floatRangeContains") -public operator fun ClosedRange.contains(value: Int): Boolean { - return contains(value.toFloat()) -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("intRangeContains") -public operator fun ClosedRange.contains(value: Long): Boolean { - return value.toIntExactOrNull().let { if (it != null) contains(it) else false } -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("byteRangeContains") -public operator fun ClosedRange.contains(value: Long): Boolean { - return value.toByteExactOrNull().let { if (it != null) contains(it) else false } -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("shortRangeContains") -public operator fun ClosedRange.contains(value: Long): Boolean { - return value.toShortExactOrNull().let { if (it != null) contains(it) else false } -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("doubleRangeContains") -public operator fun ClosedRange.contains(value: Long): Boolean { - return contains(value.toDouble()) -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("floatRangeContains") -public operator fun ClosedRange.contains(value: Long): Boolean { - return contains(value.toFloat()) -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("intRangeContains") -public operator fun ClosedRange.contains(value: Short): Boolean { - return contains(value.toInt()) -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("longRangeContains") -public operator fun ClosedRange.contains(value: Short): Boolean { - return contains(value.toLong()) -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("byteRangeContains") -public operator fun ClosedRange.contains(value: Short): Boolean { - return value.toByteExactOrNull().let { if (it != null) contains(it) else false } -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("doubleRangeContains") -public operator fun ClosedRange.contains(value: Short): Boolean { - return contains(value.toDouble()) -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("floatRangeContains") -public operator fun ClosedRange.contains(value: Short): Boolean { - return contains(value.toFloat()) -} - -/** - * Returns a progression from this value down to the specified [to] value with the step -1. - * - * The [to] value has to be less than this value. - */ -public infix fun Int.downTo(to: Byte): IntProgression { - return IntProgression.fromClosedRange(this, to.toInt(), -1) -} - -/** - * Returns a progression from this value down to the specified [to] value with the step -1. - * - * The [to] value has to be less than this value. - */ -public infix fun Long.downTo(to: Byte): LongProgression { - return LongProgression.fromClosedRange(this, to.toLong(), -1L) -} - -/** - * Returns a progression from this value down to the specified [to] value with the step -1. - * - * The [to] value has to be less than this value. - */ -public infix fun Byte.downTo(to: Byte): IntProgression { - return IntProgression.fromClosedRange(this.toInt(), to.toInt(), -1) -} - -/** - * Returns a progression from this value down to the specified [to] value with the step -1. - * - * The [to] value has to be less than this value. - */ -public infix fun Short.downTo(to: Byte): IntProgression { - return IntProgression.fromClosedRange(this.toInt(), to.toInt(), -1) -} - -/** - * Returns a progression from this value down to the specified [to] value with the step -1. - * - * The [to] value has to be less than this value. - */ -public infix fun Char.downTo(to: Char): CharProgression { - return CharProgression.fromClosedRange(this, to, -1) -} - -/** - * Returns a progression from this value down to the specified [to] value with the step -1. - * - * The [to] value has to be less than this value. - */ -public infix fun Int.downTo(to: Int): IntProgression { - return IntProgression.fromClosedRange(this, to, -1) -} - -/** - * Returns a progression from this value down to the specified [to] value with the step -1. - * - * The [to] value has to be less than this value. - */ -public infix fun Long.downTo(to: Int): LongProgression { - return LongProgression.fromClosedRange(this, to.toLong(), -1L) -} - -/** - * Returns a progression from this value down to the specified [to] value with the step -1. - * - * The [to] value has to be less than this value. - */ -public infix fun Byte.downTo(to: Int): IntProgression { - return IntProgression.fromClosedRange(this.toInt(), to, -1) -} - -/** - * Returns a progression from this value down to the specified [to] value with the step -1. - * - * The [to] value has to be less than this value. - */ -public infix fun Short.downTo(to: Int): IntProgression { - return IntProgression.fromClosedRange(this.toInt(), to, -1) -} - -/** - * Returns a progression from this value down to the specified [to] value with the step -1. - * - * The [to] value has to be less than this value. - */ -public infix fun Int.downTo(to: Long): LongProgression { - return LongProgression.fromClosedRange(this.toLong(), to, -1L) -} - -/** - * Returns a progression from this value down to the specified [to] value with the step -1. - * - * The [to] value has to be less than this value. - */ -public infix fun Long.downTo(to: Long): LongProgression { - return LongProgression.fromClosedRange(this, to, -1L) -} - -/** - * Returns a progression from this value down to the specified [to] value with the step -1. - * - * The [to] value has to be less than this value. - */ -public infix fun Byte.downTo(to: Long): LongProgression { - return LongProgression.fromClosedRange(this.toLong(), to, -1L) -} - -/** - * Returns a progression from this value down to the specified [to] value with the step -1. - * - * The [to] value has to be less than this value. - */ -public infix fun Short.downTo(to: Long): LongProgression { - return LongProgression.fromClosedRange(this.toLong(), to, -1L) -} - -/** - * Returns a progression from this value down to the specified [to] value with the step -1. - * - * The [to] value has to be less than this value. - */ -public infix fun Int.downTo(to: Short): IntProgression { - return IntProgression.fromClosedRange(this, to.toInt(), -1) -} - -/** - * Returns a progression from this value down to the specified [to] value with the step -1. - * - * The [to] value has to be less than this value. - */ -public infix fun Long.downTo(to: Short): LongProgression { - return LongProgression.fromClosedRange(this, to.toLong(), -1L) -} - -/** - * Returns a progression from this value down to the specified [to] value with the step -1. - * - * The [to] value has to be less than this value. - */ -public infix fun Byte.downTo(to: Short): IntProgression { - return IntProgression.fromClosedRange(this.toInt(), to.toInt(), -1) -} - -/** - * Returns a progression from this value down to the specified [to] value with the step -1. - * - * The [to] value has to be less than this value. - */ -public infix fun Short.downTo(to: Short): IntProgression { - return IntProgression.fromClosedRange(this.toInt(), to.toInt(), -1) -} - -/** - * Returns a progression that goes over the same range in the opposite direction with the same step. - */ -public fun IntProgression.reversed(): IntProgression { - return IntProgression.fromClosedRange(last, first, -step) -} - -/** - * Returns a progression that goes over the same range in the opposite direction with the same step. - */ -public fun LongProgression.reversed(): LongProgression { - return LongProgression.fromClosedRange(last, first, -step) -} - -/** - * Returns a progression that goes over the same range in the opposite direction with the same step. - */ -public fun CharProgression.reversed(): CharProgression { - return CharProgression.fromClosedRange(last, first, -step) -} - -/** - * Returns a progression that goes over the same range with the given step. - */ -public infix fun IntProgression.step(step: Int): IntProgression { - checkStepIsPositive(step > 0, step) - return IntProgression.fromClosedRange(first, last, if (this.step > 0) step else -step) -} - -/** - * Returns a progression that goes over the same range with the given step. - */ -public infix fun LongProgression.step(step: Long): LongProgression { - checkStepIsPositive(step > 0, step) - return LongProgression.fromClosedRange(first, last, if (this.step > 0) step else -step) -} - -/** - * Returns a progression that goes over the same range with the given step. - */ -public infix fun CharProgression.step(step: Int): CharProgression { - checkStepIsPositive(step > 0, step) - return CharProgression.fromClosedRange(first, last, if (this.step > 0) step else -step) -} - -internal fun Int.toByteExactOrNull(): Byte? { - return if (this in Byte.MIN_VALUE.toInt()..Byte.MAX_VALUE.toInt()) this.toByte() else null -} - -internal fun Long.toByteExactOrNull(): Byte? { - return if (this in Byte.MIN_VALUE.toLong()..Byte.MAX_VALUE.toLong()) this.toByte() else null -} - -internal fun Short.toByteExactOrNull(): Byte? { - return if (this in Byte.MIN_VALUE.toShort()..Byte.MAX_VALUE.toShort()) this.toByte() else null -} - -internal fun Double.toByteExactOrNull(): Byte? { - return if (this in Byte.MIN_VALUE.toDouble()..Byte.MAX_VALUE.toDouble()) this.toByte() else null -} - -internal fun Float.toByteExactOrNull(): Byte? { - return if (this in Byte.MIN_VALUE.toFloat()..Byte.MAX_VALUE.toFloat()) this.toByte() else null -} - -internal fun Long.toIntExactOrNull(): Int? { - return if (this in Int.MIN_VALUE.toLong()..Int.MAX_VALUE.toLong()) this.toInt() else null -} - -internal fun Double.toIntExactOrNull(): Int? { - return if (this in Int.MIN_VALUE.toDouble()..Int.MAX_VALUE.toDouble()) this.toInt() else null -} - -internal fun Float.toIntExactOrNull(): Int? { - return if (this in Int.MIN_VALUE.toFloat()..Int.MAX_VALUE.toFloat()) this.toInt() else null -} - -internal fun Double.toLongExactOrNull(): Long? { - return if (this in Long.MIN_VALUE.toDouble()..Long.MAX_VALUE.toDouble()) this.toLong() else null -} - -internal fun Float.toLongExactOrNull(): Long? { - return if (this in Long.MIN_VALUE.toFloat()..Long.MAX_VALUE.toFloat()) this.toLong() else null -} - -internal fun Int.toShortExactOrNull(): Short? { - return if (this in Short.MIN_VALUE.toInt()..Short.MAX_VALUE.toInt()) this.toShort() else null -} - -internal fun Long.toShortExactOrNull(): Short? { - return if (this in Short.MIN_VALUE.toLong()..Short.MAX_VALUE.toLong()) this.toShort() else null -} - -internal fun Double.toShortExactOrNull(): Short? { - return if (this in Short.MIN_VALUE.toDouble()..Short.MAX_VALUE.toDouble()) this.toShort() else null -} - -internal fun Float.toShortExactOrNull(): Short? { - return if (this in Short.MIN_VALUE.toFloat()..Short.MAX_VALUE.toFloat()) this.toShort() else null -} - -/** - * Returns a range from this value up to but excluding the specified [to] value. - */ -public infix fun Int.until(to: Byte): IntRange { - return this .. (to.toInt() - 1).toInt() -} - -/** - * Returns a range from this value up to but excluding the specified [to] value. - */ -public infix fun Long.until(to: Byte): LongRange { - return this .. (to.toLong() - 1).toLong() -} - -/** - * Returns a range from this value up to but excluding the specified [to] value. - */ -public infix fun Byte.until(to: Byte): IntRange { - return this.toInt() .. (to.toInt() - 1).toInt() -} - -/** - * Returns a range from this value up to but excluding the specified [to] value. - */ -public infix fun Short.until(to: Byte): IntRange { - return this.toInt() .. (to.toInt() - 1).toInt() -} - -/** - * Returns a range from this value up to but excluding the specified [to] value. - * - * If the [to] value is less than or equal to `'\u0000'` the returned range is empty. - */ -public infix fun Char.until(to: Char): CharRange { - if (to <= '\u0000') return CharRange.EMPTY - return this .. (to - 1).toChar() -} - -/** - * Returns a range from this value up to but excluding the specified [to] value. - * - * If the [to] value is less than or equal to [Int.MIN_VALUE] the returned range is empty. - */ -public infix fun Int.until(to: Int): IntRange { - if (to <= Int.MIN_VALUE) return IntRange.EMPTY - return this .. (to - 1).toInt() -} - -/** - * Returns a range from this value up to but excluding the specified [to] value. - */ -public infix fun Long.until(to: Int): LongRange { - return this .. (to.toLong() - 1).toLong() -} - -/** - * Returns a range from this value up to but excluding the specified [to] value. - * - * If the [to] value is less than or equal to [Int.MIN_VALUE] the returned range is empty. - */ -public infix fun Byte.until(to: Int): IntRange { - if (to <= Int.MIN_VALUE) return IntRange.EMPTY - return this.toInt() .. (to - 1).toInt() -} - -/** - * Returns a range from this value up to but excluding the specified [to] value. - * - * If the [to] value is less than or equal to [Int.MIN_VALUE] the returned range is empty. - */ -public infix fun Short.until(to: Int): IntRange { - if (to <= Int.MIN_VALUE) return IntRange.EMPTY - return this.toInt() .. (to - 1).toInt() -} - -/** - * Returns a range from this value up to but excluding the specified [to] value. - * - * If the [to] value is less than or equal to [Long.MIN_VALUE] the returned range is empty. - */ -public infix fun Int.until(to: Long): LongRange { - if (to <= Long.MIN_VALUE) return LongRange.EMPTY - return this.toLong() .. (to - 1).toLong() -} - -/** - * Returns a range from this value up to but excluding the specified [to] value. - * - * If the [to] value is less than or equal to [Long.MIN_VALUE] the returned range is empty. - */ -public infix fun Long.until(to: Long): LongRange { - if (to <= Long.MIN_VALUE) return LongRange.EMPTY - return this .. (to - 1).toLong() -} - -/** - * Returns a range from this value up to but excluding the specified [to] value. - * - * If the [to] value is less than or equal to [Long.MIN_VALUE] the returned range is empty. - */ -public infix fun Byte.until(to: Long): LongRange { - if (to <= Long.MIN_VALUE) return LongRange.EMPTY - return this.toLong() .. (to - 1).toLong() -} - -/** - * Returns a range from this value up to but excluding the specified [to] value. - * - * If the [to] value is less than or equal to [Long.MIN_VALUE] the returned range is empty. - */ -public infix fun Short.until(to: Long): LongRange { - if (to <= Long.MIN_VALUE) return LongRange.EMPTY - return this.toLong() .. (to - 1).toLong() -} - -/** - * Returns a range from this value up to but excluding the specified [to] value. - */ -public infix fun Int.until(to: Short): IntRange { - return this .. (to.toInt() - 1).toInt() -} - -/** - * Returns a range from this value up to but excluding the specified [to] value. - */ -public infix fun Long.until(to: Short): LongRange { - return this .. (to.toLong() - 1).toLong() -} - -/** - * Returns a range from this value up to but excluding the specified [to] value. - */ -public infix fun Byte.until(to: Short): IntRange { - return this.toInt() .. (to.toInt() - 1).toInt() -} - -/** - * Returns a range from this value up to but excluding the specified [to] value. - */ -public infix fun Short.until(to: Short): IntRange { - return this.toInt() .. (to.toInt() - 1).toInt() -} - -/** - * Ensures that this value is not less than the specified [minimumValue]. - * - * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. - * @sample samples.comparisons.ComparableOps.coerceAtLeastComparable - */ -public fun > T.coerceAtLeast(minimumValue: T): T { - return if (this < minimumValue) minimumValue else this -} - -/** - * Ensures that this value is not less than the specified [minimumValue]. - * - * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. - * @sample samples.comparisons.ComparableOps.coerceAtLeast - */ -public fun Byte.coerceAtLeast(minimumValue: Byte): Byte { - return if (this < minimumValue) minimumValue else this -} - -/** - * Ensures that this value is not less than the specified [minimumValue]. - * - * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. - * @sample samples.comparisons.ComparableOps.coerceAtLeast - */ -public fun Short.coerceAtLeast(minimumValue: Short): Short { - return if (this < minimumValue) minimumValue else this -} - -/** - * Ensures that this value is not less than the specified [minimumValue]. - * - * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. - * @sample samples.comparisons.ComparableOps.coerceAtLeast - */ -public fun Int.coerceAtLeast(minimumValue: Int): Int { - return if (this < minimumValue) minimumValue else this -} - -/** - * Ensures that this value is not less than the specified [minimumValue]. - * - * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. - * @sample samples.comparisons.ComparableOps.coerceAtLeast - */ -public fun Long.coerceAtLeast(minimumValue: Long): Long { - return if (this < minimumValue) minimumValue else this -} - -/** - * Ensures that this value is not less than the specified [minimumValue]. - * - * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. - * @sample samples.comparisons.ComparableOps.coerceAtLeast - */ -public fun Float.coerceAtLeast(minimumValue: Float): Float { - return if (this < minimumValue) minimumValue else this -} - -/** - * Ensures that this value is not less than the specified [minimumValue]. - * - * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. - * @sample samples.comparisons.ComparableOps.coerceAtLeast - */ -public fun Double.coerceAtLeast(minimumValue: Double): Double { - return if (this < minimumValue) minimumValue else this -} - -/** - * Ensures that this value is not greater than the specified [maximumValue]. - * - * @return this value if it's less than or equal to the [maximumValue] or the [maximumValue] otherwise. - * @sample samples.comparisons.ComparableOps.coerceAtMostComparable - */ -public fun > T.coerceAtMost(maximumValue: T): T { - return if (this > maximumValue) maximumValue else this -} - -/** - * Ensures that this value is not greater than the specified [maximumValue]. - * - * @return this value if it's less than or equal to the [maximumValue] or the [maximumValue] otherwise. - * @sample samples.comparisons.ComparableOps.coerceAtMost - */ -public fun Byte.coerceAtMost(maximumValue: Byte): Byte { - return if (this > maximumValue) maximumValue else this -} - -/** - * Ensures that this value is not greater than the specified [maximumValue]. - * - * @return this value if it's less than or equal to the [maximumValue] or the [maximumValue] otherwise. - * @sample samples.comparisons.ComparableOps.coerceAtMost - */ -public fun Short.coerceAtMost(maximumValue: Short): Short { - return if (this > maximumValue) maximumValue else this -} - -/** - * Ensures that this value is not greater than the specified [maximumValue]. - * - * @return this value if it's less than or equal to the [maximumValue] or the [maximumValue] otherwise. - * @sample samples.comparisons.ComparableOps.coerceAtMost - */ -public fun Int.coerceAtMost(maximumValue: Int): Int { - return if (this > maximumValue) maximumValue else this -} - -/** - * Ensures that this value is not greater than the specified [maximumValue]. - * - * @return this value if it's less than or equal to the [maximumValue] or the [maximumValue] otherwise. - * @sample samples.comparisons.ComparableOps.coerceAtMost - */ -public fun Long.coerceAtMost(maximumValue: Long): Long { - return if (this > maximumValue) maximumValue else this -} - -/** - * Ensures that this value is not greater than the specified [maximumValue]. - * - * @return this value if it's less than or equal to the [maximumValue] or the [maximumValue] otherwise. - * @sample samples.comparisons.ComparableOps.coerceAtMost - */ -public fun Float.coerceAtMost(maximumValue: Float): Float { - return if (this > maximumValue) maximumValue else this -} - -/** - * Ensures that this value is not greater than the specified [maximumValue]. - * - * @return this value if it's less than or equal to the [maximumValue] or the [maximumValue] otherwise. - * @sample samples.comparisons.ComparableOps.coerceAtMost - */ -public fun Double.coerceAtMost(maximumValue: Double): Double { - return if (this > maximumValue) maximumValue else this -} - -/** - * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. - * - * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. - * @sample samples.comparisons.ComparableOps.coerceInComparable - */ -public fun > T.coerceIn(minimumValue: T?, maximumValue: T?): T { - if (minimumValue !== null && maximumValue !== null) { - if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") - if (this < minimumValue) return minimumValue - if (this > maximumValue) return maximumValue - } - else { - if (minimumValue !== null && this < minimumValue) return minimumValue - if (maximumValue !== null && this > maximumValue) return maximumValue - } - return this -} - -/** - * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. - * - * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. - * @sample samples.comparisons.ComparableOps.coerceIn - */ -public fun Byte.coerceIn(minimumValue: Byte, maximumValue: Byte): Byte { - if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") - if (this < minimumValue) return minimumValue - if (this > maximumValue) return maximumValue - return this -} - -/** - * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. - * - * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. - * @sample samples.comparisons.ComparableOps.coerceIn - */ -public fun Short.coerceIn(minimumValue: Short, maximumValue: Short): Short { - if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") - if (this < minimumValue) return minimumValue - if (this > maximumValue) return maximumValue - return this -} - -/** - * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. - * - * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. - * @sample samples.comparisons.ComparableOps.coerceIn - */ -public fun Int.coerceIn(minimumValue: Int, maximumValue: Int): Int { - if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") - if (this < minimumValue) return minimumValue - if (this > maximumValue) return maximumValue - return this -} - -/** - * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. - * - * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. - * @sample samples.comparisons.ComparableOps.coerceIn - */ -public fun Long.coerceIn(minimumValue: Long, maximumValue: Long): Long { - if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") - if (this < minimumValue) return minimumValue - if (this > maximumValue) return maximumValue - return this -} - -/** - * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. - * - * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. - * @sample samples.comparisons.ComparableOps.coerceIn - */ -public fun Float.coerceIn(minimumValue: Float, maximumValue: Float): Float { - if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") - if (this < minimumValue) return minimumValue - if (this > maximumValue) return maximumValue - return this -} - -/** - * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. - * - * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. - * @sample samples.comparisons.ComparableOps.coerceIn - */ -public fun Double.coerceIn(minimumValue: Double, maximumValue: Double): Double { - if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") - if (this < minimumValue) return minimumValue - if (this > maximumValue) return maximumValue - return this -} - -/** - * Ensures that this value lies in the specified [range]. - * - * @return this value if it's in the [range], or `range.start` if this value is less than `range.start`, or `range.endInclusive` if this value is greater than `range.endInclusive`. - * @sample samples.comparisons.ComparableOps.coerceInFloatingPointRange - */ -@SinceKotlin("1.1") -public fun > T.coerceIn(range: ClosedFloatingPointRange): T { - if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") - return when { - // this < start equiv to this <= start && !(this >= start) - range.lessThanOrEquals(this, range.start) && !range.lessThanOrEquals(range.start, this) -> range.start - // this > end equiv to this >= end && !(this <= end) - range.lessThanOrEquals(range.endInclusive, this) && !range.lessThanOrEquals(this, range.endInclusive) -> range.endInclusive - else -> this - } -} - -/** - * Ensures that this value lies in the specified [range]. - * - * @return this value if it's in the [range], or `range.start` if this value is less than `range.start`, or `range.endInclusive` if this value is greater than `range.endInclusive`. - * @sample samples.comparisons.ComparableOps.coerceInComparable - */ -public fun > T.coerceIn(range: ClosedRange): T { - if (range is ClosedFloatingPointRange) { - return this.coerceIn(range) - } - if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") - return when { - this < range.start -> range.start - this > range.endInclusive -> range.endInclusive - else -> this - } -} - -/** - * Ensures that this value lies in the specified [range]. - * - * @return this value if it's in the [range], or `range.start` if this value is less than `range.start`, or `range.endInclusive` if this value is greater than `range.endInclusive`. - * @sample samples.comparisons.ComparableOps.coerceIn - */ -public fun Int.coerceIn(range: ClosedRange): Int { - if (range is ClosedFloatingPointRange) { - return this.coerceIn(range) - } - if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") - return when { - this < range.start -> range.start - this > range.endInclusive -> range.endInclusive - else -> this - } -} - -/** - * Ensures that this value lies in the specified [range]. - * - * @return this value if it's in the [range], or `range.start` if this value is less than `range.start`, or `range.endInclusive` if this value is greater than `range.endInclusive`. - * @sample samples.comparisons.ComparableOps.coerceIn - */ -public fun Long.coerceIn(range: ClosedRange): Long { - if (range is ClosedFloatingPointRange) { - return this.coerceIn(range) - } - if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") - return when { - this < range.start -> range.start - this > range.endInclusive -> range.endInclusive - else -> this - } -} - diff --git a/js/js.libraries/src/core/generated/_SequencesJs.kt b/js/js.libraries/src/core/generated/_SequencesJs.kt deleted file mode 100644 index 32b55408c4d..00000000000 --- a/js/js.libraries/src/core/generated/_SequencesJs.kt +++ /dev/null @@ -1,1849 +0,0 @@ -@file:kotlin.jvm.JvmMultifileClass -@file:kotlin.jvm.JvmName("SequencesKt") - -package kotlin.sequences - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// - -import kotlin.js.* -import kotlin.* -import kotlin.text.* -import kotlin.comparisons.* -import kotlin.coroutines.experimental.* - -/** - * Returns `true` if [element] is found in the sequence. - * - * The operation is _terminal_. - */ -public operator fun <@kotlin.internal.OnlyInputTypes T> Sequence.contains(element: T): Boolean { - return indexOf(element) >= 0 -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this sequence. - * - * The operation is _terminal_. - */ -public fun Sequence.elementAt(index: Int): T { - return elementAtOrElse(index) { throw IndexOutOfBoundsException("Sequence doesn't contain element at index $index.") } -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this sequence. - * - * The operation is _terminal_. - */ -public fun Sequence.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T { - if (index < 0) - return defaultValue(index) - val iterator = iterator() - var count = 0 - while (iterator.hasNext()) { - val element = iterator.next() - if (index == count++) - return element - } - return defaultValue(index) -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this sequence. - * - * The operation is _terminal_. - */ -public fun Sequence.elementAtOrNull(index: Int): T? { - if (index < 0) - return null - val iterator = iterator() - var count = 0 - while (iterator.hasNext()) { - val element = iterator.next() - if (index == count++) - return element - } - return null -} - -/** - * Returns the first element matching the given [predicate], or `null` if no such element was found. - * - * The operation is _terminal_. - */ -@kotlin.internal.InlineOnly -public inline fun Sequence.find(predicate: (T) -> Boolean): T? { - return firstOrNull(predicate) -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - * - * The operation is _terminal_. - */ -@kotlin.internal.InlineOnly -public inline fun Sequence.findLast(predicate: (T) -> Boolean): T? { - return lastOrNull(predicate) -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the sequence is empty. - * - * The operation is _terminal_. - */ -public fun Sequence.first(): T { - val iterator = iterator() - if (!iterator.hasNext()) - throw NoSuchElementException("Sequence is empty.") - return iterator.next() -} - -/** - * Returns the first element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - * - * The operation is _terminal_. - */ -public inline fun Sequence.first(predicate: (T) -> Boolean): T { - for (element in this) if (predicate(element)) return element - throw NoSuchElementException("Sequence contains no element matching the predicate.") -} - -/** - * Returns the first element, or `null` if the sequence is empty. - * - * The operation is _terminal_. - */ -public fun Sequence.firstOrNull(): T? { - val iterator = iterator() - if (!iterator.hasNext()) - return null - return iterator.next() -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - * - * The operation is _terminal_. - */ -public inline fun Sequence.firstOrNull(predicate: (T) -> Boolean): T? { - for (element in this) if (predicate(element)) return element - return null -} - -/** - * Returns first index of [element], or -1 if the sequence does not contain element. - * - * The operation is _terminal_. - */ -public fun <@kotlin.internal.OnlyInputTypes T> Sequence.indexOf(element: T): Int { - var index = 0 - for (item in this) { - if (element == item) - return index - index++ - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the sequence does not contain such element. - * - * The operation is _terminal_. - */ -public inline fun Sequence.indexOfFirst(predicate: (T) -> Boolean): Int { - var index = 0 - for (item in this) { - if (predicate(item)) - return index - index++ - } - return -1 -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the sequence does not contain such element. - * - * The operation is _terminal_. - */ -public inline fun Sequence.indexOfLast(predicate: (T) -> Boolean): Int { - var lastIndex = -1 - var index = 0 - for (item in this) { - if (predicate(item)) - lastIndex = index - index++ - } - return lastIndex -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the sequence is empty. - * - * The operation is _terminal_. - */ -public fun Sequence.last(): T { - val iterator = iterator() - if (!iterator.hasNext()) - throw NoSuchElementException("Sequence is empty.") - var last = iterator.next() - while (iterator.hasNext()) - last = iterator.next() - return last -} - -/** - * Returns the last element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - * - * The operation is _terminal_. - */ -public inline fun Sequence.last(predicate: (T) -> Boolean): T { - var last: T? = null - var found = false - for (element in this) { - if (predicate(element)) { - last = element - found = true - } - } - if (!found) throw NoSuchElementException("Sequence contains no element matching the predicate.") - @Suppress("UNCHECKED_CAST") - return last as T -} - -/** - * Returns last index of [element], or -1 if the sequence does not contain element. - * - * The operation is _terminal_. - */ -public fun <@kotlin.internal.OnlyInputTypes T> Sequence.lastIndexOf(element: T): Int { - var lastIndex = -1 - var index = 0 - for (item in this) { - if (element == item) - lastIndex = index - index++ - } - return lastIndex -} - -/** - * Returns the last element, or `null` if the sequence is empty. - * - * The operation is _terminal_. - */ -public fun Sequence.lastOrNull(): T? { - val iterator = iterator() - if (!iterator.hasNext()) - return null - var last = iterator.next() - while (iterator.hasNext()) - last = iterator.next() - return last -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - * - * The operation is _terminal_. - */ -public inline fun Sequence.lastOrNull(predicate: (T) -> Boolean): T? { - var last: T? = null - for (element in this) { - if (predicate(element)) { - last = element - } - } - return last -} - -/** - * Returns the single element, or throws an exception if the sequence is empty or has more than one element. - * - * The operation is _terminal_. - */ -public fun Sequence.single(): T { - val iterator = iterator() - if (!iterator.hasNext()) - throw NoSuchElementException("Sequence is empty.") - val single = iterator.next() - if (iterator.hasNext()) - throw IllegalArgumentException("Sequence has more than one element.") - return single -} - -/** - * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. - * - * The operation is _terminal_. - */ -public inline fun Sequence.single(predicate: (T) -> Boolean): T { - var single: T? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Sequence contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Sequence contains no element matching the predicate.") - @Suppress("UNCHECKED_CAST") - return single as T -} - -/** - * Returns single element, or `null` if the sequence is empty or has more than one element. - * - * The operation is _terminal_. - */ -public fun Sequence.singleOrNull(): T? { - val iterator = iterator() - if (!iterator.hasNext()) - return null - val single = iterator.next() - if (iterator.hasNext()) - return null - return single -} - -/** - * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. - * - * The operation is _terminal_. - */ -public inline fun Sequence.singleOrNull(predicate: (T) -> Boolean): T? { - var single: T? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) return null - single = element - found = true - } - } - if (!found) return null - return single -} - -/** - * Returns a sequence containing all elements except first [n] elements. - * - * The operation is _intermediate_ and _stateless_. - */ -public fun Sequence.drop(n: Int): Sequence { - require(n >= 0) { "Requested element count $n is less than zero." } - return when { - n == 0 -> this - this is DropTakeSequence -> this.drop(n) - else -> DropSequence(this, n) - } -} - -/** - * Returns a sequence containing all elements except first elements that satisfy the given [predicate]. - * - * The operation is _intermediate_ and _stateless_. - */ -public fun Sequence.dropWhile(predicate: (T) -> Boolean): Sequence { - return DropWhileSequence(this, predicate) -} - -/** - * Returns a sequence containing only elements matching the given [predicate]. - * - * The operation is _intermediate_ and _stateless_. - */ -public fun Sequence.filter(predicate: (T) -> Boolean): Sequence { - return FilteringSequence(this, true, predicate) -} - -/** - * Returns a sequence containing only elements matching the given [predicate]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - * - * The operation is _intermediate_ and _stateless_. - */ -public fun Sequence.filterIndexed(predicate: (index: Int, T) -> Boolean): Sequence { - // TODO: Rewrite with generalized MapFilterIndexingSequence - return TransformingSequence(FilteringSequence(IndexingSequence(this), true, { predicate(it.index, it.value) }), { it.value }) -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - * - * The operation is _terminal_. - */ -public inline fun > Sequence.filterIndexedTo(destination: C, predicate: (index: Int, T) -> Boolean): C { - forEachIndexed { index, element -> - if (predicate(index, element)) destination.add(element) - } - return destination -} - -/** - * Returns a sequence containing all elements that are instances of specified type parameter R. - * - * The operation is _intermediate_ and _stateless_. - */ -public inline fun Sequence<*>.filterIsInstance(): Sequence<@kotlin.internal.NoInfer R> { - @Suppress("UNCHECKED_CAST") - return filter { it is R } as Sequence -} - -/** - * Appends all elements that are instances of specified type parameter R to the given [destination]. - * - * The operation is _terminal_. - */ -public inline fun > Sequence<*>.filterIsInstanceTo(destination: C): C { - for (element in this) if (element is R) destination.add(element) - return destination -} - -/** - * Returns a sequence containing all elements not matching the given [predicate]. - * - * The operation is _intermediate_ and _stateless_. - */ -public fun Sequence.filterNot(predicate: (T) -> Boolean): Sequence { - return FilteringSequence(this, false, predicate) -} - -/** - * Returns a sequence containing all elements that are not `null`. - * - * The operation is _intermediate_ and _stateless_. - */ -public fun Sequence.filterNotNull(): Sequence { - @Suppress("UNCHECKED_CAST") - return filterNot { it == null } as Sequence -} - -/** - * Appends all elements that are not `null` to the given [destination]. - * - * The operation is _terminal_. - */ -public fun , T : Any> Sequence.filterNotNullTo(destination: C): C { - for (element in this) if (element != null) destination.add(element) - return destination -} - -/** - * Appends all elements not matching the given [predicate] to the given [destination]. - * - * The operation is _terminal_. - */ -public inline fun > Sequence.filterNotTo(destination: C, predicate: (T) -> Boolean): C { - for (element in this) if (!predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - * - * The operation is _terminal_. - */ -public inline fun > Sequence.filterTo(destination: C, predicate: (T) -> Boolean): C { - for (element in this) if (predicate(element)) destination.add(element) - return destination -} - -/** - * Returns a sequence containing first [n] elements. - * - * The operation is _intermediate_ and _stateless_. - */ -public fun Sequence.take(n: Int): Sequence { - require(n >= 0) { "Requested element count $n is less than zero." } - return when { - n == 0 -> emptySequence() - this is DropTakeSequence -> this.take(n) - else -> TakeSequence(this, n) - } -} - -/** - * Returns a sequence containing first elements satisfying the given [predicate]. - * - * The operation is _intermediate_ and _stateless_. - */ -public fun Sequence.takeWhile(predicate: (T) -> Boolean): Sequence { - return TakeWhileSequence(this, predicate) -} - -/** - * Returns a sequence that yields elements of this sequence sorted according to their natural sort order. - * - * The operation is _intermediate_ and _stateful_. - */ -public fun > Sequence.sorted(): Sequence { - return object : Sequence { - override fun iterator(): Iterator { - val sortedList = this@sorted.toMutableList() - sortedList.sort() - return sortedList.iterator() - } - } -} - -/** - * Returns a sequence that yields elements of this sequence sorted according to natural sort order of the value returned by specified [selector] function. - * - * The operation is _intermediate_ and _stateful_. - */ -public inline fun > Sequence.sortedBy(crossinline selector: (T) -> R?): Sequence { - return sortedWith(compareBy(selector)) -} - -/** - * Returns a sequence that yields elements of this sequence sorted descending according to natural sort order of the value returned by specified [selector] function. - * - * The operation is _intermediate_ and _stateful_. - */ -public inline fun > Sequence.sortedByDescending(crossinline selector: (T) -> R?): Sequence { - return sortedWith(compareByDescending(selector)) -} - -/** - * Returns a sequence that yields elements of this sequence sorted descending according to their natural sort order. - * - * The operation is _intermediate_ and _stateful_. - */ -public fun > Sequence.sortedDescending(): Sequence { - return sortedWith(reverseOrder()) -} - -/** - * Returns a sequence that yields elements of this sequence sorted according to the specified [comparator]. - * - * The operation is _intermediate_ and _stateful_. - */ -public fun Sequence.sortedWith(comparator: Comparator): Sequence { - return object : Sequence { - override fun iterator(): Iterator { - val sortedList = this@sortedWith.toMutableList() - sortedList.sortWith(comparator) - return sortedList.iterator() - } - } -} - -/** - * Returns a [Map] containing key-value pairs provided by [transform] function - * applied to elements of the given sequence. - * - * If any of two pairs would have the same key the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original sequence. - * - * The operation is _terminal_. - */ -public inline fun Sequence.associate(transform: (T) -> Pair): Map { - return associateTo(LinkedHashMap(), transform) -} - -/** - * Returns a [Map] containing the elements from the given sequence indexed by the key - * returned from [keySelector] function applied to each element. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original sequence. - * - * The operation is _terminal_. - */ -public inline fun Sequence.associateBy(keySelector: (T) -> K): Map { - return associateByTo(LinkedHashMap(), keySelector) -} - -/** - * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given sequence. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original sequence. - * - * The operation is _terminal_. - */ -public inline fun Sequence.associateBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map { - return associateByTo(LinkedHashMap(), keySelector, valueTransform) -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function applied to each element of the given sequence - * and value is the element itself. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The operation is _terminal_. - */ -public inline fun > Sequence.associateByTo(destination: M, keySelector: (T) -> K): M { - for (element in this) { - destination.put(keySelector(element), element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function and - * and value is provided by the [valueTransform] function applied to elements of the given sequence. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The operation is _terminal_. - */ -public inline fun > Sequence.associateByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M { - for (element in this) { - destination.put(keySelector(element), valueTransform(element)) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs - * provided by [transform] function applied to each element of the given sequence. - * - * If any of two pairs would have the same key the last one gets added to the map. - * - * The operation is _terminal_. - */ -public inline fun > Sequence.associateTo(destination: M, transform: (T) -> Pair): M { - for (element in this) { - destination += transform(element) - } - return destination -} - -/** - * Appends all elements to the given [destination] collection. - * - * The operation is _terminal_. - */ -public fun > Sequence.toCollection(destination: C): C { - for (item in this) { - destination.add(item) - } - return destination -} - -/** - * Returns a [HashSet] of all elements. - * - * The operation is _terminal_. - */ -public fun Sequence.toHashSet(): HashSet { - return toCollection(HashSet()) -} - -/** - * Returns a [List] containing all elements. - * - * The operation is _terminal_. - */ -public fun Sequence.toList(): List { - return this.toMutableList().optimizeReadOnlyList() -} - -/** - * Returns a [MutableList] filled with all elements of this sequence. - * - * The operation is _terminal_. - */ -public fun Sequence.toMutableList(): MutableList { - return toCollection(ArrayList()) -} - -/** - * Returns a [Set] of all elements. - * - * The returned set preserves the element iteration order of the original sequence. - * - * The operation is _terminal_. - */ -public fun Sequence.toSet(): Set { - return toCollection(LinkedHashSet()).optimizeReadOnlySet() -} - -/** - * Returns a single sequence of all elements from results of [transform] function being invoked on each element of original sequence. - * - * The operation is _intermediate_ and _stateless_. - */ -public fun Sequence.flatMap(transform: (T) -> Sequence): Sequence { - return FlatteningSequence(this, transform, { it.iterator() }) -} - -/** - * Appends all elements yielded from results of [transform] function being invoked on each element of original sequence, to the given [destination]. - * - * The operation is _terminal_. - */ -public inline fun > Sequence.flatMapTo(destination: C, transform: (T) -> Sequence): C { - for (element in this) { - val list = transform(element) - destination.addAll(list) - } - return destination -} - -/** - * Groups elements of the original sequence by the key returned by the given [keySelector] function - * applied to each element and returns a map where each group key is associated with a list of corresponding elements. - * - * The returned map preserves the entry iteration order of the keys produced from the original sequence. - * - * @sample samples.collections.Collections.Transformations.groupBy - * - * The operation is _terminal_. - */ -public inline fun Sequence.groupBy(keySelector: (T) -> K): Map> { - return groupByTo(LinkedHashMap>(), keySelector) -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original sequence - * by the key returned by the given [keySelector] function applied to the element - * and returns a map where each group key is associated with a list of corresponding values. - * - * The returned map preserves the entry iteration order of the keys produced from the original sequence. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - * - * The operation is _terminal_. - */ -public inline fun Sequence.groupBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map> { - return groupByTo(LinkedHashMap>(), keySelector, valueTransform) -} - -/** - * Groups elements of the original sequence by the key returned by the given [keySelector] function - * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupBy - * - * The operation is _terminal_. - */ -public inline fun >> Sequence.groupByTo(destination: M, keySelector: (T) -> K): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(element) - } - return destination -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original sequence - * by the key returned by the given [keySelector] function applied to the element - * and puts to the [destination] map each group key associated with a list of corresponding values. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - * - * The operation is _terminal_. - */ -public inline fun >> Sequence.groupByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(valueTransform(element)) - } - return destination -} - -/** - * Creates a [Grouping] source from a sequence to be used later with one of group-and-fold operations - * using the specified [keySelector] function to extract a key from each element. - * - * @sample samples.collections.Collections.Transformations.groupingByEachCount - * - * The operation is _intermediate_ and _stateless_. - */ -@SinceKotlin("1.1") -public inline fun Sequence.groupingBy(crossinline keySelector: (T) -> K): Grouping { - return object : Grouping { - override fun sourceIterator(): Iterator = this@groupingBy.iterator() - override fun keyOf(element: T): K = keySelector(element) - } -} - -/** - * Returns a sequence containing the results of applying the given [transform] function - * to each element in the original sequence. - * - * The operation is _intermediate_ and _stateless_. - */ -public fun Sequence.map(transform: (T) -> R): Sequence { - return TransformingSequence(this, transform) -} - -/** - * Returns a sequence containing the results of applying the given [transform] function - * to each element and its index in the original sequence. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - * - * The operation is _intermediate_ and _stateless_. - */ -public fun Sequence.mapIndexed(transform: (index: Int, T) -> R): Sequence { - return TransformingIndexedSequence(this, transform) -} - -/** - * Returns a sequence containing only the non-null results of applying the given [transform] function - * to each element and its index in the original sequence. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - * - * The operation is _intermediate_ and _stateless_. - */ -public fun Sequence.mapIndexedNotNull(transform: (index: Int, T) -> R?): Sequence { - return TransformingIndexedSequence(this, transform).filterNotNull() -} - -/** - * Applies the given [transform] function to each element and its index in the original sequence - * and appends only the non-null results to the given [destination]. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - * - * The operation is _terminal_. - */ -public inline fun > Sequence.mapIndexedNotNullTo(destination: C, transform: (index: Int, T) -> R?): C { - forEachIndexed { index, element -> transform(index, element)?.let { destination.add(it) } } - return destination -} - -/** - * Applies the given [transform] function to each element and its index in the original sequence - * and appends the results to the given [destination]. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - * - * The operation is _terminal_. - */ -public inline fun > Sequence.mapIndexedTo(destination: C, transform: (index: Int, T) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Returns a sequence containing only the non-null results of applying the given [transform] function - * to each element in the original sequence. - * - * The operation is _intermediate_ and _stateless_. - */ -public fun Sequence.mapNotNull(transform: (T) -> R?): Sequence { - return TransformingSequence(this, transform).filterNotNull() -} - -/** - * Applies the given [transform] function to each element in the original sequence - * and appends only the non-null results to the given [destination]. - * - * The operation is _terminal_. - */ -public inline fun > Sequence.mapNotNullTo(destination: C, transform: (T) -> R?): C { - forEach { element -> transform(element)?.let { destination.add(it) } } - return destination -} - -/** - * Applies the given [transform] function to each element of the original sequence - * and appends the results to the given [destination]. - * - * The operation is _terminal_. - */ -public inline fun > Sequence.mapTo(destination: C, transform: (T) -> R): C { - for (item in this) - destination.add(transform(item)) - return destination -} - -/** - * Returns a sequence of [IndexedValue] for each element of the original sequence. - * - * The operation is _intermediate_ and _stateless_. - */ -public fun Sequence.withIndex(): Sequence> { - return IndexingSequence(this) -} - -/** - * Returns a sequence containing only distinct elements from the given sequence. - * - * The elements in the resulting sequence are in the same order as they were in the source sequence. - * - * The operation is _intermediate_ and _stateful_. - */ -public fun Sequence.distinct(): Sequence { - return this.distinctBy { it } -} - -/** - * Returns a sequence containing only elements from the given sequence - * having distinct keys returned by the given [selector] function. - * - * The elements in the resulting sequence are in the same order as they were in the source sequence. - * - * The operation is _intermediate_ and _stateful_. - */ -public fun Sequence.distinctBy(selector: (T) -> K): Sequence { - return DistinctSequence(this, selector) -} - -/** - * Returns a mutable set containing all distinct elements from the given sequence. - * - * The returned set preserves the element iteration order of the original sequence. - * - * The operation is _terminal_. - */ -public fun Sequence.toMutableSet(): MutableSet { - val set = LinkedHashSet() - for (item in this) set.add(item) - return set -} - -/** - * Returns `true` if all elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.all - * - * The operation is _terminal_. - */ -public inline fun Sequence.all(predicate: (T) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if sequence has at least one element. - * - * @sample samples.collections.Collections.Aggregates.any - * - * The operation is _terminal_. - */ -public fun Sequence.any(): Boolean { - return iterator().hasNext() -} - -/** - * Returns `true` if at least one element matches the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.anyWithPredicate - * - * The operation is _terminal_. - */ -public inline fun Sequence.any(predicate: (T) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return true - return false -} - -/** - * Returns the number of elements in this sequence. - * - * The operation is _terminal_. - */ -public fun Sequence.count(): Int { - var count = 0 - for (element in this) count++ - return count -} - -/** - * Returns the number of elements matching the given [predicate]. - * - * The operation is _terminal_. - */ -public inline fun Sequence.count(predicate: (T) -> Boolean): Int { - var count = 0 - for (element in this) if (predicate(element)) count++ - return count -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. - * - * The operation is _terminal_. - */ -public inline fun Sequence.fold(initial: R, operation: (acc: R, T) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right - * to current accumulator value and each element with its index in the original sequence. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself, and calculates the next accumulator value. - * - * The operation is _terminal_. - */ -public inline fun Sequence.foldIndexed(initial: R, operation: (index: Int, acc: R, T) -> R): R { - var index = 0 - var accumulator = initial - for (element in this) accumulator = operation(index++, accumulator, element) - return accumulator -} - -/** - * Performs the given [action] on each element. - * - * The operation is _terminal_. - */ -public inline fun Sequence.forEach(action: (T) -> Unit): Unit { - for (element in this) action(element) -} - -/** - * Performs the given [action] on each element, providing sequential index with the element. - * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. - * - * The operation is _terminal_. - */ -public inline fun Sequence.forEachIndexed(action: (index: Int, T) -> Unit): Unit { - var index = 0 - for (item in this) action(index++, item) -} - -/** - * Returns the largest element or `null` if there are no elements. - * - * If any of elements is `NaN` returns `NaN`. - * - * The operation is _terminal_. - */ -@SinceKotlin("1.1") -public fun Sequence.max(): Double? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var max = iterator.next() - if (max.isNaN()) return max - while (iterator.hasNext()) { - val e = iterator.next() - if (e.isNaN()) return e - if (max < e) max = e - } - return max -} - -/** - * Returns the largest element or `null` if there are no elements. - * - * If any of elements is `NaN` returns `NaN`. - * - * The operation is _terminal_. - */ -@SinceKotlin("1.1") -public fun Sequence.max(): Float? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var max = iterator.next() - if (max.isNaN()) return max - while (iterator.hasNext()) { - val e = iterator.next() - if (e.isNaN()) return e - if (max < e) max = e - } - return max -} - -/** - * Returns the largest element or `null` if there are no elements. - * - * The operation is _terminal_. - */ -public fun > Sequence.max(): T? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var max = iterator.next() - while (iterator.hasNext()) { - val e = iterator.next() - if (max < e) max = e - } - return max -} - -/** - * Returns the first element yielding the largest value of the given function or `null` if there are no elements. - * - * The operation is _terminal_. - */ -public inline fun > Sequence.maxBy(selector: (T) -> R): T? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var maxElem = iterator.next() - var maxValue = selector(maxElem) - while (iterator.hasNext()) { - val e = iterator.next() - val v = selector(e) - if (maxValue < v) { - maxElem = e - maxValue = v - } - } - return maxElem -} - -/** - * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. - * - * The operation is _terminal_. - */ -public fun Sequence.maxWith(comparator: Comparator): T? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var max = iterator.next() - while (iterator.hasNext()) { - val e = iterator.next() - if (comparator.compare(max, e) < 0) max = e - } - return max -} - -/** - * Returns the smallest element or `null` if there are no elements. - * - * If any of elements is `NaN` returns `NaN`. - * - * The operation is _terminal_. - */ -@SinceKotlin("1.1") -public fun Sequence.min(): Double? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var min = iterator.next() - if (min.isNaN()) return min - while (iterator.hasNext()) { - val e = iterator.next() - if (e.isNaN()) return e - if (min > e) min = e - } - return min -} - -/** - * Returns the smallest element or `null` if there are no elements. - * - * If any of elements is `NaN` returns `NaN`. - * - * The operation is _terminal_. - */ -@SinceKotlin("1.1") -public fun Sequence.min(): Float? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var min = iterator.next() - if (min.isNaN()) return min - while (iterator.hasNext()) { - val e = iterator.next() - if (e.isNaN()) return e - if (min > e) min = e - } - return min -} - -/** - * Returns the smallest element or `null` if there are no elements. - * - * The operation is _terminal_. - */ -public fun > Sequence.min(): T? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var min = iterator.next() - while (iterator.hasNext()) { - val e = iterator.next() - if (min > e) min = e - } - return min -} - -/** - * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. - * - * The operation is _terminal_. - */ -public inline fun > Sequence.minBy(selector: (T) -> R): T? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var minElem = iterator.next() - var minValue = selector(minElem) - while (iterator.hasNext()) { - val e = iterator.next() - val v = selector(e) - if (minValue > v) { - minElem = e - minValue = v - } - } - return minElem -} - -/** - * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. - * - * The operation is _terminal_. - */ -public fun Sequence.minWith(comparator: Comparator): T? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var min = iterator.next() - while (iterator.hasNext()) { - val e = iterator.next() - if (comparator.compare(min, e) > 0) min = e - } - return min -} - -/** - * Returns `true` if the sequence has no elements. - * - * @sample samples.collections.Collections.Aggregates.none - * - * The operation is _terminal_. - */ -public fun Sequence.none(): Boolean { - return !iterator().hasNext() -} - -/** - * Returns `true` if no elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.noneWithPredicate - * - * The operation is _terminal_. - */ -public inline fun Sequence.none(predicate: (T) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return false - return true -} - -/** - * Returns a sequence which performs the given [action] on each element of the original sequence as they pass through it. - * - * The operation is _intermediate_ and _stateless_. - */ -@SinceKotlin("1.1") -public fun Sequence.onEach(action: (T) -> Unit): Sequence { - return map { - action(it) - it - } -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. - * - * The operation is _terminal_. - */ -public inline fun Sequence.reduce(operation: (acc: S, T) -> S): S { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty sequence can't be reduced.") - var accumulator: S = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(accumulator, iterator.next()) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right - * to current accumulator value and each element with its index in the original sequence. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself and calculates the next accumulator value. - * - * The operation is _terminal_. - */ -public inline fun Sequence.reduceIndexed(operation: (index: Int, acc: S, T) -> S): S { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty sequence can't be reduced.") - var index = 1 - var accumulator: S = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(index++, accumulator, iterator.next()) - } - return accumulator -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the sequence. - * - * The operation is _terminal_. - */ -public inline fun Sequence.sumBy(selector: (T) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the sequence. - * - * The operation is _terminal_. - */ -public inline fun Sequence.sumByDouble(selector: (T) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns an original collection containing all the non-`null` elements, throwing an [IllegalArgumentException] if there are any `null` elements. - * - * The operation is _intermediate_ and _stateless_. - */ -public fun Sequence.requireNoNulls(): Sequence { - return map { it ?: throw IllegalArgumentException("null element found in $this.") } -} - -/** - * Splits this sequence into a sequence of lists each not exceeding the given [size]. - * - * The last list in the resulting sequence may have less elements than the given [size]. - * - * @param size the number of elements to take in each list, must be positive and can be greater than the number of elements in this sequence. - * - * @sample samples.collections.Collections.Transformations.chunked - * - * The operation is _intermediate_ and _stateful_. - */ -@SinceKotlin("1.2") -public fun Sequence.chunked(size: Int): Sequence> { - return windowed(size, size, partialWindows = true) -} - -/** - * Splits this sequence into several lists each not exceeding the given [size] - * and applies the given [transform] function to an each. - * - * @return sequence of results of the [transform] applied to an each list. - * - * Note that the list passed to the [transform] function is ephemeral and is valid only inside that function. - * You should not store it or allow it to escape in some way, unless you made a snapshot of it. - * The last list may have less elements than the given [size]. - * - * @param size the number of elements to take in each list, must be positive and can be greater than the number of elements in this sequence. - * - * @sample samples.text.Strings.chunkedTransform - * - * The operation is _intermediate_ and _stateful_. - */ -@SinceKotlin("1.2") -public fun Sequence.chunked(size: Int, transform: (List) -> R): Sequence { - return windowed(size, size, partialWindows = true, transform = transform) -} - -/** - * Returns a sequence containing all elements of the original sequence without the first occurrence of the given [element]. - * - * The operation is _intermediate_ and _stateless_. - */ -public operator fun Sequence.minus(element: T): Sequence { - return object: Sequence { - override fun iterator(): Iterator { - var removed = false - return this@minus.filter { if (!removed && it == element) { removed = true; false } else true }.iterator() - } - } -} - -/** - * Returns a sequence containing all elements of original sequence except the elements contained in the given [elements] array. - * - * Note that the source sequence and the array being subtracted are iterated only when an `iterator` is requested from - * the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. - * - * The operation is _intermediate_ and _stateful_. - */ -public operator fun Sequence.minus(elements: Array): Sequence { - if (elements.isEmpty()) return this - return object: Sequence { - override fun iterator(): Iterator { - val other = elements.toHashSet() - return this@minus.filterNot { it in other }.iterator() - } - } -} - -/** - * Returns a sequence containing all elements of original sequence except the elements contained in the given [elements] collection. - * - * Note that the source sequence and the collection being subtracted are iterated only when an `iterator` is requested from - * the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. - * - * The operation is _intermediate_ and _stateful_. - */ -public operator fun Sequence.minus(elements: Iterable): Sequence { - return object: Sequence { - override fun iterator(): Iterator { - val other = elements.convertToSetForSetOperation() - if (other.isEmpty()) - return this@minus.iterator() - else - return this@minus.filterNot { it in other }.iterator() - } - } -} - -/** - * Returns a sequence containing all elements of original sequence except the elements contained in the given [elements] sequence. - * - * Note that the source sequence and the sequence being subtracted are iterated only when an `iterator` is requested from - * the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. - * - * The operation is _intermediate_ for this sequence and _terminal_ and _stateful_ for the [elements] sequence. - */ -public operator fun Sequence.minus(elements: Sequence): Sequence { - return object: Sequence { - override fun iterator(): Iterator { - val other = elements.toHashSet() - if (other.isEmpty()) - return this@minus.iterator() - else - return this@minus.filterNot { it in other }.iterator() - } - } -} - -/** - * Returns a sequence containing all elements of the original sequence without the first occurrence of the given [element]. - * - * The operation is _intermediate_ and _stateless_. - */ -@kotlin.internal.InlineOnly -public inline fun Sequence.minusElement(element: T): Sequence { - return minus(element) -} - -/** - * Splits the original sequence into pair of lists, - * where *first* list contains elements for which [predicate] yielded `true`, - * while *second* list contains elements for which [predicate] yielded `false`. - * - * The operation is _terminal_. - */ -public inline fun Sequence.partition(predicate: (T) -> Boolean): Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Returns a sequence containing all elements of the original sequence and then the given [element]. - * - * The operation is _intermediate_ and _stateless_. - */ -public operator fun Sequence.plus(element: T): Sequence { - return sequenceOf(this, sequenceOf(element)).flatten() -} - -/** - * Returns a sequence containing all elements of original sequence and then all elements of the given [elements] array. - * - * Note that the source sequence and the array being added are iterated only when an `iterator` is requested from - * the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. - * - * The operation is _intermediate_ and _stateless_. - */ -public operator fun Sequence.plus(elements: Array): Sequence { - return this.plus(elements.asList()) -} - -/** - * Returns a sequence containing all elements of original sequence and then all elements of the given [elements] collection. - * - * Note that the source sequence and the collection being added are iterated only when an `iterator` is requested from - * the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. - * - * The operation is _intermediate_ and _stateless_. - */ -public operator fun Sequence.plus(elements: Iterable): Sequence { - return sequenceOf(this, elements.asSequence()).flatten() -} - -/** - * Returns a sequence containing all elements of original sequence and then all elements of the given [elements] sequence. - * - * Note that the source sequence and the sequence being added are iterated only when an `iterator` is requested from - * the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. - * - * The operation is _intermediate_ and _stateless_. - */ -public operator fun Sequence.plus(elements: Sequence): Sequence { - return sequenceOf(this, elements).flatten() -} - -/** - * Returns a sequence containing all elements of the original sequence and then the given [element]. - * - * The operation is _intermediate_ and _stateless_. - */ -@kotlin.internal.InlineOnly -public inline fun Sequence.plusElement(element: T): Sequence { - return plus(element) -} - -/** - * Returns a sequence of snapshots of the window of the given [size] - * sliding along this sequence with the given [step], where each - * snapshot is a list. - * - * Several last lists may have less elements than the given [size]. - * - * Both [size] and [step] must be positive and can be greater than the number of elements in this sequence. - * @param size the number of elements to take in each window - * @param step the number of elements to move the window forward by on an each step, by default 1 - * @param partialWindows controls whether or not to keep partial windows in the end if any, - * by default `false` which means partial windows won't be preserved - * - * @sample samples.collections.Sequences.Transformations.takeWindows - */ -@SinceKotlin("1.2") -public fun Sequence.windowed(size: Int, step: Int = 1, partialWindows: Boolean = false): Sequence> { - return windowedSequence(size, step, partialWindows, reuseBuffer = false) -} - -/** - * Returns a sequence of results of applying the given [transform] function to - * an each list representing a view over the window of the given [size] - * sliding along this sequence with the given [step]. - * - * Note that the list passed to the [transform] function is ephemeral and is valid only inside that function. - * You should not store it or allow it to escape in some way, unless you made a snapshot of it. - * Several last lists may have less elements than the given [size]. - * - * Both [size] and [step] must be positive and can be greater than the number of elements in this sequence. - * @param size the number of elements to take in each window - * @param step the number of elements to move the window forward by on an each step, by default 1 - * @param partialWindows controls whether or not to keep partial windows in the end if any, - * by default `false` which means partial windows won't be preserved - * - * @sample samples.collections.Sequences.Transformations.averageWindows - */ -@SinceKotlin("1.2") -public fun Sequence.windowed(size: Int, step: Int = 1, partialWindows: Boolean = false, transform: (List) -> R): Sequence { - return windowedSequence(size, step, partialWindows, reuseBuffer = true).map(transform) -} - -/** - * Returns a sequence of pairs built from elements of both sequences with same indexes. - * Resulting sequence has length of shortest input sequence. - * - * The operation is _intermediate_ and _stateless_. - */ -public infix fun Sequence.zip(other: Sequence): Sequence> { - return MergingSequence(this, other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a sequence of values built from elements of both collections with same indexes using provided [transform]. Resulting sequence has length of shortest input sequences. - * - * The operation is _intermediate_ and _stateless_. - */ -public fun Sequence.zip(other: Sequence, transform: (a: T, b: R) -> V): Sequence { - return MergingSequence(this, other, transform) -} - -/** - * Returns a sequence of pairs of each two adjacent elements in this sequence. - * - * The returned sequence is empty if this sequence contains less than two elements. - * - * @sample samples.collections.Collections.Transformations.zipWithNext - * - * The operation is _intermediate_ and _stateless_. - */ -@SinceKotlin("1.2") -public fun Sequence.zipWithNext(): Sequence> { - return zipWithNext { a, b -> a to b } -} - -/** - * Returns a sequence containing the results of applying the given [transform] function - * to an each pair of two adjacent elements in this sequence. - * - * The returned sequence is empty if this sequence contains less than two elements. - * - * @sample samples.collections.Collections.Transformations.zipWithNextToFindDeltas - * - * The operation is _intermediate_ and _stateless_. - */ -@SinceKotlin("1.2") -public fun Sequence.zipWithNext(transform: (a: T, b: T) -> R): Sequence { - return buildSequence result@ { - val iterator = iterator() - if (!iterator.hasNext()) return@result - var current = iterator.next() - while (iterator.hasNext()) { - val next = iterator.next() - yield(transform(current, next)) - current = next - } - } -} - -/** - * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinTo - * - * The operation is _terminal_. - */ -public fun Sequence.joinTo(buffer: A, separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((T) -> CharSequence)? = null): A { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - buffer.appendElement(element, transform) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) - return buffer -} - -/** - * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinToString - * - * The operation is _terminal_. - */ -public fun Sequence.joinToString(separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((T) -> CharSequence)? = null): String { - return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() -} - -/** - * Creates an [Iterable] instance that wraps the original sequence returning its elements when being iterated. - */ -public fun Sequence.asIterable(): Iterable { - return Iterable { this.iterator() } -} - -/** - * Returns this sequence as a [Sequence]. - */ -@kotlin.internal.InlineOnly -public inline fun Sequence.asSequence(): Sequence { - return this -} - -/** - * Returns an average value of elements in the sequence. - * - * The operation is _terminal_. - */ -@kotlin.jvm.JvmName("averageOfByte") -public fun Sequence.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the sequence. - * - * The operation is _terminal_. - */ -@kotlin.jvm.JvmName("averageOfShort") -public fun Sequence.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the sequence. - * - * The operation is _terminal_. - */ -@kotlin.jvm.JvmName("averageOfInt") -public fun Sequence.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the sequence. - * - * The operation is _terminal_. - */ -@kotlin.jvm.JvmName("averageOfLong") -public fun Sequence.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the sequence. - * - * The operation is _terminal_. - */ -@kotlin.jvm.JvmName("averageOfFloat") -public fun Sequence.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the sequence. - * - * The operation is _terminal_. - */ -@kotlin.jvm.JvmName("averageOfDouble") -public fun Sequence.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns the sum of all elements in the sequence. - * - * The operation is _terminal_. - */ -@kotlin.jvm.JvmName("sumOfByte") -public fun Sequence.sum(): Int { - var sum: Int = 0 - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the sequence. - * - * The operation is _terminal_. - */ -@kotlin.jvm.JvmName("sumOfShort") -public fun Sequence.sum(): Int { - var sum: Int = 0 - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the sequence. - * - * The operation is _terminal_. - */ -@kotlin.jvm.JvmName("sumOfInt") -public fun Sequence.sum(): Int { - var sum: Int = 0 - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the sequence. - * - * The operation is _terminal_. - */ -@kotlin.jvm.JvmName("sumOfLong") -public fun Sequence.sum(): Long { - var sum: Long = 0L - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the sequence. - * - * The operation is _terminal_. - */ -@kotlin.jvm.JvmName("sumOfFloat") -public fun Sequence.sum(): Float { - var sum: Float = 0.0f - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the sequence. - * - * The operation is _terminal_. - */ -@kotlin.jvm.JvmName("sumOfDouble") -public fun Sequence.sum(): Double { - var sum: Double = 0.0 - for (element in this) { - sum += element - } - return sum -} - diff --git a/js/js.libraries/src/core/generated/_SetsJs.kt b/js/js.libraries/src/core/generated/_SetsJs.kt deleted file mode 100644 index 4940f609eeb..00000000000 --- a/js/js.libraries/src/core/generated/_SetsJs.kt +++ /dev/null @@ -1,134 +0,0 @@ -@file:kotlin.jvm.JvmMultifileClass -@file:kotlin.jvm.JvmName("SetsKt") - -package kotlin.collections - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// - -import kotlin.js.* -import kotlin.* -import kotlin.text.* -import kotlin.comparisons.* - -/** - * Returns a set containing all elements of the original set except the given [element]. - * - * The returned set preserves the element iteration order of the original set. - */ -public operator fun Set.minus(element: T): Set { - val result = LinkedHashSet(mapCapacity(size)) - var removed = false - return this.filterTo(result) { if (!removed && it == element) { removed = true; false } else true } -} - -/** - * Returns a set containing all elements of the original set except the elements contained in the given [elements] array. - * - * The returned set preserves the element iteration order of the original set. - */ -public operator fun Set.minus(elements: Array): Set { - val result = LinkedHashSet(this) - result.removeAll(elements) - return result -} - -/** - * Returns a set containing all elements of the original set except the elements contained in the given [elements] collection. - * - * The returned set preserves the element iteration order of the original set. - */ -public operator fun Set.minus(elements: Iterable): Set { - val other = elements.convertToSetForSetOperationWith(this) - if (other.isEmpty()) - return this.toSet() - if (other is Set) - return this.filterNotTo(LinkedHashSet()) { it in other } - val result = LinkedHashSet(this) - result.removeAll(other) - return result -} - -/** - * Returns a set containing all elements of the original set except the elements contained in the given [elements] sequence. - * - * The returned set preserves the element iteration order of the original set. - */ -public operator fun Set.minus(elements: Sequence): Set { - val result = LinkedHashSet(this) - result.removeAll(elements) - return result -} - -/** - * Returns a set containing all elements of the original set except the given [element]. - * - * The returned set preserves the element iteration order of the original set. - */ -@kotlin.internal.InlineOnly -public inline fun Set.minusElement(element: T): Set { - return minus(element) -} - -/** - * Returns a set containing all elements of the original set and then the given [element] if it isn't already in this set. - * - * The returned set preserves the element iteration order of the original set. - */ -public operator fun Set.plus(element: T): Set { - val result = LinkedHashSet(mapCapacity(size + 1)) - result.addAll(this) - result.add(element) - return result -} - -/** - * Returns a set containing all elements of the original set and the given [elements] array, - * which aren't already in this set. - * - * The returned set preserves the element iteration order of the original set. - */ -public operator fun Set.plus(elements: Array): Set { - val result = LinkedHashSet(mapCapacity(this.size + elements.size)) - result.addAll(this) - result.addAll(elements) - return result -} - -/** - * Returns a set containing all elements of the original set and the given [elements] collection, - * which aren't already in this set. - * The returned set preserves the element iteration order of the original set. - */ -public operator fun Set.plus(elements: Iterable): Set { - val result = LinkedHashSet(mapCapacity(elements.collectionSizeOrNull()?.let { this.size + it } ?: this.size * 2)) - result.addAll(this) - result.addAll(elements) - return result -} - -/** - * Returns a set containing all elements of the original set and the given [elements] sequence, - * which aren't already in this set. - * - * The returned set preserves the element iteration order of the original set. - */ -public operator fun Set.plus(elements: Sequence): Set { - val result = LinkedHashSet(mapCapacity(this.size * 2)) - result.addAll(this) - result.addAll(elements) - return result -} - -/** - * Returns a set containing all elements of the original set and then the given [element] if it isn't already in this set. - * - * The returned set preserves the element iteration order of the original set. - */ -@kotlin.internal.InlineOnly -public inline fun Set.plusElement(element: T): Set { - return plus(element) -} - diff --git a/js/js.libraries/src/core/generated/_StringsJs.kt b/js/js.libraries/src/core/generated/_StringsJs.kt deleted file mode 100644 index 2cc2f2306a7..00000000000 --- a/js/js.libraries/src/core/generated/_StringsJs.kt +++ /dev/null @@ -1,1380 +0,0 @@ -@file:kotlin.jvm.JvmMultifileClass -@file:kotlin.jvm.JvmName("StringsKt") - -package kotlin.text - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// - -import kotlin.js.* -import kotlin.* -import kotlin.text.* -import kotlin.comparisons.* - -/** - * Returns a character at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this char sequence. - */ -@kotlin.internal.InlineOnly -public inline fun CharSequence.elementAt(index: Int): Char { - return get(index) -} - -/** - * Returns a character at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this char sequence. - */ -@kotlin.internal.InlineOnly -public inline fun CharSequence.elementAtOrElse(index: Int, defaultValue: (Int) -> Char): Char { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns a character at the given [index] or `null` if the [index] is out of bounds of this char sequence. - */ -@kotlin.internal.InlineOnly -public inline fun CharSequence.elementAtOrNull(index: Int): Char? { - return this.getOrNull(index) -} - -/** - * Returns the first character matching the given [predicate], or `null` if no such character was found. - */ -@kotlin.internal.InlineOnly -public inline fun CharSequence.find(predicate: (Char) -> Boolean): Char? { - return firstOrNull(predicate) -} - -/** - * Returns the last character matching the given [predicate], or `null` if no such character was found. - */ -@kotlin.internal.InlineOnly -public inline fun CharSequence.findLast(predicate: (Char) -> Boolean): Char? { - return lastOrNull(predicate) -} - -/** - * Returns first character. - * @throws [NoSuchElementException] if the char sequence is empty. - */ -public fun CharSequence.first(): Char { - if (isEmpty()) - throw NoSuchElementException("Char sequence is empty.") - return this[0] -} - -/** - * Returns the first character matching the given [predicate]. - * @throws [NoSuchElementException] if no such character is found. - */ -public inline fun CharSequence.first(predicate: (Char) -> Boolean): Char { - for (element in this) if (predicate(element)) return element - throw NoSuchElementException("Char sequence contains no character matching the predicate.") -} - -/** - * Returns the first character, or `null` if the char sequence is empty. - */ -public fun CharSequence.firstOrNull(): Char? { - return if (isEmpty()) null else this[0] -} - -/** - * Returns the first character matching the given [predicate], or `null` if character was not found. - */ -public inline fun CharSequence.firstOrNull(predicate: (Char) -> Boolean): Char? { - for (element in this) if (predicate(element)) return element - return null -} - -/** - * Returns a character at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this char sequence. - */ -@kotlin.internal.InlineOnly -public inline fun CharSequence.getOrElse(index: Int, defaultValue: (Int) -> Char): Char { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns a character at the given [index] or `null` if the [index] is out of bounds of this char sequence. - */ -public fun CharSequence.getOrNull(index: Int): Char? { - return if (index >= 0 && index <= lastIndex) get(index) else null -} - -/** - * Returns index of the first character matching the given [predicate], or -1 if the char sequence does not contain such character. - */ -public inline fun CharSequence.indexOfFirst(predicate: (Char) -> Boolean): Int { - for (index in indices) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the last character matching the given [predicate], or -1 if the char sequence does not contain such character. - */ -public inline fun CharSequence.indexOfLast(predicate: (Char) -> Boolean): Int { - for (index in indices.reversed()) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns the last character. - * @throws [NoSuchElementException] if the char sequence is empty. - */ -public fun CharSequence.last(): Char { - if (isEmpty()) - throw NoSuchElementException("Char sequence is empty.") - return this[lastIndex] -} - -/** - * Returns the last character matching the given [predicate]. - * @throws [NoSuchElementException] if no such character is found. - */ -public inline fun CharSequence.last(predicate: (Char) -> Boolean): Char { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - throw NoSuchElementException("Char sequence contains no character matching the predicate.") -} - -/** - * Returns the last character, or `null` if the char sequence is empty. - */ -public fun CharSequence.lastOrNull(): Char? { - return if (isEmpty()) null else this[length - 1] -} - -/** - * Returns the last character matching the given [predicate], or `null` if no such character was found. - */ -public inline fun CharSequence.lastOrNull(predicate: (Char) -> Boolean): Char? { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - return null -} - -/** - * Returns the single character, or throws an exception if the char sequence is empty or has more than one character. - */ -public fun CharSequence.single(): Char { - return when (length) { - 0 -> throw NoSuchElementException("Char sequence is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Char sequence has more than one element.") - } -} - -/** - * Returns the single character matching the given [predicate], or throws exception if there is no or more than one matching character. - */ -public inline fun CharSequence.single(predicate: (Char) -> Boolean): Char { - var single: Char? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Char sequence contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Char sequence contains no character matching the predicate.") - @Suppress("UNCHECKED_CAST") - return single as Char -} - -/** - * Returns single character, or `null` if the char sequence is empty or has more than one character. - */ -public fun CharSequence.singleOrNull(): Char? { - return if (length == 1) this[0] else null -} - -/** - * Returns the single character matching the given [predicate], or `null` if character was not found or more than one character was found. - */ -public inline fun CharSequence.singleOrNull(predicate: (Char) -> Boolean): Char? { - var single: Char? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) return null - single = element - found = true - } - } - if (!found) return null - return single -} - -/** - * Returns a subsequence of this char sequence with the first [n] characters removed. - */ -public fun CharSequence.drop(n: Int): CharSequence { - require(n >= 0) { "Requested character count $n is less than zero." } - return subSequence(n.coerceAtMost(length), length) -} - -/** - * Returns a string with the first [n] characters removed. - */ -public fun String.drop(n: Int): String { - require(n >= 0) { "Requested character count $n is less than zero." } - return substring(n.coerceAtMost(length)) -} - -/** - * Returns a subsequence of this char sequence with the last [n] characters removed. - */ -public fun CharSequence.dropLast(n: Int): CharSequence { - require(n >= 0) { "Requested character count $n is less than zero." } - return take((length - n).coerceAtLeast(0)) -} - -/** - * Returns a string with the last [n] characters removed. - */ -public fun String.dropLast(n: Int): String { - require(n >= 0) { "Requested character count $n is less than zero." } - return take((length - n).coerceAtLeast(0)) -} - -/** - * Returns a subsequence of this char sequence containing all characters except last characters that satisfy the given [predicate]. - */ -public inline fun CharSequence.dropLastWhile(predicate: (Char) -> Boolean): CharSequence { - for (index in lastIndex downTo 0) - if (!predicate(this[index])) - return subSequence(0, index + 1) - return "" -} - -/** - * Returns a string containing all characters except last characters that satisfy the given [predicate]. - */ -public inline fun String.dropLastWhile(predicate: (Char) -> Boolean): String { - for (index in lastIndex downTo 0) - if (!predicate(this[index])) - return substring(0, index + 1) - return "" -} - -/** - * Returns a subsequence of this char sequence containing all characters except first characters that satisfy the given [predicate]. - */ -public inline fun CharSequence.dropWhile(predicate: (Char) -> Boolean): CharSequence { - for (index in this.indices) - if (!predicate(this[index])) - return subSequence(index, length) - return "" -} - -/** - * Returns a string containing all characters except first characters that satisfy the given [predicate]. - */ -public inline fun String.dropWhile(predicate: (Char) -> Boolean): String { - for (index in this.indices) - if (!predicate(this[index])) - return substring(index) - return "" -} - -/** - * Returns a char sequence containing only those characters from the original char sequence that match the given [predicate]. - */ -public inline fun CharSequence.filter(predicate: (Char) -> Boolean): CharSequence { - return filterTo(StringBuilder(), predicate) -} - -/** - * Returns a string containing only those characters from the original string that match the given [predicate]. - */ -public inline fun String.filter(predicate: (Char) -> Boolean): String { - return filterTo(StringBuilder(), predicate).toString() -} - -/** - * Returns a char sequence containing only those characters from the original char sequence that match the given [predicate]. - * @param [predicate] function that takes the index of a character and the character itself - * and returns the result of predicate evaluation on the character. - */ -public inline fun CharSequence.filterIndexed(predicate: (index: Int, Char) -> Boolean): CharSequence { - return filterIndexedTo(StringBuilder(), predicate) -} - -/** - * Returns a string containing only those characters from the original string that match the given [predicate]. - * @param [predicate] function that takes the index of a character and the character itself - * and returns the result of predicate evaluation on the character. - */ -public inline fun String.filterIndexed(predicate: (index: Int, Char) -> Boolean): String { - return filterIndexedTo(StringBuilder(), predicate).toString() -} - -/** - * Appends all characters matching the given [predicate] to the given [destination]. - * @param [predicate] function that takes the index of a character and the character itself - * and returns the result of predicate evaluation on the character. - */ -public inline fun CharSequence.filterIndexedTo(destination: C, predicate: (index: Int, Char) -> Boolean): C { - forEachIndexed { index, element -> - if (predicate(index, element)) destination.append(element) - } - return destination -} - -/** - * Returns a char sequence containing only those characters from the original char sequence that do not match the given [predicate]. - */ -public inline fun CharSequence.filterNot(predicate: (Char) -> Boolean): CharSequence { - return filterNotTo(StringBuilder(), predicate) -} - -/** - * Returns a string containing only those characters from the original string that do not match the given [predicate]. - */ -public inline fun String.filterNot(predicate: (Char) -> Boolean): String { - return filterNotTo(StringBuilder(), predicate).toString() -} - -/** - * Appends all characters not matching the given [predicate] to the given [destination]. - */ -public inline fun CharSequence.filterNotTo(destination: C, predicate: (Char) -> Boolean): C { - for (element in this) if (!predicate(element)) destination.append(element) - return destination -} - -/** - * Appends all characters matching the given [predicate] to the given [destination]. - */ -public inline fun CharSequence.filterTo(destination: C, predicate: (Char) -> Boolean): C { - for (index in 0 until length) { - val element = get(index) - if (predicate(element)) destination.append(element) - } - return destination -} - -/** - * Returns a char sequence containing characters of the original char sequence at the specified range of [indices]. - */ -public fun CharSequence.slice(indices: IntRange): CharSequence { - if (indices.isEmpty()) return "" - return subSequence(indices) -} - -/** - * Returns a string containing characters of the original string at the specified range of [indices]. - */ -public fun String.slice(indices: IntRange): String { - if (indices.isEmpty()) return "" - return substring(indices) -} - -/** - * Returns a char sequence containing characters of the original char sequence at specified [indices]. - */ -public fun CharSequence.slice(indices: Iterable): CharSequence { - val size = indices.collectionSizeOrDefault(10) - if (size == 0) return "" - val result = StringBuilder(size) - for (i in indices) { - result.append(get(i)) - } - return result -} - -/** - * Returns a string containing characters of the original string at specified [indices]. - */ -@kotlin.internal.InlineOnly -public inline fun String.slice(indices: Iterable): String { - return (this as CharSequence).slice(indices).toString() -} - -/** - * Returns a subsequence of this char sequence containing the first [n] characters from this char sequence, or the entire char sequence if this char sequence is shorter. - */ -public fun CharSequence.take(n: Int): CharSequence { - require(n >= 0) { "Requested character count $n is less than zero." } - return subSequence(0, n.coerceAtMost(length)) -} - -/** - * Returns a string containing the first [n] characters from this string, or the entire string if this string is shorter. - */ -public fun String.take(n: Int): String { - require(n >= 0) { "Requested character count $n is less than zero." } - return substring(0, n.coerceAtMost(length)) -} - -/** - * Returns a subsequence of this char sequence containing the last [n] characters from this char sequence, or the entire char sequence if this char sequence is shorter. - */ -public fun CharSequence.takeLast(n: Int): CharSequence { - require(n >= 0) { "Requested character count $n is less than zero." } - val length = length - return subSequence(length - n.coerceAtMost(length), length) -} - -/** - * Returns a string containing the last [n] characters from this string, or the entire string if this string is shorter. - */ -public fun String.takeLast(n: Int): String { - require(n >= 0) { "Requested character count $n is less than zero." } - val length = length - return substring(length - n.coerceAtMost(length)) -} - -/** - * Returns a subsequence of this char sequence containing last characters that satisfy the given [predicate]. - */ -public inline fun CharSequence.takeLastWhile(predicate: (Char) -> Boolean): CharSequence { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return subSequence(index + 1, length) - } - } - return subSequence(0, length) -} - -/** - * Returns a string containing last characters that satisfy the given [predicate]. - */ -public inline fun String.takeLastWhile(predicate: (Char) -> Boolean): String { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return substring(index + 1) - } - } - return this -} - -/** - * Returns a subsequence of this char sequence containing the first characters that satisfy the given [predicate]. - */ -public inline fun CharSequence.takeWhile(predicate: (Char) -> Boolean): CharSequence { - for (index in 0 until length) - if (!predicate(get(index))) { - return subSequence(0, index) - } - return subSequence(0, length) -} - -/** - * Returns a string containing the first characters that satisfy the given [predicate]. - */ -public inline fun String.takeWhile(predicate: (Char) -> Boolean): String { - for (index in 0 until length) - if (!predicate(get(index))) { - return substring(0, index) - } - return this -} - -/** - * Returns a char sequence with characters in reversed order. - */ -public fun CharSequence.reversed(): CharSequence { - return StringBuilder(this).reverse() -} - -/** - * Returns a string with characters in reversed order. - */ -@kotlin.internal.InlineOnly -public inline fun String.reversed(): String { - return (this as CharSequence).reversed().toString() -} - -/** - * Returns a [Map] containing key-value pairs provided by [transform] function - * applied to characters of the given char sequence. - * - * If any of two pairs would have the same key the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original char sequence. - */ -public inline fun CharSequence.associate(transform: (Char) -> Pair): Map { - val capacity = mapCapacity(length).coerceAtLeast(16) - return associateTo(LinkedHashMap(capacity), transform) -} - -/** - * Returns a [Map] containing the characters from the given char sequence indexed by the key - * returned from [keySelector] function applied to each character. - * - * If any two characters would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original char sequence. - */ -public inline fun CharSequence.associateBy(keySelector: (Char) -> K): Map { - val capacity = mapCapacity(length).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector) -} - -/** - * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to characters of the given char sequence. - * - * If any two characters would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original char sequence. - */ -public inline fun CharSequence.associateBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map { - val capacity = mapCapacity(length).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function applied to each character of the given char sequence - * and value is the character itself. - * - * If any two characters would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > CharSequence.associateByTo(destination: M, keySelector: (Char) -> K): M { - for (element in this) { - destination.put(keySelector(element), element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function and - * and value is provided by the [valueTransform] function applied to characters of the given char sequence. - * - * If any two characters would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > CharSequence.associateByTo(destination: M, keySelector: (Char) -> K, valueTransform: (Char) -> V): M { - for (element in this) { - destination.put(keySelector(element), valueTransform(element)) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs - * provided by [transform] function applied to each character of the given char sequence. - * - * If any of two pairs would have the same key the last one gets added to the map. - */ -public inline fun > CharSequence.associateTo(destination: M, transform: (Char) -> Pair): M { - for (element in this) { - destination += transform(element) - } - return destination -} - -/** - * Appends all characters to the given [destination] collection. - */ -public fun > CharSequence.toCollection(destination: C): C { - for (item in this) { - destination.add(item) - } - return destination -} - -/** - * Returns a [HashSet] of all characters. - */ -public fun CharSequence.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(length))) -} - -/** - * Returns a [List] containing all characters. - */ -public fun CharSequence.toList(): List { - return when (length) { - 0 -> emptyList() - 1 -> listOf(this[0]) - else -> this.toMutableList() - } -} - -/** - * Returns a [MutableList] filled with all characters of this char sequence. - */ -public fun CharSequence.toMutableList(): MutableList { - return toCollection(ArrayList(length)) -} - -/** - * Returns a [Set] of all characters. - * - * The returned set preserves the element iteration order of the original char sequence. - */ -public fun CharSequence.toSet(): Set { - return when (length) { - 0 -> emptySet() - 1 -> setOf(this[0]) - else -> toCollection(LinkedHashSet(mapCapacity(length))) - } -} - -/** - * Returns a single list of all elements yielded from results of [transform] function being invoked on each character of original char sequence. - */ -public inline fun CharSequence.flatMap(transform: (Char) -> Iterable): List { - return flatMapTo(ArrayList(), transform) -} - -/** - * Appends all elements yielded from results of [transform] function being invoked on each character of original char sequence, to the given [destination]. - */ -public inline fun > CharSequence.flatMapTo(destination: C, transform: (Char) -> Iterable): C { - for (element in this) { - val list = transform(element) - destination.addAll(list) - } - return destination -} - -/** - * Groups characters of the original char sequence by the key returned by the given [keySelector] function - * applied to each character and returns a map where each group key is associated with a list of corresponding characters. - * - * The returned map preserves the entry iteration order of the keys produced from the original char sequence. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun CharSequence.groupBy(keySelector: (Char) -> K): Map> { - return groupByTo(LinkedHashMap>(), keySelector) -} - -/** - * Groups values returned by the [valueTransform] function applied to each character of the original char sequence - * by the key returned by the given [keySelector] function applied to the character - * and returns a map where each group key is associated with a list of corresponding values. - * - * The returned map preserves the entry iteration order of the keys produced from the original char sequence. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun CharSequence.groupBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map> { - return groupByTo(LinkedHashMap>(), keySelector, valueTransform) -} - -/** - * Groups characters of the original char sequence by the key returned by the given [keySelector] function - * applied to each character and puts to the [destination] map each group key associated with a list of corresponding characters. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun >> CharSequence.groupByTo(destination: M, keySelector: (Char) -> K): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(element) - } - return destination -} - -/** - * Groups values returned by the [valueTransform] function applied to each character of the original char sequence - * by the key returned by the given [keySelector] function applied to the character - * and puts to the [destination] map each group key associated with a list of corresponding values. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun >> CharSequence.groupByTo(destination: M, keySelector: (Char) -> K, valueTransform: (Char) -> V): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(valueTransform(element)) - } - return destination -} - -/** - * Creates a [Grouping] source from a char sequence to be used later with one of group-and-fold operations - * using the specified [keySelector] function to extract a key from each character. - * - * @sample samples.collections.Collections.Transformations.groupingByEachCount - */ -@SinceKotlin("1.1") -public inline fun CharSequence.groupingBy(crossinline keySelector: (Char) -> K): Grouping { - return object : Grouping { - override fun sourceIterator(): Iterator = this@groupingBy.iterator() - override fun keyOf(element: Char): K = keySelector(element) - } -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each character in the original char sequence. - */ -public inline fun CharSequence.map(transform: (Char) -> R): List { - return mapTo(ArrayList(length), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each character and its index in the original char sequence. - * @param [transform] function that takes the index of a character and the character itself - * and returns the result of the transform applied to the character. - */ -public inline fun CharSequence.mapIndexed(transform: (index: Int, Char) -> R): List { - return mapIndexedTo(ArrayList(length), transform) -} - -/** - * Returns a list containing only the non-null results of applying the given [transform] function - * to each character and its index in the original char sequence. - * @param [transform] function that takes the index of a character and the character itself - * and returns the result of the transform applied to the character. - */ -public inline fun CharSequence.mapIndexedNotNull(transform: (index: Int, Char) -> R?): List { - return mapIndexedNotNullTo(ArrayList(), transform) -} - -/** - * Applies the given [transform] function to each character and its index in the original char sequence - * and appends only the non-null results to the given [destination]. - * @param [transform] function that takes the index of a character and the character itself - * and returns the result of the transform applied to the character. - */ -public inline fun > CharSequence.mapIndexedNotNullTo(destination: C, transform: (index: Int, Char) -> R?): C { - forEachIndexed { index, element -> transform(index, element)?.let { destination.add(it) } } - return destination -} - -/** - * Applies the given [transform] function to each character and its index in the original char sequence - * and appends the results to the given [destination]. - * @param [transform] function that takes the index of a character and the character itself - * and returns the result of the transform applied to the character. - */ -public inline fun > CharSequence.mapIndexedTo(destination: C, transform: (index: Int, Char) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Returns a list containing only the non-null results of applying the given [transform] function - * to each character in the original char sequence. - */ -public inline fun CharSequence.mapNotNull(transform: (Char) -> R?): List { - return mapNotNullTo(ArrayList(), transform) -} - -/** - * Applies the given [transform] function to each character in the original char sequence - * and appends only the non-null results to the given [destination]. - */ -public inline fun > CharSequence.mapNotNullTo(destination: C, transform: (Char) -> R?): C { - forEach { element -> transform(element)?.let { destination.add(it) } } - return destination -} - -/** - * Applies the given [transform] function to each character of the original char sequence - * and appends the results to the given [destination]. - */ -public inline fun > CharSequence.mapTo(destination: C, transform: (Char) -> R): C { - for (item in this) - destination.add(transform(item)) - return destination -} - -/** - * Returns a lazy [Iterable] of [IndexedValue] for each character of the original char sequence. - */ -public fun CharSequence.withIndex(): Iterable> { - return IndexingIterable { iterator() } -} - -/** - * Returns `true` if all characters match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.all - */ -public inline fun CharSequence.all(predicate: (Char) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if char sequence has at least one character. - * - * @sample samples.collections.Collections.Aggregates.any - */ -public fun CharSequence.any(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if at least one character matches the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.anyWithPredicate - */ -public inline fun CharSequence.any(predicate: (Char) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return true - return false -} - -/** - * Returns the length of this char sequence. - */ -@kotlin.internal.InlineOnly -public inline fun CharSequence.count(): Int { - return length -} - -/** - * Returns the number of characters matching the given [predicate]. - */ -public inline fun CharSequence.count(predicate: (Char) -> Boolean): Int { - var count = 0 - for (element in this) if (predicate(element)) count++ - return count -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each character. - */ -public inline fun CharSequence.fold(initial: R, operation: (acc: R, Char) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right - * to current accumulator value and each character with its index in the original char sequence. - * @param [operation] function that takes the index of a character, current accumulator value - * and the character itself, and calculates the next accumulator value. - */ -public inline fun CharSequence.foldIndexed(initial: R, operation: (index: Int, acc: R, Char) -> R): R { - var index = 0 - var accumulator = initial - for (element in this) accumulator = operation(index++, accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left to each character and current accumulator value. - */ -public inline fun CharSequence.foldRight(initial: R, operation: (Char, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left - * to each character with its index in the original char sequence and current accumulator value. - * @param [operation] function that takes the index of a character, the character itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun CharSequence.foldRightIndexed(initial: R, operation: (index: Int, Char, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Performs the given [action] on each character. - */ -public inline fun CharSequence.forEach(action: (Char) -> Unit): Unit { - for (element in this) action(element) -} - -/** - * Performs the given [action] on each character, providing sequential index with the character. - * @param [action] function that takes the index of a character and the character itself - * and performs the desired action on the character. - */ -public inline fun CharSequence.forEachIndexed(action: (index: Int, Char) -> Unit): Unit { - var index = 0 - for (item in this) action(index++, item) -} - -/** - * Returns the largest character or `null` if there are no characters. - */ -public fun CharSequence.max(): Char? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (max < e) max = e - } - return max -} - -/** - * Returns the first character yielding the largest value of the given function or `null` if there are no characters. - */ -public inline fun > CharSequence.maxBy(selector: (Char) -> R): Char? { - if (isEmpty()) return null - var maxElem = this[0] - var maxValue = selector(maxElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (maxValue < v) { - maxElem = e - maxValue = v - } - } - return maxElem -} - -/** - * Returns the first character having the largest value according to the provided [comparator] or `null` if there are no characters. - */ -public fun CharSequence.maxWith(comparator: Comparator): Char? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(max, e) < 0) max = e - } - return max -} - -/** - * Returns the smallest character or `null` if there are no characters. - */ -public fun CharSequence.min(): Char? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (min > e) min = e - } - return min -} - -/** - * Returns the first character yielding the smallest value of the given function or `null` if there are no characters. - */ -public inline fun > CharSequence.minBy(selector: (Char) -> R): Char? { - if (isEmpty()) return null - var minElem = this[0] - var minValue = selector(minElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (minValue > v) { - minElem = e - minValue = v - } - } - return minElem -} - -/** - * Returns the first character having the smallest value according to the provided [comparator] or `null` if there are no characters. - */ -public fun CharSequence.minWith(comparator: Comparator): Char? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(min, e) > 0) min = e - } - return min -} - -/** - * Returns `true` if the char sequence has no characters. - * - * @sample samples.collections.Collections.Aggregates.none - */ -public fun CharSequence.none(): Boolean { - return isEmpty() -} - -/** - * Returns `true` if no characters match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.noneWithPredicate - */ -public inline fun CharSequence.none(predicate: (Char) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return false - return true -} - -/** - * Performs the given [action] on each character and returns the char sequence itself afterwards. - */ -@SinceKotlin("1.1") -public inline fun S.onEach(action: (Char) -> Unit): S { - return apply { for (element in this) action(element) } -} - -/** - * Accumulates value starting with the first character and applying [operation] from left to right to current accumulator value and each character. - */ -public inline fun CharSequence.reduce(operation: (acc: Char, Char) -> Char): Char { - if (isEmpty()) - throw UnsupportedOperationException("Empty char sequence can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first character and applying [operation] from left to right - * to current accumulator value and each character with its index in the original char sequence. - * @param [operation] function that takes the index of a character, current accumulator value - * and the character itself and calculates the next accumulator value. - */ -public inline fun CharSequence.reduceIndexed(operation: (index: Int, acc: Char, Char) -> Char): Char { - if (isEmpty()) - throw UnsupportedOperationException("Empty char sequence can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(index, accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with last character and applying [operation] from right to left to each character and current accumulator value. - */ -public inline fun CharSequence.reduceRight(operation: (Char, acc: Char) -> Char): Char { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty char sequence can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with last character and applying [operation] from right to left - * to each character with its index in the original char sequence and current accumulator value. - * @param [operation] function that takes the index of a character, the character itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun CharSequence.reduceRightIndexed(operation: (index: Int, Char, acc: Char) -> Char): Char { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty char sequence can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Returns the sum of all values produced by [selector] function applied to each character in the char sequence. - */ -public inline fun CharSequence.sumBy(selector: (Char) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each character in the char sequence. - */ -public inline fun CharSequence.sumByDouble(selector: (Char) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Splits this char sequence into a list of strings each not exceeding the given [size]. - * - * The last string in the resulting list may have less characters than the given [size]. - * - * @param size the number of elements to take in each string, must be positive and can be greater than the number of elements in this char sequence. - * - * @sample samples.collections.Collections.Transformations.chunked - */ -@SinceKotlin("1.2") -public fun CharSequence.chunked(size: Int): List { - return windowed(size, size, partialWindows = true) -} - -/** - * Splits this char sequence into several char sequences each not exceeding the given [size] - * and applies the given [transform] function to an each. - * - * @return list of results of the [transform] applied to an each char sequence. - * - * Note that the char sequence passed to the [transform] function is ephemeral and is valid only inside that function. - * You should not store it or allow it to escape in some way, unless you made a snapshot of it. - * The last char sequence may have less characters than the given [size]. - * - * @param size the number of elements to take in each char sequence, must be positive and can be greater than the number of elements in this char sequence. - * - * @sample samples.text.Strings.chunkedTransform - */ -@SinceKotlin("1.2") -public fun CharSequence.chunked(size: Int, transform: (CharSequence) -> R): List { - return windowed(size, size, partialWindows = true, transform = transform) -} - -/** - * Splits this char sequence into a sequence of strings each not exceeding the given [size]. - * - * The last string in the resulting sequence may have less characters than the given [size]. - * - * @param size the number of elements to take in each string, must be positive and can be greater than the number of elements in this char sequence. - * - * @sample samples.collections.Collections.Transformations.chunked - */ -@SinceKotlin("1.2") -public fun CharSequence.chunkedSequence(size: Int): Sequence { - return chunkedSequence(size) { it.toString() } -} - -/** - * Splits this char sequence into several char sequences each not exceeding the given [size] - * and applies the given [transform] function to an each. - * - * @return sequence of results of the [transform] applied to an each char sequence. - * - * Note that the char sequence passed to the [transform] function is ephemeral and is valid only inside that function. - * You should not store it or allow it to escape in some way, unless you made a snapshot of it. - * The last char sequence may have less characters than the given [size]. - * - * @param size the number of elements to take in each char sequence, must be positive and can be greater than the number of elements in this char sequence. - * - * @sample samples.text.Strings.chunkedTransformToSequence - */ -@SinceKotlin("1.2") -public fun CharSequence.chunkedSequence(size: Int, transform: (CharSequence) -> R): Sequence { - return windowedSequence(size, size, partialWindows = true, transform = transform) -} - -/** - * Splits the original char sequence into pair of char sequences, - * where *first* char sequence contains characters for which [predicate] yielded `true`, - * while *second* char sequence contains characters for which [predicate] yielded `false`. - */ -public inline fun CharSequence.partition(predicate: (Char) -> Boolean): Pair { - val first = StringBuilder() - val second = StringBuilder() - for (element in this) { - if (predicate(element)) { - first.append(element) - } else { - second.append(element) - } - } - return Pair(first, second) -} - -/** - * Splits the original string into pair of strings, - * where *first* string contains characters for which [predicate] yielded `true`, - * while *second* string contains characters for which [predicate] yielded `false`. - */ -public inline fun String.partition(predicate: (Char) -> Boolean): Pair { - val first = StringBuilder() - val second = StringBuilder() - for (element in this) { - if (predicate(element)) { - first.append(element) - } else { - second.append(element) - } - } - return Pair(first.toString(), second.toString()) -} - -/** - * Returns a list of snapshots of the window of the given [size] - * sliding along this char sequence with the given [step], where each - * snapshot is a string. - * - * Several last strings may have less characters than the given [size]. - * - * Both [size] and [step] must be positive and can be greater than the number of elements in this char sequence. - * @param size the number of elements to take in each window - * @param step the number of elements to move the window forward by on an each step, by default 1 - * @param partialWindows controls whether or not to keep partial windows in the end if any, - * by default `false` which means partial windows won't be preserved - * - * @sample samples.collections.Sequences.Transformations.takeWindows - */ -@SinceKotlin("1.2") -public fun CharSequence.windowed(size: Int, step: Int = 1, partialWindows: Boolean = false): List { - return windowed(size, step, partialWindows) { it.toString() } -} - -/** - * Returns a list of results of applying the given [transform] function to - * an each char sequence representing a view over the window of the given [size] - * sliding along this char sequence with the given [step]. - * - * Note that the char sequence passed to the [transform] function is ephemeral and is valid only inside that function. - * You should not store it or allow it to escape in some way, unless you made a snapshot of it. - * Several last char sequences may have less characters than the given [size]. - * - * Both [size] and [step] must be positive and can be greater than the number of elements in this char sequence. - * @param size the number of elements to take in each window - * @param step the number of elements to move the window forward by on an each step, by default 1 - * @param partialWindows controls whether or not to keep partial windows in the end if any, - * by default `false` which means partial windows won't be preserved - * - * @sample samples.collections.Sequences.Transformations.averageWindows - */ -@SinceKotlin("1.2") -public fun CharSequence.windowed(size: Int, step: Int = 1, partialWindows: Boolean = false, transform: (CharSequence) -> R): List { - checkWindowSizeStep(size, step) - val thisSize = this.length - val result = ArrayList((thisSize + step - 1) / step) - var index = 0 - while (index < thisSize) { - val end = index + size - val coercedEnd = if (end > thisSize) { if (partialWindows) thisSize else break } else end - result.add(transform(subSequence(index, coercedEnd))) - index += step - } - return result -} - -/** - * Returns a sequence of snapshots of the window of the given [size] - * sliding along this char sequence with the given [step], where each - * snapshot is a string. - * - * Several last strings may have less characters than the given [size]. - * - * Both [size] and [step] must be positive and can be greater than the number of elements in this char sequence. - * @param size the number of elements to take in each window - * @param step the number of elements to move the window forward by on an each step, by default 1 - * @param partialWindows controls whether or not to keep partial windows in the end if any, - * by default `false` which means partial windows won't be preserved - * - * @sample samples.collections.Sequences.Transformations.takeWindows - */ -@SinceKotlin("1.2") -public fun CharSequence.windowedSequence(size: Int, step: Int = 1, partialWindows: Boolean = false): Sequence { - return windowedSequence(size, step, partialWindows) { it.toString() } -} - -/** - * Returns a sequence of results of applying the given [transform] function to - * an each char sequence representing a view over the window of the given [size] - * sliding along this char sequence with the given [step]. - * - * Note that the char sequence passed to the [transform] function is ephemeral and is valid only inside that function. - * You should not store it or allow it to escape in some way, unless you made a snapshot of it. - * Several last char sequences may have less characters than the given [size]. - * - * Both [size] and [step] must be positive and can be greater than the number of elements in this char sequence. - * @param size the number of elements to take in each window - * @param step the number of elements to move the window forward by on an each step, by default 1 - * @param partialWindows controls whether or not to keep partial windows in the end if any, - * by default `false` which means partial windows won't be preserved - * - * @sample samples.collections.Sequences.Transformations.averageWindows - */ -@SinceKotlin("1.2") -public fun CharSequence.windowedSequence(size: Int, step: Int = 1, partialWindows: Boolean = false, transform: (CharSequence) -> R): Sequence { - checkWindowSizeStep(size, step) - val windows = (if (partialWindows) indices else 0 until length - size + 1) step step - return windows.asSequence().map { index -> transform(subSequence(index, (index + size).coerceAtMost(length))) } -} - -/** - * Returns a list of pairs built from characters of both char sequences with same indexes. List has length of shortest char sequence. - */ -public infix fun CharSequence.zip(other: CharSequence): List> { - return zip(other) { c1, c2 -> c1 to c2 } -} - -/** - * Returns a list of values built from characters of both char sequences with same indexes using provided [transform]. List has length of shortest char sequence. - */ -public inline fun CharSequence.zip(other: CharSequence, transform: (a: Char, b: Char) -> V): List { - val length = minOf(this.length, other.length) - val list = ArrayList(length) - for (i in 0 until length) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Returns a list of pairs of each two adjacent characters in this char sequence. - * - * The returned list is empty if this char sequence contains less than two characters. - * - * @sample samples.collections.Collections.Transformations.zipWithNext - */ -@SinceKotlin("1.2") -public fun CharSequence.zipWithNext(): List> { - return zipWithNext { a, b -> a to b } -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to an each pair of two adjacent characters in this char sequence. - * - * The returned list is empty if this char sequence contains less than two characters. - * - * @sample samples.collections.Collections.Transformations.zipWithNextToFindDeltas - */ -@SinceKotlin("1.2") -public inline fun CharSequence.zipWithNext(transform: (a: Char, b: Char) -> R): List { - val size = length - 1 - if (size < 1) return emptyList() - val result = ArrayList(size) - for (index in 0 until size) { - result.add(transform(this[index], this[index + 1])) - } - return result -} - -/** - * Creates an [Iterable] instance that wraps the original char sequence returning its characters when being iterated. - */ -public fun CharSequence.asIterable(): Iterable { - if (this is String && isEmpty()) return emptyList() - return Iterable { this.iterator() } -} - -/** - * Creates a [Sequence] instance that wraps the original char sequence returning its characters when being iterated. - */ -public fun CharSequence.asSequence(): Sequence { - if (this is String && isEmpty()) return emptySequence() - return Sequence { this.iterator() } -} - diff --git a/libraries/stdlib/common/src/generated/_Arrays.kt b/libraries/stdlib/common/src/generated/_Arrays.kt index 4e21aa63287..9f9a6fb7f6b 100644 --- a/libraries/stdlib/common/src/generated/_Arrays.kt +++ b/libraries/stdlib/common/src/generated/_Arrays.kt @@ -16,2936 +16,5291 @@ import kotlin.comparisons.* * Returns 1st *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun Array.component1(): T +public inline operator fun Array.component1(): T { + return get(0) +} /** * Returns 1st *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun ByteArray.component1(): Byte +public inline operator fun ByteArray.component1(): Byte { + return get(0) +} /** * Returns 1st *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun ShortArray.component1(): Short +public inline operator fun ShortArray.component1(): Short { + return get(0) +} /** * Returns 1st *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun IntArray.component1(): Int +public inline operator fun IntArray.component1(): Int { + return get(0) +} /** * Returns 1st *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun LongArray.component1(): Long +public inline operator fun LongArray.component1(): Long { + return get(0) +} /** * Returns 1st *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun FloatArray.component1(): Float +public inline operator fun FloatArray.component1(): Float { + return get(0) +} /** * Returns 1st *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun DoubleArray.component1(): Double +public inline operator fun DoubleArray.component1(): Double { + return get(0) +} /** * Returns 1st *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun BooleanArray.component1(): Boolean +public inline operator fun BooleanArray.component1(): Boolean { + return get(0) +} /** * Returns 1st *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun CharArray.component1(): Char +public inline operator fun CharArray.component1(): Char { + return get(0) +} /** * Returns 2nd *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun Array.component2(): T +public inline operator fun Array.component2(): T { + return get(1) +} /** * Returns 2nd *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun ByteArray.component2(): Byte +public inline operator fun ByteArray.component2(): Byte { + return get(1) +} /** * Returns 2nd *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun ShortArray.component2(): Short +public inline operator fun ShortArray.component2(): Short { + return get(1) +} /** * Returns 2nd *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun IntArray.component2(): Int +public inline operator fun IntArray.component2(): Int { + return get(1) +} /** * Returns 2nd *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun LongArray.component2(): Long +public inline operator fun LongArray.component2(): Long { + return get(1) +} /** * Returns 2nd *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun FloatArray.component2(): Float +public inline operator fun FloatArray.component2(): Float { + return get(1) +} /** * Returns 2nd *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun DoubleArray.component2(): Double +public inline operator fun DoubleArray.component2(): Double { + return get(1) +} /** * Returns 2nd *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun BooleanArray.component2(): Boolean +public inline operator fun BooleanArray.component2(): Boolean { + return get(1) +} /** * Returns 2nd *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun CharArray.component2(): Char +public inline operator fun CharArray.component2(): Char { + return get(1) +} /** * Returns 3rd *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun Array.component3(): T +public inline operator fun Array.component3(): T { + return get(2) +} /** * Returns 3rd *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun ByteArray.component3(): Byte +public inline operator fun ByteArray.component3(): Byte { + return get(2) +} /** * Returns 3rd *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun ShortArray.component3(): Short +public inline operator fun ShortArray.component3(): Short { + return get(2) +} /** * Returns 3rd *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun IntArray.component3(): Int +public inline operator fun IntArray.component3(): Int { + return get(2) +} /** * Returns 3rd *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun LongArray.component3(): Long +public inline operator fun LongArray.component3(): Long { + return get(2) +} /** * Returns 3rd *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun FloatArray.component3(): Float +public inline operator fun FloatArray.component3(): Float { + return get(2) +} /** * Returns 3rd *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun DoubleArray.component3(): Double +public inline operator fun DoubleArray.component3(): Double { + return get(2) +} /** * Returns 3rd *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun BooleanArray.component3(): Boolean +public inline operator fun BooleanArray.component3(): Boolean { + return get(2) +} /** * Returns 3rd *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun CharArray.component3(): Char +public inline operator fun CharArray.component3(): Char { + return get(2) +} /** * Returns 4th *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun Array.component4(): T +public inline operator fun Array.component4(): T { + return get(3) +} /** * Returns 4th *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun ByteArray.component4(): Byte +public inline operator fun ByteArray.component4(): Byte { + return get(3) +} /** * Returns 4th *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun ShortArray.component4(): Short +public inline operator fun ShortArray.component4(): Short { + return get(3) +} /** * Returns 4th *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun IntArray.component4(): Int +public inline operator fun IntArray.component4(): Int { + return get(3) +} /** * Returns 4th *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun LongArray.component4(): Long +public inline operator fun LongArray.component4(): Long { + return get(3) +} /** * Returns 4th *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun FloatArray.component4(): Float +public inline operator fun FloatArray.component4(): Float { + return get(3) +} /** * Returns 4th *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun DoubleArray.component4(): Double +public inline operator fun DoubleArray.component4(): Double { + return get(3) +} /** * Returns 4th *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun BooleanArray.component4(): Boolean +public inline operator fun BooleanArray.component4(): Boolean { + return get(3) +} /** * Returns 4th *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun CharArray.component4(): Char +public inline operator fun CharArray.component4(): Char { + return get(3) +} /** * Returns 5th *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun Array.component5(): T +public inline operator fun Array.component5(): T { + return get(4) +} /** * Returns 5th *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun ByteArray.component5(): Byte +public inline operator fun ByteArray.component5(): Byte { + return get(4) +} /** * Returns 5th *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun ShortArray.component5(): Short +public inline operator fun ShortArray.component5(): Short { + return get(4) +} /** * Returns 5th *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun IntArray.component5(): Int +public inline operator fun IntArray.component5(): Int { + return get(4) +} /** * Returns 5th *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun LongArray.component5(): Long +public inline operator fun LongArray.component5(): Long { + return get(4) +} /** * Returns 5th *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun FloatArray.component5(): Float +public inline operator fun FloatArray.component5(): Float { + return get(4) +} /** * Returns 5th *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun DoubleArray.component5(): Double +public inline operator fun DoubleArray.component5(): Double { + return get(4) +} /** * Returns 5th *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun BooleanArray.component5(): Boolean +public inline operator fun BooleanArray.component5(): Boolean { + return get(4) +} /** * Returns 5th *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun CharArray.component5(): Char +public inline operator fun CharArray.component5(): Char { + return get(4) +} /** * Returns `true` if [element] is found in the array. */ -public expect operator fun <@kotlin.internal.OnlyInputTypes T> Array.contains(element: T): Boolean +public operator fun <@kotlin.internal.OnlyInputTypes T> Array.contains(element: T): Boolean { + return indexOf(element) >= 0 +} /** * Returns `true` if [element] is found in the array. */ -public expect operator fun ByteArray.contains(element: Byte): Boolean +public operator fun ByteArray.contains(element: Byte): Boolean { + return indexOf(element) >= 0 +} /** * Returns `true` if [element] is found in the array. */ -public expect operator fun ShortArray.contains(element: Short): Boolean +public operator fun ShortArray.contains(element: Short): Boolean { + return indexOf(element) >= 0 +} /** * Returns `true` if [element] is found in the array. */ -public expect operator fun IntArray.contains(element: Int): Boolean +public operator fun IntArray.contains(element: Int): Boolean { + return indexOf(element) >= 0 +} /** * Returns `true` if [element] is found in the array. */ -public expect operator fun LongArray.contains(element: Long): Boolean +public operator fun LongArray.contains(element: Long): Boolean { + return indexOf(element) >= 0 +} /** * Returns `true` if [element] is found in the array. */ -public expect operator fun FloatArray.contains(element: Float): Boolean +public operator fun FloatArray.contains(element: Float): Boolean { + return indexOf(element) >= 0 +} /** * Returns `true` if [element] is found in the array. */ -public expect operator fun DoubleArray.contains(element: Double): Boolean +public operator fun DoubleArray.contains(element: Double): Boolean { + return indexOf(element) >= 0 +} /** * Returns `true` if [element] is found in the array. */ -public expect operator fun BooleanArray.contains(element: Boolean): Boolean +public operator fun BooleanArray.contains(element: Boolean): Boolean { + return indexOf(element) >= 0 +} /** * Returns `true` if [element] is found in the array. */ -public expect operator fun CharArray.contains(element: Char): Boolean +public operator fun CharArray.contains(element: Char): Boolean { + return indexOf(element) >= 0 +} /** * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun Array.elementAt(index: Int): T +public inline fun Array.elementAt(index: Int): T { + return get(index) +} /** * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun ByteArray.elementAt(index: Int): Byte +public inline fun ByteArray.elementAt(index: Int): Byte { + return get(index) +} /** * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun ShortArray.elementAt(index: Int): Short +public inline fun ShortArray.elementAt(index: Int): Short { + return get(index) +} /** * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun IntArray.elementAt(index: Int): Int +public inline fun IntArray.elementAt(index: Int): Int { + return get(index) +} /** * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun LongArray.elementAt(index: Int): Long +public inline fun LongArray.elementAt(index: Int): Long { + return get(index) +} /** * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun FloatArray.elementAt(index: Int): Float +public inline fun FloatArray.elementAt(index: Int): Float { + return get(index) +} /** * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun DoubleArray.elementAt(index: Int): Double +public inline fun DoubleArray.elementAt(index: Int): Double { + return get(index) +} /** * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun BooleanArray.elementAt(index: Int): Boolean +public inline fun BooleanArray.elementAt(index: Int): Boolean { + return get(index) +} /** * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun CharArray.elementAt(index: Int): Char +public inline fun CharArray.elementAt(index: Int): Char { + return get(index) +} /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun Array.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T +public inline fun Array.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T { + return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) +} /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun ByteArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Byte): Byte +public inline fun ByteArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Byte): Byte { + return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) +} /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun ShortArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Short): Short +public inline fun ShortArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Short): Short { + return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) +} /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun IntArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Int): Int +public inline fun IntArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Int): Int { + return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) +} /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun LongArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Long): Long +public inline fun LongArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Long): Long { + return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) +} /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun FloatArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Float): Float +public inline fun FloatArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Float): Float { + return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) +} /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun DoubleArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Double): Double +public inline fun DoubleArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Double): Double { + return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) +} /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun BooleanArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Boolean): Boolean +public inline fun BooleanArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Boolean): Boolean { + return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) +} /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun CharArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Char): Char +public inline fun CharArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Char): Char { + return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) +} /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun Array.elementAtOrNull(index: Int): T? +public inline fun Array.elementAtOrNull(index: Int): T? { + return this.getOrNull(index) +} /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun ByteArray.elementAtOrNull(index: Int): Byte? +public inline fun ByteArray.elementAtOrNull(index: Int): Byte? { + return this.getOrNull(index) +} /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun ShortArray.elementAtOrNull(index: Int): Short? +public inline fun ShortArray.elementAtOrNull(index: Int): Short? { + return this.getOrNull(index) +} /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun IntArray.elementAtOrNull(index: Int): Int? +public inline fun IntArray.elementAtOrNull(index: Int): Int? { + return this.getOrNull(index) +} /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun LongArray.elementAtOrNull(index: Int): Long? +public inline fun LongArray.elementAtOrNull(index: Int): Long? { + return this.getOrNull(index) +} /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun FloatArray.elementAtOrNull(index: Int): Float? +public inline fun FloatArray.elementAtOrNull(index: Int): Float? { + return this.getOrNull(index) +} /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun DoubleArray.elementAtOrNull(index: Int): Double? +public inline fun DoubleArray.elementAtOrNull(index: Int): Double? { + return this.getOrNull(index) +} /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun BooleanArray.elementAtOrNull(index: Int): Boolean? +public inline fun BooleanArray.elementAtOrNull(index: Int): Boolean? { + return this.getOrNull(index) +} /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun CharArray.elementAtOrNull(index: Int): Char? +public inline fun CharArray.elementAtOrNull(index: Int): Char? { + return this.getOrNull(index) +} /** * Returns the first element matching the given [predicate], or `null` if no such element was found. */ @kotlin.internal.InlineOnly -public expect inline fun Array.find(predicate: (T) -> Boolean): T? +public inline fun Array.find(predicate: (T) -> Boolean): T? { + return firstOrNull(predicate) +} /** * Returns the first element matching the given [predicate], or `null` if no such element was found. */ @kotlin.internal.InlineOnly -public expect inline fun ByteArray.find(predicate: (Byte) -> Boolean): Byte? +public inline fun ByteArray.find(predicate: (Byte) -> Boolean): Byte? { + return firstOrNull(predicate) +} /** * Returns the first element matching the given [predicate], or `null` if no such element was found. */ @kotlin.internal.InlineOnly -public expect inline fun ShortArray.find(predicate: (Short) -> Boolean): Short? +public inline fun ShortArray.find(predicate: (Short) -> Boolean): Short? { + return firstOrNull(predicate) +} /** * Returns the first element matching the given [predicate], or `null` if no such element was found. */ @kotlin.internal.InlineOnly -public expect inline fun IntArray.find(predicate: (Int) -> Boolean): Int? +public inline fun IntArray.find(predicate: (Int) -> Boolean): Int? { + return firstOrNull(predicate) +} /** * Returns the first element matching the given [predicate], or `null` if no such element was found. */ @kotlin.internal.InlineOnly -public expect inline fun LongArray.find(predicate: (Long) -> Boolean): Long? +public inline fun LongArray.find(predicate: (Long) -> Boolean): Long? { + return firstOrNull(predicate) +} /** * Returns the first element matching the given [predicate], or `null` if no such element was found. */ @kotlin.internal.InlineOnly -public expect inline fun FloatArray.find(predicate: (Float) -> Boolean): Float? +public inline fun FloatArray.find(predicate: (Float) -> Boolean): Float? { + return firstOrNull(predicate) +} /** * Returns the first element matching the given [predicate], or `null` if no such element was found. */ @kotlin.internal.InlineOnly -public expect inline fun DoubleArray.find(predicate: (Double) -> Boolean): Double? +public inline fun DoubleArray.find(predicate: (Double) -> Boolean): Double? { + return firstOrNull(predicate) +} /** * Returns the first element matching the given [predicate], or `null` if no such element was found. */ @kotlin.internal.InlineOnly -public expect inline fun BooleanArray.find(predicate: (Boolean) -> Boolean): Boolean? +public inline fun BooleanArray.find(predicate: (Boolean) -> Boolean): Boolean? { + return firstOrNull(predicate) +} /** * Returns the first element matching the given [predicate], or `null` if no such element was found. */ @kotlin.internal.InlineOnly -public expect inline fun CharArray.find(predicate: (Char) -> Boolean): Char? +public inline fun CharArray.find(predicate: (Char) -> Boolean): Char? { + return firstOrNull(predicate) +} /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ @kotlin.internal.InlineOnly -public expect inline fun Array.findLast(predicate: (T) -> Boolean): T? +public inline fun Array.findLast(predicate: (T) -> Boolean): T? { + return lastOrNull(predicate) +} /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ @kotlin.internal.InlineOnly -public expect inline fun ByteArray.findLast(predicate: (Byte) -> Boolean): Byte? +public inline fun ByteArray.findLast(predicate: (Byte) -> Boolean): Byte? { + return lastOrNull(predicate) +} /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ @kotlin.internal.InlineOnly -public expect inline fun ShortArray.findLast(predicate: (Short) -> Boolean): Short? +public inline fun ShortArray.findLast(predicate: (Short) -> Boolean): Short? { + return lastOrNull(predicate) +} /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ @kotlin.internal.InlineOnly -public expect inline fun IntArray.findLast(predicate: (Int) -> Boolean): Int? +public inline fun IntArray.findLast(predicate: (Int) -> Boolean): Int? { + return lastOrNull(predicate) +} /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ @kotlin.internal.InlineOnly -public expect inline fun LongArray.findLast(predicate: (Long) -> Boolean): Long? +public inline fun LongArray.findLast(predicate: (Long) -> Boolean): Long? { + return lastOrNull(predicate) +} /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ @kotlin.internal.InlineOnly -public expect inline fun FloatArray.findLast(predicate: (Float) -> Boolean): Float? +public inline fun FloatArray.findLast(predicate: (Float) -> Boolean): Float? { + return lastOrNull(predicate) +} /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ @kotlin.internal.InlineOnly -public expect inline fun DoubleArray.findLast(predicate: (Double) -> Boolean): Double? +public inline fun DoubleArray.findLast(predicate: (Double) -> Boolean): Double? { + return lastOrNull(predicate) +} /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ @kotlin.internal.InlineOnly -public expect inline fun BooleanArray.findLast(predicate: (Boolean) -> Boolean): Boolean? +public inline fun BooleanArray.findLast(predicate: (Boolean) -> Boolean): Boolean? { + return lastOrNull(predicate) +} /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ @kotlin.internal.InlineOnly -public expect inline fun CharArray.findLast(predicate: (Char) -> Boolean): Char? +public inline fun CharArray.findLast(predicate: (Char) -> Boolean): Char? { + return lastOrNull(predicate) +} /** * Returns first element. * @throws [NoSuchElementException] if the array is empty. */ -public expect fun Array.first(): T +public fun Array.first(): T { + if (isEmpty()) + throw NoSuchElementException("Array is empty.") + return this[0] +} /** * Returns first element. * @throws [NoSuchElementException] if the array is empty. */ -public expect fun ByteArray.first(): Byte +public fun ByteArray.first(): Byte { + if (isEmpty()) + throw NoSuchElementException("Array is empty.") + return this[0] +} /** * Returns first element. * @throws [NoSuchElementException] if the array is empty. */ -public expect fun ShortArray.first(): Short +public fun ShortArray.first(): Short { + if (isEmpty()) + throw NoSuchElementException("Array is empty.") + return this[0] +} /** * Returns first element. * @throws [NoSuchElementException] if the array is empty. */ -public expect fun IntArray.first(): Int +public fun IntArray.first(): Int { + if (isEmpty()) + throw NoSuchElementException("Array is empty.") + return this[0] +} /** * Returns first element. * @throws [NoSuchElementException] if the array is empty. */ -public expect fun LongArray.first(): Long +public fun LongArray.first(): Long { + if (isEmpty()) + throw NoSuchElementException("Array is empty.") + return this[0] +} /** * Returns first element. * @throws [NoSuchElementException] if the array is empty. */ -public expect fun FloatArray.first(): Float +public fun FloatArray.first(): Float { + if (isEmpty()) + throw NoSuchElementException("Array is empty.") + return this[0] +} /** * Returns first element. * @throws [NoSuchElementException] if the array is empty. */ -public expect fun DoubleArray.first(): Double +public fun DoubleArray.first(): Double { + if (isEmpty()) + throw NoSuchElementException("Array is empty.") + return this[0] +} /** * Returns first element. * @throws [NoSuchElementException] if the array is empty. */ -public expect fun BooleanArray.first(): Boolean +public fun BooleanArray.first(): Boolean { + if (isEmpty()) + throw NoSuchElementException("Array is empty.") + return this[0] +} /** * Returns first element. * @throws [NoSuchElementException] if the array is empty. */ -public expect fun CharArray.first(): Char +public fun CharArray.first(): Char { + if (isEmpty()) + throw NoSuchElementException("Array is empty.") + return this[0] +} /** * Returns the first element matching the given [predicate]. * @throws [NoSuchElementException] if no such element is found. */ -public expect inline fun Array.first(predicate: (T) -> Boolean): T +public inline fun Array.first(predicate: (T) -> Boolean): T { + for (element in this) if (predicate(element)) return element + throw NoSuchElementException("Array contains no element matching the predicate.") +} /** * Returns the first element matching the given [predicate]. * @throws [NoSuchElementException] if no such element is found. */ -public expect inline fun ByteArray.first(predicate: (Byte) -> Boolean): Byte +public inline fun ByteArray.first(predicate: (Byte) -> Boolean): Byte { + for (element in this) if (predicate(element)) return element + throw NoSuchElementException("Array contains no element matching the predicate.") +} /** * Returns the first element matching the given [predicate]. * @throws [NoSuchElementException] if no such element is found. */ -public expect inline fun ShortArray.first(predicate: (Short) -> Boolean): Short +public inline fun ShortArray.first(predicate: (Short) -> Boolean): Short { + for (element in this) if (predicate(element)) return element + throw NoSuchElementException("Array contains no element matching the predicate.") +} /** * Returns the first element matching the given [predicate]. * @throws [NoSuchElementException] if no such element is found. */ -public expect inline fun IntArray.first(predicate: (Int) -> Boolean): Int +public inline fun IntArray.first(predicate: (Int) -> Boolean): Int { + for (element in this) if (predicate(element)) return element + throw NoSuchElementException("Array contains no element matching the predicate.") +} /** * Returns the first element matching the given [predicate]. * @throws [NoSuchElementException] if no such element is found. */ -public expect inline fun LongArray.first(predicate: (Long) -> Boolean): Long +public inline fun LongArray.first(predicate: (Long) -> Boolean): Long { + for (element in this) if (predicate(element)) return element + throw NoSuchElementException("Array contains no element matching the predicate.") +} /** * Returns the first element matching the given [predicate]. * @throws [NoSuchElementException] if no such element is found. */ -public expect inline fun FloatArray.first(predicate: (Float) -> Boolean): Float +public inline fun FloatArray.first(predicate: (Float) -> Boolean): Float { + for (element in this) if (predicate(element)) return element + throw NoSuchElementException("Array contains no element matching the predicate.") +} /** * Returns the first element matching the given [predicate]. * @throws [NoSuchElementException] if no such element is found. */ -public expect inline fun DoubleArray.first(predicate: (Double) -> Boolean): Double +public inline fun DoubleArray.first(predicate: (Double) -> Boolean): Double { + for (element in this) if (predicate(element)) return element + throw NoSuchElementException("Array contains no element matching the predicate.") +} /** * Returns the first element matching the given [predicate]. * @throws [NoSuchElementException] if no such element is found. */ -public expect inline fun BooleanArray.first(predicate: (Boolean) -> Boolean): Boolean +public inline fun BooleanArray.first(predicate: (Boolean) -> Boolean): Boolean { + for (element in this) if (predicate(element)) return element + throw NoSuchElementException("Array contains no element matching the predicate.") +} /** * Returns the first element matching the given [predicate]. * @throws [NoSuchElementException] if no such element is found. */ -public expect inline fun CharArray.first(predicate: (Char) -> Boolean): Char +public inline fun CharArray.first(predicate: (Char) -> Boolean): Char { + for (element in this) if (predicate(element)) return element + throw NoSuchElementException("Array contains no element matching the predicate.") +} /** * Returns the first element, or `null` if the array is empty. */ -public expect fun Array.firstOrNull(): T? +public fun Array.firstOrNull(): T? { + return if (isEmpty()) null else this[0] +} /** * Returns the first element, or `null` if the array is empty. */ -public expect fun ByteArray.firstOrNull(): Byte? +public fun ByteArray.firstOrNull(): Byte? { + return if (isEmpty()) null else this[0] +} /** * Returns the first element, or `null` if the array is empty. */ -public expect fun ShortArray.firstOrNull(): Short? +public fun ShortArray.firstOrNull(): Short? { + return if (isEmpty()) null else this[0] +} /** * Returns the first element, or `null` if the array is empty. */ -public expect fun IntArray.firstOrNull(): Int? +public fun IntArray.firstOrNull(): Int? { + return if (isEmpty()) null else this[0] +} /** * Returns the first element, or `null` if the array is empty. */ -public expect fun LongArray.firstOrNull(): Long? +public fun LongArray.firstOrNull(): Long? { + return if (isEmpty()) null else this[0] +} /** * Returns the first element, or `null` if the array is empty. */ -public expect fun FloatArray.firstOrNull(): Float? +public fun FloatArray.firstOrNull(): Float? { + return if (isEmpty()) null else this[0] +} /** * Returns the first element, or `null` if the array is empty. */ -public expect fun DoubleArray.firstOrNull(): Double? +public fun DoubleArray.firstOrNull(): Double? { + return if (isEmpty()) null else this[0] +} /** * Returns the first element, or `null` if the array is empty. */ -public expect fun BooleanArray.firstOrNull(): Boolean? +public fun BooleanArray.firstOrNull(): Boolean? { + return if (isEmpty()) null else this[0] +} /** * Returns the first element, or `null` if the array is empty. */ -public expect fun CharArray.firstOrNull(): Char? +public fun CharArray.firstOrNull(): Char? { + return if (isEmpty()) null else this[0] +} /** * Returns the first element matching the given [predicate], or `null` if element was not found. */ -public expect inline fun Array.firstOrNull(predicate: (T) -> Boolean): T? +public inline fun Array.firstOrNull(predicate: (T) -> Boolean): T? { + for (element in this) if (predicate(element)) return element + return null +} /** * Returns the first element matching the given [predicate], or `null` if element was not found. */ -public expect inline fun ByteArray.firstOrNull(predicate: (Byte) -> Boolean): Byte? +public inline fun ByteArray.firstOrNull(predicate: (Byte) -> Boolean): Byte? { + for (element in this) if (predicate(element)) return element + return null +} /** * Returns the first element matching the given [predicate], or `null` if element was not found. */ -public expect inline fun ShortArray.firstOrNull(predicate: (Short) -> Boolean): Short? +public inline fun ShortArray.firstOrNull(predicate: (Short) -> Boolean): Short? { + for (element in this) if (predicate(element)) return element + return null +} /** * Returns the first element matching the given [predicate], or `null` if element was not found. */ -public expect inline fun IntArray.firstOrNull(predicate: (Int) -> Boolean): Int? +public inline fun IntArray.firstOrNull(predicate: (Int) -> Boolean): Int? { + for (element in this) if (predicate(element)) return element + return null +} /** * Returns the first element matching the given [predicate], or `null` if element was not found. */ -public expect inline fun LongArray.firstOrNull(predicate: (Long) -> Boolean): Long? +public inline fun LongArray.firstOrNull(predicate: (Long) -> Boolean): Long? { + for (element in this) if (predicate(element)) return element + return null +} /** * Returns the first element matching the given [predicate], or `null` if element was not found. */ -public expect inline fun FloatArray.firstOrNull(predicate: (Float) -> Boolean): Float? +public inline fun FloatArray.firstOrNull(predicate: (Float) -> Boolean): Float? { + for (element in this) if (predicate(element)) return element + return null +} /** * Returns the first element matching the given [predicate], or `null` if element was not found. */ -public expect inline fun DoubleArray.firstOrNull(predicate: (Double) -> Boolean): Double? +public inline fun DoubleArray.firstOrNull(predicate: (Double) -> Boolean): Double? { + for (element in this) if (predicate(element)) return element + return null +} /** * Returns the first element matching the given [predicate], or `null` if element was not found. */ -public expect inline fun BooleanArray.firstOrNull(predicate: (Boolean) -> Boolean): Boolean? +public inline fun BooleanArray.firstOrNull(predicate: (Boolean) -> Boolean): Boolean? { + for (element in this) if (predicate(element)) return element + return null +} /** * Returns the first element matching the given [predicate], or `null` if element was not found. */ -public expect inline fun CharArray.firstOrNull(predicate: (Char) -> Boolean): Char? +public inline fun CharArray.firstOrNull(predicate: (Char) -> Boolean): Char? { + for (element in this) if (predicate(element)) return element + return null +} /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun Array.getOrElse(index: Int, defaultValue: (Int) -> T): T +public inline fun Array.getOrElse(index: Int, defaultValue: (Int) -> T): T { + return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) +} /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun ByteArray.getOrElse(index: Int, defaultValue: (Int) -> Byte): Byte +public inline fun ByteArray.getOrElse(index: Int, defaultValue: (Int) -> Byte): Byte { + return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) +} /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun ShortArray.getOrElse(index: Int, defaultValue: (Int) -> Short): Short +public inline fun ShortArray.getOrElse(index: Int, defaultValue: (Int) -> Short): Short { + return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) +} /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun IntArray.getOrElse(index: Int, defaultValue: (Int) -> Int): Int +public inline fun IntArray.getOrElse(index: Int, defaultValue: (Int) -> Int): Int { + return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) +} /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun LongArray.getOrElse(index: Int, defaultValue: (Int) -> Long): Long +public inline fun LongArray.getOrElse(index: Int, defaultValue: (Int) -> Long): Long { + return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) +} /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun FloatArray.getOrElse(index: Int, defaultValue: (Int) -> Float): Float +public inline fun FloatArray.getOrElse(index: Int, defaultValue: (Int) -> Float): Float { + return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) +} /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun DoubleArray.getOrElse(index: Int, defaultValue: (Int) -> Double): Double +public inline fun DoubleArray.getOrElse(index: Int, defaultValue: (Int) -> Double): Double { + return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) +} /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun BooleanArray.getOrElse(index: Int, defaultValue: (Int) -> Boolean): Boolean +public inline fun BooleanArray.getOrElse(index: Int, defaultValue: (Int) -> Boolean): Boolean { + return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) +} /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. */ @kotlin.internal.InlineOnly -public expect inline fun CharArray.getOrElse(index: Int, defaultValue: (Int) -> Char): Char +public inline fun CharArray.getOrElse(index: Int, defaultValue: (Int) -> Char): Char { + return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) +} /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. */ -public expect fun Array.getOrNull(index: Int): T? +public fun Array.getOrNull(index: Int): T? { + return if (index >= 0 && index <= lastIndex) get(index) else null +} /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. */ -public expect fun ByteArray.getOrNull(index: Int): Byte? +public fun ByteArray.getOrNull(index: Int): Byte? { + return if (index >= 0 && index <= lastIndex) get(index) else null +} /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. */ -public expect fun ShortArray.getOrNull(index: Int): Short? +public fun ShortArray.getOrNull(index: Int): Short? { + return if (index >= 0 && index <= lastIndex) get(index) else null +} /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. */ -public expect fun IntArray.getOrNull(index: Int): Int? +public fun IntArray.getOrNull(index: Int): Int? { + return if (index >= 0 && index <= lastIndex) get(index) else null +} /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. */ -public expect fun LongArray.getOrNull(index: Int): Long? +public fun LongArray.getOrNull(index: Int): Long? { + return if (index >= 0 && index <= lastIndex) get(index) else null +} /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. */ -public expect fun FloatArray.getOrNull(index: Int): Float? +public fun FloatArray.getOrNull(index: Int): Float? { + return if (index >= 0 && index <= lastIndex) get(index) else null +} /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. */ -public expect fun DoubleArray.getOrNull(index: Int): Double? +public fun DoubleArray.getOrNull(index: Int): Double? { + return if (index >= 0 && index <= lastIndex) get(index) else null +} /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. */ -public expect fun BooleanArray.getOrNull(index: Int): Boolean? +public fun BooleanArray.getOrNull(index: Int): Boolean? { + return if (index >= 0 && index <= lastIndex) get(index) else null +} /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. */ -public expect fun CharArray.getOrNull(index: Int): Char? +public fun CharArray.getOrNull(index: Int): Char? { + return if (index >= 0 && index <= lastIndex) get(index) else null +} /** * Returns first index of [element], or -1 if the array does not contain element. */ -public expect fun <@kotlin.internal.OnlyInputTypes T> Array.indexOf(element: T): Int +public fun <@kotlin.internal.OnlyInputTypes T> Array.indexOf(element: T): Int { + if (element == null) { + for (index in indices) { + if (this[index] == null) { + return index + } + } + } else { + for (index in indices) { + if (element == this[index]) { + return index + } + } + } + return -1 +} /** * Returns first index of [element], or -1 if the array does not contain element. */ -public expect fun ByteArray.indexOf(element: Byte): Int +public fun ByteArray.indexOf(element: Byte): Int { + for (index in indices) { + if (element == this[index]) { + return index + } + } + return -1 +} /** * Returns first index of [element], or -1 if the array does not contain element. */ -public expect fun ShortArray.indexOf(element: Short): Int +public fun ShortArray.indexOf(element: Short): Int { + for (index in indices) { + if (element == this[index]) { + return index + } + } + return -1 +} /** * Returns first index of [element], or -1 if the array does not contain element. */ -public expect fun IntArray.indexOf(element: Int): Int +public fun IntArray.indexOf(element: Int): Int { + for (index in indices) { + if (element == this[index]) { + return index + } + } + return -1 +} /** * Returns first index of [element], or -1 if the array does not contain element. */ -public expect fun LongArray.indexOf(element: Long): Int +public fun LongArray.indexOf(element: Long): Int { + for (index in indices) { + if (element == this[index]) { + return index + } + } + return -1 +} /** * Returns first index of [element], or -1 if the array does not contain element. */ -public expect fun FloatArray.indexOf(element: Float): Int +public fun FloatArray.indexOf(element: Float): Int { + for (index in indices) { + if (element == this[index]) { + return index + } + } + return -1 +} /** * Returns first index of [element], or -1 if the array does not contain element. */ -public expect fun DoubleArray.indexOf(element: Double): Int +public fun DoubleArray.indexOf(element: Double): Int { + for (index in indices) { + if (element == this[index]) { + return index + } + } + return -1 +} /** * Returns first index of [element], or -1 if the array does not contain element. */ -public expect fun BooleanArray.indexOf(element: Boolean): Int +public fun BooleanArray.indexOf(element: Boolean): Int { + for (index in indices) { + if (element == this[index]) { + return index + } + } + return -1 +} /** * Returns first index of [element], or -1 if the array does not contain element. */ -public expect fun CharArray.indexOf(element: Char): Int +public fun CharArray.indexOf(element: Char): Int { + for (index in indices) { + if (element == this[index]) { + return index + } + } + return -1 +} /** * Returns index of the first element matching the given [predicate], or -1 if the array does not contain such element. */ -public expect inline fun Array.indexOfFirst(predicate: (T) -> Boolean): Int +public inline fun Array.indexOfFirst(predicate: (T) -> Boolean): Int { + for (index in indices) { + if (predicate(this[index])) { + return index + } + } + return -1 +} /** * Returns index of the first element matching the given [predicate], or -1 if the array does not contain such element. */ -public expect inline fun ByteArray.indexOfFirst(predicate: (Byte) -> Boolean): Int +public inline fun ByteArray.indexOfFirst(predicate: (Byte) -> Boolean): Int { + for (index in indices) { + if (predicate(this[index])) { + return index + } + } + return -1 +} /** * Returns index of the first element matching the given [predicate], or -1 if the array does not contain such element. */ -public expect inline fun ShortArray.indexOfFirst(predicate: (Short) -> Boolean): Int +public inline fun ShortArray.indexOfFirst(predicate: (Short) -> Boolean): Int { + for (index in indices) { + if (predicate(this[index])) { + return index + } + } + return -1 +} /** * Returns index of the first element matching the given [predicate], or -1 if the array does not contain such element. */ -public expect inline fun IntArray.indexOfFirst(predicate: (Int) -> Boolean): Int +public inline fun IntArray.indexOfFirst(predicate: (Int) -> Boolean): Int { + for (index in indices) { + if (predicate(this[index])) { + return index + } + } + return -1 +} /** * Returns index of the first element matching the given [predicate], or -1 if the array does not contain such element. */ -public expect inline fun LongArray.indexOfFirst(predicate: (Long) -> Boolean): Int +public inline fun LongArray.indexOfFirst(predicate: (Long) -> Boolean): Int { + for (index in indices) { + if (predicate(this[index])) { + return index + } + } + return -1 +} /** * Returns index of the first element matching the given [predicate], or -1 if the array does not contain such element. */ -public expect inline fun FloatArray.indexOfFirst(predicate: (Float) -> Boolean): Int +public inline fun FloatArray.indexOfFirst(predicate: (Float) -> Boolean): Int { + for (index in indices) { + if (predicate(this[index])) { + return index + } + } + return -1 +} /** * Returns index of the first element matching the given [predicate], or -1 if the array does not contain such element. */ -public expect inline fun DoubleArray.indexOfFirst(predicate: (Double) -> Boolean): Int +public inline fun DoubleArray.indexOfFirst(predicate: (Double) -> Boolean): Int { + for (index in indices) { + if (predicate(this[index])) { + return index + } + } + return -1 +} /** * Returns index of the first element matching the given [predicate], or -1 if the array does not contain such element. */ -public expect inline fun BooleanArray.indexOfFirst(predicate: (Boolean) -> Boolean): Int +public inline fun BooleanArray.indexOfFirst(predicate: (Boolean) -> Boolean): Int { + for (index in indices) { + if (predicate(this[index])) { + return index + } + } + return -1 +} /** * Returns index of the first element matching the given [predicate], or -1 if the array does not contain such element. */ -public expect inline fun CharArray.indexOfFirst(predicate: (Char) -> Boolean): Int +public inline fun CharArray.indexOfFirst(predicate: (Char) -> Boolean): Int { + for (index in indices) { + if (predicate(this[index])) { + return index + } + } + return -1 +} /** * Returns index of the last element matching the given [predicate], or -1 if the array does not contain such element. */ -public expect inline fun Array.indexOfLast(predicate: (T) -> Boolean): Int +public inline fun Array.indexOfLast(predicate: (T) -> Boolean): Int { + for (index in indices.reversed()) { + if (predicate(this[index])) { + return index + } + } + return -1 +} /** * Returns index of the last element matching the given [predicate], or -1 if the array does not contain such element. */ -public expect inline fun ByteArray.indexOfLast(predicate: (Byte) -> Boolean): Int +public inline fun ByteArray.indexOfLast(predicate: (Byte) -> Boolean): Int { + for (index in indices.reversed()) { + if (predicate(this[index])) { + return index + } + } + return -1 +} /** * Returns index of the last element matching the given [predicate], or -1 if the array does not contain such element. */ -public expect inline fun ShortArray.indexOfLast(predicate: (Short) -> Boolean): Int +public inline fun ShortArray.indexOfLast(predicate: (Short) -> Boolean): Int { + for (index in indices.reversed()) { + if (predicate(this[index])) { + return index + } + } + return -1 +} /** * Returns index of the last element matching the given [predicate], or -1 if the array does not contain such element. */ -public expect inline fun IntArray.indexOfLast(predicate: (Int) -> Boolean): Int +public inline fun IntArray.indexOfLast(predicate: (Int) -> Boolean): Int { + for (index in indices.reversed()) { + if (predicate(this[index])) { + return index + } + } + return -1 +} /** * Returns index of the last element matching the given [predicate], or -1 if the array does not contain such element. */ -public expect inline fun LongArray.indexOfLast(predicate: (Long) -> Boolean): Int +public inline fun LongArray.indexOfLast(predicate: (Long) -> Boolean): Int { + for (index in indices.reversed()) { + if (predicate(this[index])) { + return index + } + } + return -1 +} /** * Returns index of the last element matching the given [predicate], or -1 if the array does not contain such element. */ -public expect inline fun FloatArray.indexOfLast(predicate: (Float) -> Boolean): Int +public inline fun FloatArray.indexOfLast(predicate: (Float) -> Boolean): Int { + for (index in indices.reversed()) { + if (predicate(this[index])) { + return index + } + } + return -1 +} /** * Returns index of the last element matching the given [predicate], or -1 if the array does not contain such element. */ -public expect inline fun DoubleArray.indexOfLast(predicate: (Double) -> Boolean): Int +public inline fun DoubleArray.indexOfLast(predicate: (Double) -> Boolean): Int { + for (index in indices.reversed()) { + if (predicate(this[index])) { + return index + } + } + return -1 +} /** * Returns index of the last element matching the given [predicate], or -1 if the array does not contain such element. */ -public expect inline fun BooleanArray.indexOfLast(predicate: (Boolean) -> Boolean): Int +public inline fun BooleanArray.indexOfLast(predicate: (Boolean) -> Boolean): Int { + for (index in indices.reversed()) { + if (predicate(this[index])) { + return index + } + } + return -1 +} /** * Returns index of the last element matching the given [predicate], or -1 if the array does not contain such element. */ -public expect inline fun CharArray.indexOfLast(predicate: (Char) -> Boolean): Int +public inline fun CharArray.indexOfLast(predicate: (Char) -> Boolean): Int { + for (index in indices.reversed()) { + if (predicate(this[index])) { + return index + } + } + return -1 +} /** * Returns the last element. * @throws [NoSuchElementException] if the array is empty. */ -public expect fun Array.last(): T +public fun Array.last(): T { + if (isEmpty()) + throw NoSuchElementException("Array is empty.") + return this[lastIndex] +} /** * Returns the last element. * @throws [NoSuchElementException] if the array is empty. */ -public expect fun ByteArray.last(): Byte +public fun ByteArray.last(): Byte { + if (isEmpty()) + throw NoSuchElementException("Array is empty.") + return this[lastIndex] +} /** * Returns the last element. * @throws [NoSuchElementException] if the array is empty. */ -public expect fun ShortArray.last(): Short +public fun ShortArray.last(): Short { + if (isEmpty()) + throw NoSuchElementException("Array is empty.") + return this[lastIndex] +} /** * Returns the last element. * @throws [NoSuchElementException] if the array is empty. */ -public expect fun IntArray.last(): Int +public fun IntArray.last(): Int { + if (isEmpty()) + throw NoSuchElementException("Array is empty.") + return this[lastIndex] +} /** * Returns the last element. * @throws [NoSuchElementException] if the array is empty. */ -public expect fun LongArray.last(): Long +public fun LongArray.last(): Long { + if (isEmpty()) + throw NoSuchElementException("Array is empty.") + return this[lastIndex] +} /** * Returns the last element. * @throws [NoSuchElementException] if the array is empty. */ -public expect fun FloatArray.last(): Float +public fun FloatArray.last(): Float { + if (isEmpty()) + throw NoSuchElementException("Array is empty.") + return this[lastIndex] +} /** * Returns the last element. * @throws [NoSuchElementException] if the array is empty. */ -public expect fun DoubleArray.last(): Double +public fun DoubleArray.last(): Double { + if (isEmpty()) + throw NoSuchElementException("Array is empty.") + return this[lastIndex] +} /** * Returns the last element. * @throws [NoSuchElementException] if the array is empty. */ -public expect fun BooleanArray.last(): Boolean +public fun BooleanArray.last(): Boolean { + if (isEmpty()) + throw NoSuchElementException("Array is empty.") + return this[lastIndex] +} /** * Returns the last element. * @throws [NoSuchElementException] if the array is empty. */ -public expect fun CharArray.last(): Char +public fun CharArray.last(): Char { + if (isEmpty()) + throw NoSuchElementException("Array is empty.") + return this[lastIndex] +} /** * Returns the last element matching the given [predicate]. * @throws [NoSuchElementException] if no such element is found. */ -public expect inline fun Array.last(predicate: (T) -> Boolean): T +public inline fun Array.last(predicate: (T) -> Boolean): T { + for (index in this.indices.reversed()) { + val element = this[index] + if (predicate(element)) return element + } + throw NoSuchElementException("Array contains no element matching the predicate.") +} /** * Returns the last element matching the given [predicate]. * @throws [NoSuchElementException] if no such element is found. */ -public expect inline fun ByteArray.last(predicate: (Byte) -> Boolean): Byte +public inline fun ByteArray.last(predicate: (Byte) -> Boolean): Byte { + for (index in this.indices.reversed()) { + val element = this[index] + if (predicate(element)) return element + } + throw NoSuchElementException("Array contains no element matching the predicate.") +} /** * Returns the last element matching the given [predicate]. * @throws [NoSuchElementException] if no such element is found. */ -public expect inline fun ShortArray.last(predicate: (Short) -> Boolean): Short +public inline fun ShortArray.last(predicate: (Short) -> Boolean): Short { + for (index in this.indices.reversed()) { + val element = this[index] + if (predicate(element)) return element + } + throw NoSuchElementException("Array contains no element matching the predicate.") +} /** * Returns the last element matching the given [predicate]. * @throws [NoSuchElementException] if no such element is found. */ -public expect inline fun IntArray.last(predicate: (Int) -> Boolean): Int +public inline fun IntArray.last(predicate: (Int) -> Boolean): Int { + for (index in this.indices.reversed()) { + val element = this[index] + if (predicate(element)) return element + } + throw NoSuchElementException("Array contains no element matching the predicate.") +} /** * Returns the last element matching the given [predicate]. * @throws [NoSuchElementException] if no such element is found. */ -public expect inline fun LongArray.last(predicate: (Long) -> Boolean): Long +public inline fun LongArray.last(predicate: (Long) -> Boolean): Long { + for (index in this.indices.reversed()) { + val element = this[index] + if (predicate(element)) return element + } + throw NoSuchElementException("Array contains no element matching the predicate.") +} /** * Returns the last element matching the given [predicate]. * @throws [NoSuchElementException] if no such element is found. */ -public expect inline fun FloatArray.last(predicate: (Float) -> Boolean): Float +public inline fun FloatArray.last(predicate: (Float) -> Boolean): Float { + for (index in this.indices.reversed()) { + val element = this[index] + if (predicate(element)) return element + } + throw NoSuchElementException("Array contains no element matching the predicate.") +} /** * Returns the last element matching the given [predicate]. * @throws [NoSuchElementException] if no such element is found. */ -public expect inline fun DoubleArray.last(predicate: (Double) -> Boolean): Double +public inline fun DoubleArray.last(predicate: (Double) -> Boolean): Double { + for (index in this.indices.reversed()) { + val element = this[index] + if (predicate(element)) return element + } + throw NoSuchElementException("Array contains no element matching the predicate.") +} /** * Returns the last element matching the given [predicate]. * @throws [NoSuchElementException] if no such element is found. */ -public expect inline fun BooleanArray.last(predicate: (Boolean) -> Boolean): Boolean +public inline fun BooleanArray.last(predicate: (Boolean) -> Boolean): Boolean { + for (index in this.indices.reversed()) { + val element = this[index] + if (predicate(element)) return element + } + throw NoSuchElementException("Array contains no element matching the predicate.") +} /** * Returns the last element matching the given [predicate]. * @throws [NoSuchElementException] if no such element is found. */ -public expect inline fun CharArray.last(predicate: (Char) -> Boolean): Char +public inline fun CharArray.last(predicate: (Char) -> Boolean): Char { + for (index in this.indices.reversed()) { + val element = this[index] + if (predicate(element)) return element + } + throw NoSuchElementException("Array contains no element matching the predicate.") +} /** * Returns last index of [element], or -1 if the array does not contain element. */ -public expect fun <@kotlin.internal.OnlyInputTypes T> Array.lastIndexOf(element: T): Int +public fun <@kotlin.internal.OnlyInputTypes T> Array.lastIndexOf(element: T): Int { + if (element == null) { + for (index in indices.reversed()) { + if (this[index] == null) { + return index + } + } + } else { + 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 expect fun ByteArray.lastIndexOf(element: Byte): Int +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 expect fun ShortArray.lastIndexOf(element: Short): Int +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 expect fun IntArray.lastIndexOf(element: Int): Int +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 expect fun LongArray.lastIndexOf(element: Long): Int +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 expect fun FloatArray.lastIndexOf(element: Float): Int +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 expect fun DoubleArray.lastIndexOf(element: Double): Int +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 expect fun BooleanArray.lastIndexOf(element: Boolean): Int +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 expect fun CharArray.lastIndexOf(element: Char): Int +public fun CharArray.lastIndexOf(element: Char): Int { + for (index in indices.reversed()) { + if (element == this[index]) { + return index + } + } + return -1 +} /** * Returns the last element, or `null` if the array is empty. */ -public expect fun Array.lastOrNull(): T? +public fun Array.lastOrNull(): T? { + return if (isEmpty()) null else this[size - 1] +} /** * Returns the last element, or `null` if the array is empty. */ -public expect fun ByteArray.lastOrNull(): Byte? +public fun ByteArray.lastOrNull(): Byte? { + return if (isEmpty()) null else this[size - 1] +} /** * Returns the last element, or `null` if the array is empty. */ -public expect fun ShortArray.lastOrNull(): Short? +public fun ShortArray.lastOrNull(): Short? { + return if (isEmpty()) null else this[size - 1] +} /** * Returns the last element, or `null` if the array is empty. */ -public expect fun IntArray.lastOrNull(): Int? +public fun IntArray.lastOrNull(): Int? { + return if (isEmpty()) null else this[size - 1] +} /** * Returns the last element, or `null` if the array is empty. */ -public expect fun LongArray.lastOrNull(): Long? +public fun LongArray.lastOrNull(): Long? { + return if (isEmpty()) null else this[size - 1] +} /** * Returns the last element, or `null` if the array is empty. */ -public expect fun FloatArray.lastOrNull(): Float? +public fun FloatArray.lastOrNull(): Float? { + return if (isEmpty()) null else this[size - 1] +} /** * Returns the last element, or `null` if the array is empty. */ -public expect fun DoubleArray.lastOrNull(): Double? +public fun DoubleArray.lastOrNull(): Double? { + return if (isEmpty()) null else this[size - 1] +} /** * Returns the last element, or `null` if the array is empty. */ -public expect fun BooleanArray.lastOrNull(): Boolean? +public fun BooleanArray.lastOrNull(): Boolean? { + return if (isEmpty()) null else this[size - 1] +} /** * Returns the last element, or `null` if the array is empty. */ -public expect fun CharArray.lastOrNull(): Char? +public fun CharArray.lastOrNull(): Char? { + return if (isEmpty()) null else this[size - 1] +} /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ -public expect inline fun Array.lastOrNull(predicate: (T) -> Boolean): T? +public inline fun Array.lastOrNull(predicate: (T) -> Boolean): T? { + for (index in this.indices.reversed()) { + val element = this[index] + if (predicate(element)) return element + } + return null +} /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ -public expect inline fun ByteArray.lastOrNull(predicate: (Byte) -> Boolean): Byte? +public inline fun ByteArray.lastOrNull(predicate: (Byte) -> Boolean): Byte? { + for (index in this.indices.reversed()) { + val element = this[index] + if (predicate(element)) return element + } + return null +} /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ -public expect inline fun ShortArray.lastOrNull(predicate: (Short) -> Boolean): Short? +public inline fun ShortArray.lastOrNull(predicate: (Short) -> Boolean): Short? { + for (index in this.indices.reversed()) { + val element = this[index] + if (predicate(element)) return element + } + return null +} /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ -public expect inline fun IntArray.lastOrNull(predicate: (Int) -> Boolean): Int? +public inline fun IntArray.lastOrNull(predicate: (Int) -> Boolean): Int? { + for (index in this.indices.reversed()) { + val element = this[index] + if (predicate(element)) return element + } + return null +} /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ -public expect inline fun LongArray.lastOrNull(predicate: (Long) -> Boolean): Long? +public inline fun LongArray.lastOrNull(predicate: (Long) -> Boolean): Long? { + for (index in this.indices.reversed()) { + val element = this[index] + if (predicate(element)) return element + } + return null +} /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ -public expect inline fun FloatArray.lastOrNull(predicate: (Float) -> Boolean): Float? +public inline fun FloatArray.lastOrNull(predicate: (Float) -> Boolean): Float? { + for (index in this.indices.reversed()) { + val element = this[index] + if (predicate(element)) return element + } + return null +} /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ -public expect inline fun DoubleArray.lastOrNull(predicate: (Double) -> Boolean): Double? +public inline fun DoubleArray.lastOrNull(predicate: (Double) -> Boolean): Double? { + for (index in this.indices.reversed()) { + val element = this[index] + if (predicate(element)) return element + } + return null +} /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ -public expect inline fun BooleanArray.lastOrNull(predicate: (Boolean) -> Boolean): Boolean? +public inline fun BooleanArray.lastOrNull(predicate: (Boolean) -> Boolean): Boolean? { + for (index in this.indices.reversed()) { + val element = this[index] + if (predicate(element)) return element + } + return null +} /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ -public expect inline fun CharArray.lastOrNull(predicate: (Char) -> Boolean): Char? +public inline fun CharArray.lastOrNull(predicate: (Char) -> Boolean): Char? { + for (index in this.indices.reversed()) { + val element = this[index] + if (predicate(element)) return element + } + return null +} /** * Returns the single element, or throws an exception if the array is empty or has more than one element. */ -public expect fun Array.single(): T +public fun Array.single(): T { + return when (size) { + 0 -> throw NoSuchElementException("Array is empty.") + 1 -> this[0] + else -> throw IllegalArgumentException("Array has more than one element.") + } +} /** * Returns the single element, or throws an exception if the array is empty or has more than one element. */ -public expect fun ByteArray.single(): Byte +public fun ByteArray.single(): Byte { + return when (size) { + 0 -> throw NoSuchElementException("Array is empty.") + 1 -> this[0] + else -> throw IllegalArgumentException("Array has more than one element.") + } +} /** * Returns the single element, or throws an exception if the array is empty or has more than one element. */ -public expect fun ShortArray.single(): Short +public fun ShortArray.single(): Short { + return when (size) { + 0 -> throw NoSuchElementException("Array is empty.") + 1 -> this[0] + else -> throw IllegalArgumentException("Array has more than one element.") + } +} /** * Returns the single element, or throws an exception if the array is empty or has more than one element. */ -public expect fun IntArray.single(): Int +public fun IntArray.single(): Int { + return when (size) { + 0 -> throw NoSuchElementException("Array is empty.") + 1 -> this[0] + else -> throw IllegalArgumentException("Array has more than one element.") + } +} /** * Returns the single element, or throws an exception if the array is empty or has more than one element. */ -public expect fun LongArray.single(): Long +public fun LongArray.single(): Long { + return when (size) { + 0 -> throw NoSuchElementException("Array is empty.") + 1 -> this[0] + else -> throw IllegalArgumentException("Array has more than one element.") + } +} /** * Returns the single element, or throws an exception if the array is empty or has more than one element. */ -public expect fun FloatArray.single(): Float +public fun FloatArray.single(): Float { + return when (size) { + 0 -> throw NoSuchElementException("Array is empty.") + 1 -> this[0] + else -> throw IllegalArgumentException("Array has more than one element.") + } +} /** * Returns the single element, or throws an exception if the array is empty or has more than one element. */ -public expect fun DoubleArray.single(): Double +public fun DoubleArray.single(): Double { + return when (size) { + 0 -> throw NoSuchElementException("Array is empty.") + 1 -> this[0] + else -> throw IllegalArgumentException("Array has more than one element.") + } +} /** * Returns the single element, or throws an exception if the array is empty or has more than one element. */ -public expect fun BooleanArray.single(): Boolean +public fun BooleanArray.single(): Boolean { + return when (size) { + 0 -> throw NoSuchElementException("Array is empty.") + 1 -> this[0] + else -> throw IllegalArgumentException("Array has more than one element.") + } +} /** * Returns the single element, or throws an exception if the array is empty or has more than one element. */ -public expect fun CharArray.single(): Char +public fun CharArray.single(): Char { + return when (size) { + 0 -> throw NoSuchElementException("Array is empty.") + 1 -> this[0] + else -> throw IllegalArgumentException("Array has more than one element.") + } +} /** * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. */ -public expect inline fun Array.single(predicate: (T) -> Boolean): T +public inline fun Array.single(predicate: (T) -> Boolean): T { + var single: T? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) throw IllegalArgumentException("Array contains more than one matching element.") + single = element + found = true + } + } + if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") + @Suppress("UNCHECKED_CAST") + return single as T +} /** * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. */ -public expect inline fun ByteArray.single(predicate: (Byte) -> Boolean): Byte +public inline fun ByteArray.single(predicate: (Byte) -> Boolean): Byte { + var single: Byte? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) throw IllegalArgumentException("Array contains more than one matching element.") + single = element + found = true + } + } + if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") + @Suppress("UNCHECKED_CAST") + return single as Byte +} /** * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. */ -public expect inline fun ShortArray.single(predicate: (Short) -> Boolean): Short +public inline fun ShortArray.single(predicate: (Short) -> Boolean): Short { + var single: Short? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) throw IllegalArgumentException("Array contains more than one matching element.") + single = element + found = true + } + } + if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") + @Suppress("UNCHECKED_CAST") + return single as Short +} /** * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. */ -public expect inline fun IntArray.single(predicate: (Int) -> Boolean): Int +public inline fun IntArray.single(predicate: (Int) -> Boolean): Int { + var single: Int? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) throw IllegalArgumentException("Array contains more than one matching element.") + single = element + found = true + } + } + if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") + @Suppress("UNCHECKED_CAST") + return single as Int +} /** * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. */ -public expect inline fun LongArray.single(predicate: (Long) -> Boolean): Long +public inline fun LongArray.single(predicate: (Long) -> Boolean): Long { + var single: Long? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) throw IllegalArgumentException("Array contains more than one matching element.") + single = element + found = true + } + } + if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") + @Suppress("UNCHECKED_CAST") + return single as Long +} /** * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. */ -public expect inline fun FloatArray.single(predicate: (Float) -> Boolean): Float +public inline fun FloatArray.single(predicate: (Float) -> Boolean): Float { + var single: Float? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) throw IllegalArgumentException("Array contains more than one matching element.") + single = element + found = true + } + } + if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") + @Suppress("UNCHECKED_CAST") + return single as Float +} /** * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. */ -public expect inline fun DoubleArray.single(predicate: (Double) -> Boolean): Double +public inline fun DoubleArray.single(predicate: (Double) -> Boolean): Double { + var single: Double? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) throw IllegalArgumentException("Array contains more than one matching element.") + single = element + found = true + } + } + if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") + @Suppress("UNCHECKED_CAST") + return single as Double +} /** * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. */ -public expect inline fun BooleanArray.single(predicate: (Boolean) -> Boolean): Boolean +public inline fun BooleanArray.single(predicate: (Boolean) -> Boolean): Boolean { + var single: Boolean? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) throw IllegalArgumentException("Array contains more than one matching element.") + single = element + found = true + } + } + if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") + @Suppress("UNCHECKED_CAST") + return single as Boolean +} /** * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. */ -public expect inline fun CharArray.single(predicate: (Char) -> Boolean): Char +public inline fun CharArray.single(predicate: (Char) -> Boolean): Char { + var single: Char? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) throw IllegalArgumentException("Array contains more than one matching element.") + single = element + found = true + } + } + if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") + @Suppress("UNCHECKED_CAST") + return single as Char +} /** * Returns single element, or `null` if the array is empty or has more than one element. */ -public expect fun Array.singleOrNull(): T? +public fun Array.singleOrNull(): T? { + return if (size == 1) this[0] else null +} /** * Returns single element, or `null` if the array is empty or has more than one element. */ -public expect fun ByteArray.singleOrNull(): Byte? +public fun ByteArray.singleOrNull(): Byte? { + return if (size == 1) this[0] else null +} /** * Returns single element, or `null` if the array is empty or has more than one element. */ -public expect fun ShortArray.singleOrNull(): Short? +public fun ShortArray.singleOrNull(): Short? { + return if (size == 1) this[0] else null +} /** * Returns single element, or `null` if the array is empty or has more than one element. */ -public expect fun IntArray.singleOrNull(): Int? +public fun IntArray.singleOrNull(): Int? { + return if (size == 1) this[0] else null +} /** * Returns single element, or `null` if the array is empty or has more than one element. */ -public expect fun LongArray.singleOrNull(): Long? +public fun LongArray.singleOrNull(): Long? { + return if (size == 1) this[0] else null +} /** * Returns single element, or `null` if the array is empty or has more than one element. */ -public expect fun FloatArray.singleOrNull(): Float? +public fun FloatArray.singleOrNull(): Float? { + return if (size == 1) this[0] else null +} /** * Returns single element, or `null` if the array is empty or has more than one element. */ -public expect fun DoubleArray.singleOrNull(): Double? +public fun DoubleArray.singleOrNull(): Double? { + return if (size == 1) this[0] else null +} /** * Returns single element, or `null` if the array is empty or has more than one element. */ -public expect fun BooleanArray.singleOrNull(): Boolean? +public fun BooleanArray.singleOrNull(): Boolean? { + return if (size == 1) this[0] else null +} /** * Returns single element, or `null` if the array is empty or has more than one element. */ -public expect fun CharArray.singleOrNull(): Char? +public fun CharArray.singleOrNull(): Char? { + return if (size == 1) this[0] else null +} /** * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. */ -public expect inline fun Array.singleOrNull(predicate: (T) -> Boolean): T? +public inline fun Array.singleOrNull(predicate: (T) -> Boolean): T? { + var single: T? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) return null + single = element + found = true + } + } + if (!found) return null + return single +} /** * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. */ -public expect inline fun ByteArray.singleOrNull(predicate: (Byte) -> Boolean): Byte? +public inline fun ByteArray.singleOrNull(predicate: (Byte) -> Boolean): Byte? { + var single: Byte? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) return null + single = element + found = true + } + } + if (!found) return null + return single +} /** * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. */ -public expect inline fun ShortArray.singleOrNull(predicate: (Short) -> Boolean): Short? +public inline fun ShortArray.singleOrNull(predicate: (Short) -> Boolean): Short? { + var single: Short? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) return null + single = element + found = true + } + } + if (!found) return null + return single +} /** * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. */ -public expect inline fun IntArray.singleOrNull(predicate: (Int) -> Boolean): Int? +public inline fun IntArray.singleOrNull(predicate: (Int) -> Boolean): Int? { + var single: Int? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) return null + single = element + found = true + } + } + if (!found) return null + return single +} /** * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. */ -public expect inline fun LongArray.singleOrNull(predicate: (Long) -> Boolean): Long? +public inline fun LongArray.singleOrNull(predicate: (Long) -> Boolean): Long? { + var single: Long? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) return null + single = element + found = true + } + } + if (!found) return null + return single +} /** * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. */ -public expect inline fun FloatArray.singleOrNull(predicate: (Float) -> Boolean): Float? +public inline fun FloatArray.singleOrNull(predicate: (Float) -> Boolean): Float? { + var single: Float? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) return null + single = element + found = true + } + } + if (!found) return null + return single +} /** * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. */ -public expect inline fun DoubleArray.singleOrNull(predicate: (Double) -> Boolean): Double? +public inline fun DoubleArray.singleOrNull(predicate: (Double) -> Boolean): Double? { + var single: Double? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) return null + single = element + found = true + } + } + if (!found) return null + return single +} /** * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. */ -public expect inline fun BooleanArray.singleOrNull(predicate: (Boolean) -> Boolean): Boolean? +public inline fun BooleanArray.singleOrNull(predicate: (Boolean) -> Boolean): Boolean? { + var single: Boolean? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) return null + single = element + found = true + } + } + if (!found) return null + return single +} /** * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. */ -public expect inline fun CharArray.singleOrNull(predicate: (Char) -> Boolean): Char? +public inline fun CharArray.singleOrNull(predicate: (Char) -> Boolean): Char? { + var single: Char? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) return null + single = element + found = true + } + } + if (!found) return null + return single +} /** * Returns a list containing all elements except first [n] elements. * * @sample samples.collections.Collections.Transformations.drop */ -public expect fun Array.drop(n: Int): List +public fun Array.drop(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + return takeLast((size - n).coerceAtLeast(0)) +} /** * Returns a list containing all elements except first [n] elements. * * @sample samples.collections.Collections.Transformations.drop */ -public expect fun ByteArray.drop(n: Int): List +public fun ByteArray.drop(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + return takeLast((size - n).coerceAtLeast(0)) +} /** * Returns a list containing all elements except first [n] elements. * * @sample samples.collections.Collections.Transformations.drop */ -public expect fun ShortArray.drop(n: Int): List +public fun ShortArray.drop(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + return takeLast((size - n).coerceAtLeast(0)) +} /** * Returns a list containing all elements except first [n] elements. * * @sample samples.collections.Collections.Transformations.drop */ -public expect fun IntArray.drop(n: Int): List +public fun IntArray.drop(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + return takeLast((size - n).coerceAtLeast(0)) +} /** * Returns a list containing all elements except first [n] elements. * * @sample samples.collections.Collections.Transformations.drop */ -public expect fun LongArray.drop(n: Int): List +public fun LongArray.drop(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + return takeLast((size - n).coerceAtLeast(0)) +} /** * Returns a list containing all elements except first [n] elements. * * @sample samples.collections.Collections.Transformations.drop */ -public expect fun FloatArray.drop(n: Int): List +public fun FloatArray.drop(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + return takeLast((size - n).coerceAtLeast(0)) +} /** * Returns a list containing all elements except first [n] elements. * * @sample samples.collections.Collections.Transformations.drop */ -public expect fun DoubleArray.drop(n: Int): List +public fun DoubleArray.drop(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + return takeLast((size - n).coerceAtLeast(0)) +} /** * Returns a list containing all elements except first [n] elements. * * @sample samples.collections.Collections.Transformations.drop */ -public expect fun BooleanArray.drop(n: Int): List +public fun BooleanArray.drop(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + return takeLast((size - n).coerceAtLeast(0)) +} /** * Returns a list containing all elements except first [n] elements. * * @sample samples.collections.Collections.Transformations.drop */ -public expect fun CharArray.drop(n: Int): List +public fun CharArray.drop(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + return takeLast((size - n).coerceAtLeast(0)) +} /** * Returns a list containing all elements except last [n] elements. * * @sample samples.collections.Collections.Transformations.drop */ -public expect fun Array.dropLast(n: Int): List +public fun Array.dropLast(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + return take((size - n).coerceAtLeast(0)) +} /** * Returns a list containing all elements except last [n] elements. * * @sample samples.collections.Collections.Transformations.drop */ -public expect fun ByteArray.dropLast(n: Int): List +public fun ByteArray.dropLast(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + return take((size - n).coerceAtLeast(0)) +} /** * Returns a list containing all elements except last [n] elements. * * @sample samples.collections.Collections.Transformations.drop */ -public expect fun ShortArray.dropLast(n: Int): List +public fun ShortArray.dropLast(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + return take((size - n).coerceAtLeast(0)) +} /** * Returns a list containing all elements except last [n] elements. * * @sample samples.collections.Collections.Transformations.drop */ -public expect fun IntArray.dropLast(n: Int): List +public fun IntArray.dropLast(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + return take((size - n).coerceAtLeast(0)) +} /** * Returns a list containing all elements except last [n] elements. * * @sample samples.collections.Collections.Transformations.drop */ -public expect fun LongArray.dropLast(n: Int): List +public fun LongArray.dropLast(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + return take((size - n).coerceAtLeast(0)) +} /** * Returns a list containing all elements except last [n] elements. * * @sample samples.collections.Collections.Transformations.drop */ -public expect fun FloatArray.dropLast(n: Int): List +public fun FloatArray.dropLast(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + return take((size - n).coerceAtLeast(0)) +} /** * Returns a list containing all elements except last [n] elements. * * @sample samples.collections.Collections.Transformations.drop */ -public expect fun DoubleArray.dropLast(n: Int): List +public fun DoubleArray.dropLast(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + return take((size - n).coerceAtLeast(0)) +} /** * Returns a list containing all elements except last [n] elements. * * @sample samples.collections.Collections.Transformations.drop */ -public expect fun BooleanArray.dropLast(n: Int): List +public fun BooleanArray.dropLast(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + return take((size - n).coerceAtLeast(0)) +} /** * Returns a list containing all elements except last [n] elements. * * @sample samples.collections.Collections.Transformations.drop */ -public expect fun CharArray.dropLast(n: Int): List +public fun CharArray.dropLast(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + return take((size - n).coerceAtLeast(0)) +} /** * Returns a list containing all elements except last elements that satisfy the given [predicate]. * * @sample samples.collections.Collections.Transformations.drop */ -public expect inline fun Array.dropLastWhile(predicate: (T) -> Boolean): List +public inline fun Array.dropLastWhile(predicate: (T) -> Boolean): List { + for (index in lastIndex downTo 0) { + if (!predicate(this[index])) { + return take(index + 1) + } + } + return emptyList() +} /** * Returns a list containing all elements except last elements that satisfy the given [predicate]. * * @sample samples.collections.Collections.Transformations.drop */ -public expect inline fun ByteArray.dropLastWhile(predicate: (Byte) -> Boolean): List +public inline fun ByteArray.dropLastWhile(predicate: (Byte) -> Boolean): List { + for (index in lastIndex downTo 0) { + if (!predicate(this[index])) { + return take(index + 1) + } + } + return emptyList() +} /** * Returns a list containing all elements except last elements that satisfy the given [predicate]. * * @sample samples.collections.Collections.Transformations.drop */ -public expect inline fun ShortArray.dropLastWhile(predicate: (Short) -> Boolean): List +public inline fun ShortArray.dropLastWhile(predicate: (Short) -> Boolean): List { + for (index in lastIndex downTo 0) { + if (!predicate(this[index])) { + return take(index + 1) + } + } + return emptyList() +} /** * Returns a list containing all elements except last elements that satisfy the given [predicate]. * * @sample samples.collections.Collections.Transformations.drop */ -public expect inline fun IntArray.dropLastWhile(predicate: (Int) -> Boolean): List +public inline fun IntArray.dropLastWhile(predicate: (Int) -> Boolean): List { + for (index in lastIndex downTo 0) { + if (!predicate(this[index])) { + return take(index + 1) + } + } + return emptyList() +} /** * Returns a list containing all elements except last elements that satisfy the given [predicate]. * * @sample samples.collections.Collections.Transformations.drop */ -public expect inline fun LongArray.dropLastWhile(predicate: (Long) -> Boolean): List +public inline fun LongArray.dropLastWhile(predicate: (Long) -> Boolean): List { + for (index in lastIndex downTo 0) { + if (!predicate(this[index])) { + return take(index + 1) + } + } + return emptyList() +} /** * Returns a list containing all elements except last elements that satisfy the given [predicate]. * * @sample samples.collections.Collections.Transformations.drop */ -public expect inline fun FloatArray.dropLastWhile(predicate: (Float) -> Boolean): List +public inline fun FloatArray.dropLastWhile(predicate: (Float) -> Boolean): List { + for (index in lastIndex downTo 0) { + if (!predicate(this[index])) { + return take(index + 1) + } + } + return emptyList() +} /** * Returns a list containing all elements except last elements that satisfy the given [predicate]. * * @sample samples.collections.Collections.Transformations.drop */ -public expect inline fun DoubleArray.dropLastWhile(predicate: (Double) -> Boolean): List +public inline fun DoubleArray.dropLastWhile(predicate: (Double) -> Boolean): List { + for (index in lastIndex downTo 0) { + if (!predicate(this[index])) { + return take(index + 1) + } + } + return emptyList() +} /** * Returns a list containing all elements except last elements that satisfy the given [predicate]. * * @sample samples.collections.Collections.Transformations.drop */ -public expect inline fun BooleanArray.dropLastWhile(predicate: (Boolean) -> Boolean): List +public inline fun BooleanArray.dropLastWhile(predicate: (Boolean) -> Boolean): List { + for (index in lastIndex downTo 0) { + if (!predicate(this[index])) { + return take(index + 1) + } + } + return emptyList() +} /** * Returns a list containing all elements except last elements that satisfy the given [predicate]. * * @sample samples.collections.Collections.Transformations.drop */ -public expect inline fun CharArray.dropLastWhile(predicate: (Char) -> Boolean): List +public inline fun CharArray.dropLastWhile(predicate: (Char) -> Boolean): List { + for (index in lastIndex downTo 0) { + if (!predicate(this[index])) { + return take(index + 1) + } + } + return emptyList() +} /** * Returns a list containing all elements except first elements that satisfy the given [predicate]. * * @sample samples.collections.Collections.Transformations.drop */ -public expect inline fun Array.dropWhile(predicate: (T) -> Boolean): List +public inline fun Array.dropWhile(predicate: (T) -> Boolean): List { + var yielding = false + val list = ArrayList() + for (item in this) + if (yielding) + list.add(item) + else if (!predicate(item)) { + list.add(item) + yielding = true + } + return list +} /** * Returns a list containing all elements except first elements that satisfy the given [predicate]. * * @sample samples.collections.Collections.Transformations.drop */ -public expect inline fun ByteArray.dropWhile(predicate: (Byte) -> Boolean): List +public inline fun ByteArray.dropWhile(predicate: (Byte) -> Boolean): List { + var yielding = false + val list = ArrayList() + for (item in this) + if (yielding) + list.add(item) + else if (!predicate(item)) { + list.add(item) + yielding = true + } + return list +} /** * Returns a list containing all elements except first elements that satisfy the given [predicate]. * * @sample samples.collections.Collections.Transformations.drop */ -public expect inline fun ShortArray.dropWhile(predicate: (Short) -> Boolean): List +public inline fun ShortArray.dropWhile(predicate: (Short) -> Boolean): List { + var yielding = false + val list = ArrayList() + for (item in this) + if (yielding) + list.add(item) + else if (!predicate(item)) { + list.add(item) + yielding = true + } + return list +} /** * Returns a list containing all elements except first elements that satisfy the given [predicate]. * * @sample samples.collections.Collections.Transformations.drop */ -public expect inline fun IntArray.dropWhile(predicate: (Int) -> Boolean): List +public inline fun IntArray.dropWhile(predicate: (Int) -> Boolean): List { + var yielding = false + val list = ArrayList() + for (item in this) + if (yielding) + list.add(item) + else if (!predicate(item)) { + list.add(item) + yielding = true + } + return list +} /** * Returns a list containing all elements except first elements that satisfy the given [predicate]. * * @sample samples.collections.Collections.Transformations.drop */ -public expect inline fun LongArray.dropWhile(predicate: (Long) -> Boolean): List +public inline fun LongArray.dropWhile(predicate: (Long) -> Boolean): List { + var yielding = false + val list = ArrayList() + for (item in this) + if (yielding) + list.add(item) + else if (!predicate(item)) { + list.add(item) + yielding = true + } + return list +} /** * Returns a list containing all elements except first elements that satisfy the given [predicate]. * * @sample samples.collections.Collections.Transformations.drop */ -public expect inline fun FloatArray.dropWhile(predicate: (Float) -> Boolean): List +public inline fun FloatArray.dropWhile(predicate: (Float) -> Boolean): List { + var yielding = false + val list = ArrayList() + for (item in this) + if (yielding) + list.add(item) + else if (!predicate(item)) { + list.add(item) + yielding = true + } + return list +} /** * Returns a list containing all elements except first elements that satisfy the given [predicate]. * * @sample samples.collections.Collections.Transformations.drop */ -public expect inline fun DoubleArray.dropWhile(predicate: (Double) -> Boolean): List +public inline fun DoubleArray.dropWhile(predicate: (Double) -> Boolean): List { + var yielding = false + val list = ArrayList() + for (item in this) + if (yielding) + list.add(item) + else if (!predicate(item)) { + list.add(item) + yielding = true + } + return list +} /** * Returns a list containing all elements except first elements that satisfy the given [predicate]. * * @sample samples.collections.Collections.Transformations.drop */ -public expect inline fun BooleanArray.dropWhile(predicate: (Boolean) -> Boolean): List +public inline fun BooleanArray.dropWhile(predicate: (Boolean) -> Boolean): List { + var yielding = false + val list = ArrayList() + for (item in this) + if (yielding) + list.add(item) + else if (!predicate(item)) { + list.add(item) + yielding = true + } + return list +} /** * Returns a list containing all elements except first elements that satisfy the given [predicate]. * * @sample samples.collections.Collections.Transformations.drop */ -public expect inline fun CharArray.dropWhile(predicate: (Char) -> Boolean): List +public inline fun CharArray.dropWhile(predicate: (Char) -> Boolean): List { + var yielding = false + val list = ArrayList() + for (item in this) + if (yielding) + list.add(item) + else if (!predicate(item)) { + list.add(item) + yielding = true + } + return list +} /** * Returns a list containing only elements matching the given [predicate]. */ -public expect inline fun Array.filter(predicate: (T) -> Boolean): List +public inline fun Array.filter(predicate: (T) -> Boolean): List { + return filterTo(ArrayList(), predicate) +} /** * Returns a list containing only elements matching the given [predicate]. */ -public expect inline fun ByteArray.filter(predicate: (Byte) -> Boolean): List +public inline fun ByteArray.filter(predicate: (Byte) -> Boolean): List { + return filterTo(ArrayList(), predicate) +} /** * Returns a list containing only elements matching the given [predicate]. */ -public expect inline fun ShortArray.filter(predicate: (Short) -> Boolean): List +public inline fun ShortArray.filter(predicate: (Short) -> Boolean): List { + return filterTo(ArrayList(), predicate) +} /** * Returns a list containing only elements matching the given [predicate]. */ -public expect inline fun IntArray.filter(predicate: (Int) -> Boolean): List +public inline fun IntArray.filter(predicate: (Int) -> Boolean): List { + return filterTo(ArrayList(), predicate) +} /** * Returns a list containing only elements matching the given [predicate]. */ -public expect inline fun LongArray.filter(predicate: (Long) -> Boolean): List +public inline fun LongArray.filter(predicate: (Long) -> Boolean): List { + return filterTo(ArrayList(), predicate) +} /** * Returns a list containing only elements matching the given [predicate]. */ -public expect inline fun FloatArray.filter(predicate: (Float) -> Boolean): List +public inline fun FloatArray.filter(predicate: (Float) -> Boolean): List { + return filterTo(ArrayList(), predicate) +} /** * Returns a list containing only elements matching the given [predicate]. */ -public expect inline fun DoubleArray.filter(predicate: (Double) -> Boolean): List +public inline fun DoubleArray.filter(predicate: (Double) -> Boolean): List { + return filterTo(ArrayList(), predicate) +} /** * Returns a list containing only elements matching the given [predicate]. */ -public expect inline fun BooleanArray.filter(predicate: (Boolean) -> Boolean): List +public inline fun BooleanArray.filter(predicate: (Boolean) -> Boolean): List { + return filterTo(ArrayList(), predicate) +} /** * Returns a list containing only elements matching the given [predicate]. */ -public expect inline fun CharArray.filter(predicate: (Char) -> Boolean): List +public inline fun CharArray.filter(predicate: (Char) -> Boolean): List { + return filterTo(ArrayList(), predicate) +} /** * Returns a list containing only elements matching the given [predicate]. * @param [predicate] function that takes the index of an element and the element itself * and returns the result of predicate evaluation on the element. */ -public expect inline fun Array.filterIndexed(predicate: (index: Int, T) -> Boolean): List +public inline fun Array.filterIndexed(predicate: (index: Int, T) -> Boolean): List { + return filterIndexedTo(ArrayList(), predicate) +} /** * Returns a list containing only elements matching the given [predicate]. * @param [predicate] function that takes the index of an element and the element itself * and returns the result of predicate evaluation on the element. */ -public expect inline fun ByteArray.filterIndexed(predicate: (index: Int, Byte) -> Boolean): List +public inline fun ByteArray.filterIndexed(predicate: (index: Int, Byte) -> Boolean): List { + return filterIndexedTo(ArrayList(), predicate) +} /** * Returns a list containing only elements matching the given [predicate]. * @param [predicate] function that takes the index of an element and the element itself * and returns the result of predicate evaluation on the element. */ -public expect inline fun ShortArray.filterIndexed(predicate: (index: Int, Short) -> Boolean): List +public inline fun ShortArray.filterIndexed(predicate: (index: Int, Short) -> Boolean): List { + return filterIndexedTo(ArrayList(), predicate) +} /** * Returns a list containing only elements matching the given [predicate]. * @param [predicate] function that takes the index of an element and the element itself * and returns the result of predicate evaluation on the element. */ -public expect inline fun IntArray.filterIndexed(predicate: (index: Int, Int) -> Boolean): List +public inline fun IntArray.filterIndexed(predicate: (index: Int, Int) -> Boolean): List { + return filterIndexedTo(ArrayList(), predicate) +} /** * Returns a list containing only elements matching the given [predicate]. * @param [predicate] function that takes the index of an element and the element itself * and returns the result of predicate evaluation on the element. */ -public expect inline fun LongArray.filterIndexed(predicate: (index: Int, Long) -> Boolean): List +public inline fun LongArray.filterIndexed(predicate: (index: Int, Long) -> Boolean): List { + return filterIndexedTo(ArrayList(), predicate) +} /** * Returns a list containing only elements matching the given [predicate]. * @param [predicate] function that takes the index of an element and the element itself * and returns the result of predicate evaluation on the element. */ -public expect inline fun FloatArray.filterIndexed(predicate: (index: Int, Float) -> Boolean): List +public inline fun FloatArray.filterIndexed(predicate: (index: Int, Float) -> Boolean): List { + return filterIndexedTo(ArrayList(), predicate) +} /** * Returns a list containing only elements matching the given [predicate]. * @param [predicate] function that takes the index of an element and the element itself * and returns the result of predicate evaluation on the element. */ -public expect inline fun DoubleArray.filterIndexed(predicate: (index: Int, Double) -> Boolean): List +public inline fun DoubleArray.filterIndexed(predicate: (index: Int, Double) -> Boolean): List { + return filterIndexedTo(ArrayList(), predicate) +} /** * Returns a list containing only elements matching the given [predicate]. * @param [predicate] function that takes the index of an element and the element itself * and returns the result of predicate evaluation on the element. */ -public expect inline fun BooleanArray.filterIndexed(predicate: (index: Int, Boolean) -> Boolean): List +public inline fun BooleanArray.filterIndexed(predicate: (index: Int, Boolean) -> Boolean): List { + return filterIndexedTo(ArrayList(), predicate) +} /** * Returns a list containing only elements matching the given [predicate]. * @param [predicate] function that takes the index of an element and the element itself * and returns the result of predicate evaluation on the element. */ -public expect inline fun CharArray.filterIndexed(predicate: (index: Int, Char) -> Boolean): List +public inline fun CharArray.filterIndexed(predicate: (index: Int, Char) -> Boolean): List { + return filterIndexedTo(ArrayList(), predicate) +} /** * Appends all elements matching the given [predicate] to the given [destination]. * @param [predicate] function that takes the index of an element and the element itself * and returns the result of predicate evaluation on the element. */ -public expect inline fun > Array.filterIndexedTo(destination: C, predicate: (index: Int, T) -> Boolean): C +public inline fun > Array.filterIndexedTo(destination: C, predicate: (index: Int, T) -> Boolean): C { + forEachIndexed { index, element -> + if (predicate(index, element)) destination.add(element) + } + return destination +} /** * Appends all elements matching the given [predicate] to the given [destination]. * @param [predicate] function that takes the index of an element and the element itself * and returns the result of predicate evaluation on the element. */ -public expect inline fun > ByteArray.filterIndexedTo(destination: C, predicate: (index: Int, Byte) -> Boolean): C +public inline fun > ByteArray.filterIndexedTo(destination: C, predicate: (index: Int, Byte) -> Boolean): C { + forEachIndexed { index, element -> + if (predicate(index, element)) destination.add(element) + } + return destination +} /** * Appends all elements matching the given [predicate] to the given [destination]. * @param [predicate] function that takes the index of an element and the element itself * and returns the result of predicate evaluation on the element. */ -public expect inline fun > ShortArray.filterIndexedTo(destination: C, predicate: (index: Int, Short) -> Boolean): C +public inline fun > ShortArray.filterIndexedTo(destination: C, predicate: (index: Int, Short) -> Boolean): C { + forEachIndexed { index, element -> + if (predicate(index, element)) destination.add(element) + } + return destination +} /** * Appends all elements matching the given [predicate] to the given [destination]. * @param [predicate] function that takes the index of an element and the element itself * and returns the result of predicate evaluation on the element. */ -public expect inline fun > IntArray.filterIndexedTo(destination: C, predicate: (index: Int, Int) -> Boolean): C +public inline fun > IntArray.filterIndexedTo(destination: C, predicate: (index: Int, Int) -> Boolean): C { + forEachIndexed { index, element -> + if (predicate(index, element)) destination.add(element) + } + return destination +} /** * Appends all elements matching the given [predicate] to the given [destination]. * @param [predicate] function that takes the index of an element and the element itself * and returns the result of predicate evaluation on the element. */ -public expect inline fun > LongArray.filterIndexedTo(destination: C, predicate: (index: Int, Long) -> Boolean): C +public inline fun > LongArray.filterIndexedTo(destination: C, predicate: (index: Int, Long) -> Boolean): C { + forEachIndexed { index, element -> + if (predicate(index, element)) destination.add(element) + } + return destination +} /** * Appends all elements matching the given [predicate] to the given [destination]. * @param [predicate] function that takes the index of an element and the element itself * and returns the result of predicate evaluation on the element. */ -public expect inline fun > FloatArray.filterIndexedTo(destination: C, predicate: (index: Int, Float) -> Boolean): C +public inline fun > FloatArray.filterIndexedTo(destination: C, predicate: (index: Int, Float) -> Boolean): C { + forEachIndexed { index, element -> + if (predicate(index, element)) destination.add(element) + } + return destination +} /** * Appends all elements matching the given [predicate] to the given [destination]. * @param [predicate] function that takes the index of an element and the element itself * and returns the result of predicate evaluation on the element. */ -public expect inline fun > DoubleArray.filterIndexedTo(destination: C, predicate: (index: Int, Double) -> Boolean): C +public inline fun > DoubleArray.filterIndexedTo(destination: C, predicate: (index: Int, Double) -> Boolean): C { + forEachIndexed { index, element -> + if (predicate(index, element)) destination.add(element) + } + return destination +} /** * Appends all elements matching the given [predicate] to the given [destination]. * @param [predicate] function that takes the index of an element and the element itself * and returns the result of predicate evaluation on the element. */ -public expect inline fun > BooleanArray.filterIndexedTo(destination: C, predicate: (index: Int, Boolean) -> Boolean): C +public inline fun > BooleanArray.filterIndexedTo(destination: C, predicate: (index: Int, Boolean) -> Boolean): C { + forEachIndexed { index, element -> + if (predicate(index, element)) destination.add(element) + } + return destination +} /** * Appends all elements matching the given [predicate] to the given [destination]. * @param [predicate] function that takes the index of an element and the element itself * and returns the result of predicate evaluation on the element. */ -public expect inline fun > CharArray.filterIndexedTo(destination: C, predicate: (index: Int, Char) -> Boolean): C +public inline fun > CharArray.filterIndexedTo(destination: C, predicate: (index: Int, Char) -> Boolean): C { + forEachIndexed { index, element -> + if (predicate(index, element)) destination.add(element) + } + return destination +} /** * Returns a list containing all elements that are instances of specified type parameter R. */ -public expect inline fun Array<*>.filterIsInstance(): List<@kotlin.internal.NoInfer R> +public inline fun Array<*>.filterIsInstance(): List<@kotlin.internal.NoInfer R> { + return filterIsInstanceTo(ArrayList()) +} /** * Appends all elements that are instances of specified type parameter R to the given [destination]. */ -public expect inline fun > Array<*>.filterIsInstanceTo(destination: C): C +public inline fun > Array<*>.filterIsInstanceTo(destination: C): C { + for (element in this) if (element is R) destination.add(element) + return destination +} /** * Returns a list containing all elements not matching the given [predicate]. */ -public expect inline fun Array.filterNot(predicate: (T) -> Boolean): List +public inline fun Array.filterNot(predicate: (T) -> Boolean): List { + return filterNotTo(ArrayList(), predicate) +} /** * Returns a list containing all elements not matching the given [predicate]. */ -public expect inline fun ByteArray.filterNot(predicate: (Byte) -> Boolean): List +public inline fun ByteArray.filterNot(predicate: (Byte) -> Boolean): List { + return filterNotTo(ArrayList(), predicate) +} /** * Returns a list containing all elements not matching the given [predicate]. */ -public expect inline fun ShortArray.filterNot(predicate: (Short) -> Boolean): List +public inline fun ShortArray.filterNot(predicate: (Short) -> Boolean): List { + return filterNotTo(ArrayList(), predicate) +} /** * Returns a list containing all elements not matching the given [predicate]. */ -public expect inline fun IntArray.filterNot(predicate: (Int) -> Boolean): List +public inline fun IntArray.filterNot(predicate: (Int) -> Boolean): List { + return filterNotTo(ArrayList(), predicate) +} /** * Returns a list containing all elements not matching the given [predicate]. */ -public expect inline fun LongArray.filterNot(predicate: (Long) -> Boolean): List +public inline fun LongArray.filterNot(predicate: (Long) -> Boolean): List { + return filterNotTo(ArrayList(), predicate) +} /** * Returns a list containing all elements not matching the given [predicate]. */ -public expect inline fun FloatArray.filterNot(predicate: (Float) -> Boolean): List +public inline fun FloatArray.filterNot(predicate: (Float) -> Boolean): List { + return filterNotTo(ArrayList(), predicate) +} /** * Returns a list containing all elements not matching the given [predicate]. */ -public expect inline fun DoubleArray.filterNot(predicate: (Double) -> Boolean): List +public inline fun DoubleArray.filterNot(predicate: (Double) -> Boolean): List { + return filterNotTo(ArrayList(), predicate) +} /** * Returns a list containing all elements not matching the given [predicate]. */ -public expect inline fun BooleanArray.filterNot(predicate: (Boolean) -> Boolean): List +public inline fun BooleanArray.filterNot(predicate: (Boolean) -> Boolean): List { + return filterNotTo(ArrayList(), predicate) +} /** * Returns a list containing all elements not matching the given [predicate]. */ -public expect inline fun CharArray.filterNot(predicate: (Char) -> Boolean): List +public inline fun CharArray.filterNot(predicate: (Char) -> Boolean): List { + return filterNotTo(ArrayList(), predicate) +} /** * Returns a list containing all elements that are not `null`. */ -public expect fun Array.filterNotNull(): List +public fun Array.filterNotNull(): List { + return filterNotNullTo(ArrayList()) +} /** * Appends all elements that are not `null` to the given [destination]. */ -public expect fun , T : Any> Array.filterNotNullTo(destination: C): C +public fun , T : Any> Array.filterNotNullTo(destination: C): C { + for (element in this) if (element != null) destination.add(element) + return destination +} /** * Appends all elements not matching the given [predicate] to the given [destination]. */ -public expect inline fun > Array.filterNotTo(destination: C, predicate: (T) -> Boolean): C +public inline fun > Array.filterNotTo(destination: C, predicate: (T) -> Boolean): C { + for (element in this) if (!predicate(element)) destination.add(element) + return destination +} /** * Appends all elements not matching the given [predicate] to the given [destination]. */ -public expect inline fun > ByteArray.filterNotTo(destination: C, predicate: (Byte) -> Boolean): C +public inline fun > ByteArray.filterNotTo(destination: C, predicate: (Byte) -> Boolean): C { + for (element in this) if (!predicate(element)) destination.add(element) + return destination +} /** * Appends all elements not matching the given [predicate] to the given [destination]. */ -public expect inline fun > ShortArray.filterNotTo(destination: C, predicate: (Short) -> Boolean): C +public inline fun > ShortArray.filterNotTo(destination: C, predicate: (Short) -> Boolean): C { + for (element in this) if (!predicate(element)) destination.add(element) + return destination +} /** * Appends all elements not matching the given [predicate] to the given [destination]. */ -public expect inline fun > IntArray.filterNotTo(destination: C, predicate: (Int) -> Boolean): C +public inline fun > IntArray.filterNotTo(destination: C, predicate: (Int) -> Boolean): C { + for (element in this) if (!predicate(element)) destination.add(element) + return destination +} /** * Appends all elements not matching the given [predicate] to the given [destination]. */ -public expect inline fun > LongArray.filterNotTo(destination: C, predicate: (Long) -> Boolean): C +public inline fun > LongArray.filterNotTo(destination: C, predicate: (Long) -> Boolean): C { + for (element in this) if (!predicate(element)) destination.add(element) + return destination +} /** * Appends all elements not matching the given [predicate] to the given [destination]. */ -public expect inline fun > FloatArray.filterNotTo(destination: C, predicate: (Float) -> Boolean): C +public inline fun > FloatArray.filterNotTo(destination: C, predicate: (Float) -> Boolean): C { + for (element in this) if (!predicate(element)) destination.add(element) + return destination +} /** * Appends all elements not matching the given [predicate] to the given [destination]. */ -public expect inline fun > DoubleArray.filterNotTo(destination: C, predicate: (Double) -> Boolean): C +public inline fun > DoubleArray.filterNotTo(destination: C, predicate: (Double) -> Boolean): C { + for (element in this) if (!predicate(element)) destination.add(element) + return destination +} /** * Appends all elements not matching the given [predicate] to the given [destination]. */ -public expect inline fun > BooleanArray.filterNotTo(destination: C, predicate: (Boolean) -> Boolean): C +public inline fun > BooleanArray.filterNotTo(destination: C, predicate: (Boolean) -> Boolean): C { + for (element in this) if (!predicate(element)) destination.add(element) + return destination +} /** * Appends all elements not matching the given [predicate] to the given [destination]. */ -public expect inline fun > CharArray.filterNotTo(destination: C, predicate: (Char) -> Boolean): C +public inline fun > CharArray.filterNotTo(destination: C, predicate: (Char) -> Boolean): C { + for (element in this) if (!predicate(element)) destination.add(element) + return destination +} /** * Appends all elements matching the given [predicate] to the given [destination]. */ -public expect inline fun > Array.filterTo(destination: C, predicate: (T) -> Boolean): C +public inline fun > Array.filterTo(destination: C, predicate: (T) -> Boolean): C { + for (element in this) if (predicate(element)) destination.add(element) + return destination +} /** * Appends all elements matching the given [predicate] to the given [destination]. */ -public expect inline fun > ByteArray.filterTo(destination: C, predicate: (Byte) -> Boolean): C +public inline fun > ByteArray.filterTo(destination: C, predicate: (Byte) -> Boolean): C { + for (element in this) if (predicate(element)) destination.add(element) + return destination +} /** * Appends all elements matching the given [predicate] to the given [destination]. */ -public expect inline fun > ShortArray.filterTo(destination: C, predicate: (Short) -> Boolean): C +public inline fun > ShortArray.filterTo(destination: C, predicate: (Short) -> Boolean): C { + for (element in this) if (predicate(element)) destination.add(element) + return destination +} /** * Appends all elements matching the given [predicate] to the given [destination]. */ -public expect inline fun > IntArray.filterTo(destination: C, predicate: (Int) -> Boolean): C +public inline fun > IntArray.filterTo(destination: C, predicate: (Int) -> Boolean): C { + for (element in this) if (predicate(element)) destination.add(element) + return destination +} /** * Appends all elements matching the given [predicate] to the given [destination]. */ -public expect inline fun > LongArray.filterTo(destination: C, predicate: (Long) -> Boolean): C +public inline fun > LongArray.filterTo(destination: C, predicate: (Long) -> Boolean): C { + for (element in this) if (predicate(element)) destination.add(element) + return destination +} /** * Appends all elements matching the given [predicate] to the given [destination]. */ -public expect inline fun > FloatArray.filterTo(destination: C, predicate: (Float) -> Boolean): C +public inline fun > FloatArray.filterTo(destination: C, predicate: (Float) -> Boolean): C { + for (element in this) if (predicate(element)) destination.add(element) + return destination +} /** * Appends all elements matching the given [predicate] to the given [destination]. */ -public expect inline fun > DoubleArray.filterTo(destination: C, predicate: (Double) -> Boolean): C +public inline fun > DoubleArray.filterTo(destination: C, predicate: (Double) -> Boolean): C { + for (element in this) if (predicate(element)) destination.add(element) + return destination +} /** * Appends all elements matching the given [predicate] to the given [destination]. */ -public expect inline fun > BooleanArray.filterTo(destination: C, predicate: (Boolean) -> Boolean): C +public inline fun > BooleanArray.filterTo(destination: C, predicate: (Boolean) -> Boolean): C { + for (element in this) if (predicate(element)) destination.add(element) + return destination +} /** * Appends all elements matching the given [predicate] to the given [destination]. */ -public expect inline fun > CharArray.filterTo(destination: C, predicate: (Char) -> Boolean): C +public inline fun > CharArray.filterTo(destination: C, predicate: (Char) -> Boolean): C { + for (element in this) if (predicate(element)) destination.add(element) + return destination +} /** * Returns a list containing elements at indices in the specified [indices] range. */ -public expect fun Array.slice(indices: IntRange): List +public fun Array.slice(indices: IntRange): List { + if (indices.isEmpty()) return listOf() + return copyOfRange(indices.start, indices.endInclusive + 1).asList() +} /** * Returns a list containing elements at indices in the specified [indices] range. */ -public expect fun ByteArray.slice(indices: IntRange): List +public fun ByteArray.slice(indices: IntRange): List { + if (indices.isEmpty()) return listOf() + return copyOfRange(indices.start, indices.endInclusive + 1).asList() +} /** * Returns a list containing elements at indices in the specified [indices] range. */ -public expect fun ShortArray.slice(indices: IntRange): List +public fun ShortArray.slice(indices: IntRange): List { + if (indices.isEmpty()) return listOf() + return copyOfRange(indices.start, indices.endInclusive + 1).asList() +} /** * Returns a list containing elements at indices in the specified [indices] range. */ -public expect fun IntArray.slice(indices: IntRange): List +public fun IntArray.slice(indices: IntRange): List { + if (indices.isEmpty()) return listOf() + return copyOfRange(indices.start, indices.endInclusive + 1).asList() +} /** * Returns a list containing elements at indices in the specified [indices] range. */ -public expect fun LongArray.slice(indices: IntRange): List +public fun LongArray.slice(indices: IntRange): List { + if (indices.isEmpty()) return listOf() + return copyOfRange(indices.start, indices.endInclusive + 1).asList() +} /** * Returns a list containing elements at indices in the specified [indices] range. */ -public expect fun FloatArray.slice(indices: IntRange): List +public fun FloatArray.slice(indices: IntRange): List { + if (indices.isEmpty()) return listOf() + return copyOfRange(indices.start, indices.endInclusive + 1).asList() +} /** * Returns a list containing elements at indices in the specified [indices] range. */ -public expect fun DoubleArray.slice(indices: IntRange): List +public fun DoubleArray.slice(indices: IntRange): List { + if (indices.isEmpty()) return listOf() + return copyOfRange(indices.start, indices.endInclusive + 1).asList() +} /** * Returns a list containing elements at indices in the specified [indices] range. */ -public expect fun BooleanArray.slice(indices: IntRange): List +public fun BooleanArray.slice(indices: IntRange): List { + if (indices.isEmpty()) return listOf() + return copyOfRange(indices.start, indices.endInclusive + 1).asList() +} /** * Returns a list containing elements at indices in the specified [indices] range. */ -public expect fun CharArray.slice(indices: IntRange): List +public fun CharArray.slice(indices: IntRange): List { + if (indices.isEmpty()) return listOf() + return copyOfRange(indices.start, indices.endInclusive + 1).asList() +} /** * Returns a list containing elements at specified [indices]. */ -public expect fun Array.slice(indices: Iterable): List +public fun Array.slice(indices: Iterable): List { + val size = indices.collectionSizeOrDefault(10) + if (size == 0) return emptyList() + val list = ArrayList(size) + for (index in indices) { + list.add(get(index)) + } + return list +} /** * Returns a list containing elements at specified [indices]. */ -public expect fun ByteArray.slice(indices: Iterable): List +public fun ByteArray.slice(indices: Iterable): List { + val size = indices.collectionSizeOrDefault(10) + if (size == 0) return emptyList() + val list = ArrayList(size) + for (index in indices) { + list.add(get(index)) + } + return list +} /** * Returns a list containing elements at specified [indices]. */ -public expect fun ShortArray.slice(indices: Iterable): List +public fun ShortArray.slice(indices: Iterable): List { + val size = indices.collectionSizeOrDefault(10) + if (size == 0) return emptyList() + val list = ArrayList(size) + for (index in indices) { + list.add(get(index)) + } + return list +} /** * Returns a list containing elements at specified [indices]. */ -public expect fun IntArray.slice(indices: Iterable): List +public fun IntArray.slice(indices: Iterable): List { + val size = indices.collectionSizeOrDefault(10) + if (size == 0) return emptyList() + val list = ArrayList(size) + for (index in indices) { + list.add(get(index)) + } + return list +} /** * Returns a list containing elements at specified [indices]. */ -public expect fun LongArray.slice(indices: Iterable): List +public fun LongArray.slice(indices: Iterable): List { + val size = indices.collectionSizeOrDefault(10) + if (size == 0) return emptyList() + val list = ArrayList(size) + for (index in indices) { + list.add(get(index)) + } + return list +} /** * Returns a list containing elements at specified [indices]. */ -public expect fun FloatArray.slice(indices: Iterable): List +public fun FloatArray.slice(indices: Iterable): List { + val size = indices.collectionSizeOrDefault(10) + if (size == 0) return emptyList() + val list = ArrayList(size) + for (index in indices) { + list.add(get(index)) + } + return list +} /** * Returns a list containing elements at specified [indices]. */ -public expect fun DoubleArray.slice(indices: Iterable): List +public fun DoubleArray.slice(indices: Iterable): List { + val size = indices.collectionSizeOrDefault(10) + if (size == 0) return emptyList() + val list = ArrayList(size) + for (index in indices) { + list.add(get(index)) + } + return list +} /** * Returns a list containing elements at specified [indices]. */ -public expect fun BooleanArray.slice(indices: Iterable): List +public fun BooleanArray.slice(indices: Iterable): List { + val size = indices.collectionSizeOrDefault(10) + if (size == 0) return emptyList() + val list = ArrayList(size) + for (index in indices) { + list.add(get(index)) + } + return list +} /** * Returns a list containing elements at specified [indices]. */ -public expect fun CharArray.slice(indices: Iterable): List +public fun CharArray.slice(indices: Iterable): List { + val size = indices.collectionSizeOrDefault(10) + if (size == 0) return emptyList() + val list = ArrayList(size) + for (index in indices) { + list.add(get(index)) + } + return list +} /** * Returns an array containing elements of this array at specified [indices]. */ -public expect fun Array.sliceArray(indices: Collection): Array +public fun Array.sliceArray(indices: Collection): Array { + val result = arrayOfNulls(this, indices.size) + var targetIndex = 0 + for (sourceIndex in indices) { + result[targetIndex++] = this[sourceIndex] + } + return result +} /** * Returns an array containing elements of this array at specified [indices]. */ -public expect fun ByteArray.sliceArray(indices: Collection): ByteArray +public fun ByteArray.sliceArray(indices: Collection): ByteArray { + val result = ByteArray(indices.size) + var targetIndex = 0 + for (sourceIndex in indices) { + result[targetIndex++] = this[sourceIndex] + } + return result +} /** * Returns an array containing elements of this array at specified [indices]. */ -public expect fun ShortArray.sliceArray(indices: Collection): ShortArray +public fun ShortArray.sliceArray(indices: Collection): ShortArray { + val result = ShortArray(indices.size) + var targetIndex = 0 + for (sourceIndex in indices) { + result[targetIndex++] = this[sourceIndex] + } + return result +} /** * Returns an array containing elements of this array at specified [indices]. */ -public expect fun IntArray.sliceArray(indices: Collection): IntArray +public fun IntArray.sliceArray(indices: Collection): IntArray { + val result = IntArray(indices.size) + var targetIndex = 0 + for (sourceIndex in indices) { + result[targetIndex++] = this[sourceIndex] + } + return result +} /** * Returns an array containing elements of this array at specified [indices]. */ -public expect fun LongArray.sliceArray(indices: Collection): LongArray +public fun LongArray.sliceArray(indices: Collection): LongArray { + val result = LongArray(indices.size) + var targetIndex = 0 + for (sourceIndex in indices) { + result[targetIndex++] = this[sourceIndex] + } + return result +} /** * Returns an array containing elements of this array at specified [indices]. */ -public expect fun FloatArray.sliceArray(indices: Collection): FloatArray +public fun FloatArray.sliceArray(indices: Collection): FloatArray { + val result = FloatArray(indices.size) + var targetIndex = 0 + for (sourceIndex in indices) { + result[targetIndex++] = this[sourceIndex] + } + return result +} /** * Returns an array containing elements of this array at specified [indices]. */ -public expect fun DoubleArray.sliceArray(indices: Collection): DoubleArray +public fun DoubleArray.sliceArray(indices: Collection): DoubleArray { + val result = DoubleArray(indices.size) + var targetIndex = 0 + for (sourceIndex in indices) { + result[targetIndex++] = this[sourceIndex] + } + return result +} /** * Returns an array containing elements of this array at specified [indices]. */ -public expect fun BooleanArray.sliceArray(indices: Collection): BooleanArray +public fun BooleanArray.sliceArray(indices: Collection): BooleanArray { + val result = BooleanArray(indices.size) + var targetIndex = 0 + for (sourceIndex in indices) { + result[targetIndex++] = this[sourceIndex] + } + return result +} /** * Returns an array containing elements of this array at specified [indices]. */ -public expect fun CharArray.sliceArray(indices: Collection): CharArray +public fun CharArray.sliceArray(indices: Collection): CharArray { + val result = CharArray(indices.size) + var targetIndex = 0 + for (sourceIndex in indices) { + result[targetIndex++] = this[sourceIndex] + } + return result +} /** * Returns a list containing elements at indices in the specified [indices] range. */ -public expect fun Array.sliceArray(indices: IntRange): Array +public fun Array.sliceArray(indices: IntRange): Array { + if (indices.isEmpty()) return copyOfRange(0, 0) + return copyOfRange(indices.start, indices.endInclusive + 1) +} /** * Returns a list containing elements at indices in the specified [indices] range. */ -public expect fun ByteArray.sliceArray(indices: IntRange): ByteArray +public fun ByteArray.sliceArray(indices: IntRange): ByteArray { + if (indices.isEmpty()) return ByteArray(0) + return copyOfRange(indices.start, indices.endInclusive + 1) +} /** * Returns a list containing elements at indices in the specified [indices] range. */ -public expect fun ShortArray.sliceArray(indices: IntRange): ShortArray +public fun ShortArray.sliceArray(indices: IntRange): ShortArray { + if (indices.isEmpty()) return ShortArray(0) + return copyOfRange(indices.start, indices.endInclusive + 1) +} /** * Returns a list containing elements at indices in the specified [indices] range. */ -public expect fun IntArray.sliceArray(indices: IntRange): IntArray +public fun IntArray.sliceArray(indices: IntRange): IntArray { + if (indices.isEmpty()) return IntArray(0) + return copyOfRange(indices.start, indices.endInclusive + 1) +} /** * Returns a list containing elements at indices in the specified [indices] range. */ -public expect fun LongArray.sliceArray(indices: IntRange): LongArray +public fun LongArray.sliceArray(indices: IntRange): LongArray { + if (indices.isEmpty()) return LongArray(0) + return copyOfRange(indices.start, indices.endInclusive + 1) +} /** * Returns a list containing elements at indices in the specified [indices] range. */ -public expect fun FloatArray.sliceArray(indices: IntRange): FloatArray +public fun FloatArray.sliceArray(indices: IntRange): FloatArray { + if (indices.isEmpty()) return FloatArray(0) + return copyOfRange(indices.start, indices.endInclusive + 1) +} /** * Returns a list containing elements at indices in the specified [indices] range. */ -public expect fun DoubleArray.sliceArray(indices: IntRange): DoubleArray +public fun DoubleArray.sliceArray(indices: IntRange): DoubleArray { + if (indices.isEmpty()) return DoubleArray(0) + return copyOfRange(indices.start, indices.endInclusive + 1) +} /** * Returns a list containing elements at indices in the specified [indices] range. */ -public expect fun BooleanArray.sliceArray(indices: IntRange): BooleanArray +public fun BooleanArray.sliceArray(indices: IntRange): BooleanArray { + if (indices.isEmpty()) return BooleanArray(0) + return copyOfRange(indices.start, indices.endInclusive + 1) +} /** * Returns a list containing elements at indices in the specified [indices] range. */ -public expect fun CharArray.sliceArray(indices: IntRange): CharArray +public fun CharArray.sliceArray(indices: IntRange): CharArray { + if (indices.isEmpty()) return CharArray(0) + return copyOfRange(indices.start, indices.endInclusive + 1) +} /** * Returns a list containing first [n] elements. * * @sample samples.collections.Collections.Transformations.take */ -public expect fun Array.take(n: Int): List +public fun Array.take(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + if (n == 0) return emptyList() + if (n >= size) return toList() + if (n == 1) return listOf(this[0]) + var count = 0 + val list = ArrayList(n) + for (item in this) { + if (count++ == n) + break + list.add(item) + } + return list +} /** * Returns a list containing first [n] elements. * * @sample samples.collections.Collections.Transformations.take */ -public expect fun ByteArray.take(n: Int): List +public fun ByteArray.take(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + if (n == 0) return emptyList() + if (n >= size) return toList() + if (n == 1) return listOf(this[0]) + var count = 0 + val list = ArrayList(n) + for (item in this) { + if (count++ == n) + break + list.add(item) + } + return list +} /** * Returns a list containing first [n] elements. * * @sample samples.collections.Collections.Transformations.take */ -public expect fun ShortArray.take(n: Int): List +public fun ShortArray.take(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + if (n == 0) return emptyList() + if (n >= size) return toList() + if (n == 1) return listOf(this[0]) + var count = 0 + val list = ArrayList(n) + for (item in this) { + if (count++ == n) + break + list.add(item) + } + return list +} /** * Returns a list containing first [n] elements. * * @sample samples.collections.Collections.Transformations.take */ -public expect fun IntArray.take(n: Int): List +public fun IntArray.take(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + if (n == 0) return emptyList() + if (n >= size) return toList() + if (n == 1) return listOf(this[0]) + var count = 0 + val list = ArrayList(n) + for (item in this) { + if (count++ == n) + break + list.add(item) + } + return list +} /** * Returns a list containing first [n] elements. * * @sample samples.collections.Collections.Transformations.take */ -public expect fun LongArray.take(n: Int): List +public fun LongArray.take(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + if (n == 0) return emptyList() + if (n >= size) return toList() + if (n == 1) return listOf(this[0]) + var count = 0 + val list = ArrayList(n) + for (item in this) { + if (count++ == n) + break + list.add(item) + } + return list +} /** * Returns a list containing first [n] elements. * * @sample samples.collections.Collections.Transformations.take */ -public expect fun FloatArray.take(n: Int): List +public fun FloatArray.take(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + if (n == 0) return emptyList() + if (n >= size) return toList() + if (n == 1) return listOf(this[0]) + var count = 0 + val list = ArrayList(n) + for (item in this) { + if (count++ == n) + break + list.add(item) + } + return list +} /** * Returns a list containing first [n] elements. * * @sample samples.collections.Collections.Transformations.take */ -public expect fun DoubleArray.take(n: Int): List +public fun DoubleArray.take(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + if (n == 0) return emptyList() + if (n >= size) return toList() + if (n == 1) return listOf(this[0]) + var count = 0 + val list = ArrayList(n) + for (item in this) { + if (count++ == n) + break + list.add(item) + } + return list +} /** * Returns a list containing first [n] elements. * * @sample samples.collections.Collections.Transformations.take */ -public expect fun BooleanArray.take(n: Int): List +public fun BooleanArray.take(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + if (n == 0) return emptyList() + if (n >= size) return toList() + if (n == 1) return listOf(this[0]) + var count = 0 + val list = ArrayList(n) + for (item in this) { + if (count++ == n) + break + list.add(item) + } + return list +} /** * Returns a list containing first [n] elements. * * @sample samples.collections.Collections.Transformations.take */ -public expect fun CharArray.take(n: Int): List +public fun CharArray.take(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + if (n == 0) return emptyList() + if (n >= size) return toList() + if (n == 1) return listOf(this[0]) + var count = 0 + val list = ArrayList(n) + for (item in this) { + if (count++ == n) + break + list.add(item) + } + return list +} /** * Returns a list containing last [n] elements. * * @sample samples.collections.Collections.Transformations.take */ -public expect fun Array.takeLast(n: Int): List +public fun Array.takeLast(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + if (n == 0) return emptyList() + val size = size + if (n >= size) return toList() + if (n == 1) return listOf(this[size - 1]) + val list = ArrayList(n) + for (index in size - n until size) + list.add(this[index]) + return list +} /** * Returns a list containing last [n] elements. * * @sample samples.collections.Collections.Transformations.take */ -public expect fun ByteArray.takeLast(n: Int): List +public fun ByteArray.takeLast(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + if (n == 0) return emptyList() + val size = size + if (n >= size) return toList() + if (n == 1) return listOf(this[size - 1]) + val list = ArrayList(n) + for (index in size - n until size) + list.add(this[index]) + return list +} /** * Returns a list containing last [n] elements. * * @sample samples.collections.Collections.Transformations.take */ -public expect fun ShortArray.takeLast(n: Int): List +public fun ShortArray.takeLast(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + if (n == 0) return emptyList() + val size = size + if (n >= size) return toList() + if (n == 1) return listOf(this[size - 1]) + val list = ArrayList(n) + for (index in size - n until size) + list.add(this[index]) + return list +} /** * Returns a list containing last [n] elements. * * @sample samples.collections.Collections.Transformations.take */ -public expect fun IntArray.takeLast(n: Int): List +public fun IntArray.takeLast(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + if (n == 0) return emptyList() + val size = size + if (n >= size) return toList() + if (n == 1) return listOf(this[size - 1]) + val list = ArrayList(n) + for (index in size - n until size) + list.add(this[index]) + return list +} /** * Returns a list containing last [n] elements. * * @sample samples.collections.Collections.Transformations.take */ -public expect fun LongArray.takeLast(n: Int): List +public fun LongArray.takeLast(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + if (n == 0) return emptyList() + val size = size + if (n >= size) return toList() + if (n == 1) return listOf(this[size - 1]) + val list = ArrayList(n) + for (index in size - n until size) + list.add(this[index]) + return list +} /** * Returns a list containing last [n] elements. * * @sample samples.collections.Collections.Transformations.take */ -public expect fun FloatArray.takeLast(n: Int): List +public fun FloatArray.takeLast(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + if (n == 0) return emptyList() + val size = size + if (n >= size) return toList() + if (n == 1) return listOf(this[size - 1]) + val list = ArrayList(n) + for (index in size - n until size) + list.add(this[index]) + return list +} /** * Returns a list containing last [n] elements. * * @sample samples.collections.Collections.Transformations.take */ -public expect fun DoubleArray.takeLast(n: Int): List +public fun DoubleArray.takeLast(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + if (n == 0) return emptyList() + val size = size + if (n >= size) return toList() + if (n == 1) return listOf(this[size - 1]) + val list = ArrayList(n) + for (index in size - n until size) + list.add(this[index]) + return list +} /** * Returns a list containing last [n] elements. * * @sample samples.collections.Collections.Transformations.take */ -public expect fun BooleanArray.takeLast(n: Int): List +public fun BooleanArray.takeLast(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + if (n == 0) return emptyList() + val size = size + if (n >= size) return toList() + if (n == 1) return listOf(this[size - 1]) + val list = ArrayList(n) + for (index in size - n until size) + list.add(this[index]) + return list +} /** * Returns a list containing last [n] elements. * * @sample samples.collections.Collections.Transformations.take */ -public expect fun CharArray.takeLast(n: Int): List +public fun CharArray.takeLast(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + if (n == 0) return emptyList() + val size = size + if (n >= size) return toList() + if (n == 1) return listOf(this[size - 1]) + val list = ArrayList(n) + for (index in size - n until size) + list.add(this[index]) + return list +} /** * Returns a list containing last elements satisfying the given [predicate]. * * @sample samples.collections.Collections.Transformations.take */ -public expect inline fun Array.takeLastWhile(predicate: (T) -> Boolean): List +public inline fun Array.takeLastWhile(predicate: (T) -> Boolean): List { + for (index in lastIndex downTo 0) { + if (!predicate(this[index])) { + return drop(index + 1) + } + } + return toList() +} /** * Returns a list containing last elements satisfying the given [predicate]. * * @sample samples.collections.Collections.Transformations.take */ -public expect inline fun ByteArray.takeLastWhile(predicate: (Byte) -> Boolean): List +public inline fun ByteArray.takeLastWhile(predicate: (Byte) -> Boolean): List { + for (index in lastIndex downTo 0) { + if (!predicate(this[index])) { + return drop(index + 1) + } + } + return toList() +} /** * Returns a list containing last elements satisfying the given [predicate]. * * @sample samples.collections.Collections.Transformations.take */ -public expect inline fun ShortArray.takeLastWhile(predicate: (Short) -> Boolean): List +public inline fun ShortArray.takeLastWhile(predicate: (Short) -> Boolean): List { + for (index in lastIndex downTo 0) { + if (!predicate(this[index])) { + return drop(index + 1) + } + } + return toList() +} /** * Returns a list containing last elements satisfying the given [predicate]. * * @sample samples.collections.Collections.Transformations.take */ -public expect inline fun IntArray.takeLastWhile(predicate: (Int) -> Boolean): List +public inline fun IntArray.takeLastWhile(predicate: (Int) -> Boolean): List { + for (index in lastIndex downTo 0) { + if (!predicate(this[index])) { + return drop(index + 1) + } + } + return toList() +} /** * Returns a list containing last elements satisfying the given [predicate]. * * @sample samples.collections.Collections.Transformations.take */ -public expect inline fun LongArray.takeLastWhile(predicate: (Long) -> Boolean): List +public inline fun LongArray.takeLastWhile(predicate: (Long) -> Boolean): List { + for (index in lastIndex downTo 0) { + if (!predicate(this[index])) { + return drop(index + 1) + } + } + return toList() +} /** * Returns a list containing last elements satisfying the given [predicate]. * * @sample samples.collections.Collections.Transformations.take */ -public expect inline fun FloatArray.takeLastWhile(predicate: (Float) -> Boolean): List +public inline fun FloatArray.takeLastWhile(predicate: (Float) -> Boolean): List { + for (index in lastIndex downTo 0) { + if (!predicate(this[index])) { + return drop(index + 1) + } + } + return toList() +} /** * Returns a list containing last elements satisfying the given [predicate]. * * @sample samples.collections.Collections.Transformations.take */ -public expect inline fun DoubleArray.takeLastWhile(predicate: (Double) -> Boolean): List +public inline fun DoubleArray.takeLastWhile(predicate: (Double) -> Boolean): List { + for (index in lastIndex downTo 0) { + if (!predicate(this[index])) { + return drop(index + 1) + } + } + return toList() +} /** * Returns a list containing last elements satisfying the given [predicate]. * * @sample samples.collections.Collections.Transformations.take */ -public expect inline fun BooleanArray.takeLastWhile(predicate: (Boolean) -> Boolean): List +public inline fun BooleanArray.takeLastWhile(predicate: (Boolean) -> Boolean): List { + for (index in lastIndex downTo 0) { + if (!predicate(this[index])) { + return drop(index + 1) + } + } + return toList() +} /** * Returns a list containing last elements satisfying the given [predicate]. * * @sample samples.collections.Collections.Transformations.take */ -public expect inline fun CharArray.takeLastWhile(predicate: (Char) -> Boolean): List +public inline fun CharArray.takeLastWhile(predicate: (Char) -> Boolean): List { + for (index in lastIndex downTo 0) { + if (!predicate(this[index])) { + return drop(index + 1) + } + } + return toList() +} /** * Returns a list containing first elements satisfying the given [predicate]. * * @sample samples.collections.Collections.Transformations.take */ -public expect inline fun Array.takeWhile(predicate: (T) -> Boolean): List +public inline fun Array.takeWhile(predicate: (T) -> Boolean): List { + val list = ArrayList() + for (item in this) { + if (!predicate(item)) + break + list.add(item) + } + return list +} /** * Returns a list containing first elements satisfying the given [predicate]. * * @sample samples.collections.Collections.Transformations.take */ -public expect inline fun ByteArray.takeWhile(predicate: (Byte) -> Boolean): List +public inline fun ByteArray.takeWhile(predicate: (Byte) -> Boolean): List { + val list = ArrayList() + for (item in this) { + if (!predicate(item)) + break + list.add(item) + } + return list +} /** * Returns a list containing first elements satisfying the given [predicate]. * * @sample samples.collections.Collections.Transformations.take */ -public expect inline fun ShortArray.takeWhile(predicate: (Short) -> Boolean): List +public inline fun ShortArray.takeWhile(predicate: (Short) -> Boolean): List { + val list = ArrayList() + for (item in this) { + if (!predicate(item)) + break + list.add(item) + } + return list +} /** * Returns a list containing first elements satisfying the given [predicate]. * * @sample samples.collections.Collections.Transformations.take */ -public expect inline fun IntArray.takeWhile(predicate: (Int) -> Boolean): List +public inline fun IntArray.takeWhile(predicate: (Int) -> Boolean): List { + val list = ArrayList() + for (item in this) { + if (!predicate(item)) + break + list.add(item) + } + return list +} /** * Returns a list containing first elements satisfying the given [predicate]. * * @sample samples.collections.Collections.Transformations.take */ -public expect inline fun LongArray.takeWhile(predicate: (Long) -> Boolean): List +public inline fun LongArray.takeWhile(predicate: (Long) -> Boolean): List { + val list = ArrayList() + for (item in this) { + if (!predicate(item)) + break + list.add(item) + } + return list +} /** * Returns a list containing first elements satisfying the given [predicate]. * * @sample samples.collections.Collections.Transformations.take */ -public expect inline fun FloatArray.takeWhile(predicate: (Float) -> Boolean): List +public inline fun FloatArray.takeWhile(predicate: (Float) -> Boolean): List { + val list = ArrayList() + for (item in this) { + if (!predicate(item)) + break + list.add(item) + } + return list +} /** * Returns a list containing first elements satisfying the given [predicate]. * * @sample samples.collections.Collections.Transformations.take */ -public expect inline fun DoubleArray.takeWhile(predicate: (Double) -> Boolean): List +public inline fun DoubleArray.takeWhile(predicate: (Double) -> Boolean): List { + val list = ArrayList() + for (item in this) { + if (!predicate(item)) + break + list.add(item) + } + return list +} /** * Returns a list containing first elements satisfying the given [predicate]. * * @sample samples.collections.Collections.Transformations.take */ -public expect inline fun BooleanArray.takeWhile(predicate: (Boolean) -> Boolean): List +public inline fun BooleanArray.takeWhile(predicate: (Boolean) -> Boolean): List { + val list = ArrayList() + for (item in this) { + if (!predicate(item)) + break + list.add(item) + } + return list +} /** * Returns a list containing first elements satisfying the given [predicate]. * * @sample samples.collections.Collections.Transformations.take */ -public expect inline fun CharArray.takeWhile(predicate: (Char) -> Boolean): List +public inline fun CharArray.takeWhile(predicate: (Char) -> Boolean): List { + val list = ArrayList() + for (item in this) { + if (!predicate(item)) + break + list.add(item) + } + return list +} /** * Reverses elements in the array in-place. */ -public expect fun Array.reverse(): Unit +public fun Array.reverse(): Unit { + val midPoint = (size / 2) - 1 + if (midPoint < 0) return + var reverseIndex = lastIndex + for (index in 0..midPoint) { + val tmp = this[index] + this[index] = this[reverseIndex] + this[reverseIndex] = tmp + reverseIndex-- + } +} /** * Reverses elements in the array in-place. */ -public expect fun ByteArray.reverse(): Unit +public fun ByteArray.reverse(): Unit { + val midPoint = (size / 2) - 1 + if (midPoint < 0) return + var reverseIndex = lastIndex + for (index in 0..midPoint) { + val tmp = this[index] + this[index] = this[reverseIndex] + this[reverseIndex] = tmp + reverseIndex-- + } +} /** * Reverses elements in the array in-place. */ -public expect fun ShortArray.reverse(): Unit +public fun ShortArray.reverse(): Unit { + val midPoint = (size / 2) - 1 + if (midPoint < 0) return + var reverseIndex = lastIndex + for (index in 0..midPoint) { + val tmp = this[index] + this[index] = this[reverseIndex] + this[reverseIndex] = tmp + reverseIndex-- + } +} /** * Reverses elements in the array in-place. */ -public expect fun IntArray.reverse(): Unit +public fun IntArray.reverse(): Unit { + val midPoint = (size / 2) - 1 + if (midPoint < 0) return + var reverseIndex = lastIndex + for (index in 0..midPoint) { + val tmp = this[index] + this[index] = this[reverseIndex] + this[reverseIndex] = tmp + reverseIndex-- + } +} /** * Reverses elements in the array in-place. */ -public expect fun LongArray.reverse(): Unit +public fun LongArray.reverse(): Unit { + val midPoint = (size / 2) - 1 + if (midPoint < 0) return + var reverseIndex = lastIndex + for (index in 0..midPoint) { + val tmp = this[index] + this[index] = this[reverseIndex] + this[reverseIndex] = tmp + reverseIndex-- + } +} /** * Reverses elements in the array in-place. */ -public expect fun FloatArray.reverse(): Unit +public fun FloatArray.reverse(): Unit { + val midPoint = (size / 2) - 1 + if (midPoint < 0) return + var reverseIndex = lastIndex + for (index in 0..midPoint) { + val tmp = this[index] + this[index] = this[reverseIndex] + this[reverseIndex] = tmp + reverseIndex-- + } +} /** * Reverses elements in the array in-place. */ -public expect fun DoubleArray.reverse(): Unit +public fun DoubleArray.reverse(): Unit { + val midPoint = (size / 2) - 1 + if (midPoint < 0) return + var reverseIndex = lastIndex + for (index in 0..midPoint) { + val tmp = this[index] + this[index] = this[reverseIndex] + this[reverseIndex] = tmp + reverseIndex-- + } +} /** * Reverses elements in the array in-place. */ -public expect fun BooleanArray.reverse(): Unit +public fun BooleanArray.reverse(): Unit { + val midPoint = (size / 2) - 1 + if (midPoint < 0) return + var reverseIndex = lastIndex + for (index in 0..midPoint) { + val tmp = this[index] + this[index] = this[reverseIndex] + this[reverseIndex] = tmp + reverseIndex-- + } +} /** * Reverses elements in the array in-place. */ -public expect fun CharArray.reverse(): Unit +public fun CharArray.reverse(): Unit { + val midPoint = (size / 2) - 1 + if (midPoint < 0) return + var reverseIndex = lastIndex + for (index in 0..midPoint) { + val tmp = this[index] + this[index] = this[reverseIndex] + this[reverseIndex] = tmp + reverseIndex-- + } +} /** * Returns a list with elements in reversed order. */ -public expect fun Array.reversed(): List +public fun Array.reversed(): List { + if (isEmpty()) return emptyList() + val list = toMutableList() + list.reverse() + return list +} /** * Returns a list with elements in reversed order. */ -public expect fun ByteArray.reversed(): List +public fun ByteArray.reversed(): List { + if (isEmpty()) return emptyList() + val list = toMutableList() + list.reverse() + return list +} /** * Returns a list with elements in reversed order. */ -public expect fun ShortArray.reversed(): List +public fun ShortArray.reversed(): List { + if (isEmpty()) return emptyList() + val list = toMutableList() + list.reverse() + return list +} /** * Returns a list with elements in reversed order. */ -public expect fun IntArray.reversed(): List +public fun IntArray.reversed(): List { + if (isEmpty()) return emptyList() + val list = toMutableList() + list.reverse() + return list +} /** * Returns a list with elements in reversed order. */ -public expect fun LongArray.reversed(): List +public fun LongArray.reversed(): List { + if (isEmpty()) return emptyList() + val list = toMutableList() + list.reverse() + return list +} /** * Returns a list with elements in reversed order. */ -public expect fun FloatArray.reversed(): List +public fun FloatArray.reversed(): List { + if (isEmpty()) return emptyList() + val list = toMutableList() + list.reverse() + return list +} /** * Returns a list with elements in reversed order. */ -public expect fun DoubleArray.reversed(): List +public fun DoubleArray.reversed(): List { + if (isEmpty()) return emptyList() + val list = toMutableList() + list.reverse() + return list +} /** * Returns a list with elements in reversed order. */ -public expect fun BooleanArray.reversed(): List +public fun BooleanArray.reversed(): List { + if (isEmpty()) return emptyList() + val list = toMutableList() + list.reverse() + return list +} /** * Returns a list with elements in reversed order. */ -public expect fun CharArray.reversed(): List +public fun CharArray.reversed(): List { + if (isEmpty()) return emptyList() + val list = toMutableList() + list.reverse() + return list +} /** * Returns an array with elements of this array in reversed order. */ -public expect fun Array.reversedArray(): Array +public fun Array.reversedArray(): Array { + if (isEmpty()) return this + val result = arrayOfNulls(this, size) + val lastIndex = lastIndex + for (i in 0..lastIndex) + result[lastIndex - i] = this[i] + return result +} /** * Returns an array with elements of this array in reversed order. */ -public expect fun ByteArray.reversedArray(): ByteArray +public fun ByteArray.reversedArray(): ByteArray { + if (isEmpty()) return this + val result = ByteArray(size) + val lastIndex = lastIndex + for (i in 0..lastIndex) + result[lastIndex - i] = this[i] + return result +} /** * Returns an array with elements of this array in reversed order. */ -public expect fun ShortArray.reversedArray(): ShortArray +public fun ShortArray.reversedArray(): ShortArray { + if (isEmpty()) return this + val result = ShortArray(size) + val lastIndex = lastIndex + for (i in 0..lastIndex) + result[lastIndex - i] = this[i] + return result +} /** * Returns an array with elements of this array in reversed order. */ -public expect fun IntArray.reversedArray(): IntArray +public fun IntArray.reversedArray(): IntArray { + if (isEmpty()) return this + val result = IntArray(size) + val lastIndex = lastIndex + for (i in 0..lastIndex) + result[lastIndex - i] = this[i] + return result +} /** * Returns an array with elements of this array in reversed order. */ -public expect fun LongArray.reversedArray(): LongArray +public fun LongArray.reversedArray(): LongArray { + if (isEmpty()) return this + val result = LongArray(size) + val lastIndex = lastIndex + for (i in 0..lastIndex) + result[lastIndex - i] = this[i] + return result +} /** * Returns an array with elements of this array in reversed order. */ -public expect fun FloatArray.reversedArray(): FloatArray +public fun FloatArray.reversedArray(): FloatArray { + if (isEmpty()) return this + val result = FloatArray(size) + val lastIndex = lastIndex + for (i in 0..lastIndex) + result[lastIndex - i] = this[i] + return result +} /** * Returns an array with elements of this array in reversed order. */ -public expect fun DoubleArray.reversedArray(): DoubleArray +public fun DoubleArray.reversedArray(): DoubleArray { + if (isEmpty()) return this + val result = DoubleArray(size) + val lastIndex = lastIndex + for (i in 0..lastIndex) + result[lastIndex - i] = this[i] + return result +} /** * Returns an array with elements of this array in reversed order. */ -public expect fun BooleanArray.reversedArray(): BooleanArray +public fun BooleanArray.reversedArray(): BooleanArray { + if (isEmpty()) return this + val result = BooleanArray(size) + val lastIndex = lastIndex + for (i in 0..lastIndex) + result[lastIndex - i] = this[i] + return result +} /** * Returns an array with elements of this array in reversed order. */ -public expect fun CharArray.reversedArray(): CharArray +public fun CharArray.reversedArray(): CharArray { + if (isEmpty()) return this + val result = CharArray(size) + val lastIndex = lastIndex + for (i in 0..lastIndex) + result[lastIndex - i] = this[i] + return result +} /** * Sorts elements in the array in-place according to natural sort order of the value returned by specified [selector] function. */ -public expect inline fun > Array.sortBy(crossinline selector: (T) -> R?): Unit +public inline fun > Array.sortBy(crossinline selector: (T) -> R?): Unit { + if (size > 1) sortWith(compareBy(selector)) +} /** * Sorts elements in the array in-place descending according to natural sort order of the value returned by specified [selector] function. */ -public expect inline fun > Array.sortByDescending(crossinline selector: (T) -> R?): Unit +public inline fun > Array.sortByDescending(crossinline selector: (T) -> R?): Unit { + if (size > 1) sortWith(compareByDescending(selector)) +} /** * Sorts elements in the array in-place descending according to their natural sort order. */ -public expect fun > Array.sortDescending(): Unit +public fun > Array.sortDescending(): Unit { + sortWith(reverseOrder()) +} /** * Sorts elements in the array in-place descending according to their natural sort order. */ -public expect fun ByteArray.sortDescending(): Unit +public fun ByteArray.sortDescending(): Unit { + if (size > 1) { + sort() + reverse() + } +} /** * Sorts elements in the array in-place descending according to their natural sort order. */ -public expect fun ShortArray.sortDescending(): Unit +public fun ShortArray.sortDescending(): Unit { + if (size > 1) { + sort() + reverse() + } +} /** * Sorts elements in the array in-place descending according to their natural sort order. */ -public expect fun IntArray.sortDescending(): Unit +public fun IntArray.sortDescending(): Unit { + if (size > 1) { + sort() + reverse() + } +} /** * Sorts elements in the array in-place descending according to their natural sort order. */ -public expect fun LongArray.sortDescending(): Unit +public fun LongArray.sortDescending(): Unit { + if (size > 1) { + sort() + reverse() + } +} /** * Sorts elements in the array in-place descending according to their natural sort order. */ -public expect fun FloatArray.sortDescending(): Unit +public fun FloatArray.sortDescending(): Unit { + if (size > 1) { + sort() + reverse() + } +} /** * Sorts elements in the array in-place descending according to their natural sort order. */ -public expect fun DoubleArray.sortDescending(): Unit +public fun DoubleArray.sortDescending(): Unit { + if (size > 1) { + sort() + reverse() + } +} /** * Sorts elements in the array in-place descending according to their natural sort order. */ -public expect fun CharArray.sortDescending(): Unit +public fun CharArray.sortDescending(): Unit { + if (size > 1) { + sort() + reverse() + } +} /** * Returns a list of all elements sorted according to their natural sort order. */ -public expect fun > Array.sorted(): List +public fun > Array.sorted(): List { + return sortedArray().asList() +} /** * Returns a list of all elements sorted according to their natural sort order. */ -public expect fun ByteArray.sorted(): List +public fun ByteArray.sorted(): List { + return toTypedArray().apply { sort() }.asList() +} /** * Returns a list of all elements sorted according to their natural sort order. */ -public expect fun ShortArray.sorted(): List +public fun ShortArray.sorted(): List { + return toTypedArray().apply { sort() }.asList() +} /** * Returns a list of all elements sorted according to their natural sort order. */ -public expect fun IntArray.sorted(): List +public fun IntArray.sorted(): List { + return toTypedArray().apply { sort() }.asList() +} /** * Returns a list of all elements sorted according to their natural sort order. */ -public expect fun LongArray.sorted(): List +public fun LongArray.sorted(): List { + return toTypedArray().apply { sort() }.asList() +} /** * Returns a list of all elements sorted according to their natural sort order. */ -public expect fun FloatArray.sorted(): List +public fun FloatArray.sorted(): List { + return toTypedArray().apply { sort() }.asList() +} /** * Returns a list of all elements sorted according to their natural sort order. */ -public expect fun DoubleArray.sorted(): List +public fun DoubleArray.sorted(): List { + return toTypedArray().apply { sort() }.asList() +} /** * Returns a list of all elements sorted according to their natural sort order. */ -public expect fun CharArray.sorted(): List +public fun CharArray.sorted(): List { + return toTypedArray().apply { sort() }.asList() +} /** * Returns an array with all elements of this array sorted according to their natural sort order. */ -public expect fun > Array.sortedArray(): Array +public fun > Array.sortedArray(): Array { + if (isEmpty()) return this + return this.copyOf().apply { sort() } +} /** * Returns an array with all elements of this array sorted according to their natural sort order. */ -public expect fun ByteArray.sortedArray(): ByteArray +public fun ByteArray.sortedArray(): ByteArray { + if (isEmpty()) return this + return this.copyOf().apply { sort() } +} /** * Returns an array with all elements of this array sorted according to their natural sort order. */ -public expect fun ShortArray.sortedArray(): ShortArray +public fun ShortArray.sortedArray(): ShortArray { + if (isEmpty()) return this + return this.copyOf().apply { sort() } +} /** * Returns an array with all elements of this array sorted according to their natural sort order. */ -public expect fun IntArray.sortedArray(): IntArray +public fun IntArray.sortedArray(): IntArray { + if (isEmpty()) return this + return this.copyOf().apply { sort() } +} /** * Returns an array with all elements of this array sorted according to their natural sort order. */ -public expect fun LongArray.sortedArray(): LongArray +public fun LongArray.sortedArray(): LongArray { + if (isEmpty()) return this + return this.copyOf().apply { sort() } +} /** * Returns an array with all elements of this array sorted according to their natural sort order. */ -public expect fun FloatArray.sortedArray(): FloatArray +public fun FloatArray.sortedArray(): FloatArray { + if (isEmpty()) return this + return this.copyOf().apply { sort() } +} /** * Returns an array with all elements of this array sorted according to their natural sort order. */ -public expect fun DoubleArray.sortedArray(): DoubleArray +public fun DoubleArray.sortedArray(): DoubleArray { + if (isEmpty()) return this + return this.copyOf().apply { sort() } +} /** * Returns an array with all elements of this array sorted according to their natural sort order. */ -public expect fun CharArray.sortedArray(): CharArray +public fun CharArray.sortedArray(): CharArray { + if (isEmpty()) return this + return this.copyOf().apply { sort() } +} /** * Returns an array with all elements of this array sorted descending according to their natural sort order. */ -public expect fun > Array.sortedArrayDescending(): Array +public fun > Array.sortedArrayDescending(): Array { + if (isEmpty()) return this + return this.copyOf().apply { sortWith(reverseOrder()) } +} /** * Returns an array with all elements of this array sorted descending according to their natural sort order. */ -public expect fun ByteArray.sortedArrayDescending(): ByteArray +public fun ByteArray.sortedArrayDescending(): ByteArray { + if (isEmpty()) return this + return this.copyOf().apply { sortDescending() } +} /** * Returns an array with all elements of this array sorted descending according to their natural sort order. */ -public expect fun ShortArray.sortedArrayDescending(): ShortArray +public fun ShortArray.sortedArrayDescending(): ShortArray { + if (isEmpty()) return this + return this.copyOf().apply { sortDescending() } +} /** * Returns an array with all elements of this array sorted descending according to their natural sort order. */ -public expect fun IntArray.sortedArrayDescending(): IntArray +public fun IntArray.sortedArrayDescending(): IntArray { + if (isEmpty()) return this + return this.copyOf().apply { sortDescending() } +} /** * Returns an array with all elements of this array sorted descending according to their natural sort order. */ -public expect fun LongArray.sortedArrayDescending(): LongArray +public fun LongArray.sortedArrayDescending(): LongArray { + if (isEmpty()) return this + return this.copyOf().apply { sortDescending() } +} /** * Returns an array with all elements of this array sorted descending according to their natural sort order. */ -public expect fun FloatArray.sortedArrayDescending(): FloatArray +public fun FloatArray.sortedArrayDescending(): FloatArray { + if (isEmpty()) return this + return this.copyOf().apply { sortDescending() } +} /** * Returns an array with all elements of this array sorted descending according to their natural sort order. */ -public expect fun DoubleArray.sortedArrayDescending(): DoubleArray +public fun DoubleArray.sortedArrayDescending(): DoubleArray { + if (isEmpty()) return this + return this.copyOf().apply { sortDescending() } +} /** * Returns an array with all elements of this array sorted descending according to their natural sort order. */ -public expect fun CharArray.sortedArrayDescending(): CharArray +public fun CharArray.sortedArrayDescending(): CharArray { + if (isEmpty()) return this + return this.copyOf().apply { sortDescending() } +} /** * Returns an array with all elements of this array sorted according the specified [comparator]. */ -public expect fun Array.sortedArrayWith(comparator: Comparator): Array +public fun Array.sortedArrayWith(comparator: Comparator): Array { + if (isEmpty()) return this + return this.copyOf().apply { sortWith(comparator) } +} /** * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. */ -public expect inline fun > Array.sortedBy(crossinline selector: (T) -> R?): List +public inline fun > Array.sortedBy(crossinline selector: (T) -> R?): List { + return sortedWith(compareBy(selector)) +} /** * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. */ -public expect inline fun > ByteArray.sortedBy(crossinline selector: (Byte) -> R?): List +public inline fun > ByteArray.sortedBy(crossinline selector: (Byte) -> R?): List { + return sortedWith(compareBy(selector)) +} /** * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. */ -public expect inline fun > ShortArray.sortedBy(crossinline selector: (Short) -> R?): List +public inline fun > ShortArray.sortedBy(crossinline selector: (Short) -> R?): List { + return sortedWith(compareBy(selector)) +} /** * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. */ -public expect inline fun > IntArray.sortedBy(crossinline selector: (Int) -> R?): List +public inline fun > IntArray.sortedBy(crossinline selector: (Int) -> R?): List { + return sortedWith(compareBy(selector)) +} /** * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. */ -public expect inline fun > LongArray.sortedBy(crossinline selector: (Long) -> R?): List +public inline fun > LongArray.sortedBy(crossinline selector: (Long) -> R?): List { + return sortedWith(compareBy(selector)) +} /** * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. */ -public expect inline fun > FloatArray.sortedBy(crossinline selector: (Float) -> R?): List +public inline fun > FloatArray.sortedBy(crossinline selector: (Float) -> R?): List { + return sortedWith(compareBy(selector)) +} /** * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. */ -public expect inline fun > DoubleArray.sortedBy(crossinline selector: (Double) -> R?): List +public inline fun > DoubleArray.sortedBy(crossinline selector: (Double) -> R?): List { + return sortedWith(compareBy(selector)) +} /** * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. */ -public expect inline fun > BooleanArray.sortedBy(crossinline selector: (Boolean) -> R?): List +public inline fun > BooleanArray.sortedBy(crossinline selector: (Boolean) -> R?): List { + return sortedWith(compareBy(selector)) +} /** * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. */ -public expect inline fun > CharArray.sortedBy(crossinline selector: (Char) -> R?): List +public inline fun > CharArray.sortedBy(crossinline selector: (Char) -> R?): List { + return sortedWith(compareBy(selector)) +} /** * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. */ -public expect inline fun > Array.sortedByDescending(crossinline selector: (T) -> R?): List +public inline fun > Array.sortedByDescending(crossinline selector: (T) -> R?): List { + return sortedWith(compareByDescending(selector)) +} /** * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. */ -public expect inline fun > ByteArray.sortedByDescending(crossinline selector: (Byte) -> R?): List +public inline fun > ByteArray.sortedByDescending(crossinline selector: (Byte) -> R?): List { + return sortedWith(compareByDescending(selector)) +} /** * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. */ -public expect inline fun > ShortArray.sortedByDescending(crossinline selector: (Short) -> R?): List +public inline fun > ShortArray.sortedByDescending(crossinline selector: (Short) -> R?): List { + return sortedWith(compareByDescending(selector)) +} /** * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. */ -public expect inline fun > IntArray.sortedByDescending(crossinline selector: (Int) -> R?): List +public inline fun > IntArray.sortedByDescending(crossinline selector: (Int) -> R?): List { + return sortedWith(compareByDescending(selector)) +} /** * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. */ -public expect inline fun > LongArray.sortedByDescending(crossinline selector: (Long) -> R?): List +public inline fun > LongArray.sortedByDescending(crossinline selector: (Long) -> R?): List { + return sortedWith(compareByDescending(selector)) +} /** * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. */ -public expect inline fun > FloatArray.sortedByDescending(crossinline selector: (Float) -> R?): List +public inline fun > FloatArray.sortedByDescending(crossinline selector: (Float) -> R?): List { + return sortedWith(compareByDescending(selector)) +} /** * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. */ -public expect inline fun > DoubleArray.sortedByDescending(crossinline selector: (Double) -> R?): List +public inline fun > DoubleArray.sortedByDescending(crossinline selector: (Double) -> R?): List { + return sortedWith(compareByDescending(selector)) +} /** * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. */ -public expect inline fun > BooleanArray.sortedByDescending(crossinline selector: (Boolean) -> R?): List +public inline fun > BooleanArray.sortedByDescending(crossinline selector: (Boolean) -> R?): List { + return sortedWith(compareByDescending(selector)) +} /** * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. */ -public expect inline fun > CharArray.sortedByDescending(crossinline selector: (Char) -> R?): List +public inline fun > CharArray.sortedByDescending(crossinline selector: (Char) -> R?): List { + return sortedWith(compareByDescending(selector)) +} /** * Returns a list of all elements sorted descending according to their natural sort order. */ -public expect fun > Array.sortedDescending(): List +public fun > Array.sortedDescending(): List { + return sortedWith(reverseOrder()) +} /** * Returns a list of all elements sorted descending according to their natural sort order. */ -public expect fun ByteArray.sortedDescending(): List +public fun ByteArray.sortedDescending(): List { + return copyOf().apply { sort() }.reversed() +} /** * Returns a list of all elements sorted descending according to their natural sort order. */ -public expect fun ShortArray.sortedDescending(): List +public fun ShortArray.sortedDescending(): List { + return copyOf().apply { sort() }.reversed() +} /** * Returns a list of all elements sorted descending according to their natural sort order. */ -public expect fun IntArray.sortedDescending(): List +public fun IntArray.sortedDescending(): List { + return copyOf().apply { sort() }.reversed() +} /** * Returns a list of all elements sorted descending according to their natural sort order. */ -public expect fun LongArray.sortedDescending(): List +public fun LongArray.sortedDescending(): List { + return copyOf().apply { sort() }.reversed() +} /** * Returns a list of all elements sorted descending according to their natural sort order. */ -public expect fun FloatArray.sortedDescending(): List +public fun FloatArray.sortedDescending(): List { + return copyOf().apply { sort() }.reversed() +} /** * Returns a list of all elements sorted descending according to their natural sort order. */ -public expect fun DoubleArray.sortedDescending(): List +public fun DoubleArray.sortedDescending(): List { + return copyOf().apply { sort() }.reversed() +} /** * Returns a list of all elements sorted descending according to their natural sort order. */ -public expect fun CharArray.sortedDescending(): List +public fun CharArray.sortedDescending(): List { + return copyOf().apply { sort() }.reversed() +} /** * Returns a list of all elements sorted according to the specified [comparator]. */ -public expect fun Array.sortedWith(comparator: Comparator): List +public fun Array.sortedWith(comparator: Comparator): List { + return sortedArrayWith(comparator).asList() +} /** * Returns a list of all elements sorted according to the specified [comparator]. */ -public expect fun ByteArray.sortedWith(comparator: Comparator): List +public fun ByteArray.sortedWith(comparator: Comparator): List { + return toTypedArray().apply { sortWith(comparator) }.asList() +} /** * Returns a list of all elements sorted according to the specified [comparator]. */ -public expect fun ShortArray.sortedWith(comparator: Comparator): List +public fun ShortArray.sortedWith(comparator: Comparator): List { + return toTypedArray().apply { sortWith(comparator) }.asList() +} /** * Returns a list of all elements sorted according to the specified [comparator]. */ -public expect fun IntArray.sortedWith(comparator: Comparator): List +public fun IntArray.sortedWith(comparator: Comparator): List { + return toTypedArray().apply { sortWith(comparator) }.asList() +} /** * Returns a list of all elements sorted according to the specified [comparator]. */ -public expect fun LongArray.sortedWith(comparator: Comparator): List +public fun LongArray.sortedWith(comparator: Comparator): List { + return toTypedArray().apply { sortWith(comparator) }.asList() +} /** * Returns a list of all elements sorted according to the specified [comparator]. */ -public expect fun FloatArray.sortedWith(comparator: Comparator): List +public fun FloatArray.sortedWith(comparator: Comparator): List { + return toTypedArray().apply { sortWith(comparator) }.asList() +} /** * Returns a list of all elements sorted according to the specified [comparator]. */ -public expect fun DoubleArray.sortedWith(comparator: Comparator): List +public fun DoubleArray.sortedWith(comparator: Comparator): List { + return toTypedArray().apply { sortWith(comparator) }.asList() +} /** * Returns a list of all elements sorted according to the specified [comparator]. */ -public expect fun BooleanArray.sortedWith(comparator: Comparator): List +public fun BooleanArray.sortedWith(comparator: Comparator): List { + return toTypedArray().apply { sortWith(comparator) }.asList() +} /** * Returns a list of all elements sorted according to the specified [comparator]. */ -public expect fun CharArray.sortedWith(comparator: Comparator): List +public fun CharArray.sortedWith(comparator: Comparator): List { + return toTypedArray().apply { sortWith(comparator) }.asList() +} /** * Returns a [List] that wraps the original array. @@ -3350,200 +5705,254 @@ public expect fun CharArray.copyOfRange(fromIndex: Int, toIndex: Int): CharArray /** * Returns the range of valid indices for the array. */ -public expect val Array.indices: IntRange +public val Array.indices: IntRange + get() = IntRange(0, lastIndex) /** * Returns the range of valid indices for the array. */ -public expect val ByteArray.indices: IntRange +public val ByteArray.indices: IntRange + get() = IntRange(0, lastIndex) /** * Returns the range of valid indices for the array. */ -public expect val ShortArray.indices: IntRange +public val ShortArray.indices: IntRange + get() = IntRange(0, lastIndex) /** * Returns the range of valid indices for the array. */ -public expect val IntArray.indices: IntRange +public val IntArray.indices: IntRange + get() = IntRange(0, lastIndex) /** * Returns the range of valid indices for the array. */ -public expect val LongArray.indices: IntRange +public val LongArray.indices: IntRange + get() = IntRange(0, lastIndex) /** * Returns the range of valid indices for the array. */ -public expect val FloatArray.indices: IntRange +public val FloatArray.indices: IntRange + get() = IntRange(0, lastIndex) /** * Returns the range of valid indices for the array. */ -public expect val DoubleArray.indices: IntRange +public val DoubleArray.indices: IntRange + get() = IntRange(0, lastIndex) /** * Returns the range of valid indices for the array. */ -public expect val BooleanArray.indices: IntRange +public val BooleanArray.indices: IntRange + get() = IntRange(0, lastIndex) /** * Returns the range of valid indices for the array. */ -public expect val CharArray.indices: IntRange +public val CharArray.indices: IntRange + get() = IntRange(0, lastIndex) /** * Returns `true` if the array is empty. */ @kotlin.internal.InlineOnly -public expect inline fun Array.isEmpty(): Boolean +public inline fun Array.isEmpty(): Boolean { + return size == 0 +} /** * Returns `true` if the array is empty. */ @kotlin.internal.InlineOnly -public expect inline fun ByteArray.isEmpty(): Boolean +public inline fun ByteArray.isEmpty(): Boolean { + return size == 0 +} /** * Returns `true` if the array is empty. */ @kotlin.internal.InlineOnly -public expect inline fun ShortArray.isEmpty(): Boolean +public inline fun ShortArray.isEmpty(): Boolean { + return size == 0 +} /** * Returns `true` if the array is empty. */ @kotlin.internal.InlineOnly -public expect inline fun IntArray.isEmpty(): Boolean +public inline fun IntArray.isEmpty(): Boolean { + return size == 0 +} /** * Returns `true` if the array is empty. */ @kotlin.internal.InlineOnly -public expect inline fun LongArray.isEmpty(): Boolean +public inline fun LongArray.isEmpty(): Boolean { + return size == 0 +} /** * Returns `true` if the array is empty. */ @kotlin.internal.InlineOnly -public expect inline fun FloatArray.isEmpty(): Boolean +public inline fun FloatArray.isEmpty(): Boolean { + return size == 0 +} /** * Returns `true` if the array is empty. */ @kotlin.internal.InlineOnly -public expect inline fun DoubleArray.isEmpty(): Boolean +public inline fun DoubleArray.isEmpty(): Boolean { + return size == 0 +} /** * Returns `true` if the array is empty. */ @kotlin.internal.InlineOnly -public expect inline fun BooleanArray.isEmpty(): Boolean +public inline fun BooleanArray.isEmpty(): Boolean { + return size == 0 +} /** * Returns `true` if the array is empty. */ @kotlin.internal.InlineOnly -public expect inline fun CharArray.isEmpty(): Boolean +public inline fun CharArray.isEmpty(): Boolean { + return size == 0 +} /** * Returns `true` if the array is not empty. */ @kotlin.internal.InlineOnly -public expect inline fun Array.isNotEmpty(): Boolean +public inline fun Array.isNotEmpty(): Boolean { + return !isEmpty() +} /** * Returns `true` if the array is not empty. */ @kotlin.internal.InlineOnly -public expect inline fun ByteArray.isNotEmpty(): Boolean +public inline fun ByteArray.isNotEmpty(): Boolean { + return !isEmpty() +} /** * Returns `true` if the array is not empty. */ @kotlin.internal.InlineOnly -public expect inline fun ShortArray.isNotEmpty(): Boolean +public inline fun ShortArray.isNotEmpty(): Boolean { + return !isEmpty() +} /** * Returns `true` if the array is not empty. */ @kotlin.internal.InlineOnly -public expect inline fun IntArray.isNotEmpty(): Boolean +public inline fun IntArray.isNotEmpty(): Boolean { + return !isEmpty() +} /** * Returns `true` if the array is not empty. */ @kotlin.internal.InlineOnly -public expect inline fun LongArray.isNotEmpty(): Boolean +public inline fun LongArray.isNotEmpty(): Boolean { + return !isEmpty() +} /** * Returns `true` if the array is not empty. */ @kotlin.internal.InlineOnly -public expect inline fun FloatArray.isNotEmpty(): Boolean +public inline fun FloatArray.isNotEmpty(): Boolean { + return !isEmpty() +} /** * Returns `true` if the array is not empty. */ @kotlin.internal.InlineOnly -public expect inline fun DoubleArray.isNotEmpty(): Boolean +public inline fun DoubleArray.isNotEmpty(): Boolean { + return !isEmpty() +} /** * Returns `true` if the array is not empty. */ @kotlin.internal.InlineOnly -public expect inline fun BooleanArray.isNotEmpty(): Boolean +public inline fun BooleanArray.isNotEmpty(): Boolean { + return !isEmpty() +} /** * Returns `true` if the array is not empty. */ @kotlin.internal.InlineOnly -public expect inline fun CharArray.isNotEmpty(): Boolean +public inline fun CharArray.isNotEmpty(): Boolean { + return !isEmpty() +} /** * Returns the last valid index for the array. */ -public expect val Array.lastIndex: Int +public val Array.lastIndex: Int + get() = size - 1 /** * Returns the last valid index for the array. */ -public expect val ByteArray.lastIndex: Int +public val ByteArray.lastIndex: Int + get() = size - 1 /** * Returns the last valid index for the array. */ -public expect val ShortArray.lastIndex: Int +public val ShortArray.lastIndex: Int + get() = size - 1 /** * Returns the last valid index for the array. */ -public expect val IntArray.lastIndex: Int +public val IntArray.lastIndex: Int + get() = size - 1 /** * Returns the last valid index for the array. */ -public expect val LongArray.lastIndex: Int +public val LongArray.lastIndex: Int + get() = size - 1 /** * Returns the last valid index for the array. */ -public expect val FloatArray.lastIndex: Int +public val FloatArray.lastIndex: Int + get() = size - 1 /** * Returns the last valid index for the array. */ -public expect val DoubleArray.lastIndex: Int +public val DoubleArray.lastIndex: Int + get() = size - 1 /** * Returns the last valid index for the array. */ -public expect val BooleanArray.lastIndex: Int +public val BooleanArray.lastIndex: Int + get() = size - 1 /** * Returns the last valid index for the array. */ -public expect val CharArray.lastIndex: Int +public val CharArray.lastIndex: Int + get() = size - 1 /** * Returns an array containing all elements of the original array and then the given [element]. @@ -3733,42 +6142,82 @@ public expect fun Array.sortWith(comparator: Comparator): Unit /** * Returns an array of Boolean containing all of the elements of this generic array. */ -public expect fun Array.toBooleanArray(): BooleanArray +public fun Array.toBooleanArray(): BooleanArray { + val result = BooleanArray(size) + for (index in indices) + result[index] = this[index] + return result +} /** * Returns an array of Byte containing all of the elements of this generic array. */ -public expect fun Array.toByteArray(): ByteArray +public fun Array.toByteArray(): ByteArray { + val result = ByteArray(size) + for (index in indices) + result[index] = this[index] + return result +} /** * Returns an array of Char containing all of the elements of this generic array. */ -public expect fun Array.toCharArray(): CharArray +public fun Array.toCharArray(): CharArray { + val result = CharArray(size) + for (index in indices) + result[index] = this[index] + return result +} /** * Returns an array of Double containing all of the elements of this generic array. */ -public expect fun Array.toDoubleArray(): DoubleArray +public fun Array.toDoubleArray(): DoubleArray { + val result = DoubleArray(size) + for (index in indices) + result[index] = this[index] + return result +} /** * Returns an array of Float containing all of the elements of this generic array. */ -public expect fun Array.toFloatArray(): FloatArray +public fun Array.toFloatArray(): FloatArray { + val result = FloatArray(size) + for (index in indices) + result[index] = this[index] + return result +} /** * Returns an array of Int containing all of the elements of this generic array. */ -public expect fun Array.toIntArray(): IntArray +public fun Array.toIntArray(): IntArray { + val result = IntArray(size) + for (index in indices) + result[index] = this[index] + return result +} /** * Returns an array of Long containing all of the elements of this generic array. */ -public expect fun Array.toLongArray(): LongArray +public fun Array.toLongArray(): LongArray { + val result = LongArray(size) + for (index in indices) + result[index] = this[index] + return result +} /** * Returns an array of Short containing all of the elements of this generic array. */ -public expect fun Array.toShortArray(): ShortArray +public fun Array.toShortArray(): ShortArray { + val result = ShortArray(size) + for (index in indices) + result[index] = this[index] + return result +} /** * Returns a *typed* object array containing all of the elements of this primitive array. @@ -3818,7 +6267,10 @@ public expect fun CharArray.toTypedArray(): Array * * The returned map preserves the entry iteration order of the original array. */ -public expect inline fun Array.associate(transform: (T) -> Pair): Map +public inline fun Array.associate(transform: (T) -> Pair): Map { + val capacity = mapCapacity(size).coerceAtLeast(16) + return associateTo(LinkedHashMap(capacity), transform) +} /** * Returns a [Map] containing key-value pairs provided by [transform] function @@ -3828,7 +6280,10 @@ public expect inline fun Array.associate(transform: (T) -> Pair * * The returned map preserves the entry iteration order of the original array. */ -public expect inline fun ByteArray.associate(transform: (Byte) -> Pair): Map +public inline fun ByteArray.associate(transform: (Byte) -> Pair): Map { + val capacity = mapCapacity(size).coerceAtLeast(16) + return associateTo(LinkedHashMap(capacity), transform) +} /** * Returns a [Map] containing key-value pairs provided by [transform] function @@ -3838,7 +6293,10 @@ public expect inline fun ByteArray.associate(transform: (Byte) -> Pair ShortArray.associate(transform: (Short) -> Pair): Map +public inline fun ShortArray.associate(transform: (Short) -> Pair): Map { + val capacity = mapCapacity(size).coerceAtLeast(16) + return associateTo(LinkedHashMap(capacity), transform) +} /** * Returns a [Map] containing key-value pairs provided by [transform] function @@ -3848,7 +6306,10 @@ public expect inline fun ShortArray.associate(transform: (Short) -> Pair< * * The returned map preserves the entry iteration order of the original array. */ -public expect inline fun IntArray.associate(transform: (Int) -> Pair): Map +public inline fun IntArray.associate(transform: (Int) -> Pair): Map { + val capacity = mapCapacity(size).coerceAtLeast(16) + return associateTo(LinkedHashMap(capacity), transform) +} /** * Returns a [Map] containing key-value pairs provided by [transform] function @@ -3858,7 +6319,10 @@ public expect inline fun IntArray.associate(transform: (Int) -> Pair LongArray.associate(transform: (Long) -> Pair): Map +public inline fun LongArray.associate(transform: (Long) -> Pair): Map { + val capacity = mapCapacity(size).coerceAtLeast(16) + return associateTo(LinkedHashMap(capacity), transform) +} /** * Returns a [Map] containing key-value pairs provided by [transform] function @@ -3868,7 +6332,10 @@ public expect inline fun LongArray.associate(transform: (Long) -> Pair FloatArray.associate(transform: (Float) -> Pair): Map +public inline fun FloatArray.associate(transform: (Float) -> Pair): Map { + val capacity = mapCapacity(size).coerceAtLeast(16) + return associateTo(LinkedHashMap(capacity), transform) +} /** * Returns a [Map] containing key-value pairs provided by [transform] function @@ -3878,7 +6345,10 @@ public expect inline fun FloatArray.associate(transform: (Float) -> Pair< * * The returned map preserves the entry iteration order of the original array. */ -public expect inline fun DoubleArray.associate(transform: (Double) -> Pair): Map +public inline fun DoubleArray.associate(transform: (Double) -> Pair): Map { + val capacity = mapCapacity(size).coerceAtLeast(16) + return associateTo(LinkedHashMap(capacity), transform) +} /** * Returns a [Map] containing key-value pairs provided by [transform] function @@ -3888,7 +6358,10 @@ public expect inline fun DoubleArray.associate(transform: (Double) -> Pai * * The returned map preserves the entry iteration order of the original array. */ -public expect inline fun BooleanArray.associate(transform: (Boolean) -> Pair): Map +public inline fun BooleanArray.associate(transform: (Boolean) -> Pair): Map { + val capacity = mapCapacity(size).coerceAtLeast(16) + return associateTo(LinkedHashMap(capacity), transform) +} /** * Returns a [Map] containing key-value pairs provided by [transform] function @@ -3898,7 +6371,10 @@ public expect inline fun BooleanArray.associate(transform: (Boolean) -> P * * The returned map preserves the entry iteration order of the original array. */ -public expect inline fun CharArray.associate(transform: (Char) -> Pair): Map +public inline fun CharArray.associate(transform: (Char) -> Pair): Map { + val capacity = mapCapacity(size).coerceAtLeast(16) + return associateTo(LinkedHashMap(capacity), transform) +} /** * Returns a [Map] containing the elements from the given array indexed by the key @@ -3908,7 +6384,10 @@ public expect inline fun CharArray.associate(transform: (Char) -> Pair Array.associateBy(keySelector: (T) -> K): Map +public inline fun Array.associateBy(keySelector: (T) -> K): Map { + val capacity = mapCapacity(size).coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector) +} /** * Returns a [Map] containing the elements from the given array indexed by the key @@ -3918,7 +6397,10 @@ public expect inline fun Array.associateBy(keySelector: (T) -> K): * * The returned map preserves the entry iteration order of the original array. */ -public expect inline fun ByteArray.associateBy(keySelector: (Byte) -> K): Map +public inline fun ByteArray.associateBy(keySelector: (Byte) -> K): Map { + val capacity = mapCapacity(size).coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector) +} /** * Returns a [Map] containing the elements from the given array indexed by the key @@ -3928,7 +6410,10 @@ public expect inline fun ByteArray.associateBy(keySelector: (Byte) -> K): Ma * * The returned map preserves the entry iteration order of the original array. */ -public expect inline fun ShortArray.associateBy(keySelector: (Short) -> K): Map +public inline fun ShortArray.associateBy(keySelector: (Short) -> K): Map { + val capacity = mapCapacity(size).coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector) +} /** * Returns a [Map] containing the elements from the given array indexed by the key @@ -3938,7 +6423,10 @@ public expect inline fun ShortArray.associateBy(keySelector: (Short) -> K): * * The returned map preserves the entry iteration order of the original array. */ -public expect inline fun IntArray.associateBy(keySelector: (Int) -> K): Map +public inline fun IntArray.associateBy(keySelector: (Int) -> K): Map { + val capacity = mapCapacity(size).coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector) +} /** * Returns a [Map] containing the elements from the given array indexed by the key @@ -3948,7 +6436,10 @@ public expect inline fun IntArray.associateBy(keySelector: (Int) -> K): Map< * * The returned map preserves the entry iteration order of the original array. */ -public expect inline fun LongArray.associateBy(keySelector: (Long) -> K): Map +public inline fun LongArray.associateBy(keySelector: (Long) -> K): Map { + val capacity = mapCapacity(size).coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector) +} /** * Returns a [Map] containing the elements from the given array indexed by the key @@ -3958,7 +6449,10 @@ public expect inline fun LongArray.associateBy(keySelector: (Long) -> K): Ma * * The returned map preserves the entry iteration order of the original array. */ -public expect inline fun FloatArray.associateBy(keySelector: (Float) -> K): Map +public inline fun FloatArray.associateBy(keySelector: (Float) -> K): Map { + val capacity = mapCapacity(size).coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector) +} /** * Returns a [Map] containing the elements from the given array indexed by the key @@ -3968,7 +6462,10 @@ public expect inline fun FloatArray.associateBy(keySelector: (Float) -> K): * * The returned map preserves the entry iteration order of the original array. */ -public expect inline fun DoubleArray.associateBy(keySelector: (Double) -> K): Map +public inline fun DoubleArray.associateBy(keySelector: (Double) -> K): Map { + val capacity = mapCapacity(size).coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector) +} /** * Returns a [Map] containing the elements from the given array indexed by the key @@ -3978,7 +6475,10 @@ public expect inline fun DoubleArray.associateBy(keySelector: (Double) -> K) * * The returned map preserves the entry iteration order of the original array. */ -public expect inline fun BooleanArray.associateBy(keySelector: (Boolean) -> K): Map +public inline fun BooleanArray.associateBy(keySelector: (Boolean) -> K): Map { + val capacity = mapCapacity(size).coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector) +} /** * Returns a [Map] containing the elements from the given array indexed by the key @@ -3988,7 +6488,10 @@ public expect inline fun BooleanArray.associateBy(keySelector: (Boolean) -> * * The returned map preserves the entry iteration order of the original array. */ -public expect inline fun CharArray.associateBy(keySelector: (Char) -> K): Map +public inline fun CharArray.associateBy(keySelector: (Char) -> K): Map { + val capacity = mapCapacity(size).coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector) +} /** * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. @@ -3997,7 +6500,10 @@ public expect inline fun CharArray.associateBy(keySelector: (Char) -> K): Ma * * The returned map preserves the entry iteration order of the original array. */ -public expect inline fun Array.associateBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map +public inline fun Array.associateBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map { + val capacity = mapCapacity(size).coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) +} /** * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. @@ -4006,7 +6512,10 @@ public expect inline fun Array.associateBy(keySelector: (T) -> * * The returned map preserves the entry iteration order of the original array. */ -public expect inline fun ByteArray.associateBy(keySelector: (Byte) -> K, valueTransform: (Byte) -> V): Map +public inline fun ByteArray.associateBy(keySelector: (Byte) -> K, valueTransform: (Byte) -> V): Map { + val capacity = mapCapacity(size).coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) +} /** * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. @@ -4015,7 +6524,10 @@ public expect inline fun ByteArray.associateBy(keySelector: (Byte) -> K, * * The returned map preserves the entry iteration order of the original array. */ -public expect inline fun ShortArray.associateBy(keySelector: (Short) -> K, valueTransform: (Short) -> V): Map +public inline fun ShortArray.associateBy(keySelector: (Short) -> K, valueTransform: (Short) -> V): Map { + val capacity = mapCapacity(size).coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) +} /** * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. @@ -4024,7 +6536,10 @@ public expect inline fun ShortArray.associateBy(keySelector: (Short) -> K * * The returned map preserves the entry iteration order of the original array. */ -public expect inline fun IntArray.associateBy(keySelector: (Int) -> K, valueTransform: (Int) -> V): Map +public inline fun IntArray.associateBy(keySelector: (Int) -> K, valueTransform: (Int) -> V): Map { + val capacity = mapCapacity(size).coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) +} /** * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. @@ -4033,7 +6548,10 @@ public expect inline fun IntArray.associateBy(keySelector: (Int) -> K, va * * The returned map preserves the entry iteration order of the original array. */ -public expect inline fun LongArray.associateBy(keySelector: (Long) -> K, valueTransform: (Long) -> V): Map +public inline fun LongArray.associateBy(keySelector: (Long) -> K, valueTransform: (Long) -> V): Map { + val capacity = mapCapacity(size).coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) +} /** * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. @@ -4042,7 +6560,10 @@ public expect inline fun LongArray.associateBy(keySelector: (Long) -> K, * * The returned map preserves the entry iteration order of the original array. */ -public expect inline fun FloatArray.associateBy(keySelector: (Float) -> K, valueTransform: (Float) -> V): Map +public inline fun FloatArray.associateBy(keySelector: (Float) -> K, valueTransform: (Float) -> V): Map { + val capacity = mapCapacity(size).coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) +} /** * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. @@ -4051,7 +6572,10 @@ public expect inline fun FloatArray.associateBy(keySelector: (Float) -> K * * The returned map preserves the entry iteration order of the original array. */ -public expect inline fun DoubleArray.associateBy(keySelector: (Double) -> K, valueTransform: (Double) -> V): Map +public inline fun DoubleArray.associateBy(keySelector: (Double) -> K, valueTransform: (Double) -> V): Map { + val capacity = mapCapacity(size).coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) +} /** * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. @@ -4060,7 +6584,10 @@ public expect inline fun DoubleArray.associateBy(keySelector: (Double) -> * * The returned map preserves the entry iteration order of the original array. */ -public expect inline fun BooleanArray.associateBy(keySelector: (Boolean) -> K, valueTransform: (Boolean) -> V): Map +public inline fun BooleanArray.associateBy(keySelector: (Boolean) -> K, valueTransform: (Boolean) -> V): Map { + val capacity = mapCapacity(size).coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) +} /** * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. @@ -4069,7 +6596,10 @@ public expect inline fun BooleanArray.associateBy(keySelector: (Boolean) * * The returned map preserves the entry iteration order of the original array. */ -public expect inline fun CharArray.associateBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map +public inline fun CharArray.associateBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map { + val capacity = mapCapacity(size).coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) +} /** * Populates and returns the [destination] mutable map with key-value pairs, @@ -4078,7 +6608,12 @@ public expect inline fun CharArray.associateBy(keySelector: (Char) -> K, * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. */ -public expect inline fun > Array.associateByTo(destination: M, keySelector: (T) -> K): M +public inline fun > Array.associateByTo(destination: M, keySelector: (T) -> K): M { + for (element in this) { + destination.put(keySelector(element), element) + } + return destination +} /** * Populates and returns the [destination] mutable map with key-value pairs, @@ -4087,7 +6622,12 @@ public expect inline fun > Array.associa * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. */ -public expect inline fun > ByteArray.associateByTo(destination: M, keySelector: (Byte) -> K): M +public inline fun > ByteArray.associateByTo(destination: M, keySelector: (Byte) -> K): M { + for (element in this) { + destination.put(keySelector(element), element) + } + return destination +} /** * Populates and returns the [destination] mutable map with key-value pairs, @@ -4096,7 +6636,12 @@ public expect inline fun > ByteArray.associateB * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. */ -public expect inline fun > ShortArray.associateByTo(destination: M, keySelector: (Short) -> K): M +public inline fun > ShortArray.associateByTo(destination: M, keySelector: (Short) -> K): M { + for (element in this) { + destination.put(keySelector(element), element) + } + return destination +} /** * Populates and returns the [destination] mutable map with key-value pairs, @@ -4105,7 +6650,12 @@ public expect inline fun > ShortArray.associat * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. */ -public expect inline fun > IntArray.associateByTo(destination: M, keySelector: (Int) -> K): M +public inline fun > IntArray.associateByTo(destination: M, keySelector: (Int) -> K): M { + for (element in this) { + destination.put(keySelector(element), element) + } + return destination +} /** * Populates and returns the [destination] mutable map with key-value pairs, @@ -4114,7 +6664,12 @@ public expect inline fun > IntArray.associateByT * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. */ -public expect inline fun > LongArray.associateByTo(destination: M, keySelector: (Long) -> K): M +public inline fun > LongArray.associateByTo(destination: M, keySelector: (Long) -> K): M { + for (element in this) { + destination.put(keySelector(element), element) + } + return destination +} /** * Populates and returns the [destination] mutable map with key-value pairs, @@ -4123,7 +6678,12 @@ public expect inline fun > LongArray.associateB * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. */ -public expect inline fun > FloatArray.associateByTo(destination: M, keySelector: (Float) -> K): M +public inline fun > FloatArray.associateByTo(destination: M, keySelector: (Float) -> K): M { + for (element in this) { + destination.put(keySelector(element), element) + } + return destination +} /** * Populates and returns the [destination] mutable map with key-value pairs, @@ -4132,7 +6692,12 @@ public expect inline fun > FloatArray.associat * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. */ -public expect inline fun > DoubleArray.associateByTo(destination: M, keySelector: (Double) -> K): M +public inline fun > DoubleArray.associateByTo(destination: M, keySelector: (Double) -> K): M { + for (element in this) { + destination.put(keySelector(element), element) + } + return destination +} /** * Populates and returns the [destination] mutable map with key-value pairs, @@ -4141,7 +6706,12 @@ public expect inline fun > DoubleArray.associ * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. */ -public expect inline fun > BooleanArray.associateByTo(destination: M, keySelector: (Boolean) -> K): M +public inline fun > BooleanArray.associateByTo(destination: M, keySelector: (Boolean) -> K): M { + for (element in this) { + destination.put(keySelector(element), element) + } + return destination +} /** * Populates and returns the [destination] mutable map with key-value pairs, @@ -4150,7 +6720,12 @@ public expect inline fun > BooleanArray.asso * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. */ -public expect inline fun > CharArray.associateByTo(destination: M, keySelector: (Char) -> K): M +public inline fun > CharArray.associateByTo(destination: M, keySelector: (Char) -> K): M { + for (element in this) { + destination.put(keySelector(element), element) + } + return destination +} /** * Populates and returns the [destination] mutable map with key-value pairs, @@ -4159,7 +6734,12 @@ public expect inline fun > CharArray.associateB * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. */ -public expect inline fun > Array.associateByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M +public inline fun > Array.associateByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M { + for (element in this) { + destination.put(keySelector(element), valueTransform(element)) + } + return destination +} /** * Populates and returns the [destination] mutable map with key-value pairs, @@ -4168,7 +6748,12 @@ public expect inline fun > Array.asso * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. */ -public expect inline fun > ByteArray.associateByTo(destination: M, keySelector: (Byte) -> K, valueTransform: (Byte) -> V): M +public inline fun > ByteArray.associateByTo(destination: M, keySelector: (Byte) -> K, valueTransform: (Byte) -> V): M { + for (element in this) { + destination.put(keySelector(element), valueTransform(element)) + } + return destination +} /** * Populates and returns the [destination] mutable map with key-value pairs, @@ -4177,7 +6762,12 @@ public expect inline fun > ByteArray.associateB * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. */ -public expect inline fun > ShortArray.associateByTo(destination: M, keySelector: (Short) -> K, valueTransform: (Short) -> V): M +public inline fun > ShortArray.associateByTo(destination: M, keySelector: (Short) -> K, valueTransform: (Short) -> V): M { + for (element in this) { + destination.put(keySelector(element), valueTransform(element)) + } + return destination +} /** * Populates and returns the [destination] mutable map with key-value pairs, @@ -4186,7 +6776,12 @@ public expect inline fun > ShortArray.associate * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. */ -public expect inline fun > IntArray.associateByTo(destination: M, keySelector: (Int) -> K, valueTransform: (Int) -> V): M +public inline fun > IntArray.associateByTo(destination: M, keySelector: (Int) -> K, valueTransform: (Int) -> V): M { + for (element in this) { + destination.put(keySelector(element), valueTransform(element)) + } + return destination +} /** * Populates and returns the [destination] mutable map with key-value pairs, @@ -4195,7 +6790,12 @@ public expect inline fun > IntArray.associateBy * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. */ -public expect inline fun > LongArray.associateByTo(destination: M, keySelector: (Long) -> K, valueTransform: (Long) -> V): M +public inline fun > LongArray.associateByTo(destination: M, keySelector: (Long) -> K, valueTransform: (Long) -> V): M { + for (element in this) { + destination.put(keySelector(element), valueTransform(element)) + } + return destination +} /** * Populates and returns the [destination] mutable map with key-value pairs, @@ -4204,7 +6804,12 @@ public expect inline fun > LongArray.associateB * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. */ -public expect inline fun > FloatArray.associateByTo(destination: M, keySelector: (Float) -> K, valueTransform: (Float) -> V): M +public inline fun > FloatArray.associateByTo(destination: M, keySelector: (Float) -> K, valueTransform: (Float) -> V): M { + for (element in this) { + destination.put(keySelector(element), valueTransform(element)) + } + return destination +} /** * Populates and returns the [destination] mutable map with key-value pairs, @@ -4213,7 +6818,12 @@ public expect inline fun > FloatArray.associate * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. */ -public expect inline fun > DoubleArray.associateByTo(destination: M, keySelector: (Double) -> K, valueTransform: (Double) -> V): M +public inline fun > DoubleArray.associateByTo(destination: M, keySelector: (Double) -> K, valueTransform: (Double) -> V): M { + for (element in this) { + destination.put(keySelector(element), valueTransform(element)) + } + return destination +} /** * Populates and returns the [destination] mutable map with key-value pairs, @@ -4222,7 +6832,12 @@ public expect inline fun > DoubleArray.associat * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. */ -public expect inline fun > BooleanArray.associateByTo(destination: M, keySelector: (Boolean) -> K, valueTransform: (Boolean) -> V): M +public inline fun > BooleanArray.associateByTo(destination: M, keySelector: (Boolean) -> K, valueTransform: (Boolean) -> V): M { + for (element in this) { + destination.put(keySelector(element), valueTransform(element)) + } + return destination +} /** * Populates and returns the [destination] mutable map with key-value pairs, @@ -4231,7 +6846,12 @@ public expect inline fun > BooleanArray.associa * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. */ -public expect inline fun > CharArray.associateByTo(destination: M, keySelector: (Char) -> K, valueTransform: (Char) -> V): M +public inline fun > CharArray.associateByTo(destination: M, keySelector: (Char) -> K, valueTransform: (Char) -> V): M { + for (element in this) { + destination.put(keySelector(element), valueTransform(element)) + } + return destination +} /** * Populates and returns the [destination] mutable map with key-value pairs @@ -4239,7 +6859,12 @@ public expect inline fun > CharArray.associateB * * If any of two pairs would have the same key the last one gets added to the map. */ -public expect inline fun > Array.associateTo(destination: M, transform: (T) -> Pair): M +public inline fun > Array.associateTo(destination: M, transform: (T) -> Pair): M { + for (element in this) { + destination += transform(element) + } + return destination +} /** * Populates and returns the [destination] mutable map with key-value pairs @@ -4247,7 +6872,12 @@ public expect inline fun > Array.asso * * If any of two pairs would have the same key the last one gets added to the map. */ -public expect inline fun > ByteArray.associateTo(destination: M, transform: (Byte) -> Pair): M +public inline fun > ByteArray.associateTo(destination: M, transform: (Byte) -> Pair): M { + for (element in this) { + destination += transform(element) + } + return destination +} /** * Populates and returns the [destination] mutable map with key-value pairs @@ -4255,7 +6885,12 @@ public expect inline fun > ByteArray.associateT * * If any of two pairs would have the same key the last one gets added to the map. */ -public expect inline fun > ShortArray.associateTo(destination: M, transform: (Short) -> Pair): M +public inline fun > ShortArray.associateTo(destination: M, transform: (Short) -> Pair): M { + for (element in this) { + destination += transform(element) + } + return destination +} /** * Populates and returns the [destination] mutable map with key-value pairs @@ -4263,7 +6898,12 @@ public expect inline fun > ShortArray.associate * * If any of two pairs would have the same key the last one gets added to the map. */ -public expect inline fun > IntArray.associateTo(destination: M, transform: (Int) -> Pair): M +public inline fun > IntArray.associateTo(destination: M, transform: (Int) -> Pair): M { + for (element in this) { + destination += transform(element) + } + return destination +} /** * Populates and returns the [destination] mutable map with key-value pairs @@ -4271,7 +6911,12 @@ public expect inline fun > IntArray.associateTo * * If any of two pairs would have the same key the last one gets added to the map. */ -public expect inline fun > LongArray.associateTo(destination: M, transform: (Long) -> Pair): M +public inline fun > LongArray.associateTo(destination: M, transform: (Long) -> Pair): M { + for (element in this) { + destination += transform(element) + } + return destination +} /** * Populates and returns the [destination] mutable map with key-value pairs @@ -4279,7 +6924,12 @@ public expect inline fun > LongArray.associateT * * If any of two pairs would have the same key the last one gets added to the map. */ -public expect inline fun > FloatArray.associateTo(destination: M, transform: (Float) -> Pair): M +public inline fun > FloatArray.associateTo(destination: M, transform: (Float) -> Pair): M { + for (element in this) { + destination += transform(element) + } + return destination +} /** * Populates and returns the [destination] mutable map with key-value pairs @@ -4287,7 +6937,12 @@ public expect inline fun > FloatArray.associate * * If any of two pairs would have the same key the last one gets added to the map. */ -public expect inline fun > DoubleArray.associateTo(destination: M, transform: (Double) -> Pair): M +public inline fun > DoubleArray.associateTo(destination: M, transform: (Double) -> Pair): M { + for (element in this) { + destination += transform(element) + } + return destination +} /** * Populates and returns the [destination] mutable map with key-value pairs @@ -4295,7 +6950,12 @@ public expect inline fun > DoubleArray.associat * * If any of two pairs would have the same key the last one gets added to the map. */ -public expect inline fun > BooleanArray.associateTo(destination: M, transform: (Boolean) -> Pair): M +public inline fun > BooleanArray.associateTo(destination: M, transform: (Boolean) -> Pair): M { + for (element in this) { + destination += transform(element) + } + return destination +} /** * Populates and returns the [destination] mutable map with key-value pairs @@ -4303,340 +6963,622 @@ public expect inline fun > BooleanArray.associa * * If any of two pairs would have the same key the last one gets added to the map. */ -public expect inline fun > CharArray.associateTo(destination: M, transform: (Char) -> Pair): M +public inline fun > CharArray.associateTo(destination: M, transform: (Char) -> Pair): M { + for (element in this) { + destination += transform(element) + } + return destination +} /** * Appends all elements to the given [destination] collection. */ -public expect fun > Array.toCollection(destination: C): C +public fun > Array.toCollection(destination: C): C { + for (item in this) { + destination.add(item) + } + return destination +} /** * Appends all elements to the given [destination] collection. */ -public expect fun > ByteArray.toCollection(destination: C): C +public fun > ByteArray.toCollection(destination: C): C { + for (item in this) { + destination.add(item) + } + return destination +} /** * Appends all elements to the given [destination] collection. */ -public expect fun > ShortArray.toCollection(destination: C): C +public fun > ShortArray.toCollection(destination: C): C { + for (item in this) { + destination.add(item) + } + return destination +} /** * Appends all elements to the given [destination] collection. */ -public expect fun > IntArray.toCollection(destination: C): C +public fun > IntArray.toCollection(destination: C): C { + for (item in this) { + destination.add(item) + } + return destination +} /** * Appends all elements to the given [destination] collection. */ -public expect fun > LongArray.toCollection(destination: C): C +public fun > LongArray.toCollection(destination: C): C { + for (item in this) { + destination.add(item) + } + return destination +} /** * Appends all elements to the given [destination] collection. */ -public expect fun > FloatArray.toCollection(destination: C): C +public fun > FloatArray.toCollection(destination: C): C { + for (item in this) { + destination.add(item) + } + return destination +} /** * Appends all elements to the given [destination] collection. */ -public expect fun > DoubleArray.toCollection(destination: C): C +public fun > DoubleArray.toCollection(destination: C): C { + for (item in this) { + destination.add(item) + } + return destination +} /** * Appends all elements to the given [destination] collection. */ -public expect fun > BooleanArray.toCollection(destination: C): C +public fun > BooleanArray.toCollection(destination: C): C { + for (item in this) { + destination.add(item) + } + return destination +} /** * Appends all elements to the given [destination] collection. */ -public expect fun > CharArray.toCollection(destination: C): C +public fun > CharArray.toCollection(destination: C): C { + for (item in this) { + destination.add(item) + } + return destination +} /** * Returns a [HashSet] of all elements. */ -public expect fun Array.toHashSet(): HashSet +public fun Array.toHashSet(): HashSet { + return toCollection(HashSet(mapCapacity(size))) +} /** * Returns a [HashSet] of all elements. */ -public expect fun ByteArray.toHashSet(): HashSet +public fun ByteArray.toHashSet(): HashSet { + return toCollection(HashSet(mapCapacity(size))) +} /** * Returns a [HashSet] of all elements. */ -public expect fun ShortArray.toHashSet(): HashSet +public fun ShortArray.toHashSet(): HashSet { + return toCollection(HashSet(mapCapacity(size))) +} /** * Returns a [HashSet] of all elements. */ -public expect fun IntArray.toHashSet(): HashSet +public fun IntArray.toHashSet(): HashSet { + return toCollection(HashSet(mapCapacity(size))) +} /** * Returns a [HashSet] of all elements. */ -public expect fun LongArray.toHashSet(): HashSet +public fun LongArray.toHashSet(): HashSet { + return toCollection(HashSet(mapCapacity(size))) +} /** * Returns a [HashSet] of all elements. */ -public expect fun FloatArray.toHashSet(): HashSet +public fun FloatArray.toHashSet(): HashSet { + return toCollection(HashSet(mapCapacity(size))) +} /** * Returns a [HashSet] of all elements. */ -public expect fun DoubleArray.toHashSet(): HashSet +public fun DoubleArray.toHashSet(): HashSet { + return toCollection(HashSet(mapCapacity(size))) +} /** * Returns a [HashSet] of all elements. */ -public expect fun BooleanArray.toHashSet(): HashSet +public fun BooleanArray.toHashSet(): HashSet { + return toCollection(HashSet(mapCapacity(size))) +} /** * Returns a [HashSet] of all elements. */ -public expect fun CharArray.toHashSet(): HashSet +public fun CharArray.toHashSet(): HashSet { + return toCollection(HashSet(mapCapacity(size))) +} /** * Returns a [List] containing all elements. */ -public expect fun Array.toList(): List +public fun Array.toList(): List { + return when (size) { + 0 -> emptyList() + 1 -> listOf(this[0]) + else -> this.toMutableList() + } +} /** * Returns a [List] containing all elements. */ -public expect fun ByteArray.toList(): List +public fun ByteArray.toList(): List { + return when (size) { + 0 -> emptyList() + 1 -> listOf(this[0]) + else -> this.toMutableList() + } +} /** * Returns a [List] containing all elements. */ -public expect fun ShortArray.toList(): List +public fun ShortArray.toList(): List { + return when (size) { + 0 -> emptyList() + 1 -> listOf(this[0]) + else -> this.toMutableList() + } +} /** * Returns a [List] containing all elements. */ -public expect fun IntArray.toList(): List +public fun IntArray.toList(): List { + return when (size) { + 0 -> emptyList() + 1 -> listOf(this[0]) + else -> this.toMutableList() + } +} /** * Returns a [List] containing all elements. */ -public expect fun LongArray.toList(): List +public fun LongArray.toList(): List { + return when (size) { + 0 -> emptyList() + 1 -> listOf(this[0]) + else -> this.toMutableList() + } +} /** * Returns a [List] containing all elements. */ -public expect fun FloatArray.toList(): List +public fun FloatArray.toList(): List { + return when (size) { + 0 -> emptyList() + 1 -> listOf(this[0]) + else -> this.toMutableList() + } +} /** * Returns a [List] containing all elements. */ -public expect fun DoubleArray.toList(): List +public fun DoubleArray.toList(): List { + return when (size) { + 0 -> emptyList() + 1 -> listOf(this[0]) + else -> this.toMutableList() + } +} /** * Returns a [List] containing all elements. */ -public expect fun BooleanArray.toList(): List +public fun BooleanArray.toList(): List { + return when (size) { + 0 -> emptyList() + 1 -> listOf(this[0]) + else -> this.toMutableList() + } +} /** * Returns a [List] containing all elements. */ -public expect fun CharArray.toList(): List +public fun CharArray.toList(): List { + return when (size) { + 0 -> emptyList() + 1 -> listOf(this[0]) + else -> this.toMutableList() + } +} /** * Returns a [MutableList] filled with all elements of this array. */ -public expect fun Array.toMutableList(): MutableList +public fun Array.toMutableList(): MutableList { + return ArrayList(this.asCollection()) +} /** * Returns a [MutableList] filled with all elements of this array. */ -public expect fun ByteArray.toMutableList(): MutableList +public fun ByteArray.toMutableList(): MutableList { + val list = ArrayList(size) + for (item in this) list.add(item) + return list +} /** * Returns a [MutableList] filled with all elements of this array. */ -public expect fun ShortArray.toMutableList(): MutableList +public fun ShortArray.toMutableList(): MutableList { + val list = ArrayList(size) + for (item in this) list.add(item) + return list +} /** * Returns a [MutableList] filled with all elements of this array. */ -public expect fun IntArray.toMutableList(): MutableList +public fun IntArray.toMutableList(): MutableList { + val list = ArrayList(size) + for (item in this) list.add(item) + return list +} /** * Returns a [MutableList] filled with all elements of this array. */ -public expect fun LongArray.toMutableList(): MutableList +public fun LongArray.toMutableList(): MutableList { + val list = ArrayList(size) + for (item in this) list.add(item) + return list +} /** * Returns a [MutableList] filled with all elements of this array. */ -public expect fun FloatArray.toMutableList(): MutableList +public fun FloatArray.toMutableList(): MutableList { + val list = ArrayList(size) + for (item in this) list.add(item) + return list +} /** * Returns a [MutableList] filled with all elements of this array. */ -public expect fun DoubleArray.toMutableList(): MutableList +public fun DoubleArray.toMutableList(): MutableList { + val list = ArrayList(size) + for (item in this) list.add(item) + return list +} /** * Returns a [MutableList] filled with all elements of this array. */ -public expect fun BooleanArray.toMutableList(): MutableList +public fun BooleanArray.toMutableList(): MutableList { + val list = ArrayList(size) + for (item in this) list.add(item) + return list +} /** * Returns a [MutableList] filled with all elements of this array. */ -public expect fun CharArray.toMutableList(): MutableList +public fun CharArray.toMutableList(): MutableList { + val list = ArrayList(size) + for (item in this) list.add(item) + return list +} /** * Returns a [Set] of all elements. * * The returned set preserves the element iteration order of the original array. */ -public expect fun Array.toSet(): Set +public fun Array.toSet(): Set { + return when (size) { + 0 -> emptySet() + 1 -> setOf(this[0]) + else -> toCollection(LinkedHashSet(mapCapacity(size))) + } +} /** * Returns a [Set] of all elements. * * The returned set preserves the element iteration order of the original array. */ -public expect fun ByteArray.toSet(): Set +public fun ByteArray.toSet(): Set { + return when (size) { + 0 -> emptySet() + 1 -> setOf(this[0]) + else -> toCollection(LinkedHashSet(mapCapacity(size))) + } +} /** * Returns a [Set] of all elements. * * The returned set preserves the element iteration order of the original array. */ -public expect fun ShortArray.toSet(): Set +public fun ShortArray.toSet(): Set { + return when (size) { + 0 -> emptySet() + 1 -> setOf(this[0]) + else -> toCollection(LinkedHashSet(mapCapacity(size))) + } +} /** * Returns a [Set] of all elements. * * The returned set preserves the element iteration order of the original array. */ -public expect fun IntArray.toSet(): Set +public fun IntArray.toSet(): Set { + return when (size) { + 0 -> emptySet() + 1 -> setOf(this[0]) + else -> toCollection(LinkedHashSet(mapCapacity(size))) + } +} /** * Returns a [Set] of all elements. * * The returned set preserves the element iteration order of the original array. */ -public expect fun LongArray.toSet(): Set +public fun LongArray.toSet(): Set { + return when (size) { + 0 -> emptySet() + 1 -> setOf(this[0]) + else -> toCollection(LinkedHashSet(mapCapacity(size))) + } +} /** * Returns a [Set] of all elements. * * The returned set preserves the element iteration order of the original array. */ -public expect fun FloatArray.toSet(): Set +public fun FloatArray.toSet(): Set { + return when (size) { + 0 -> emptySet() + 1 -> setOf(this[0]) + else -> toCollection(LinkedHashSet(mapCapacity(size))) + } +} /** * Returns a [Set] of all elements. * * The returned set preserves the element iteration order of the original array. */ -public expect fun DoubleArray.toSet(): Set +public fun DoubleArray.toSet(): Set { + return when (size) { + 0 -> emptySet() + 1 -> setOf(this[0]) + else -> toCollection(LinkedHashSet(mapCapacity(size))) + } +} /** * Returns a [Set] of all elements. * * The returned set preserves the element iteration order of the original array. */ -public expect fun BooleanArray.toSet(): Set +public fun BooleanArray.toSet(): Set { + return when (size) { + 0 -> emptySet() + 1 -> setOf(this[0]) + else -> toCollection(LinkedHashSet(mapCapacity(size))) + } +} /** * Returns a [Set] of all elements. * * The returned set preserves the element iteration order of the original array. */ -public expect fun CharArray.toSet(): Set +public fun CharArray.toSet(): Set { + return when (size) { + 0 -> emptySet() + 1 -> setOf(this[0]) + else -> toCollection(LinkedHashSet(mapCapacity(size))) + } +} /** * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array. */ -public expect inline fun Array.flatMap(transform: (T) -> Iterable): List +public inline fun Array.flatMap(transform: (T) -> Iterable): List { + return flatMapTo(ArrayList(), transform) +} /** * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array. */ -public expect inline fun ByteArray.flatMap(transform: (Byte) -> Iterable): List +public inline fun ByteArray.flatMap(transform: (Byte) -> Iterable): List { + return flatMapTo(ArrayList(), transform) +} /** * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array. */ -public expect inline fun ShortArray.flatMap(transform: (Short) -> Iterable): List +public inline fun ShortArray.flatMap(transform: (Short) -> Iterable): List { + return flatMapTo(ArrayList(), transform) +} /** * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array. */ -public expect inline fun IntArray.flatMap(transform: (Int) -> Iterable): List +public inline fun IntArray.flatMap(transform: (Int) -> Iterable): List { + return flatMapTo(ArrayList(), transform) +} /** * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array. */ -public expect inline fun LongArray.flatMap(transform: (Long) -> Iterable): List +public inline fun LongArray.flatMap(transform: (Long) -> Iterable): List { + return flatMapTo(ArrayList(), transform) +} /** * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array. */ -public expect inline fun FloatArray.flatMap(transform: (Float) -> Iterable): List +public inline fun FloatArray.flatMap(transform: (Float) -> Iterable): List { + return flatMapTo(ArrayList(), transform) +} /** * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array. */ -public expect inline fun DoubleArray.flatMap(transform: (Double) -> Iterable): List +public inline fun DoubleArray.flatMap(transform: (Double) -> Iterable): List { + return flatMapTo(ArrayList(), transform) +} /** * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array. */ -public expect inline fun BooleanArray.flatMap(transform: (Boolean) -> Iterable): List +public inline fun BooleanArray.flatMap(transform: (Boolean) -> Iterable): List { + return flatMapTo(ArrayList(), transform) +} /** * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array. */ -public expect inline fun CharArray.flatMap(transform: (Char) -> Iterable): List +public inline fun CharArray.flatMap(transform: (Char) -> Iterable): List { + return flatMapTo(ArrayList(), transform) +} /** * Appends all elements yielded from results of [transform] function being invoked on each element of original array, to the given [destination]. */ -public expect inline fun > Array.flatMapTo(destination: C, transform: (T) -> Iterable): C +public inline fun > Array.flatMapTo(destination: C, transform: (T) -> Iterable): C { + for (element in this) { + val list = transform(element) + destination.addAll(list) + } + return destination +} /** * Appends all elements yielded from results of [transform] function being invoked on each element of original array, to the given [destination]. */ -public expect inline fun > ByteArray.flatMapTo(destination: C, transform: (Byte) -> Iterable): C +public inline fun > ByteArray.flatMapTo(destination: C, transform: (Byte) -> Iterable): C { + for (element in this) { + val list = transform(element) + destination.addAll(list) + } + return destination +} /** * Appends all elements yielded from results of [transform] function being invoked on each element of original array, to the given [destination]. */ -public expect inline fun > ShortArray.flatMapTo(destination: C, transform: (Short) -> Iterable): C +public inline fun > ShortArray.flatMapTo(destination: C, transform: (Short) -> Iterable): C { + for (element in this) { + val list = transform(element) + destination.addAll(list) + } + return destination +} /** * Appends all elements yielded from results of [transform] function being invoked on each element of original array, to the given [destination]. */ -public expect inline fun > IntArray.flatMapTo(destination: C, transform: (Int) -> Iterable): C +public inline fun > IntArray.flatMapTo(destination: C, transform: (Int) -> Iterable): C { + for (element in this) { + val list = transform(element) + destination.addAll(list) + } + return destination +} /** * Appends all elements yielded from results of [transform] function being invoked on each element of original array, to the given [destination]. */ -public expect inline fun > LongArray.flatMapTo(destination: C, transform: (Long) -> Iterable): C +public inline fun > LongArray.flatMapTo(destination: C, transform: (Long) -> Iterable): C { + for (element in this) { + val list = transform(element) + destination.addAll(list) + } + return destination +} /** * Appends all elements yielded from results of [transform] function being invoked on each element of original array, to the given [destination]. */ -public expect inline fun > FloatArray.flatMapTo(destination: C, transform: (Float) -> Iterable): C +public inline fun > FloatArray.flatMapTo(destination: C, transform: (Float) -> Iterable): C { + for (element in this) { + val list = transform(element) + destination.addAll(list) + } + return destination +} /** * Appends all elements yielded from results of [transform] function being invoked on each element of original array, to the given [destination]. */ -public expect inline fun > DoubleArray.flatMapTo(destination: C, transform: (Double) -> Iterable): C +public inline fun > DoubleArray.flatMapTo(destination: C, transform: (Double) -> Iterable): C { + for (element in this) { + val list = transform(element) + destination.addAll(list) + } + return destination +} /** * Appends all elements yielded from results of [transform] function being invoked on each element of original array, to the given [destination]. */ -public expect inline fun > BooleanArray.flatMapTo(destination: C, transform: (Boolean) -> Iterable): C +public inline fun > BooleanArray.flatMapTo(destination: C, transform: (Boolean) -> Iterable): C { + for (element in this) { + val list = transform(element) + destination.addAll(list) + } + return destination +} /** * Appends all elements yielded from results of [transform] function being invoked on each element of original array, to the given [destination]. */ -public expect inline fun > CharArray.flatMapTo(destination: C, transform: (Char) -> Iterable): C +public inline fun > CharArray.flatMapTo(destination: C, transform: (Char) -> Iterable): C { + for (element in this) { + val list = transform(element) + destination.addAll(list) + } + return destination +} /** * Groups elements of the original array by the key returned by the given [keySelector] function @@ -4646,7 +7588,9 @@ public expect inline fun > CharArray.flatMapTo(de * * @sample samples.collections.Collections.Transformations.groupBy */ -public expect inline fun Array.groupBy(keySelector: (T) -> K): Map> +public inline fun Array.groupBy(keySelector: (T) -> K): Map> { + return groupByTo(LinkedHashMap>(), keySelector) +} /** * Groups elements of the original array by the key returned by the given [keySelector] function @@ -4656,7 +7600,9 @@ public expect inline fun Array.groupBy(keySelector: (T) -> K): Map * * @sample samples.collections.Collections.Transformations.groupBy */ -public expect inline fun ByteArray.groupBy(keySelector: (Byte) -> K): Map> +public inline fun ByteArray.groupBy(keySelector: (Byte) -> K): Map> { + return groupByTo(LinkedHashMap>(), keySelector) +} /** * Groups elements of the original array by the key returned by the given [keySelector] function @@ -4666,7 +7612,9 @@ public expect inline fun ByteArray.groupBy(keySelector: (Byte) -> K): Map ShortArray.groupBy(keySelector: (Short) -> K): Map> +public inline fun ShortArray.groupBy(keySelector: (Short) -> K): Map> { + return groupByTo(LinkedHashMap>(), keySelector) +} /** * Groups elements of the original array by the key returned by the given [keySelector] function @@ -4676,7 +7624,9 @@ public expect inline fun ShortArray.groupBy(keySelector: (Short) -> K): Map< * * @sample samples.collections.Collections.Transformations.groupBy */ -public expect inline fun IntArray.groupBy(keySelector: (Int) -> K): Map> +public inline fun IntArray.groupBy(keySelector: (Int) -> K): Map> { + return groupByTo(LinkedHashMap>(), keySelector) +} /** * Groups elements of the original array by the key returned by the given [keySelector] function @@ -4686,7 +7636,9 @@ public expect inline fun IntArray.groupBy(keySelector: (Int) -> K): Map LongArray.groupBy(keySelector: (Long) -> K): Map> +public inline fun LongArray.groupBy(keySelector: (Long) -> K): Map> { + return groupByTo(LinkedHashMap>(), keySelector) +} /** * Groups elements of the original array by the key returned by the given [keySelector] function @@ -4696,7 +7648,9 @@ public expect inline fun LongArray.groupBy(keySelector: (Long) -> K): Map FloatArray.groupBy(keySelector: (Float) -> K): Map> +public inline fun FloatArray.groupBy(keySelector: (Float) -> K): Map> { + return groupByTo(LinkedHashMap>(), keySelector) +} /** * Groups elements of the original array by the key returned by the given [keySelector] function @@ -4706,7 +7660,9 @@ public expect inline fun FloatArray.groupBy(keySelector: (Float) -> K): Map< * * @sample samples.collections.Collections.Transformations.groupBy */ -public expect inline fun DoubleArray.groupBy(keySelector: (Double) -> K): Map> +public inline fun DoubleArray.groupBy(keySelector: (Double) -> K): Map> { + return groupByTo(LinkedHashMap>(), keySelector) +} /** * Groups elements of the original array by the key returned by the given [keySelector] function @@ -4716,7 +7672,9 @@ public expect inline fun DoubleArray.groupBy(keySelector: (Double) -> K): Ma * * @sample samples.collections.Collections.Transformations.groupBy */ -public expect inline fun BooleanArray.groupBy(keySelector: (Boolean) -> K): Map> +public inline fun BooleanArray.groupBy(keySelector: (Boolean) -> K): Map> { + return groupByTo(LinkedHashMap>(), keySelector) +} /** * Groups elements of the original array by the key returned by the given [keySelector] function @@ -4726,7 +7684,9 @@ public expect inline fun BooleanArray.groupBy(keySelector: (Boolean) -> K): * * @sample samples.collections.Collections.Transformations.groupBy */ -public expect inline fun CharArray.groupBy(keySelector: (Char) -> K): Map> +public inline fun CharArray.groupBy(keySelector: (Char) -> K): Map> { + return groupByTo(LinkedHashMap>(), keySelector) +} /** * Groups values returned by the [valueTransform] function applied to each element of the original array @@ -4737,7 +7697,9 @@ public expect inline fun CharArray.groupBy(keySelector: (Char) -> K): Map Array.groupBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map> +public inline fun Array.groupBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map> { + return groupByTo(LinkedHashMap>(), keySelector, valueTransform) +} /** * Groups values returned by the [valueTransform] function applied to each element of the original array @@ -4748,7 +7710,9 @@ public expect inline fun Array.groupBy(keySelector: (T) -> K, v * * @sample samples.collections.Collections.Transformations.groupByKeysAndValues */ -public expect inline fun ByteArray.groupBy(keySelector: (Byte) -> K, valueTransform: (Byte) -> V): Map> +public inline fun ByteArray.groupBy(keySelector: (Byte) -> K, valueTransform: (Byte) -> V): Map> { + return groupByTo(LinkedHashMap>(), keySelector, valueTransform) +} /** * Groups values returned by the [valueTransform] function applied to each element of the original array @@ -4759,7 +7723,9 @@ public expect inline fun ByteArray.groupBy(keySelector: (Byte) -> K, valu * * @sample samples.collections.Collections.Transformations.groupByKeysAndValues */ -public expect inline fun ShortArray.groupBy(keySelector: (Short) -> K, valueTransform: (Short) -> V): Map> +public inline fun ShortArray.groupBy(keySelector: (Short) -> K, valueTransform: (Short) -> V): Map> { + return groupByTo(LinkedHashMap>(), keySelector, valueTransform) +} /** * Groups values returned by the [valueTransform] function applied to each element of the original array @@ -4770,7 +7736,9 @@ public expect inline fun ShortArray.groupBy(keySelector: (Short) -> K, va * * @sample samples.collections.Collections.Transformations.groupByKeysAndValues */ -public expect inline fun IntArray.groupBy(keySelector: (Int) -> K, valueTransform: (Int) -> V): Map> +public inline fun IntArray.groupBy(keySelector: (Int) -> K, valueTransform: (Int) -> V): Map> { + return groupByTo(LinkedHashMap>(), keySelector, valueTransform) +} /** * Groups values returned by the [valueTransform] function applied to each element of the original array @@ -4781,7 +7749,9 @@ public expect inline fun IntArray.groupBy(keySelector: (Int) -> K, valueT * * @sample samples.collections.Collections.Transformations.groupByKeysAndValues */ -public expect inline fun LongArray.groupBy(keySelector: (Long) -> K, valueTransform: (Long) -> V): Map> +public inline fun LongArray.groupBy(keySelector: (Long) -> K, valueTransform: (Long) -> V): Map> { + return groupByTo(LinkedHashMap>(), keySelector, valueTransform) +} /** * Groups values returned by the [valueTransform] function applied to each element of the original array @@ -4792,7 +7762,9 @@ public expect inline fun LongArray.groupBy(keySelector: (Long) -> K, valu * * @sample samples.collections.Collections.Transformations.groupByKeysAndValues */ -public expect inline fun FloatArray.groupBy(keySelector: (Float) -> K, valueTransform: (Float) -> V): Map> +public inline fun FloatArray.groupBy(keySelector: (Float) -> K, valueTransform: (Float) -> V): Map> { + return groupByTo(LinkedHashMap>(), keySelector, valueTransform) +} /** * Groups values returned by the [valueTransform] function applied to each element of the original array @@ -4803,7 +7775,9 @@ public expect inline fun FloatArray.groupBy(keySelector: (Float) -> K, va * * @sample samples.collections.Collections.Transformations.groupByKeysAndValues */ -public expect inline fun DoubleArray.groupBy(keySelector: (Double) -> K, valueTransform: (Double) -> V): Map> +public inline fun DoubleArray.groupBy(keySelector: (Double) -> K, valueTransform: (Double) -> V): Map> { + return groupByTo(LinkedHashMap>(), keySelector, valueTransform) +} /** * Groups values returned by the [valueTransform] function applied to each element of the original array @@ -4814,7 +7788,9 @@ public expect inline fun DoubleArray.groupBy(keySelector: (Double) -> K, * * @sample samples.collections.Collections.Transformations.groupByKeysAndValues */ -public expect inline fun BooleanArray.groupBy(keySelector: (Boolean) -> K, valueTransform: (Boolean) -> V): Map> +public inline fun BooleanArray.groupBy(keySelector: (Boolean) -> K, valueTransform: (Boolean) -> V): Map> { + return groupByTo(LinkedHashMap>(), keySelector, valueTransform) +} /** * Groups values returned by the [valueTransform] function applied to each element of the original array @@ -4825,7 +7801,9 @@ public expect inline fun BooleanArray.groupBy(keySelector: (Boolean) -> K * * @sample samples.collections.Collections.Transformations.groupByKeysAndValues */ -public expect inline fun CharArray.groupBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map> +public inline fun CharArray.groupBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map> { + return groupByTo(LinkedHashMap>(), keySelector, valueTransform) +} /** * Groups elements of the original array by the key returned by the given [keySelector] function @@ -4835,7 +7813,14 @@ public expect inline fun CharArray.groupBy(keySelector: (Char) -> K, valu * * @sample samples.collections.Collections.Transformations.groupBy */ -public expect inline fun >> Array.groupByTo(destination: M, keySelector: (T) -> K): M +public inline fun >> Array.groupByTo(destination: M, keySelector: (T) -> K): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(element) + } + return destination +} /** * Groups elements of the original array by the key returned by the given [keySelector] function @@ -4845,7 +7830,14 @@ public expect inline fun >> Array>> ByteArray.groupByTo(destination: M, keySelector: (Byte) -> K): M +public inline fun >> ByteArray.groupByTo(destination: M, keySelector: (Byte) -> K): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(element) + } + return destination +} /** * Groups elements of the original array by the key returned by the given [keySelector] function @@ -4855,7 +7847,14 @@ public expect inline fun >> ByteArray. * * @sample samples.collections.Collections.Transformations.groupBy */ -public expect inline fun >> ShortArray.groupByTo(destination: M, keySelector: (Short) -> K): M +public inline fun >> ShortArray.groupByTo(destination: M, keySelector: (Short) -> K): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(element) + } + return destination +} /** * Groups elements of the original array by the key returned by the given [keySelector] function @@ -4865,7 +7864,14 @@ public expect inline fun >> ShortArra * * @sample samples.collections.Collections.Transformations.groupBy */ -public expect inline fun >> IntArray.groupByTo(destination: M, keySelector: (Int) -> K): M +public inline fun >> IntArray.groupByTo(destination: M, keySelector: (Int) -> K): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(element) + } + return destination +} /** * Groups elements of the original array by the key returned by the given [keySelector] function @@ -4875,7 +7881,14 @@ public expect inline fun >> IntArray.gr * * @sample samples.collections.Collections.Transformations.groupBy */ -public expect inline fun >> LongArray.groupByTo(destination: M, keySelector: (Long) -> K): M +public inline fun >> LongArray.groupByTo(destination: M, keySelector: (Long) -> K): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(element) + } + return destination +} /** * Groups elements of the original array by the key returned by the given [keySelector] function @@ -4885,7 +7898,14 @@ public expect inline fun >> LongArray. * * @sample samples.collections.Collections.Transformations.groupBy */ -public expect inline fun >> FloatArray.groupByTo(destination: M, keySelector: (Float) -> K): M +public inline fun >> FloatArray.groupByTo(destination: M, keySelector: (Float) -> K): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(element) + } + return destination +} /** * Groups elements of the original array by the key returned by the given [keySelector] function @@ -4895,7 +7915,14 @@ public expect inline fun >> FloatArra * * @sample samples.collections.Collections.Transformations.groupBy */ -public expect inline fun >> DoubleArray.groupByTo(destination: M, keySelector: (Double) -> K): M +public inline fun >> DoubleArray.groupByTo(destination: M, keySelector: (Double) -> K): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(element) + } + return destination +} /** * Groups elements of the original array by the key returned by the given [keySelector] function @@ -4905,7 +7932,14 @@ public expect inline fun >> DoubleAr * * @sample samples.collections.Collections.Transformations.groupBy */ -public expect inline fun >> BooleanArray.groupByTo(destination: M, keySelector: (Boolean) -> K): M +public inline fun >> BooleanArray.groupByTo(destination: M, keySelector: (Boolean) -> K): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(element) + } + return destination +} /** * Groups elements of the original array by the key returned by the given [keySelector] function @@ -4915,7 +7949,14 @@ public expect inline fun >> Boolean * * @sample samples.collections.Collections.Transformations.groupBy */ -public expect inline fun >> CharArray.groupByTo(destination: M, keySelector: (Char) -> K): M +public inline fun >> CharArray.groupByTo(destination: M, keySelector: (Char) -> K): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(element) + } + return destination +} /** * Groups values returned by the [valueTransform] function applied to each element of the original array @@ -4926,7 +7967,14 @@ public expect inline fun >> CharArray. * * @sample samples.collections.Collections.Transformations.groupByKeysAndValues */ -public expect inline fun >> Array.groupByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M +public inline fun >> Array.groupByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(valueTransform(element)) + } + return destination +} /** * Groups values returned by the [valueTransform] function applied to each element of the original array @@ -4937,7 +7985,14 @@ public expect inline fun >> Array>> ByteArray.groupByTo(destination: M, keySelector: (Byte) -> K, valueTransform: (Byte) -> V): M +public inline fun >> ByteArray.groupByTo(destination: M, keySelector: (Byte) -> K, valueTransform: (Byte) -> V): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(valueTransform(element)) + } + return destination +} /** * Groups values returned by the [valueTransform] function applied to each element of the original array @@ -4948,7 +8003,14 @@ public expect inline fun >> ByteArray. * * @sample samples.collections.Collections.Transformations.groupByKeysAndValues */ -public expect inline fun >> ShortArray.groupByTo(destination: M, keySelector: (Short) -> K, valueTransform: (Short) -> V): M +public inline fun >> ShortArray.groupByTo(destination: M, keySelector: (Short) -> K, valueTransform: (Short) -> V): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(valueTransform(element)) + } + return destination +} /** * Groups values returned by the [valueTransform] function applied to each element of the original array @@ -4959,7 +8021,14 @@ public expect inline fun >> ShortArray * * @sample samples.collections.Collections.Transformations.groupByKeysAndValues */ -public expect inline fun >> IntArray.groupByTo(destination: M, keySelector: (Int) -> K, valueTransform: (Int) -> V): M +public inline fun >> IntArray.groupByTo(destination: M, keySelector: (Int) -> K, valueTransform: (Int) -> V): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(valueTransform(element)) + } + return destination +} /** * Groups values returned by the [valueTransform] function applied to each element of the original array @@ -4970,7 +8039,14 @@ public expect inline fun >> IntArray.g * * @sample samples.collections.Collections.Transformations.groupByKeysAndValues */ -public expect inline fun >> LongArray.groupByTo(destination: M, keySelector: (Long) -> K, valueTransform: (Long) -> V): M +public inline fun >> LongArray.groupByTo(destination: M, keySelector: (Long) -> K, valueTransform: (Long) -> V): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(valueTransform(element)) + } + return destination +} /** * Groups values returned by the [valueTransform] function applied to each element of the original array @@ -4981,7 +8057,14 @@ public expect inline fun >> LongArray. * * @sample samples.collections.Collections.Transformations.groupByKeysAndValues */ -public expect inline fun >> FloatArray.groupByTo(destination: M, keySelector: (Float) -> K, valueTransform: (Float) -> V): M +public inline fun >> FloatArray.groupByTo(destination: M, keySelector: (Float) -> K, valueTransform: (Float) -> V): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(valueTransform(element)) + } + return destination +} /** * Groups values returned by the [valueTransform] function applied to each element of the original array @@ -4992,7 +8075,14 @@ public expect inline fun >> FloatArray * * @sample samples.collections.Collections.Transformations.groupByKeysAndValues */ -public expect inline fun >> DoubleArray.groupByTo(destination: M, keySelector: (Double) -> K, valueTransform: (Double) -> V): M +public inline fun >> DoubleArray.groupByTo(destination: M, keySelector: (Double) -> K, valueTransform: (Double) -> V): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(valueTransform(element)) + } + return destination +} /** * Groups values returned by the [valueTransform] function applied to each element of the original array @@ -5003,7 +8093,14 @@ public expect inline fun >> DoubleArra * * @sample samples.collections.Collections.Transformations.groupByKeysAndValues */ -public expect inline fun >> BooleanArray.groupByTo(destination: M, keySelector: (Boolean) -> K, valueTransform: (Boolean) -> V): M +public inline fun >> BooleanArray.groupByTo(destination: M, keySelector: (Boolean) -> K, valueTransform: (Boolean) -> V): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(valueTransform(element)) + } + return destination +} /** * Groups values returned by the [valueTransform] function applied to each element of the original array @@ -5014,7 +8111,14 @@ public expect inline fun >> BooleanArr * * @sample samples.collections.Collections.Transformations.groupByKeysAndValues */ -public expect inline fun >> CharArray.groupByTo(destination: M, keySelector: (Char) -> K, valueTransform: (Char) -> V): M +public inline fun >> CharArray.groupByTo(destination: M, keySelector: (Char) -> K, valueTransform: (Char) -> V): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(valueTransform(element)) + } + return destination +} /** * Creates a [Grouping] source from an array to be used later with one of group-and-fold operations @@ -5023,61 +8127,84 @@ public expect inline fun >> CharArray. * @sample samples.collections.Collections.Transformations.groupingByEachCount */ @SinceKotlin("1.1") -public expect inline fun Array.groupingBy(crossinline keySelector: (T) -> K): Grouping +public inline fun Array.groupingBy(crossinline keySelector: (T) -> K): Grouping { + return object : Grouping { + override fun sourceIterator(): Iterator = this@groupingBy.iterator() + override fun keyOf(element: T): K = keySelector(element) + } +} /** * Returns a list containing the results of applying the given [transform] function * to each element in the original array. */ -public expect inline fun Array.map(transform: (T) -> R): List +public inline fun Array.map(transform: (T) -> R): List { + return mapTo(ArrayList(size), transform) +} /** * Returns a list containing the results of applying the given [transform] function * to each element in the original array. */ -public expect inline fun ByteArray.map(transform: (Byte) -> R): List +public inline fun ByteArray.map(transform: (Byte) -> R): List { + return mapTo(ArrayList(size), transform) +} /** * Returns a list containing the results of applying the given [transform] function * to each element in the original array. */ -public expect inline fun ShortArray.map(transform: (Short) -> R): List +public inline fun ShortArray.map(transform: (Short) -> R): List { + return mapTo(ArrayList(size), transform) +} /** * Returns a list containing the results of applying the given [transform] function * to each element in the original array. */ -public expect inline fun IntArray.map(transform: (Int) -> R): List +public inline fun IntArray.map(transform: (Int) -> R): List { + return mapTo(ArrayList(size), transform) +} /** * Returns a list containing the results of applying the given [transform] function * to each element in the original array. */ -public expect inline fun LongArray.map(transform: (Long) -> R): List +public inline fun LongArray.map(transform: (Long) -> R): List { + return mapTo(ArrayList(size), transform) +} /** * Returns a list containing the results of applying the given [transform] function * to each element in the original array. */ -public expect inline fun FloatArray.map(transform: (Float) -> R): List +public inline fun FloatArray.map(transform: (Float) -> R): List { + return mapTo(ArrayList(size), transform) +} /** * Returns a list containing the results of applying the given [transform] function * to each element in the original array. */ -public expect inline fun DoubleArray.map(transform: (Double) -> R): List +public inline fun DoubleArray.map(transform: (Double) -> R): List { + return mapTo(ArrayList(size), transform) +} /** * Returns a list containing the results of applying the given [transform] function * to each element in the original array. */ -public expect inline fun BooleanArray.map(transform: (Boolean) -> R): List +public inline fun BooleanArray.map(transform: (Boolean) -> R): List { + return mapTo(ArrayList(size), transform) +} /** * Returns a list containing the results of applying the given [transform] function * to each element in the original array. */ -public expect inline fun CharArray.map(transform: (Char) -> R): List +public inline fun CharArray.map(transform: (Char) -> R): List { + return mapTo(ArrayList(size), transform) +} /** * Returns a list containing the results of applying the given [transform] function @@ -5085,7 +8212,9 @@ public expect inline fun CharArray.map(transform: (Char) -> R): List * @param [transform] function that takes the index of an element and the element itself * and returns the result of the transform applied to the element. */ -public expect inline fun Array.mapIndexed(transform: (index: Int, T) -> R): List +public inline fun Array.mapIndexed(transform: (index: Int, T) -> R): List { + return mapIndexedTo(ArrayList(size), transform) +} /** * Returns a list containing the results of applying the given [transform] function @@ -5093,7 +8222,9 @@ public expect inline fun Array.mapIndexed(transform: (index: Int, * @param [transform] function that takes the index of an element and the element itself * and returns the result of the transform applied to the element. */ -public expect inline fun ByteArray.mapIndexed(transform: (index: Int, Byte) -> R): List +public inline fun ByteArray.mapIndexed(transform: (index: Int, Byte) -> R): List { + return mapIndexedTo(ArrayList(size), transform) +} /** * Returns a list containing the results of applying the given [transform] function @@ -5101,7 +8232,9 @@ public expect inline fun ByteArray.mapIndexed(transform: (index: Int, Byte) * @param [transform] function that takes the index of an element and the element itself * and returns the result of the transform applied to the element. */ -public expect inline fun ShortArray.mapIndexed(transform: (index: Int, Short) -> R): List +public inline fun ShortArray.mapIndexed(transform: (index: Int, Short) -> R): List { + return mapIndexedTo(ArrayList(size), transform) +} /** * Returns a list containing the results of applying the given [transform] function @@ -5109,7 +8242,9 @@ public expect inline fun ShortArray.mapIndexed(transform: (index: Int, Short * @param [transform] function that takes the index of an element and the element itself * and returns the result of the transform applied to the element. */ -public expect inline fun IntArray.mapIndexed(transform: (index: Int, Int) -> R): List +public inline fun IntArray.mapIndexed(transform: (index: Int, Int) -> R): List { + return mapIndexedTo(ArrayList(size), transform) +} /** * Returns a list containing the results of applying the given [transform] function @@ -5117,7 +8252,9 @@ public expect inline fun IntArray.mapIndexed(transform: (index: Int, Int) -> * @param [transform] function that takes the index of an element and the element itself * and returns the result of the transform applied to the element. */ -public expect inline fun LongArray.mapIndexed(transform: (index: Int, Long) -> R): List +public inline fun LongArray.mapIndexed(transform: (index: Int, Long) -> R): List { + return mapIndexedTo(ArrayList(size), transform) +} /** * Returns a list containing the results of applying the given [transform] function @@ -5125,7 +8262,9 @@ public expect inline fun LongArray.mapIndexed(transform: (index: Int, Long) * @param [transform] function that takes the index of an element and the element itself * and returns the result of the transform applied to the element. */ -public expect inline fun FloatArray.mapIndexed(transform: (index: Int, Float) -> R): List +public inline fun FloatArray.mapIndexed(transform: (index: Int, Float) -> R): List { + return mapIndexedTo(ArrayList(size), transform) +} /** * Returns a list containing the results of applying the given [transform] function @@ -5133,7 +8272,9 @@ public expect inline fun FloatArray.mapIndexed(transform: (index: Int, Float * @param [transform] function that takes the index of an element and the element itself * and returns the result of the transform applied to the element. */ -public expect inline fun DoubleArray.mapIndexed(transform: (index: Int, Double) -> R): List +public inline fun DoubleArray.mapIndexed(transform: (index: Int, Double) -> R): List { + return mapIndexedTo(ArrayList(size), transform) +} /** * Returns a list containing the results of applying the given [transform] function @@ -5141,7 +8282,9 @@ public expect inline fun DoubleArray.mapIndexed(transform: (index: Int, Doub * @param [transform] function that takes the index of an element and the element itself * and returns the result of the transform applied to the element. */ -public expect inline fun BooleanArray.mapIndexed(transform: (index: Int, Boolean) -> R): List +public inline fun BooleanArray.mapIndexed(transform: (index: Int, Boolean) -> R): List { + return mapIndexedTo(ArrayList(size), transform) +} /** * Returns a list containing the results of applying the given [transform] function @@ -5149,7 +8292,9 @@ public expect inline fun BooleanArray.mapIndexed(transform: (index: Int, Boo * @param [transform] function that takes the index of an element and the element itself * and returns the result of the transform applied to the element. */ -public expect inline fun CharArray.mapIndexed(transform: (index: Int, Char) -> R): List +public inline fun CharArray.mapIndexed(transform: (index: Int, Char) -> R): List { + return mapIndexedTo(ArrayList(size), transform) +} /** * Returns a list containing only the non-null results of applying the given [transform] function @@ -5157,7 +8302,9 @@ public expect inline fun CharArray.mapIndexed(transform: (index: Int, Char) * @param [transform] function that takes the index of an element and the element itself * and returns the result of the transform applied to the element. */ -public expect inline fun Array.mapIndexedNotNull(transform: (index: Int, T) -> R?): List +public inline fun Array.mapIndexedNotNull(transform: (index: Int, T) -> R?): List { + return mapIndexedNotNullTo(ArrayList(), transform) +} /** * Applies the given [transform] function to each element and its index in the original array @@ -5165,7 +8312,10 @@ public expect inline fun Array.mapIndexedNotNull(transform: * @param [transform] function that takes the index of an element and the element itself * and returns the result of the transform applied to the element. */ -public expect inline fun > Array.mapIndexedNotNullTo(destination: C, transform: (index: Int, T) -> R?): C +public inline fun > Array.mapIndexedNotNullTo(destination: C, transform: (index: Int, T) -> R?): C { + forEachIndexed { index, element -> transform(index, element)?.let { destination.add(it) } } + return destination +} /** * Applies the given [transform] function to each element and its index in the original array @@ -5173,7 +8323,12 @@ public expect inline fun > Array. * @param [transform] function that takes the index of an element and the element itself * and returns the result of the transform applied to the element. */ -public expect inline fun > Array.mapIndexedTo(destination: C, transform: (index: Int, T) -> R): C +public inline fun > Array.mapIndexedTo(destination: C, transform: (index: Int, T) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} /** * Applies the given [transform] function to each element and its index in the original array @@ -5181,7 +8336,12 @@ public expect inline fun > Array.mapInd * @param [transform] function that takes the index of an element and the element itself * and returns the result of the transform applied to the element. */ -public expect inline fun > ByteArray.mapIndexedTo(destination: C, transform: (index: Int, Byte) -> R): C +public inline fun > ByteArray.mapIndexedTo(destination: C, transform: (index: Int, Byte) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} /** * Applies the given [transform] function to each element and its index in the original array @@ -5189,7 +8349,12 @@ public expect inline fun > ByteArray.mapIndexedTo * @param [transform] function that takes the index of an element and the element itself * and returns the result of the transform applied to the element. */ -public expect inline fun > ShortArray.mapIndexedTo(destination: C, transform: (index: Int, Short) -> R): C +public inline fun > ShortArray.mapIndexedTo(destination: C, transform: (index: Int, Short) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} /** * Applies the given [transform] function to each element and its index in the original array @@ -5197,7 +8362,12 @@ public expect inline fun > ShortArray.mapIndexedT * @param [transform] function that takes the index of an element and the element itself * and returns the result of the transform applied to the element. */ -public expect inline fun > IntArray.mapIndexedTo(destination: C, transform: (index: Int, Int) -> R): C +public inline fun > IntArray.mapIndexedTo(destination: C, transform: (index: Int, Int) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} /** * Applies the given [transform] function to each element and its index in the original array @@ -5205,7 +8375,12 @@ public expect inline fun > IntArray.mapIndexedTo( * @param [transform] function that takes the index of an element and the element itself * and returns the result of the transform applied to the element. */ -public expect inline fun > LongArray.mapIndexedTo(destination: C, transform: (index: Int, Long) -> R): C +public inline fun > LongArray.mapIndexedTo(destination: C, transform: (index: Int, Long) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} /** * Applies the given [transform] function to each element and its index in the original array @@ -5213,7 +8388,12 @@ public expect inline fun > LongArray.mapIndexedTo * @param [transform] function that takes the index of an element and the element itself * and returns the result of the transform applied to the element. */ -public expect inline fun > FloatArray.mapIndexedTo(destination: C, transform: (index: Int, Float) -> R): C +public inline fun > FloatArray.mapIndexedTo(destination: C, transform: (index: Int, Float) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} /** * Applies the given [transform] function to each element and its index in the original array @@ -5221,7 +8401,12 @@ public expect inline fun > FloatArray.mapIndexedT * @param [transform] function that takes the index of an element and the element itself * and returns the result of the transform applied to the element. */ -public expect inline fun > DoubleArray.mapIndexedTo(destination: C, transform: (index: Int, Double) -> R): C +public inline fun > DoubleArray.mapIndexedTo(destination: C, transform: (index: Int, Double) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} /** * Applies the given [transform] function to each element and its index in the original array @@ -5229,7 +8414,12 @@ public expect inline fun > DoubleArray.mapIndexed * @param [transform] function that takes the index of an element and the element itself * and returns the result of the transform applied to the element. */ -public expect inline fun > BooleanArray.mapIndexedTo(destination: C, transform: (index: Int, Boolean) -> R): C +public inline fun > BooleanArray.mapIndexedTo(destination: C, transform: (index: Int, Boolean) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} /** * Applies the given [transform] function to each element and its index in the original array @@ -5237,181 +8427,263 @@ public expect inline fun > BooleanArray.mapIndexe * @param [transform] function that takes the index of an element and the element itself * and returns the result of the transform applied to the element. */ -public expect inline fun > CharArray.mapIndexedTo(destination: C, transform: (index: Int, Char) -> R): C +public inline fun > CharArray.mapIndexedTo(destination: C, transform: (index: Int, Char) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} /** * Returns a list containing only the non-null results of applying the given [transform] function * to each element in the original array. */ -public expect inline fun Array.mapNotNull(transform: (T) -> R?): List +public inline fun Array.mapNotNull(transform: (T) -> R?): List { + return mapNotNullTo(ArrayList(), transform) +} /** * Applies the given [transform] function to each element in the original array * and appends only the non-null results to the given [destination]. */ -public expect inline fun > Array.mapNotNullTo(destination: C, transform: (T) -> R?): C +public inline fun > Array.mapNotNullTo(destination: C, transform: (T) -> R?): C { + forEach { element -> transform(element)?.let { destination.add(it) } } + return destination +} /** * Applies the given [transform] function to each element of the original array * and appends the results to the given [destination]. */ -public expect inline fun > Array.mapTo(destination: C, transform: (T) -> R): C +public inline fun > Array.mapTo(destination: C, transform: (T) -> R): C { + for (item in this) + destination.add(transform(item)) + return destination +} /** * Applies the given [transform] function to each element of the original array * and appends the results to the given [destination]. */ -public expect inline fun > ByteArray.mapTo(destination: C, transform: (Byte) -> R): C +public inline fun > ByteArray.mapTo(destination: C, transform: (Byte) -> R): C { + for (item in this) + destination.add(transform(item)) + return destination +} /** * Applies the given [transform] function to each element of the original array * and appends the results to the given [destination]. */ -public expect inline fun > ShortArray.mapTo(destination: C, transform: (Short) -> R): C +public inline fun > ShortArray.mapTo(destination: C, transform: (Short) -> R): C { + for (item in this) + destination.add(transform(item)) + return destination +} /** * Applies the given [transform] function to each element of the original array * and appends the results to the given [destination]. */ -public expect inline fun > IntArray.mapTo(destination: C, transform: (Int) -> R): C +public inline fun > IntArray.mapTo(destination: C, transform: (Int) -> R): C { + for (item in this) + destination.add(transform(item)) + return destination +} /** * Applies the given [transform] function to each element of the original array * and appends the results to the given [destination]. */ -public expect inline fun > LongArray.mapTo(destination: C, transform: (Long) -> R): C +public inline fun > LongArray.mapTo(destination: C, transform: (Long) -> R): C { + for (item in this) + destination.add(transform(item)) + return destination +} /** * Applies the given [transform] function to each element of the original array * and appends the results to the given [destination]. */ -public expect inline fun > FloatArray.mapTo(destination: C, transform: (Float) -> R): C +public inline fun > FloatArray.mapTo(destination: C, transform: (Float) -> R): C { + for (item in this) + destination.add(transform(item)) + return destination +} /** * Applies the given [transform] function to each element of the original array * and appends the results to the given [destination]. */ -public expect inline fun > DoubleArray.mapTo(destination: C, transform: (Double) -> R): C +public inline fun > DoubleArray.mapTo(destination: C, transform: (Double) -> R): C { + for (item in this) + destination.add(transform(item)) + return destination +} /** * Applies the given [transform] function to each element of the original array * and appends the results to the given [destination]. */ -public expect inline fun > BooleanArray.mapTo(destination: C, transform: (Boolean) -> R): C +public inline fun > BooleanArray.mapTo(destination: C, transform: (Boolean) -> R): C { + for (item in this) + destination.add(transform(item)) + return destination +} /** * Applies the given [transform] function to each element of the original array * and appends the results to the given [destination]. */ -public expect inline fun > CharArray.mapTo(destination: C, transform: (Char) -> R): C +public inline fun > CharArray.mapTo(destination: C, transform: (Char) -> R): C { + for (item in this) + destination.add(transform(item)) + return destination +} /** * Returns a lazy [Iterable] of [IndexedValue] for each element of the original array. */ -public expect fun Array.withIndex(): Iterable> +public fun Array.withIndex(): Iterable> { + return IndexingIterable { iterator() } +} /** * Returns a lazy [Iterable] of [IndexedValue] for each element of the original array. */ -public expect fun ByteArray.withIndex(): Iterable> +public fun ByteArray.withIndex(): Iterable> { + return IndexingIterable { iterator() } +} /** * Returns a lazy [Iterable] of [IndexedValue] for each element of the original array. */ -public expect fun ShortArray.withIndex(): Iterable> +public fun ShortArray.withIndex(): Iterable> { + return IndexingIterable { iterator() } +} /** * Returns a lazy [Iterable] of [IndexedValue] for each element of the original array. */ -public expect fun IntArray.withIndex(): Iterable> +public fun IntArray.withIndex(): Iterable> { + return IndexingIterable { iterator() } +} /** * Returns a lazy [Iterable] of [IndexedValue] for each element of the original array. */ -public expect fun LongArray.withIndex(): Iterable> +public fun LongArray.withIndex(): Iterable> { + return IndexingIterable { iterator() } +} /** * Returns a lazy [Iterable] of [IndexedValue] for each element of the original array. */ -public expect fun FloatArray.withIndex(): Iterable> +public fun FloatArray.withIndex(): Iterable> { + return IndexingIterable { iterator() } +} /** * Returns a lazy [Iterable] of [IndexedValue] for each element of the original array. */ -public expect fun DoubleArray.withIndex(): Iterable> +public fun DoubleArray.withIndex(): Iterable> { + return IndexingIterable { iterator() } +} /** * Returns a lazy [Iterable] of [IndexedValue] for each element of the original array. */ -public expect fun BooleanArray.withIndex(): Iterable> +public fun BooleanArray.withIndex(): Iterable> { + return IndexingIterable { iterator() } +} /** * Returns a lazy [Iterable] of [IndexedValue] for each element of the original array. */ -public expect fun CharArray.withIndex(): Iterable> +public fun CharArray.withIndex(): Iterable> { + return IndexingIterable { iterator() } +} /** * Returns a list containing only distinct elements from the given array. * * The elements in the resulting list are in the same order as they were in the source array. */ -public expect fun Array.distinct(): List +public fun Array.distinct(): List { + return this.toMutableSet().toList() +} /** * Returns a list containing only distinct elements from the given array. * * The elements in the resulting list are in the same order as they were in the source array. */ -public expect fun ByteArray.distinct(): List +public fun ByteArray.distinct(): List { + return this.toMutableSet().toList() +} /** * Returns a list containing only distinct elements from the given array. * * The elements in the resulting list are in the same order as they were in the source array. */ -public expect fun ShortArray.distinct(): List +public fun ShortArray.distinct(): List { + return this.toMutableSet().toList() +} /** * Returns a list containing only distinct elements from the given array. * * The elements in the resulting list are in the same order as they were in the source array. */ -public expect fun IntArray.distinct(): List +public fun IntArray.distinct(): List { + return this.toMutableSet().toList() +} /** * Returns a list containing only distinct elements from the given array. * * The elements in the resulting list are in the same order as they were in the source array. */ -public expect fun LongArray.distinct(): List +public fun LongArray.distinct(): List { + return this.toMutableSet().toList() +} /** * Returns a list containing only distinct elements from the given array. * * The elements in the resulting list are in the same order as they were in the source array. */ -public expect fun FloatArray.distinct(): List +public fun FloatArray.distinct(): List { + return this.toMutableSet().toList() +} /** * Returns a list containing only distinct elements from the given array. * * The elements in the resulting list are in the same order as they were in the source array. */ -public expect fun DoubleArray.distinct(): List +public fun DoubleArray.distinct(): List { + return this.toMutableSet().toList() +} /** * Returns a list containing only distinct elements from the given array. * * The elements in the resulting list are in the same order as they were in the source array. */ -public expect fun BooleanArray.distinct(): List +public fun BooleanArray.distinct(): List { + return this.toMutableSet().toList() +} /** * Returns a list containing only distinct elements from the given array. * * The elements in the resulting list are in the same order as they were in the source array. */ -public expect fun CharArray.distinct(): List +public fun CharArray.distinct(): List { + return this.toMutableSet().toList() +} /** * Returns a list containing only elements from the given array @@ -5419,7 +8691,16 @@ public expect fun CharArray.distinct(): List * * The elements in the resulting list are in the same order as they were in the source array. */ -public expect inline fun Array.distinctBy(selector: (T) -> K): List +public inline fun Array.distinctBy(selector: (T) -> K): List { + val set = HashSet() + val list = ArrayList() + for (e in this) { + val key = selector(e) + if (set.add(key)) + list.add(e) + } + return list +} /** * Returns a list containing only elements from the given array @@ -5427,7 +8708,16 @@ public expect inline fun Array.distinctBy(selector: (T) -> K): Lis * * The elements in the resulting list are in the same order as they were in the source array. */ -public expect inline fun ByteArray.distinctBy(selector: (Byte) -> K): List +public inline fun ByteArray.distinctBy(selector: (Byte) -> K): List { + val set = HashSet() + val list = ArrayList() + for (e in this) { + val key = selector(e) + if (set.add(key)) + list.add(e) + } + return list +} /** * Returns a list containing only elements from the given array @@ -5435,7 +8725,16 @@ public expect inline fun ByteArray.distinctBy(selector: (Byte) -> K): List ShortArray.distinctBy(selector: (Short) -> K): List +public inline fun ShortArray.distinctBy(selector: (Short) -> K): List { + val set = HashSet() + val list = ArrayList() + for (e in this) { + val key = selector(e) + if (set.add(key)) + list.add(e) + } + return list +} /** * Returns a list containing only elements from the given array @@ -5443,7 +8742,16 @@ public expect inline fun ShortArray.distinctBy(selector: (Short) -> K): List * * The elements in the resulting list are in the same order as they were in the source array. */ -public expect inline fun IntArray.distinctBy(selector: (Int) -> K): List +public inline fun IntArray.distinctBy(selector: (Int) -> K): List { + val set = HashSet() + val list = ArrayList() + for (e in this) { + val key = selector(e) + if (set.add(key)) + list.add(e) + } + return list +} /** * Returns a list containing only elements from the given array @@ -5451,7 +8759,16 @@ public expect inline fun IntArray.distinctBy(selector: (Int) -> K): List LongArray.distinctBy(selector: (Long) -> K): List +public inline fun LongArray.distinctBy(selector: (Long) -> K): List { + val set = HashSet() + val list = ArrayList() + for (e in this) { + val key = selector(e) + if (set.add(key)) + list.add(e) + } + return list +} /** * Returns a list containing only elements from the given array @@ -5459,7 +8776,16 @@ public expect inline fun LongArray.distinctBy(selector: (Long) -> K): List FloatArray.distinctBy(selector: (Float) -> K): List +public inline fun FloatArray.distinctBy(selector: (Float) -> K): List { + val set = HashSet() + val list = ArrayList() + for (e in this) { + val key = selector(e) + if (set.add(key)) + list.add(e) + } + return list +} /** * Returns a list containing only elements from the given array @@ -5467,7 +8793,16 @@ public expect inline fun FloatArray.distinctBy(selector: (Float) -> K): List * * The elements in the resulting list are in the same order as they were in the source array. */ -public expect inline fun DoubleArray.distinctBy(selector: (Double) -> K): List +public inline fun DoubleArray.distinctBy(selector: (Double) -> K): List { + val set = HashSet() + val list = ArrayList() + for (e in this) { + val key = selector(e) + if (set.add(key)) + list.add(e) + } + return list +} /** * Returns a list containing only elements from the given array @@ -5475,7 +8810,16 @@ public expect inline fun DoubleArray.distinctBy(selector: (Double) -> K): Li * * The elements in the resulting list are in the same order as they were in the source array. */ -public expect inline fun BooleanArray.distinctBy(selector: (Boolean) -> K): List +public inline fun BooleanArray.distinctBy(selector: (Boolean) -> K): List { + val set = HashSet() + val list = ArrayList() + for (e in this) { + val key = selector(e) + if (set.add(key)) + list.add(e) + } + return list +} /** * Returns a list containing only elements from the given array @@ -5483,196 +8827,313 @@ public expect inline fun BooleanArray.distinctBy(selector: (Boolean) -> K): * * The elements in the resulting list are in the same order as they were in the source array. */ -public expect inline fun CharArray.distinctBy(selector: (Char) -> K): List +public inline fun CharArray.distinctBy(selector: (Char) -> K): List { + val set = HashSet() + val list = ArrayList() + for (e in this) { + val key = selector(e) + if (set.add(key)) + list.add(e) + } + return list +} /** * Returns a set containing all elements that are contained by both this set and the specified collection. * * The returned set preserves the element iteration order of the original array. */ -public expect infix fun Array.intersect(other: Iterable): Set +public infix fun Array.intersect(other: Iterable): Set { + val set = this.toMutableSet() + set.retainAll(other) + return set +} /** * Returns a set containing all elements that are contained by both this set and the specified collection. * * The returned set preserves the element iteration order of the original array. */ -public expect infix fun ByteArray.intersect(other: Iterable): Set +public infix fun ByteArray.intersect(other: Iterable): Set { + val set = this.toMutableSet() + set.retainAll(other) + return set +} /** * Returns a set containing all elements that are contained by both this set and the specified collection. * * The returned set preserves the element iteration order of the original array. */ -public expect infix fun ShortArray.intersect(other: Iterable): Set +public infix fun ShortArray.intersect(other: Iterable): Set { + val set = this.toMutableSet() + set.retainAll(other) + return set +} /** * Returns a set containing all elements that are contained by both this set and the specified collection. * * The returned set preserves the element iteration order of the original array. */ -public expect infix fun IntArray.intersect(other: Iterable): Set +public infix fun IntArray.intersect(other: Iterable): Set { + val set = this.toMutableSet() + set.retainAll(other) + return set +} /** * Returns a set containing all elements that are contained by both this set and the specified collection. * * The returned set preserves the element iteration order of the original array. */ -public expect infix fun LongArray.intersect(other: Iterable): Set +public infix fun LongArray.intersect(other: Iterable): Set { + val set = this.toMutableSet() + set.retainAll(other) + return set +} /** * Returns a set containing all elements that are contained by both this set and the specified collection. * * The returned set preserves the element iteration order of the original array. */ -public expect infix fun FloatArray.intersect(other: Iterable): Set +public infix fun FloatArray.intersect(other: Iterable): Set { + val set = this.toMutableSet() + set.retainAll(other) + return set +} /** * Returns a set containing all elements that are contained by both this set and the specified collection. * * The returned set preserves the element iteration order of the original array. */ -public expect infix fun DoubleArray.intersect(other: Iterable): Set +public infix fun DoubleArray.intersect(other: Iterable): Set { + val set = this.toMutableSet() + set.retainAll(other) + return set +} /** * Returns a set containing all elements that are contained by both this set and the specified collection. * * The returned set preserves the element iteration order of the original array. */ -public expect infix fun BooleanArray.intersect(other: Iterable): Set +public infix fun BooleanArray.intersect(other: Iterable): Set { + val set = this.toMutableSet() + set.retainAll(other) + return set +} /** * Returns a set containing all elements that are contained by both this set and the specified collection. * * The returned set preserves the element iteration order of the original array. */ -public expect infix fun CharArray.intersect(other: Iterable): Set +public infix fun CharArray.intersect(other: Iterable): Set { + val set = this.toMutableSet() + set.retainAll(other) + return set +} /** * Returns a set containing all elements that are contained by this array and not contained by the specified collection. * * The returned set preserves the element iteration order of the original array. */ -public expect infix fun Array.subtract(other: Iterable): Set +public infix fun Array.subtract(other: Iterable): Set { + val set = this.toMutableSet() + set.removeAll(other) + return set +} /** * Returns a set containing all elements that are contained by this array and not contained by the specified collection. * * The returned set preserves the element iteration order of the original array. */ -public expect infix fun ByteArray.subtract(other: Iterable): Set +public infix fun ByteArray.subtract(other: Iterable): Set { + val set = this.toMutableSet() + set.removeAll(other) + return set +} /** * Returns a set containing all elements that are contained by this array and not contained by the specified collection. * * The returned set preserves the element iteration order of the original array. */ -public expect infix fun ShortArray.subtract(other: Iterable): Set +public infix fun ShortArray.subtract(other: Iterable): Set { + val set = this.toMutableSet() + set.removeAll(other) + return set +} /** * Returns a set containing all elements that are contained by this array and not contained by the specified collection. * * The returned set preserves the element iteration order of the original array. */ -public expect infix fun IntArray.subtract(other: Iterable): Set +public infix fun IntArray.subtract(other: Iterable): Set { + val set = this.toMutableSet() + set.removeAll(other) + return set +} /** * Returns a set containing all elements that are contained by this array and not contained by the specified collection. * * The returned set preserves the element iteration order of the original array. */ -public expect infix fun LongArray.subtract(other: Iterable): Set +public infix fun LongArray.subtract(other: Iterable): Set { + val set = this.toMutableSet() + set.removeAll(other) + return set +} /** * Returns a set containing all elements that are contained by this array and not contained by the specified collection. * * The returned set preserves the element iteration order of the original array. */ -public expect infix fun FloatArray.subtract(other: Iterable): Set +public infix fun FloatArray.subtract(other: Iterable): Set { + val set = this.toMutableSet() + set.removeAll(other) + return set +} /** * Returns a set containing all elements that are contained by this array and not contained by the specified collection. * * The returned set preserves the element iteration order of the original array. */ -public expect infix fun DoubleArray.subtract(other: Iterable): Set +public infix fun DoubleArray.subtract(other: Iterable): Set { + val set = this.toMutableSet() + set.removeAll(other) + return set +} /** * Returns a set containing all elements that are contained by this array and not contained by the specified collection. * * The returned set preserves the element iteration order of the original array. */ -public expect infix fun BooleanArray.subtract(other: Iterable): Set +public infix fun BooleanArray.subtract(other: Iterable): Set { + val set = this.toMutableSet() + set.removeAll(other) + return set +} /** * Returns a set containing all elements that are contained by this array and not contained by the specified collection. * * The returned set preserves the element iteration order of the original array. */ -public expect infix fun CharArray.subtract(other: Iterable): Set +public infix fun CharArray.subtract(other: Iterable): Set { + val set = this.toMutableSet() + set.removeAll(other) + return set +} /** * Returns a mutable set containing all distinct elements from the given array. * * The returned set preserves the element iteration order of the original array. */ -public expect fun Array.toMutableSet(): MutableSet +public fun Array.toMutableSet(): MutableSet { + val set = LinkedHashSet(mapCapacity(size)) + for (item in this) set.add(item) + return set +} /** * Returns a mutable set containing all distinct elements from the given array. * * The returned set preserves the element iteration order of the original array. */ -public expect fun ByteArray.toMutableSet(): MutableSet +public fun ByteArray.toMutableSet(): MutableSet { + val set = LinkedHashSet(mapCapacity(size)) + for (item in this) set.add(item) + return set +} /** * Returns a mutable set containing all distinct elements from the given array. * * The returned set preserves the element iteration order of the original array. */ -public expect fun ShortArray.toMutableSet(): MutableSet +public fun ShortArray.toMutableSet(): MutableSet { + val set = LinkedHashSet(mapCapacity(size)) + for (item in this) set.add(item) + return set +} /** * Returns a mutable set containing all distinct elements from the given array. * * The returned set preserves the element iteration order of the original array. */ -public expect fun IntArray.toMutableSet(): MutableSet +public fun IntArray.toMutableSet(): MutableSet { + val set = LinkedHashSet(mapCapacity(size)) + for (item in this) set.add(item) + return set +} /** * Returns a mutable set containing all distinct elements from the given array. * * The returned set preserves the element iteration order of the original array. */ -public expect fun LongArray.toMutableSet(): MutableSet +public fun LongArray.toMutableSet(): MutableSet { + val set = LinkedHashSet(mapCapacity(size)) + for (item in this) set.add(item) + return set +} /** * Returns a mutable set containing all distinct elements from the given array. * * The returned set preserves the element iteration order of the original array. */ -public expect fun FloatArray.toMutableSet(): MutableSet +public fun FloatArray.toMutableSet(): MutableSet { + val set = LinkedHashSet(mapCapacity(size)) + for (item in this) set.add(item) + return set +} /** * Returns a mutable set containing all distinct elements from the given array. * * The returned set preserves the element iteration order of the original array. */ -public expect fun DoubleArray.toMutableSet(): MutableSet +public fun DoubleArray.toMutableSet(): MutableSet { + val set = LinkedHashSet(mapCapacity(size)) + for (item in this) set.add(item) + return set +} /** * Returns a mutable set containing all distinct elements from the given array. * * The returned set preserves the element iteration order of the original array. */ -public expect fun BooleanArray.toMutableSet(): MutableSet +public fun BooleanArray.toMutableSet(): MutableSet { + val set = LinkedHashSet(mapCapacity(size)) + for (item in this) set.add(item) + return set +} /** * Returns a mutable set containing all distinct elements from the given array. * * The returned set preserves the element iteration order of the original array. */ -public expect fun CharArray.toMutableSet(): MutableSet +public fun CharArray.toMutableSet(): MutableSet { + val set = LinkedHashSet(mapCapacity(size)) + for (item in this) set.add(item) + return set +} /** * Returns a set containing all distinct elements from both collections. @@ -5681,7 +9142,11 @@ public expect fun CharArray.toMutableSet(): MutableSet * Those elements of the [other] collection that are unique are iterated in the end * in the order of the [other] collection. */ -public expect infix fun Array.union(other: Iterable): Set +public infix fun Array.union(other: Iterable): Set { + val set = this.toMutableSet() + set.addAll(other) + return set +} /** * Returns a set containing all distinct elements from both collections. @@ -5690,7 +9155,11 @@ public expect infix fun Array.union(other: Iterable): Set * Those elements of the [other] collection that are unique are iterated in the end * in the order of the [other] collection. */ -public expect infix fun ByteArray.union(other: Iterable): Set +public infix fun ByteArray.union(other: Iterable): Set { + val set = this.toMutableSet() + set.addAll(other) + return set +} /** * Returns a set containing all distinct elements from both collections. @@ -5699,7 +9168,11 @@ public expect infix fun ByteArray.union(other: Iterable): Set * Those elements of the [other] collection that are unique are iterated in the end * in the order of the [other] collection. */ -public expect infix fun ShortArray.union(other: Iterable): Set +public infix fun ShortArray.union(other: Iterable): Set { + val set = this.toMutableSet() + set.addAll(other) + return set +} /** * Returns a set containing all distinct elements from both collections. @@ -5708,7 +9181,11 @@ public expect infix fun ShortArray.union(other: Iterable): Set * Those elements of the [other] collection that are unique are iterated in the end * in the order of the [other] collection. */ -public expect infix fun IntArray.union(other: Iterable): Set +public infix fun IntArray.union(other: Iterable): Set { + val set = this.toMutableSet() + set.addAll(other) + return set +} /** * Returns a set containing all distinct elements from both collections. @@ -5717,7 +9194,11 @@ public expect infix fun IntArray.union(other: Iterable): Set * Those elements of the [other] collection that are unique are iterated in the end * in the order of the [other] collection. */ -public expect infix fun LongArray.union(other: Iterable): Set +public infix fun LongArray.union(other: Iterable): Set { + val set = this.toMutableSet() + set.addAll(other) + return set +} /** * Returns a set containing all distinct elements from both collections. @@ -5726,7 +9207,11 @@ public expect infix fun LongArray.union(other: Iterable): Set * Those elements of the [other] collection that are unique are iterated in the end * in the order of the [other] collection. */ -public expect infix fun FloatArray.union(other: Iterable): Set +public infix fun FloatArray.union(other: Iterable): Set { + val set = this.toMutableSet() + set.addAll(other) + return set +} /** * Returns a set containing all distinct elements from both collections. @@ -5735,7 +9220,11 @@ public expect infix fun FloatArray.union(other: Iterable): Set * Those elements of the [other] collection that are unique are iterated in the end * in the order of the [other] collection. */ -public expect infix fun DoubleArray.union(other: Iterable): Set +public infix fun DoubleArray.union(other: Iterable): Set { + val set = this.toMutableSet() + set.addAll(other) + return set +} /** * Returns a set containing all distinct elements from both collections. @@ -5744,7 +9233,11 @@ public expect infix fun DoubleArray.union(other: Iterable): Set * Those elements of the [other] collection that are unique are iterated in the end * in the order of the [other] collection. */ -public expect infix fun BooleanArray.union(other: Iterable): Set +public infix fun BooleanArray.union(other: Iterable): Set { + val set = this.toMutableSet() + set.addAll(other) + return set +} /** * Returns a set containing all distinct elements from both collections. @@ -5753,340 +9246,506 @@ public expect infix fun BooleanArray.union(other: Iterable): Set): Set +public infix fun CharArray.union(other: Iterable): Set { + val set = this.toMutableSet() + set.addAll(other) + return set +} /** * Returns `true` if all elements match the given [predicate]. * * @sample samples.collections.Collections.Aggregates.all */ -public expect inline fun Array.all(predicate: (T) -> Boolean): Boolean +public inline fun Array.all(predicate: (T) -> Boolean): Boolean { + for (element in this) if (!predicate(element)) return false + return true +} /** * Returns `true` if all elements match the given [predicate]. * * @sample samples.collections.Collections.Aggregates.all */ -public expect inline fun ByteArray.all(predicate: (Byte) -> Boolean): Boolean +public inline fun ByteArray.all(predicate: (Byte) -> Boolean): Boolean { + for (element in this) if (!predicate(element)) return false + return true +} /** * Returns `true` if all elements match the given [predicate]. * * @sample samples.collections.Collections.Aggregates.all */ -public expect inline fun ShortArray.all(predicate: (Short) -> Boolean): Boolean +public inline fun ShortArray.all(predicate: (Short) -> Boolean): Boolean { + for (element in this) if (!predicate(element)) return false + return true +} /** * Returns `true` if all elements match the given [predicate]. * * @sample samples.collections.Collections.Aggregates.all */ -public expect inline fun IntArray.all(predicate: (Int) -> Boolean): Boolean +public inline fun IntArray.all(predicate: (Int) -> Boolean): Boolean { + for (element in this) if (!predicate(element)) return false + return true +} /** * Returns `true` if all elements match the given [predicate]. * * @sample samples.collections.Collections.Aggregates.all */ -public expect inline fun LongArray.all(predicate: (Long) -> Boolean): Boolean +public inline fun LongArray.all(predicate: (Long) -> Boolean): Boolean { + for (element in this) if (!predicate(element)) return false + return true +} /** * Returns `true` if all elements match the given [predicate]. * * @sample samples.collections.Collections.Aggregates.all */ -public expect inline fun FloatArray.all(predicate: (Float) -> Boolean): Boolean +public inline fun FloatArray.all(predicate: (Float) -> Boolean): Boolean { + for (element in this) if (!predicate(element)) return false + return true +} /** * Returns `true` if all elements match the given [predicate]. * * @sample samples.collections.Collections.Aggregates.all */ -public expect inline fun DoubleArray.all(predicate: (Double) -> Boolean): Boolean +public inline fun DoubleArray.all(predicate: (Double) -> Boolean): Boolean { + for (element in this) if (!predicate(element)) return false + return true +} /** * Returns `true` if all elements match the given [predicate]. * * @sample samples.collections.Collections.Aggregates.all */ -public expect inline fun BooleanArray.all(predicate: (Boolean) -> Boolean): Boolean +public inline fun BooleanArray.all(predicate: (Boolean) -> Boolean): Boolean { + for (element in this) if (!predicate(element)) return false + return true +} /** * Returns `true` if all elements match the given [predicate]. * * @sample samples.collections.Collections.Aggregates.all */ -public expect inline fun CharArray.all(predicate: (Char) -> Boolean): Boolean +public inline fun CharArray.all(predicate: (Char) -> Boolean): Boolean { + for (element in this) if (!predicate(element)) return false + return true +} /** * Returns `true` if array has at least one element. * * @sample samples.collections.Collections.Aggregates.any */ -public expect fun Array.any(): Boolean +public fun Array.any(): Boolean { + return !isEmpty() +} /** * Returns `true` if array has at least one element. * * @sample samples.collections.Collections.Aggregates.any */ -public expect fun ByteArray.any(): Boolean +public fun ByteArray.any(): Boolean { + return !isEmpty() +} /** * Returns `true` if array has at least one element. * * @sample samples.collections.Collections.Aggregates.any */ -public expect fun ShortArray.any(): Boolean +public fun ShortArray.any(): Boolean { + return !isEmpty() +} /** * Returns `true` if array has at least one element. * * @sample samples.collections.Collections.Aggregates.any */ -public expect fun IntArray.any(): Boolean +public fun IntArray.any(): Boolean { + return !isEmpty() +} /** * Returns `true` if array has at least one element. * * @sample samples.collections.Collections.Aggregates.any */ -public expect fun LongArray.any(): Boolean +public fun LongArray.any(): Boolean { + return !isEmpty() +} /** * Returns `true` if array has at least one element. * * @sample samples.collections.Collections.Aggregates.any */ -public expect fun FloatArray.any(): Boolean +public fun FloatArray.any(): Boolean { + return !isEmpty() +} /** * Returns `true` if array has at least one element. * * @sample samples.collections.Collections.Aggregates.any */ -public expect fun DoubleArray.any(): Boolean +public fun DoubleArray.any(): Boolean { + return !isEmpty() +} /** * Returns `true` if array has at least one element. * * @sample samples.collections.Collections.Aggregates.any */ -public expect fun BooleanArray.any(): Boolean +public fun BooleanArray.any(): Boolean { + return !isEmpty() +} /** * Returns `true` if array has at least one element. * * @sample samples.collections.Collections.Aggregates.any */ -public expect fun CharArray.any(): Boolean +public fun CharArray.any(): Boolean { + return !isEmpty() +} /** * Returns `true` if at least one element matches the given [predicate]. * * @sample samples.collections.Collections.Aggregates.anyWithPredicate */ -public expect inline fun Array.any(predicate: (T) -> Boolean): Boolean +public inline fun Array.any(predicate: (T) -> Boolean): Boolean { + for (element in this) if (predicate(element)) return true + return false +} /** * Returns `true` if at least one element matches the given [predicate]. * * @sample samples.collections.Collections.Aggregates.anyWithPredicate */ -public expect inline fun ByteArray.any(predicate: (Byte) -> Boolean): Boolean +public inline fun ByteArray.any(predicate: (Byte) -> Boolean): Boolean { + for (element in this) if (predicate(element)) return true + return false +} /** * Returns `true` if at least one element matches the given [predicate]. * * @sample samples.collections.Collections.Aggregates.anyWithPredicate */ -public expect inline fun ShortArray.any(predicate: (Short) -> Boolean): Boolean +public inline fun ShortArray.any(predicate: (Short) -> Boolean): Boolean { + for (element in this) if (predicate(element)) return true + return false +} /** * Returns `true` if at least one element matches the given [predicate]. * * @sample samples.collections.Collections.Aggregates.anyWithPredicate */ -public expect inline fun IntArray.any(predicate: (Int) -> Boolean): Boolean +public inline fun IntArray.any(predicate: (Int) -> Boolean): Boolean { + for (element in this) if (predicate(element)) return true + return false +} /** * Returns `true` if at least one element matches the given [predicate]. * * @sample samples.collections.Collections.Aggregates.anyWithPredicate */ -public expect inline fun LongArray.any(predicate: (Long) -> Boolean): Boolean +public inline fun LongArray.any(predicate: (Long) -> Boolean): Boolean { + for (element in this) if (predicate(element)) return true + return false +} /** * Returns `true` if at least one element matches the given [predicate]. * * @sample samples.collections.Collections.Aggregates.anyWithPredicate */ -public expect inline fun FloatArray.any(predicate: (Float) -> Boolean): Boolean +public inline fun FloatArray.any(predicate: (Float) -> Boolean): Boolean { + for (element in this) if (predicate(element)) return true + return false +} /** * Returns `true` if at least one element matches the given [predicate]. * * @sample samples.collections.Collections.Aggregates.anyWithPredicate */ -public expect inline fun DoubleArray.any(predicate: (Double) -> Boolean): Boolean +public inline fun DoubleArray.any(predicate: (Double) -> Boolean): Boolean { + for (element in this) if (predicate(element)) return true + return false +} /** * Returns `true` if at least one element matches the given [predicate]. * * @sample samples.collections.Collections.Aggregates.anyWithPredicate */ -public expect inline fun BooleanArray.any(predicate: (Boolean) -> Boolean): Boolean +public inline fun BooleanArray.any(predicate: (Boolean) -> Boolean): Boolean { + for (element in this) if (predicate(element)) return true + return false +} /** * Returns `true` if at least one element matches the given [predicate]. * * @sample samples.collections.Collections.Aggregates.anyWithPredicate */ -public expect inline fun CharArray.any(predicate: (Char) -> Boolean): Boolean +public inline fun CharArray.any(predicate: (Char) -> Boolean): Boolean { + for (element in this) if (predicate(element)) return true + return false +} /** * Returns the number of elements in this array. */ @kotlin.internal.InlineOnly -public expect inline fun Array.count(): Int +public inline fun Array.count(): Int { + return size +} /** * Returns the number of elements in this array. */ @kotlin.internal.InlineOnly -public expect inline fun ByteArray.count(): Int +public inline fun ByteArray.count(): Int { + return size +} /** * Returns the number of elements in this array. */ @kotlin.internal.InlineOnly -public expect inline fun ShortArray.count(): Int +public inline fun ShortArray.count(): Int { + return size +} /** * Returns the number of elements in this array. */ @kotlin.internal.InlineOnly -public expect inline fun IntArray.count(): Int +public inline fun IntArray.count(): Int { + return size +} /** * Returns the number of elements in this array. */ @kotlin.internal.InlineOnly -public expect inline fun LongArray.count(): Int +public inline fun LongArray.count(): Int { + return size +} /** * Returns the number of elements in this array. */ @kotlin.internal.InlineOnly -public expect inline fun FloatArray.count(): Int +public inline fun FloatArray.count(): Int { + return size +} /** * Returns the number of elements in this array. */ @kotlin.internal.InlineOnly -public expect inline fun DoubleArray.count(): Int +public inline fun DoubleArray.count(): Int { + return size +} /** * Returns the number of elements in this array. */ @kotlin.internal.InlineOnly -public expect inline fun BooleanArray.count(): Int +public inline fun BooleanArray.count(): Int { + return size +} /** * Returns the number of elements in this array. */ @kotlin.internal.InlineOnly -public expect inline fun CharArray.count(): Int +public inline fun CharArray.count(): Int { + return size +} /** * Returns the number of elements matching the given [predicate]. */ -public expect inline fun Array.count(predicate: (T) -> Boolean): Int +public inline fun Array.count(predicate: (T) -> Boolean): Int { + var count = 0 + for (element in this) if (predicate(element)) count++ + return count +} /** * Returns the number of elements matching the given [predicate]. */ -public expect inline fun ByteArray.count(predicate: (Byte) -> Boolean): Int +public inline fun ByteArray.count(predicate: (Byte) -> Boolean): Int { + var count = 0 + for (element in this) if (predicate(element)) count++ + return count +} /** * Returns the number of elements matching the given [predicate]. */ -public expect inline fun ShortArray.count(predicate: (Short) -> Boolean): Int +public inline fun ShortArray.count(predicate: (Short) -> Boolean): Int { + var count = 0 + for (element in this) if (predicate(element)) count++ + return count +} /** * Returns the number of elements matching the given [predicate]. */ -public expect inline fun IntArray.count(predicate: (Int) -> Boolean): Int +public inline fun IntArray.count(predicate: (Int) -> Boolean): Int { + var count = 0 + for (element in this) if (predicate(element)) count++ + return count +} /** * Returns the number of elements matching the given [predicate]. */ -public expect inline fun LongArray.count(predicate: (Long) -> Boolean): Int +public inline fun LongArray.count(predicate: (Long) -> Boolean): Int { + var count = 0 + for (element in this) if (predicate(element)) count++ + return count +} /** * Returns the number of elements matching the given [predicate]. */ -public expect inline fun FloatArray.count(predicate: (Float) -> Boolean): Int +public inline fun FloatArray.count(predicate: (Float) -> Boolean): Int { + var count = 0 + for (element in this) if (predicate(element)) count++ + return count +} /** * Returns the number of elements matching the given [predicate]. */ -public expect inline fun DoubleArray.count(predicate: (Double) -> Boolean): Int +public inline fun DoubleArray.count(predicate: (Double) -> Boolean): Int { + var count = 0 + for (element in this) if (predicate(element)) count++ + return count +} /** * Returns the number of elements matching the given [predicate]. */ -public expect inline fun BooleanArray.count(predicate: (Boolean) -> Boolean): Int +public inline fun BooleanArray.count(predicate: (Boolean) -> Boolean): Int { + var count = 0 + for (element in this) if (predicate(element)) count++ + return count +} /** * Returns the number of elements matching the given [predicate]. */ -public expect inline fun CharArray.count(predicate: (Char) -> Boolean): Int +public inline fun CharArray.count(predicate: (Char) -> Boolean): Int { + var count = 0 + for (element in this) if (predicate(element)) count++ + return count +} /** * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. */ -public expect inline fun Array.fold(initial: R, operation: (acc: R, T) -> R): R +public inline fun Array.fold(initial: R, operation: (acc: R, T) -> R): R { + var accumulator = initial + for (element in this) accumulator = operation(accumulator, element) + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. */ -public expect inline fun ByteArray.fold(initial: R, operation: (acc: R, Byte) -> R): R +public inline fun ByteArray.fold(initial: R, operation: (acc: R, Byte) -> R): R { + var accumulator = initial + for (element in this) accumulator = operation(accumulator, element) + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. */ -public expect inline fun ShortArray.fold(initial: R, operation: (acc: R, Short) -> R): R +public inline fun ShortArray.fold(initial: R, operation: (acc: R, Short) -> R): R { + var accumulator = initial + for (element in this) accumulator = operation(accumulator, element) + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. */ -public expect inline fun IntArray.fold(initial: R, operation: (acc: R, Int) -> R): R +public inline fun IntArray.fold(initial: R, operation: (acc: R, Int) -> R): R { + var accumulator = initial + for (element in this) accumulator = operation(accumulator, element) + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. */ -public expect inline fun LongArray.fold(initial: R, operation: (acc: R, Long) -> R): R +public inline fun LongArray.fold(initial: R, operation: (acc: R, Long) -> R): R { + var accumulator = initial + for (element in this) accumulator = operation(accumulator, element) + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. */ -public expect inline fun FloatArray.fold(initial: R, operation: (acc: R, Float) -> R): R +public inline fun FloatArray.fold(initial: R, operation: (acc: R, Float) -> R): R { + var accumulator = initial + for (element in this) accumulator = operation(accumulator, element) + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. */ -public expect inline fun DoubleArray.fold(initial: R, operation: (acc: R, Double) -> R): R +public inline fun DoubleArray.fold(initial: R, operation: (acc: R, Double) -> R): R { + var accumulator = initial + for (element in this) accumulator = operation(accumulator, element) + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. */ -public expect inline fun BooleanArray.fold(initial: R, operation: (acc: R, Boolean) -> R): R +public inline fun BooleanArray.fold(initial: R, operation: (acc: R, Boolean) -> R): R { + var accumulator = initial + for (element in this) accumulator = operation(accumulator, element) + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. */ -public expect inline fun CharArray.fold(initial: R, operation: (acc: R, Char) -> R): R +public inline fun CharArray.fold(initial: R, operation: (acc: R, Char) -> R): R { + var accumulator = initial + for (element in this) accumulator = operation(accumulator, element) + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from left to right @@ -6094,7 +9753,12 @@ public expect inline fun CharArray.fold(initial: R, operation: (acc: R, Char * @param [operation] function that takes the index of an element, current accumulator value * and the element itself, and calculates the next accumulator value. */ -public expect inline fun Array.foldIndexed(initial: R, operation: (index: Int, acc: R, T) -> R): R +public inline fun Array.foldIndexed(initial: R, operation: (index: Int, acc: R, T) -> R): R { + var index = 0 + var accumulator = initial + for (element in this) accumulator = operation(index++, accumulator, element) + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from left to right @@ -6102,7 +9766,12 @@ public expect inline fun Array.foldIndexed(initial: R, operation: * @param [operation] function that takes the index of an element, current accumulator value * and the element itself, and calculates the next accumulator value. */ -public expect inline fun ByteArray.foldIndexed(initial: R, operation: (index: Int, acc: R, Byte) -> R): R +public inline fun ByteArray.foldIndexed(initial: R, operation: (index: Int, acc: R, Byte) -> R): R { + var index = 0 + var accumulator = initial + for (element in this) accumulator = operation(index++, accumulator, element) + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from left to right @@ -6110,7 +9779,12 @@ public expect inline fun ByteArray.foldIndexed(initial: R, operation: (index * @param [operation] function that takes the index of an element, current accumulator value * and the element itself, and calculates the next accumulator value. */ -public expect inline fun ShortArray.foldIndexed(initial: R, operation: (index: Int, acc: R, Short) -> R): R +public inline fun ShortArray.foldIndexed(initial: R, operation: (index: Int, acc: R, Short) -> R): R { + var index = 0 + var accumulator = initial + for (element in this) accumulator = operation(index++, accumulator, element) + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from left to right @@ -6118,7 +9792,12 @@ public expect inline fun ShortArray.foldIndexed(initial: R, operation: (inde * @param [operation] function that takes the index of an element, current accumulator value * and the element itself, and calculates the next accumulator value. */ -public expect inline fun IntArray.foldIndexed(initial: R, operation: (index: Int, acc: R, Int) -> R): R +public inline fun IntArray.foldIndexed(initial: R, operation: (index: Int, acc: R, Int) -> R): R { + var index = 0 + var accumulator = initial + for (element in this) accumulator = operation(index++, accumulator, element) + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from left to right @@ -6126,7 +9805,12 @@ public expect inline fun IntArray.foldIndexed(initial: R, operation: (index: * @param [operation] function that takes the index of an element, current accumulator value * and the element itself, and calculates the next accumulator value. */ -public expect inline fun LongArray.foldIndexed(initial: R, operation: (index: Int, acc: R, Long) -> R): R +public inline fun LongArray.foldIndexed(initial: R, operation: (index: Int, acc: R, Long) -> R): R { + var index = 0 + var accumulator = initial + for (element in this) accumulator = operation(index++, accumulator, element) + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from left to right @@ -6134,7 +9818,12 @@ public expect inline fun LongArray.foldIndexed(initial: R, operation: (index * @param [operation] function that takes the index of an element, current accumulator value * and the element itself, and calculates the next accumulator value. */ -public expect inline fun FloatArray.foldIndexed(initial: R, operation: (index: Int, acc: R, Float) -> R): R +public inline fun FloatArray.foldIndexed(initial: R, operation: (index: Int, acc: R, Float) -> R): R { + var index = 0 + var accumulator = initial + for (element in this) accumulator = operation(index++, accumulator, element) + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from left to right @@ -6142,7 +9831,12 @@ public expect inline fun FloatArray.foldIndexed(initial: R, operation: (inde * @param [operation] function that takes the index of an element, current accumulator value * and the element itself, and calculates the next accumulator value. */ -public expect inline fun DoubleArray.foldIndexed(initial: R, operation: (index: Int, acc: R, Double) -> R): R +public inline fun DoubleArray.foldIndexed(initial: R, operation: (index: Int, acc: R, Double) -> R): R { + var index = 0 + var accumulator = initial + for (element in this) accumulator = operation(index++, accumulator, element) + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from left to right @@ -6150,7 +9844,12 @@ public expect inline fun DoubleArray.foldIndexed(initial: R, operation: (ind * @param [operation] function that takes the index of an element, current accumulator value * and the element itself, and calculates the next accumulator value. */ -public expect inline fun BooleanArray.foldIndexed(initial: R, operation: (index: Int, acc: R, Boolean) -> R): R +public inline fun BooleanArray.foldIndexed(initial: R, operation: (index: Int, acc: R, Boolean) -> R): R { + var index = 0 + var accumulator = initial + for (element in this) accumulator = operation(index++, accumulator, element) + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from left to right @@ -6158,52 +9857,120 @@ public expect inline fun BooleanArray.foldIndexed(initial: R, operation: (in * @param [operation] function that takes the index of an element, current accumulator value * and the element itself, and calculates the next accumulator value. */ -public expect inline fun CharArray.foldIndexed(initial: R, operation: (index: Int, acc: R, Char) -> R): R +public inline fun CharArray.foldIndexed(initial: R, operation: (index: Int, acc: R, Char) -> R): R { + var index = 0 + var accumulator = initial + for (element in this) accumulator = operation(index++, accumulator, element) + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. */ -public expect inline fun Array.foldRight(initial: R, operation: (T, acc: R) -> R): R +public inline fun Array.foldRight(initial: R, operation: (T, acc: R) -> R): R { + var index = lastIndex + var accumulator = initial + while (index >= 0) { + accumulator = operation(get(index--), accumulator) + } + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. */ -public expect inline fun ByteArray.foldRight(initial: R, operation: (Byte, acc: R) -> R): R +public inline fun ByteArray.foldRight(initial: R, operation: (Byte, acc: R) -> R): R { + var index = lastIndex + var accumulator = initial + while (index >= 0) { + accumulator = operation(get(index--), accumulator) + } + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. */ -public expect inline fun ShortArray.foldRight(initial: R, operation: (Short, acc: R) -> R): R +public inline fun ShortArray.foldRight(initial: R, operation: (Short, acc: R) -> R): R { + var index = lastIndex + var accumulator = initial + while (index >= 0) { + accumulator = operation(get(index--), accumulator) + } + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. */ -public expect inline fun IntArray.foldRight(initial: R, operation: (Int, acc: R) -> R): R +public inline fun IntArray.foldRight(initial: R, operation: (Int, acc: R) -> R): R { + var index = lastIndex + var accumulator = initial + while (index >= 0) { + accumulator = operation(get(index--), accumulator) + } + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. */ -public expect inline fun LongArray.foldRight(initial: R, operation: (Long, acc: R) -> R): R +public inline fun LongArray.foldRight(initial: R, operation: (Long, acc: R) -> R): R { + var index = lastIndex + var accumulator = initial + while (index >= 0) { + accumulator = operation(get(index--), accumulator) + } + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. */ -public expect inline fun FloatArray.foldRight(initial: R, operation: (Float, acc: R) -> R): R +public inline fun FloatArray.foldRight(initial: R, operation: (Float, acc: R) -> R): R { + var index = lastIndex + var accumulator = initial + while (index >= 0) { + accumulator = operation(get(index--), accumulator) + } + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. */ -public expect inline fun DoubleArray.foldRight(initial: R, operation: (Double, acc: R) -> R): R +public inline fun DoubleArray.foldRight(initial: R, operation: (Double, acc: R) -> R): R { + var index = lastIndex + var accumulator = initial + while (index >= 0) { + accumulator = operation(get(index--), accumulator) + } + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. */ -public expect inline fun BooleanArray.foldRight(initial: R, operation: (Boolean, acc: R) -> R): R +public inline fun BooleanArray.foldRight(initial: R, operation: (Boolean, acc: R) -> R): R { + var index = lastIndex + var accumulator = initial + while (index >= 0) { + accumulator = operation(get(index--), accumulator) + } + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. */ -public expect inline fun CharArray.foldRight(initial: R, operation: (Char, acc: R) -> R): R +public inline fun CharArray.foldRight(initial: R, operation: (Char, acc: R) -> R): R { + var index = lastIndex + var accumulator = initial + while (index >= 0) { + accumulator = operation(get(index--), accumulator) + } + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from right to left @@ -6211,7 +9978,15 @@ public expect inline fun CharArray.foldRight(initial: R, operation: (Char, a * @param [operation] function that takes the index of an element, the element itself * and current accumulator value, and calculates the next accumulator value. */ -public expect inline fun Array.foldRightIndexed(initial: R, operation: (index: Int, T, acc: R) -> R): R +public inline fun Array.foldRightIndexed(initial: R, operation: (index: Int, T, acc: R) -> R): R { + var index = lastIndex + var accumulator = initial + while (index >= 0) { + accumulator = operation(index, get(index), accumulator) + --index + } + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from right to left @@ -6219,7 +9994,15 @@ public expect inline fun Array.foldRightIndexed(initial: R, operat * @param [operation] function that takes the index of an element, the element itself * and current accumulator value, and calculates the next accumulator value. */ -public expect inline fun ByteArray.foldRightIndexed(initial: R, operation: (index: Int, Byte, acc: R) -> R): R +public inline fun ByteArray.foldRightIndexed(initial: R, operation: (index: Int, Byte, acc: R) -> R): R { + var index = lastIndex + var accumulator = initial + while (index >= 0) { + accumulator = operation(index, get(index), accumulator) + --index + } + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from right to left @@ -6227,7 +10010,15 @@ public expect inline fun ByteArray.foldRightIndexed(initial: R, operation: ( * @param [operation] function that takes the index of an element, the element itself * and current accumulator value, and calculates the next accumulator value. */ -public expect inline fun ShortArray.foldRightIndexed(initial: R, operation: (index: Int, Short, acc: R) -> R): R +public inline fun ShortArray.foldRightIndexed(initial: R, operation: (index: Int, Short, acc: R) -> R): R { + var index = lastIndex + var accumulator = initial + while (index >= 0) { + accumulator = operation(index, get(index), accumulator) + --index + } + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from right to left @@ -6235,7 +10026,15 @@ public expect inline fun ShortArray.foldRightIndexed(initial: R, operation: * @param [operation] function that takes the index of an element, the element itself * and current accumulator value, and calculates the next accumulator value. */ -public expect inline fun IntArray.foldRightIndexed(initial: R, operation: (index: Int, Int, acc: R) -> R): R +public inline fun IntArray.foldRightIndexed(initial: R, operation: (index: Int, Int, acc: R) -> R): R { + var index = lastIndex + var accumulator = initial + while (index >= 0) { + accumulator = operation(index, get(index), accumulator) + --index + } + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from right to left @@ -6243,7 +10042,15 @@ public expect inline fun IntArray.foldRightIndexed(initial: R, operation: (i * @param [operation] function that takes the index of an element, the element itself * and current accumulator value, and calculates the next accumulator value. */ -public expect inline fun LongArray.foldRightIndexed(initial: R, operation: (index: Int, Long, acc: R) -> R): R +public inline fun LongArray.foldRightIndexed(initial: R, operation: (index: Int, Long, acc: R) -> R): R { + var index = lastIndex + var accumulator = initial + while (index >= 0) { + accumulator = operation(index, get(index), accumulator) + --index + } + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from right to left @@ -6251,7 +10058,15 @@ public expect inline fun LongArray.foldRightIndexed(initial: R, operation: ( * @param [operation] function that takes the index of an element, the element itself * and current accumulator value, and calculates the next accumulator value. */ -public expect inline fun FloatArray.foldRightIndexed(initial: R, operation: (index: Int, Float, acc: R) -> R): R +public inline fun FloatArray.foldRightIndexed(initial: R, operation: (index: Int, Float, acc: R) -> R): R { + var index = lastIndex + var accumulator = initial + while (index >= 0) { + accumulator = operation(index, get(index), accumulator) + --index + } + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from right to left @@ -6259,7 +10074,15 @@ public expect inline fun FloatArray.foldRightIndexed(initial: R, operation: * @param [operation] function that takes the index of an element, the element itself * and current accumulator value, and calculates the next accumulator value. */ -public expect inline fun DoubleArray.foldRightIndexed(initial: R, operation: (index: Int, Double, acc: R) -> R): R +public inline fun DoubleArray.foldRightIndexed(initial: R, operation: (index: Int, Double, acc: R) -> R): R { + var index = lastIndex + var accumulator = initial + while (index >= 0) { + accumulator = operation(index, get(index), accumulator) + --index + } + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from right to left @@ -6267,7 +10090,15 @@ public expect inline fun DoubleArray.foldRightIndexed(initial: R, operation: * @param [operation] function that takes the index of an element, the element itself * and current accumulator value, and calculates the next accumulator value. */ -public expect inline fun BooleanArray.foldRightIndexed(initial: R, operation: (index: Int, Boolean, acc: R) -> R): R +public inline fun BooleanArray.foldRightIndexed(initial: R, operation: (index: Int, Boolean, acc: R) -> R): R { + var index = lastIndex + var accumulator = initial + while (index >= 0) { + accumulator = operation(index, get(index), accumulator) + --index + } + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from right to left @@ -6275,115 +10106,168 @@ public expect inline fun BooleanArray.foldRightIndexed(initial: R, operation * @param [operation] function that takes the index of an element, the element itself * and current accumulator value, and calculates the next accumulator value. */ -public expect inline fun CharArray.foldRightIndexed(initial: R, operation: (index: Int, Char, acc: R) -> R): R +public inline fun CharArray.foldRightIndexed(initial: R, operation: (index: Int, Char, acc: R) -> R): R { + var index = lastIndex + var accumulator = initial + while (index >= 0) { + accumulator = operation(index, get(index), accumulator) + --index + } + return accumulator +} /** * Performs the given [action] on each element. */ -public expect inline fun Array.forEach(action: (T) -> Unit): Unit +public inline fun Array.forEach(action: (T) -> Unit): Unit { + for (element in this) action(element) +} /** * Performs the given [action] on each element. */ -public expect inline fun ByteArray.forEach(action: (Byte) -> Unit): Unit +public inline fun ByteArray.forEach(action: (Byte) -> Unit): Unit { + for (element in this) action(element) +} /** * Performs the given [action] on each element. */ -public expect inline fun ShortArray.forEach(action: (Short) -> Unit): Unit +public inline fun ShortArray.forEach(action: (Short) -> Unit): Unit { + for (element in this) action(element) +} /** * Performs the given [action] on each element. */ -public expect inline fun IntArray.forEach(action: (Int) -> Unit): Unit +public inline fun IntArray.forEach(action: (Int) -> Unit): Unit { + for (element in this) action(element) +} /** * Performs the given [action] on each element. */ -public expect inline fun LongArray.forEach(action: (Long) -> Unit): Unit +public inline fun LongArray.forEach(action: (Long) -> Unit): Unit { + for (element in this) action(element) +} /** * Performs the given [action] on each element. */ -public expect inline fun FloatArray.forEach(action: (Float) -> Unit): Unit +public inline fun FloatArray.forEach(action: (Float) -> Unit): Unit { + for (element in this) action(element) +} /** * Performs the given [action] on each element. */ -public expect inline fun DoubleArray.forEach(action: (Double) -> Unit): Unit +public inline fun DoubleArray.forEach(action: (Double) -> Unit): Unit { + for (element in this) action(element) +} /** * Performs the given [action] on each element. */ -public expect inline fun BooleanArray.forEach(action: (Boolean) -> Unit): Unit +public inline fun BooleanArray.forEach(action: (Boolean) -> Unit): Unit { + for (element in this) action(element) +} /** * Performs the given [action] on each element. */ -public expect inline fun CharArray.forEach(action: (Char) -> Unit): Unit +public inline fun CharArray.forEach(action: (Char) -> Unit): Unit { + for (element in this) action(element) +} /** * Performs the given [action] on each element, providing sequential index with the element. * @param [action] function that takes the index of an element and the element itself * and performs the desired action on the element. */ -public expect inline fun Array.forEachIndexed(action: (index: Int, T) -> Unit): Unit +public inline fun Array.forEachIndexed(action: (index: Int, T) -> Unit): Unit { + var index = 0 + for (item in this) action(index++, item) +} /** * Performs the given [action] on each element, providing sequential index with the element. * @param [action] function that takes the index of an element and the element itself * and performs the desired action on the element. */ -public expect inline fun ByteArray.forEachIndexed(action: (index: Int, Byte) -> Unit): Unit +public inline fun ByteArray.forEachIndexed(action: (index: Int, Byte) -> Unit): Unit { + var index = 0 + for (item in this) action(index++, item) +} /** * Performs the given [action] on each element, providing sequential index with the element. * @param [action] function that takes the index of an element and the element itself * and performs the desired action on the element. */ -public expect inline fun ShortArray.forEachIndexed(action: (index: Int, Short) -> Unit): Unit +public inline fun ShortArray.forEachIndexed(action: (index: Int, Short) -> Unit): Unit { + var index = 0 + for (item in this) action(index++, item) +} /** * Performs the given [action] on each element, providing sequential index with the element. * @param [action] function that takes the index of an element and the element itself * and performs the desired action on the element. */ -public expect inline fun IntArray.forEachIndexed(action: (index: Int, Int) -> Unit): Unit +public inline fun IntArray.forEachIndexed(action: (index: Int, Int) -> Unit): Unit { + var index = 0 + for (item in this) action(index++, item) +} /** * Performs the given [action] on each element, providing sequential index with the element. * @param [action] function that takes the index of an element and the element itself * and performs the desired action on the element. */ -public expect inline fun LongArray.forEachIndexed(action: (index: Int, Long) -> Unit): Unit +public inline fun LongArray.forEachIndexed(action: (index: Int, Long) -> Unit): Unit { + var index = 0 + for (item in this) action(index++, item) +} /** * Performs the given [action] on each element, providing sequential index with the element. * @param [action] function that takes the index of an element and the element itself * and performs the desired action on the element. */ -public expect inline fun FloatArray.forEachIndexed(action: (index: Int, Float) -> Unit): Unit +public inline fun FloatArray.forEachIndexed(action: (index: Int, Float) -> Unit): Unit { + var index = 0 + for (item in this) action(index++, item) +} /** * Performs the given [action] on each element, providing sequential index with the element. * @param [action] function that takes the index of an element and the element itself * and performs the desired action on the element. */ -public expect inline fun DoubleArray.forEachIndexed(action: (index: Int, Double) -> Unit): Unit +public inline fun DoubleArray.forEachIndexed(action: (index: Int, Double) -> Unit): Unit { + var index = 0 + for (item in this) action(index++, item) +} /** * Performs the given [action] on each element, providing sequential index with the element. * @param [action] function that takes the index of an element and the element itself * and performs the desired action on the element. */ -public expect inline fun BooleanArray.forEachIndexed(action: (index: Int, Boolean) -> Unit): Unit +public inline fun BooleanArray.forEachIndexed(action: (index: Int, Boolean) -> Unit): Unit { + var index = 0 + for (item in this) action(index++, item) +} /** * Performs the given [action] on each element, providing sequential index with the element. * @param [action] function that takes the index of an element and the element itself * and performs the desired action on the element. */ -public expect inline fun CharArray.forEachIndexed(action: (index: Int, Char) -> Unit): Unit +public inline fun CharArray.forEachIndexed(action: (index: Int, Char) -> Unit): Unit { + var index = 0 + for (item in this) action(index++, item) +} /** * Returns the largest element or `null` if there are no elements. @@ -6391,7 +10275,17 @@ public expect inline fun CharArray.forEachIndexed(action: (index: Int, Char) -> * If any of elements is `NaN` returns `NaN`. */ @SinceKotlin("1.1") -public expect fun Array.max(): Double? +public fun Array.max(): Double? { + if (isEmpty()) return null + var max = this[0] + if (max.isNaN()) return max + for (i in 1..lastIndex) { + val e = this[i] + if (e.isNaN()) return e + if (max < e) max = e + } + return max +} /** * Returns the largest element or `null` if there are no elements. @@ -6399,141 +10293,408 @@ public expect fun Array.max(): Double? * If any of elements is `NaN` returns `NaN`. */ @SinceKotlin("1.1") -public expect fun Array.max(): Float? +public fun Array.max(): Float? { + if (isEmpty()) return null + var max = this[0] + if (max.isNaN()) return max + for (i in 1..lastIndex) { + val e = this[i] + if (e.isNaN()) return e + if (max < e) max = e + } + return max +} /** * Returns the largest element or `null` if there are no elements. */ -public expect fun > Array.max(): T? +public fun > Array.max(): T? { + if (isEmpty()) return null + var max = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (max < e) max = e + } + return max +} /** * Returns the largest element or `null` if there are no elements. */ -public expect fun ByteArray.max(): Byte? +public fun ByteArray.max(): Byte? { + if (isEmpty()) return null + var max = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (max < e) max = e + } + return max +} /** * Returns the largest element or `null` if there are no elements. */ -public expect fun ShortArray.max(): Short? +public fun ShortArray.max(): Short? { + if (isEmpty()) return null + var max = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (max < e) max = e + } + return max +} /** * Returns the largest element or `null` if there are no elements. */ -public expect fun IntArray.max(): Int? +public fun IntArray.max(): Int? { + if (isEmpty()) return null + var max = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (max < e) max = e + } + return max +} /** * Returns the largest element or `null` if there are no elements. */ -public expect fun LongArray.max(): Long? +public fun LongArray.max(): Long? { + if (isEmpty()) return null + var max = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (max < e) max = e + } + return max +} /** * Returns the largest element or `null` if there are no elements. * * If any of elements is `NaN` returns `NaN`. */ -public expect fun FloatArray.max(): Float? +public fun FloatArray.max(): Float? { + if (isEmpty()) return null + var max = this[0] + if (max.isNaN()) return max + for (i in 1..lastIndex) { + val e = this[i] + if (e.isNaN()) return e + if (max < e) max = e + } + return max +} /** * Returns the largest element or `null` if there are no elements. * * If any of elements is `NaN` returns `NaN`. */ -public expect fun DoubleArray.max(): Double? +public fun DoubleArray.max(): Double? { + if (isEmpty()) return null + var max = this[0] + if (max.isNaN()) return max + for (i in 1..lastIndex) { + val e = this[i] + if (e.isNaN()) return e + if (max < e) max = e + } + return max +} /** * Returns the largest element or `null` if there are no elements. */ -public expect fun CharArray.max(): Char? +public fun CharArray.max(): Char? { + if (isEmpty()) return null + var max = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (max < e) max = e + } + return max +} /** * Returns the first element yielding the largest value of the given function or `null` if there are no elements. */ -public expect inline fun > Array.maxBy(selector: (T) -> R): T? +public inline fun > Array.maxBy(selector: (T) -> R): T? { + if (isEmpty()) return null + var maxElem = this[0] + var maxValue = selector(maxElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = selector(e) + if (maxValue < v) { + maxElem = e + maxValue = v + } + } + return maxElem +} /** * Returns the first element yielding the largest value of the given function or `null` if there are no elements. */ -public expect inline fun > ByteArray.maxBy(selector: (Byte) -> R): Byte? +public inline fun > ByteArray.maxBy(selector: (Byte) -> R): Byte? { + if (isEmpty()) return null + var maxElem = this[0] + var maxValue = selector(maxElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = selector(e) + if (maxValue < v) { + maxElem = e + maxValue = v + } + } + return maxElem +} /** * Returns the first element yielding the largest value of the given function or `null` if there are no elements. */ -public expect inline fun > ShortArray.maxBy(selector: (Short) -> R): Short? +public inline fun > ShortArray.maxBy(selector: (Short) -> R): Short? { + if (isEmpty()) return null + var maxElem = this[0] + var maxValue = selector(maxElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = selector(e) + if (maxValue < v) { + maxElem = e + maxValue = v + } + } + return maxElem +} /** * Returns the first element yielding the largest value of the given function or `null` if there are no elements. */ -public expect inline fun > IntArray.maxBy(selector: (Int) -> R): Int? +public inline fun > IntArray.maxBy(selector: (Int) -> R): Int? { + if (isEmpty()) return null + var maxElem = this[0] + var maxValue = selector(maxElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = selector(e) + if (maxValue < v) { + maxElem = e + maxValue = v + } + } + return maxElem +} /** * Returns the first element yielding the largest value of the given function or `null` if there are no elements. */ -public expect inline fun > LongArray.maxBy(selector: (Long) -> R): Long? +public inline fun > LongArray.maxBy(selector: (Long) -> R): Long? { + if (isEmpty()) return null + var maxElem = this[0] + var maxValue = selector(maxElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = selector(e) + if (maxValue < v) { + maxElem = e + maxValue = v + } + } + return maxElem +} /** * Returns the first element yielding the largest value of the given function or `null` if there are no elements. */ -public expect inline fun > FloatArray.maxBy(selector: (Float) -> R): Float? +public inline fun > FloatArray.maxBy(selector: (Float) -> R): Float? { + if (isEmpty()) return null + var maxElem = this[0] + var maxValue = selector(maxElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = selector(e) + if (maxValue < v) { + maxElem = e + maxValue = v + } + } + return maxElem +} /** * Returns the first element yielding the largest value of the given function or `null` if there are no elements. */ -public expect inline fun > DoubleArray.maxBy(selector: (Double) -> R): Double? +public inline fun > DoubleArray.maxBy(selector: (Double) -> R): Double? { + if (isEmpty()) return null + var maxElem = this[0] + var maxValue = selector(maxElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = selector(e) + if (maxValue < v) { + maxElem = e + maxValue = v + } + } + return maxElem +} /** * Returns the first element yielding the largest value of the given function or `null` if there are no elements. */ -public expect inline fun > BooleanArray.maxBy(selector: (Boolean) -> R): Boolean? +public inline fun > BooleanArray.maxBy(selector: (Boolean) -> R): Boolean? { + if (isEmpty()) return null + var maxElem = this[0] + var maxValue = selector(maxElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = selector(e) + if (maxValue < v) { + maxElem = e + maxValue = v + } + } + return maxElem +} /** * Returns the first element yielding the largest value of the given function or `null` if there are no elements. */ -public expect inline fun > CharArray.maxBy(selector: (Char) -> R): Char? +public inline fun > CharArray.maxBy(selector: (Char) -> R): Char? { + if (isEmpty()) return null + var maxElem = this[0] + var maxValue = selector(maxElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = selector(e) + if (maxValue < v) { + maxElem = e + maxValue = v + } + } + return maxElem +} /** * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. */ -public expect fun Array.maxWith(comparator: Comparator): T? +public fun Array.maxWith(comparator: Comparator): T? { + if (isEmpty()) return null + var max = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(max, e) < 0) max = e + } + return max +} /** * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. */ -public expect fun ByteArray.maxWith(comparator: Comparator): Byte? +public fun ByteArray.maxWith(comparator: Comparator): Byte? { + if (isEmpty()) return null + var max = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(max, e) < 0) max = e + } + return max +} /** * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. */ -public expect fun ShortArray.maxWith(comparator: Comparator): Short? +public fun ShortArray.maxWith(comparator: Comparator): Short? { + if (isEmpty()) return null + var max = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(max, e) < 0) max = e + } + return max +} /** * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. */ -public expect fun IntArray.maxWith(comparator: Comparator): Int? +public fun IntArray.maxWith(comparator: Comparator): Int? { + if (isEmpty()) return null + var max = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(max, e) < 0) max = e + } + return max +} /** * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. */ -public expect fun LongArray.maxWith(comparator: Comparator): Long? +public fun LongArray.maxWith(comparator: Comparator): Long? { + if (isEmpty()) return null + var max = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(max, e) < 0) max = e + } + return max +} /** * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. */ -public expect fun FloatArray.maxWith(comparator: Comparator): Float? +public fun FloatArray.maxWith(comparator: Comparator): Float? { + if (isEmpty()) return null + var max = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(max, e) < 0) max = e + } + return max +} /** * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. */ -public expect fun DoubleArray.maxWith(comparator: Comparator): Double? +public fun DoubleArray.maxWith(comparator: Comparator): Double? { + if (isEmpty()) return null + var max = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(max, e) < 0) max = e + } + return max +} /** * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. */ -public expect fun BooleanArray.maxWith(comparator: Comparator): Boolean? +public fun BooleanArray.maxWith(comparator: Comparator): Boolean? { + if (isEmpty()) return null + var max = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(max, e) < 0) max = e + } + return max +} /** * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. */ -public expect fun CharArray.maxWith(comparator: Comparator): Char? +public fun CharArray.maxWith(comparator: Comparator): Char? { + if (isEmpty()) return null + var max = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(max, e) < 0) max = e + } + return max +} /** * Returns the smallest element or `null` if there are no elements. @@ -6541,7 +10702,17 @@ public expect fun CharArray.maxWith(comparator: Comparator): Char? * If any of elements is `NaN` returns `NaN`. */ @SinceKotlin("1.1") -public expect fun Array.min(): Double? +public fun Array.min(): Double? { + if (isEmpty()) return null + var min = this[0] + if (min.isNaN()) return min + for (i in 1..lastIndex) { + val e = this[i] + if (e.isNaN()) return e + if (min > e) min = e + } + return min +} /** * Returns the smallest element or `null` if there are no elements. @@ -6549,312 +10720,696 @@ public expect fun Array.min(): Double? * If any of elements is `NaN` returns `NaN`. */ @SinceKotlin("1.1") -public expect fun Array.min(): Float? +public fun Array.min(): Float? { + if (isEmpty()) return null + var min = this[0] + if (min.isNaN()) return min + for (i in 1..lastIndex) { + val e = this[i] + if (e.isNaN()) return e + if (min > e) min = e + } + return min +} /** * Returns the smallest element or `null` if there are no elements. */ -public expect fun > Array.min(): T? +public fun > Array.min(): T? { + if (isEmpty()) return null + var min = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (min > e) min = e + } + return min +} /** * Returns the smallest element or `null` if there are no elements. */ -public expect fun ByteArray.min(): Byte? +public fun ByteArray.min(): Byte? { + if (isEmpty()) return null + var min = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (min > e) min = e + } + return min +} /** * Returns the smallest element or `null` if there are no elements. */ -public expect fun ShortArray.min(): Short? +public fun ShortArray.min(): Short? { + if (isEmpty()) return null + var min = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (min > e) min = e + } + return min +} /** * Returns the smallest element or `null` if there are no elements. */ -public expect fun IntArray.min(): Int? +public fun IntArray.min(): Int? { + if (isEmpty()) return null + var min = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (min > e) min = e + } + return min +} /** * Returns the smallest element or `null` if there are no elements. */ -public expect fun LongArray.min(): Long? +public fun LongArray.min(): Long? { + if (isEmpty()) return null + var min = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (min > e) min = e + } + return min +} /** * Returns the smallest element or `null` if there are no elements. * * If any of elements is `NaN` returns `NaN`. */ -public expect fun FloatArray.min(): Float? +public fun FloatArray.min(): Float? { + if (isEmpty()) return null + var min = this[0] + if (min.isNaN()) return min + for (i in 1..lastIndex) { + val e = this[i] + if (e.isNaN()) return e + if (min > e) min = e + } + return min +} /** * Returns the smallest element or `null` if there are no elements. * * If any of elements is `NaN` returns `NaN`. */ -public expect fun DoubleArray.min(): Double? +public fun DoubleArray.min(): Double? { + if (isEmpty()) return null + var min = this[0] + if (min.isNaN()) return min + for (i in 1..lastIndex) { + val e = this[i] + if (e.isNaN()) return e + if (min > e) min = e + } + return min +} /** * Returns the smallest element or `null` if there are no elements. */ -public expect fun CharArray.min(): Char? +public fun CharArray.min(): Char? { + if (isEmpty()) return null + var min = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (min > e) min = e + } + return min +} /** * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. */ -public expect inline fun > Array.minBy(selector: (T) -> R): T? +public inline fun > Array.minBy(selector: (T) -> R): T? { + if (isEmpty()) return null + var minElem = this[0] + var minValue = selector(minElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = selector(e) + if (minValue > v) { + minElem = e + minValue = v + } + } + return minElem +} /** * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. */ -public expect inline fun > ByteArray.minBy(selector: (Byte) -> R): Byte? +public inline fun > ByteArray.minBy(selector: (Byte) -> R): Byte? { + if (isEmpty()) return null + var minElem = this[0] + var minValue = selector(minElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = selector(e) + if (minValue > v) { + minElem = e + minValue = v + } + } + return minElem +} /** * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. */ -public expect inline fun > ShortArray.minBy(selector: (Short) -> R): Short? +public inline fun > ShortArray.minBy(selector: (Short) -> R): Short? { + if (isEmpty()) return null + var minElem = this[0] + var minValue = selector(minElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = selector(e) + if (minValue > v) { + minElem = e + minValue = v + } + } + return minElem +} /** * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. */ -public expect inline fun > IntArray.minBy(selector: (Int) -> R): Int? +public inline fun > IntArray.minBy(selector: (Int) -> R): Int? { + if (isEmpty()) return null + var minElem = this[0] + var minValue = selector(minElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = selector(e) + if (minValue > v) { + minElem = e + minValue = v + } + } + return minElem +} /** * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. */ -public expect inline fun > LongArray.minBy(selector: (Long) -> R): Long? +public inline fun > LongArray.minBy(selector: (Long) -> R): Long? { + if (isEmpty()) return null + var minElem = this[0] + var minValue = selector(minElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = selector(e) + if (minValue > v) { + minElem = e + minValue = v + } + } + return minElem +} /** * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. */ -public expect inline fun > FloatArray.minBy(selector: (Float) -> R): Float? +public inline fun > FloatArray.minBy(selector: (Float) -> R): Float? { + if (isEmpty()) return null + var minElem = this[0] + var minValue = selector(minElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = selector(e) + if (minValue > v) { + minElem = e + minValue = v + } + } + return minElem +} /** * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. */ -public expect inline fun > DoubleArray.minBy(selector: (Double) -> R): Double? +public inline fun > DoubleArray.minBy(selector: (Double) -> R): Double? { + if (isEmpty()) return null + var minElem = this[0] + var minValue = selector(minElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = selector(e) + if (minValue > v) { + minElem = e + minValue = v + } + } + return minElem +} /** * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. */ -public expect inline fun > BooleanArray.minBy(selector: (Boolean) -> R): Boolean? +public inline fun > BooleanArray.minBy(selector: (Boolean) -> R): Boolean? { + if (isEmpty()) return null + var minElem = this[0] + var minValue = selector(minElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = selector(e) + if (minValue > v) { + minElem = e + minValue = v + } + } + return minElem +} /** * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. */ -public expect inline fun > CharArray.minBy(selector: (Char) -> R): Char? +public inline fun > CharArray.minBy(selector: (Char) -> R): Char? { + if (isEmpty()) return null + var minElem = this[0] + var minValue = selector(minElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = selector(e) + if (minValue > v) { + minElem = e + minValue = v + } + } + return minElem +} /** * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. */ -public expect fun Array.minWith(comparator: Comparator): T? +public fun Array.minWith(comparator: Comparator): T? { + if (isEmpty()) return null + var min = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(min, e) > 0) min = e + } + return min +} /** * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. */ -public expect fun ByteArray.minWith(comparator: Comparator): Byte? +public fun ByteArray.minWith(comparator: Comparator): Byte? { + if (isEmpty()) return null + var min = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(min, e) > 0) min = e + } + return min +} /** * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. */ -public expect fun ShortArray.minWith(comparator: Comparator): Short? +public fun ShortArray.minWith(comparator: Comparator): Short? { + if (isEmpty()) return null + var min = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(min, e) > 0) min = e + } + return min +} /** * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. */ -public expect fun IntArray.minWith(comparator: Comparator): Int? +public fun IntArray.minWith(comparator: Comparator): Int? { + if (isEmpty()) return null + var min = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(min, e) > 0) min = e + } + return min +} /** * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. */ -public expect fun LongArray.minWith(comparator: Comparator): Long? +public fun LongArray.minWith(comparator: Comparator): Long? { + if (isEmpty()) return null + var min = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(min, e) > 0) min = e + } + return min +} /** * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. */ -public expect fun FloatArray.minWith(comparator: Comparator): Float? +public fun FloatArray.minWith(comparator: Comparator): Float? { + if (isEmpty()) return null + var min = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(min, e) > 0) min = e + } + return min +} /** * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. */ -public expect fun DoubleArray.minWith(comparator: Comparator): Double? +public fun DoubleArray.minWith(comparator: Comparator): Double? { + if (isEmpty()) return null + var min = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(min, e) > 0) min = e + } + return min +} /** * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. */ -public expect fun BooleanArray.minWith(comparator: Comparator): Boolean? +public fun BooleanArray.minWith(comparator: Comparator): Boolean? { + if (isEmpty()) return null + var min = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(min, e) > 0) min = e + } + return min +} /** * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. */ -public expect fun CharArray.minWith(comparator: Comparator): Char? +public fun CharArray.minWith(comparator: Comparator): Char? { + if (isEmpty()) return null + var min = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(min, e) > 0) min = e + } + return min +} /** * Returns `true` if the array has no elements. * * @sample samples.collections.Collections.Aggregates.none */ -public expect fun Array.none(): Boolean +public fun Array.none(): Boolean { + return isEmpty() +} /** * Returns `true` if the array has no elements. * * @sample samples.collections.Collections.Aggregates.none */ -public expect fun ByteArray.none(): Boolean +public fun ByteArray.none(): Boolean { + return isEmpty() +} /** * Returns `true` if the array has no elements. * * @sample samples.collections.Collections.Aggregates.none */ -public expect fun ShortArray.none(): Boolean +public fun ShortArray.none(): Boolean { + return isEmpty() +} /** * Returns `true` if the array has no elements. * * @sample samples.collections.Collections.Aggregates.none */ -public expect fun IntArray.none(): Boolean +public fun IntArray.none(): Boolean { + return isEmpty() +} /** * Returns `true` if the array has no elements. * * @sample samples.collections.Collections.Aggregates.none */ -public expect fun LongArray.none(): Boolean +public fun LongArray.none(): Boolean { + return isEmpty() +} /** * Returns `true` if the array has no elements. * * @sample samples.collections.Collections.Aggregates.none */ -public expect fun FloatArray.none(): Boolean +public fun FloatArray.none(): Boolean { + return isEmpty() +} /** * Returns `true` if the array has no elements. * * @sample samples.collections.Collections.Aggregates.none */ -public expect fun DoubleArray.none(): Boolean +public fun DoubleArray.none(): Boolean { + return isEmpty() +} /** * Returns `true` if the array has no elements. * * @sample samples.collections.Collections.Aggregates.none */ -public expect fun BooleanArray.none(): Boolean +public fun BooleanArray.none(): Boolean { + return isEmpty() +} /** * Returns `true` if the array has no elements. * * @sample samples.collections.Collections.Aggregates.none */ -public expect fun CharArray.none(): Boolean +public fun CharArray.none(): Boolean { + return isEmpty() +} /** * Returns `true` if no elements match the given [predicate]. * * @sample samples.collections.Collections.Aggregates.noneWithPredicate */ -public expect inline fun Array.none(predicate: (T) -> Boolean): Boolean +public inline fun Array.none(predicate: (T) -> Boolean): Boolean { + for (element in this) if (predicate(element)) return false + return true +} /** * Returns `true` if no elements match the given [predicate]. * * @sample samples.collections.Collections.Aggregates.noneWithPredicate */ -public expect inline fun ByteArray.none(predicate: (Byte) -> Boolean): Boolean +public inline fun ByteArray.none(predicate: (Byte) -> Boolean): Boolean { + for (element in this) if (predicate(element)) return false + return true +} /** * Returns `true` if no elements match the given [predicate]. * * @sample samples.collections.Collections.Aggregates.noneWithPredicate */ -public expect inline fun ShortArray.none(predicate: (Short) -> Boolean): Boolean +public inline fun ShortArray.none(predicate: (Short) -> Boolean): Boolean { + for (element in this) if (predicate(element)) return false + return true +} /** * Returns `true` if no elements match the given [predicate]. * * @sample samples.collections.Collections.Aggregates.noneWithPredicate */ -public expect inline fun IntArray.none(predicate: (Int) -> Boolean): Boolean +public inline fun IntArray.none(predicate: (Int) -> Boolean): Boolean { + for (element in this) if (predicate(element)) return false + return true +} /** * Returns `true` if no elements match the given [predicate]. * * @sample samples.collections.Collections.Aggregates.noneWithPredicate */ -public expect inline fun LongArray.none(predicate: (Long) -> Boolean): Boolean +public inline fun LongArray.none(predicate: (Long) -> Boolean): Boolean { + for (element in this) if (predicate(element)) return false + return true +} /** * Returns `true` if no elements match the given [predicate]. * * @sample samples.collections.Collections.Aggregates.noneWithPredicate */ -public expect inline fun FloatArray.none(predicate: (Float) -> Boolean): Boolean +public inline fun FloatArray.none(predicate: (Float) -> Boolean): Boolean { + for (element in this) if (predicate(element)) return false + return true +} /** * Returns `true` if no elements match the given [predicate]. * * @sample samples.collections.Collections.Aggregates.noneWithPredicate */ -public expect inline fun DoubleArray.none(predicate: (Double) -> Boolean): Boolean +public inline fun DoubleArray.none(predicate: (Double) -> Boolean): Boolean { + for (element in this) if (predicate(element)) return false + return true +} /** * Returns `true` if no elements match the given [predicate]. * * @sample samples.collections.Collections.Aggregates.noneWithPredicate */ -public expect inline fun BooleanArray.none(predicate: (Boolean) -> Boolean): Boolean +public inline fun BooleanArray.none(predicate: (Boolean) -> Boolean): Boolean { + for (element in this) if (predicate(element)) return false + return true +} /** * Returns `true` if no elements match the given [predicate]. * * @sample samples.collections.Collections.Aggregates.noneWithPredicate */ -public expect inline fun CharArray.none(predicate: (Char) -> Boolean): Boolean +public inline fun CharArray.none(predicate: (Char) -> Boolean): Boolean { + for (element in this) if (predicate(element)) return false + return true +} /** * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. */ -public expect inline fun Array.reduce(operation: (acc: S, T) -> S): S +public inline fun Array.reduce(operation: (acc: S, T) -> S): S { + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator: S = this[0] + for (index in 1..lastIndex) { + accumulator = operation(accumulator, this[index]) + } + return accumulator +} /** * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. */ -public expect inline fun ByteArray.reduce(operation: (acc: Byte, Byte) -> Byte): Byte +public inline fun ByteArray.reduce(operation: (acc: Byte, Byte) -> Byte): Byte { + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(accumulator, this[index]) + } + return accumulator +} /** * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. */ -public expect inline fun ShortArray.reduce(operation: (acc: Short, Short) -> Short): Short +public inline fun ShortArray.reduce(operation: (acc: Short, Short) -> Short): Short { + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(accumulator, this[index]) + } + return accumulator +} /** * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. */ -public expect inline fun IntArray.reduce(operation: (acc: Int, Int) -> Int): Int +public inline fun IntArray.reduce(operation: (acc: Int, Int) -> Int): Int { + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(accumulator, this[index]) + } + return accumulator +} /** * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. */ -public expect inline fun LongArray.reduce(operation: (acc: Long, Long) -> Long): Long +public inline fun LongArray.reduce(operation: (acc: Long, Long) -> Long): Long { + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(accumulator, this[index]) + } + return accumulator +} /** * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. */ -public expect inline fun FloatArray.reduce(operation: (acc: Float, Float) -> Float): Float +public inline fun FloatArray.reduce(operation: (acc: Float, Float) -> Float): Float { + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(accumulator, this[index]) + } + return accumulator +} /** * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. */ -public expect inline fun DoubleArray.reduce(operation: (acc: Double, Double) -> Double): Double +public inline fun DoubleArray.reduce(operation: (acc: Double, Double) -> Double): Double { + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(accumulator, this[index]) + } + return accumulator +} /** * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. */ -public expect inline fun BooleanArray.reduce(operation: (acc: Boolean, Boolean) -> Boolean): Boolean +public inline fun BooleanArray.reduce(operation: (acc: Boolean, Boolean) -> Boolean): Boolean { + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(accumulator, this[index]) + } + return accumulator +} /** * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. */ -public expect inline fun CharArray.reduce(operation: (acc: Char, Char) -> Char): Char +public inline fun CharArray.reduce(operation: (acc: Char, Char) -> Char): Char { + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(accumulator, this[index]) + } + return accumulator +} /** * Accumulates value starting with the first element and applying [operation] from left to right @@ -6862,7 +11417,15 @@ public expect inline fun CharArray.reduce(operation: (acc: Char, Char) -> Char): * @param [operation] function that takes the index of an element, current accumulator value * and the element itself and calculates the next accumulator value. */ -public expect inline fun Array.reduceIndexed(operation: (index: Int, acc: S, T) -> S): S +public inline fun Array.reduceIndexed(operation: (index: Int, acc: S, T) -> S): S { + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator: S = this[0] + for (index in 1..lastIndex) { + accumulator = operation(index, accumulator, this[index]) + } + return accumulator +} /** * Accumulates value starting with the first element and applying [operation] from left to right @@ -6870,7 +11433,15 @@ public expect inline fun Array.reduceIndexed(operation: (index: * @param [operation] function that takes the index of an element, current accumulator value * and the element itself and calculates the next accumulator value. */ -public expect inline fun ByteArray.reduceIndexed(operation: (index: Int, acc: Byte, Byte) -> Byte): Byte +public inline fun ByteArray.reduceIndexed(operation: (index: Int, acc: Byte, Byte) -> Byte): Byte { + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(index, accumulator, this[index]) + } + return accumulator +} /** * Accumulates value starting with the first element and applying [operation] from left to right @@ -6878,7 +11449,15 @@ public expect inline fun ByteArray.reduceIndexed(operation: (index: Int, acc: By * @param [operation] function that takes the index of an element, current accumulator value * and the element itself and calculates the next accumulator value. */ -public expect inline fun ShortArray.reduceIndexed(operation: (index: Int, acc: Short, Short) -> Short): Short +public inline fun ShortArray.reduceIndexed(operation: (index: Int, acc: Short, Short) -> Short): Short { + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(index, accumulator, this[index]) + } + return accumulator +} /** * Accumulates value starting with the first element and applying [operation] from left to right @@ -6886,7 +11465,15 @@ public expect inline fun ShortArray.reduceIndexed(operation: (index: Int, acc: S * @param [operation] function that takes the index of an element, current accumulator value * and the element itself and calculates the next accumulator value. */ -public expect inline fun IntArray.reduceIndexed(operation: (index: Int, acc: Int, Int) -> Int): Int +public inline fun IntArray.reduceIndexed(operation: (index: Int, acc: Int, Int) -> Int): Int { + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(index, accumulator, this[index]) + } + return accumulator +} /** * Accumulates value starting with the first element and applying [operation] from left to right @@ -6894,7 +11481,15 @@ public expect inline fun IntArray.reduceIndexed(operation: (index: Int, acc: Int * @param [operation] function that takes the index of an element, current accumulator value * and the element itself and calculates the next accumulator value. */ -public expect inline fun LongArray.reduceIndexed(operation: (index: Int, acc: Long, Long) -> Long): Long +public inline fun LongArray.reduceIndexed(operation: (index: Int, acc: Long, Long) -> Long): Long { + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(index, accumulator, this[index]) + } + return accumulator +} /** * Accumulates value starting with the first element and applying [operation] from left to right @@ -6902,7 +11497,15 @@ public expect inline fun LongArray.reduceIndexed(operation: (index: Int, acc: Lo * @param [operation] function that takes the index of an element, current accumulator value * and the element itself and calculates the next accumulator value. */ -public expect inline fun FloatArray.reduceIndexed(operation: (index: Int, acc: Float, Float) -> Float): Float +public inline fun FloatArray.reduceIndexed(operation: (index: Int, acc: Float, Float) -> Float): Float { + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(index, accumulator, this[index]) + } + return accumulator +} /** * Accumulates value starting with the first element and applying [operation] from left to right @@ -6910,7 +11513,15 @@ public expect inline fun FloatArray.reduceIndexed(operation: (index: Int, acc: F * @param [operation] function that takes the index of an element, current accumulator value * and the element itself and calculates the next accumulator value. */ -public expect inline fun DoubleArray.reduceIndexed(operation: (index: Int, acc: Double, Double) -> Double): Double +public inline fun DoubleArray.reduceIndexed(operation: (index: Int, acc: Double, Double) -> Double): Double { + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(index, accumulator, this[index]) + } + return accumulator +} /** * Accumulates value starting with the first element and applying [operation] from left to right @@ -6918,7 +11529,15 @@ public expect inline fun DoubleArray.reduceIndexed(operation: (index: Int, acc: * @param [operation] function that takes the index of an element, current accumulator value * and the element itself and calculates the next accumulator value. */ -public expect inline fun BooleanArray.reduceIndexed(operation: (index: Int, acc: Boolean, Boolean) -> Boolean): Boolean +public inline fun BooleanArray.reduceIndexed(operation: (index: Int, acc: Boolean, Boolean) -> Boolean): Boolean { + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(index, accumulator, this[index]) + } + return accumulator +} /** * Accumulates value starting with the first element and applying [operation] from left to right @@ -6926,52 +11545,132 @@ public expect inline fun BooleanArray.reduceIndexed(operation: (index: Int, acc: * @param [operation] function that takes the index of an element, current accumulator value * and the element itself and calculates the next accumulator value. */ -public expect inline fun CharArray.reduceIndexed(operation: (index: Int, acc: Char, Char) -> Char): Char +public inline fun CharArray.reduceIndexed(operation: (index: Int, acc: Char, Char) -> Char): Char { + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(index, accumulator, this[index]) + } + return accumulator +} /** * Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. */ -public expect inline fun Array.reduceRight(operation: (T, acc: S) -> S): S +public inline fun Array.reduceRight(operation: (T, acc: S) -> S): S { + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator: S = get(index--) + while (index >= 0) { + accumulator = operation(get(index--), accumulator) + } + return accumulator +} /** * Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. */ -public expect inline fun ByteArray.reduceRight(operation: (Byte, acc: Byte) -> Byte): Byte +public inline fun ByteArray.reduceRight(operation: (Byte, acc: Byte) -> Byte): Byte { + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = get(index--) + while (index >= 0) { + accumulator = operation(get(index--), accumulator) + } + return accumulator +} /** * Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. */ -public expect inline fun ShortArray.reduceRight(operation: (Short, acc: Short) -> Short): Short +public inline fun ShortArray.reduceRight(operation: (Short, acc: Short) -> Short): Short { + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = get(index--) + while (index >= 0) { + accumulator = operation(get(index--), accumulator) + } + return accumulator +} /** * Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. */ -public expect inline fun IntArray.reduceRight(operation: (Int, acc: Int) -> Int): Int +public inline fun IntArray.reduceRight(operation: (Int, acc: Int) -> Int): Int { + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = get(index--) + while (index >= 0) { + accumulator = operation(get(index--), accumulator) + } + return accumulator +} /** * Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. */ -public expect inline fun LongArray.reduceRight(operation: (Long, acc: Long) -> Long): Long +public inline fun LongArray.reduceRight(operation: (Long, acc: Long) -> Long): Long { + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = get(index--) + while (index >= 0) { + accumulator = operation(get(index--), accumulator) + } + return accumulator +} /** * Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. */ -public expect inline fun FloatArray.reduceRight(operation: (Float, acc: Float) -> Float): Float +public inline fun FloatArray.reduceRight(operation: (Float, acc: Float) -> Float): Float { + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = get(index--) + while (index >= 0) { + accumulator = operation(get(index--), accumulator) + } + return accumulator +} /** * Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. */ -public expect inline fun DoubleArray.reduceRight(operation: (Double, acc: Double) -> Double): Double +public inline fun DoubleArray.reduceRight(operation: (Double, acc: Double) -> Double): Double { + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = get(index--) + while (index >= 0) { + accumulator = operation(get(index--), accumulator) + } + return accumulator +} /** * Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. */ -public expect inline fun BooleanArray.reduceRight(operation: (Boolean, acc: Boolean) -> Boolean): Boolean +public inline fun BooleanArray.reduceRight(operation: (Boolean, acc: Boolean) -> Boolean): Boolean { + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = get(index--) + while (index >= 0) { + accumulator = operation(get(index--), accumulator) + } + return accumulator +} /** * Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. */ -public expect inline fun CharArray.reduceRight(operation: (Char, acc: Char) -> Char): Char +public inline fun CharArray.reduceRight(operation: (Char, acc: Char) -> Char): Char { + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = get(index--) + while (index >= 0) { + accumulator = operation(get(index--), accumulator) + } + return accumulator +} /** * Accumulates value starting with last element and applying [operation] from right to left @@ -6979,7 +11678,16 @@ public expect inline fun CharArray.reduceRight(operation: (Char, acc: Char) -> C * @param [operation] function that takes the index of an element, the element itself * and current accumulator value, and calculates the next accumulator value. */ -public expect inline fun Array.reduceRightIndexed(operation: (index: Int, T, acc: S) -> S): S +public inline fun Array.reduceRightIndexed(operation: (index: Int, T, acc: S) -> S): S { + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator: S = get(index--) + while (index >= 0) { + accumulator = operation(index, get(index), accumulator) + --index + } + return accumulator +} /** * Accumulates value starting with last element and applying [operation] from right to left @@ -6987,7 +11695,16 @@ public expect inline fun Array.reduceRightIndexed(operation: (i * @param [operation] function that takes the index of an element, the element itself * and current accumulator value, and calculates the next accumulator value. */ -public expect inline fun ByteArray.reduceRightIndexed(operation: (index: Int, Byte, acc: Byte) -> Byte): Byte +public inline fun ByteArray.reduceRightIndexed(operation: (index: Int, Byte, acc: Byte) -> Byte): Byte { + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = get(index--) + while (index >= 0) { + accumulator = operation(index, get(index), accumulator) + --index + } + return accumulator +} /** * Accumulates value starting with last element and applying [operation] from right to left @@ -6995,7 +11712,16 @@ public expect inline fun ByteArray.reduceRightIndexed(operation: (index: Int, By * @param [operation] function that takes the index of an element, the element itself * and current accumulator value, and calculates the next accumulator value. */ -public expect inline fun ShortArray.reduceRightIndexed(operation: (index: Int, Short, acc: Short) -> Short): Short +public inline fun ShortArray.reduceRightIndexed(operation: (index: Int, Short, acc: Short) -> Short): Short { + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = get(index--) + while (index >= 0) { + accumulator = operation(index, get(index), accumulator) + --index + } + return accumulator +} /** * Accumulates value starting with last element and applying [operation] from right to left @@ -7003,7 +11729,16 @@ public expect inline fun ShortArray.reduceRightIndexed(operation: (index: Int, S * @param [operation] function that takes the index of an element, the element itself * and current accumulator value, and calculates the next accumulator value. */ -public expect inline fun IntArray.reduceRightIndexed(operation: (index: Int, Int, acc: Int) -> Int): Int +public inline fun IntArray.reduceRightIndexed(operation: (index: Int, Int, acc: Int) -> Int): Int { + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = get(index--) + while (index >= 0) { + accumulator = operation(index, get(index), accumulator) + --index + } + return accumulator +} /** * Accumulates value starting with last element and applying [operation] from right to left @@ -7011,7 +11746,16 @@ public expect inline fun IntArray.reduceRightIndexed(operation: (index: Int, Int * @param [operation] function that takes the index of an element, the element itself * and current accumulator value, and calculates the next accumulator value. */ -public expect inline fun LongArray.reduceRightIndexed(operation: (index: Int, Long, acc: Long) -> Long): Long +public inline fun LongArray.reduceRightIndexed(operation: (index: Int, Long, acc: Long) -> Long): Long { + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = get(index--) + while (index >= 0) { + accumulator = operation(index, get(index), accumulator) + --index + } + return accumulator +} /** * Accumulates value starting with last element and applying [operation] from right to left @@ -7019,7 +11763,16 @@ public expect inline fun LongArray.reduceRightIndexed(operation: (index: Int, Lo * @param [operation] function that takes the index of an element, the element itself * and current accumulator value, and calculates the next accumulator value. */ -public expect inline fun FloatArray.reduceRightIndexed(operation: (index: Int, Float, acc: Float) -> Float): Float +public inline fun FloatArray.reduceRightIndexed(operation: (index: Int, Float, acc: Float) -> Float): Float { + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = get(index--) + while (index >= 0) { + accumulator = operation(index, get(index), accumulator) + --index + } + return accumulator +} /** * Accumulates value starting with last element and applying [operation] from right to left @@ -7027,7 +11780,16 @@ public expect inline fun FloatArray.reduceRightIndexed(operation: (index: Int, F * @param [operation] function that takes the index of an element, the element itself * and current accumulator value, and calculates the next accumulator value. */ -public expect inline fun DoubleArray.reduceRightIndexed(operation: (index: Int, Double, acc: Double) -> Double): Double +public inline fun DoubleArray.reduceRightIndexed(operation: (index: Int, Double, acc: Double) -> Double): Double { + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = get(index--) + while (index >= 0) { + accumulator = operation(index, get(index), accumulator) + --index + } + return accumulator +} /** * Accumulates value starting with last element and applying [operation] from right to left @@ -7035,7 +11797,16 @@ public expect inline fun DoubleArray.reduceRightIndexed(operation: (index: Int, * @param [operation] function that takes the index of an element, the element itself * and current accumulator value, and calculates the next accumulator value. */ -public expect inline fun BooleanArray.reduceRightIndexed(operation: (index: Int, Boolean, acc: Boolean) -> Boolean): Boolean +public inline fun BooleanArray.reduceRightIndexed(operation: (index: Int, Boolean, acc: Boolean) -> Boolean): Boolean { + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = get(index--) + while (index >= 0) { + accumulator = operation(index, get(index), accumulator) + --index + } + return accumulator +} /** * Accumulates value starting with last element and applying [operation] from right to left @@ -7043,425 +11814,901 @@ public expect inline fun BooleanArray.reduceRightIndexed(operation: (index: Int, * @param [operation] function that takes the index of an element, the element itself * and current accumulator value, and calculates the next accumulator value. */ -public expect inline fun CharArray.reduceRightIndexed(operation: (index: Int, Char, acc: Char) -> Char): Char +public inline fun CharArray.reduceRightIndexed(operation: (index: Int, Char, acc: Char) -> Char): Char { + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = get(index--) + while (index >= 0) { + accumulator = operation(index, get(index), accumulator) + --index + } + return accumulator +} /** * Returns the sum of all values produced by [selector] function applied to each element in the array. */ -public expect inline fun Array.sumBy(selector: (T) -> Int): Int +public inline fun Array.sumBy(selector: (T) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += selector(element) + } + return sum +} /** * Returns the sum of all values produced by [selector] function applied to each element in the array. */ -public expect inline fun ByteArray.sumBy(selector: (Byte) -> Int): Int +public inline fun ByteArray.sumBy(selector: (Byte) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += selector(element) + } + return sum +} /** * Returns the sum of all values produced by [selector] function applied to each element in the array. */ -public expect inline fun ShortArray.sumBy(selector: (Short) -> Int): Int +public inline fun ShortArray.sumBy(selector: (Short) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += selector(element) + } + return sum +} /** * Returns the sum of all values produced by [selector] function applied to each element in the array. */ -public expect inline fun IntArray.sumBy(selector: (Int) -> Int): Int +public inline fun IntArray.sumBy(selector: (Int) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += selector(element) + } + return sum +} /** * Returns the sum of all values produced by [selector] function applied to each element in the array. */ -public expect inline fun LongArray.sumBy(selector: (Long) -> Int): Int +public inline fun LongArray.sumBy(selector: (Long) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += selector(element) + } + return sum +} /** * Returns the sum of all values produced by [selector] function applied to each element in the array. */ -public expect inline fun FloatArray.sumBy(selector: (Float) -> Int): Int +public inline fun FloatArray.sumBy(selector: (Float) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += selector(element) + } + return sum +} /** * Returns the sum of all values produced by [selector] function applied to each element in the array. */ -public expect inline fun DoubleArray.sumBy(selector: (Double) -> Int): Int +public inline fun DoubleArray.sumBy(selector: (Double) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += selector(element) + } + return sum +} /** * Returns the sum of all values produced by [selector] function applied to each element in the array. */ -public expect inline fun BooleanArray.sumBy(selector: (Boolean) -> Int): Int +public inline fun BooleanArray.sumBy(selector: (Boolean) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += selector(element) + } + return sum +} /** * Returns the sum of all values produced by [selector] function applied to each element in the array. */ -public expect inline fun CharArray.sumBy(selector: (Char) -> Int): Int +public inline fun CharArray.sumBy(selector: (Char) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += selector(element) + } + return sum +} /** * Returns the sum of all values produced by [selector] function applied to each element in the array. */ -public expect inline fun Array.sumByDouble(selector: (T) -> Double): Double +public inline fun Array.sumByDouble(selector: (T) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += selector(element) + } + return sum +} /** * Returns the sum of all values produced by [selector] function applied to each element in the array. */ -public expect inline fun ByteArray.sumByDouble(selector: (Byte) -> Double): Double +public inline fun ByteArray.sumByDouble(selector: (Byte) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += selector(element) + } + return sum +} /** * Returns the sum of all values produced by [selector] function applied to each element in the array. */ -public expect inline fun ShortArray.sumByDouble(selector: (Short) -> Double): Double +public inline fun ShortArray.sumByDouble(selector: (Short) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += selector(element) + } + return sum +} /** * Returns the sum of all values produced by [selector] function applied to each element in the array. */ -public expect inline fun IntArray.sumByDouble(selector: (Int) -> Double): Double +public inline fun IntArray.sumByDouble(selector: (Int) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += selector(element) + } + return sum +} /** * Returns the sum of all values produced by [selector] function applied to each element in the array. */ -public expect inline fun LongArray.sumByDouble(selector: (Long) -> Double): Double +public inline fun LongArray.sumByDouble(selector: (Long) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += selector(element) + } + return sum +} /** * Returns the sum of all values produced by [selector] function applied to each element in the array. */ -public expect inline fun FloatArray.sumByDouble(selector: (Float) -> Double): Double +public inline fun FloatArray.sumByDouble(selector: (Float) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += selector(element) + } + return sum +} /** * Returns the sum of all values produced by [selector] function applied to each element in the array. */ -public expect inline fun DoubleArray.sumByDouble(selector: (Double) -> Double): Double +public inline fun DoubleArray.sumByDouble(selector: (Double) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += selector(element) + } + return sum +} /** * Returns the sum of all values produced by [selector] function applied to each element in the array. */ -public expect inline fun BooleanArray.sumByDouble(selector: (Boolean) -> Double): Double +public inline fun BooleanArray.sumByDouble(selector: (Boolean) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += selector(element) + } + return sum +} /** * Returns the sum of all values produced by [selector] function applied to each element in the array. */ -public expect inline fun CharArray.sumByDouble(selector: (Char) -> Double): Double +public inline fun CharArray.sumByDouble(selector: (Char) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += selector(element) + } + return sum +} /** * Returns an original collection containing all the non-`null` elements, throwing an [IllegalArgumentException] if there are any `null` elements. */ -public expect fun Array.requireNoNulls(): Array +public fun Array.requireNoNulls(): Array { + for (element in this) { + if (element == null) { + throw IllegalArgumentException("null element found in $this.") + } + } + @Suppress("UNCHECKED_CAST") + return this as Array +} /** * Splits the original array into pair of lists, * where *first* list contains elements for which [predicate] yielded `true`, * while *second* list contains elements for which [predicate] yielded `false`. */ -public expect inline fun Array.partition(predicate: (T) -> Boolean): Pair, List> +public inline fun Array.partition(predicate: (T) -> Boolean): Pair, List> { + val first = ArrayList() + val second = ArrayList() + for (element in this) { + if (predicate(element)) { + first.add(element) + } else { + second.add(element) + } + } + return Pair(first, second) +} /** * Splits the original array into pair of lists, * where *first* list contains elements for which [predicate] yielded `true`, * while *second* list contains elements for which [predicate] yielded `false`. */ -public expect inline fun ByteArray.partition(predicate: (Byte) -> Boolean): Pair, List> +public inline fun ByteArray.partition(predicate: (Byte) -> Boolean): Pair, List> { + val first = ArrayList() + val second = ArrayList() + for (element in this) { + if (predicate(element)) { + first.add(element) + } else { + second.add(element) + } + } + return Pair(first, second) +} /** * Splits the original array into pair of lists, * where *first* list contains elements for which [predicate] yielded `true`, * while *second* list contains elements for which [predicate] yielded `false`. */ -public expect inline fun ShortArray.partition(predicate: (Short) -> Boolean): Pair, List> +public inline fun ShortArray.partition(predicate: (Short) -> Boolean): Pair, List> { + val first = ArrayList() + val second = ArrayList() + for (element in this) { + if (predicate(element)) { + first.add(element) + } else { + second.add(element) + } + } + return Pair(first, second) +} /** * Splits the original array into pair of lists, * where *first* list contains elements for which [predicate] yielded `true`, * while *second* list contains elements for which [predicate] yielded `false`. */ -public expect inline fun IntArray.partition(predicate: (Int) -> Boolean): Pair, List> +public inline fun IntArray.partition(predicate: (Int) -> Boolean): Pair, List> { + val first = ArrayList() + val second = ArrayList() + for (element in this) { + if (predicate(element)) { + first.add(element) + } else { + second.add(element) + } + } + return Pair(first, second) +} /** * Splits the original array into pair of lists, * where *first* list contains elements for which [predicate] yielded `true`, * while *second* list contains elements for which [predicate] yielded `false`. */ -public expect inline fun LongArray.partition(predicate: (Long) -> Boolean): Pair, List> +public inline fun LongArray.partition(predicate: (Long) -> Boolean): Pair, List> { + val first = ArrayList() + val second = ArrayList() + for (element in this) { + if (predicate(element)) { + first.add(element) + } else { + second.add(element) + } + } + return Pair(first, second) +} /** * Splits the original array into pair of lists, * where *first* list contains elements for which [predicate] yielded `true`, * while *second* list contains elements for which [predicate] yielded `false`. */ -public expect inline fun FloatArray.partition(predicate: (Float) -> Boolean): Pair, List> +public inline fun FloatArray.partition(predicate: (Float) -> Boolean): Pair, List> { + val first = ArrayList() + val second = ArrayList() + for (element in this) { + if (predicate(element)) { + first.add(element) + } else { + second.add(element) + } + } + return Pair(first, second) +} /** * Splits the original array into pair of lists, * where *first* list contains elements for which [predicate] yielded `true`, * while *second* list contains elements for which [predicate] yielded `false`. */ -public expect inline fun DoubleArray.partition(predicate: (Double) -> Boolean): Pair, List> +public inline fun DoubleArray.partition(predicate: (Double) -> Boolean): Pair, List> { + val first = ArrayList() + val second = ArrayList() + for (element in this) { + if (predicate(element)) { + first.add(element) + } else { + second.add(element) + } + } + return Pair(first, second) +} /** * Splits the original array into pair of lists, * where *first* list contains elements for which [predicate] yielded `true`, * while *second* list contains elements for which [predicate] yielded `false`. */ -public expect inline fun BooleanArray.partition(predicate: (Boolean) -> Boolean): Pair, List> +public inline fun BooleanArray.partition(predicate: (Boolean) -> Boolean): Pair, List> { + val first = ArrayList() + val second = ArrayList() + for (element in this) { + if (predicate(element)) { + first.add(element) + } else { + second.add(element) + } + } + return Pair(first, second) +} /** * Splits the original array into pair of lists, * where *first* list contains elements for which [predicate] yielded `true`, * while *second* list contains elements for which [predicate] yielded `false`. */ -public expect inline fun CharArray.partition(predicate: (Char) -> Boolean): Pair, List> +public inline fun CharArray.partition(predicate: (Char) -> Boolean): Pair, List> { + val first = ArrayList() + val second = ArrayList() + for (element in this) { + if (predicate(element)) { + first.add(element) + } else { + second.add(element) + } + } + return Pair(first, second) +} /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ -public expect infix fun Array.zip(other: Array): List> +public infix fun Array.zip(other: Array): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ -public expect infix fun ByteArray.zip(other: Array): List> +public infix fun ByteArray.zip(other: Array): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ -public expect infix fun ShortArray.zip(other: Array): List> +public infix fun ShortArray.zip(other: Array): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ -public expect infix fun IntArray.zip(other: Array): List> +public infix fun IntArray.zip(other: Array): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ -public expect infix fun LongArray.zip(other: Array): List> +public infix fun LongArray.zip(other: Array): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ -public expect infix fun FloatArray.zip(other: Array): List> +public infix fun FloatArray.zip(other: Array): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ -public expect infix fun DoubleArray.zip(other: Array): List> +public infix fun DoubleArray.zip(other: Array): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ -public expect infix fun BooleanArray.zip(other: Array): List> +public infix fun BooleanArray.zip(other: Array): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ -public expect infix fun CharArray.zip(other: Array): List> +public infix fun CharArray.zip(other: Array): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} /** * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public expect inline fun Array.zip(other: Array, transform: (a: T, b: R) -> V): List +public inline fun Array.zip(other: Array, transform: (a: T, b: R) -> V): List { + val size = minOf(size, other.size) + val list = ArrayList(size) + for (i in 0 until size) { + list.add(transform(this[i], other[i])) + } + return list +} /** * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public expect inline fun ByteArray.zip(other: Array, transform: (a: Byte, b: R) -> V): List +public inline fun ByteArray.zip(other: Array, transform: (a: Byte, b: R) -> V): List { + val size = minOf(size, other.size) + val list = ArrayList(size) + for (i in 0 until size) { + list.add(transform(this[i], other[i])) + } + return list +} /** * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public expect inline fun ShortArray.zip(other: Array, transform: (a: Short, b: R) -> V): List +public inline fun ShortArray.zip(other: Array, transform: (a: Short, b: R) -> V): List { + val size = minOf(size, other.size) + val list = ArrayList(size) + for (i in 0 until size) { + list.add(transform(this[i], other[i])) + } + return list +} /** * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public expect inline fun IntArray.zip(other: Array, transform: (a: Int, b: R) -> V): List +public inline fun IntArray.zip(other: Array, transform: (a: Int, b: R) -> V): List { + val size = minOf(size, other.size) + val list = ArrayList(size) + for (i in 0 until size) { + list.add(transform(this[i], other[i])) + } + return list +} /** * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public expect inline fun LongArray.zip(other: Array, transform: (a: Long, b: R) -> V): List +public inline fun LongArray.zip(other: Array, transform: (a: Long, b: R) -> V): List { + val size = minOf(size, other.size) + val list = ArrayList(size) + for (i in 0 until size) { + list.add(transform(this[i], other[i])) + } + return list +} /** * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public expect inline fun FloatArray.zip(other: Array, transform: (a: Float, b: R) -> V): List +public inline fun FloatArray.zip(other: Array, transform: (a: Float, b: R) -> V): List { + val size = minOf(size, other.size) + val list = ArrayList(size) + for (i in 0 until size) { + list.add(transform(this[i], other[i])) + } + return list +} /** * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public expect inline fun DoubleArray.zip(other: Array, transform: (a: Double, b: R) -> V): List +public inline fun DoubleArray.zip(other: Array, transform: (a: Double, b: R) -> V): List { + val size = minOf(size, other.size) + val list = ArrayList(size) + for (i in 0 until size) { + list.add(transform(this[i], other[i])) + } + return list +} /** * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public expect inline fun BooleanArray.zip(other: Array, transform: (a: Boolean, b: R) -> V): List +public inline fun BooleanArray.zip(other: Array, transform: (a: Boolean, b: R) -> V): List { + val size = minOf(size, other.size) + val list = ArrayList(size) + for (i in 0 until size) { + list.add(transform(this[i], other[i])) + } + return list +} /** * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public expect inline fun CharArray.zip(other: Array, transform: (a: Char, b: R) -> V): List +public inline fun CharArray.zip(other: Array, transform: (a: Char, b: R) -> V): List { + val size = minOf(size, other.size) + val list = ArrayList(size) + for (i in 0 until size) { + list.add(transform(this[i], other[i])) + } + return list +} /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ -public expect infix fun Array.zip(other: Iterable): List> +public infix fun Array.zip(other: Iterable): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ -public expect infix fun ByteArray.zip(other: Iterable): List> +public infix fun ByteArray.zip(other: Iterable): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ -public expect infix fun ShortArray.zip(other: Iterable): List> +public infix fun ShortArray.zip(other: Iterable): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ -public expect infix fun IntArray.zip(other: Iterable): List> +public infix fun IntArray.zip(other: Iterable): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ -public expect infix fun LongArray.zip(other: Iterable): List> +public infix fun LongArray.zip(other: Iterable): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ -public expect infix fun FloatArray.zip(other: Iterable): List> +public infix fun FloatArray.zip(other: Iterable): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ -public expect infix fun DoubleArray.zip(other: Iterable): List> +public infix fun DoubleArray.zip(other: Iterable): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ -public expect infix fun BooleanArray.zip(other: Iterable): List> +public infix fun BooleanArray.zip(other: Iterable): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ -public expect infix fun CharArray.zip(other: Iterable): List> +public infix fun CharArray.zip(other: Iterable): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} /** * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public expect inline fun Array.zip(other: Iterable, transform: (a: T, b: R) -> V): List +public inline fun Array.zip(other: Iterable, transform: (a: T, b: R) -> V): List { + val arraySize = size + val list = ArrayList(minOf(other.collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in other) { + if (i >= arraySize) break + list.add(transform(this[i++], element)) + } + return list +} /** * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public expect inline fun ByteArray.zip(other: Iterable, transform: (a: Byte, b: R) -> V): List +public inline fun ByteArray.zip(other: Iterable, transform: (a: Byte, b: R) -> V): List { + val arraySize = size + val list = ArrayList(minOf(other.collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in other) { + if (i >= arraySize) break + list.add(transform(this[i++], element)) + } + return list +} /** * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public expect inline fun ShortArray.zip(other: Iterable, transform: (a: Short, b: R) -> V): List +public inline fun ShortArray.zip(other: Iterable, transform: (a: Short, b: R) -> V): List { + val arraySize = size + val list = ArrayList(minOf(other.collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in other) { + if (i >= arraySize) break + list.add(transform(this[i++], element)) + } + return list +} /** * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public expect inline fun IntArray.zip(other: Iterable, transform: (a: Int, b: R) -> V): List +public inline fun IntArray.zip(other: Iterable, transform: (a: Int, b: R) -> V): List { + val arraySize = size + val list = ArrayList(minOf(other.collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in other) { + if (i >= arraySize) break + list.add(transform(this[i++], element)) + } + return list +} /** * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public expect inline fun LongArray.zip(other: Iterable, transform: (a: Long, b: R) -> V): List +public inline fun LongArray.zip(other: Iterable, transform: (a: Long, b: R) -> V): List { + val arraySize = size + val list = ArrayList(minOf(other.collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in other) { + if (i >= arraySize) break + list.add(transform(this[i++], element)) + } + return list +} /** * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public expect inline fun FloatArray.zip(other: Iterable, transform: (a: Float, b: R) -> V): List +public inline fun FloatArray.zip(other: Iterable, transform: (a: Float, b: R) -> V): List { + val arraySize = size + val list = ArrayList(minOf(other.collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in other) { + if (i >= arraySize) break + list.add(transform(this[i++], element)) + } + return list +} /** * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public expect inline fun DoubleArray.zip(other: Iterable, transform: (a: Double, b: R) -> V): List +public inline fun DoubleArray.zip(other: Iterable, transform: (a: Double, b: R) -> V): List { + val arraySize = size + val list = ArrayList(minOf(other.collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in other) { + if (i >= arraySize) break + list.add(transform(this[i++], element)) + } + return list +} /** * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public expect inline fun BooleanArray.zip(other: Iterable, transform: (a: Boolean, b: R) -> V): List +public inline fun BooleanArray.zip(other: Iterable, transform: (a: Boolean, b: R) -> V): List { + val arraySize = size + val list = ArrayList(minOf(other.collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in other) { + if (i >= arraySize) break + list.add(transform(this[i++], element)) + } + return list +} /** * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public expect inline fun CharArray.zip(other: Iterable, transform: (a: Char, b: R) -> V): List +public inline fun CharArray.zip(other: Iterable, transform: (a: Char, b: R) -> V): List { + val arraySize = size + val list = ArrayList(minOf(other.collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in other) { + if (i >= arraySize) break + list.add(transform(this[i++], element)) + } + return list +} /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ -public expect infix fun ByteArray.zip(other: ByteArray): List> +public infix fun ByteArray.zip(other: ByteArray): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ -public expect infix fun ShortArray.zip(other: ShortArray): List> +public infix fun ShortArray.zip(other: ShortArray): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ -public expect infix fun IntArray.zip(other: IntArray): List> +public infix fun IntArray.zip(other: IntArray): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ -public expect infix fun LongArray.zip(other: LongArray): List> +public infix fun LongArray.zip(other: LongArray): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ -public expect infix fun FloatArray.zip(other: FloatArray): List> +public infix fun FloatArray.zip(other: FloatArray): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ -public expect infix fun DoubleArray.zip(other: DoubleArray): List> +public infix fun DoubleArray.zip(other: DoubleArray): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ -public expect infix fun BooleanArray.zip(other: BooleanArray): List> +public infix fun BooleanArray.zip(other: BooleanArray): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ -public expect infix fun CharArray.zip(other: CharArray): List> +public infix fun CharArray.zip(other: CharArray): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} /** * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public expect inline fun ByteArray.zip(other: ByteArray, transform: (a: Byte, b: Byte) -> V): List +public inline fun ByteArray.zip(other: ByteArray, transform: (a: Byte, b: Byte) -> V): List { + val size = minOf(size, other.size) + val list = ArrayList(size) + for (i in 0 until size) { + list.add(transform(this[i], other[i])) + } + return list +} /** * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public expect inline fun ShortArray.zip(other: ShortArray, transform: (a: Short, b: Short) -> V): List +public inline fun ShortArray.zip(other: ShortArray, transform: (a: Short, b: Short) -> V): List { + val size = minOf(size, other.size) + val list = ArrayList(size) + for (i in 0 until size) { + list.add(transform(this[i], other[i])) + } + return list +} /** * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public expect inline fun IntArray.zip(other: IntArray, transform: (a: Int, b: Int) -> V): List +public inline fun IntArray.zip(other: IntArray, transform: (a: Int, b: Int) -> V): List { + val size = minOf(size, other.size) + val list = ArrayList(size) + for (i in 0 until size) { + list.add(transform(this[i], other[i])) + } + return list +} /** * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public expect inline fun LongArray.zip(other: LongArray, transform: (a: Long, b: Long) -> V): List +public inline fun LongArray.zip(other: LongArray, transform: (a: Long, b: Long) -> V): List { + val size = minOf(size, other.size) + val list = ArrayList(size) + for (i in 0 until size) { + list.add(transform(this[i], other[i])) + } + return list +} /** * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public expect inline fun FloatArray.zip(other: FloatArray, transform: (a: Float, b: Float) -> V): List +public inline fun FloatArray.zip(other: FloatArray, transform: (a: Float, b: Float) -> V): List { + val size = minOf(size, other.size) + val list = ArrayList(size) + for (i in 0 until size) { + list.add(transform(this[i], other[i])) + } + return list +} /** * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public expect inline fun DoubleArray.zip(other: DoubleArray, transform: (a: Double, b: Double) -> V): List +public inline fun DoubleArray.zip(other: DoubleArray, transform: (a: Double, b: Double) -> V): List { + val size = minOf(size, other.size) + val list = ArrayList(size) + for (i in 0 until size) { + list.add(transform(this[i], other[i])) + } + return list +} /** * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public expect inline fun BooleanArray.zip(other: BooleanArray, transform: (a: Boolean, b: Boolean) -> V): List +public inline fun BooleanArray.zip(other: BooleanArray, transform: (a: Boolean, b: Boolean) -> V): List { + val size = minOf(size, other.size) + val list = ArrayList(size) + for (i in 0 until size) { + list.add(transform(this[i], other[i])) + } + return list +} /** * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public expect inline fun CharArray.zip(other: CharArray, transform: (a: Char, b: Char) -> V): List +public inline fun CharArray.zip(other: CharArray, transform: (a: Char, b: Char) -> V): List { + val size = minOf(size, other.size) + val list = ArrayList(size) + for (i in 0 until size) { + list.add(transform(this[i], other[i])) + } + return list +} /** * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. @@ -7796,240 +13043,462 @@ public fun CharArray.joinToString(separator: CharSequence = ", ", prefix: CharSe /** * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. */ -public expect fun Array.asIterable(): Iterable +public fun Array.asIterable(): Iterable { + if (isEmpty()) return emptyList() + return Iterable { this.iterator() } +} /** * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. */ -public expect fun ByteArray.asIterable(): Iterable +public fun ByteArray.asIterable(): Iterable { + if (isEmpty()) return emptyList() + return Iterable { this.iterator() } +} /** * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. */ -public expect fun ShortArray.asIterable(): Iterable +public fun ShortArray.asIterable(): Iterable { + if (isEmpty()) return emptyList() + return Iterable { this.iterator() } +} /** * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. */ -public expect fun IntArray.asIterable(): Iterable +public fun IntArray.asIterable(): Iterable { + if (isEmpty()) return emptyList() + return Iterable { this.iterator() } +} /** * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. */ -public expect fun LongArray.asIterable(): Iterable +public fun LongArray.asIterable(): Iterable { + if (isEmpty()) return emptyList() + return Iterable { this.iterator() } +} /** * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. */ -public expect fun FloatArray.asIterable(): Iterable +public fun FloatArray.asIterable(): Iterable { + if (isEmpty()) return emptyList() + return Iterable { this.iterator() } +} /** * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. */ -public expect fun DoubleArray.asIterable(): Iterable +public fun DoubleArray.asIterable(): Iterable { + if (isEmpty()) return emptyList() + return Iterable { this.iterator() } +} /** * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. */ -public expect fun BooleanArray.asIterable(): Iterable +public fun BooleanArray.asIterable(): Iterable { + if (isEmpty()) return emptyList() + return Iterable { this.iterator() } +} /** * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. */ -public expect fun CharArray.asIterable(): Iterable +public fun CharArray.asIterable(): Iterable { + if (isEmpty()) return emptyList() + return Iterable { this.iterator() } +} /** * Creates a [Sequence] instance that wraps the original array returning its elements when being iterated. * * @sample samples.collections.Sequences.Building.sequenceFromArray */ -public expect fun Array.asSequence(): Sequence +public fun Array.asSequence(): Sequence { + if (isEmpty()) return emptySequence() + return Sequence { this.iterator() } +} /** * Creates a [Sequence] instance that wraps the original array returning its elements when being iterated. * * @sample samples.collections.Sequences.Building.sequenceFromArray */ -public expect fun ByteArray.asSequence(): Sequence +public fun ByteArray.asSequence(): Sequence { + if (isEmpty()) return emptySequence() + return Sequence { this.iterator() } +} /** * Creates a [Sequence] instance that wraps the original array returning its elements when being iterated. * * @sample samples.collections.Sequences.Building.sequenceFromArray */ -public expect fun ShortArray.asSequence(): Sequence +public fun ShortArray.asSequence(): Sequence { + if (isEmpty()) return emptySequence() + return Sequence { this.iterator() } +} /** * Creates a [Sequence] instance that wraps the original array returning its elements when being iterated. * * @sample samples.collections.Sequences.Building.sequenceFromArray */ -public expect fun IntArray.asSequence(): Sequence +public fun IntArray.asSequence(): Sequence { + if (isEmpty()) return emptySequence() + return Sequence { this.iterator() } +} /** * Creates a [Sequence] instance that wraps the original array returning its elements when being iterated. * * @sample samples.collections.Sequences.Building.sequenceFromArray */ -public expect fun LongArray.asSequence(): Sequence +public fun LongArray.asSequence(): Sequence { + if (isEmpty()) return emptySequence() + return Sequence { this.iterator() } +} /** * Creates a [Sequence] instance that wraps the original array returning its elements when being iterated. * * @sample samples.collections.Sequences.Building.sequenceFromArray */ -public expect fun FloatArray.asSequence(): Sequence +public fun FloatArray.asSequence(): Sequence { + if (isEmpty()) return emptySequence() + return Sequence { this.iterator() } +} /** * Creates a [Sequence] instance that wraps the original array returning its elements when being iterated. * * @sample samples.collections.Sequences.Building.sequenceFromArray */ -public expect fun DoubleArray.asSequence(): Sequence +public fun DoubleArray.asSequence(): Sequence { + if (isEmpty()) return emptySequence() + return Sequence { this.iterator() } +} /** * Creates a [Sequence] instance that wraps the original array returning its elements when being iterated. * * @sample samples.collections.Sequences.Building.sequenceFromArray */ -public expect fun BooleanArray.asSequence(): Sequence +public fun BooleanArray.asSequence(): Sequence { + if (isEmpty()) return emptySequence() + return Sequence { this.iterator() } +} /** * Creates a [Sequence] instance that wraps the original array returning its elements when being iterated. * * @sample samples.collections.Sequences.Building.sequenceFromArray */ -public expect fun CharArray.asSequence(): Sequence +public fun CharArray.asSequence(): Sequence { + if (isEmpty()) return emptySequence() + return Sequence { this.iterator() } +} /** * Returns an average value of elements in the array. */ @kotlin.jvm.JvmName("averageOfByte") -public expect fun Array.average(): Double +public fun Array.average(): Double { + var sum: Double = 0.0 + var count: Int = 0 + for (element in this) { + sum += element + count += 1 + } + return if (count == 0) Double.NaN else sum / count +} /** * Returns an average value of elements in the array. */ @kotlin.jvm.JvmName("averageOfShort") -public expect fun Array.average(): Double +public fun Array.average(): Double { + var sum: Double = 0.0 + var count: Int = 0 + for (element in this) { + sum += element + count += 1 + } + return if (count == 0) Double.NaN else sum / count +} /** * Returns an average value of elements in the array. */ @kotlin.jvm.JvmName("averageOfInt") -public expect fun Array.average(): Double +public fun Array.average(): Double { + var sum: Double = 0.0 + var count: Int = 0 + for (element in this) { + sum += element + count += 1 + } + return if (count == 0) Double.NaN else sum / count +} /** * Returns an average value of elements in the array. */ @kotlin.jvm.JvmName("averageOfLong") -public expect fun Array.average(): Double +public fun Array.average(): Double { + var sum: Double = 0.0 + var count: Int = 0 + for (element in this) { + sum += element + count += 1 + } + return if (count == 0) Double.NaN else sum / count +} /** * Returns an average value of elements in the array. */ @kotlin.jvm.JvmName("averageOfFloat") -public expect fun Array.average(): Double +public fun Array.average(): Double { + var sum: Double = 0.0 + var count: Int = 0 + for (element in this) { + sum += element + count += 1 + } + return if (count == 0) Double.NaN else sum / count +} /** * Returns an average value of elements in the array. */ @kotlin.jvm.JvmName("averageOfDouble") -public expect fun Array.average(): Double +public fun Array.average(): Double { + var sum: Double = 0.0 + var count: Int = 0 + for (element in this) { + sum += element + count += 1 + } + return if (count == 0) Double.NaN else sum / count +} /** * Returns an average value of elements in the array. */ -public expect fun ByteArray.average(): Double +public fun ByteArray.average(): Double { + var sum: Double = 0.0 + var count: Int = 0 + for (element in this) { + sum += element + count += 1 + } + return if (count == 0) Double.NaN else sum / count +} /** * Returns an average value of elements in the array. */ -public expect fun ShortArray.average(): Double +public fun ShortArray.average(): Double { + var sum: Double = 0.0 + var count: Int = 0 + for (element in this) { + sum += element + count += 1 + } + return if (count == 0) Double.NaN else sum / count +} /** * Returns an average value of elements in the array. */ -public expect fun IntArray.average(): Double +public fun IntArray.average(): Double { + var sum: Double = 0.0 + var count: Int = 0 + for (element in this) { + sum += element + count += 1 + } + return if (count == 0) Double.NaN else sum / count +} /** * Returns an average value of elements in the array. */ -public expect fun LongArray.average(): Double +public fun LongArray.average(): Double { + var sum: Double = 0.0 + var count: Int = 0 + for (element in this) { + sum += element + count += 1 + } + return if (count == 0) Double.NaN else sum / count +} /** * Returns an average value of elements in the array. */ -public expect fun FloatArray.average(): Double +public fun FloatArray.average(): Double { + var sum: Double = 0.0 + var count: Int = 0 + for (element in this) { + sum += element + count += 1 + } + return if (count == 0) Double.NaN else sum / count +} /** * Returns an average value of elements in the array. */ -public expect fun DoubleArray.average(): Double +public fun DoubleArray.average(): Double { + var sum: Double = 0.0 + var count: Int = 0 + for (element in this) { + sum += element + count += 1 + } + return if (count == 0) Double.NaN else sum / count +} /** * Returns the sum of all elements in the array. */ @kotlin.jvm.JvmName("sumOfByte") -public expect fun Array.sum(): Int +public fun Array.sum(): Int { + var sum: Int = 0 + for (element in this) { + sum += element + } + return sum +} /** * Returns the sum of all elements in the array. */ @kotlin.jvm.JvmName("sumOfShort") -public expect fun Array.sum(): Int +public fun Array.sum(): Int { + var sum: Int = 0 + for (element in this) { + sum += element + } + return sum +} /** * Returns the sum of all elements in the array. */ @kotlin.jvm.JvmName("sumOfInt") -public expect fun Array.sum(): Int +public fun Array.sum(): Int { + var sum: Int = 0 + for (element in this) { + sum += element + } + return sum +} /** * Returns the sum of all elements in the array. */ @kotlin.jvm.JvmName("sumOfLong") -public expect fun Array.sum(): Long +public fun Array.sum(): Long { + var sum: Long = 0L + for (element in this) { + sum += element + } + return sum +} /** * Returns the sum of all elements in the array. */ @kotlin.jvm.JvmName("sumOfFloat") -public expect fun Array.sum(): Float +public fun Array.sum(): Float { + var sum: Float = 0.0f + for (element in this) { + sum += element + } + return sum +} /** * Returns the sum of all elements in the array. */ @kotlin.jvm.JvmName("sumOfDouble") -public expect fun Array.sum(): Double +public fun Array.sum(): Double { + var sum: Double = 0.0 + for (element in this) { + sum += element + } + return sum +} /** * Returns the sum of all elements in the array. */ -public expect fun ByteArray.sum(): Int +public fun ByteArray.sum(): Int { + var sum: Int = 0 + for (element in this) { + sum += element + } + return sum +} /** * Returns the sum of all elements in the array. */ -public expect fun ShortArray.sum(): Int +public fun ShortArray.sum(): Int { + var sum: Int = 0 + for (element in this) { + sum += element + } + return sum +} /** * Returns the sum of all elements in the array. */ -public expect fun IntArray.sum(): Int +public fun IntArray.sum(): Int { + var sum: Int = 0 + for (element in this) { + sum += element + } + return sum +} /** * Returns the sum of all elements in the array. */ -public expect fun LongArray.sum(): Long +public fun LongArray.sum(): Long { + var sum: Long = 0L + for (element in this) { + sum += element + } + return sum +} /** * Returns the sum of all elements in the array. */ -public expect fun FloatArray.sum(): Float +public fun FloatArray.sum(): Float { + var sum: Float = 0.0f + for (element in this) { + sum += element + } + return sum +} /** * Returns the sum of all elements in the array. */ -public expect fun DoubleArray.sum(): Double +public fun DoubleArray.sum(): Double { + var sum: Double = 0.0 + for (element in this) { + sum += element + } + return sum +} diff --git a/libraries/stdlib/common/src/generated/_Collections.kt b/libraries/stdlib/common/src/generated/_Collections.kt index 3551b48fc71..bdf8f729e2f 100644 --- a/libraries/stdlib/common/src/generated/_Collections.kt +++ b/libraries/stdlib/common/src/generated/_Collections.kt @@ -16,367 +16,791 @@ import kotlin.comparisons.* * Returns 1st *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun List.component1(): T +public inline operator fun List.component1(): T { + return get(0) +} /** * Returns 2nd *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun List.component2(): T +public inline operator fun List.component2(): T { + return get(1) +} /** * Returns 3rd *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun List.component3(): T +public inline operator fun List.component3(): T { + return get(2) +} /** * Returns 4th *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun List.component4(): T +public inline operator fun List.component4(): T { + return get(3) +} /** * Returns 5th *element* from the collection. */ @kotlin.internal.InlineOnly -public expect inline operator fun List.component5(): T +public inline operator fun List.component5(): T { + return get(4) +} /** * Returns `true` if [element] is found in the collection. */ -public expect operator fun <@kotlin.internal.OnlyInputTypes T> Iterable.contains(element: T): Boolean +public operator fun <@kotlin.internal.OnlyInputTypes T> Iterable.contains(element: T): Boolean { + if (this is Collection) + return contains(element) + return indexOf(element) >= 0 +} /** * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. */ -public expect fun Iterable.elementAt(index: Int): T +public fun Iterable.elementAt(index: Int): T { + if (this is List) + return get(index) + return elementAtOrElse(index) { throw IndexOutOfBoundsException("Collection doesn't contain element at index $index.") } +} /** * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this list. */ @kotlin.internal.InlineOnly -public expect inline fun List.elementAt(index: Int): T +public inline fun List.elementAt(index: Int): T { + return get(index) +} /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. */ -public expect fun Iterable.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T +public fun Iterable.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T { + if (this is List) + return this.getOrElse(index, defaultValue) + if (index < 0) + return defaultValue(index) + val iterator = iterator() + var count = 0 + while (iterator.hasNext()) { + val element = iterator.next() + if (index == count++) + return element + } + return defaultValue(index) +} /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this list. */ @kotlin.internal.InlineOnly -public expect inline fun List.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T +public inline fun List.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T { + return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) +} /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. */ -public expect fun Iterable.elementAtOrNull(index: Int): T? +public fun Iterable.elementAtOrNull(index: Int): T? { + if (this is List) + return this.getOrNull(index) + if (index < 0) + return null + val iterator = iterator() + var count = 0 + while (iterator.hasNext()) { + val element = iterator.next() + if (index == count++) + return element + } + return null +} /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this list. */ @kotlin.internal.InlineOnly -public expect inline fun List.elementAtOrNull(index: Int): T? +public inline fun List.elementAtOrNull(index: Int): T? { + return this.getOrNull(index) +} /** * Returns the first element matching the given [predicate], or `null` if no such element was found. */ @kotlin.internal.InlineOnly -public expect inline fun Iterable.find(predicate: (T) -> Boolean): T? +public inline fun Iterable.find(predicate: (T) -> Boolean): T? { + return firstOrNull(predicate) +} /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ @kotlin.internal.InlineOnly -public expect inline fun Iterable.findLast(predicate: (T) -> Boolean): T? +public inline fun Iterable.findLast(predicate: (T) -> Boolean): T? { + return lastOrNull(predicate) +} /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ @kotlin.internal.InlineOnly -public expect inline fun List.findLast(predicate: (T) -> Boolean): T? +public inline fun List.findLast(predicate: (T) -> Boolean): T? { + return lastOrNull(predicate) +} /** * Returns first element. * @throws [NoSuchElementException] if the collection is empty. */ -public expect fun Iterable.first(): T +public fun Iterable.first(): T { + when (this) { + is List -> return this.first() + else -> { + val iterator = iterator() + if (!iterator.hasNext()) + throw NoSuchElementException("Collection is empty.") + return iterator.next() + } + } +} /** * Returns first element. * @throws [NoSuchElementException] if the list is empty. */ -public expect fun List.first(): T +public fun List.first(): T { + if (isEmpty()) + throw NoSuchElementException("List is empty.") + return this[0] +} /** * Returns the first element matching the given [predicate]. * @throws [NoSuchElementException] if no such element is found. */ -public expect inline fun Iterable.first(predicate: (T) -> Boolean): T +public inline fun Iterable.first(predicate: (T) -> Boolean): T { + for (element in this) if (predicate(element)) return element + throw NoSuchElementException("Collection contains no element matching the predicate.") +} /** * Returns the first element, or `null` if the collection is empty. */ -public expect fun Iterable.firstOrNull(): T? +public fun Iterable.firstOrNull(): T? { + when (this) { + is List -> { + if (isEmpty()) + return null + else + return this[0] + } + else -> { + val iterator = iterator() + if (!iterator.hasNext()) + return null + return iterator.next() + } + } +} /** * Returns the first element, or `null` if the list is empty. */ -public expect fun List.firstOrNull(): T? +public fun List.firstOrNull(): T? { + return if (isEmpty()) null else this[0] +} /** * Returns the first element matching the given [predicate], or `null` if element was not found. */ -public expect inline fun Iterable.firstOrNull(predicate: (T) -> Boolean): T? +public inline fun Iterable.firstOrNull(predicate: (T) -> Boolean): T? { + for (element in this) if (predicate(element)) return element + return null +} /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this list. */ @kotlin.internal.InlineOnly -public expect inline fun List.getOrElse(index: Int, defaultValue: (Int) -> T): T +public inline fun List.getOrElse(index: Int, defaultValue: (Int) -> T): T { + return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) +} /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this list. */ -public expect fun List.getOrNull(index: Int): T? +public fun List.getOrNull(index: Int): T? { + return if (index >= 0 && index <= lastIndex) get(index) else null +} /** * Returns first index of [element], or -1 if the collection does not contain element. */ -public expect fun <@kotlin.internal.OnlyInputTypes T> Iterable.indexOf(element: T): Int +public fun <@kotlin.internal.OnlyInputTypes T> Iterable.indexOf(element: T): Int { + if (this is List) return this.indexOf(element) + var index = 0 + for (item in this) { + if (element == item) + return index + index++ + } + return -1 +} /** * Returns first index of [element], or -1 if the list does not contain element. */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER") // false warning, extension takes precedence in some cases -public expect fun <@kotlin.internal.OnlyInputTypes T> List.indexOf(element: T): Int +public fun <@kotlin.internal.OnlyInputTypes T> List.indexOf(element: T): Int { + return indexOf(element) +} /** * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element. */ -public expect inline fun Iterable.indexOfFirst(predicate: (T) -> Boolean): Int +public inline fun Iterable.indexOfFirst(predicate: (T) -> Boolean): Int { + var index = 0 + for (item in this) { + if (predicate(item)) + return index + index++ + } + return -1 +} /** * Returns index of the first element matching the given [predicate], or -1 if the list does not contain such element. */ -public expect inline fun List.indexOfFirst(predicate: (T) -> Boolean): Int +public inline fun List.indexOfFirst(predicate: (T) -> Boolean): Int { + var index = 0 + for (item in this) { + if (predicate(item)) + return index + index++ + } + return -1 +} /** * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element. */ -public expect inline fun Iterable.indexOfLast(predicate: (T) -> Boolean): Int +public inline fun Iterable.indexOfLast(predicate: (T) -> Boolean): Int { + var lastIndex = -1 + var index = 0 + for (item in this) { + if (predicate(item)) + lastIndex = index + index++ + } + return lastIndex +} /** * Returns index of the last element matching the given [predicate], or -1 if the list does not contain such element. */ -public expect inline fun List.indexOfLast(predicate: (T) -> Boolean): Int +public inline fun List.indexOfLast(predicate: (T) -> Boolean): Int { + val iterator = this.listIterator(size) + while (iterator.hasPrevious()) { + if (predicate(iterator.previous())) { + return iterator.nextIndex() + } + } + return -1 +} /** * Returns the last element. * @throws [NoSuchElementException] if the collection is empty. */ -public expect fun Iterable.last(): T +public fun Iterable.last(): T { + when (this) { + is List -> return this.last() + else -> { + val iterator = iterator() + if (!iterator.hasNext()) + throw NoSuchElementException("Collection is empty.") + var last = iterator.next() + while (iterator.hasNext()) + last = iterator.next() + return last + } + } +} /** * Returns the last element. * @throws [NoSuchElementException] if the list is empty. */ -public expect fun List.last(): T +public fun List.last(): T { + if (isEmpty()) + throw NoSuchElementException("List is empty.") + return this[lastIndex] +} /** * Returns the last element matching the given [predicate]. * @throws [NoSuchElementException] if no such element is found. */ -public expect inline fun Iterable.last(predicate: (T) -> Boolean): T +public inline fun Iterable.last(predicate: (T) -> Boolean): T { + var last: T? = null + var found = false + for (element in this) { + if (predicate(element)) { + last = element + found = true + } + } + if (!found) throw NoSuchElementException("Collection contains no element matching the predicate.") + @Suppress("UNCHECKED_CAST") + return last as T +} /** * Returns the last element matching the given [predicate]. * @throws [NoSuchElementException] if no such element is found. */ -public expect inline fun List.last(predicate: (T) -> Boolean): T +public inline fun List.last(predicate: (T) -> Boolean): T { + val iterator = this.listIterator(size) + while (iterator.hasPrevious()) { + val element = iterator.previous() + if (predicate(element)) return element + } + throw NoSuchElementException("List contains no element matching the predicate.") +} /** * Returns last index of [element], or -1 if the collection does not contain element. */ -public expect fun <@kotlin.internal.OnlyInputTypes T> Iterable.lastIndexOf(element: T): Int +public fun <@kotlin.internal.OnlyInputTypes T> Iterable.lastIndexOf(element: T): Int { + if (this is List) return this.lastIndexOf(element) + var lastIndex = -1 + var index = 0 + for (item in this) { + if (element == item) + lastIndex = index + index++ + } + return lastIndex +} /** * Returns last index of [element], or -1 if the list does not contain element. */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER") // false warning, extension takes precedence in some cases -public expect fun <@kotlin.internal.OnlyInputTypes T> List.lastIndexOf(element: T): Int +public fun <@kotlin.internal.OnlyInputTypes T> List.lastIndexOf(element: T): Int { + return lastIndexOf(element) +} /** * Returns the last element, or `null` if the collection is empty. */ -public expect fun Iterable.lastOrNull(): T? +public fun Iterable.lastOrNull(): T? { + when (this) { + is List -> return if (isEmpty()) null else this[size - 1] + else -> { + val iterator = iterator() + if (!iterator.hasNext()) + return null + var last = iterator.next() + while (iterator.hasNext()) + last = iterator.next() + return last + } + } +} /** * Returns the last element, or `null` if the list is empty. */ -public expect fun List.lastOrNull(): T? +public fun List.lastOrNull(): T? { + return if (isEmpty()) null else this[size - 1] +} /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ -public expect inline fun Iterable.lastOrNull(predicate: (T) -> Boolean): T? +public inline fun Iterable.lastOrNull(predicate: (T) -> Boolean): T? { + var last: T? = null + for (element in this) { + if (predicate(element)) { + last = element + } + } + return last +} /** * Returns the last element matching the given [predicate], or `null` if no such element was found. */ -public expect inline fun List.lastOrNull(predicate: (T) -> Boolean): T? +public inline fun List.lastOrNull(predicate: (T) -> Boolean): T? { + val iterator = this.listIterator(size) + while (iterator.hasPrevious()) { + val element = iterator.previous() + if (predicate(element)) return element + } + return null +} /** * Returns the single element, or throws an exception if the collection is empty or has more than one element. */ -public expect fun Iterable.single(): T +public fun Iterable.single(): T { + when (this) { + is List -> return this.single() + else -> { + val iterator = iterator() + if (!iterator.hasNext()) + throw NoSuchElementException("Collection is empty.") + val single = iterator.next() + if (iterator.hasNext()) + throw IllegalArgumentException("Collection has more than one element.") + return single + } + } +} /** * Returns the single element, or throws an exception if the list is empty or has more than one element. */ -public expect fun List.single(): T +public fun List.single(): T { + return when (size) { + 0 -> throw NoSuchElementException("List is empty.") + 1 -> this[0] + else -> throw IllegalArgumentException("List has more than one element.") + } +} /** * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. */ -public expect inline fun Iterable.single(predicate: (T) -> Boolean): T +public inline fun Iterable.single(predicate: (T) -> Boolean): T { + var single: T? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) throw IllegalArgumentException("Collection contains more than one matching element.") + single = element + found = true + } + } + if (!found) throw NoSuchElementException("Collection contains no element matching the predicate.") + @Suppress("UNCHECKED_CAST") + return single as T +} /** * Returns single element, or `null` if the collection is empty or has more than one element. */ -public expect fun Iterable.singleOrNull(): T? +public fun Iterable.singleOrNull(): T? { + when (this) { + is List -> return if (size == 1) this[0] else null + else -> { + val iterator = iterator() + if (!iterator.hasNext()) + return null + val single = iterator.next() + if (iterator.hasNext()) + return null + return single + } + } +} /** * Returns single element, or `null` if the list is empty or has more than one element. */ -public expect fun List.singleOrNull(): T? +public fun List.singleOrNull(): T? { + return if (size == 1) this[0] else null +} /** * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. */ -public expect inline fun Iterable.singleOrNull(predicate: (T) -> Boolean): T? +public inline fun Iterable.singleOrNull(predicate: (T) -> Boolean): T? { + var single: T? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) return null + single = element + found = true + } + } + if (!found) return null + return single +} /** * Returns a list containing all elements except first [n] elements. * * @sample samples.collections.Collections.Transformations.drop */ -public expect fun Iterable.drop(n: Int): List +public fun Iterable.drop(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + if (n == 0) return toList() + val list: ArrayList + if (this is Collection<*>) { + val resultSize = size - n + if (resultSize <= 0) + return emptyList() + if (resultSize == 1) + return listOf(last()) + list = ArrayList(resultSize) + if (this is List) { + if (this is RandomAccess) { + for (index in n until size) + list.add(this[index]) + } else { + for (item in listIterator(n)) + list.add(item) + } + return list + } + } + else { + list = ArrayList() + } + var count = 0 + for (item in this) { + if (count++ >= n) list.add(item) + } + return list.optimizeReadOnlyList() +} /** * Returns a list containing all elements except last [n] elements. * * @sample samples.collections.Collections.Transformations.drop */ -public expect fun List.dropLast(n: Int): List +public fun List.dropLast(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + return take((size - n).coerceAtLeast(0)) +} /** * Returns a list containing all elements except last elements that satisfy the given [predicate]. * * @sample samples.collections.Collections.Transformations.drop */ -public expect inline fun List.dropLastWhile(predicate: (T) -> Boolean): List +public inline fun List.dropLastWhile(predicate: (T) -> Boolean): List { + if (!isEmpty()) { + val iterator = listIterator(size) + while (iterator.hasPrevious()) { + if (!predicate(iterator.previous())) { + return take(iterator.nextIndex() + 1) + } + } + } + return emptyList() +} /** * Returns a list containing all elements except first elements that satisfy the given [predicate]. * * @sample samples.collections.Collections.Transformations.drop */ -public expect inline fun Iterable.dropWhile(predicate: (T) -> Boolean): List +public inline fun Iterable.dropWhile(predicate: (T) -> Boolean): List { + var yielding = false + val list = ArrayList() + for (item in this) + if (yielding) + list.add(item) + else if (!predicate(item)) { + list.add(item) + yielding = true + } + return list +} /** * Returns a list containing only elements matching the given [predicate]. */ -public expect inline fun Iterable.filter(predicate: (T) -> Boolean): List +public inline fun Iterable.filter(predicate: (T) -> Boolean): List { + return filterTo(ArrayList(), predicate) +} /** * Returns a list containing only elements matching the given [predicate]. * @param [predicate] function that takes the index of an element and the element itself * and returns the result of predicate evaluation on the element. */ -public expect inline fun Iterable.filterIndexed(predicate: (index: Int, T) -> Boolean): List +public inline fun Iterable.filterIndexed(predicate: (index: Int, T) -> Boolean): List { + return filterIndexedTo(ArrayList(), predicate) +} /** * Appends all elements matching the given [predicate] to the given [destination]. * @param [predicate] function that takes the index of an element and the element itself * and returns the result of predicate evaluation on the element. */ -public expect inline fun > Iterable.filterIndexedTo(destination: C, predicate: (index: Int, T) -> Boolean): C +public inline fun > Iterable.filterIndexedTo(destination: C, predicate: (index: Int, T) -> Boolean): C { + forEachIndexed { index, element -> + if (predicate(index, element)) destination.add(element) + } + return destination +} /** * Returns a list containing all elements that are instances of specified type parameter R. */ -public expect inline fun Iterable<*>.filterIsInstance(): List<@kotlin.internal.NoInfer R> +public inline fun Iterable<*>.filterIsInstance(): List<@kotlin.internal.NoInfer R> { + return filterIsInstanceTo(ArrayList()) +} /** * Appends all elements that are instances of specified type parameter R to the given [destination]. */ -public expect inline fun > Iterable<*>.filterIsInstanceTo(destination: C): C +public inline fun > Iterable<*>.filterIsInstanceTo(destination: C): C { + for (element in this) if (element is R) destination.add(element) + return destination +} /** * Returns a list containing all elements not matching the given [predicate]. */ -public expect inline fun Iterable.filterNot(predicate: (T) -> Boolean): List +public inline fun Iterable.filterNot(predicate: (T) -> Boolean): List { + return filterNotTo(ArrayList(), predicate) +} /** * Returns a list containing all elements that are not `null`. */ -public expect fun Iterable.filterNotNull(): List +public fun Iterable.filterNotNull(): List { + return filterNotNullTo(ArrayList()) +} /** * Appends all elements that are not `null` to the given [destination]. */ -public expect fun , T : Any> Iterable.filterNotNullTo(destination: C): C +public fun , T : Any> Iterable.filterNotNullTo(destination: C): C { + for (element in this) if (element != null) destination.add(element) + return destination +} /** * Appends all elements not matching the given [predicate] to the given [destination]. */ -public expect inline fun > Iterable.filterNotTo(destination: C, predicate: (T) -> Boolean): C +public inline fun > Iterable.filterNotTo(destination: C, predicate: (T) -> Boolean): C { + for (element in this) if (!predicate(element)) destination.add(element) + return destination +} /** * Appends all elements matching the given [predicate] to the given [destination]. */ -public expect inline fun > Iterable.filterTo(destination: C, predicate: (T) -> Boolean): C +public inline fun > Iterable.filterTo(destination: C, predicate: (T) -> Boolean): C { + for (element in this) if (predicate(element)) destination.add(element) + return destination +} /** * Returns a list containing elements at indices in the specified [indices] range. */ -public expect fun List.slice(indices: IntRange): List +public fun List.slice(indices: IntRange): List { + if (indices.isEmpty()) return listOf() + return this.subList(indices.start, indices.endInclusive + 1).toList() +} /** * Returns a list containing elements at specified [indices]. */ -public expect fun List.slice(indices: Iterable): List +public fun List.slice(indices: Iterable): List { + val size = indices.collectionSizeOrDefault(10) + if (size == 0) return emptyList() + val list = ArrayList(size) + for (index in indices) { + list.add(get(index)) + } + return list +} /** * Returns a list containing first [n] elements. * * @sample samples.collections.Collections.Transformations.take */ -public expect fun Iterable.take(n: Int): List +public fun Iterable.take(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + if (n == 0) return emptyList() + if (this is Collection) { + if (n >= size) return toList() + if (n == 1) return listOf(first()) + } + var count = 0 + val list = ArrayList(n) + for (item in this) { + if (count++ == n) + break + list.add(item) + } + return list.optimizeReadOnlyList() +} /** * Returns a list containing last [n] elements. * * @sample samples.collections.Collections.Transformations.take */ -public expect fun List.takeLast(n: Int): List +public fun List.takeLast(n: Int): List { + require(n >= 0) { "Requested element count $n is less than zero." } + if (n == 0) return emptyList() + val size = size + if (n >= size) return toList() + if (n == 1) return listOf(last()) + val list = ArrayList(n) + if (this is RandomAccess) { + for (index in size - n until size) + list.add(this[index]) + } else { + for (item in listIterator(size - n)) + list.add(item) + } + return list +} /** * Returns a list containing last elements satisfying the given [predicate]. * * @sample samples.collections.Collections.Transformations.take */ -public expect inline fun List.takeLastWhile(predicate: (T) -> Boolean): List +public inline fun List.takeLastWhile(predicate: (T) -> Boolean): List { + if (isEmpty()) + return emptyList() + val iterator = listIterator(size) + while (iterator.hasPrevious()) { + if (!predicate(iterator.previous())) { + iterator.next() + val expectedSize = size - iterator.nextIndex() + if (expectedSize == 0) return emptyList() + return ArrayList(expectedSize).apply { + while (iterator.hasNext()) + add(iterator.next()) + } + } + } + return toList() +} /** * Returns a list containing first elements satisfying the given [predicate]. * * @sample samples.collections.Collections.Transformations.take */ -public expect inline fun Iterable.takeWhile(predicate: (T) -> Boolean): List +public inline fun Iterable.takeWhile(predicate: (T) -> Boolean): List { + val list = ArrayList() + for (item in this) { + if (!predicate(item)) + break + list.add(item) + } + return list +} /** * Reverses elements in the list in-place. @@ -386,87 +810,166 @@ public expect fun MutableList.reverse(): Unit /** * Returns a list with elements in reversed order. */ -public expect fun Iterable.reversed(): List +public fun Iterable.reversed(): List { + if (this is Collection && size <= 1) return toList() + val list = toMutableList() + list.reverse() + return list +} /** * Sorts elements in the list in-place according to natural sort order of the value returned by specified [selector] function. */ -public expect inline fun > MutableList.sortBy(crossinline selector: (T) -> R?): Unit +public inline fun > MutableList.sortBy(crossinline selector: (T) -> R?): Unit { + if (size > 1) sortWith(compareBy(selector)) +} /** * Sorts elements in the list in-place descending according to natural sort order of the value returned by specified [selector] function. */ -public expect inline fun > MutableList.sortByDescending(crossinline selector: (T) -> R?): Unit +public inline fun > MutableList.sortByDescending(crossinline selector: (T) -> R?): Unit { + if (size > 1) sortWith(compareByDescending(selector)) +} /** * Sorts elements in the list in-place descending according to their natural sort order. */ -public expect fun > MutableList.sortDescending(): Unit +public fun > MutableList.sortDescending(): Unit { + sortWith(reverseOrder()) +} /** * Returns a list of all elements sorted according to their natural sort order. */ -public expect fun > Iterable.sorted(): List +public fun > Iterable.sorted(): List { + if (this is Collection) { + if (size <= 1) return this.toList() + @Suppress("UNCHECKED_CAST") + return (toTypedArray>() as Array).apply { sort() }.asList() + } + return toMutableList().apply { sort() } +} /** * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. */ -public expect inline fun > Iterable.sortedBy(crossinline selector: (T) -> R?): List +public inline fun > Iterable.sortedBy(crossinline selector: (T) -> R?): List { + return sortedWith(compareBy(selector)) +} /** * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. */ -public expect inline fun > Iterable.sortedByDescending(crossinline selector: (T) -> R?): List +public inline fun > Iterable.sortedByDescending(crossinline selector: (T) -> R?): List { + return sortedWith(compareByDescending(selector)) +} /** * Returns a list of all elements sorted descending according to their natural sort order. */ -public expect fun > Iterable.sortedDescending(): List +public fun > Iterable.sortedDescending(): List { + return sortedWith(reverseOrder()) +} /** * Returns a list of all elements sorted according to the specified [comparator]. */ -public expect fun Iterable.sortedWith(comparator: Comparator): List +public fun Iterable.sortedWith(comparator: Comparator): List { + if (this is Collection) { + if (size <= 1) return this.toList() + @Suppress("UNCHECKED_CAST") + return (toTypedArray() as Array).apply { sortWith(comparator) }.asList() + } + return toMutableList().apply { sortWith(comparator) } +} /** * Returns an array of Boolean containing all of the elements of this collection. */ -public expect fun Collection.toBooleanArray(): BooleanArray +public fun Collection.toBooleanArray(): BooleanArray { + val result = BooleanArray(size) + var index = 0 + for (element in this) + result[index++] = element + return result +} /** * Returns an array of Byte containing all of the elements of this collection. */ -public expect fun Collection.toByteArray(): ByteArray +public fun Collection.toByteArray(): ByteArray { + val result = ByteArray(size) + var index = 0 + for (element in this) + result[index++] = element + return result +} /** * Returns an array of Char containing all of the elements of this collection. */ -public expect fun Collection.toCharArray(): CharArray +public fun Collection.toCharArray(): CharArray { + val result = CharArray(size) + var index = 0 + for (element in this) + result[index++] = element + return result +} /** * Returns an array of Double containing all of the elements of this collection. */ -public expect fun Collection.toDoubleArray(): DoubleArray +public fun Collection.toDoubleArray(): DoubleArray { + val result = DoubleArray(size) + var index = 0 + for (element in this) + result[index++] = element + return result +} /** * Returns an array of Float containing all of the elements of this collection. */ -public expect fun Collection.toFloatArray(): FloatArray +public fun Collection.toFloatArray(): FloatArray { + val result = FloatArray(size) + var index = 0 + for (element in this) + result[index++] = element + return result +} /** * Returns an array of Int containing all of the elements of this collection. */ -public expect fun Collection.toIntArray(): IntArray +public fun Collection.toIntArray(): IntArray { + val result = IntArray(size) + var index = 0 + for (element in this) + result[index++] = element + return result +} /** * Returns an array of Long containing all of the elements of this collection. */ -public expect fun Collection.toLongArray(): LongArray +public fun Collection.toLongArray(): LongArray { + val result = LongArray(size) + var index = 0 + for (element in this) + result[index++] = element + return result +} /** * Returns an array of Short containing all of the elements of this collection. */ -public expect fun Collection.toShortArray(): ShortArray +public fun Collection.toShortArray(): ShortArray { + val result = ShortArray(size) + var index = 0 + for (element in this) + result[index++] = element + return result +} /** * Returns a [Map] containing key-value pairs provided by [transform] function @@ -476,7 +979,10 @@ public expect fun Collection.toShortArray(): ShortArray * * The returned map preserves the entry iteration order of the original collection. */ -public expect inline fun Iterable.associate(transform: (T) -> Pair): Map +public inline fun Iterable.associate(transform: (T) -> Pair): Map { + val capacity = mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16) + return associateTo(LinkedHashMap(capacity), transform) +} /** * Returns a [Map] containing the elements from the given collection indexed by the key @@ -486,7 +992,10 @@ public expect inline fun Iterable.associate(transform: (T) -> Pair< * * The returned map preserves the entry iteration order of the original collection. */ -public expect inline fun Iterable.associateBy(keySelector: (T) -> K): Map +public inline fun Iterable.associateBy(keySelector: (T) -> K): Map { + val capacity = mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector) +} /** * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given collection. @@ -495,7 +1004,10 @@ public expect inline fun Iterable.associateBy(keySelector: (T) -> K): * * The returned map preserves the entry iteration order of the original collection. */ -public expect inline fun Iterable.associateBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map +public inline fun Iterable.associateBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map { + val capacity = mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) +} /** * Populates and returns the [destination] mutable map with key-value pairs, @@ -504,7 +1016,12 @@ public expect inline fun Iterable.associateBy(keySelector: (T) -> K * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. */ -public expect inline fun > Iterable.associateByTo(destination: M, keySelector: (T) -> K): M +public inline fun > Iterable.associateByTo(destination: M, keySelector: (T) -> K): M { + for (element in this) { + destination.put(keySelector(element), element) + } + return destination +} /** * Populates and returns the [destination] mutable map with key-value pairs, @@ -513,7 +1030,12 @@ public expect inline fun > Iterable.associat * * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. */ -public expect inline fun > Iterable.associateByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M +public inline fun > Iterable.associateByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M { + for (element in this) { + destination.put(keySelector(element), valueTransform(element)) + } + return destination +} /** * Populates and returns the [destination] mutable map with key-value pairs @@ -521,49 +1043,93 @@ public expect inline fun > Iterable.assoc * * If any of two pairs would have the same key the last one gets added to the map. */ -public expect inline fun > Iterable.associateTo(destination: M, transform: (T) -> Pair): M +public inline fun > Iterable.associateTo(destination: M, transform: (T) -> Pair): M { + for (element in this) { + destination += transform(element) + } + return destination +} /** * Appends all elements to the given [destination] collection. */ -public expect fun > Iterable.toCollection(destination: C): C +public fun > Iterable.toCollection(destination: C): C { + for (item in this) { + destination.add(item) + } + return destination +} /** * Returns a [HashSet] of all elements. */ -public expect fun Iterable.toHashSet(): HashSet +public fun Iterable.toHashSet(): HashSet { + return toCollection(HashSet(mapCapacity(collectionSizeOrDefault(12)))) +} /** * Returns a [List] containing all elements. */ -public expect fun Iterable.toList(): List +public fun Iterable.toList(): List { + if (this is Collection) { + return when (size) { + 0 -> emptyList() + 1 -> listOf(if (this is List) get(0) else iterator().next()) + else -> this.toMutableList() + } + } + return this.toMutableList().optimizeReadOnlyList() +} /** * Returns a [MutableList] filled with all elements of this collection. */ -public expect fun Iterable.toMutableList(): MutableList +public fun Iterable.toMutableList(): MutableList { + if (this is Collection) + return this.toMutableList() + return toCollection(ArrayList()) +} /** * Returns a [MutableList] filled with all elements of this collection. */ -public expect fun Collection.toMutableList(): MutableList +public fun Collection.toMutableList(): MutableList { + return ArrayList(this) +} /** * Returns a [Set] of all elements. * * The returned set preserves the element iteration order of the original collection. */ -public expect fun Iterable.toSet(): Set +public fun Iterable.toSet(): Set { + if (this is Collection) { + return when (size) { + 0 -> emptySet() + 1 -> setOf(if (this is List) this[0] else iterator().next()) + else -> toCollection(LinkedHashSet(mapCapacity(size))) + } + } + return toCollection(LinkedHashSet()).optimizeReadOnlySet() +} /** * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original collection. */ -public expect inline fun Iterable.flatMap(transform: (T) -> Iterable): List +public inline fun Iterable.flatMap(transform: (T) -> Iterable): List { + return flatMapTo(ArrayList(), transform) +} /** * Appends all elements yielded from results of [transform] function being invoked on each element of original collection, to the given [destination]. */ -public expect inline fun > Iterable.flatMapTo(destination: C, transform: (T) -> Iterable): C +public inline fun > Iterable.flatMapTo(destination: C, transform: (T) -> Iterable): C { + for (element in this) { + val list = transform(element) + destination.addAll(list) + } + return destination +} /** * Groups elements of the original collection by the key returned by the given [keySelector] function @@ -573,7 +1139,9 @@ public expect inline fun > Iterable.flatMap * * @sample samples.collections.Collections.Transformations.groupBy */ -public expect inline fun Iterable.groupBy(keySelector: (T) -> K): Map> +public inline fun Iterable.groupBy(keySelector: (T) -> K): Map> { + return groupByTo(LinkedHashMap>(), keySelector) +} /** * Groups values returned by the [valueTransform] function applied to each element of the original collection @@ -584,7 +1152,9 @@ public expect inline fun Iterable.groupBy(keySelector: (T) -> K): Map< * * @sample samples.collections.Collections.Transformations.groupByKeysAndValues */ -public expect inline fun Iterable.groupBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map> +public inline fun Iterable.groupBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map> { + return groupByTo(LinkedHashMap>(), keySelector, valueTransform) +} /** * Groups elements of the original collection by the key returned by the given [keySelector] function @@ -594,7 +1164,14 @@ public expect inline fun Iterable.groupBy(keySelector: (T) -> K, va * * @sample samples.collections.Collections.Transformations.groupBy */ -public expect inline fun >> Iterable.groupByTo(destination: M, keySelector: (T) -> K): M +public inline fun >> Iterable.groupByTo(destination: M, keySelector: (T) -> K): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(element) + } + return destination +} /** * Groups values returned by the [valueTransform] function applied to each element of the original collection @@ -605,7 +1182,14 @@ public expect inline fun >> Iterable>> Iterable.groupByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M +public inline fun >> Iterable.groupByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(valueTransform(element)) + } + return destination +} /** * Creates a [Grouping] source from a collection to be used later with one of group-and-fold operations @@ -614,13 +1198,20 @@ public expect inline fun >> Iterabl * @sample samples.collections.Collections.Transformations.groupingByEachCount */ @SinceKotlin("1.1") -public expect inline fun Iterable.groupingBy(crossinline keySelector: (T) -> K): Grouping +public inline fun Iterable.groupingBy(crossinline keySelector: (T) -> K): Grouping { + return object : Grouping { + override fun sourceIterator(): Iterator = this@groupingBy.iterator() + override fun keyOf(element: T): K = keySelector(element) + } +} /** * Returns a list containing the results of applying the given [transform] function * to each element in the original collection. */ -public expect inline fun Iterable.map(transform: (T) -> R): List +public inline fun Iterable.map(transform: (T) -> R): List { + return mapTo(ArrayList(collectionSizeOrDefault(10)), transform) +} /** * Returns a list containing the results of applying the given [transform] function @@ -628,7 +1219,9 @@ public expect inline fun Iterable.map(transform: (T) -> R): List * @param [transform] function that takes the index of an element and the element itself * and returns the result of the transform applied to the element. */ -public expect inline fun Iterable.mapIndexed(transform: (index: Int, T) -> R): List +public inline fun Iterable.mapIndexed(transform: (index: Int, T) -> R): List { + return mapIndexedTo(ArrayList(collectionSizeOrDefault(10)), transform) +} /** * Returns a list containing only the non-null results of applying the given [transform] function @@ -636,7 +1229,9 @@ public expect inline fun Iterable.mapIndexed(transform: (index: Int, T * @param [transform] function that takes the index of an element and the element itself * and returns the result of the transform applied to the element. */ -public expect inline fun Iterable.mapIndexedNotNull(transform: (index: Int, T) -> R?): List +public inline fun Iterable.mapIndexedNotNull(transform: (index: Int, T) -> R?): List { + return mapIndexedNotNullTo(ArrayList(), transform) +} /** * Applies the given [transform] function to each element and its index in the original collection @@ -644,7 +1239,10 @@ public expect inline fun Iterable.mapIndexedNotNull(transform: ( * @param [transform] function that takes the index of an element and the element itself * and returns the result of the transform applied to the element. */ -public expect inline fun > Iterable.mapIndexedNotNullTo(destination: C, transform: (index: Int, T) -> R?): C +public inline fun > Iterable.mapIndexedNotNullTo(destination: C, transform: (index: Int, T) -> R?): C { + forEachIndexed { index, element -> transform(index, element)?.let { destination.add(it) } } + return destination +} /** * Applies the given [transform] function to each element and its index in the original collection @@ -652,37 +1250,55 @@ public expect inline fun > Iterable.m * @param [transform] function that takes the index of an element and the element itself * and returns the result of the transform applied to the element. */ -public expect inline fun > Iterable.mapIndexedTo(destination: C, transform: (index: Int, T) -> R): C +public inline fun > Iterable.mapIndexedTo(destination: C, transform: (index: Int, T) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} /** * Returns a list containing only the non-null results of applying the given [transform] function * to each element in the original collection. */ -public expect inline fun Iterable.mapNotNull(transform: (T) -> R?): List +public inline fun Iterable.mapNotNull(transform: (T) -> R?): List { + return mapNotNullTo(ArrayList(), transform) +} /** * Applies the given [transform] function to each element in the original collection * and appends only the non-null results to the given [destination]. */ -public expect inline fun > Iterable.mapNotNullTo(destination: C, transform: (T) -> R?): C +public inline fun > Iterable.mapNotNullTo(destination: C, transform: (T) -> R?): C { + forEach { element -> transform(element)?.let { destination.add(it) } } + return destination +} /** * Applies the given [transform] function to each element of the original collection * and appends the results to the given [destination]. */ -public expect inline fun > Iterable.mapTo(destination: C, transform: (T) -> R): C +public inline fun > Iterable.mapTo(destination: C, transform: (T) -> R): C { + for (item in this) + destination.add(transform(item)) + return destination +} /** * Returns a lazy [Iterable] of [IndexedValue] for each element of the original collection. */ -public expect fun Iterable.withIndex(): Iterable> +public fun Iterable.withIndex(): Iterable> { + return IndexingIterable { iterator() } +} /** * Returns a list containing only distinct elements from the given collection. * * The elements in the resulting list are in the same order as they were in the source collection. */ -public expect fun Iterable.distinct(): List +public fun Iterable.distinct(): List { + return this.toMutableSet().toList() +} /** * Returns a list containing only elements from the given collection @@ -690,28 +1306,50 @@ public expect fun Iterable.distinct(): List * * The elements in the resulting list are in the same order as they were in the source collection. */ -public expect inline fun Iterable.distinctBy(selector: (T) -> K): List +public inline fun Iterable.distinctBy(selector: (T) -> K): List { + val set = HashSet() + val list = ArrayList() + for (e in this) { + val key = selector(e) + if (set.add(key)) + list.add(e) + } + return list +} /** * Returns a set containing all elements that are contained by both this set and the specified collection. * * The returned set preserves the element iteration order of the original collection. */ -public expect infix fun Iterable.intersect(other: Iterable): Set +public infix fun Iterable.intersect(other: Iterable): Set { + val set = this.toMutableSet() + set.retainAll(other) + return set +} /** * Returns a set containing all elements that are contained by this collection and not contained by the specified collection. * * The returned set preserves the element iteration order of the original collection. */ -public expect infix fun Iterable.subtract(other: Iterable): Set +public infix fun Iterable.subtract(other: Iterable): Set { + val set = this.toMutableSet() + set.removeAll(other) + return set +} /** * Returns a mutable set containing all distinct elements from the given collection. * * The returned set preserves the element iteration order of the original collection. */ -public expect fun Iterable.toMutableSet(): MutableSet +public fun Iterable.toMutableSet(): MutableSet { + return when (this) { + is Collection -> LinkedHashSet(this) + else -> toCollection(LinkedHashSet()) + } +} /** * Returns a set containing all distinct elements from both collections. @@ -720,49 +1358,80 @@ public expect fun Iterable.toMutableSet(): MutableSet * Those elements of the [other] collection that are unique are iterated in the end * in the order of the [other] collection. */ -public expect infix fun Iterable.union(other: Iterable): Set +public infix fun Iterable.union(other: Iterable): Set { + val set = this.toMutableSet() + set.addAll(other) + return set +} /** * Returns `true` if all elements match the given [predicate]. * * @sample samples.collections.Collections.Aggregates.all */ -public expect inline fun Iterable.all(predicate: (T) -> Boolean): Boolean +public inline fun Iterable.all(predicate: (T) -> Boolean): Boolean { + if (this is Collection && isEmpty()) return true + for (element in this) if (!predicate(element)) return false + return true +} /** * Returns `true` if collection has at least one element. * * @sample samples.collections.Collections.Aggregates.any */ -public expect fun Iterable.any(): Boolean +public fun Iterable.any(): Boolean { + if (this is Collection) return !isEmpty() + return iterator().hasNext() +} /** * Returns `true` if at least one element matches the given [predicate]. * * @sample samples.collections.Collections.Aggregates.anyWithPredicate */ -public expect inline fun Iterable.any(predicate: (T) -> Boolean): Boolean +public inline fun Iterable.any(predicate: (T) -> Boolean): Boolean { + if (this is Collection && isEmpty()) return false + for (element in this) if (predicate(element)) return true + return false +} /** * Returns the number of elements in this collection. */ -public expect fun Iterable.count(): Int +public fun Iterable.count(): Int { + if (this is Collection) return size + var count = 0 + for (element in this) count++ + return count +} /** * Returns the number of elements in this collection. */ @kotlin.internal.InlineOnly -public expect inline fun Collection.count(): Int +public inline fun Collection.count(): Int { + return size +} /** * Returns the number of elements matching the given [predicate]. */ -public expect inline fun Iterable.count(predicate: (T) -> Boolean): Int +public inline fun Iterable.count(predicate: (T) -> Boolean): Int { + if (this is Collection && isEmpty()) return 0 + var count = 0 + for (element in this) if (predicate(element)) count++ + return count +} /** * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. */ -public expect inline fun Iterable.fold(initial: R, operation: (acc: R, T) -> R): R +public inline fun Iterable.fold(initial: R, operation: (acc: R, T) -> R): R { + var accumulator = initial + for (element in this) accumulator = operation(accumulator, element) + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from left to right @@ -770,12 +1439,26 @@ public expect inline fun Iterable.fold(initial: R, operation: (acc: R, * @param [operation] function that takes the index of an element, current accumulator value * and the element itself, and calculates the next accumulator value. */ -public expect inline fun Iterable.foldIndexed(initial: R, operation: (index: Int, acc: R, T) -> R): R +public inline fun Iterable.foldIndexed(initial: R, operation: (index: Int, acc: R, T) -> R): R { + var index = 0 + var accumulator = initial + for (element in this) accumulator = operation(index++, accumulator, element) + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. */ -public expect inline fun List.foldRight(initial: R, operation: (T, acc: R) -> R): R +public inline fun List.foldRight(initial: R, operation: (T, acc: R) -> R): R { + var accumulator = initial + if (!isEmpty()) { + val iterator = listIterator(size) + while (iterator.hasPrevious()) { + accumulator = operation(iterator.previous(), accumulator) + } + } + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from right to left @@ -783,20 +1466,35 @@ public expect inline fun List.foldRight(initial: R, operation: (T, acc * @param [operation] function that takes the index of an element, the element itself * and current accumulator value, and calculates the next accumulator value. */ -public expect inline fun List.foldRightIndexed(initial: R, operation: (index: Int, T, acc: R) -> R): R +public inline fun List.foldRightIndexed(initial: R, operation: (index: Int, T, acc: R) -> R): R { + var accumulator = initial + if (!isEmpty()) { + val iterator = listIterator(size) + while (iterator.hasPrevious()) { + val index = iterator.previousIndex() + accumulator = operation(index, iterator.previous(), accumulator) + } + } + return accumulator +} /** * Performs the given [action] on each element. */ @kotlin.internal.HidesMembers -public expect inline fun Iterable.forEach(action: (T) -> Unit): Unit +public inline fun Iterable.forEach(action: (T) -> Unit): Unit { + for (element in this) action(element) +} /** * Performs the given [action] on each element, providing sequential index with the element. * @param [action] function that takes the index of an element and the element itself * and performs the desired action on the element. */ -public expect inline fun Iterable.forEachIndexed(action: (index: Int, T) -> Unit): Unit +public inline fun Iterable.forEachIndexed(action: (index: Int, T) -> Unit): Unit { + var index = 0 + for (item in this) action(index++, item) +} /** * Returns the largest element or `null` if there are no elements. @@ -804,7 +1502,18 @@ public expect inline fun Iterable.forEachIndexed(action: (index: Int, T) * If any of elements is `NaN` returns `NaN`. */ @SinceKotlin("1.1") -public expect fun Iterable.max(): Double? +public fun Iterable.max(): Double? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var max = iterator.next() + if (max.isNaN()) return max + while (iterator.hasNext()) { + val e = iterator.next() + if (e.isNaN()) return e + if (max < e) max = e + } + return max +} /** * Returns the largest element or `null` if there are no elements. @@ -812,22 +1521,65 @@ public expect fun Iterable.max(): Double? * If any of elements is `NaN` returns `NaN`. */ @SinceKotlin("1.1") -public expect fun Iterable.max(): Float? +public fun Iterable.max(): Float? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var max = iterator.next() + if (max.isNaN()) return max + while (iterator.hasNext()) { + val e = iterator.next() + if (e.isNaN()) return e + if (max < e) max = e + } + return max +} /** * Returns the largest element or `null` if there are no elements. */ -public expect fun > Iterable.max(): T? +public fun > Iterable.max(): T? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var max = iterator.next() + while (iterator.hasNext()) { + val e = iterator.next() + if (max < e) max = e + } + return max +} /** * Returns the first element yielding the largest value of the given function or `null` if there are no elements. */ -public expect inline fun > Iterable.maxBy(selector: (T) -> R): T? +public inline fun > Iterable.maxBy(selector: (T) -> R): T? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var maxElem = iterator.next() + var maxValue = selector(maxElem) + while (iterator.hasNext()) { + val e = iterator.next() + val v = selector(e) + if (maxValue < v) { + maxElem = e + maxValue = v + } + } + return maxElem +} /** * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. */ -public expect fun Iterable.maxWith(comparator: Comparator): T? +public fun Iterable.maxWith(comparator: Comparator): T? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var max = iterator.next() + while (iterator.hasNext()) { + val e = iterator.next() + if (comparator.compare(max, e) < 0) max = e + } + return max +} /** * Returns the smallest element or `null` if there are no elements. @@ -835,7 +1587,18 @@ public expect fun Iterable.maxWith(comparator: Comparator): T? * If any of elements is `NaN` returns `NaN`. */ @SinceKotlin("1.1") -public expect fun Iterable.min(): Double? +public fun Iterable.min(): Double? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var min = iterator.next() + if (min.isNaN()) return min + while (iterator.hasNext()) { + val e = iterator.next() + if (e.isNaN()) return e + if (min > e) min = e + } + return min +} /** * Returns the smallest element or `null` if there are no elements. @@ -843,47 +1606,107 @@ public expect fun Iterable.min(): Double? * If any of elements is `NaN` returns `NaN`. */ @SinceKotlin("1.1") -public expect fun Iterable.min(): Float? +public fun Iterable.min(): Float? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var min = iterator.next() + if (min.isNaN()) return min + while (iterator.hasNext()) { + val e = iterator.next() + if (e.isNaN()) return e + if (min > e) min = e + } + return min +} /** * Returns the smallest element or `null` if there are no elements. */ -public expect fun > Iterable.min(): T? +public fun > Iterable.min(): T? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var min = iterator.next() + while (iterator.hasNext()) { + val e = iterator.next() + if (min > e) min = e + } + return min +} /** * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. */ -public expect inline fun > Iterable.minBy(selector: (T) -> R): T? +public inline fun > Iterable.minBy(selector: (T) -> R): T? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var minElem = iterator.next() + var minValue = selector(minElem) + while (iterator.hasNext()) { + val e = iterator.next() + val v = selector(e) + if (minValue > v) { + minElem = e + minValue = v + } + } + return minElem +} /** * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. */ -public expect fun Iterable.minWith(comparator: Comparator): T? +public fun Iterable.minWith(comparator: Comparator): T? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var min = iterator.next() + while (iterator.hasNext()) { + val e = iterator.next() + if (comparator.compare(min, e) > 0) min = e + } + return min +} /** * Returns `true` if the collection has no elements. * * @sample samples.collections.Collections.Aggregates.none */ -public expect fun Iterable.none(): Boolean +public fun Iterable.none(): Boolean { + if (this is Collection) return isEmpty() + return !iterator().hasNext() +} /** * Returns `true` if no elements match the given [predicate]. * * @sample samples.collections.Collections.Aggregates.noneWithPredicate */ -public expect inline fun Iterable.none(predicate: (T) -> Boolean): Boolean +public inline fun Iterable.none(predicate: (T) -> Boolean): Boolean { + if (this is Collection && isEmpty()) return true + for (element in this) if (predicate(element)) return false + return true +} /** * Performs the given [action] on each element and returns the collection itself afterwards. */ @SinceKotlin("1.1") -public expect inline fun > C.onEach(action: (T) -> Unit): C +public inline fun > C.onEach(action: (T) -> Unit): C { + return apply { for (element in this) action(element) } +} /** * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. */ -public expect inline fun Iterable.reduce(operation: (acc: S, T) -> S): S +public inline fun Iterable.reduce(operation: (acc: S, T) -> S): S { + val iterator = this.iterator() + if (!iterator.hasNext()) throw UnsupportedOperationException("Empty collection can't be reduced.") + var accumulator: S = iterator.next() + while (iterator.hasNext()) { + accumulator = operation(accumulator, iterator.next()) + } + return accumulator +} /** * Accumulates value starting with the first element and applying [operation] from left to right @@ -891,12 +1714,30 @@ public expect inline fun Iterable.reduce(operation: (acc: S, T) -> * @param [operation] function that takes the index of an element, current accumulator value * and the element itself and calculates the next accumulator value. */ -public expect inline fun Iterable.reduceIndexed(operation: (index: Int, acc: S, T) -> S): S +public inline fun Iterable.reduceIndexed(operation: (index: Int, acc: S, T) -> S): S { + val iterator = this.iterator() + if (!iterator.hasNext()) throw UnsupportedOperationException("Empty collection can't be reduced.") + var index = 1 + var accumulator: S = iterator.next() + while (iterator.hasNext()) { + accumulator = operation(index++, accumulator, iterator.next()) + } + return accumulator +} /** * Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. */ -public expect inline fun List.reduceRight(operation: (T, acc: S) -> S): S +public inline fun List.reduceRight(operation: (T, acc: S) -> S): S { + val iterator = listIterator(size) + if (!iterator.hasPrevious()) + throw UnsupportedOperationException("Empty list can't be reduced.") + var accumulator: S = iterator.previous() + while (iterator.hasPrevious()) { + accumulator = operation(iterator.previous(), accumulator) + } + return accumulator +} /** * Accumulates value starting with last element and applying [operation] from right to left @@ -904,27 +1745,65 @@ public expect inline fun List.reduceRight(operation: (T, acc: S) -> * @param [operation] function that takes the index of an element, the element itself * and current accumulator value, and calculates the next accumulator value. */ -public expect inline fun List.reduceRightIndexed(operation: (index: Int, T, acc: S) -> S): S +public inline fun List.reduceRightIndexed(operation: (index: Int, T, acc: S) -> S): S { + val iterator = listIterator(size) + if (!iterator.hasPrevious()) + throw UnsupportedOperationException("Empty list can't be reduced.") + var accumulator: S = iterator.previous() + while (iterator.hasPrevious()) { + val index = iterator.previousIndex() + accumulator = operation(index, iterator.previous(), accumulator) + } + return accumulator +} /** * Returns the sum of all values produced by [selector] function applied to each element in the collection. */ -public expect inline fun Iterable.sumBy(selector: (T) -> Int): Int +public inline fun Iterable.sumBy(selector: (T) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += selector(element) + } + return sum +} /** * Returns the sum of all values produced by [selector] function applied to each element in the collection. */ -public expect inline fun Iterable.sumByDouble(selector: (T) -> Double): Double +public inline fun Iterable.sumByDouble(selector: (T) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += selector(element) + } + return sum +} /** * Returns an original collection containing all the non-`null` elements, throwing an [IllegalArgumentException] if there are any `null` elements. */ -public expect fun Iterable.requireNoNulls(): Iterable +public fun Iterable.requireNoNulls(): Iterable { + for (element in this) { + if (element == null) { + throw IllegalArgumentException("null element found in $this.") + } + } + @Suppress("UNCHECKED_CAST") + return this as Iterable +} /** * Returns an original collection containing all the non-`null` elements, throwing an [IllegalArgumentException] if there are any `null` elements. */ -public expect fun List.requireNoNulls(): List +public fun List.requireNoNulls(): List { + for (element in this) { + if (element == null) { + throw IllegalArgumentException("null element found in $this.") + } + } + @Suppress("UNCHECKED_CAST") + return this as List +} /** * Splits this collection into a list of lists each not exceeding the given [size]. @@ -936,7 +1815,9 @@ public expect fun List.requireNoNulls(): List * @sample samples.collections.Collections.Transformations.chunked */ @SinceKotlin("1.2") -public expect fun Iterable.chunked(size: Int): List> +public fun Iterable.chunked(size: Int): List> { + return windowed(size, size, partialWindows = true) +} /** * Splits this collection into several lists each not exceeding the given [size] @@ -953,92 +1834,178 @@ public expect fun Iterable.chunked(size: Int): List> * @sample samples.text.Strings.chunkedTransform */ @SinceKotlin("1.2") -public expect fun Iterable.chunked(size: Int, transform: (List) -> R): List +public fun Iterable.chunked(size: Int, transform: (List) -> R): List { + return windowed(size, size, partialWindows = true, transform = transform) +} /** * Returns a list containing all elements of the original collection without the first occurrence of the given [element]. */ -public expect operator fun Iterable.minus(element: T): List +public operator fun Iterable.minus(element: T): List { + val result = ArrayList(collectionSizeOrDefault(10)) + var removed = false + return this.filterTo(result) { if (!removed && it == element) { removed = true; false } else true } +} /** * Returns a list containing all elements of the original collection except the elements contained in the given [elements] array. */ -public expect operator fun Iterable.minus(elements: Array): List +public operator fun Iterable.minus(elements: Array): List { + if (elements.isEmpty()) return this.toList() + val other = elements.toHashSet() + return this.filterNot { it in other } +} /** * Returns a list containing all elements of the original collection except the elements contained in the given [elements] collection. */ -public expect operator fun Iterable.minus(elements: Iterable): List +public operator fun Iterable.minus(elements: Iterable): List { + val other = elements.convertToSetForSetOperationWith(this) + if (other.isEmpty()) + return this.toList() + return this.filterNot { it in other } +} /** * Returns a list containing all elements of the original collection except the elements contained in the given [elements] sequence. */ -public expect operator fun Iterable.minus(elements: Sequence): List +public operator fun Iterable.minus(elements: Sequence): List { + val other = elements.toHashSet() + if (other.isEmpty()) + return this.toList() + return this.filterNot { it in other } +} /** * Returns a list containing all elements of the original collection without the first occurrence of the given [element]. */ @kotlin.internal.InlineOnly -public expect inline fun Iterable.minusElement(element: T): List +public inline fun Iterable.minusElement(element: T): List { + return minus(element) +} /** * Splits the original collection into pair of lists, * where *first* list contains elements for which [predicate] yielded `true`, * while *second* list contains elements for which [predicate] yielded `false`. */ -public expect inline fun Iterable.partition(predicate: (T) -> Boolean): Pair, List> +public inline fun Iterable.partition(predicate: (T) -> Boolean): Pair, List> { + val first = ArrayList() + val second = ArrayList() + for (element in this) { + if (predicate(element)) { + first.add(element) + } else { + second.add(element) + } + } + return Pair(first, second) +} /** * Returns a list containing all elements of the original collection and then the given [element]. */ -public expect operator fun Iterable.plus(element: T): List +public operator fun Iterable.plus(element: T): List { + if (this is Collection) return this.plus(element) + val result = ArrayList() + result.addAll(this) + result.add(element) + return result +} /** * Returns a list containing all elements of the original collection and then the given [element]. */ -public expect operator fun Collection.plus(element: T): List +public operator fun Collection.plus(element: T): List { + val result = ArrayList(size + 1) + result.addAll(this) + result.add(element) + return result +} /** * Returns a list containing all elements of the original collection and then all elements of the given [elements] array. */ -public expect operator fun Iterable.plus(elements: Array): List +public operator fun Iterable.plus(elements: Array): List { + if (this is Collection) return this.plus(elements) + val result = ArrayList() + result.addAll(this) + result.addAll(elements) + return result +} /** * Returns a list containing all elements of the original collection and then all elements of the given [elements] array. */ -public expect operator fun Collection.plus(elements: Array): List +public operator fun Collection.plus(elements: Array): List { + val result = ArrayList(this.size + elements.size) + result.addAll(this) + result.addAll(elements) + return result +} /** * Returns a list containing all elements of the original collection and then all elements of the given [elements] collection. */ -public expect operator fun Iterable.plus(elements: Iterable): List +public operator fun Iterable.plus(elements: Iterable): List { + if (this is Collection) return this.plus(elements) + val result = ArrayList() + result.addAll(this) + result.addAll(elements) + return result +} /** * Returns a list containing all elements of the original collection and then all elements of the given [elements] collection. */ -public expect operator fun Collection.plus(elements: Iterable): List +public operator fun Collection.plus(elements: Iterable): List { + if (elements is Collection) { + val result = ArrayList(this.size + elements.size) + result.addAll(this) + result.addAll(elements) + return result + } else { + val result = ArrayList(this) + result.addAll(elements) + return result + } +} /** * Returns a list containing all elements of the original collection and then all elements of the given [elements] sequence. */ -public expect operator fun Iterable.plus(elements: Sequence): List +public operator fun Iterable.plus(elements: Sequence): List { + val result = ArrayList() + result.addAll(this) + result.addAll(elements) + return result +} /** * Returns a list containing all elements of the original collection and then all elements of the given [elements] sequence. */ -public expect operator fun Collection.plus(elements: Sequence): List +public operator fun Collection.plus(elements: Sequence): List { + val result = ArrayList(this.size + 10) + result.addAll(this) + result.addAll(elements) + return result +} /** * Returns a list containing all elements of the original collection and then the given [element]. */ @kotlin.internal.InlineOnly -public expect inline fun Iterable.plusElement(element: T): List +public inline fun Iterable.plusElement(element: T): List { + return plus(element) +} /** * Returns a list containing all elements of the original collection and then the given [element]. */ @kotlin.internal.InlineOnly -public expect inline fun Collection.plusElement(element: T): List +public inline fun Collection.plusElement(element: T): List { + return plus(element) +} /** * Returns a list of snapshots of the window of the given [size] @@ -1120,22 +2087,43 @@ public fun Iterable.windowed(size: Int, step: Int = 1, partialWindows: /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ -public expect infix fun Iterable.zip(other: Array): List> +public infix fun Iterable.zip(other: Array): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} /** * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public expect inline fun Iterable.zip(other: Array, transform: (a: T, b: R) -> V): List +public inline fun Iterable.zip(other: Array, transform: (a: T, b: R) -> V): List { + val arraySize = other.size + val list = ArrayList(minOf(collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in this) { + if (i >= arraySize) break + list.add(transform(element, other[i++])) + } + return list +} /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ -public expect infix fun Iterable.zip(other: Iterable): List> +public infix fun Iterable.zip(other: Iterable): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} /** * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public expect inline fun Iterable.zip(other: Iterable, transform: (a: T, b: R) -> V): List +public inline fun Iterable.zip(other: Iterable, transform: (a: T, b: R) -> V): List { + val first = iterator() + val second = other.iterator() + val list = ArrayList(minOf(collectionSizeOrDefault(10), other.collectionSizeOrDefault(10))) + while (first.hasNext() && second.hasNext()) { + list.add(transform(first.next(), second.next())) + } + return list +} /** * Returns a list of pairs of each two adjacent elements in this collection. @@ -1145,7 +2133,9 @@ public expect inline fun Iterable.zip(other: Iterable, transform * @sample samples.collections.Collections.Transformations.zipWithNext */ @SinceKotlin("1.2") -public expect fun Iterable.zipWithNext(): List> +public fun Iterable.zipWithNext(): List> { + return zipWithNext { a, b -> a to b } +} /** * Returns a list containing the results of applying the given [transform] function @@ -1156,7 +2146,18 @@ public expect fun Iterable.zipWithNext(): List> * @sample samples.collections.Collections.Transformations.zipWithNextToFindDeltas */ @SinceKotlin("1.2") -public expect inline fun Iterable.zipWithNext(transform: (a: T, b: T) -> R): List +public inline fun Iterable.zipWithNext(transform: (a: T, b: T) -> R): List { + val iterator = iterator() + if (!iterator.hasNext()) return emptyList() + val result = mutableListOf() + var current = iterator.next() + while (iterator.hasNext()) { + val next = iterator.next() + result.add(transform(current, next)) + current = next + } + return result +} /** * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. @@ -1196,84 +2197,172 @@ public fun Iterable.joinToString(separator: CharSequence = ", ", prefix: * Returns this collection as an [Iterable]. */ @kotlin.internal.InlineOnly -public expect inline fun Iterable.asIterable(): Iterable +public inline fun Iterable.asIterable(): Iterable { + return this +} /** * Creates a [Sequence] instance that wraps the original collection returning its elements when being iterated. * * @sample samples.collections.Sequences.Building.sequenceFromCollection */ -public expect fun Iterable.asSequence(): Sequence +public fun Iterable.asSequence(): Sequence { + return Sequence { this.iterator() } +} /** * Returns an average value of elements in the collection. */ @kotlin.jvm.JvmName("averageOfByte") -public expect fun Iterable.average(): Double +public fun Iterable.average(): Double { + var sum: Double = 0.0 + var count: Int = 0 + for (element in this) { + sum += element + count += 1 + } + return if (count == 0) Double.NaN else sum / count +} /** * Returns an average value of elements in the collection. */ @kotlin.jvm.JvmName("averageOfShort") -public expect fun Iterable.average(): Double +public fun Iterable.average(): Double { + var sum: Double = 0.0 + var count: Int = 0 + for (element in this) { + sum += element + count += 1 + } + return if (count == 0) Double.NaN else sum / count +} /** * Returns an average value of elements in the collection. */ @kotlin.jvm.JvmName("averageOfInt") -public expect fun Iterable.average(): Double +public fun Iterable.average(): Double { + var sum: Double = 0.0 + var count: Int = 0 + for (element in this) { + sum += element + count += 1 + } + return if (count == 0) Double.NaN else sum / count +} /** * Returns an average value of elements in the collection. */ @kotlin.jvm.JvmName("averageOfLong") -public expect fun Iterable.average(): Double +public fun Iterable.average(): Double { + var sum: Double = 0.0 + var count: Int = 0 + for (element in this) { + sum += element + count += 1 + } + return if (count == 0) Double.NaN else sum / count +} /** * Returns an average value of elements in the collection. */ @kotlin.jvm.JvmName("averageOfFloat") -public expect fun Iterable.average(): Double +public fun Iterable.average(): Double { + var sum: Double = 0.0 + var count: Int = 0 + for (element in this) { + sum += element + count += 1 + } + return if (count == 0) Double.NaN else sum / count +} /** * Returns an average value of elements in the collection. */ @kotlin.jvm.JvmName("averageOfDouble") -public expect fun Iterable.average(): Double +public fun Iterable.average(): Double { + var sum: Double = 0.0 + var count: Int = 0 + for (element in this) { + sum += element + count += 1 + } + return if (count == 0) Double.NaN else sum / count +} /** * Returns the sum of all elements in the collection. */ @kotlin.jvm.JvmName("sumOfByte") -public expect fun Iterable.sum(): Int +public fun Iterable.sum(): Int { + var sum: Int = 0 + for (element in this) { + sum += element + } + return sum +} /** * Returns the sum of all elements in the collection. */ @kotlin.jvm.JvmName("sumOfShort") -public expect fun Iterable.sum(): Int +public fun Iterable.sum(): Int { + var sum: Int = 0 + for (element in this) { + sum += element + } + return sum +} /** * Returns the sum of all elements in the collection. */ @kotlin.jvm.JvmName("sumOfInt") -public expect fun Iterable.sum(): Int +public fun Iterable.sum(): Int { + var sum: Int = 0 + for (element in this) { + sum += element + } + return sum +} /** * Returns the sum of all elements in the collection. */ @kotlin.jvm.JvmName("sumOfLong") -public expect fun Iterable.sum(): Long +public fun Iterable.sum(): Long { + var sum: Long = 0L + for (element in this) { + sum += element + } + return sum +} /** * Returns the sum of all elements in the collection. */ @kotlin.jvm.JvmName("sumOfFloat") -public expect fun Iterable.sum(): Float +public fun Iterable.sum(): Float { + var sum: Float = 0.0f + for (element in this) { + sum += element + } + return sum +} /** * Returns the sum of all elements in the collection. */ @kotlin.jvm.JvmName("sumOfDouble") -public expect fun Iterable.sum(): Double +public fun Iterable.sum(): Double { + var sum: Double = 0.0 + for (element in this) { + sum += element + } + return sum +} diff --git a/libraries/stdlib/common/src/generated/_Comparisons.kt b/libraries/stdlib/common/src/generated/_Comparisons.kt index 887787f42e3..e224f591a86 100644 --- a/libraries/stdlib/common/src/generated/_Comparisons.kt +++ b/libraries/stdlib/common/src/generated/_Comparisons.kt @@ -17,7 +17,9 @@ import kotlin.comparisons.* * If values are equal, returns the first one. */ @SinceKotlin("1.1") -public expect fun > maxOf(a: T, b: T): T +public fun > maxOf(a: T, b: T): T { + return if (a >= b) a else b +} /** * Returns the greater of two values. @@ -65,7 +67,9 @@ public expect inline fun maxOf(a: Double, b: Double): Double * Returns the greater of three values. */ @SinceKotlin("1.1") -public expect fun > maxOf(a: T, b: T, c: T): T +public fun > maxOf(a: T, b: T, c: T): T { + return maxOf(a, maxOf(b, c)) +} /** * Returns the greater of three values. @@ -93,7 +97,9 @@ public expect inline fun maxOf(a: Int, b: Int, c: Int): Int */ @SinceKotlin("1.1") @kotlin.internal.InlineOnly -public expect inline fun maxOf(a: Long, b: Long, c: Long): Long +public inline fun maxOf(a: Long, b: Long, c: Long): Long { + return maxOf(a, maxOf(b, c)) +} /** * Returns the greater of three values. @@ -113,21 +119,27 @@ public expect inline fun maxOf(a: Double, b: Double, c: Double): Double * Returns the greater of three values according to the order specified by the given [comparator]. */ @SinceKotlin("1.1") -public expect fun maxOf(a: T, b: T, c: T, comparator: Comparator): T +public fun maxOf(a: T, b: T, c: T, comparator: Comparator): T { + return maxOf(a, maxOf(b, c, comparator), comparator) +} /** * Returns the greater of two values according to the order specified by the given [comparator]. * If values are equal, returns the first one. */ @SinceKotlin("1.1") -public expect fun maxOf(a: T, b: T, comparator: Comparator): T +public fun maxOf(a: T, b: T, comparator: Comparator): T { + return if (comparator.compare(a, b) >= 0) a else b +} /** * Returns the smaller of two values. * If values are equal, returns the first one. */ @SinceKotlin("1.1") -public expect fun > minOf(a: T, b: T): T +public fun > minOf(a: T, b: T): T { + return if (a <= b) a else b +} /** * Returns the smaller of two values. @@ -175,7 +187,9 @@ public expect inline fun minOf(a: Double, b: Double): Double * Returns the smaller of three values. */ @SinceKotlin("1.1") -public expect fun > minOf(a: T, b: T, c: T): T +public fun > minOf(a: T, b: T, c: T): T { + return minOf(a, minOf(b, c)) +} /** * Returns the smaller of three values. @@ -203,7 +217,9 @@ public expect inline fun minOf(a: Int, b: Int, c: Int): Int */ @SinceKotlin("1.1") @kotlin.internal.InlineOnly -public expect inline fun minOf(a: Long, b: Long, c: Long): Long +public inline fun minOf(a: Long, b: Long, c: Long): Long { + return minOf(a, minOf(b, c)) +} /** * Returns the smaller of three values. @@ -223,12 +239,16 @@ public expect inline fun minOf(a: Double, b: Double, c: Double): Double * Returns the smaller of three values according to the order specified by the given [comparator]. */ @SinceKotlin("1.1") -public expect fun minOf(a: T, b: T, c: T, comparator: Comparator): T +public fun minOf(a: T, b: T, c: T, comparator: Comparator): T { + return minOf(a, minOf(b, c, comparator), comparator) +} /** * Returns the smaller of two values according to the order specified by the given [comparator]. * If values are equal, returns the first one. */ @SinceKotlin("1.1") -public expect fun minOf(a: T, b: T, comparator: Comparator): T +public fun minOf(a: T, b: T, comparator: Comparator): T { + return if (comparator.compare(a, b) <= 0) a else b +} diff --git a/libraries/stdlib/common/src/generated/_Maps.kt b/libraries/stdlib/common/src/generated/_Maps.kt index ba606c884b3..37bdbcc315f 100644 --- a/libraries/stdlib/common/src/generated/_Maps.kt +++ b/libraries/stdlib/common/src/generated/_Maps.kt @@ -15,130 +15,203 @@ import kotlin.comparisons.* /** * Returns a [List] containing all key-value pairs. */ -public expect fun Map.toList(): List> +public fun Map.toList(): List> { + if (size == 0) + return emptyList() + val iterator = entries.iterator() + if (!iterator.hasNext()) + return emptyList() + val first = iterator.next() + if (!iterator.hasNext()) + return listOf(first.toPair()) + val result = ArrayList>(size) + result.add(first.toPair()) + do { + result.add(iterator.next().toPair()) + } while (iterator.hasNext()) + return result +} /** * Returns a single list of all elements yielded from results of [transform] function being invoked on each entry of original map. */ -public expect inline fun Map.flatMap(transform: (Map.Entry) -> Iterable): List +public inline fun Map.flatMap(transform: (Map.Entry) -> Iterable): List { + return flatMapTo(ArrayList(), transform) +} /** * Appends all elements yielded from results of [transform] function being invoked on each entry of original map, to the given [destination]. */ -public expect inline fun > Map.flatMapTo(destination: C, transform: (Map.Entry) -> Iterable): C +public inline fun > Map.flatMapTo(destination: C, transform: (Map.Entry) -> Iterable): C { + for (element in this) { + val list = transform(element) + destination.addAll(list) + } + return destination +} /** * Returns a list containing the results of applying the given [transform] function * to each entry in the original map. */ -public expect inline fun Map.map(transform: (Map.Entry) -> R): List +public inline fun Map.map(transform: (Map.Entry) -> R): List { + return mapTo(ArrayList(size), transform) +} /** * Returns a list containing only the non-null results of applying the given [transform] function * to each entry in the original map. */ -public expect inline fun Map.mapNotNull(transform: (Map.Entry) -> R?): List +public inline fun Map.mapNotNull(transform: (Map.Entry) -> R?): List { + return mapNotNullTo(ArrayList(), transform) +} /** * Applies the given [transform] function to each entry in the original map * and appends only the non-null results to the given [destination]. */ -public expect inline fun > Map.mapNotNullTo(destination: C, transform: (Map.Entry) -> R?): C +public inline fun > Map.mapNotNullTo(destination: C, transform: (Map.Entry) -> R?): C { + forEach { element -> transform(element)?.let { destination.add(it) } } + return destination +} /** * Applies the given [transform] function to each entry of the original map * and appends the results to the given [destination]. */ -public expect inline fun > Map.mapTo(destination: C, transform: (Map.Entry) -> R): C +public inline fun > Map.mapTo(destination: C, transform: (Map.Entry) -> R): C { + for (item in this) + destination.add(transform(item)) + return destination +} /** * Returns `true` if all entries match the given [predicate]. * * @sample samples.collections.Collections.Aggregates.all */ -public expect inline fun Map.all(predicate: (Map.Entry) -> Boolean): Boolean +public inline fun Map.all(predicate: (Map.Entry) -> Boolean): Boolean { + if (isEmpty()) return true + for (element in this) if (!predicate(element)) return false + return true +} /** * Returns `true` if map has at least one entry. * * @sample samples.collections.Collections.Aggregates.any */ -public expect fun Map.any(): Boolean +public fun Map.any(): Boolean { + return !isEmpty() +} /** * Returns `true` if at least one entry matches the given [predicate]. * * @sample samples.collections.Collections.Aggregates.anyWithPredicate */ -public expect inline fun Map.any(predicate: (Map.Entry) -> Boolean): Boolean +public inline fun Map.any(predicate: (Map.Entry) -> Boolean): Boolean { + if (isEmpty()) return false + for (element in this) if (predicate(element)) return true + return false +} /** * Returns the number of entries in this map. */ @kotlin.internal.InlineOnly -public expect inline fun Map.count(): Int +public inline fun Map.count(): Int { + return size +} /** * Returns the number of entries matching the given [predicate]. */ -public expect inline fun Map.count(predicate: (Map.Entry) -> Boolean): Int +public inline fun Map.count(predicate: (Map.Entry) -> Boolean): Int { + if (isEmpty()) return 0 + var count = 0 + for (element in this) if (predicate(element)) count++ + return count +} /** * Performs the given [action] on each entry. */ @kotlin.internal.HidesMembers -public expect inline fun Map.forEach(action: (Map.Entry) -> Unit): Unit +public inline fun Map.forEach(action: (Map.Entry) -> Unit): Unit { + for (element in this) action(element) +} /** * Returns the first entry yielding the largest value of the given function or `null` if there are no entries. */ @kotlin.internal.InlineOnly -public expect inline fun > Map.maxBy(selector: (Map.Entry) -> R): Map.Entry? +public inline fun > Map.maxBy(selector: (Map.Entry) -> R): Map.Entry? { + return entries.maxBy(selector) +} /** * Returns the first entry having the largest value according to the provided [comparator] or `null` if there are no entries. */ @kotlin.internal.InlineOnly -public expect inline fun Map.maxWith(comparator: Comparator>): Map.Entry? +public inline fun Map.maxWith(comparator: Comparator>): Map.Entry? { + return entries.maxWith(comparator) +} /** * Returns the first entry yielding the smallest value of the given function or `null` if there are no entries. */ -public expect inline fun > Map.minBy(selector: (Map.Entry) -> R): Map.Entry? +public inline fun > Map.minBy(selector: (Map.Entry) -> R): Map.Entry? { + return entries.minBy(selector) +} /** * Returns the first entry having the smallest value according to the provided [comparator] or `null` if there are no entries. */ -public expect fun Map.minWith(comparator: Comparator>): Map.Entry? +public fun Map.minWith(comparator: Comparator>): Map.Entry? { + return entries.minWith(comparator) +} /** * Returns `true` if the map has no entries. * * @sample samples.collections.Collections.Aggregates.none */ -public expect fun Map.none(): Boolean +public fun Map.none(): Boolean { + return isEmpty() +} /** * Returns `true` if no entries match the given [predicate]. * * @sample samples.collections.Collections.Aggregates.noneWithPredicate */ -public expect inline fun Map.none(predicate: (Map.Entry) -> Boolean): Boolean +public inline fun Map.none(predicate: (Map.Entry) -> Boolean): Boolean { + if (isEmpty()) return true + for (element in this) if (predicate(element)) return false + return true +} /** * Performs the given [action] on each entry and returns the map itself afterwards. */ @SinceKotlin("1.1") -public expect inline fun > M.onEach(action: (Map.Entry) -> Unit): M +public inline fun > M.onEach(action: (Map.Entry) -> Unit): M { + return apply { for (element in this) action(element) } +} /** * Creates an [Iterable] instance that wraps the original map returning its entries when being iterated. */ @kotlin.internal.InlineOnly -public expect inline fun Map.asIterable(): Iterable> +public inline fun Map.asIterable(): Iterable> { + return entries +} /** * Creates a [Sequence] instance that wraps the original map returning its entries when being iterated. */ -public expect fun Map.asSequence(): Sequence> +public fun Map.asSequence(): Sequence> { + return entries.asSequence() +} diff --git a/libraries/stdlib/common/src/generated/_Ranges.kt b/libraries/stdlib/common/src/generated/_Ranges.kt index d9ffc3a1bfd..6027857dd79 100644 --- a/libraries/stdlib/common/src/generated/_Ranges.kt +++ b/libraries/stdlib/common/src/generated/_Ranges.kt @@ -16,459 +16,638 @@ import kotlin.comparisons.* * Checks if the specified [value] belongs to this range. */ @kotlin.jvm.JvmName("intRangeContains") -public expect operator fun ClosedRange.contains(value: Byte): Boolean +public operator fun ClosedRange.contains(value: Byte): Boolean { + return contains(value.toInt()) +} /** * Checks if the specified [value] belongs to this range. */ @kotlin.jvm.JvmName("longRangeContains") -public expect operator fun ClosedRange.contains(value: Byte): Boolean +public operator fun ClosedRange.contains(value: Byte): Boolean { + return contains(value.toLong()) +} /** * Checks if the specified [value] belongs to this range. */ @kotlin.jvm.JvmName("shortRangeContains") -public expect operator fun ClosedRange.contains(value: Byte): Boolean +public operator fun ClosedRange.contains(value: Byte): Boolean { + return contains(value.toShort()) +} /** * Checks if the specified [value] belongs to this range. */ @kotlin.jvm.JvmName("doubleRangeContains") -public expect operator fun ClosedRange.contains(value: Byte): Boolean +public operator fun ClosedRange.contains(value: Byte): Boolean { + return contains(value.toDouble()) +} /** * Checks if the specified [value] belongs to this range. */ @kotlin.jvm.JvmName("floatRangeContains") -public expect operator fun ClosedRange.contains(value: Byte): Boolean +public operator fun ClosedRange.contains(value: Byte): Boolean { + return contains(value.toFloat()) +} /** * Checks if the specified [value] belongs to this range. */ @kotlin.jvm.JvmName("intRangeContains") -public expect operator fun ClosedRange.contains(value: Double): Boolean +public operator fun ClosedRange.contains(value: Double): Boolean { + return value.toIntExactOrNull().let { if (it != null) contains(it) else false } +} /** * Checks if the specified [value] belongs to this range. */ @kotlin.jvm.JvmName("longRangeContains") -public expect operator fun ClosedRange.contains(value: Double): Boolean +public operator fun ClosedRange.contains(value: Double): Boolean { + return value.toLongExactOrNull().let { if (it != null) contains(it) else false } +} /** * Checks if the specified [value] belongs to this range. */ @kotlin.jvm.JvmName("byteRangeContains") -public expect operator fun ClosedRange.contains(value: Double): Boolean +public operator fun ClosedRange.contains(value: Double): Boolean { + return value.toByteExactOrNull().let { if (it != null) contains(it) else false } +} /** * Checks if the specified [value] belongs to this range. */ @kotlin.jvm.JvmName("shortRangeContains") -public expect operator fun ClosedRange.contains(value: Double): Boolean +public operator fun ClosedRange.contains(value: Double): Boolean { + return value.toShortExactOrNull().let { if (it != null) contains(it) else false } +} /** * Checks if the specified [value] belongs to this range. */ @kotlin.jvm.JvmName("floatRangeContains") -public expect operator fun ClosedRange.contains(value: Double): Boolean +public operator fun ClosedRange.contains(value: Double): Boolean { + return contains(value.toFloat()) +} /** * Checks if the specified [value] belongs to this range. */ @kotlin.jvm.JvmName("intRangeContains") -public expect operator fun ClosedRange.contains(value: Float): Boolean +public operator fun ClosedRange.contains(value: Float): Boolean { + return value.toIntExactOrNull().let { if (it != null) contains(it) else false } +} /** * Checks if the specified [value] belongs to this range. */ @kotlin.jvm.JvmName("longRangeContains") -public expect operator fun ClosedRange.contains(value: Float): Boolean +public operator fun ClosedRange.contains(value: Float): Boolean { + return value.toLongExactOrNull().let { if (it != null) contains(it) else false } +} /** * Checks if the specified [value] belongs to this range. */ @kotlin.jvm.JvmName("byteRangeContains") -public expect operator fun ClosedRange.contains(value: Float): Boolean +public operator fun ClosedRange.contains(value: Float): Boolean { + return value.toByteExactOrNull().let { if (it != null) contains(it) else false } +} /** * Checks if the specified [value] belongs to this range. */ @kotlin.jvm.JvmName("shortRangeContains") -public expect operator fun ClosedRange.contains(value: Float): Boolean +public operator fun ClosedRange.contains(value: Float): Boolean { + return value.toShortExactOrNull().let { if (it != null) contains(it) else false } +} /** * Checks if the specified [value] belongs to this range. */ @kotlin.jvm.JvmName("doubleRangeContains") -public expect operator fun ClosedRange.contains(value: Float): Boolean +public operator fun ClosedRange.contains(value: Float): Boolean { + return contains(value.toDouble()) +} /** * Checks if the specified [value] belongs to this range. */ @kotlin.jvm.JvmName("longRangeContains") -public expect operator fun ClosedRange.contains(value: Int): Boolean +public operator fun ClosedRange.contains(value: Int): Boolean { + return contains(value.toLong()) +} /** * Checks if the specified [value] belongs to this range. */ @kotlin.jvm.JvmName("byteRangeContains") -public expect operator fun ClosedRange.contains(value: Int): Boolean +public operator fun ClosedRange.contains(value: Int): Boolean { + return value.toByteExactOrNull().let { if (it != null) contains(it) else false } +} /** * Checks if the specified [value] belongs to this range. */ @kotlin.jvm.JvmName("shortRangeContains") -public expect operator fun ClosedRange.contains(value: Int): Boolean +public operator fun ClosedRange.contains(value: Int): Boolean { + return value.toShortExactOrNull().let { if (it != null) contains(it) else false } +} /** * Checks if the specified [value] belongs to this range. */ @kotlin.jvm.JvmName("doubleRangeContains") -public expect operator fun ClosedRange.contains(value: Int): Boolean +public operator fun ClosedRange.contains(value: Int): Boolean { + return contains(value.toDouble()) +} /** * Checks if the specified [value] belongs to this range. */ @kotlin.jvm.JvmName("floatRangeContains") -public expect operator fun ClosedRange.contains(value: Int): Boolean +public operator fun ClosedRange.contains(value: Int): Boolean { + return contains(value.toFloat()) +} /** * Checks if the specified [value] belongs to this range. */ @kotlin.jvm.JvmName("intRangeContains") -public expect operator fun ClosedRange.contains(value: Long): Boolean +public operator fun ClosedRange.contains(value: Long): Boolean { + return value.toIntExactOrNull().let { if (it != null) contains(it) else false } +} /** * Checks if the specified [value] belongs to this range. */ @kotlin.jvm.JvmName("byteRangeContains") -public expect operator fun ClosedRange.contains(value: Long): Boolean +public operator fun ClosedRange.contains(value: Long): Boolean { + return value.toByteExactOrNull().let { if (it != null) contains(it) else false } +} /** * Checks if the specified [value] belongs to this range. */ @kotlin.jvm.JvmName("shortRangeContains") -public expect operator fun ClosedRange.contains(value: Long): Boolean +public operator fun ClosedRange.contains(value: Long): Boolean { + return value.toShortExactOrNull().let { if (it != null) contains(it) else false } +} /** * Checks if the specified [value] belongs to this range. */ @kotlin.jvm.JvmName("doubleRangeContains") -public expect operator fun ClosedRange.contains(value: Long): Boolean +public operator fun ClosedRange.contains(value: Long): Boolean { + return contains(value.toDouble()) +} /** * Checks if the specified [value] belongs to this range. */ @kotlin.jvm.JvmName("floatRangeContains") -public expect operator fun ClosedRange.contains(value: Long): Boolean +public operator fun ClosedRange.contains(value: Long): Boolean { + return contains(value.toFloat()) +} /** * Checks if the specified [value] belongs to this range. */ @kotlin.jvm.JvmName("intRangeContains") -public expect operator fun ClosedRange.contains(value: Short): Boolean +public operator fun ClosedRange.contains(value: Short): Boolean { + return contains(value.toInt()) +} /** * Checks if the specified [value] belongs to this range. */ @kotlin.jvm.JvmName("longRangeContains") -public expect operator fun ClosedRange.contains(value: Short): Boolean +public operator fun ClosedRange.contains(value: Short): Boolean { + return contains(value.toLong()) +} /** * Checks if the specified [value] belongs to this range. */ @kotlin.jvm.JvmName("byteRangeContains") -public expect operator fun ClosedRange.contains(value: Short): Boolean +public operator fun ClosedRange.contains(value: Short): Boolean { + return value.toByteExactOrNull().let { if (it != null) contains(it) else false } +} /** * Checks if the specified [value] belongs to this range. */ @kotlin.jvm.JvmName("doubleRangeContains") -public expect operator fun ClosedRange.contains(value: Short): Boolean +public operator fun ClosedRange.contains(value: Short): Boolean { + return contains(value.toDouble()) +} /** * Checks if the specified [value] belongs to this range. */ @kotlin.jvm.JvmName("floatRangeContains") -public expect operator fun ClosedRange.contains(value: Short): Boolean +public operator fun ClosedRange.contains(value: Short): Boolean { + return contains(value.toFloat()) +} /** * Returns a progression from this value down to the specified [to] value with the step -1. * * The [to] value has to be less than this value. */ -public expect infix fun Int.downTo(to: Byte): IntProgression +public infix fun Int.downTo(to: Byte): IntProgression { + return IntProgression.fromClosedRange(this, to.toInt(), -1) +} /** * Returns a progression from this value down to the specified [to] value with the step -1. * * The [to] value has to be less than this value. */ -public expect infix fun Long.downTo(to: Byte): LongProgression +public infix fun Long.downTo(to: Byte): LongProgression { + return LongProgression.fromClosedRange(this, to.toLong(), -1L) +} /** * Returns a progression from this value down to the specified [to] value with the step -1. * * The [to] value has to be less than this value. */ -public expect infix fun Byte.downTo(to: Byte): IntProgression +public infix fun Byte.downTo(to: Byte): IntProgression { + return IntProgression.fromClosedRange(this.toInt(), to.toInt(), -1) +} /** * Returns a progression from this value down to the specified [to] value with the step -1. * * The [to] value has to be less than this value. */ -public expect infix fun Short.downTo(to: Byte): IntProgression +public infix fun Short.downTo(to: Byte): IntProgression { + return IntProgression.fromClosedRange(this.toInt(), to.toInt(), -1) +} /** * Returns a progression from this value down to the specified [to] value with the step -1. * * The [to] value has to be less than this value. */ -public expect infix fun Char.downTo(to: Char): CharProgression +public infix fun Char.downTo(to: Char): CharProgression { + return CharProgression.fromClosedRange(this, to, -1) +} /** * Returns a progression from this value down to the specified [to] value with the step -1. * * The [to] value has to be less than this value. */ -public expect infix fun Int.downTo(to: Int): IntProgression +public infix fun Int.downTo(to: Int): IntProgression { + return IntProgression.fromClosedRange(this, to, -1) +} /** * Returns a progression from this value down to the specified [to] value with the step -1. * * The [to] value has to be less than this value. */ -public expect infix fun Long.downTo(to: Int): LongProgression +public infix fun Long.downTo(to: Int): LongProgression { + return LongProgression.fromClosedRange(this, to.toLong(), -1L) +} /** * Returns a progression from this value down to the specified [to] value with the step -1. * * The [to] value has to be less than this value. */ -public expect infix fun Byte.downTo(to: Int): IntProgression +public infix fun Byte.downTo(to: Int): IntProgression { + return IntProgression.fromClosedRange(this.toInt(), to, -1) +} /** * Returns a progression from this value down to the specified [to] value with the step -1. * * The [to] value has to be less than this value. */ -public expect infix fun Short.downTo(to: Int): IntProgression +public infix fun Short.downTo(to: Int): IntProgression { + return IntProgression.fromClosedRange(this.toInt(), to, -1) +} /** * Returns a progression from this value down to the specified [to] value with the step -1. * * The [to] value has to be less than this value. */ -public expect infix fun Int.downTo(to: Long): LongProgression +public infix fun Int.downTo(to: Long): LongProgression { + return LongProgression.fromClosedRange(this.toLong(), to, -1L) +} /** * Returns a progression from this value down to the specified [to] value with the step -1. * * The [to] value has to be less than this value. */ -public expect infix fun Long.downTo(to: Long): LongProgression +public infix fun Long.downTo(to: Long): LongProgression { + return LongProgression.fromClosedRange(this, to, -1L) +} /** * Returns a progression from this value down to the specified [to] value with the step -1. * * The [to] value has to be less than this value. */ -public expect infix fun Byte.downTo(to: Long): LongProgression +public infix fun Byte.downTo(to: Long): LongProgression { + return LongProgression.fromClosedRange(this.toLong(), to, -1L) +} /** * Returns a progression from this value down to the specified [to] value with the step -1. * * The [to] value has to be less than this value. */ -public expect infix fun Short.downTo(to: Long): LongProgression +public infix fun Short.downTo(to: Long): LongProgression { + return LongProgression.fromClosedRange(this.toLong(), to, -1L) +} /** * Returns a progression from this value down to the specified [to] value with the step -1. * * The [to] value has to be less than this value. */ -public expect infix fun Int.downTo(to: Short): IntProgression +public infix fun Int.downTo(to: Short): IntProgression { + return IntProgression.fromClosedRange(this, to.toInt(), -1) +} /** * Returns a progression from this value down to the specified [to] value with the step -1. * * The [to] value has to be less than this value. */ -public expect infix fun Long.downTo(to: Short): LongProgression +public infix fun Long.downTo(to: Short): LongProgression { + return LongProgression.fromClosedRange(this, to.toLong(), -1L) +} /** * Returns a progression from this value down to the specified [to] value with the step -1. * * The [to] value has to be less than this value. */ -public expect infix fun Byte.downTo(to: Short): IntProgression +public infix fun Byte.downTo(to: Short): IntProgression { + return IntProgression.fromClosedRange(this.toInt(), to.toInt(), -1) +} /** * Returns a progression from this value down to the specified [to] value with the step -1. * * The [to] value has to be less than this value. */ -public expect infix fun Short.downTo(to: Short): IntProgression +public infix fun Short.downTo(to: Short): IntProgression { + return IntProgression.fromClosedRange(this.toInt(), to.toInt(), -1) +} /** * Returns a progression that goes over the same range in the opposite direction with the same step. */ -public expect fun IntProgression.reversed(): IntProgression +public fun IntProgression.reversed(): IntProgression { + return IntProgression.fromClosedRange(last, first, -step) +} /** * Returns a progression that goes over the same range in the opposite direction with the same step. */ -public expect fun LongProgression.reversed(): LongProgression +public fun LongProgression.reversed(): LongProgression { + return LongProgression.fromClosedRange(last, first, -step) +} /** * Returns a progression that goes over the same range in the opposite direction with the same step. */ -public expect fun CharProgression.reversed(): CharProgression +public fun CharProgression.reversed(): CharProgression { + return CharProgression.fromClosedRange(last, first, -step) +} /** * Returns a progression that goes over the same range with the given step. */ -public expect infix fun IntProgression.step(step: Int): IntProgression +public infix fun IntProgression.step(step: Int): IntProgression { + checkStepIsPositive(step > 0, step) + return IntProgression.fromClosedRange(first, last, if (this.step > 0) step else -step) +} /** * Returns a progression that goes over the same range with the given step. */ -public expect infix fun LongProgression.step(step: Long): LongProgression +public infix fun LongProgression.step(step: Long): LongProgression { + checkStepIsPositive(step > 0, step) + return LongProgression.fromClosedRange(first, last, if (this.step > 0) step else -step) +} /** * Returns a progression that goes over the same range with the given step. */ -public expect infix fun CharProgression.step(step: Int): CharProgression +public infix fun CharProgression.step(step: Int): CharProgression { + checkStepIsPositive(step > 0, step) + return CharProgression.fromClosedRange(first, last, if (this.step > 0) step else -step) +} -internal expect fun Int.toByteExactOrNull(): Byte? +internal fun Int.toByteExactOrNull(): Byte? { + return if (this in Byte.MIN_VALUE.toInt()..Byte.MAX_VALUE.toInt()) this.toByte() else null +} -internal expect fun Long.toByteExactOrNull(): Byte? +internal fun Long.toByteExactOrNull(): Byte? { + return if (this in Byte.MIN_VALUE.toLong()..Byte.MAX_VALUE.toLong()) this.toByte() else null +} -internal expect fun Short.toByteExactOrNull(): Byte? +internal fun Short.toByteExactOrNull(): Byte? { + return if (this in Byte.MIN_VALUE.toShort()..Byte.MAX_VALUE.toShort()) this.toByte() else null +} -internal expect fun Double.toByteExactOrNull(): Byte? +internal fun Double.toByteExactOrNull(): Byte? { + return if (this in Byte.MIN_VALUE.toDouble()..Byte.MAX_VALUE.toDouble()) this.toByte() else null +} -internal expect fun Float.toByteExactOrNull(): Byte? +internal fun Float.toByteExactOrNull(): Byte? { + return if (this in Byte.MIN_VALUE.toFloat()..Byte.MAX_VALUE.toFloat()) this.toByte() else null +} -internal expect fun Long.toIntExactOrNull(): Int? +internal fun Long.toIntExactOrNull(): Int? { + return if (this in Int.MIN_VALUE.toLong()..Int.MAX_VALUE.toLong()) this.toInt() else null +} -internal expect fun Double.toIntExactOrNull(): Int? +internal fun Double.toIntExactOrNull(): Int? { + return if (this in Int.MIN_VALUE.toDouble()..Int.MAX_VALUE.toDouble()) this.toInt() else null +} -internal expect fun Float.toIntExactOrNull(): Int? +internal fun Float.toIntExactOrNull(): Int? { + return if (this in Int.MIN_VALUE.toFloat()..Int.MAX_VALUE.toFloat()) this.toInt() else null +} -internal expect fun Double.toLongExactOrNull(): Long? +internal fun Double.toLongExactOrNull(): Long? { + return if (this in Long.MIN_VALUE.toDouble()..Long.MAX_VALUE.toDouble()) this.toLong() else null +} -internal expect fun Float.toLongExactOrNull(): Long? +internal fun Float.toLongExactOrNull(): Long? { + return if (this in Long.MIN_VALUE.toFloat()..Long.MAX_VALUE.toFloat()) this.toLong() else null +} -internal expect fun Int.toShortExactOrNull(): Short? +internal fun Int.toShortExactOrNull(): Short? { + return if (this in Short.MIN_VALUE.toInt()..Short.MAX_VALUE.toInt()) this.toShort() else null +} -internal expect fun Long.toShortExactOrNull(): Short? +internal fun Long.toShortExactOrNull(): Short? { + return if (this in Short.MIN_VALUE.toLong()..Short.MAX_VALUE.toLong()) this.toShort() else null +} -internal expect fun Double.toShortExactOrNull(): Short? +internal fun Double.toShortExactOrNull(): Short? { + return if (this in Short.MIN_VALUE.toDouble()..Short.MAX_VALUE.toDouble()) this.toShort() else null +} -internal expect fun Float.toShortExactOrNull(): Short? +internal fun Float.toShortExactOrNull(): Short? { + return if (this in Short.MIN_VALUE.toFloat()..Short.MAX_VALUE.toFloat()) this.toShort() else null +} /** * Returns a range from this value up to but excluding the specified [to] value. */ -public expect infix fun Int.until(to: Byte): IntRange +public infix fun Int.until(to: Byte): IntRange { + return this .. (to.toInt() - 1).toInt() +} /** * Returns a range from this value up to but excluding the specified [to] value. */ -public expect infix fun Long.until(to: Byte): LongRange +public infix fun Long.until(to: Byte): LongRange { + return this .. (to.toLong() - 1).toLong() +} /** * Returns a range from this value up to but excluding the specified [to] value. */ -public expect infix fun Byte.until(to: Byte): IntRange +public infix fun Byte.until(to: Byte): IntRange { + return this.toInt() .. (to.toInt() - 1).toInt() +} /** * Returns a range from this value up to but excluding the specified [to] value. */ -public expect infix fun Short.until(to: Byte): IntRange +public infix fun Short.until(to: Byte): IntRange { + return this.toInt() .. (to.toInt() - 1).toInt() +} /** * Returns a range from this value up to but excluding the specified [to] value. * * If the [to] value is less than or equal to `'\u0000'` the returned range is empty. */ -public expect infix fun Char.until(to: Char): CharRange +public infix fun Char.until(to: Char): CharRange { + if (to <= '\u0000') return CharRange.EMPTY + return this .. (to - 1).toChar() +} /** * Returns a range from this value up to but excluding the specified [to] value. * * If the [to] value is less than or equal to [Int.MIN_VALUE] the returned range is empty. */ -public expect infix fun Int.until(to: Int): IntRange +public infix fun Int.until(to: Int): IntRange { + if (to <= Int.MIN_VALUE) return IntRange.EMPTY + return this .. (to - 1).toInt() +} /** * Returns a range from this value up to but excluding the specified [to] value. */ -public expect infix fun Long.until(to: Int): LongRange +public infix fun Long.until(to: Int): LongRange { + return this .. (to.toLong() - 1).toLong() +} /** * Returns a range from this value up to but excluding the specified [to] value. * * If the [to] value is less than or equal to [Int.MIN_VALUE] the returned range is empty. */ -public expect infix fun Byte.until(to: Int): IntRange +public infix fun Byte.until(to: Int): IntRange { + if (to <= Int.MIN_VALUE) return IntRange.EMPTY + return this.toInt() .. (to - 1).toInt() +} /** * Returns a range from this value up to but excluding the specified [to] value. * * If the [to] value is less than or equal to [Int.MIN_VALUE] the returned range is empty. */ -public expect infix fun Short.until(to: Int): IntRange +public infix fun Short.until(to: Int): IntRange { + if (to <= Int.MIN_VALUE) return IntRange.EMPTY + return this.toInt() .. (to - 1).toInt() +} /** * Returns a range from this value up to but excluding the specified [to] value. * * If the [to] value is less than or equal to [Long.MIN_VALUE] the returned range is empty. */ -public expect infix fun Int.until(to: Long): LongRange +public infix fun Int.until(to: Long): LongRange { + if (to <= Long.MIN_VALUE) return LongRange.EMPTY + return this.toLong() .. (to - 1).toLong() +} /** * Returns a range from this value up to but excluding the specified [to] value. * * If the [to] value is less than or equal to [Long.MIN_VALUE] the returned range is empty. */ -public expect infix fun Long.until(to: Long): LongRange +public infix fun Long.until(to: Long): LongRange { + if (to <= Long.MIN_VALUE) return LongRange.EMPTY + return this .. (to - 1).toLong() +} /** * Returns a range from this value up to but excluding the specified [to] value. * * If the [to] value is less than or equal to [Long.MIN_VALUE] the returned range is empty. */ -public expect infix fun Byte.until(to: Long): LongRange +public infix fun Byte.until(to: Long): LongRange { + if (to <= Long.MIN_VALUE) return LongRange.EMPTY + return this.toLong() .. (to - 1).toLong() +} /** * Returns a range from this value up to but excluding the specified [to] value. * * If the [to] value is less than or equal to [Long.MIN_VALUE] the returned range is empty. */ -public expect infix fun Short.until(to: Long): LongRange +public infix fun Short.until(to: Long): LongRange { + if (to <= Long.MIN_VALUE) return LongRange.EMPTY + return this.toLong() .. (to - 1).toLong() +} /** * Returns a range from this value up to but excluding the specified [to] value. */ -public expect infix fun Int.until(to: Short): IntRange +public infix fun Int.until(to: Short): IntRange { + return this .. (to.toInt() - 1).toInt() +} /** * Returns a range from this value up to but excluding the specified [to] value. */ -public expect infix fun Long.until(to: Short): LongRange +public infix fun Long.until(to: Short): LongRange { + return this .. (to.toLong() - 1).toLong() +} /** * Returns a range from this value up to but excluding the specified [to] value. */ -public expect infix fun Byte.until(to: Short): IntRange +public infix fun Byte.until(to: Short): IntRange { + return this.toInt() .. (to.toInt() - 1).toInt() +} /** * Returns a range from this value up to but excluding the specified [to] value. */ -public expect infix fun Short.until(to: Short): IntRange +public infix fun Short.until(to: Short): IntRange { + return this.toInt() .. (to.toInt() - 1).toInt() +} /** * Ensures that this value is not less than the specified [minimumValue]. @@ -476,7 +655,9 @@ public expect infix fun Short.until(to: Short): IntRange * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. * @sample samples.comparisons.ComparableOps.coerceAtLeastComparable */ -public expect fun > T.coerceAtLeast(minimumValue: T): T +public fun > T.coerceAtLeast(minimumValue: T): T { + return if (this < minimumValue) minimumValue else this +} /** * Ensures that this value is not less than the specified [minimumValue]. @@ -484,7 +665,9 @@ public expect fun > T.coerceAtLeast(minimumValue: T): T * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. * @sample samples.comparisons.ComparableOps.coerceAtLeast */ -public expect fun Byte.coerceAtLeast(minimumValue: Byte): Byte +public fun Byte.coerceAtLeast(minimumValue: Byte): Byte { + return if (this < minimumValue) minimumValue else this +} /** * Ensures that this value is not less than the specified [minimumValue]. @@ -492,7 +675,9 @@ public expect fun Byte.coerceAtLeast(minimumValue: Byte): Byte * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. * @sample samples.comparisons.ComparableOps.coerceAtLeast */ -public expect fun Short.coerceAtLeast(minimumValue: Short): Short +public fun Short.coerceAtLeast(minimumValue: Short): Short { + return if (this < minimumValue) minimumValue else this +} /** * Ensures that this value is not less than the specified [minimumValue]. @@ -500,7 +685,9 @@ public expect fun Short.coerceAtLeast(minimumValue: Short): Short * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. * @sample samples.comparisons.ComparableOps.coerceAtLeast */ -public expect fun Int.coerceAtLeast(minimumValue: Int): Int +public fun Int.coerceAtLeast(minimumValue: Int): Int { + return if (this < minimumValue) minimumValue else this +} /** * Ensures that this value is not less than the specified [minimumValue]. @@ -508,7 +695,9 @@ public expect fun Int.coerceAtLeast(minimumValue: Int): Int * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. * @sample samples.comparisons.ComparableOps.coerceAtLeast */ -public expect fun Long.coerceAtLeast(minimumValue: Long): Long +public fun Long.coerceAtLeast(minimumValue: Long): Long { + return if (this < minimumValue) minimumValue else this +} /** * Ensures that this value is not less than the specified [minimumValue]. @@ -516,7 +705,9 @@ public expect fun Long.coerceAtLeast(minimumValue: Long): Long * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. * @sample samples.comparisons.ComparableOps.coerceAtLeast */ -public expect fun Float.coerceAtLeast(minimumValue: Float): Float +public fun Float.coerceAtLeast(minimumValue: Float): Float { + return if (this < minimumValue) minimumValue else this +} /** * Ensures that this value is not less than the specified [minimumValue]. @@ -524,7 +715,9 @@ public expect fun Float.coerceAtLeast(minimumValue: Float): Float * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. * @sample samples.comparisons.ComparableOps.coerceAtLeast */ -public expect fun Double.coerceAtLeast(minimumValue: Double): Double +public fun Double.coerceAtLeast(minimumValue: Double): Double { + return if (this < minimumValue) minimumValue else this +} /** * Ensures that this value is not greater than the specified [maximumValue]. @@ -532,7 +725,9 @@ public expect fun Double.coerceAtLeast(minimumValue: Double): Double * @return this value if it's less than or equal to the [maximumValue] or the [maximumValue] otherwise. * @sample samples.comparisons.ComparableOps.coerceAtMostComparable */ -public expect fun > T.coerceAtMost(maximumValue: T): T +public fun > T.coerceAtMost(maximumValue: T): T { + return if (this > maximumValue) maximumValue else this +} /** * Ensures that this value is not greater than the specified [maximumValue]. @@ -540,7 +735,9 @@ public expect fun > T.coerceAtMost(maximumValue: T): T * @return this value if it's less than or equal to the [maximumValue] or the [maximumValue] otherwise. * @sample samples.comparisons.ComparableOps.coerceAtMost */ -public expect fun Byte.coerceAtMost(maximumValue: Byte): Byte +public fun Byte.coerceAtMost(maximumValue: Byte): Byte { + return if (this > maximumValue) maximumValue else this +} /** * Ensures that this value is not greater than the specified [maximumValue]. @@ -548,7 +745,9 @@ public expect fun Byte.coerceAtMost(maximumValue: Byte): Byte * @return this value if it's less than or equal to the [maximumValue] or the [maximumValue] otherwise. * @sample samples.comparisons.ComparableOps.coerceAtMost */ -public expect fun Short.coerceAtMost(maximumValue: Short): Short +public fun Short.coerceAtMost(maximumValue: Short): Short { + return if (this > maximumValue) maximumValue else this +} /** * Ensures that this value is not greater than the specified [maximumValue]. @@ -556,7 +755,9 @@ public expect fun Short.coerceAtMost(maximumValue: Short): Short * @return this value if it's less than or equal to the [maximumValue] or the [maximumValue] otherwise. * @sample samples.comparisons.ComparableOps.coerceAtMost */ -public expect fun Int.coerceAtMost(maximumValue: Int): Int +public fun Int.coerceAtMost(maximumValue: Int): Int { + return if (this > maximumValue) maximumValue else this +} /** * Ensures that this value is not greater than the specified [maximumValue]. @@ -564,7 +765,9 @@ public expect fun Int.coerceAtMost(maximumValue: Int): Int * @return this value if it's less than or equal to the [maximumValue] or the [maximumValue] otherwise. * @sample samples.comparisons.ComparableOps.coerceAtMost */ -public expect fun Long.coerceAtMost(maximumValue: Long): Long +public fun Long.coerceAtMost(maximumValue: Long): Long { + return if (this > maximumValue) maximumValue else this +} /** * Ensures that this value is not greater than the specified [maximumValue]. @@ -572,7 +775,9 @@ public expect fun Long.coerceAtMost(maximumValue: Long): Long * @return this value if it's less than or equal to the [maximumValue] or the [maximumValue] otherwise. * @sample samples.comparisons.ComparableOps.coerceAtMost */ -public expect fun Float.coerceAtMost(maximumValue: Float): Float +public fun Float.coerceAtMost(maximumValue: Float): Float { + return if (this > maximumValue) maximumValue else this +} /** * Ensures that this value is not greater than the specified [maximumValue]. @@ -580,7 +785,9 @@ public expect fun Float.coerceAtMost(maximumValue: Float): Float * @return this value if it's less than or equal to the [maximumValue] or the [maximumValue] otherwise. * @sample samples.comparisons.ComparableOps.coerceAtMost */ -public expect fun Double.coerceAtMost(maximumValue: Double): Double +public fun Double.coerceAtMost(maximumValue: Double): Double { + return if (this > maximumValue) maximumValue else this +} /** * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. @@ -588,7 +795,18 @@ public expect fun Double.coerceAtMost(maximumValue: Double): Double * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. * @sample samples.comparisons.ComparableOps.coerceInComparable */ -public expect fun > T.coerceIn(minimumValue: T?, maximumValue: T?): T +public fun > T.coerceIn(minimumValue: T?, maximumValue: T?): T { + if (minimumValue !== null && maximumValue !== null) { + if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") + if (this < minimumValue) return minimumValue + if (this > maximumValue) return maximumValue + } + else { + if (minimumValue !== null && this < minimumValue) return minimumValue + if (maximumValue !== null && this > maximumValue) return maximumValue + } + return this +} /** * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. @@ -596,7 +814,12 @@ public expect fun > T.coerceIn(minimumValue: T?, maximumValue: * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. * @sample samples.comparisons.ComparableOps.coerceIn */ -public expect fun Byte.coerceIn(minimumValue: Byte, maximumValue: Byte): Byte +public fun Byte.coerceIn(minimumValue: Byte, maximumValue: Byte): Byte { + if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") + if (this < minimumValue) return minimumValue + if (this > maximumValue) return maximumValue + return this +} /** * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. @@ -604,7 +827,12 @@ public expect fun Byte.coerceIn(minimumValue: Byte, maximumValue: Byte): Byte * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. * @sample samples.comparisons.ComparableOps.coerceIn */ -public expect fun Short.coerceIn(minimumValue: Short, maximumValue: Short): Short +public fun Short.coerceIn(minimumValue: Short, maximumValue: Short): Short { + if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") + if (this < minimumValue) return minimumValue + if (this > maximumValue) return maximumValue + return this +} /** * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. @@ -612,7 +840,12 @@ public expect fun Short.coerceIn(minimumValue: Short, maximumValue: Short): Shor * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. * @sample samples.comparisons.ComparableOps.coerceIn */ -public expect fun Int.coerceIn(minimumValue: Int, maximumValue: Int): Int +public fun Int.coerceIn(minimumValue: Int, maximumValue: Int): Int { + if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") + if (this < minimumValue) return minimumValue + if (this > maximumValue) return maximumValue + return this +} /** * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. @@ -620,7 +853,12 @@ public expect fun Int.coerceIn(minimumValue: Int, maximumValue: Int): Int * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. * @sample samples.comparisons.ComparableOps.coerceIn */ -public expect fun Long.coerceIn(minimumValue: Long, maximumValue: Long): Long +public fun Long.coerceIn(minimumValue: Long, maximumValue: Long): Long { + if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") + if (this < minimumValue) return minimumValue + if (this > maximumValue) return maximumValue + return this +} /** * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. @@ -628,7 +866,12 @@ public expect fun Long.coerceIn(minimumValue: Long, maximumValue: Long): Long * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. * @sample samples.comparisons.ComparableOps.coerceIn */ -public expect fun Float.coerceIn(minimumValue: Float, maximumValue: Float): Float +public fun Float.coerceIn(minimumValue: Float, maximumValue: Float): Float { + if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") + if (this < minimumValue) return minimumValue + if (this > maximumValue) return maximumValue + return this +} /** * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. @@ -636,7 +879,12 @@ public expect fun Float.coerceIn(minimumValue: Float, maximumValue: Float): Floa * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. * @sample samples.comparisons.ComparableOps.coerceIn */ -public expect fun Double.coerceIn(minimumValue: Double, maximumValue: Double): Double +public fun Double.coerceIn(minimumValue: Double, maximumValue: Double): Double { + if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") + if (this < minimumValue) return minimumValue + if (this > maximumValue) return maximumValue + return this +} /** * Ensures that this value lies in the specified [range]. @@ -645,7 +893,16 @@ public expect fun Double.coerceIn(minimumValue: Double, maximumValue: Double): D * @sample samples.comparisons.ComparableOps.coerceInFloatingPointRange */ @SinceKotlin("1.1") -public expect fun > T.coerceIn(range: ClosedFloatingPointRange): T +public fun > T.coerceIn(range: ClosedFloatingPointRange): T { + if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") + return when { + // this < start equiv to this <= start && !(this >= start) + range.lessThanOrEquals(this, range.start) && !range.lessThanOrEquals(range.start, this) -> range.start + // this > end equiv to this >= end && !(this <= end) + range.lessThanOrEquals(range.endInclusive, this) && !range.lessThanOrEquals(this, range.endInclusive) -> range.endInclusive + else -> this + } +} /** * Ensures that this value lies in the specified [range]. @@ -653,7 +910,17 @@ public expect fun > T.coerceIn(range: ClosedFloatingPointRange< * @return this value if it's in the [range], or `range.start` if this value is less than `range.start`, or `range.endInclusive` if this value is greater than `range.endInclusive`. * @sample samples.comparisons.ComparableOps.coerceInComparable */ -public expect fun > T.coerceIn(range: ClosedRange): T +public fun > T.coerceIn(range: ClosedRange): T { + if (range is ClosedFloatingPointRange) { + return this.coerceIn(range) + } + if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") + return when { + this < range.start -> range.start + this > range.endInclusive -> range.endInclusive + else -> this + } +} /** * Ensures that this value lies in the specified [range]. @@ -661,7 +928,17 @@ public expect fun > T.coerceIn(range: ClosedRange): T * @return this value if it's in the [range], or `range.start` if this value is less than `range.start`, or `range.endInclusive` if this value is greater than `range.endInclusive`. * @sample samples.comparisons.ComparableOps.coerceIn */ -public expect fun Int.coerceIn(range: ClosedRange): Int +public fun Int.coerceIn(range: ClosedRange): Int { + if (range is ClosedFloatingPointRange) { + return this.coerceIn(range) + } + if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") + return when { + this < range.start -> range.start + this > range.endInclusive -> range.endInclusive + else -> this + } +} /** * Ensures that this value lies in the specified [range]. @@ -669,5 +946,15 @@ public expect fun Int.coerceIn(range: ClosedRange): Int * @return this value if it's in the [range], or `range.start` if this value is less than `range.start`, or `range.endInclusive` if this value is greater than `range.endInclusive`. * @sample samples.comparisons.ComparableOps.coerceIn */ -public expect fun Long.coerceIn(range: ClosedRange): Long +public fun Long.coerceIn(range: ClosedRange): Long { + if (range is ClosedFloatingPointRange) { + return this.coerceIn(range) + } + if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") + return when { + this < range.start -> range.start + this > range.endInclusive -> range.endInclusive + else -> this + } +} diff --git a/libraries/stdlib/common/src/generated/_Sequences.kt b/libraries/stdlib/common/src/generated/_Sequences.kt index ac483164c36..0974ac6f628 100644 --- a/libraries/stdlib/common/src/generated/_Sequences.kt +++ b/libraries/stdlib/common/src/generated/_Sequences.kt @@ -18,28 +18,54 @@ import kotlin.coroutines.experimental.* * * The operation is _terminal_. */ -public expect operator fun <@kotlin.internal.OnlyInputTypes T> Sequence.contains(element: T): Boolean +public operator fun <@kotlin.internal.OnlyInputTypes T> Sequence.contains(element: T): Boolean { + return indexOf(element) >= 0 +} /** * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this sequence. * * The operation is _terminal_. */ -public expect fun Sequence.elementAt(index: Int): T +public fun Sequence.elementAt(index: Int): T { + return elementAtOrElse(index) { throw IndexOutOfBoundsException("Sequence doesn't contain element at index $index.") } +} /** * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this sequence. * * The operation is _terminal_. */ -public expect fun Sequence.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T +public fun Sequence.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T { + if (index < 0) + return defaultValue(index) + val iterator = iterator() + var count = 0 + while (iterator.hasNext()) { + val element = iterator.next() + if (index == count++) + return element + } + return defaultValue(index) +} /** * Returns an element at the given [index] or `null` if the [index] is out of bounds of this sequence. * * The operation is _terminal_. */ -public expect fun Sequence.elementAtOrNull(index: Int): T? +public fun Sequence.elementAtOrNull(index: Int): T? { + if (index < 0) + return null + val iterator = iterator() + var count = 0 + while (iterator.hasNext()) { + val element = iterator.next() + if (index == count++) + return element + } + return null +} /** * Returns the first element matching the given [predicate], or `null` if no such element was found. @@ -47,7 +73,9 @@ public expect fun Sequence.elementAtOrNull(index: Int): T? * The operation is _terminal_. */ @kotlin.internal.InlineOnly -public expect inline fun Sequence.find(predicate: (T) -> Boolean): T? +public inline fun Sequence.find(predicate: (T) -> Boolean): T? { + return firstOrNull(predicate) +} /** * Returns the last element matching the given [predicate], or `null` if no such element was found. @@ -55,7 +83,9 @@ public expect inline fun Sequence.find(predicate: (T) -> Boolean): T? * The operation is _terminal_. */ @kotlin.internal.InlineOnly -public expect inline fun Sequence.findLast(predicate: (T) -> Boolean): T? +public inline fun Sequence.findLast(predicate: (T) -> Boolean): T? { + return lastOrNull(predicate) +} /** * Returns first element. @@ -63,7 +93,12 @@ public expect inline fun Sequence.findLast(predicate: (T) -> Boolean): T? * * The operation is _terminal_. */ -public expect fun Sequence.first(): T +public fun Sequence.first(): T { + val iterator = iterator() + if (!iterator.hasNext()) + throw NoSuchElementException("Sequence is empty.") + return iterator.next() +} /** * Returns the first element matching the given [predicate]. @@ -71,42 +106,78 @@ public expect fun Sequence.first(): T * * The operation is _terminal_. */ -public expect inline fun Sequence.first(predicate: (T) -> Boolean): T +public inline fun Sequence.first(predicate: (T) -> Boolean): T { + for (element in this) if (predicate(element)) return element + throw NoSuchElementException("Sequence contains no element matching the predicate.") +} /** * Returns the first element, or `null` if the sequence is empty. * * The operation is _terminal_. */ -public expect fun Sequence.firstOrNull(): T? +public fun Sequence.firstOrNull(): T? { + val iterator = iterator() + if (!iterator.hasNext()) + return null + return iterator.next() +} /** * Returns the first element matching the given [predicate], or `null` if element was not found. * * The operation is _terminal_. */ -public expect inline fun Sequence.firstOrNull(predicate: (T) -> Boolean): T? +public inline fun Sequence.firstOrNull(predicate: (T) -> Boolean): T? { + for (element in this) if (predicate(element)) return element + return null +} /** * Returns first index of [element], or -1 if the sequence does not contain element. * * The operation is _terminal_. */ -public expect fun <@kotlin.internal.OnlyInputTypes T> Sequence.indexOf(element: T): Int +public fun <@kotlin.internal.OnlyInputTypes T> Sequence.indexOf(element: T): Int { + var index = 0 + for (item in this) { + if (element == item) + return index + index++ + } + return -1 +} /** * Returns index of the first element matching the given [predicate], or -1 if the sequence does not contain such element. * * The operation is _terminal_. */ -public expect inline fun Sequence.indexOfFirst(predicate: (T) -> Boolean): Int +public inline fun Sequence.indexOfFirst(predicate: (T) -> Boolean): Int { + var index = 0 + for (item in this) { + if (predicate(item)) + return index + index++ + } + return -1 +} /** * Returns index of the last element matching the given [predicate], or -1 if the sequence does not contain such element. * * The operation is _terminal_. */ -public expect inline fun Sequence.indexOfLast(predicate: (T) -> Boolean): Int +public inline fun Sequence.indexOfLast(predicate: (T) -> Boolean): Int { + var lastIndex = -1 + var index = 0 + for (item in this) { + if (predicate(item)) + lastIndex = index + index++ + } + return lastIndex +} /** * Returns the last element. @@ -114,7 +185,15 @@ public expect inline fun Sequence.indexOfLast(predicate: (T) -> Boolean): * * The operation is _terminal_. */ -public expect fun Sequence.last(): T +public fun Sequence.last(): T { + val iterator = iterator() + if (!iterator.hasNext()) + throw NoSuchElementException("Sequence is empty.") + var last = iterator.next() + while (iterator.hasNext()) + last = iterator.next() + return last +} /** * Returns the last element matching the given [predicate]. @@ -122,77 +201,166 @@ public expect fun Sequence.last(): T * * The operation is _terminal_. */ -public expect inline fun Sequence.last(predicate: (T) -> Boolean): T +public inline fun Sequence.last(predicate: (T) -> Boolean): T { + var last: T? = null + var found = false + for (element in this) { + if (predicate(element)) { + last = element + found = true + } + } + if (!found) throw NoSuchElementException("Sequence contains no element matching the predicate.") + @Suppress("UNCHECKED_CAST") + return last as T +} /** * Returns last index of [element], or -1 if the sequence does not contain element. * * The operation is _terminal_. */ -public expect fun <@kotlin.internal.OnlyInputTypes T> Sequence.lastIndexOf(element: T): Int +public fun <@kotlin.internal.OnlyInputTypes T> Sequence.lastIndexOf(element: T): Int { + var lastIndex = -1 + var index = 0 + for (item in this) { + if (element == item) + lastIndex = index + index++ + } + return lastIndex +} /** * Returns the last element, or `null` if the sequence is empty. * * The operation is _terminal_. */ -public expect fun Sequence.lastOrNull(): T? +public fun Sequence.lastOrNull(): T? { + val iterator = iterator() + if (!iterator.hasNext()) + return null + var last = iterator.next() + while (iterator.hasNext()) + last = iterator.next() + return last +} /** * Returns the last element matching the given [predicate], or `null` if no such element was found. * * The operation is _terminal_. */ -public expect inline fun Sequence.lastOrNull(predicate: (T) -> Boolean): T? +public inline fun Sequence.lastOrNull(predicate: (T) -> Boolean): T? { + var last: T? = null + for (element in this) { + if (predicate(element)) { + last = element + } + } + return last +} /** * Returns the single element, or throws an exception if the sequence is empty or has more than one element. * * The operation is _terminal_. */ -public expect fun Sequence.single(): T +public fun Sequence.single(): T { + val iterator = iterator() + if (!iterator.hasNext()) + throw NoSuchElementException("Sequence is empty.") + val single = iterator.next() + if (iterator.hasNext()) + throw IllegalArgumentException("Sequence has more than one element.") + return single +} /** * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. * * The operation is _terminal_. */ -public expect inline fun Sequence.single(predicate: (T) -> Boolean): T +public inline fun Sequence.single(predicate: (T) -> Boolean): T { + var single: T? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) throw IllegalArgumentException("Sequence contains more than one matching element.") + single = element + found = true + } + } + if (!found) throw NoSuchElementException("Sequence contains no element matching the predicate.") + @Suppress("UNCHECKED_CAST") + return single as T +} /** * Returns single element, or `null` if the sequence is empty or has more than one element. * * The operation is _terminal_. */ -public expect fun Sequence.singleOrNull(): T? +public fun Sequence.singleOrNull(): T? { + val iterator = iterator() + if (!iterator.hasNext()) + return null + val single = iterator.next() + if (iterator.hasNext()) + return null + return single +} /** * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. * * The operation is _terminal_. */ -public expect inline fun Sequence.singleOrNull(predicate: (T) -> Boolean): T? +public inline fun Sequence.singleOrNull(predicate: (T) -> Boolean): T? { + var single: T? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) return null + single = element + found = true + } + } + if (!found) return null + return single +} /** * Returns a sequence containing all elements except first [n] elements. * * The operation is _intermediate_ and _stateless_. */ -public expect fun Sequence.drop(n: Int): Sequence +public fun Sequence.drop(n: Int): Sequence { + require(n >= 0) { "Requested element count $n is less than zero." } + return when { + n == 0 -> this + this is DropTakeSequence -> this.drop(n) + else -> DropSequence(this, n) + } +} /** * Returns a sequence containing all elements except first elements that satisfy the given [predicate]. * * The operation is _intermediate_ and _stateless_. */ -public expect fun Sequence.dropWhile(predicate: (T) -> Boolean): Sequence +public fun Sequence.dropWhile(predicate: (T) -> Boolean): Sequence { + return DropWhileSequence(this, predicate) +} /** * Returns a sequence containing only elements matching the given [predicate]. * * The operation is _intermediate_ and _stateless_. */ -public expect fun Sequence.filter(predicate: (T) -> Boolean): Sequence +public fun Sequence.filter(predicate: (T) -> Boolean): Sequence { + return FilteringSequence(this, true, predicate) +} /** * Returns a sequence containing only elements matching the given [predicate]. @@ -201,7 +369,10 @@ public expect fun Sequence.filter(predicate: (T) -> Boolean): Sequence * * The operation is _intermediate_ and _stateless_. */ -public expect fun Sequence.filterIndexed(predicate: (index: Int, T) -> Boolean): Sequence +public fun Sequence.filterIndexed(predicate: (index: Int, T) -> Boolean): Sequence { + // TODO: Rewrite with generalized MapFilterIndexingSequence + return TransformingSequence(FilteringSequence(IndexingSequence(this), true, { predicate(it.index, it.value) }), { it.value }) +} /** * Appends all elements matching the given [predicate] to the given [destination]. @@ -210,105 +381,161 @@ public expect fun Sequence.filterIndexed(predicate: (index: Int, T) -> Bo * * The operation is _terminal_. */ -public expect inline fun > Sequence.filterIndexedTo(destination: C, predicate: (index: Int, T) -> Boolean): C +public inline fun > Sequence.filterIndexedTo(destination: C, predicate: (index: Int, T) -> Boolean): C { + forEachIndexed { index, element -> + if (predicate(index, element)) destination.add(element) + } + return destination +} /** * Returns a sequence containing all elements that are instances of specified type parameter R. * * The operation is _intermediate_ and _stateless_. */ -public expect inline fun Sequence<*>.filterIsInstance(): Sequence<@kotlin.internal.NoInfer R> +public inline fun Sequence<*>.filterIsInstance(): Sequence<@kotlin.internal.NoInfer R> { + @Suppress("UNCHECKED_CAST") + return filter { it is R } as Sequence +} /** * Appends all elements that are instances of specified type parameter R to the given [destination]. * * The operation is _terminal_. */ -public expect inline fun > Sequence<*>.filterIsInstanceTo(destination: C): C +public inline fun > Sequence<*>.filterIsInstanceTo(destination: C): C { + for (element in this) if (element is R) destination.add(element) + return destination +} /** * Returns a sequence containing all elements not matching the given [predicate]. * * The operation is _intermediate_ and _stateless_. */ -public expect fun Sequence.filterNot(predicate: (T) -> Boolean): Sequence +public fun Sequence.filterNot(predicate: (T) -> Boolean): Sequence { + return FilteringSequence(this, false, predicate) +} /** * Returns a sequence containing all elements that are not `null`. * * The operation is _intermediate_ and _stateless_. */ -public expect fun Sequence.filterNotNull(): Sequence +public fun Sequence.filterNotNull(): Sequence { + @Suppress("UNCHECKED_CAST") + return filterNot { it == null } as Sequence +} /** * Appends all elements that are not `null` to the given [destination]. * * The operation is _terminal_. */ -public expect fun , T : Any> Sequence.filterNotNullTo(destination: C): C +public fun , T : Any> Sequence.filterNotNullTo(destination: C): C { + for (element in this) if (element != null) destination.add(element) + return destination +} /** * Appends all elements not matching the given [predicate] to the given [destination]. * * The operation is _terminal_. */ -public expect inline fun > Sequence.filterNotTo(destination: C, predicate: (T) -> Boolean): C +public inline fun > Sequence.filterNotTo(destination: C, predicate: (T) -> Boolean): C { + for (element in this) if (!predicate(element)) destination.add(element) + return destination +} /** * Appends all elements matching the given [predicate] to the given [destination]. * * The operation is _terminal_. */ -public expect inline fun > Sequence.filterTo(destination: C, predicate: (T) -> Boolean): C +public inline fun > Sequence.filterTo(destination: C, predicate: (T) -> Boolean): C { + for (element in this) if (predicate(element)) destination.add(element) + return destination +} /** * Returns a sequence containing first [n] elements. * * The operation is _intermediate_ and _stateless_. */ -public expect fun Sequence.take(n: Int): Sequence +public fun Sequence.take(n: Int): Sequence { + require(n >= 0) { "Requested element count $n is less than zero." } + return when { + n == 0 -> emptySequence() + this is DropTakeSequence -> this.take(n) + else -> TakeSequence(this, n) + } +} /** * Returns a sequence containing first elements satisfying the given [predicate]. * * The operation is _intermediate_ and _stateless_. */ -public expect fun Sequence.takeWhile(predicate: (T) -> Boolean): Sequence +public fun Sequence.takeWhile(predicate: (T) -> Boolean): Sequence { + return TakeWhileSequence(this, predicate) +} /** * Returns a sequence that yields elements of this sequence sorted according to their natural sort order. * * The operation is _intermediate_ and _stateful_. */ -public expect fun > Sequence.sorted(): Sequence +public fun > Sequence.sorted(): Sequence { + return object : Sequence { + override fun iterator(): Iterator { + val sortedList = this@sorted.toMutableList() + sortedList.sort() + return sortedList.iterator() + } + } +} /** * Returns a sequence that yields elements of this sequence sorted according to natural sort order of the value returned by specified [selector] function. * * The operation is _intermediate_ and _stateful_. */ -public expect inline fun > Sequence.sortedBy(crossinline selector: (T) -> R?): Sequence +public inline fun > Sequence.sortedBy(crossinline selector: (T) -> R?): Sequence { + return sortedWith(compareBy(selector)) +} /** * Returns a sequence that yields elements of this sequence sorted descending according to natural sort order of the value returned by specified [selector] function. * * The operation is _intermediate_ and _stateful_. */ -public expect inline fun > Sequence.sortedByDescending(crossinline selector: (T) -> R?): Sequence +public inline fun > Sequence.sortedByDescending(crossinline selector: (T) -> R?): Sequence { + return sortedWith(compareByDescending(selector)) +} /** * Returns a sequence that yields elements of this sequence sorted descending according to their natural sort order. * * The operation is _intermediate_ and _stateful_. */ -public expect fun > Sequence.sortedDescending(): Sequence +public fun > Sequence.sortedDescending(): Sequence { + return sortedWith(reverseOrder()) +} /** * Returns a sequence that yields elements of this sequence sorted according to the specified [comparator]. * * The operation is _intermediate_ and _stateful_. */ -public expect fun Sequence.sortedWith(comparator: Comparator): Sequence +public fun Sequence.sortedWith(comparator: Comparator): Sequence { + return object : Sequence { + override fun iterator(): Iterator { + val sortedList = this@sortedWith.toMutableList() + sortedList.sortWith(comparator) + return sortedList.iterator() + } + } +} /** * Returns a [Map] containing key-value pairs provided by [transform] function @@ -320,7 +547,9 @@ public expect fun Sequence.sortedWith(comparator: Comparator): Sequ * * The operation is _terminal_. */ -public expect inline fun Sequence.associate(transform: (T) -> Pair): Map +public inline fun Sequence.associate(transform: (T) -> Pair): Map { + return associateTo(LinkedHashMap(), transform) +} /** * Returns a [Map] containing the elements from the given sequence indexed by the key @@ -332,7 +561,9 @@ public expect inline fun Sequence.associate(transform: (T) -> Pair< * * The operation is _terminal_. */ -public expect inline fun Sequence.associateBy(keySelector: (T) -> K): Map +public inline fun Sequence.associateBy(keySelector: (T) -> K): Map { + return associateByTo(LinkedHashMap(), keySelector) +} /** * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given sequence. @@ -343,7 +574,9 @@ public expect inline fun Sequence.associateBy(keySelector: (T) -> K): * * The operation is _terminal_. */ -public expect inline fun Sequence.associateBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map +public inline fun Sequence.associateBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map { + return associateByTo(LinkedHashMap(), keySelector, valueTransform) +} /** * Populates and returns the [destination] mutable map with key-value pairs, @@ -354,7 +587,12 @@ public expect inline fun Sequence.associateBy(keySelector: (T) -> K * * The operation is _terminal_. */ -public expect inline fun > Sequence.associateByTo(destination: M, keySelector: (T) -> K): M +public inline fun > Sequence.associateByTo(destination: M, keySelector: (T) -> K): M { + for (element in this) { + destination.put(keySelector(element), element) + } + return destination +} /** * Populates and returns the [destination] mutable map with key-value pairs, @@ -365,7 +603,12 @@ public expect inline fun > Sequence.associat * * The operation is _terminal_. */ -public expect inline fun > Sequence.associateByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M +public inline fun > Sequence.associateByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M { + for (element in this) { + destination.put(keySelector(element), valueTransform(element)) + } + return destination +} /** * Populates and returns the [destination] mutable map with key-value pairs @@ -375,35 +618,51 @@ public expect inline fun > Sequence.assoc * * The operation is _terminal_. */ -public expect inline fun > Sequence.associateTo(destination: M, transform: (T) -> Pair): M +public inline fun > Sequence.associateTo(destination: M, transform: (T) -> Pair): M { + for (element in this) { + destination += transform(element) + } + return destination +} /** * Appends all elements to the given [destination] collection. * * The operation is _terminal_. */ -public expect fun > Sequence.toCollection(destination: C): C +public fun > Sequence.toCollection(destination: C): C { + for (item in this) { + destination.add(item) + } + return destination +} /** * Returns a [HashSet] of all elements. * * The operation is _terminal_. */ -public expect fun Sequence.toHashSet(): HashSet +public fun Sequence.toHashSet(): HashSet { + return toCollection(HashSet()) +} /** * Returns a [List] containing all elements. * * The operation is _terminal_. */ -public expect fun Sequence.toList(): List +public fun Sequence.toList(): List { + return this.toMutableList().optimizeReadOnlyList() +} /** * Returns a [MutableList] filled with all elements of this sequence. * * The operation is _terminal_. */ -public expect fun Sequence.toMutableList(): MutableList +public fun Sequence.toMutableList(): MutableList { + return toCollection(ArrayList()) +} /** * Returns a [Set] of all elements. @@ -412,21 +671,31 @@ public expect fun Sequence.toMutableList(): MutableList * * The operation is _terminal_. */ -public expect fun Sequence.toSet(): Set +public fun Sequence.toSet(): Set { + return toCollection(LinkedHashSet()).optimizeReadOnlySet() +} /** * Returns a single sequence of all elements from results of [transform] function being invoked on each element of original sequence. * * The operation is _intermediate_ and _stateless_. */ -public expect fun Sequence.flatMap(transform: (T) -> Sequence): Sequence +public fun Sequence.flatMap(transform: (T) -> Sequence): Sequence { + return FlatteningSequence(this, transform, { it.iterator() }) +} /** * Appends all elements yielded from results of [transform] function being invoked on each element of original sequence, to the given [destination]. * * The operation is _terminal_. */ -public expect inline fun > Sequence.flatMapTo(destination: C, transform: (T) -> Sequence): C +public inline fun > Sequence.flatMapTo(destination: C, transform: (T) -> Sequence): C { + for (element in this) { + val list = transform(element) + destination.addAll(list) + } + return destination +} /** * Groups elements of the original sequence by the key returned by the given [keySelector] function @@ -438,7 +707,9 @@ public expect inline fun > Sequence.flatMap * * The operation is _terminal_. */ -public expect inline fun Sequence.groupBy(keySelector: (T) -> K): Map> +public inline fun Sequence.groupBy(keySelector: (T) -> K): Map> { + return groupByTo(LinkedHashMap>(), keySelector) +} /** * Groups values returned by the [valueTransform] function applied to each element of the original sequence @@ -451,7 +722,9 @@ public expect inline fun Sequence.groupBy(keySelector: (T) -> K): Map< * * The operation is _terminal_. */ -public expect inline fun Sequence.groupBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map> +public inline fun Sequence.groupBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map> { + return groupByTo(LinkedHashMap>(), keySelector, valueTransform) +} /** * Groups elements of the original sequence by the key returned by the given [keySelector] function @@ -463,7 +736,14 @@ public expect inline fun Sequence.groupBy(keySelector: (T) -> K, va * * The operation is _terminal_. */ -public expect inline fun >> Sequence.groupByTo(destination: M, keySelector: (T) -> K): M +public inline fun >> Sequence.groupByTo(destination: M, keySelector: (T) -> K): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(element) + } + return destination +} /** * Groups values returned by the [valueTransform] function applied to each element of the original sequence @@ -476,7 +756,14 @@ public expect inline fun >> Sequence>> Sequence.groupByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M +public inline fun >> Sequence.groupByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(valueTransform(element)) + } + return destination +} /** * Creates a [Grouping] source from a sequence to be used later with one of group-and-fold operations @@ -487,7 +774,12 @@ public expect inline fun >> Sequenc * The operation is _intermediate_ and _stateless_. */ @SinceKotlin("1.1") -public expect inline fun Sequence.groupingBy(crossinline keySelector: (T) -> K): Grouping +public inline fun Sequence.groupingBy(crossinline keySelector: (T) -> K): Grouping { + return object : Grouping { + override fun sourceIterator(): Iterator = this@groupingBy.iterator() + override fun keyOf(element: T): K = keySelector(element) + } +} /** * Returns a sequence containing the results of applying the given [transform] function @@ -495,7 +787,9 @@ public expect inline fun Sequence.groupingBy(crossinline keySelector: * * The operation is _intermediate_ and _stateless_. */ -public expect fun Sequence.map(transform: (T) -> R): Sequence +public fun Sequence.map(transform: (T) -> R): Sequence { + return TransformingSequence(this, transform) +} /** * Returns a sequence containing the results of applying the given [transform] function @@ -505,7 +799,9 @@ public expect fun Sequence.map(transform: (T) -> R): Sequence * * The operation is _intermediate_ and _stateless_. */ -public expect fun Sequence.mapIndexed(transform: (index: Int, T) -> R): Sequence +public fun Sequence.mapIndexed(transform: (index: Int, T) -> R): Sequence { + return TransformingIndexedSequence(this, transform) +} /** * Returns a sequence containing only the non-null results of applying the given [transform] function @@ -515,7 +811,9 @@ public expect fun Sequence.mapIndexed(transform: (index: Int, T) -> R) * * The operation is _intermediate_ and _stateless_. */ -public expect fun Sequence.mapIndexedNotNull(transform: (index: Int, T) -> R?): Sequence +public fun Sequence.mapIndexedNotNull(transform: (index: Int, T) -> R?): Sequence { + return TransformingIndexedSequence(this, transform).filterNotNull() +} /** * Applies the given [transform] function to each element and its index in the original sequence @@ -525,7 +823,10 @@ public expect fun Sequence.mapIndexedNotNull(transform: (index: * * The operation is _terminal_. */ -public expect inline fun > Sequence.mapIndexedNotNullTo(destination: C, transform: (index: Int, T) -> R?): C +public inline fun > Sequence.mapIndexedNotNullTo(destination: C, transform: (index: Int, T) -> R?): C { + forEachIndexed { index, element -> transform(index, element)?.let { destination.add(it) } } + return destination +} /** * Applies the given [transform] function to each element and its index in the original sequence @@ -535,7 +836,12 @@ public expect inline fun > Sequence.m * * The operation is _terminal_. */ -public expect inline fun > Sequence.mapIndexedTo(destination: C, transform: (index: Int, T) -> R): C +public inline fun > Sequence.mapIndexedTo(destination: C, transform: (index: Int, T) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} /** * Returns a sequence containing only the non-null results of applying the given [transform] function @@ -543,7 +849,9 @@ public expect inline fun > Sequence.mapInde * * The operation is _intermediate_ and _stateless_. */ -public expect fun Sequence.mapNotNull(transform: (T) -> R?): Sequence +public fun Sequence.mapNotNull(transform: (T) -> R?): Sequence { + return TransformingSequence(this, transform).filterNotNull() +} /** * Applies the given [transform] function to each element in the original sequence @@ -551,7 +859,10 @@ public expect fun Sequence.mapNotNull(transform: (T) -> R?): Seq * * The operation is _terminal_. */ -public expect inline fun > Sequence.mapNotNullTo(destination: C, transform: (T) -> R?): C +public inline fun > Sequence.mapNotNullTo(destination: C, transform: (T) -> R?): C { + forEach { element -> transform(element)?.let { destination.add(it) } } + return destination +} /** * Applies the given [transform] function to each element of the original sequence @@ -559,14 +870,20 @@ public expect inline fun > Sequence.m * * The operation is _terminal_. */ -public expect inline fun > Sequence.mapTo(destination: C, transform: (T) -> R): C +public inline fun > Sequence.mapTo(destination: C, transform: (T) -> R): C { + for (item in this) + destination.add(transform(item)) + return destination +} /** * Returns a sequence of [IndexedValue] for each element of the original sequence. * * The operation is _intermediate_ and _stateless_. */ -public expect fun Sequence.withIndex(): Sequence> +public fun Sequence.withIndex(): Sequence> { + return IndexingSequence(this) +} /** * Returns a sequence containing only distinct elements from the given sequence. @@ -575,7 +892,9 @@ public expect fun Sequence.withIndex(): Sequence> * * The operation is _intermediate_ and _stateful_. */ -public expect fun Sequence.distinct(): Sequence +public fun Sequence.distinct(): Sequence { + return this.distinctBy { it } +} /** * Returns a sequence containing only elements from the given sequence @@ -585,7 +904,9 @@ public expect fun Sequence.distinct(): Sequence * * The operation is _intermediate_ and _stateful_. */ -public expect fun Sequence.distinctBy(selector: (T) -> K): Sequence +public fun Sequence.distinctBy(selector: (T) -> K): Sequence { + return DistinctSequence(this, selector) +} /** * Returns a mutable set containing all distinct elements from the given sequence. @@ -594,7 +915,11 @@ public expect fun Sequence.distinctBy(selector: (T) -> K): Sequence * * The operation is _terminal_. */ -public expect fun Sequence.toMutableSet(): MutableSet +public fun Sequence.toMutableSet(): MutableSet { + val set = LinkedHashSet() + for (item in this) set.add(item) + return set +} /** * Returns `true` if all elements match the given [predicate]. @@ -603,7 +928,10 @@ public expect fun Sequence.toMutableSet(): MutableSet * * The operation is _terminal_. */ -public expect inline fun Sequence.all(predicate: (T) -> Boolean): Boolean +public inline fun Sequence.all(predicate: (T) -> Boolean): Boolean { + for (element in this) if (!predicate(element)) return false + return true +} /** * Returns `true` if sequence has at least one element. @@ -612,7 +940,9 @@ public expect inline fun Sequence.all(predicate: (T) -> Boolean): Boolean * * The operation is _terminal_. */ -public expect fun Sequence.any(): Boolean +public fun Sequence.any(): Boolean { + return iterator().hasNext() +} /** * Returns `true` if at least one element matches the given [predicate]. @@ -621,28 +951,43 @@ public expect fun Sequence.any(): Boolean * * The operation is _terminal_. */ -public expect inline fun Sequence.any(predicate: (T) -> Boolean): Boolean +public inline fun Sequence.any(predicate: (T) -> Boolean): Boolean { + for (element in this) if (predicate(element)) return true + return false +} /** * Returns the number of elements in this sequence. * * The operation is _terminal_. */ -public expect fun Sequence.count(): Int +public fun Sequence.count(): Int { + var count = 0 + for (element in this) count++ + return count +} /** * Returns the number of elements matching the given [predicate]. * * The operation is _terminal_. */ -public expect inline fun Sequence.count(predicate: (T) -> Boolean): Int +public inline fun Sequence.count(predicate: (T) -> Boolean): Int { + var count = 0 + for (element in this) if (predicate(element)) count++ + return count +} /** * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. * * The operation is _terminal_. */ -public expect inline fun Sequence.fold(initial: R, operation: (acc: R, T) -> R): R +public inline fun Sequence.fold(initial: R, operation: (acc: R, T) -> R): R { + var accumulator = initial + for (element in this) accumulator = operation(accumulator, element) + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from left to right @@ -652,14 +997,21 @@ public expect inline fun Sequence.fold(initial: R, operation: (acc: R, * * The operation is _terminal_. */ -public expect inline fun Sequence.foldIndexed(initial: R, operation: (index: Int, acc: R, T) -> R): R +public inline fun Sequence.foldIndexed(initial: R, operation: (index: Int, acc: R, T) -> R): R { + var index = 0 + var accumulator = initial + for (element in this) accumulator = operation(index++, accumulator, element) + return accumulator +} /** * Performs the given [action] on each element. * * The operation is _terminal_. */ -public expect inline fun Sequence.forEach(action: (T) -> Unit): Unit +public inline fun Sequence.forEach(action: (T) -> Unit): Unit { + for (element in this) action(element) +} /** * Performs the given [action] on each element, providing sequential index with the element. @@ -668,7 +1020,10 @@ public expect inline fun Sequence.forEach(action: (T) -> Unit): Unit * * The operation is _terminal_. */ -public expect inline fun Sequence.forEachIndexed(action: (index: Int, T) -> Unit): Unit +public inline fun Sequence.forEachIndexed(action: (index: Int, T) -> Unit): Unit { + var index = 0 + for (item in this) action(index++, item) +} /** * Returns the largest element or `null` if there are no elements. @@ -678,7 +1033,18 @@ public expect inline fun Sequence.forEachIndexed(action: (index: Int, T) * The operation is _terminal_. */ @SinceKotlin("1.1") -public expect fun Sequence.max(): Double? +public fun Sequence.max(): Double? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var max = iterator.next() + if (max.isNaN()) return max + while (iterator.hasNext()) { + val e = iterator.next() + if (e.isNaN()) return e + if (max < e) max = e + } + return max +} /** * Returns the largest element or `null` if there are no elements. @@ -688,28 +1054,71 @@ public expect fun Sequence.max(): Double? * The operation is _terminal_. */ @SinceKotlin("1.1") -public expect fun Sequence.max(): Float? +public fun Sequence.max(): Float? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var max = iterator.next() + if (max.isNaN()) return max + while (iterator.hasNext()) { + val e = iterator.next() + if (e.isNaN()) return e + if (max < e) max = e + } + return max +} /** * Returns the largest element or `null` if there are no elements. * * The operation is _terminal_. */ -public expect fun > Sequence.max(): T? +public fun > Sequence.max(): T? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var max = iterator.next() + while (iterator.hasNext()) { + val e = iterator.next() + if (max < e) max = e + } + return max +} /** * Returns the first element yielding the largest value of the given function or `null` if there are no elements. * * The operation is _terminal_. */ -public expect inline fun > Sequence.maxBy(selector: (T) -> R): T? +public inline fun > Sequence.maxBy(selector: (T) -> R): T? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var maxElem = iterator.next() + var maxValue = selector(maxElem) + while (iterator.hasNext()) { + val e = iterator.next() + val v = selector(e) + if (maxValue < v) { + maxElem = e + maxValue = v + } + } + return maxElem +} /** * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. * * The operation is _terminal_. */ -public expect fun Sequence.maxWith(comparator: Comparator): T? +public fun Sequence.maxWith(comparator: Comparator): T? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var max = iterator.next() + while (iterator.hasNext()) { + val e = iterator.next() + if (comparator.compare(max, e) < 0) max = e + } + return max +} /** * Returns the smallest element or `null` if there are no elements. @@ -719,7 +1128,18 @@ public expect fun Sequence.maxWith(comparator: Comparator): T? * The operation is _terminal_. */ @SinceKotlin("1.1") -public expect fun Sequence.min(): Double? +public fun Sequence.min(): Double? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var min = iterator.next() + if (min.isNaN()) return min + while (iterator.hasNext()) { + val e = iterator.next() + if (e.isNaN()) return e + if (min > e) min = e + } + return min +} /** * Returns the smallest element or `null` if there are no elements. @@ -729,28 +1149,71 @@ public expect fun Sequence.min(): Double? * The operation is _terminal_. */ @SinceKotlin("1.1") -public expect fun Sequence.min(): Float? +public fun Sequence.min(): Float? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var min = iterator.next() + if (min.isNaN()) return min + while (iterator.hasNext()) { + val e = iterator.next() + if (e.isNaN()) return e + if (min > e) min = e + } + return min +} /** * Returns the smallest element or `null` if there are no elements. * * The operation is _terminal_. */ -public expect fun > Sequence.min(): T? +public fun > Sequence.min(): T? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var min = iterator.next() + while (iterator.hasNext()) { + val e = iterator.next() + if (min > e) min = e + } + return min +} /** * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. * * The operation is _terminal_. */ -public expect inline fun > Sequence.minBy(selector: (T) -> R): T? +public inline fun > Sequence.minBy(selector: (T) -> R): T? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var minElem = iterator.next() + var minValue = selector(minElem) + while (iterator.hasNext()) { + val e = iterator.next() + val v = selector(e) + if (minValue > v) { + minElem = e + minValue = v + } + } + return minElem +} /** * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. * * The operation is _terminal_. */ -public expect fun Sequence.minWith(comparator: Comparator): T? +public fun Sequence.minWith(comparator: Comparator): T? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var min = iterator.next() + while (iterator.hasNext()) { + val e = iterator.next() + if (comparator.compare(min, e) > 0) min = e + } + return min +} /** * Returns `true` if the sequence has no elements. @@ -759,7 +1222,9 @@ public expect fun Sequence.minWith(comparator: Comparator): T? * * The operation is _terminal_. */ -public expect fun Sequence.none(): Boolean +public fun Sequence.none(): Boolean { + return !iterator().hasNext() +} /** * Returns `true` if no elements match the given [predicate]. @@ -768,7 +1233,10 @@ public expect fun Sequence.none(): Boolean * * The operation is _terminal_. */ -public expect inline fun Sequence.none(predicate: (T) -> Boolean): Boolean +public inline fun Sequence.none(predicate: (T) -> Boolean): Boolean { + for (element in this) if (predicate(element)) return false + return true +} /** * Returns a sequence which performs the given [action] on each element of the original sequence as they pass through it. @@ -776,14 +1244,27 @@ public expect inline fun Sequence.none(predicate: (T) -> Boolean): Boolea * The operation is _intermediate_ and _stateless_. */ @SinceKotlin("1.1") -public expect fun Sequence.onEach(action: (T) -> Unit): Sequence +public fun Sequence.onEach(action: (T) -> Unit): Sequence { + return map { + action(it) + it + } +} /** * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. * * The operation is _terminal_. */ -public expect inline fun Sequence.reduce(operation: (acc: S, T) -> S): S +public inline fun Sequence.reduce(operation: (acc: S, T) -> S): S { + val iterator = this.iterator() + if (!iterator.hasNext()) throw UnsupportedOperationException("Empty sequence can't be reduced.") + var accumulator: S = iterator.next() + while (iterator.hasNext()) { + accumulator = operation(accumulator, iterator.next()) + } + return accumulator +} /** * Accumulates value starting with the first element and applying [operation] from left to right @@ -793,28 +1274,51 @@ public expect inline fun Sequence.reduce(operation: (acc: S, T) -> * * The operation is _terminal_. */ -public expect inline fun Sequence.reduceIndexed(operation: (index: Int, acc: S, T) -> S): S +public inline fun Sequence.reduceIndexed(operation: (index: Int, acc: S, T) -> S): S { + val iterator = this.iterator() + if (!iterator.hasNext()) throw UnsupportedOperationException("Empty sequence can't be reduced.") + var index = 1 + var accumulator: S = iterator.next() + while (iterator.hasNext()) { + accumulator = operation(index++, accumulator, iterator.next()) + } + return accumulator +} /** * Returns the sum of all values produced by [selector] function applied to each element in the sequence. * * The operation is _terminal_. */ -public expect inline fun Sequence.sumBy(selector: (T) -> Int): Int +public inline fun Sequence.sumBy(selector: (T) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += selector(element) + } + return sum +} /** * Returns the sum of all values produced by [selector] function applied to each element in the sequence. * * The operation is _terminal_. */ -public expect inline fun Sequence.sumByDouble(selector: (T) -> Double): Double +public inline fun Sequence.sumByDouble(selector: (T) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += selector(element) + } + return sum +} /** * Returns an original collection containing all the non-`null` elements, throwing an [IllegalArgumentException] if there are any `null` elements. * * The operation is _intermediate_ and _stateless_. */ -public expect fun Sequence.requireNoNulls(): Sequence +public fun Sequence.requireNoNulls(): Sequence { + return map { it ?: throw IllegalArgumentException("null element found in $this.") } +} /** * Splits this sequence into a sequence of lists each not exceeding the given [size]. @@ -828,7 +1332,9 @@ public expect fun Sequence.requireNoNulls(): Sequence * The operation is _intermediate_ and _stateful_. */ @SinceKotlin("1.2") -public expect fun Sequence.chunked(size: Int): Sequence> +public fun Sequence.chunked(size: Int): Sequence> { + return windowed(size, size, partialWindows = true) +} /** * Splits this sequence into several lists each not exceeding the given [size] @@ -847,14 +1353,23 @@ public expect fun Sequence.chunked(size: Int): Sequence> * The operation is _intermediate_ and _stateful_. */ @SinceKotlin("1.2") -public expect fun Sequence.chunked(size: Int, transform: (List) -> R): Sequence +public fun Sequence.chunked(size: Int, transform: (List) -> R): Sequence { + return windowed(size, size, partialWindows = true, transform = transform) +} /** * Returns a sequence containing all elements of the original sequence without the first occurrence of the given [element]. * * The operation is _intermediate_ and _stateless_. */ -public expect operator fun Sequence.minus(element: T): Sequence +public operator fun Sequence.minus(element: T): Sequence { + return object: Sequence { + override fun iterator(): Iterator { + var removed = false + return this@minus.filter { if (!removed && it == element) { removed = true; false } else true }.iterator() + } + } +} /** * Returns a sequence containing all elements of original sequence except the elements contained in the given [elements] array. @@ -864,7 +1379,15 @@ public expect operator fun Sequence.minus(element: T): Sequence * * The operation is _intermediate_ and _stateful_. */ -public expect operator fun Sequence.minus(elements: Array): Sequence +public operator fun Sequence.minus(elements: Array): Sequence { + if (elements.isEmpty()) return this + return object: Sequence { + override fun iterator(): Iterator { + val other = elements.toHashSet() + return this@minus.filterNot { it in other }.iterator() + } + } +} /** * Returns a sequence containing all elements of original sequence except the elements contained in the given [elements] collection. @@ -874,7 +1397,17 @@ public expect operator fun Sequence.minus(elements: Array): Sequen * * The operation is _intermediate_ and _stateful_. */ -public expect operator fun Sequence.minus(elements: Iterable): Sequence +public operator fun Sequence.minus(elements: Iterable): Sequence { + return object: Sequence { + override fun iterator(): Iterator { + val other = elements.convertToSetForSetOperation() + if (other.isEmpty()) + return this@minus.iterator() + else + return this@minus.filterNot { it in other }.iterator() + } + } +} /** * Returns a sequence containing all elements of original sequence except the elements contained in the given [elements] sequence. @@ -884,7 +1417,17 @@ public expect operator fun Sequence.minus(elements: Iterable): Sequenc * * The operation is _intermediate_ for this sequence and _terminal_ and _stateful_ for the [elements] sequence. */ -public expect operator fun Sequence.minus(elements: Sequence): Sequence +public operator fun Sequence.minus(elements: Sequence): Sequence { + return object: Sequence { + override fun iterator(): Iterator { + val other = elements.toHashSet() + if (other.isEmpty()) + return this@minus.iterator() + else + return this@minus.filterNot { it in other }.iterator() + } + } +} /** * Returns a sequence containing all elements of the original sequence without the first occurrence of the given [element]. @@ -892,7 +1435,9 @@ public expect operator fun Sequence.minus(elements: Sequence): Sequenc * The operation is _intermediate_ and _stateless_. */ @kotlin.internal.InlineOnly -public expect inline fun Sequence.minusElement(element: T): Sequence +public inline fun Sequence.minusElement(element: T): Sequence { + return minus(element) +} /** * Splits the original sequence into pair of lists, @@ -901,14 +1446,27 @@ public expect inline fun Sequence.minusElement(element: T): Sequence * * The operation is _terminal_. */ -public expect inline fun Sequence.partition(predicate: (T) -> Boolean): Pair, List> +public inline fun Sequence.partition(predicate: (T) -> Boolean): Pair, List> { + val first = ArrayList() + val second = ArrayList() + for (element in this) { + if (predicate(element)) { + first.add(element) + } else { + second.add(element) + } + } + return Pair(first, second) +} /** * Returns a sequence containing all elements of the original sequence and then the given [element]. * * The operation is _intermediate_ and _stateless_. */ -public expect operator fun Sequence.plus(element: T): Sequence +public operator fun Sequence.plus(element: T): Sequence { + return sequenceOf(this, sequenceOf(element)).flatten() +} /** * Returns a sequence containing all elements of original sequence and then all elements of the given [elements] array. @@ -918,7 +1476,9 @@ public expect operator fun Sequence.plus(element: T): Sequence * * The operation is _intermediate_ and _stateless_. */ -public expect operator fun Sequence.plus(elements: Array): Sequence +public operator fun Sequence.plus(elements: Array): Sequence { + return this.plus(elements.asList()) +} /** * Returns a sequence containing all elements of original sequence and then all elements of the given [elements] collection. @@ -928,7 +1488,9 @@ public expect operator fun Sequence.plus(elements: Array): Sequenc * * The operation is _intermediate_ and _stateless_. */ -public expect operator fun Sequence.plus(elements: Iterable): Sequence +public operator fun Sequence.plus(elements: Iterable): Sequence { + return sequenceOf(this, elements.asSequence()).flatten() +} /** * Returns a sequence containing all elements of original sequence and then all elements of the given [elements] sequence. @@ -938,7 +1500,9 @@ public expect operator fun Sequence.plus(elements: Iterable): Sequence * * The operation is _intermediate_ and _stateless_. */ -public expect operator fun Sequence.plus(elements: Sequence): Sequence +public operator fun Sequence.plus(elements: Sequence): Sequence { + return sequenceOf(this, elements).flatten() +} /** * Returns a sequence containing all elements of the original sequence and then the given [element]. @@ -946,7 +1510,9 @@ public expect operator fun Sequence.plus(elements: Sequence): Sequence * The operation is _intermediate_ and _stateless_. */ @kotlin.internal.InlineOnly -public expect inline fun Sequence.plusElement(element: T): Sequence +public inline fun Sequence.plusElement(element: T): Sequence { + return plus(element) +} /** * Returns a sequence of snapshots of the window of the given [size] @@ -996,14 +1562,18 @@ public fun Sequence.windowed(size: Int, step: Int = 1, partialWindows: * * The operation is _intermediate_ and _stateless_. */ -public expect infix fun Sequence.zip(other: Sequence): Sequence> +public infix fun Sequence.zip(other: Sequence): Sequence> { + return MergingSequence(this, other) { t1, t2 -> t1 to t2 } +} /** * Returns a sequence of values built from elements of both collections with same indexes using provided [transform]. Resulting sequence has length of shortest input sequences. * * The operation is _intermediate_ and _stateless_. */ -public expect fun Sequence.zip(other: Sequence, transform: (a: T, b: R) -> V): Sequence +public fun Sequence.zip(other: Sequence, transform: (a: T, b: R) -> V): Sequence { + return MergingSequence(this, other, transform) +} /** * Returns a sequence of pairs of each two adjacent elements in this sequence. @@ -1015,7 +1585,9 @@ public expect fun Sequence.zip(other: Sequence, transform: (a: T * The operation is _intermediate_ and _stateless_. */ @SinceKotlin("1.2") -public expect fun Sequence.zipWithNext(): Sequence> +public fun Sequence.zipWithNext(): Sequence> { + return zipWithNext { a, b -> a to b } +} /** * Returns a sequence containing the results of applying the given [transform] function @@ -1028,7 +1600,18 @@ public expect fun Sequence.zipWithNext(): Sequence> * The operation is _intermediate_ and _stateless_. */ @SinceKotlin("1.2") -public expect fun Sequence.zipWithNext(transform: (a: T, b: T) -> R): Sequence +public fun Sequence.zipWithNext(transform: (a: T, b: T) -> R): Sequence { + return buildSequence result@ { + val iterator = iterator() + if (!iterator.hasNext()) return@result + var current = iterator.next() + while (iterator.hasNext()) { + val next = iterator.next() + yield(transform(current, next)) + current = next + } + } +} /** * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. @@ -1071,13 +1654,17 @@ public fun Sequence.joinToString(separator: CharSequence = ", ", prefix: /** * Creates an [Iterable] instance that wraps the original sequence returning its elements when being iterated. */ -public expect fun Sequence.asIterable(): Iterable +public fun Sequence.asIterable(): Iterable { + return Iterable { this.iterator() } +} /** * Returns this sequence as a [Sequence]. */ @kotlin.internal.InlineOnly -public expect inline fun Sequence.asSequence(): Sequence +public inline fun Sequence.asSequence(): Sequence { + return this +} /** * Returns an average value of elements in the sequence. @@ -1085,7 +1672,15 @@ public expect inline fun Sequence.asSequence(): Sequence * The operation is _terminal_. */ @kotlin.jvm.JvmName("averageOfByte") -public expect fun Sequence.average(): Double +public fun Sequence.average(): Double { + var sum: Double = 0.0 + var count: Int = 0 + for (element in this) { + sum += element + count += 1 + } + return if (count == 0) Double.NaN else sum / count +} /** * Returns an average value of elements in the sequence. @@ -1093,7 +1688,15 @@ public expect fun Sequence.average(): Double * The operation is _terminal_. */ @kotlin.jvm.JvmName("averageOfShort") -public expect fun Sequence.average(): Double +public fun Sequence.average(): Double { + var sum: Double = 0.0 + var count: Int = 0 + for (element in this) { + sum += element + count += 1 + } + return if (count == 0) Double.NaN else sum / count +} /** * Returns an average value of elements in the sequence. @@ -1101,7 +1704,15 @@ public expect fun Sequence.average(): Double * The operation is _terminal_. */ @kotlin.jvm.JvmName("averageOfInt") -public expect fun Sequence.average(): Double +public fun Sequence.average(): Double { + var sum: Double = 0.0 + var count: Int = 0 + for (element in this) { + sum += element + count += 1 + } + return if (count == 0) Double.NaN else sum / count +} /** * Returns an average value of elements in the sequence. @@ -1109,7 +1720,15 @@ public expect fun Sequence.average(): Double * The operation is _terminal_. */ @kotlin.jvm.JvmName("averageOfLong") -public expect fun Sequence.average(): Double +public fun Sequence.average(): Double { + var sum: Double = 0.0 + var count: Int = 0 + for (element in this) { + sum += element + count += 1 + } + return if (count == 0) Double.NaN else sum / count +} /** * Returns an average value of elements in the sequence. @@ -1117,7 +1736,15 @@ public expect fun Sequence.average(): Double * The operation is _terminal_. */ @kotlin.jvm.JvmName("averageOfFloat") -public expect fun Sequence.average(): Double +public fun Sequence.average(): Double { + var sum: Double = 0.0 + var count: Int = 0 + for (element in this) { + sum += element + count += 1 + } + return if (count == 0) Double.NaN else sum / count +} /** * Returns an average value of elements in the sequence. @@ -1125,7 +1752,15 @@ public expect fun Sequence.average(): Double * The operation is _terminal_. */ @kotlin.jvm.JvmName("averageOfDouble") -public expect fun Sequence.average(): Double +public fun Sequence.average(): Double { + var sum: Double = 0.0 + var count: Int = 0 + for (element in this) { + sum += element + count += 1 + } + return if (count == 0) Double.NaN else sum / count +} /** * Returns the sum of all elements in the sequence. @@ -1133,7 +1768,13 @@ public expect fun Sequence.average(): Double * The operation is _terminal_. */ @kotlin.jvm.JvmName("sumOfByte") -public expect fun Sequence.sum(): Int +public fun Sequence.sum(): Int { + var sum: Int = 0 + for (element in this) { + sum += element + } + return sum +} /** * Returns the sum of all elements in the sequence. @@ -1141,7 +1782,13 @@ public expect fun Sequence.sum(): Int * The operation is _terminal_. */ @kotlin.jvm.JvmName("sumOfShort") -public expect fun Sequence.sum(): Int +public fun Sequence.sum(): Int { + var sum: Int = 0 + for (element in this) { + sum += element + } + return sum +} /** * Returns the sum of all elements in the sequence. @@ -1149,7 +1796,13 @@ public expect fun Sequence.sum(): Int * The operation is _terminal_. */ @kotlin.jvm.JvmName("sumOfInt") -public expect fun Sequence.sum(): Int +public fun Sequence.sum(): Int { + var sum: Int = 0 + for (element in this) { + sum += element + } + return sum +} /** * Returns the sum of all elements in the sequence. @@ -1157,7 +1810,13 @@ public expect fun Sequence.sum(): Int * The operation is _terminal_. */ @kotlin.jvm.JvmName("sumOfLong") -public expect fun Sequence.sum(): Long +public fun Sequence.sum(): Long { + var sum: Long = 0L + for (element in this) { + sum += element + } + return sum +} /** * Returns the sum of all elements in the sequence. @@ -1165,7 +1824,13 @@ public expect fun Sequence.sum(): Long * The operation is _terminal_. */ @kotlin.jvm.JvmName("sumOfFloat") -public expect fun Sequence.sum(): Float +public fun Sequence.sum(): Float { + var sum: Float = 0.0f + for (element in this) { + sum += element + } + return sum +} /** * Returns the sum of all elements in the sequence. @@ -1173,5 +1838,11 @@ public expect fun Sequence.sum(): Float * The operation is _terminal_. */ @kotlin.jvm.JvmName("sumOfDouble") -public expect fun Sequence.sum(): Double +public fun Sequence.sum(): Double { + var sum: Double = 0.0 + for (element in this) { + sum += element + } + return sum +} diff --git a/libraries/stdlib/common/src/generated/_Sets.kt b/libraries/stdlib/common/src/generated/_Sets.kt index 455e101e006..be1e6a382d2 100644 --- a/libraries/stdlib/common/src/generated/_Sets.kt +++ b/libraries/stdlib/common/src/generated/_Sets.kt @@ -17,28 +17,49 @@ import kotlin.comparisons.* * * The returned set preserves the element iteration order of the original set. */ -public expect operator fun Set.minus(element: T): Set +public operator fun Set.minus(element: T): Set { + val result = LinkedHashSet(mapCapacity(size)) + var removed = false + return this.filterTo(result) { if (!removed && it == element) { removed = true; false } else true } +} /** * Returns a set containing all elements of the original set except the elements contained in the given [elements] array. * * The returned set preserves the element iteration order of the original set. */ -public expect operator fun Set.minus(elements: Array): Set +public operator fun Set.minus(elements: Array): Set { + val result = LinkedHashSet(this) + result.removeAll(elements) + return result +} /** * Returns a set containing all elements of the original set except the elements contained in the given [elements] collection. * * The returned set preserves the element iteration order of the original set. */ -public expect operator fun Set.minus(elements: Iterable): Set +public operator fun Set.minus(elements: Iterable): Set { + val other = elements.convertToSetForSetOperationWith(this) + if (other.isEmpty()) + return this.toSet() + if (other is Set) + return this.filterNotTo(LinkedHashSet()) { it in other } + val result = LinkedHashSet(this) + result.removeAll(other) + return result +} /** * Returns a set containing all elements of the original set except the elements contained in the given [elements] sequence. * * The returned set preserves the element iteration order of the original set. */ -public expect operator fun Set.minus(elements: Sequence): Set +public operator fun Set.minus(elements: Sequence): Set { + val result = LinkedHashSet(this) + result.removeAll(elements) + return result +} /** * Returns a set containing all elements of the original set except the given [element]. @@ -46,14 +67,21 @@ public expect operator fun Set.minus(elements: Sequence): Set * The returned set preserves the element iteration order of the original set. */ @kotlin.internal.InlineOnly -public expect inline fun Set.minusElement(element: T): Set +public inline fun Set.minusElement(element: T): Set { + return minus(element) +} /** * Returns a set containing all elements of the original set and then the given [element] if it isn't already in this set. * * The returned set preserves the element iteration order of the original set. */ -public expect operator fun Set.plus(element: T): Set +public operator fun Set.plus(element: T): Set { + val result = LinkedHashSet(mapCapacity(size + 1)) + result.addAll(this) + result.add(element) + return result +} /** * Returns a set containing all elements of the original set and the given [elements] array, @@ -61,14 +89,24 @@ public expect operator fun Set.plus(element: T): Set * * The returned set preserves the element iteration order of the original set. */ -public expect operator fun Set.plus(elements: Array): Set +public operator fun Set.plus(elements: Array): Set { + val result = LinkedHashSet(mapCapacity(this.size + elements.size)) + result.addAll(this) + result.addAll(elements) + return result +} /** * Returns a set containing all elements of the original set and the given [elements] collection, * which aren't already in this set. * The returned set preserves the element iteration order of the original set. */ -public expect operator fun Set.plus(elements: Iterable): Set +public operator fun Set.plus(elements: Iterable): Set { + val result = LinkedHashSet(mapCapacity(elements.collectionSizeOrNull()?.let { this.size + it } ?: this.size * 2)) + result.addAll(this) + result.addAll(elements) + return result +} /** * Returns a set containing all elements of the original set and the given [elements] sequence, @@ -76,7 +114,12 @@ public expect operator fun Set.plus(elements: Iterable): Set * * The returned set preserves the element iteration order of the original set. */ -public expect operator fun Set.plus(elements: Sequence): Set +public operator fun Set.plus(elements: Sequence): Set { + val result = LinkedHashSet(mapCapacity(this.size * 2)) + result.addAll(this) + result.addAll(elements) + return result +} /** * Returns a set containing all elements of the original set and then the given [element] if it isn't already in this set. @@ -84,5 +127,7 @@ public expect operator fun Set.plus(elements: Sequence): Set * The returned set preserves the element iteration order of the original set. */ @kotlin.internal.InlineOnly -public expect inline fun Set.plusElement(element: T): Set +public inline fun Set.plusElement(element: T): Set { + return plus(element) +} diff --git a/libraries/stdlib/common/src/generated/_Strings.kt b/libraries/stdlib/common/src/generated/_Strings.kt index ca1cb370b40..4a39409b184 100644 --- a/libraries/stdlib/common/src/generated/_Strings.kt +++ b/libraries/stdlib/common/src/generated/_Strings.kt @@ -16,279 +16,488 @@ import kotlin.comparisons.* * Returns a character at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this char sequence. */ @kotlin.internal.InlineOnly -public expect inline fun CharSequence.elementAt(index: Int): Char +public inline fun CharSequence.elementAt(index: Int): Char { + return get(index) +} /** * Returns a character at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this char sequence. */ @kotlin.internal.InlineOnly -public expect inline fun CharSequence.elementAtOrElse(index: Int, defaultValue: (Int) -> Char): Char +public inline fun CharSequence.elementAtOrElse(index: Int, defaultValue: (Int) -> Char): Char { + return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) +} /** * Returns a character at the given [index] or `null` if the [index] is out of bounds of this char sequence. */ @kotlin.internal.InlineOnly -public expect inline fun CharSequence.elementAtOrNull(index: Int): Char? +public inline fun CharSequence.elementAtOrNull(index: Int): Char? { + return this.getOrNull(index) +} /** * Returns the first character matching the given [predicate], or `null` if no such character was found. */ @kotlin.internal.InlineOnly -public expect inline fun CharSequence.find(predicate: (Char) -> Boolean): Char? +public inline fun CharSequence.find(predicate: (Char) -> Boolean): Char? { + return firstOrNull(predicate) +} /** * Returns the last character matching the given [predicate], or `null` if no such character was found. */ @kotlin.internal.InlineOnly -public expect inline fun CharSequence.findLast(predicate: (Char) -> Boolean): Char? +public inline fun CharSequence.findLast(predicate: (Char) -> Boolean): Char? { + return lastOrNull(predicate) +} /** * Returns first character. * @throws [NoSuchElementException] if the char sequence is empty. */ -public expect fun CharSequence.first(): Char +public fun CharSequence.first(): Char { + if (isEmpty()) + throw NoSuchElementException("Char sequence is empty.") + return this[0] +} /** * Returns the first character matching the given [predicate]. * @throws [NoSuchElementException] if no such character is found. */ -public expect inline fun CharSequence.first(predicate: (Char) -> Boolean): Char +public inline fun CharSequence.first(predicate: (Char) -> Boolean): Char { + for (element in this) if (predicate(element)) return element + throw NoSuchElementException("Char sequence contains no character matching the predicate.") +} /** * Returns the first character, or `null` if the char sequence is empty. */ -public expect fun CharSequence.firstOrNull(): Char? +public fun CharSequence.firstOrNull(): Char? { + return if (isEmpty()) null else this[0] +} /** * Returns the first character matching the given [predicate], or `null` if character was not found. */ -public expect inline fun CharSequence.firstOrNull(predicate: (Char) -> Boolean): Char? +public inline fun CharSequence.firstOrNull(predicate: (Char) -> Boolean): Char? { + for (element in this) if (predicate(element)) return element + return null +} /** * Returns a character at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this char sequence. */ @kotlin.internal.InlineOnly -public expect inline fun CharSequence.getOrElse(index: Int, defaultValue: (Int) -> Char): Char +public inline fun CharSequence.getOrElse(index: Int, defaultValue: (Int) -> Char): Char { + return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) +} /** * Returns a character at the given [index] or `null` if the [index] is out of bounds of this char sequence. */ -public expect fun CharSequence.getOrNull(index: Int): Char? +public fun CharSequence.getOrNull(index: Int): Char? { + return if (index >= 0 && index <= lastIndex) get(index) else null +} /** * Returns index of the first character matching the given [predicate], or -1 if the char sequence does not contain such character. */ -public expect inline fun CharSequence.indexOfFirst(predicate: (Char) -> Boolean): Int +public inline fun CharSequence.indexOfFirst(predicate: (Char) -> Boolean): Int { + for (index in indices) { + if (predicate(this[index])) { + return index + } + } + return -1 +} /** * Returns index of the last character matching the given [predicate], or -1 if the char sequence does not contain such character. */ -public expect inline fun CharSequence.indexOfLast(predicate: (Char) -> Boolean): Int +public inline fun CharSequence.indexOfLast(predicate: (Char) -> Boolean): Int { + for (index in indices.reversed()) { + if (predicate(this[index])) { + return index + } + } + return -1 +} /** * Returns the last character. * @throws [NoSuchElementException] if the char sequence is empty. */ -public expect fun CharSequence.last(): Char +public fun CharSequence.last(): Char { + if (isEmpty()) + throw NoSuchElementException("Char sequence is empty.") + return this[lastIndex] +} /** * Returns the last character matching the given [predicate]. * @throws [NoSuchElementException] if no such character is found. */ -public expect inline fun CharSequence.last(predicate: (Char) -> Boolean): Char +public inline fun CharSequence.last(predicate: (Char) -> Boolean): Char { + for (index in this.indices.reversed()) { + val element = this[index] + if (predicate(element)) return element + } + throw NoSuchElementException("Char sequence contains no character matching the predicate.") +} /** * Returns the last character, or `null` if the char sequence is empty. */ -public expect fun CharSequence.lastOrNull(): Char? +public fun CharSequence.lastOrNull(): Char? { + return if (isEmpty()) null else this[length - 1] +} /** * Returns the last character matching the given [predicate], or `null` if no such character was found. */ -public expect inline fun CharSequence.lastOrNull(predicate: (Char) -> Boolean): Char? +public inline fun CharSequence.lastOrNull(predicate: (Char) -> Boolean): Char? { + for (index in this.indices.reversed()) { + val element = this[index] + if (predicate(element)) return element + } + return null +} /** * Returns the single character, or throws an exception if the char sequence is empty or has more than one character. */ -public expect fun CharSequence.single(): Char +public fun CharSequence.single(): Char { + return when (length) { + 0 -> throw NoSuchElementException("Char sequence is empty.") + 1 -> this[0] + else -> throw IllegalArgumentException("Char sequence has more than one element.") + } +} /** * Returns the single character matching the given [predicate], or throws exception if there is no or more than one matching character. */ -public expect inline fun CharSequence.single(predicate: (Char) -> Boolean): Char +public inline fun CharSequence.single(predicate: (Char) -> Boolean): Char { + var single: Char? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) throw IllegalArgumentException("Char sequence contains more than one matching element.") + single = element + found = true + } + } + if (!found) throw NoSuchElementException("Char sequence contains no character matching the predicate.") + @Suppress("UNCHECKED_CAST") + return single as Char +} /** * Returns single character, or `null` if the char sequence is empty or has more than one character. */ -public expect fun CharSequence.singleOrNull(): Char? +public fun CharSequence.singleOrNull(): Char? { + return if (length == 1) this[0] else null +} /** * Returns the single character matching the given [predicate], or `null` if character was not found or more than one character was found. */ -public expect inline fun CharSequence.singleOrNull(predicate: (Char) -> Boolean): Char? +public inline fun CharSequence.singleOrNull(predicate: (Char) -> Boolean): Char? { + var single: Char? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) return null + single = element + found = true + } + } + if (!found) return null + return single +} /** * Returns a subsequence of this char sequence with the first [n] characters removed. */ -public expect fun CharSequence.drop(n: Int): CharSequence +public fun CharSequence.drop(n: Int): CharSequence { + require(n >= 0) { "Requested character count $n is less than zero." } + return subSequence(n.coerceAtMost(length), length) +} /** * Returns a string with the first [n] characters removed. */ -public expect fun String.drop(n: Int): String +public fun String.drop(n: Int): String { + require(n >= 0) { "Requested character count $n is less than zero." } + return substring(n.coerceAtMost(length)) +} /** * Returns a subsequence of this char sequence with the last [n] characters removed. */ -public expect fun CharSequence.dropLast(n: Int): CharSequence +public fun CharSequence.dropLast(n: Int): CharSequence { + require(n >= 0) { "Requested character count $n is less than zero." } + return take((length - n).coerceAtLeast(0)) +} /** * Returns a string with the last [n] characters removed. */ -public expect fun String.dropLast(n: Int): String +public fun String.dropLast(n: Int): String { + require(n >= 0) { "Requested character count $n is less than zero." } + return take((length - n).coerceAtLeast(0)) +} /** * Returns a subsequence of this char sequence containing all characters except last characters that satisfy the given [predicate]. */ -public expect inline fun CharSequence.dropLastWhile(predicate: (Char) -> Boolean): CharSequence +public inline fun CharSequence.dropLastWhile(predicate: (Char) -> Boolean): CharSequence { + for (index in lastIndex downTo 0) + if (!predicate(this[index])) + return subSequence(0, index + 1) + return "" +} /** * Returns a string containing all characters except last characters that satisfy the given [predicate]. */ -public expect inline fun String.dropLastWhile(predicate: (Char) -> Boolean): String +public inline fun String.dropLastWhile(predicate: (Char) -> Boolean): String { + for (index in lastIndex downTo 0) + if (!predicate(this[index])) + return substring(0, index + 1) + return "" +} /** * Returns a subsequence of this char sequence containing all characters except first characters that satisfy the given [predicate]. */ -public expect inline fun CharSequence.dropWhile(predicate: (Char) -> Boolean): CharSequence +public inline fun CharSequence.dropWhile(predicate: (Char) -> Boolean): CharSequence { + for (index in this.indices) + if (!predicate(this[index])) + return subSequence(index, length) + return "" +} /** * Returns a string containing all characters except first characters that satisfy the given [predicate]. */ -public expect inline fun String.dropWhile(predicate: (Char) -> Boolean): String +public inline fun String.dropWhile(predicate: (Char) -> Boolean): String { + for (index in this.indices) + if (!predicate(this[index])) + return substring(index) + return "" +} /** * Returns a char sequence containing only those characters from the original char sequence that match the given [predicate]. */ -public expect inline fun CharSequence.filter(predicate: (Char) -> Boolean): CharSequence +public inline fun CharSequence.filter(predicate: (Char) -> Boolean): CharSequence { + return filterTo(StringBuilder(), predicate) +} /** * Returns a string containing only those characters from the original string that match the given [predicate]. */ -public expect inline fun String.filter(predicate: (Char) -> Boolean): String +public inline fun String.filter(predicate: (Char) -> Boolean): String { + return filterTo(StringBuilder(), predicate).toString() +} /** * Returns a char sequence containing only those characters from the original char sequence that match the given [predicate]. * @param [predicate] function that takes the index of a character and the character itself * and returns the result of predicate evaluation on the character. */ -public expect inline fun CharSequence.filterIndexed(predicate: (index: Int, Char) -> Boolean): CharSequence +public inline fun CharSequence.filterIndexed(predicate: (index: Int, Char) -> Boolean): CharSequence { + return filterIndexedTo(StringBuilder(), predicate) +} /** * Returns a string containing only those characters from the original string that match the given [predicate]. * @param [predicate] function that takes the index of a character and the character itself * and returns the result of predicate evaluation on the character. */ -public expect inline fun String.filterIndexed(predicate: (index: Int, Char) -> Boolean): String +public inline fun String.filterIndexed(predicate: (index: Int, Char) -> Boolean): String { + return filterIndexedTo(StringBuilder(), predicate).toString() +} /** * Appends all characters matching the given [predicate] to the given [destination]. * @param [predicate] function that takes the index of a character and the character itself * and returns the result of predicate evaluation on the character. */ -public expect inline fun CharSequence.filterIndexedTo(destination: C, predicate: (index: Int, Char) -> Boolean): C +public inline fun CharSequence.filterIndexedTo(destination: C, predicate: (index: Int, Char) -> Boolean): C { + forEachIndexed { index, element -> + if (predicate(index, element)) destination.append(element) + } + return destination +} /** * Returns a char sequence containing only those characters from the original char sequence that do not match the given [predicate]. */ -public expect inline fun CharSequence.filterNot(predicate: (Char) -> Boolean): CharSequence +public inline fun CharSequence.filterNot(predicate: (Char) -> Boolean): CharSequence { + return filterNotTo(StringBuilder(), predicate) +} /** * Returns a string containing only those characters from the original string that do not match the given [predicate]. */ -public expect inline fun String.filterNot(predicate: (Char) -> Boolean): String +public inline fun String.filterNot(predicate: (Char) -> Boolean): String { + return filterNotTo(StringBuilder(), predicate).toString() +} /** * Appends all characters not matching the given [predicate] to the given [destination]. */ -public expect inline fun CharSequence.filterNotTo(destination: C, predicate: (Char) -> Boolean): C +public inline fun CharSequence.filterNotTo(destination: C, predicate: (Char) -> Boolean): C { + for (element in this) if (!predicate(element)) destination.append(element) + return destination +} /** * Appends all characters matching the given [predicate] to the given [destination]. */ -public expect inline fun CharSequence.filterTo(destination: C, predicate: (Char) -> Boolean): C +public inline fun CharSequence.filterTo(destination: C, predicate: (Char) -> Boolean): C { + for (index in 0 until length) { + val element = get(index) + if (predicate(element)) destination.append(element) + } + return destination +} /** * Returns a char sequence containing characters of the original char sequence at the specified range of [indices]. */ -public expect fun CharSequence.slice(indices: IntRange): CharSequence +public fun CharSequence.slice(indices: IntRange): CharSequence { + if (indices.isEmpty()) return "" + return subSequence(indices) +} /** * Returns a string containing characters of the original string at the specified range of [indices]. */ -public expect fun String.slice(indices: IntRange): String +public fun String.slice(indices: IntRange): String { + if (indices.isEmpty()) return "" + return substring(indices) +} /** * Returns a char sequence containing characters of the original char sequence at specified [indices]. */ -public expect fun CharSequence.slice(indices: Iterable): CharSequence +public fun CharSequence.slice(indices: Iterable): CharSequence { + val size = indices.collectionSizeOrDefault(10) + if (size == 0) return "" + val result = StringBuilder(size) + for (i in indices) { + result.append(get(i)) + } + return result +} /** * Returns a string containing characters of the original string at specified [indices]. */ @kotlin.internal.InlineOnly -public expect inline fun String.slice(indices: Iterable): String +public inline fun String.slice(indices: Iterable): String { + return (this as CharSequence).slice(indices).toString() +} /** * Returns a subsequence of this char sequence containing the first [n] characters from this char sequence, or the entire char sequence if this char sequence is shorter. */ -public expect fun CharSequence.take(n: Int): CharSequence +public fun CharSequence.take(n: Int): CharSequence { + require(n >= 0) { "Requested character count $n is less than zero." } + return subSequence(0, n.coerceAtMost(length)) +} /** * Returns a string containing the first [n] characters from this string, or the entire string if this string is shorter. */ -public expect fun String.take(n: Int): String +public fun String.take(n: Int): String { + require(n >= 0) { "Requested character count $n is less than zero." } + return substring(0, n.coerceAtMost(length)) +} /** * Returns a subsequence of this char sequence containing the last [n] characters from this char sequence, or the entire char sequence if this char sequence is shorter. */ -public expect fun CharSequence.takeLast(n: Int): CharSequence +public fun CharSequence.takeLast(n: Int): CharSequence { + require(n >= 0) { "Requested character count $n is less than zero." } + val length = length + return subSequence(length - n.coerceAtMost(length), length) +} /** * Returns a string containing the last [n] characters from this string, or the entire string if this string is shorter. */ -public expect fun String.takeLast(n: Int): String +public fun String.takeLast(n: Int): String { + require(n >= 0) { "Requested character count $n is less than zero." } + val length = length + return substring(length - n.coerceAtMost(length)) +} /** * Returns a subsequence of this char sequence containing last characters that satisfy the given [predicate]. */ -public expect inline fun CharSequence.takeLastWhile(predicate: (Char) -> Boolean): CharSequence +public inline fun CharSequence.takeLastWhile(predicate: (Char) -> Boolean): CharSequence { + for (index in lastIndex downTo 0) { + if (!predicate(this[index])) { + return subSequence(index + 1, length) + } + } + return subSequence(0, length) +} /** * Returns a string containing last characters that satisfy the given [predicate]. */ -public expect inline fun String.takeLastWhile(predicate: (Char) -> Boolean): String +public inline fun String.takeLastWhile(predicate: (Char) -> Boolean): String { + for (index in lastIndex downTo 0) { + if (!predicate(this[index])) { + return substring(index + 1) + } + } + return this +} /** * Returns a subsequence of this char sequence containing the first characters that satisfy the given [predicate]. */ -public expect inline fun CharSequence.takeWhile(predicate: (Char) -> Boolean): CharSequence +public inline fun CharSequence.takeWhile(predicate: (Char) -> Boolean): CharSequence { + for (index in 0 until length) + if (!predicate(get(index))) { + return subSequence(0, index) + } + return subSequence(0, length) +} /** * Returns a string containing the first characters that satisfy the given [predicate]. */ -public expect inline fun String.takeWhile(predicate: (Char) -> Boolean): String +public inline fun String.takeWhile(predicate: (Char) -> Boolean): String { + for (index in 0 until length) + if (!predicate(get(index))) { + return substring(0, index) + } + return this +} /** * Returns a char sequence with characters in reversed order. */ -public expect fun CharSequence.reversed(): CharSequence +public fun CharSequence.reversed(): CharSequence { + return StringBuilder(this).reverse() +} /** * Returns a string with characters in reversed order. */ @kotlin.internal.InlineOnly -public expect inline fun String.reversed(): String +public inline fun String.reversed(): String { + return (this as CharSequence).reversed().toString() +} /** * Returns a [Map] containing key-value pairs provided by [transform] function @@ -298,7 +507,10 @@ public expect inline fun String.reversed(): String * * The returned map preserves the entry iteration order of the original char sequence. */ -public expect inline fun CharSequence.associate(transform: (Char) -> Pair): Map +public inline fun CharSequence.associate(transform: (Char) -> Pair): Map { + val capacity = mapCapacity(length).coerceAtLeast(16) + return associateTo(LinkedHashMap(capacity), transform) +} /** * Returns a [Map] containing the characters from the given char sequence indexed by the key @@ -308,7 +520,10 @@ public expect inline fun CharSequence.associate(transform: (Char) -> Pair * * The returned map preserves the entry iteration order of the original char sequence. */ -public expect inline fun CharSequence.associateBy(keySelector: (Char) -> K): Map +public inline fun CharSequence.associateBy(keySelector: (Char) -> K): Map { + val capacity = mapCapacity(length).coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector) +} /** * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to characters of the given char sequence. @@ -317,7 +532,10 @@ public expect inline fun CharSequence.associateBy(keySelector: (Char) -> K): * * The returned map preserves the entry iteration order of the original char sequence. */ -public expect inline fun CharSequence.associateBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map +public inline fun CharSequence.associateBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map { + val capacity = mapCapacity(length).coerceAtLeast(16) + return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) +} /** * Populates and returns the [destination] mutable map with key-value pairs, @@ -326,7 +544,12 @@ public expect inline fun CharSequence.associateBy(keySelector: (Char) -> * * If any two characters would have the same key returned by [keySelector] the last one gets added to the map. */ -public expect inline fun > CharSequence.associateByTo(destination: M, keySelector: (Char) -> K): M +public inline fun > CharSequence.associateByTo(destination: M, keySelector: (Char) -> K): M { + for (element in this) { + destination.put(keySelector(element), element) + } + return destination +} /** * Populates and returns the [destination] mutable map with key-value pairs, @@ -335,7 +558,12 @@ public expect inline fun > CharSequence.associa * * If any two characters would have the same key returned by [keySelector] the last one gets added to the map. */ -public expect inline fun > CharSequence.associateByTo(destination: M, keySelector: (Char) -> K, valueTransform: (Char) -> V): M +public inline fun > CharSequence.associateByTo(destination: M, keySelector: (Char) -> K, valueTransform: (Char) -> V): M { + for (element in this) { + destination.put(keySelector(element), valueTransform(element)) + } + return destination +} /** * Populates and returns the [destination] mutable map with key-value pairs @@ -343,44 +571,78 @@ public expect inline fun > CharSequence.associa * * If any of two pairs would have the same key the last one gets added to the map. */ -public expect inline fun > CharSequence.associateTo(destination: M, transform: (Char) -> Pair): M +public inline fun > CharSequence.associateTo(destination: M, transform: (Char) -> Pair): M { + for (element in this) { + destination += transform(element) + } + return destination +} /** * Appends all characters to the given [destination] collection. */ -public expect fun > CharSequence.toCollection(destination: C): C +public fun > CharSequence.toCollection(destination: C): C { + for (item in this) { + destination.add(item) + } + return destination +} /** * Returns a [HashSet] of all characters. */ -public expect fun CharSequence.toHashSet(): HashSet +public fun CharSequence.toHashSet(): HashSet { + return toCollection(HashSet(mapCapacity(length))) +} /** * Returns a [List] containing all characters. */ -public expect fun CharSequence.toList(): List +public fun CharSequence.toList(): List { + return when (length) { + 0 -> emptyList() + 1 -> listOf(this[0]) + else -> this.toMutableList() + } +} /** * Returns a [MutableList] filled with all characters of this char sequence. */ -public expect fun CharSequence.toMutableList(): MutableList +public fun CharSequence.toMutableList(): MutableList { + return toCollection(ArrayList(length)) +} /** * Returns a [Set] of all characters. * * The returned set preserves the element iteration order of the original char sequence. */ -public expect fun CharSequence.toSet(): Set +public fun CharSequence.toSet(): Set { + return when (length) { + 0 -> emptySet() + 1 -> setOf(this[0]) + else -> toCollection(LinkedHashSet(mapCapacity(length))) + } +} /** * Returns a single list of all elements yielded from results of [transform] function being invoked on each character of original char sequence. */ -public expect inline fun CharSequence.flatMap(transform: (Char) -> Iterable): List +public inline fun CharSequence.flatMap(transform: (Char) -> Iterable): List { + return flatMapTo(ArrayList(), transform) +} /** * Appends all elements yielded from results of [transform] function being invoked on each character of original char sequence, to the given [destination]. */ -public expect inline fun > CharSequence.flatMapTo(destination: C, transform: (Char) -> Iterable): C +public inline fun > CharSequence.flatMapTo(destination: C, transform: (Char) -> Iterable): C { + for (element in this) { + val list = transform(element) + destination.addAll(list) + } + return destination +} /** * Groups characters of the original char sequence by the key returned by the given [keySelector] function @@ -390,7 +652,9 @@ public expect inline fun > CharSequence.flatMapTo * * @sample samples.collections.Collections.Transformations.groupBy */ -public expect inline fun CharSequence.groupBy(keySelector: (Char) -> K): Map> +public inline fun CharSequence.groupBy(keySelector: (Char) -> K): Map> { + return groupByTo(LinkedHashMap>(), keySelector) +} /** * Groups values returned by the [valueTransform] function applied to each character of the original char sequence @@ -401,7 +665,9 @@ public expect inline fun CharSequence.groupBy(keySelector: (Char) -> K): Map * * @sample samples.collections.Collections.Transformations.groupByKeysAndValues */ -public expect inline fun CharSequence.groupBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map> +public inline fun CharSequence.groupBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map> { + return groupByTo(LinkedHashMap>(), keySelector, valueTransform) +} /** * Groups characters of the original char sequence by the key returned by the given [keySelector] function @@ -411,7 +677,14 @@ public expect inline fun CharSequence.groupBy(keySelector: (Char) -> K, v * * @sample samples.collections.Collections.Transformations.groupBy */ -public expect inline fun >> CharSequence.groupByTo(destination: M, keySelector: (Char) -> K): M +public inline fun >> CharSequence.groupByTo(destination: M, keySelector: (Char) -> K): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(element) + } + return destination +} /** * Groups values returned by the [valueTransform] function applied to each character of the original char sequence @@ -422,7 +695,14 @@ public expect inline fun >> CharSequen * * @sample samples.collections.Collections.Transformations.groupByKeysAndValues */ -public expect inline fun >> CharSequence.groupByTo(destination: M, keySelector: (Char) -> K, valueTransform: (Char) -> V): M +public inline fun >> CharSequence.groupByTo(destination: M, keySelector: (Char) -> K, valueTransform: (Char) -> V): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(valueTransform(element)) + } + return destination +} /** * Creates a [Grouping] source from a char sequence to be used later with one of group-and-fold operations @@ -431,13 +711,20 @@ public expect inline fun >> CharSequen * @sample samples.collections.Collections.Transformations.groupingByEachCount */ @SinceKotlin("1.1") -public expect inline fun CharSequence.groupingBy(crossinline keySelector: (Char) -> K): Grouping +public inline fun CharSequence.groupingBy(crossinline keySelector: (Char) -> K): Grouping { + return object : Grouping { + override fun sourceIterator(): Iterator = this@groupingBy.iterator() + override fun keyOf(element: Char): K = keySelector(element) + } +} /** * Returns a list containing the results of applying the given [transform] function * to each character in the original char sequence. */ -public expect inline fun CharSequence.map(transform: (Char) -> R): List +public inline fun CharSequence.map(transform: (Char) -> R): List { + return mapTo(ArrayList(length), transform) +} /** * Returns a list containing the results of applying the given [transform] function @@ -445,7 +732,9 @@ public expect inline fun CharSequence.map(transform: (Char) -> R): List * @param [transform] function that takes the index of a character and the character itself * and returns the result of the transform applied to the character. */ -public expect inline fun CharSequence.mapIndexed(transform: (index: Int, Char) -> R): List +public inline fun CharSequence.mapIndexed(transform: (index: Int, Char) -> R): List { + return mapIndexedTo(ArrayList(length), transform) +} /** * Returns a list containing only the non-null results of applying the given [transform] function @@ -453,7 +742,9 @@ public expect inline fun CharSequence.mapIndexed(transform: (index: Int, Cha * @param [transform] function that takes the index of a character and the character itself * and returns the result of the transform applied to the character. */ -public expect inline fun CharSequence.mapIndexedNotNull(transform: (index: Int, Char) -> R?): List +public inline fun CharSequence.mapIndexedNotNull(transform: (index: Int, Char) -> R?): List { + return mapIndexedNotNullTo(ArrayList(), transform) +} /** * Applies the given [transform] function to each character and its index in the original char sequence @@ -461,7 +752,10 @@ public expect inline fun CharSequence.mapIndexedNotNull(transform: (in * @param [transform] function that takes the index of a character and the character itself * and returns the result of the transform applied to the character. */ -public expect inline fun > CharSequence.mapIndexedNotNullTo(destination: C, transform: (index: Int, Char) -> R?): C +public inline fun > CharSequence.mapIndexedNotNullTo(destination: C, transform: (index: Int, Char) -> R?): C { + forEachIndexed { index, element -> transform(index, element)?.let { destination.add(it) } } + return destination +} /** * Applies the given [transform] function to each character and its index in the original char sequence @@ -469,67 +763,101 @@ public expect inline fun > CharSequence.map * @param [transform] function that takes the index of a character and the character itself * and returns the result of the transform applied to the character. */ -public expect inline fun > CharSequence.mapIndexedTo(destination: C, transform: (index: Int, Char) -> R): C +public inline fun > CharSequence.mapIndexedTo(destination: C, transform: (index: Int, Char) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} /** * Returns a list containing only the non-null results of applying the given [transform] function * to each character in the original char sequence. */ -public expect inline fun CharSequence.mapNotNull(transform: (Char) -> R?): List +public inline fun CharSequence.mapNotNull(transform: (Char) -> R?): List { + return mapNotNullTo(ArrayList(), transform) +} /** * Applies the given [transform] function to each character in the original char sequence * and appends only the non-null results to the given [destination]. */ -public expect inline fun > CharSequence.mapNotNullTo(destination: C, transform: (Char) -> R?): C +public inline fun > CharSequence.mapNotNullTo(destination: C, transform: (Char) -> R?): C { + forEach { element -> transform(element)?.let { destination.add(it) } } + return destination +} /** * Applies the given [transform] function to each character of the original char sequence * and appends the results to the given [destination]. */ -public expect inline fun > CharSequence.mapTo(destination: C, transform: (Char) -> R): C +public inline fun > CharSequence.mapTo(destination: C, transform: (Char) -> R): C { + for (item in this) + destination.add(transform(item)) + return destination +} /** * Returns a lazy [Iterable] of [IndexedValue] for each character of the original char sequence. */ -public expect fun CharSequence.withIndex(): Iterable> +public fun CharSequence.withIndex(): Iterable> { + return IndexingIterable { iterator() } +} /** * Returns `true` if all characters match the given [predicate]. * * @sample samples.collections.Collections.Aggregates.all */ -public expect inline fun CharSequence.all(predicate: (Char) -> Boolean): Boolean +public inline fun CharSequence.all(predicate: (Char) -> Boolean): Boolean { + for (element in this) if (!predicate(element)) return false + return true +} /** * Returns `true` if char sequence has at least one character. * * @sample samples.collections.Collections.Aggregates.any */ -public expect fun CharSequence.any(): Boolean +public fun CharSequence.any(): Boolean { + return !isEmpty() +} /** * Returns `true` if at least one character matches the given [predicate]. * * @sample samples.collections.Collections.Aggregates.anyWithPredicate */ -public expect inline fun CharSequence.any(predicate: (Char) -> Boolean): Boolean +public inline fun CharSequence.any(predicate: (Char) -> Boolean): Boolean { + for (element in this) if (predicate(element)) return true + return false +} /** * Returns the length of this char sequence. */ @kotlin.internal.InlineOnly -public expect inline fun CharSequence.count(): Int +public inline fun CharSequence.count(): Int { + return length +} /** * Returns the number of characters matching the given [predicate]. */ -public expect inline fun CharSequence.count(predicate: (Char) -> Boolean): Int +public inline fun CharSequence.count(predicate: (Char) -> Boolean): Int { + var count = 0 + for (element in this) if (predicate(element)) count++ + return count +} /** * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each character. */ -public expect inline fun CharSequence.fold(initial: R, operation: (acc: R, Char) -> R): R +public inline fun CharSequence.fold(initial: R, operation: (acc: R, Char) -> R): R { + var accumulator = initial + for (element in this) accumulator = operation(accumulator, element) + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from left to right @@ -537,12 +865,24 @@ public expect inline fun CharSequence.fold(initial: R, operation: (acc: R, C * @param [operation] function that takes the index of a character, current accumulator value * and the character itself, and calculates the next accumulator value. */ -public expect inline fun CharSequence.foldIndexed(initial: R, operation: (index: Int, acc: R, Char) -> R): R +public inline fun CharSequence.foldIndexed(initial: R, operation: (index: Int, acc: R, Char) -> R): R { + var index = 0 + var accumulator = initial + for (element in this) accumulator = operation(index++, accumulator, element) + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from right to left to each character and current accumulator value. */ -public expect inline fun CharSequence.foldRight(initial: R, operation: (Char, acc: R) -> R): R +public inline fun CharSequence.foldRight(initial: R, operation: (Char, acc: R) -> R): R { + var index = lastIndex + var accumulator = initial + while (index >= 0) { + accumulator = operation(get(index--), accumulator) + } + return accumulator +} /** * Accumulates value starting with [initial] value and applying [operation] from right to left @@ -550,74 +890,160 @@ public expect inline fun CharSequence.foldRight(initial: R, operation: (Char * @param [operation] function that takes the index of a character, the character itself * and current accumulator value, and calculates the next accumulator value. */ -public expect inline fun CharSequence.foldRightIndexed(initial: R, operation: (index: Int, Char, acc: R) -> R): R +public inline fun CharSequence.foldRightIndexed(initial: R, operation: (index: Int, Char, acc: R) -> R): R { + var index = lastIndex + var accumulator = initial + while (index >= 0) { + accumulator = operation(index, get(index), accumulator) + --index + } + return accumulator +} /** * Performs the given [action] on each character. */ -public expect inline fun CharSequence.forEach(action: (Char) -> Unit): Unit +public inline fun CharSequence.forEach(action: (Char) -> Unit): Unit { + for (element in this) action(element) +} /** * Performs the given [action] on each character, providing sequential index with the character. * @param [action] function that takes the index of a character and the character itself * and performs the desired action on the character. */ -public expect inline fun CharSequence.forEachIndexed(action: (index: Int, Char) -> Unit): Unit +public inline fun CharSequence.forEachIndexed(action: (index: Int, Char) -> Unit): Unit { + var index = 0 + for (item in this) action(index++, item) +} /** * Returns the largest character or `null` if there are no characters. */ -public expect fun CharSequence.max(): Char? +public fun CharSequence.max(): Char? { + if (isEmpty()) return null + var max = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (max < e) max = e + } + return max +} /** * Returns the first character yielding the largest value of the given function or `null` if there are no characters. */ -public expect inline fun > CharSequence.maxBy(selector: (Char) -> R): Char? +public inline fun > CharSequence.maxBy(selector: (Char) -> R): Char? { + if (isEmpty()) return null + var maxElem = this[0] + var maxValue = selector(maxElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = selector(e) + if (maxValue < v) { + maxElem = e + maxValue = v + } + } + return maxElem +} /** * Returns the first character having the largest value according to the provided [comparator] or `null` if there are no characters. */ -public expect fun CharSequence.maxWith(comparator: Comparator): Char? +public fun CharSequence.maxWith(comparator: Comparator): Char? { + if (isEmpty()) return null + var max = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(max, e) < 0) max = e + } + return max +} /** * Returns the smallest character or `null` if there are no characters. */ -public expect fun CharSequence.min(): Char? +public fun CharSequence.min(): Char? { + if (isEmpty()) return null + var min = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (min > e) min = e + } + return min +} /** * Returns the first character yielding the smallest value of the given function or `null` if there are no characters. */ -public expect inline fun > CharSequence.minBy(selector: (Char) -> R): Char? +public inline fun > CharSequence.minBy(selector: (Char) -> R): Char? { + if (isEmpty()) return null + var minElem = this[0] + var minValue = selector(minElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = selector(e) + if (minValue > v) { + minElem = e + minValue = v + } + } + return minElem +} /** * Returns the first character having the smallest value according to the provided [comparator] or `null` if there are no characters. */ -public expect fun CharSequence.minWith(comparator: Comparator): Char? +public fun CharSequence.minWith(comparator: Comparator): Char? { + if (isEmpty()) return null + var min = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(min, e) > 0) min = e + } + return min +} /** * Returns `true` if the char sequence has no characters. * * @sample samples.collections.Collections.Aggregates.none */ -public expect fun CharSequence.none(): Boolean +public fun CharSequence.none(): Boolean { + return isEmpty() +} /** * Returns `true` if no characters match the given [predicate]. * * @sample samples.collections.Collections.Aggregates.noneWithPredicate */ -public expect inline fun CharSequence.none(predicate: (Char) -> Boolean): Boolean +public inline fun CharSequence.none(predicate: (Char) -> Boolean): Boolean { + for (element in this) if (predicate(element)) return false + return true +} /** * Performs the given [action] on each character and returns the char sequence itself afterwards. */ @SinceKotlin("1.1") -public expect inline fun S.onEach(action: (Char) -> Unit): S +public inline fun S.onEach(action: (Char) -> Unit): S { + return apply { for (element in this) action(element) } +} /** * Accumulates value starting with the first character and applying [operation] from left to right to current accumulator value and each character. */ -public expect inline fun CharSequence.reduce(operation: (acc: Char, Char) -> Char): Char +public inline fun CharSequence.reduce(operation: (acc: Char, Char) -> Char): Char { + if (isEmpty()) + throw UnsupportedOperationException("Empty char sequence can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(accumulator, this[index]) + } + return accumulator +} /** * Accumulates value starting with the first character and applying [operation] from left to right @@ -625,12 +1051,28 @@ public expect inline fun CharSequence.reduce(operation: (acc: Char, Char) -> Cha * @param [operation] function that takes the index of a character, current accumulator value * and the character itself and calculates the next accumulator value. */ -public expect inline fun CharSequence.reduceIndexed(operation: (index: Int, acc: Char, Char) -> Char): Char +public inline fun CharSequence.reduceIndexed(operation: (index: Int, acc: Char, Char) -> Char): Char { + if (isEmpty()) + throw UnsupportedOperationException("Empty char sequence can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(index, accumulator, this[index]) + } + return accumulator +} /** * Accumulates value starting with last character and applying [operation] from right to left to each character and current accumulator value. */ -public expect inline fun CharSequence.reduceRight(operation: (Char, acc: Char) -> Char): Char +public inline fun CharSequence.reduceRight(operation: (Char, acc: Char) -> Char): Char { + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty char sequence can't be reduced.") + var accumulator = get(index--) + while (index >= 0) { + accumulator = operation(get(index--), accumulator) + } + return accumulator +} /** * Accumulates value starting with last character and applying [operation] from right to left @@ -638,17 +1080,38 @@ public expect inline fun CharSequence.reduceRight(operation: (Char, acc: Char) - * @param [operation] function that takes the index of a character, the character itself * and current accumulator value, and calculates the next accumulator value. */ -public expect inline fun CharSequence.reduceRightIndexed(operation: (index: Int, Char, acc: Char) -> Char): Char +public inline fun CharSequence.reduceRightIndexed(operation: (index: Int, Char, acc: Char) -> Char): Char { + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty char sequence can't be reduced.") + var accumulator = get(index--) + while (index >= 0) { + accumulator = operation(index, get(index), accumulator) + --index + } + return accumulator +} /** * Returns the sum of all values produced by [selector] function applied to each character in the char sequence. */ -public expect inline fun CharSequence.sumBy(selector: (Char) -> Int): Int +public inline fun CharSequence.sumBy(selector: (Char) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += selector(element) + } + return sum +} /** * Returns the sum of all values produced by [selector] function applied to each character in the char sequence. */ -public expect inline fun CharSequence.sumByDouble(selector: (Char) -> Double): Double +public inline fun CharSequence.sumByDouble(selector: (Char) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += selector(element) + } + return sum +} /** * Splits this char sequence into a list of strings each not exceeding the given [size]. @@ -660,7 +1123,9 @@ public expect inline fun CharSequence.sumByDouble(selector: (Char) -> Double): D * @sample samples.collections.Collections.Transformations.chunked */ @SinceKotlin("1.2") -public expect fun CharSequence.chunked(size: Int): List +public fun CharSequence.chunked(size: Int): List { + return windowed(size, size, partialWindows = true) +} /** * Splits this char sequence into several char sequences each not exceeding the given [size] @@ -677,7 +1142,9 @@ public expect fun CharSequence.chunked(size: Int): List * @sample samples.text.Strings.chunkedTransform */ @SinceKotlin("1.2") -public expect fun CharSequence.chunked(size: Int, transform: (CharSequence) -> R): List +public fun CharSequence.chunked(size: Int, transform: (CharSequence) -> R): List { + return windowed(size, size, partialWindows = true, transform = transform) +} /** * Splits this char sequence into a sequence of strings each not exceeding the given [size]. @@ -689,7 +1156,9 @@ public expect fun CharSequence.chunked(size: Int, transform: (CharSequence) * @sample samples.collections.Collections.Transformations.chunked */ @SinceKotlin("1.2") -public expect fun CharSequence.chunkedSequence(size: Int): Sequence +public fun CharSequence.chunkedSequence(size: Int): Sequence { + return chunkedSequence(size) { it.toString() } +} /** * Splits this char sequence into several char sequences each not exceeding the given [size] @@ -706,21 +1175,45 @@ public expect fun CharSequence.chunkedSequence(size: Int): Sequence * @sample samples.text.Strings.chunkedTransformToSequence */ @SinceKotlin("1.2") -public expect fun CharSequence.chunkedSequence(size: Int, transform: (CharSequence) -> R): Sequence +public fun CharSequence.chunkedSequence(size: Int, transform: (CharSequence) -> R): Sequence { + return windowedSequence(size, size, partialWindows = true, transform = transform) +} /** * Splits the original char sequence into pair of char sequences, * where *first* char sequence contains characters for which [predicate] yielded `true`, * while *second* char sequence contains characters for which [predicate] yielded `false`. */ -public expect inline fun CharSequence.partition(predicate: (Char) -> Boolean): Pair +public inline fun CharSequence.partition(predicate: (Char) -> Boolean): Pair { + val first = StringBuilder() + val second = StringBuilder() + for (element in this) { + if (predicate(element)) { + first.append(element) + } else { + second.append(element) + } + } + return Pair(first, second) +} /** * Splits the original string into pair of strings, * where *first* string contains characters for which [predicate] yielded `true`, * while *second* string contains characters for which [predicate] yielded `false`. */ -public expect inline fun String.partition(predicate: (Char) -> Boolean): Pair +public inline fun String.partition(predicate: (Char) -> Boolean): Pair { + val first = StringBuilder() + val second = StringBuilder() + for (element in this) { + if (predicate(element)) { + first.append(element) + } else { + second.append(element) + } + } + return Pair(first.toString(), second.toString()) +} /** * Returns a list of snapshots of the window of the given [size] @@ -821,12 +1314,21 @@ public fun CharSequence.windowedSequence(size: Int, step: Int = 1, partialWi /** * Returns a list of pairs built from characters of both char sequences with same indexes. List has length of shortest char sequence. */ -public expect infix fun CharSequence.zip(other: CharSequence): List> +public infix fun CharSequence.zip(other: CharSequence): List> { + return zip(other) { c1, c2 -> c1 to c2 } +} /** * Returns a list of values built from characters of both char sequences with same indexes using provided [transform]. List has length of shortest char sequence. */ -public expect inline fun CharSequence.zip(other: CharSequence, transform: (a: Char, b: Char) -> V): List +public inline fun CharSequence.zip(other: CharSequence, transform: (a: Char, b: Char) -> V): List { + val length = minOf(this.length, other.length) + val list = ArrayList(length) + for (i in 0 until length) { + list.add(transform(this[i], other[i])) + } + return list +} /** * Returns a list of pairs of each two adjacent characters in this char sequence. @@ -836,7 +1338,9 @@ public expect inline fun CharSequence.zip(other: CharSequence, transform: (a * @sample samples.collections.Collections.Transformations.zipWithNext */ @SinceKotlin("1.2") -public expect fun CharSequence.zipWithNext(): List> +public fun CharSequence.zipWithNext(): List> { + return zipWithNext { a, b -> a to b } +} /** * Returns a list containing the results of applying the given [transform] function @@ -847,15 +1351,29 @@ public expect fun CharSequence.zipWithNext(): List> * @sample samples.collections.Collections.Transformations.zipWithNextToFindDeltas */ @SinceKotlin("1.2") -public expect inline fun CharSequence.zipWithNext(transform: (a: Char, b: Char) -> R): List +public inline fun CharSequence.zipWithNext(transform: (a: Char, b: Char) -> R): List { + val size = length - 1 + if (size < 1) return emptyList() + val result = ArrayList(size) + for (index in 0 until size) { + result.add(transform(this[index], this[index + 1])) + } + return result +} /** * Creates an [Iterable] instance that wraps the original char sequence returning its characters when being iterated. */ -public expect fun CharSequence.asIterable(): Iterable +public fun CharSequence.asIterable(): Iterable { + if (this is String && isEmpty()) return emptyList() + return Iterable { this.iterator() } +} /** * Creates a [Sequence] instance that wraps the original char sequence returning its characters when being iterated. */ -public expect fun CharSequence.asSequence(): Sequence +public fun CharSequence.asSequence(): Sequence { + if (this is String && isEmpty()) return emptySequence() + return Sequence { this.iterator() } +} diff --git a/libraries/stdlib/js/src/generated/_ArraysJs.kt b/libraries/stdlib/js/src/generated/_ArraysJs.kt new file mode 100644 index 00000000000..694da1d0a47 --- /dev/null +++ b/libraries/stdlib/js/src/generated/_ArraysJs.kt @@ -0,0 +1,1022 @@ +@file:kotlin.jvm.JvmMultifileClass +@file:kotlin.jvm.JvmName("ArraysKt") + +package kotlin.collections + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// + +import kotlin.js.* +import primitiveArrayConcat +import withType +import kotlin.* +import kotlin.text.* +import kotlin.comparisons.* + +/** + * Returns a [List] that wraps the original array. + */ +public actual fun Array.asList(): List { + return ArrayList(this.unsafeCast>()) +} + +/** + * Returns a [List] that wraps the original array. + */ +@kotlin.internal.InlineOnly +public actual inline fun ByteArray.asList(): List { + return this.unsafeCast>().asList() +} + +/** + * Returns a [List] that wraps the original array. + */ +@kotlin.internal.InlineOnly +public actual inline fun ShortArray.asList(): List { + return this.unsafeCast>().asList() +} + +/** + * Returns a [List] that wraps the original array. + */ +@kotlin.internal.InlineOnly +public actual inline fun IntArray.asList(): List { + return this.unsafeCast>().asList() +} + +/** + * Returns a [List] that wraps the original array. + */ +@kotlin.internal.InlineOnly +public actual inline fun LongArray.asList(): List { + return this.unsafeCast>().asList() +} + +/** + * Returns a [List] that wraps the original array. + */ +@kotlin.internal.InlineOnly +public actual inline fun FloatArray.asList(): List { + return this.unsafeCast>().asList() +} + +/** + * Returns a [List] that wraps the original array. + */ +@kotlin.internal.InlineOnly +public actual inline fun DoubleArray.asList(): List { + return this.unsafeCast>().asList() +} + +/** + * Returns a [List] that wraps the original array. + */ +@kotlin.internal.InlineOnly +public actual inline fun BooleanArray.asList(): List { + return this.unsafeCast>().asList() +} + +/** + * Returns a [List] that wraps the original array. + */ +public actual 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) + } +} + +/** + * Returns `true` if the two specified arrays are *deeply* equal to one another, + * i.e. contain the same number of the same elements in the same order. + * + * If two corresponding elements are nested arrays, they are also compared deeply. + * If any of arrays contains itself on any nesting level the behavior is undefined. + */ +@SinceKotlin("1.1") +@library("arrayDeepEquals") +@Suppress("UNUSED_PARAMETER") +public actual infix fun Array.contentDeepEquals(other: Array): Boolean { + definedExternally +} + +/** + * Returns a hash code based on the contents of this array as if it is [List]. + * Nested arrays are treated as lists too. + * + * If any of arrays contains itself on any nesting level the behavior is undefined. + */ +@SinceKotlin("1.1") +@library("arrayDeepHashCode") +public actual fun Array.contentDeepHashCode(): Int { + definedExternally +} + +/** + * Returns a string representation of the contents of this array as if it is a [List]. + * Nested arrays are treated as lists too. + * + * If any of arrays contains itself on any nesting level that reference + * is rendered as `"[...]"` to prevent recursion. + * + * @sample samples.collections.Arrays.ContentOperations.contentDeepToString + */ +@SinceKotlin("1.1") +@library("arrayDeepToString") +public actual fun Array.contentDeepToString(): String { + definedExternally +} + +/** + * Returns `true` if the two specified arrays are *structurally* equal to one another, + * i.e. contain the same number of the same elements in the same order. + */ +@SinceKotlin("1.1") +@library("arrayEquals") +@Suppress("UNUSED_PARAMETER") +public actual infix fun Array.contentEquals(other: Array): Boolean { + definedExternally +} + +/** + * Returns `true` if the two specified arrays are *structurally* equal to one another, + * i.e. contain the same number of the same elements in the same order. + */ +@SinceKotlin("1.1") +@library("arrayEquals") +@Suppress("UNUSED_PARAMETER") +public actual infix fun ByteArray.contentEquals(other: ByteArray): Boolean { + definedExternally +} + +/** + * Returns `true` if the two specified arrays are *structurally* equal to one another, + * i.e. contain the same number of the same elements in the same order. + */ +@SinceKotlin("1.1") +@library("arrayEquals") +@Suppress("UNUSED_PARAMETER") +public actual infix fun ShortArray.contentEquals(other: ShortArray): Boolean { + definedExternally +} + +/** + * Returns `true` if the two specified arrays are *structurally* equal to one another, + * i.e. contain the same number of the same elements in the same order. + */ +@SinceKotlin("1.1") +@library("arrayEquals") +@Suppress("UNUSED_PARAMETER") +public actual infix fun IntArray.contentEquals(other: IntArray): Boolean { + definedExternally +} + +/** + * Returns `true` if the two specified arrays are *structurally* equal to one another, + * i.e. contain the same number of the same elements in the same order. + */ +@SinceKotlin("1.1") +@library("arrayEquals") +@Suppress("UNUSED_PARAMETER") +public actual infix fun LongArray.contentEquals(other: LongArray): Boolean { + definedExternally +} + +/** + * Returns `true` if the two specified arrays are *structurally* equal to one another, + * i.e. contain the same number of the same elements in the same order. + */ +@SinceKotlin("1.1") +@library("arrayEquals") +@Suppress("UNUSED_PARAMETER") +public actual infix fun FloatArray.contentEquals(other: FloatArray): Boolean { + definedExternally +} + +/** + * Returns `true` if the two specified arrays are *structurally* equal to one another, + * i.e. contain the same number of the same elements in the same order. + */ +@SinceKotlin("1.1") +@library("arrayEquals") +@Suppress("UNUSED_PARAMETER") +public actual infix fun DoubleArray.contentEquals(other: DoubleArray): Boolean { + definedExternally +} + +/** + * Returns `true` if the two specified arrays are *structurally* equal to one another, + * i.e. contain the same number of the same elements in the same order. + */ +@SinceKotlin("1.1") +@library("arrayEquals") +@Suppress("UNUSED_PARAMETER") +public actual infix fun BooleanArray.contentEquals(other: BooleanArray): Boolean { + definedExternally +} + +/** + * Returns `true` if the two specified arrays are *structurally* equal to one another, + * i.e. contain the same number of the same elements in the same order. + */ +@SinceKotlin("1.1") +@library("arrayEquals") +@Suppress("UNUSED_PARAMETER") +public actual infix fun CharArray.contentEquals(other: CharArray): Boolean { + definedExternally +} + +/** + * Returns a hash code based on the contents of this array as if it is [List]. + */ +@SinceKotlin("1.1") +@library("arrayHashCode") +public actual fun Array.contentHashCode(): Int { + definedExternally +} + +/** + * Returns a hash code based on the contents of this array as if it is [List]. + */ +@SinceKotlin("1.1") +@library("arrayHashCode") +public actual fun ByteArray.contentHashCode(): Int { + definedExternally +} + +/** + * Returns a hash code based on the contents of this array as if it is [List]. + */ +@SinceKotlin("1.1") +@library("arrayHashCode") +public actual fun ShortArray.contentHashCode(): Int { + definedExternally +} + +/** + * Returns a hash code based on the contents of this array as if it is [List]. + */ +@SinceKotlin("1.1") +@library("arrayHashCode") +public actual fun IntArray.contentHashCode(): Int { + definedExternally +} + +/** + * Returns a hash code based on the contents of this array as if it is [List]. + */ +@SinceKotlin("1.1") +@library("arrayHashCode") +public actual fun LongArray.contentHashCode(): Int { + definedExternally +} + +/** + * Returns a hash code based on the contents of this array as if it is [List]. + */ +@SinceKotlin("1.1") +@library("arrayHashCode") +public actual fun FloatArray.contentHashCode(): Int { + definedExternally +} + +/** + * Returns a hash code based on the contents of this array as if it is [List]. + */ +@SinceKotlin("1.1") +@library("arrayHashCode") +public actual fun DoubleArray.contentHashCode(): Int { + definedExternally +} + +/** + * Returns a hash code based on the contents of this array as if it is [List]. + */ +@SinceKotlin("1.1") +@library("arrayHashCode") +public actual fun BooleanArray.contentHashCode(): Int { + definedExternally +} + +/** + * Returns a hash code based on the contents of this array as if it is [List]. + */ +@SinceKotlin("1.1") +@library("arrayHashCode") +public actual fun CharArray.contentHashCode(): Int { + definedExternally +} + +/** + * Returns a string representation of the contents of the specified array as if it is [List]. + * + * @sample samples.collections.Arrays.ContentOperations.contentToString + */ +@SinceKotlin("1.1") +@library("arrayToString") +public actual fun Array.contentToString(): String { + definedExternally +} + +/** + * Returns a string representation of the contents of the specified array as if it is [List]. + * + * @sample samples.collections.Arrays.ContentOperations.contentToString + */ +@SinceKotlin("1.1") +@library("arrayToString") +public actual fun ByteArray.contentToString(): String { + definedExternally +} + +/** + * Returns a string representation of the contents of the specified array as if it is [List]. + * + * @sample samples.collections.Arrays.ContentOperations.contentToString + */ +@SinceKotlin("1.1") +@library("arrayToString") +public actual fun ShortArray.contentToString(): String { + definedExternally +} + +/** + * Returns a string representation of the contents of the specified array as if it is [List]. + * + * @sample samples.collections.Arrays.ContentOperations.contentToString + */ +@SinceKotlin("1.1") +@library("arrayToString") +public actual fun IntArray.contentToString(): String { + definedExternally +} + +/** + * Returns a string representation of the contents of the specified array as if it is [List]. + * + * @sample samples.collections.Arrays.ContentOperations.contentToString + */ +@SinceKotlin("1.1") +@library("arrayToString") +public actual fun LongArray.contentToString(): String { + definedExternally +} + +/** + * Returns a string representation of the contents of the specified array as if it is [List]. + * + * @sample samples.collections.Arrays.ContentOperations.contentToString + */ +@SinceKotlin("1.1") +@library("arrayToString") +public actual fun FloatArray.contentToString(): String { + definedExternally +} + +/** + * Returns a string representation of the contents of the specified array as if it is [List]. + * + * @sample samples.collections.Arrays.ContentOperations.contentToString + */ +@SinceKotlin("1.1") +@library("arrayToString") +public actual fun DoubleArray.contentToString(): String { + definedExternally +} + +/** + * Returns a string representation of the contents of the specified array as if it is [List]. + * + * @sample samples.collections.Arrays.ContentOperations.contentToString + */ +@SinceKotlin("1.1") +@library("arrayToString") +public actual fun BooleanArray.contentToString(): String { + definedExternally +} + +/** + * Returns a string representation of the contents of the specified array as if it is [List]. + * + * @sample samples.collections.Arrays.ContentOperations.contentToString + */ +@SinceKotlin("1.1") +@library("arrayToString") +public actual fun CharArray.contentToString(): String { + definedExternally +} + +/** + * Returns new array which is a copy of the original array. + */ +@Suppress("NOTHING_TO_INLINE") +public actual inline fun Array.copyOf(): Array { + return this.asDynamic().slice() +} + +/** + * Returns new array which is a copy of the original array. + */ +@Suppress("NOTHING_TO_INLINE") +public actual inline fun ByteArray.copyOf(): ByteArray { + return this.asDynamic().slice() +} + +/** + * Returns new array which is a copy of the original array. + */ +@Suppress("NOTHING_TO_INLINE") +public actual inline fun ShortArray.copyOf(): ShortArray { + return this.asDynamic().slice() +} + +/** + * Returns new array which is a copy of the original array. + */ +@Suppress("NOTHING_TO_INLINE") +public actual inline fun IntArray.copyOf(): IntArray { + return this.asDynamic().slice() +} + +/** + * Returns new array which is a copy of the original array. + */ +public actual fun LongArray.copyOf(): LongArray { + return withType("LongArray", this.asDynamic().slice()) +} + +/** + * Returns new array which is a copy of the original array. + */ +@Suppress("NOTHING_TO_INLINE") +public actual inline fun FloatArray.copyOf(): FloatArray { + return this.asDynamic().slice() +} + +/** + * Returns new array which is a copy of the original array. + */ +@Suppress("NOTHING_TO_INLINE") +public actual inline fun DoubleArray.copyOf(): DoubleArray { + return this.asDynamic().slice() +} + +/** + * Returns new array which is a copy of the original array. + */ +public actual fun BooleanArray.copyOf(): BooleanArray { + return withType("BooleanArray", this.asDynamic().slice()) +} + +/** + * Returns new array which is a copy of the original array. + */ +public actual fun CharArray.copyOf(): CharArray { + return withType("CharArray", this.asDynamic().slice()) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +public actual fun ByteArray.copyOf(newSize: Int): ByteArray { + return fillFrom(this, ByteArray(newSize)) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +public actual fun ShortArray.copyOf(newSize: Int): ShortArray { + return fillFrom(this, ShortArray(newSize)) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +public actual fun IntArray.copyOf(newSize: Int): IntArray { + return fillFrom(this, IntArray(newSize)) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +public actual fun LongArray.copyOf(newSize: Int): LongArray { + return withType("LongArray", arrayCopyResize(this, newSize, 0L)) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +public actual fun FloatArray.copyOf(newSize: Int): FloatArray { + return fillFrom(this, FloatArray(newSize)) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +public actual fun DoubleArray.copyOf(newSize: Int): DoubleArray { + return fillFrom(this, DoubleArray(newSize)) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +public actual fun BooleanArray.copyOf(newSize: Int): BooleanArray { + return withType("BooleanArray", arrayCopyResize(this, newSize, false)) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +public actual fun CharArray.copyOf(newSize: Int): CharArray { + return withType("CharArray", fillFrom(this, CharArray(newSize))) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +public actual fun Array.copyOf(newSize: Int): Array { + return arrayCopyResize(this, newSize, null) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@Suppress("NOTHING_TO_INLINE") +public actual inline fun Array.copyOfRange(fromIndex: Int, toIndex: Int): Array { + return this.asDynamic().slice(fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@Suppress("NOTHING_TO_INLINE") +public actual inline fun ByteArray.copyOfRange(fromIndex: Int, toIndex: Int): ByteArray { + return this.asDynamic().slice(fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@Suppress("NOTHING_TO_INLINE") +public actual inline fun ShortArray.copyOfRange(fromIndex: Int, toIndex: Int): ShortArray { + return this.asDynamic().slice(fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@Suppress("NOTHING_TO_INLINE") +public actual inline fun IntArray.copyOfRange(fromIndex: Int, toIndex: Int): IntArray { + return this.asDynamic().slice(fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +public actual fun LongArray.copyOfRange(fromIndex: Int, toIndex: Int): LongArray { + return withType("LongArray", this.asDynamic().slice(fromIndex, toIndex)) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@Suppress("NOTHING_TO_INLINE") +public actual inline fun FloatArray.copyOfRange(fromIndex: Int, toIndex: Int): FloatArray { + return this.asDynamic().slice(fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@Suppress("NOTHING_TO_INLINE") +public actual inline fun DoubleArray.copyOfRange(fromIndex: Int, toIndex: Int): DoubleArray { + return this.asDynamic().slice(fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +public actual fun BooleanArray.copyOfRange(fromIndex: Int, toIndex: Int): BooleanArray { + return withType("BooleanArray", this.asDynamic().slice(fromIndex, toIndex)) +} + +/** + * Returns new array which is a copy of range of original array. + */ +public actual fun CharArray.copyOfRange(fromIndex: Int, toIndex: Int): CharArray { + return withType("CharArray", this.asDynamic().slice(fromIndex, toIndex)) +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +@Suppress("NOTHING_TO_INLINE") +public actual inline operator fun Array.plus(element: T): Array { + return this.asDynamic().concat(arrayOf(element)) +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +@Suppress("NOTHING_TO_INLINE") +public actual inline operator fun ByteArray.plus(element: Byte): ByteArray { + return plus(byteArrayOf(element)) +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +@Suppress("NOTHING_TO_INLINE") +public actual inline operator fun ShortArray.plus(element: Short): ShortArray { + return plus(shortArrayOf(element)) +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +@Suppress("NOTHING_TO_INLINE") +public actual inline operator fun IntArray.plus(element: Int): IntArray { + return plus(intArrayOf(element)) +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +@Suppress("NOTHING_TO_INLINE") +public actual inline operator fun LongArray.plus(element: Long): LongArray { + return plus(longArrayOf(element)) +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +@Suppress("NOTHING_TO_INLINE") +public actual inline operator fun FloatArray.plus(element: Float): FloatArray { + return plus(floatArrayOf(element)) +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +@Suppress("NOTHING_TO_INLINE") +public actual inline operator fun DoubleArray.plus(element: Double): DoubleArray { + return plus(doubleArrayOf(element)) +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +@Suppress("NOTHING_TO_INLINE") +public actual inline operator fun BooleanArray.plus(element: Boolean): BooleanArray { + return plus(booleanArrayOf(element)) +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +@Suppress("NOTHING_TO_INLINE") +public actual inline operator fun CharArray.plus(element: Char): CharArray { + return plus(charArrayOf(element)) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public actual operator fun Array.plus(elements: Collection): Array { + return arrayPlusCollection(this, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public actual operator fun ByteArray.plus(elements: Collection): ByteArray { + return fillFromCollection(this.copyOf(size + elements.size), this.size, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public actual operator fun ShortArray.plus(elements: Collection): ShortArray { + return fillFromCollection(this.copyOf(size + elements.size), this.size, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public actual operator fun IntArray.plus(elements: Collection): IntArray { + return fillFromCollection(this.copyOf(size + elements.size), this.size, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public actual operator fun LongArray.plus(elements: Collection): LongArray { + return arrayPlusCollection(this, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public actual operator fun FloatArray.plus(elements: Collection): FloatArray { + return fillFromCollection(this.copyOf(size + elements.size), this.size, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public actual operator fun DoubleArray.plus(elements: Collection): DoubleArray { + return fillFromCollection(this.copyOf(size + elements.size), this.size, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public actual operator fun BooleanArray.plus(elements: Collection): BooleanArray { + return arrayPlusCollection(this, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public actual operator fun CharArray.plus(elements: Collection): CharArray { + return fillFromCollection(this.copyOf(size + elements.size), this.size, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +@Suppress("NOTHING_TO_INLINE") +public actual inline operator fun Array.plus(elements: Array): Array { + return this.asDynamic().concat(elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +@Suppress("NOTHING_TO_INLINE") +public actual inline operator fun ByteArray.plus(elements: ByteArray): ByteArray { + return primitiveArrayConcat(this, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +@Suppress("NOTHING_TO_INLINE") +public actual inline operator fun ShortArray.plus(elements: ShortArray): ShortArray { + return primitiveArrayConcat(this, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +@Suppress("NOTHING_TO_INLINE") +public actual inline operator fun IntArray.plus(elements: IntArray): IntArray { + return primitiveArrayConcat(this, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +@Suppress("NOTHING_TO_INLINE") +public actual inline operator fun LongArray.plus(elements: LongArray): LongArray { + return primitiveArrayConcat(this, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +@Suppress("NOTHING_TO_INLINE") +public actual inline operator fun FloatArray.plus(elements: FloatArray): FloatArray { + return primitiveArrayConcat(this, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +@Suppress("NOTHING_TO_INLINE") +public actual inline operator fun DoubleArray.plus(elements: DoubleArray): DoubleArray { + return primitiveArrayConcat(this, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +@Suppress("NOTHING_TO_INLINE") +public actual inline operator fun BooleanArray.plus(elements: BooleanArray): BooleanArray { + return primitiveArrayConcat(this, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +@Suppress("NOTHING_TO_INLINE") +public actual inline operator fun CharArray.plus(elements: CharArray): CharArray { + return primitiveArrayConcat(this, elements) +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +@Suppress("NOTHING_TO_INLINE") +public actual inline fun Array.plusElement(element: T): Array { + return this.asDynamic().concat(arrayOf(element)) +} + +/** + * Sorts the array in-place. + */ +@library("primitiveArraySort") +public actual fun IntArray.sort(): Unit { + definedExternally +} + +/** + * Sorts the array in-place. + */ +public actual fun LongArray.sort(): Unit { + if (size > 1) + sort { a: Long, b: Long -> a.compareTo(b) } +} + +/** + * Sorts the array in-place. + */ +@library("primitiveArraySort") +public actual fun ByteArray.sort(): Unit { + definedExternally +} + +/** + * Sorts the array in-place. + */ +@library("primitiveArraySort") +public actual fun ShortArray.sort(): Unit { + definedExternally +} + +/** + * Sorts the array in-place. + */ +@library("primitiveArraySort") +public actual fun DoubleArray.sort(): Unit { + definedExternally +} + +/** + * Sorts the array in-place. + */ +@library("primitiveArraySort") +public actual fun FloatArray.sort(): Unit { + definedExternally +} + +/** + * Sorts the array in-place. + */ +@library("primitiveArraySort") +public actual fun CharArray.sort(): Unit { + definedExternally +} + +/** + * Sorts the array in-place according to the natural order of its elements. + */ +public actual fun > Array.sort(): Unit { + if (size > 1) + sort { a: T, b: T -> a.compareTo(b) } +} + +/** + * Sorts the array in-place according to the order specified by the given [comparison] function. + */ +@kotlin.internal.InlineOnly +public inline fun Array.sort(noinline comparison: (a: T, b: T) -> Int): Unit { + asDynamic().sort(comparison) +} + +/** + * Sorts the array in-place according to the order specified by the given [comparison] function. + */ +@kotlin.internal.InlineOnly +public inline fun ByteArray.sort(noinline comparison: (a: Byte, b: Byte) -> Int): Unit { + asDynamic().sort(comparison) +} + +/** + * Sorts the array in-place according to the order specified by the given [comparison] function. + */ +@kotlin.internal.InlineOnly +public inline fun ShortArray.sort(noinline comparison: (a: Short, b: Short) -> Int): Unit { + asDynamic().sort(comparison) +} + +/** + * Sorts the array in-place according to the order specified by the given [comparison] function. + */ +@kotlin.internal.InlineOnly +public inline fun IntArray.sort(noinline comparison: (a: Int, b: Int) -> Int): Unit { + asDynamic().sort(comparison) +} + +/** + * Sorts the array in-place according to the order specified by the given [comparison] function. + */ +@kotlin.internal.InlineOnly +public inline fun LongArray.sort(noinline comparison: (a: Long, b: Long) -> Int): Unit { + asDynamic().sort(comparison) +} + +/** + * Sorts the array in-place according to the order specified by the given [comparison] function. + */ +@kotlin.internal.InlineOnly +public inline fun FloatArray.sort(noinline comparison: (a: Float, b: Float) -> Int): Unit { + asDynamic().sort(comparison) +} + +/** + * Sorts the array in-place according to the order specified by the given [comparison] function. + */ +@kotlin.internal.InlineOnly +public inline fun DoubleArray.sort(noinline comparison: (a: Double, b: Double) -> Int): Unit { + asDynamic().sort(comparison) +} + +/** + * Sorts the array in-place according to the order specified by the given [comparison] function. + */ +@kotlin.internal.InlineOnly +public inline fun CharArray.sort(noinline comparison: (a: Char, b: Char) -> Int): Unit { + asDynamic().sort(comparison) +} + +/** + * Sorts the array in-place according to the order specified by the given [comparator]. + */ +public actual fun Array.sortWith(comparator: Comparator): Unit { + if (size > 1) + sort { a, b -> comparator.compare(a, b) } +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public actual fun ByteArray.toTypedArray(): Array { + return js("[]").slice.call(this) +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public actual fun ShortArray.toTypedArray(): Array { + return js("[]").slice.call(this) +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public actual fun IntArray.toTypedArray(): Array { + return js("[]").slice.call(this) +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public actual fun LongArray.toTypedArray(): Array { + return copyOf().unsafeCast>() +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public actual fun FloatArray.toTypedArray(): Array { + return js("[]").slice.call(this) +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public actual fun DoubleArray.toTypedArray(): Array { + return js("[]").slice.call(this) +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public actual fun BooleanArray.toTypedArray(): Array { + return copyOf().unsafeCast>() +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public actual fun CharArray.toTypedArray(): Array { + return Array(size, { i -> this[i] }) +} + diff --git a/libraries/stdlib/js/src/generated/_CollectionsJs.kt b/libraries/stdlib/js/src/generated/_CollectionsJs.kt new file mode 100644 index 00000000000..f79b61469f3 --- /dev/null +++ b/libraries/stdlib/js/src/generated/_CollectionsJs.kt @@ -0,0 +1,30 @@ +@file:kotlin.jvm.JvmMultifileClass +@file:kotlin.jvm.JvmName("CollectionsKt") + +package kotlin.collections + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// + +import kotlin.js.* +import kotlin.* +import kotlin.text.* +import kotlin.comparisons.* + +/** + * Reverses elements in the list in-place. + */ +public actual fun MutableList.reverse(): Unit { + val midPoint = (size / 2) - 1 + if (midPoint < 0) return + var reverseIndex = lastIndex + for (index in 0..midPoint) { + val tmp = this[index] + this[index] = this[reverseIndex] + this[reverseIndex] = tmp + reverseIndex-- + } +} + diff --git a/libraries/stdlib/js/src/generated/_ComparisonsJs.kt b/libraries/stdlib/js/src/generated/_ComparisonsJs.kt new file mode 100644 index 00000000000..559c9c02a7b --- /dev/null +++ b/libraries/stdlib/js/src/generated/_ComparisonsJs.kt @@ -0,0 +1,213 @@ +@file:kotlin.jvm.JvmMultifileClass +@file:kotlin.jvm.JvmName("ComparisonsKt") + +package kotlin.comparisons + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// + +import kotlin.js.* +import kotlin.* +import kotlin.text.* +import kotlin.comparisons.* + +/** + * Returns the greater of two values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun maxOf(a: Byte, b: Byte): Byte { + return Math.max(a.toInt(), b.toInt()).unsafeCast() +} + +/** + * Returns the greater of two values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun maxOf(a: Short, b: Short): Short { + return Math.max(a.toInt(), b.toInt()).unsafeCast() +} + +/** + * Returns the greater of two values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun maxOf(a: Int, b: Int): Int { + return Math.max(a, b) +} + +/** + * Returns the greater of two values. + */ +@SinceKotlin("1.1") +@Suppress("NOTHING_TO_INLINE") +public actual inline fun maxOf(a: Long, b: Long): Long { + return if (a >= b) a else b +} + +/** + * Returns the greater of two values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun maxOf(a: Float, b: Float): Float { + return Math.max(a, b) +} + +/** + * Returns the greater of two values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun maxOf(a: Double, b: Double): Double { + return Math.max(a, b) +} + +/** + * Returns the greater of three values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun maxOf(a: Byte, b: Byte, c: Byte): Byte { + return Math.max(a.toInt(), b.toInt(), c.toInt()).unsafeCast() +} + +/** + * Returns the greater of three values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun maxOf(a: Short, b: Short, c: Short): Short { + return Math.max(a.toInt(), b.toInt(), c.toInt()).unsafeCast() +} + +/** + * Returns the greater of three values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun maxOf(a: Int, b: Int, c: Int): Int { + return Math.max(a, b, c) +} + +/** + * Returns the greater of three values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun maxOf(a: Float, b: Float, c: Float): Float { + return Math.max(a, b, c) +} + +/** + * Returns the greater of three values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun maxOf(a: Double, b: Double, c: Double): Double { + return Math.max(a, b, c) +} + +/** + * Returns the smaller of two values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun minOf(a: Byte, b: Byte): Byte { + return Math.min(a.toInt(), b.toInt()).unsafeCast() +} + +/** + * Returns the smaller of two values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun minOf(a: Short, b: Short): Short { + return Math.min(a.toInt(), b.toInt()).unsafeCast() +} + +/** + * Returns the smaller of two values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun minOf(a: Int, b: Int): Int { + return Math.min(a, b) +} + +/** + * Returns the smaller of two values. + */ +@SinceKotlin("1.1") +@Suppress("NOTHING_TO_INLINE") +public actual inline fun minOf(a: Long, b: Long): Long { + return if (a <= b) a else b +} + +/** + * Returns the smaller of two values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun minOf(a: Float, b: Float): Float { + return Math.min(a, b) +} + +/** + * Returns the smaller of two values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun minOf(a: Double, b: Double): Double { + return Math.min(a, b) +} + +/** + * Returns the smaller of three values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun minOf(a: Byte, b: Byte, c: Byte): Byte { + return Math.min(a.toInt(), b.toInt(), c.toInt()).unsafeCast() +} + +/** + * Returns the smaller of three values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun minOf(a: Short, b: Short, c: Short): Short { + return Math.min(a.toInt(), b.toInt(), c.toInt()).unsafeCast() +} + +/** + * Returns the smaller of three values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun minOf(a: Int, b: Int, c: Int): Int { + return Math.min(a, b, c) +} + +/** + * Returns the smaller of three values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun minOf(a: Float, b: Float, c: Float): Float { + return Math.min(a, b, c) +} + +/** + * Returns the smaller of three values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun minOf(a: Double, b: Double, c: Double): Double { + return Math.min(a, b, c) +} + diff --git a/libraries/stdlib/jvm/src/generated/_ArraysJvm.kt b/libraries/stdlib/jvm/src/generated/_ArraysJvm.kt new file mode 100644 index 00000000000..4df8353083e --- /dev/null +++ b/libraries/stdlib/jvm/src/generated/_ArraysJvm.kt @@ -0,0 +1,1458 @@ +@file:kotlin.jvm.JvmMultifileClass +@file:kotlin.jvm.JvmName("ArraysKt") +@file:kotlin.jvm.JvmVersion + +package kotlin.collections + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// + +import kotlin.* +import kotlin.text.* +import kotlin.comparisons.* + +/** + * Returns a list containing all elements that are instances of specified class. + */ +public fun Array<*>.filterIsInstance(klass: Class): List { + return filterIsInstanceTo(ArrayList(), klass) +} + +/** + * Appends all elements that are instances of specified class to the given [destination]. + */ +public fun , R> Array<*>.filterIsInstanceTo(destination: C, klass: Class): C { + @Suppress("UNCHECKED_CAST") + for (element in this) if (klass.isInstance(element)) destination.add(element as R) + return destination +} + +/** + * Returns a [List] that wraps the original array. + */ +public actual fun Array.asList(): List { + return ArraysUtilJVM.asList(this) +} + +/** + * Returns a [List] that wraps the original array. + */ +public actual 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 actual 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 actual 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 actual 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 actual 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 actual 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 actual 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 actual 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) + } +} + +/** + * Searches the array or the range of the array for the provided [element] using the binary search algorithm. + * The array is expected to be sorted according to the specified [comparator], otherwise the result is undefined. + * + * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. + * + * @return the index of the element, if it is contained in the array within the specified range; + * otherwise, the inverted insertion point `(-insertion point - 1)`. + * The insertion point is defined as the index at which the element should be inserted, + * so that the array (or the specified subrange of array) still remains sorted according to the specified [comparator]. + */ +public fun Array.binarySearch(element: T, comparator: Comparator, fromIndex: Int = 0, toIndex: Int = size): Int { + return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element, comparator) +} + +/** + * Searches the array or the range of the array for the provided [element] using the binary search algorithm. + * The array is expected to be sorted, otherwise the result is undefined. + * + * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. + * + * @return the index of the element, if it is contained in the array within the specified range; + * otherwise, the inverted insertion point `(-insertion point - 1)`. + * The insertion point is defined as the index at which the element should be inserted, + * so that the array (or the specified subrange of array) still remains sorted. + */ +public fun Array.binarySearch(element: T, fromIndex: Int = 0, toIndex: Int = size): Int { + return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element) +} + +/** + * Searches the array or the range of the array for the provided [element] using the binary search algorithm. + * The array is expected to be sorted, otherwise the result is undefined. + * + * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. + * + * @return the index of the element, if it is contained in the array within the specified range; + * otherwise, the inverted insertion point `(-insertion point - 1)`. + * The insertion point is defined as the index at which the element should be inserted, + * so that the array (or the specified subrange of array) still remains sorted. + */ +public fun ByteArray.binarySearch(element: Byte, fromIndex: Int = 0, toIndex: Int = size): Int { + return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element) +} + +/** + * Searches the array or the range of the array for the provided [element] using the binary search algorithm. + * The array is expected to be sorted, otherwise the result is undefined. + * + * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. + * + * @return the index of the element, if it is contained in the array within the specified range; + * otherwise, the inverted insertion point `(-insertion point - 1)`. + * The insertion point is defined as the index at which the element should be inserted, + * so that the array (or the specified subrange of array) still remains sorted. + */ +public fun ShortArray.binarySearch(element: Short, fromIndex: Int = 0, toIndex: Int = size): Int { + return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element) +} + +/** + * Searches the array or the range of the array for the provided [element] using the binary search algorithm. + * The array is expected to be sorted, otherwise the result is undefined. + * + * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. + * + * @return the index of the element, if it is contained in the array within the specified range; + * otherwise, the inverted insertion point `(-insertion point - 1)`. + * The insertion point is defined as the index at which the element should be inserted, + * so that the array (or the specified subrange of array) still remains sorted. + */ +public fun IntArray.binarySearch(element: Int, fromIndex: Int = 0, toIndex: Int = size): Int { + return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element) +} + +/** + * Searches the array or the range of the array for the provided [element] using the binary search algorithm. + * The array is expected to be sorted, otherwise the result is undefined. + * + * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. + * + * @return the index of the element, if it is contained in the array within the specified range; + * otherwise, the inverted insertion point `(-insertion point - 1)`. + * The insertion point is defined as the index at which the element should be inserted, + * so that the array (or the specified subrange of array) still remains sorted. + */ +public fun LongArray.binarySearch(element: Long, fromIndex: Int = 0, toIndex: Int = size): Int { + return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element) +} + +/** + * Searches the array or the range of the array for the provided [element] using the binary search algorithm. + * The array is expected to be sorted, otherwise the result is undefined. + * + * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. + * + * @return the index of the element, if it is contained in the array within the specified range; + * otherwise, the inverted insertion point `(-insertion point - 1)`. + * The insertion point is defined as the index at which the element should be inserted, + * so that the array (or the specified subrange of array) still remains sorted. + */ +public fun FloatArray.binarySearch(element: Float, fromIndex: Int = 0, toIndex: Int = size): Int { + return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element) +} + +/** + * Searches the array or the range of the array for the provided [element] using the binary search algorithm. + * The array is expected to be sorted, otherwise the result is undefined. + * + * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. + * + * @return the index of the element, if it is contained in the array within the specified range; + * otherwise, the inverted insertion point `(-insertion point - 1)`. + * The insertion point is defined as the index at which the element should be inserted, + * so that the array (or the specified subrange of array) still remains sorted. + */ +public fun DoubleArray.binarySearch(element: Double, fromIndex: Int = 0, toIndex: Int = size): Int { + return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element) +} + +/** + * Searches the array or the range of the array for the provided [element] using the binary search algorithm. + * The array is expected to be sorted, otherwise the result is undefined. + * + * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. + * + * @return the index of the element, if it is contained in the array within the specified range; + * otherwise, the inverted insertion point `(-insertion point - 1)`. + * The insertion point is defined as the index at which the element should be inserted, + * so that the array (or the specified subrange of array) still remains sorted. + */ +public fun CharArray.binarySearch(element: Char, fromIndex: Int = 0, toIndex: Int = size): Int { + return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element) +} + +/** + * Returns `true` if the two specified arrays are *deeply* equal to one another, + * i.e. contain the same number of the same elements in the same order. + * + * If two corresponding elements are nested arrays, they are also compared deeply. + * If any of arrays contains itself on any nesting level the behavior is undefined. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline infix fun Array.contentDeepEquals(other: Array): Boolean { + return java.util.Arrays.deepEquals(this, other) +} + +/** + * Returns a hash code based on the contents of this array as if it is [List]. + * Nested arrays are treated as lists too. + * + * If any of arrays contains itself on any nesting level the behavior is undefined. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun Array.contentDeepHashCode(): Int { + return java.util.Arrays.deepHashCode(this) +} + +/** + * Returns a string representation of the contents of this array as if it is a [List]. + * Nested arrays are treated as lists too. + * + * If any of arrays contains itself on any nesting level that reference + * is rendered as `"[...]"` to prevent recursion. + * + * @sample samples.collections.Arrays.ContentOperations.contentDeepToString + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun Array.contentDeepToString(): String { + return java.util.Arrays.deepToString(this) +} + +/** + * Returns `true` if the two specified arrays are *structurally* equal to one another, + * i.e. contain the same number of the same elements in the same order. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline infix fun Array.contentEquals(other: Array): Boolean { + return java.util.Arrays.equals(this, other) +} + +/** + * Returns `true` if the two specified arrays are *structurally* equal to one another, + * i.e. contain the same number of the same elements in the same order. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline infix fun ByteArray.contentEquals(other: ByteArray): Boolean { + return java.util.Arrays.equals(this, other) +} + +/** + * Returns `true` if the two specified arrays are *structurally* equal to one another, + * i.e. contain the same number of the same elements in the same order. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline infix fun ShortArray.contentEquals(other: ShortArray): Boolean { + return java.util.Arrays.equals(this, other) +} + +/** + * Returns `true` if the two specified arrays are *structurally* equal to one another, + * i.e. contain the same number of the same elements in the same order. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline infix fun IntArray.contentEquals(other: IntArray): Boolean { + return java.util.Arrays.equals(this, other) +} + +/** + * Returns `true` if the two specified arrays are *structurally* equal to one another, + * i.e. contain the same number of the same elements in the same order. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline infix fun LongArray.contentEquals(other: LongArray): Boolean { + return java.util.Arrays.equals(this, other) +} + +/** + * Returns `true` if the two specified arrays are *structurally* equal to one another, + * i.e. contain the same number of the same elements in the same order. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline infix fun FloatArray.contentEquals(other: FloatArray): Boolean { + return java.util.Arrays.equals(this, other) +} + +/** + * Returns `true` if the two specified arrays are *structurally* equal to one another, + * i.e. contain the same number of the same elements in the same order. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline infix fun DoubleArray.contentEquals(other: DoubleArray): Boolean { + return java.util.Arrays.equals(this, other) +} + +/** + * Returns `true` if the two specified arrays are *structurally* equal to one another, + * i.e. contain the same number of the same elements in the same order. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline infix fun BooleanArray.contentEquals(other: BooleanArray): Boolean { + return java.util.Arrays.equals(this, other) +} + +/** + * Returns `true` if the two specified arrays are *structurally* equal to one another, + * i.e. contain the same number of the same elements in the same order. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline infix fun CharArray.contentEquals(other: CharArray): Boolean { + return java.util.Arrays.equals(this, other) +} + +/** + * Returns a hash code based on the contents of this array as if it is [List]. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun Array.contentHashCode(): Int { + return java.util.Arrays.hashCode(this) +} + +/** + * Returns a hash code based on the contents of this array as if it is [List]. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun ByteArray.contentHashCode(): Int { + return java.util.Arrays.hashCode(this) +} + +/** + * Returns a hash code based on the contents of this array as if it is [List]. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun ShortArray.contentHashCode(): Int { + return java.util.Arrays.hashCode(this) +} + +/** + * Returns a hash code based on the contents of this array as if it is [List]. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun IntArray.contentHashCode(): Int { + return java.util.Arrays.hashCode(this) +} + +/** + * Returns a hash code based on the contents of this array as if it is [List]. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun LongArray.contentHashCode(): Int { + return java.util.Arrays.hashCode(this) +} + +/** + * Returns a hash code based on the contents of this array as if it is [List]. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun FloatArray.contentHashCode(): Int { + return java.util.Arrays.hashCode(this) +} + +/** + * Returns a hash code based on the contents of this array as if it is [List]. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun DoubleArray.contentHashCode(): Int { + return java.util.Arrays.hashCode(this) +} + +/** + * Returns a hash code based on the contents of this array as if it is [List]. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun BooleanArray.contentHashCode(): Int { + return java.util.Arrays.hashCode(this) +} + +/** + * Returns a hash code based on the contents of this array as if it is [List]. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun CharArray.contentHashCode(): Int { + return java.util.Arrays.hashCode(this) +} + +/** + * Returns a string representation of the contents of the specified array as if it is [List]. + * + * @sample samples.collections.Arrays.ContentOperations.contentToString + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun Array.contentToString(): String { + return java.util.Arrays.toString(this) +} + +/** + * Returns a string representation of the contents of the specified array as if it is [List]. + * + * @sample samples.collections.Arrays.ContentOperations.contentToString + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun ByteArray.contentToString(): String { + return java.util.Arrays.toString(this) +} + +/** + * Returns a string representation of the contents of the specified array as if it is [List]. + * + * @sample samples.collections.Arrays.ContentOperations.contentToString + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun ShortArray.contentToString(): String { + return java.util.Arrays.toString(this) +} + +/** + * Returns a string representation of the contents of the specified array as if it is [List]. + * + * @sample samples.collections.Arrays.ContentOperations.contentToString + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun IntArray.contentToString(): String { + return java.util.Arrays.toString(this) +} + +/** + * Returns a string representation of the contents of the specified array as if it is [List]. + * + * @sample samples.collections.Arrays.ContentOperations.contentToString + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun LongArray.contentToString(): String { + return java.util.Arrays.toString(this) +} + +/** + * Returns a string representation of the contents of the specified array as if it is [List]. + * + * @sample samples.collections.Arrays.ContentOperations.contentToString + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun FloatArray.contentToString(): String { + return java.util.Arrays.toString(this) +} + +/** + * Returns a string representation of the contents of the specified array as if it is [List]. + * + * @sample samples.collections.Arrays.ContentOperations.contentToString + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun DoubleArray.contentToString(): String { + return java.util.Arrays.toString(this) +} + +/** + * Returns a string representation of the contents of the specified array as if it is [List]. + * + * @sample samples.collections.Arrays.ContentOperations.contentToString + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun BooleanArray.contentToString(): String { + return java.util.Arrays.toString(this) +} + +/** + * Returns a string representation of the contents of the specified array as if it is [List]. + * + * @sample samples.collections.Arrays.ContentOperations.contentToString + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun CharArray.contentToString(): String { + return java.util.Arrays.toString(this) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.internal.InlineOnly +public actual inline fun Array.copyOf(): Array { + return java.util.Arrays.copyOf(this, size) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.internal.InlineOnly +public actual inline fun ByteArray.copyOf(): ByteArray { + return java.util.Arrays.copyOf(this, size) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.internal.InlineOnly +public actual inline fun ShortArray.copyOf(): ShortArray { + return java.util.Arrays.copyOf(this, size) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.internal.InlineOnly +public actual inline fun IntArray.copyOf(): IntArray { + return java.util.Arrays.copyOf(this, size) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.internal.InlineOnly +public actual inline fun LongArray.copyOf(): LongArray { + return java.util.Arrays.copyOf(this, size) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.internal.InlineOnly +public actual inline fun FloatArray.copyOf(): FloatArray { + return java.util.Arrays.copyOf(this, size) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.internal.InlineOnly +public actual inline fun DoubleArray.copyOf(): DoubleArray { + return java.util.Arrays.copyOf(this, size) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.internal.InlineOnly +public actual inline fun BooleanArray.copyOf(): BooleanArray { + return java.util.Arrays.copyOf(this, size) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.internal.InlineOnly +public actual inline fun CharArray.copyOf(): CharArray { + return java.util.Arrays.copyOf(this, size) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +@kotlin.internal.InlineOnly +public actual inline fun ByteArray.copyOf(newSize: Int): ByteArray { + return java.util.Arrays.copyOf(this, newSize) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +@kotlin.internal.InlineOnly +public actual inline fun ShortArray.copyOf(newSize: Int): ShortArray { + return java.util.Arrays.copyOf(this, newSize) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +@kotlin.internal.InlineOnly +public actual inline fun IntArray.copyOf(newSize: Int): IntArray { + return java.util.Arrays.copyOf(this, newSize) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +@kotlin.internal.InlineOnly +public actual inline fun LongArray.copyOf(newSize: Int): LongArray { + return java.util.Arrays.copyOf(this, newSize) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +@kotlin.internal.InlineOnly +public actual inline fun FloatArray.copyOf(newSize: Int): FloatArray { + return java.util.Arrays.copyOf(this, newSize) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +@kotlin.internal.InlineOnly +public actual inline fun DoubleArray.copyOf(newSize: Int): DoubleArray { + return java.util.Arrays.copyOf(this, newSize) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +@kotlin.internal.InlineOnly +public actual inline fun BooleanArray.copyOf(newSize: Int): BooleanArray { + return java.util.Arrays.copyOf(this, newSize) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +@kotlin.internal.InlineOnly +public actual inline fun CharArray.copyOf(newSize: Int): CharArray { + return java.util.Arrays.copyOf(this, newSize) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +@kotlin.internal.InlineOnly +public actual inline fun Array.copyOf(newSize: Int): Array { + return java.util.Arrays.copyOf(this, newSize) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@kotlin.internal.InlineOnly +public actual inline fun Array.copyOfRange(fromIndex: Int, toIndex: Int): Array { + return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@kotlin.internal.InlineOnly +public actual inline fun ByteArray.copyOfRange(fromIndex: Int, toIndex: Int): ByteArray { + return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@kotlin.internal.InlineOnly +public actual inline fun ShortArray.copyOfRange(fromIndex: Int, toIndex: Int): ShortArray { + return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@kotlin.internal.InlineOnly +public actual inline fun IntArray.copyOfRange(fromIndex: Int, toIndex: Int): IntArray { + return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@kotlin.internal.InlineOnly +public actual inline fun LongArray.copyOfRange(fromIndex: Int, toIndex: Int): LongArray { + return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@kotlin.internal.InlineOnly +public actual inline fun FloatArray.copyOfRange(fromIndex: Int, toIndex: Int): FloatArray { + return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@kotlin.internal.InlineOnly +public actual inline fun DoubleArray.copyOfRange(fromIndex: Int, toIndex: Int): DoubleArray { + return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@kotlin.internal.InlineOnly +public actual inline fun BooleanArray.copyOfRange(fromIndex: Int, toIndex: Int): BooleanArray { + return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@kotlin.internal.InlineOnly +public actual inline fun CharArray.copyOfRange(fromIndex: Int, toIndex: Int): CharArray { + return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) +} + +/** + * Fills original array with the provided value. + */ +public fun Array.fill(element: T, fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.fill(this, fromIndex, toIndex, element) +} + +/** + * Fills original array with the provided value. + */ +public fun ByteArray.fill(element: Byte, fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.fill(this, fromIndex, toIndex, element) +} + +/** + * Fills original array with the provided value. + */ +public fun ShortArray.fill(element: Short, fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.fill(this, fromIndex, toIndex, element) +} + +/** + * Fills original array with the provided value. + */ +public fun IntArray.fill(element: Int, fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.fill(this, fromIndex, toIndex, element) +} + +/** + * Fills original array with the provided value. + */ +public fun LongArray.fill(element: Long, fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.fill(this, fromIndex, toIndex, element) +} + +/** + * Fills original array with the provided value. + */ +public fun FloatArray.fill(element: Float, fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.fill(this, fromIndex, toIndex, element) +} + +/** + * Fills original array with the provided value. + */ +public fun DoubleArray.fill(element: Double, fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.fill(this, fromIndex, toIndex, element) +} + +/** + * Fills original array with the provided value. + */ +public fun BooleanArray.fill(element: Boolean, fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.fill(this, fromIndex, toIndex, element) +} + +/** + * Fills original array with the provided value. + */ +public fun CharArray.fill(element: Char, fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.fill(this, fromIndex, toIndex, element) +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public actual operator fun Array.plus(element: T): Array { + val index = size + val result = java.util.Arrays.copyOf(this, index + 1) + result[index] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public actual operator fun ByteArray.plus(element: Byte): ByteArray { + val index = size + val result = java.util.Arrays.copyOf(this, index + 1) + result[index] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public actual operator fun ShortArray.plus(element: Short): ShortArray { + val index = size + val result = java.util.Arrays.copyOf(this, index + 1) + result[index] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public actual operator fun IntArray.plus(element: Int): IntArray { + val index = size + val result = java.util.Arrays.copyOf(this, index + 1) + result[index] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public actual operator fun LongArray.plus(element: Long): LongArray { + val index = size + val result = java.util.Arrays.copyOf(this, index + 1) + result[index] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public actual operator fun FloatArray.plus(element: Float): FloatArray { + val index = size + val result = java.util.Arrays.copyOf(this, index + 1) + result[index] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public actual operator fun DoubleArray.plus(element: Double): DoubleArray { + val index = size + val result = java.util.Arrays.copyOf(this, index + 1) + result[index] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public actual operator fun BooleanArray.plus(element: Boolean): BooleanArray { + val index = size + val result = java.util.Arrays.copyOf(this, index + 1) + result[index] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public actual operator fun CharArray.plus(element: Char): CharArray { + val index = size + val result = java.util.Arrays.copyOf(this, index + 1) + result[index] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public actual operator fun Array.plus(elements: Collection): Array { + var index = size + val result = java.util.Arrays.copyOf(this, index + elements.size) + for (element in elements) result[index++] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public actual operator fun ByteArray.plus(elements: Collection): ByteArray { + var index = size + val result = java.util.Arrays.copyOf(this, index + elements.size) + for (element in elements) result[index++] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public actual operator fun ShortArray.plus(elements: Collection): ShortArray { + var index = size + val result = java.util.Arrays.copyOf(this, index + elements.size) + for (element in elements) result[index++] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public actual operator fun IntArray.plus(elements: Collection): IntArray { + var index = size + val result = java.util.Arrays.copyOf(this, index + elements.size) + for (element in elements) result[index++] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public actual operator fun LongArray.plus(elements: Collection): LongArray { + var index = size + val result = java.util.Arrays.copyOf(this, index + elements.size) + for (element in elements) result[index++] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public actual operator fun FloatArray.plus(elements: Collection): FloatArray { + var index = size + val result = java.util.Arrays.copyOf(this, index + elements.size) + for (element in elements) result[index++] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public actual operator fun DoubleArray.plus(elements: Collection): DoubleArray { + var index = size + val result = java.util.Arrays.copyOf(this, index + elements.size) + for (element in elements) result[index++] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public actual operator fun BooleanArray.plus(elements: Collection): BooleanArray { + var index = size + val result = java.util.Arrays.copyOf(this, index + elements.size) + for (element in elements) result[index++] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public actual operator fun CharArray.plus(elements: Collection): CharArray { + var index = size + val result = java.util.Arrays.copyOf(this, index + elements.size) + for (element in elements) result[index++] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +public actual operator fun Array.plus(elements: Array): Array { + val thisSize = size + val arraySize = elements.size + val result = java.util.Arrays.copyOf(this, thisSize + arraySize) + System.arraycopy(elements, 0, result, thisSize, arraySize) + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +public actual operator fun ByteArray.plus(elements: ByteArray): ByteArray { + val thisSize = size + val arraySize = elements.size + val result = java.util.Arrays.copyOf(this, thisSize + arraySize) + System.arraycopy(elements, 0, result, thisSize, arraySize) + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +public actual operator fun ShortArray.plus(elements: ShortArray): ShortArray { + val thisSize = size + val arraySize = elements.size + val result = java.util.Arrays.copyOf(this, thisSize + arraySize) + System.arraycopy(elements, 0, result, thisSize, arraySize) + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +public actual operator fun IntArray.plus(elements: IntArray): IntArray { + val thisSize = size + val arraySize = elements.size + val result = java.util.Arrays.copyOf(this, thisSize + arraySize) + System.arraycopy(elements, 0, result, thisSize, arraySize) + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +public actual operator fun LongArray.plus(elements: LongArray): LongArray { + val thisSize = size + val arraySize = elements.size + val result = java.util.Arrays.copyOf(this, thisSize + arraySize) + System.arraycopy(elements, 0, result, thisSize, arraySize) + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +public actual operator fun FloatArray.plus(elements: FloatArray): FloatArray { + val thisSize = size + val arraySize = elements.size + val result = java.util.Arrays.copyOf(this, thisSize + arraySize) + System.arraycopy(elements, 0, result, thisSize, arraySize) + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +public actual operator fun DoubleArray.plus(elements: DoubleArray): DoubleArray { + val thisSize = size + val arraySize = elements.size + val result = java.util.Arrays.copyOf(this, thisSize + arraySize) + System.arraycopy(elements, 0, result, thisSize, arraySize) + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +public actual operator fun BooleanArray.plus(elements: BooleanArray): BooleanArray { + val thisSize = size + val arraySize = elements.size + val result = java.util.Arrays.copyOf(this, thisSize + arraySize) + System.arraycopy(elements, 0, result, thisSize, arraySize) + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +public actual operator fun CharArray.plus(elements: CharArray): CharArray { + val thisSize = size + val arraySize = elements.size + val result = java.util.Arrays.copyOf(this, thisSize + arraySize) + System.arraycopy(elements, 0, result, thisSize, arraySize) + return result +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +@kotlin.internal.InlineOnly +public actual inline fun Array.plusElement(element: T): Array { + return plus(element) +} + +/** + * Sorts the array in-place. + */ +public actual fun IntArray.sort(): Unit { + if (size > 1) java.util.Arrays.sort(this) +} + +/** + * Sorts the array in-place. + */ +public actual fun LongArray.sort(): Unit { + if (size > 1) java.util.Arrays.sort(this) +} + +/** + * Sorts the array in-place. + */ +public actual fun ByteArray.sort(): Unit { + if (size > 1) java.util.Arrays.sort(this) +} + +/** + * Sorts the array in-place. + */ +public actual fun ShortArray.sort(): Unit { + if (size > 1) java.util.Arrays.sort(this) +} + +/** + * Sorts the array in-place. + */ +public actual fun DoubleArray.sort(): Unit { + if (size > 1) java.util.Arrays.sort(this) +} + +/** + * Sorts the array in-place. + */ +public actual fun FloatArray.sort(): Unit { + if (size > 1) java.util.Arrays.sort(this) +} + +/** + * Sorts the array in-place. + */ +public actual fun CharArray.sort(): Unit { + if (size > 1) java.util.Arrays.sort(this) +} + +/** + * Sorts the array in-place according to the natural order of its elements. + */ +@kotlin.internal.InlineOnly +public actual inline fun > Array.sort(): Unit { + @Suppress("UNCHECKED_CAST") + (this as Array).sort() +} + +/** + * Sorts the array in-place according to the natural order of its elements. + * + * @throws ClassCastException if any element of the array is not [Comparable]. + */ +public fun Array.sort(): Unit { + if (size > 1) java.util.Arrays.sort(this) +} + +/** + * Sorts a range in the array in-place. + */ +public fun Array.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.sort(this, fromIndex, toIndex) +} + +/** + * Sorts a range in the array in-place. + */ +public fun ByteArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.sort(this, fromIndex, toIndex) +} + +/** + * Sorts a range in the array in-place. + */ +public fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.sort(this, fromIndex, toIndex) +} + +/** + * Sorts a range in the array in-place. + */ +public fun IntArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.sort(this, fromIndex, toIndex) +} + +/** + * Sorts a range in the array in-place. + */ +public fun LongArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.sort(this, fromIndex, toIndex) +} + +/** + * Sorts a range in the array in-place. + */ +public fun FloatArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.sort(this, fromIndex, toIndex) +} + +/** + * Sorts a range in the array in-place. + */ +public fun DoubleArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.sort(this, fromIndex, toIndex) +} + +/** + * Sorts a range in the array in-place. + */ +public fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.sort(this, fromIndex, toIndex) +} + +/** + * Sorts the array in-place according to the order specified by the given [comparator]. + */ +public actual fun Array.sortWith(comparator: Comparator): Unit { + if (size > 1) java.util.Arrays.sort(this, comparator) +} + +/** + * Sorts a range in the array in-place with the given [comparator]. + */ +public fun Array.sortWith(comparator: Comparator, fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.sort(this, fromIndex, toIndex, comparator) +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public actual fun ByteArray.toTypedArray(): Array { + val result = arrayOfNulls(size) + for (index in indices) + result[index] = this[index] + @Suppress("UNCHECKED_CAST") + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public actual fun ShortArray.toTypedArray(): Array { + val result = arrayOfNulls(size) + for (index in indices) + result[index] = this[index] + @Suppress("UNCHECKED_CAST") + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public actual fun IntArray.toTypedArray(): Array { + val result = arrayOfNulls(size) + for (index in indices) + result[index] = this[index] + @Suppress("UNCHECKED_CAST") + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public actual fun LongArray.toTypedArray(): Array { + val result = arrayOfNulls(size) + for (index in indices) + result[index] = this[index] + @Suppress("UNCHECKED_CAST") + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public actual fun FloatArray.toTypedArray(): Array { + val result = arrayOfNulls(size) + for (index in indices) + result[index] = this[index] + @Suppress("UNCHECKED_CAST") + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public actual fun DoubleArray.toTypedArray(): Array { + val result = arrayOfNulls(size) + for (index in indices) + result[index] = this[index] + @Suppress("UNCHECKED_CAST") + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public actual fun BooleanArray.toTypedArray(): Array { + val result = arrayOfNulls(size) + for (index in indices) + result[index] = this[index] + @Suppress("UNCHECKED_CAST") + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public actual fun CharArray.toTypedArray(): Array { + val result = arrayOfNulls(size) + for (index in indices) + result[index] = this[index] + @Suppress("UNCHECKED_CAST") + return result as Array +} + +/** + * Returns a [SortedSet][java.util.SortedSet] of all elements. + */ +public fun > Array.toSortedSet(): java.util.SortedSet { + return toCollection(java.util.TreeSet()) +} + +/** + * Returns a [SortedSet][java.util.SortedSet] of all elements. + */ +public fun ByteArray.toSortedSet(): java.util.SortedSet { + return toCollection(java.util.TreeSet()) +} + +/** + * Returns a [SortedSet][java.util.SortedSet] of all elements. + */ +public fun ShortArray.toSortedSet(): java.util.SortedSet { + return toCollection(java.util.TreeSet()) +} + +/** + * Returns a [SortedSet][java.util.SortedSet] of all elements. + */ +public fun IntArray.toSortedSet(): java.util.SortedSet { + return toCollection(java.util.TreeSet()) +} + +/** + * Returns a [SortedSet][java.util.SortedSet] of all elements. + */ +public fun LongArray.toSortedSet(): java.util.SortedSet { + return toCollection(java.util.TreeSet()) +} + +/** + * Returns a [SortedSet][java.util.SortedSet] of all elements. + */ +public fun FloatArray.toSortedSet(): java.util.SortedSet { + return toCollection(java.util.TreeSet()) +} + +/** + * Returns a [SortedSet][java.util.SortedSet] of all elements. + */ +public fun DoubleArray.toSortedSet(): java.util.SortedSet { + return toCollection(java.util.TreeSet()) +} + +/** + * Returns a [SortedSet][java.util.SortedSet] of all elements. + */ +public fun BooleanArray.toSortedSet(): java.util.SortedSet { + return toCollection(java.util.TreeSet()) +} + +/** + * Returns a [SortedSet][java.util.SortedSet] of all elements. + */ +public fun CharArray.toSortedSet(): java.util.SortedSet { + return toCollection(java.util.TreeSet()) +} + +/** + * Returns a [SortedSet][java.util.SortedSet] of all elements. + * + * Elements in the set returned are sorted according to the given [comparator]. + */ +public fun Array.toSortedSet(comparator: Comparator): java.util.SortedSet { + return toCollection(java.util.TreeSet(comparator)) +} + diff --git a/libraries/stdlib/jvm/src/generated/_CollectionsJvm.kt b/libraries/stdlib/jvm/src/generated/_CollectionsJvm.kt new file mode 100644 index 00000000000..56bf6b51668 --- /dev/null +++ b/libraries/stdlib/jvm/src/generated/_CollectionsJvm.kt @@ -0,0 +1,54 @@ +@file:kotlin.jvm.JvmMultifileClass +@file:kotlin.jvm.JvmName("CollectionsKt") +@file:kotlin.jvm.JvmVersion + +package kotlin.collections + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// + +import kotlin.* +import kotlin.text.* +import kotlin.comparisons.* + +/** + * Returns a list containing all elements that are instances of specified class. + */ +public fun Iterable<*>.filterIsInstance(klass: Class): List { + return filterIsInstanceTo(ArrayList(), klass) +} + +/** + * Appends all elements that are instances of specified class to the given [destination]. + */ +public fun , R> Iterable<*>.filterIsInstanceTo(destination: C, klass: Class): C { + @Suppress("UNCHECKED_CAST") + for (element in this) if (klass.isInstance(element)) destination.add(element as R) + return destination +} + +/** + * Reverses elements in the list in-place. + */ +public actual fun MutableList.reverse(): Unit { + java.util.Collections.reverse(this) +} + +/** + * Returns a [SortedSet][java.util.SortedSet] of all elements. + */ +public fun > Iterable.toSortedSet(): java.util.SortedSet { + return toCollection(java.util.TreeSet()) +} + +/** + * Returns a [SortedSet][java.util.SortedSet] of all elements. + * + * Elements in the set returned are sorted according to the given [comparator]. + */ +public fun Iterable.toSortedSet(comparator: Comparator): java.util.SortedSet { + return toCollection(java.util.TreeSet(comparator)) +} + diff --git a/libraries/stdlib/jvm/src/generated/_ComparisonsJvm.kt b/libraries/stdlib/jvm/src/generated/_ComparisonsJvm.kt new file mode 100644 index 00000000000..73017b7ef8f --- /dev/null +++ b/libraries/stdlib/jvm/src/generated/_ComparisonsJvm.kt @@ -0,0 +1,213 @@ +@file:kotlin.jvm.JvmMultifileClass +@file:kotlin.jvm.JvmName("ComparisonsKt") +@file:kotlin.jvm.JvmVersion + +package kotlin.comparisons + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// + +import kotlin.* +import kotlin.text.* +import kotlin.comparisons.* + +/** + * Returns the greater of two values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun maxOf(a: Byte, b: Byte): Byte { + return Math.max(a.toInt(), b.toInt()).toByte() +} + +/** + * Returns the greater of two values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun maxOf(a: Short, b: Short): Short { + return Math.max(a.toInt(), b.toInt()).toShort() +} + +/** + * Returns the greater of two values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun maxOf(a: Int, b: Int): Int { + return Math.max(a, b) +} + +/** + * Returns the greater of two values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun maxOf(a: Long, b: Long): Long { + return Math.max(a, b) +} + +/** + * Returns the greater of two values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun maxOf(a: Float, b: Float): Float { + return Math.max(a, b) +} + +/** + * Returns the greater of two values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun maxOf(a: Double, b: Double): Double { + return Math.max(a, b) +} + +/** + * Returns the greater of three values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun maxOf(a: Byte, b: Byte, c: Byte): Byte { + return Math.max(a.toInt(), Math.max(b.toInt(), c.toInt())).toByte() +} + +/** + * Returns the greater of three values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun maxOf(a: Short, b: Short, c: Short): Short { + return Math.max(a.toInt(), Math.max(b.toInt(), c.toInt())).toShort() +} + +/** + * Returns the greater of three values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun maxOf(a: Int, b: Int, c: Int): Int { + return maxOf(a, maxOf(b, c)) +} + +/** + * Returns the greater of three values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun maxOf(a: Float, b: Float, c: Float): Float { + return maxOf(a, maxOf(b, c)) +} + +/** + * Returns the greater of three values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun maxOf(a: Double, b: Double, c: Double): Double { + return maxOf(a, maxOf(b, c)) +} + +/** + * Returns the smaller of two values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun minOf(a: Byte, b: Byte): Byte { + return Math.min(a.toInt(), b.toInt()).toByte() +} + +/** + * Returns the smaller of two values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun minOf(a: Short, b: Short): Short { + return Math.min(a.toInt(), b.toInt()).toShort() +} + +/** + * Returns the smaller of two values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun minOf(a: Int, b: Int): Int { + return Math.min(a, b) +} + +/** + * Returns the smaller of two values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun minOf(a: Long, b: Long): Long { + return Math.min(a, b) +} + +/** + * Returns the smaller of two values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun minOf(a: Float, b: Float): Float { + return Math.min(a, b) +} + +/** + * Returns the smaller of two values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun minOf(a: Double, b: Double): Double { + return Math.min(a, b) +} + +/** + * Returns the smaller of three values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun minOf(a: Byte, b: Byte, c: Byte): Byte { + return Math.min(a.toInt(), Math.min(b.toInt(), c.toInt())).toByte() +} + +/** + * Returns the smaller of three values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun minOf(a: Short, b: Short, c: Short): Short { + return Math.min(a.toInt(), Math.min(b.toInt(), c.toInt())).toShort() +} + +/** + * Returns the smaller of three values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun minOf(a: Int, b: Int, c: Int): Int { + return minOf(a, minOf(b, c)) +} + +/** + * Returns the smaller of three values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun minOf(a: Float, b: Float, c: Float): Float { + return minOf(a, minOf(b, c)) +} + +/** + * Returns the smaller of three values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public actual inline fun minOf(a: Double, b: Double, c: Double): Double { + return minOf(a, minOf(b, c)) +} + diff --git a/libraries/stdlib/jvm/src/generated/_SequencesJvm.kt b/libraries/stdlib/jvm/src/generated/_SequencesJvm.kt new file mode 100644 index 00000000000..955c8225f64 --- /dev/null +++ b/libraries/stdlib/jvm/src/generated/_SequencesJvm.kt @@ -0,0 +1,57 @@ +@file:kotlin.jvm.JvmMultifileClass +@file:kotlin.jvm.JvmName("SequencesKt") +@file:kotlin.jvm.JvmVersion + +package kotlin.sequences + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// + +import kotlin.* +import kotlin.text.* +import kotlin.comparisons.* +import kotlin.coroutines.experimental.* + +/** + * Returns a sequence containing all elements that are instances of specified class. + * + * The operation is _intermediate_ and _stateless_. + */ +public fun Sequence<*>.filterIsInstance(klass: Class): Sequence { + @Suppress("UNCHECKED_CAST") + return filter { klass.isInstance(it) } as Sequence +} + +/** + * Appends all elements that are instances of specified class to the given [destination]. + * + * The operation is _terminal_. + */ +public fun , R> Sequence<*>.filterIsInstanceTo(destination: C, klass: Class): C { + @Suppress("UNCHECKED_CAST") + for (element in this) if (klass.isInstance(element)) destination.add(element as R) + return destination +} + +/** + * Returns a [SortedSet][java.util.SortedSet] of all elements. + * + * The operation is _terminal_. + */ +public fun > Sequence.toSortedSet(): java.util.SortedSet { + return toCollection(java.util.TreeSet()) +} + +/** + * Returns a [SortedSet][java.util.SortedSet] of all elements. + * + * Elements in the set returned are sorted according to the given [comparator]. + * + * The operation is _terminal_. + */ +public fun Sequence.toSortedSet(comparator: Comparator): java.util.SortedSet { + return toCollection(java.util.TreeSet(comparator)) +} + diff --git a/libraries/stdlib/jvm/src/generated/_StringsJvm.kt b/libraries/stdlib/jvm/src/generated/_StringsJvm.kt new file mode 100644 index 00000000000..36707d60fa0 --- /dev/null +++ b/libraries/stdlib/jvm/src/generated/_StringsJvm.kt @@ -0,0 +1,22 @@ +@file:kotlin.jvm.JvmMultifileClass +@file:kotlin.jvm.JvmName("StringsKt") +@file:kotlin.jvm.JvmVersion + +package kotlin.text + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// + +import kotlin.* +import kotlin.text.* +import kotlin.comparisons.* + +/** + * Returns a [SortedSet][java.util.SortedSet] of all characters. + */ +public fun CharSequence.toSortedSet(): java.util.SortedSet { + return toCollection(java.util.TreeSet()) +} + diff --git a/libraries/stdlib/src/generated/_Arrays.kt b/libraries/stdlib/src/generated/_Arrays.kt deleted file mode 100644 index fbebd2df875..00000000000 --- a/libraries/stdlib/src/generated/_Arrays.kt +++ /dev/null @@ -1,14323 +0,0 @@ -@file:kotlin.jvm.JvmMultifileClass -@file:kotlin.jvm.JvmName("ArraysKt") -@file:kotlin.jvm.JvmVersion - -package kotlin.collections - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// - -import kotlin.* -import kotlin.text.* -import kotlin.comparisons.* - -/** - * 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 [element] is found in the array. - */ -public operator fun <@kotlin.internal.OnlyInputTypes T> Array.contains(element: T): Boolean { - return indexOf(element) >= 0 -} - -/** - * Returns `true` if [element] is found in the array. - */ -public operator fun ByteArray.contains(element: Byte): Boolean { - return indexOf(element) >= 0 -} - -/** - * Returns `true` if [element] is found in the array. - */ -public operator fun ShortArray.contains(element: Short): Boolean { - return indexOf(element) >= 0 -} - -/** - * Returns `true` if [element] is found in the array. - */ -public operator fun IntArray.contains(element: Int): Boolean { - return indexOf(element) >= 0 -} - -/** - * Returns `true` if [element] is found in the array. - */ -public operator fun LongArray.contains(element: Long): Boolean { - return indexOf(element) >= 0 -} - -/** - * Returns `true` if [element] is found in the array. - */ -public operator fun FloatArray.contains(element: Float): Boolean { - return indexOf(element) >= 0 -} - -/** - * Returns `true` if [element] is found in the array. - */ -public operator fun DoubleArray.contains(element: Double): Boolean { - return indexOf(element) >= 0 -} - -/** - * Returns `true` if [element] is found in the array. - */ -public operator fun BooleanArray.contains(element: Boolean): Boolean { - return indexOf(element) >= 0 -} - -/** - * Returns `true` if [element] is found in the array. - */ -public operator fun CharArray.contains(element: Char): Boolean { - return indexOf(element) >= 0 -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun Array.elementAt(index: Int): T { - return get(index) -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun ByteArray.elementAt(index: Int): Byte { - return get(index) -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun ShortArray.elementAt(index: Int): Short { - return get(index) -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun IntArray.elementAt(index: Int): Int { - return get(index) -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun LongArray.elementAt(index: Int): Long { - return get(index) -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun FloatArray.elementAt(index: Int): Float { - return get(index) -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun DoubleArray.elementAt(index: Int): Double { - return get(index) -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun BooleanArray.elementAt(index: Int): Boolean { - return get(index) -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun CharArray.elementAt(index: Int): Char { - return get(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun Array.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun ByteArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Byte): Byte { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun ShortArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Short): Short { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun IntArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Int): Int { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun LongArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Long): Long { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun FloatArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Float): Float { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun DoubleArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Double): Double { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun BooleanArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Boolean): Boolean { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun CharArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Char): Char { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun Array.elementAtOrNull(index: Int): T? { - return this.getOrNull(index) -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun ByteArray.elementAtOrNull(index: Int): Byte? { - return this.getOrNull(index) -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun ShortArray.elementAtOrNull(index: Int): Short? { - return this.getOrNull(index) -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun IntArray.elementAtOrNull(index: Int): Int? { - return this.getOrNull(index) -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun LongArray.elementAtOrNull(index: Int): Long? { - return this.getOrNull(index) -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun FloatArray.elementAtOrNull(index: Int): Float? { - return this.getOrNull(index) -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun DoubleArray.elementAtOrNull(index: Int): Double? { - return this.getOrNull(index) -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun BooleanArray.elementAtOrNull(index: Int): Boolean? { - return this.getOrNull(index) -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun CharArray.elementAtOrNull(index: Int): Char? { - return this.getOrNull(index) -} - -/** - * Returns the first element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun Array.find(predicate: (T) -> Boolean): T? { - return firstOrNull(predicate) -} - -/** - * Returns the first element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun ByteArray.find(predicate: (Byte) -> Boolean): Byte? { - return firstOrNull(predicate) -} - -/** - * Returns the first element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun ShortArray.find(predicate: (Short) -> Boolean): Short? { - return firstOrNull(predicate) -} - -/** - * Returns the first element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun IntArray.find(predicate: (Int) -> Boolean): Int? { - return firstOrNull(predicate) -} - -/** - * Returns the first element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun LongArray.find(predicate: (Long) -> Boolean): Long? { - return firstOrNull(predicate) -} - -/** - * Returns the first element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun FloatArray.find(predicate: (Float) -> Boolean): Float? { - return firstOrNull(predicate) -} - -/** - * Returns the first element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun DoubleArray.find(predicate: (Double) -> Boolean): Double? { - return firstOrNull(predicate) -} - -/** - * Returns the first element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun BooleanArray.find(predicate: (Boolean) -> Boolean): Boolean? { - return firstOrNull(predicate) -} - -/** - * Returns the first element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun CharArray.find(predicate: (Char) -> Boolean): Char? { - return firstOrNull(predicate) -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun Array.findLast(predicate: (T) -> Boolean): T? { - return lastOrNull(predicate) -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun ByteArray.findLast(predicate: (Byte) -> Boolean): Byte? { - return lastOrNull(predicate) -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun ShortArray.findLast(predicate: (Short) -> Boolean): Short? { - return lastOrNull(predicate) -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun IntArray.findLast(predicate: (Int) -> Boolean): Int? { - return lastOrNull(predicate) -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun LongArray.findLast(predicate: (Long) -> Boolean): Long? { - return lastOrNull(predicate) -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun FloatArray.findLast(predicate: (Float) -> Boolean): Float? { - return lastOrNull(predicate) -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun DoubleArray.findLast(predicate: (Double) -> Boolean): Double? { - return lastOrNull(predicate) -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun BooleanArray.findLast(predicate: (Boolean) -> Boolean): Boolean? { - return lastOrNull(predicate) -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun CharArray.findLast(predicate: (Char) -> Boolean): Char? { - return lastOrNull(predicate) -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun Array.first(): T { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[0] -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun ByteArray.first(): Byte { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[0] -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun ShortArray.first(): Short { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[0] -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun IntArray.first(): Int { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[0] -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun LongArray.first(): Long { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[0] -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun FloatArray.first(): Float { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[0] -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun DoubleArray.first(): Double { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[0] -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun BooleanArray.first(): Boolean { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[0] -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun CharArray.first(): Char { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[0] -} - -/** - * Returns the first element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun Array.first(predicate: (T) -> Boolean): T { - for (element in this) if (predicate(element)) return element - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns the first element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun ByteArray.first(predicate: (Byte) -> Boolean): Byte { - for (element in this) if (predicate(element)) return element - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns the first element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun ShortArray.first(predicate: (Short) -> Boolean): Short { - for (element in this) if (predicate(element)) return element - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns the first element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun IntArray.first(predicate: (Int) -> Boolean): Int { - for (element in this) if (predicate(element)) return element - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns the first element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun LongArray.first(predicate: (Long) -> Boolean): Long { - for (element in this) if (predicate(element)) return element - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns the first element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun FloatArray.first(predicate: (Float) -> Boolean): Float { - for (element in this) if (predicate(element)) return element - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns the first element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun DoubleArray.first(predicate: (Double) -> Boolean): Double { - for (element in this) if (predicate(element)) return element - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns the first element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun BooleanArray.first(predicate: (Boolean) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return element - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns the first element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun CharArray.first(predicate: (Char) -> Boolean): Char { - for (element in this) if (predicate(element)) return element - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns the first element, or `null` if the array is empty. - */ -public fun Array.firstOrNull(): T? { - return if (isEmpty()) null else this[0] -} - -/** - * Returns the first element, or `null` if the array is empty. - */ -public fun ByteArray.firstOrNull(): Byte? { - return if (isEmpty()) null else this[0] -} - -/** - * Returns the first element, or `null` if the array is empty. - */ -public fun ShortArray.firstOrNull(): Short? { - return if (isEmpty()) null else this[0] -} - -/** - * Returns the first element, or `null` if the array is empty. - */ -public fun IntArray.firstOrNull(): Int? { - return if (isEmpty()) null else this[0] -} - -/** - * Returns the first element, or `null` if the array is empty. - */ -public fun LongArray.firstOrNull(): Long? { - return if (isEmpty()) null else this[0] -} - -/** - * Returns the first element, or `null` if the array is empty. - */ -public fun FloatArray.firstOrNull(): Float? { - return if (isEmpty()) null else this[0] -} - -/** - * Returns the first element, or `null` if the array is empty. - */ -public fun DoubleArray.firstOrNull(): Double? { - return if (isEmpty()) null else this[0] -} - -/** - * Returns the first element, or `null` if the array is empty. - */ -public fun BooleanArray.firstOrNull(): Boolean? { - return if (isEmpty()) null else this[0] -} - -/** - * Returns the first element, or `null` if the array is empty. - */ -public fun CharArray.firstOrNull(): Char? { - return if (isEmpty()) null else this[0] -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - */ -public inline fun Array.firstOrNull(predicate: (T) -> Boolean): T? { - for (element in this) if (predicate(element)) return element - return null -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - */ -public inline fun ByteArray.firstOrNull(predicate: (Byte) -> Boolean): Byte? { - for (element in this) if (predicate(element)) return element - return null -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - */ -public inline fun ShortArray.firstOrNull(predicate: (Short) -> Boolean): Short? { - for (element in this) if (predicate(element)) return element - return null -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - */ -public inline fun IntArray.firstOrNull(predicate: (Int) -> Boolean): Int? { - for (element in this) if (predicate(element)) return element - return null -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - */ -public inline fun LongArray.firstOrNull(predicate: (Long) -> Boolean): Long? { - for (element in this) if (predicate(element)) return element - return null -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - */ -public inline fun FloatArray.firstOrNull(predicate: (Float) -> Boolean): Float? { - for (element in this) if (predicate(element)) return element - return null -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - */ -public inline fun DoubleArray.firstOrNull(predicate: (Double) -> Boolean): Double? { - for (element in this) if (predicate(element)) return element - return null -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - */ -public inline fun BooleanArray.firstOrNull(predicate: (Boolean) -> Boolean): Boolean? { - for (element in this) if (predicate(element)) return element - return null -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - */ -public inline fun CharArray.firstOrNull(predicate: (Char) -> Boolean): Char? { - for (element in this) if (predicate(element)) return element - return null -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun Array.getOrElse(index: Int, defaultValue: (Int) -> T): T { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun ByteArray.getOrElse(index: Int, defaultValue: (Int) -> Byte): Byte { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun ShortArray.getOrElse(index: Int, defaultValue: (Int) -> Short): Short { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun IntArray.getOrElse(index: Int, defaultValue: (Int) -> Int): Int { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun LongArray.getOrElse(index: Int, defaultValue: (Int) -> Long): Long { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun FloatArray.getOrElse(index: Int, defaultValue: (Int) -> Float): Float { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun DoubleArray.getOrElse(index: Int, defaultValue: (Int) -> Double): Double { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun BooleanArray.getOrElse(index: Int, defaultValue: (Int) -> Boolean): Boolean { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. - */ -@kotlin.internal.InlineOnly -public inline fun CharArray.getOrElse(index: Int, defaultValue: (Int) -> Char): Char { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -public fun Array.getOrNull(index: Int): T? { - return if (index >= 0 && index <= lastIndex) get(index) else null -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -public fun ByteArray.getOrNull(index: Int): Byte? { - return if (index >= 0 && index <= lastIndex) get(index) else null -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -public fun ShortArray.getOrNull(index: Int): Short? { - return if (index >= 0 && index <= lastIndex) get(index) else null -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -public fun IntArray.getOrNull(index: Int): Int? { - return if (index >= 0 && index <= lastIndex) get(index) else null -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -public fun LongArray.getOrNull(index: Int): Long? { - return if (index >= 0 && index <= lastIndex) get(index) else null -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -public fun FloatArray.getOrNull(index: Int): Float? { - return if (index >= 0 && index <= lastIndex) get(index) else null -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -public fun DoubleArray.getOrNull(index: Int): Double? { - return if (index >= 0 && index <= lastIndex) get(index) else null -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -public fun BooleanArray.getOrNull(index: Int): Boolean? { - return if (index >= 0 && index <= lastIndex) get(index) else null -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. - */ -public fun CharArray.getOrNull(index: Int): Char? { - return if (index >= 0 && index <= lastIndex) get(index) else null -} - -/** - * Returns first index of [element], or -1 if the array does not contain element. - */ -public fun <@kotlin.internal.OnlyInputTypes T> Array.indexOf(element: T): Int { - if (element == null) { - for (index in indices) { - if (this[index] == null) { - return index - } - } - } else { - for (index in indices) { - if (element == this[index]) { - return index - } - } - } - return -1 -} - -/** - * Returns first index of [element], or -1 if the array does not contain element. - */ -public fun ByteArray.indexOf(element: Byte): Int { - for (index in indices) { - if (element == this[index]) { - return index - } - } - return -1 -} - -/** - * Returns first index of [element], or -1 if the array does not contain element. - */ -public fun ShortArray.indexOf(element: Short): Int { - for (index in indices) { - if (element == this[index]) { - return index - } - } - return -1 -} - -/** - * Returns first index of [element], or -1 if the array does not contain element. - */ -public fun IntArray.indexOf(element: Int): Int { - for (index in indices) { - if (element == this[index]) { - return index - } - } - return -1 -} - -/** - * Returns first index of [element], or -1 if the array does not contain element. - */ -public fun LongArray.indexOf(element: Long): Int { - for (index in indices) { - if (element == this[index]) { - return index - } - } - return -1 -} - -/** - * Returns first index of [element], or -1 if the array does not contain element. - */ -public fun FloatArray.indexOf(element: Float): Int { - for (index in indices) { - if (element == this[index]) { - return index - } - } - return -1 -} - -/** - * Returns first index of [element], or -1 if the array does not contain element. - */ -public fun DoubleArray.indexOf(element: Double): Int { - for (index in indices) { - if (element == this[index]) { - return index - } - } - return -1 -} - -/** - * Returns first index of [element], or -1 if the array does not contain element. - */ -public fun BooleanArray.indexOf(element: Boolean): Int { - for (index in indices) { - if (element == this[index]) { - return index - } - } - return -1 -} - -/** - * Returns first index of [element], or -1 if the array does not contain element. - */ -public fun CharArray.indexOf(element: Char): Int { - for (index in indices) { - if (element == this[index]) { - return index - } - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun Array.indexOfFirst(predicate: (T) -> Boolean): Int { - for (index in indices) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun ByteArray.indexOfFirst(predicate: (Byte) -> Boolean): Int { - for (index in indices) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun ShortArray.indexOfFirst(predicate: (Short) -> Boolean): Int { - for (index in indices) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun IntArray.indexOfFirst(predicate: (Int) -> Boolean): Int { - for (index in indices) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun LongArray.indexOfFirst(predicate: (Long) -> Boolean): Int { - for (index in indices) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun FloatArray.indexOfFirst(predicate: (Float) -> Boolean): Int { - for (index in indices) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun DoubleArray.indexOfFirst(predicate: (Double) -> Boolean): Int { - for (index in indices) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun BooleanArray.indexOfFirst(predicate: (Boolean) -> Boolean): Int { - for (index in indices) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun CharArray.indexOfFirst(predicate: (Char) -> Boolean): Int { - for (index in indices) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun Array.indexOfLast(predicate: (T) -> Boolean): Int { - for (index in indices.reversed()) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun ByteArray.indexOfLast(predicate: (Byte) -> Boolean): Int { - for (index in indices.reversed()) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun ShortArray.indexOfLast(predicate: (Short) -> Boolean): Int { - for (index in indices.reversed()) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun IntArray.indexOfLast(predicate: (Int) -> Boolean): Int { - for (index in indices.reversed()) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun LongArray.indexOfLast(predicate: (Long) -> Boolean): Int { - for (index in indices.reversed()) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun FloatArray.indexOfLast(predicate: (Float) -> Boolean): Int { - for (index in indices.reversed()) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun DoubleArray.indexOfLast(predicate: (Double) -> Boolean): Int { - for (index in indices.reversed()) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun BooleanArray.indexOfLast(predicate: (Boolean) -> Boolean): Int { - for (index in indices.reversed()) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the array does not contain such element. - */ -public inline fun CharArray.indexOfLast(predicate: (Char) -> Boolean): Int { - for (index in indices.reversed()) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun Array.last(): T { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[lastIndex] -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun ByteArray.last(): Byte { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[lastIndex] -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun ShortArray.last(): Short { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[lastIndex] -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun IntArray.last(): Int { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[lastIndex] -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun LongArray.last(): Long { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[lastIndex] -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun FloatArray.last(): Float { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[lastIndex] -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun DoubleArray.last(): Double { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[lastIndex] -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun BooleanArray.last(): Boolean { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[lastIndex] -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the array is empty. - */ -public fun CharArray.last(): Char { - if (isEmpty()) - throw NoSuchElementException("Array is empty.") - return this[lastIndex] -} - -/** - * Returns the last element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun Array.last(predicate: (T) -> Boolean): T { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns the last element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun ByteArray.last(predicate: (Byte) -> Boolean): Byte { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns the last element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun ShortArray.last(predicate: (Short) -> Boolean): Short { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns the last element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun IntArray.last(predicate: (Int) -> Boolean): Int { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns the last element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun LongArray.last(predicate: (Long) -> Boolean): Long { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns the last element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun FloatArray.last(predicate: (Float) -> Boolean): Float { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns the last element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun DoubleArray.last(predicate: (Double) -> Boolean): Double { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns the last element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun BooleanArray.last(predicate: (Boolean) -> Boolean): Boolean { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns the last element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun CharArray.last(predicate: (Char) -> Boolean): Char { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - throw NoSuchElementException("Array contains no element matching the predicate.") -} - -/** - * Returns last index of [element], or -1 if the array does not contain element. - */ -public fun <@kotlin.internal.OnlyInputTypes T> Array.lastIndexOf(element: T): Int { - if (element == null) { - for (index in indices.reversed()) { - if (this[index] == null) { - return index - } - } - } else { - 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 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 -} - -/** - * Returns the last element, or `null` if the array is empty. - */ -public fun Array.lastOrNull(): T? { - return if (isEmpty()) null else this[size - 1] -} - -/** - * Returns the last element, or `null` if the array is empty. - */ -public fun ByteArray.lastOrNull(): Byte? { - return if (isEmpty()) null else this[size - 1] -} - -/** - * Returns the last element, or `null` if the array is empty. - */ -public fun ShortArray.lastOrNull(): Short? { - return if (isEmpty()) null else this[size - 1] -} - -/** - * Returns the last element, or `null` if the array is empty. - */ -public fun IntArray.lastOrNull(): Int? { - return if (isEmpty()) null else this[size - 1] -} - -/** - * Returns the last element, or `null` if the array is empty. - */ -public fun LongArray.lastOrNull(): Long? { - return if (isEmpty()) null else this[size - 1] -} - -/** - * Returns the last element, or `null` if the array is empty. - */ -public fun FloatArray.lastOrNull(): Float? { - return if (isEmpty()) null else this[size - 1] -} - -/** - * Returns the last element, or `null` if the array is empty. - */ -public fun DoubleArray.lastOrNull(): Double? { - return if (isEmpty()) null else this[size - 1] -} - -/** - * Returns the last element, or `null` if the array is empty. - */ -public fun BooleanArray.lastOrNull(): Boolean? { - return if (isEmpty()) null else this[size - 1] -} - -/** - * Returns the last element, or `null` if the array is empty. - */ -public fun CharArray.lastOrNull(): Char? { - return if (isEmpty()) null else this[size - 1] -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -public inline fun Array.lastOrNull(predicate: (T) -> Boolean): T? { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - return null -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -public inline fun ByteArray.lastOrNull(predicate: (Byte) -> Boolean): Byte? { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - return null -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -public inline fun ShortArray.lastOrNull(predicate: (Short) -> Boolean): Short? { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - return null -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -public inline fun IntArray.lastOrNull(predicate: (Int) -> Boolean): Int? { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - return null -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -public inline fun LongArray.lastOrNull(predicate: (Long) -> Boolean): Long? { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - return null -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -public inline fun FloatArray.lastOrNull(predicate: (Float) -> Boolean): Float? { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - return null -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -public inline fun DoubleArray.lastOrNull(predicate: (Double) -> Boolean): Double? { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - return null -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -public inline fun BooleanArray.lastOrNull(predicate: (Boolean) -> Boolean): Boolean? { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - return null -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -public inline fun CharArray.lastOrNull(predicate: (Char) -> Boolean): Char? { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - return null -} - -/** - * Returns the single element, or throws an exception if the array is empty or has more than one element. - */ -public fun Array.single(): T { - return when (size) { - 0 -> throw NoSuchElementException("Array is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Array has more than one element.") - } -} - -/** - * Returns the single element, or throws an exception if the array is empty or has more than one element. - */ -public fun ByteArray.single(): Byte { - return when (size) { - 0 -> throw NoSuchElementException("Array is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Array has more than one element.") - } -} - -/** - * Returns the single element, or throws an exception if the array is empty or has more than one element. - */ -public fun ShortArray.single(): Short { - return when (size) { - 0 -> throw NoSuchElementException("Array is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Array has more than one element.") - } -} - -/** - * Returns the single element, or throws an exception if the array is empty or has more than one element. - */ -public fun IntArray.single(): Int { - return when (size) { - 0 -> throw NoSuchElementException("Array is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Array has more than one element.") - } -} - -/** - * Returns the single element, or throws an exception if the array is empty or has more than one element. - */ -public fun LongArray.single(): Long { - return when (size) { - 0 -> throw NoSuchElementException("Array is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Array has more than one element.") - } -} - -/** - * Returns the single element, or throws an exception if the array is empty or has more than one element. - */ -public fun FloatArray.single(): Float { - return when (size) { - 0 -> throw NoSuchElementException("Array is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Array has more than one element.") - } -} - -/** - * Returns the single element, or throws an exception if the array is empty or has more than one element. - */ -public fun DoubleArray.single(): Double { - return when (size) { - 0 -> throw NoSuchElementException("Array is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Array has more than one element.") - } -} - -/** - * Returns the single element, or throws an exception if the array is empty or has more than one element. - */ -public fun BooleanArray.single(): Boolean { - return when (size) { - 0 -> throw NoSuchElementException("Array is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Array has more than one element.") - } -} - -/** - * Returns the single element, or throws an exception if the array is empty or has more than one element. - */ -public fun CharArray.single(): Char { - return when (size) { - 0 -> throw NoSuchElementException("Array is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Array has more than one element.") - } -} - -/** - * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. - */ -public inline fun Array.single(predicate: (T) -> Boolean): T { - var single: T? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Array contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") - @Suppress("UNCHECKED_CAST") - return single as T -} - -/** - * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. - */ -public inline fun ByteArray.single(predicate: (Byte) -> Boolean): Byte { - var single: Byte? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Array contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") - @Suppress("UNCHECKED_CAST") - return single as Byte -} - -/** - * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. - */ -public inline fun ShortArray.single(predicate: (Short) -> Boolean): Short { - var single: Short? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Array contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") - @Suppress("UNCHECKED_CAST") - return single as Short -} - -/** - * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. - */ -public inline fun IntArray.single(predicate: (Int) -> Boolean): Int { - var single: Int? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Array contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") - @Suppress("UNCHECKED_CAST") - return single as Int -} - -/** - * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. - */ -public inline fun LongArray.single(predicate: (Long) -> Boolean): Long { - var single: Long? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Array contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") - @Suppress("UNCHECKED_CAST") - return single as Long -} - -/** - * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. - */ -public inline fun FloatArray.single(predicate: (Float) -> Boolean): Float { - var single: Float? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Array contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") - @Suppress("UNCHECKED_CAST") - return single as Float -} - -/** - * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. - */ -public inline fun DoubleArray.single(predicate: (Double) -> Boolean): Double { - var single: Double? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Array contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") - @Suppress("UNCHECKED_CAST") - return single as Double -} - -/** - * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. - */ -public inline fun BooleanArray.single(predicate: (Boolean) -> Boolean): Boolean { - var single: Boolean? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Array contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") - @Suppress("UNCHECKED_CAST") - return single as Boolean -} - -/** - * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. - */ -public inline fun CharArray.single(predicate: (Char) -> Boolean): Char { - var single: Char? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Array contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") - @Suppress("UNCHECKED_CAST") - return single as Char -} - -/** - * Returns single element, or `null` if the array is empty or has more than one element. - */ -public fun Array.singleOrNull(): T? { - return if (size == 1) this[0] else null -} - -/** - * Returns single element, or `null` if the array is empty or has more than one element. - */ -public fun ByteArray.singleOrNull(): Byte? { - return if (size == 1) this[0] else null -} - -/** - * Returns single element, or `null` if the array is empty or has more than one element. - */ -public fun ShortArray.singleOrNull(): Short? { - return if (size == 1) this[0] else null -} - -/** - * Returns single element, or `null` if the array is empty or has more than one element. - */ -public fun IntArray.singleOrNull(): Int? { - return if (size == 1) this[0] else null -} - -/** - * Returns single element, or `null` if the array is empty or has more than one element. - */ -public fun LongArray.singleOrNull(): Long? { - return if (size == 1) this[0] else null -} - -/** - * Returns single element, or `null` if the array is empty or has more than one element. - */ -public fun FloatArray.singleOrNull(): Float? { - return if (size == 1) this[0] else null -} - -/** - * Returns single element, or `null` if the array is empty or has more than one element. - */ -public fun DoubleArray.singleOrNull(): Double? { - return if (size == 1) this[0] else null -} - -/** - * Returns single element, or `null` if the array is empty or has more than one element. - */ -public fun BooleanArray.singleOrNull(): Boolean? { - return if (size == 1) this[0] else null -} - -/** - * Returns single element, or `null` if the array is empty or has more than one element. - */ -public fun CharArray.singleOrNull(): Char? { - return if (size == 1) this[0] else null -} - -/** - * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. - */ -public inline fun Array.singleOrNull(predicate: (T) -> Boolean): T? { - var single: T? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) return null - single = element - found = true - } - } - if (!found) return null - return single -} - -/** - * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. - */ -public inline fun ByteArray.singleOrNull(predicate: (Byte) -> Boolean): Byte? { - var single: Byte? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) return null - single = element - found = true - } - } - if (!found) return null - return single -} - -/** - * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. - */ -public inline fun ShortArray.singleOrNull(predicate: (Short) -> Boolean): Short? { - var single: Short? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) return null - single = element - found = true - } - } - if (!found) return null - return single -} - -/** - * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. - */ -public inline fun IntArray.singleOrNull(predicate: (Int) -> Boolean): Int? { - var single: Int? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) return null - single = element - found = true - } - } - if (!found) return null - return single -} - -/** - * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. - */ -public inline fun LongArray.singleOrNull(predicate: (Long) -> Boolean): Long? { - var single: Long? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) return null - single = element - found = true - } - } - if (!found) return null - return single -} - -/** - * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. - */ -public inline fun FloatArray.singleOrNull(predicate: (Float) -> Boolean): Float? { - var single: Float? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) return null - single = element - found = true - } - } - if (!found) return null - return single -} - -/** - * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. - */ -public inline fun DoubleArray.singleOrNull(predicate: (Double) -> Boolean): Double? { - var single: Double? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) return null - single = element - found = true - } - } - if (!found) return null - return single -} - -/** - * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. - */ -public inline fun BooleanArray.singleOrNull(predicate: (Boolean) -> Boolean): Boolean? { - var single: Boolean? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) return null - single = element - found = true - } - } - if (!found) return null - return single -} - -/** - * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. - */ -public inline fun CharArray.singleOrNull(predicate: (Char) -> Boolean): Char? { - var single: Char? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) return null - single = element - found = true - } - } - if (!found) return null - return single -} - -/** - * Returns a list containing all elements except first [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun Array.drop(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return takeLast((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except first [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun ByteArray.drop(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return takeLast((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except first [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun ShortArray.drop(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return takeLast((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except first [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun IntArray.drop(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return takeLast((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except first [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun LongArray.drop(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return takeLast((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except first [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun FloatArray.drop(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return takeLast((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except first [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun DoubleArray.drop(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return takeLast((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except first [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun BooleanArray.drop(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return takeLast((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except first [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun CharArray.drop(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return takeLast((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except last [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun Array.dropLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return take((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except last [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun ByteArray.dropLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return take((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except last [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun ShortArray.dropLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return take((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except last [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun IntArray.dropLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return take((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except last [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun LongArray.dropLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return take((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except last [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun FloatArray.dropLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return take((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except last [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun DoubleArray.dropLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return take((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except last [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun BooleanArray.dropLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return take((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except last [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun CharArray.dropLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return take((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except last elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun Array.dropLastWhile(predicate: (T) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return take(index + 1) - } - } - return emptyList() -} - -/** - * Returns a list containing all elements except last elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun ByteArray.dropLastWhile(predicate: (Byte) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return take(index + 1) - } - } - return emptyList() -} - -/** - * Returns a list containing all elements except last elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun ShortArray.dropLastWhile(predicate: (Short) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return take(index + 1) - } - } - return emptyList() -} - -/** - * Returns a list containing all elements except last elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun IntArray.dropLastWhile(predicate: (Int) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return take(index + 1) - } - } - return emptyList() -} - -/** - * Returns a list containing all elements except last elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun LongArray.dropLastWhile(predicate: (Long) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return take(index + 1) - } - } - return emptyList() -} - -/** - * Returns a list containing all elements except last elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun FloatArray.dropLastWhile(predicate: (Float) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return take(index + 1) - } - } - return emptyList() -} - -/** - * Returns a list containing all elements except last elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun DoubleArray.dropLastWhile(predicate: (Double) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return take(index + 1) - } - } - return emptyList() -} - -/** - * Returns a list containing all elements except last elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun BooleanArray.dropLastWhile(predicate: (Boolean) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return take(index + 1) - } - } - return emptyList() -} - -/** - * Returns a list containing all elements except last elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun CharArray.dropLastWhile(predicate: (Char) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return take(index + 1) - } - } - return emptyList() -} - -/** - * Returns a list containing all elements except first elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun Array.dropWhile(predicate: (T) -> Boolean): List { - var yielding = false - val list = ArrayList() - for (item in this) - if (yielding) - list.add(item) - else if (!predicate(item)) { - list.add(item) - yielding = true - } - return list -} - -/** - * Returns a list containing all elements except first elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun ByteArray.dropWhile(predicate: (Byte) -> Boolean): List { - var yielding = false - val list = ArrayList() - for (item in this) - if (yielding) - list.add(item) - else if (!predicate(item)) { - list.add(item) - yielding = true - } - return list -} - -/** - * Returns a list containing all elements except first elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun ShortArray.dropWhile(predicate: (Short) -> Boolean): List { - var yielding = false - val list = ArrayList() - for (item in this) - if (yielding) - list.add(item) - else if (!predicate(item)) { - list.add(item) - yielding = true - } - return list -} - -/** - * Returns a list containing all elements except first elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun IntArray.dropWhile(predicate: (Int) -> Boolean): List { - var yielding = false - val list = ArrayList() - for (item in this) - if (yielding) - list.add(item) - else if (!predicate(item)) { - list.add(item) - yielding = true - } - return list -} - -/** - * Returns a list containing all elements except first elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun LongArray.dropWhile(predicate: (Long) -> Boolean): List { - var yielding = false - val list = ArrayList() - for (item in this) - if (yielding) - list.add(item) - else if (!predicate(item)) { - list.add(item) - yielding = true - } - return list -} - -/** - * Returns a list containing all elements except first elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun FloatArray.dropWhile(predicate: (Float) -> Boolean): List { - var yielding = false - val list = ArrayList() - for (item in this) - if (yielding) - list.add(item) - else if (!predicate(item)) { - list.add(item) - yielding = true - } - return list -} - -/** - * Returns a list containing all elements except first elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun DoubleArray.dropWhile(predicate: (Double) -> Boolean): List { - var yielding = false - val list = ArrayList() - for (item in this) - if (yielding) - list.add(item) - else if (!predicate(item)) { - list.add(item) - yielding = true - } - return list -} - -/** - * Returns a list containing all elements except first elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun BooleanArray.dropWhile(predicate: (Boolean) -> Boolean): List { - var yielding = false - val list = ArrayList() - for (item in this) - if (yielding) - list.add(item) - else if (!predicate(item)) { - list.add(item) - yielding = true - } - return list -} - -/** - * Returns a list containing all elements except first elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun CharArray.dropWhile(predicate: (Char) -> Boolean): List { - var yielding = false - val list = ArrayList() - for (item in this) - if (yielding) - list.add(item) - else if (!predicate(item)) { - list.add(item) - yielding = true - } - return list -} - -/** - * Returns a list containing only elements matching the given [predicate]. - */ -public inline fun Array.filter(predicate: (T) -> Boolean): List { - return filterTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - */ -public inline fun ByteArray.filter(predicate: (Byte) -> Boolean): List { - return filterTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - */ -public inline fun ShortArray.filter(predicate: (Short) -> Boolean): List { - return filterTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - */ -public inline fun IntArray.filter(predicate: (Int) -> Boolean): List { - return filterTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - */ -public inline fun LongArray.filter(predicate: (Long) -> Boolean): List { - return filterTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - */ -public inline fun FloatArray.filter(predicate: (Float) -> Boolean): List { - return filterTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - */ -public inline fun DoubleArray.filter(predicate: (Double) -> Boolean): List { - return filterTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - */ -public inline fun BooleanArray.filter(predicate: (Boolean) -> Boolean): List { - return filterTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - */ -public inline fun CharArray.filter(predicate: (Char) -> Boolean): List { - return filterTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun Array.filterIndexed(predicate: (index: Int, T) -> Boolean): List { - return filterIndexedTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun ByteArray.filterIndexed(predicate: (index: Int, Byte) -> Boolean): List { - return filterIndexedTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun ShortArray.filterIndexed(predicate: (index: Int, Short) -> Boolean): List { - return filterIndexedTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun IntArray.filterIndexed(predicate: (index: Int, Int) -> Boolean): List { - return filterIndexedTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun LongArray.filterIndexed(predicate: (index: Int, Long) -> Boolean): List { - return filterIndexedTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun FloatArray.filterIndexed(predicate: (index: Int, Float) -> Boolean): List { - return filterIndexedTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun DoubleArray.filterIndexed(predicate: (index: Int, Double) -> Boolean): List { - return filterIndexedTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun BooleanArray.filterIndexed(predicate: (index: Int, Boolean) -> Boolean): List { - return filterIndexedTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun CharArray.filterIndexed(predicate: (index: Int, Char) -> Boolean): List { - return filterIndexedTo(ArrayList(), predicate) -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun > Array.filterIndexedTo(destination: C, predicate: (index: Int, T) -> Boolean): C { - forEachIndexed { index, element -> - if (predicate(index, element)) destination.add(element) - } - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun > ByteArray.filterIndexedTo(destination: C, predicate: (index: Int, Byte) -> Boolean): C { - forEachIndexed { index, element -> - if (predicate(index, element)) destination.add(element) - } - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun > ShortArray.filterIndexedTo(destination: C, predicate: (index: Int, Short) -> Boolean): C { - forEachIndexed { index, element -> - if (predicate(index, element)) destination.add(element) - } - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun > IntArray.filterIndexedTo(destination: C, predicate: (index: Int, Int) -> Boolean): C { - forEachIndexed { index, element -> - if (predicate(index, element)) destination.add(element) - } - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun > LongArray.filterIndexedTo(destination: C, predicate: (index: Int, Long) -> Boolean): C { - forEachIndexed { index, element -> - if (predicate(index, element)) destination.add(element) - } - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun > FloatArray.filterIndexedTo(destination: C, predicate: (index: Int, Float) -> Boolean): C { - forEachIndexed { index, element -> - if (predicate(index, element)) destination.add(element) - } - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun > DoubleArray.filterIndexedTo(destination: C, predicate: (index: Int, Double) -> Boolean): C { - forEachIndexed { index, element -> - if (predicate(index, element)) destination.add(element) - } - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun > BooleanArray.filterIndexedTo(destination: C, predicate: (index: Int, Boolean) -> Boolean): C { - forEachIndexed { index, element -> - if (predicate(index, element)) destination.add(element) - } - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun > CharArray.filterIndexedTo(destination: C, predicate: (index: Int, Char) -> Boolean): C { - forEachIndexed { index, element -> - if (predicate(index, element)) destination.add(element) - } - return destination -} - -/** - * Returns a list containing all elements that are instances of specified type parameter R. - */ -public inline fun Array<*>.filterIsInstance(): List<@kotlin.internal.NoInfer R> { - return filterIsInstanceTo(ArrayList()) -} - -/** - * Returns a list containing all elements that are instances of specified class. - */ -public fun Array<*>.filterIsInstance(klass: Class): List { - return filterIsInstanceTo(ArrayList(), klass) -} - -/** - * Appends all elements that are instances of specified type parameter R to the given [destination]. - */ -public inline fun > Array<*>.filterIsInstanceTo(destination: C): C { - for (element in this) if (element is R) destination.add(element) - return destination -} - -/** - * Appends all elements that are instances of specified class to the given [destination]. - */ -public fun , R> Array<*>.filterIsInstanceTo(destination: C, klass: Class): C { - @Suppress("UNCHECKED_CAST") - for (element in this) if (klass.isInstance(element)) destination.add(element as R) - return destination -} - -/** - * Returns a list containing all elements not matching the given [predicate]. - */ -public inline fun Array.filterNot(predicate: (T) -> Boolean): List { - return filterNotTo(ArrayList(), predicate) -} - -/** - * Returns a list containing all elements not matching the given [predicate]. - */ -public inline fun ByteArray.filterNot(predicate: (Byte) -> Boolean): List { - return filterNotTo(ArrayList(), predicate) -} - -/** - * Returns a list containing all elements not matching the given [predicate]. - */ -public inline fun ShortArray.filterNot(predicate: (Short) -> Boolean): List { - return filterNotTo(ArrayList(), predicate) -} - -/** - * Returns a list containing all elements not matching the given [predicate]. - */ -public inline fun IntArray.filterNot(predicate: (Int) -> Boolean): List { - return filterNotTo(ArrayList(), predicate) -} - -/** - * Returns a list containing all elements not matching the given [predicate]. - */ -public inline fun LongArray.filterNot(predicate: (Long) -> Boolean): List { - return filterNotTo(ArrayList(), predicate) -} - -/** - * Returns a list containing all elements not matching the given [predicate]. - */ -public inline fun FloatArray.filterNot(predicate: (Float) -> Boolean): List { - return filterNotTo(ArrayList(), predicate) -} - -/** - * Returns a list containing all elements not matching the given [predicate]. - */ -public inline fun DoubleArray.filterNot(predicate: (Double) -> Boolean): List { - return filterNotTo(ArrayList(), predicate) -} - -/** - * Returns a list containing all elements not matching the given [predicate]. - */ -public inline fun BooleanArray.filterNot(predicate: (Boolean) -> Boolean): List { - return filterNotTo(ArrayList(), predicate) -} - -/** - * Returns a list containing all elements not matching the given [predicate]. - */ -public inline fun CharArray.filterNot(predicate: (Char) -> Boolean): List { - return filterNotTo(ArrayList(), predicate) -} - -/** - * Returns a list containing all elements that are not `null`. - */ -public fun Array.filterNotNull(): List { - return filterNotNullTo(ArrayList()) -} - -/** - * Appends all elements that are not `null` to the given [destination]. - */ -public fun , T : Any> Array.filterNotNullTo(destination: C): C { - for (element in this) if (element != null) destination.add(element) - return destination -} - -/** - * Appends all elements not matching the given [predicate] to the given [destination]. - */ -public inline fun > Array.filterNotTo(destination: C, predicate: (T) -> Boolean): C { - for (element in this) if (!predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements not matching the given [predicate] to the given [destination]. - */ -public inline fun > ByteArray.filterNotTo(destination: C, predicate: (Byte) -> Boolean): C { - for (element in this) if (!predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements not matching the given [predicate] to the given [destination]. - */ -public inline fun > ShortArray.filterNotTo(destination: C, predicate: (Short) -> Boolean): C { - for (element in this) if (!predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements not matching the given [predicate] to the given [destination]. - */ -public inline fun > IntArray.filterNotTo(destination: C, predicate: (Int) -> Boolean): C { - for (element in this) if (!predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements not matching the given [predicate] to the given [destination]. - */ -public inline fun > LongArray.filterNotTo(destination: C, predicate: (Long) -> Boolean): C { - for (element in this) if (!predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements not matching the given [predicate] to the given [destination]. - */ -public inline fun > FloatArray.filterNotTo(destination: C, predicate: (Float) -> Boolean): C { - for (element in this) if (!predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements not matching the given [predicate] to the given [destination]. - */ -public inline fun > DoubleArray.filterNotTo(destination: C, predicate: (Double) -> Boolean): C { - for (element in this) if (!predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements not matching the given [predicate] to the given [destination]. - */ -public inline fun > BooleanArray.filterNotTo(destination: C, predicate: (Boolean) -> Boolean): C { - for (element in this) if (!predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements not matching the given [predicate] to the given [destination]. - */ -public inline fun > CharArray.filterNotTo(destination: C, predicate: (Char) -> Boolean): C { - for (element in this) if (!predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - */ -public inline fun > Array.filterTo(destination: C, predicate: (T) -> Boolean): C { - for (element in this) if (predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - */ -public inline fun > ByteArray.filterTo(destination: C, predicate: (Byte) -> Boolean): C { - for (element in this) if (predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - */ -public inline fun > ShortArray.filterTo(destination: C, predicate: (Short) -> Boolean): C { - for (element in this) if (predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - */ -public inline fun > IntArray.filterTo(destination: C, predicate: (Int) -> Boolean): C { - for (element in this) if (predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - */ -public inline fun > LongArray.filterTo(destination: C, predicate: (Long) -> Boolean): C { - for (element in this) if (predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - */ -public inline fun > FloatArray.filterTo(destination: C, predicate: (Float) -> Boolean): C { - for (element in this) if (predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - */ -public inline fun > DoubleArray.filterTo(destination: C, predicate: (Double) -> Boolean): C { - for (element in this) if (predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - */ -public inline fun > BooleanArray.filterTo(destination: C, predicate: (Boolean) -> Boolean): C { - for (element in this) if (predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - */ -public inline fun > CharArray.filterTo(destination: C, predicate: (Char) -> Boolean): C { - for (element in this) if (predicate(element)) destination.add(element) - return destination -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun Array.slice(indices: IntRange): List { - if (indices.isEmpty()) return listOf() - return copyOfRange(indices.start, indices.endInclusive + 1).asList() -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun ByteArray.slice(indices: IntRange): List { - if (indices.isEmpty()) return listOf() - return copyOfRange(indices.start, indices.endInclusive + 1).asList() -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun ShortArray.slice(indices: IntRange): List { - if (indices.isEmpty()) return listOf() - return copyOfRange(indices.start, indices.endInclusive + 1).asList() -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun IntArray.slice(indices: IntRange): List { - if (indices.isEmpty()) return listOf() - return copyOfRange(indices.start, indices.endInclusive + 1).asList() -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun LongArray.slice(indices: IntRange): List { - if (indices.isEmpty()) return listOf() - return copyOfRange(indices.start, indices.endInclusive + 1).asList() -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun FloatArray.slice(indices: IntRange): List { - if (indices.isEmpty()) return listOf() - return copyOfRange(indices.start, indices.endInclusive + 1).asList() -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun DoubleArray.slice(indices: IntRange): List { - if (indices.isEmpty()) return listOf() - return copyOfRange(indices.start, indices.endInclusive + 1).asList() -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun BooleanArray.slice(indices: IntRange): List { - if (indices.isEmpty()) return listOf() - return copyOfRange(indices.start, indices.endInclusive + 1).asList() -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun CharArray.slice(indices: IntRange): List { - if (indices.isEmpty()) return listOf() - return copyOfRange(indices.start, indices.endInclusive + 1).asList() -} - -/** - * Returns a list containing elements at specified [indices]. - */ -public fun Array.slice(indices: Iterable): List { - val size = indices.collectionSizeOrDefault(10) - if (size == 0) return emptyList() - val list = ArrayList(size) - for (index in indices) { - list.add(get(index)) - } - return list -} - -/** - * Returns a list containing elements at specified [indices]. - */ -public fun ByteArray.slice(indices: Iterable): List { - val size = indices.collectionSizeOrDefault(10) - if (size == 0) return emptyList() - val list = ArrayList(size) - for (index in indices) { - list.add(get(index)) - } - return list -} - -/** - * Returns a list containing elements at specified [indices]. - */ -public fun ShortArray.slice(indices: Iterable): List { - val size = indices.collectionSizeOrDefault(10) - if (size == 0) return emptyList() - val list = ArrayList(size) - for (index in indices) { - list.add(get(index)) - } - return list -} - -/** - * Returns a list containing elements at specified [indices]. - */ -public fun IntArray.slice(indices: Iterable): List { - val size = indices.collectionSizeOrDefault(10) - if (size == 0) return emptyList() - val list = ArrayList(size) - for (index in indices) { - list.add(get(index)) - } - return list -} - -/** - * Returns a list containing elements at specified [indices]. - */ -public fun LongArray.slice(indices: Iterable): List { - val size = indices.collectionSizeOrDefault(10) - if (size == 0) return emptyList() - val list = ArrayList(size) - for (index in indices) { - list.add(get(index)) - } - return list -} - -/** - * Returns a list containing elements at specified [indices]. - */ -public fun FloatArray.slice(indices: Iterable): List { - val size = indices.collectionSizeOrDefault(10) - if (size == 0) return emptyList() - val list = ArrayList(size) - for (index in indices) { - list.add(get(index)) - } - return list -} - -/** - * Returns a list containing elements at specified [indices]. - */ -public fun DoubleArray.slice(indices: Iterable): List { - val size = indices.collectionSizeOrDefault(10) - if (size == 0) return emptyList() - val list = ArrayList(size) - for (index in indices) { - list.add(get(index)) - } - return list -} - -/** - * Returns a list containing elements at specified [indices]. - */ -public fun BooleanArray.slice(indices: Iterable): List { - val size = indices.collectionSizeOrDefault(10) - if (size == 0) return emptyList() - val list = ArrayList(size) - for (index in indices) { - list.add(get(index)) - } - return list -} - -/** - * Returns a list containing elements at specified [indices]. - */ -public fun CharArray.slice(indices: Iterable): List { - val size = indices.collectionSizeOrDefault(10) - if (size == 0) return emptyList() - val list = ArrayList(size) - for (index in indices) { - list.add(get(index)) - } - return list -} - -/** - * Returns an array containing elements of this array at specified [indices]. - */ -public fun Array.sliceArray(indices: Collection): Array { - val result = arrayOfNulls(this, indices.size) - var targetIndex = 0 - for (sourceIndex in indices) { - result[targetIndex++] = this[sourceIndex] - } - return result -} - -/** - * Returns an array containing elements of this array at specified [indices]. - */ -public fun ByteArray.sliceArray(indices: Collection): ByteArray { - val result = ByteArray(indices.size) - var targetIndex = 0 - for (sourceIndex in indices) { - result[targetIndex++] = this[sourceIndex] - } - return result -} - -/** - * Returns an array containing elements of this array at specified [indices]. - */ -public fun ShortArray.sliceArray(indices: Collection): ShortArray { - val result = ShortArray(indices.size) - var targetIndex = 0 - for (sourceIndex in indices) { - result[targetIndex++] = this[sourceIndex] - } - return result -} - -/** - * Returns an array containing elements of this array at specified [indices]. - */ -public fun IntArray.sliceArray(indices: Collection): IntArray { - val result = IntArray(indices.size) - var targetIndex = 0 - for (sourceIndex in indices) { - result[targetIndex++] = this[sourceIndex] - } - return result -} - -/** - * Returns an array containing elements of this array at specified [indices]. - */ -public fun LongArray.sliceArray(indices: Collection): LongArray { - val result = LongArray(indices.size) - var targetIndex = 0 - for (sourceIndex in indices) { - result[targetIndex++] = this[sourceIndex] - } - return result -} - -/** - * Returns an array containing elements of this array at specified [indices]. - */ -public fun FloatArray.sliceArray(indices: Collection): FloatArray { - val result = FloatArray(indices.size) - var targetIndex = 0 - for (sourceIndex in indices) { - result[targetIndex++] = this[sourceIndex] - } - return result -} - -/** - * Returns an array containing elements of this array at specified [indices]. - */ -public fun DoubleArray.sliceArray(indices: Collection): DoubleArray { - val result = DoubleArray(indices.size) - var targetIndex = 0 - for (sourceIndex in indices) { - result[targetIndex++] = this[sourceIndex] - } - return result -} - -/** - * Returns an array containing elements of this array at specified [indices]. - */ -public fun BooleanArray.sliceArray(indices: Collection): BooleanArray { - val result = BooleanArray(indices.size) - var targetIndex = 0 - for (sourceIndex in indices) { - result[targetIndex++] = this[sourceIndex] - } - return result -} - -/** - * Returns an array containing elements of this array at specified [indices]. - */ -public fun CharArray.sliceArray(indices: Collection): CharArray { - val result = CharArray(indices.size) - var targetIndex = 0 - for (sourceIndex in indices) { - result[targetIndex++] = this[sourceIndex] - } - return result -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun Array.sliceArray(indices: IntRange): Array { - if (indices.isEmpty()) return copyOfRange(0, 0) - return copyOfRange(indices.start, indices.endInclusive + 1) -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun ByteArray.sliceArray(indices: IntRange): ByteArray { - if (indices.isEmpty()) return ByteArray(0) - return copyOfRange(indices.start, indices.endInclusive + 1) -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun ShortArray.sliceArray(indices: IntRange): ShortArray { - if (indices.isEmpty()) return ShortArray(0) - return copyOfRange(indices.start, indices.endInclusive + 1) -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun IntArray.sliceArray(indices: IntRange): IntArray { - if (indices.isEmpty()) return IntArray(0) - return copyOfRange(indices.start, indices.endInclusive + 1) -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun LongArray.sliceArray(indices: IntRange): LongArray { - if (indices.isEmpty()) return LongArray(0) - return copyOfRange(indices.start, indices.endInclusive + 1) -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun FloatArray.sliceArray(indices: IntRange): FloatArray { - if (indices.isEmpty()) return FloatArray(0) - return copyOfRange(indices.start, indices.endInclusive + 1) -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun DoubleArray.sliceArray(indices: IntRange): DoubleArray { - if (indices.isEmpty()) return DoubleArray(0) - return copyOfRange(indices.start, indices.endInclusive + 1) -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun BooleanArray.sliceArray(indices: IntRange): BooleanArray { - if (indices.isEmpty()) return BooleanArray(0) - return copyOfRange(indices.start, indices.endInclusive + 1) -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun CharArray.sliceArray(indices: IntRange): CharArray { - if (indices.isEmpty()) return CharArray(0) - return copyOfRange(indices.start, indices.endInclusive + 1) -} - -/** - * Returns a list containing first [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun Array.take(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - if (n >= size) return toList() - if (n == 1) return listOf(this[0]) - var count = 0 - val list = ArrayList(n) - for (item in this) { - if (count++ == n) - break - list.add(item) - } - return list -} - -/** - * Returns a list containing first [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun ByteArray.take(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - if (n >= size) return toList() - if (n == 1) return listOf(this[0]) - var count = 0 - val list = ArrayList(n) - for (item in this) { - if (count++ == n) - break - list.add(item) - } - return list -} - -/** - * Returns a list containing first [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun ShortArray.take(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - if (n >= size) return toList() - if (n == 1) return listOf(this[0]) - var count = 0 - val list = ArrayList(n) - for (item in this) { - if (count++ == n) - break - list.add(item) - } - return list -} - -/** - * Returns a list containing first [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun IntArray.take(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - if (n >= size) return toList() - if (n == 1) return listOf(this[0]) - var count = 0 - val list = ArrayList(n) - for (item in this) { - if (count++ == n) - break - list.add(item) - } - return list -} - -/** - * Returns a list containing first [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun LongArray.take(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - if (n >= size) return toList() - if (n == 1) return listOf(this[0]) - var count = 0 - val list = ArrayList(n) - for (item in this) { - if (count++ == n) - break - list.add(item) - } - return list -} - -/** - * Returns a list containing first [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun FloatArray.take(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - if (n >= size) return toList() - if (n == 1) return listOf(this[0]) - var count = 0 - val list = ArrayList(n) - for (item in this) { - if (count++ == n) - break - list.add(item) - } - return list -} - -/** - * Returns a list containing first [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun DoubleArray.take(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - if (n >= size) return toList() - if (n == 1) return listOf(this[0]) - var count = 0 - val list = ArrayList(n) - for (item in this) { - if (count++ == n) - break - list.add(item) - } - return list -} - -/** - * Returns a list containing first [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun BooleanArray.take(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - if (n >= size) return toList() - if (n == 1) return listOf(this[0]) - var count = 0 - val list = ArrayList(n) - for (item in this) { - if (count++ == n) - break - list.add(item) - } - return list -} - -/** - * Returns a list containing first [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun CharArray.take(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - if (n >= size) return toList() - if (n == 1) return listOf(this[0]) - var count = 0 - val list = ArrayList(n) - for (item in this) { - if (count++ == n) - break - list.add(item) - } - return list -} - -/** - * Returns a list containing last [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun Array.takeLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - val size = size - if (n >= size) return toList() - if (n == 1) return listOf(this[size - 1]) - val list = ArrayList(n) - for (index in size - n until size) - list.add(this[index]) - return list -} - -/** - * Returns a list containing last [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun ByteArray.takeLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - val size = size - if (n >= size) return toList() - if (n == 1) return listOf(this[size - 1]) - val list = ArrayList(n) - for (index in size - n until size) - list.add(this[index]) - return list -} - -/** - * Returns a list containing last [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun ShortArray.takeLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - val size = size - if (n >= size) return toList() - if (n == 1) return listOf(this[size - 1]) - val list = ArrayList(n) - for (index in size - n until size) - list.add(this[index]) - return list -} - -/** - * Returns a list containing last [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun IntArray.takeLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - val size = size - if (n >= size) return toList() - if (n == 1) return listOf(this[size - 1]) - val list = ArrayList(n) - for (index in size - n until size) - list.add(this[index]) - return list -} - -/** - * Returns a list containing last [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun LongArray.takeLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - val size = size - if (n >= size) return toList() - if (n == 1) return listOf(this[size - 1]) - val list = ArrayList(n) - for (index in size - n until size) - list.add(this[index]) - return list -} - -/** - * Returns a list containing last [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun FloatArray.takeLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - val size = size - if (n >= size) return toList() - if (n == 1) return listOf(this[size - 1]) - val list = ArrayList(n) - for (index in size - n until size) - list.add(this[index]) - return list -} - -/** - * Returns a list containing last [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun DoubleArray.takeLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - val size = size - if (n >= size) return toList() - if (n == 1) return listOf(this[size - 1]) - val list = ArrayList(n) - for (index in size - n until size) - list.add(this[index]) - return list -} - -/** - * Returns a list containing last [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun BooleanArray.takeLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - val size = size - if (n >= size) return toList() - if (n == 1) return listOf(this[size - 1]) - val list = ArrayList(n) - for (index in size - n until size) - list.add(this[index]) - return list -} - -/** - * Returns a list containing last [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun CharArray.takeLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - val size = size - if (n >= size) return toList() - if (n == 1) return listOf(this[size - 1]) - val list = ArrayList(n) - for (index in size - n until size) - list.add(this[index]) - return list -} - -/** - * Returns a list containing last elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun Array.takeLastWhile(predicate: (T) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return drop(index + 1) - } - } - return toList() -} - -/** - * Returns a list containing last elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun ByteArray.takeLastWhile(predicate: (Byte) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return drop(index + 1) - } - } - return toList() -} - -/** - * Returns a list containing last elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun ShortArray.takeLastWhile(predicate: (Short) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return drop(index + 1) - } - } - return toList() -} - -/** - * Returns a list containing last elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun IntArray.takeLastWhile(predicate: (Int) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return drop(index + 1) - } - } - return toList() -} - -/** - * Returns a list containing last elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun LongArray.takeLastWhile(predicate: (Long) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return drop(index + 1) - } - } - return toList() -} - -/** - * Returns a list containing last elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun FloatArray.takeLastWhile(predicate: (Float) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return drop(index + 1) - } - } - return toList() -} - -/** - * Returns a list containing last elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun DoubleArray.takeLastWhile(predicate: (Double) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return drop(index + 1) - } - } - return toList() -} - -/** - * Returns a list containing last elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun BooleanArray.takeLastWhile(predicate: (Boolean) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return drop(index + 1) - } - } - return toList() -} - -/** - * Returns a list containing last elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun CharArray.takeLastWhile(predicate: (Char) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return drop(index + 1) - } - } - return toList() -} - -/** - * Returns a list containing first elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun Array.takeWhile(predicate: (T) -> Boolean): List { - val list = ArrayList() - for (item in this) { - if (!predicate(item)) - break - list.add(item) - } - return list -} - -/** - * Returns a list containing first elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun ByteArray.takeWhile(predicate: (Byte) -> Boolean): List { - val list = ArrayList() - for (item in this) { - if (!predicate(item)) - break - list.add(item) - } - return list -} - -/** - * Returns a list containing first elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun ShortArray.takeWhile(predicate: (Short) -> Boolean): List { - val list = ArrayList() - for (item in this) { - if (!predicate(item)) - break - list.add(item) - } - return list -} - -/** - * Returns a list containing first elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun IntArray.takeWhile(predicate: (Int) -> Boolean): List { - val list = ArrayList() - for (item in this) { - if (!predicate(item)) - break - list.add(item) - } - return list -} - -/** - * Returns a list containing first elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun LongArray.takeWhile(predicate: (Long) -> Boolean): List { - val list = ArrayList() - for (item in this) { - if (!predicate(item)) - break - list.add(item) - } - return list -} - -/** - * Returns a list containing first elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun FloatArray.takeWhile(predicate: (Float) -> Boolean): List { - val list = ArrayList() - for (item in this) { - if (!predicate(item)) - break - list.add(item) - } - return list -} - -/** - * Returns a list containing first elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun DoubleArray.takeWhile(predicate: (Double) -> Boolean): List { - val list = ArrayList() - for (item in this) { - if (!predicate(item)) - break - list.add(item) - } - return list -} - -/** - * Returns a list containing first elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun BooleanArray.takeWhile(predicate: (Boolean) -> Boolean): List { - val list = ArrayList() - for (item in this) { - if (!predicate(item)) - break - list.add(item) - } - return list -} - -/** - * Returns a list containing first elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun CharArray.takeWhile(predicate: (Char) -> Boolean): List { - val list = ArrayList() - for (item in this) { - if (!predicate(item)) - break - list.add(item) - } - return list -} - -/** - * Reverses elements in the array in-place. - */ -public fun Array.reverse(): Unit { - val midPoint = (size / 2) - 1 - if (midPoint < 0) return - var reverseIndex = lastIndex - for (index in 0..midPoint) { - val tmp = this[index] - this[index] = this[reverseIndex] - this[reverseIndex] = tmp - reverseIndex-- - } -} - -/** - * Reverses elements in the array in-place. - */ -public fun ByteArray.reverse(): Unit { - val midPoint = (size / 2) - 1 - if (midPoint < 0) return - var reverseIndex = lastIndex - for (index in 0..midPoint) { - val tmp = this[index] - this[index] = this[reverseIndex] - this[reverseIndex] = tmp - reverseIndex-- - } -} - -/** - * Reverses elements in the array in-place. - */ -public fun ShortArray.reverse(): Unit { - val midPoint = (size / 2) - 1 - if (midPoint < 0) return - var reverseIndex = lastIndex - for (index in 0..midPoint) { - val tmp = this[index] - this[index] = this[reverseIndex] - this[reverseIndex] = tmp - reverseIndex-- - } -} - -/** - * Reverses elements in the array in-place. - */ -public fun IntArray.reverse(): Unit { - val midPoint = (size / 2) - 1 - if (midPoint < 0) return - var reverseIndex = lastIndex - for (index in 0..midPoint) { - val tmp = this[index] - this[index] = this[reverseIndex] - this[reverseIndex] = tmp - reverseIndex-- - } -} - -/** - * Reverses elements in the array in-place. - */ -public fun LongArray.reverse(): Unit { - val midPoint = (size / 2) - 1 - if (midPoint < 0) return - var reverseIndex = lastIndex - for (index in 0..midPoint) { - val tmp = this[index] - this[index] = this[reverseIndex] - this[reverseIndex] = tmp - reverseIndex-- - } -} - -/** - * Reverses elements in the array in-place. - */ -public fun FloatArray.reverse(): Unit { - val midPoint = (size / 2) - 1 - if (midPoint < 0) return - var reverseIndex = lastIndex - for (index in 0..midPoint) { - val tmp = this[index] - this[index] = this[reverseIndex] - this[reverseIndex] = tmp - reverseIndex-- - } -} - -/** - * Reverses elements in the array in-place. - */ -public fun DoubleArray.reverse(): Unit { - val midPoint = (size / 2) - 1 - if (midPoint < 0) return - var reverseIndex = lastIndex - for (index in 0..midPoint) { - val tmp = this[index] - this[index] = this[reverseIndex] - this[reverseIndex] = tmp - reverseIndex-- - } -} - -/** - * Reverses elements in the array in-place. - */ -public fun BooleanArray.reverse(): Unit { - val midPoint = (size / 2) - 1 - if (midPoint < 0) return - var reverseIndex = lastIndex - for (index in 0..midPoint) { - val tmp = this[index] - this[index] = this[reverseIndex] - this[reverseIndex] = tmp - reverseIndex-- - } -} - -/** - * Reverses elements in the array in-place. - */ -public fun CharArray.reverse(): Unit { - val midPoint = (size / 2) - 1 - if (midPoint < 0) return - var reverseIndex = lastIndex - for (index in 0..midPoint) { - val tmp = this[index] - this[index] = this[reverseIndex] - this[reverseIndex] = tmp - reverseIndex-- - } -} - -/** - * Returns a list with elements in reversed order. - */ -public fun Array.reversed(): List { - if (isEmpty()) return emptyList() - val list = toMutableList() - list.reverse() - return list -} - -/** - * Returns a list with elements in reversed order. - */ -public fun ByteArray.reversed(): List { - if (isEmpty()) return emptyList() - val list = toMutableList() - list.reverse() - return list -} - -/** - * Returns a list with elements in reversed order. - */ -public fun ShortArray.reversed(): List { - if (isEmpty()) return emptyList() - val list = toMutableList() - list.reverse() - return list -} - -/** - * Returns a list with elements in reversed order. - */ -public fun IntArray.reversed(): List { - if (isEmpty()) return emptyList() - val list = toMutableList() - list.reverse() - return list -} - -/** - * Returns a list with elements in reversed order. - */ -public fun LongArray.reversed(): List { - if (isEmpty()) return emptyList() - val list = toMutableList() - list.reverse() - return list -} - -/** - * Returns a list with elements in reversed order. - */ -public fun FloatArray.reversed(): List { - if (isEmpty()) return emptyList() - val list = toMutableList() - list.reverse() - return list -} - -/** - * Returns a list with elements in reversed order. - */ -public fun DoubleArray.reversed(): List { - if (isEmpty()) return emptyList() - val list = toMutableList() - list.reverse() - return list -} - -/** - * Returns a list with elements in reversed order. - */ -public fun BooleanArray.reversed(): List { - if (isEmpty()) return emptyList() - val list = toMutableList() - list.reverse() - return list -} - -/** - * Returns a list with elements in reversed order. - */ -public fun CharArray.reversed(): List { - if (isEmpty()) return emptyList() - val list = toMutableList() - list.reverse() - return list -} - -/** - * Returns an array with elements of this array in reversed order. - */ -public fun Array.reversedArray(): Array { - if (isEmpty()) return this - val result = arrayOfNulls(this, size) - val lastIndex = lastIndex - for (i in 0..lastIndex) - result[lastIndex - i] = this[i] - return result -} - -/** - * Returns an array with elements of this array in reversed order. - */ -public fun ByteArray.reversedArray(): ByteArray { - if (isEmpty()) return this - val result = ByteArray(size) - val lastIndex = lastIndex - for (i in 0..lastIndex) - result[lastIndex - i] = this[i] - return result -} - -/** - * Returns an array with elements of this array in reversed order. - */ -public fun ShortArray.reversedArray(): ShortArray { - if (isEmpty()) return this - val result = ShortArray(size) - val lastIndex = lastIndex - for (i in 0..lastIndex) - result[lastIndex - i] = this[i] - return result -} - -/** - * Returns an array with elements of this array in reversed order. - */ -public fun IntArray.reversedArray(): IntArray { - if (isEmpty()) return this - val result = IntArray(size) - val lastIndex = lastIndex - for (i in 0..lastIndex) - result[lastIndex - i] = this[i] - return result -} - -/** - * Returns an array with elements of this array in reversed order. - */ -public fun LongArray.reversedArray(): LongArray { - if (isEmpty()) return this - val result = LongArray(size) - val lastIndex = lastIndex - for (i in 0..lastIndex) - result[lastIndex - i] = this[i] - return result -} - -/** - * Returns an array with elements of this array in reversed order. - */ -public fun FloatArray.reversedArray(): FloatArray { - if (isEmpty()) return this - val result = FloatArray(size) - val lastIndex = lastIndex - for (i in 0..lastIndex) - result[lastIndex - i] = this[i] - return result -} - -/** - * Returns an array with elements of this array in reversed order. - */ -public fun DoubleArray.reversedArray(): DoubleArray { - if (isEmpty()) return this - val result = DoubleArray(size) - val lastIndex = lastIndex - for (i in 0..lastIndex) - result[lastIndex - i] = this[i] - return result -} - -/** - * Returns an array with elements of this array in reversed order. - */ -public fun BooleanArray.reversedArray(): BooleanArray { - if (isEmpty()) return this - val result = BooleanArray(size) - val lastIndex = lastIndex - for (i in 0..lastIndex) - result[lastIndex - i] = this[i] - return result -} - -/** - * Returns an array with elements of this array in reversed order. - */ -public fun CharArray.reversedArray(): CharArray { - if (isEmpty()) return this - val result = CharArray(size) - val lastIndex = lastIndex - for (i in 0..lastIndex) - result[lastIndex - i] = this[i] - return result -} - -/** - * Sorts elements in the array in-place according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > Array.sortBy(crossinline selector: (T) -> R?): Unit { - if (size > 1) sortWith(compareBy(selector)) -} - -/** - * Sorts elements in the array in-place descending according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > Array.sortByDescending(crossinline selector: (T) -> R?): Unit { - if (size > 1) sortWith(compareByDescending(selector)) -} - -/** - * Sorts elements in the array in-place descending according to their natural sort order. - */ -public fun > Array.sortDescending(): Unit { - sortWith(reverseOrder()) -} - -/** - * Sorts elements in the array in-place descending according to their natural sort order. - */ -public fun ByteArray.sortDescending(): Unit { - if (size > 1) { - sort() - reverse() - } -} - -/** - * Sorts elements in the array in-place descending according to their natural sort order. - */ -public fun ShortArray.sortDescending(): Unit { - if (size > 1) { - sort() - reverse() - } -} - -/** - * Sorts elements in the array in-place descending according to their natural sort order. - */ -public fun IntArray.sortDescending(): Unit { - if (size > 1) { - sort() - reverse() - } -} - -/** - * Sorts elements in the array in-place descending according to their natural sort order. - */ -public fun LongArray.sortDescending(): Unit { - if (size > 1) { - sort() - reverse() - } -} - -/** - * Sorts elements in the array in-place descending according to their natural sort order. - */ -public fun FloatArray.sortDescending(): Unit { - if (size > 1) { - sort() - reverse() - } -} - -/** - * Sorts elements in the array in-place descending according to their natural sort order. - */ -public fun DoubleArray.sortDescending(): Unit { - if (size > 1) { - sort() - reverse() - } -} - -/** - * Sorts elements in the array in-place descending according to their natural sort order. - */ -public fun CharArray.sortDescending(): Unit { - if (size > 1) { - sort() - reverse() - } -} - -/** - * Returns a list of all elements sorted according to their natural sort order. - */ -public fun > Array.sorted(): List { - return sortedArray().asList() -} - -/** - * Returns a list of all elements sorted according to their natural sort order. - */ -public fun ByteArray.sorted(): List { - return toTypedArray().apply { sort() }.asList() -} - -/** - * Returns a list of all elements sorted according to their natural sort order. - */ -public fun ShortArray.sorted(): List { - return toTypedArray().apply { sort() }.asList() -} - -/** - * Returns a list of all elements sorted according to their natural sort order. - */ -public fun IntArray.sorted(): List { - return toTypedArray().apply { sort() }.asList() -} - -/** - * Returns a list of all elements sorted according to their natural sort order. - */ -public fun LongArray.sorted(): List { - return toTypedArray().apply { sort() }.asList() -} - -/** - * Returns a list of all elements sorted according to their natural sort order. - */ -public fun FloatArray.sorted(): List { - return toTypedArray().apply { sort() }.asList() -} - -/** - * Returns a list of all elements sorted according to their natural sort order. - */ -public fun DoubleArray.sorted(): List { - return toTypedArray().apply { sort() }.asList() -} - -/** - * Returns a list of all elements sorted according to their natural sort order. - */ -public fun CharArray.sorted(): List { - return toTypedArray().apply { sort() }.asList() -} - -/** - * Returns an array with all elements of this array sorted according to their natural sort order. - */ -public fun > Array.sortedArray(): Array { - if (isEmpty()) return this - return this.copyOf().apply { sort() } -} - -/** - * Returns an array with all elements of this array sorted according to their natural sort order. - */ -public fun ByteArray.sortedArray(): ByteArray { - if (isEmpty()) return this - return this.copyOf().apply { sort() } -} - -/** - * Returns an array with all elements of this array sorted according to their natural sort order. - */ -public fun ShortArray.sortedArray(): ShortArray { - if (isEmpty()) return this - return this.copyOf().apply { sort() } -} - -/** - * Returns an array with all elements of this array sorted according to their natural sort order. - */ -public fun IntArray.sortedArray(): IntArray { - if (isEmpty()) return this - return this.copyOf().apply { sort() } -} - -/** - * Returns an array with all elements of this array sorted according to their natural sort order. - */ -public fun LongArray.sortedArray(): LongArray { - if (isEmpty()) return this - return this.copyOf().apply { sort() } -} - -/** - * Returns an array with all elements of this array sorted according to their natural sort order. - */ -public fun FloatArray.sortedArray(): FloatArray { - if (isEmpty()) return this - return this.copyOf().apply { sort() } -} - -/** - * Returns an array with all elements of this array sorted according to their natural sort order. - */ -public fun DoubleArray.sortedArray(): DoubleArray { - if (isEmpty()) return this - return this.copyOf().apply { sort() } -} - -/** - * Returns an array with all elements of this array sorted according to their natural sort order. - */ -public fun CharArray.sortedArray(): CharArray { - if (isEmpty()) return this - return this.copyOf().apply { sort() } -} - -/** - * Returns an array with all elements of this array sorted descending according to their natural sort order. - */ -public fun > Array.sortedArrayDescending(): Array { - if (isEmpty()) return this - return this.copyOf().apply { sortWith(reverseOrder()) } -} - -/** - * Returns an array with all elements of this array sorted descending according to their natural sort order. - */ -public fun ByteArray.sortedArrayDescending(): ByteArray { - if (isEmpty()) return this - return this.copyOf().apply { sortDescending() } -} - -/** - * Returns an array with all elements of this array sorted descending according to their natural sort order. - */ -public fun ShortArray.sortedArrayDescending(): ShortArray { - if (isEmpty()) return this - return this.copyOf().apply { sortDescending() } -} - -/** - * Returns an array with all elements of this array sorted descending according to their natural sort order. - */ -public fun IntArray.sortedArrayDescending(): IntArray { - if (isEmpty()) return this - return this.copyOf().apply { sortDescending() } -} - -/** - * Returns an array with all elements of this array sorted descending according to their natural sort order. - */ -public fun LongArray.sortedArrayDescending(): LongArray { - if (isEmpty()) return this - return this.copyOf().apply { sortDescending() } -} - -/** - * Returns an array with all elements of this array sorted descending according to their natural sort order. - */ -public fun FloatArray.sortedArrayDescending(): FloatArray { - if (isEmpty()) return this - return this.copyOf().apply { sortDescending() } -} - -/** - * Returns an array with all elements of this array sorted descending according to their natural sort order. - */ -public fun DoubleArray.sortedArrayDescending(): DoubleArray { - if (isEmpty()) return this - return this.copyOf().apply { sortDescending() } -} - -/** - * Returns an array with all elements of this array sorted descending according to their natural sort order. - */ -public fun CharArray.sortedArrayDescending(): CharArray { - if (isEmpty()) return this - return this.copyOf().apply { sortDescending() } -} - -/** - * Returns an array with all elements of this array sorted according the specified [comparator]. - */ -public fun Array.sortedArrayWith(comparator: Comparator): Array { - if (isEmpty()) return this - return this.copyOf().apply { sortWith(comparator) } -} - -/** - * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > Array.sortedBy(crossinline selector: (T) -> R?): List { - return sortedWith(compareBy(selector)) -} - -/** - * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > ByteArray.sortedBy(crossinline selector: (Byte) -> R?): List { - return sortedWith(compareBy(selector)) -} - -/** - * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > ShortArray.sortedBy(crossinline selector: (Short) -> R?): List { - return sortedWith(compareBy(selector)) -} - -/** - * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > IntArray.sortedBy(crossinline selector: (Int) -> R?): List { - return sortedWith(compareBy(selector)) -} - -/** - * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > LongArray.sortedBy(crossinline selector: (Long) -> R?): List { - return sortedWith(compareBy(selector)) -} - -/** - * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > FloatArray.sortedBy(crossinline selector: (Float) -> R?): List { - return sortedWith(compareBy(selector)) -} - -/** - * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > DoubleArray.sortedBy(crossinline selector: (Double) -> R?): List { - return sortedWith(compareBy(selector)) -} - -/** - * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > BooleanArray.sortedBy(crossinline selector: (Boolean) -> R?): List { - return sortedWith(compareBy(selector)) -} - -/** - * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > CharArray.sortedBy(crossinline selector: (Char) -> R?): List { - return sortedWith(compareBy(selector)) -} - -/** - * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > Array.sortedByDescending(crossinline selector: (T) -> R?): List { - return sortedWith(compareByDescending(selector)) -} - -/** - * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > ByteArray.sortedByDescending(crossinline selector: (Byte) -> R?): List { - return sortedWith(compareByDescending(selector)) -} - -/** - * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > ShortArray.sortedByDescending(crossinline selector: (Short) -> R?): List { - return sortedWith(compareByDescending(selector)) -} - -/** - * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > IntArray.sortedByDescending(crossinline selector: (Int) -> R?): List { - return sortedWith(compareByDescending(selector)) -} - -/** - * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > LongArray.sortedByDescending(crossinline selector: (Long) -> R?): List { - return sortedWith(compareByDescending(selector)) -} - -/** - * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > FloatArray.sortedByDescending(crossinline selector: (Float) -> R?): List { - return sortedWith(compareByDescending(selector)) -} - -/** - * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > DoubleArray.sortedByDescending(crossinline selector: (Double) -> R?): List { - return sortedWith(compareByDescending(selector)) -} - -/** - * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > BooleanArray.sortedByDescending(crossinline selector: (Boolean) -> R?): List { - return sortedWith(compareByDescending(selector)) -} - -/** - * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > CharArray.sortedByDescending(crossinline selector: (Char) -> R?): List { - return sortedWith(compareByDescending(selector)) -} - -/** - * Returns a list of all elements sorted descending according to their natural sort order. - */ -public fun > Array.sortedDescending(): List { - return sortedWith(reverseOrder()) -} - -/** - * Returns a list of all elements sorted descending according to their natural sort order. - */ -public fun ByteArray.sortedDescending(): List { - return copyOf().apply { sort() }.reversed() -} - -/** - * Returns a list of all elements sorted descending according to their natural sort order. - */ -public fun ShortArray.sortedDescending(): List { - return copyOf().apply { sort() }.reversed() -} - -/** - * Returns a list of all elements sorted descending according to their natural sort order. - */ -public fun IntArray.sortedDescending(): List { - return copyOf().apply { sort() }.reversed() -} - -/** - * Returns a list of all elements sorted descending according to their natural sort order. - */ -public fun LongArray.sortedDescending(): List { - return copyOf().apply { sort() }.reversed() -} - -/** - * Returns a list of all elements sorted descending according to their natural sort order. - */ -public fun FloatArray.sortedDescending(): List { - return copyOf().apply { sort() }.reversed() -} - -/** - * Returns a list of all elements sorted descending according to their natural sort order. - */ -public fun DoubleArray.sortedDescending(): List { - return copyOf().apply { sort() }.reversed() -} - -/** - * Returns a list of all elements sorted descending according to their natural sort order. - */ -public fun CharArray.sortedDescending(): List { - return copyOf().apply { sort() }.reversed() -} - -/** - * Returns a list of all elements sorted according to the specified [comparator]. - */ -public fun Array.sortedWith(comparator: Comparator): List { - return sortedArrayWith(comparator).asList() -} - -/** - * Returns a list of all elements sorted according to the specified [comparator]. - */ -public fun ByteArray.sortedWith(comparator: Comparator): List { - return toTypedArray().apply { sortWith(comparator) }.asList() -} - -/** - * Returns a list of all elements sorted according to the specified [comparator]. - */ -public fun ShortArray.sortedWith(comparator: Comparator): List { - return toTypedArray().apply { sortWith(comparator) }.asList() -} - -/** - * Returns a list of all elements sorted according to the specified [comparator]. - */ -public fun IntArray.sortedWith(comparator: Comparator): List { - return toTypedArray().apply { sortWith(comparator) }.asList() -} - -/** - * Returns a list of all elements sorted according to the specified [comparator]. - */ -public fun LongArray.sortedWith(comparator: Comparator): List { - return toTypedArray().apply { sortWith(comparator) }.asList() -} - -/** - * Returns a list of all elements sorted according to the specified [comparator]. - */ -public fun FloatArray.sortedWith(comparator: Comparator): List { - return toTypedArray().apply { sortWith(comparator) }.asList() -} - -/** - * Returns a list of all elements sorted according to the specified [comparator]. - */ -public fun DoubleArray.sortedWith(comparator: Comparator): List { - return toTypedArray().apply { sortWith(comparator) }.asList() -} - -/** - * Returns a list of all elements sorted according to the specified [comparator]. - */ -public fun BooleanArray.sortedWith(comparator: Comparator): List { - return toTypedArray().apply { sortWith(comparator) }.asList() -} - -/** - * Returns a list of all elements sorted according to the specified [comparator]. - */ -public fun CharArray.sortedWith(comparator: Comparator): List { - return toTypedArray().apply { sortWith(comparator) }.asList() -} - -/** - * Returns a [List] that wraps the original array. - */ -public fun Array.asList(): List { - return ArraysUtilJVM.asList(this) -} - -/** - * 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) - } -} - -/** - * Searches the array or the range of the array for the provided [element] using the binary search algorithm. - * The array is expected to be sorted according to the specified [comparator], otherwise the result is undefined. - * - * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. - * - * @return the index of the element, if it is contained in the array within the specified range; - * otherwise, the inverted insertion point `(-insertion point - 1)`. - * The insertion point is defined as the index at which the element should be inserted, - * so that the array (or the specified subrange of array) still remains sorted according to the specified [comparator]. - */ -public fun Array.binarySearch(element: T, comparator: Comparator, fromIndex: Int = 0, toIndex: Int = size): Int { - return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element, comparator) -} - -/** - * Searches the array or the range of the array for the provided [element] using the binary search algorithm. - * The array is expected to be sorted, otherwise the result is undefined. - * - * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. - * - * @return the index of the element, if it is contained in the array within the specified range; - * otherwise, the inverted insertion point `(-insertion point - 1)`. - * The insertion point is defined as the index at which the element should be inserted, - * so that the array (or the specified subrange of array) still remains sorted. - */ -public fun Array.binarySearch(element: T, fromIndex: Int = 0, toIndex: Int = size): Int { - return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element) -} - -/** - * Searches the array or the range of the array for the provided [element] using the binary search algorithm. - * The array is expected to be sorted, otherwise the result is undefined. - * - * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. - * - * @return the index of the element, if it is contained in the array within the specified range; - * otherwise, the inverted insertion point `(-insertion point - 1)`. - * The insertion point is defined as the index at which the element should be inserted, - * so that the array (or the specified subrange of array) still remains sorted. - */ -public fun ByteArray.binarySearch(element: Byte, fromIndex: Int = 0, toIndex: Int = size): Int { - return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element) -} - -/** - * Searches the array or the range of the array for the provided [element] using the binary search algorithm. - * The array is expected to be sorted, otherwise the result is undefined. - * - * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. - * - * @return the index of the element, if it is contained in the array within the specified range; - * otherwise, the inverted insertion point `(-insertion point - 1)`. - * The insertion point is defined as the index at which the element should be inserted, - * so that the array (or the specified subrange of array) still remains sorted. - */ -public fun ShortArray.binarySearch(element: Short, fromIndex: Int = 0, toIndex: Int = size): Int { - return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element) -} - -/** - * Searches the array or the range of the array for the provided [element] using the binary search algorithm. - * The array is expected to be sorted, otherwise the result is undefined. - * - * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. - * - * @return the index of the element, if it is contained in the array within the specified range; - * otherwise, the inverted insertion point `(-insertion point - 1)`. - * The insertion point is defined as the index at which the element should be inserted, - * so that the array (or the specified subrange of array) still remains sorted. - */ -public fun IntArray.binarySearch(element: Int, fromIndex: Int = 0, toIndex: Int = size): Int { - return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element) -} - -/** - * Searches the array or the range of the array for the provided [element] using the binary search algorithm. - * The array is expected to be sorted, otherwise the result is undefined. - * - * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. - * - * @return the index of the element, if it is contained in the array within the specified range; - * otherwise, the inverted insertion point `(-insertion point - 1)`. - * The insertion point is defined as the index at which the element should be inserted, - * so that the array (or the specified subrange of array) still remains sorted. - */ -public fun LongArray.binarySearch(element: Long, fromIndex: Int = 0, toIndex: Int = size): Int { - return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element) -} - -/** - * Searches the array or the range of the array for the provided [element] using the binary search algorithm. - * The array is expected to be sorted, otherwise the result is undefined. - * - * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. - * - * @return the index of the element, if it is contained in the array within the specified range; - * otherwise, the inverted insertion point `(-insertion point - 1)`. - * The insertion point is defined as the index at which the element should be inserted, - * so that the array (or the specified subrange of array) still remains sorted. - */ -public fun FloatArray.binarySearch(element: Float, fromIndex: Int = 0, toIndex: Int = size): Int { - return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element) -} - -/** - * Searches the array or the range of the array for the provided [element] using the binary search algorithm. - * The array is expected to be sorted, otherwise the result is undefined. - * - * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. - * - * @return the index of the element, if it is contained in the array within the specified range; - * otherwise, the inverted insertion point `(-insertion point - 1)`. - * The insertion point is defined as the index at which the element should be inserted, - * so that the array (or the specified subrange of array) still remains sorted. - */ -public fun DoubleArray.binarySearch(element: Double, fromIndex: Int = 0, toIndex: Int = size): Int { - return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element) -} - -/** - * Searches the array or the range of the array for the provided [element] using the binary search algorithm. - * The array is expected to be sorted, otherwise the result is undefined. - * - * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. - * - * @return the index of the element, if it is contained in the array within the specified range; - * otherwise, the inverted insertion point `(-insertion point - 1)`. - * The insertion point is defined as the index at which the element should be inserted, - * so that the array (or the specified subrange of array) still remains sorted. - */ -public fun CharArray.binarySearch(element: Char, fromIndex: Int = 0, toIndex: Int = size): Int { - return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element) -} - -/** - * Returns `true` if the two specified arrays are *deeply* equal to one another, - * i.e. contain the same number of the same elements in the same order. - * - * If two corresponding elements are nested arrays, they are also compared deeply. - * If any of arrays contains itself on any nesting level the behavior is undefined. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline infix fun Array.contentDeepEquals(other: Array): Boolean { - return java.util.Arrays.deepEquals(this, other) -} - -/** - * Returns a hash code based on the contents of this array as if it is [List]. - * Nested arrays are treated as lists too. - * - * If any of arrays contains itself on any nesting level the behavior is undefined. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun Array.contentDeepHashCode(): Int { - return java.util.Arrays.deepHashCode(this) -} - -/** - * Returns a string representation of the contents of this array as if it is a [List]. - * Nested arrays are treated as lists too. - * - * If any of arrays contains itself on any nesting level that reference - * is rendered as `"[...]"` to prevent recursion. - * - * @sample samples.collections.Arrays.ContentOperations.contentDeepToString - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun Array.contentDeepToString(): String { - return java.util.Arrays.deepToString(this) -} - -/** - * Returns `true` if the two specified arrays are *structurally* equal to one another, - * i.e. contain the same number of the same elements in the same order. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline infix fun Array.contentEquals(other: Array): Boolean { - return java.util.Arrays.equals(this, other) -} - -/** - * Returns `true` if the two specified arrays are *structurally* equal to one another, - * i.e. contain the same number of the same elements in the same order. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline infix fun ByteArray.contentEquals(other: ByteArray): Boolean { - return java.util.Arrays.equals(this, other) -} - -/** - * Returns `true` if the two specified arrays are *structurally* equal to one another, - * i.e. contain the same number of the same elements in the same order. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline infix fun ShortArray.contentEquals(other: ShortArray): Boolean { - return java.util.Arrays.equals(this, other) -} - -/** - * Returns `true` if the two specified arrays are *structurally* equal to one another, - * i.e. contain the same number of the same elements in the same order. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline infix fun IntArray.contentEquals(other: IntArray): Boolean { - return java.util.Arrays.equals(this, other) -} - -/** - * Returns `true` if the two specified arrays are *structurally* equal to one another, - * i.e. contain the same number of the same elements in the same order. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline infix fun LongArray.contentEquals(other: LongArray): Boolean { - return java.util.Arrays.equals(this, other) -} - -/** - * Returns `true` if the two specified arrays are *structurally* equal to one another, - * i.e. contain the same number of the same elements in the same order. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline infix fun FloatArray.contentEquals(other: FloatArray): Boolean { - return java.util.Arrays.equals(this, other) -} - -/** - * Returns `true` if the two specified arrays are *structurally* equal to one another, - * i.e. contain the same number of the same elements in the same order. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline infix fun DoubleArray.contentEquals(other: DoubleArray): Boolean { - return java.util.Arrays.equals(this, other) -} - -/** - * Returns `true` if the two specified arrays are *structurally* equal to one another, - * i.e. contain the same number of the same elements in the same order. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline infix fun BooleanArray.contentEquals(other: BooleanArray): Boolean { - return java.util.Arrays.equals(this, other) -} - -/** - * Returns `true` if the two specified arrays are *structurally* equal to one another, - * i.e. contain the same number of the same elements in the same order. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline infix fun CharArray.contentEquals(other: CharArray): Boolean { - return java.util.Arrays.equals(this, other) -} - -/** - * Returns a hash code based on the contents of this array as if it is [List]. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun Array.contentHashCode(): Int { - return java.util.Arrays.hashCode(this) -} - -/** - * Returns a hash code based on the contents of this array as if it is [List]. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun ByteArray.contentHashCode(): Int { - return java.util.Arrays.hashCode(this) -} - -/** - * Returns a hash code based on the contents of this array as if it is [List]. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun ShortArray.contentHashCode(): Int { - return java.util.Arrays.hashCode(this) -} - -/** - * Returns a hash code based on the contents of this array as if it is [List]. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun IntArray.contentHashCode(): Int { - return java.util.Arrays.hashCode(this) -} - -/** - * Returns a hash code based on the contents of this array as if it is [List]. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun LongArray.contentHashCode(): Int { - return java.util.Arrays.hashCode(this) -} - -/** - * Returns a hash code based on the contents of this array as if it is [List]. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun FloatArray.contentHashCode(): Int { - return java.util.Arrays.hashCode(this) -} - -/** - * Returns a hash code based on the contents of this array as if it is [List]. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun DoubleArray.contentHashCode(): Int { - return java.util.Arrays.hashCode(this) -} - -/** - * Returns a hash code based on the contents of this array as if it is [List]. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun BooleanArray.contentHashCode(): Int { - return java.util.Arrays.hashCode(this) -} - -/** - * Returns a hash code based on the contents of this array as if it is [List]. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun CharArray.contentHashCode(): Int { - return java.util.Arrays.hashCode(this) -} - -/** - * Returns a string representation of the contents of the specified array as if it is [List]. - * - * @sample samples.collections.Arrays.ContentOperations.contentToString - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun Array.contentToString(): String { - return java.util.Arrays.toString(this) -} - -/** - * Returns a string representation of the contents of the specified array as if it is [List]. - * - * @sample samples.collections.Arrays.ContentOperations.contentToString - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun ByteArray.contentToString(): String { - return java.util.Arrays.toString(this) -} - -/** - * Returns a string representation of the contents of the specified array as if it is [List]. - * - * @sample samples.collections.Arrays.ContentOperations.contentToString - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun ShortArray.contentToString(): String { - return java.util.Arrays.toString(this) -} - -/** - * Returns a string representation of the contents of the specified array as if it is [List]. - * - * @sample samples.collections.Arrays.ContentOperations.contentToString - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun IntArray.contentToString(): String { - return java.util.Arrays.toString(this) -} - -/** - * Returns a string representation of the contents of the specified array as if it is [List]. - * - * @sample samples.collections.Arrays.ContentOperations.contentToString - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun LongArray.contentToString(): String { - return java.util.Arrays.toString(this) -} - -/** - * Returns a string representation of the contents of the specified array as if it is [List]. - * - * @sample samples.collections.Arrays.ContentOperations.contentToString - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun FloatArray.contentToString(): String { - return java.util.Arrays.toString(this) -} - -/** - * Returns a string representation of the contents of the specified array as if it is [List]. - * - * @sample samples.collections.Arrays.ContentOperations.contentToString - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun DoubleArray.contentToString(): String { - return java.util.Arrays.toString(this) -} - -/** - * Returns a string representation of the contents of the specified array as if it is [List]. - * - * @sample samples.collections.Arrays.ContentOperations.contentToString - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun BooleanArray.contentToString(): String { - return java.util.Arrays.toString(this) -} - -/** - * Returns a string representation of the contents of the specified array as if it is [List]. - * - * @sample samples.collections.Arrays.ContentOperations.contentToString - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun CharArray.contentToString(): String { - return java.util.Arrays.toString(this) -} - -/** - * Returns new array which is a copy of the original array. - */ -@kotlin.internal.InlineOnly -public inline fun Array.copyOf(): Array { - return java.util.Arrays.copyOf(this, size) -} - -/** - * Returns new array which is a copy of the original array. - */ -@kotlin.internal.InlineOnly -public inline fun ByteArray.copyOf(): ByteArray { - return java.util.Arrays.copyOf(this, size) -} - -/** - * Returns new array which is a copy of the original array. - */ -@kotlin.internal.InlineOnly -public inline fun ShortArray.copyOf(): ShortArray { - return java.util.Arrays.copyOf(this, size) -} - -/** - * Returns new array which is a copy of the original array. - */ -@kotlin.internal.InlineOnly -public inline fun IntArray.copyOf(): IntArray { - return java.util.Arrays.copyOf(this, size) -} - -/** - * Returns new array which is a copy of the original array. - */ -@kotlin.internal.InlineOnly -public inline fun LongArray.copyOf(): LongArray { - return java.util.Arrays.copyOf(this, size) -} - -/** - * Returns new array which is a copy of the original array. - */ -@kotlin.internal.InlineOnly -public inline fun FloatArray.copyOf(): FloatArray { - return java.util.Arrays.copyOf(this, size) -} - -/** - * Returns new array which is a copy of the original array. - */ -@kotlin.internal.InlineOnly -public inline fun DoubleArray.copyOf(): DoubleArray { - return java.util.Arrays.copyOf(this, size) -} - -/** - * Returns new array which is a copy of the original array. - */ -@kotlin.internal.InlineOnly -public inline fun BooleanArray.copyOf(): BooleanArray { - return java.util.Arrays.copyOf(this, size) -} - -/** - * Returns new array which is a copy of the original array. - */ -@kotlin.internal.InlineOnly -public inline fun CharArray.copyOf(): CharArray { - return java.util.Arrays.copyOf(this, size) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -@kotlin.internal.InlineOnly -public inline fun ByteArray.copyOf(newSize: Int): ByteArray { - return java.util.Arrays.copyOf(this, newSize) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -@kotlin.internal.InlineOnly -public inline fun ShortArray.copyOf(newSize: Int): ShortArray { - return java.util.Arrays.copyOf(this, newSize) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -@kotlin.internal.InlineOnly -public inline fun IntArray.copyOf(newSize: Int): IntArray { - return java.util.Arrays.copyOf(this, newSize) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -@kotlin.internal.InlineOnly -public inline fun LongArray.copyOf(newSize: Int): LongArray { - return java.util.Arrays.copyOf(this, newSize) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -@kotlin.internal.InlineOnly -public inline fun FloatArray.copyOf(newSize: Int): FloatArray { - return java.util.Arrays.copyOf(this, newSize) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -@kotlin.internal.InlineOnly -public inline fun DoubleArray.copyOf(newSize: Int): DoubleArray { - return java.util.Arrays.copyOf(this, newSize) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -@kotlin.internal.InlineOnly -public inline fun BooleanArray.copyOf(newSize: Int): BooleanArray { - return java.util.Arrays.copyOf(this, newSize) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -@kotlin.internal.InlineOnly -public inline fun CharArray.copyOf(newSize: Int): CharArray { - return java.util.Arrays.copyOf(this, newSize) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -@kotlin.internal.InlineOnly -public inline fun Array.copyOf(newSize: Int): Array { - return java.util.Arrays.copyOf(this, newSize) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@kotlin.internal.InlineOnly -public inline fun Array.copyOfRange(fromIndex: Int, toIndex: Int): Array { - return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@kotlin.internal.InlineOnly -public inline fun ByteArray.copyOfRange(fromIndex: Int, toIndex: Int): ByteArray { - return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@kotlin.internal.InlineOnly -public inline fun ShortArray.copyOfRange(fromIndex: Int, toIndex: Int): ShortArray { - return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@kotlin.internal.InlineOnly -public inline fun IntArray.copyOfRange(fromIndex: Int, toIndex: Int): IntArray { - return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@kotlin.internal.InlineOnly -public inline fun LongArray.copyOfRange(fromIndex: Int, toIndex: Int): LongArray { - return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@kotlin.internal.InlineOnly -public inline fun FloatArray.copyOfRange(fromIndex: Int, toIndex: Int): FloatArray { - return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@kotlin.internal.InlineOnly -public inline fun DoubleArray.copyOfRange(fromIndex: Int, toIndex: Int): DoubleArray { - return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@kotlin.internal.InlineOnly -public inline fun BooleanArray.copyOfRange(fromIndex: Int, toIndex: Int): BooleanArray { - return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@kotlin.internal.InlineOnly -public inline fun CharArray.copyOfRange(fromIndex: Int, toIndex: Int): CharArray { - return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) -} - -/** - * Fills original array with the provided value. - */ -public fun Array.fill(element: T, fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.fill(this, fromIndex, toIndex, element) -} - -/** - * Fills original array with the provided value. - */ -public fun ByteArray.fill(element: Byte, fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.fill(this, fromIndex, toIndex, element) -} - -/** - * Fills original array with the provided value. - */ -public fun ShortArray.fill(element: Short, fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.fill(this, fromIndex, toIndex, element) -} - -/** - * Fills original array with the provided value. - */ -public fun IntArray.fill(element: Int, fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.fill(this, fromIndex, toIndex, element) -} - -/** - * Fills original array with the provided value. - */ -public fun LongArray.fill(element: Long, fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.fill(this, fromIndex, toIndex, element) -} - -/** - * Fills original array with the provided value. - */ -public fun FloatArray.fill(element: Float, fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.fill(this, fromIndex, toIndex, element) -} - -/** - * Fills original array with the provided value. - */ -public fun DoubleArray.fill(element: Double, fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.fill(this, fromIndex, toIndex, element) -} - -/** - * Fills original array with the provided value. - */ -public fun BooleanArray.fill(element: Boolean, fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.fill(this, fromIndex, toIndex, element) -} - -/** - * Fills original array with the provided value. - */ -public fun CharArray.fill(element: Char, fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.fill(this, fromIndex, toIndex, element) -} - -/** - * Returns the range of valid indices for the array. - */ -public val Array.indices: IntRange - get() = IntRange(0, lastIndex) - -/** - * Returns the range of valid indices for the array. - */ -public val ByteArray.indices: IntRange - get() = IntRange(0, lastIndex) - -/** - * Returns the range of valid indices for the array. - */ -public val ShortArray.indices: IntRange - get() = IntRange(0, lastIndex) - -/** - * Returns the range of valid indices for the array. - */ -public val IntArray.indices: IntRange - get() = IntRange(0, lastIndex) - -/** - * Returns the range of valid indices for the array. - */ -public val LongArray.indices: IntRange - get() = IntRange(0, lastIndex) - -/** - * Returns the range of valid indices for the array. - */ -public val FloatArray.indices: IntRange - get() = IntRange(0, lastIndex) - -/** - * Returns the range of valid indices for the array. - */ -public val DoubleArray.indices: IntRange - get() = IntRange(0, lastIndex) - -/** - * Returns the range of valid indices for the array. - */ -public val BooleanArray.indices: IntRange - get() = IntRange(0, lastIndex) - -/** - * Returns the range of valid indices for the array. - */ -public val CharArray.indices: IntRange - get() = IntRange(0, lastIndex) - -/** - * Returns `true` if the array is empty. - */ -@kotlin.internal.InlineOnly -public inline fun Array.isEmpty(): Boolean { - return size == 0 -} - -/** - * Returns `true` if the array is empty. - */ -@kotlin.internal.InlineOnly -public inline fun ByteArray.isEmpty(): Boolean { - return size == 0 -} - -/** - * Returns `true` if the array is empty. - */ -@kotlin.internal.InlineOnly -public inline fun ShortArray.isEmpty(): Boolean { - return size == 0 -} - -/** - * Returns `true` if the array is empty. - */ -@kotlin.internal.InlineOnly -public inline fun IntArray.isEmpty(): Boolean { - return size == 0 -} - -/** - * Returns `true` if the array is empty. - */ -@kotlin.internal.InlineOnly -public inline fun LongArray.isEmpty(): Boolean { - return size == 0 -} - -/** - * Returns `true` if the array is empty. - */ -@kotlin.internal.InlineOnly -public inline fun FloatArray.isEmpty(): Boolean { - return size == 0 -} - -/** - * Returns `true` if the array is empty. - */ -@kotlin.internal.InlineOnly -public inline fun DoubleArray.isEmpty(): Boolean { - return size == 0 -} - -/** - * Returns `true` if the array is empty. - */ -@kotlin.internal.InlineOnly -public inline fun BooleanArray.isEmpty(): Boolean { - return size == 0 -} - -/** - * Returns `true` if the array is empty. - */ -@kotlin.internal.InlineOnly -public inline fun CharArray.isEmpty(): Boolean { - return size == 0 -} - -/** - * Returns `true` if the array is not empty. - */ -@kotlin.internal.InlineOnly -public inline fun Array.isNotEmpty(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if the array is not empty. - */ -@kotlin.internal.InlineOnly -public inline fun ByteArray.isNotEmpty(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if the array is not empty. - */ -@kotlin.internal.InlineOnly -public inline fun ShortArray.isNotEmpty(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if the array is not empty. - */ -@kotlin.internal.InlineOnly -public inline fun IntArray.isNotEmpty(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if the array is not empty. - */ -@kotlin.internal.InlineOnly -public inline fun LongArray.isNotEmpty(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if the array is not empty. - */ -@kotlin.internal.InlineOnly -public inline fun FloatArray.isNotEmpty(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if the array is not empty. - */ -@kotlin.internal.InlineOnly -public inline fun DoubleArray.isNotEmpty(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if the array is not empty. - */ -@kotlin.internal.InlineOnly -public inline fun BooleanArray.isNotEmpty(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if the array is not empty. - */ -@kotlin.internal.InlineOnly -public inline fun CharArray.isNotEmpty(): Boolean { - return !isEmpty() -} - -/** - * Returns the last valid index for the array. - */ -public val Array.lastIndex: Int - get() = size - 1 - -/** - * Returns the last valid index for the array. - */ -public val ByteArray.lastIndex: Int - get() = size - 1 - -/** - * Returns the last valid index for the array. - */ -public val ShortArray.lastIndex: Int - get() = size - 1 - -/** - * Returns the last valid index for the array. - */ -public val IntArray.lastIndex: Int - get() = size - 1 - -/** - * Returns the last valid index for the array. - */ -public val LongArray.lastIndex: Int - get() = size - 1 - -/** - * Returns the last valid index for the array. - */ -public val FloatArray.lastIndex: Int - get() = size - 1 - -/** - * Returns the last valid index for the array. - */ -public val DoubleArray.lastIndex: Int - get() = size - 1 - -/** - * Returns the last valid index for the array. - */ -public val BooleanArray.lastIndex: Int - get() = size - 1 - -/** - * Returns the last valid index for the array. - */ -public val CharArray.lastIndex: Int - get() = size - 1 - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public operator fun Array.plus(element: T): Array { - val index = size - val result = java.util.Arrays.copyOf(this, index + 1) - result[index] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public operator fun ByteArray.plus(element: Byte): ByteArray { - val index = size - val result = java.util.Arrays.copyOf(this, index + 1) - result[index] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public operator fun ShortArray.plus(element: Short): ShortArray { - val index = size - val result = java.util.Arrays.copyOf(this, index + 1) - result[index] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public operator fun IntArray.plus(element: Int): IntArray { - val index = size - val result = java.util.Arrays.copyOf(this, index + 1) - result[index] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public operator fun LongArray.plus(element: Long): LongArray { - val index = size - val result = java.util.Arrays.copyOf(this, index + 1) - result[index] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public operator fun FloatArray.plus(element: Float): FloatArray { - val index = size - val result = java.util.Arrays.copyOf(this, index + 1) - result[index] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public operator fun DoubleArray.plus(element: Double): DoubleArray { - val index = size - val result = java.util.Arrays.copyOf(this, index + 1) - result[index] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public operator fun BooleanArray.plus(element: Boolean): BooleanArray { - val index = size - val result = java.util.Arrays.copyOf(this, index + 1) - result[index] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public operator fun CharArray.plus(element: Char): CharArray { - val index = size - val result = java.util.Arrays.copyOf(this, index + 1) - result[index] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun Array.plus(elements: Collection): Array { - var index = size - val result = java.util.Arrays.copyOf(this, index + elements.size) - for (element in elements) result[index++] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun ByteArray.plus(elements: Collection): ByteArray { - var index = size - val result = java.util.Arrays.copyOf(this, index + elements.size) - for (element in elements) result[index++] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun ShortArray.plus(elements: Collection): ShortArray { - var index = size - val result = java.util.Arrays.copyOf(this, index + elements.size) - for (element in elements) result[index++] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun IntArray.plus(elements: Collection): IntArray { - var index = size - val result = java.util.Arrays.copyOf(this, index + elements.size) - for (element in elements) result[index++] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun LongArray.plus(elements: Collection): LongArray { - var index = size - val result = java.util.Arrays.copyOf(this, index + elements.size) - for (element in elements) result[index++] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun FloatArray.plus(elements: Collection): FloatArray { - var index = size - val result = java.util.Arrays.copyOf(this, index + elements.size) - for (element in elements) result[index++] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun DoubleArray.plus(elements: Collection): DoubleArray { - var index = size - val result = java.util.Arrays.copyOf(this, index + elements.size) - for (element in elements) result[index++] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun BooleanArray.plus(elements: Collection): BooleanArray { - var index = size - val result = java.util.Arrays.copyOf(this, index + elements.size) - for (element in elements) result[index++] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun CharArray.plus(elements: Collection): CharArray { - var index = size - val result = java.util.Arrays.copyOf(this, index + elements.size) - for (element in elements) result[index++] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -public operator fun Array.plus(elements: Array): Array { - val thisSize = size - val arraySize = elements.size - val result = java.util.Arrays.copyOf(this, thisSize + arraySize) - System.arraycopy(elements, 0, result, thisSize, arraySize) - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -public operator fun ByteArray.plus(elements: ByteArray): ByteArray { - val thisSize = size - val arraySize = elements.size - val result = java.util.Arrays.copyOf(this, thisSize + arraySize) - System.arraycopy(elements, 0, result, thisSize, arraySize) - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -public operator fun ShortArray.plus(elements: ShortArray): ShortArray { - val thisSize = size - val arraySize = elements.size - val result = java.util.Arrays.copyOf(this, thisSize + arraySize) - System.arraycopy(elements, 0, result, thisSize, arraySize) - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -public operator fun IntArray.plus(elements: IntArray): IntArray { - val thisSize = size - val arraySize = elements.size - val result = java.util.Arrays.copyOf(this, thisSize + arraySize) - System.arraycopy(elements, 0, result, thisSize, arraySize) - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -public operator fun LongArray.plus(elements: LongArray): LongArray { - val thisSize = size - val arraySize = elements.size - val result = java.util.Arrays.copyOf(this, thisSize + arraySize) - System.arraycopy(elements, 0, result, thisSize, arraySize) - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -public operator fun FloatArray.plus(elements: FloatArray): FloatArray { - val thisSize = size - val arraySize = elements.size - val result = java.util.Arrays.copyOf(this, thisSize + arraySize) - System.arraycopy(elements, 0, result, thisSize, arraySize) - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -public operator fun DoubleArray.plus(elements: DoubleArray): DoubleArray { - val thisSize = size - val arraySize = elements.size - val result = java.util.Arrays.copyOf(this, thisSize + arraySize) - System.arraycopy(elements, 0, result, thisSize, arraySize) - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -public operator fun BooleanArray.plus(elements: BooleanArray): BooleanArray { - val thisSize = size - val arraySize = elements.size - val result = java.util.Arrays.copyOf(this, thisSize + arraySize) - System.arraycopy(elements, 0, result, thisSize, arraySize) - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -public operator fun CharArray.plus(elements: CharArray): CharArray { - val thisSize = size - val arraySize = elements.size - val result = java.util.Arrays.copyOf(this, thisSize + arraySize) - System.arraycopy(elements, 0, result, thisSize, arraySize) - return result -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -@kotlin.internal.InlineOnly -public inline fun Array.plusElement(element: T): Array { - return plus(element) -} - -/** - * Sorts the array in-place. - */ -public fun IntArray.sort(): Unit { - if (size > 1) java.util.Arrays.sort(this) -} - -/** - * Sorts the array in-place. - */ -public fun LongArray.sort(): Unit { - if (size > 1) java.util.Arrays.sort(this) -} - -/** - * Sorts the array in-place. - */ -public fun ByteArray.sort(): Unit { - if (size > 1) java.util.Arrays.sort(this) -} - -/** - * Sorts the array in-place. - */ -public fun ShortArray.sort(): Unit { - if (size > 1) java.util.Arrays.sort(this) -} - -/** - * Sorts the array in-place. - */ -public fun DoubleArray.sort(): Unit { - if (size > 1) java.util.Arrays.sort(this) -} - -/** - * Sorts the array in-place. - */ -public fun FloatArray.sort(): Unit { - if (size > 1) java.util.Arrays.sort(this) -} - -/** - * Sorts the array in-place. - */ -public fun CharArray.sort(): Unit { - if (size > 1) java.util.Arrays.sort(this) -} - -/** - * Sorts the array in-place according to the natural order of its elements. - */ -@kotlin.internal.InlineOnly -public inline fun > Array.sort(): Unit { - @Suppress("UNCHECKED_CAST") - (this as Array).sort() -} - -/** - * Sorts the array in-place according to the natural order of its elements. - * - * @throws ClassCastException if any element of the array is not [Comparable]. - */ -public fun Array.sort(): Unit { - if (size > 1) java.util.Arrays.sort(this) -} - -/** - * Sorts a range in the array in-place. - */ -public fun Array.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.sort(this, fromIndex, toIndex) -} - -/** - * Sorts a range in the array in-place. - */ -public fun ByteArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.sort(this, fromIndex, toIndex) -} - -/** - * Sorts a range in the array in-place. - */ -public fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.sort(this, fromIndex, toIndex) -} - -/** - * Sorts a range in the array in-place. - */ -public fun IntArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.sort(this, fromIndex, toIndex) -} - -/** - * Sorts a range in the array in-place. - */ -public fun LongArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.sort(this, fromIndex, toIndex) -} - -/** - * Sorts a range in the array in-place. - */ -public fun FloatArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.sort(this, fromIndex, toIndex) -} - -/** - * Sorts a range in the array in-place. - */ -public fun DoubleArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.sort(this, fromIndex, toIndex) -} - -/** - * Sorts a range in the array in-place. - */ -public fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.sort(this, fromIndex, toIndex) -} - -/** - * Sorts the array in-place according to the order specified by the given [comparator]. - */ -public fun Array.sortWith(comparator: Comparator): Unit { - if (size > 1) java.util.Arrays.sort(this, comparator) -} - -/** - * Sorts a range in the array in-place with the given [comparator]. - */ -public fun Array.sortWith(comparator: Comparator, fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.sort(this, fromIndex, toIndex, comparator) -} - -/** - * Returns an array of Boolean containing all of the elements of this generic array. - */ -public fun Array.toBooleanArray(): BooleanArray { - val result = BooleanArray(size) - for (index in indices) - result[index] = this[index] - return result -} - -/** - * Returns an array of Byte containing all of the elements of this generic array. - */ -public fun Array.toByteArray(): ByteArray { - val result = ByteArray(size) - for (index in indices) - result[index] = this[index] - return result -} - -/** - * Returns an array of Char containing all of the elements of this generic array. - */ -public fun Array.toCharArray(): CharArray { - val result = CharArray(size) - for (index in indices) - result[index] = this[index] - return result -} - -/** - * Returns an array of Double containing all of the elements of this generic array. - */ -public fun Array.toDoubleArray(): DoubleArray { - val result = DoubleArray(size) - for (index in indices) - result[index] = this[index] - return result -} - -/** - * Returns an array of Float containing all of the elements of this generic array. - */ -public fun Array.toFloatArray(): FloatArray { - val result = FloatArray(size) - for (index in indices) - result[index] = this[index] - return result -} - -/** - * Returns an array of Int containing all of the elements of this generic array. - */ -public fun Array.toIntArray(): IntArray { - val result = IntArray(size) - for (index in indices) - result[index] = this[index] - return result -} - -/** - * Returns an array of Long containing all of the elements of this generic array. - */ -public fun Array.toLongArray(): LongArray { - val result = LongArray(size) - for (index in indices) - result[index] = this[index] - return result -} - -/** - * Returns an array of Short containing all of the elements of this generic array. - */ -public fun Array.toShortArray(): ShortArray { - val result = ShortArray(size) - for (index in indices) - result[index] = this[index] - return result -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun ByteArray.toTypedArray(): Array { - val result = arrayOfNulls(size) - for (index in indices) - result[index] = this[index] - @Suppress("UNCHECKED_CAST") - return result as Array -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun ShortArray.toTypedArray(): Array { - val result = arrayOfNulls(size) - for (index in indices) - result[index] = this[index] - @Suppress("UNCHECKED_CAST") - return result as Array -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun IntArray.toTypedArray(): Array { - val result = arrayOfNulls(size) - for (index in indices) - result[index] = this[index] - @Suppress("UNCHECKED_CAST") - return result as Array -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun LongArray.toTypedArray(): Array { - val result = arrayOfNulls(size) - for (index in indices) - result[index] = this[index] - @Suppress("UNCHECKED_CAST") - return result as Array -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun FloatArray.toTypedArray(): Array { - val result = arrayOfNulls(size) - for (index in indices) - result[index] = this[index] - @Suppress("UNCHECKED_CAST") - return result as Array -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun DoubleArray.toTypedArray(): Array { - val result = arrayOfNulls(size) - for (index in indices) - result[index] = this[index] - @Suppress("UNCHECKED_CAST") - return result as Array -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun BooleanArray.toTypedArray(): Array { - val result = arrayOfNulls(size) - for (index in indices) - result[index] = this[index] - @Suppress("UNCHECKED_CAST") - return result as Array -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun CharArray.toTypedArray(): Array { - val result = arrayOfNulls(size) - for (index in indices) - result[index] = this[index] - @Suppress("UNCHECKED_CAST") - return result as Array -} - -/** - * Returns a [Map] containing key-value pairs provided by [transform] function - * applied to elements of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun Array.associate(transform: (T) -> Pair): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateTo(LinkedHashMap(capacity), transform) -} - -/** - * Returns a [Map] containing key-value pairs provided by [transform] function - * applied to elements of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun ByteArray.associate(transform: (Byte) -> Pair): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateTo(LinkedHashMap(capacity), transform) -} - -/** - * Returns a [Map] containing key-value pairs provided by [transform] function - * applied to elements of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun ShortArray.associate(transform: (Short) -> Pair): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateTo(LinkedHashMap(capacity), transform) -} - -/** - * Returns a [Map] containing key-value pairs provided by [transform] function - * applied to elements of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun IntArray.associate(transform: (Int) -> Pair): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateTo(LinkedHashMap(capacity), transform) -} - -/** - * Returns a [Map] containing key-value pairs provided by [transform] function - * applied to elements of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun LongArray.associate(transform: (Long) -> Pair): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateTo(LinkedHashMap(capacity), transform) -} - -/** - * Returns a [Map] containing key-value pairs provided by [transform] function - * applied to elements of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun FloatArray.associate(transform: (Float) -> Pair): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateTo(LinkedHashMap(capacity), transform) -} - -/** - * Returns a [Map] containing key-value pairs provided by [transform] function - * applied to elements of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun DoubleArray.associate(transform: (Double) -> Pair): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateTo(LinkedHashMap(capacity), transform) -} - -/** - * Returns a [Map] containing key-value pairs provided by [transform] function - * applied to elements of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun BooleanArray.associate(transform: (Boolean) -> Pair): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateTo(LinkedHashMap(capacity), transform) -} - -/** - * Returns a [Map] containing key-value pairs provided by [transform] function - * applied to elements of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun CharArray.associate(transform: (Char) -> Pair): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateTo(LinkedHashMap(capacity), transform) -} - -/** - * Returns a [Map] containing the elements from the given array indexed by the key - * returned from [keySelector] function applied to each element. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun Array.associateBy(keySelector: (T) -> K): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector) -} - -/** - * Returns a [Map] containing the elements from the given array indexed by the key - * returned from [keySelector] function applied to each element. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun ByteArray.associateBy(keySelector: (Byte) -> K): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector) -} - -/** - * Returns a [Map] containing the elements from the given array indexed by the key - * returned from [keySelector] function applied to each element. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun ShortArray.associateBy(keySelector: (Short) -> K): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector) -} - -/** - * Returns a [Map] containing the elements from the given array indexed by the key - * returned from [keySelector] function applied to each element. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun IntArray.associateBy(keySelector: (Int) -> K): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector) -} - -/** - * Returns a [Map] containing the elements from the given array indexed by the key - * returned from [keySelector] function applied to each element. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun LongArray.associateBy(keySelector: (Long) -> K): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector) -} - -/** - * Returns a [Map] containing the elements from the given array indexed by the key - * returned from [keySelector] function applied to each element. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun FloatArray.associateBy(keySelector: (Float) -> K): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector) -} - -/** - * Returns a [Map] containing the elements from the given array indexed by the key - * returned from [keySelector] function applied to each element. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun DoubleArray.associateBy(keySelector: (Double) -> K): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector) -} - -/** - * Returns a [Map] containing the elements from the given array indexed by the key - * returned from [keySelector] function applied to each element. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun BooleanArray.associateBy(keySelector: (Boolean) -> K): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector) -} - -/** - * Returns a [Map] containing the elements from the given array indexed by the key - * returned from [keySelector] function applied to each element. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun CharArray.associateBy(keySelector: (Char) -> K): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector) -} - -/** - * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun Array.associateBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) -} - -/** - * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun ByteArray.associateBy(keySelector: (Byte) -> K, valueTransform: (Byte) -> V): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) -} - -/** - * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun ShortArray.associateBy(keySelector: (Short) -> K, valueTransform: (Short) -> V): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) -} - -/** - * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun IntArray.associateBy(keySelector: (Int) -> K, valueTransform: (Int) -> V): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) -} - -/** - * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun LongArray.associateBy(keySelector: (Long) -> K, valueTransform: (Long) -> V): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) -} - -/** - * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun FloatArray.associateBy(keySelector: (Float) -> K, valueTransform: (Float) -> V): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) -} - -/** - * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun DoubleArray.associateBy(keySelector: (Double) -> K, valueTransform: (Double) -> V): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) -} - -/** - * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun BooleanArray.associateBy(keySelector: (Boolean) -> K, valueTransform: (Boolean) -> V): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) -} - -/** - * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original array. - */ -public inline fun CharArray.associateBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map { - val capacity = mapCapacity(size).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function applied to each element of the given array - * and value is the element itself. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > Array.associateByTo(destination: M, keySelector: (T) -> K): M { - for (element in this) { - destination.put(keySelector(element), element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function applied to each element of the given array - * and value is the element itself. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > ByteArray.associateByTo(destination: M, keySelector: (Byte) -> K): M { - for (element in this) { - destination.put(keySelector(element), element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function applied to each element of the given array - * and value is the element itself. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > ShortArray.associateByTo(destination: M, keySelector: (Short) -> K): M { - for (element in this) { - destination.put(keySelector(element), element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function applied to each element of the given array - * and value is the element itself. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > IntArray.associateByTo(destination: M, keySelector: (Int) -> K): M { - for (element in this) { - destination.put(keySelector(element), element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function applied to each element of the given array - * and value is the element itself. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > LongArray.associateByTo(destination: M, keySelector: (Long) -> K): M { - for (element in this) { - destination.put(keySelector(element), element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function applied to each element of the given array - * and value is the element itself. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > FloatArray.associateByTo(destination: M, keySelector: (Float) -> K): M { - for (element in this) { - destination.put(keySelector(element), element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function applied to each element of the given array - * and value is the element itself. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > DoubleArray.associateByTo(destination: M, keySelector: (Double) -> K): M { - for (element in this) { - destination.put(keySelector(element), element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function applied to each element of the given array - * and value is the element itself. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > BooleanArray.associateByTo(destination: M, keySelector: (Boolean) -> K): M { - for (element in this) { - destination.put(keySelector(element), element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function applied to each element of the given array - * and value is the element itself. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > CharArray.associateByTo(destination: M, keySelector: (Char) -> K): M { - for (element in this) { - destination.put(keySelector(element), element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function and - * and value is provided by the [valueTransform] function applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > Array.associateByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M { - for (element in this) { - destination.put(keySelector(element), valueTransform(element)) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function and - * and value is provided by the [valueTransform] function applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > ByteArray.associateByTo(destination: M, keySelector: (Byte) -> K, valueTransform: (Byte) -> V): M { - for (element in this) { - destination.put(keySelector(element), valueTransform(element)) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function and - * and value is provided by the [valueTransform] function applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > ShortArray.associateByTo(destination: M, keySelector: (Short) -> K, valueTransform: (Short) -> V): M { - for (element in this) { - destination.put(keySelector(element), valueTransform(element)) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function and - * and value is provided by the [valueTransform] function applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > IntArray.associateByTo(destination: M, keySelector: (Int) -> K, valueTransform: (Int) -> V): M { - for (element in this) { - destination.put(keySelector(element), valueTransform(element)) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function and - * and value is provided by the [valueTransform] function applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > LongArray.associateByTo(destination: M, keySelector: (Long) -> K, valueTransform: (Long) -> V): M { - for (element in this) { - destination.put(keySelector(element), valueTransform(element)) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function and - * and value is provided by the [valueTransform] function applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > FloatArray.associateByTo(destination: M, keySelector: (Float) -> K, valueTransform: (Float) -> V): M { - for (element in this) { - destination.put(keySelector(element), valueTransform(element)) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function and - * and value is provided by the [valueTransform] function applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > DoubleArray.associateByTo(destination: M, keySelector: (Double) -> K, valueTransform: (Double) -> V): M { - for (element in this) { - destination.put(keySelector(element), valueTransform(element)) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function and - * and value is provided by the [valueTransform] function applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > BooleanArray.associateByTo(destination: M, keySelector: (Boolean) -> K, valueTransform: (Boolean) -> V): M { - for (element in this) { - destination.put(keySelector(element), valueTransform(element)) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function and - * and value is provided by the [valueTransform] function applied to elements of the given array. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > CharArray.associateByTo(destination: M, keySelector: (Char) -> K, valueTransform: (Char) -> V): M { - for (element in this) { - destination.put(keySelector(element), valueTransform(element)) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs - * provided by [transform] function applied to each element of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - */ -public inline fun > Array.associateTo(destination: M, transform: (T) -> Pair): M { - for (element in this) { - destination += transform(element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs - * provided by [transform] function applied to each element of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - */ -public inline fun > ByteArray.associateTo(destination: M, transform: (Byte) -> Pair): M { - for (element in this) { - destination += transform(element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs - * provided by [transform] function applied to each element of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - */ -public inline fun > ShortArray.associateTo(destination: M, transform: (Short) -> Pair): M { - for (element in this) { - destination += transform(element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs - * provided by [transform] function applied to each element of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - */ -public inline fun > IntArray.associateTo(destination: M, transform: (Int) -> Pair): M { - for (element in this) { - destination += transform(element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs - * provided by [transform] function applied to each element of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - */ -public inline fun > LongArray.associateTo(destination: M, transform: (Long) -> Pair): M { - for (element in this) { - destination += transform(element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs - * provided by [transform] function applied to each element of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - */ -public inline fun > FloatArray.associateTo(destination: M, transform: (Float) -> Pair): M { - for (element in this) { - destination += transform(element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs - * provided by [transform] function applied to each element of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - */ -public inline fun > DoubleArray.associateTo(destination: M, transform: (Double) -> Pair): M { - for (element in this) { - destination += transform(element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs - * provided by [transform] function applied to each element of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - */ -public inline fun > BooleanArray.associateTo(destination: M, transform: (Boolean) -> Pair): M { - for (element in this) { - destination += transform(element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs - * provided by [transform] function applied to each element of the given array. - * - * If any of two pairs would have the same key the last one gets added to the map. - */ -public inline fun > CharArray.associateTo(destination: M, transform: (Char) -> Pair): M { - for (element in this) { - destination += transform(element) - } - return destination -} - -/** - * Appends all elements to the given [destination] collection. - */ -public fun > Array.toCollection(destination: C): C { - for (item in this) { - destination.add(item) - } - return destination -} - -/** - * Appends all elements to the given [destination] collection. - */ -public fun > ByteArray.toCollection(destination: C): C { - for (item in this) { - destination.add(item) - } - return destination -} - -/** - * Appends all elements to the given [destination] collection. - */ -public fun > ShortArray.toCollection(destination: C): C { - for (item in this) { - destination.add(item) - } - return destination -} - -/** - * Appends all elements to the given [destination] collection. - */ -public fun > IntArray.toCollection(destination: C): C { - for (item in this) { - destination.add(item) - } - return destination -} - -/** - * Appends all elements to the given [destination] collection. - */ -public fun > LongArray.toCollection(destination: C): C { - for (item in this) { - destination.add(item) - } - return destination -} - -/** - * Appends all elements to the given [destination] collection. - */ -public fun > FloatArray.toCollection(destination: C): C { - for (item in this) { - destination.add(item) - } - return destination -} - -/** - * Appends all elements to the given [destination] collection. - */ -public fun > DoubleArray.toCollection(destination: C): C { - for (item in this) { - destination.add(item) - } - return destination -} - -/** - * Appends all elements to the given [destination] collection. - */ -public fun > BooleanArray.toCollection(destination: C): C { - for (item in this) { - destination.add(item) - } - return destination -} - -/** - * Appends all elements to the given [destination] collection. - */ -public fun > CharArray.toCollection(destination: C): C { - for (item in this) { - destination.add(item) - } - return destination -} - -/** - * Returns a [HashSet] of all elements. - */ -public fun Array.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(size))) -} - -/** - * Returns a [HashSet] of all elements. - */ -public fun ByteArray.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(size))) -} - -/** - * Returns a [HashSet] of all elements. - */ -public fun ShortArray.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(size))) -} - -/** - * Returns a [HashSet] of all elements. - */ -public fun IntArray.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(size))) -} - -/** - * Returns a [HashSet] of all elements. - */ -public fun LongArray.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(size))) -} - -/** - * Returns a [HashSet] of all elements. - */ -public fun FloatArray.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(size))) -} - -/** - * Returns a [HashSet] of all elements. - */ -public fun DoubleArray.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(size))) -} - -/** - * Returns a [HashSet] of all elements. - */ -public fun BooleanArray.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(size))) -} - -/** - * Returns a [HashSet] of all elements. - */ -public fun CharArray.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(size))) -} - -/** - * Returns a [List] containing all elements. - */ -public fun Array.toList(): List { - return when (size) { - 0 -> emptyList() - 1 -> listOf(this[0]) - else -> this.toMutableList() - } -} - -/** - * Returns a [List] containing all elements. - */ -public fun ByteArray.toList(): List { - return when (size) { - 0 -> emptyList() - 1 -> listOf(this[0]) - else -> this.toMutableList() - } -} - -/** - * Returns a [List] containing all elements. - */ -public fun ShortArray.toList(): List { - return when (size) { - 0 -> emptyList() - 1 -> listOf(this[0]) - else -> this.toMutableList() - } -} - -/** - * Returns a [List] containing all elements. - */ -public fun IntArray.toList(): List { - return when (size) { - 0 -> emptyList() - 1 -> listOf(this[0]) - else -> this.toMutableList() - } -} - -/** - * Returns a [List] containing all elements. - */ -public fun LongArray.toList(): List { - return when (size) { - 0 -> emptyList() - 1 -> listOf(this[0]) - else -> this.toMutableList() - } -} - -/** - * Returns a [List] containing all elements. - */ -public fun FloatArray.toList(): List { - return when (size) { - 0 -> emptyList() - 1 -> listOf(this[0]) - else -> this.toMutableList() - } -} - -/** - * Returns a [List] containing all elements. - */ -public fun DoubleArray.toList(): List { - return when (size) { - 0 -> emptyList() - 1 -> listOf(this[0]) - else -> this.toMutableList() - } -} - -/** - * Returns a [List] containing all elements. - */ -public fun BooleanArray.toList(): List { - return when (size) { - 0 -> emptyList() - 1 -> listOf(this[0]) - else -> this.toMutableList() - } -} - -/** - * Returns a [List] containing all elements. - */ -public fun CharArray.toList(): List { - return when (size) { - 0 -> emptyList() - 1 -> listOf(this[0]) - else -> this.toMutableList() - } -} - -/** - * Returns a [MutableList] filled with all elements of this array. - */ -public fun Array.toMutableList(): MutableList { - return ArrayList(this.asCollection()) -} - -/** - * Returns a [MutableList] filled with all elements of this array. - */ -public fun ByteArray.toMutableList(): MutableList { - val list = ArrayList(size) - for (item in this) list.add(item) - return list -} - -/** - * Returns a [MutableList] filled with all elements of this array. - */ -public fun ShortArray.toMutableList(): MutableList { - val list = ArrayList(size) - for (item in this) list.add(item) - return list -} - -/** - * Returns a [MutableList] filled with all elements of this array. - */ -public fun IntArray.toMutableList(): MutableList { - val list = ArrayList(size) - for (item in this) list.add(item) - return list -} - -/** - * Returns a [MutableList] filled with all elements of this array. - */ -public fun LongArray.toMutableList(): MutableList { - val list = ArrayList(size) - for (item in this) list.add(item) - return list -} - -/** - * Returns a [MutableList] filled with all elements of this array. - */ -public fun FloatArray.toMutableList(): MutableList { - val list = ArrayList(size) - for (item in this) list.add(item) - return list -} - -/** - * Returns a [MutableList] filled with all elements of this array. - */ -public fun DoubleArray.toMutableList(): MutableList { - val list = ArrayList(size) - for (item in this) list.add(item) - return list -} - -/** - * Returns a [MutableList] filled with all elements of this array. - */ -public fun BooleanArray.toMutableList(): MutableList { - val list = ArrayList(size) - for (item in this) list.add(item) - return list -} - -/** - * Returns a [MutableList] filled with all elements of this array. - */ -public fun CharArray.toMutableList(): MutableList { - val list = ArrayList(size) - for (item in this) list.add(item) - return list -} - -/** - * Returns a [Set] of all elements. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun Array.toSet(): Set { - return when (size) { - 0 -> emptySet() - 1 -> setOf(this[0]) - else -> toCollection(LinkedHashSet(mapCapacity(size))) - } -} - -/** - * Returns a [Set] of all elements. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun ByteArray.toSet(): Set { - return when (size) { - 0 -> emptySet() - 1 -> setOf(this[0]) - else -> toCollection(LinkedHashSet(mapCapacity(size))) - } -} - -/** - * Returns a [Set] of all elements. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun ShortArray.toSet(): Set { - return when (size) { - 0 -> emptySet() - 1 -> setOf(this[0]) - else -> toCollection(LinkedHashSet(mapCapacity(size))) - } -} - -/** - * Returns a [Set] of all elements. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun IntArray.toSet(): Set { - return when (size) { - 0 -> emptySet() - 1 -> setOf(this[0]) - else -> toCollection(LinkedHashSet(mapCapacity(size))) - } -} - -/** - * Returns a [Set] of all elements. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun LongArray.toSet(): Set { - return when (size) { - 0 -> emptySet() - 1 -> setOf(this[0]) - else -> toCollection(LinkedHashSet(mapCapacity(size))) - } -} - -/** - * Returns a [Set] of all elements. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun FloatArray.toSet(): Set { - return when (size) { - 0 -> emptySet() - 1 -> setOf(this[0]) - else -> toCollection(LinkedHashSet(mapCapacity(size))) - } -} - -/** - * Returns a [Set] of all elements. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun DoubleArray.toSet(): Set { - return when (size) { - 0 -> emptySet() - 1 -> setOf(this[0]) - else -> toCollection(LinkedHashSet(mapCapacity(size))) - } -} - -/** - * Returns a [Set] of all elements. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun BooleanArray.toSet(): Set { - return when (size) { - 0 -> emptySet() - 1 -> setOf(this[0]) - else -> toCollection(LinkedHashSet(mapCapacity(size))) - } -} - -/** - * Returns a [Set] of all elements. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun CharArray.toSet(): Set { - return when (size) { - 0 -> emptySet() - 1 -> setOf(this[0]) - else -> toCollection(LinkedHashSet(mapCapacity(size))) - } -} - -/** - * Returns a [SortedSet][java.util.SortedSet] of all elements. - */ -public fun > Array.toSortedSet(): java.util.SortedSet { - return toCollection(java.util.TreeSet()) -} - -/** - * Returns a [SortedSet][java.util.SortedSet] of all elements. - */ -public fun ByteArray.toSortedSet(): java.util.SortedSet { - return toCollection(java.util.TreeSet()) -} - -/** - * Returns a [SortedSet][java.util.SortedSet] of all elements. - */ -public fun ShortArray.toSortedSet(): java.util.SortedSet { - return toCollection(java.util.TreeSet()) -} - -/** - * Returns a [SortedSet][java.util.SortedSet] of all elements. - */ -public fun IntArray.toSortedSet(): java.util.SortedSet { - return toCollection(java.util.TreeSet()) -} - -/** - * Returns a [SortedSet][java.util.SortedSet] of all elements. - */ -public fun LongArray.toSortedSet(): java.util.SortedSet { - return toCollection(java.util.TreeSet()) -} - -/** - * Returns a [SortedSet][java.util.SortedSet] of all elements. - */ -public fun FloatArray.toSortedSet(): java.util.SortedSet { - return toCollection(java.util.TreeSet()) -} - -/** - * Returns a [SortedSet][java.util.SortedSet] of all elements. - */ -public fun DoubleArray.toSortedSet(): java.util.SortedSet { - return toCollection(java.util.TreeSet()) -} - -/** - * Returns a [SortedSet][java.util.SortedSet] of all elements. - */ -public fun BooleanArray.toSortedSet(): java.util.SortedSet { - return toCollection(java.util.TreeSet()) -} - -/** - * Returns a [SortedSet][java.util.SortedSet] of all elements. - */ -public fun CharArray.toSortedSet(): java.util.SortedSet { - return toCollection(java.util.TreeSet()) -} - -/** - * Returns a [SortedSet][java.util.SortedSet] of all elements. - * - * Elements in the set returned are sorted according to the given [comparator]. - */ -public fun Array.toSortedSet(comparator: Comparator): java.util.SortedSet { - return toCollection(java.util.TreeSet(comparator)) -} - -/** - * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array. - */ -public inline fun Array.flatMap(transform: (T) -> Iterable): List { - return flatMapTo(ArrayList(), transform) -} - -/** - * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array. - */ -public inline fun ByteArray.flatMap(transform: (Byte) -> Iterable): List { - return flatMapTo(ArrayList(), transform) -} - -/** - * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array. - */ -public inline fun ShortArray.flatMap(transform: (Short) -> Iterable): List { - return flatMapTo(ArrayList(), transform) -} - -/** - * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array. - */ -public inline fun IntArray.flatMap(transform: (Int) -> Iterable): List { - return flatMapTo(ArrayList(), transform) -} - -/** - * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array. - */ -public inline fun LongArray.flatMap(transform: (Long) -> Iterable): List { - return flatMapTo(ArrayList(), transform) -} - -/** - * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array. - */ -public inline fun FloatArray.flatMap(transform: (Float) -> Iterable): List { - return flatMapTo(ArrayList(), transform) -} - -/** - * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array. - */ -public inline fun DoubleArray.flatMap(transform: (Double) -> Iterable): List { - return flatMapTo(ArrayList(), transform) -} - -/** - * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array. - */ -public inline fun BooleanArray.flatMap(transform: (Boolean) -> Iterable): List { - return flatMapTo(ArrayList(), transform) -} - -/** - * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array. - */ -public inline fun CharArray.flatMap(transform: (Char) -> Iterable): List { - return flatMapTo(ArrayList(), transform) -} - -/** - * Appends all elements yielded from results of [transform] function being invoked on each element of original array, to the given [destination]. - */ -public inline fun > Array.flatMapTo(destination: C, transform: (T) -> Iterable): C { - for (element in this) { - val list = transform(element) - destination.addAll(list) - } - return destination -} - -/** - * Appends all elements yielded from results of [transform] function being invoked on each element of original array, to the given [destination]. - */ -public inline fun > ByteArray.flatMapTo(destination: C, transform: (Byte) -> Iterable): C { - for (element in this) { - val list = transform(element) - destination.addAll(list) - } - return destination -} - -/** - * Appends all elements yielded from results of [transform] function being invoked on each element of original array, to the given [destination]. - */ -public inline fun > ShortArray.flatMapTo(destination: C, transform: (Short) -> Iterable): C { - for (element in this) { - val list = transform(element) - destination.addAll(list) - } - return destination -} - -/** - * Appends all elements yielded from results of [transform] function being invoked on each element of original array, to the given [destination]. - */ -public inline fun > IntArray.flatMapTo(destination: C, transform: (Int) -> Iterable): C { - for (element in this) { - val list = transform(element) - destination.addAll(list) - } - return destination -} - -/** - * Appends all elements yielded from results of [transform] function being invoked on each element of original array, to the given [destination]. - */ -public inline fun > LongArray.flatMapTo(destination: C, transform: (Long) -> Iterable): C { - for (element in this) { - val list = transform(element) - destination.addAll(list) - } - return destination -} - -/** - * Appends all elements yielded from results of [transform] function being invoked on each element of original array, to the given [destination]. - */ -public inline fun > FloatArray.flatMapTo(destination: C, transform: (Float) -> Iterable): C { - for (element in this) { - val list = transform(element) - destination.addAll(list) - } - return destination -} - -/** - * Appends all elements yielded from results of [transform] function being invoked on each element of original array, to the given [destination]. - */ -public inline fun > DoubleArray.flatMapTo(destination: C, transform: (Double) -> Iterable): C { - for (element in this) { - val list = transform(element) - destination.addAll(list) - } - return destination -} - -/** - * Appends all elements yielded from results of [transform] function being invoked on each element of original array, to the given [destination]. - */ -public inline fun > BooleanArray.flatMapTo(destination: C, transform: (Boolean) -> Iterable): C { - for (element in this) { - val list = transform(element) - destination.addAll(list) - } - return destination -} - -/** - * Appends all elements yielded from results of [transform] function being invoked on each element of original array, to the given [destination]. - */ -public inline fun > CharArray.flatMapTo(destination: C, transform: (Char) -> Iterable): C { - for (element in this) { - val list = transform(element) - destination.addAll(list) - } - return destination -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and returns a map where each group key is associated with a list of corresponding elements. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun Array.groupBy(keySelector: (T) -> K): Map> { - return groupByTo(LinkedHashMap>(), keySelector) -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and returns a map where each group key is associated with a list of corresponding elements. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun ByteArray.groupBy(keySelector: (Byte) -> K): Map> { - return groupByTo(LinkedHashMap>(), keySelector) -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and returns a map where each group key is associated with a list of corresponding elements. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun ShortArray.groupBy(keySelector: (Short) -> K): Map> { - return groupByTo(LinkedHashMap>(), keySelector) -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and returns a map where each group key is associated with a list of corresponding elements. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun IntArray.groupBy(keySelector: (Int) -> K): Map> { - return groupByTo(LinkedHashMap>(), keySelector) -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and returns a map where each group key is associated with a list of corresponding elements. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun LongArray.groupBy(keySelector: (Long) -> K): Map> { - return groupByTo(LinkedHashMap>(), keySelector) -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and returns a map where each group key is associated with a list of corresponding elements. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun FloatArray.groupBy(keySelector: (Float) -> K): Map> { - return groupByTo(LinkedHashMap>(), keySelector) -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and returns a map where each group key is associated with a list of corresponding elements. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun DoubleArray.groupBy(keySelector: (Double) -> K): Map> { - return groupByTo(LinkedHashMap>(), keySelector) -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and returns a map where each group key is associated with a list of corresponding elements. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun BooleanArray.groupBy(keySelector: (Boolean) -> K): Map> { - return groupByTo(LinkedHashMap>(), keySelector) -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and returns a map where each group key is associated with a list of corresponding elements. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun CharArray.groupBy(keySelector: (Char) -> K): Map> { - return groupByTo(LinkedHashMap>(), keySelector) -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and returns a map where each group key is associated with a list of corresponding values. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun Array.groupBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map> { - return groupByTo(LinkedHashMap>(), keySelector, valueTransform) -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and returns a map where each group key is associated with a list of corresponding values. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun ByteArray.groupBy(keySelector: (Byte) -> K, valueTransform: (Byte) -> V): Map> { - return groupByTo(LinkedHashMap>(), keySelector, valueTransform) -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and returns a map where each group key is associated with a list of corresponding values. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun ShortArray.groupBy(keySelector: (Short) -> K, valueTransform: (Short) -> V): Map> { - return groupByTo(LinkedHashMap>(), keySelector, valueTransform) -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and returns a map where each group key is associated with a list of corresponding values. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun IntArray.groupBy(keySelector: (Int) -> K, valueTransform: (Int) -> V): Map> { - return groupByTo(LinkedHashMap>(), keySelector, valueTransform) -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and returns a map where each group key is associated with a list of corresponding values. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun LongArray.groupBy(keySelector: (Long) -> K, valueTransform: (Long) -> V): Map> { - return groupByTo(LinkedHashMap>(), keySelector, valueTransform) -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and returns a map where each group key is associated with a list of corresponding values. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun FloatArray.groupBy(keySelector: (Float) -> K, valueTransform: (Float) -> V): Map> { - return groupByTo(LinkedHashMap>(), keySelector, valueTransform) -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and returns a map where each group key is associated with a list of corresponding values. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun DoubleArray.groupBy(keySelector: (Double) -> K, valueTransform: (Double) -> V): Map> { - return groupByTo(LinkedHashMap>(), keySelector, valueTransform) -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and returns a map where each group key is associated with a list of corresponding values. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun BooleanArray.groupBy(keySelector: (Boolean) -> K, valueTransform: (Boolean) -> V): Map> { - return groupByTo(LinkedHashMap>(), keySelector, valueTransform) -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and returns a map where each group key is associated with a list of corresponding values. - * - * The returned map preserves the entry iteration order of the keys produced from the original array. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun CharArray.groupBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map> { - return groupByTo(LinkedHashMap>(), keySelector, valueTransform) -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun >> Array.groupByTo(destination: M, keySelector: (T) -> K): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(element) - } - return destination -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun >> ByteArray.groupByTo(destination: M, keySelector: (Byte) -> K): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(element) - } - return destination -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun >> ShortArray.groupByTo(destination: M, keySelector: (Short) -> K): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(element) - } - return destination -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun >> IntArray.groupByTo(destination: M, keySelector: (Int) -> K): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(element) - } - return destination -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun >> LongArray.groupByTo(destination: M, keySelector: (Long) -> K): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(element) - } - return destination -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun >> FloatArray.groupByTo(destination: M, keySelector: (Float) -> K): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(element) - } - return destination -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun >> DoubleArray.groupByTo(destination: M, keySelector: (Double) -> K): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(element) - } - return destination -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun >> BooleanArray.groupByTo(destination: M, keySelector: (Boolean) -> K): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(element) - } - return destination -} - -/** - * Groups elements of the original array by the key returned by the given [keySelector] function - * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun >> CharArray.groupByTo(destination: M, keySelector: (Char) -> K): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(element) - } - return destination -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and puts to the [destination] map each group key associated with a list of corresponding values. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun >> Array.groupByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(valueTransform(element)) - } - return destination -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and puts to the [destination] map each group key associated with a list of corresponding values. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun >> ByteArray.groupByTo(destination: M, keySelector: (Byte) -> K, valueTransform: (Byte) -> V): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(valueTransform(element)) - } - return destination -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and puts to the [destination] map each group key associated with a list of corresponding values. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun >> ShortArray.groupByTo(destination: M, keySelector: (Short) -> K, valueTransform: (Short) -> V): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(valueTransform(element)) - } - return destination -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and puts to the [destination] map each group key associated with a list of corresponding values. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun >> IntArray.groupByTo(destination: M, keySelector: (Int) -> K, valueTransform: (Int) -> V): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(valueTransform(element)) - } - return destination -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and puts to the [destination] map each group key associated with a list of corresponding values. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun >> LongArray.groupByTo(destination: M, keySelector: (Long) -> K, valueTransform: (Long) -> V): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(valueTransform(element)) - } - return destination -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and puts to the [destination] map each group key associated with a list of corresponding values. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun >> FloatArray.groupByTo(destination: M, keySelector: (Float) -> K, valueTransform: (Float) -> V): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(valueTransform(element)) - } - return destination -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and puts to the [destination] map each group key associated with a list of corresponding values. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun >> DoubleArray.groupByTo(destination: M, keySelector: (Double) -> K, valueTransform: (Double) -> V): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(valueTransform(element)) - } - return destination -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and puts to the [destination] map each group key associated with a list of corresponding values. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun >> BooleanArray.groupByTo(destination: M, keySelector: (Boolean) -> K, valueTransform: (Boolean) -> V): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(valueTransform(element)) - } - return destination -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original array - * by the key returned by the given [keySelector] function applied to the element - * and puts to the [destination] map each group key associated with a list of corresponding values. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun >> CharArray.groupByTo(destination: M, keySelector: (Char) -> K, valueTransform: (Char) -> V): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(valueTransform(element)) - } - return destination -} - -/** - * Creates a [Grouping] source from an array to be used later with one of group-and-fold operations - * using the specified [keySelector] function to extract a key from each element. - * - * @sample samples.collections.Collections.Transformations.groupingByEachCount - */ -@SinceKotlin("1.1") -public inline fun Array.groupingBy(crossinline keySelector: (T) -> K): Grouping { - return object : Grouping { - override fun sourceIterator(): Iterator = this@groupingBy.iterator() - override fun keyOf(element: T): K = keySelector(element) - } -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element in the original array. - */ -public inline fun Array.map(transform: (T) -> R): List { - return mapTo(ArrayList(size), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element in the original array. - */ -public inline fun ByteArray.map(transform: (Byte) -> R): List { - return mapTo(ArrayList(size), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element in the original array. - */ -public inline fun ShortArray.map(transform: (Short) -> R): List { - return mapTo(ArrayList(size), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element in the original array. - */ -public inline fun IntArray.map(transform: (Int) -> R): List { - return mapTo(ArrayList(size), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element in the original array. - */ -public inline fun LongArray.map(transform: (Long) -> R): List { - return mapTo(ArrayList(size), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element in the original array. - */ -public inline fun FloatArray.map(transform: (Float) -> R): List { - return mapTo(ArrayList(size), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element in the original array. - */ -public inline fun DoubleArray.map(transform: (Double) -> R): List { - return mapTo(ArrayList(size), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element in the original array. - */ -public inline fun BooleanArray.map(transform: (Boolean) -> R): List { - return mapTo(ArrayList(size), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element in the original array. - */ -public inline fun CharArray.map(transform: (Char) -> R): List { - return mapTo(ArrayList(size), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element and its index in the original array. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun Array.mapIndexed(transform: (index: Int, T) -> R): List { - return mapIndexedTo(ArrayList(size), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element and its index in the original array. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun ByteArray.mapIndexed(transform: (index: Int, Byte) -> R): List { - return mapIndexedTo(ArrayList(size), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element and its index in the original array. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun ShortArray.mapIndexed(transform: (index: Int, Short) -> R): List { - return mapIndexedTo(ArrayList(size), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element and its index in the original array. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun IntArray.mapIndexed(transform: (index: Int, Int) -> R): List { - return mapIndexedTo(ArrayList(size), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element and its index in the original array. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun LongArray.mapIndexed(transform: (index: Int, Long) -> R): List { - return mapIndexedTo(ArrayList(size), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element and its index in the original array. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun FloatArray.mapIndexed(transform: (index: Int, Float) -> R): List { - return mapIndexedTo(ArrayList(size), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element and its index in the original array. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun DoubleArray.mapIndexed(transform: (index: Int, Double) -> R): List { - return mapIndexedTo(ArrayList(size), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element and its index in the original array. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun BooleanArray.mapIndexed(transform: (index: Int, Boolean) -> R): List { - return mapIndexedTo(ArrayList(size), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element and its index in the original array. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun CharArray.mapIndexed(transform: (index: Int, Char) -> R): List { - return mapIndexedTo(ArrayList(size), transform) -} - -/** - * Returns a list containing only the non-null results of applying the given [transform] function - * to each element and its index in the original array. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun Array.mapIndexedNotNull(transform: (index: Int, T) -> R?): List { - return mapIndexedNotNullTo(ArrayList(), transform) -} - -/** - * Applies the given [transform] function to each element and its index in the original array - * and appends only the non-null results to the given [destination]. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun > Array.mapIndexedNotNullTo(destination: C, transform: (index: Int, T) -> R?): C { - forEachIndexed { index, element -> transform(index, element)?.let { destination.add(it) } } - return destination -} - -/** - * Applies the given [transform] function to each element and its index in the original array - * and appends the results to the given [destination]. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun > Array.mapIndexedTo(destination: C, transform: (index: Int, T) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Applies the given [transform] function to each element and its index in the original array - * and appends the results to the given [destination]. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun > ByteArray.mapIndexedTo(destination: C, transform: (index: Int, Byte) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Applies the given [transform] function to each element and its index in the original array - * and appends the results to the given [destination]. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun > ShortArray.mapIndexedTo(destination: C, transform: (index: Int, Short) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Applies the given [transform] function to each element and its index in the original array - * and appends the results to the given [destination]. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun > IntArray.mapIndexedTo(destination: C, transform: (index: Int, Int) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Applies the given [transform] function to each element and its index in the original array - * and appends the results to the given [destination]. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun > LongArray.mapIndexedTo(destination: C, transform: (index: Int, Long) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Applies the given [transform] function to each element and its index in the original array - * and appends the results to the given [destination]. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun > FloatArray.mapIndexedTo(destination: C, transform: (index: Int, Float) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Applies the given [transform] function to each element and its index in the original array - * and appends the results to the given [destination]. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun > DoubleArray.mapIndexedTo(destination: C, transform: (index: Int, Double) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Applies the given [transform] function to each element and its index in the original array - * and appends the results to the given [destination]. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun > BooleanArray.mapIndexedTo(destination: C, transform: (index: Int, Boolean) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Applies the given [transform] function to each element and its index in the original array - * and appends the results to the given [destination]. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun > CharArray.mapIndexedTo(destination: C, transform: (index: Int, Char) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Returns a list containing only the non-null results of applying the given [transform] function - * to each element in the original array. - */ -public inline fun Array.mapNotNull(transform: (T) -> R?): List { - return mapNotNullTo(ArrayList(), transform) -} - -/** - * Applies the given [transform] function to each element in the original array - * and appends only the non-null results to the given [destination]. - */ -public inline fun > Array.mapNotNullTo(destination: C, transform: (T) -> R?): C { - forEach { element -> transform(element)?.let { destination.add(it) } } - return destination -} - -/** - * Applies the given [transform] function to each element of the original array - * and appends the results to the given [destination]. - */ -public inline fun > Array.mapTo(destination: C, transform: (T) -> R): C { - for (item in this) - destination.add(transform(item)) - return destination -} - -/** - * Applies the given [transform] function to each element of the original array - * and appends the results to the given [destination]. - */ -public inline fun > ByteArray.mapTo(destination: C, transform: (Byte) -> R): C { - for (item in this) - destination.add(transform(item)) - return destination -} - -/** - * Applies the given [transform] function to each element of the original array - * and appends the results to the given [destination]. - */ -public inline fun > ShortArray.mapTo(destination: C, transform: (Short) -> R): C { - for (item in this) - destination.add(transform(item)) - return destination -} - -/** - * Applies the given [transform] function to each element of the original array - * and appends the results to the given [destination]. - */ -public inline fun > IntArray.mapTo(destination: C, transform: (Int) -> R): C { - for (item in this) - destination.add(transform(item)) - return destination -} - -/** - * Applies the given [transform] function to each element of the original array - * and appends the results to the given [destination]. - */ -public inline fun > LongArray.mapTo(destination: C, transform: (Long) -> R): C { - for (item in this) - destination.add(transform(item)) - return destination -} - -/** - * Applies the given [transform] function to each element of the original array - * and appends the results to the given [destination]. - */ -public inline fun > FloatArray.mapTo(destination: C, transform: (Float) -> R): C { - for (item in this) - destination.add(transform(item)) - return destination -} - -/** - * Applies the given [transform] function to each element of the original array - * and appends the results to the given [destination]. - */ -public inline fun > DoubleArray.mapTo(destination: C, transform: (Double) -> R): C { - for (item in this) - destination.add(transform(item)) - return destination -} - -/** - * Applies the given [transform] function to each element of the original array - * and appends the results to the given [destination]. - */ -public inline fun > BooleanArray.mapTo(destination: C, transform: (Boolean) -> R): C { - for (item in this) - destination.add(transform(item)) - return destination -} - -/** - * Applies the given [transform] function to each element of the original array - * and appends the results to the given [destination]. - */ -public inline fun > CharArray.mapTo(destination: C, transform: (Char) -> R): C { - for (item in this) - destination.add(transform(item)) - return destination -} - -/** - * Returns a lazy [Iterable] of [IndexedValue] for each element of the original array. - */ -public fun Array.withIndex(): Iterable> { - return IndexingIterable { iterator() } -} - -/** - * Returns a lazy [Iterable] of [IndexedValue] for each element of the original array. - */ -public fun ByteArray.withIndex(): Iterable> { - return IndexingIterable { iterator() } -} - -/** - * Returns a lazy [Iterable] of [IndexedValue] for each element of the original array. - */ -public fun ShortArray.withIndex(): Iterable> { - return IndexingIterable { iterator() } -} - -/** - * Returns a lazy [Iterable] of [IndexedValue] for each element of the original array. - */ -public fun IntArray.withIndex(): Iterable> { - return IndexingIterable { iterator() } -} - -/** - * Returns a lazy [Iterable] of [IndexedValue] for each element of the original array. - */ -public fun LongArray.withIndex(): Iterable> { - return IndexingIterable { iterator() } -} - -/** - * Returns a lazy [Iterable] of [IndexedValue] for each element of the original array. - */ -public fun FloatArray.withIndex(): Iterable> { - return IndexingIterable { iterator() } -} - -/** - * Returns a lazy [Iterable] of [IndexedValue] for each element of the original array. - */ -public fun DoubleArray.withIndex(): Iterable> { - return IndexingIterable { iterator() } -} - -/** - * Returns a lazy [Iterable] of [IndexedValue] for each element of the original array. - */ -public fun BooleanArray.withIndex(): Iterable> { - return IndexingIterable { iterator() } -} - -/** - * Returns a lazy [Iterable] of [IndexedValue] for each element of the original array. - */ -public fun CharArray.withIndex(): Iterable> { - return IndexingIterable { iterator() } -} - -/** - * Returns a list containing only distinct elements from the given array. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public fun Array.distinct(): List { - return this.toMutableSet().toList() -} - -/** - * Returns a list containing only distinct elements from the given array. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public fun ByteArray.distinct(): List { - return this.toMutableSet().toList() -} - -/** - * Returns a list containing only distinct elements from the given array. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public fun ShortArray.distinct(): List { - return this.toMutableSet().toList() -} - -/** - * Returns a list containing only distinct elements from the given array. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public fun IntArray.distinct(): List { - return this.toMutableSet().toList() -} - -/** - * Returns a list containing only distinct elements from the given array. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public fun LongArray.distinct(): List { - return this.toMutableSet().toList() -} - -/** - * Returns a list containing only distinct elements from the given array. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public fun FloatArray.distinct(): List { - return this.toMutableSet().toList() -} - -/** - * Returns a list containing only distinct elements from the given array. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public fun DoubleArray.distinct(): List { - return this.toMutableSet().toList() -} - -/** - * Returns a list containing only distinct elements from the given array. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public fun BooleanArray.distinct(): List { - return this.toMutableSet().toList() -} - -/** - * Returns a list containing only distinct elements from the given array. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public fun CharArray.distinct(): List { - return this.toMutableSet().toList() -} - -/** - * Returns a list containing only elements from the given array - * having distinct keys returned by the given [selector] function. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public inline fun Array.distinctBy(selector: (T) -> K): List { - val set = HashSet() - val list = ArrayList() - for (e in this) { - val key = selector(e) - if (set.add(key)) - list.add(e) - } - return list -} - -/** - * Returns a list containing only elements from the given array - * having distinct keys returned by the given [selector] function. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public inline fun ByteArray.distinctBy(selector: (Byte) -> K): List { - val set = HashSet() - val list = ArrayList() - for (e in this) { - val key = selector(e) - if (set.add(key)) - list.add(e) - } - return list -} - -/** - * Returns a list containing only elements from the given array - * having distinct keys returned by the given [selector] function. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public inline fun ShortArray.distinctBy(selector: (Short) -> K): List { - val set = HashSet() - val list = ArrayList() - for (e in this) { - val key = selector(e) - if (set.add(key)) - list.add(e) - } - return list -} - -/** - * Returns a list containing only elements from the given array - * having distinct keys returned by the given [selector] function. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public inline fun IntArray.distinctBy(selector: (Int) -> K): List { - val set = HashSet() - val list = ArrayList() - for (e in this) { - val key = selector(e) - if (set.add(key)) - list.add(e) - } - return list -} - -/** - * Returns a list containing only elements from the given array - * having distinct keys returned by the given [selector] function. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public inline fun LongArray.distinctBy(selector: (Long) -> K): List { - val set = HashSet() - val list = ArrayList() - for (e in this) { - val key = selector(e) - if (set.add(key)) - list.add(e) - } - return list -} - -/** - * Returns a list containing only elements from the given array - * having distinct keys returned by the given [selector] function. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public inline fun FloatArray.distinctBy(selector: (Float) -> K): List { - val set = HashSet() - val list = ArrayList() - for (e in this) { - val key = selector(e) - if (set.add(key)) - list.add(e) - } - return list -} - -/** - * Returns a list containing only elements from the given array - * having distinct keys returned by the given [selector] function. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public inline fun DoubleArray.distinctBy(selector: (Double) -> K): List { - val set = HashSet() - val list = ArrayList() - for (e in this) { - val key = selector(e) - if (set.add(key)) - list.add(e) - } - return list -} - -/** - * Returns a list containing only elements from the given array - * having distinct keys returned by the given [selector] function. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public inline fun BooleanArray.distinctBy(selector: (Boolean) -> K): List { - val set = HashSet() - val list = ArrayList() - for (e in this) { - val key = selector(e) - if (set.add(key)) - list.add(e) - } - return list -} - -/** - * Returns a list containing only elements from the given array - * having distinct keys returned by the given [selector] function. - * - * The elements in the resulting list are in the same order as they were in the source array. - */ -public inline fun CharArray.distinctBy(selector: (Char) -> K): List { - val set = HashSet() - val list = ArrayList() - for (e in this) { - val key = selector(e) - if (set.add(key)) - list.add(e) - } - return list -} - -/** - * Returns a set containing all elements that are contained by both this set and the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun Array.intersect(other: Iterable): Set { - val set = this.toMutableSet() - set.retainAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by both this set and the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun ByteArray.intersect(other: Iterable): Set { - val set = this.toMutableSet() - set.retainAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by both this set and the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun ShortArray.intersect(other: Iterable): Set { - val set = this.toMutableSet() - set.retainAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by both this set and the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun IntArray.intersect(other: Iterable): Set { - val set = this.toMutableSet() - set.retainAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by both this set and the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun LongArray.intersect(other: Iterable): Set { - val set = this.toMutableSet() - set.retainAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by both this set and the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun FloatArray.intersect(other: Iterable): Set { - val set = this.toMutableSet() - set.retainAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by both this set and the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun DoubleArray.intersect(other: Iterable): Set { - val set = this.toMutableSet() - set.retainAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by both this set and the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun BooleanArray.intersect(other: Iterable): Set { - val set = this.toMutableSet() - set.retainAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by both this set and the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun CharArray.intersect(other: Iterable): Set { - val set = this.toMutableSet() - set.retainAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by this array and not contained by the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun Array.subtract(other: Iterable): Set { - val set = this.toMutableSet() - set.removeAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by this array and not contained by the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun ByteArray.subtract(other: Iterable): Set { - val set = this.toMutableSet() - set.removeAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by this array and not contained by the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun ShortArray.subtract(other: Iterable): Set { - val set = this.toMutableSet() - set.removeAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by this array and not contained by the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun IntArray.subtract(other: Iterable): Set { - val set = this.toMutableSet() - set.removeAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by this array and not contained by the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun LongArray.subtract(other: Iterable): Set { - val set = this.toMutableSet() - set.removeAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by this array and not contained by the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun FloatArray.subtract(other: Iterable): Set { - val set = this.toMutableSet() - set.removeAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by this array and not contained by the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun DoubleArray.subtract(other: Iterable): Set { - val set = this.toMutableSet() - set.removeAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by this array and not contained by the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun BooleanArray.subtract(other: Iterable): Set { - val set = this.toMutableSet() - set.removeAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by this array and not contained by the specified collection. - * - * The returned set preserves the element iteration order of the original array. - */ -public infix fun CharArray.subtract(other: Iterable): Set { - val set = this.toMutableSet() - set.removeAll(other) - return set -} - -/** - * Returns a mutable set containing all distinct elements from the given array. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun Array.toMutableSet(): MutableSet { - val set = LinkedHashSet(mapCapacity(size)) - for (item in this) set.add(item) - return set -} - -/** - * Returns a mutable set containing all distinct elements from the given array. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun ByteArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(mapCapacity(size)) - for (item in this) set.add(item) - return set -} - -/** - * Returns a mutable set containing all distinct elements from the given array. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun ShortArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(mapCapacity(size)) - for (item in this) set.add(item) - return set -} - -/** - * Returns a mutable set containing all distinct elements from the given array. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun IntArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(mapCapacity(size)) - for (item in this) set.add(item) - return set -} - -/** - * Returns a mutable set containing all distinct elements from the given array. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun LongArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(mapCapacity(size)) - for (item in this) set.add(item) - return set -} - -/** - * Returns a mutable set containing all distinct elements from the given array. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun FloatArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(mapCapacity(size)) - for (item in this) set.add(item) - return set -} - -/** - * Returns a mutable set containing all distinct elements from the given array. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun DoubleArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(mapCapacity(size)) - for (item in this) set.add(item) - return set -} - -/** - * Returns a mutable set containing all distinct elements from the given array. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun BooleanArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(mapCapacity(size)) - for (item in this) set.add(item) - return set -} - -/** - * Returns a mutable set containing all distinct elements from the given array. - * - * The returned set preserves the element iteration order of the original array. - */ -public fun CharArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(mapCapacity(size)) - for (item in this) set.add(item) - return set -} - -/** - * Returns a set containing all distinct elements from both collections. - * - * The returned set preserves the element iteration order of the original array. - * Those elements of the [other] collection that are unique are iterated in the end - * in the order of the [other] collection. - */ -public infix fun Array.union(other: Iterable): Set { - val set = this.toMutableSet() - set.addAll(other) - return set -} - -/** - * Returns a set containing all distinct elements from both collections. - * - * The returned set preserves the element iteration order of the original array. - * Those elements of the [other] collection that are unique are iterated in the end - * in the order of the [other] collection. - */ -public infix fun ByteArray.union(other: Iterable): Set { - val set = this.toMutableSet() - set.addAll(other) - return set -} - -/** - * Returns a set containing all distinct elements from both collections. - * - * The returned set preserves the element iteration order of the original array. - * Those elements of the [other] collection that are unique are iterated in the end - * in the order of the [other] collection. - */ -public infix fun ShortArray.union(other: Iterable): Set { - val set = this.toMutableSet() - set.addAll(other) - return set -} - -/** - * Returns a set containing all distinct elements from both collections. - * - * The returned set preserves the element iteration order of the original array. - * Those elements of the [other] collection that are unique are iterated in the end - * in the order of the [other] collection. - */ -public infix fun IntArray.union(other: Iterable): Set { - val set = this.toMutableSet() - set.addAll(other) - return set -} - -/** - * Returns a set containing all distinct elements from both collections. - * - * The returned set preserves the element iteration order of the original array. - * Those elements of the [other] collection that are unique are iterated in the end - * in the order of the [other] collection. - */ -public infix fun LongArray.union(other: Iterable): Set { - val set = this.toMutableSet() - set.addAll(other) - return set -} - -/** - * Returns a set containing all distinct elements from both collections. - * - * The returned set preserves the element iteration order of the original array. - * Those elements of the [other] collection that are unique are iterated in the end - * in the order of the [other] collection. - */ -public infix fun FloatArray.union(other: Iterable): Set { - val set = this.toMutableSet() - set.addAll(other) - return set -} - -/** - * Returns a set containing all distinct elements from both collections. - * - * The returned set preserves the element iteration order of the original array. - * Those elements of the [other] collection that are unique are iterated in the end - * in the order of the [other] collection. - */ -public infix fun DoubleArray.union(other: Iterable): Set { - val set = this.toMutableSet() - set.addAll(other) - return set -} - -/** - * Returns a set containing all distinct elements from both collections. - * - * The returned set preserves the element iteration order of the original array. - * Those elements of the [other] collection that are unique are iterated in the end - * in the order of the [other] collection. - */ -public infix fun BooleanArray.union(other: Iterable): Set { - val set = this.toMutableSet() - set.addAll(other) - return set -} - -/** - * Returns a set containing all distinct elements from both collections. - * - * The returned set preserves the element iteration order of the original array. - * Those elements of the [other] collection that are unique are iterated in the end - * in the order of the [other] collection. - */ -public infix fun CharArray.union(other: Iterable): Set { - val set = this.toMutableSet() - set.addAll(other) - return set -} - -/** - * Returns `true` if all elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.all - */ -public inline fun Array.all(predicate: (T) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if all elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.all - */ -public inline fun ByteArray.all(predicate: (Byte) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if all elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.all - */ -public inline fun ShortArray.all(predicate: (Short) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if all elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.all - */ -public inline fun IntArray.all(predicate: (Int) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if all elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.all - */ -public inline fun LongArray.all(predicate: (Long) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if all elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.all - */ -public inline fun FloatArray.all(predicate: (Float) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if all elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.all - */ -public inline fun DoubleArray.all(predicate: (Double) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if all elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.all - */ -public inline fun BooleanArray.all(predicate: (Boolean) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if all elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.all - */ -public inline fun CharArray.all(predicate: (Char) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if array has at least one element. - * - * @sample samples.collections.Collections.Aggregates.any - */ -public fun Array.any(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if array has at least one element. - * - * @sample samples.collections.Collections.Aggregates.any - */ -public fun ByteArray.any(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if array has at least one element. - * - * @sample samples.collections.Collections.Aggregates.any - */ -public fun ShortArray.any(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if array has at least one element. - * - * @sample samples.collections.Collections.Aggregates.any - */ -public fun IntArray.any(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if array has at least one element. - * - * @sample samples.collections.Collections.Aggregates.any - */ -public fun LongArray.any(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if array has at least one element. - * - * @sample samples.collections.Collections.Aggregates.any - */ -public fun FloatArray.any(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if array has at least one element. - * - * @sample samples.collections.Collections.Aggregates.any - */ -public fun DoubleArray.any(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if array has at least one element. - * - * @sample samples.collections.Collections.Aggregates.any - */ -public fun BooleanArray.any(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if array has at least one element. - * - * @sample samples.collections.Collections.Aggregates.any - */ -public fun CharArray.any(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if at least one element matches the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.anyWithPredicate - */ -public inline fun Array.any(predicate: (T) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return true - return false -} - -/** - * Returns `true` if at least one element matches the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.anyWithPredicate - */ -public inline fun ByteArray.any(predicate: (Byte) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return true - return false -} - -/** - * Returns `true` if at least one element matches the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.anyWithPredicate - */ -public inline fun ShortArray.any(predicate: (Short) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return true - return false -} - -/** - * Returns `true` if at least one element matches the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.anyWithPredicate - */ -public inline fun IntArray.any(predicate: (Int) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return true - return false -} - -/** - * Returns `true` if at least one element matches the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.anyWithPredicate - */ -public inline fun LongArray.any(predicate: (Long) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return true - return false -} - -/** - * Returns `true` if at least one element matches the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.anyWithPredicate - */ -public inline fun FloatArray.any(predicate: (Float) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return true - return false -} - -/** - * Returns `true` if at least one element matches the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.anyWithPredicate - */ -public inline fun DoubleArray.any(predicate: (Double) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return true - return false -} - -/** - * Returns `true` if at least one element matches the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.anyWithPredicate - */ -public inline fun BooleanArray.any(predicate: (Boolean) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return true - return false -} - -/** - * Returns `true` if at least one element matches the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.anyWithPredicate - */ -public inline fun CharArray.any(predicate: (Char) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return true - return false -} - -/** - * Returns the number of elements in this array. - */ -@kotlin.internal.InlineOnly -public inline fun Array.count(): Int { - return size -} - -/** - * Returns the number of elements in this array. - */ -@kotlin.internal.InlineOnly -public inline fun ByteArray.count(): Int { - return size -} - -/** - * Returns the number of elements in this array. - */ -@kotlin.internal.InlineOnly -public inline fun ShortArray.count(): Int { - return size -} - -/** - * Returns the number of elements in this array. - */ -@kotlin.internal.InlineOnly -public inline fun IntArray.count(): Int { - return size -} - -/** - * Returns the number of elements in this array. - */ -@kotlin.internal.InlineOnly -public inline fun LongArray.count(): Int { - return size -} - -/** - * Returns the number of elements in this array. - */ -@kotlin.internal.InlineOnly -public inline fun FloatArray.count(): Int { - return size -} - -/** - * Returns the number of elements in this array. - */ -@kotlin.internal.InlineOnly -public inline fun DoubleArray.count(): Int { - return size -} - -/** - * Returns the number of elements in this array. - */ -@kotlin.internal.InlineOnly -public inline fun BooleanArray.count(): Int { - return size -} - -/** - * Returns the number of elements in this array. - */ -@kotlin.internal.InlineOnly -public inline fun CharArray.count(): Int { - return size -} - -/** - * Returns the number of elements matching the given [predicate]. - */ -public inline fun Array.count(predicate: (T) -> Boolean): Int { - var count = 0 - for (element in this) if (predicate(element)) count++ - return count -} - -/** - * Returns the number of elements matching the given [predicate]. - */ -public inline fun ByteArray.count(predicate: (Byte) -> Boolean): Int { - var count = 0 - for (element in this) if (predicate(element)) count++ - return count -} - -/** - * Returns the number of elements matching the given [predicate]. - */ -public inline fun ShortArray.count(predicate: (Short) -> Boolean): Int { - var count = 0 - for (element in this) if (predicate(element)) count++ - return count -} - -/** - * Returns the number of elements matching the given [predicate]. - */ -public inline fun IntArray.count(predicate: (Int) -> Boolean): Int { - var count = 0 - for (element in this) if (predicate(element)) count++ - return count -} - -/** - * Returns the number of elements matching the given [predicate]. - */ -public inline fun LongArray.count(predicate: (Long) -> Boolean): Int { - var count = 0 - for (element in this) if (predicate(element)) count++ - return count -} - -/** - * Returns the number of elements matching the given [predicate]. - */ -public inline fun FloatArray.count(predicate: (Float) -> Boolean): Int { - var count = 0 - for (element in this) if (predicate(element)) count++ - return count -} - -/** - * Returns the number of elements matching the given [predicate]. - */ -public inline fun DoubleArray.count(predicate: (Double) -> Boolean): Int { - var count = 0 - for (element in this) if (predicate(element)) count++ - return count -} - -/** - * Returns the number of elements matching the given [predicate]. - */ -public inline fun BooleanArray.count(predicate: (Boolean) -> Boolean): Int { - var count = 0 - for (element in this) if (predicate(element)) count++ - return count -} - -/** - * Returns the number of elements matching the given [predicate]. - */ -public inline fun CharArray.count(predicate: (Char) -> Boolean): Int { - var count = 0 - for (element in this) if (predicate(element)) count++ - return count -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun Array.fold(initial: R, operation: (acc: R, T) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun ByteArray.fold(initial: R, operation: (acc: R, Byte) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun ShortArray.fold(initial: R, operation: (acc: R, Short) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun IntArray.fold(initial: R, operation: (acc: R, Int) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun LongArray.fold(initial: R, operation: (acc: R, Long) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun FloatArray.fold(initial: R, operation: (acc: R, Float) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun DoubleArray.fold(initial: R, operation: (acc: R, Double) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun BooleanArray.fold(initial: R, operation: (acc: R, Boolean) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun CharArray.fold(initial: R, operation: (acc: R, Char) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself, and calculates the next accumulator value. - */ -public inline fun Array.foldIndexed(initial: R, operation: (index: Int, acc: R, T) -> R): R { - var index = 0 - var accumulator = initial - for (element in this) accumulator = operation(index++, accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself, and calculates the next accumulator value. - */ -public inline fun ByteArray.foldIndexed(initial: R, operation: (index: Int, acc: R, Byte) -> R): R { - var index = 0 - var accumulator = initial - for (element in this) accumulator = operation(index++, accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself, and calculates the next accumulator value. - */ -public inline fun ShortArray.foldIndexed(initial: R, operation: (index: Int, acc: R, Short) -> R): R { - var index = 0 - var accumulator = initial - for (element in this) accumulator = operation(index++, accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself, and calculates the next accumulator value. - */ -public inline fun IntArray.foldIndexed(initial: R, operation: (index: Int, acc: R, Int) -> R): R { - var index = 0 - var accumulator = initial - for (element in this) accumulator = operation(index++, accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself, and calculates the next accumulator value. - */ -public inline fun LongArray.foldIndexed(initial: R, operation: (index: Int, acc: R, Long) -> R): R { - var index = 0 - var accumulator = initial - for (element in this) accumulator = operation(index++, accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself, and calculates the next accumulator value. - */ -public inline fun FloatArray.foldIndexed(initial: R, operation: (index: Int, acc: R, Float) -> R): R { - var index = 0 - var accumulator = initial - for (element in this) accumulator = operation(index++, accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself, and calculates the next accumulator value. - */ -public inline fun DoubleArray.foldIndexed(initial: R, operation: (index: Int, acc: R, Double) -> R): R { - var index = 0 - var accumulator = initial - for (element in this) accumulator = operation(index++, accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself, and calculates the next accumulator value. - */ -public inline fun BooleanArray.foldIndexed(initial: R, operation: (index: Int, acc: R, Boolean) -> R): R { - var index = 0 - var accumulator = initial - for (element in this) accumulator = operation(index++, accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself, and calculates the next accumulator value. - */ -public inline fun CharArray.foldIndexed(initial: R, operation: (index: Int, acc: R, Char) -> R): R { - var index = 0 - var accumulator = initial - for (element in this) accumulator = operation(index++, accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun Array.foldRight(initial: R, operation: (T, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun ByteArray.foldRight(initial: R, operation: (Byte, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun ShortArray.foldRight(initial: R, operation: (Short, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun IntArray.foldRight(initial: R, operation: (Int, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun LongArray.foldRight(initial: R, operation: (Long, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun FloatArray.foldRight(initial: R, operation: (Float, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun DoubleArray.foldRight(initial: R, operation: (Double, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun BooleanArray.foldRight(initial: R, operation: (Boolean, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun CharArray.foldRight(initial: R, operation: (Char, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun Array.foldRightIndexed(initial: R, operation: (index: Int, T, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun ByteArray.foldRightIndexed(initial: R, operation: (index: Int, Byte, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun ShortArray.foldRightIndexed(initial: R, operation: (index: Int, Short, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun IntArray.foldRightIndexed(initial: R, operation: (index: Int, Int, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun LongArray.foldRightIndexed(initial: R, operation: (index: Int, Long, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun FloatArray.foldRightIndexed(initial: R, operation: (index: Int, Float, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun DoubleArray.foldRightIndexed(initial: R, operation: (index: Int, Double, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun BooleanArray.foldRightIndexed(initial: R, operation: (index: Int, Boolean, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun CharArray.foldRightIndexed(initial: R, operation: (index: Int, Char, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Performs the given [action] on each element. - */ -public inline fun Array.forEach(action: (T) -> Unit): Unit { - for (element in this) action(element) -} - -/** - * Performs the given [action] on each element. - */ -public inline fun ByteArray.forEach(action: (Byte) -> Unit): Unit { - for (element in this) action(element) -} - -/** - * Performs the given [action] on each element. - */ -public inline fun ShortArray.forEach(action: (Short) -> Unit): Unit { - for (element in this) action(element) -} - -/** - * Performs the given [action] on each element. - */ -public inline fun IntArray.forEach(action: (Int) -> Unit): Unit { - for (element in this) action(element) -} - -/** - * Performs the given [action] on each element. - */ -public inline fun LongArray.forEach(action: (Long) -> Unit): Unit { - for (element in this) action(element) -} - -/** - * Performs the given [action] on each element. - */ -public inline fun FloatArray.forEach(action: (Float) -> Unit): Unit { - for (element in this) action(element) -} - -/** - * Performs the given [action] on each element. - */ -public inline fun DoubleArray.forEach(action: (Double) -> Unit): Unit { - for (element in this) action(element) -} - -/** - * Performs the given [action] on each element. - */ -public inline fun BooleanArray.forEach(action: (Boolean) -> Unit): Unit { - for (element in this) action(element) -} - -/** - * Performs the given [action] on each element. - */ -public inline fun CharArray.forEach(action: (Char) -> Unit): Unit { - for (element in this) action(element) -} - -/** - * Performs the given [action] on each element, providing sequential index with the element. - * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. - */ -public inline fun Array.forEachIndexed(action: (index: Int, T) -> Unit): Unit { - var index = 0 - for (item in this) action(index++, item) -} - -/** - * Performs the given [action] on each element, providing sequential index with the element. - * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. - */ -public inline fun ByteArray.forEachIndexed(action: (index: Int, Byte) -> Unit): Unit { - var index = 0 - for (item in this) action(index++, item) -} - -/** - * Performs the given [action] on each element, providing sequential index with the element. - * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. - */ -public inline fun ShortArray.forEachIndexed(action: (index: Int, Short) -> Unit): Unit { - var index = 0 - for (item in this) action(index++, item) -} - -/** - * Performs the given [action] on each element, providing sequential index with the element. - * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. - */ -public inline fun IntArray.forEachIndexed(action: (index: Int, Int) -> Unit): Unit { - var index = 0 - for (item in this) action(index++, item) -} - -/** - * Performs the given [action] on each element, providing sequential index with the element. - * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. - */ -public inline fun LongArray.forEachIndexed(action: (index: Int, Long) -> Unit): Unit { - var index = 0 - for (item in this) action(index++, item) -} - -/** - * Performs the given [action] on each element, providing sequential index with the element. - * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. - */ -public inline fun FloatArray.forEachIndexed(action: (index: Int, Float) -> Unit): Unit { - var index = 0 - for (item in this) action(index++, item) -} - -/** - * Performs the given [action] on each element, providing sequential index with the element. - * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. - */ -public inline fun DoubleArray.forEachIndexed(action: (index: Int, Double) -> Unit): Unit { - var index = 0 - for (item in this) action(index++, item) -} - -/** - * Performs the given [action] on each element, providing sequential index with the element. - * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. - */ -public inline fun BooleanArray.forEachIndexed(action: (index: Int, Boolean) -> Unit): Unit { - var index = 0 - for (item in this) action(index++, item) -} - -/** - * Performs the given [action] on each element, providing sequential index with the element. - * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. - */ -public inline fun CharArray.forEachIndexed(action: (index: Int, Char) -> Unit): Unit { - var index = 0 - for (item in this) action(index++, item) -} - -/** - * Returns the largest element or `null` if there are no elements. - * - * If any of elements is `NaN` returns `NaN`. - */ -@SinceKotlin("1.1") -public fun Array.max(): Double? { - if (isEmpty()) return null - var max = this[0] - if (max.isNaN()) return max - for (i in 1..lastIndex) { - val e = this[i] - if (e.isNaN()) return e - if (max < e) max = e - } - return max -} - -/** - * Returns the largest element or `null` if there are no elements. - * - * If any of elements is `NaN` returns `NaN`. - */ -@SinceKotlin("1.1") -public fun Array.max(): Float? { - if (isEmpty()) return null - var max = this[0] - if (max.isNaN()) return max - for (i in 1..lastIndex) { - val e = this[i] - if (e.isNaN()) return e - if (max < e) max = e - } - return max -} - -/** - * Returns the largest element or `null` if there are no elements. - */ -public fun > Array.max(): T? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (max < e) max = e - } - return max -} - -/** - * Returns the largest element or `null` if there are no elements. - */ -public fun ByteArray.max(): Byte? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (max < e) max = e - } - return max -} - -/** - * Returns the largest element or `null` if there are no elements. - */ -public fun ShortArray.max(): Short? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (max < e) max = e - } - return max -} - -/** - * Returns the largest element or `null` if there are no elements. - */ -public fun IntArray.max(): Int? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (max < e) max = e - } - return max -} - -/** - * Returns the largest element or `null` if there are no elements. - */ -public fun LongArray.max(): Long? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (max < e) max = e - } - return max -} - -/** - * Returns the largest element or `null` if there are no elements. - * - * If any of elements is `NaN` returns `NaN`. - */ -public fun FloatArray.max(): Float? { - if (isEmpty()) return null - var max = this[0] - if (max.isNaN()) return max - for (i in 1..lastIndex) { - val e = this[i] - if (e.isNaN()) return e - if (max < e) max = e - } - return max -} - -/** - * Returns the largest element or `null` if there are no elements. - * - * If any of elements is `NaN` returns `NaN`. - */ -public fun DoubleArray.max(): Double? { - if (isEmpty()) return null - var max = this[0] - if (max.isNaN()) return max - for (i in 1..lastIndex) { - val e = this[i] - if (e.isNaN()) return e - if (max < e) max = e - } - return max -} - -/** - * Returns the largest element or `null` if there are no elements. - */ -public fun CharArray.max(): Char? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (max < e) max = e - } - return max -} - -/** - * Returns the first element yielding the largest value of the given function or `null` if there are no elements. - */ -public inline fun > Array.maxBy(selector: (T) -> R): T? { - if (isEmpty()) return null - var maxElem = this[0] - var maxValue = selector(maxElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (maxValue < v) { - maxElem = e - maxValue = v - } - } - return maxElem -} - -/** - * Returns the first element yielding the largest value of the given function or `null` if there are no elements. - */ -public inline fun > ByteArray.maxBy(selector: (Byte) -> R): Byte? { - if (isEmpty()) return null - var maxElem = this[0] - var maxValue = selector(maxElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (maxValue < v) { - maxElem = e - maxValue = v - } - } - return maxElem -} - -/** - * Returns the first element yielding the largest value of the given function or `null` if there are no elements. - */ -public inline fun > ShortArray.maxBy(selector: (Short) -> R): Short? { - if (isEmpty()) return null - var maxElem = this[0] - var maxValue = selector(maxElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (maxValue < v) { - maxElem = e - maxValue = v - } - } - return maxElem -} - -/** - * Returns the first element yielding the largest value of the given function or `null` if there are no elements. - */ -public inline fun > IntArray.maxBy(selector: (Int) -> R): Int? { - if (isEmpty()) return null - var maxElem = this[0] - var maxValue = selector(maxElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (maxValue < v) { - maxElem = e - maxValue = v - } - } - return maxElem -} - -/** - * Returns the first element yielding the largest value of the given function or `null` if there are no elements. - */ -public inline fun > LongArray.maxBy(selector: (Long) -> R): Long? { - if (isEmpty()) return null - var maxElem = this[0] - var maxValue = selector(maxElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (maxValue < v) { - maxElem = e - maxValue = v - } - } - return maxElem -} - -/** - * Returns the first element yielding the largest value of the given function or `null` if there are no elements. - */ -public inline fun > FloatArray.maxBy(selector: (Float) -> R): Float? { - if (isEmpty()) return null - var maxElem = this[0] - var maxValue = selector(maxElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (maxValue < v) { - maxElem = e - maxValue = v - } - } - return maxElem -} - -/** - * Returns the first element yielding the largest value of the given function or `null` if there are no elements. - */ -public inline fun > DoubleArray.maxBy(selector: (Double) -> R): Double? { - if (isEmpty()) return null - var maxElem = this[0] - var maxValue = selector(maxElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (maxValue < v) { - maxElem = e - maxValue = v - } - } - return maxElem -} - -/** - * Returns the first element yielding the largest value of the given function or `null` if there are no elements. - */ -public inline fun > BooleanArray.maxBy(selector: (Boolean) -> R): Boolean? { - if (isEmpty()) return null - var maxElem = this[0] - var maxValue = selector(maxElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (maxValue < v) { - maxElem = e - maxValue = v - } - } - return maxElem -} - -/** - * Returns the first element yielding the largest value of the given function or `null` if there are no elements. - */ -public inline fun > CharArray.maxBy(selector: (Char) -> R): Char? { - if (isEmpty()) return null - var maxElem = this[0] - var maxValue = selector(maxElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (maxValue < v) { - maxElem = e - maxValue = v - } - } - return maxElem -} - -/** - * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun Array.maxWith(comparator: Comparator): T? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(max, e) < 0) max = e - } - return max -} - -/** - * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun ByteArray.maxWith(comparator: Comparator): Byte? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(max, e) < 0) max = e - } - return max -} - -/** - * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun ShortArray.maxWith(comparator: Comparator): Short? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(max, e) < 0) max = e - } - return max -} - -/** - * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun IntArray.maxWith(comparator: Comparator): Int? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(max, e) < 0) max = e - } - return max -} - -/** - * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun LongArray.maxWith(comparator: Comparator): Long? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(max, e) < 0) max = e - } - return max -} - -/** - * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun FloatArray.maxWith(comparator: Comparator): Float? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(max, e) < 0) max = e - } - return max -} - -/** - * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun DoubleArray.maxWith(comparator: Comparator): Double? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(max, e) < 0) max = e - } - return max -} - -/** - * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun BooleanArray.maxWith(comparator: Comparator): Boolean? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(max, e) < 0) max = e - } - return max -} - -/** - * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun CharArray.maxWith(comparator: Comparator): Char? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(max, e) < 0) max = e - } - return max -} - -/** - * Returns the smallest element or `null` if there are no elements. - * - * If any of elements is `NaN` returns `NaN`. - */ -@SinceKotlin("1.1") -public fun Array.min(): Double? { - if (isEmpty()) return null - var min = this[0] - if (min.isNaN()) return min - for (i in 1..lastIndex) { - val e = this[i] - if (e.isNaN()) return e - if (min > e) min = e - } - return min -} - -/** - * Returns the smallest element or `null` if there are no elements. - * - * If any of elements is `NaN` returns `NaN`. - */ -@SinceKotlin("1.1") -public fun Array.min(): Float? { - if (isEmpty()) return null - var min = this[0] - if (min.isNaN()) return min - for (i in 1..lastIndex) { - val e = this[i] - if (e.isNaN()) return e - if (min > e) min = e - } - return min -} - -/** - * Returns the smallest element or `null` if there are no elements. - */ -public fun > Array.min(): T? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (min > e) min = e - } - return min -} - -/** - * Returns the smallest element or `null` if there are no elements. - */ -public fun ByteArray.min(): Byte? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (min > e) min = e - } - return min -} - -/** - * Returns the smallest element or `null` if there are no elements. - */ -public fun ShortArray.min(): Short? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (min > e) min = e - } - return min -} - -/** - * Returns the smallest element or `null` if there are no elements. - */ -public fun IntArray.min(): Int? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (min > e) min = e - } - return min -} - -/** - * Returns the smallest element or `null` if there are no elements. - */ -public fun LongArray.min(): Long? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (min > e) min = e - } - return min -} - -/** - * Returns the smallest element or `null` if there are no elements. - * - * If any of elements is `NaN` returns `NaN`. - */ -public fun FloatArray.min(): Float? { - if (isEmpty()) return null - var min = this[0] - if (min.isNaN()) return min - for (i in 1..lastIndex) { - val e = this[i] - if (e.isNaN()) return e - if (min > e) min = e - } - return min -} - -/** - * Returns the smallest element or `null` if there are no elements. - * - * If any of elements is `NaN` returns `NaN`. - */ -public fun DoubleArray.min(): Double? { - if (isEmpty()) return null - var min = this[0] - if (min.isNaN()) return min - for (i in 1..lastIndex) { - val e = this[i] - if (e.isNaN()) return e - if (min > e) min = e - } - return min -} - -/** - * Returns the smallest element or `null` if there are no elements. - */ -public fun CharArray.min(): Char? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (min > e) min = e - } - return min -} - -/** - * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. - */ -public inline fun > Array.minBy(selector: (T) -> R): T? { - if (isEmpty()) return null - var minElem = this[0] - var minValue = selector(minElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (minValue > v) { - minElem = e - minValue = v - } - } - return minElem -} - -/** - * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. - */ -public inline fun > ByteArray.minBy(selector: (Byte) -> R): Byte? { - if (isEmpty()) return null - var minElem = this[0] - var minValue = selector(minElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (minValue > v) { - minElem = e - minValue = v - } - } - return minElem -} - -/** - * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. - */ -public inline fun > ShortArray.minBy(selector: (Short) -> R): Short? { - if (isEmpty()) return null - var minElem = this[0] - var minValue = selector(minElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (minValue > v) { - minElem = e - minValue = v - } - } - return minElem -} - -/** - * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. - */ -public inline fun > IntArray.minBy(selector: (Int) -> R): Int? { - if (isEmpty()) return null - var minElem = this[0] - var minValue = selector(minElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (minValue > v) { - minElem = e - minValue = v - } - } - return minElem -} - -/** - * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. - */ -public inline fun > LongArray.minBy(selector: (Long) -> R): Long? { - if (isEmpty()) return null - var minElem = this[0] - var minValue = selector(minElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (minValue > v) { - minElem = e - minValue = v - } - } - return minElem -} - -/** - * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. - */ -public inline fun > FloatArray.minBy(selector: (Float) -> R): Float? { - if (isEmpty()) return null - var minElem = this[0] - var minValue = selector(minElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (minValue > v) { - minElem = e - minValue = v - } - } - return minElem -} - -/** - * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. - */ -public inline fun > DoubleArray.minBy(selector: (Double) -> R): Double? { - if (isEmpty()) return null - var minElem = this[0] - var minValue = selector(minElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (minValue > v) { - minElem = e - minValue = v - } - } - return minElem -} - -/** - * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. - */ -public inline fun > BooleanArray.minBy(selector: (Boolean) -> R): Boolean? { - if (isEmpty()) return null - var minElem = this[0] - var minValue = selector(minElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (minValue > v) { - minElem = e - minValue = v - } - } - return minElem -} - -/** - * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. - */ -public inline fun > CharArray.minBy(selector: (Char) -> R): Char? { - if (isEmpty()) return null - var minElem = this[0] - var minValue = selector(minElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (minValue > v) { - minElem = e - minValue = v - } - } - return minElem -} - -/** - * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun Array.minWith(comparator: Comparator): T? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(min, e) > 0) min = e - } - return min -} - -/** - * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun ByteArray.minWith(comparator: Comparator): Byte? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(min, e) > 0) min = e - } - return min -} - -/** - * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun ShortArray.minWith(comparator: Comparator): Short? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(min, e) > 0) min = e - } - return min -} - -/** - * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun IntArray.minWith(comparator: Comparator): Int? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(min, e) > 0) min = e - } - return min -} - -/** - * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun LongArray.minWith(comparator: Comparator): Long? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(min, e) > 0) min = e - } - return min -} - -/** - * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun FloatArray.minWith(comparator: Comparator): Float? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(min, e) > 0) min = e - } - return min -} - -/** - * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun DoubleArray.minWith(comparator: Comparator): Double? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(min, e) > 0) min = e - } - return min -} - -/** - * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun BooleanArray.minWith(comparator: Comparator): Boolean? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(min, e) > 0) min = e - } - return min -} - -/** - * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun CharArray.minWith(comparator: Comparator): Char? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(min, e) > 0) min = e - } - return min -} - -/** - * Returns `true` if the array has no elements. - * - * @sample samples.collections.Collections.Aggregates.none - */ -public fun Array.none(): Boolean { - return isEmpty() -} - -/** - * Returns `true` if the array has no elements. - * - * @sample samples.collections.Collections.Aggregates.none - */ -public fun ByteArray.none(): Boolean { - return isEmpty() -} - -/** - * Returns `true` if the array has no elements. - * - * @sample samples.collections.Collections.Aggregates.none - */ -public fun ShortArray.none(): Boolean { - return isEmpty() -} - -/** - * Returns `true` if the array has no elements. - * - * @sample samples.collections.Collections.Aggregates.none - */ -public fun IntArray.none(): Boolean { - return isEmpty() -} - -/** - * Returns `true` if the array has no elements. - * - * @sample samples.collections.Collections.Aggregates.none - */ -public fun LongArray.none(): Boolean { - return isEmpty() -} - -/** - * Returns `true` if the array has no elements. - * - * @sample samples.collections.Collections.Aggregates.none - */ -public fun FloatArray.none(): Boolean { - return isEmpty() -} - -/** - * Returns `true` if the array has no elements. - * - * @sample samples.collections.Collections.Aggregates.none - */ -public fun DoubleArray.none(): Boolean { - return isEmpty() -} - -/** - * Returns `true` if the array has no elements. - * - * @sample samples.collections.Collections.Aggregates.none - */ -public fun BooleanArray.none(): Boolean { - return isEmpty() -} - -/** - * Returns `true` if the array has no elements. - * - * @sample samples.collections.Collections.Aggregates.none - */ -public fun CharArray.none(): Boolean { - return isEmpty() -} - -/** - * Returns `true` if no elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.noneWithPredicate - */ -public inline fun Array.none(predicate: (T) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return false - return true -} - -/** - * Returns `true` if no elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.noneWithPredicate - */ -public inline fun ByteArray.none(predicate: (Byte) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return false - return true -} - -/** - * Returns `true` if no elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.noneWithPredicate - */ -public inline fun ShortArray.none(predicate: (Short) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return false - return true -} - -/** - * Returns `true` if no elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.noneWithPredicate - */ -public inline fun IntArray.none(predicate: (Int) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return false - return true -} - -/** - * Returns `true` if no elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.noneWithPredicate - */ -public inline fun LongArray.none(predicate: (Long) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return false - return true -} - -/** - * Returns `true` if no elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.noneWithPredicate - */ -public inline fun FloatArray.none(predicate: (Float) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return false - return true -} - -/** - * Returns `true` if no elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.noneWithPredicate - */ -public inline fun DoubleArray.none(predicate: (Double) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return false - return true -} - -/** - * Returns `true` if no elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.noneWithPredicate - */ -public inline fun BooleanArray.none(predicate: (Boolean) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return false - return true -} - -/** - * Returns `true` if no elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.noneWithPredicate - */ -public inline fun CharArray.none(predicate: (Char) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return false - return true -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun Array.reduce(operation: (acc: S, T) -> S): S { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator: S = this[0] - for (index in 1..lastIndex) { - accumulator = operation(accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun ByteArray.reduce(operation: (acc: Byte, Byte) -> Byte): Byte { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun ShortArray.reduce(operation: (acc: Short, Short) -> Short): Short { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun IntArray.reduce(operation: (acc: Int, Int) -> Int): Int { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun LongArray.reduce(operation: (acc: Long, Long) -> Long): Long { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun FloatArray.reduce(operation: (acc: Float, Float) -> Float): Float { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun DoubleArray.reduce(operation: (acc: Double, Double) -> Double): Double { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun BooleanArray.reduce(operation: (acc: Boolean, Boolean) -> Boolean): Boolean { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun CharArray.reduce(operation: (acc: Char, Char) -> Char): Char { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself and calculates the next accumulator value. - */ -public inline fun Array.reduceIndexed(operation: (index: Int, acc: S, T) -> S): S { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator: S = this[0] - for (index in 1..lastIndex) { - accumulator = operation(index, accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself and calculates the next accumulator value. - */ -public inline fun ByteArray.reduceIndexed(operation: (index: Int, acc: Byte, Byte) -> Byte): Byte { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(index, accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself and calculates the next accumulator value. - */ -public inline fun ShortArray.reduceIndexed(operation: (index: Int, acc: Short, Short) -> Short): Short { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(index, accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself and calculates the next accumulator value. - */ -public inline fun IntArray.reduceIndexed(operation: (index: Int, acc: Int, Int) -> Int): Int { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(index, accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself and calculates the next accumulator value. - */ -public inline fun LongArray.reduceIndexed(operation: (index: Int, acc: Long, Long) -> Long): Long { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(index, accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself and calculates the next accumulator value. - */ -public inline fun FloatArray.reduceIndexed(operation: (index: Int, acc: Float, Float) -> Float): Float { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(index, accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself and calculates the next accumulator value. - */ -public inline fun DoubleArray.reduceIndexed(operation: (index: Int, acc: Double, Double) -> Double): Double { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(index, accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself and calculates the next accumulator value. - */ -public inline fun BooleanArray.reduceIndexed(operation: (index: Int, acc: Boolean, Boolean) -> Boolean): Boolean { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(index, accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right - * to current accumulator value and each element with its index in the original array. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself and calculates the next accumulator value. - */ -public inline fun CharArray.reduceIndexed(operation: (index: Int, acc: Char, Char) -> Char): Char { - if (isEmpty()) - throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(index, accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun Array.reduceRight(operation: (T, acc: S) -> S): S { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator: S = get(index--) - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun ByteArray.reduceRight(operation: (Byte, acc: Byte) -> Byte): Byte { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun ShortArray.reduceRight(operation: (Short, acc: Short) -> Short): Short { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun IntArray.reduceRight(operation: (Int, acc: Int) -> Int): Int { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun LongArray.reduceRight(operation: (Long, acc: Long) -> Long): Long { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun FloatArray.reduceRight(operation: (Float, acc: Float) -> Float): Float { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun DoubleArray.reduceRight(operation: (Double, acc: Double) -> Double): Double { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun BooleanArray.reduceRight(operation: (Boolean, acc: Boolean) -> Boolean): Boolean { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun CharArray.reduceRight(operation: (Char, acc: Char) -> Char): Char { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun Array.reduceRightIndexed(operation: (index: Int, T, acc: S) -> S): S { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator: S = get(index--) - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun ByteArray.reduceRightIndexed(operation: (index: Int, Byte, acc: Byte) -> Byte): Byte { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun ShortArray.reduceRightIndexed(operation: (index: Int, Short, acc: Short) -> Short): Short { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun IntArray.reduceRightIndexed(operation: (index: Int, Int, acc: Int) -> Int): Int { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun LongArray.reduceRightIndexed(operation: (index: Int, Long, acc: Long) -> Long): Long { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun FloatArray.reduceRightIndexed(operation: (index: Int, Float, acc: Float) -> Float): Float { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun DoubleArray.reduceRightIndexed(operation: (index: Int, Double, acc: Double) -> Double): Double { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun BooleanArray.reduceRightIndexed(operation: (index: Int, Boolean, acc: Boolean) -> Boolean): Boolean { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left - * to each element with its index in the original array and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun CharArray.reduceRightIndexed(operation: (index: Int, Char, acc: Char) -> Char): Char { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun Array.sumBy(selector: (T) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun ByteArray.sumBy(selector: (Byte) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun ShortArray.sumBy(selector: (Short) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun IntArray.sumBy(selector: (Int) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun LongArray.sumBy(selector: (Long) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun FloatArray.sumBy(selector: (Float) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun DoubleArray.sumBy(selector: (Double) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun BooleanArray.sumBy(selector: (Boolean) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun CharArray.sumBy(selector: (Char) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun Array.sumByDouble(selector: (T) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun ByteArray.sumByDouble(selector: (Byte) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun ShortArray.sumByDouble(selector: (Short) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun IntArray.sumByDouble(selector: (Int) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun LongArray.sumByDouble(selector: (Long) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun FloatArray.sumByDouble(selector: (Float) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun DoubleArray.sumByDouble(selector: (Double) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun BooleanArray.sumByDouble(selector: (Boolean) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the array. - */ -public inline fun CharArray.sumByDouble(selector: (Char) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns an original collection containing all the non-`null` elements, throwing an [IllegalArgumentException] if there are any `null` elements. - */ -public fun Array.requireNoNulls(): Array { - for (element in this) { - if (element == null) { - throw IllegalArgumentException("null element found in $this.") - } - } - @Suppress("UNCHECKED_CAST") - return this as Array -} - -/** - * Splits the original array into pair of lists, - * where *first* list contains elements for which [predicate] yielded `true`, - * while *second* list contains elements for which [predicate] yielded `false`. - */ -public inline fun Array.partition(predicate: (T) -> Boolean): Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Splits the original array into pair of lists, - * where *first* list contains elements for which [predicate] yielded `true`, - * while *second* list contains elements for which [predicate] yielded `false`. - */ -public inline fun ByteArray.partition(predicate: (Byte) -> Boolean): Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Splits the original array into pair of lists, - * where *first* list contains elements for which [predicate] yielded `true`, - * while *second* list contains elements for which [predicate] yielded `false`. - */ -public inline fun ShortArray.partition(predicate: (Short) -> Boolean): Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Splits the original array into pair of lists, - * where *first* list contains elements for which [predicate] yielded `true`, - * while *second* list contains elements for which [predicate] yielded `false`. - */ -public inline fun IntArray.partition(predicate: (Int) -> Boolean): Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Splits the original array into pair of lists, - * where *first* list contains elements for which [predicate] yielded `true`, - * while *second* list contains elements for which [predicate] yielded `false`. - */ -public inline fun LongArray.partition(predicate: (Long) -> Boolean): Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Splits the original array into pair of lists, - * where *first* list contains elements for which [predicate] yielded `true`, - * while *second* list contains elements for which [predicate] yielded `false`. - */ -public inline fun FloatArray.partition(predicate: (Float) -> Boolean): Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Splits the original array into pair of lists, - * where *first* list contains elements for which [predicate] yielded `true`, - * while *second* list contains elements for which [predicate] yielded `false`. - */ -public inline fun DoubleArray.partition(predicate: (Double) -> Boolean): Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Splits the original array into pair of lists, - * where *first* list contains elements for which [predicate] yielded `true`, - * while *second* list contains elements for which [predicate] yielded `false`. - */ -public inline fun BooleanArray.partition(predicate: (Boolean) -> Boolean): Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Splits the original array into pair of lists, - * where *first* list contains elements for which [predicate] yielded `true`, - * while *second* list contains elements for which [predicate] yielded `false`. - */ -public inline fun CharArray.partition(predicate: (Char) -> Boolean): Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun Array.zip(other: Array): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun ByteArray.zip(other: Array): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun ShortArray.zip(other: Array): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun IntArray.zip(other: Array): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun LongArray.zip(other: Array): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun FloatArray.zip(other: Array): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun DoubleArray.zip(other: Array): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun BooleanArray.zip(other: Array): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun CharArray.zip(other: Array): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun Array.zip(other: Array, transform: (a: T, b: R) -> V): List { - val size = minOf(size, other.size) - val list = ArrayList(size) - for (i in 0 until size) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun ByteArray.zip(other: Array, transform: (a: Byte, b: R) -> V): List { - val size = minOf(size, other.size) - val list = ArrayList(size) - for (i in 0 until size) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun ShortArray.zip(other: Array, transform: (a: Short, b: R) -> V): List { - val size = minOf(size, other.size) - val list = ArrayList(size) - for (i in 0 until size) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun IntArray.zip(other: Array, transform: (a: Int, b: R) -> V): List { - val size = minOf(size, other.size) - val list = ArrayList(size) - for (i in 0 until size) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun LongArray.zip(other: Array, transform: (a: Long, b: R) -> V): List { - val size = minOf(size, other.size) - val list = ArrayList(size) - for (i in 0 until size) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun FloatArray.zip(other: Array, transform: (a: Float, b: R) -> V): List { - val size = minOf(size, other.size) - val list = ArrayList(size) - for (i in 0 until size) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun DoubleArray.zip(other: Array, transform: (a: Double, b: R) -> V): List { - val size = minOf(size, other.size) - val list = ArrayList(size) - for (i in 0 until size) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun BooleanArray.zip(other: Array, transform: (a: Boolean, b: R) -> V): List { - val size = minOf(size, other.size) - val list = ArrayList(size) - for (i in 0 until size) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun CharArray.zip(other: Array, transform: (a: Char, b: R) -> V): List { - val size = minOf(size, other.size) - val list = ArrayList(size) - for (i in 0 until size) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun Array.zip(other: Iterable): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun ByteArray.zip(other: Iterable): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun ShortArray.zip(other: Iterable): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun IntArray.zip(other: Iterable): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun LongArray.zip(other: Iterable): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun FloatArray.zip(other: Iterable): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun DoubleArray.zip(other: Iterable): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun BooleanArray.zip(other: Iterable): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun CharArray.zip(other: Iterable): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun Array.zip(other: Iterable, transform: (a: T, b: R) -> V): List { - val arraySize = size - val list = ArrayList(minOf(other.collectionSizeOrDefault(10), arraySize)) - var i = 0 - for (element in other) { - if (i >= arraySize) break - list.add(transform(this[i++], element)) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun ByteArray.zip(other: Iterable, transform: (a: Byte, b: R) -> V): List { - val arraySize = size - val list = ArrayList(minOf(other.collectionSizeOrDefault(10), arraySize)) - var i = 0 - for (element in other) { - if (i >= arraySize) break - list.add(transform(this[i++], element)) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun ShortArray.zip(other: Iterable, transform: (a: Short, b: R) -> V): List { - val arraySize = size - val list = ArrayList(minOf(other.collectionSizeOrDefault(10), arraySize)) - var i = 0 - for (element in other) { - if (i >= arraySize) break - list.add(transform(this[i++], element)) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun IntArray.zip(other: Iterable, transform: (a: Int, b: R) -> V): List { - val arraySize = size - val list = ArrayList(minOf(other.collectionSizeOrDefault(10), arraySize)) - var i = 0 - for (element in other) { - if (i >= arraySize) break - list.add(transform(this[i++], element)) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun LongArray.zip(other: Iterable, transform: (a: Long, b: R) -> V): List { - val arraySize = size - val list = ArrayList(minOf(other.collectionSizeOrDefault(10), arraySize)) - var i = 0 - for (element in other) { - if (i >= arraySize) break - list.add(transform(this[i++], element)) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun FloatArray.zip(other: Iterable, transform: (a: Float, b: R) -> V): List { - val arraySize = size - val list = ArrayList(minOf(other.collectionSizeOrDefault(10), arraySize)) - var i = 0 - for (element in other) { - if (i >= arraySize) break - list.add(transform(this[i++], element)) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun DoubleArray.zip(other: Iterable, transform: (a: Double, b: R) -> V): List { - val arraySize = size - val list = ArrayList(minOf(other.collectionSizeOrDefault(10), arraySize)) - var i = 0 - for (element in other) { - if (i >= arraySize) break - list.add(transform(this[i++], element)) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun BooleanArray.zip(other: Iterable, transform: (a: Boolean, b: R) -> V): List { - val arraySize = size - val list = ArrayList(minOf(other.collectionSizeOrDefault(10), arraySize)) - var i = 0 - for (element in other) { - if (i >= arraySize) break - list.add(transform(this[i++], element)) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun CharArray.zip(other: Iterable, transform: (a: Char, b: R) -> V): List { - val arraySize = size - val list = ArrayList(minOf(other.collectionSizeOrDefault(10), arraySize)) - var i = 0 - for (element in other) { - if (i >= arraySize) break - list.add(transform(this[i++], element)) - } - return list -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun ByteArray.zip(other: ByteArray): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun ShortArray.zip(other: ShortArray): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun IntArray.zip(other: IntArray): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun LongArray.zip(other: LongArray): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun FloatArray.zip(other: FloatArray): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun DoubleArray.zip(other: DoubleArray): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun BooleanArray.zip(other: BooleanArray): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun CharArray.zip(other: CharArray): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun ByteArray.zip(other: ByteArray, transform: (a: Byte, b: Byte) -> V): List { - val size = minOf(size, other.size) - val list = ArrayList(size) - for (i in 0 until size) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun ShortArray.zip(other: ShortArray, transform: (a: Short, b: Short) -> V): List { - val size = minOf(size, other.size) - val list = ArrayList(size) - for (i in 0 until size) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun IntArray.zip(other: IntArray, transform: (a: Int, b: Int) -> V): List { - val size = minOf(size, other.size) - val list = ArrayList(size) - for (i in 0 until size) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun LongArray.zip(other: LongArray, transform: (a: Long, b: Long) -> V): List { - val size = minOf(size, other.size) - val list = ArrayList(size) - for (i in 0 until size) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun FloatArray.zip(other: FloatArray, transform: (a: Float, b: Float) -> V): List { - val size = minOf(size, other.size) - val list = ArrayList(size) - for (i in 0 until size) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun DoubleArray.zip(other: DoubleArray, transform: (a: Double, b: Double) -> V): List { - val size = minOf(size, other.size) - val list = ArrayList(size) - for (i in 0 until size) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun BooleanArray.zip(other: BooleanArray, transform: (a: Boolean, b: Boolean) -> V): List { - val size = minOf(size, other.size) - val list = ArrayList(size) - for (i in 0 until size) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun CharArray.zip(other: CharArray, transform: (a: Char, b: Char) -> V): List { - val size = minOf(size, other.size) - val list = ArrayList(size) - for (i in 0 until size) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinTo - */ -public fun Array.joinTo(buffer: A, separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((T) -> CharSequence)? = null): A { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - buffer.appendElement(element, transform) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) - return buffer -} - -/** - * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinTo - */ -public fun ByteArray.joinTo(buffer: A, separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Byte) -> CharSequence)? = null): A { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - if (transform != null) - buffer.append(transform(element)) - else - buffer.append(element.toString()) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) - return buffer -} - -/** - * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinTo - */ -public fun ShortArray.joinTo(buffer: A, separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Short) -> CharSequence)? = null): A { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - if (transform != null) - buffer.append(transform(element)) - else - buffer.append(element.toString()) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) - return buffer -} - -/** - * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinTo - */ -public fun IntArray.joinTo(buffer: A, separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Int) -> CharSequence)? = null): A { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - if (transform != null) - buffer.append(transform(element)) - else - buffer.append(element.toString()) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) - return buffer -} - -/** - * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinTo - */ -public fun LongArray.joinTo(buffer: A, separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Long) -> CharSequence)? = null): A { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - if (transform != null) - buffer.append(transform(element)) - else - buffer.append(element.toString()) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) - return buffer -} - -/** - * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinTo - */ -public fun FloatArray.joinTo(buffer: A, separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Float) -> CharSequence)? = null): A { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - if (transform != null) - buffer.append(transform(element)) - else - buffer.append(element.toString()) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) - return buffer -} - -/** - * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinTo - */ -public fun DoubleArray.joinTo(buffer: A, separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Double) -> CharSequence)? = null): A { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - if (transform != null) - buffer.append(transform(element)) - else - buffer.append(element.toString()) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) - return buffer -} - -/** - * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinTo - */ -public fun BooleanArray.joinTo(buffer: A, separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Boolean) -> CharSequence)? = null): A { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - if (transform != null) - buffer.append(transform(element)) - else - buffer.append(element.toString()) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) - return buffer -} - -/** - * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinTo - */ -public fun CharArray.joinTo(buffer: A, separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Char) -> CharSequence)? = null): A { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - if (transform != null) - buffer.append(transform(element)) - else - buffer.append(element) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) - return buffer -} - -/** - * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinToString - */ -public fun Array.joinToString(separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((T) -> CharSequence)? = null): String { - return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() -} - -/** - * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinToString - */ -public fun ByteArray.joinToString(separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Byte) -> CharSequence)? = null): String { - return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() -} - -/** - * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinToString - */ -public fun ShortArray.joinToString(separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Short) -> CharSequence)? = null): String { - return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() -} - -/** - * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinToString - */ -public fun IntArray.joinToString(separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Int) -> CharSequence)? = null): String { - return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() -} - -/** - * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinToString - */ -public fun LongArray.joinToString(separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Long) -> CharSequence)? = null): String { - return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() -} - -/** - * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinToString - */ -public fun FloatArray.joinToString(separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Float) -> CharSequence)? = null): String { - return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() -} - -/** - * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinToString - */ -public fun DoubleArray.joinToString(separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Double) -> CharSequence)? = null): String { - return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() -} - -/** - * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinToString - */ -public fun BooleanArray.joinToString(separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Boolean) -> CharSequence)? = null): String { - return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() -} - -/** - * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinToString - */ -public fun CharArray.joinToString(separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Char) -> CharSequence)? = null): String { - return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() -} - -/** - * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. - */ -public fun Array.asIterable(): Iterable { - if (isEmpty()) return emptyList() - return Iterable { this.iterator() } -} - -/** - * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. - */ -public fun ByteArray.asIterable(): Iterable { - if (isEmpty()) return emptyList() - return Iterable { this.iterator() } -} - -/** - * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. - */ -public fun ShortArray.asIterable(): Iterable { - if (isEmpty()) return emptyList() - return Iterable { this.iterator() } -} - -/** - * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. - */ -public fun IntArray.asIterable(): Iterable { - if (isEmpty()) return emptyList() - return Iterable { this.iterator() } -} - -/** - * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. - */ -public fun LongArray.asIterable(): Iterable { - if (isEmpty()) return emptyList() - return Iterable { this.iterator() } -} - -/** - * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. - */ -public fun FloatArray.asIterable(): Iterable { - if (isEmpty()) return emptyList() - return Iterable { this.iterator() } -} - -/** - * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. - */ -public fun DoubleArray.asIterable(): Iterable { - if (isEmpty()) return emptyList() - return Iterable { this.iterator() } -} - -/** - * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. - */ -public fun BooleanArray.asIterable(): Iterable { - if (isEmpty()) return emptyList() - return Iterable { this.iterator() } -} - -/** - * Creates an [Iterable] instance that wraps the original array returning its elements when being iterated. - */ -public fun CharArray.asIterable(): Iterable { - if (isEmpty()) return emptyList() - return Iterable { this.iterator() } -} - -/** - * Creates a [Sequence] instance that wraps the original array returning its elements when being iterated. - * - * @sample samples.collections.Sequences.Building.sequenceFromArray - */ -public fun Array.asSequence(): Sequence { - if (isEmpty()) return emptySequence() - return Sequence { this.iterator() } -} - -/** - * Creates a [Sequence] instance that wraps the original array returning its elements when being iterated. - * - * @sample samples.collections.Sequences.Building.sequenceFromArray - */ -public fun ByteArray.asSequence(): Sequence { - if (isEmpty()) return emptySequence() - return Sequence { this.iterator() } -} - -/** - * Creates a [Sequence] instance that wraps the original array returning its elements when being iterated. - * - * @sample samples.collections.Sequences.Building.sequenceFromArray - */ -public fun ShortArray.asSequence(): Sequence { - if (isEmpty()) return emptySequence() - return Sequence { this.iterator() } -} - -/** - * Creates a [Sequence] instance that wraps the original array returning its elements when being iterated. - * - * @sample samples.collections.Sequences.Building.sequenceFromArray - */ -public fun IntArray.asSequence(): Sequence { - if (isEmpty()) return emptySequence() - return Sequence { this.iterator() } -} - -/** - * Creates a [Sequence] instance that wraps the original array returning its elements when being iterated. - * - * @sample samples.collections.Sequences.Building.sequenceFromArray - */ -public fun LongArray.asSequence(): Sequence { - if (isEmpty()) return emptySequence() - return Sequence { this.iterator() } -} - -/** - * Creates a [Sequence] instance that wraps the original array returning its elements when being iterated. - * - * @sample samples.collections.Sequences.Building.sequenceFromArray - */ -public fun FloatArray.asSequence(): Sequence { - if (isEmpty()) return emptySequence() - return Sequence { this.iterator() } -} - -/** - * Creates a [Sequence] instance that wraps the original array returning its elements when being iterated. - * - * @sample samples.collections.Sequences.Building.sequenceFromArray - */ -public fun DoubleArray.asSequence(): Sequence { - if (isEmpty()) return emptySequence() - return Sequence { this.iterator() } -} - -/** - * Creates a [Sequence] instance that wraps the original array returning its elements when being iterated. - * - * @sample samples.collections.Sequences.Building.sequenceFromArray - */ -public fun BooleanArray.asSequence(): Sequence { - if (isEmpty()) return emptySequence() - return Sequence { this.iterator() } -} - -/** - * Creates a [Sequence] instance that wraps the original array returning its elements when being iterated. - * - * @sample samples.collections.Sequences.Building.sequenceFromArray - */ -public fun CharArray.asSequence(): Sequence { - if (isEmpty()) return emptySequence() - return Sequence { this.iterator() } -} - -/** - * Returns an average value of elements in the array. - */ -@kotlin.jvm.JvmName("averageOfByte") -public fun Array.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the array. - */ -@kotlin.jvm.JvmName("averageOfShort") -public fun Array.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the array. - */ -@kotlin.jvm.JvmName("averageOfInt") -public fun Array.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the array. - */ -@kotlin.jvm.JvmName("averageOfLong") -public fun Array.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the array. - */ -@kotlin.jvm.JvmName("averageOfFloat") -public fun Array.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the array. - */ -@kotlin.jvm.JvmName("averageOfDouble") -public fun Array.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the array. - */ -public fun ByteArray.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the array. - */ -public fun ShortArray.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the array. - */ -public fun IntArray.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the array. - */ -public fun LongArray.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the array. - */ -public fun FloatArray.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the array. - */ -public fun DoubleArray.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns the sum of all elements in the array. - */ -@kotlin.jvm.JvmName("sumOfByte") -public fun Array.sum(): Int { - var sum: Int = 0 - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the array. - */ -@kotlin.jvm.JvmName("sumOfShort") -public fun Array.sum(): Int { - var sum: Int = 0 - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the array. - */ -@kotlin.jvm.JvmName("sumOfInt") -public fun Array.sum(): Int { - var sum: Int = 0 - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the array. - */ -@kotlin.jvm.JvmName("sumOfLong") -public fun Array.sum(): Long { - var sum: Long = 0L - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the array. - */ -@kotlin.jvm.JvmName("sumOfFloat") -public fun Array.sum(): Float { - var sum: Float = 0.0f - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the array. - */ -@kotlin.jvm.JvmName("sumOfDouble") -public fun Array.sum(): Double { - var sum: Double = 0.0 - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the array. - */ -public fun ByteArray.sum(): Int { - var sum: Int = 0 - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the array. - */ -public fun ShortArray.sum(): Int { - var sum: Int = 0 - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the array. - */ -public fun IntArray.sum(): Int { - var sum: Int = 0 - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the array. - */ -public fun LongArray.sum(): Long { - var sum: Long = 0L - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the array. - */ -public fun FloatArray.sum(): Float { - var sum: Float = 0.0f - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the array. - */ -public fun DoubleArray.sum(): Double { - var sum: Double = 0.0 - for (element in this) { - sum += element - } - return sum -} - diff --git a/libraries/stdlib/src/generated/_Collections.kt b/libraries/stdlib/src/generated/_Collections.kt deleted file mode 100644 index bccf284cb8a..00000000000 --- a/libraries/stdlib/src/generated/_Collections.kt +++ /dev/null @@ -1,2403 +0,0 @@ -@file:kotlin.jvm.JvmMultifileClass -@file:kotlin.jvm.JvmName("CollectionsKt") -@file:kotlin.jvm.JvmVersion - -package kotlin.collections - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// - -import kotlin.* -import kotlin.text.* -import kotlin.comparisons.* - -/** - * Returns 1st *element* from the collection. - */ -@kotlin.internal.InlineOnly -public inline operator fun List.component1(): T { - return get(0) -} - -/** - * Returns 2nd *element* from the collection. - */ -@kotlin.internal.InlineOnly -public inline operator fun List.component2(): T { - return get(1) -} - -/** - * Returns 3rd *element* from the collection. - */ -@kotlin.internal.InlineOnly -public inline operator fun List.component3(): T { - return get(2) -} - -/** - * Returns 4th *element* from the collection. - */ -@kotlin.internal.InlineOnly -public inline operator fun List.component4(): T { - return get(3) -} - -/** - * Returns 5th *element* from the collection. - */ -@kotlin.internal.InlineOnly -public inline operator fun List.component5(): T { - return get(4) -} - -/** - * Returns `true` if [element] is found in the collection. - */ -public operator fun <@kotlin.internal.OnlyInputTypes T> Iterable.contains(element: T): Boolean { - if (this is Collection) - return contains(element) - return indexOf(element) >= 0 -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. - */ -public fun Iterable.elementAt(index: Int): T { - if (this is List) - return get(index) - return elementAtOrElse(index) { throw IndexOutOfBoundsException("Collection doesn't contain element at index $index.") } -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this list. - */ -@kotlin.internal.InlineOnly -public inline fun List.elementAt(index: Int): T { - return get(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. - */ -public fun Iterable.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T { - if (this is List) - return this.getOrElse(index, defaultValue) - if (index < 0) - return defaultValue(index) - val iterator = iterator() - var count = 0 - while (iterator.hasNext()) { - val element = iterator.next() - if (index == count++) - return element - } - return defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this list. - */ -@kotlin.internal.InlineOnly -public inline fun List.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. - */ -public fun Iterable.elementAtOrNull(index: Int): T? { - if (this is List) - return this.getOrNull(index) - if (index < 0) - return null - val iterator = iterator() - var count = 0 - while (iterator.hasNext()) { - val element = iterator.next() - if (index == count++) - return element - } - return null -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this list. - */ -@kotlin.internal.InlineOnly -public inline fun List.elementAtOrNull(index: Int): T? { - return this.getOrNull(index) -} - -/** - * Returns the first element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun Iterable.find(predicate: (T) -> Boolean): T? { - return firstOrNull(predicate) -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun Iterable.findLast(predicate: (T) -> Boolean): T? { - return lastOrNull(predicate) -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -@kotlin.internal.InlineOnly -public inline fun List.findLast(predicate: (T) -> Boolean): T? { - return lastOrNull(predicate) -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the collection is empty. - */ -public fun Iterable.first(): T { - when (this) { - is List -> return this.first() - else -> { - val iterator = iterator() - if (!iterator.hasNext()) - throw NoSuchElementException("Collection is empty.") - return iterator.next() - } - } -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the list is empty. - */ -public fun List.first(): T { - if (isEmpty()) - throw NoSuchElementException("List is empty.") - return this[0] -} - -/** - * Returns the first element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun Iterable.first(predicate: (T) -> Boolean): T { - for (element in this) if (predicate(element)) return element - throw NoSuchElementException("Collection contains no element matching the predicate.") -} - -/** - * Returns the first element, or `null` if the collection is empty. - */ -public fun Iterable.firstOrNull(): T? { - when (this) { - is List -> { - if (isEmpty()) - return null - else - return this[0] - } - else -> { - val iterator = iterator() - if (!iterator.hasNext()) - return null - return iterator.next() - } - } -} - -/** - * Returns the first element, or `null` if the list is empty. - */ -public fun List.firstOrNull(): T? { - return if (isEmpty()) null else this[0] -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - */ -public inline fun Iterable.firstOrNull(predicate: (T) -> Boolean): T? { - for (element in this) if (predicate(element)) return element - return null -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this list. - */ -@kotlin.internal.InlineOnly -public inline fun List.getOrElse(index: Int, defaultValue: (Int) -> T): T { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this list. - */ -public fun List.getOrNull(index: Int): T? { - return if (index >= 0 && index <= lastIndex) get(index) else null -} - -/** - * Returns first index of [element], or -1 if the collection does not contain element. - */ -public fun <@kotlin.internal.OnlyInputTypes T> Iterable.indexOf(element: T): Int { - if (this is List) return this.indexOf(element) - var index = 0 - for (item in this) { - if (element == item) - return index - index++ - } - return -1 -} - -/** - * Returns first index of [element], or -1 if the list does not contain element. - */ -@Suppress("EXTENSION_SHADOWED_BY_MEMBER") // false warning, extension takes precedence in some cases -public fun <@kotlin.internal.OnlyInputTypes T> List.indexOf(element: T): Int { - return indexOf(element) -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element. - */ -public inline fun Iterable.indexOfFirst(predicate: (T) -> Boolean): Int { - var index = 0 - for (item in this) { - if (predicate(item)) - return index - index++ - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the list does not contain such element. - */ -public inline fun List.indexOfFirst(predicate: (T) -> Boolean): Int { - var index = 0 - for (item in this) { - if (predicate(item)) - return index - index++ - } - return -1 -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element. - */ -public inline fun Iterable.indexOfLast(predicate: (T) -> Boolean): Int { - var lastIndex = -1 - var index = 0 - for (item in this) { - if (predicate(item)) - lastIndex = index - index++ - } - return lastIndex -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the list does not contain such element. - */ -public inline fun List.indexOfLast(predicate: (T) -> Boolean): Int { - val iterator = this.listIterator(size) - while (iterator.hasPrevious()) { - if (predicate(iterator.previous())) { - return iterator.nextIndex() - } - } - return -1 -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the collection is empty. - */ -public fun Iterable.last(): T { - when (this) { - is List -> return this.last() - else -> { - val iterator = iterator() - if (!iterator.hasNext()) - throw NoSuchElementException("Collection is empty.") - var last = iterator.next() - while (iterator.hasNext()) - last = iterator.next() - return last - } - } -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the list is empty. - */ -public fun List.last(): T { - if (isEmpty()) - throw NoSuchElementException("List is empty.") - return this[lastIndex] -} - -/** - * Returns the last element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun Iterable.last(predicate: (T) -> Boolean): T { - var last: T? = null - var found = false - for (element in this) { - if (predicate(element)) { - last = element - found = true - } - } - if (!found) throw NoSuchElementException("Collection contains no element matching the predicate.") - @Suppress("UNCHECKED_CAST") - return last as T -} - -/** - * Returns the last element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun List.last(predicate: (T) -> Boolean): T { - val iterator = this.listIterator(size) - while (iterator.hasPrevious()) { - val element = iterator.previous() - if (predicate(element)) return element - } - throw NoSuchElementException("List contains no element matching the predicate.") -} - -/** - * Returns last index of [element], or -1 if the collection does not contain element. - */ -public fun <@kotlin.internal.OnlyInputTypes T> Iterable.lastIndexOf(element: T): Int { - if (this is List) return this.lastIndexOf(element) - var lastIndex = -1 - var index = 0 - for (item in this) { - if (element == item) - lastIndex = index - index++ - } - return lastIndex -} - -/** - * Returns last index of [element], or -1 if the list does not contain element. - */ -@Suppress("EXTENSION_SHADOWED_BY_MEMBER") // false warning, extension takes precedence in some cases -public fun <@kotlin.internal.OnlyInputTypes T> List.lastIndexOf(element: T): Int { - return lastIndexOf(element) -} - -/** - * Returns the last element, or `null` if the collection is empty. - */ -public fun Iterable.lastOrNull(): T? { - when (this) { - is List -> return if (isEmpty()) null else this[size - 1] - else -> { - val iterator = iterator() - if (!iterator.hasNext()) - return null - var last = iterator.next() - while (iterator.hasNext()) - last = iterator.next() - return last - } - } -} - -/** - * Returns the last element, or `null` if the list is empty. - */ -public fun List.lastOrNull(): T? { - return if (isEmpty()) null else this[size - 1] -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -public inline fun Iterable.lastOrNull(predicate: (T) -> Boolean): T? { - var last: T? = null - for (element in this) { - if (predicate(element)) { - last = element - } - } - return last -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -public inline fun List.lastOrNull(predicate: (T) -> Boolean): T? { - val iterator = this.listIterator(size) - while (iterator.hasPrevious()) { - val element = iterator.previous() - if (predicate(element)) return element - } - return null -} - -/** - * Returns the single element, or throws an exception if the collection is empty or has more than one element. - */ -public fun Iterable.single(): T { - when (this) { - is List -> return this.single() - else -> { - val iterator = iterator() - if (!iterator.hasNext()) - throw NoSuchElementException("Collection is empty.") - val single = iterator.next() - if (iterator.hasNext()) - throw IllegalArgumentException("Collection has more than one element.") - return single - } - } -} - -/** - * Returns the single element, or throws an exception if the list is empty or has more than one element. - */ -public fun List.single(): T { - return when (size) { - 0 -> throw NoSuchElementException("List is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("List has more than one element.") - } -} - -/** - * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. - */ -public inline fun Iterable.single(predicate: (T) -> Boolean): T { - var single: T? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Collection contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Collection contains no element matching the predicate.") - @Suppress("UNCHECKED_CAST") - return single as T -} - -/** - * Returns single element, or `null` if the collection is empty or has more than one element. - */ -public fun Iterable.singleOrNull(): T? { - when (this) { - is List -> return if (size == 1) this[0] else null - else -> { - val iterator = iterator() - if (!iterator.hasNext()) - return null - val single = iterator.next() - if (iterator.hasNext()) - return null - return single - } - } -} - -/** - * Returns single element, or `null` if the list is empty or has more than one element. - */ -public fun List.singleOrNull(): T? { - return if (size == 1) this[0] else null -} - -/** - * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. - */ -public inline fun Iterable.singleOrNull(predicate: (T) -> Boolean): T? { - var single: T? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) return null - single = element - found = true - } - } - if (!found) return null - return single -} - -/** - * Returns a list containing all elements except first [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun Iterable.drop(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return toList() - val list: ArrayList - if (this is Collection<*>) { - val resultSize = size - n - if (resultSize <= 0) - return emptyList() - if (resultSize == 1) - return listOf(last()) - list = ArrayList(resultSize) - if (this is List) { - if (this is RandomAccess) { - for (index in n until size) - list.add(this[index]) - } else { - for (item in listIterator(n)) - list.add(item) - } - return list - } - } - else { - list = ArrayList() - } - var count = 0 - for (item in this) { - if (count++ >= n) list.add(item) - } - return list.optimizeReadOnlyList() -} - -/** - * Returns a list containing all elements except last [n] elements. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public fun List.dropLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - return take((size - n).coerceAtLeast(0)) -} - -/** - * Returns a list containing all elements except last elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun List.dropLastWhile(predicate: (T) -> Boolean): List { - if (!isEmpty()) { - val iterator = listIterator(size) - while (iterator.hasPrevious()) { - if (!predicate(iterator.previous())) { - return take(iterator.nextIndex() + 1) - } - } - } - return emptyList() -} - -/** - * Returns a list containing all elements except first elements that satisfy the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.drop - */ -public inline fun Iterable.dropWhile(predicate: (T) -> Boolean): List { - var yielding = false - val list = ArrayList() - for (item in this) - if (yielding) - list.add(item) - else if (!predicate(item)) { - list.add(item) - yielding = true - } - return list -} - -/** - * Returns a list containing only elements matching the given [predicate]. - */ -public inline fun Iterable.filter(predicate: (T) -> Boolean): List { - return filterTo(ArrayList(), predicate) -} - -/** - * Returns a list containing only elements matching the given [predicate]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun Iterable.filterIndexed(predicate: (index: Int, T) -> Boolean): List { - return filterIndexedTo(ArrayList(), predicate) -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - */ -public inline fun > Iterable.filterIndexedTo(destination: C, predicate: (index: Int, T) -> Boolean): C { - forEachIndexed { index, element -> - if (predicate(index, element)) destination.add(element) - } - return destination -} - -/** - * Returns a list containing all elements that are instances of specified type parameter R. - */ -public inline fun Iterable<*>.filterIsInstance(): List<@kotlin.internal.NoInfer R> { - return filterIsInstanceTo(ArrayList()) -} - -/** - * Returns a list containing all elements that are instances of specified class. - */ -public fun Iterable<*>.filterIsInstance(klass: Class): List { - return filterIsInstanceTo(ArrayList(), klass) -} - -/** - * Appends all elements that are instances of specified type parameter R to the given [destination]. - */ -public inline fun > Iterable<*>.filterIsInstanceTo(destination: C): C { - for (element in this) if (element is R) destination.add(element) - return destination -} - -/** - * Appends all elements that are instances of specified class to the given [destination]. - */ -public fun , R> Iterable<*>.filterIsInstanceTo(destination: C, klass: Class): C { - @Suppress("UNCHECKED_CAST") - for (element in this) if (klass.isInstance(element)) destination.add(element as R) - return destination -} - -/** - * Returns a list containing all elements not matching the given [predicate]. - */ -public inline fun Iterable.filterNot(predicate: (T) -> Boolean): List { - return filterNotTo(ArrayList(), predicate) -} - -/** - * Returns a list containing all elements that are not `null`. - */ -public fun Iterable.filterNotNull(): List { - return filterNotNullTo(ArrayList()) -} - -/** - * Appends all elements that are not `null` to the given [destination]. - */ -public fun , T : Any> Iterable.filterNotNullTo(destination: C): C { - for (element in this) if (element != null) destination.add(element) - return destination -} - -/** - * Appends all elements not matching the given [predicate] to the given [destination]. - */ -public inline fun > Iterable.filterNotTo(destination: C, predicate: (T) -> Boolean): C { - for (element in this) if (!predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - */ -public inline fun > Iterable.filterTo(destination: C, predicate: (T) -> Boolean): C { - for (element in this) if (predicate(element)) destination.add(element) - return destination -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun List.slice(indices: IntRange): List { - if (indices.isEmpty()) return listOf() - return this.subList(indices.start, indices.endInclusive + 1).toList() -} - -/** - * Returns a list containing elements at specified [indices]. - */ -public fun List.slice(indices: Iterable): List { - val size = indices.collectionSizeOrDefault(10) - if (size == 0) return emptyList() - val list = ArrayList(size) - for (index in indices) { - list.add(get(index)) - } - return list -} - -/** - * Returns a list containing first [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun Iterable.take(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - if (this is Collection) { - if (n >= size) return toList() - if (n == 1) return listOf(first()) - } - var count = 0 - val list = ArrayList(n) - for (item in this) { - if (count++ == n) - break - list.add(item) - } - return list.optimizeReadOnlyList() -} - -/** - * Returns a list containing last [n] elements. - * - * @sample samples.collections.Collections.Transformations.take - */ -public fun List.takeLast(n: Int): List { - require(n >= 0) { "Requested element count $n is less than zero." } - if (n == 0) return emptyList() - val size = size - if (n >= size) return toList() - if (n == 1) return listOf(last()) - val list = ArrayList(n) - if (this is RandomAccess) { - for (index in size - n until size) - list.add(this[index]) - } else { - for (item in listIterator(size - n)) - list.add(item) - } - return list -} - -/** - * Returns a list containing last elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun List.takeLastWhile(predicate: (T) -> Boolean): List { - if (isEmpty()) - return emptyList() - val iterator = listIterator(size) - while (iterator.hasPrevious()) { - if (!predicate(iterator.previous())) { - iterator.next() - val expectedSize = size - iterator.nextIndex() - if (expectedSize == 0) return emptyList() - return ArrayList(expectedSize).apply { - while (iterator.hasNext()) - add(iterator.next()) - } - } - } - return toList() -} - -/** - * Returns a list containing first elements satisfying the given [predicate]. - * - * @sample samples.collections.Collections.Transformations.take - */ -public inline fun Iterable.takeWhile(predicate: (T) -> Boolean): List { - val list = ArrayList() - for (item in this) { - if (!predicate(item)) - break - list.add(item) - } - return list -} - -/** - * Reverses elements in the list in-place. - */ -public fun MutableList.reverse(): Unit { - java.util.Collections.reverse(this) -} - -/** - * Returns a list with elements in reversed order. - */ -public fun Iterable.reversed(): List { - if (this is Collection && size <= 1) return toList() - val list = toMutableList() - list.reverse() - return list -} - -/** - * Sorts elements in the list in-place according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > MutableList.sortBy(crossinline selector: (T) -> R?): Unit { - if (size > 1) sortWith(compareBy(selector)) -} - -/** - * Sorts elements in the list in-place descending according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > MutableList.sortByDescending(crossinline selector: (T) -> R?): Unit { - if (size > 1) sortWith(compareByDescending(selector)) -} - -/** - * Sorts elements in the list in-place descending according to their natural sort order. - */ -public fun > MutableList.sortDescending(): Unit { - sortWith(reverseOrder()) -} - -/** - * Returns a list of all elements sorted according to their natural sort order. - */ -public fun > Iterable.sorted(): List { - if (this is Collection) { - if (size <= 1) return this.toList() - @Suppress("UNCHECKED_CAST") - return (toTypedArray>() as Array).apply { sort() }.asList() - } - return toMutableList().apply { sort() } -} - -/** - * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > Iterable.sortedBy(crossinline selector: (T) -> R?): List { - return sortedWith(compareBy(selector)) -} - -/** - * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > Iterable.sortedByDescending(crossinline selector: (T) -> R?): List { - return sortedWith(compareByDescending(selector)) -} - -/** - * Returns a list of all elements sorted descending according to their natural sort order. - */ -public fun > Iterable.sortedDescending(): List { - return sortedWith(reverseOrder()) -} - -/** - * Returns a list of all elements sorted according to the specified [comparator]. - */ -public fun Iterable.sortedWith(comparator: Comparator): List { - if (this is Collection) { - if (size <= 1) return this.toList() - @Suppress("UNCHECKED_CAST") - return (toTypedArray() as Array).apply { sortWith(comparator) }.asList() - } - return toMutableList().apply { sortWith(comparator) } -} - -/** - * Returns an array of Boolean containing all of the elements of this collection. - */ -public fun Collection.toBooleanArray(): BooleanArray { - val result = BooleanArray(size) - var index = 0 - for (element in this) - result[index++] = element - return result -} - -/** - * Returns an array of Byte containing all of the elements of this collection. - */ -public fun Collection.toByteArray(): ByteArray { - val result = ByteArray(size) - var index = 0 - for (element in this) - result[index++] = element - return result -} - -/** - * Returns an array of Char containing all of the elements of this collection. - */ -public fun Collection.toCharArray(): CharArray { - val result = CharArray(size) - var index = 0 - for (element in this) - result[index++] = element - return result -} - -/** - * Returns an array of Double containing all of the elements of this collection. - */ -public fun Collection.toDoubleArray(): DoubleArray { - val result = DoubleArray(size) - var index = 0 - for (element in this) - result[index++] = element - return result -} - -/** - * Returns an array of Float containing all of the elements of this collection. - */ -public fun Collection.toFloatArray(): FloatArray { - val result = FloatArray(size) - var index = 0 - for (element in this) - result[index++] = element - return result -} - -/** - * Returns an array of Int containing all of the elements of this collection. - */ -public fun Collection.toIntArray(): IntArray { - val result = IntArray(size) - var index = 0 - for (element in this) - result[index++] = element - return result -} - -/** - * Returns an array of Long containing all of the elements of this collection. - */ -public fun Collection.toLongArray(): LongArray { - val result = LongArray(size) - var index = 0 - for (element in this) - result[index++] = element - return result -} - -/** - * Returns an array of Short containing all of the elements of this collection. - */ -public fun Collection.toShortArray(): ShortArray { - val result = ShortArray(size) - var index = 0 - for (element in this) - result[index++] = element - return result -} - -/** - * Returns a [Map] containing key-value pairs provided by [transform] function - * applied to elements of the given collection. - * - * If any of two pairs would have the same key the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original collection. - */ -public inline fun Iterable.associate(transform: (T) -> Pair): Map { - val capacity = mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16) - return associateTo(LinkedHashMap(capacity), transform) -} - -/** - * Returns a [Map] containing the elements from the given collection indexed by the key - * returned from [keySelector] function applied to each element. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original collection. - */ -public inline fun Iterable.associateBy(keySelector: (T) -> K): Map { - val capacity = mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector) -} - -/** - * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given collection. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original collection. - */ -public inline fun Iterable.associateBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map { - val capacity = mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function applied to each element of the given collection - * and value is the element itself. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > Iterable.associateByTo(destination: M, keySelector: (T) -> K): M { - for (element in this) { - destination.put(keySelector(element), element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function and - * and value is provided by the [valueTransform] function applied to elements of the given collection. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > Iterable.associateByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M { - for (element in this) { - destination.put(keySelector(element), valueTransform(element)) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs - * provided by [transform] function applied to each element of the given collection. - * - * If any of two pairs would have the same key the last one gets added to the map. - */ -public inline fun > Iterable.associateTo(destination: M, transform: (T) -> Pair): M { - for (element in this) { - destination += transform(element) - } - return destination -} - -/** - * Appends all elements to the given [destination] collection. - */ -public fun > Iterable.toCollection(destination: C): C { - for (item in this) { - destination.add(item) - } - return destination -} - -/** - * Returns a [HashSet] of all elements. - */ -public fun Iterable.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(collectionSizeOrDefault(12)))) -} - -/** - * Returns a [List] containing all elements. - */ -public fun Iterable.toList(): List { - if (this is Collection) { - return when (size) { - 0 -> emptyList() - 1 -> listOf(if (this is List) get(0) else iterator().next()) - else -> this.toMutableList() - } - } - return this.toMutableList().optimizeReadOnlyList() -} - -/** - * Returns a [MutableList] filled with all elements of this collection. - */ -public fun Iterable.toMutableList(): MutableList { - if (this is Collection) - return this.toMutableList() - return toCollection(ArrayList()) -} - -/** - * Returns a [MutableList] filled with all elements of this collection. - */ -public fun Collection.toMutableList(): MutableList { - return ArrayList(this) -} - -/** - * Returns a [Set] of all elements. - * - * The returned set preserves the element iteration order of the original collection. - */ -public fun Iterable.toSet(): Set { - if (this is Collection) { - return when (size) { - 0 -> emptySet() - 1 -> setOf(if (this is List) this[0] else iterator().next()) - else -> toCollection(LinkedHashSet(mapCapacity(size))) - } - } - return toCollection(LinkedHashSet()).optimizeReadOnlySet() -} - -/** - * Returns a [SortedSet][java.util.SortedSet] of all elements. - */ -public fun > Iterable.toSortedSet(): java.util.SortedSet { - return toCollection(java.util.TreeSet()) -} - -/** - * Returns a [SortedSet][java.util.SortedSet] of all elements. - * - * Elements in the set returned are sorted according to the given [comparator]. - */ -public fun Iterable.toSortedSet(comparator: Comparator): java.util.SortedSet { - return toCollection(java.util.TreeSet(comparator)) -} - -/** - * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original collection. - */ -public inline fun Iterable.flatMap(transform: (T) -> Iterable): List { - return flatMapTo(ArrayList(), transform) -} - -/** - * Appends all elements yielded from results of [transform] function being invoked on each element of original collection, to the given [destination]. - */ -public inline fun > Iterable.flatMapTo(destination: C, transform: (T) -> Iterable): C { - for (element in this) { - val list = transform(element) - destination.addAll(list) - } - return destination -} - -/** - * Groups elements of the original collection by the key returned by the given [keySelector] function - * applied to each element and returns a map where each group key is associated with a list of corresponding elements. - * - * The returned map preserves the entry iteration order of the keys produced from the original collection. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun Iterable.groupBy(keySelector: (T) -> K): Map> { - return groupByTo(LinkedHashMap>(), keySelector) -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original collection - * by the key returned by the given [keySelector] function applied to the element - * and returns a map where each group key is associated with a list of corresponding values. - * - * The returned map preserves the entry iteration order of the keys produced from the original collection. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun Iterable.groupBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map> { - return groupByTo(LinkedHashMap>(), keySelector, valueTransform) -} - -/** - * Groups elements of the original collection by the key returned by the given [keySelector] function - * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun >> Iterable.groupByTo(destination: M, keySelector: (T) -> K): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(element) - } - return destination -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original collection - * by the key returned by the given [keySelector] function applied to the element - * and puts to the [destination] map each group key associated with a list of corresponding values. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun >> Iterable.groupByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(valueTransform(element)) - } - return destination -} - -/** - * Creates a [Grouping] source from a collection to be used later with one of group-and-fold operations - * using the specified [keySelector] function to extract a key from each element. - * - * @sample samples.collections.Collections.Transformations.groupingByEachCount - */ -@SinceKotlin("1.1") -public inline fun Iterable.groupingBy(crossinline keySelector: (T) -> K): Grouping { - return object : Grouping { - override fun sourceIterator(): Iterator = this@groupingBy.iterator() - override fun keyOf(element: T): K = keySelector(element) - } -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element in the original collection. - */ -public inline fun Iterable.map(transform: (T) -> R): List { - return mapTo(ArrayList(collectionSizeOrDefault(10)), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each element and its index in the original collection. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun Iterable.mapIndexed(transform: (index: Int, T) -> R): List { - return mapIndexedTo(ArrayList(collectionSizeOrDefault(10)), transform) -} - -/** - * Returns a list containing only the non-null results of applying the given [transform] function - * to each element and its index in the original collection. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun Iterable.mapIndexedNotNull(transform: (index: Int, T) -> R?): List { - return mapIndexedNotNullTo(ArrayList(), transform) -} - -/** - * Applies the given [transform] function to each element and its index in the original collection - * and appends only the non-null results to the given [destination]. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun > Iterable.mapIndexedNotNullTo(destination: C, transform: (index: Int, T) -> R?): C { - forEachIndexed { index, element -> transform(index, element)?.let { destination.add(it) } } - return destination -} - -/** - * Applies the given [transform] function to each element and its index in the original collection - * and appends the results to the given [destination]. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - */ -public inline fun > Iterable.mapIndexedTo(destination: C, transform: (index: Int, T) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Returns a list containing only the non-null results of applying the given [transform] function - * to each element in the original collection. - */ -public inline fun Iterable.mapNotNull(transform: (T) -> R?): List { - return mapNotNullTo(ArrayList(), transform) -} - -/** - * Applies the given [transform] function to each element in the original collection - * and appends only the non-null results to the given [destination]. - */ -public inline fun > Iterable.mapNotNullTo(destination: C, transform: (T) -> R?): C { - forEach { element -> transform(element)?.let { destination.add(it) } } - return destination -} - -/** - * Applies the given [transform] function to each element of the original collection - * and appends the results to the given [destination]. - */ -public inline fun > Iterable.mapTo(destination: C, transform: (T) -> R): C { - for (item in this) - destination.add(transform(item)) - return destination -} - -/** - * Returns a lazy [Iterable] of [IndexedValue] for each element of the original collection. - */ -public fun Iterable.withIndex(): Iterable> { - return IndexingIterable { iterator() } -} - -/** - * Returns a list containing only distinct elements from the given collection. - * - * The elements in the resulting list are in the same order as they were in the source collection. - */ -public fun Iterable.distinct(): List { - return this.toMutableSet().toList() -} - -/** - * Returns a list containing only elements from the given collection - * having distinct keys returned by the given [selector] function. - * - * The elements in the resulting list are in the same order as they were in the source collection. - */ -public inline fun Iterable.distinctBy(selector: (T) -> K): List { - val set = HashSet() - val list = ArrayList() - for (e in this) { - val key = selector(e) - if (set.add(key)) - list.add(e) - } - return list -} - -/** - * Returns a set containing all elements that are contained by both this set and the specified collection. - * - * The returned set preserves the element iteration order of the original collection. - */ -public infix fun Iterable.intersect(other: Iterable): Set { - val set = this.toMutableSet() - set.retainAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by this collection and not contained by the specified collection. - * - * The returned set preserves the element iteration order of the original collection. - */ -public infix fun Iterable.subtract(other: Iterable): Set { - val set = this.toMutableSet() - set.removeAll(other) - return set -} - -/** - * Returns a mutable set containing all distinct elements from the given collection. - * - * The returned set preserves the element iteration order of the original collection. - */ -public fun Iterable.toMutableSet(): MutableSet { - return when (this) { - is Collection -> LinkedHashSet(this) - else -> toCollection(LinkedHashSet()) - } -} - -/** - * Returns a set containing all distinct elements from both collections. - * - * The returned set preserves the element iteration order of the original collection. - * Those elements of the [other] collection that are unique are iterated in the end - * in the order of the [other] collection. - */ -public infix fun Iterable.union(other: Iterable): Set { - val set = this.toMutableSet() - set.addAll(other) - return set -} - -/** - * Returns `true` if all elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.all - */ -public inline fun Iterable.all(predicate: (T) -> Boolean): Boolean { - if (this is Collection && isEmpty()) return true - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if collection has at least one element. - * - * @sample samples.collections.Collections.Aggregates.any - */ -public fun Iterable.any(): Boolean { - if (this is Collection) return !isEmpty() - return iterator().hasNext() -} - -/** - * Returns `true` if at least one element matches the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.anyWithPredicate - */ -public inline fun Iterable.any(predicate: (T) -> Boolean): Boolean { - if (this is Collection && isEmpty()) return false - for (element in this) if (predicate(element)) return true - return false -} - -/** - * Returns the number of elements in this collection. - */ -public fun Iterable.count(): Int { - if (this is Collection) return size - var count = 0 - for (element in this) count++ - return count -} - -/** - * Returns the number of elements in this collection. - */ -@kotlin.internal.InlineOnly -public inline fun Collection.count(): Int { - return size -} - -/** - * Returns the number of elements matching the given [predicate]. - */ -public inline fun Iterable.count(predicate: (T) -> Boolean): Int { - if (this is Collection && isEmpty()) return 0 - var count = 0 - for (element in this) if (predicate(element)) count++ - return count -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun Iterable.fold(initial: R, operation: (acc: R, T) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right - * to current accumulator value and each element with its index in the original collection. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself, and calculates the next accumulator value. - */ -public inline fun Iterable.foldIndexed(initial: R, operation: (index: Int, acc: R, T) -> R): R { - var index = 0 - var accumulator = initial - for (element in this) accumulator = operation(index++, accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun List.foldRight(initial: R, operation: (T, acc: R) -> R): R { - var accumulator = initial - if (!isEmpty()) { - val iterator = listIterator(size) - while (iterator.hasPrevious()) { - accumulator = operation(iterator.previous(), accumulator) - } - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left - * to each element with its index in the original list and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun List.foldRightIndexed(initial: R, operation: (index: Int, T, acc: R) -> R): R { - var accumulator = initial - if (!isEmpty()) { - val iterator = listIterator(size) - while (iterator.hasPrevious()) { - val index = iterator.previousIndex() - accumulator = operation(index, iterator.previous(), accumulator) - } - } - return accumulator -} - -/** - * Performs the given [action] on each element. - */ -@kotlin.internal.HidesMembers -public inline fun Iterable.forEach(action: (T) -> Unit): Unit { - for (element in this) action(element) -} - -/** - * Performs the given [action] on each element, providing sequential index with the element. - * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. - */ -public inline fun Iterable.forEachIndexed(action: (index: Int, T) -> Unit): Unit { - var index = 0 - for (item in this) action(index++, item) -} - -/** - * Returns the largest element or `null` if there are no elements. - * - * If any of elements is `NaN` returns `NaN`. - */ -@SinceKotlin("1.1") -public fun Iterable.max(): Double? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var max = iterator.next() - if (max.isNaN()) return max - while (iterator.hasNext()) { - val e = iterator.next() - if (e.isNaN()) return e - if (max < e) max = e - } - return max -} - -/** - * Returns the largest element or `null` if there are no elements. - * - * If any of elements is `NaN` returns `NaN`. - */ -@SinceKotlin("1.1") -public fun Iterable.max(): Float? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var max = iterator.next() - if (max.isNaN()) return max - while (iterator.hasNext()) { - val e = iterator.next() - if (e.isNaN()) return e - if (max < e) max = e - } - return max -} - -/** - * Returns the largest element or `null` if there are no elements. - */ -public fun > Iterable.max(): T? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var max = iterator.next() - while (iterator.hasNext()) { - val e = iterator.next() - if (max < e) max = e - } - return max -} - -/** - * Returns the first element yielding the largest value of the given function or `null` if there are no elements. - */ -public inline fun > Iterable.maxBy(selector: (T) -> R): T? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var maxElem = iterator.next() - var maxValue = selector(maxElem) - while (iterator.hasNext()) { - val e = iterator.next() - val v = selector(e) - if (maxValue < v) { - maxElem = e - maxValue = v - } - } - return maxElem -} - -/** - * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun Iterable.maxWith(comparator: Comparator): T? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var max = iterator.next() - while (iterator.hasNext()) { - val e = iterator.next() - if (comparator.compare(max, e) < 0) max = e - } - return max -} - -/** - * Returns the smallest element or `null` if there are no elements. - * - * If any of elements is `NaN` returns `NaN`. - */ -@SinceKotlin("1.1") -public fun Iterable.min(): Double? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var min = iterator.next() - if (min.isNaN()) return min - while (iterator.hasNext()) { - val e = iterator.next() - if (e.isNaN()) return e - if (min > e) min = e - } - return min -} - -/** - * Returns the smallest element or `null` if there are no elements. - * - * If any of elements is `NaN` returns `NaN`. - */ -@SinceKotlin("1.1") -public fun Iterable.min(): Float? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var min = iterator.next() - if (min.isNaN()) return min - while (iterator.hasNext()) { - val e = iterator.next() - if (e.isNaN()) return e - if (min > e) min = e - } - return min -} - -/** - * Returns the smallest element or `null` if there are no elements. - */ -public fun > Iterable.min(): T? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var min = iterator.next() - while (iterator.hasNext()) { - val e = iterator.next() - if (min > e) min = e - } - return min -} - -/** - * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. - */ -public inline fun > Iterable.minBy(selector: (T) -> R): T? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var minElem = iterator.next() - var minValue = selector(minElem) - while (iterator.hasNext()) { - val e = iterator.next() - val v = selector(e) - if (minValue > v) { - minElem = e - minValue = v - } - } - return minElem -} - -/** - * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. - */ -public fun Iterable.minWith(comparator: Comparator): T? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var min = iterator.next() - while (iterator.hasNext()) { - val e = iterator.next() - if (comparator.compare(min, e) > 0) min = e - } - return min -} - -/** - * Returns `true` if the collection has no elements. - * - * @sample samples.collections.Collections.Aggregates.none - */ -public fun Iterable.none(): Boolean { - if (this is Collection) return isEmpty() - return !iterator().hasNext() -} - -/** - * Returns `true` if no elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.noneWithPredicate - */ -public inline fun Iterable.none(predicate: (T) -> Boolean): Boolean { - if (this is Collection && isEmpty()) return true - for (element in this) if (predicate(element)) return false - return true -} - -/** - * Performs the given [action] on each element and returns the collection itself afterwards. - */ -@SinceKotlin("1.1") -public inline fun > C.onEach(action: (T) -> Unit): C { - return apply { for (element in this) action(element) } -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun Iterable.reduce(operation: (acc: S, T) -> S): S { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty collection can't be reduced.") - var accumulator: S = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(accumulator, iterator.next()) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right - * to current accumulator value and each element with its index in the original collection. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself and calculates the next accumulator value. - */ -public inline fun Iterable.reduceIndexed(operation: (index: Int, acc: S, T) -> S): S { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty collection can't be reduced.") - var index = 1 - var accumulator: S = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(index++, accumulator, iterator.next()) - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun List.reduceRight(operation: (T, acc: S) -> S): S { - val iterator = listIterator(size) - if (!iterator.hasPrevious()) - throw UnsupportedOperationException("Empty list can't be reduced.") - var accumulator: S = iterator.previous() - while (iterator.hasPrevious()) { - accumulator = operation(iterator.previous(), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left - * to each element with its index in the original list and current accumulator value. - * @param [operation] function that takes the index of an element, the element itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun List.reduceRightIndexed(operation: (index: Int, T, acc: S) -> S): S { - val iterator = listIterator(size) - if (!iterator.hasPrevious()) - throw UnsupportedOperationException("Empty list can't be reduced.") - var accumulator: S = iterator.previous() - while (iterator.hasPrevious()) { - val index = iterator.previousIndex() - accumulator = operation(index, iterator.previous(), accumulator) - } - return accumulator -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the collection. - */ -public inline fun Iterable.sumBy(selector: (T) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the collection. - */ -public inline fun Iterable.sumByDouble(selector: (T) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns an original collection containing all the non-`null` elements, throwing an [IllegalArgumentException] if there are any `null` elements. - */ -public fun Iterable.requireNoNulls(): Iterable { - for (element in this) { - if (element == null) { - throw IllegalArgumentException("null element found in $this.") - } - } - @Suppress("UNCHECKED_CAST") - return this as Iterable -} - -/** - * Returns an original collection containing all the non-`null` elements, throwing an [IllegalArgumentException] if there are any `null` elements. - */ -public fun List.requireNoNulls(): List { - for (element in this) { - if (element == null) { - throw IllegalArgumentException("null element found in $this.") - } - } - @Suppress("UNCHECKED_CAST") - return this as List -} - -/** - * Splits this collection into a list of lists each not exceeding the given [size]. - * - * The last list in the resulting list may have less elements than the given [size]. - * - * @param size the number of elements to take in each list, must be positive and can be greater than the number of elements in this collection. - * - * @sample samples.collections.Collections.Transformations.chunked - */ -@SinceKotlin("1.2") -public fun Iterable.chunked(size: Int): List> { - return windowed(size, size, partialWindows = true) -} - -/** - * Splits this collection into several lists each not exceeding the given [size] - * and applies the given [transform] function to an each. - * - * @return list of results of the [transform] applied to an each list. - * - * Note that the list passed to the [transform] function is ephemeral and is valid only inside that function. - * You should not store it or allow it to escape in some way, unless you made a snapshot of it. - * The last list may have less elements than the given [size]. - * - * @param size the number of elements to take in each list, must be positive and can be greater than the number of elements in this collection. - * - * @sample samples.text.Strings.chunkedTransform - */ -@SinceKotlin("1.2") -public fun Iterable.chunked(size: Int, transform: (List) -> R): List { - return windowed(size, size, partialWindows = true, transform = transform) -} - -/** - * Returns a list containing all elements of the original collection without the first occurrence of the given [element]. - */ -public operator fun Iterable.minus(element: T): List { - val result = ArrayList(collectionSizeOrDefault(10)) - var removed = false - return this.filterTo(result) { if (!removed && it == element) { removed = true; false } else true } -} - -/** - * Returns a list containing all elements of the original collection except the elements contained in the given [elements] array. - */ -public operator fun Iterable.minus(elements: Array): List { - if (elements.isEmpty()) return this.toList() - val other = elements.toHashSet() - return this.filterNot { it in other } -} - -/** - * Returns a list containing all elements of the original collection except the elements contained in the given [elements] collection. - */ -public operator fun Iterable.minus(elements: Iterable): List { - val other = elements.convertToSetForSetOperationWith(this) - if (other.isEmpty()) - return this.toList() - return this.filterNot { it in other } -} - -/** - * Returns a list containing all elements of the original collection except the elements contained in the given [elements] sequence. - */ -public operator fun Iterable.minus(elements: Sequence): List { - val other = elements.toHashSet() - if (other.isEmpty()) - return this.toList() - return this.filterNot { it in other } -} - -/** - * Returns a list containing all elements of the original collection without the first occurrence of the given [element]. - */ -@kotlin.internal.InlineOnly -public inline fun Iterable.minusElement(element: T): List { - return minus(element) -} - -/** - * Splits the original collection into pair of lists, - * where *first* list contains elements for which [predicate] yielded `true`, - * while *second* list contains elements for which [predicate] yielded `false`. - */ -public inline fun Iterable.partition(predicate: (T) -> Boolean): Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Returns a list containing all elements of the original collection and then the given [element]. - */ -public operator fun Iterable.plus(element: T): List { - if (this is Collection) return this.plus(element) - val result = ArrayList() - result.addAll(this) - result.add(element) - return result -} - -/** - * Returns a list containing all elements of the original collection and then the given [element]. - */ -public operator fun Collection.plus(element: T): List { - val result = ArrayList(size + 1) - result.addAll(this) - result.add(element) - return result -} - -/** - * Returns a list containing all elements of the original collection and then all elements of the given [elements] array. - */ -public operator fun Iterable.plus(elements: Array): List { - if (this is Collection) return this.plus(elements) - val result = ArrayList() - result.addAll(this) - result.addAll(elements) - return result -} - -/** - * Returns a list containing all elements of the original collection and then all elements of the given [elements] array. - */ -public operator fun Collection.plus(elements: Array): List { - val result = ArrayList(this.size + elements.size) - result.addAll(this) - result.addAll(elements) - return result -} - -/** - * Returns a list containing all elements of the original collection and then all elements of the given [elements] collection. - */ -public operator fun Iterable.plus(elements: Iterable): List { - if (this is Collection) return this.plus(elements) - val result = ArrayList() - result.addAll(this) - result.addAll(elements) - return result -} - -/** - * Returns a list containing all elements of the original collection and then all elements of the given [elements] collection. - */ -public operator fun Collection.plus(elements: Iterable): List { - if (elements is Collection) { - val result = ArrayList(this.size + elements.size) - result.addAll(this) - result.addAll(elements) - return result - } else { - val result = ArrayList(this) - result.addAll(elements) - return result - } -} - -/** - * Returns a list containing all elements of the original collection and then all elements of the given [elements] sequence. - */ -public operator fun Iterable.plus(elements: Sequence): List { - val result = ArrayList() - result.addAll(this) - result.addAll(elements) - return result -} - -/** - * Returns a list containing all elements of the original collection and then all elements of the given [elements] sequence. - */ -public operator fun Collection.plus(elements: Sequence): List { - val result = ArrayList(this.size + 10) - result.addAll(this) - result.addAll(elements) - return result -} - -/** - * Returns a list containing all elements of the original collection and then the given [element]. - */ -@kotlin.internal.InlineOnly -public inline fun Iterable.plusElement(element: T): List { - return plus(element) -} - -/** - * Returns a list containing all elements of the original collection and then the given [element]. - */ -@kotlin.internal.InlineOnly -public inline fun Collection.plusElement(element: T): List { - return plus(element) -} - -/** - * Returns a list of snapshots of the window of the given [size] - * sliding along this collection with the given [step], where each - * snapshot is a list. - * - * Several last lists may have less elements than the given [size]. - * - * Both [size] and [step] must be positive and can be greater than the number of elements in this collection. - * @param size the number of elements to take in each window - * @param step the number of elements to move the window forward by on an each step, by default 1 - * @param partialWindows controls whether or not to keep partial windows in the end if any, - * by default `false` which means partial windows won't be preserved - * - * @sample samples.collections.Sequences.Transformations.takeWindows - */ -@SinceKotlin("1.2") -public fun Iterable.windowed(size: Int, step: Int = 1, partialWindows: Boolean = false): List> { - checkWindowSizeStep(size, step) - if (this is RandomAccess && this is List) { - val thisSize = this.size - val result = ArrayList>((thisSize + step - 1) / step) - var index = 0 - while (index < thisSize) { - val windowSize = size.coerceAtMost(thisSize - index) - if (windowSize < size && !partialWindows) break - result.add(List(windowSize) { this[it + index] }) - index += step - } - return result - } - val result = ArrayList>() - windowedIterator(iterator(), size, step, partialWindows, reuseBuffer = false).forEach { - result.add(it) - } - return result -} - -/** - * Returns a list of results of applying the given [transform] function to - * an each list representing a view over the window of the given [size] - * sliding along this collection with the given [step]. - * - * Note that the list passed to the [transform] function is ephemeral and is valid only inside that function. - * You should not store it or allow it to escape in some way, unless you made a snapshot of it. - * Several last lists may have less elements than the given [size]. - * - * Both [size] and [step] must be positive and can be greater than the number of elements in this collection. - * @param size the number of elements to take in each window - * @param step the number of elements to move the window forward by on an each step, by default 1 - * @param partialWindows controls whether or not to keep partial windows in the end if any, - * by default `false` which means partial windows won't be preserved - * - * @sample samples.collections.Sequences.Transformations.averageWindows - */ -@SinceKotlin("1.2") -public fun Iterable.windowed(size: Int, step: Int = 1, partialWindows: Boolean = false, transform: (List) -> R): List { - checkWindowSizeStep(size, step) - if (this is RandomAccess && this is List) { - val thisSize = this.size - val result = ArrayList((thisSize + step - 1) / step) - val window = MovingSubList(this) - var index = 0 - while (index < thisSize) { - window.move(index, (index + size).coerceAtMost(thisSize)) - if (!partialWindows && window.size < size) break - result.add(transform(window)) - index += step - } - return result - } - val result = ArrayList() - windowedIterator(iterator(), size, step, partialWindows, reuseBuffer = true).forEach { - result.add(transform(it)) - } - return result -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun Iterable.zip(other: Array): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun Iterable.zip(other: Array, transform: (a: T, b: R) -> V): List { - val arraySize = other.size - val list = ArrayList(minOf(collectionSizeOrDefault(10), arraySize)) - var i = 0 - for (element in this) { - if (i >= arraySize) break - list.add(transform(element, other[i++])) - } - return list -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public infix fun Iterable.zip(other: Iterable): List> { - return zip(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun Iterable.zip(other: Iterable, transform: (a: T, b: R) -> V): List { - val first = iterator() - val second = other.iterator() - val list = ArrayList(minOf(collectionSizeOrDefault(10), other.collectionSizeOrDefault(10))) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) - } - return list -} - -/** - * Returns a list of pairs of each two adjacent elements in this collection. - * - * The returned list is empty if this collection contains less than two elements. - * - * @sample samples.collections.Collections.Transformations.zipWithNext - */ -@SinceKotlin("1.2") -public fun Iterable.zipWithNext(): List> { - return zipWithNext { a, b -> a to b } -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to an each pair of two adjacent elements in this collection. - * - * The returned list is empty if this collection contains less than two elements. - * - * @sample samples.collections.Collections.Transformations.zipWithNextToFindDeltas - */ -@SinceKotlin("1.2") -public inline fun Iterable.zipWithNext(transform: (a: T, b: T) -> R): List { - val iterator = iterator() - if (!iterator.hasNext()) return emptyList() - val result = mutableListOf() - var current = iterator.next() - while (iterator.hasNext()) { - val next = iterator.next() - result.add(transform(current, next)) - current = next - } - return result -} - -/** - * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinTo - */ -public fun Iterable.joinTo(buffer: A, separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((T) -> CharSequence)? = null): A { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - buffer.appendElement(element, transform) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) - return buffer -} - -/** - * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinToString - */ -public fun Iterable.joinToString(separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((T) -> CharSequence)? = null): String { - return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() -} - -/** - * Returns this collection as an [Iterable]. - */ -@kotlin.internal.InlineOnly -public inline fun Iterable.asIterable(): Iterable { - return this -} - -/** - * Creates a [Sequence] instance that wraps the original collection returning its elements when being iterated. - * - * @sample samples.collections.Sequences.Building.sequenceFromCollection - */ -public fun Iterable.asSequence(): Sequence { - return Sequence { this.iterator() } -} - -/** - * Returns an average value of elements in the collection. - */ -@kotlin.jvm.JvmName("averageOfByte") -public fun Iterable.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the collection. - */ -@kotlin.jvm.JvmName("averageOfShort") -public fun Iterable.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the collection. - */ -@kotlin.jvm.JvmName("averageOfInt") -public fun Iterable.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the collection. - */ -@kotlin.jvm.JvmName("averageOfLong") -public fun Iterable.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the collection. - */ -@kotlin.jvm.JvmName("averageOfFloat") -public fun Iterable.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the collection. - */ -@kotlin.jvm.JvmName("averageOfDouble") -public fun Iterable.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns the sum of all elements in the collection. - */ -@kotlin.jvm.JvmName("sumOfByte") -public fun Iterable.sum(): Int { - var sum: Int = 0 - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the collection. - */ -@kotlin.jvm.JvmName("sumOfShort") -public fun Iterable.sum(): Int { - var sum: Int = 0 - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the collection. - */ -@kotlin.jvm.JvmName("sumOfInt") -public fun Iterable.sum(): Int { - var sum: Int = 0 - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the collection. - */ -@kotlin.jvm.JvmName("sumOfLong") -public fun Iterable.sum(): Long { - var sum: Long = 0L - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the collection. - */ -@kotlin.jvm.JvmName("sumOfFloat") -public fun Iterable.sum(): Float { - var sum: Float = 0.0f - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the collection. - */ -@kotlin.jvm.JvmName("sumOfDouble") -public fun Iterable.sum(): Double { - var sum: Double = 0.0 - for (element in this) { - sum += element - } - return sum -} - diff --git a/libraries/stdlib/src/generated/_Comparisons.kt b/libraries/stdlib/src/generated/_Comparisons.kt deleted file mode 100644 index 8dd5b305fc5..00000000000 --- a/libraries/stdlib/src/generated/_Comparisons.kt +++ /dev/null @@ -1,299 +0,0 @@ -@file:kotlin.jvm.JvmMultifileClass -@file:kotlin.jvm.JvmName("ComparisonsKt") -@file:kotlin.jvm.JvmVersion - -package kotlin.comparisons - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// - -import kotlin.* -import kotlin.text.* -import kotlin.comparisons.* - -/** - * Returns the greater of two values. - * If values are equal, returns the first one. - */ -@SinceKotlin("1.1") -public fun > maxOf(a: T, b: T): T { - return if (a >= b) a else b -} - -/** - * Returns the greater of two values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun maxOf(a: Byte, b: Byte): Byte { - return Math.max(a.toInt(), b.toInt()).toByte() -} - -/** - * Returns the greater of two values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun maxOf(a: Short, b: Short): Short { - return Math.max(a.toInt(), b.toInt()).toShort() -} - -/** - * Returns the greater of two values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun maxOf(a: Int, b: Int): Int { - return Math.max(a, b) -} - -/** - * Returns the greater of two values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun maxOf(a: Long, b: Long): Long { - return Math.max(a, b) -} - -/** - * Returns the greater of two values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun maxOf(a: Float, b: Float): Float { - return Math.max(a, b) -} - -/** - * Returns the greater of two values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun maxOf(a: Double, b: Double): Double { - return Math.max(a, b) -} - -/** - * Returns the greater of three values. - */ -@SinceKotlin("1.1") -public fun > maxOf(a: T, b: T, c: T): T { - return maxOf(a, maxOf(b, c)) -} - -/** - * Returns the greater of three values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun maxOf(a: Byte, b: Byte, c: Byte): Byte { - return Math.max(a.toInt(), Math.max(b.toInt(), c.toInt())).toByte() -} - -/** - * Returns the greater of three values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun maxOf(a: Short, b: Short, c: Short): Short { - return Math.max(a.toInt(), Math.max(b.toInt(), c.toInt())).toShort() -} - -/** - * Returns the greater of three values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun maxOf(a: Int, b: Int, c: Int): Int { - return maxOf(a, maxOf(b, c)) -} - -/** - * Returns the greater of three values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun maxOf(a: Long, b: Long, c: Long): Long { - return maxOf(a, maxOf(b, c)) -} - -/** - * Returns the greater of three values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun maxOf(a: Float, b: Float, c: Float): Float { - return maxOf(a, maxOf(b, c)) -} - -/** - * Returns the greater of three values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun maxOf(a: Double, b: Double, c: Double): Double { - return maxOf(a, maxOf(b, c)) -} - -/** - * Returns the greater of three values according to the order specified by the given [comparator]. - */ -@SinceKotlin("1.1") -public fun maxOf(a: T, b: T, c: T, comparator: Comparator): T { - return maxOf(a, maxOf(b, c, comparator), comparator) -} - -/** - * Returns the greater of two values according to the order specified by the given [comparator]. - * If values are equal, returns the first one. - */ -@SinceKotlin("1.1") -public fun maxOf(a: T, b: T, comparator: Comparator): T { - return if (comparator.compare(a, b) >= 0) a else b -} - -/** - * Returns the smaller of two values. - * If values are equal, returns the first one. - */ -@SinceKotlin("1.1") -public fun > minOf(a: T, b: T): T { - return if (a <= b) a else b -} - -/** - * Returns the smaller of two values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun minOf(a: Byte, b: Byte): Byte { - return Math.min(a.toInt(), b.toInt()).toByte() -} - -/** - * Returns the smaller of two values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun minOf(a: Short, b: Short): Short { - return Math.min(a.toInt(), b.toInt()).toShort() -} - -/** - * Returns the smaller of two values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun minOf(a: Int, b: Int): Int { - return Math.min(a, b) -} - -/** - * Returns the smaller of two values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun minOf(a: Long, b: Long): Long { - return Math.min(a, b) -} - -/** - * Returns the smaller of two values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun minOf(a: Float, b: Float): Float { - return Math.min(a, b) -} - -/** - * Returns the smaller of two values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun minOf(a: Double, b: Double): Double { - return Math.min(a, b) -} - -/** - * Returns the smaller of three values. - */ -@SinceKotlin("1.1") -public fun > minOf(a: T, b: T, c: T): T { - return minOf(a, minOf(b, c)) -} - -/** - * Returns the smaller of three values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun minOf(a: Byte, b: Byte, c: Byte): Byte { - return Math.min(a.toInt(), Math.min(b.toInt(), c.toInt())).toByte() -} - -/** - * Returns the smaller of three values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun minOf(a: Short, b: Short, c: Short): Short { - return Math.min(a.toInt(), Math.min(b.toInt(), c.toInt())).toShort() -} - -/** - * Returns the smaller of three values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun minOf(a: Int, b: Int, c: Int): Int { - return minOf(a, minOf(b, c)) -} - -/** - * Returns the smaller of three values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun minOf(a: Long, b: Long, c: Long): Long { - return minOf(a, minOf(b, c)) -} - -/** - * Returns the smaller of three values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun minOf(a: Float, b: Float, c: Float): Float { - return minOf(a, minOf(b, c)) -} - -/** - * Returns the smaller of three values. - */ -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline fun minOf(a: Double, b: Double, c: Double): Double { - return minOf(a, minOf(b, c)) -} - -/** - * Returns the smaller of three values according to the order specified by the given [comparator]. - */ -@SinceKotlin("1.1") -public fun minOf(a: T, b: T, c: T, comparator: Comparator): T { - return minOf(a, minOf(b, c, comparator), comparator) -} - -/** - * Returns the smaller of two values according to the order specified by the given [comparator]. - * If values are equal, returns the first one. - */ -@SinceKotlin("1.1") -public fun minOf(a: T, b: T, comparator: Comparator): T { - return if (comparator.compare(a, b) <= 0) a else b -} - diff --git a/libraries/stdlib/src/generated/_Maps.kt b/libraries/stdlib/src/generated/_Maps.kt deleted file mode 100644 index e0db373bad1..00000000000 --- a/libraries/stdlib/src/generated/_Maps.kt +++ /dev/null @@ -1,218 +0,0 @@ -@file:kotlin.jvm.JvmMultifileClass -@file:kotlin.jvm.JvmName("MapsKt") -@file:kotlin.jvm.JvmVersion - -package kotlin.collections - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// - -import kotlin.* -import kotlin.text.* -import kotlin.comparisons.* - -/** - * Returns a [List] containing all key-value pairs. - */ -public fun Map.toList(): List> { - if (size == 0) - return emptyList() - val iterator = entries.iterator() - if (!iterator.hasNext()) - return emptyList() - val first = iterator.next() - if (!iterator.hasNext()) - return listOf(first.toPair()) - val result = ArrayList>(size) - result.add(first.toPair()) - do { - result.add(iterator.next().toPair()) - } while (iterator.hasNext()) - return result -} - -/** - * Returns a single list of all elements yielded from results of [transform] function being invoked on each entry of original map. - */ -public inline fun Map.flatMap(transform: (Map.Entry) -> Iterable): List { - return flatMapTo(ArrayList(), transform) -} - -/** - * Appends all elements yielded from results of [transform] function being invoked on each entry of original map, to the given [destination]. - */ -public inline fun > Map.flatMapTo(destination: C, transform: (Map.Entry) -> Iterable): C { - for (element in this) { - val list = transform(element) - destination.addAll(list) - } - return destination -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each entry in the original map. - */ -public inline fun Map.map(transform: (Map.Entry) -> R): List { - return mapTo(ArrayList(size), transform) -} - -/** - * Returns a list containing only the non-null results of applying the given [transform] function - * to each entry in the original map. - */ -public inline fun Map.mapNotNull(transform: (Map.Entry) -> R?): List { - return mapNotNullTo(ArrayList(), transform) -} - -/** - * Applies the given [transform] function to each entry in the original map - * and appends only the non-null results to the given [destination]. - */ -public inline fun > Map.mapNotNullTo(destination: C, transform: (Map.Entry) -> R?): C { - forEach { element -> transform(element)?.let { destination.add(it) } } - return destination -} - -/** - * Applies the given [transform] function to each entry of the original map - * and appends the results to the given [destination]. - */ -public inline fun > Map.mapTo(destination: C, transform: (Map.Entry) -> R): C { - for (item in this) - destination.add(transform(item)) - return destination -} - -/** - * Returns `true` if all entries match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.all - */ -public inline fun Map.all(predicate: (Map.Entry) -> Boolean): Boolean { - if (isEmpty()) return true - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if map has at least one entry. - * - * @sample samples.collections.Collections.Aggregates.any - */ -public fun Map.any(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if at least one entry matches the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.anyWithPredicate - */ -public inline fun Map.any(predicate: (Map.Entry) -> Boolean): Boolean { - if (isEmpty()) return false - for (element in this) if (predicate(element)) return true - return false -} - -/** - * Returns the number of entries in this map. - */ -@kotlin.internal.InlineOnly -public inline fun Map.count(): Int { - return size -} - -/** - * Returns the number of entries matching the given [predicate]. - */ -public inline fun Map.count(predicate: (Map.Entry) -> Boolean): Int { - if (isEmpty()) return 0 - var count = 0 - for (element in this) if (predicate(element)) count++ - return count -} - -/** - * Performs the given [action] on each entry. - */ -@kotlin.internal.HidesMembers -public inline fun Map.forEach(action: (Map.Entry) -> Unit): Unit { - for (element in this) action(element) -} - -/** - * Returns the first entry yielding the largest value of the given function or `null` if there are no entries. - */ -@kotlin.internal.InlineOnly -public inline fun > Map.maxBy(selector: (Map.Entry) -> R): Map.Entry? { - return entries.maxBy(selector) -} - -/** - * Returns the first entry having the largest value according to the provided [comparator] or `null` if there are no entries. - */ -@kotlin.internal.InlineOnly -public inline fun Map.maxWith(comparator: Comparator>): Map.Entry? { - return entries.maxWith(comparator) -} - -/** - * Returns the first entry yielding the smallest value of the given function or `null` if there are no entries. - */ -public inline fun > Map.minBy(selector: (Map.Entry) -> R): Map.Entry? { - return entries.minBy(selector) -} - -/** - * Returns the first entry having the smallest value according to the provided [comparator] or `null` if there are no entries. - */ -public fun Map.minWith(comparator: Comparator>): Map.Entry? { - return entries.minWith(comparator) -} - -/** - * Returns `true` if the map has no entries. - * - * @sample samples.collections.Collections.Aggregates.none - */ -public fun Map.none(): Boolean { - return isEmpty() -} - -/** - * Returns `true` if no entries match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.noneWithPredicate - */ -public inline fun Map.none(predicate: (Map.Entry) -> Boolean): Boolean { - if (isEmpty()) return true - for (element in this) if (predicate(element)) return false - return true -} - -/** - * Performs the given [action] on each entry and returns the map itself afterwards. - */ -@SinceKotlin("1.1") -public inline fun > M.onEach(action: (Map.Entry) -> Unit): M { - return apply { for (element in this) action(element) } -} - -/** - * Creates an [Iterable] instance that wraps the original map returning its entries when being iterated. - */ -@kotlin.internal.InlineOnly -public inline fun Map.asIterable(): Iterable> { - return entries -} - -/** - * Creates a [Sequence] instance that wraps the original map returning its entries when being iterated. - */ -public fun Map.asSequence(): Sequence> { - return entries.asSequence() -} - diff --git a/libraries/stdlib/src/generated/_Ranges.kt b/libraries/stdlib/src/generated/_Ranges.kt deleted file mode 100644 index e1337a4a784..00000000000 --- a/libraries/stdlib/src/generated/_Ranges.kt +++ /dev/null @@ -1,961 +0,0 @@ -@file:kotlin.jvm.JvmMultifileClass -@file:kotlin.jvm.JvmName("RangesKt") -@file:kotlin.jvm.JvmVersion - -package kotlin.ranges - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// - -import kotlin.* -import kotlin.text.* -import kotlin.comparisons.* - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("intRangeContains") -public operator fun ClosedRange.contains(value: Byte): Boolean { - return contains(value.toInt()) -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("longRangeContains") -public operator fun ClosedRange.contains(value: Byte): Boolean { - return contains(value.toLong()) -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("shortRangeContains") -public operator fun ClosedRange.contains(value: Byte): Boolean { - return contains(value.toShort()) -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("doubleRangeContains") -public operator fun ClosedRange.contains(value: Byte): Boolean { - return contains(value.toDouble()) -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("floatRangeContains") -public operator fun ClosedRange.contains(value: Byte): Boolean { - return contains(value.toFloat()) -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("intRangeContains") -public operator fun ClosedRange.contains(value: Double): Boolean { - return value.toIntExactOrNull().let { if (it != null) contains(it) else false } -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("longRangeContains") -public operator fun ClosedRange.contains(value: Double): Boolean { - return value.toLongExactOrNull().let { if (it != null) contains(it) else false } -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("byteRangeContains") -public operator fun ClosedRange.contains(value: Double): Boolean { - return value.toByteExactOrNull().let { if (it != null) contains(it) else false } -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("shortRangeContains") -public operator fun ClosedRange.contains(value: Double): Boolean { - return value.toShortExactOrNull().let { if (it != null) contains(it) else false } -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("floatRangeContains") -public operator fun ClosedRange.contains(value: Double): Boolean { - return contains(value.toFloat()) -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("intRangeContains") -public operator fun ClosedRange.contains(value: Float): Boolean { - return value.toIntExactOrNull().let { if (it != null) contains(it) else false } -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("longRangeContains") -public operator fun ClosedRange.contains(value: Float): Boolean { - return value.toLongExactOrNull().let { if (it != null) contains(it) else false } -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("byteRangeContains") -public operator fun ClosedRange.contains(value: Float): Boolean { - return value.toByteExactOrNull().let { if (it != null) contains(it) else false } -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("shortRangeContains") -public operator fun ClosedRange.contains(value: Float): Boolean { - return value.toShortExactOrNull().let { if (it != null) contains(it) else false } -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("doubleRangeContains") -public operator fun ClosedRange.contains(value: Float): Boolean { - return contains(value.toDouble()) -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("longRangeContains") -public operator fun ClosedRange.contains(value: Int): Boolean { - return contains(value.toLong()) -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("byteRangeContains") -public operator fun ClosedRange.contains(value: Int): Boolean { - return value.toByteExactOrNull().let { if (it != null) contains(it) else false } -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("shortRangeContains") -public operator fun ClosedRange.contains(value: Int): Boolean { - return value.toShortExactOrNull().let { if (it != null) contains(it) else false } -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("doubleRangeContains") -public operator fun ClosedRange.contains(value: Int): Boolean { - return contains(value.toDouble()) -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("floatRangeContains") -public operator fun ClosedRange.contains(value: Int): Boolean { - return contains(value.toFloat()) -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("intRangeContains") -public operator fun ClosedRange.contains(value: Long): Boolean { - return value.toIntExactOrNull().let { if (it != null) contains(it) else false } -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("byteRangeContains") -public operator fun ClosedRange.contains(value: Long): Boolean { - return value.toByteExactOrNull().let { if (it != null) contains(it) else false } -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("shortRangeContains") -public operator fun ClosedRange.contains(value: Long): Boolean { - return value.toShortExactOrNull().let { if (it != null) contains(it) else false } -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("doubleRangeContains") -public operator fun ClosedRange.contains(value: Long): Boolean { - return contains(value.toDouble()) -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("floatRangeContains") -public operator fun ClosedRange.contains(value: Long): Boolean { - return contains(value.toFloat()) -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("intRangeContains") -public operator fun ClosedRange.contains(value: Short): Boolean { - return contains(value.toInt()) -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("longRangeContains") -public operator fun ClosedRange.contains(value: Short): Boolean { - return contains(value.toLong()) -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("byteRangeContains") -public operator fun ClosedRange.contains(value: Short): Boolean { - return value.toByteExactOrNull().let { if (it != null) contains(it) else false } -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("doubleRangeContains") -public operator fun ClosedRange.contains(value: Short): Boolean { - return contains(value.toDouble()) -} - -/** - * Checks if the specified [value] belongs to this range. - */ -@kotlin.jvm.JvmName("floatRangeContains") -public operator fun ClosedRange.contains(value: Short): Boolean { - return contains(value.toFloat()) -} - -/** - * Returns a progression from this value down to the specified [to] value with the step -1. - * - * The [to] value has to be less than this value. - */ -public infix fun Int.downTo(to: Byte): IntProgression { - return IntProgression.fromClosedRange(this, to.toInt(), -1) -} - -/** - * Returns a progression from this value down to the specified [to] value with the step -1. - * - * The [to] value has to be less than this value. - */ -public infix fun Long.downTo(to: Byte): LongProgression { - return LongProgression.fromClosedRange(this, to.toLong(), -1L) -} - -/** - * Returns a progression from this value down to the specified [to] value with the step -1. - * - * The [to] value has to be less than this value. - */ -public infix fun Byte.downTo(to: Byte): IntProgression { - return IntProgression.fromClosedRange(this.toInt(), to.toInt(), -1) -} - -/** - * Returns a progression from this value down to the specified [to] value with the step -1. - * - * The [to] value has to be less than this value. - */ -public infix fun Short.downTo(to: Byte): IntProgression { - return IntProgression.fromClosedRange(this.toInt(), to.toInt(), -1) -} - -/** - * Returns a progression from this value down to the specified [to] value with the step -1. - * - * The [to] value has to be less than this value. - */ -public infix fun Char.downTo(to: Char): CharProgression { - return CharProgression.fromClosedRange(this, to, -1) -} - -/** - * Returns a progression from this value down to the specified [to] value with the step -1. - * - * The [to] value has to be less than this value. - */ -public infix fun Int.downTo(to: Int): IntProgression { - return IntProgression.fromClosedRange(this, to, -1) -} - -/** - * Returns a progression from this value down to the specified [to] value with the step -1. - * - * The [to] value has to be less than this value. - */ -public infix fun Long.downTo(to: Int): LongProgression { - return LongProgression.fromClosedRange(this, to.toLong(), -1L) -} - -/** - * Returns a progression from this value down to the specified [to] value with the step -1. - * - * The [to] value has to be less than this value. - */ -public infix fun Byte.downTo(to: Int): IntProgression { - return IntProgression.fromClosedRange(this.toInt(), to, -1) -} - -/** - * Returns a progression from this value down to the specified [to] value with the step -1. - * - * The [to] value has to be less than this value. - */ -public infix fun Short.downTo(to: Int): IntProgression { - return IntProgression.fromClosedRange(this.toInt(), to, -1) -} - -/** - * Returns a progression from this value down to the specified [to] value with the step -1. - * - * The [to] value has to be less than this value. - */ -public infix fun Int.downTo(to: Long): LongProgression { - return LongProgression.fromClosedRange(this.toLong(), to, -1L) -} - -/** - * Returns a progression from this value down to the specified [to] value with the step -1. - * - * The [to] value has to be less than this value. - */ -public infix fun Long.downTo(to: Long): LongProgression { - return LongProgression.fromClosedRange(this, to, -1L) -} - -/** - * Returns a progression from this value down to the specified [to] value with the step -1. - * - * The [to] value has to be less than this value. - */ -public infix fun Byte.downTo(to: Long): LongProgression { - return LongProgression.fromClosedRange(this.toLong(), to, -1L) -} - -/** - * Returns a progression from this value down to the specified [to] value with the step -1. - * - * The [to] value has to be less than this value. - */ -public infix fun Short.downTo(to: Long): LongProgression { - return LongProgression.fromClosedRange(this.toLong(), to, -1L) -} - -/** - * Returns a progression from this value down to the specified [to] value with the step -1. - * - * The [to] value has to be less than this value. - */ -public infix fun Int.downTo(to: Short): IntProgression { - return IntProgression.fromClosedRange(this, to.toInt(), -1) -} - -/** - * Returns a progression from this value down to the specified [to] value with the step -1. - * - * The [to] value has to be less than this value. - */ -public infix fun Long.downTo(to: Short): LongProgression { - return LongProgression.fromClosedRange(this, to.toLong(), -1L) -} - -/** - * Returns a progression from this value down to the specified [to] value with the step -1. - * - * The [to] value has to be less than this value. - */ -public infix fun Byte.downTo(to: Short): IntProgression { - return IntProgression.fromClosedRange(this.toInt(), to.toInt(), -1) -} - -/** - * Returns a progression from this value down to the specified [to] value with the step -1. - * - * The [to] value has to be less than this value. - */ -public infix fun Short.downTo(to: Short): IntProgression { - return IntProgression.fromClosedRange(this.toInt(), to.toInt(), -1) -} - -/** - * Returns a progression that goes over the same range in the opposite direction with the same step. - */ -public fun IntProgression.reversed(): IntProgression { - return IntProgression.fromClosedRange(last, first, -step) -} - -/** - * Returns a progression that goes over the same range in the opposite direction with the same step. - */ -public fun LongProgression.reversed(): LongProgression { - return LongProgression.fromClosedRange(last, first, -step) -} - -/** - * Returns a progression that goes over the same range in the opposite direction with the same step. - */ -public fun CharProgression.reversed(): CharProgression { - return CharProgression.fromClosedRange(last, first, -step) -} - -/** - * Returns a progression that goes over the same range with the given step. - */ -public infix fun IntProgression.step(step: Int): IntProgression { - checkStepIsPositive(step > 0, step) - return IntProgression.fromClosedRange(first, last, if (this.step > 0) step else -step) -} - -/** - * Returns a progression that goes over the same range with the given step. - */ -public infix fun LongProgression.step(step: Long): LongProgression { - checkStepIsPositive(step > 0, step) - return LongProgression.fromClosedRange(first, last, if (this.step > 0) step else -step) -} - -/** - * Returns a progression that goes over the same range with the given step. - */ -public infix fun CharProgression.step(step: Int): CharProgression { - checkStepIsPositive(step > 0, step) - return CharProgression.fromClosedRange(first, last, if (this.step > 0) step else -step) -} - -internal fun Int.toByteExactOrNull(): Byte? { - return if (this in Byte.MIN_VALUE.toInt()..Byte.MAX_VALUE.toInt()) this.toByte() else null -} - -internal fun Long.toByteExactOrNull(): Byte? { - return if (this in Byte.MIN_VALUE.toLong()..Byte.MAX_VALUE.toLong()) this.toByte() else null -} - -internal fun Short.toByteExactOrNull(): Byte? { - return if (this in Byte.MIN_VALUE.toShort()..Byte.MAX_VALUE.toShort()) this.toByte() else null -} - -internal fun Double.toByteExactOrNull(): Byte? { - return if (this in Byte.MIN_VALUE.toDouble()..Byte.MAX_VALUE.toDouble()) this.toByte() else null -} - -internal fun Float.toByteExactOrNull(): Byte? { - return if (this in Byte.MIN_VALUE.toFloat()..Byte.MAX_VALUE.toFloat()) this.toByte() else null -} - -internal fun Long.toIntExactOrNull(): Int? { - return if (this in Int.MIN_VALUE.toLong()..Int.MAX_VALUE.toLong()) this.toInt() else null -} - -internal fun Double.toIntExactOrNull(): Int? { - return if (this in Int.MIN_VALUE.toDouble()..Int.MAX_VALUE.toDouble()) this.toInt() else null -} - -internal fun Float.toIntExactOrNull(): Int? { - return if (this in Int.MIN_VALUE.toFloat()..Int.MAX_VALUE.toFloat()) this.toInt() else null -} - -internal fun Double.toLongExactOrNull(): Long? { - return if (this in Long.MIN_VALUE.toDouble()..Long.MAX_VALUE.toDouble()) this.toLong() else null -} - -internal fun Float.toLongExactOrNull(): Long? { - return if (this in Long.MIN_VALUE.toFloat()..Long.MAX_VALUE.toFloat()) this.toLong() else null -} - -internal fun Int.toShortExactOrNull(): Short? { - return if (this in Short.MIN_VALUE.toInt()..Short.MAX_VALUE.toInt()) this.toShort() else null -} - -internal fun Long.toShortExactOrNull(): Short? { - return if (this in Short.MIN_VALUE.toLong()..Short.MAX_VALUE.toLong()) this.toShort() else null -} - -internal fun Double.toShortExactOrNull(): Short? { - return if (this in Short.MIN_VALUE.toDouble()..Short.MAX_VALUE.toDouble()) this.toShort() else null -} - -internal fun Float.toShortExactOrNull(): Short? { - return if (this in Short.MIN_VALUE.toFloat()..Short.MAX_VALUE.toFloat()) this.toShort() else null -} - -/** - * Returns a range from this value up to but excluding the specified [to] value. - */ -public infix fun Int.until(to: Byte): IntRange { - return this .. (to.toInt() - 1).toInt() -} - -/** - * Returns a range from this value up to but excluding the specified [to] value. - */ -public infix fun Long.until(to: Byte): LongRange { - return this .. (to.toLong() - 1).toLong() -} - -/** - * Returns a range from this value up to but excluding the specified [to] value. - */ -public infix fun Byte.until(to: Byte): IntRange { - return this.toInt() .. (to.toInt() - 1).toInt() -} - -/** - * Returns a range from this value up to but excluding the specified [to] value. - */ -public infix fun Short.until(to: Byte): IntRange { - return this.toInt() .. (to.toInt() - 1).toInt() -} - -/** - * Returns a range from this value up to but excluding the specified [to] value. - * - * If the [to] value is less than or equal to `'\u0000'` the returned range is empty. - */ -public infix fun Char.until(to: Char): CharRange { - if (to <= '\u0000') return CharRange.EMPTY - return this .. (to - 1).toChar() -} - -/** - * Returns a range from this value up to but excluding the specified [to] value. - * - * If the [to] value is less than or equal to [Int.MIN_VALUE] the returned range is empty. - */ -public infix fun Int.until(to: Int): IntRange { - if (to <= Int.MIN_VALUE) return IntRange.EMPTY - return this .. (to - 1).toInt() -} - -/** - * Returns a range from this value up to but excluding the specified [to] value. - */ -public infix fun Long.until(to: Int): LongRange { - return this .. (to.toLong() - 1).toLong() -} - -/** - * Returns a range from this value up to but excluding the specified [to] value. - * - * If the [to] value is less than or equal to [Int.MIN_VALUE] the returned range is empty. - */ -public infix fun Byte.until(to: Int): IntRange { - if (to <= Int.MIN_VALUE) return IntRange.EMPTY - return this.toInt() .. (to - 1).toInt() -} - -/** - * Returns a range from this value up to but excluding the specified [to] value. - * - * If the [to] value is less than or equal to [Int.MIN_VALUE] the returned range is empty. - */ -public infix fun Short.until(to: Int): IntRange { - if (to <= Int.MIN_VALUE) return IntRange.EMPTY - return this.toInt() .. (to - 1).toInt() -} - -/** - * Returns a range from this value up to but excluding the specified [to] value. - * - * If the [to] value is less than or equal to [Long.MIN_VALUE] the returned range is empty. - */ -public infix fun Int.until(to: Long): LongRange { - if (to <= Long.MIN_VALUE) return LongRange.EMPTY - return this.toLong() .. (to - 1).toLong() -} - -/** - * Returns a range from this value up to but excluding the specified [to] value. - * - * If the [to] value is less than or equal to [Long.MIN_VALUE] the returned range is empty. - */ -public infix fun Long.until(to: Long): LongRange { - if (to <= Long.MIN_VALUE) return LongRange.EMPTY - return this .. (to - 1).toLong() -} - -/** - * Returns a range from this value up to but excluding the specified [to] value. - * - * If the [to] value is less than or equal to [Long.MIN_VALUE] the returned range is empty. - */ -public infix fun Byte.until(to: Long): LongRange { - if (to <= Long.MIN_VALUE) return LongRange.EMPTY - return this.toLong() .. (to - 1).toLong() -} - -/** - * Returns a range from this value up to but excluding the specified [to] value. - * - * If the [to] value is less than or equal to [Long.MIN_VALUE] the returned range is empty. - */ -public infix fun Short.until(to: Long): LongRange { - if (to <= Long.MIN_VALUE) return LongRange.EMPTY - return this.toLong() .. (to - 1).toLong() -} - -/** - * Returns a range from this value up to but excluding the specified [to] value. - */ -public infix fun Int.until(to: Short): IntRange { - return this .. (to.toInt() - 1).toInt() -} - -/** - * Returns a range from this value up to but excluding the specified [to] value. - */ -public infix fun Long.until(to: Short): LongRange { - return this .. (to.toLong() - 1).toLong() -} - -/** - * Returns a range from this value up to but excluding the specified [to] value. - */ -public infix fun Byte.until(to: Short): IntRange { - return this.toInt() .. (to.toInt() - 1).toInt() -} - -/** - * Returns a range from this value up to but excluding the specified [to] value. - */ -public infix fun Short.until(to: Short): IntRange { - return this.toInt() .. (to.toInt() - 1).toInt() -} - -/** - * Ensures that this value is not less than the specified [minimumValue]. - * - * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. - * @sample samples.comparisons.ComparableOps.coerceAtLeastComparable - */ -public fun > T.coerceAtLeast(minimumValue: T): T { - return if (this < minimumValue) minimumValue else this -} - -/** - * Ensures that this value is not less than the specified [minimumValue]. - * - * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. - * @sample samples.comparisons.ComparableOps.coerceAtLeast - */ -public fun Byte.coerceAtLeast(minimumValue: Byte): Byte { - return if (this < minimumValue) minimumValue else this -} - -/** - * Ensures that this value is not less than the specified [minimumValue]. - * - * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. - * @sample samples.comparisons.ComparableOps.coerceAtLeast - */ -public fun Short.coerceAtLeast(minimumValue: Short): Short { - return if (this < minimumValue) minimumValue else this -} - -/** - * Ensures that this value is not less than the specified [minimumValue]. - * - * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. - * @sample samples.comparisons.ComparableOps.coerceAtLeast - */ -public fun Int.coerceAtLeast(minimumValue: Int): Int { - return if (this < minimumValue) minimumValue else this -} - -/** - * Ensures that this value is not less than the specified [minimumValue]. - * - * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. - * @sample samples.comparisons.ComparableOps.coerceAtLeast - */ -public fun Long.coerceAtLeast(minimumValue: Long): Long { - return if (this < minimumValue) minimumValue else this -} - -/** - * Ensures that this value is not less than the specified [minimumValue]. - * - * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. - * @sample samples.comparisons.ComparableOps.coerceAtLeast - */ -public fun Float.coerceAtLeast(minimumValue: Float): Float { - return if (this < minimumValue) minimumValue else this -} - -/** - * Ensures that this value is not less than the specified [minimumValue]. - * - * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. - * @sample samples.comparisons.ComparableOps.coerceAtLeast - */ -public fun Double.coerceAtLeast(minimumValue: Double): Double { - return if (this < minimumValue) minimumValue else this -} - -/** - * Ensures that this value is not greater than the specified [maximumValue]. - * - * @return this value if it's less than or equal to the [maximumValue] or the [maximumValue] otherwise. - * @sample samples.comparisons.ComparableOps.coerceAtMostComparable - */ -public fun > T.coerceAtMost(maximumValue: T): T { - return if (this > maximumValue) maximumValue else this -} - -/** - * Ensures that this value is not greater than the specified [maximumValue]. - * - * @return this value if it's less than or equal to the [maximumValue] or the [maximumValue] otherwise. - * @sample samples.comparisons.ComparableOps.coerceAtMost - */ -public fun Byte.coerceAtMost(maximumValue: Byte): Byte { - return if (this > maximumValue) maximumValue else this -} - -/** - * Ensures that this value is not greater than the specified [maximumValue]. - * - * @return this value if it's less than or equal to the [maximumValue] or the [maximumValue] otherwise. - * @sample samples.comparisons.ComparableOps.coerceAtMost - */ -public fun Short.coerceAtMost(maximumValue: Short): Short { - return if (this > maximumValue) maximumValue else this -} - -/** - * Ensures that this value is not greater than the specified [maximumValue]. - * - * @return this value if it's less than or equal to the [maximumValue] or the [maximumValue] otherwise. - * @sample samples.comparisons.ComparableOps.coerceAtMost - */ -public fun Int.coerceAtMost(maximumValue: Int): Int { - return if (this > maximumValue) maximumValue else this -} - -/** - * Ensures that this value is not greater than the specified [maximumValue]. - * - * @return this value if it's less than or equal to the [maximumValue] or the [maximumValue] otherwise. - * @sample samples.comparisons.ComparableOps.coerceAtMost - */ -public fun Long.coerceAtMost(maximumValue: Long): Long { - return if (this > maximumValue) maximumValue else this -} - -/** - * Ensures that this value is not greater than the specified [maximumValue]. - * - * @return this value if it's less than or equal to the [maximumValue] or the [maximumValue] otherwise. - * @sample samples.comparisons.ComparableOps.coerceAtMost - */ -public fun Float.coerceAtMost(maximumValue: Float): Float { - return if (this > maximumValue) maximumValue else this -} - -/** - * Ensures that this value is not greater than the specified [maximumValue]. - * - * @return this value if it's less than or equal to the [maximumValue] or the [maximumValue] otherwise. - * @sample samples.comparisons.ComparableOps.coerceAtMost - */ -public fun Double.coerceAtMost(maximumValue: Double): Double { - return if (this > maximumValue) maximumValue else this -} - -/** - * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. - * - * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. - * @sample samples.comparisons.ComparableOps.coerceInComparable - */ -public fun > T.coerceIn(minimumValue: T?, maximumValue: T?): T { - if (minimumValue !== null && maximumValue !== null) { - if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") - if (this < minimumValue) return minimumValue - if (this > maximumValue) return maximumValue - } - else { - if (minimumValue !== null && this < minimumValue) return minimumValue - if (maximumValue !== null && this > maximumValue) return maximumValue - } - return this -} - -/** - * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. - * - * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. - * @sample samples.comparisons.ComparableOps.coerceIn - */ -public fun Byte.coerceIn(minimumValue: Byte, maximumValue: Byte): Byte { - if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") - if (this < minimumValue) return minimumValue - if (this > maximumValue) return maximumValue - return this -} - -/** - * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. - * - * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. - * @sample samples.comparisons.ComparableOps.coerceIn - */ -public fun Short.coerceIn(minimumValue: Short, maximumValue: Short): Short { - if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") - if (this < minimumValue) return minimumValue - if (this > maximumValue) return maximumValue - return this -} - -/** - * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. - * - * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. - * @sample samples.comparisons.ComparableOps.coerceIn - */ -public fun Int.coerceIn(minimumValue: Int, maximumValue: Int): Int { - if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") - if (this < minimumValue) return minimumValue - if (this > maximumValue) return maximumValue - return this -} - -/** - * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. - * - * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. - * @sample samples.comparisons.ComparableOps.coerceIn - */ -public fun Long.coerceIn(minimumValue: Long, maximumValue: Long): Long { - if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") - if (this < minimumValue) return minimumValue - if (this > maximumValue) return maximumValue - return this -} - -/** - * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. - * - * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. - * @sample samples.comparisons.ComparableOps.coerceIn - */ -public fun Float.coerceIn(minimumValue: Float, maximumValue: Float): Float { - if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") - if (this < minimumValue) return minimumValue - if (this > maximumValue) return maximumValue - return this -} - -/** - * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. - * - * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. - * @sample samples.comparisons.ComparableOps.coerceIn - */ -public fun Double.coerceIn(minimumValue: Double, maximumValue: Double): Double { - if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") - if (this < minimumValue) return minimumValue - if (this > maximumValue) return maximumValue - return this -} - -/** - * Ensures that this value lies in the specified [range]. - * - * @return this value if it's in the [range], or `range.start` if this value is less than `range.start`, or `range.endInclusive` if this value is greater than `range.endInclusive`. - * @sample samples.comparisons.ComparableOps.coerceInFloatingPointRange - */ -@SinceKotlin("1.1") -public fun > T.coerceIn(range: ClosedFloatingPointRange): T { - if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") - return when { - // this < start equiv to this <= start && !(this >= start) - range.lessThanOrEquals(this, range.start) && !range.lessThanOrEquals(range.start, this) -> range.start - // this > end equiv to this >= end && !(this <= end) - range.lessThanOrEquals(range.endInclusive, this) && !range.lessThanOrEquals(this, range.endInclusive) -> range.endInclusive - else -> this - } -} - -/** - * Ensures that this value lies in the specified [range]. - * - * @return this value if it's in the [range], or `range.start` if this value is less than `range.start`, or `range.endInclusive` if this value is greater than `range.endInclusive`. - * @sample samples.comparisons.ComparableOps.coerceInComparable - */ -public fun > T.coerceIn(range: ClosedRange): T { - if (range is ClosedFloatingPointRange) { - return this.coerceIn(range) - } - if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") - return when { - this < range.start -> range.start - this > range.endInclusive -> range.endInclusive - else -> this - } -} - -/** - * Ensures that this value lies in the specified [range]. - * - * @return this value if it's in the [range], or `range.start` if this value is less than `range.start`, or `range.endInclusive` if this value is greater than `range.endInclusive`. - * @sample samples.comparisons.ComparableOps.coerceIn - */ -public fun Int.coerceIn(range: ClosedRange): Int { - if (range is ClosedFloatingPointRange) { - return this.coerceIn(range) - } - if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") - return when { - this < range.start -> range.start - this > range.endInclusive -> range.endInclusive - else -> this - } -} - -/** - * Ensures that this value lies in the specified [range]. - * - * @return this value if it's in the [range], or `range.start` if this value is less than `range.start`, or `range.endInclusive` if this value is greater than `range.endInclusive`. - * @sample samples.comparisons.ComparableOps.coerceIn - */ -public fun Long.coerceIn(range: ClosedRange): Long { - if (range is ClosedFloatingPointRange) { - return this.coerceIn(range) - } - if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") - return when { - this < range.start -> range.start - this > range.endInclusive -> range.endInclusive - else -> this - } -} - diff --git a/libraries/stdlib/src/generated/_Sequences.kt b/libraries/stdlib/src/generated/_Sequences.kt deleted file mode 100644 index 678b15de9c1..00000000000 --- a/libraries/stdlib/src/generated/_Sequences.kt +++ /dev/null @@ -1,1890 +0,0 @@ -@file:kotlin.jvm.JvmMultifileClass -@file:kotlin.jvm.JvmName("SequencesKt") -@file:kotlin.jvm.JvmVersion - -package kotlin.sequences - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// - -import kotlin.* -import kotlin.text.* -import kotlin.comparisons.* -import kotlin.coroutines.experimental.* - -/** - * Returns `true` if [element] is found in the sequence. - * - * The operation is _terminal_. - */ -public operator fun <@kotlin.internal.OnlyInputTypes T> Sequence.contains(element: T): Boolean { - return indexOf(element) >= 0 -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this sequence. - * - * The operation is _terminal_. - */ -public fun Sequence.elementAt(index: Int): T { - return elementAtOrElse(index) { throw IndexOutOfBoundsException("Sequence doesn't contain element at index $index.") } -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this sequence. - * - * The operation is _terminal_. - */ -public fun Sequence.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T { - if (index < 0) - return defaultValue(index) - val iterator = iterator() - var count = 0 - while (iterator.hasNext()) { - val element = iterator.next() - if (index == count++) - return element - } - return defaultValue(index) -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this sequence. - * - * The operation is _terminal_. - */ -public fun Sequence.elementAtOrNull(index: Int): T? { - if (index < 0) - return null - val iterator = iterator() - var count = 0 - while (iterator.hasNext()) { - val element = iterator.next() - if (index == count++) - return element - } - return null -} - -/** - * Returns the first element matching the given [predicate], or `null` if no such element was found. - * - * The operation is _terminal_. - */ -@kotlin.internal.InlineOnly -public inline fun Sequence.find(predicate: (T) -> Boolean): T? { - return firstOrNull(predicate) -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - * - * The operation is _terminal_. - */ -@kotlin.internal.InlineOnly -public inline fun Sequence.findLast(predicate: (T) -> Boolean): T? { - return lastOrNull(predicate) -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the sequence is empty. - * - * The operation is _terminal_. - */ -public fun Sequence.first(): T { - val iterator = iterator() - if (!iterator.hasNext()) - throw NoSuchElementException("Sequence is empty.") - return iterator.next() -} - -/** - * Returns the first element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - * - * The operation is _terminal_. - */ -public inline fun Sequence.first(predicate: (T) -> Boolean): T { - for (element in this) if (predicate(element)) return element - throw NoSuchElementException("Sequence contains no element matching the predicate.") -} - -/** - * Returns the first element, or `null` if the sequence is empty. - * - * The operation is _terminal_. - */ -public fun Sequence.firstOrNull(): T? { - val iterator = iterator() - if (!iterator.hasNext()) - return null - return iterator.next() -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - * - * The operation is _terminal_. - */ -public inline fun Sequence.firstOrNull(predicate: (T) -> Boolean): T? { - for (element in this) if (predicate(element)) return element - return null -} - -/** - * Returns first index of [element], or -1 if the sequence does not contain element. - * - * The operation is _terminal_. - */ -public fun <@kotlin.internal.OnlyInputTypes T> Sequence.indexOf(element: T): Int { - var index = 0 - for (item in this) { - if (element == item) - return index - index++ - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the sequence does not contain such element. - * - * The operation is _terminal_. - */ -public inline fun Sequence.indexOfFirst(predicate: (T) -> Boolean): Int { - var index = 0 - for (item in this) { - if (predicate(item)) - return index - index++ - } - return -1 -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the sequence does not contain such element. - * - * The operation is _terminal_. - */ -public inline fun Sequence.indexOfLast(predicate: (T) -> Boolean): Int { - var lastIndex = -1 - var index = 0 - for (item in this) { - if (predicate(item)) - lastIndex = index - index++ - } - return lastIndex -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the sequence is empty. - * - * The operation is _terminal_. - */ -public fun Sequence.last(): T { - val iterator = iterator() - if (!iterator.hasNext()) - throw NoSuchElementException("Sequence is empty.") - var last = iterator.next() - while (iterator.hasNext()) - last = iterator.next() - return last -} - -/** - * Returns the last element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - * - * The operation is _terminal_. - */ -public inline fun Sequence.last(predicate: (T) -> Boolean): T { - var last: T? = null - var found = false - for (element in this) { - if (predicate(element)) { - last = element - found = true - } - } - if (!found) throw NoSuchElementException("Sequence contains no element matching the predicate.") - @Suppress("UNCHECKED_CAST") - return last as T -} - -/** - * Returns last index of [element], or -1 if the sequence does not contain element. - * - * The operation is _terminal_. - */ -public fun <@kotlin.internal.OnlyInputTypes T> Sequence.lastIndexOf(element: T): Int { - var lastIndex = -1 - var index = 0 - for (item in this) { - if (element == item) - lastIndex = index - index++ - } - return lastIndex -} - -/** - * Returns the last element, or `null` if the sequence is empty. - * - * The operation is _terminal_. - */ -public fun Sequence.lastOrNull(): T? { - val iterator = iterator() - if (!iterator.hasNext()) - return null - var last = iterator.next() - while (iterator.hasNext()) - last = iterator.next() - return last -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - * - * The operation is _terminal_. - */ -public inline fun Sequence.lastOrNull(predicate: (T) -> Boolean): T? { - var last: T? = null - for (element in this) { - if (predicate(element)) { - last = element - } - } - return last -} - -/** - * Returns the single element, or throws an exception if the sequence is empty or has more than one element. - * - * The operation is _terminal_. - */ -public fun Sequence.single(): T { - val iterator = iterator() - if (!iterator.hasNext()) - throw NoSuchElementException("Sequence is empty.") - val single = iterator.next() - if (iterator.hasNext()) - throw IllegalArgumentException("Sequence has more than one element.") - return single -} - -/** - * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. - * - * The operation is _terminal_. - */ -public inline fun Sequence.single(predicate: (T) -> Boolean): T { - var single: T? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Sequence contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Sequence contains no element matching the predicate.") - @Suppress("UNCHECKED_CAST") - return single as T -} - -/** - * Returns single element, or `null` if the sequence is empty or has more than one element. - * - * The operation is _terminal_. - */ -public fun Sequence.singleOrNull(): T? { - val iterator = iterator() - if (!iterator.hasNext()) - return null - val single = iterator.next() - if (iterator.hasNext()) - return null - return single -} - -/** - * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. - * - * The operation is _terminal_. - */ -public inline fun Sequence.singleOrNull(predicate: (T) -> Boolean): T? { - var single: T? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) return null - single = element - found = true - } - } - if (!found) return null - return single -} - -/** - * Returns a sequence containing all elements except first [n] elements. - * - * The operation is _intermediate_ and _stateless_. - */ -public fun Sequence.drop(n: Int): Sequence { - require(n >= 0) { "Requested element count $n is less than zero." } - return when { - n == 0 -> this - this is DropTakeSequence -> this.drop(n) - else -> DropSequence(this, n) - } -} - -/** - * Returns a sequence containing all elements except first elements that satisfy the given [predicate]. - * - * The operation is _intermediate_ and _stateless_. - */ -public fun Sequence.dropWhile(predicate: (T) -> Boolean): Sequence { - return DropWhileSequence(this, predicate) -} - -/** - * Returns a sequence containing only elements matching the given [predicate]. - * - * The operation is _intermediate_ and _stateless_. - */ -public fun Sequence.filter(predicate: (T) -> Boolean): Sequence { - return FilteringSequence(this, true, predicate) -} - -/** - * Returns a sequence containing only elements matching the given [predicate]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - * - * The operation is _intermediate_ and _stateless_. - */ -public fun Sequence.filterIndexed(predicate: (index: Int, T) -> Boolean): Sequence { - // TODO: Rewrite with generalized MapFilterIndexingSequence - return TransformingSequence(FilteringSequence(IndexingSequence(this), true, { predicate(it.index, it.value) }), { it.value }) -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - * @param [predicate] function that takes the index of an element and the element itself - * and returns the result of predicate evaluation on the element. - * - * The operation is _terminal_. - */ -public inline fun > Sequence.filterIndexedTo(destination: C, predicate: (index: Int, T) -> Boolean): C { - forEachIndexed { index, element -> - if (predicate(index, element)) destination.add(element) - } - return destination -} - -/** - * Returns a sequence containing all elements that are instances of specified type parameter R. - * - * The operation is _intermediate_ and _stateless_. - */ -public inline fun Sequence<*>.filterIsInstance(): Sequence<@kotlin.internal.NoInfer R> { - @Suppress("UNCHECKED_CAST") - return filter { it is R } as Sequence -} - -/** - * Returns a sequence containing all elements that are instances of specified class. - * - * The operation is _intermediate_ and _stateless_. - */ -public fun Sequence<*>.filterIsInstance(klass: Class): Sequence { - @Suppress("UNCHECKED_CAST") - return filter { klass.isInstance(it) } as Sequence -} - -/** - * Appends all elements that are instances of specified type parameter R to the given [destination]. - * - * The operation is _terminal_. - */ -public inline fun > Sequence<*>.filterIsInstanceTo(destination: C): C { - for (element in this) if (element is R) destination.add(element) - return destination -} - -/** - * Appends all elements that are instances of specified class to the given [destination]. - * - * The operation is _terminal_. - */ -public fun , R> Sequence<*>.filterIsInstanceTo(destination: C, klass: Class): C { - @Suppress("UNCHECKED_CAST") - for (element in this) if (klass.isInstance(element)) destination.add(element as R) - return destination -} - -/** - * Returns a sequence containing all elements not matching the given [predicate]. - * - * The operation is _intermediate_ and _stateless_. - */ -public fun Sequence.filterNot(predicate: (T) -> Boolean): Sequence { - return FilteringSequence(this, false, predicate) -} - -/** - * Returns a sequence containing all elements that are not `null`. - * - * The operation is _intermediate_ and _stateless_. - */ -public fun Sequence.filterNotNull(): Sequence { - @Suppress("UNCHECKED_CAST") - return filterNot { it == null } as Sequence -} - -/** - * Appends all elements that are not `null` to the given [destination]. - * - * The operation is _terminal_. - */ -public fun , T : Any> Sequence.filterNotNullTo(destination: C): C { - for (element in this) if (element != null) destination.add(element) - return destination -} - -/** - * Appends all elements not matching the given [predicate] to the given [destination]. - * - * The operation is _terminal_. - */ -public inline fun > Sequence.filterNotTo(destination: C, predicate: (T) -> Boolean): C { - for (element in this) if (!predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements matching the given [predicate] to the given [destination]. - * - * The operation is _terminal_. - */ -public inline fun > Sequence.filterTo(destination: C, predicate: (T) -> Boolean): C { - for (element in this) if (predicate(element)) destination.add(element) - return destination -} - -/** - * Returns a sequence containing first [n] elements. - * - * The operation is _intermediate_ and _stateless_. - */ -public fun Sequence.take(n: Int): Sequence { - require(n >= 0) { "Requested element count $n is less than zero." } - return when { - n == 0 -> emptySequence() - this is DropTakeSequence -> this.take(n) - else -> TakeSequence(this, n) - } -} - -/** - * Returns a sequence containing first elements satisfying the given [predicate]. - * - * The operation is _intermediate_ and _stateless_. - */ -public fun Sequence.takeWhile(predicate: (T) -> Boolean): Sequence { - return TakeWhileSequence(this, predicate) -} - -/** - * Returns a sequence that yields elements of this sequence sorted according to their natural sort order. - * - * The operation is _intermediate_ and _stateful_. - */ -public fun > Sequence.sorted(): Sequence { - return object : Sequence { - override fun iterator(): Iterator { - val sortedList = this@sorted.toMutableList() - sortedList.sort() - return sortedList.iterator() - } - } -} - -/** - * Returns a sequence that yields elements of this sequence sorted according to natural sort order of the value returned by specified [selector] function. - * - * The operation is _intermediate_ and _stateful_. - */ -public inline fun > Sequence.sortedBy(crossinline selector: (T) -> R?): Sequence { - return sortedWith(compareBy(selector)) -} - -/** - * Returns a sequence that yields elements of this sequence sorted descending according to natural sort order of the value returned by specified [selector] function. - * - * The operation is _intermediate_ and _stateful_. - */ -public inline fun > Sequence.sortedByDescending(crossinline selector: (T) -> R?): Sequence { - return sortedWith(compareByDescending(selector)) -} - -/** - * Returns a sequence that yields elements of this sequence sorted descending according to their natural sort order. - * - * The operation is _intermediate_ and _stateful_. - */ -public fun > Sequence.sortedDescending(): Sequence { - return sortedWith(reverseOrder()) -} - -/** - * Returns a sequence that yields elements of this sequence sorted according to the specified [comparator]. - * - * The operation is _intermediate_ and _stateful_. - */ -public fun Sequence.sortedWith(comparator: Comparator): Sequence { - return object : Sequence { - override fun iterator(): Iterator { - val sortedList = this@sortedWith.toMutableList() - sortedList.sortWith(comparator) - return sortedList.iterator() - } - } -} - -/** - * Returns a [Map] containing key-value pairs provided by [transform] function - * applied to elements of the given sequence. - * - * If any of two pairs would have the same key the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original sequence. - * - * The operation is _terminal_. - */ -public inline fun Sequence.associate(transform: (T) -> Pair): Map { - return associateTo(LinkedHashMap(), transform) -} - -/** - * Returns a [Map] containing the elements from the given sequence indexed by the key - * returned from [keySelector] function applied to each element. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original sequence. - * - * The operation is _terminal_. - */ -public inline fun Sequence.associateBy(keySelector: (T) -> K): Map { - return associateByTo(LinkedHashMap(), keySelector) -} - -/** - * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given sequence. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original sequence. - * - * The operation is _terminal_. - */ -public inline fun Sequence.associateBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map { - return associateByTo(LinkedHashMap(), keySelector, valueTransform) -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function applied to each element of the given sequence - * and value is the element itself. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The operation is _terminal_. - */ -public inline fun > Sequence.associateByTo(destination: M, keySelector: (T) -> K): M { - for (element in this) { - destination.put(keySelector(element), element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function and - * and value is provided by the [valueTransform] function applied to elements of the given sequence. - * - * If any two elements would have the same key returned by [keySelector] the last one gets added to the map. - * - * The operation is _terminal_. - */ -public inline fun > Sequence.associateByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M { - for (element in this) { - destination.put(keySelector(element), valueTransform(element)) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs - * provided by [transform] function applied to each element of the given sequence. - * - * If any of two pairs would have the same key the last one gets added to the map. - * - * The operation is _terminal_. - */ -public inline fun > Sequence.associateTo(destination: M, transform: (T) -> Pair): M { - for (element in this) { - destination += transform(element) - } - return destination -} - -/** - * Appends all elements to the given [destination] collection. - * - * The operation is _terminal_. - */ -public fun > Sequence.toCollection(destination: C): C { - for (item in this) { - destination.add(item) - } - return destination -} - -/** - * Returns a [HashSet] of all elements. - * - * The operation is _terminal_. - */ -public fun Sequence.toHashSet(): HashSet { - return toCollection(HashSet()) -} - -/** - * Returns a [List] containing all elements. - * - * The operation is _terminal_. - */ -public fun Sequence.toList(): List { - return this.toMutableList().optimizeReadOnlyList() -} - -/** - * Returns a [MutableList] filled with all elements of this sequence. - * - * The operation is _terminal_. - */ -public fun Sequence.toMutableList(): MutableList { - return toCollection(ArrayList()) -} - -/** - * Returns a [Set] of all elements. - * - * The returned set preserves the element iteration order of the original sequence. - * - * The operation is _terminal_. - */ -public fun Sequence.toSet(): Set { - return toCollection(LinkedHashSet()).optimizeReadOnlySet() -} - -/** - * Returns a [SortedSet][java.util.SortedSet] of all elements. - * - * The operation is _terminal_. - */ -public fun > Sequence.toSortedSet(): java.util.SortedSet { - return toCollection(java.util.TreeSet()) -} - -/** - * Returns a [SortedSet][java.util.SortedSet] of all elements. - * - * Elements in the set returned are sorted according to the given [comparator]. - * - * The operation is _terminal_. - */ -public fun Sequence.toSortedSet(comparator: Comparator): java.util.SortedSet { - return toCollection(java.util.TreeSet(comparator)) -} - -/** - * Returns a single sequence of all elements from results of [transform] function being invoked on each element of original sequence. - * - * The operation is _intermediate_ and _stateless_. - */ -public fun Sequence.flatMap(transform: (T) -> Sequence): Sequence { - return FlatteningSequence(this, transform, { it.iterator() }) -} - -/** - * Appends all elements yielded from results of [transform] function being invoked on each element of original sequence, to the given [destination]. - * - * The operation is _terminal_. - */ -public inline fun > Sequence.flatMapTo(destination: C, transform: (T) -> Sequence): C { - for (element in this) { - val list = transform(element) - destination.addAll(list) - } - return destination -} - -/** - * Groups elements of the original sequence by the key returned by the given [keySelector] function - * applied to each element and returns a map where each group key is associated with a list of corresponding elements. - * - * The returned map preserves the entry iteration order of the keys produced from the original sequence. - * - * @sample samples.collections.Collections.Transformations.groupBy - * - * The operation is _terminal_. - */ -public inline fun Sequence.groupBy(keySelector: (T) -> K): Map> { - return groupByTo(LinkedHashMap>(), keySelector) -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original sequence - * by the key returned by the given [keySelector] function applied to the element - * and returns a map where each group key is associated with a list of corresponding values. - * - * The returned map preserves the entry iteration order of the keys produced from the original sequence. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - * - * The operation is _terminal_. - */ -public inline fun Sequence.groupBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map> { - return groupByTo(LinkedHashMap>(), keySelector, valueTransform) -} - -/** - * Groups elements of the original sequence by the key returned by the given [keySelector] function - * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupBy - * - * The operation is _terminal_. - */ -public inline fun >> Sequence.groupByTo(destination: M, keySelector: (T) -> K): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(element) - } - return destination -} - -/** - * Groups values returned by the [valueTransform] function applied to each element of the original sequence - * by the key returned by the given [keySelector] function applied to the element - * and puts to the [destination] map each group key associated with a list of corresponding values. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - * - * The operation is _terminal_. - */ -public inline fun >> Sequence.groupByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(valueTransform(element)) - } - return destination -} - -/** - * Creates a [Grouping] source from a sequence to be used later with one of group-and-fold operations - * using the specified [keySelector] function to extract a key from each element. - * - * @sample samples.collections.Collections.Transformations.groupingByEachCount - * - * The operation is _intermediate_ and _stateless_. - */ -@SinceKotlin("1.1") -public inline fun Sequence.groupingBy(crossinline keySelector: (T) -> K): Grouping { - return object : Grouping { - override fun sourceIterator(): Iterator = this@groupingBy.iterator() - override fun keyOf(element: T): K = keySelector(element) - } -} - -/** - * Returns a sequence containing the results of applying the given [transform] function - * to each element in the original sequence. - * - * The operation is _intermediate_ and _stateless_. - */ -public fun Sequence.map(transform: (T) -> R): Sequence { - return TransformingSequence(this, transform) -} - -/** - * Returns a sequence containing the results of applying the given [transform] function - * to each element and its index in the original sequence. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - * - * The operation is _intermediate_ and _stateless_. - */ -public fun Sequence.mapIndexed(transform: (index: Int, T) -> R): Sequence { - return TransformingIndexedSequence(this, transform) -} - -/** - * Returns a sequence containing only the non-null results of applying the given [transform] function - * to each element and its index in the original sequence. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - * - * The operation is _intermediate_ and _stateless_. - */ -public fun Sequence.mapIndexedNotNull(transform: (index: Int, T) -> R?): Sequence { - return TransformingIndexedSequence(this, transform).filterNotNull() -} - -/** - * Applies the given [transform] function to each element and its index in the original sequence - * and appends only the non-null results to the given [destination]. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - * - * The operation is _terminal_. - */ -public inline fun > Sequence.mapIndexedNotNullTo(destination: C, transform: (index: Int, T) -> R?): C { - forEachIndexed { index, element -> transform(index, element)?.let { destination.add(it) } } - return destination -} - -/** - * Applies the given [transform] function to each element and its index in the original sequence - * and appends the results to the given [destination]. - * @param [transform] function that takes the index of an element and the element itself - * and returns the result of the transform applied to the element. - * - * The operation is _terminal_. - */ -public inline fun > Sequence.mapIndexedTo(destination: C, transform: (index: Int, T) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Returns a sequence containing only the non-null results of applying the given [transform] function - * to each element in the original sequence. - * - * The operation is _intermediate_ and _stateless_. - */ -public fun Sequence.mapNotNull(transform: (T) -> R?): Sequence { - return TransformingSequence(this, transform).filterNotNull() -} - -/** - * Applies the given [transform] function to each element in the original sequence - * and appends only the non-null results to the given [destination]. - * - * The operation is _terminal_. - */ -public inline fun > Sequence.mapNotNullTo(destination: C, transform: (T) -> R?): C { - forEach { element -> transform(element)?.let { destination.add(it) } } - return destination -} - -/** - * Applies the given [transform] function to each element of the original sequence - * and appends the results to the given [destination]. - * - * The operation is _terminal_. - */ -public inline fun > Sequence.mapTo(destination: C, transform: (T) -> R): C { - for (item in this) - destination.add(transform(item)) - return destination -} - -/** - * Returns a sequence of [IndexedValue] for each element of the original sequence. - * - * The operation is _intermediate_ and _stateless_. - */ -public fun Sequence.withIndex(): Sequence> { - return IndexingSequence(this) -} - -/** - * Returns a sequence containing only distinct elements from the given sequence. - * - * The elements in the resulting sequence are in the same order as they were in the source sequence. - * - * The operation is _intermediate_ and _stateful_. - */ -public fun Sequence.distinct(): Sequence { - return this.distinctBy { it } -} - -/** - * Returns a sequence containing only elements from the given sequence - * having distinct keys returned by the given [selector] function. - * - * The elements in the resulting sequence are in the same order as they were in the source sequence. - * - * The operation is _intermediate_ and _stateful_. - */ -public fun Sequence.distinctBy(selector: (T) -> K): Sequence { - return DistinctSequence(this, selector) -} - -/** - * Returns a mutable set containing all distinct elements from the given sequence. - * - * The returned set preserves the element iteration order of the original sequence. - * - * The operation is _terminal_. - */ -public fun Sequence.toMutableSet(): MutableSet { - val set = LinkedHashSet() - for (item in this) set.add(item) - return set -} - -/** - * Returns `true` if all elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.all - * - * The operation is _terminal_. - */ -public inline fun Sequence.all(predicate: (T) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if sequence has at least one element. - * - * @sample samples.collections.Collections.Aggregates.any - * - * The operation is _terminal_. - */ -public fun Sequence.any(): Boolean { - return iterator().hasNext() -} - -/** - * Returns `true` if at least one element matches the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.anyWithPredicate - * - * The operation is _terminal_. - */ -public inline fun Sequence.any(predicate: (T) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return true - return false -} - -/** - * Returns the number of elements in this sequence. - * - * The operation is _terminal_. - */ -public fun Sequence.count(): Int { - var count = 0 - for (element in this) count++ - return count -} - -/** - * Returns the number of elements matching the given [predicate]. - * - * The operation is _terminal_. - */ -public inline fun Sequence.count(predicate: (T) -> Boolean): Int { - var count = 0 - for (element in this) if (predicate(element)) count++ - return count -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. - * - * The operation is _terminal_. - */ -public inline fun Sequence.fold(initial: R, operation: (acc: R, T) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right - * to current accumulator value and each element with its index in the original sequence. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself, and calculates the next accumulator value. - * - * The operation is _terminal_. - */ -public inline fun Sequence.foldIndexed(initial: R, operation: (index: Int, acc: R, T) -> R): R { - var index = 0 - var accumulator = initial - for (element in this) accumulator = operation(index++, accumulator, element) - return accumulator -} - -/** - * Performs the given [action] on each element. - * - * The operation is _terminal_. - */ -public inline fun Sequence.forEach(action: (T) -> Unit): Unit { - for (element in this) action(element) -} - -/** - * Performs the given [action] on each element, providing sequential index with the element. - * @param [action] function that takes the index of an element and the element itself - * and performs the desired action on the element. - * - * The operation is _terminal_. - */ -public inline fun Sequence.forEachIndexed(action: (index: Int, T) -> Unit): Unit { - var index = 0 - for (item in this) action(index++, item) -} - -/** - * Returns the largest element or `null` if there are no elements. - * - * If any of elements is `NaN` returns `NaN`. - * - * The operation is _terminal_. - */ -@SinceKotlin("1.1") -public fun Sequence.max(): Double? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var max = iterator.next() - if (max.isNaN()) return max - while (iterator.hasNext()) { - val e = iterator.next() - if (e.isNaN()) return e - if (max < e) max = e - } - return max -} - -/** - * Returns the largest element or `null` if there are no elements. - * - * If any of elements is `NaN` returns `NaN`. - * - * The operation is _terminal_. - */ -@SinceKotlin("1.1") -public fun Sequence.max(): Float? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var max = iterator.next() - if (max.isNaN()) return max - while (iterator.hasNext()) { - val e = iterator.next() - if (e.isNaN()) return e - if (max < e) max = e - } - return max -} - -/** - * Returns the largest element or `null` if there are no elements. - * - * The operation is _terminal_. - */ -public fun > Sequence.max(): T? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var max = iterator.next() - while (iterator.hasNext()) { - val e = iterator.next() - if (max < e) max = e - } - return max -} - -/** - * Returns the first element yielding the largest value of the given function or `null` if there are no elements. - * - * The operation is _terminal_. - */ -public inline fun > Sequence.maxBy(selector: (T) -> R): T? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var maxElem = iterator.next() - var maxValue = selector(maxElem) - while (iterator.hasNext()) { - val e = iterator.next() - val v = selector(e) - if (maxValue < v) { - maxElem = e - maxValue = v - } - } - return maxElem -} - -/** - * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. - * - * The operation is _terminal_. - */ -public fun Sequence.maxWith(comparator: Comparator): T? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var max = iterator.next() - while (iterator.hasNext()) { - val e = iterator.next() - if (comparator.compare(max, e) < 0) max = e - } - return max -} - -/** - * Returns the smallest element or `null` if there are no elements. - * - * If any of elements is `NaN` returns `NaN`. - * - * The operation is _terminal_. - */ -@SinceKotlin("1.1") -public fun Sequence.min(): Double? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var min = iterator.next() - if (min.isNaN()) return min - while (iterator.hasNext()) { - val e = iterator.next() - if (e.isNaN()) return e - if (min > e) min = e - } - return min -} - -/** - * Returns the smallest element or `null` if there are no elements. - * - * If any of elements is `NaN` returns `NaN`. - * - * The operation is _terminal_. - */ -@SinceKotlin("1.1") -public fun Sequence.min(): Float? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var min = iterator.next() - if (min.isNaN()) return min - while (iterator.hasNext()) { - val e = iterator.next() - if (e.isNaN()) return e - if (min > e) min = e - } - return min -} - -/** - * Returns the smallest element or `null` if there are no elements. - * - * The operation is _terminal_. - */ -public fun > Sequence.min(): T? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var min = iterator.next() - while (iterator.hasNext()) { - val e = iterator.next() - if (min > e) min = e - } - return min -} - -/** - * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. - * - * The operation is _terminal_. - */ -public inline fun > Sequence.minBy(selector: (T) -> R): T? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var minElem = iterator.next() - var minValue = selector(minElem) - while (iterator.hasNext()) { - val e = iterator.next() - val v = selector(e) - if (minValue > v) { - minElem = e - minValue = v - } - } - return minElem -} - -/** - * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. - * - * The operation is _terminal_. - */ -public fun Sequence.minWith(comparator: Comparator): T? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var min = iterator.next() - while (iterator.hasNext()) { - val e = iterator.next() - if (comparator.compare(min, e) > 0) min = e - } - return min -} - -/** - * Returns `true` if the sequence has no elements. - * - * @sample samples.collections.Collections.Aggregates.none - * - * The operation is _terminal_. - */ -public fun Sequence.none(): Boolean { - return !iterator().hasNext() -} - -/** - * Returns `true` if no elements match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.noneWithPredicate - * - * The operation is _terminal_. - */ -public inline fun Sequence.none(predicate: (T) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return false - return true -} - -/** - * Returns a sequence which performs the given [action] on each element of the original sequence as they pass through it. - * - * The operation is _intermediate_ and _stateless_. - */ -@SinceKotlin("1.1") -public fun Sequence.onEach(action: (T) -> Unit): Sequence { - return map { - action(it) - it - } -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. - * - * The operation is _terminal_. - */ -public inline fun Sequence.reduce(operation: (acc: S, T) -> S): S { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty sequence can't be reduced.") - var accumulator: S = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(accumulator, iterator.next()) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right - * to current accumulator value and each element with its index in the original sequence. - * @param [operation] function that takes the index of an element, current accumulator value - * and the element itself and calculates the next accumulator value. - * - * The operation is _terminal_. - */ -public inline fun Sequence.reduceIndexed(operation: (index: Int, acc: S, T) -> S): S { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty sequence can't be reduced.") - var index = 1 - var accumulator: S = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(index++, accumulator, iterator.next()) - } - return accumulator -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the sequence. - * - * The operation is _terminal_. - */ -public inline fun Sequence.sumBy(selector: (T) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each element in the sequence. - * - * The operation is _terminal_. - */ -public inline fun Sequence.sumByDouble(selector: (T) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns an original collection containing all the non-`null` elements, throwing an [IllegalArgumentException] if there are any `null` elements. - * - * The operation is _intermediate_ and _stateless_. - */ -public fun Sequence.requireNoNulls(): Sequence { - return map { it ?: throw IllegalArgumentException("null element found in $this.") } -} - -/** - * Splits this sequence into a sequence of lists each not exceeding the given [size]. - * - * The last list in the resulting sequence may have less elements than the given [size]. - * - * @param size the number of elements to take in each list, must be positive and can be greater than the number of elements in this sequence. - * - * @sample samples.collections.Collections.Transformations.chunked - * - * The operation is _intermediate_ and _stateful_. - */ -@SinceKotlin("1.2") -public fun Sequence.chunked(size: Int): Sequence> { - return windowed(size, size, partialWindows = true) -} - -/** - * Splits this sequence into several lists each not exceeding the given [size] - * and applies the given [transform] function to an each. - * - * @return sequence of results of the [transform] applied to an each list. - * - * Note that the list passed to the [transform] function is ephemeral and is valid only inside that function. - * You should not store it or allow it to escape in some way, unless you made a snapshot of it. - * The last list may have less elements than the given [size]. - * - * @param size the number of elements to take in each list, must be positive and can be greater than the number of elements in this sequence. - * - * @sample samples.text.Strings.chunkedTransform - * - * The operation is _intermediate_ and _stateful_. - */ -@SinceKotlin("1.2") -public fun Sequence.chunked(size: Int, transform: (List) -> R): Sequence { - return windowed(size, size, partialWindows = true, transform = transform) -} - -/** - * Returns a sequence containing all elements of the original sequence without the first occurrence of the given [element]. - * - * The operation is _intermediate_ and _stateless_. - */ -public operator fun Sequence.minus(element: T): Sequence { - return object: Sequence { - override fun iterator(): Iterator { - var removed = false - return this@minus.filter { if (!removed && it == element) { removed = true; false } else true }.iterator() - } - } -} - -/** - * Returns a sequence containing all elements of original sequence except the elements contained in the given [elements] array. - * - * Note that the source sequence and the array being subtracted are iterated only when an `iterator` is requested from - * the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. - * - * The operation is _intermediate_ and _stateful_. - */ -public operator fun Sequence.minus(elements: Array): Sequence { - if (elements.isEmpty()) return this - return object: Sequence { - override fun iterator(): Iterator { - val other = elements.toHashSet() - return this@minus.filterNot { it in other }.iterator() - } - } -} - -/** - * Returns a sequence containing all elements of original sequence except the elements contained in the given [elements] collection. - * - * Note that the source sequence and the collection being subtracted are iterated only when an `iterator` is requested from - * the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. - * - * The operation is _intermediate_ and _stateful_. - */ -public operator fun Sequence.minus(elements: Iterable): Sequence { - return object: Sequence { - override fun iterator(): Iterator { - val other = elements.convertToSetForSetOperation() - if (other.isEmpty()) - return this@minus.iterator() - else - return this@minus.filterNot { it in other }.iterator() - } - } -} - -/** - * Returns a sequence containing all elements of original sequence except the elements contained in the given [elements] sequence. - * - * Note that the source sequence and the sequence being subtracted are iterated only when an `iterator` is requested from - * the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. - * - * The operation is _intermediate_ for this sequence and _terminal_ and _stateful_ for the [elements] sequence. - */ -public operator fun Sequence.minus(elements: Sequence): Sequence { - return object: Sequence { - override fun iterator(): Iterator { - val other = elements.toHashSet() - if (other.isEmpty()) - return this@minus.iterator() - else - return this@minus.filterNot { it in other }.iterator() - } - } -} - -/** - * Returns a sequence containing all elements of the original sequence without the first occurrence of the given [element]. - * - * The operation is _intermediate_ and _stateless_. - */ -@kotlin.internal.InlineOnly -public inline fun Sequence.minusElement(element: T): Sequence { - return minus(element) -} - -/** - * Splits the original sequence into pair of lists, - * where *first* list contains elements for which [predicate] yielded `true`, - * while *second* list contains elements for which [predicate] yielded `false`. - * - * The operation is _terminal_. - */ -public inline fun Sequence.partition(predicate: (T) -> Boolean): Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Returns a sequence containing all elements of the original sequence and then the given [element]. - * - * The operation is _intermediate_ and _stateless_. - */ -public operator fun Sequence.plus(element: T): Sequence { - return sequenceOf(this, sequenceOf(element)).flatten() -} - -/** - * Returns a sequence containing all elements of original sequence and then all elements of the given [elements] array. - * - * Note that the source sequence and the array being added are iterated only when an `iterator` is requested from - * the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. - * - * The operation is _intermediate_ and _stateless_. - */ -public operator fun Sequence.plus(elements: Array): Sequence { - return this.plus(elements.asList()) -} - -/** - * Returns a sequence containing all elements of original sequence and then all elements of the given [elements] collection. - * - * Note that the source sequence and the collection being added are iterated only when an `iterator` is requested from - * the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. - * - * The operation is _intermediate_ and _stateless_. - */ -public operator fun Sequence.plus(elements: Iterable): Sequence { - return sequenceOf(this, elements.asSequence()).flatten() -} - -/** - * Returns a sequence containing all elements of original sequence and then all elements of the given [elements] sequence. - * - * Note that the source sequence and the sequence being added are iterated only when an `iterator` is requested from - * the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. - * - * The operation is _intermediate_ and _stateless_. - */ -public operator fun Sequence.plus(elements: Sequence): Sequence { - return sequenceOf(this, elements).flatten() -} - -/** - * Returns a sequence containing all elements of the original sequence and then the given [element]. - * - * The operation is _intermediate_ and _stateless_. - */ -@kotlin.internal.InlineOnly -public inline fun Sequence.plusElement(element: T): Sequence { - return plus(element) -} - -/** - * Returns a sequence of snapshots of the window of the given [size] - * sliding along this sequence with the given [step], where each - * snapshot is a list. - * - * Several last lists may have less elements than the given [size]. - * - * Both [size] and [step] must be positive and can be greater than the number of elements in this sequence. - * @param size the number of elements to take in each window - * @param step the number of elements to move the window forward by on an each step, by default 1 - * @param partialWindows controls whether or not to keep partial windows in the end if any, - * by default `false` which means partial windows won't be preserved - * - * @sample samples.collections.Sequences.Transformations.takeWindows - */ -@SinceKotlin("1.2") -public fun Sequence.windowed(size: Int, step: Int = 1, partialWindows: Boolean = false): Sequence> { - return windowedSequence(size, step, partialWindows, reuseBuffer = false) -} - -/** - * Returns a sequence of results of applying the given [transform] function to - * an each list representing a view over the window of the given [size] - * sliding along this sequence with the given [step]. - * - * Note that the list passed to the [transform] function is ephemeral and is valid only inside that function. - * You should not store it or allow it to escape in some way, unless you made a snapshot of it. - * Several last lists may have less elements than the given [size]. - * - * Both [size] and [step] must be positive and can be greater than the number of elements in this sequence. - * @param size the number of elements to take in each window - * @param step the number of elements to move the window forward by on an each step, by default 1 - * @param partialWindows controls whether or not to keep partial windows in the end if any, - * by default `false` which means partial windows won't be preserved - * - * @sample samples.collections.Sequences.Transformations.averageWindows - */ -@SinceKotlin("1.2") -public fun Sequence.windowed(size: Int, step: Int = 1, partialWindows: Boolean = false, transform: (List) -> R): Sequence { - return windowedSequence(size, step, partialWindows, reuseBuffer = true).map(transform) -} - -/** - * Returns a sequence of pairs built from elements of both sequences with same indexes. - * Resulting sequence has length of shortest input sequence. - * - * The operation is _intermediate_ and _stateless_. - */ -public infix fun Sequence.zip(other: Sequence): Sequence> { - return MergingSequence(this, other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a sequence of values built from elements of both collections with same indexes using provided [transform]. Resulting sequence has length of shortest input sequences. - * - * The operation is _intermediate_ and _stateless_. - */ -public fun Sequence.zip(other: Sequence, transform: (a: T, b: R) -> V): Sequence { - return MergingSequence(this, other, transform) -} - -/** - * Returns a sequence of pairs of each two adjacent elements in this sequence. - * - * The returned sequence is empty if this sequence contains less than two elements. - * - * @sample samples.collections.Collections.Transformations.zipWithNext - * - * The operation is _intermediate_ and _stateless_. - */ -@SinceKotlin("1.2") -public fun Sequence.zipWithNext(): Sequence> { - return zipWithNext { a, b -> a to b } -} - -/** - * Returns a sequence containing the results of applying the given [transform] function - * to an each pair of two adjacent elements in this sequence. - * - * The returned sequence is empty if this sequence contains less than two elements. - * - * @sample samples.collections.Collections.Transformations.zipWithNextToFindDeltas - * - * The operation is _intermediate_ and _stateless_. - */ -@SinceKotlin("1.2") -public fun Sequence.zipWithNext(transform: (a: T, b: T) -> R): Sequence { - return buildSequence result@ { - val iterator = iterator() - if (!iterator.hasNext()) return@result - var current = iterator.next() - while (iterator.hasNext()) { - val next = iterator.next() - yield(transform(current, next)) - current = next - } - } -} - -/** - * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinTo - * - * The operation is _terminal_. - */ -public fun Sequence.joinTo(buffer: A, separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((T) -> CharSequence)? = null): A { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - buffer.appendElement(element, transform) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) - return buffer -} - -/** - * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). - * - * @sample samples.collections.Collections.Transformations.joinToString - * - * The operation is _terminal_. - */ -public fun Sequence.joinToString(separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((T) -> CharSequence)? = null): String { - return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() -} - -/** - * Creates an [Iterable] instance that wraps the original sequence returning its elements when being iterated. - */ -public fun Sequence.asIterable(): Iterable { - return Iterable { this.iterator() } -} - -/** - * Returns this sequence as a [Sequence]. - */ -@kotlin.internal.InlineOnly -public inline fun Sequence.asSequence(): Sequence { - return this -} - -/** - * Returns an average value of elements in the sequence. - * - * The operation is _terminal_. - */ -@kotlin.jvm.JvmName("averageOfByte") -public fun Sequence.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the sequence. - * - * The operation is _terminal_. - */ -@kotlin.jvm.JvmName("averageOfShort") -public fun Sequence.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the sequence. - * - * The operation is _terminal_. - */ -@kotlin.jvm.JvmName("averageOfInt") -public fun Sequence.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the sequence. - * - * The operation is _terminal_. - */ -@kotlin.jvm.JvmName("averageOfLong") -public fun Sequence.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the sequence. - * - * The operation is _terminal_. - */ -@kotlin.jvm.JvmName("averageOfFloat") -public fun Sequence.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns an average value of elements in the sequence. - * - * The operation is _terminal_. - */ -@kotlin.jvm.JvmName("averageOfDouble") -public fun Sequence.average(): Double { - var sum: Double = 0.0 - var count: Int = 0 - for (element in this) { - sum += element - count += 1 - } - return if (count == 0) Double.NaN else sum / count -} - -/** - * Returns the sum of all elements in the sequence. - * - * The operation is _terminal_. - */ -@kotlin.jvm.JvmName("sumOfByte") -public fun Sequence.sum(): Int { - var sum: Int = 0 - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the sequence. - * - * The operation is _terminal_. - */ -@kotlin.jvm.JvmName("sumOfShort") -public fun Sequence.sum(): Int { - var sum: Int = 0 - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the sequence. - * - * The operation is _terminal_. - */ -@kotlin.jvm.JvmName("sumOfInt") -public fun Sequence.sum(): Int { - var sum: Int = 0 - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the sequence. - * - * The operation is _terminal_. - */ -@kotlin.jvm.JvmName("sumOfLong") -public fun Sequence.sum(): Long { - var sum: Long = 0L - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the sequence. - * - * The operation is _terminal_. - */ -@kotlin.jvm.JvmName("sumOfFloat") -public fun Sequence.sum(): Float { - var sum: Float = 0.0f - for (element in this) { - sum += element - } - return sum -} - -/** - * Returns the sum of all elements in the sequence. - * - * The operation is _terminal_. - */ -@kotlin.jvm.JvmName("sumOfDouble") -public fun Sequence.sum(): Double { - var sum: Double = 0.0 - for (element in this) { - sum += element - } - return sum -} - diff --git a/libraries/stdlib/src/generated/_Sets.kt b/libraries/stdlib/src/generated/_Sets.kt deleted file mode 100644 index 5540dac8616..00000000000 --- a/libraries/stdlib/src/generated/_Sets.kt +++ /dev/null @@ -1,134 +0,0 @@ -@file:kotlin.jvm.JvmMultifileClass -@file:kotlin.jvm.JvmName("SetsKt") -@file:kotlin.jvm.JvmVersion - -package kotlin.collections - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// - -import kotlin.* -import kotlin.text.* -import kotlin.comparisons.* - -/** - * Returns a set containing all elements of the original set except the given [element]. - * - * The returned set preserves the element iteration order of the original set. - */ -public operator fun Set.minus(element: T): Set { - val result = LinkedHashSet(mapCapacity(size)) - var removed = false - return this.filterTo(result) { if (!removed && it == element) { removed = true; false } else true } -} - -/** - * Returns a set containing all elements of the original set except the elements contained in the given [elements] array. - * - * The returned set preserves the element iteration order of the original set. - */ -public operator fun Set.minus(elements: Array): Set { - val result = LinkedHashSet(this) - result.removeAll(elements) - return result -} - -/** - * Returns a set containing all elements of the original set except the elements contained in the given [elements] collection. - * - * The returned set preserves the element iteration order of the original set. - */ -public operator fun Set.minus(elements: Iterable): Set { - val other = elements.convertToSetForSetOperationWith(this) - if (other.isEmpty()) - return this.toSet() - if (other is Set) - return this.filterNotTo(LinkedHashSet()) { it in other } - val result = LinkedHashSet(this) - result.removeAll(other) - return result -} - -/** - * Returns a set containing all elements of the original set except the elements contained in the given [elements] sequence. - * - * The returned set preserves the element iteration order of the original set. - */ -public operator fun Set.minus(elements: Sequence): Set { - val result = LinkedHashSet(this) - result.removeAll(elements) - return result -} - -/** - * Returns a set containing all elements of the original set except the given [element]. - * - * The returned set preserves the element iteration order of the original set. - */ -@kotlin.internal.InlineOnly -public inline fun Set.minusElement(element: T): Set { - return minus(element) -} - -/** - * Returns a set containing all elements of the original set and then the given [element] if it isn't already in this set. - * - * The returned set preserves the element iteration order of the original set. - */ -public operator fun Set.plus(element: T): Set { - val result = LinkedHashSet(mapCapacity(size + 1)) - result.addAll(this) - result.add(element) - return result -} - -/** - * Returns a set containing all elements of the original set and the given [elements] array, - * which aren't already in this set. - * - * The returned set preserves the element iteration order of the original set. - */ -public operator fun Set.plus(elements: Array): Set { - val result = LinkedHashSet(mapCapacity(this.size + elements.size)) - result.addAll(this) - result.addAll(elements) - return result -} - -/** - * Returns a set containing all elements of the original set and the given [elements] collection, - * which aren't already in this set. - * The returned set preserves the element iteration order of the original set. - */ -public operator fun Set.plus(elements: Iterable): Set { - val result = LinkedHashSet(mapCapacity(elements.collectionSizeOrNull()?.let { this.size + it } ?: this.size * 2)) - result.addAll(this) - result.addAll(elements) - return result -} - -/** - * Returns a set containing all elements of the original set and the given [elements] sequence, - * which aren't already in this set. - * - * The returned set preserves the element iteration order of the original set. - */ -public operator fun Set.plus(elements: Sequence): Set { - val result = LinkedHashSet(mapCapacity(this.size * 2)) - result.addAll(this) - result.addAll(elements) - return result -} - -/** - * Returns a set containing all elements of the original set and then the given [element] if it isn't already in this set. - * - * The returned set preserves the element iteration order of the original set. - */ -@kotlin.internal.InlineOnly -public inline fun Set.plusElement(element: T): Set { - return plus(element) -} - diff --git a/libraries/stdlib/src/generated/_Strings.kt b/libraries/stdlib/src/generated/_Strings.kt deleted file mode 100644 index 28baa196b49..00000000000 --- a/libraries/stdlib/src/generated/_Strings.kt +++ /dev/null @@ -1,1387 +0,0 @@ -@file:kotlin.jvm.JvmMultifileClass -@file:kotlin.jvm.JvmName("StringsKt") -@file:kotlin.jvm.JvmVersion - -package kotlin.text - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// - -import kotlin.* -import kotlin.text.* -import kotlin.comparisons.* - -/** - * Returns a character at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this char sequence. - */ -@kotlin.internal.InlineOnly -public inline fun CharSequence.elementAt(index: Int): Char { - return get(index) -} - -/** - * Returns a character at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this char sequence. - */ -@kotlin.internal.InlineOnly -public inline fun CharSequence.elementAtOrElse(index: Int, defaultValue: (Int) -> Char): Char { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns a character at the given [index] or `null` if the [index] is out of bounds of this char sequence. - */ -@kotlin.internal.InlineOnly -public inline fun CharSequence.elementAtOrNull(index: Int): Char? { - return this.getOrNull(index) -} - -/** - * Returns the first character matching the given [predicate], or `null` if no such character was found. - */ -@kotlin.internal.InlineOnly -public inline fun CharSequence.find(predicate: (Char) -> Boolean): Char? { - return firstOrNull(predicate) -} - -/** - * Returns the last character matching the given [predicate], or `null` if no such character was found. - */ -@kotlin.internal.InlineOnly -public inline fun CharSequence.findLast(predicate: (Char) -> Boolean): Char? { - return lastOrNull(predicate) -} - -/** - * Returns first character. - * @throws [NoSuchElementException] if the char sequence is empty. - */ -public fun CharSequence.first(): Char { - if (isEmpty()) - throw NoSuchElementException("Char sequence is empty.") - return this[0] -} - -/** - * Returns the first character matching the given [predicate]. - * @throws [NoSuchElementException] if no such character is found. - */ -public inline fun CharSequence.first(predicate: (Char) -> Boolean): Char { - for (element in this) if (predicate(element)) return element - throw NoSuchElementException("Char sequence contains no character matching the predicate.") -} - -/** - * Returns the first character, or `null` if the char sequence is empty. - */ -public fun CharSequence.firstOrNull(): Char? { - return if (isEmpty()) null else this[0] -} - -/** - * Returns the first character matching the given [predicate], or `null` if character was not found. - */ -public inline fun CharSequence.firstOrNull(predicate: (Char) -> Boolean): Char? { - for (element in this) if (predicate(element)) return element - return null -} - -/** - * Returns a character at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this char sequence. - */ -@kotlin.internal.InlineOnly -public inline fun CharSequence.getOrElse(index: Int, defaultValue: (Int) -> Char): Char { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns a character at the given [index] or `null` if the [index] is out of bounds of this char sequence. - */ -public fun CharSequence.getOrNull(index: Int): Char? { - return if (index >= 0 && index <= lastIndex) get(index) else null -} - -/** - * Returns index of the first character matching the given [predicate], or -1 if the char sequence does not contain such character. - */ -public inline fun CharSequence.indexOfFirst(predicate: (Char) -> Boolean): Int { - for (index in indices) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the last character matching the given [predicate], or -1 if the char sequence does not contain such character. - */ -public inline fun CharSequence.indexOfLast(predicate: (Char) -> Boolean): Int { - for (index in indices.reversed()) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns the last character. - * @throws [NoSuchElementException] if the char sequence is empty. - */ -public fun CharSequence.last(): Char { - if (isEmpty()) - throw NoSuchElementException("Char sequence is empty.") - return this[lastIndex] -} - -/** - * Returns the last character matching the given [predicate]. - * @throws [NoSuchElementException] if no such character is found. - */ -public inline fun CharSequence.last(predicate: (Char) -> Boolean): Char { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - throw NoSuchElementException("Char sequence contains no character matching the predicate.") -} - -/** - * Returns the last character, or `null` if the char sequence is empty. - */ -public fun CharSequence.lastOrNull(): Char? { - return if (isEmpty()) null else this[length - 1] -} - -/** - * Returns the last character matching the given [predicate], or `null` if no such character was found. - */ -public inline fun CharSequence.lastOrNull(predicate: (Char) -> Boolean): Char? { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - return null -} - -/** - * Returns the single character, or throws an exception if the char sequence is empty or has more than one character. - */ -public fun CharSequence.single(): Char { - return when (length) { - 0 -> throw NoSuchElementException("Char sequence is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Char sequence has more than one element.") - } -} - -/** - * Returns the single character matching the given [predicate], or throws exception if there is no or more than one matching character. - */ -public inline fun CharSequence.single(predicate: (Char) -> Boolean): Char { - var single: Char? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Char sequence contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Char sequence contains no character matching the predicate.") - @Suppress("UNCHECKED_CAST") - return single as Char -} - -/** - * Returns single character, or `null` if the char sequence is empty or has more than one character. - */ -public fun CharSequence.singleOrNull(): Char? { - return if (length == 1) this[0] else null -} - -/** - * Returns the single character matching the given [predicate], or `null` if character was not found or more than one character was found. - */ -public inline fun CharSequence.singleOrNull(predicate: (Char) -> Boolean): Char? { - var single: Char? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) return null - single = element - found = true - } - } - if (!found) return null - return single -} - -/** - * Returns a subsequence of this char sequence with the first [n] characters removed. - */ -public fun CharSequence.drop(n: Int): CharSequence { - require(n >= 0) { "Requested character count $n is less than zero." } - return subSequence(n.coerceAtMost(length), length) -} - -/** - * Returns a string with the first [n] characters removed. - */ -public fun String.drop(n: Int): String { - require(n >= 0) { "Requested character count $n is less than zero." } - return substring(n.coerceAtMost(length)) -} - -/** - * Returns a subsequence of this char sequence with the last [n] characters removed. - */ -public fun CharSequence.dropLast(n: Int): CharSequence { - require(n >= 0) { "Requested character count $n is less than zero." } - return take((length - n).coerceAtLeast(0)) -} - -/** - * Returns a string with the last [n] characters removed. - */ -public fun String.dropLast(n: Int): String { - require(n >= 0) { "Requested character count $n is less than zero." } - return take((length - n).coerceAtLeast(0)) -} - -/** - * Returns a subsequence of this char sequence containing all characters except last characters that satisfy the given [predicate]. - */ -public inline fun CharSequence.dropLastWhile(predicate: (Char) -> Boolean): CharSequence { - for (index in lastIndex downTo 0) - if (!predicate(this[index])) - return subSequence(0, index + 1) - return "" -} - -/** - * Returns a string containing all characters except last characters that satisfy the given [predicate]. - */ -public inline fun String.dropLastWhile(predicate: (Char) -> Boolean): String { - for (index in lastIndex downTo 0) - if (!predicate(this[index])) - return substring(0, index + 1) - return "" -} - -/** - * Returns a subsequence of this char sequence containing all characters except first characters that satisfy the given [predicate]. - */ -public inline fun CharSequence.dropWhile(predicate: (Char) -> Boolean): CharSequence { - for (index in this.indices) - if (!predicate(this[index])) - return subSequence(index, length) - return "" -} - -/** - * Returns a string containing all characters except first characters that satisfy the given [predicate]. - */ -public inline fun String.dropWhile(predicate: (Char) -> Boolean): String { - for (index in this.indices) - if (!predicate(this[index])) - return substring(index) - return "" -} - -/** - * Returns a char sequence containing only those characters from the original char sequence that match the given [predicate]. - */ -public inline fun CharSequence.filter(predicate: (Char) -> Boolean): CharSequence { - return filterTo(StringBuilder(), predicate) -} - -/** - * Returns a string containing only those characters from the original string that match the given [predicate]. - */ -public inline fun String.filter(predicate: (Char) -> Boolean): String { - return filterTo(StringBuilder(), predicate).toString() -} - -/** - * Returns a char sequence containing only those characters from the original char sequence that match the given [predicate]. - * @param [predicate] function that takes the index of a character and the character itself - * and returns the result of predicate evaluation on the character. - */ -public inline fun CharSequence.filterIndexed(predicate: (index: Int, Char) -> Boolean): CharSequence { - return filterIndexedTo(StringBuilder(), predicate) -} - -/** - * Returns a string containing only those characters from the original string that match the given [predicate]. - * @param [predicate] function that takes the index of a character and the character itself - * and returns the result of predicate evaluation on the character. - */ -public inline fun String.filterIndexed(predicate: (index: Int, Char) -> Boolean): String { - return filterIndexedTo(StringBuilder(), predicate).toString() -} - -/** - * Appends all characters matching the given [predicate] to the given [destination]. - * @param [predicate] function that takes the index of a character and the character itself - * and returns the result of predicate evaluation on the character. - */ -public inline fun CharSequence.filterIndexedTo(destination: C, predicate: (index: Int, Char) -> Boolean): C { - forEachIndexed { index, element -> - if (predicate(index, element)) destination.append(element) - } - return destination -} - -/** - * Returns a char sequence containing only those characters from the original char sequence that do not match the given [predicate]. - */ -public inline fun CharSequence.filterNot(predicate: (Char) -> Boolean): CharSequence { - return filterNotTo(StringBuilder(), predicate) -} - -/** - * Returns a string containing only those characters from the original string that do not match the given [predicate]. - */ -public inline fun String.filterNot(predicate: (Char) -> Boolean): String { - return filterNotTo(StringBuilder(), predicate).toString() -} - -/** - * Appends all characters not matching the given [predicate] to the given [destination]. - */ -public inline fun CharSequence.filterNotTo(destination: C, predicate: (Char) -> Boolean): C { - for (element in this) if (!predicate(element)) destination.append(element) - return destination -} - -/** - * Appends all characters matching the given [predicate] to the given [destination]. - */ -public inline fun CharSequence.filterTo(destination: C, predicate: (Char) -> Boolean): C { - for (index in 0 until length) { - val element = get(index) - if (predicate(element)) destination.append(element) - } - return destination -} - -/** - * Returns a char sequence containing characters of the original char sequence at the specified range of [indices]. - */ -public fun CharSequence.slice(indices: IntRange): CharSequence { - if (indices.isEmpty()) return "" - return subSequence(indices) -} - -/** - * Returns a string containing characters of the original string at the specified range of [indices]. - */ -public fun String.slice(indices: IntRange): String { - if (indices.isEmpty()) return "" - return substring(indices) -} - -/** - * Returns a char sequence containing characters of the original char sequence at specified [indices]. - */ -public fun CharSequence.slice(indices: Iterable): CharSequence { - val size = indices.collectionSizeOrDefault(10) - if (size == 0) return "" - val result = StringBuilder(size) - for (i in indices) { - result.append(get(i)) - } - return result -} - -/** - * Returns a string containing characters of the original string at specified [indices]. - */ -@kotlin.internal.InlineOnly -public inline fun String.slice(indices: Iterable): String { - return (this as CharSequence).slice(indices).toString() -} - -/** - * Returns a subsequence of this char sequence containing the first [n] characters from this char sequence, or the entire char sequence if this char sequence is shorter. - */ -public fun CharSequence.take(n: Int): CharSequence { - require(n >= 0) { "Requested character count $n is less than zero." } - return subSequence(0, n.coerceAtMost(length)) -} - -/** - * Returns a string containing the first [n] characters from this string, or the entire string if this string is shorter. - */ -public fun String.take(n: Int): String { - require(n >= 0) { "Requested character count $n is less than zero." } - return substring(0, n.coerceAtMost(length)) -} - -/** - * Returns a subsequence of this char sequence containing the last [n] characters from this char sequence, or the entire char sequence if this char sequence is shorter. - */ -public fun CharSequence.takeLast(n: Int): CharSequence { - require(n >= 0) { "Requested character count $n is less than zero." } - val length = length - return subSequence(length - n.coerceAtMost(length), length) -} - -/** - * Returns a string containing the last [n] characters from this string, or the entire string if this string is shorter. - */ -public fun String.takeLast(n: Int): String { - require(n >= 0) { "Requested character count $n is less than zero." } - val length = length - return substring(length - n.coerceAtMost(length)) -} - -/** - * Returns a subsequence of this char sequence containing last characters that satisfy the given [predicate]. - */ -public inline fun CharSequence.takeLastWhile(predicate: (Char) -> Boolean): CharSequence { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return subSequence(index + 1, length) - } - } - return subSequence(0, length) -} - -/** - * Returns a string containing last characters that satisfy the given [predicate]. - */ -public inline fun String.takeLastWhile(predicate: (Char) -> Boolean): String { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return substring(index + 1) - } - } - return this -} - -/** - * Returns a subsequence of this char sequence containing the first characters that satisfy the given [predicate]. - */ -public inline fun CharSequence.takeWhile(predicate: (Char) -> Boolean): CharSequence { - for (index in 0 until length) - if (!predicate(get(index))) { - return subSequence(0, index) - } - return subSequence(0, length) -} - -/** - * Returns a string containing the first characters that satisfy the given [predicate]. - */ -public inline fun String.takeWhile(predicate: (Char) -> Boolean): String { - for (index in 0 until length) - if (!predicate(get(index))) { - return substring(0, index) - } - return this -} - -/** - * Returns a char sequence with characters in reversed order. - */ -public fun CharSequence.reversed(): CharSequence { - return StringBuilder(this).reverse() -} - -/** - * Returns a string with characters in reversed order. - */ -@kotlin.internal.InlineOnly -public inline fun String.reversed(): String { - return (this as CharSequence).reversed().toString() -} - -/** - * Returns a [Map] containing key-value pairs provided by [transform] function - * applied to characters of the given char sequence. - * - * If any of two pairs would have the same key the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original char sequence. - */ -public inline fun CharSequence.associate(transform: (Char) -> Pair): Map { - val capacity = mapCapacity(length).coerceAtLeast(16) - return associateTo(LinkedHashMap(capacity), transform) -} - -/** - * Returns a [Map] containing the characters from the given char sequence indexed by the key - * returned from [keySelector] function applied to each character. - * - * If any two characters would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original char sequence. - */ -public inline fun CharSequence.associateBy(keySelector: (Char) -> K): Map { - val capacity = mapCapacity(length).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector) -} - -/** - * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to characters of the given char sequence. - * - * If any two characters would have the same key returned by [keySelector] the last one gets added to the map. - * - * The returned map preserves the entry iteration order of the original char sequence. - */ -public inline fun CharSequence.associateBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map { - val capacity = mapCapacity(length).coerceAtLeast(16) - return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform) -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function applied to each character of the given char sequence - * and value is the character itself. - * - * If any two characters would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > CharSequence.associateByTo(destination: M, keySelector: (Char) -> K): M { - for (element in this) { - destination.put(keySelector(element), element) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs, - * where key is provided by the [keySelector] function and - * and value is provided by the [valueTransform] function applied to characters of the given char sequence. - * - * If any two characters would have the same key returned by [keySelector] the last one gets added to the map. - */ -public inline fun > CharSequence.associateByTo(destination: M, keySelector: (Char) -> K, valueTransform: (Char) -> V): M { - for (element in this) { - destination.put(keySelector(element), valueTransform(element)) - } - return destination -} - -/** - * Populates and returns the [destination] mutable map with key-value pairs - * provided by [transform] function applied to each character of the given char sequence. - * - * If any of two pairs would have the same key the last one gets added to the map. - */ -public inline fun > CharSequence.associateTo(destination: M, transform: (Char) -> Pair): M { - for (element in this) { - destination += transform(element) - } - return destination -} - -/** - * Appends all characters to the given [destination] collection. - */ -public fun > CharSequence.toCollection(destination: C): C { - for (item in this) { - destination.add(item) - } - return destination -} - -/** - * Returns a [HashSet] of all characters. - */ -public fun CharSequence.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(length))) -} - -/** - * Returns a [List] containing all characters. - */ -public fun CharSequence.toList(): List { - return when (length) { - 0 -> emptyList() - 1 -> listOf(this[0]) - else -> this.toMutableList() - } -} - -/** - * Returns a [MutableList] filled with all characters of this char sequence. - */ -public fun CharSequence.toMutableList(): MutableList { - return toCollection(ArrayList(length)) -} - -/** - * Returns a [Set] of all characters. - * - * The returned set preserves the element iteration order of the original char sequence. - */ -public fun CharSequence.toSet(): Set { - return when (length) { - 0 -> emptySet() - 1 -> setOf(this[0]) - else -> toCollection(LinkedHashSet(mapCapacity(length))) - } -} - -/** - * Returns a [SortedSet][java.util.SortedSet] of all characters. - */ -public fun CharSequence.toSortedSet(): java.util.SortedSet { - return toCollection(java.util.TreeSet()) -} - -/** - * Returns a single list of all elements yielded from results of [transform] function being invoked on each character of original char sequence. - */ -public inline fun CharSequence.flatMap(transform: (Char) -> Iterable): List { - return flatMapTo(ArrayList(), transform) -} - -/** - * Appends all elements yielded from results of [transform] function being invoked on each character of original char sequence, to the given [destination]. - */ -public inline fun > CharSequence.flatMapTo(destination: C, transform: (Char) -> Iterable): C { - for (element in this) { - val list = transform(element) - destination.addAll(list) - } - return destination -} - -/** - * Groups characters of the original char sequence by the key returned by the given [keySelector] function - * applied to each character and returns a map where each group key is associated with a list of corresponding characters. - * - * The returned map preserves the entry iteration order of the keys produced from the original char sequence. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun CharSequence.groupBy(keySelector: (Char) -> K): Map> { - return groupByTo(LinkedHashMap>(), keySelector) -} - -/** - * Groups values returned by the [valueTransform] function applied to each character of the original char sequence - * by the key returned by the given [keySelector] function applied to the character - * and returns a map where each group key is associated with a list of corresponding values. - * - * The returned map preserves the entry iteration order of the keys produced from the original char sequence. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun CharSequence.groupBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map> { - return groupByTo(LinkedHashMap>(), keySelector, valueTransform) -} - -/** - * Groups characters of the original char sequence by the key returned by the given [keySelector] function - * applied to each character and puts to the [destination] map each group key associated with a list of corresponding characters. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupBy - */ -public inline fun >> CharSequence.groupByTo(destination: M, keySelector: (Char) -> K): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(element) - } - return destination -} - -/** - * Groups values returned by the [valueTransform] function applied to each character of the original char sequence - * by the key returned by the given [keySelector] function applied to the character - * and puts to the [destination] map each group key associated with a list of corresponding values. - * - * @return The [destination] map. - * - * @sample samples.collections.Collections.Transformations.groupByKeysAndValues - */ -public inline fun >> CharSequence.groupByTo(destination: M, keySelector: (Char) -> K, valueTransform: (Char) -> V): M { - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(valueTransform(element)) - } - return destination -} - -/** - * Creates a [Grouping] source from a char sequence to be used later with one of group-and-fold operations - * using the specified [keySelector] function to extract a key from each character. - * - * @sample samples.collections.Collections.Transformations.groupingByEachCount - */ -@SinceKotlin("1.1") -public inline fun CharSequence.groupingBy(crossinline keySelector: (Char) -> K): Grouping { - return object : Grouping { - override fun sourceIterator(): Iterator = this@groupingBy.iterator() - override fun keyOf(element: Char): K = keySelector(element) - } -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each character in the original char sequence. - */ -public inline fun CharSequence.map(transform: (Char) -> R): List { - return mapTo(ArrayList(length), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to each character and its index in the original char sequence. - * @param [transform] function that takes the index of a character and the character itself - * and returns the result of the transform applied to the character. - */ -public inline fun CharSequence.mapIndexed(transform: (index: Int, Char) -> R): List { - return mapIndexedTo(ArrayList(length), transform) -} - -/** - * Returns a list containing only the non-null results of applying the given [transform] function - * to each character and its index in the original char sequence. - * @param [transform] function that takes the index of a character and the character itself - * and returns the result of the transform applied to the character. - */ -public inline fun CharSequence.mapIndexedNotNull(transform: (index: Int, Char) -> R?): List { - return mapIndexedNotNullTo(ArrayList(), transform) -} - -/** - * Applies the given [transform] function to each character and its index in the original char sequence - * and appends only the non-null results to the given [destination]. - * @param [transform] function that takes the index of a character and the character itself - * and returns the result of the transform applied to the character. - */ -public inline fun > CharSequence.mapIndexedNotNullTo(destination: C, transform: (index: Int, Char) -> R?): C { - forEachIndexed { index, element -> transform(index, element)?.let { destination.add(it) } } - return destination -} - -/** - * Applies the given [transform] function to each character and its index in the original char sequence - * and appends the results to the given [destination]. - * @param [transform] function that takes the index of a character and the character itself - * and returns the result of the transform applied to the character. - */ -public inline fun > CharSequence.mapIndexedTo(destination: C, transform: (index: Int, Char) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Returns a list containing only the non-null results of applying the given [transform] function - * to each character in the original char sequence. - */ -public inline fun CharSequence.mapNotNull(transform: (Char) -> R?): List { - return mapNotNullTo(ArrayList(), transform) -} - -/** - * Applies the given [transform] function to each character in the original char sequence - * and appends only the non-null results to the given [destination]. - */ -public inline fun > CharSequence.mapNotNullTo(destination: C, transform: (Char) -> R?): C { - forEach { element -> transform(element)?.let { destination.add(it) } } - return destination -} - -/** - * Applies the given [transform] function to each character of the original char sequence - * and appends the results to the given [destination]. - */ -public inline fun > CharSequence.mapTo(destination: C, transform: (Char) -> R): C { - for (item in this) - destination.add(transform(item)) - return destination -} - -/** - * Returns a lazy [Iterable] of [IndexedValue] for each character of the original char sequence. - */ -public fun CharSequence.withIndex(): Iterable> { - return IndexingIterable { iterator() } -} - -/** - * Returns `true` if all characters match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.all - */ -public inline fun CharSequence.all(predicate: (Char) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if char sequence has at least one character. - * - * @sample samples.collections.Collections.Aggregates.any - */ -public fun CharSequence.any(): Boolean { - return !isEmpty() -} - -/** - * Returns `true` if at least one character matches the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.anyWithPredicate - */ -public inline fun CharSequence.any(predicate: (Char) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return true - return false -} - -/** - * Returns the length of this char sequence. - */ -@kotlin.internal.InlineOnly -public inline fun CharSequence.count(): Int { - return length -} - -/** - * Returns the number of characters matching the given [predicate]. - */ -public inline fun CharSequence.count(predicate: (Char) -> Boolean): Int { - var count = 0 - for (element in this) if (predicate(element)) count++ - return count -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each character. - */ -public inline fun CharSequence.fold(initial: R, operation: (acc: R, Char) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right - * to current accumulator value and each character with its index in the original char sequence. - * @param [operation] function that takes the index of a character, current accumulator value - * and the character itself, and calculates the next accumulator value. - */ -public inline fun CharSequence.foldIndexed(initial: R, operation: (index: Int, acc: R, Char) -> R): R { - var index = 0 - var accumulator = initial - for (element in this) accumulator = operation(index++, accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left to each character and current accumulator value. - */ -public inline fun CharSequence.foldRight(initial: R, operation: (Char, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left - * to each character with its index in the original char sequence and current accumulator value. - * @param [operation] function that takes the index of a character, the character itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun CharSequence.foldRightIndexed(initial: R, operation: (index: Int, Char, acc: R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Performs the given [action] on each character. - */ -public inline fun CharSequence.forEach(action: (Char) -> Unit): Unit { - for (element in this) action(element) -} - -/** - * Performs the given [action] on each character, providing sequential index with the character. - * @param [action] function that takes the index of a character and the character itself - * and performs the desired action on the character. - */ -public inline fun CharSequence.forEachIndexed(action: (index: Int, Char) -> Unit): Unit { - var index = 0 - for (item in this) action(index++, item) -} - -/** - * Returns the largest character or `null` if there are no characters. - */ -public fun CharSequence.max(): Char? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (max < e) max = e - } - return max -} - -/** - * Returns the first character yielding the largest value of the given function or `null` if there are no characters. - */ -public inline fun > CharSequence.maxBy(selector: (Char) -> R): Char? { - if (isEmpty()) return null - var maxElem = this[0] - var maxValue = selector(maxElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (maxValue < v) { - maxElem = e - maxValue = v - } - } - return maxElem -} - -/** - * Returns the first character having the largest value according to the provided [comparator] or `null` if there are no characters. - */ -public fun CharSequence.maxWith(comparator: Comparator): Char? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(max, e) < 0) max = e - } - return max -} - -/** - * Returns the smallest character or `null` if there are no characters. - */ -public fun CharSequence.min(): Char? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (min > e) min = e - } - return min -} - -/** - * Returns the first character yielding the smallest value of the given function or `null` if there are no characters. - */ -public inline fun > CharSequence.minBy(selector: (Char) -> R): Char? { - if (isEmpty()) return null - var minElem = this[0] - var minValue = selector(minElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = selector(e) - if (minValue > v) { - minElem = e - minValue = v - } - } - return minElem -} - -/** - * Returns the first character having the smallest value according to the provided [comparator] or `null` if there are no characters. - */ -public fun CharSequence.minWith(comparator: Comparator): Char? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(min, e) > 0) min = e - } - return min -} - -/** - * Returns `true` if the char sequence has no characters. - * - * @sample samples.collections.Collections.Aggregates.none - */ -public fun CharSequence.none(): Boolean { - return isEmpty() -} - -/** - * Returns `true` if no characters match the given [predicate]. - * - * @sample samples.collections.Collections.Aggregates.noneWithPredicate - */ -public inline fun CharSequence.none(predicate: (Char) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return false - return true -} - -/** - * Performs the given [action] on each character and returns the char sequence itself afterwards. - */ -@SinceKotlin("1.1") -public inline fun S.onEach(action: (Char) -> Unit): S { - return apply { for (element in this) action(element) } -} - -/** - * Accumulates value starting with the first character and applying [operation] from left to right to current accumulator value and each character. - */ -public inline fun CharSequence.reduce(operation: (acc: Char, Char) -> Char): Char { - if (isEmpty()) - throw UnsupportedOperationException("Empty char sequence can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with the first character and applying [operation] from left to right - * to current accumulator value and each character with its index in the original char sequence. - * @param [operation] function that takes the index of a character, current accumulator value - * and the character itself and calculates the next accumulator value. - */ -public inline fun CharSequence.reduceIndexed(operation: (index: Int, acc: Char, Char) -> Char): Char { - if (isEmpty()) - throw UnsupportedOperationException("Empty char sequence can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(index, accumulator, this[index]) - } - return accumulator -} - -/** - * Accumulates value starting with last character and applying [operation] from right to left to each character and current accumulator value. - */ -public inline fun CharSequence.reduceRight(operation: (Char, acc: Char) -> Char): Char { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty char sequence can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Accumulates value starting with last character and applying [operation] from right to left - * to each character with its index in the original char sequence and current accumulator value. - * @param [operation] function that takes the index of a character, the character itself - * and current accumulator value, and calculates the next accumulator value. - */ -public inline fun CharSequence.reduceRightIndexed(operation: (index: Int, Char, acc: Char) -> Char): Char { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty char sequence can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - return accumulator -} - -/** - * Returns the sum of all values produced by [selector] function applied to each character in the char sequence. - */ -public inline fun CharSequence.sumBy(selector: (Char) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [selector] function applied to each character in the char sequence. - */ -public inline fun CharSequence.sumByDouble(selector: (Char) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += selector(element) - } - return sum -} - -/** - * Splits this char sequence into a list of strings each not exceeding the given [size]. - * - * The last string in the resulting list may have less characters than the given [size]. - * - * @param size the number of elements to take in each string, must be positive and can be greater than the number of elements in this char sequence. - * - * @sample samples.collections.Collections.Transformations.chunked - */ -@SinceKotlin("1.2") -public fun CharSequence.chunked(size: Int): List { - return windowed(size, size, partialWindows = true) -} - -/** - * Splits this char sequence into several char sequences each not exceeding the given [size] - * and applies the given [transform] function to an each. - * - * @return list of results of the [transform] applied to an each char sequence. - * - * Note that the char sequence passed to the [transform] function is ephemeral and is valid only inside that function. - * You should not store it or allow it to escape in some way, unless you made a snapshot of it. - * The last char sequence may have less characters than the given [size]. - * - * @param size the number of elements to take in each char sequence, must be positive and can be greater than the number of elements in this char sequence. - * - * @sample samples.text.Strings.chunkedTransform - */ -@SinceKotlin("1.2") -public fun CharSequence.chunked(size: Int, transform: (CharSequence) -> R): List { - return windowed(size, size, partialWindows = true, transform = transform) -} - -/** - * Splits this char sequence into a sequence of strings each not exceeding the given [size]. - * - * The last string in the resulting sequence may have less characters than the given [size]. - * - * @param size the number of elements to take in each string, must be positive and can be greater than the number of elements in this char sequence. - * - * @sample samples.collections.Collections.Transformations.chunked - */ -@SinceKotlin("1.2") -public fun CharSequence.chunkedSequence(size: Int): Sequence { - return chunkedSequence(size) { it.toString() } -} - -/** - * Splits this char sequence into several char sequences each not exceeding the given [size] - * and applies the given [transform] function to an each. - * - * @return sequence of results of the [transform] applied to an each char sequence. - * - * Note that the char sequence passed to the [transform] function is ephemeral and is valid only inside that function. - * You should not store it or allow it to escape in some way, unless you made a snapshot of it. - * The last char sequence may have less characters than the given [size]. - * - * @param size the number of elements to take in each char sequence, must be positive and can be greater than the number of elements in this char sequence. - * - * @sample samples.text.Strings.chunkedTransformToSequence - */ -@SinceKotlin("1.2") -public fun CharSequence.chunkedSequence(size: Int, transform: (CharSequence) -> R): Sequence { - return windowedSequence(size, size, partialWindows = true, transform = transform) -} - -/** - * Splits the original char sequence into pair of char sequences, - * where *first* char sequence contains characters for which [predicate] yielded `true`, - * while *second* char sequence contains characters for which [predicate] yielded `false`. - */ -public inline fun CharSequence.partition(predicate: (Char) -> Boolean): Pair { - val first = StringBuilder() - val second = StringBuilder() - for (element in this) { - if (predicate(element)) { - first.append(element) - } else { - second.append(element) - } - } - return Pair(first, second) -} - -/** - * Splits the original string into pair of strings, - * where *first* string contains characters for which [predicate] yielded `true`, - * while *second* string contains characters for which [predicate] yielded `false`. - */ -public inline fun String.partition(predicate: (Char) -> Boolean): Pair { - val first = StringBuilder() - val second = StringBuilder() - for (element in this) { - if (predicate(element)) { - first.append(element) - } else { - second.append(element) - } - } - return Pair(first.toString(), second.toString()) -} - -/** - * Returns a list of snapshots of the window of the given [size] - * sliding along this char sequence with the given [step], where each - * snapshot is a string. - * - * Several last strings may have less characters than the given [size]. - * - * Both [size] and [step] must be positive and can be greater than the number of elements in this char sequence. - * @param size the number of elements to take in each window - * @param step the number of elements to move the window forward by on an each step, by default 1 - * @param partialWindows controls whether or not to keep partial windows in the end if any, - * by default `false` which means partial windows won't be preserved - * - * @sample samples.collections.Sequences.Transformations.takeWindows - */ -@SinceKotlin("1.2") -public fun CharSequence.windowed(size: Int, step: Int = 1, partialWindows: Boolean = false): List { - return windowed(size, step, partialWindows) { it.toString() } -} - -/** - * Returns a list of results of applying the given [transform] function to - * an each char sequence representing a view over the window of the given [size] - * sliding along this char sequence with the given [step]. - * - * Note that the char sequence passed to the [transform] function is ephemeral and is valid only inside that function. - * You should not store it or allow it to escape in some way, unless you made a snapshot of it. - * Several last char sequences may have less characters than the given [size]. - * - * Both [size] and [step] must be positive and can be greater than the number of elements in this char sequence. - * @param size the number of elements to take in each window - * @param step the number of elements to move the window forward by on an each step, by default 1 - * @param partialWindows controls whether or not to keep partial windows in the end if any, - * by default `false` which means partial windows won't be preserved - * - * @sample samples.collections.Sequences.Transformations.averageWindows - */ -@SinceKotlin("1.2") -public fun CharSequence.windowed(size: Int, step: Int = 1, partialWindows: Boolean = false, transform: (CharSequence) -> R): List { - checkWindowSizeStep(size, step) - val thisSize = this.length - val result = ArrayList((thisSize + step - 1) / step) - var index = 0 - while (index < thisSize) { - val end = index + size - val coercedEnd = if (end > thisSize) { if (partialWindows) thisSize else break } else end - result.add(transform(subSequence(index, coercedEnd))) - index += step - } - return result -} - -/** - * Returns a sequence of snapshots of the window of the given [size] - * sliding along this char sequence with the given [step], where each - * snapshot is a string. - * - * Several last strings may have less characters than the given [size]. - * - * Both [size] and [step] must be positive and can be greater than the number of elements in this char sequence. - * @param size the number of elements to take in each window - * @param step the number of elements to move the window forward by on an each step, by default 1 - * @param partialWindows controls whether or not to keep partial windows in the end if any, - * by default `false` which means partial windows won't be preserved - * - * @sample samples.collections.Sequences.Transformations.takeWindows - */ -@SinceKotlin("1.2") -public fun CharSequence.windowedSequence(size: Int, step: Int = 1, partialWindows: Boolean = false): Sequence { - return windowedSequence(size, step, partialWindows) { it.toString() } -} - -/** - * Returns a sequence of results of applying the given [transform] function to - * an each char sequence representing a view over the window of the given [size] - * sliding along this char sequence with the given [step]. - * - * Note that the char sequence passed to the [transform] function is ephemeral and is valid only inside that function. - * You should not store it or allow it to escape in some way, unless you made a snapshot of it. - * Several last char sequences may have less characters than the given [size]. - * - * Both [size] and [step] must be positive and can be greater than the number of elements in this char sequence. - * @param size the number of elements to take in each window - * @param step the number of elements to move the window forward by on an each step, by default 1 - * @param partialWindows controls whether or not to keep partial windows in the end if any, - * by default `false` which means partial windows won't be preserved - * - * @sample samples.collections.Sequences.Transformations.averageWindows - */ -@SinceKotlin("1.2") -public fun CharSequence.windowedSequence(size: Int, step: Int = 1, partialWindows: Boolean = false, transform: (CharSequence) -> R): Sequence { - checkWindowSizeStep(size, step) - val windows = (if (partialWindows) indices else 0 until length - size + 1) step step - return windows.asSequence().map { index -> transform(subSequence(index, (index + size).coerceAtMost(length))) } -} - -/** - * Returns a list of pairs built from characters of both char sequences with same indexes. List has length of shortest char sequence. - */ -public infix fun CharSequence.zip(other: CharSequence): List> { - return zip(other) { c1, c2 -> c1 to c2 } -} - -/** - * Returns a list of values built from characters of both char sequences with same indexes using provided [transform]. List has length of shortest char sequence. - */ -public inline fun CharSequence.zip(other: CharSequence, transform: (a: Char, b: Char) -> V): List { - val length = minOf(this.length, other.length) - val list = ArrayList(length) - for (i in 0 until length) { - list.add(transform(this[i], other[i])) - } - return list -} - -/** - * Returns a list of pairs of each two adjacent characters in this char sequence. - * - * The returned list is empty if this char sequence contains less than two characters. - * - * @sample samples.collections.Collections.Transformations.zipWithNext - */ -@SinceKotlin("1.2") -public fun CharSequence.zipWithNext(): List> { - return zipWithNext { a, b -> a to b } -} - -/** - * Returns a list containing the results of applying the given [transform] function - * to an each pair of two adjacent characters in this char sequence. - * - * The returned list is empty if this char sequence contains less than two characters. - * - * @sample samples.collections.Collections.Transformations.zipWithNextToFindDeltas - */ -@SinceKotlin("1.2") -public inline fun CharSequence.zipWithNext(transform: (a: Char, b: Char) -> R): List { - val size = length - 1 - if (size < 1) return emptyList() - val result = ArrayList(size) - for (index in 0 until size) { - result.add(transform(this[index], this[index + 1])) - } - return result -} - -/** - * Creates an [Iterable] instance that wraps the original char sequence returning its characters when being iterated. - */ -public fun CharSequence.asIterable(): Iterable { - if (this is String && isEmpty()) return emptyList() - return Iterable { this.iterator() } -} - -/** - * Creates a [Sequence] instance that wraps the original char sequence returning its characters when being iterated. - */ -public fun CharSequence.asSequence(): Sequence { - if (this is String && isEmpty()) return emptySequence() - return Sequence { this.iterator() } -} - diff --git a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt index 26f7f4b17d2..d152e47389a 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt @@ -37,14 +37,14 @@ fun main(args: Array) { } val commonDir = baseDir.resolveExistingDir("libraries/stdlib/common/src/generated") - val jvmDir = baseDir.resolveExistingDir("libraries/stdlib/src/generated") - val jsDir = baseDir.resolveExistingDir("js/js.libraries/src/core/generated") + val jvmDir = baseDir.resolveExistingDir("libraries/stdlib/jvm/src/generated") + val jsDir = baseDir.resolveExistingDir("libraries/stdlib/js/src/generated") templateGroups.groupByFileAndWrite { (platform, source) -> // File("build/out/$platform/$source.kt") when (platform) { Platform.Common -> commonDir.resolve("_${source.name.capitalize()}.kt") - Platform.JVM -> jvmDir.resolve("_${source.name.capitalize()}.kt") + Platform.JVM -> jvmDir.resolve("_${source.name.capitalize()}Jvm.kt") Platform.JS -> jsDir.resolve("_${source.name.capitalize()}Js.kt") Platform.Native -> error("Native is unsupported yet") } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/dsl/MemberBuilder.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/dsl/MemberBuilder.kt index a51dd4fe1b1..4b270a34ed1 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/dsl/MemberBuilder.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/dsl/MemberBuilder.kt @@ -49,7 +49,7 @@ class MemberBuilder( val f get() = family - private val legacyMode = true + private val legacyMode = false var hasPlatformSpecializations: Boolean = legacyMode private set