From 113b97dda36c8e8b536a365961a1ce102748ca6d Mon Sep 17 00:00:00 2001 From: Ilya Matveev Date: Tue, 11 Apr 2017 19:15:27 +0700 Subject: [PATCH] stdlib: Add remaining extensions for arrays --- runtime/src/main/kotlin/kotlin/Arrays.kt | 10451 +++++++++++++++- .../kotlin/kotlin/collections/ArrayUtil.kt | 123 +- .../kotlin/kotlin/collections/Collections.kt | 10 +- .../main/kotlin/kotlin/collections/Maps.kt | 1 + 4 files changed, 10477 insertions(+), 108 deletions(-) diff --git a/runtime/src/main/kotlin/kotlin/Arrays.kt b/runtime/src/main/kotlin/kotlin/Arrays.kt index cad538c5da4..f3928687b9d 100644 --- a/runtime/src/main/kotlin/kotlin/Arrays.kt +++ b/runtime/src/main/kotlin/kotlin/Arrays.kt @@ -14,12 +14,17 @@ * limitations under the License. */ +// TODO: Add SortedSed class and implement toSortedSet extensions +// TODO: Add sort and binary search for primitive arrays + package kotlin import kotlin.collections.* +import kotlin.comparisons.* import kotlin.internal.PureReifiable import kotlin.util.sortArrayComparable import kotlin.util.sortArrayWith +import kotlin.util.sortArray // TODO: make all iterator() methods inline. /** @@ -101,8 +106,9 @@ public final class CharArray { @SymbolName("Kotlin_CharArray_set") external public operator fun set(index: Int, value: Char): Unit - @SymbolName("Kotlin_CharArray_copyOf") - external public fun copyOf(newSize: Int): CharArray + // TODO: Do we need it? + //@SymbolName("Kotlin_CharArray_copyOf") + //external public fun copyOf(newSize: Int): CharArray @SymbolName("Kotlin_CharArray_getArrayLength") external private fun getArrayLength(): Int @@ -411,6 +417,10 @@ private class BooleanIteratorImpl(val collection: BooleanArray) : BooleanIterato } } +// From ArraysJVM.kt +/** Returns the array if it's not `null`, or an empty array otherwise. */ +public inline fun Array?.orEmpty(): Array = this ?: emptyArray() + // This part is from generated _Arrays.kt. @@ -854,6 +864,70 @@ public fun Array.any(): Boolean { return false } +/** + * Returns `true` if array has at least one element. + */ +public fun ByteArray.any(): Boolean { + for (element in this) return true + return false +} + +/** + * Returns `true` if array has at least one element. + */ +public fun ShortArray.any(): Boolean { + for (element in this) return true + return false +} + +/** + * Returns `true` if array has at least one element. + */ +public fun IntArray.any(): Boolean { + for (element in this) return true + return false +} + +/** + * Returns `true` if array has at least one element. + */ +public fun LongArray.any(): Boolean { + for (element in this) return true + return false +} + +/** + * Returns `true` if array has at least one element. + */ +public fun FloatArray.any(): Boolean { + for (element in this) return true + return false +} + +/** + * Returns `true` if array has at least one element. + */ +public fun DoubleArray.any(): Boolean { + for (element in this) return true + return false +} + +/** + * Returns `true` if array has at least one element. + */ +public fun BooleanArray.any(): Boolean { + for (element in this) return true + return false +} + +/** + * Returns `true` if array has at least one element. + */ +public fun CharArray.any(): Boolean { + for (element in this) return true + return false +} + /** * Returns `true` if at least one element matches the given [predicate]. */ @@ -862,6 +936,70 @@ public inline fun Array.any(predicate: (T) -> Boolean): Boolean { return false } +/** + * Returns `true` if at least one element matches the given [predicate]. + */ +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]. + */ +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]. + */ +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]. + */ +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]. + */ +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]. + */ +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]. + */ +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]. + */ +public inline fun CharArray.any(predicate: (Char) -> Boolean): Boolean { + for (element in this) if (predicate(element)) return true + return false +} + /** * Returns `true` if [element] is found in the array. */ @@ -931,6 +1069,151 @@ public operator fun CharArray.contains(element: Char): Boolean { @kotlin.internal.InlineOnly public inline fun Array.count(): Int = this.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 +} + /** * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. */ @@ -1003,6 +1286,456 @@ 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 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 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 +} + /** * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. */ @@ -1057,6 +1790,375 @@ public inline fun CharArray.fold(initial: R, operation: (acc: R, Char) -> R) 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. */ @@ -1210,6 +2312,1535 @@ public inline fun CharArray.forEachIndexed(action: (index: Int, Char) -> Unit): 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. + */ +public fun Array.none(): Boolean { + for (element in this) return false + return true +} + +/** + * Returns `true` if the array has no elements. + */ +public fun ByteArray.none(): Boolean { + for (element in this) return false + return true +} + +/** + * Returns `true` if the array has no elements. + */ +public fun ShortArray.none(): Boolean { + for (element in this) return false + return true +} + +/** + * Returns `true` if the array has no elements. + */ +public fun IntArray.none(): Boolean { + for (element in this) return false + return true +} + +/** + * Returns `true` if the array has no elements. + */ +public fun LongArray.none(): Boolean { + for (element in this) return false + return true +} + +/** + * Returns `true` if the array has no elements. + */ +public fun FloatArray.none(): Boolean { + for (element in this) return false + return true +} + +/** + * Returns `true` if the array has no elements. + */ +public fun DoubleArray.none(): Boolean { + for (element in this) return false + return true +} + +/** + * Returns `true` if the array has no elements. + */ +public fun BooleanArray.none(): Boolean { + for (element in this) return false + return true +} + +/** + * Returns `true` if the array has no elements. + */ +public fun CharArray.none(): Boolean { + for (element in this) return false + return true +} + +/** + * Returns `true` if no elements match the given [predicate]. + */ +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]. + */ +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]. + */ +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]. + */ +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]. + */ +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]. + */ +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]. + */ +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]. + */ +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]. + */ +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 first index of [element], or -1 if the array does not contain element. */ @@ -1542,6 +4173,204 @@ public inline fun CharArray.indexOfLast(predicate: (Char) -> Boolean): Int { 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 a list containing all elements not matching the given [predicate]. */ @@ -1549,6 +4378,62 @@ 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) +} + /** * Appends all elements not matching the given [predicate] to the given [destination]. */ @@ -1557,6 +4442,70 @@ public inline fun > Array.filterNotTo(dest 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 +} + /** * Returns a list containing all elements that are not `null`. */ @@ -1579,6 +4528,70 @@ 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. */ @@ -1586,6 +4599,70 @@ 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 the first element, or `null` if the array is empty. */ @@ -1593,6 +4670,62 @@ 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. */ @@ -1601,6 +4734,70 @@ public inline fun Array.firstOrNull(predicate: (T) -> Boolean): T? { 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 the last element, or `null` if the array is empty. */ @@ -1608,6 +4805,62 @@ 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. */ @@ -1619,6 +4872,94 @@ public inline fun Array.lastOrNull(predicate: (T) -> Boolean): T? { 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. */ @@ -1868,15 +5209,2173 @@ public fun Array.singleOrNull(): T? { } /** - * Applies the given [transform] function to each element of the original array - * and appends the results to the given [destination]. + * Returns single element, or `null` if the array is empty or has more than one element. */ -public inline fun > Array.mapTo(destination: C, transform: (T) -> R): C { +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. + */ +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. + */ +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. + */ +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. + */ +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. + */ +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. + */ +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. + */ +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. + */ +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. + */ +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. + */ +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. + */ +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. + */ +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. + */ +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. + */ +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. + */ +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. + */ +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. + */ +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. + */ +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]. + */ +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]. + */ +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]. + */ +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]. + */ +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]. + */ +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]. + */ +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]. + */ +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]. + */ +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]. + */ +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]. + */ +public inline fun Array.dropWhile(predicate: (T) -> Boolean): List { + var yielding = false + val list = ArrayList() for (item in this) - destination.add(transform(item)) + 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]. + */ +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]. + */ +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]. + */ +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]. + */ +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]. + */ +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]. + */ +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]. + */ +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]. + */ +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 +} + +/** + * 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 = arrayOfUninitializedElements(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. + */ +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. + */ +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. + */ +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. + */ +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. + */ +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. + */ +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. + */ +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. + */ +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. + */ +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. + */ +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 .. size - 1) + list.add(this[index]) + return list +} + +/** + * Returns a list containing last [n] elements. + */ +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 .. size - 1) + list.add(this[index]) + return list +} + +/** + * Returns a list containing last [n] elements. + */ +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 .. size - 1) + list.add(this[index]) + return list +} + +/** + * Returns a list containing last [n] elements. + */ +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 .. size - 1) + list.add(this[index]) + return list +} + +/** + * Returns a list containing last [n] elements. + */ +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 .. size - 1) + list.add(this[index]) + return list +} + +/** + * Returns a list containing last [n] elements. + */ +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 .. size - 1) + list.add(this[index]) + return list +} + +/** + * Returns a list containing last [n] elements. + */ +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 .. size - 1) + list.add(this[index]) + return list +} + +/** + * Returns a list containing last [n] elements. + */ +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 .. size - 1) + list.add(this[index]) + return list +} + +/** + * Returns a list containing last [n] elements. + */ +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 .. size - 1) + list.add(this[index]) + return list +} + +/** + * Returns a list containing last elements satisfying the given [predicate]. + */ +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]. + */ +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]. + */ +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]. + */ +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]. + */ +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]. + */ +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]. + */ +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]. + */ +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]. + */ +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]. + */ +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]. + */ +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]. + */ +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]. + */ +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]. + */ +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]. + */ +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]. + */ +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]. + */ +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]. + */ +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 = arrayOfUninitializedElements(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 +} + /** * Returns a list containing the results of applying the given [transform] function * to each element in the original array. @@ -1885,6 +7384,1115 @@ 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 an average value of elements in the array. @@ -2109,6 +8717,72 @@ public fun Array.sum(): Double { 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 +} + // From _Arrays.kt. /** * Returns the range of valid indices for the array. @@ -2578,6 +9252,95 @@ public fun Array.toList(): List { } } +/** + * 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. */ @@ -2585,6 +9348,79 @@ 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 [HashSet] of all elements. */ @@ -2648,6 +9484,839 @@ public fun CharArray.toHashSet(): HashSet { return toCollection(HashSet(mapCapacity(size))) } +/** + * 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 new array which is a copy of the original array. */ @@ -2656,6 +10325,236 @@ public inline fun Array.copyOf(): Array { return this.copyOfUninitializedElements(size) } +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.internal.InlineOnly +public inline fun ByteArray.copyOf(): ByteArray { + return copyOfUninitializedElements(size) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.internal.InlineOnly +public inline fun ShortArray.copyOf(): ShortArray { + return copyOfUninitializedElements(size) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.internal.InlineOnly +public inline fun IntArray.copyOf(): IntArray { + return copyOfUninitializedElements(size) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.internal.InlineOnly +public inline fun LongArray.copyOf(): LongArray { + return copyOfUninitializedElements(size) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.internal.InlineOnly +public inline fun FloatArray.copyOf(): FloatArray { + return copyOfUninitializedElements(size) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.internal.InlineOnly +public inline fun DoubleArray.copyOf(): DoubleArray { + return copyOfUninitializedElements(size) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.internal.InlineOnly +public inline fun BooleanArray.copyOf(): BooleanArray { + return copyOfUninitializedElements(size) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.internal.InlineOnly +public inline fun CharArray.copyOf(): CharArray { + return copyOfUninitializedElements(size) +} + +/** + * 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 copyOfNulls(newSize) as Array +} + +/** + * 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 copyOfUninitializedElements(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 copyOfUninitializedElements(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 copyOfUninitializedElements(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 copyOfUninitializedElements(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 copyOfUninitializedElements(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 copyOfUninitializedElements(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 copyOfUninitializedElements(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 copyOfUninitializedElements(newSize) +} + +/** + * Returns new array which is a copy of range of original array. + */ +// TODO: The method may check input or return Array. +// Now we check its input (fromIndex <= toIndex < size). +// Sync its behaviour wiht Kotlin JVM when the problem is solved there. +@kotlin.internal.InlineOnly +public inline fun Array.copyOfRange(fromIndex: Int, toIndex: Int): Array { + if (fromIndex > toIndex || toIndex > size) + throw IllegalArgumentException("Wrong indices: fromIndex: $fromIndex, toIndex: $toIndex, array size: $size") + return copyOfUninitializedElements(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 { + if (fromIndex > toIndex || toIndex > size) + throw IllegalArgumentException("Wrong indices: fromIndex: $fromIndex, toIndex: $toIndex, array size: $size") + return copyOfUninitializedElements(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 { + if (fromIndex > toIndex || toIndex > size) + throw IllegalArgumentException("Wrong indices: fromIndex: $fromIndex, toIndex: $toIndex, array size: $size") + return copyOfUninitializedElements(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 { + if (fromIndex > toIndex || toIndex > size) + throw IllegalArgumentException("Wrong indices: fromIndex: $fromIndex, toIndex: $toIndex, array size: $size") + return copyOfUninitializedElements(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 { + if (fromIndex > toIndex || toIndex > size) + throw IllegalArgumentException("Wrong indices: fromIndex: $fromIndex, toIndex: $toIndex, array size: $size") + return copyOfUninitializedElements(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 { + if (fromIndex > toIndex || toIndex > size) + throw IllegalArgumentException("Wrong indices: fromIndex: $fromIndex, toIndex: $toIndex, array size: $size") + return copyOfUninitializedElements(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 { + if (fromIndex > toIndex || toIndex > size) + throw IllegalArgumentException("Wrong indices: fromIndex: $fromIndex, toIndex: $toIndex, array size: $size") + return copyOfUninitializedElements(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 { + if (fromIndex > toIndex || toIndex > size) + throw IllegalArgumentException("Wrong indices: fromIndex: $fromIndex, toIndex: $toIndex, array size: $size") + return copyOfUninitializedElements(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 { + if (fromIndex > toIndex || toIndex > size) + throw IllegalArgumentException("Wrong indices: fromIndex: $fromIndex, toIndex: $toIndex, array size: $size") + return copyOfUninitializedElements(fromIndex, toIndex) +} + + /** * Sorts the array in-place according to the order specified by the given [comparator]. */ @@ -2686,6 +10585,76 @@ 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. */ @@ -2693,6 +10662,20 @@ 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. */ @@ -2700,6 +10683,34 @@ 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. */ @@ -2708,6 +10719,62 @@ public fun > Array.sortedArray(): Array { 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. */ @@ -2716,6 +10783,62 @@ public fun > Array.sortedArrayDescending(): Array { 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 a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. */ @@ -2723,6 +10846,62 @@ public inline fun > Array.sortedBy(crossinline selec 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. */ @@ -2730,6 +10909,62 @@ public inline fun > Array.sortedByDescending(crossin 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. */ @@ -2737,6 +10972,55 @@ 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() +} + /** * Sorts the array in-place according to the natural order of its elements. * @@ -2746,11 +11030,81 @@ public fun Array.sort(): Unit { if (size > 1) sortArrayComparable(this) } - public fun Array.sortWith(comparator: Comparator, fromIndex: Int = 0, toIndex: Int = size): Unit { sortArrayWith(this, fromIndex, toIndex, comparator) } +/** + * Sorts the array in-place. + */ +public fun IntArray.sort(): Unit { + if (size > 1) sortArray(this) +} + +/** + * Sorts the array in-place. + */ +public fun LongArray.sort(): Unit { + if (size > 1) sortArray(this) +} + +/** + * Sorts the array in-place. + */ +public fun ByteArray.sort(): Unit { + if (size > 1) sortArray(this) +} + +/** + * Sorts the array in-place. + */ +public fun ShortArray.sort(): Unit { + if (size > 1) sortArray(this) +} + +/** + * Sorts the array in-place. + */ +public fun DoubleArray.sort(): Unit { + if (size > 1) sortArray(this) +} + +/** + * Sorts the array in-place. + */ +public fun FloatArray.sort(): Unit { + if (size > 1) sortArray(this) +} + +/** + * Sorts the array in-place. + */ +public fun CharArray.sort(): Unit { + if (size > 1) sortArray(this) +} + +/** + * 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. */ @@ -2762,6 +11116,61 @@ public fun IntArray.toTypedArray(): Array { 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 list of all elements sorted according to the specified [comparator]. */ @@ -2769,6 +11178,62 @@ 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 an array with all elements of this array sorted according the specified [comparator]. */ @@ -3106,7 +11571,6 @@ private fun Array.contentDeepToString(buf: StringBuilder, dejaVu: Mut * i.e. contain the same number of the same elements in the same order. */ @SinceKotlin("1.1") -@kotlin.internal.InlineOnly public infix fun Array.contentEquals(other: Array): Boolean { if (this === other) { return true @@ -3127,7 +11591,6 @@ public infix fun Array.contentEquals(other: Array): Boolean { * i.e. contain the same number of the same elements in the same order. */ @SinceKotlin("1.1") -@kotlin.internal.InlineOnly public infix fun ByteArray.contentEquals(other: ByteArray): Boolean { if (this === other) { return true @@ -3148,7 +11611,6 @@ public infix fun ByteArray.contentEquals(other: ByteArray): Boolean { * i.e. contain the same number of the same elements in the same order. */ @SinceKotlin("1.1") -@kotlin.internal.InlineOnly public infix fun ShortArray.contentEquals(other: ShortArray): Boolean { if (this === other) { return true @@ -3169,7 +11631,6 @@ public infix fun ShortArray.contentEquals(other: ShortArray): Boolean { * i.e. contain the same number of the same elements in the same order. */ @SinceKotlin("1.1") -@kotlin.internal.InlineOnly public infix fun IntArray.contentEquals(other: IntArray): Boolean { if (this === other) { return true @@ -3190,7 +11651,6 @@ public infix fun IntArray.contentEquals(other: IntArray): Boolean { * i.e. contain the same number of the same elements in the same order. */ @SinceKotlin("1.1") -@kotlin.internal.InlineOnly public infix fun LongArray.contentEquals(other: LongArray): Boolean { if (this === other) { return true @@ -3211,7 +11671,6 @@ public infix fun LongArray.contentEquals(other: LongArray): Boolean { * i.e. contain the same number of the same elements in the same order. */ @SinceKotlin("1.1") -@kotlin.internal.InlineOnly public infix fun FloatArray.contentEquals(other: FloatArray): Boolean { if (this === other) { return true @@ -3232,7 +11691,6 @@ public infix fun FloatArray.contentEquals(other: FloatArray): Boolean { * i.e. contain the same number of the same elements in the same order. */ @SinceKotlin("1.1") -@kotlin.internal.InlineOnly public infix fun DoubleArray.contentEquals(other: DoubleArray): Boolean { if (this === other) { return true @@ -3253,7 +11711,6 @@ public infix fun DoubleArray.contentEquals(other: DoubleArray): Boolean { * i.e. contain the same number of the same elements in the same order. */ @SinceKotlin("1.1") -@kotlin.internal.InlineOnly public infix fun BooleanArray.contentEquals(other: BooleanArray): Boolean { if (this === other) { return true @@ -3274,7 +11731,6 @@ public infix fun BooleanArray.contentEquals(other: BooleanArray): Boolean { * i.e. contain the same number of the same elements in the same order. */ @SinceKotlin("1.1") -@kotlin.internal.InlineOnly public infix fun CharArray.contentEquals(other: CharArray): Boolean { if (this === other) { return true @@ -3290,6 +11746,896 @@ public infix fun CharArray.contentEquals(other: CharArray): Boolean { return true; } +/** + * Returns a hash code based on the contents of this array as if it is [List]. + */ +@SinceKotlin("1.1") +public fun Array.contentHashCode(): Int { + var result = 1 + for (element in this) + result = 31 * result + (element?.hashCode() ?: 0) + return result +} + +/** + * Returns a hash code based on the contents of this array as if it is [List]. + */ +@SinceKotlin("1.1") +public fun ByteArray.contentHashCode(): Int { + var result = 1 + for (element in this) + result = 31 * result + (element?.hashCode() ?: 0) + return result +} + +/** + * Returns a hash code based on the contents of this array as if it is [List]. + */ +@SinceKotlin("1.1") +public fun ShortArray.contentHashCode(): Int { + var result = 1 + for (element in this) + result = 31 * result + (element?.hashCode() ?: 0) + return result +} + +/** + * Returns a hash code based on the contents of this array as if it is [List]. + */ +@SinceKotlin("1.1") +public fun IntArray.contentHashCode(): Int { + var result = 1 + for (element in this) + result = 31 * result + (element?.hashCode() ?: 0) + return result +} + +/** + * Returns a hash code based on the contents of this array as if it is [List]. + */ +@SinceKotlin("1.1") +public fun LongArray.contentHashCode(): Int { + var result = 1 + for (element in this) + result = 31 * result + (element?.hashCode() ?: 0) + return result +} + +/** + * Returns a hash code based on the contents of this array as if it is [List]. + */ +@SinceKotlin("1.1") +public fun FloatArray.contentHashCode(): Int { + var result = 1 + for (element in this) + result = 31 * result + (element?.hashCode() ?: 0) + return result +} + +/** + * Returns a hash code based on the contents of this array as if it is [List]. + */ +@SinceKotlin("1.1") +public fun DoubleArray.contentHashCode(): Int { + var result = 1 + for (element in this) + result = 31 * result + (element?.hashCode() ?: 0) + return result +} + +/** + * Returns a hash code based on the contents of this array as if it is [List]. + */ +@SinceKotlin("1.1") +public fun BooleanArray.contentHashCode(): Int { + var result = 1 + for (element in this) + result = 31 * result + (element?.hashCode() ?: 0) + return result +} + +/** + * Returns a hash code based on the contents of this array as if it is [List]. + */ +@SinceKotlin("1.1") +public fun CharArray.contentHashCode(): Int { + var result = 1 + for (element in this) + result = 31 * result + (element?.hashCode() ?: 0) + return result +} + +/** + * 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 [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 +} + // From Library.kt. /** * Returns an array of objects of the given type with the given [size], initialized with null values. @@ -3933,161 +13279,1074 @@ public inline fun CharArray.sumBy(selector: (Char) -> Int): Int { } /** - * Returns a [List] containing all elements. + * Returns the sum of all values produced by [selector] function applied to each element in the array. */ -public fun ByteArray.toList(): List { - return when (size) { - 0 -> emptyList() - 1 -> listOf(this[0]) - else -> this.toMutableList() +public inline fun Array.sumByDouble(selector: (T) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += selector(element) } + return sum } /** - * Returns a [List] containing all elements. + * Returns the sum of all values produced by [selector] function applied to each element in the array. */ -public fun ShortArray.toList(): List { - return when (size) { - 0 -> emptyList() - 1 -> listOf(this[0]) - else -> this.toMutableList() +public inline fun ByteArray.sumByDouble(selector: (Byte) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += selector(element) } + return sum } /** - * Returns a [List] containing all elements. + * Returns the sum of all values produced by [selector] function applied to each element in the array. */ -public fun IntArray.toList(): List { - return when (size) { - 0 -> emptyList() - 1 -> listOf(this[0]) - else -> this.toMutableList() +public inline fun ShortArray.sumByDouble(selector: (Short) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += selector(element) } + return sum } /** - * Returns a [List] containing all elements. + * Returns the sum of all values produced by [selector] function applied to each element in the array. */ -public fun LongArray.toList(): List { - return when (size) { - 0 -> emptyList() - 1 -> listOf(this[0]) - else -> this.toMutableList() +public inline fun IntArray.sumByDouble(selector: (Int) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += selector(element) } + return sum } /** - * Returns a [List] containing all elements. + * Returns the sum of all values produced by [selector] function applied to each element in the array. */ -public fun FloatArray.toList(): List { - return when (size) { - 0 -> emptyList() - 1 -> listOf(this[0]) - else -> this.toMutableList() +public inline fun LongArray.sumByDouble(selector: (Long) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += selector(element) } + return sum } /** - * Returns a [List] containing all elements. + * Returns the sum of all values produced by [selector] function applied to each element in the array. */ -public fun DoubleArray.toList(): List { - return when (size) { - 0 -> emptyList() - 1 -> listOf(this[0]) - else -> this.toMutableList() +public inline fun FloatArray.sumByDouble(selector: (Float) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += selector(element) } + return sum } /** - * Returns a [List] containing all elements. + * Returns the sum of all values produced by [selector] function applied to each element in the array. */ -public fun BooleanArray.toList(): List { - return when (size) { - 0 -> emptyList() - 1 -> listOf(this[0]) - else -> this.toMutableList() +public inline fun DoubleArray.sumByDouble(selector: (Double) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += selector(element) } + return sum } /** - * Returns a [List] containing all elements. + * Returns the sum of all values produced by [selector] function applied to each element in the array. */ -public fun CharArray.toList(): List { - return when (size) { - 0 -> emptyList() - 1 -> listOf(this[0]) - else -> this.toMutableList() +public inline fun BooleanArray.sumByDouble(selector: (Boolean) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += selector(element) } + return sum } /** - * Returns a [MutableList] filled with all elements of this array. + * Returns the sum of all values produced by [selector] function applied to each element in the array. */ -public fun ByteArray.toMutableList(): MutableList { - val list = ArrayList(size) - for (item in this) list.add(item) +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..size-1) { + list.add(transform(this[i], other[i])) + } return list } /** - * Returns a [MutableList] filled with all elements of this array. + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public fun ShortArray.toMutableList(): MutableList { - val list = ArrayList(size) - for (item in this) list.add(item) +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..size-1) { + list.add(transform(this[i], other[i])) + } return list } /** - * Returns a [MutableList] filled with all elements of this array. + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public fun IntArray.toMutableList(): MutableList { - val list = ArrayList(size) - for (item in this) list.add(item) +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..size-1) { + list.add(transform(this[i], other[i])) + } return list } /** - * Returns a [MutableList] filled with all elements of this array. + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public fun LongArray.toMutableList(): MutableList { - val list = ArrayList(size) - for (item in this) list.add(item) +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..size-1) { + list.add(transform(this[i], other[i])) + } return list } /** - * Returns a [MutableList] filled with all elements of this array. + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public fun FloatArray.toMutableList(): MutableList { - val list = ArrayList(size) - for (item in this) list.add(item) +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..size-1) { + list.add(transform(this[i], other[i])) + } return list } /** - * Returns a [MutableList] filled with all elements of this array. + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public fun DoubleArray.toMutableList(): MutableList { - val list = ArrayList(size) - for (item in this) list.add(item) +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..size-1) { + list.add(transform(this[i], other[i])) + } return list } /** - * Returns a [MutableList] filled with all elements of this array. + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public fun BooleanArray.toMutableList(): MutableList { - val list = ArrayList(size) - for (item in this) list.add(item) +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..size-1) { + list.add(transform(this[i], other[i])) + } return list } /** - * Returns a [MutableList] filled with all elements of this array. + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ -public fun CharArray.toMutableList(): MutableList { - val list = ArrayList(size) - for (item in this) list.add(item) +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..size-1) { + 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..size-1) { + 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..size-1) { + 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..size-1) { + 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..size-1) { + 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..size-1) { + 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..size-1) { + 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..size-1) { + 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..size-1) { + 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..size-1) { + list.add(transform(this[i], other[i])) + } + return list +} + +/** + * 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 = copyOfUninitializedElements(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 = copyOfUninitializedElements(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 = copyOfUninitializedElements(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 = copyOfUninitializedElements(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 = copyOfUninitializedElements(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 = copyOfUninitializedElements(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 = copyOfUninitializedElements(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 = copyOfUninitializedElements(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 = copyOfUninitializedElements(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 = copyOfUninitializedElements(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 = copyOfUninitializedElements(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 = copyOfUninitializedElements(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 = copyOfUninitializedElements(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 = copyOfUninitializedElements(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 = copyOfUninitializedElements(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 = copyOfUninitializedElements(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 = copyOfUninitializedElements(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 = copyOfUninitializedElements(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 = copyOfUninitializedElements(thisSize + arraySize) + elements.copyRangeTo(result, 0, arraySize, thisSize) + 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 = copyOfUninitializedElements(thisSize + arraySize) + elements.copyRangeTo(result, 0, arraySize, thisSize) + 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 = copyOfUninitializedElements(thisSize + arraySize) + elements.copyRangeTo(result, 0, arraySize, thisSize) + 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 = copyOfUninitializedElements(thisSize + arraySize) + elements.copyRangeTo(result, 0, arraySize, thisSize) + 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 = copyOfUninitializedElements(thisSize + arraySize) + elements.copyRangeTo(result, 0, arraySize, thisSize) + 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 = copyOfUninitializedElements(thisSize + arraySize) + elements.copyRangeTo(result, 0, arraySize, thisSize) + 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 = copyOfUninitializedElements(thisSize + arraySize) + elements.copyRangeTo(result, 0, arraySize, thisSize) + 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 = copyOfUninitializedElements(thisSize + arraySize) + elements.copyRangeTo(result, 0, arraySize, thisSize) + 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 = copyOfUninitializedElements(thisSize + arraySize) + elements.copyRangeTo(result, 0, arraySize, thisSize) + 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) +} diff --git a/runtime/src/main/kotlin/kotlin/collections/ArrayUtil.kt b/runtime/src/main/kotlin/kotlin/collections/ArrayUtil.kt index 390712df1dd..5d09a8e9e75 100644 --- a/runtime/src/main/kotlin/kotlin/collections/ArrayUtil.kt +++ b/runtime/src/main/kotlin/kotlin/collections/ArrayUtil.kt @@ -31,18 +31,127 @@ internal fun arrayOfUninitializedElements(size: Int): Array { * Attempts to read _uninitialized_ values from this array work in implementation-dependent manner, * either throwing exception or returning some kind of implementation-specific default value. */ -fun Array.copyOfUninitializedElements(newSize: Int): Array { - val result = arrayOfUninitializedElements(newSize) - this.copyRangeTo(result, 0, if (newSize > this.size) this.size else newSize, 0) +fun Array.copyOfUninitializedElements(newSize: Int): Array = copyOfUninitializedElements(0, newSize) +fun ByteArray.copyOfUninitializedElements(newSize: Int): ByteArray = copyOfUninitializedElements(0, newSize) +fun ShortArray.copyOfUninitializedElements(newSize: Int): ShortArray = copyOfUninitializedElements(0, newSize) +fun IntArray.copyOfUninitializedElements(newSize: Int): IntArray = copyOfUninitializedElements(0, newSize) +fun LongArray.copyOfUninitializedElements(newSize: Int): LongArray = copyOfUninitializedElements(0, newSize) +fun CharArray.copyOfUninitializedElements(newSize: Int): CharArray = copyOfUninitializedElements(0, newSize) +fun FloatArray.copyOfUninitializedElements(newSize: Int): FloatArray = copyOfUninitializedElements(0, newSize) +fun DoubleArray.copyOfUninitializedElements(newSize: Int): DoubleArray = copyOfUninitializedElements(0, newSize) +fun BooleanArray.copyOfUninitializedElements(newSize: Int): BooleanArray = copyOfUninitializedElements(0, newSize) + +/** + * Returns a new array which is a copy of the original array with new elements filled with null values. + */ +fun Array.copyOfNulls(newSize: Int): Array = copyOfNulls(0, newSize) +fun Array.copyOfNulls(fromIndex: Int, toIndex: Int): Array { + val newSize = toIndex - fromIndex + if (newSize < 0) { + throw IllegalArgumentException("$fromIndex > $toIndex") + } + val result = arrayOfNulls(newSize) + copyRangeTo(result, fromIndex, if (toIndex > size) size else toIndex, 0) return result } -fun IntArray.copyOfUninitializedElements(newSize: Int): IntArray { - val result = IntArray(newSize) - this.copyRangeTo(result, 0, if (newSize > this.size) this.size else newSize, 0) +/** + * Returns new array which is a copy of the original array's range between [fromIndex] (inclusive) + * and [toIndex] (exclusive) with new elements filled with **lateinit** _uninitialized_ values. + * Attempts to read _uninitialized_ values from this array work in implementation-dependent manner, + * either throwing exception or returning some kind of implementation-specific default value. + */ +fun Array.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): Array { + val newSize = toIndex - fromIndex + if (newSize < 0) { + throw IllegalArgumentException("$fromIndex > $toIndex") + } + val result = arrayOfUninitializedElements(newSize) + copyRangeTo(result, fromIndex, if (toIndex > size) size else toIndex, 0) return result } +fun ByteArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): ByteArray { + val newSize = toIndex - fromIndex + if (newSize < 0) { + throw IllegalArgumentException("$fromIndex > $toIndex") + } + val result = ByteArray(newSize) + copyRangeTo(result, fromIndex, if (toIndex > size) size else toIndex, 0) + return result +} + +fun ShortArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): ShortArray { + val newSize = toIndex - fromIndex + if (newSize < 0) { + throw IllegalArgumentException("$fromIndex > $toIndex") + } + val result = ShortArray(newSize) + copyRangeTo(result, fromIndex, if (toIndex > size) size else toIndex, 0) + return result +} + +fun IntArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): IntArray { + val newSize = toIndex - fromIndex + if (newSize < 0) { + throw IllegalArgumentException("$fromIndex > $toIndex") + } + val result = IntArray(newSize) + copyRangeTo(result, fromIndex, if (toIndex > size) size else toIndex, 0) + return result +} + +fun LongArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): LongArray { + val newSize = toIndex - fromIndex + if (newSize < 0) { + throw IllegalArgumentException("$fromIndex > $toIndex") + } + val result = LongArray(newSize) + copyRangeTo(result, fromIndex, if (toIndex > size) size else toIndex, 0) + return result +} + +fun CharArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): CharArray { + val newSize = toIndex - fromIndex + if (newSize < 0) { + throw IllegalArgumentException("$fromIndex > $toIndex") + } + val result = CharArray(newSize) + copyRangeTo(result, fromIndex, if (toIndex > size) size else toIndex, 0) + return result +} + +fun FloatArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): FloatArray { + val newSize = toIndex - fromIndex + if (newSize < 0) { + throw IllegalArgumentException("$fromIndex > $toIndex") + } + val result = FloatArray(newSize) + copyRangeTo(result, fromIndex, if (toIndex > size) size else toIndex, 0) + return result +} + +fun DoubleArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): DoubleArray { + val newSize = toIndex - fromIndex + if (newSize < 0) { + throw IllegalArgumentException("$fromIndex > $toIndex") + } + val result = DoubleArray(newSize) + copyRangeTo(result, fromIndex, if (toIndex > size) size else toIndex, 0) + return result +} + +fun BooleanArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): BooleanArray { + val newSize = toIndex - fromIndex + if (newSize < 0) { + throw IllegalArgumentException("$fromIndex > $toIndex") + } + val result = BooleanArray(newSize) + copyRangeTo(result, fromIndex, if (toIndex > size) size else toIndex, 0) + return result +} + + /** * Resets an array element at a specified index to some implementation-specific _uninitialized_ value. * In particular, references stored in this element are released and become available for garbage collection. @@ -113,7 +222,7 @@ external private fun copyImpl(array: BooleanArray, fromIndex: Int, * Copies a range of array elements at a specified [fromIndex] (inclusive) to [toIndex] (exclusive) range of indices * to another [destination] array starting at [destinationIndex]. */ -fun Array.copyRangeTo(destination: Array, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { +fun Array.copyRangeTo(destination: Array, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { copyImpl(@Suppress("UNCHECKED_CAST") (this as Array), fromIndex, @Suppress("UNCHECKED_CAST") (destination as Array), destinationIndex, toIndex - fromIndex) diff --git a/runtime/src/main/kotlin/kotlin/collections/Collections.kt b/runtime/src/main/kotlin/kotlin/collections/Collections.kt index 880009bd2c4..fbd0798813e 100644 --- a/runtime/src/main/kotlin/kotlin/collections/Collections.kt +++ b/runtime/src/main/kotlin/kotlin/collections/Collections.kt @@ -290,12 +290,12 @@ public fun CharArray.asList(): List { } -fun Array.toSet(): Set { - val result = HashSet(this.size) - for (e in this) { - result.add(e) +@FixmeVariance +public fun > Iterable.toCollection(destination: C): C { + for (item in this) { + destination.add(item) } - return result + return destination } @Fixme diff --git a/runtime/src/main/kotlin/kotlin/collections/Maps.kt b/runtime/src/main/kotlin/kotlin/collections/Maps.kt index 036db3e9a16..1a0a8dd498c 100644 --- a/runtime/src/main/kotlin/kotlin/collections/Maps.kt +++ b/runtime/src/main/kotlin/kotlin/collections/Maps.kt @@ -104,6 +104,7 @@ public fun hashMapOf(vararg pairs: Pair): HashMap * to the Collection constructor for HashSet, (c.size()/.75f) + 1, but provides further optimisations for very small or * very large sizes, allows support non-collection classes, and provides consistency for all map based class construction. */ +@PublishedApi internal fun mapCapacity(expectedSize: Int): Int { if (expectedSize < 3) { return expectedSize + 1