From 60c2f0fe0b84b10b903e74f300ce089cb8dbbbcf Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Wed, 16 Sep 2015 15:51:20 +0300 Subject: [PATCH] Redistribute generated code between files. --- libraries/stdlib/src/generated/_Aggregates.kt | 2509 ---- libraries/stdlib/src/generated/_Arrays.kt | 11453 +++++++++++++++- .../src/generated/_ArraysToPrimitiveArrays.kt | 180 - .../stdlib/src/generated/_Collections.kt | 1860 +++ .../stdlib/src/generated/_Comparables.kt | 306 - libraries/stdlib/src/generated/_Elements.kt | 3444 ----- libraries/stdlib/src/generated/_Filtering.kt | 2023 --- libraries/stdlib/src/generated/_Generators.kt | 1112 -- libraries/stdlib/src/generated/_Guards.kt | 55 - libraries/stdlib/src/generated/_Mapping.kt | 1174 -- libraries/stdlib/src/generated/_Maps.kt | 190 + libraries/stdlib/src/generated/_Numeric.kt | 672 - libraries/stdlib/src/generated/_Ordering.kt | 1195 -- libraries/stdlib/src/generated/_Ranges.kt | 298 +- libraries/stdlib/src/generated/_Sequences.kt | 1336 +- libraries/stdlib/src/generated/_Sets.kt | 637 +- libraries/stdlib/src/generated/_Snapshots.kt | 981 -- libraries/stdlib/src/generated/_SpecialJVM.kt | 990 -- libraries/stdlib/src/generated/_Strings.kt | 874 +- 19 files changed, 15648 insertions(+), 15641 deletions(-) delete mode 100644 libraries/stdlib/src/generated/_Aggregates.kt delete mode 100644 libraries/stdlib/src/generated/_ArraysToPrimitiveArrays.kt create mode 100644 libraries/stdlib/src/generated/_Collections.kt delete mode 100644 libraries/stdlib/src/generated/_Comparables.kt delete mode 100644 libraries/stdlib/src/generated/_Elements.kt delete mode 100644 libraries/stdlib/src/generated/_Filtering.kt delete mode 100644 libraries/stdlib/src/generated/_Generators.kt delete mode 100644 libraries/stdlib/src/generated/_Guards.kt delete mode 100644 libraries/stdlib/src/generated/_Mapping.kt create mode 100644 libraries/stdlib/src/generated/_Maps.kt delete mode 100644 libraries/stdlib/src/generated/_Numeric.kt delete mode 100644 libraries/stdlib/src/generated/_Ordering.kt delete mode 100644 libraries/stdlib/src/generated/_Snapshots.kt delete mode 100644 libraries/stdlib/src/generated/_SpecialJVM.kt diff --git a/libraries/stdlib/src/generated/_Aggregates.kt b/libraries/stdlib/src/generated/_Aggregates.kt deleted file mode 100644 index 6dccea3be3b..00000000000 --- a/libraries/stdlib/src/generated/_Aggregates.kt +++ /dev/null @@ -1,2509 +0,0 @@ -package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// - -import kotlin.platform.* -import java.util.* - -import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js - -/** - * Returns `true` if all elements match the given [predicate]. - */ -public inline fun Array.all(predicate: (T) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if all elements match the given [predicate]. - */ -public inline fun BooleanArray.all(predicate: (Boolean) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if all elements match the given [predicate]. - */ -public inline fun ByteArray.all(predicate: (Byte) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if all elements match the given [predicate]. - */ -public inline fun CharArray.all(predicate: (Char) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if all elements match the given [predicate]. - */ -public inline fun DoubleArray.all(predicate: (Double) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if all elements match the given [predicate]. - */ -public inline fun FloatArray.all(predicate: (Float) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if all elements match the given [predicate]. - */ -public inline fun IntArray.all(predicate: (Int) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if all elements match the given [predicate]. - */ -public inline fun LongArray.all(predicate: (Long) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if all elements match the given [predicate]. - */ -public inline fun ShortArray.all(predicate: (Short) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if all elements match the given [predicate]. - */ -public inline fun Iterable.all(predicate: (T) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if all elements match the given [predicate]. - */ -public inline fun Map.all(predicate: (Map.Entry) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if all elements match the given [predicate]. - */ -public inline fun Sequence.all(predicate: (T) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if all elements match the given [predicate]. - */ -public inline fun String.all(predicate: (Char) -> Boolean): Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns `true` if collection has at least one element. - */ -public fun Array.any(): Boolean { - for (element in this) return true - return false -} - -/** - * Returns `true` if collection has at least one element. - */ -public fun BooleanArray.any(): Boolean { - for (element in this) return true - return false -} - -/** - * Returns `true` if collection has at least one element. - */ -public fun ByteArray.any(): Boolean { - for (element in this) return true - return false -} - -/** - * Returns `true` if collection has at least one element. - */ -public fun CharArray.any(): Boolean { - for (element in this) return true - return false -} - -/** - * Returns `true` if collection has at least one element. - */ -public fun DoubleArray.any(): Boolean { - for (element in this) return true - return false -} - -/** - * Returns `true` if collection has at least one element. - */ -public fun FloatArray.any(): Boolean { - for (element in this) return true - return false -} - -/** - * Returns `true` if collection has at least one element. - */ -public fun IntArray.any(): Boolean { - for (element in this) return true - return false -} - -/** - * Returns `true` if collection has at least one element. - */ -public fun LongArray.any(): Boolean { - for (element in this) return true - return false -} - -/** - * Returns `true` if collection has at least one element. - */ -public fun ShortArray.any(): Boolean { - for (element in this) return true - return false -} - -/** - * Returns `true` if collection has at least one element. - */ -public fun Iterable.any(): Boolean { - for (element in this) return true - return false -} - -/** - * Returns `true` if collection has at least one element. - */ -public fun Map.any(): Boolean { - for (element in this) return true - return false -} - -/** - * Returns `true` if collection has at least one element. - */ -public fun Sequence.any(): Boolean { - for (element in this) return true - return false -} - -/** - * Returns `true` if collection has at least one element. - */ -public fun String.any(): Boolean { - for (element in this) return true - return false -} - -/** - * Returns `true` if at least one element matches the given [predicate]. - */ -public inline fun Array.any(predicate: (T) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return true - return false -} - -/** - * Returns `true` if at least one element matches the given [predicate]. - */ -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 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 CharArray.any(predicate: (Char) -> 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 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 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 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 Iterable.any(predicate: (T) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return true - return false -} - -/** - * Returns `true` if at least one element matches the given [predicate]. - */ -public inline fun Map.any(predicate: (Map.Entry) -> 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 Sequence.any(predicate: (T) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return true - return false -} - -/** - * Returns `true` if at least one element matches the given [predicate]. - */ -public inline fun String.any(predicate: (Char) -> Boolean): Boolean { - for (element in this) if (predicate(element)) return true - return false -} - -/** - * Returns the number of elements in this collection. - */ -public fun Array.count(): Int { - return size() -} - -/** - * Returns the number of elements in this collection. - */ -public fun BooleanArray.count(): Int { - return size() -} - -/** - * Returns the number of elements in this collection. - */ -public fun ByteArray.count(): Int { - return size() -} - -/** - * Returns the number of elements in this collection. - */ -public fun CharArray.count(): Int { - return size() -} - -/** - * Returns the number of elements in this collection. - */ -public fun DoubleArray.count(): Int { - return size() -} - -/** - * Returns the number of elements in this collection. - */ -public fun FloatArray.count(): Int { - return size() -} - -/** - * Returns the number of elements in this collection. - */ -public fun IntArray.count(): Int { - return size() -} - -/** - * Returns the number of elements in this collection. - */ -public fun LongArray.count(): Int { - return size() -} - -/** - * Returns the number of elements in this collection. - */ -public fun ShortArray.count(): Int { - return size() -} - -/** - * Returns the number of elements in this collection. - */ -public fun Collection.count(): Int { - return size() -} - -/** - * Returns the number of elements in this collection. - */ -public fun Iterable.count(): Int { - var count = 0 - for (element in this) count++ - return count -} - -/** - * Returns the number of elements in this collection. - */ -public fun Map.count(): Int { - return size() -} - -/** - * Returns the number of elements in this collection. - */ -public fun Sequence.count(): Int { - var count = 0 - for (element in this) count++ - return count -} - -/** - * Returns the length of this string. - */ -public fun String.count(): Int { - return length() -} - -/** - * 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 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 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 CharArray.count(predicate: (Char) -> 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 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 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 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 Iterable.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 Map.count(predicate: (Map.Entry) -> 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 Sequence.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 String.count(predicate: (Char) -> Boolean): Int { - var count = 0 - for (element in this) if (predicate(element)) count++ - return count -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun Array.fold(initial: R, operation: (R, T) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun BooleanArray.fold(initial: R, operation: (R, Boolean) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun ByteArray.fold(initial: R, operation: (R, Byte) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun CharArray.fold(initial: R, operation: (R, Char) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun DoubleArray.fold(initial: R, operation: (R, Double) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun FloatArray.fold(initial: R, operation: (R, Float) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun IntArray.fold(initial: R, operation: (R, Int) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun LongArray.fold(initial: R, operation: (R, Long) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun ShortArray.fold(initial: R, operation: (R, Short) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun Iterable.fold(initial: R, operation: (R, T) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun Sequence.fold(initial: R, operation: (R, T) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun String.fold(initial: R, operation: (R, Char) -> R): R { - var accumulator = initial - for (element in this) accumulator = operation(accumulator, element) - return accumulator -} - -/** - * Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun Array.foldRight(initial: R, operation: (T, 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, 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, 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, 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, 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, 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, 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, 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, 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 List.foldRight(initial: R, operation: (T, 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 String.foldRight(initial: R, operation: (Char, R) -> R): R { - var index = lastIndex - var accumulator = initial - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Performs the given [operation] on each element. - */ -public inline fun Array.forEach(operation: (T) -> Unit): Unit { - for (element in this) operation(element) -} - -/** - * Performs the given [operation] on each element. - */ -public inline fun BooleanArray.forEach(operation: (Boolean) -> Unit): Unit { - for (element in this) operation(element) -} - -/** - * Performs the given [operation] on each element. - */ -public inline fun ByteArray.forEach(operation: (Byte) -> Unit): Unit { - for (element in this) operation(element) -} - -/** - * Performs the given [operation] on each element. - */ -public inline fun CharArray.forEach(operation: (Char) -> Unit): Unit { - for (element in this) operation(element) -} - -/** - * Performs the given [operation] on each element. - */ -public inline fun DoubleArray.forEach(operation: (Double) -> Unit): Unit { - for (element in this) operation(element) -} - -/** - * Performs the given [operation] on each element. - */ -public inline fun FloatArray.forEach(operation: (Float) -> Unit): Unit { - for (element in this) operation(element) -} - -/** - * Performs the given [operation] on each element. - */ -public inline fun IntArray.forEach(operation: (Int) -> Unit): Unit { - for (element in this) operation(element) -} - -/** - * Performs the given [operation] on each element. - */ -public inline fun LongArray.forEach(operation: (Long) -> Unit): Unit { - for (element in this) operation(element) -} - -/** - * Performs the given [operation] on each element. - */ -public inline fun ShortArray.forEach(operation: (Short) -> Unit): Unit { - for (element in this) operation(element) -} - -/** - * Performs the given [operation] on each element. - */ -public inline fun Iterable.forEach(operation: (T) -> Unit): Unit { - for (element in this) operation(element) -} - -/** - * Performs the given [operation] on each element. - */ -public inline fun Map.forEach(operation: (Map.Entry) -> Unit): Unit { - for (element in this) operation(element) -} - -/** - * Performs the given [operation] on each element. - */ -public inline fun Sequence.forEach(operation: (T) -> Unit): Unit { - for (element in this) operation(element) -} - -/** - * Performs the given [operation] on each element. - */ -public inline fun String.forEach(operation: (Char) -> Unit): Unit { - for (element in this) operation(element) -} - -/** - * Performs the given [operation] on each element, providing sequential index with the element. - */ -public inline fun Array.forEachIndexed(operation: (Int, T) -> Unit): Unit { - var index = 0 - for (item in this) operation(index++, item) -} - -/** - * Performs the given [operation] on each element, providing sequential index with the element. - */ -public inline fun BooleanArray.forEachIndexed(operation: (Int, Boolean) -> Unit): Unit { - var index = 0 - for (item in this) operation(index++, item) -} - -/** - * Performs the given [operation] on each element, providing sequential index with the element. - */ -public inline fun ByteArray.forEachIndexed(operation: (Int, Byte) -> Unit): Unit { - var index = 0 - for (item in this) operation(index++, item) -} - -/** - * Performs the given [operation] on each element, providing sequential index with the element. - */ -public inline fun CharArray.forEachIndexed(operation: (Int, Char) -> Unit): Unit { - var index = 0 - for (item in this) operation(index++, item) -} - -/** - * Performs the given [operation] on each element, providing sequential index with the element. - */ -public inline fun DoubleArray.forEachIndexed(operation: (Int, Double) -> Unit): Unit { - var index = 0 - for (item in this) operation(index++, item) -} - -/** - * Performs the given [operation] on each element, providing sequential index with the element. - */ -public inline fun FloatArray.forEachIndexed(operation: (Int, Float) -> Unit): Unit { - var index = 0 - for (item in this) operation(index++, item) -} - -/** - * Performs the given [operation] on each element, providing sequential index with the element. - */ -public inline fun IntArray.forEachIndexed(operation: (Int, Int) -> Unit): Unit { - var index = 0 - for (item in this) operation(index++, item) -} - -/** - * Performs the given [operation] on each element, providing sequential index with the element. - */ -public inline fun LongArray.forEachIndexed(operation: (Int, Long) -> Unit): Unit { - var index = 0 - for (item in this) operation(index++, item) -} - -/** - * Performs the given [operation] on each element, providing sequential index with the element. - */ -public inline fun ShortArray.forEachIndexed(operation: (Int, Short) -> Unit): Unit { - var index = 0 - for (item in this) operation(index++, item) -} - -/** - * Performs the given [operation] on each element, providing sequential index with the element. - */ -public inline fun Iterable.forEachIndexed(operation: (Int, T) -> Unit): Unit { - var index = 0 - for (item in this) operation(index++, item) -} - -/** - * Performs the given [operation] on each element, providing sequential index with the element. - */ -public inline fun Sequence.forEachIndexed(operation: (Int, T) -> Unit): Unit { - var index = 0 - for (item in this) operation(index++, item) -} - -/** - * Performs the given [operation] on each element, providing sequential index with the element. - */ -public inline fun String.forEachIndexed(operation: (Int, Char) -> Unit): Unit { - var index = 0 - for (item in this) operation(index++, item) -} - -/** - * 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 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 largest element or `null` if there are no elements. - */ -public fun DoubleArray.max(): Double? { - 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 FloatArray.max(): Float? { - 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. - */ -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 > Iterable.max(): T? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var max = iterator.next() - while (iterator.hasNext()) { - val e = iterator.next() - if (max < e) max = e - } - return max -} - -/** - * Returns the largest element or `null` if there are no elements. - */ -public fun > Sequence.max(): T? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var max = iterator.next() - while (iterator.hasNext()) { - val e = iterator.next() - if (max < e) max = e - } - return max -} - -/** - * Returns the largest element or `null` if there are no elements. - */ -public fun String.max(): Char? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var max = iterator.next() - while (iterator.hasNext()) { - val e = iterator.next() - if (max < e) max = e - } - return max -} - -/** - * Returns the first element yielding the largest value of the given function or `null` if there are no elements. - */ -public inline fun , T : Any> Array.maxBy(f: (T) -> R): T? { - if (isEmpty()) return null - var maxElem = this[0] - var maxValue = f(maxElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = f(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(f: (Boolean) -> R): Boolean? { - if (isEmpty()) return null - var maxElem = this[0] - var maxValue = f(maxElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = f(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(f: (Byte) -> R): Byte? { - if (isEmpty()) return null - var maxElem = this[0] - var maxValue = f(maxElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = f(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(f: (Char) -> R): Char? { - if (isEmpty()) return null - var maxElem = this[0] - var maxValue = f(maxElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = f(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(f: (Double) -> R): Double? { - if (isEmpty()) return null - var maxElem = this[0] - var maxValue = f(maxElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = f(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(f: (Float) -> R): Float? { - if (isEmpty()) return null - var maxElem = this[0] - var maxValue = f(maxElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = f(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(f: (Int) -> R): Int? { - if (isEmpty()) return null - var maxElem = this[0] - var maxValue = f(maxElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = f(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(f: (Long) -> R): Long? { - if (isEmpty()) return null - var maxElem = this[0] - var maxValue = f(maxElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = f(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(f: (Short) -> R): Short? { - if (isEmpty()) return null - var maxElem = this[0] - var maxValue = f(maxElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = f(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 , T : Any> Iterable.maxBy(f: (T) -> R): T? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var maxElem = iterator.next() - var maxValue = f(maxElem) - while (iterator.hasNext()) { - val e = iterator.next() - val v = f(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 , T : Any> Sequence.maxBy(f: (T) -> R): T? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var maxElem = iterator.next() - var maxValue = f(maxElem) - while (iterator.hasNext()) { - val e = iterator.next() - val v = f(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 > String.maxBy(f: (Char) -> R): Char? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var maxElem = iterator.next() - var maxValue = f(maxElem) - while (iterator.hasNext()) { - val e = iterator.next() - val v = f(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 > Map.maxBy(f: (Map.Entry) -> R): Map.Entry? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var maxElem = iterator.next() - var maxValue = f(maxElem) - while (iterator.hasNext()) { - val e = iterator.next() - val v = f(e) - if (maxValue < v) { - maxElem = e - maxValue = v - } - } - return maxElem -} - -/** - * 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 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 smallest element or `null` if there are no elements. - */ -public fun DoubleArray.min(): Double? { - 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 FloatArray.min(): Float? { - 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. - */ -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 > Iterable.min(): T? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var min = iterator.next() - while (iterator.hasNext()) { - val e = iterator.next() - if (min > e) min = e - } - return min -} - -/** - * Returns the smallest element or `null` if there are no elements. - */ -public fun > Sequence.min(): T? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var min = iterator.next() - while (iterator.hasNext()) { - val e = iterator.next() - if (min > e) min = e - } - return min -} - -/** - * Returns the smallest element or `null` if there are no elements. - */ -public fun String.min(): Char? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var min = iterator.next() - while (iterator.hasNext()) { - val e = iterator.next() - if (min > e) min = e - } - return min -} - -/** - * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. - */ -public inline fun , T : Any> Array.minBy(f: (T) -> R): T? { - if (size() == 0) return null - var minElem = this[0] - var minValue = f(minElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = f(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(f: (Boolean) -> R): Boolean? { - if (size() == 0) return null - var minElem = this[0] - var minValue = f(minElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = f(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(f: (Byte) -> R): Byte? { - if (size() == 0) return null - var minElem = this[0] - var minValue = f(minElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = f(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(f: (Char) -> R): Char? { - if (size() == 0) return null - var minElem = this[0] - var minValue = f(minElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = f(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(f: (Double) -> R): Double? { - if (size() == 0) return null - var minElem = this[0] - var minValue = f(minElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = f(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(f: (Float) -> R): Float? { - if (size() == 0) return null - var minElem = this[0] - var minValue = f(minElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = f(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(f: (Int) -> R): Int? { - if (size() == 0) return null - var minElem = this[0] - var minValue = f(minElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = f(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(f: (Long) -> R): Long? { - if (size() == 0) return null - var minElem = this[0] - var minValue = f(minElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = f(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(f: (Short) -> R): Short? { - if (size() == 0) return null - var minElem = this[0] - var minValue = f(minElem) - for (i in 1..lastIndex) { - val e = this[i] - val v = f(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 , T : Any> Iterable.minBy(f: (T) -> R): T? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var minElem = iterator.next() - var minValue = f(minElem) - while (iterator.hasNext()) { - val e = iterator.next() - val v = f(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 , T : Any> Sequence.minBy(f: (T) -> R): T? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var minElem = iterator.next() - var minValue = f(minElem) - while (iterator.hasNext()) { - val e = iterator.next() - val v = f(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 > String.minBy(f: (Char) -> R): Char? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var minElem = iterator.next() - var minValue = f(minElem) - while (iterator.hasNext()) { - val e = iterator.next() - val v = f(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 > Map.minBy(f: (Map.Entry) -> R): Map.Entry? { - val iterator = iterator() - if (!iterator.hasNext()) return null - var minElem = iterator.next() - var minValue = f(minElem) - while (iterator.hasNext()) { - val e = iterator.next() - val v = f(e) - if (minValue > v) { - minElem = e - minValue = v - } - } - return minElem -} - -/** - * Returns `true` if collection has no elements. - */ -public fun Array.none(): Boolean { - for (element in this) return false - return true -} - -/** - * Returns `true` if collection has no elements. - */ -public fun BooleanArray.none(): Boolean { - for (element in this) return false - return true -} - -/** - * Returns `true` if collection has no elements. - */ -public fun ByteArray.none(): Boolean { - for (element in this) return false - return true -} - -/** - * Returns `true` if collection has no elements. - */ -public fun CharArray.none(): Boolean { - for (element in this) return false - return true -} - -/** - * Returns `true` if collection has no elements. - */ -public fun DoubleArray.none(): Boolean { - for (element in this) return false - return true -} - -/** - * Returns `true` if collection has no elements. - */ -public fun FloatArray.none(): Boolean { - for (element in this) return false - return true -} - -/** - * Returns `true` if collection has no elements. - */ -public fun IntArray.none(): Boolean { - for (element in this) return false - return true -} - -/** - * Returns `true` if collection has no elements. - */ -public fun LongArray.none(): Boolean { - for (element in this) return false - return true -} - -/** - * Returns `true` if collection has no elements. - */ -public fun ShortArray.none(): Boolean { - for (element in this) return false - return true -} - -/** - * Returns `true` if collection has no elements. - */ -public fun Iterable.none(): Boolean { - for (element in this) return false - return true -} - -/** - * Returns `true` if collection has no elements. - */ -public fun Map.none(): Boolean { - for (element in this) return false - return true -} - -/** - * Returns `true` if collection has no elements. - */ -public fun Sequence.none(): Boolean { - for (element in this) return false - return true -} - -/** - * Returns `true` if collection has no elements. - */ -public fun String.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 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 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 CharArray.none(predicate: (Char) -> 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 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 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 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 Iterable.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 Map.none(predicate: (Map.Entry) -> 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 Sequence.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 String.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: (S, T) -> S): S { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") - var accumulator: S = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(accumulator, iterator.next()) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun Iterable.reduce(operation: (S, T) -> S): S { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") - var accumulator: S = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(accumulator, iterator.next()) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun Sequence.reduce(operation: (S, T) -> S): S { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") - var accumulator: S = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(accumulator, iterator.next()) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun BooleanArray.reduce(operation: (Boolean, Boolean) -> Boolean): Boolean { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") - var accumulator = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(accumulator, iterator.next()) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun ByteArray.reduce(operation: (Byte, Byte) -> Byte): Byte { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") - var accumulator = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(accumulator, iterator.next()) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun CharArray.reduce(operation: (Char, Char) -> Char): Char { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") - var accumulator = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(accumulator, iterator.next()) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun DoubleArray.reduce(operation: (Double, Double) -> Double): Double { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") - var accumulator = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(accumulator, iterator.next()) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun FloatArray.reduce(operation: (Float, Float) -> Float): Float { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") - var accumulator = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(accumulator, iterator.next()) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun IntArray.reduce(operation: (Int, Int) -> Int): Int { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") - var accumulator = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(accumulator, iterator.next()) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun LongArray.reduce(operation: (Long, Long) -> Long): Long { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") - var accumulator = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(accumulator, iterator.next()) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun ShortArray.reduce(operation: (Short, Short) -> Short): Short { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") - var accumulator = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(accumulator, iterator.next()) - } - return accumulator -} - -/** - * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. - */ -public inline fun String.reduce(operation: (Char, Char) -> Char): Char { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") - var accumulator = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(accumulator, iterator.next()) - } - return accumulator -} - -/** - * Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. - */ -public inline fun Array.reduceRight(operation: (T, S) -> S): S { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable 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 List.reduceRight(operation: (T, S) -> S): S { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable 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 BooleanArray.reduceRight(operation: (Boolean, Boolean) -> Boolean): Boolean { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable 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 ByteArray.reduceRight(operation: (Byte, Byte) -> Byte): Byte { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable 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, Char) -> Char): Char { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable 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, Double) -> Double): Double { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable 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, Float) -> Float): Float { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable 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, Int) -> Int): Int { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable 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, Long) -> Long): Long { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable 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, Short) -> Short): Short { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable 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 String.reduceRight(operation: (Char, Char) -> Char): Char { - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(get(index--), accumulator) - } - return accumulator -} - -/** - * Returns the sum of all values produced by [transform] function from elements in the collection. - */ -public inline fun Array.sumBy(transform: (T) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += transform(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [transform] function from elements in the collection. - */ -public inline fun BooleanArray.sumBy(transform: (Boolean) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += transform(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [transform] function from elements in the collection. - */ -public inline fun ByteArray.sumBy(transform: (Byte) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += transform(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [transform] function from elements in the collection. - */ -public inline fun CharArray.sumBy(transform: (Char) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += transform(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [transform] function from elements in the collection. - */ -public inline fun DoubleArray.sumBy(transform: (Double) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += transform(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [transform] function from elements in the collection. - */ -public inline fun FloatArray.sumBy(transform: (Float) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += transform(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [transform] function from elements in the collection. - */ -public inline fun IntArray.sumBy(transform: (Int) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += transform(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [transform] function from elements in the collection. - */ -public inline fun LongArray.sumBy(transform: (Long) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += transform(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [transform] function from elements in the collection. - */ -public inline fun ShortArray.sumBy(transform: (Short) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += transform(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [transform] function from elements in the collection. - */ -public inline fun Iterable.sumBy(transform: (T) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += transform(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [transform] function from elements in the collection. - */ -public inline fun Sequence.sumBy(transform: (T) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += transform(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [transform] function from characters in the string. - */ -public inline fun String.sumBy(transform: (Char) -> Int): Int { - var sum: Int = 0 - for (element in this) { - sum += transform(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [transform] function from elements in the collection. - */ -public inline fun Array.sumByDouble(transform: (T) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += transform(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [transform] function from elements in the collection. - */ -public inline fun BooleanArray.sumByDouble(transform: (Boolean) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += transform(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [transform] function from elements in the collection. - */ -public inline fun ByteArray.sumByDouble(transform: (Byte) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += transform(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [transform] function from elements in the collection. - */ -public inline fun CharArray.sumByDouble(transform: (Char) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += transform(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [transform] function from elements in the collection. - */ -public inline fun DoubleArray.sumByDouble(transform: (Double) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += transform(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [transform] function from elements in the collection. - */ -public inline fun FloatArray.sumByDouble(transform: (Float) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += transform(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [transform] function from elements in the collection. - */ -public inline fun IntArray.sumByDouble(transform: (Int) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += transform(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [transform] function from elements in the collection. - */ -public inline fun LongArray.sumByDouble(transform: (Long) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += transform(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [transform] function from elements in the collection. - */ -public inline fun ShortArray.sumByDouble(transform: (Short) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += transform(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [transform] function from elements in the collection. - */ -public inline fun Iterable.sumByDouble(transform: (T) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += transform(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [transform] function from elements in the collection. - */ -public inline fun Sequence.sumByDouble(transform: (T) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += transform(element) - } - return sum -} - -/** - * Returns the sum of all values produced by [transform] function from characters in the string. - */ -public inline fun String.sumByDouble(transform: (Char) -> Double): Double { - var sum: Double = 0.0 - for (element in this) { - sum += transform(element) - } - return sum -} - diff --git a/libraries/stdlib/src/generated/_Arrays.kt b/libraries/stdlib/src/generated/_Arrays.kt index 7c2d0602365..4f2d04daef0 100644 --- a/libraries/stdlib/src/generated/_Arrays.kt +++ b/libraries/stdlib/src/generated/_Arrays.kt @@ -1,3 +1,6 @@ +@file:kotlin.jvm.JvmMultifileClass +@file:kotlin.jvm.JvmName("ArraysKt") + package kotlin // @@ -5,11 +8,5052 @@ package kotlin // See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib // -import kotlin.platform.* import java.util.* import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js +/** + * Returns 1st *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun Array.component1(): T { + return get(0) +} + +/** + * Returns 1st *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun BooleanArray.component1(): Boolean { + return get(0) +} + +/** + * Returns 1st *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun ByteArray.component1(): Byte { + return get(0) +} + +/** + * Returns 1st *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun CharArray.component1(): Char { + return get(0) +} + +/** + * Returns 1st *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun DoubleArray.component1(): Double { + return get(0) +} + +/** + * Returns 1st *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun FloatArray.component1(): Float { + return get(0) +} + +/** + * Returns 1st *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun IntArray.component1(): Int { + return get(0) +} + +/** + * Returns 1st *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun LongArray.component1(): Long { + return get(0) +} + +/** + * Returns 1st *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun ShortArray.component1(): Short { + return get(0) +} + +/** + * Returns 2nd *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun Array.component2(): T { + return get(1) +} + +/** + * Returns 2nd *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun BooleanArray.component2(): Boolean { + return get(1) +} + +/** + * Returns 2nd *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun ByteArray.component2(): Byte { + return get(1) +} + +/** + * Returns 2nd *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun CharArray.component2(): Char { + return get(1) +} + +/** + * Returns 2nd *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun DoubleArray.component2(): Double { + return get(1) +} + +/** + * Returns 2nd *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun FloatArray.component2(): Float { + return get(1) +} + +/** + * Returns 2nd *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun IntArray.component2(): Int { + return get(1) +} + +/** + * Returns 2nd *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun LongArray.component2(): Long { + return get(1) +} + +/** + * Returns 2nd *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun ShortArray.component2(): Short { + return get(1) +} + +/** + * Returns 3rd *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun Array.component3(): T { + return get(2) +} + +/** + * Returns 3rd *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun BooleanArray.component3(): Boolean { + return get(2) +} + +/** + * Returns 3rd *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun ByteArray.component3(): Byte { + return get(2) +} + +/** + * Returns 3rd *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun CharArray.component3(): Char { + return get(2) +} + +/** + * Returns 3rd *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun DoubleArray.component3(): Double { + return get(2) +} + +/** + * Returns 3rd *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun FloatArray.component3(): Float { + return get(2) +} + +/** + * Returns 3rd *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun IntArray.component3(): Int { + return get(2) +} + +/** + * Returns 3rd *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun LongArray.component3(): Long { + return get(2) +} + +/** + * Returns 3rd *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun ShortArray.component3(): Short { + return get(2) +} + +/** + * Returns 4th *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun Array.component4(): T { + return get(3) +} + +/** + * Returns 4th *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun BooleanArray.component4(): Boolean { + return get(3) +} + +/** + * Returns 4th *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun ByteArray.component4(): Byte { + return get(3) +} + +/** + * Returns 4th *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun CharArray.component4(): Char { + return get(3) +} + +/** + * Returns 4th *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun DoubleArray.component4(): Double { + return get(3) +} + +/** + * Returns 4th *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun FloatArray.component4(): Float { + return get(3) +} + +/** + * Returns 4th *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun IntArray.component4(): Int { + return get(3) +} + +/** + * Returns 4th *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun LongArray.component4(): Long { + return get(3) +} + +/** + * Returns 4th *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun ShortArray.component4(): Short { + return get(3) +} + +/** + * Returns 5th *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun Array.component5(): T { + return get(4) +} + +/** + * Returns 5th *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun BooleanArray.component5(): Boolean { + return get(4) +} + +/** + * Returns 5th *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun ByteArray.component5(): Byte { + return get(4) +} + +/** + * Returns 5th *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun CharArray.component5(): Char { + return get(4) +} + +/** + * Returns 5th *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun DoubleArray.component5(): Double { + return get(4) +} + +/** + * Returns 5th *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun FloatArray.component5(): Float { + return get(4) +} + +/** + * Returns 5th *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun IntArray.component5(): Int { + return get(4) +} + +/** + * Returns 5th *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun LongArray.component5(): Long { + return get(4) +} + +/** + * Returns 5th *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun ShortArray.component5(): Short { + return get(4) +} + +/** + * Returns `true` if [element] is found in the collection. + */ +public operator fun Array.contains(element: T): Boolean { + return indexOf(element) >= 0 +} + +/** + * Returns `true` if [element] is found in the collection. + */ +public operator fun BooleanArray.contains(element: Boolean): Boolean { + return indexOf(element) >= 0 +} + +/** + * Returns `true` if [element] is found in the collection. + */ +public operator fun ByteArray.contains(element: Byte): Boolean { + return indexOf(element) >= 0 +} + +/** + * Returns `true` if [element] is found in the collection. + */ +public operator fun CharArray.contains(element: Char): Boolean { + return indexOf(element) >= 0 +} + +/** + * Returns `true` if [element] is found in the collection. + */ +public operator fun DoubleArray.contains(element: Double): Boolean { + return indexOf(element) >= 0 +} + +/** + * Returns `true` if [element] is found in the collection. + */ +public operator fun FloatArray.contains(element: Float): Boolean { + return indexOf(element) >= 0 +} + +/** + * Returns `true` if [element] is found in the collection. + */ +public operator fun IntArray.contains(element: Int): Boolean { + return indexOf(element) >= 0 +} + +/** + * Returns `true` if [element] is found in the collection. + */ +public operator fun LongArray.contains(element: Long): Boolean { + return indexOf(element) >= 0 +} + +/** + * Returns `true` if [element] is found in the collection. + */ +public operator fun ShortArray.contains(element: Short): Boolean { + return indexOf(element) >= 0 +} + +/** + * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. + */ +public fun Array.elementAt(index: Int): T { + return get(index) +} + +/** + * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. + */ +public fun BooleanArray.elementAt(index: Int): Boolean { + return get(index) +} + +/** + * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. + */ +public fun ByteArray.elementAt(index: Int): Byte { + return get(index) +} + +/** + * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. + */ +public fun CharArray.elementAt(index: Int): Char { + return get(index) +} + +/** + * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. + */ +public fun DoubleArray.elementAt(index: Int): Double { + return get(index) +} + +/** + * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. + */ +public fun FloatArray.elementAt(index: Int): Float { + return get(index) +} + +/** + * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. + */ +public fun IntArray.elementAt(index: Int): Int { + return get(index) +} + +/** + * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. + */ +public fun LongArray.elementAt(index: Int): Long { + return get(index) +} + +/** + * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. + */ +public fun ShortArray.elementAt(index: Int): Short { + return get(index) +} + +/** + * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. + */ +public 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 collection. + */ +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 collection. + */ +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 collection. + */ +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 the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. + */ +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 collection. + */ +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 collection. + */ +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 collection. + */ +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 collection. + */ +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 `null` if the [index] is out of bounds of this collection. + */ +public fun Array.elementAtOrNull(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 collection. + */ +public fun BooleanArray.elementAtOrNull(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 collection. + */ +public fun ByteArray.elementAtOrNull(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 collection. + */ +public fun CharArray.elementAtOrNull(index: Int): Char? { + 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 collection. + */ +public fun DoubleArray.elementAtOrNull(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 collection. + */ +public fun FloatArray.elementAtOrNull(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 collection. + */ +public fun IntArray.elementAtOrNull(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 collection. + */ +public fun LongArray.elementAtOrNull(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 collection. + */ +public fun ShortArray.elementAtOrNull(index: Int): Short? { + return if (index >= 0 && index <= lastIndex) get(index) else null +} + +/** + * Returns the first element matching the given [predicate], or `null` if element was not found. + */ +public inline fun Array.find(predicate: (T) -> Boolean): T? { + return firstOrNull(predicate) +} + +/** + * Returns the first element matching the given [predicate], or `null` if element was not found. + */ +public inline fun BooleanArray.find(predicate: (Boolean) -> Boolean): Boolean? { + return firstOrNull(predicate) +} + +/** + * Returns the first element matching the given [predicate], or `null` if element was not found. + */ +public inline fun ByteArray.find(predicate: (Byte) -> Boolean): Byte? { + return firstOrNull(predicate) +} + +/** + * Returns the first element matching the given [predicate], or `null` if element was not found. + */ +public inline fun CharArray.find(predicate: (Char) -> Boolean): Char? { + return firstOrNull(predicate) +} + +/** + * Returns the first element matching the given [predicate], or `null` if element was not found. + */ +public inline fun DoubleArray.find(predicate: (Double) -> Boolean): Double? { + return firstOrNull(predicate) +} + +/** + * Returns the first element matching the given [predicate], or `null` if element was not found. + */ +public inline fun FloatArray.find(predicate: (Float) -> Boolean): Float? { + return firstOrNull(predicate) +} + +/** + * Returns the first element matching the given [predicate], or `null` if element was not found. + */ +public inline fun IntArray.find(predicate: (Int) -> Boolean): Int? { + return firstOrNull(predicate) +} + +/** + * Returns the first element matching the given [predicate], or `null` if element was not found. + */ +public inline fun LongArray.find(predicate: (Long) -> Boolean): Long? { + return firstOrNull(predicate) +} + +/** + * Returns the first element matching the given [predicate], or `null` if element was not found. + */ +public inline fun ShortArray.find(predicate: (Short) -> Boolean): Short? { + return firstOrNull(predicate) +} + +/** + * Returns the last element matching the given [predicate], or `null` if no such element was found. + */ +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. + */ +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. + */ +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. + */ +public inline fun CharArray.findLast(predicate: (Char) -> Boolean): Char? { + return lastOrNull(predicate) +} + +/** + * Returns the last element matching the given [predicate], or `null` if no such element was found. + */ +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. + */ +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. + */ +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. + */ +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. + */ +public inline fun ShortArray.findLast(predicate: (Short) -> Boolean): Short? { + return lastOrNull(predicate) +} + +/** + * Returns first element. + * @throws [NoSuchElementException] if the collection is empty. + */ +public fun Array.first(): T { + if (isEmpty()) + throw NoSuchElementException("Collection is empty.") + return this[0] +} + +/** + * Returns first element. + * @throws [NoSuchElementException] if the collection is empty. + */ +public fun BooleanArray.first(): Boolean { + if (isEmpty()) + throw NoSuchElementException("Collection is empty.") + return this[0] +} + +/** + * Returns first element. + * @throws [NoSuchElementException] if the collection is empty. + */ +public fun ByteArray.first(): Byte { + if (isEmpty()) + throw NoSuchElementException("Collection is empty.") + return this[0] +} + +/** + * Returns first element. + * @throws [NoSuchElementException] if the collection is empty. + */ +public fun CharArray.first(): Char { + if (isEmpty()) + throw NoSuchElementException("Collection is empty.") + return this[0] +} + +/** + * Returns first element. + * @throws [NoSuchElementException] if the collection is empty. + */ +public fun DoubleArray.first(): Double { + if (isEmpty()) + throw NoSuchElementException("Collection is empty.") + return this[0] +} + +/** + * Returns first element. + * @throws [NoSuchElementException] if the collection is empty. + */ +public fun FloatArray.first(): Float { + if (isEmpty()) + throw NoSuchElementException("Collection is empty.") + return this[0] +} + +/** + * Returns first element. + * @throws [NoSuchElementException] if the collection is empty. + */ +public fun IntArray.first(): Int { + if (isEmpty()) + throw NoSuchElementException("Collection is empty.") + return this[0] +} + +/** + * Returns first element. + * @throws [NoSuchElementException] if the collection is empty. + */ +public fun LongArray.first(): Long { + if (isEmpty()) + throw NoSuchElementException("Collection is empty.") + return this[0] +} + +/** + * Returns first element. + * @throws [NoSuchElementException] if the collection is empty. + */ +public fun ShortArray.first(): Short { + if (isEmpty()) + throw NoSuchElementException("Collection 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("No element matching predicate was found.") +} + +/** + * 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("No element matching predicate was found.") +} + +/** + * 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("No element matching predicate was found.") +} + +/** + * 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("No element matching predicate was found.") +} + +/** + * 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("No element matching predicate was found.") +} + +/** + * 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("No element matching predicate was found.") +} + +/** + * 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("No element matching predicate was found.") +} + +/** + * 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("No element matching predicate was found.") +} + +/** + * 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("No element matching predicate was found.") +} + +/** + * Returns the first element, or `null` if the collection is empty. + */ +public fun Array.firstOrNull(): T? { + return if (isEmpty()) null else this[0] +} + +/** + * Returns the first element, or `null` if the collection is empty. + */ +public fun BooleanArray.firstOrNull(): Boolean? { + return if (isEmpty()) null else this[0] +} + +/** + * Returns the first element, or `null` if the collection is empty. + */ +public fun ByteArray.firstOrNull(): Byte? { + return if (isEmpty()) null else this[0] +} + +/** + * Returns the first element, or `null` if the collection is empty. + */ +public fun CharArray.firstOrNull(): Char? { + return if (isEmpty()) null else this[0] +} + +/** + * Returns the first element, or `null` if the collection is empty. + */ +public fun DoubleArray.firstOrNull(): Double? { + return if (isEmpty()) null else this[0] +} + +/** + * Returns the first element, or `null` if the collection is empty. + */ +public fun FloatArray.firstOrNull(): Float? { + return if (isEmpty()) null else this[0] +} + +/** + * Returns the first element, or `null` if the collection is empty. + */ +public fun IntArray.firstOrNull(): Int? { + return if (isEmpty()) null else this[0] +} + +/** + * Returns the first element, or `null` if the collection is empty. + */ +public fun LongArray.firstOrNull(): Long? { + return if (isEmpty()) null else this[0] +} + +/** + * Returns the first element, or `null` if the collection is empty. + */ +public fun ShortArray.firstOrNull(): Short? { + return if (isEmpty()) null else this[0] +} + +/** + * Returns the first element matching the given [predicate], or `null` if element was not found. + */ +public inline fun Array.firstOrNull(predicate: (T) -> Boolean): T? { + for (element in this) if (predicate(element)) return element + return null +} + +/** + * Returns the first element matching the given [predicate], or `null` if element was not found. + */ +public inline fun 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 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 CharArray.firstOrNull(predicate: (Char) -> Boolean): Char? { + 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 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 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 ShortArray.firstOrNull(predicate: (Short) -> Boolean): Short? { + for (element in this) if (predicate(element)) return element + return null +} + +/** + * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. + */ +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 collection. + */ +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 collection. + */ +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 collection. + */ +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 the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. + */ +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 collection. + */ +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 collection. + */ +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 collection. + */ +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 collection. + */ +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 `null` if the [index] is out of bounds of this collection. + */ +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 collection. + */ +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 collection. + */ +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 collection. + */ +public fun CharArray.getOrNull(index: Int): Char? { + 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 collection. + */ +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 collection. + */ +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 collection. + */ +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 collection. + */ +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 collection. + */ +public fun ShortArray.getOrNull(index: Int): Short? { + return if (index >= 0 && index <= lastIndex) get(index) else null +} + +/** + * Returns first index of [element], or -1 if the collection does not contain element. + */ +public fun Array.indexOf(element: T): Int { + if (element == null) { + for (index in indices) { + if (this[index] == null) { + return index + } + } + } else { + for (index in indices) { + if (element == this[index]) { + return index + } + } + } + return -1 +} + +/** + * Returns first index of [element], or -1 if the collection does not contain element. + */ +public fun BooleanArray.indexOf(element: Boolean): Int { + for (index in indices) { + if (element == this[index]) { + return index + } + } + return -1 +} + +/** + * Returns first index of [element], or -1 if the collection does not contain element. + */ +public fun ByteArray.indexOf(element: Byte): Int { + for (index in indices) { + if (element == this[index]) { + return index + } + } + return -1 +} + +/** + * Returns first index of [element], or -1 if the collection does not contain element. + */ +public fun CharArray.indexOf(element: Char): Int { + for (index in indices) { + if (element == this[index]) { + return index + } + } + return -1 +} + +/** + * Returns first index of [element], or -1 if the collection does not contain element. + */ +public fun DoubleArray.indexOf(element: Double): Int { + for (index in indices) { + if (element == this[index]) { + return index + } + } + return -1 +} + +/** + * Returns first index of [element], or -1 if the collection does not contain element. + */ +public fun FloatArray.indexOf(element: Float): Int { + for (index in indices) { + if (element == this[index]) { + return index + } + } + return -1 +} + +/** + * Returns first index of [element], or -1 if the collection does not contain element. + */ +public fun IntArray.indexOf(element: Int): Int { + for (index in indices) { + if (element == this[index]) { + return index + } + } + return -1 +} + +/** + * Returns first index of [element], or -1 if the collection does not contain element. + */ +public fun LongArray.indexOf(element: Long): Int { + for (index in indices) { + if (element == this[index]) { + return index + } + } + return -1 +} + +/** + * Returns first index of [element], or -1 if the collection does not contain element. + */ +public fun ShortArray.indexOf(element: Short): Int { + for (index in indices) { + if (element == this[index]) { + return index + } + } + return -1 +} + +/** + * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element. + */ +public inline fun Array.indexOfFirst(predicate: (T) -> Boolean): Int { + for (index in indices) { + if (predicate(this[index])) { + return index + } + } + return -1 +} + +/** + * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element. + */ +public inline fun BooleanArray.indexOfFirst(predicate: (Boolean) -> Boolean): Int { + for (index in indices) { + if (predicate(this[index])) { + return index + } + } + return -1 +} + +/** + * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element. + */ +public inline fun ByteArray.indexOfFirst(predicate: (Byte) -> Boolean): Int { + for (index in indices) { + if (predicate(this[index])) { + return index + } + } + return -1 +} + +/** + * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element. + */ +public inline fun CharArray.indexOfFirst(predicate: (Char) -> Boolean): Int { + for (index in indices) { + if (predicate(this[index])) { + return index + } + } + return -1 +} + +/** + * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element. + */ +public inline fun DoubleArray.indexOfFirst(predicate: (Double) -> Boolean): Int { + for (index in indices) { + if (predicate(this[index])) { + return index + } + } + return -1 +} + +/** + * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element. + */ +public inline fun FloatArray.indexOfFirst(predicate: (Float) -> Boolean): Int { + for (index in indices) { + if (predicate(this[index])) { + return index + } + } + return -1 +} + +/** + * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element. + */ +public inline fun IntArray.indexOfFirst(predicate: (Int) -> Boolean): Int { + for (index in indices) { + if (predicate(this[index])) { + return index + } + } + return -1 +} + +/** + * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element. + */ +public inline fun LongArray.indexOfFirst(predicate: (Long) -> Boolean): Int { + for (index in indices) { + if (predicate(this[index])) { + return index + } + } + return -1 +} + +/** + * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element. + */ +public inline fun ShortArray.indexOfFirst(predicate: (Short) -> Boolean): Int { + for (index in indices) { + if (predicate(this[index])) { + return index + } + } + return -1 +} + +/** + * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element. + */ +public inline fun Array.indexOfLast(predicate: (T) -> Boolean): Int { + for (index in indices.reversed()) { + if (predicate(this[index])) { + return index + } + } + return -1 +} + +/** + * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element. + */ +public inline fun BooleanArray.indexOfLast(predicate: (Boolean) -> Boolean): Int { + for (index in indices.reversed()) { + if (predicate(this[index])) { + return index + } + } + return -1 +} + +/** + * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element. + */ +public inline fun ByteArray.indexOfLast(predicate: (Byte) -> Boolean): Int { + for (index in indices.reversed()) { + if (predicate(this[index])) { + return index + } + } + return -1 +} + +/** + * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element. + */ +public inline fun CharArray.indexOfLast(predicate: (Char) -> Boolean): Int { + for (index in indices.reversed()) { + if (predicate(this[index])) { + return index + } + } + return -1 +} + +/** + * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element. + */ +public inline fun DoubleArray.indexOfLast(predicate: (Double) -> Boolean): Int { + for (index in indices.reversed()) { + if (predicate(this[index])) { + return index + } + } + return -1 +} + +/** + * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element. + */ +public inline fun FloatArray.indexOfLast(predicate: (Float) -> Boolean): Int { + for (index in indices.reversed()) { + if (predicate(this[index])) { + return index + } + } + return -1 +} + +/** + * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element. + */ +public inline fun IntArray.indexOfLast(predicate: (Int) -> Boolean): Int { + for (index in indices.reversed()) { + if (predicate(this[index])) { + return index + } + } + return -1 +} + +/** + * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element. + */ +public inline fun LongArray.indexOfLast(predicate: (Long) -> Boolean): Int { + for (index in indices.reversed()) { + if (predicate(this[index])) { + return index + } + } + return -1 +} + +/** + * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element. + */ +public inline fun ShortArray.indexOfLast(predicate: (Short) -> Boolean): Int { + for (index in indices.reversed()) { + if (predicate(this[index])) { + return index + } + } + return -1 +} + +/** + * Returns the last element. + * @throws [NoSuchElementException] if the collection is empty. + */ +public fun Array.last(): T { + if (isEmpty()) + throw NoSuchElementException("Collection is empty.") + return this[lastIndex] +} + +/** + * Returns the last element. + * @throws [NoSuchElementException] if the collection is empty. + */ +public fun BooleanArray.last(): Boolean { + if (isEmpty()) + throw NoSuchElementException("Collection is empty.") + return this[lastIndex] +} + +/** + * Returns the last element. + * @throws [NoSuchElementException] if the collection is empty. + */ +public fun ByteArray.last(): Byte { + if (isEmpty()) + throw NoSuchElementException("Collection is empty.") + return this[lastIndex] +} + +/** + * Returns the last element. + * @throws [NoSuchElementException] if the collection is empty. + */ +public fun CharArray.last(): Char { + if (isEmpty()) + throw NoSuchElementException("Collection is empty.") + return this[lastIndex] +} + +/** + * Returns the last element. + * @throws [NoSuchElementException] if the collection is empty. + */ +public fun DoubleArray.last(): Double { + if (isEmpty()) + throw NoSuchElementException("Collection is empty.") + return this[lastIndex] +} + +/** + * Returns the last element. + * @throws [NoSuchElementException] if the collection is empty. + */ +public fun FloatArray.last(): Float { + if (isEmpty()) + throw NoSuchElementException("Collection is empty.") + return this[lastIndex] +} + +/** + * Returns the last element. + * @throws [NoSuchElementException] if the collection is empty. + */ +public fun IntArray.last(): Int { + if (isEmpty()) + throw NoSuchElementException("Collection is empty.") + return this[lastIndex] +} + +/** + * Returns the last element. + * @throws [NoSuchElementException] if the collection is empty. + */ +public fun LongArray.last(): Long { + if (isEmpty()) + throw NoSuchElementException("Collection is empty.") + return this[lastIndex] +} + +/** + * Returns the last element. + * @throws [NoSuchElementException] if the collection is empty. + */ +public fun ShortArray.last(): Short { + if (isEmpty()) + throw NoSuchElementException("Collection 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("Collection doesn't contain any 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("Collection doesn't contain any 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("Collection doesn't contain any 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("Collection doesn't contain any 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("Collection doesn't contain any 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("Collection doesn't contain any 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("Collection doesn't contain any 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("Collection doesn't contain any 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("Collection doesn't contain any element matching the predicate.") +} + +/** + * Returns last index of [element], or -1 if the collection does not contain element. + */ +public fun Array.lastIndexOf(element: T): Int { + if (element == null) { + for (index in indices.reversed()) { + if (this[index] == null) { + return index + } + } + } else { + for (index in indices.reversed()) { + if (element == this[index]) { + return index + } + } + } + return -1 +} + +/** + * Returns last index of [element], or -1 if the collection does not contain element. + */ +public fun BooleanArray.lastIndexOf(element: Boolean): Int { + for (index in indices.reversed()) { + if (element == this[index]) { + return index + } + } + return -1 +} + +/** + * Returns last index of [element], or -1 if the collection does not contain element. + */ +public fun ByteArray.lastIndexOf(element: Byte): Int { + for (index in indices.reversed()) { + if (element == this[index]) { + return index + } + } + return -1 +} + +/** + * Returns last index of [element], or -1 if the collection does not contain element. + */ +public fun CharArray.lastIndexOf(element: Char): Int { + for (index in indices.reversed()) { + if (element == this[index]) { + return index + } + } + return -1 +} + +/** + * Returns last index of [element], or -1 if the collection does not contain element. + */ +public fun DoubleArray.lastIndexOf(element: Double): Int { + for (index in indices.reversed()) { + if (element == this[index]) { + return index + } + } + return -1 +} + +/** + * Returns last index of [element], or -1 if the collection does not contain element. + */ +public fun FloatArray.lastIndexOf(element: Float): Int { + for (index in indices.reversed()) { + if (element == this[index]) { + return index + } + } + return -1 +} + +/** + * Returns last index of [element], or -1 if the collection does not contain element. + */ +public fun IntArray.lastIndexOf(element: Int): Int { + for (index in indices.reversed()) { + if (element == this[index]) { + return index + } + } + return -1 +} + +/** + * Returns last index of [element], or -1 if the collection does not contain element. + */ +public fun LongArray.lastIndexOf(element: Long): Int { + for (index in indices.reversed()) { + if (element == this[index]) { + return index + } + } + return -1 +} + +/** + * Returns last index of [element], or -1 if the collection does not contain element. + */ +public fun ShortArray.lastIndexOf(element: Short): Int { + for (index in indices.reversed()) { + if (element == this[index]) { + return index + } + } + return -1 +} + +/** + * Returns the last element, or `null` if the collection is empty. + */ +public fun Array.lastOrNull(): T? { + return if (isEmpty()) null else this[size() - 1] +} + +/** + * Returns the last element, or `null` if the collection is empty. + */ +public fun BooleanArray.lastOrNull(): Boolean? { + return if (isEmpty()) null else this[size() - 1] +} + +/** + * Returns the last element, or `null` if the collection is empty. + */ +public fun ByteArray.lastOrNull(): Byte? { + return if (isEmpty()) null else this[size() - 1] +} + +/** + * Returns the last element, or `null` if the collection is empty. + */ +public fun CharArray.lastOrNull(): Char? { + return if (isEmpty()) null else this[size() - 1] +} + +/** + * Returns the last element, or `null` if the collection is empty. + */ +public fun DoubleArray.lastOrNull(): Double? { + return if (isEmpty()) null else this[size() - 1] +} + +/** + * Returns the last element, or `null` if the collection is empty. + */ +public fun FloatArray.lastOrNull(): Float? { + return if (isEmpty()) null else this[size() - 1] +} + +/** + * Returns the last element, or `null` if the collection is empty. + */ +public fun IntArray.lastOrNull(): Int? { + return if (isEmpty()) null else this[size() - 1] +} + +/** + * Returns the last element, or `null` if the collection is empty. + */ +public fun LongArray.lastOrNull(): Long? { + return if (isEmpty()) null else this[size() - 1] +} + +/** + * Returns the last element, or `null` if the collection is empty. + */ +public fun ShortArray.lastOrNull(): Short? { + return if (isEmpty()) null else this[size() - 1] +} + +/** + * Returns the last element matching the given [predicate], or `null` if no such element was found. + */ +public inline fun Array.lastOrNull(predicate: (T) -> Boolean): T? { + for (index in this.indices.reversed()) { + val element = this[index] + if (predicate(element)) return element + } + return null +} + +/** + * Returns the last element matching the given [predicate], or `null` if no such element was found. + */ +public inline fun 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 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 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 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 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 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 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 single element, or throws an exception if the collection is empty or has more than one element. + */ +public fun Array.single(): T { + return when (size()) { + 0 -> throw NoSuchElementException("Collection is empty.") + 1 -> this[0] + else -> throw IllegalArgumentException("Collection has more than one element.") + } +} + +/** + * Returns the single element, or throws an exception if the collection is empty or has more than one element. + */ +public fun BooleanArray.single(): Boolean { + return when (size()) { + 0 -> throw NoSuchElementException("Collection is empty.") + 1 -> this[0] + else -> throw IllegalArgumentException("Collection has more than one element.") + } +} + +/** + * Returns the single element, or throws an exception if the collection is empty or has more than one element. + */ +public fun ByteArray.single(): Byte { + return when (size()) { + 0 -> throw NoSuchElementException("Collection is empty.") + 1 -> this[0] + else -> throw IllegalArgumentException("Collection has more than one element.") + } +} + +/** + * Returns the single element, or throws an exception if the collection is empty or has more than one element. + */ +public fun CharArray.single(): Char { + return when (size()) { + 0 -> throw NoSuchElementException("Collection is empty.") + 1 -> this[0] + else -> throw IllegalArgumentException("Collection has more than one element.") + } +} + +/** + * Returns the single element, or throws an exception if the collection is empty or has more than one element. + */ +public fun DoubleArray.single(): Double { + return when (size()) { + 0 -> throw NoSuchElementException("Collection is empty.") + 1 -> this[0] + else -> throw IllegalArgumentException("Collection has more than one element.") + } +} + +/** + * Returns the single element, or throws an exception if the collection is empty or has more than one element. + */ +public fun FloatArray.single(): Float { + return when (size()) { + 0 -> throw NoSuchElementException("Collection is empty.") + 1 -> this[0] + else -> throw IllegalArgumentException("Collection has more than one element.") + } +} + +/** + * Returns the single element, or throws an exception if the collection is empty or has more than one element. + */ +public fun IntArray.single(): Int { + return when (size()) { + 0 -> throw NoSuchElementException("Collection is empty.") + 1 -> this[0] + else -> throw IllegalArgumentException("Collection has more than one element.") + } +} + +/** + * Returns the single element, or throws an exception if the collection is empty or has more than one element. + */ +public fun LongArray.single(): Long { + return when (size()) { + 0 -> throw NoSuchElementException("Collection is empty.") + 1 -> this[0] + else -> throw IllegalArgumentException("Collection has more than one element.") + } +} + +/** + * Returns the single element, or throws an exception if the collection is empty or has more than one element. + */ +public fun ShortArray.single(): Short { + return when (size()) { + 0 -> throw NoSuchElementException("Collection is empty.") + 1 -> this[0] + else -> throw IllegalArgumentException("Collection has more than one element.") + } +} + +/** + * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. + */ +public inline fun Array.single(predicate: (T) -> Boolean): T { + var single: T? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) throw IllegalArgumentException("Collection contains more than one matching element.") + single = element + found = true + } + } + if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") + return single as T +} + +/** + * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. + */ +public inline fun BooleanArray.single(predicate: (Boolean) -> Boolean): Boolean { + var single: Boolean? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) throw IllegalArgumentException("Collection contains more than one matching element.") + single = element + found = true + } + } + if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") + return single as Boolean +} + +/** + * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. + */ +public inline fun ByteArray.single(predicate: (Byte) -> Boolean): Byte { + var single: Byte? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) throw IllegalArgumentException("Collection contains more than one matching element.") + single = element + found = true + } + } + if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") + return single as Byte +} + +/** + * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. + */ +public inline fun CharArray.single(predicate: (Char) -> Boolean): Char { + var single: Char? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) throw IllegalArgumentException("Collection contains more than one matching element.") + single = element + found = true + } + } + if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") + return single as Char +} + +/** + * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. + */ +public inline fun DoubleArray.single(predicate: (Double) -> Boolean): Double { + var single: Double? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) throw IllegalArgumentException("Collection contains more than one matching element.") + single = element + found = true + } + } + if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") + return single as Double +} + +/** + * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. + */ +public inline fun FloatArray.single(predicate: (Float) -> Boolean): Float { + var single: Float? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) throw IllegalArgumentException("Collection contains more than one matching element.") + single = element + found = true + } + } + if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") + return single as Float +} + +/** + * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. + */ +public inline fun IntArray.single(predicate: (Int) -> Boolean): Int { + var single: Int? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) throw IllegalArgumentException("Collection contains more than one matching element.") + single = element + found = true + } + } + if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") + return single as Int +} + +/** + * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. + */ +public inline fun LongArray.single(predicate: (Long) -> Boolean): Long { + var single: Long? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) throw IllegalArgumentException("Collection contains more than one matching element.") + single = element + found = true + } + } + if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") + return single as Long +} + +/** + * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. + */ +public inline fun ShortArray.single(predicate: (Short) -> Boolean): Short { + var single: Short? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) throw IllegalArgumentException("Collection contains more than one matching element.") + single = element + found = true + } + } + if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") + return single as Short +} + +/** + * Returns single element, or `null` if the collection is empty or has more than one element. + */ +public fun Array.singleOrNull(): T? { + return if (size() == 1) this[0] else null +} + +/** + * Returns single element, or `null` if the collection 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 collection is empty or has more than one element. + */ +public fun ByteArray.singleOrNull(): Byte? { + return if (size() == 1) this[0] else null +} + +/** + * Returns single element, or `null` if the collection is empty or has more than one element. + */ +public fun CharArray.singleOrNull(): Char? { + return if (size() == 1) this[0] else null +} + +/** + * Returns single element, or `null` if the collection 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 collection 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 collection 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 collection 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 collection is empty or has more than one element. + */ +public fun ShortArray.singleOrNull(): Short? { + 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 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 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 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 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 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 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 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 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." }) + if (n == 0) + return toList() + if (n >= size()) + return emptyList() + val list = ArrayList(size() - n) + for (index in n..size() - 1) { + list.add(this[index]) + } + return list +} + +/** + * 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." }) + if (n == 0) + return toList() + if (n >= size()) + return emptyList() + val list = ArrayList(size() - n) + for (index in n..size() - 1) { + list.add(this[index]) + } + return list +} + +/** + * 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." }) + if (n == 0) + return toList() + if (n >= size()) + return emptyList() + val list = ArrayList(size() - n) + for (index in n..size() - 1) { + list.add(this[index]) + } + return list +} + +/** + * 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." }) + if (n == 0) + return toList() + if (n >= size()) + return emptyList() + val list = ArrayList(size() - n) + for (index in n..size() - 1) { + list.add(this[index]) + } + return list +} + +/** + * 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." }) + if (n == 0) + return toList() + if (n >= size()) + return emptyList() + val list = ArrayList(size() - n) + for (index in n..size() - 1) { + list.add(this[index]) + } + return list +} + +/** + * 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." }) + if (n == 0) + return toList() + if (n >= size()) + return emptyList() + val list = ArrayList(size() - n) + for (index in n..size() - 1) { + list.add(this[index]) + } + return list +} + +/** + * 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." }) + if (n == 0) + return toList() + if (n >= size()) + return emptyList() + val list = ArrayList(size() - n) + for (index in n..size() - 1) { + list.add(this[index]) + } + return list +} + +/** + * 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." }) + if (n == 0) + return toList() + if (n >= size()) + return emptyList() + val list = ArrayList(size() - n) + for (index in n..size() - 1) { + list.add(this[index]) + } + return list +} + +/** + * 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." }) + if (n == 0) + return toList() + if (n >= size()) + return emptyList() + val list = ArrayList(size() - n) + for (index in n..size() - 1) { + list.add(this[index]) + } + return list +} + +/** + * 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 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 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 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 [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 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 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 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 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 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 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 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 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 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 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 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 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) + 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 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 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 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 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 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 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 matching the given [predicate]. + */ +public inline fun Array.filter(predicate: (T) -> Boolean): List { + return filterTo(ArrayList(), predicate) +} + +/** + * Returns a list containing all elements matching the given [predicate]. + */ +public inline fun BooleanArray.filter(predicate: (Boolean) -> Boolean): List { + return filterTo(ArrayList(), predicate) +} + +/** + * Returns a list containing all elements matching the given [predicate]. + */ +public inline fun ByteArray.filter(predicate: (Byte) -> Boolean): List { + return filterTo(ArrayList(), predicate) +} + +/** + * Returns a list containing all elements matching the given [predicate]. + */ +public inline fun CharArray.filter(predicate: (Char) -> Boolean): List { + return filterTo(ArrayList(), predicate) +} + +/** + * Returns a list containing all elements matching the given [predicate]. + */ +public inline fun DoubleArray.filter(predicate: (Double) -> Boolean): List { + return filterTo(ArrayList(), predicate) +} + +/** + * Returns a list containing all elements matching the given [predicate]. + */ +public inline fun FloatArray.filter(predicate: (Float) -> Boolean): List { + return filterTo(ArrayList(), predicate) +} + +/** + * Returns a list containing all elements matching the given [predicate]. + */ +public inline fun IntArray.filter(predicate: (Int) -> Boolean): List { + return filterTo(ArrayList(), predicate) +} + +/** + * Returns a list containing all elements matching the given [predicate]. + */ +public inline fun LongArray.filter(predicate: (Long) -> Boolean): List { + return filterTo(ArrayList(), predicate) +} + +/** + * Returns a list containing all elements matching the given [predicate]. + */ +public inline fun ShortArray.filter(predicate: (Short) -> Boolean): List { + return filterTo(ArrayList(), predicate) +} + +/** + * Returns a list containing all elements not matching the given [predicate]. + */ +public inline fun Array.filterNot(predicate: (T) -> Boolean): List { + return filterNotTo(ArrayList(), predicate) +} + +/** + * Returns a list containing all elements not matching the given [predicate]. + */ +public inline fun BooleanArray.filterNot(predicate: (Boolean) -> 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 CharArray.filterNot(predicate: (Char) -> 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 FloatArray.filterNot(predicate: (Float) -> 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 ShortArray.filterNot(predicate: (Short) -> Boolean): List { + return filterNotTo(ArrayList(), predicate) +} + +/** + * Returns a list containing all elements that are not `null`. + */ +public fun Array.filterNotNull(): List { + return filterNotNullTo(ArrayList()) +} + +/** + * Appends all elements that are not `null` to the given [destination]. + */ +public fun , T : Any> Array.filterNotNullTo(destination: C): C { + for (element in this) if (element != null) destination.add(element) + return destination +} + +/** + * Appends all elements not matching the given [predicate] to the given [destination]. + */ +public inline fun > Array.filterNotTo(destination: C, predicate: (T) -> Boolean): C { + for (element in this) if (!predicate(element)) destination.add(element) + return destination +} + +/** + * Appends all elements not matching the given [predicate] to the given [destination]. + */ +public inline fun > 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 > 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 > CharArray.filterNotTo(destination: C, predicate: (Char) -> 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 > 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 > 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 > ShortArray.filterNotTo(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] into 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] into 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] into 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] into 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 +} + +/** + * Appends all elements matching the given [predicate] into 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] into 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] into 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] into 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] into 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 +} + +/** + * 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.end + 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.end + 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.end + 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.end + 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.end + 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.end + 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.end + 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.end + 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.end + 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 listOf() + 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 listOf() + 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 listOf() + 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 listOf() + 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 listOf() + 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 listOf() + 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 listOf() + 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 listOf() + 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 listOf() + val list = ArrayList(size) + for (index in indices) { + list.add(get(index)) + } + return list +} + +/** + * Returns an array containing elements of this array at specified [indices]. + */ +public fun Array.sliceArray(indices: Collection): Array { + val result = arrayOfNulls(this, indices.size()) as Array + 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 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 CharArray.sliceArray(indices: Collection): CharArray { + val result = CharArray(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 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 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 ShortArray.sliceArray(indices: Collection): ShortArray { + val result = ShortArray(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.end + 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.end + 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.end + 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.end + 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.end + 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.end + 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.end + 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.end + 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.end + 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() + 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() + 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() + 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() + 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() + 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() + 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() + 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() + 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() + 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() + 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() + 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() + 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() + 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() + 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() + 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() + 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() + 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() + 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 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 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 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 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 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 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 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 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 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 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 CharArray.takeWhile(predicate: (Char) -> 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 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 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 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 with elements in reversed order. + */ +@Deprecated("reverse will change its behavior soon. Use reversed() instead.", ReplaceWith("reversed()")) +public fun Array.reverse(): List { + return reversed() +} + +/** + * Returns a list with elements in reversed order. + */ +@Deprecated("reverse will change its behavior soon. Use reversed() instead.", ReplaceWith("reversed()")) +public fun BooleanArray.reverse(): List { + return reversed() +} + +/** + * Returns a list with elements in reversed order. + */ +@Deprecated("reverse will change its behavior soon. Use reversed() instead.", ReplaceWith("reversed()")) +public fun ByteArray.reverse(): List { + return reversed() +} + +/** + * Returns a list with elements in reversed order. + */ +@Deprecated("reverse will change its behavior soon. Use reversed() instead.", ReplaceWith("reversed()")) +public fun CharArray.reverse(): List { + return reversed() +} + +/** + * Returns a list with elements in reversed order. + */ +@Deprecated("reverse will change its behavior soon. Use reversed() instead.", ReplaceWith("reversed()")) +public fun DoubleArray.reverse(): List { + return reversed() +} + +/** + * Returns a list with elements in reversed order. + */ +@Deprecated("reverse will change its behavior soon. Use reversed() instead.", ReplaceWith("reversed()")) +public fun FloatArray.reverse(): List { + return reversed() +} + +/** + * Returns a list with elements in reversed order. + */ +@Deprecated("reverse will change its behavior soon. Use reversed() instead.", ReplaceWith("reversed()")) +public fun IntArray.reverse(): List { + return reversed() +} + +/** + * Returns a list with elements in reversed order. + */ +@Deprecated("reverse will change its behavior soon. Use reversed() instead.", ReplaceWith("reversed()")) +public fun LongArray.reverse(): List { + return reversed() +} + +/** + * Returns a list with elements in reversed order. + */ +@Deprecated("reverse will change its behavior soon. Use reversed() instead.", ReplaceWith("reversed()")) +public fun ShortArray.reverse(): List { + return reversed() +} + +/** + * Returns a list with elements in reversed order. + */ +public fun Array.reversed(): List { + if (isEmpty()) return emptyList() + val list = toArrayList() + Collections.reverse(list) + return list +} + +/** + * Returns a list with elements in reversed order. + */ +public fun BooleanArray.reversed(): List { + if (isEmpty()) return emptyList() + val list = toArrayList() + Collections.reverse(list) + return list +} + +/** + * Returns a list with elements in reversed order. + */ +public fun ByteArray.reversed(): List { + if (isEmpty()) return emptyList() + val list = toArrayList() + Collections.reverse(list) + return list +} + +/** + * Returns a list with elements in reversed order. + */ +public fun CharArray.reversed(): List { + if (isEmpty()) return emptyList() + val list = toArrayList() + Collections.reverse(list) + return list +} + +/** + * Returns a list with elements in reversed order. + */ +public fun DoubleArray.reversed(): List { + if (isEmpty()) return emptyList() + val list = toArrayList() + Collections.reverse(list) + return list +} + +/** + * Returns a list with elements in reversed order. + */ +public fun FloatArray.reversed(): List { + if (isEmpty()) return emptyList() + val list = toArrayList() + Collections.reverse(list) + return list +} + +/** + * Returns a list with elements in reversed order. + */ +public fun IntArray.reversed(): List { + if (isEmpty()) return emptyList() + val list = toArrayList() + Collections.reverse(list) + return list +} + +/** + * Returns a list with elements in reversed order. + */ +public fun LongArray.reversed(): List { + if (isEmpty()) return emptyList() + val list = toArrayList() + Collections.reverse(list) + return list +} + +/** + * Returns a list with elements in reversed order. + */ +public fun ShortArray.reversed(): List { + if (isEmpty()) return emptyList() + val list = toArrayList() + Collections.reverse(list) + return list +} + +/** + * Returns an array with elements of this array in reversed order. + */ +public fun Array.reversedArray(): Array { + if (isEmpty()) return this + val result = arrayOfNulls(this, size()) as Array + 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 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 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 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 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 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 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 a list of all elements, sorted by the specified [comparator]. + */ +@Deprecated("This method may change its behavior soon. Use sortedWith() instead.", ReplaceWith("sortedWith(comparator)")) +public fun Array.sortBy(comparator: Comparator): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList, comparator) + return sortedList +} + +/** + * Returns a sorted list of all elements, ordered by results of specified [order] function. + */ +@Deprecated("This method may change its behavior soon. Use sortedBy() instead.", ReplaceWith("sortedBy(order)")) +public inline fun > Array.sortBy(crossinline order: (T) -> R): List { + val sortedList = toArrayList() + val sortBy: Comparator = compareBy(order) + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Returns a sorted list of all elements, in descending order by results of specified [order] function. + */ +@Deprecated("This method may change its behavior soon. Use sortedByDescending() instead.", ReplaceWith("sortedByDescending(order)")) +public inline fun > Array.sortDescendingBy(crossinline order: (T) -> R): List { + val sortedList = toArrayList() + val sortBy: Comparator = compareByDescending(order) + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Returns a list of all elements sorted according to their natural sort order. + */ +public fun > Array.sorted(): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList) + return sortedList +} + +/** + * Returns a list of all elements sorted according to their natural sort order. + */ +public fun ByteArray.sorted(): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList) + return sortedList +} + +/** + * Returns a list of all elements sorted according to their natural sort order. + */ +public fun CharArray.sorted(): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList) + return sortedList +} + +/** + * Returns a list of all elements sorted according to their natural sort order. + */ +public fun DoubleArray.sorted(): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList) + return sortedList +} + +/** + * Returns a list of all elements sorted according to their natural sort order. + */ +public fun FloatArray.sorted(): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList) + return sortedList +} + +/** + * Returns a list of all elements sorted according to their natural sort order. + */ +public fun IntArray.sorted(): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList) + return sortedList +} + +/** + * Returns a list of all elements sorted according to their natural sort order. + */ +public fun LongArray.sorted(): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList) + return sortedList +} + +/** + * Returns a list of all elements sorted according to their natural sort order. + */ +public fun ShortArray.sorted(): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList) + return sortedList +} + +/** + * Returns an array with all elements of this array sorted according to their natural sort order. + */ +public fun > Array.sortedArray(): Array { + if (isEmpty()) return this + return this.copyOf().apply { sort() } +} + +/** + * Returns an array with all elements of this array sorted according to their natural sort order. + */ +public fun ByteArray.sortedArray(): ByteArray { + if (isEmpty()) return this + return this.copyOf().apply { sort() } +} + +/** + * Returns an array with all elements of this array sorted according to their natural sort order. + */ +public fun CharArray.sortedArray(): CharArray { + 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 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 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 ShortArray.sortedArray(): ShortArray { + if (isEmpty()) return this + return this.copyOf().apply { sort() } +} + +/** + * Returns an array with all elements of this array sorted descending according to their natural sort order. + */ +public fun > Array.sortedArrayDescending(): Array { + if (isEmpty()) return this + // TODO: Use reverseOrder() + return this.copyOf().apply { sortWith(comparator { a, b -> b.compareTo(a) }) } +} + +/** + * 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 + // TODO: Use in-place reverse + return this.copyOf().apply { sort() }.reversedArray() +} + +/** + * 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 + // TODO: Use in-place reverse + return this.copyOf().apply { sort() }.reversedArray() +} + +/** + * 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 + // TODO: Use in-place reverse + return this.copyOf().apply { sort() }.reversedArray() +} + +/** + * 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 + // TODO: Use in-place reverse + return this.copyOf().apply { sort() }.reversedArray() +} + +/** + * 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 + // TODO: Use in-place reverse + return this.copyOf().apply { sort() }.reversedArray() +} + +/** + * 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 + // TODO: Use in-place reverse + return this.copyOf().apply { sort() }.reversedArray() +} + +/** + * 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 + // TODO: Use in-place reverse + return this.copyOf().apply { sort() }.reversedArray() +} + +/** + * Returns an array with all elements of this array sorted according the specified [comparator]. + */ +public fun Array.sortedArrayWith(comparator: Comparator): Array { + if (isEmpty()) return this + return this.copyOf().apply { sortWith(comparator) } +} + +/** + * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. + */ +public inline fun > Array.sortedBy(crossinline selector: (T) -> R?): List { + return sortedWith(compareBy(selector)) +} + +/** + * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. + */ +public inline fun > 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 > 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 > CharArray.sortedBy(crossinline selector: (Char) -> 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 > 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 > 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 > ShortArray.sortedBy(crossinline selector: (Short) -> R?): List { + return sortedWith(compareBy(selector)) +} + +/** + * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. + */ +public inline fun > Array.sortedByDescending(crossinline selector: (T) -> R?): List { + return sortedWith(compareByDescending(selector)) +} + +/** + * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. + */ +public inline fun > 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 > 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 > CharArray.sortedByDescending(crossinline selector: (Char) -> 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 > 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 > 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 > ShortArray.sortedByDescending(crossinline selector: (Short) -> R?): List { + return sortedWith(compareByDescending(selector)) +} + +/** + * Returns a list of all elements sorted descending according to their natural sort order. + */ +public fun > Array.sortedDescending(): List { + return sortedWith(comparator { x, y -> y.compareTo(x) }) +} + +/** + * 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 CharArray.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 FloatArray.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 ShortArray.sortedDescending(): List { + return copyOf().apply { sort() }.reversed() +} + +/** + * Returns a list of all elements sorted according to the specified [comparator]. + */ +public fun Array.sortedWith(comparator: Comparator): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList, comparator) + return sortedList +} + +/** + * Returns a list of all elements sorted according to the specified [comparator]. + */ +public fun BooleanArray.sortedWith(comparator: Comparator): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList, comparator) + return sortedList +} + +/** + * Returns a list of all elements sorted according to the specified [comparator]. + */ +public fun ByteArray.sortedWith(comparator: Comparator): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList, comparator) + return sortedList +} + +/** + * Returns a list of all elements sorted according to the specified [comparator]. + */ +public fun CharArray.sortedWith(comparator: Comparator): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList, comparator) + return sortedList +} + +/** + * Returns a list of all elements sorted according to the specified [comparator]. + */ +public fun DoubleArray.sortedWith(comparator: Comparator): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList, comparator) + return sortedList +} + +/** + * Returns a list of all elements sorted according to the specified [comparator]. + */ +public fun FloatArray.sortedWith(comparator: Comparator): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList, comparator) + return sortedList +} + +/** + * Returns a list of all elements sorted according to the specified [comparator]. + */ +public fun IntArray.sortedWith(comparator: Comparator): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList, comparator) + return sortedList +} + +/** + * Returns a list of all elements sorted according to the specified [comparator]. + */ +public fun LongArray.sortedWith(comparator: Comparator): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList, comparator) + return sortedList +} + +/** + * Returns a list of all elements sorted according to the specified [comparator]. + */ +public fun ShortArray.sortedWith(comparator: Comparator): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList, comparator) + return sortedList +} + +/** + * Returns a sorted list of all elements. + */ +@Deprecated("Use sorted() instead.", ReplaceWith("sorted()")) +public fun > Array.toSortedList(): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList) + return sortedList +} + +/** + * Returns a sorted list of all elements. + */ +@Deprecated("Use sorted() instead.", ReplaceWith("sorted()")) +public fun BooleanArray.toSortedList(): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList) + return sortedList +} + +/** + * Returns a sorted list of all elements. + */ +@Deprecated("Use sorted() instead.", ReplaceWith("sorted()")) +public fun ByteArray.toSortedList(): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList) + return sortedList +} + +/** + * Returns a sorted list of all elements. + */ +@Deprecated("Use sorted() instead.", ReplaceWith("sorted()")) +public fun CharArray.toSortedList(): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList) + return sortedList +} + +/** + * Returns a sorted list of all elements. + */ +@Deprecated("Use sorted() instead.", ReplaceWith("sorted()")) +public fun DoubleArray.toSortedList(): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList) + return sortedList +} + +/** + * Returns a sorted list of all elements. + */ +@Deprecated("Use sorted() instead.", ReplaceWith("sorted()")) +public fun FloatArray.toSortedList(): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList) + return sortedList +} + +/** + * Returns a sorted list of all elements. + */ +@Deprecated("Use sorted() instead.", ReplaceWith("sorted()")) +public fun IntArray.toSortedList(): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList) + return sortedList +} + +/** + * Returns a sorted list of all elements. + */ +@Deprecated("Use sorted() instead.", ReplaceWith("sorted()")) +public fun LongArray.toSortedList(): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList) + return sortedList +} + +/** + * Returns a sorted list of all elements. + */ +@Deprecated("Use sorted() instead.", ReplaceWith("sorted()")) +public fun ShortArray.toSortedList(): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList) + return sortedList +} + +/** + * Returns a sorted list of all elements, ordered by results of specified [order] function. + */ +@Deprecated("Use sortedBy(order) instead.", ReplaceWith("sortedBy(order)")) +public fun > Array.toSortedListBy(order: (T) -> V): List { + val sortedList = toArrayList() + val sortBy: Comparator = compareBy(order) + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Returns a sorted list of all elements, ordered by results of specified [order] function. + */ +@Deprecated("Use sortedBy(order) instead.", ReplaceWith("sortedBy(order)")) +public fun > BooleanArray.toSortedListBy(order: (Boolean) -> V): List { + val sortedList = toArrayList() + val sortBy: Comparator = compareBy(order) + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Returns a sorted list of all elements, ordered by results of specified [order] function. + */ +@Deprecated("Use sortedBy(order) instead.", ReplaceWith("sortedBy(order)")) +public fun > ByteArray.toSortedListBy(order: (Byte) -> V): List { + val sortedList = toArrayList() + val sortBy: Comparator = compareBy(order) + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Returns a sorted list of all elements, ordered by results of specified [order] function. + */ +@Deprecated("Use sortedBy(order) instead.", ReplaceWith("sortedBy(order)")) +public fun > CharArray.toSortedListBy(order: (Char) -> V): List { + val sortedList = toArrayList() + val sortBy: Comparator = compareBy(order) + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Returns a sorted list of all elements, ordered by results of specified [order] function. + */ +@Deprecated("Use sortedBy(order) instead.", ReplaceWith("sortedBy(order)")) +public fun > DoubleArray.toSortedListBy(order: (Double) -> V): List { + val sortedList = toArrayList() + val sortBy: Comparator = compareBy(order) + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Returns a sorted list of all elements, ordered by results of specified [order] function. + */ +@Deprecated("Use sortedBy(order) instead.", ReplaceWith("sortedBy(order)")) +public fun > FloatArray.toSortedListBy(order: (Float) -> V): List { + val sortedList = toArrayList() + val sortBy: Comparator = compareBy(order) + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Returns a sorted list of all elements, ordered by results of specified [order] function. + */ +@Deprecated("Use sortedBy(order) instead.", ReplaceWith("sortedBy(order)")) +public fun > IntArray.toSortedListBy(order: (Int) -> V): List { + val sortedList = toArrayList() + val sortBy: Comparator = compareBy(order) + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Returns a sorted list of all elements, ordered by results of specified [order] function. + */ +@Deprecated("Use sortedBy(order) instead.", ReplaceWith("sortedBy(order)")) +public fun > LongArray.toSortedListBy(order: (Long) -> V): List { + val sortedList = toArrayList() + val sortBy: Comparator = compareBy(order) + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Returns a sorted list of all elements, ordered by results of specified [order] function. + */ +@Deprecated("Use sortedBy(order) instead.", ReplaceWith("sortedBy(order)")) +public fun > ShortArray.toSortedListBy(order: (Short) -> V): List { + val sortedList = toArrayList() + val sortBy: Comparator = compareBy(order) + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + /** * Returns the Iterable that wraps the original array. */ @@ -325,3 +5369,6410 @@ public val LongArray.lastIndex: Int public val ShortArray.lastIndex: Int get() = size() - 1 +/** + * 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 an [ArrayList] of all elements. + */ +public fun Array.toArrayList(): ArrayList { + return this.asList().toArrayList() +} + +/** + * Returns an [ArrayList] of all elements. + */ +public fun BooleanArray.toArrayList(): ArrayList { + val list = ArrayList(size()) + for (item in this) list.add(item) + return list +} + +/** + * Returns an [ArrayList] of all elements. + */ +public fun ByteArray.toArrayList(): ArrayList { + val list = ArrayList(size()) + for (item in this) list.add(item) + return list +} + +/** + * Returns an [ArrayList] of all elements. + */ +public fun CharArray.toArrayList(): ArrayList { + val list = ArrayList(size()) + for (item in this) list.add(item) + return list +} + +/** + * Returns an [ArrayList] of all elements. + */ +public fun DoubleArray.toArrayList(): ArrayList { + val list = ArrayList(size()) + for (item in this) list.add(item) + return list +} + +/** + * Returns an [ArrayList] of all elements. + */ +public fun FloatArray.toArrayList(): ArrayList { + val list = ArrayList(size()) + for (item in this) list.add(item) + return list +} + +/** + * Returns an [ArrayList] of all elements. + */ +public fun IntArray.toArrayList(): ArrayList { + val list = ArrayList(size()) + for (item in this) list.add(item) + return list +} + +/** + * Returns an [ArrayList] of all elements. + */ +public fun LongArray.toArrayList(): ArrayList { + val list = ArrayList(size()) + for (item in this) list.add(item) + return list +} + +/** + * Returns an [ArrayList] of all elements. + */ +public fun ShortArray.toArrayList(): ArrayList { + val list = ArrayList(size()) + for (item in this) list.add(item) + return list +} + +/** + * Appends all elements to the given [collection]. + */ +public fun > Array.toCollection(collection: C): C { + for (item in this) { + collection.add(item) + } + return collection +} + +/** + * Appends all elements to the given [collection]. + */ +public fun > BooleanArray.toCollection(collection: C): C { + for (item in this) { + collection.add(item) + } + return collection +} + +/** + * Appends all elements to the given [collection]. + */ +public fun > ByteArray.toCollection(collection: C): C { + for (item in this) { + collection.add(item) + } + return collection +} + +/** + * Appends all elements to the given [collection]. + */ +public fun > CharArray.toCollection(collection: C): C { + for (item in this) { + collection.add(item) + } + return collection +} + +/** + * Appends all elements to the given [collection]. + */ +public fun > DoubleArray.toCollection(collection: C): C { + for (item in this) { + collection.add(item) + } + return collection +} + +/** + * Appends all elements to the given [collection]. + */ +public fun > FloatArray.toCollection(collection: C): C { + for (item in this) { + collection.add(item) + } + return collection +} + +/** + * Appends all elements to the given [collection]. + */ +public fun > IntArray.toCollection(collection: C): C { + for (item in this) { + collection.add(item) + } + return collection +} + +/** + * Appends all elements to the given [collection]. + */ +public fun > LongArray.toCollection(collection: C): C { + for (item in this) { + collection.add(item) + } + return collection +} + +/** + * Appends all elements to the given [collection]. + */ +public fun > ShortArray.toCollection(collection: C): C { + for (item in this) { + collection.add(item) + } + return collection +} + +/** + * Returns a [HashSet] of all elements. + */ +public fun Array.toHashSet(): HashSet { + return toCollection(HashSet(mapCapacity(size()))) +} + +/** + * Returns a [HashSet] of all elements. + */ +public fun BooleanArray.toHashSet(): HashSet { + return toCollection(HashSet(mapCapacity(size()))) +} + +/** + * Returns a [HashSet] of all elements. + */ +public fun ByteArray.toHashSet(): HashSet { + return toCollection(HashSet(mapCapacity(size()))) +} + +/** + * Returns a [HashSet] of all elements. + */ +public fun CharArray.toHashSet(): HashSet { + return toCollection(HashSet(mapCapacity(size()))) +} + +/** + * Returns a [HashSet] of all elements. + */ +public fun DoubleArray.toHashSet(): HashSet { + return toCollection(HashSet(mapCapacity(size()))) +} + +/** + * Returns a [HashSet] of all elements. + */ +public fun FloatArray.toHashSet(): HashSet { + return toCollection(HashSet(mapCapacity(size()))) +} + +/** + * Returns a [HashSet] of all elements. + */ +public fun IntArray.toHashSet(): HashSet { + return toCollection(HashSet(mapCapacity(size()))) +} + +/** + * Returns a [HashSet] of all elements. + */ +public fun LongArray.toHashSet(): HashSet { + return toCollection(HashSet(mapCapacity(size()))) +} + +/** + * Returns a [HashSet] of all elements. + */ +public fun ShortArray.toHashSet(): HashSet { + return toCollection(HashSet(mapCapacity(size()))) +} + +/** + * Returns a [LinkedList] containing all elements. + */ +public fun Array.toLinkedList(): LinkedList { + return toCollection(LinkedList()) +} + +/** + * Returns a [LinkedList] containing all elements. + */ +public fun BooleanArray.toLinkedList(): LinkedList { + return toCollection(LinkedList()) +} + +/** + * Returns a [LinkedList] containing all elements. + */ +public fun ByteArray.toLinkedList(): LinkedList { + return toCollection(LinkedList()) +} + +/** + * Returns a [LinkedList] containing all elements. + */ +public fun CharArray.toLinkedList(): LinkedList { + return toCollection(LinkedList()) +} + +/** + * Returns a [LinkedList] containing all elements. + */ +public fun DoubleArray.toLinkedList(): LinkedList { + return toCollection(LinkedList()) +} + +/** + * Returns a [LinkedList] containing all elements. + */ +public fun FloatArray.toLinkedList(): LinkedList { + return toCollection(LinkedList()) +} + +/** + * Returns a [LinkedList] containing all elements. + */ +public fun IntArray.toLinkedList(): LinkedList { + return toCollection(LinkedList()) +} + +/** + * Returns a [LinkedList] containing all elements. + */ +public fun LongArray.toLinkedList(): LinkedList { + return toCollection(LinkedList()) +} + +/** + * Returns a [LinkedList] containing all elements. + */ +public fun ShortArray.toLinkedList(): LinkedList { + return toCollection(LinkedList()) +} + +/** + * Returns a [List] containing all elements. + */ +public fun Array.toList(): List { + return this.toArrayList() +} + +/** + * Returns a [List] containing all elements. + */ +public fun BooleanArray.toList(): List { + return this.toArrayList() +} + +/** + * Returns a [List] containing all elements. + */ +public fun ByteArray.toList(): List { + return this.toArrayList() +} + +/** + * Returns a [List] containing all elements. + */ +public fun CharArray.toList(): List { + return this.toArrayList() +} + +/** + * Returns a [List] containing all elements. + */ +public fun DoubleArray.toList(): List { + return this.toArrayList() +} + +/** + * Returns a [List] containing all elements. + */ +public fun FloatArray.toList(): List { + return this.toArrayList() +} + +/** + * Returns a [List] containing all elements. + */ +public fun IntArray.toList(): List { + return this.toArrayList() +} + +/** + * Returns a [List] containing all elements. + */ +public fun LongArray.toList(): List { + return this.toArrayList() +} + +/** + * Returns a [List] containing all elements. + */ +public fun ShortArray.toList(): List { + return this.toArrayList() +} + +/** + * Returns Map containing the values from the given collection indexed by [selector]. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun Array.toMap(selector: (T) -> K): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), element) + } + return result +} + +/** + * Returns Map containing the values from the given collection indexed by [selector]. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun BooleanArray.toMap(selector: (Boolean) -> K): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), element) + } + return result +} + +/** + * Returns Map containing the values from the given collection indexed by [selector]. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun ByteArray.toMap(selector: (Byte) -> K): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), element) + } + return result +} + +/** + * Returns Map containing the values from the given collection indexed by [selector]. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun CharArray.toMap(selector: (Char) -> K): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), element) + } + return result +} + +/** + * Returns Map containing the values from the given collection indexed by [selector]. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun DoubleArray.toMap(selector: (Double) -> K): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), element) + } + return result +} + +/** + * Returns Map containing the values from the given collection indexed by [selector]. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun FloatArray.toMap(selector: (Float) -> K): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), element) + } + return result +} + +/** + * Returns Map containing the values from the given collection indexed by [selector]. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun IntArray.toMap(selector: (Int) -> K): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), element) + } + return result +} + +/** + * Returns Map containing the values from the given collection indexed by [selector]. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun LongArray.toMap(selector: (Long) -> K): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), element) + } + return result +} + +/** + * Returns Map containing the values from the given collection indexed by [selector]. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun ShortArray.toMap(selector: (Short) -> K): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), element) + } + return result +} + +/** + * Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun Array.toMap(selector: (T) -> K, transform: (T) -> V): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(element)) + } + return result +} + +/** + * Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun BooleanArray.toMap(selector: (Boolean) -> K, transform: (Boolean) -> V): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(element)) + } + return result +} + +/** + * Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun ByteArray.toMap(selector: (Byte) -> K, transform: (Byte) -> V): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(element)) + } + return result +} + +/** + * Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun CharArray.toMap(selector: (Char) -> K, transform: (Char) -> V): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(element)) + } + return result +} + +/** + * Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun DoubleArray.toMap(selector: (Double) -> K, transform: (Double) -> V): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(element)) + } + return result +} + +/** + * Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun FloatArray.toMap(selector: (Float) -> K, transform: (Float) -> V): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(element)) + } + return result +} + +/** + * Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun IntArray.toMap(selector: (Int) -> K, transform: (Int) -> V): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(element)) + } + return result +} + +/** + * Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun LongArray.toMap(selector: (Long) -> K, transform: (Long) -> V): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(element)) + } + return result +} + +/** + * Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun ShortArray.toMap(selector: (Short) -> K, transform: (Short) -> V): Map { + val capacity = (size()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(element)) + } + return result +} + +/** + * Returns a [Set] of all elements. + */ +public fun Array.toSet(): Set { + return toCollection(LinkedHashSet(mapCapacity(size()))) +} + +/** + * Returns a [Set] of all elements. + */ +public fun BooleanArray.toSet(): Set { + return toCollection(LinkedHashSet(mapCapacity(size()))) +} + +/** + * Returns a [Set] of all elements. + */ +public fun ByteArray.toSet(): Set { + return toCollection(LinkedHashSet(mapCapacity(size()))) +} + +/** + * Returns a [Set] of all elements. + */ +public fun CharArray.toSet(): Set { + return toCollection(LinkedHashSet(mapCapacity(size()))) +} + +/** + * Returns a [Set] of all elements. + */ +public fun DoubleArray.toSet(): Set { + return toCollection(LinkedHashSet(mapCapacity(size()))) +} + +/** + * Returns a [Set] of all elements. + */ +public fun FloatArray.toSet(): Set { + return toCollection(LinkedHashSet(mapCapacity(size()))) +} + +/** + * Returns a [Set] of all elements. + */ +public fun IntArray.toSet(): Set { + return toCollection(LinkedHashSet(mapCapacity(size()))) +} + +/** + * Returns a [Set] of all elements. + */ +public fun LongArray.toSet(): Set { + return toCollection(LinkedHashSet(mapCapacity(size()))) +} + +/** + * Returns a [Set] of all elements. + */ +public fun ShortArray.toSet(): Set { + return toCollection(LinkedHashSet(mapCapacity(size()))) +} + +/** + * Returns a [SortedSet] of all elements. + */ +public fun Array.toSortedSet(): SortedSet { + return toCollection(TreeSet()) +} + +/** + * Returns a [SortedSet] of all elements. + */ +public fun BooleanArray.toSortedSet(): SortedSet { + return toCollection(TreeSet()) +} + +/** + * Returns a [SortedSet] of all elements. + */ +public fun ByteArray.toSortedSet(): SortedSet { + return toCollection(TreeSet()) +} + +/** + * Returns a [SortedSet] of all elements. + */ +public fun CharArray.toSortedSet(): SortedSet { + return toCollection(TreeSet()) +} + +/** + * Returns a [SortedSet] of all elements. + */ +public fun DoubleArray.toSortedSet(): SortedSet { + return toCollection(TreeSet()) +} + +/** + * Returns a [SortedSet] of all elements. + */ +public fun FloatArray.toSortedSet(): SortedSet { + return toCollection(TreeSet()) +} + +/** + * Returns a [SortedSet] of all elements. + */ +public fun IntArray.toSortedSet(): SortedSet { + return toCollection(TreeSet()) +} + +/** + * Returns a [SortedSet] of all elements. + */ +public fun LongArray.toSortedSet(): SortedSet { + return toCollection(TreeSet()) +} + +/** + * Returns a [SortedSet] of all elements. + */ +public fun ShortArray.toSortedSet(): SortedSet { + return toCollection(TreeSet()) +} + +/** + * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original collection. + */ +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 collection. + */ +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 collection. + */ +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 collection. + */ +public inline fun CharArray.flatMap(transform: (Char) -> 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 collection. + */ +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 collection. + */ +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 collection. + */ +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 collection. + */ +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 collection. + */ +public inline fun ShortArray.flatMap(transform: (Short) -> Iterable): List { + return flatMapTo(ArrayList(), transform) +} + +/** + * Appends all elements yielded from results of [transform] function being invoked on each element of original collection, to the given [destination]. + */ +public inline fun > 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 collection, 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 collection, 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 collection, 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 +} + +/** + * Appends all elements yielded from results of [transform] function being invoked on each element of original collection, 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 collection, 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 collection, 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 collection, 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 collection, 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 +} + +/** + * Returns a map of the elements in original collection grouped by the result of given [toKey] function. + */ +public inline fun Array.groupBy(toKey: (T) -> K): Map> { + return groupByTo(LinkedHashMap>(), toKey) +} + +/** + * Returns a map of the elements in original collection grouped by the result of given [toKey] function. + */ +public inline fun BooleanArray.groupBy(toKey: (Boolean) -> K): Map> { + return groupByTo(LinkedHashMap>(), toKey) +} + +/** + * Returns a map of the elements in original collection grouped by the result of given [toKey] function. + */ +public inline fun ByteArray.groupBy(toKey: (Byte) -> K): Map> { + return groupByTo(LinkedHashMap>(), toKey) +} + +/** + * Returns a map of the elements in original collection grouped by the result of given [toKey] function. + */ +public inline fun CharArray.groupBy(toKey: (Char) -> K): Map> { + return groupByTo(LinkedHashMap>(), toKey) +} + +/** + * Returns a map of the elements in original collection grouped by the result of given [toKey] function. + */ +public inline fun DoubleArray.groupBy(toKey: (Double) -> K): Map> { + return groupByTo(LinkedHashMap>(), toKey) +} + +/** + * Returns a map of the elements in original collection grouped by the result of given [toKey] function. + */ +public inline fun FloatArray.groupBy(toKey: (Float) -> K): Map> { + return groupByTo(LinkedHashMap>(), toKey) +} + +/** + * Returns a map of the elements in original collection grouped by the result of given [toKey] function. + */ +public inline fun IntArray.groupBy(toKey: (Int) -> K): Map> { + return groupByTo(LinkedHashMap>(), toKey) +} + +/** + * Returns a map of the elements in original collection grouped by the result of given [toKey] function. + */ +public inline fun LongArray.groupBy(toKey: (Long) -> K): Map> { + return groupByTo(LinkedHashMap>(), toKey) +} + +/** + * Returns a map of the elements in original collection grouped by the result of given [toKey] function. + */ +public inline fun ShortArray.groupBy(toKey: (Short) -> K): Map> { + return groupByTo(LinkedHashMap>(), toKey) +} + +/** + * Appends elements from original collection grouped by the result of given [toKey] function to the given [map]. + */ +public inline fun Array.groupByTo(map: MutableMap>, toKey: (T) -> K): Map> { + for (element in this) { + val key = toKey(element) + val list = map.getOrPut(key) { ArrayList() } + list.add(element) + } + return map +} + +/** + * Appends elements from original collection grouped by the result of given [toKey] function to the given [map]. + */ +public inline fun BooleanArray.groupByTo(map: MutableMap>, toKey: (Boolean) -> K): Map> { + for (element in this) { + val key = toKey(element) + val list = map.getOrPut(key) { ArrayList() } + list.add(element) + } + return map +} + +/** + * Appends elements from original collection grouped by the result of given [toKey] function to the given [map]. + */ +public inline fun ByteArray.groupByTo(map: MutableMap>, toKey: (Byte) -> K): Map> { + for (element in this) { + val key = toKey(element) + val list = map.getOrPut(key) { ArrayList() } + list.add(element) + } + return map +} + +/** + * Appends elements from original collection grouped by the result of given [toKey] function to the given [map]. + */ +public inline fun CharArray.groupByTo(map: MutableMap>, toKey: (Char) -> K): Map> { + for (element in this) { + val key = toKey(element) + val list = map.getOrPut(key) { ArrayList() } + list.add(element) + } + return map +} + +/** + * Appends elements from original collection grouped by the result of given [toKey] function to the given [map]. + */ +public inline fun DoubleArray.groupByTo(map: MutableMap>, toKey: (Double) -> K): Map> { + for (element in this) { + val key = toKey(element) + val list = map.getOrPut(key) { ArrayList() } + list.add(element) + } + return map +} + +/** + * Appends elements from original collection grouped by the result of given [toKey] function to the given [map]. + */ +public inline fun FloatArray.groupByTo(map: MutableMap>, toKey: (Float) -> K): Map> { + for (element in this) { + val key = toKey(element) + val list = map.getOrPut(key) { ArrayList() } + list.add(element) + } + return map +} + +/** + * Appends elements from original collection grouped by the result of given [toKey] function to the given [map]. + */ +public inline fun IntArray.groupByTo(map: MutableMap>, toKey: (Int) -> K): Map> { + for (element in this) { + val key = toKey(element) + val list = map.getOrPut(key) { ArrayList() } + list.add(element) + } + return map +} + +/** + * Appends elements from original collection grouped by the result of given [toKey] function to the given [map]. + */ +public inline fun LongArray.groupByTo(map: MutableMap>, toKey: (Long) -> K): Map> { + for (element in this) { + val key = toKey(element) + val list = map.getOrPut(key) { ArrayList() } + list.add(element) + } + return map +} + +/** + * Appends elements from original collection grouped by the result of given [toKey] function to the given [map]. + */ +public inline fun ShortArray.groupByTo(map: MutableMap>, toKey: (Short) -> K): Map> { + for (element in this) { + val key = toKey(element) + val list = map.getOrPut(key) { ArrayList() } + list.add(element) + } + return map +} + +/** + * Returns a list containing the results of applying the given [transform] function to each element of the original collection. + */ +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 of the original collection. + */ +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 of the original collection. + */ +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 of the original collection. + */ +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 of the original collection. + */ +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 of the original collection. + */ +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 of the original collection. + */ +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 of the original collection. + */ +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 of the original collection. + */ +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 and its index of the original collection. + */ +public inline fun Array.mapIndexed(transform: (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 of the original collection. + */ +public inline fun BooleanArray.mapIndexed(transform: (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 of the original collection. + */ +public inline fun ByteArray.mapIndexed(transform: (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 of the original collection. + */ +public inline fun CharArray.mapIndexed(transform: (Int, Char) -> 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 of the original collection. + */ +public inline fun DoubleArray.mapIndexed(transform: (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 of the original collection. + */ +public inline fun FloatArray.mapIndexed(transform: (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 of the original collection. + */ +public inline fun IntArray.mapIndexed(transform: (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 of the original collection. + */ +public inline fun LongArray.mapIndexed(transform: (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 of the original collection. + */ +public inline fun ShortArray.mapIndexed(transform: (Int, Short) -> R): List { + return mapIndexedTo(ArrayList(size()), transform) +} + +/** + * Appends transformed elements and their indices of the original collection using the given [transform] function + * to the given [destination]. + */ +public inline fun > Array.mapIndexedTo(destination: C, transform: (Int, T) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} + +/** + * Appends transformed elements and their indices of the original collection using the given [transform] function + * to the given [destination]. + */ +public inline fun > BooleanArray.mapIndexedTo(destination: C, transform: (Int, Boolean) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} + +/** + * Appends transformed elements and their indices of the original collection using the given [transform] function + * to the given [destination]. + */ +public inline fun > ByteArray.mapIndexedTo(destination: C, transform: (Int, Byte) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} + +/** + * Appends transformed elements and their indices of the original collection using the given [transform] function + * to the given [destination]. + */ +public inline fun > CharArray.mapIndexedTo(destination: C, transform: (Int, Char) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} + +/** + * Appends transformed elements and their indices of the original collection using the given [transform] function + * to the given [destination]. + */ +public inline fun > DoubleArray.mapIndexedTo(destination: C, transform: (Int, Double) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} + +/** + * Appends transformed elements and their indices of the original collection using the given [transform] function + * to the given [destination]. + */ +public inline fun > FloatArray.mapIndexedTo(destination: C, transform: (Int, Float) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} + +/** + * Appends transformed elements and their indices of the original collection using the given [transform] function + * to the given [destination]. + */ +public inline fun > IntArray.mapIndexedTo(destination: C, transform: (Int, Int) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} + +/** + * Appends transformed elements and their indices of the original collection using the given [transform] function + * to the given [destination]. + */ +public inline fun > LongArray.mapIndexedTo(destination: C, transform: (Int, Long) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} + +/** + * Appends transformed elements and their indices of the original collection using the given [transform] function + * to the given [destination]. + */ +public inline fun > ShortArray.mapIndexedTo(destination: C, transform: (Int, Short) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} + +/** + * Returns a list containing the results of applying the given [transform] function to each non-null element of the original collection. + */ +public inline fun Array.mapNotNull(transform: (T) -> R): List { + return mapNotNullTo(ArrayList(), transform) +} + +/** + * Appends transformed non-null elements of original collection using the given [transform] function + * to the given [destination]. + */ +public inline fun > Array.mapNotNullTo(destination: C, transform: (T) -> R): C { + for (element in this) { + if (element != null) { + destination.add(transform(element)) + } + } + return destination +} + +/** + * Appends transformed elements of the original collection using the given [transform] function + * 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 +} + +/** + * Appends transformed elements of the original collection using the given [transform] function + * 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 +} + +/** + * Appends transformed elements of the original collection using the given [transform] function + * 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 +} + +/** + * Appends transformed elements of the original collection using the given [transform] function + * 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 +} + +/** + * Appends transformed elements of the original collection using the given [transform] function + * 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 +} + +/** + * Appends transformed elements of the original collection using the given [transform] function + * 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 +} + +/** + * Appends transformed elements of the original collection using the given [transform] function + * 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 +} + +/** + * Appends transformed elements of the original collection using the given [transform] function + * 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 +} + +/** + * Appends transformed elements of the original collection using the given [transform] function + * 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 +} + +/** + * Returns a lazy [Iterable] of [IndexedValue] for each element of the original collection. + */ +public fun Array.withIndex(): Iterable> { + return IndexingIterable { iterator() } +} + +/** + * Returns a lazy [Iterable] of [IndexedValue] for each element of the original collection. + */ +public fun BooleanArray.withIndex(): Iterable> { + return IndexingIterable { iterator() } +} + +/** + * Returns a lazy [Iterable] of [IndexedValue] for each element of the original collection. + */ +public fun ByteArray.withIndex(): Iterable> { + return IndexingIterable { iterator() } +} + +/** + * Returns a lazy [Iterable] of [IndexedValue] for each element of the original collection. + */ +public fun CharArray.withIndex(): Iterable> { + return IndexingIterable { iterator() } +} + +/** + * Returns a lazy [Iterable] of [IndexedValue] for each element of the original collection. + */ +public fun DoubleArray.withIndex(): Iterable> { + return IndexingIterable { iterator() } +} + +/** + * Returns a lazy [Iterable] of [IndexedValue] for each element of the original collection. + */ +public fun FloatArray.withIndex(): Iterable> { + return IndexingIterable { iterator() } +} + +/** + * Returns a lazy [Iterable] of [IndexedValue] for each element of the original collection. + */ +public fun IntArray.withIndex(): Iterable> { + return IndexingIterable { iterator() } +} + +/** + * Returns a lazy [Iterable] of [IndexedValue] for each element of the original collection. + */ +public fun LongArray.withIndex(): Iterable> { + return IndexingIterable { iterator() } +} + +/** + * Returns a lazy [Iterable] of [IndexedValue] for each element of the original collection. + */ +public fun ShortArray.withIndex(): Iterable> { + return IndexingIterable { iterator() } +} + +/** + * Returns a list containing pairs of each element of the original collection and their index. + */ +@Deprecated("Use withIndex() instead.") +public fun Array.withIndices(): List> { + var index = 0 + return mapTo(ArrayList>(), { index++ to it }) +} + +/** + * Returns a list containing pairs of each element of the original collection and their index. + */ +@Deprecated("Use withIndex() instead.") +public fun BooleanArray.withIndices(): List> { + var index = 0 + return mapTo(ArrayList>(), { index++ to it }) +} + +/** + * Returns a list containing pairs of each element of the original collection and their index. + */ +@Deprecated("Use withIndex() instead.") +public fun ByteArray.withIndices(): List> { + var index = 0 + return mapTo(ArrayList>(), { index++ to it }) +} + +/** + * Returns a list containing pairs of each element of the original collection and their index. + */ +@Deprecated("Use withIndex() instead.") +public fun CharArray.withIndices(): List> { + var index = 0 + return mapTo(ArrayList>(), { index++ to it }) +} + +/** + * Returns a list containing pairs of each element of the original collection and their index. + */ +@Deprecated("Use withIndex() instead.") +public fun DoubleArray.withIndices(): List> { + var index = 0 + return mapTo(ArrayList>(), { index++ to it }) +} + +/** + * Returns a list containing pairs of each element of the original collection and their index. + */ +@Deprecated("Use withIndex() instead.") +public fun FloatArray.withIndices(): List> { + var index = 0 + return mapTo(ArrayList>(), { index++ to it }) +} + +/** + * Returns a list containing pairs of each element of the original collection and their index. + */ +@Deprecated("Use withIndex() instead.") +public fun IntArray.withIndices(): List> { + var index = 0 + return mapTo(ArrayList>(), { index++ to it }) +} + +/** + * Returns a list containing pairs of each element of the original collection and their index. + */ +@Deprecated("Use withIndex() instead.") +public fun LongArray.withIndices(): List> { + var index = 0 + return mapTo(ArrayList>(), { index++ to it }) +} + +/** + * Returns a list containing pairs of each element of the original collection and their index. + */ +@Deprecated("Use withIndex() instead.") +public fun ShortArray.withIndices(): List> { + var index = 0 + return mapTo(ArrayList>(), { index++ to it }) +} + +/** + * Returns a list containing only distinct elements from the given collection. + * The elements in the resulting list are in the same order as they were in the source collection. + */ +public fun Array.distinct(): List { + return this.toMutableSet().toList() +} + +/** + * Returns a list containing only distinct elements from the given collection. + * The elements in the resulting list are in the same order as they were in the source collection. + */ +public fun BooleanArray.distinct(): List { + return this.toMutableSet().toList() +} + +/** + * Returns a list containing only distinct elements from the given collection. + * The elements in the resulting list are in the same order as they were in the source collection. + */ +public fun ByteArray.distinct(): List { + return this.toMutableSet().toList() +} + +/** + * Returns a list containing only distinct elements from the given collection. + * The elements in the resulting list are in the same order as they were in the source collection. + */ +public fun CharArray.distinct(): List { + return this.toMutableSet().toList() +} + +/** + * Returns a list containing only distinct elements from the given collection. + * The elements in the resulting list are in the same order as they were in the source collection. + */ +public fun DoubleArray.distinct(): List { + return this.toMutableSet().toList() +} + +/** + * Returns a list containing only distinct elements from the given collection. + * The elements in the resulting list are in the same order as they were in the source collection. + */ +public fun FloatArray.distinct(): List { + return this.toMutableSet().toList() +} + +/** + * Returns a list containing only distinct elements from the given collection. + * The elements in the resulting list are in the same order as they were in the source collection. + */ +public fun IntArray.distinct(): List { + return this.toMutableSet().toList() +} + +/** + * Returns a list containing only distinct elements from the given collection. + * The elements in the resulting list are in the same order as they were in the source collection. + */ +public fun LongArray.distinct(): List { + return this.toMutableSet().toList() +} + +/** + * Returns a list containing only distinct elements from the given collection. + * The elements in the resulting list are in the same order as they were in the source collection. + */ +public fun ShortArray.distinct(): List { + return this.toMutableSet().toList() +} + +/** + * Returns a list containing only distinct elements from the given collection according to the [keySelector]. + * The elements in the resulting list are in the same order as they were in the source collection. + */ +public inline fun Array.distinctBy(keySelector: (T) -> K): List { + val set = HashSet() + val list = ArrayList() + for (e in this) { + val key = keySelector(e) + if (set.add(key)) + list.add(e) + } + return list +} + +/** + * Returns a list containing only distinct elements from the given collection according to the [keySelector]. + * The elements in the resulting list are in the same order as they were in the source collection. + */ +public inline fun BooleanArray.distinctBy(keySelector: (Boolean) -> K): List { + val set = HashSet() + val list = ArrayList() + for (e in this) { + val key = keySelector(e) + if (set.add(key)) + list.add(e) + } + return list +} + +/** + * Returns a list containing only distinct elements from the given collection according to the [keySelector]. + * The elements in the resulting list are in the same order as they were in the source collection. + */ +public inline fun ByteArray.distinctBy(keySelector: (Byte) -> K): List { + val set = HashSet() + val list = ArrayList() + for (e in this) { + val key = keySelector(e) + if (set.add(key)) + list.add(e) + } + return list +} + +/** + * Returns a list containing only distinct elements from the given collection according to the [keySelector]. + * The elements in the resulting list are in the same order as they were in the source collection. + */ +public inline fun CharArray.distinctBy(keySelector: (Char) -> K): List { + val set = HashSet() + val list = ArrayList() + for (e in this) { + val key = keySelector(e) + if (set.add(key)) + list.add(e) + } + return list +} + +/** + * Returns a list containing only distinct elements from the given collection according to the [keySelector]. + * The elements in the resulting list are in the same order as they were in the source collection. + */ +public inline fun DoubleArray.distinctBy(keySelector: (Double) -> K): List { + val set = HashSet() + val list = ArrayList() + for (e in this) { + val key = keySelector(e) + if (set.add(key)) + list.add(e) + } + return list +} + +/** + * Returns a list containing only distinct elements from the given collection according to the [keySelector]. + * The elements in the resulting list are in the same order as they were in the source collection. + */ +public inline fun FloatArray.distinctBy(keySelector: (Float) -> K): List { + val set = HashSet() + val list = ArrayList() + for (e in this) { + val key = keySelector(e) + if (set.add(key)) + list.add(e) + } + return list +} + +/** + * Returns a list containing only distinct elements from the given collection according to the [keySelector]. + * The elements in the resulting list are in the same order as they were in the source collection. + */ +public inline fun IntArray.distinctBy(keySelector: (Int) -> K): List { + val set = HashSet() + val list = ArrayList() + for (e in this) { + val key = keySelector(e) + if (set.add(key)) + list.add(e) + } + return list +} + +/** + * Returns a list containing only distinct elements from the given collection according to the [keySelector]. + * The elements in the resulting list are in the same order as they were in the source collection. + */ +public inline fun LongArray.distinctBy(keySelector: (Long) -> K): List { + val set = HashSet() + val list = ArrayList() + for (e in this) { + val key = keySelector(e) + if (set.add(key)) + list.add(e) + } + return list +} + +/** + * Returns a list containing only distinct elements from the given collection according to the [keySelector]. + * The elements in the resulting list are in the same order as they were in the source collection. + */ +public inline fun ShortArray.distinctBy(keySelector: (Short) -> K): List { + val set = HashSet() + val list = ArrayList() + for (e in this) { + val key = keySelector(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. + */ +public 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. + */ +public 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. + */ +public 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. + */ +public 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 both this set and the specified collection. + */ +public 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. + */ +public 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. + */ +public 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. + */ +public 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. + */ +public 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 this set and not contained by the specified collection. + */ +public 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 set and not contained by the specified collection. + */ +public 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 set and not contained by the specified collection. + */ +public 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 set and not contained by the specified collection. + */ +public fun CharArray.subtract(other: Iterable): Set { + val set = this.toMutableSet() + set.removeAll(other) + return set +} + +/** + * Returns a set containing all elements that are contained by this set and not contained by the specified collection. + */ +public 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 set and not contained by the specified collection. + */ +public 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 set and not contained by the specified collection. + */ +public 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 set and not contained by the specified collection. + */ +public 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 set and not contained by the specified collection. + */ +public fun ShortArray.subtract(other: Iterable): Set { + val set = this.toMutableSet() + set.removeAll(other) + return set +} + +/** + * Returns a mutable set containing all distinct elements from the given collection. + */ +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 collection. + */ +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 collection. + */ +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 collection. + */ +public fun CharArray.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 collection. + */ +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 collection. + */ +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 collection. + */ +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 collection. + */ +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 collection. + */ +public fun ShortArray.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. + */ +public 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. + */ +public 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. + */ +public 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. + */ +public fun CharArray.union(other: Iterable): Set { + val set = this.toMutableSet() + set.addAll(other) + return set +} + +/** + * Returns a set containing all distinct elements from both collections. + */ +public 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. + */ +public 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. + */ +public 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. + */ +public 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. + */ +public fun ShortArray.union(other: Iterable): Set { + val set = this.toMutableSet() + set.addAll(other) + return set +} + +/** + * Returns `true` if all elements match the given [predicate]. + */ +public inline fun Array.all(predicate: (T) -> Boolean): Boolean { + for (element in this) if (!predicate(element)) return false + return true +} + +/** + * Returns `true` if all elements match the given [predicate]. + */ +public inline fun BooleanArray.all(predicate: (Boolean) -> Boolean): Boolean { + for (element in this) if (!predicate(element)) return false + return true +} + +/** + * Returns `true` if all elements match the given [predicate]. + */ +public inline fun ByteArray.all(predicate: (Byte) -> Boolean): Boolean { + for (element in this) if (!predicate(element)) return false + return true +} + +/** + * Returns `true` if all elements match the given [predicate]. + */ +public inline fun CharArray.all(predicate: (Char) -> Boolean): Boolean { + for (element in this) if (!predicate(element)) return false + return true +} + +/** + * Returns `true` if all elements match the given [predicate]. + */ +public inline fun DoubleArray.all(predicate: (Double) -> Boolean): Boolean { + for (element in this) if (!predicate(element)) return false + return true +} + +/** + * Returns `true` if all elements match the given [predicate]. + */ +public inline fun FloatArray.all(predicate: (Float) -> Boolean): Boolean { + for (element in this) if (!predicate(element)) return false + return true +} + +/** + * Returns `true` if all elements match the given [predicate]. + */ +public inline fun IntArray.all(predicate: (Int) -> Boolean): Boolean { + for (element in this) if (!predicate(element)) return false + return true +} + +/** + * Returns `true` if all elements match the given [predicate]. + */ +public inline fun LongArray.all(predicate: (Long) -> Boolean): Boolean { + for (element in this) if (!predicate(element)) return false + return true +} + +/** + * Returns `true` if all elements match the given [predicate]. + */ +public inline fun ShortArray.all(predicate: (Short) -> Boolean): Boolean { + for (element in this) if (!predicate(element)) return false + return true +} + +/** + * Returns `true` if collection has at least one element. + */ +public fun Array.any(): Boolean { + for (element in this) return true + return false +} + +/** + * Returns `true` if collection has at least one element. + */ +public fun BooleanArray.any(): Boolean { + for (element in this) return true + return false +} + +/** + * Returns `true` if collection has at least one element. + */ +public fun ByteArray.any(): Boolean { + for (element in this) return true + return false +} + +/** + * Returns `true` if collection has at least one element. + */ +public fun CharArray.any(): Boolean { + for (element in this) return true + return false +} + +/** + * Returns `true` if collection has at least one element. + */ +public fun DoubleArray.any(): Boolean { + for (element in this) return true + return false +} + +/** + * Returns `true` if collection has at least one element. + */ +public fun FloatArray.any(): Boolean { + for (element in this) return true + return false +} + +/** + * Returns `true` if collection has at least one element. + */ +public fun IntArray.any(): Boolean { + for (element in this) return true + return false +} + +/** + * Returns `true` if collection has at least one element. + */ +public fun LongArray.any(): Boolean { + for (element in this) return true + return false +} + +/** + * Returns `true` if collection has at least one element. + */ +public fun ShortArray.any(): Boolean { + for (element in this) return true + return false +} + +/** + * Returns `true` if at least one element matches the given [predicate]. + */ +public inline fun Array.any(predicate: (T) -> Boolean): Boolean { + for (element in this) if (predicate(element)) return true + return false +} + +/** + * Returns `true` if at least one element matches the given [predicate]. + */ +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 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 CharArray.any(predicate: (Char) -> 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 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 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 ShortArray.any(predicate: (Short) -> Boolean): Boolean { + for (element in this) if (predicate(element)) return true + return false +} + +/** + * Returns the number of elements in this collection. + */ +public fun Array.count(): Int { + return size() +} + +/** + * Returns the number of elements in this collection. + */ +public fun BooleanArray.count(): Int { + return size() +} + +/** + * Returns the number of elements in this collection. + */ +public fun ByteArray.count(): Int { + return size() +} + +/** + * Returns the number of elements in this collection. + */ +public fun CharArray.count(): Int { + return size() +} + +/** + * Returns the number of elements in this collection. + */ +public fun DoubleArray.count(): Int { + return size() +} + +/** + * Returns the number of elements in this collection. + */ +public fun FloatArray.count(): Int { + return size() +} + +/** + * Returns the number of elements in this collection. + */ +public fun IntArray.count(): Int { + return size() +} + +/** + * Returns the number of elements in this collection. + */ +public fun LongArray.count(): Int { + return size() +} + +/** + * Returns the number of elements in this collection. + */ +public fun ShortArray.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 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 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 CharArray.count(predicate: (Char) -> 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 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 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 ShortArray.count(predicate: (Short) -> Boolean): Int { + var count = 0 + for (element in this) if (predicate(element)) count++ + return count +} + +/** + * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. + */ +public inline fun Array.fold(initial: R, operation: (R, T) -> R): R { + var accumulator = initial + for (element in this) accumulator = operation(accumulator, element) + return accumulator +} + +/** + * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. + */ +public inline fun BooleanArray.fold(initial: R, operation: (R, Boolean) -> R): R { + var accumulator = initial + for (element in this) accumulator = operation(accumulator, element) + return accumulator +} + +/** + * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. + */ +public inline fun ByteArray.fold(initial: R, operation: (R, Byte) -> R): R { + var accumulator = initial + for (element in this) accumulator = operation(accumulator, element) + return accumulator +} + +/** + * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. + */ +public inline fun CharArray.fold(initial: R, operation: (R, Char) -> R): R { + var accumulator = initial + for (element in this) accumulator = operation(accumulator, element) + return accumulator +} + +/** + * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. + */ +public inline fun DoubleArray.fold(initial: R, operation: (R, Double) -> R): R { + var accumulator = initial + for (element in this) accumulator = operation(accumulator, element) + return accumulator +} + +/** + * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. + */ +public inline fun FloatArray.fold(initial: R, operation: (R, Float) -> R): R { + var accumulator = initial + for (element in this) accumulator = operation(accumulator, element) + return accumulator +} + +/** + * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. + */ +public inline fun IntArray.fold(initial: R, operation: (R, Int) -> R): R { + var accumulator = initial + for (element in this) accumulator = operation(accumulator, element) + return accumulator +} + +/** + * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. + */ +public inline fun LongArray.fold(initial: R, operation: (R, Long) -> R): R { + var accumulator = initial + for (element in this) accumulator = operation(accumulator, element) + return accumulator +} + +/** + * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. + */ +public inline fun ShortArray.fold(initial: R, operation: (R, Short) -> R): R { + var accumulator = initial + for (element in this) accumulator = operation(accumulator, element) + return accumulator +} + +/** + * Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. + */ +public inline fun Array.foldRight(initial: R, operation: (T, 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, 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, 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, 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, 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, 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, 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, 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, R) -> R): R { + var index = lastIndex + var accumulator = initial + while (index >= 0) { + accumulator = operation(get(index--), accumulator) + } + return accumulator +} + +/** + * Performs the given [operation] on each element. + */ +public inline fun Array.forEach(operation: (T) -> Unit): Unit { + for (element in this) operation(element) +} + +/** + * Performs the given [operation] on each element. + */ +public inline fun BooleanArray.forEach(operation: (Boolean) -> Unit): Unit { + for (element in this) operation(element) +} + +/** + * Performs the given [operation] on each element. + */ +public inline fun ByteArray.forEach(operation: (Byte) -> Unit): Unit { + for (element in this) operation(element) +} + +/** + * Performs the given [operation] on each element. + */ +public inline fun CharArray.forEach(operation: (Char) -> Unit): Unit { + for (element in this) operation(element) +} + +/** + * Performs the given [operation] on each element. + */ +public inline fun DoubleArray.forEach(operation: (Double) -> Unit): Unit { + for (element in this) operation(element) +} + +/** + * Performs the given [operation] on each element. + */ +public inline fun FloatArray.forEach(operation: (Float) -> Unit): Unit { + for (element in this) operation(element) +} + +/** + * Performs the given [operation] on each element. + */ +public inline fun IntArray.forEach(operation: (Int) -> Unit): Unit { + for (element in this) operation(element) +} + +/** + * Performs the given [operation] on each element. + */ +public inline fun LongArray.forEach(operation: (Long) -> Unit): Unit { + for (element in this) operation(element) +} + +/** + * Performs the given [operation] on each element. + */ +public inline fun ShortArray.forEach(operation: (Short) -> Unit): Unit { + for (element in this) operation(element) +} + +/** + * Performs the given [operation] on each element, providing sequential index with the element. + */ +public inline fun Array.forEachIndexed(operation: (Int, T) -> Unit): Unit { + var index = 0 + for (item in this) operation(index++, item) +} + +/** + * Performs the given [operation] on each element, providing sequential index with the element. + */ +public inline fun BooleanArray.forEachIndexed(operation: (Int, Boolean) -> Unit): Unit { + var index = 0 + for (item in this) operation(index++, item) +} + +/** + * Performs the given [operation] on each element, providing sequential index with the element. + */ +public inline fun ByteArray.forEachIndexed(operation: (Int, Byte) -> Unit): Unit { + var index = 0 + for (item in this) operation(index++, item) +} + +/** + * Performs the given [operation] on each element, providing sequential index with the element. + */ +public inline fun CharArray.forEachIndexed(operation: (Int, Char) -> Unit): Unit { + var index = 0 + for (item in this) operation(index++, item) +} + +/** + * Performs the given [operation] on each element, providing sequential index with the element. + */ +public inline fun DoubleArray.forEachIndexed(operation: (Int, Double) -> Unit): Unit { + var index = 0 + for (item in this) operation(index++, item) +} + +/** + * Performs the given [operation] on each element, providing sequential index with the element. + */ +public inline fun FloatArray.forEachIndexed(operation: (Int, Float) -> Unit): Unit { + var index = 0 + for (item in this) operation(index++, item) +} + +/** + * Performs the given [operation] on each element, providing sequential index with the element. + */ +public inline fun IntArray.forEachIndexed(operation: (Int, Int) -> Unit): Unit { + var index = 0 + for (item in this) operation(index++, item) +} + +/** + * Performs the given [operation] on each element, providing sequential index with the element. + */ +public inline fun LongArray.forEachIndexed(operation: (Int, Long) -> Unit): Unit { + var index = 0 + for (item in this) operation(index++, item) +} + +/** + * Performs the given [operation] on each element, providing sequential index with the element. + */ +public inline fun ShortArray.forEachIndexed(operation: (Int, Short) -> Unit): Unit { + var index = 0 + for (item in this) operation(index++, item) +} + +/** + * 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 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 largest element or `null` if there are no elements. + */ +public fun DoubleArray.max(): Double? { + 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 FloatArray.max(): Float? { + 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. + */ +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 first element yielding the largest value of the given function or `null` if there are no elements. + */ +public inline fun , T : Any> Array.maxBy(f: (T) -> R): T? { + if (isEmpty()) return null + var maxElem = this[0] + var maxValue = f(maxElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = f(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(f: (Boolean) -> R): Boolean? { + if (isEmpty()) return null + var maxElem = this[0] + var maxValue = f(maxElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = f(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(f: (Byte) -> R): Byte? { + if (isEmpty()) return null + var maxElem = this[0] + var maxValue = f(maxElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = f(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(f: (Char) -> R): Char? { + if (isEmpty()) return null + var maxElem = this[0] + var maxValue = f(maxElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = f(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(f: (Double) -> R): Double? { + if (isEmpty()) return null + var maxElem = this[0] + var maxValue = f(maxElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = f(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(f: (Float) -> R): Float? { + if (isEmpty()) return null + var maxElem = this[0] + var maxValue = f(maxElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = f(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(f: (Int) -> R): Int? { + if (isEmpty()) return null + var maxElem = this[0] + var maxValue = f(maxElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = f(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(f: (Long) -> R): Long? { + if (isEmpty()) return null + var maxElem = this[0] + var maxValue = f(maxElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = f(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(f: (Short) -> R): Short? { + if (isEmpty()) return null + var maxElem = this[0] + var maxValue = f(maxElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = f(e) + if (maxValue < v) { + maxElem = e + maxValue = v + } + } + return maxElem +} + +/** + * 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 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 smallest element or `null` if there are no elements. + */ +public fun DoubleArray.min(): Double? { + 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 FloatArray.min(): Float? { + 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. + */ +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 first element yielding the smallest value of the given function or `null` if there are no elements. + */ +public inline fun , T : Any> Array.minBy(f: (T) -> R): T? { + if (size() == 0) return null + var minElem = this[0] + var minValue = f(minElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = f(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(f: (Boolean) -> R): Boolean? { + if (size() == 0) return null + var minElem = this[0] + var minValue = f(minElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = f(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(f: (Byte) -> R): Byte? { + if (size() == 0) return null + var minElem = this[0] + var minValue = f(minElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = f(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(f: (Char) -> R): Char? { + if (size() == 0) return null + var minElem = this[0] + var minValue = f(minElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = f(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(f: (Double) -> R): Double? { + if (size() == 0) return null + var minElem = this[0] + var minValue = f(minElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = f(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(f: (Float) -> R): Float? { + if (size() == 0) return null + var minElem = this[0] + var minValue = f(minElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = f(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(f: (Int) -> R): Int? { + if (size() == 0) return null + var minElem = this[0] + var minValue = f(minElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = f(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(f: (Long) -> R): Long? { + if (size() == 0) return null + var minElem = this[0] + var minValue = f(minElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = f(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(f: (Short) -> R): Short? { + if (size() == 0) return null + var minElem = this[0] + var minValue = f(minElem) + for (i in 1..lastIndex) { + val e = this[i] + val v = f(e) + if (minValue > v) { + minElem = e + minValue = v + } + } + return minElem +} + +/** + * Returns `true` if collection has no elements. + */ +public fun Array.none(): Boolean { + for (element in this) return false + return true +} + +/** + * Returns `true` if collection has no elements. + */ +public fun BooleanArray.none(): Boolean { + for (element in this) return false + return true +} + +/** + * Returns `true` if collection has no elements. + */ +public fun ByteArray.none(): Boolean { + for (element in this) return false + return true +} + +/** + * Returns `true` if collection has no elements. + */ +public fun CharArray.none(): Boolean { + for (element in this) return false + return true +} + +/** + * Returns `true` if collection has no elements. + */ +public fun DoubleArray.none(): Boolean { + for (element in this) return false + return true +} + +/** + * Returns `true` if collection has no elements. + */ +public fun FloatArray.none(): Boolean { + for (element in this) return false + return true +} + +/** + * Returns `true` if collection has no elements. + */ +public fun IntArray.none(): Boolean { + for (element in this) return false + return true +} + +/** + * Returns `true` if collection has no elements. + */ +public fun LongArray.none(): Boolean { + for (element in this) return false + return true +} + +/** + * Returns `true` if collection has no elements. + */ +public fun ShortArray.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 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 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 CharArray.none(predicate: (Char) -> 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 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 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 ShortArray.none(predicate: (Short) -> 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: (S, T) -> S): S { + val iterator = this.iterator() + if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") + var accumulator: S = iterator.next() + while (iterator.hasNext()) { + accumulator = operation(accumulator, iterator.next()) + } + return accumulator +} + +/** + * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. + */ +public inline fun BooleanArray.reduce(operation: (Boolean, Boolean) -> Boolean): Boolean { + val iterator = this.iterator() + if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") + var accumulator = iterator.next() + while (iterator.hasNext()) { + accumulator = operation(accumulator, iterator.next()) + } + return accumulator +} + +/** + * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. + */ +public inline fun ByteArray.reduce(operation: (Byte, Byte) -> Byte): Byte { + val iterator = this.iterator() + if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") + var accumulator = iterator.next() + while (iterator.hasNext()) { + accumulator = operation(accumulator, iterator.next()) + } + return accumulator +} + +/** + * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. + */ +public inline fun CharArray.reduce(operation: (Char, Char) -> Char): Char { + val iterator = this.iterator() + if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") + var accumulator = iterator.next() + while (iterator.hasNext()) { + accumulator = operation(accumulator, iterator.next()) + } + return accumulator +} + +/** + * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. + */ +public inline fun DoubleArray.reduce(operation: (Double, Double) -> Double): Double { + val iterator = this.iterator() + if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") + var accumulator = iterator.next() + while (iterator.hasNext()) { + accumulator = operation(accumulator, iterator.next()) + } + return accumulator +} + +/** + * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. + */ +public inline fun FloatArray.reduce(operation: (Float, Float) -> Float): Float { + val iterator = this.iterator() + if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") + var accumulator = iterator.next() + while (iterator.hasNext()) { + accumulator = operation(accumulator, iterator.next()) + } + return accumulator +} + +/** + * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. + */ +public inline fun IntArray.reduce(operation: (Int, Int) -> Int): Int { + val iterator = this.iterator() + if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") + var accumulator = iterator.next() + while (iterator.hasNext()) { + accumulator = operation(accumulator, iterator.next()) + } + return accumulator +} + +/** + * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. + */ +public inline fun LongArray.reduce(operation: (Long, Long) -> Long): Long { + val iterator = this.iterator() + if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") + var accumulator = iterator.next() + while (iterator.hasNext()) { + accumulator = operation(accumulator, iterator.next()) + } + return accumulator +} + +/** + * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. + */ +public inline fun ShortArray.reduce(operation: (Short, Short) -> Short): Short { + val iterator = this.iterator() + if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") + var accumulator = iterator.next() + while (iterator.hasNext()) { + accumulator = operation(accumulator, iterator.next()) + } + return accumulator +} + +/** + * Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. + */ +public inline fun Array.reduceRight(operation: (T, S) -> S): S { + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty iterable 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 BooleanArray.reduceRight(operation: (Boolean, Boolean) -> Boolean): Boolean { + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty iterable 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 ByteArray.reduceRight(operation: (Byte, Byte) -> Byte): Byte { + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty iterable 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, Char) -> Char): Char { + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty iterable 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, Double) -> Double): Double { + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty iterable 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, Float) -> Float): Float { + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty iterable 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, Int) -> Int): Int { + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty iterable 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, Long) -> Long): Long { + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty iterable 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, Short) -> Short): Short { + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.") + var accumulator = get(index--) + while (index >= 0) { + accumulator = operation(get(index--), accumulator) + } + return accumulator +} + +/** + * Returns the sum of all values produced by [transform] function from elements in the collection. + */ +public inline fun Array.sumBy(transform: (T) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [transform] function from elements in the collection. + */ +public inline fun BooleanArray.sumBy(transform: (Boolean) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [transform] function from elements in the collection. + */ +public inline fun ByteArray.sumBy(transform: (Byte) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [transform] function from elements in the collection. + */ +public inline fun CharArray.sumBy(transform: (Char) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [transform] function from elements in the collection. + */ +public inline fun DoubleArray.sumBy(transform: (Double) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [transform] function from elements in the collection. + */ +public inline fun FloatArray.sumBy(transform: (Float) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [transform] function from elements in the collection. + */ +public inline fun IntArray.sumBy(transform: (Int) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [transform] function from elements in the collection. + */ +public inline fun LongArray.sumBy(transform: (Long) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [transform] function from elements in the collection. + */ +public inline fun ShortArray.sumBy(transform: (Short) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [transform] function from elements in the collection. + */ +public inline fun Array.sumByDouble(transform: (T) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [transform] function from elements in the collection. + */ +public inline fun BooleanArray.sumByDouble(transform: (Boolean) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [transform] function from elements in the collection. + */ +public inline fun ByteArray.sumByDouble(transform: (Byte) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [transform] function from elements in the collection. + */ +public inline fun CharArray.sumByDouble(transform: (Char) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [transform] function from elements in the collection. + */ +public inline fun DoubleArray.sumByDouble(transform: (Double) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [transform] function from elements in the collection. + */ +public inline fun FloatArray.sumByDouble(transform: (Float) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [transform] function from elements in the collection. + */ +public inline fun IntArray.sumByDouble(transform: (Int) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [transform] function from elements in the collection. + */ +public inline fun LongArray.sumByDouble(transform: (Long) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [transform] function from elements in the collection. + */ +public inline fun ShortArray.sumByDouble(transform: (Short) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += transform(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.") + } + } + return this as 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 inline fun Array.merge(array: Array, transform: (T, R) -> V): List { + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[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.merge(array: Array, transform: (Boolean, R) -> V): List { + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) + } + return list +} + +/** + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. + */ +public inline fun ByteArray.merge(array: Array, transform: (Byte, R) -> V): List { + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[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.merge(array: Array, transform: (Char, R) -> V): List { + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[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.merge(array: Array, transform: (Double, R) -> V): List { + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[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.merge(array: Array, transform: (Float, R) -> V): List { + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[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.merge(array: Array, transform: (Int, R) -> V): List { + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[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.merge(array: Array, transform: (Long, R) -> V): List { + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[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.merge(array: Array, transform: (Short, R) -> V): List { + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[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.merge(array: BooleanArray, transform: (Boolean, Boolean) -> V): List { + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) + } + return list +} + +/** + * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. + */ +public inline fun ByteArray.merge(array: ByteArray, transform: (Byte, Byte) -> V): List { + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[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.merge(array: CharArray, transform: (Char, Char) -> V): List { + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[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.merge(array: DoubleArray, transform: (Double, Double) -> V): List { + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[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.merge(array: FloatArray, transform: (Float, Float) -> V): List { + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[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.merge(array: IntArray, transform: (Int, Int) -> V): List { + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[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.merge(array: LongArray, transform: (Long, Long) -> V): List { + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[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.merge(array: ShortArray, transform: (Short, Short) -> V): List { + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[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 Array.merge(other: Iterable, transform: (T, R) -> V): List { + val arraySize = size() + val list = ArrayList(Math.min(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.merge(other: Iterable, transform: (Boolean, R) -> V): List { + val arraySize = size() + val list = ArrayList(Math.min(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.merge(other: Iterable, transform: (Byte, R) -> V): List { + val arraySize = size() + val list = ArrayList(Math.min(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.merge(other: Iterable, transform: (Char, R) -> V): List { + val arraySize = size() + val list = ArrayList(Math.min(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.merge(other: Iterable, transform: (Double, R) -> V): List { + val arraySize = size() + val list = ArrayList(Math.min(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.merge(other: Iterable, transform: (Float, R) -> V): List { + val arraySize = size() + val list = ArrayList(Math.min(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.merge(other: Iterable, transform: (Int, R) -> V): List { + val arraySize = size() + val list = ArrayList(Math.min(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.merge(other: Iterable, transform: (Long, R) -> V): List { + val arraySize = size() + val list = ArrayList(Math.min(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.merge(other: Iterable, transform: (Short, R) -> V): List { + val arraySize = size() + val list = ArrayList(Math.min(other.collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in other) { + if (i >= arraySize) break + list.add(transform(this[i++], element)) + } + return list +} + +/** + * Splits the original collection into pair of collections, + * where *first* collection contains elements for which [predicate] yielded `true`, + * while *second* collection 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 collection into pair of collections, + * where *first* collection contains elements for which [predicate] yielded `true`, + * while *second* collection 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 collection into pair of collections, + * where *first* collection contains elements for which [predicate] yielded `true`, + * while *second* collection 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 collection into pair of collections, + * where *first* collection contains elements for which [predicate] yielded `true`, + * while *second* collection 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) +} + +/** + * Splits the original collection into pair of collections, + * where *first* collection contains elements for which [predicate] yielded `true`, + * while *second* collection 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 collection into pair of collections, + * where *first* collection contains elements for which [predicate] yielded `true`, + * while *second* collection 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 collection into pair of collections, + * where *first* collection contains elements for which [predicate] yielded `true`, + * while *second* collection 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 collection into pair of collections, + * where *first* collection contains elements for which [predicate] yielded `true`, + * while *second* collection 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 collection into pair of collections, + * where *first* collection contains elements for which [predicate] yielded `true`, + * while *second* collection 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) +} + +/** + * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. + */ +public fun Array.zip(array: Array): List> { + return merge(array) { 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 fun BooleanArray.zip(array: Array): List> { + return merge(array) { 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 fun ByteArray.zip(array: Array): List> { + return merge(array) { 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 fun CharArray.zip(array: Array): List> { + return merge(array) { 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 fun DoubleArray.zip(array: Array): List> { + return merge(array) { 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 fun FloatArray.zip(array: Array): List> { + return merge(array) { 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 fun IntArray.zip(array: Array): List> { + return merge(array) { 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 fun LongArray.zip(array: Array): List> { + return merge(array) { 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 fun ShortArray.zip(array: Array): List> { + return merge(array) { 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 fun BooleanArray.zip(array: BooleanArray): List> { + return merge(array) { 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 fun ByteArray.zip(array: ByteArray): List> { + return merge(array) { 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 fun CharArray.zip(array: CharArray): List> { + return merge(array) { 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 fun DoubleArray.zip(array: DoubleArray): List> { + return merge(array) { 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 fun FloatArray.zip(array: FloatArray): List> { + return merge(array) { 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 fun IntArray.zip(array: IntArray): List> { + return merge(array) { 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 fun LongArray.zip(array: LongArray): List> { + return merge(array) { 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 fun ShortArray.zip(array: ShortArray): List> { + return merge(array) { 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 fun Array.zip(other: Iterable): List> { + return merge(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 fun BooleanArray.zip(other: Iterable): List> { + return merge(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 fun ByteArray.zip(other: Iterable): List> { + return merge(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 fun CharArray.zip(other: Iterable): List> { + return merge(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 fun DoubleArray.zip(other: Iterable): List> { + return merge(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 fun FloatArray.zip(other: Iterable): List> { + return merge(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 fun IntArray.zip(other: Iterable): List> { + return merge(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 fun LongArray.zip(other: Iterable): List> { + return merge(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 fun ShortArray.zip(other: Iterable): List> { + return merge(other) { t1, t2 -> t1 to t2 } +} + +/** + * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. + * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] + * elements will be appended, followed by the [truncated] string (which defaults to "..."). + */ +public fun Array.joinTo(buffer: A, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((T) -> String)? = null): A { + buffer.append(prefix) + var count = 0 + for (element in this) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (transform != null) transform(element) else if (element == null) "null" else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) + return buffer +} + +/** + * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. + * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] + * elements will be appended, followed by the [truncated] string (which defaults to "..."). + */ +public fun BooleanArray.joinTo(buffer: A, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Boolean) -> String)? = null): A { + buffer.append(prefix) + var count = 0 + for (element in this) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (transform != null) transform(element) else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) + return buffer +} + +/** + * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. + * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] + * elements will be appended, followed by the [truncated] string (which defaults to "..."). + */ +public fun ByteArray.joinTo(buffer: A, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Byte) -> String)? = null): A { + buffer.append(prefix) + var count = 0 + for (element in this) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (transform != null) transform(element) else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) + return buffer +} + +/** + * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. + * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] + * elements will be appended, followed by the [truncated] string (which defaults to "..."). + */ +public fun CharArray.joinTo(buffer: A, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Char) -> String)? = null): A { + buffer.append(prefix) + var count = 0 + for (element in this) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (transform != null) transform(element) else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) + return buffer +} + +/** + * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. + * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] + * elements will be appended, followed by the [truncated] string (which defaults to "..."). + */ +public fun DoubleArray.joinTo(buffer: A, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Double) -> String)? = null): A { + buffer.append(prefix) + var count = 0 + for (element in this) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (transform != null) transform(element) else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) + return buffer +} + +/** + * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. + * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] + * elements will be appended, followed by the [truncated] string (which defaults to "..."). + */ +public fun FloatArray.joinTo(buffer: A, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Float) -> String)? = null): A { + buffer.append(prefix) + var count = 0 + for (element in this) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (transform != null) transform(element) else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) + return buffer +} + +/** + * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. + * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] + * elements will be appended, followed by the [truncated] string (which defaults to "..."). + */ +public fun IntArray.joinTo(buffer: A, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Int) -> String)? = null): A { + buffer.append(prefix) + var count = 0 + for (element in this) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (transform != null) transform(element) else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) + return buffer +} + +/** + * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. + * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] + * elements will be appended, followed by the [truncated] string (which defaults to "..."). + */ +public fun LongArray.joinTo(buffer: A, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Long) -> String)? = null): A { + buffer.append(prefix) + var count = 0 + for (element in this) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (transform != null) transform(element) else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) + return buffer +} + +/** + * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. + * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] + * elements will be appended, followed by the [truncated] string (which defaults to "..."). + */ +public fun ShortArray.joinTo(buffer: A, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Short) -> String)? = null): A { + buffer.append(prefix) + var count = 0 + for (element in this) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (transform != null) transform(element) else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) + return buffer +} + +/** + * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. + * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] + * elements will be appended, followed by the [truncated] string (which defaults to "..."). + */ +public fun Array.joinToString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((T) -> String)? = null): String { + return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() +} + +/** + * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. + * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] + * elements will be appended, followed by the [truncated] string (which defaults to "..."). + */ +public fun BooleanArray.joinToString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Boolean) -> String)? = null): String { + return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() +} + +/** + * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. + * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] + * elements will be appended, followed by the [truncated] string (which defaults to "..."). + */ +public fun ByteArray.joinToString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Byte) -> String)? = null): String { + return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() +} + +/** + * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. + * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] + * elements will be appended, followed by the [truncated] string (which defaults to "..."). + */ +public fun CharArray.joinToString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Char) -> String)? = null): String { + return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() +} + +/** + * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. + * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] + * elements will be appended, followed by the [truncated] string (which defaults to "..."). + */ +public fun DoubleArray.joinToString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Double) -> String)? = null): String { + return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() +} + +/** + * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. + * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] + * elements will be appended, followed by the [truncated] string (which defaults to "..."). + */ +public fun FloatArray.joinToString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Float) -> String)? = null): String { + return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() +} + +/** + * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. + * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] + * elements will be appended, followed by the [truncated] string (which defaults to "..."). + */ +public fun IntArray.joinToString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Int) -> String)? = null): String { + return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() +} + +/** + * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. + * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] + * elements will be appended, followed by the [truncated] string (which defaults to "..."). + */ +public fun LongArray.joinToString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Long) -> String)? = null): String { + return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() +} + +/** + * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. + * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] + * elements will be appended, followed by the [truncated] string (which defaults to "..."). + */ +public fun ShortArray.joinToString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Short) -> String)? = null): String { + return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() +} + +/** + * Returns a sequence from the given collection. + */ +public fun Array.asSequence(): Sequence { + if (isEmpty()) return emptySequence() + return object : Sequence { + override fun iterator(): Iterator { + return this@asSequence.iterator() + } + } +} + +/** + * Returns a sequence from the given collection. + */ +public fun BooleanArray.asSequence(): Sequence { + if (isEmpty()) return emptySequence() + return object : Sequence { + override fun iterator(): Iterator { + return this@asSequence.iterator() + } + } +} + +/** + * Returns a sequence from the given collection. + */ +public fun ByteArray.asSequence(): Sequence { + if (isEmpty()) return emptySequence() + return object : Sequence { + override fun iterator(): Iterator { + return this@asSequence.iterator() + } + } +} + +/** + * Returns a sequence from the given collection. + */ +public fun CharArray.asSequence(): Sequence { + if (isEmpty()) return emptySequence() + return object : Sequence { + override fun iterator(): Iterator { + return this@asSequence.iterator() + } + } +} + +/** + * Returns a sequence from the given collection. + */ +public fun DoubleArray.asSequence(): Sequence { + if (isEmpty()) return emptySequence() + return object : Sequence { + override fun iterator(): Iterator { + return this@asSequence.iterator() + } + } +} + +/** + * Returns a sequence from the given collection. + */ +public fun FloatArray.asSequence(): Sequence { + if (isEmpty()) return emptySequence() + return object : Sequence { + override fun iterator(): Iterator { + return this@asSequence.iterator() + } + } +} + +/** + * Returns a sequence from the given collection. + */ +public fun IntArray.asSequence(): Sequence { + if (isEmpty()) return emptySequence() + return object : Sequence { + override fun iterator(): Iterator { + return this@asSequence.iterator() + } + } +} + +/** + * Returns a sequence from the given collection. + */ +public fun LongArray.asSequence(): Sequence { + if (isEmpty()) return emptySequence() + return object : Sequence { + override fun iterator(): Iterator { + return this@asSequence.iterator() + } + } +} + +/** + * Returns a sequence from the given collection. + */ +public fun ShortArray.asSequence(): Sequence { + if (isEmpty()) return emptySequence() + return object : Sequence { + override fun iterator(): Iterator { + return this@asSequence.iterator() + } + } +} + +/** + * Returns a sequence from the given collection + */ +@Deprecated("Use asSequence() instead", ReplaceWith("asSequence()")) +public fun Array.sequence(): Sequence { + return asSequence() +} + +/** + * Returns a sequence from the given collection + */ +@Deprecated("Use asSequence() instead", ReplaceWith("asSequence()")) +public fun BooleanArray.sequence(): Sequence { + return asSequence() +} + +/** + * Returns a sequence from the given collection + */ +@Deprecated("Use asSequence() instead", ReplaceWith("asSequence()")) +public fun ByteArray.sequence(): Sequence { + return asSequence() +} + +/** + * Returns a sequence from the given collection + */ +@Deprecated("Use asSequence() instead", ReplaceWith("asSequence()")) +public fun CharArray.sequence(): Sequence { + return asSequence() +} + +/** + * Returns a sequence from the given collection + */ +@Deprecated("Use asSequence() instead", ReplaceWith("asSequence()")) +public fun DoubleArray.sequence(): Sequence { + return asSequence() +} + +/** + * Returns a sequence from the given collection + */ +@Deprecated("Use asSequence() instead", ReplaceWith("asSequence()")) +public fun FloatArray.sequence(): Sequence { + return asSequence() +} + +/** + * Returns a sequence from the given collection + */ +@Deprecated("Use asSequence() instead", ReplaceWith("asSequence()")) +public fun IntArray.sequence(): Sequence { + return asSequence() +} + +/** + * Returns a sequence from the given collection + */ +@Deprecated("Use asSequence() instead", ReplaceWith("asSequence()")) +public fun LongArray.sequence(): Sequence { + return asSequence() +} + +/** + * Returns a sequence from the given collection + */ +@Deprecated("Use asSequence() instead", ReplaceWith("asSequence()")) +public fun ShortArray.sequence(): Sequence { + return asSequence() +} + +/** + * Returns a [List] that wraps the original array. + */ +@kotlin.jvm.JvmVersion +public fun Array.asList(): List { + return Arrays.asList(*this) +} + +/** + * Returns a [List] that wraps the original array. + */ +@kotlin.jvm.JvmVersion +public fun BooleanArray.asList(): List { + return object : AbstractList(), RandomAccess { + override fun size(): Int = this@asList.size() + override fun isEmpty(): Boolean = this@asList.isEmpty() + override fun contains(o: Any?): Boolean = this@asList.contains(o as Boolean) + override fun iterator(): MutableIterator = this@asList.iterator() as MutableIterator + override fun get(index: Int): Boolean = this@asList[index] + override fun indexOf(o: Any?): Int = this@asList.indexOf(o as Boolean) + override fun lastIndexOf(o: Any?): Int = this@asList.lastIndexOf(o as Boolean) + } +} + +/** + * Returns a [List] that wraps the original array. + */ +@kotlin.jvm.JvmVersion +public fun ByteArray.asList(): List { + return object : AbstractList(), RandomAccess { + override fun size(): Int = this@asList.size() + override fun isEmpty(): Boolean = this@asList.isEmpty() + override fun contains(o: Any?): Boolean = this@asList.contains(o as Byte) + override fun iterator(): MutableIterator = this@asList.iterator() as MutableIterator + override fun get(index: Int): Byte = this@asList[index] + override fun indexOf(o: Any?): Int = this@asList.indexOf(o as Byte) + override fun lastIndexOf(o: Any?): Int = this@asList.lastIndexOf(o as Byte) + } +} + +/** + * Returns a [List] that wraps the original array. + */ +@kotlin.jvm.JvmVersion +public fun CharArray.asList(): List { + return object : AbstractList(), RandomAccess { + override fun size(): Int = this@asList.size() + override fun isEmpty(): Boolean = this@asList.isEmpty() + override fun contains(o: Any?): Boolean = this@asList.contains(o as Char) + override fun iterator(): MutableIterator = this@asList.iterator() as MutableIterator + override fun get(index: Int): Char = this@asList[index] + override fun indexOf(o: Any?): Int = this@asList.indexOf(o as Char) + override fun lastIndexOf(o: Any?): Int = this@asList.lastIndexOf(o as Char) + } +} + +/** + * Returns a [List] that wraps the original array. + */ +@kotlin.jvm.JvmVersion +public fun DoubleArray.asList(): List { + return object : AbstractList(), RandomAccess { + override fun size(): Int = this@asList.size() + override fun isEmpty(): Boolean = this@asList.isEmpty() + override fun contains(o: Any?): Boolean = this@asList.contains(o as Double) + override fun iterator(): MutableIterator = this@asList.iterator() as MutableIterator + override fun get(index: Int): Double = this@asList[index] + override fun indexOf(o: Any?): Int = this@asList.indexOf(o as Double) + override fun lastIndexOf(o: Any?): Int = this@asList.lastIndexOf(o as Double) + } +} + +/** + * Returns a [List] that wraps the original array. + */ +@kotlin.jvm.JvmVersion +public fun FloatArray.asList(): List { + return object : AbstractList(), RandomAccess { + override fun size(): Int = this@asList.size() + override fun isEmpty(): Boolean = this@asList.isEmpty() + override fun contains(o: Any?): Boolean = this@asList.contains(o as Float) + override fun iterator(): MutableIterator = this@asList.iterator() as MutableIterator + override fun get(index: Int): Float = this@asList[index] + override fun indexOf(o: Any?): Int = this@asList.indexOf(o as Float) + override fun lastIndexOf(o: Any?): Int = this@asList.lastIndexOf(o as Float) + } +} + +/** + * Returns a [List] that wraps the original array. + */ +@kotlin.jvm.JvmVersion +public fun IntArray.asList(): List { + return object : AbstractList(), RandomAccess { + override fun size(): Int = this@asList.size() + override fun isEmpty(): Boolean = this@asList.isEmpty() + override fun contains(o: Any?): Boolean = this@asList.contains(o as Int) + override fun iterator(): MutableIterator = this@asList.iterator() as MutableIterator + override fun get(index: Int): Int = this@asList[index] + override fun indexOf(o: Any?): Int = this@asList.indexOf(o as Int) + override fun lastIndexOf(o: Any?): Int = this@asList.lastIndexOf(o as Int) + } +} + +/** + * Returns a [List] that wraps the original array. + */ +@kotlin.jvm.JvmVersion +public fun LongArray.asList(): List { + return object : AbstractList(), RandomAccess { + override fun size(): Int = this@asList.size() + override fun isEmpty(): Boolean = this@asList.isEmpty() + override fun contains(o: Any?): Boolean = this@asList.contains(o as Long) + override fun iterator(): MutableIterator = this@asList.iterator() as MutableIterator + override fun get(index: Int): Long = this@asList[index] + override fun indexOf(o: Any?): Int = this@asList.indexOf(o as Long) + override fun lastIndexOf(o: Any?): Int = this@asList.lastIndexOf(o as Long) + } +} + +/** + * Returns a [List] that wraps the original array. + */ +@kotlin.jvm.JvmVersion +public fun ShortArray.asList(): List { + return object : AbstractList(), RandomAccess { + override fun size(): Int = this@asList.size() + override fun isEmpty(): Boolean = this@asList.isEmpty() + override fun contains(o: Any?): Boolean = this@asList.contains(o as Short) + override fun iterator(): MutableIterator = this@asList.iterator() as MutableIterator + override fun get(index: Int): Short = this@asList[index] + override fun indexOf(o: Any?): Int = this@asList.indexOf(o as Short) + override fun lastIndexOf(o: Any?): Int = this@asList.lastIndexOf(o as Short) + } +} + +/** + * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted according to the specified [comparator]. + */ +@kotlin.jvm.JvmVersion +public fun Array.binarySearch(element: T, comparator: Comparator, fromIndex: Int = 0, toIndex: Int = size()): Int { + return Arrays.binarySearch(this, fromIndex, toIndex, element, comparator) +} + +/** + * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. + */ +@kotlin.jvm.JvmVersion +public fun Array.binarySearch(element: T, fromIndex: Int = 0, toIndex: Int = size()): Int { + return Arrays.binarySearch(this, fromIndex, toIndex, element) +} + +/** + * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. + */ +@kotlin.jvm.JvmVersion +public fun ByteArray.binarySearch(element: Byte, fromIndex: Int = 0, toIndex: Int = size()): Int { + return Arrays.binarySearch(this, fromIndex, toIndex, element) +} + +/** + * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. + */ +@kotlin.jvm.JvmVersion +public fun CharArray.binarySearch(element: Char, fromIndex: Int = 0, toIndex: Int = size()): Int { + return Arrays.binarySearch(this, fromIndex, toIndex, element) +} + +/** + * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. + */ +@kotlin.jvm.JvmVersion +public fun DoubleArray.binarySearch(element: Double, fromIndex: Int = 0, toIndex: Int = size()): Int { + return Arrays.binarySearch(this, fromIndex, toIndex, element) +} + +/** + * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. + */ +@kotlin.jvm.JvmVersion +public fun FloatArray.binarySearch(element: Float, fromIndex: Int = 0, toIndex: Int = size()): Int { + return Arrays.binarySearch(this, fromIndex, toIndex, element) +} + +/** + * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. + */ +@kotlin.jvm.JvmVersion +public fun IntArray.binarySearch(element: Int, fromIndex: Int = 0, toIndex: Int = size()): Int { + return Arrays.binarySearch(this, fromIndex, toIndex, element) +} + +/** + * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. + */ +@kotlin.jvm.JvmVersion +public fun LongArray.binarySearch(element: Long, fromIndex: Int = 0, toIndex: Int = size()): Int { + return Arrays.binarySearch(this, fromIndex, toIndex, element) +} + +/** + * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. + */ +@kotlin.jvm.JvmVersion +public fun ShortArray.binarySearch(element: Short, fromIndex: Int = 0, toIndex: Int = size()): Int { + return Arrays.binarySearch(this, fromIndex, toIndex, element) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.jvm.JvmVersion +public fun Array.copyOf(): Array { + return Arrays.copyOf(this, size()) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.jvm.JvmVersion +public fun BooleanArray.copyOf(): BooleanArray { + return Arrays.copyOf(this, size()) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.jvm.JvmVersion +public fun ByteArray.copyOf(): ByteArray { + return Arrays.copyOf(this, size()) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.jvm.JvmVersion +public fun CharArray.copyOf(): CharArray { + return Arrays.copyOf(this, size()) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.jvm.JvmVersion +public fun DoubleArray.copyOf(): DoubleArray { + return Arrays.copyOf(this, size()) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.jvm.JvmVersion +public fun FloatArray.copyOf(): FloatArray { + return Arrays.copyOf(this, size()) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.jvm.JvmVersion +public fun IntArray.copyOf(): IntArray { + return Arrays.copyOf(this, size()) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.jvm.JvmVersion +public fun LongArray.copyOf(): LongArray { + return Arrays.copyOf(this, size()) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.jvm.JvmVersion +public fun ShortArray.copyOf(): ShortArray { + return Arrays.copyOf(this, size()) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.jvm.JvmVersion +@JvmName("mutableCopyOf") +public fun Array.copyOf(): Array { + return Arrays.copyOf(this, size()) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.jvm.JvmVersion +public fun Array.copyOf(newSize: Int): Array { + return Arrays.copyOf(this, newSize) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.jvm.JvmVersion +public fun BooleanArray.copyOf(newSize: Int): BooleanArray { + return Arrays.copyOf(this, newSize) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.jvm.JvmVersion +public fun ByteArray.copyOf(newSize: Int): ByteArray { + return Arrays.copyOf(this, newSize) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.jvm.JvmVersion +public fun CharArray.copyOf(newSize: Int): CharArray { + return Arrays.copyOf(this, newSize) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.jvm.JvmVersion +public fun DoubleArray.copyOf(newSize: Int): DoubleArray { + return Arrays.copyOf(this, newSize) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.jvm.JvmVersion +public fun FloatArray.copyOf(newSize: Int): FloatArray { + return Arrays.copyOf(this, newSize) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.jvm.JvmVersion +public fun IntArray.copyOf(newSize: Int): IntArray { + return Arrays.copyOf(this, newSize) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.jvm.JvmVersion +public fun LongArray.copyOf(newSize: Int): LongArray { + return Arrays.copyOf(this, newSize) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.jvm.JvmVersion +public fun ShortArray.copyOf(newSize: Int): ShortArray { + return Arrays.copyOf(this, newSize) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.jvm.JvmVersion +@JvmName("mutableCopyOf") +public fun Array.copyOf(newSize: Int): Array { + return Arrays.copyOf(this, newSize) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@kotlin.jvm.JvmVersion +public fun Array.copyOfRange(fromIndex: Int, toIndex: Int): Array { + return Arrays.copyOfRange(this, fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@kotlin.jvm.JvmVersion +public fun BooleanArray.copyOfRange(fromIndex: Int, toIndex: Int): BooleanArray { + return Arrays.copyOfRange(this, fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@kotlin.jvm.JvmVersion +public fun ByteArray.copyOfRange(fromIndex: Int, toIndex: Int): ByteArray { + return Arrays.copyOfRange(this, fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@kotlin.jvm.JvmVersion +public fun CharArray.copyOfRange(fromIndex: Int, toIndex: Int): CharArray { + return Arrays.copyOfRange(this, fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@kotlin.jvm.JvmVersion +public fun DoubleArray.copyOfRange(fromIndex: Int, toIndex: Int): DoubleArray { + return Arrays.copyOfRange(this, fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@kotlin.jvm.JvmVersion +public fun FloatArray.copyOfRange(fromIndex: Int, toIndex: Int): FloatArray { + return Arrays.copyOfRange(this, fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@kotlin.jvm.JvmVersion +public fun IntArray.copyOfRange(fromIndex: Int, toIndex: Int): IntArray { + return Arrays.copyOfRange(this, fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@kotlin.jvm.JvmVersion +public fun LongArray.copyOfRange(fromIndex: Int, toIndex: Int): LongArray { + return Arrays.copyOfRange(this, fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@kotlin.jvm.JvmVersion +public fun ShortArray.copyOfRange(fromIndex: Int, toIndex: Int): ShortArray { + return Arrays.copyOfRange(this, fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@kotlin.jvm.JvmVersion +@JvmName("mutableCopyOfRange") +public fun Array.copyOfRange(fromIndex: Int, toIndex: Int): Array { + return Arrays.copyOfRange(this, fromIndex, toIndex) +} + +/** + * Fills original array with the provided value. + */ +@kotlin.jvm.JvmVersion +public fun BooleanArray.fill(element: Boolean, fromIndex: Int = 0, toIndex: Int = size()): Unit { + Arrays.fill(this, fromIndex, toIndex, element) +} + +/** + * Fills original array with the provided value. + */ +@kotlin.jvm.JvmVersion +public fun ByteArray.fill(element: Byte, fromIndex: Int = 0, toIndex: Int = size()): Unit { + Arrays.fill(this, fromIndex, toIndex, element) +} + +/** + * Fills original array with the provided value. + */ +@kotlin.jvm.JvmVersion +public fun CharArray.fill(element: Char, fromIndex: Int = 0, toIndex: Int = size()): Unit { + Arrays.fill(this, fromIndex, toIndex, element) +} + +/** + * Fills original array with the provided value. + */ +@kotlin.jvm.JvmVersion +public fun DoubleArray.fill(element: Double, fromIndex: Int = 0, toIndex: Int = size()): Unit { + Arrays.fill(this, fromIndex, toIndex, element) +} + +/** + * Fills original array with the provided value. + */ +@kotlin.jvm.JvmVersion +public fun FloatArray.fill(element: Float, fromIndex: Int = 0, toIndex: Int = size()): Unit { + Arrays.fill(this, fromIndex, toIndex, element) +} + +/** + * Fills original array with the provided value. + */ +@kotlin.jvm.JvmVersion +public fun IntArray.fill(element: Int, fromIndex: Int = 0, toIndex: Int = size()): Unit { + Arrays.fill(this, fromIndex, toIndex, element) +} + +/** + * Fills original array with the provided value. + */ +@kotlin.jvm.JvmVersion +public fun LongArray.fill(element: Long, fromIndex: Int = 0, toIndex: Int = size()): Unit { + Arrays.fill(this, fromIndex, toIndex, element) +} + +/** + * Fills original array with the provided value. + */ +@kotlin.jvm.JvmVersion +public fun ShortArray.fill(element: Short, fromIndex: Int = 0, toIndex: Int = size()): Unit { + Arrays.fill(this, fromIndex, toIndex, element) +} + +/** + * Fills original array with the provided value. + */ +@kotlin.jvm.JvmVersion +public fun Array.fill(element: T, fromIndex: Int = 0, toIndex: Int = size()): Unit { + Arrays.fill(this, fromIndex, toIndex, element) +} + +/** + * Returns a list containing all elements that are instances of specified type parameter R. + */ +@kotlin.jvm.JvmVersion +public inline fun Array<*>.filterIsInstance(): List { + return filterIsInstanceTo(ArrayList()) +} + +/** + * Returns a list containing all elements that are instances of specified class. + */ +@kotlin.jvm.JvmVersion +public fun Array<*>.filterIsInstance(klass: Class): List { + return filterIsInstanceTo(ArrayList(), klass) +} + +/** + * Appends all elements that are instances of specified type parameter R to the given [destination]. + */ +@kotlin.jvm.JvmVersion +public inline fun > Array<*>.filterIsInstanceTo(destination: C): C { + for (element in this) if (element is R) destination.add(element) + return destination +} + +/** + * Appends all elements that are instances of specified class to the given [destination]. + */ +@kotlin.jvm.JvmVersion +public fun , R> Array<*>.filterIsInstanceTo(destination: C, klass: Class): C { + for (element in this) if (klass.isInstance(element)) destination.add(element as R) + return destination +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [array]. + */ +@kotlin.jvm.JvmVersion +public operator fun BooleanArray.plus(array: BooleanArray): BooleanArray { + val thisSize = size() + val arraySize = array.size() + val result = Arrays.copyOf(this, thisSize + arraySize) + System.arraycopy(array, 0, result, thisSize, arraySize) + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [array]. + */ +@kotlin.jvm.JvmVersion +public operator fun ByteArray.plus(array: ByteArray): ByteArray { + val thisSize = size() + val arraySize = array.size() + val result = Arrays.copyOf(this, thisSize + arraySize) + System.arraycopy(array, 0, result, thisSize, arraySize) + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [array]. + */ +@kotlin.jvm.JvmVersion +public operator fun CharArray.plus(array: CharArray): CharArray { + val thisSize = size() + val arraySize = array.size() + val result = Arrays.copyOf(this, thisSize + arraySize) + System.arraycopy(array, 0, result, thisSize, arraySize) + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [array]. + */ +@kotlin.jvm.JvmVersion +public operator fun DoubleArray.plus(array: DoubleArray): DoubleArray { + val thisSize = size() + val arraySize = array.size() + val result = Arrays.copyOf(this, thisSize + arraySize) + System.arraycopy(array, 0, result, thisSize, arraySize) + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [array]. + */ +@kotlin.jvm.JvmVersion +public operator fun FloatArray.plus(array: FloatArray): FloatArray { + val thisSize = size() + val arraySize = array.size() + val result = Arrays.copyOf(this, thisSize + arraySize) + System.arraycopy(array, 0, result, thisSize, arraySize) + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [array]. + */ +@kotlin.jvm.JvmVersion +public operator fun IntArray.plus(array: IntArray): IntArray { + val thisSize = size() + val arraySize = array.size() + val result = Arrays.copyOf(this, thisSize + arraySize) + System.arraycopy(array, 0, result, thisSize, arraySize) + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [array]. + */ +@kotlin.jvm.JvmVersion +public operator fun LongArray.plus(array: LongArray): LongArray { + val thisSize = size() + val arraySize = array.size() + val result = Arrays.copyOf(this, thisSize + arraySize) + System.arraycopy(array, 0, result, thisSize, arraySize) + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [array]. + */ +@kotlin.jvm.JvmVersion +public operator fun ShortArray.plus(array: ShortArray): ShortArray { + val thisSize = size() + val arraySize = array.size() + val result = Arrays.copyOf(this, thisSize + arraySize) + System.arraycopy(array, 0, result, thisSize, arraySize) + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [array]. + */ +@kotlin.jvm.JvmVersion +public operator fun Array.plus(array: Array): Array { + val thisSize = size() + val arraySize = array.size() + val result = Arrays.copyOf(this, thisSize + arraySize) + System.arraycopy(array, 0, result, thisSize, arraySize) + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [collection]. + */ +@kotlin.jvm.JvmVersion +public operator fun BooleanArray.plus(collection: Collection): BooleanArray { + var index = size() + val result = Arrays.copyOf(this, index + collection.size()) + for (element in collection) result[index++] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [collection]. + */ +@kotlin.jvm.JvmVersion +public operator fun ByteArray.plus(collection: Collection): ByteArray { + var index = size() + val result = Arrays.copyOf(this, index + collection.size()) + for (element in collection) result[index++] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [collection]. + */ +@kotlin.jvm.JvmVersion +public operator fun CharArray.plus(collection: Collection): CharArray { + var index = size() + val result = Arrays.copyOf(this, index + collection.size()) + for (element in collection) result[index++] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [collection]. + */ +@kotlin.jvm.JvmVersion +public operator fun DoubleArray.plus(collection: Collection): DoubleArray { + var index = size() + val result = Arrays.copyOf(this, index + collection.size()) + for (element in collection) result[index++] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [collection]. + */ +@kotlin.jvm.JvmVersion +public operator fun FloatArray.plus(collection: Collection): FloatArray { + var index = size() + val result = Arrays.copyOf(this, index + collection.size()) + for (element in collection) result[index++] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [collection]. + */ +@kotlin.jvm.JvmVersion +public operator fun IntArray.plus(collection: Collection): IntArray { + var index = size() + val result = Arrays.copyOf(this, index + collection.size()) + for (element in collection) result[index++] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [collection]. + */ +@kotlin.jvm.JvmVersion +public operator fun LongArray.plus(collection: Collection): LongArray { + var index = size() + val result = Arrays.copyOf(this, index + collection.size()) + for (element in collection) result[index++] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [collection]. + */ +@kotlin.jvm.JvmVersion +public operator fun ShortArray.plus(collection: Collection): ShortArray { + var index = size() + val result = Arrays.copyOf(this, index + collection.size()) + for (element in collection) result[index++] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [collection]. + */ +@kotlin.jvm.JvmVersion +public operator fun Array.plus(collection: Collection): Array { + var index = size() + val result = Arrays.copyOf(this, index + collection.size()) + for (element in collection) result[index++] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +@kotlin.jvm.JvmVersion +public operator fun BooleanArray.plus(element: Boolean): BooleanArray { + val index = size() + val result = Arrays.copyOf(this, index + 1) + result[index] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +@kotlin.jvm.JvmVersion +public operator fun ByteArray.plus(element: Byte): ByteArray { + val index = size() + val result = Arrays.copyOf(this, index + 1) + result[index] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +@kotlin.jvm.JvmVersion +public operator fun CharArray.plus(element: Char): CharArray { + val index = size() + val result = Arrays.copyOf(this, index + 1) + result[index] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +@kotlin.jvm.JvmVersion +public operator fun DoubleArray.plus(element: Double): DoubleArray { + val index = size() + val result = Arrays.copyOf(this, index + 1) + result[index] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +@kotlin.jvm.JvmVersion +public operator fun FloatArray.plus(element: Float): FloatArray { + val index = size() + val result = Arrays.copyOf(this, index + 1) + result[index] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +@kotlin.jvm.JvmVersion +public operator fun IntArray.plus(element: Int): IntArray { + val index = size() + val result = Arrays.copyOf(this, index + 1) + result[index] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +@kotlin.jvm.JvmVersion +public operator fun LongArray.plus(element: Long): LongArray { + val index = size() + val result = Arrays.copyOf(this, index + 1) + result[index] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +@kotlin.jvm.JvmVersion +public operator fun ShortArray.plus(element: Short): ShortArray { + val index = size() + val result = Arrays.copyOf(this, index + 1) + result[index] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +@kotlin.jvm.JvmVersion +public operator fun Array.plus(element: T): Array { + val index = size() + val result = Arrays.copyOf(this, index + 1) + result[index] = element + return result +} + +/** + * Sorts array or range in array inplace. + */ +@kotlin.jvm.JvmVersion +public fun Array.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { + Arrays.sort(this, fromIndex, toIndex) +} + +/** + * Sorts array or range in array inplace. + */ +@kotlin.jvm.JvmVersion +public fun ByteArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { + Arrays.sort(this, fromIndex, toIndex) +} + +/** + * Sorts array or range in array inplace. + */ +@kotlin.jvm.JvmVersion +public fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { + Arrays.sort(this, fromIndex, toIndex) +} + +/** + * Sorts array or range in array inplace. + */ +@kotlin.jvm.JvmVersion +public fun DoubleArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { + Arrays.sort(this, fromIndex, toIndex) +} + +/** + * Sorts array or range in array inplace. + */ +@kotlin.jvm.JvmVersion +public fun FloatArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { + Arrays.sort(this, fromIndex, toIndex) +} + +/** + * Sorts array or range in array inplace. + */ +@kotlin.jvm.JvmVersion +public fun IntArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { + Arrays.sort(this, fromIndex, toIndex) +} + +/** + * Sorts array or range in array inplace. + */ +@kotlin.jvm.JvmVersion +public fun LongArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { + Arrays.sort(this, fromIndex, toIndex) +} + +/** + * Sorts array or range in array inplace. + */ +@kotlin.jvm.JvmVersion +public fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { + Arrays.sort(this, fromIndex, toIndex) +} + +/** + * Sorts array or range in array inplace. + */ +@kotlin.jvm.JvmVersion +public fun Array.sortWith(comparator: Comparator, fromIndex: Int = 0, toIndex: Int = size()): Unit { + Arrays.sort(this, fromIndex, toIndex, comparator) +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +@kotlin.jvm.JvmVersion +public fun BooleanArray.toTypedArray(): Array { + val result = arrayOfNulls(size()) + for (index in indices) + result[index] = this[index] + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +@kotlin.jvm.JvmVersion +public fun ByteArray.toTypedArray(): Array { + val result = arrayOfNulls(size()) + for (index in indices) + result[index] = this[index] + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +@kotlin.jvm.JvmVersion +public fun CharArray.toTypedArray(): Array { + val result = arrayOfNulls(size()) + for (index in indices) + result[index] = this[index] + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +@kotlin.jvm.JvmVersion +public fun DoubleArray.toTypedArray(): Array { + val result = arrayOfNulls(size()) + for (index in indices) + result[index] = this[index] + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +@kotlin.jvm.JvmVersion +public fun FloatArray.toTypedArray(): Array { + val result = arrayOfNulls(size()) + for (index in indices) + result[index] = this[index] + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +@kotlin.jvm.JvmVersion +public fun IntArray.toTypedArray(): Array { + val result = arrayOfNulls(size()) + for (index in indices) + result[index] = this[index] + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +@kotlin.jvm.JvmVersion +public fun LongArray.toTypedArray(): Array { + val result = arrayOfNulls(size()) + for (index in indices) + result[index] = this[index] + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +@kotlin.jvm.JvmVersion +public fun ShortArray.toTypedArray(): Array { + val result = arrayOfNulls(size()) + for (index in indices) + result[index] = this[index] + return result as Array +} + +/** + * Returns an average value of elements in the collection. + */ +@kotlin.jvm.JvmName("averageOfByte") +public fun Array.average(): Double { + val iterator = iterator() + var sum: Double = 0.0 + var count: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + count += 1 + } + return if (count == 0) 0.0 else sum / count +} + +/** + * Returns an average value of elements in the collection. + */ +@kotlin.jvm.JvmName("averageOfDouble") +public fun Array.average(): Double { + val iterator = iterator() + var sum: Double = 0.0 + var count: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + count += 1 + } + return if (count == 0) 0.0 else sum / count +} + +/** + * Returns an average value of elements in the collection. + */ +@kotlin.jvm.JvmName("averageOfFloat") +public fun Array.average(): Double { + val iterator = iterator() + var sum: Double = 0.0 + var count: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + count += 1 + } + return if (count == 0) 0.0 else sum / count +} + +/** + * Returns an average value of elements in the collection. + */ +@kotlin.jvm.JvmName("averageOfInt") +public fun Array.average(): Double { + val iterator = iterator() + var sum: Double = 0.0 + var count: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + count += 1 + } + return if (count == 0) 0.0 else sum / count +} + +/** + * Returns an average value of elements in the collection. + */ +@kotlin.jvm.JvmName("averageOfLong") +public fun Array.average(): Double { + val iterator = iterator() + var sum: Double = 0.0 + var count: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + count += 1 + } + return if (count == 0) 0.0 else sum / count +} + +/** + * Returns an average value of elements in the collection. + */ +@kotlin.jvm.JvmName("averageOfShort") +public fun Array.average(): Double { + val iterator = iterator() + var sum: Double = 0.0 + var count: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + count += 1 + } + return if (count == 0) 0.0 else sum / count +} + +/** + * Returns an average value of elements in the collection. + */ +public fun ByteArray.average(): Double { + val iterator = iterator() + var sum: Double = 0.0 + var count: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + count += 1 + } + return if (count == 0) 0.0 else sum / count +} + +/** + * Returns an average value of elements in the collection. + */ +public fun DoubleArray.average(): Double { + val iterator = iterator() + var sum: Double = 0.0 + var count: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + count += 1 + } + return if (count == 0) 0.0 else sum / count +} + +/** + * Returns an average value of elements in the collection. + */ +public fun FloatArray.average(): Double { + val iterator = iterator() + var sum: Double = 0.0 + var count: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + count += 1 + } + return if (count == 0) 0.0 else sum / count +} + +/** + * Returns an average value of elements in the collection. + */ +public fun IntArray.average(): Double { + val iterator = iterator() + var sum: Double = 0.0 + var count: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + count += 1 + } + return if (count == 0) 0.0 else sum / count +} + +/** + * Returns an average value of elements in the collection. + */ +public fun LongArray.average(): Double { + val iterator = iterator() + var sum: Double = 0.0 + var count: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + count += 1 + } + return if (count == 0) 0.0 else sum / count +} + +/** + * Returns an average value of elements in the collection. + */ +public fun ShortArray.average(): Double { + val iterator = iterator() + var sum: Double = 0.0 + var count: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + count += 1 + } + return if (count == 0) 0.0 else sum / count +} + +/** + * Returns the sum of all elements in the collection. + */ +@kotlin.jvm.JvmName("sumOfByte") +public fun Array.sum(): Int { + val iterator = iterator() + var sum: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +@kotlin.jvm.JvmName("sumOfDouble") +public fun Array.sum(): Double { + val iterator = iterator() + var sum: Double = 0.0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +@kotlin.jvm.JvmName("sumOfFloat") +public fun Array.sum(): Float { + val iterator = iterator() + var sum: Float = 0.0f + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +@kotlin.jvm.JvmName("sumOfInt") +public fun Array.sum(): Int { + val iterator = iterator() + var sum: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +@kotlin.jvm.JvmName("sumOfLong") +public fun Array.sum(): Long { + val iterator = iterator() + var sum: Long = 0L + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +@kotlin.jvm.JvmName("sumOfShort") +public fun Array.sum(): Int { + val iterator = iterator() + var sum: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +public fun ByteArray.sum(): Int { + val iterator = iterator() + var sum: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +public fun DoubleArray.sum(): Double { + val iterator = iterator() + var sum: Double = 0.0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +public fun FloatArray.sum(): Float { + val iterator = iterator() + var sum: Float = 0.0f + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +public fun IntArray.sum(): Int { + val iterator = iterator() + var sum: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +public fun LongArray.sum(): Long { + val iterator = iterator() + var sum: Long = 0L + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +public fun ShortArray.sum(): Int { + val iterator = iterator() + var sum: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + diff --git a/libraries/stdlib/src/generated/_ArraysToPrimitiveArrays.kt b/libraries/stdlib/src/generated/_ArraysToPrimitiveArrays.kt deleted file mode 100644 index 14175cea9e1..00000000000 --- a/libraries/stdlib/src/generated/_ArraysToPrimitiveArrays.kt +++ /dev/null @@ -1,180 +0,0 @@ -package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// - -import kotlin.platform.* -import java.util.* - -import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js - -/** - * 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 Boolean containing all of the elements of this collection. - */ -public fun Collection.toBooleanArray(): BooleanArray { - val result = BooleanArray(size()) - var index = 0 - for (element in this) - result[index++] = element - return result -} - -/** - * Returns an array of Byte containing all of the elements of this 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 Byte containing all of the elements of this collection. - */ -public fun Collection.toByteArray(): ByteArray { - val result = ByteArray(size()) - var index = 0 - for (element in this) - result[index++] = element - return result -} - -/** - * Returns an array of Char containing all of the elements of this 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 Char containing all of the elements of this collection. - */ -public fun Collection.toCharArray(): CharArray { - val result = CharArray(size()) - var index = 0 - for (element in this) - result[index++] = element - return result -} - -/** - * Returns an array of Double containing all of the elements of this 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 Double containing all of the elements of this collection. - */ -public fun Collection.toDoubleArray(): DoubleArray { - val result = DoubleArray(size()) - var index = 0 - for (element in this) - result[index++] = element - return result -} - -/** - * Returns an array of Float containing all of the elements of this 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 Float containing all of the elements of this collection. - */ -public fun Collection.toFloatArray(): FloatArray { - val result = FloatArray(size()) - var index = 0 - for (element in this) - result[index++] = element - return result -} - -/** - * Returns an array of Int containing all of the elements of this 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 Int containing all of the elements of this collection. - */ -public fun Collection.toIntArray(): IntArray { - val result = IntArray(size()) - var index = 0 - for (element in this) - result[index++] = element - return result -} - -/** - * Returns an array of Long containing all of the elements of this 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 Long containing all of the elements of this collection. - */ -public fun Collection.toLongArray(): LongArray { - val result = LongArray(size()) - var index = 0 - for (element in this) - result[index++] = element - return result -} - -/** - * Returns an array of Short containing all of the elements of this generic array. - */ -public fun Array.toShortArray(): ShortArray { - val result = ShortArray(size()) - for (index in indices) - result[index] = this[index] - return result -} - -/** - * Returns an array of Short containing all of the elements of this collection. - */ -public fun Collection.toShortArray(): ShortArray { - val result = ShortArray(size()) - var index = 0 - for (element in this) - result[index++] = element - return result -} - diff --git a/libraries/stdlib/src/generated/_Collections.kt b/libraries/stdlib/src/generated/_Collections.kt new file mode 100644 index 00000000000..294dbe9e38c --- /dev/null +++ b/libraries/stdlib/src/generated/_Collections.kt @@ -0,0 +1,1860 @@ +@file:kotlin.jvm.JvmMultifileClass +@file:kotlin.jvm.JvmName("CollectionsKt") + +package kotlin + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// + +import java.util.* + +import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js + +/** + * Returns 1st *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun List.component1(): T { + return get(0) +} + +/** + * Returns 2nd *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun List.component2(): T { + return get(1) +} + +/** + * Returns 3rd *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun List.component3(): T { + return get(2) +} + +/** + * Returns 4th *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun List.component4(): T { + return get(3) +} + +/** + * Returns 5th *element* from the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun List.component5(): T { + return get(4) +} + +/** + * Returns `true` if [element] is found in the collection. + */ +public operator fun Iterable.contains(element: T): Boolean { + if (this is Collection) + return contains(element) + return indexOf(element) >= 0 +} + +/** + * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. + */ +public fun Iterable.elementAt(index: Int): T { + if (this is List) + return get(index) + return elementAtOrElse(index) { throw IndexOutOfBoundsException("Collection doesn't contain element at index $index.") } +} + +/** + * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. + */ +public fun List.elementAt(index: Int): T { + return get(index) +} + +/** + * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. + */ +public fun Iterable.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T { + if (this is List) + return this.getOrElse(index, defaultValue) + if (index < 0) + return defaultValue(index) + val iterator = iterator() + var count = 0 + while (iterator.hasNext()) { + val element = iterator.next() + if (index == count++) + return element + } + return defaultValue(index) +} + +/** + * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. + */ +public inline fun List.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T { + return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) +} + +/** + * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. + */ +public fun Iterable.elementAtOrNull(index: Int): T? { + if (this is List) + return this.getOrNull(index) + if (index < 0) + return null + val iterator = iterator() + var count = 0 + while (iterator.hasNext()) { + val element = iterator.next() + if (index == count++) + return element + } + return null +} + +/** + * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. + */ +public fun List.elementAtOrNull(index: Int): T? { + return if (index >= 0 && index <= lastIndex) get(index) else null +} + +/** + * Returns the first element matching the given [predicate], or `null` if element was not found. + */ +public inline fun Iterable.find(predicate: (T) -> Boolean): T? { + return firstOrNull(predicate) +} + +/** + * Returns the last element matching the given [predicate], or `null` if no such element was found. + */ +public inline fun Iterable.findLast(predicate: (T) -> Boolean): T? { + return lastOrNull(predicate) +} + +/** + * Returns the last element matching the given [predicate], or `null` if no such element was found. + */ +public inline fun List.findLast(predicate: (T) -> Boolean): T? { + return lastOrNull(predicate) +} + +/** + * Returns first element. + * @throws [NoSuchElementException] if the collection is empty. + */ +public fun Iterable.first(): T { + when (this) { + is List -> { + if (isEmpty()) + throw NoSuchElementException("Collection is empty.") + else + return this[0] + } + else -> { + val iterator = iterator() + if (!iterator.hasNext()) + throw NoSuchElementException("Collection is empty.") + return iterator.next() + } + } +} + +/** + * Returns first element. + * @throws [NoSuchElementException] if the collection is empty. + */ +public fun List.first(): T { + if (isEmpty()) + throw NoSuchElementException("Collection is empty.") + return this[0] +} + +/** + * Returns the first element matching the given [predicate]. + * @throws [NoSuchElementException] if no such element is found. + */ +public inline fun Iterable.first(predicate: (T) -> Boolean): T { + for (element in this) if (predicate(element)) return element + throw NoSuchElementException("No element matching predicate was found.") +} + +/** + * Returns the first element, or `null` if the collection is empty. + */ +public fun Iterable.firstOrNull(): T? { + when (this) { + is List -> { + if (isEmpty()) + return null + else + return this[0] + } + else -> { + val iterator = iterator() + if (!iterator.hasNext()) + return null + return iterator.next() + } + } +} + +/** + * Returns the first element, or `null` if the collection is empty. + */ +public fun List.firstOrNull(): T? { + return if (isEmpty()) null else this[0] +} + +/** + * Returns the first element matching the given [predicate], or `null` if element was not found. + */ +public inline fun Iterable.firstOrNull(predicate: (T) -> Boolean): T? { + for (element in this) if (predicate(element)) return element + return null +} + +/** + * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. + */ +public inline fun List.getOrElse(index: Int, defaultValue: (Int) -> T): T { + return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) +} + +/** + * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. + */ +public fun List.getOrNull(index: Int): T? { + return if (index >= 0 && index <= lastIndex) get(index) else null +} + +/** + * Returns first index of [element], or -1 if the collection does not contain element. + */ +public fun Iterable.indexOf(element: T): Int { + var index = 0 + for (item in this) { + if (element == item) + return index + index++ + } + return -1 +} + +/** + * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element. + */ +public inline fun Iterable.indexOfFirst(predicate: (T) -> Boolean): Int { + var index = 0 + for (item in this) { + if (predicate(item)) + return index + index++ + } + return -1 +} + +/** + * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element. + */ +public inline fun List.indexOfFirst(predicate: (T) -> Boolean): Int { + for (index in indices) { + if (predicate(this[index])) { + return index + } + } + return -1 +} + +/** + * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element. + */ +public inline fun Iterable.indexOfLast(predicate: (T) -> Boolean): Int { + var lastIndex = -1 + var index = 0 + for (item in this) { + if (predicate(item)) + lastIndex = index + index++ + } + return lastIndex +} + +/** + * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element. + */ +public inline fun List.indexOfLast(predicate: (T) -> Boolean): Int { + for (index in indices.reversed()) { + if (predicate(this[index])) { + return index + } + } + return -1 +} + +/** + * Returns the last element. + * @throws [NoSuchElementException] if the collection is empty. + */ +public fun Iterable.last(): T { + when (this) { + is List -> { + if (isEmpty()) + throw NoSuchElementException("Collection is empty.") + else + return this[this.lastIndex] + } + else -> { + val iterator = iterator() + if (!iterator.hasNext()) + throw NoSuchElementException("Collection is empty.") + var last = iterator.next() + while (iterator.hasNext()) + last = iterator.next() + return last + } + } +} + +/** + * Returns the last element. + * @throws [NoSuchElementException] if the collection is empty. + */ +public fun List.last(): T { + if (isEmpty()) + throw NoSuchElementException("Collection is empty.") + return this[lastIndex] +} + +/** + * Returns the last element matching the given [predicate]. + * @throws [NoSuchElementException] if no such element is found. + */ +public inline fun Iterable.last(predicate: (T) -> Boolean): T { + if (this is List) + return this.last(predicate) + var last: T? = null + var found = false + for (element in this) { + if (predicate(element)) { + last = element + found = true + } + } + if (!found) throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") + return last as T +} + +/** + * Returns the last element matching the given [predicate]. + * @throws [NoSuchElementException] if no such element is found. + */ +public inline fun List.last(predicate: (T) -> Boolean): T { + for (index in this.indices.reversed()) { + val element = this[index] + if (predicate(element)) return element + } + throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") +} + +/** + * Returns last index of [element], or -1 if the collection does not contain element. + */ +public fun Iterable.lastIndexOf(element: T): Int { + var lastIndex = -1 + var index = 0 + for (item in this) { + if (element == item) + lastIndex = index + index++ + } + return lastIndex +} + +/** + * Returns the last element, or `null` if the collection is empty. + */ +public fun Iterable.lastOrNull(): T? { + when (this) { + is List -> return if (isEmpty()) null else this[size() - 1] + else -> { + val iterator = iterator() + if (!iterator.hasNext()) + return null + var last = iterator.next() + while (iterator.hasNext()) + last = iterator.next() + return last + } + } +} + +/** + * Returns the last element, or `null` if the collection is empty. + */ +public fun List.lastOrNull(): T? { + return if (isEmpty()) null else this[size() - 1] +} + +/** + * Returns the last element matching the given [predicate], or `null` if no such element was found. + */ +public inline fun Iterable.lastOrNull(predicate: (T) -> Boolean): T? { + if (this is List) + return this.lastOrNull(predicate) + var last: T? = null + for (element in this) { + if (predicate(element)) { + last = element + } + } + return last +} + +/** + * Returns the last element matching the given [predicate], or `null` if no such element was found. + */ +public inline fun List.lastOrNull(predicate: (T) -> Boolean): T? { + 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 collection is empty or has more than one element. + */ +public fun Iterable.single(): T { + when (this) { + is List -> return when (size()) { + 0 -> throw NoSuchElementException("Collection is empty.") + 1 -> this[0] + else -> throw IllegalArgumentException("Collection has more than one element.") + } + else -> { + val iterator = iterator() + if (!iterator.hasNext()) + throw NoSuchElementException("Collection is empty.") + var single = iterator.next() + if (iterator.hasNext()) + throw IllegalArgumentException("Collection has more than one element.") + return single + } + } +} + +/** + * Returns the single element, or throws an exception if the collection is empty or has more than one element. + */ +public fun List.single(): T { + return when (size()) { + 0 -> throw NoSuchElementException("Collection is empty.") + 1 -> this[0] + else -> throw IllegalArgumentException("Collection has more than one element.") + } +} + +/** + * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. + */ +public inline fun Iterable.single(predicate: (T) -> Boolean): T { + var single: T? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) throw IllegalArgumentException("Collection contains more than one matching element.") + single = element + found = true + } + } + if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") + return single as T +} + +/** + * Returns single element, or `null` if the collection is empty or has more than one element. + */ +public fun Iterable.singleOrNull(): T? { + when (this) { + is List -> return if (size() == 1) this[0] else null + else -> { + val iterator = iterator() + if (!iterator.hasNext()) + return null + var single = iterator.next() + if (iterator.hasNext()) + return null + return single + } + } +} + +/** + * Returns single element, or `null` if the collection is empty or has more than one element. + */ +public fun List.singleOrNull(): T? { + return if (size() == 1) this[0] else null +} + +/** + * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. + */ +public inline fun Iterable.singleOrNull(predicate: (T) -> Boolean): T? { + var single: T? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) return null + single = element + found = true + } + } + if (!found) return null + return single +} + +/** + * Returns a list containing all elements except first [n] elements. + */ +public fun Iterable.drop(n: Int): List { + require(n >= 0, { "Requested element count $n is less than zero." }) + if (n == 0) return toList() + val list: ArrayList + if (this is Collection<*>) { + val resultSize = size() - n + if (resultSize <= 0) + return emptyList() + list = ArrayList(resultSize) + if (this is List) { + for (index in n..size() - 1) { + list.add(this[index]) + } + return list + } + } + else { + list = ArrayList() + } + var count = 0 + for (item in this) { + if (count++ >= n) list.add(item) + } + return list +} + +/** + * Returns a list containing all elements except last [n] elements. + */ +public fun List.dropLast(n: Int): List { + require(n >= 0, { "Requested element count $n is less than zero." }) + return take((size() - n).coerceAtLeast(0)) +} + +/** + * Returns a list containing all elements except last elements that satisfy the given [predicate]. + */ +public inline fun List.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 first elements that satisfy the given [predicate]. + */ +public inline fun Iterable.dropWhile(predicate: (T) -> Boolean): List { + var yielding = false + val list = ArrayList() + for (item in this) + if (yielding) + list.add(item) + else if (!predicate(item)) { + list.add(item) + yielding = true + } + return list +} + +/** + * Returns a list containing all elements matching the given [predicate]. + */ +public inline fun Iterable.filter(predicate: (T) -> Boolean): List { + return filterTo(ArrayList(), predicate) +} + +/** + * Returns a list containing all elements not matching the given [predicate]. + */ +public inline fun Iterable.filterNot(predicate: (T) -> Boolean): List { + return filterNotTo(ArrayList(), predicate) +} + +/** + * Returns a list containing all elements that are not `null`. + */ +public fun Iterable.filterNotNull(): List { + return filterNotNullTo(ArrayList()) +} + +/** + * Appends all elements that are not `null` to the given [destination]. + */ +public fun , T : Any> Iterable.filterNotNullTo(destination: C): C { + for (element in this) if (element != null) destination.add(element) + return destination +} + +/** + * Appends all elements not matching the given [predicate] to the given [destination]. + */ +public inline fun > Iterable.filterNotTo(destination: C, predicate: (T) -> Boolean): C { + for (element in this) if (!predicate(element)) destination.add(element) + return destination +} + +/** + * Appends all elements matching the given [predicate] into the given [destination]. + */ +public inline fun > Iterable.filterTo(destination: C, predicate: (T) -> Boolean): C { + for (element in this) if (predicate(element)) destination.add(element) + return destination +} + +/** + * Returns a list containing elements at indices in the specified [indices] range. + */ +public fun List.slice(indices: IntRange): List { + if (indices.isEmpty()) return listOf() + return this.subList(indices.start, indices.end + 1).toList() +} + +/** + * Returns a list containing elements at specified [indices]. + */ +public fun List.slice(indices: Iterable): List { + val size = indices.collectionSizeOrDefault(10) + if (size == 0) return listOf() + val list = ArrayList(size) + for (index in indices) { + list.add(get(index)) + } + return list +} + +/** + * Returns a list containing first [n] elements. + */ +public fun Iterable.take(n: Int): List { + require(n >= 0, { "Requested element count $n is less than zero." }) + if (n == 0) return emptyList() + if (this is Collection && n >= size()) return toList() + 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 List.takeLast(n: Int): List { + require(n >= 0, { "Requested element count $n is less than zero." }) + if (n == 0) return emptyList() + val size = size() + if (n >= size) return toList() + 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 List.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 first elements satisfying the given [predicate]. + */ +public inline fun Iterable.takeWhile(predicate: (T) -> Boolean): List { + val list = ArrayList() + for (item in this) { + if (!predicate(item)) + break; + list.add(item) + } + return list +} + +/** + * Returns a list with elements in reversed order. + */ +@Deprecated("reverse will change its behavior soon. Use reversed() instead.", ReplaceWith("reversed()")) +public fun Iterable.reverse(): List { + return reversed() +} + +/** + * Returns a list with elements in reversed order. + */ +public fun Iterable.reversed(): List { + if (this is Collection && isEmpty()) return emptyList() + val list = toArrayList() + Collections.reverse(list) + return list +} + +/** + * Returns a sorted list of all elements. + */ +@Deprecated("This method may change its behavior soon. Use sorted() instead.", ReplaceWith("sorted()")) +public fun > Iterable.sort(): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList) + return sortedList +} + +/** + * Returns a list of all elements, sorted by the specified [comparator]. + */ +@Deprecated("This method may change its behavior soon. Use sortedWith() instead.", ReplaceWith("sortedWith(comparator)")) +public fun Iterable.sortBy(comparator: Comparator): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList, comparator) + return sortedList +} + +/** + * Returns a sorted list of all elements, ordered by results of specified [order] function. + */ +@Deprecated("This method may change its behavior soon. Use sortedBy() instead.", ReplaceWith("sortedBy(order)")) +public inline fun > Iterable.sortBy(crossinline order: (T) -> R): List { + val sortedList = toArrayList() + val sortBy: Comparator = compareBy(order) + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Returns a sorted list of all elements, in descending order. + */ +@Deprecated("This method may change its behavior soon. Use sortedDescending() instead.", ReplaceWith("sortedDescending()")) +public fun > Iterable.sortDescending(): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList, comparator { x, y -> y.compareTo(x) }) + return sortedList +} + +/** + * Returns a sorted list of all elements, in descending order by results of specified [order] function. + */ +@Deprecated("This method may change its behavior soon. Use sortedByDescending() instead.", ReplaceWith("sortedByDescending(order)")) +public inline fun > Iterable.sortDescendingBy(crossinline order: (T) -> R): List { + val sortedList = toArrayList() + val sortBy: Comparator = compareByDescending(order) + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Returns a list of all elements sorted according to their natural sort order. + */ +public fun > Iterable.sorted(): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList) + return sortedList +} + +/** + * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. + */ +public inline fun > Iterable.sortedBy(crossinline selector: (T) -> R?): List { + return sortedWith(compareBy(selector)) +} + +/** + * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. + */ +public inline fun > Iterable.sortedByDescending(crossinline selector: (T) -> R?): List { + return sortedWith(compareByDescending(selector)) +} + +/** + * Returns a list of all elements sorted descending according to their natural sort order. + */ +public fun > Iterable.sortedDescending(): List { + return sortedWith(comparator { x, y -> y.compareTo(x) }) +} + +/** + * Returns a list of all elements sorted according to the specified [comparator]. + */ +public fun Iterable.sortedWith(comparator: Comparator): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList, comparator) + return sortedList +} + +/** + * Returns a sorted list of all elements. + */ +@Deprecated("Use sorted() instead.", ReplaceWith("sorted()")) +public fun > Iterable.toSortedList(): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList) + return sortedList +} + +/** + * Returns a sorted list of all elements, ordered by results of specified [order] function. + */ +@Deprecated("Use sortedBy(order) instead.", ReplaceWith("sortedBy(order)")) +public fun > Iterable.toSortedListBy(order: (T) -> V): List { + val sortedList = toArrayList() + val sortBy: Comparator = compareBy(order) + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Returns an array of Boolean containing all of the elements of this collection. + */ +public fun Collection.toBooleanArray(): BooleanArray { + val result = BooleanArray(size()) + var index = 0 + for (element in this) + result[index++] = element + return result +} + +/** + * Returns an array of Byte containing all of the elements of this collection. + */ +public fun Collection.toByteArray(): ByteArray { + val result = ByteArray(size()) + var index = 0 + for (element in this) + result[index++] = element + return result +} + +/** + * Returns an array of Char containing all of the elements of this collection. + */ +public fun Collection.toCharArray(): CharArray { + val result = CharArray(size()) + var index = 0 + for (element in this) + result[index++] = element + return result +} + +/** + * Returns an array of Double containing all of the elements of this collection. + */ +public fun Collection.toDoubleArray(): DoubleArray { + val result = DoubleArray(size()) + var index = 0 + for (element in this) + result[index++] = element + return result +} + +/** + * Returns an array of Float containing all of the elements of this collection. + */ +public fun Collection.toFloatArray(): FloatArray { + val result = FloatArray(size()) + var index = 0 + for (element in this) + result[index++] = element + return result +} + +/** + * Returns an array of Int containing all of the elements of this collection. + */ +public fun Collection.toIntArray(): IntArray { + val result = IntArray(size()) + var index = 0 + for (element in this) + result[index++] = element + return result +} + +/** + * Returns an array of Long containing all of the elements of this collection. + */ +public fun Collection.toLongArray(): LongArray { + val result = LongArray(size()) + var index = 0 + for (element in this) + result[index++] = element + return result +} + +/** + * Returns an array of Short containing all of the elements of this collection. + */ +public fun Collection.toShortArray(): ShortArray { + val result = ShortArray(size()) + var index = 0 + for (element in this) + result[index++] = element + return result +} + +/** + * Returns an [ArrayList] of all elements. + */ +public fun Collection.toArrayList(): ArrayList { + return ArrayList(this) +} + +/** + * Returns an [ArrayList] of all elements. + */ +public fun Iterable.toArrayList(): ArrayList { + if (this is Collection) + return this.toArrayList() + return toCollection(ArrayList()) +} + +/** + * Appends all elements to the given [collection]. + */ +public fun > Iterable.toCollection(collection: C): C { + for (item in this) { + collection.add(item) + } + return collection +} + +/** + * Returns a [HashSet] of all elements. + */ +public fun Iterable.toHashSet(): HashSet { + return toCollection(HashSet(mapCapacity(collectionSizeOrDefault(12)))) +} + +/** + * Returns a [LinkedList] containing all elements. + */ +public fun Iterable.toLinkedList(): LinkedList { + return toCollection(LinkedList()) +} + +/** + * Returns a [List] containing all elements. + */ +public fun Iterable.toList(): List { + return this.toArrayList() +} + +/** + * Returns Map containing the values from the given collection indexed by [selector]. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun Iterable.toMap(selector: (T) -> K): Map { + val capacity = (collectionSizeOrDefault(10)/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), element) + } + return result +} + +/** + * Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun Iterable.toMap(selector: (T) -> K, transform: (T) -> V): Map { + val capacity = (collectionSizeOrDefault(10)/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) + for (element in this) { + result.put(selector(element), transform(element)) + } + return result +} + +/** + * Returns a [Set] of all elements. + */ +public fun Iterable.toSet(): Set { + return toCollection(LinkedHashSet(mapCapacity(collectionSizeOrDefault(12)))) +} + +/** + * Returns a [SortedSet] of all elements. + */ +public fun Iterable.toSortedSet(): SortedSet { + return toCollection(TreeSet()) +} + +/** + * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original collection. + */ +public inline fun Iterable.flatMap(transform: (T) -> Iterable): List { + return flatMapTo(ArrayList(), transform) +} + +/** + * Appends all elements yielded from results of [transform] function being invoked on each element of original collection, to the given [destination]. + */ +public inline fun > Iterable.flatMapTo(destination: C, transform: (T) -> Iterable): C { + for (element in this) { + val list = transform(element) + destination.addAll(list) + } + return destination +} + +/** + * Returns a map of the elements in original collection grouped by the result of given [toKey] function. + */ +public inline fun Iterable.groupBy(toKey: (T) -> K): Map> { + return groupByTo(LinkedHashMap>(), toKey) +} + +/** + * Appends elements from original collection grouped by the result of given [toKey] function to the given [map]. + */ +public inline fun Iterable.groupByTo(map: MutableMap>, toKey: (T) -> K): Map> { + for (element in this) { + val key = toKey(element) + val list = map.getOrPut(key) { ArrayList() } + list.add(element) + } + return map +} + +/** + * Returns a list containing the results of applying the given [transform] function to each element of the original collection. + */ +public inline fun Iterable.map(transform: (T) -> R): List { + return mapTo(ArrayList(collectionSizeOrDefault(10)), transform) +} + +/** + * Returns a list containing the results of applying the given [transform] function to each element and its index of the original collection. + */ +public inline fun Iterable.mapIndexed(transform: (Int, T) -> R): List { + return mapIndexedTo(ArrayList(collectionSizeOrDefault(10)), transform) +} + +/** + * Appends transformed elements and their indices of the original collection using the given [transform] function + * to the given [destination]. + */ +public inline fun > Iterable.mapIndexedTo(destination: C, transform: (Int, T) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} + +/** + * Returns a list containing the results of applying the given [transform] function to each non-null element of the original collection. + */ +public inline fun Iterable.mapNotNull(transform: (T) -> R): List { + return mapNotNullTo(ArrayList(), transform) +} + +/** + * Appends transformed non-null elements of original collection using the given [transform] function + * to the given [destination]. + */ +public inline fun > Iterable.mapNotNullTo(destination: C, transform: (T) -> R): C { + for (element in this) { + if (element != null) { + destination.add(transform(element)) + } + } + return destination +} + +/** + * Appends transformed elements of the original collection using the given [transform] function + * to the given [destination]. + */ +public inline fun > Iterable.mapTo(destination: C, transform: (T) -> R): C { + for (item in this) + destination.add(transform(item)) + return destination +} + +/** + * Returns a lazy [Iterable] of [IndexedValue] for each element of the original collection. + */ +public fun Iterable.withIndex(): Iterable> { + return IndexingIterable { iterator() } +} + +/** + * Returns a list containing pairs of each element of the original collection and their index. + */ +@Deprecated("Use withIndex() instead.") +public fun Iterable.withIndices(): List> { + var index = 0 + return mapTo(ArrayList>(), { index++ to it }) +} + +/** + * Returns a list containing only distinct elements from the given collection. + * The elements in the resulting list are in the same order as they were in the source collection. + */ +public fun Iterable.distinct(): List { + return this.toMutableSet().toList() +} + +/** + * Returns a list containing only distinct elements from the given collection according to the [keySelector]. + * The elements in the resulting list are in the same order as they were in the source collection. + */ +public inline fun Iterable.distinctBy(keySelector: (T) -> K): List { + val set = HashSet() + val list = ArrayList() + for (e in this) { + val key = keySelector(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. + */ +public fun Iterable.intersect(other: Iterable): Set { + val set = this.toMutableSet() + set.retainAll(other) + return set +} + +/** + * Returns a set containing all elements that are contained by this set and not contained by the specified collection. + */ +public fun Iterable.subtract(other: Iterable): Set { + val set = this.toMutableSet() + set.removeAll(other) + return set +} + +/** + * Returns a mutable set containing all distinct elements from the given collection. + */ +public fun Iterable.toMutableSet(): MutableSet { + return when (this) { + is Collection -> LinkedHashSet(this) + else -> toCollection(LinkedHashSet()) + } +} + +/** + * Returns a set containing all distinct elements from both collections. + */ +public fun Iterable.union(other: Iterable): Set { + val set = this.toMutableSet() + set.addAll(other) + return set +} + +/** + * Returns `true` if all elements match the given [predicate]. + */ +public inline fun Iterable.all(predicate: (T) -> Boolean): Boolean { + for (element in this) if (!predicate(element)) return false + return true +} + +/** + * Returns `true` if collection has at least one element. + */ +public fun Iterable.any(): Boolean { + for (element in this) return true + return false +} + +/** + * Returns `true` if at least one element matches the given [predicate]. + */ +public inline fun Iterable.any(predicate: (T) -> Boolean): Boolean { + for (element in this) if (predicate(element)) return true + return false +} + +/** + * Returns the number of elements in this collection. + */ +public fun Collection.count(): Int { + return size() +} + +/** + * Returns the number of elements in this collection. + */ +public fun Iterable.count(): Int { + var count = 0 + for (element in this) count++ + return count +} + +/** + * Returns the number of elements matching the given [predicate]. + */ +public inline fun Iterable.count(predicate: (T) -> Boolean): Int { + var count = 0 + for (element in this) if (predicate(element)) count++ + return count +} + +/** + * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. + */ +public inline fun Iterable.fold(initial: R, operation: (R, T) -> R): R { + var accumulator = initial + for (element in this) accumulator = operation(accumulator, element) + return accumulator +} + +/** + * Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. + */ +public inline fun List.foldRight(initial: R, operation: (T, R) -> R): R { + var index = lastIndex + var accumulator = initial + while (index >= 0) { + accumulator = operation(get(index--), accumulator) + } + return accumulator +} + +/** + * Performs the given [operation] on each element. + */ +public inline fun Iterable.forEach(operation: (T) -> Unit): Unit { + for (element in this) operation(element) +} + +/** + * Performs the given [operation] on each element, providing sequential index with the element. + */ +public inline fun Iterable.forEachIndexed(operation: (Int, T) -> Unit): Unit { + var index = 0 + for (item in this) operation(index++, item) +} + +/** + * Returns the largest element or `null` if there are no elements. + */ +public fun > Iterable.max(): T? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var max = iterator.next() + while (iterator.hasNext()) { + val e = iterator.next() + if (max < e) max = e + } + return max +} + +/** + * Returns the first element yielding the largest value of the given function or `null` if there are no elements. + */ +public inline fun , T : Any> Iterable.maxBy(f: (T) -> R): T? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var maxElem = iterator.next() + var maxValue = f(maxElem) + while (iterator.hasNext()) { + val e = iterator.next() + val v = f(e) + if (maxValue < v) { + maxElem = e + maxValue = v + } + } + return maxElem +} + +/** + * Returns the smallest element or `null` if there are no elements. + */ +public fun > Iterable.min(): T? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var min = iterator.next() + while (iterator.hasNext()) { + val e = iterator.next() + if (min > e) min = e + } + return min +} + +/** + * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. + */ +public inline fun , T : Any> Iterable.minBy(f: (T) -> R): T? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var minElem = iterator.next() + var minValue = f(minElem) + while (iterator.hasNext()) { + val e = iterator.next() + val v = f(e) + if (minValue > v) { + minElem = e + minValue = v + } + } + return minElem +} + +/** + * Returns `true` if collection has no elements. + */ +public fun Iterable.none(): Boolean { + for (element in this) return false + return true +} + +/** + * Returns `true` if no elements match the given [predicate]. + */ +public inline fun Iterable.none(predicate: (T) -> 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 Iterable.reduce(operation: (S, T) -> S): S { + val iterator = this.iterator() + if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") + var accumulator: S = iterator.next() + while (iterator.hasNext()) { + accumulator = operation(accumulator, iterator.next()) + } + return accumulator +} + +/** + * Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. + */ +public inline fun List.reduceRight(operation: (T, S) -> S): S { + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.") + var accumulator: S = get(index--) + while (index >= 0) { + accumulator = operation(get(index--), accumulator) + } + return accumulator +} + +/** + * Returns the sum of all values produced by [transform] function from elements in the collection. + */ +public inline fun Iterable.sumBy(transform: (T) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [transform] function from elements in the collection. + */ +public inline fun Iterable.sumByDouble(transform: (T) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns an original collection containing all the non-`null` elements, throwing an [IllegalArgumentException] if there are any `null` elements. + */ +public fun Iterable.requireNoNulls(): Iterable { + for (element in this) { + if (element == null) { + throw IllegalArgumentException("null element found in $this.") + } + } + return this as Iterable +} + +/** + * Returns an original collection containing all the non-`null` elements, throwing an [IllegalArgumentException] if there are any `null` elements. + */ +public fun List.requireNoNulls(): List { + for (element in this) { + if (element == null) { + throw IllegalArgumentException("null element found in $this.") + } + } + return this as 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 Iterable.merge(array: Array, transform: (T, R) -> V): List { + val arraySize = array.size() + val list = ArrayList(Math.min(collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in this) { + if (i >= arraySize) break + list.add(transform(element, array[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 Iterable.merge(other: Iterable, transform: (T, R) -> V): List { + val first = iterator() + val second = other.iterator() + val list = ArrayList(Math.min(collectionSizeOrDefault(10), other.collectionSizeOrDefault(10))) + while (first.hasNext() && second.hasNext()) { + list.add(transform(first.next(), second.next())) + } + return list +} + +/** + * Returns a list containing all elements of the original collection except the elements contained in the given [array]. + */ +public operator fun Iterable.minus(array: Array): List { + if (array.isEmpty()) return this.toList() + val other = array.toHashSet() + return this.filterNot { it in other } +} + +/** + * Returns a list containing all elements of the original collection except the elements contained in the given [collection]. + */ +public operator fun Iterable.minus(collection: Iterable): List { + val other = collection.convertToSetForSetOperationWith(this) + if (other.isEmpty()) + return this.toList() + return this.filterNot { it in other } +} + +/** + * Returns a list containing all elements of the original collection without the first occurrence of the given [element]. + */ +public operator fun Iterable.minus(element: T): List { + val result = ArrayList(collectionSizeOrDefault(10)) + var removed = false + return this.filterTo(result) { if (!removed && it == element) { removed = true; false } else true } +} + +/** + * Returns a list containing all elements of the original collection except the elements contained in the given [sequence]. + */ +public operator fun Iterable.minus(sequence: Sequence): List { + val other = sequence.toHashSet() + if (other.isEmpty()) + return this.toList() + return this.filterNot { it in other } +} + +/** + * Splits the original collection into pair of collections, + * where *first* collection contains elements for which [predicate] yielded `true`, + * while *second* collection contains elements for which [predicate] yielded `false`. + */ +public inline fun Iterable.partition(predicate: (T) -> Boolean): Pair, List> { + val first = ArrayList() + val second = ArrayList() + for (element in this) { + if (predicate(element)) { + first.add(element) + } else { + second.add(element) + } + } + return Pair(first, second) +} + +/** + * Returns a list containing all elements of the original collection and then all elements of the given [array]. + */ +public operator fun Collection.plus(array: Array): List { + val result = ArrayList(this.size() + array.size()) + result.addAll(this) + result.addAll(array) + return result +} + +/** + * Returns a list containing all elements of the original collection and then all elements of the given [array]. + */ +public operator fun Iterable.plus(array: Array): List { + if (this is Collection) return this.plus(array) + val result = ArrayList() + result.addAll(this) + result.addAll(array) + return result +} + +/** + * Returns a list containing all elements of the original collection and then all elements of the given [collection]. + */ +public operator fun Collection.plus(collection: Iterable): List { + if (collection is Collection) { + val result = ArrayList(this.size() + collection.size()) + result.addAll(this) + result.addAll(collection) + return result + } else { + val result = ArrayList(this) + result.addAll(collection) + return result + } +} + +/** + * Returns a list containing all elements of the original collection and then all elements of the given [collection]. + */ +public operator fun Iterable.plus(collection: Iterable): List { + if (this is Collection) return this.plus(collection) + val result = ArrayList() + result.addAll(this) + result.addAll(collection) + return result +} + +/** + * Returns a list containing all elements of the original collection and then the given [element]. + */ +public operator fun Collection.plus(element: T): List { + val result = ArrayList(size() + 1) + result.addAll(this) + result.add(element) + return result +} + +/** + * Returns a list containing all elements of the original collection and then the given [element]. + */ +public operator fun Iterable.plus(element: T): List { + if (this is Collection) return this.plus(element) + val result = ArrayList() + result.addAll(this) + result.add(element) + return result +} + +/** + * Returns a list containing all elements of the original collection and then all elements of the given [sequence]. + */ +public operator fun Collection.plus(sequence: Sequence): List { + val result = ArrayList(this.size() + 10) + result.addAll(this) + result.addAll(sequence) + return result +} + +/** + * Returns a list containing all elements of the original collection and then all elements of the given [sequence]. + */ +public operator fun Iterable.plus(sequence: Sequence): List { + val result = ArrayList() + result.addAll(this) + result.addAll(sequence) + return result +} + +/** + * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. + */ +public fun Iterable.zip(array: Array): List> { + return merge(array) { 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 fun Iterable.zip(other: Iterable): List> { + return merge(other) { t1, t2 -> t1 to t2 } +} + +/** + * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. + * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] + * elements will be appended, followed by the [truncated] string (which defaults to "..."). + */ +public fun Iterable.joinTo(buffer: A, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((T) -> String)? = null): A { + buffer.append(prefix) + var count = 0 + for (element in this) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (transform != null) transform(element) else if (element == null) "null" else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) + return buffer +} + +/** + * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. + * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] + * elements will be appended, followed by the [truncated] string (which defaults to "..."). + */ +public fun Iterable.joinToString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((T) -> String)? = null): String { + return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() +} + +/** + * Returns a sequence from the given collection. + */ +public fun Iterable.asSequence(): Sequence { + return object : Sequence { + override fun iterator(): Iterator { + return this@asSequence.iterator() + } + } +} + +/** + * Returns a sequence from the given collection + */ +@Deprecated("Use asSequence() instead", ReplaceWith("asSequence()")) +public fun Iterable.sequence(): Sequence { + return asSequence() +} + +/** + * Returns a list containing all elements that are instances of specified type parameter R. + */ +@kotlin.jvm.JvmVersion +public inline fun Iterable<*>.filterIsInstance(): List { + return filterIsInstanceTo(ArrayList()) +} + +/** + * Returns a list containing all elements that are instances of specified class. + */ +@kotlin.jvm.JvmVersion +public fun Iterable<*>.filterIsInstance(klass: Class): List { + return filterIsInstanceTo(ArrayList(), klass) +} + +/** + * Appends all elements that are instances of specified type parameter R to the given [destination]. + */ +@kotlin.jvm.JvmVersion +public inline fun > Iterable<*>.filterIsInstanceTo(destination: C): C { + for (element in this) if (element is R) destination.add(element) + return destination +} + +/** + * Appends all elements that are instances of specified class to the given [destination]. + */ +@kotlin.jvm.JvmVersion +public fun , R> Iterable<*>.filterIsInstanceTo(destination: C, klass: Class): C { + for (element in this) if (klass.isInstance(element)) destination.add(element as R) + return destination +} + +/** + * Returns an average value of elements in the collection. + */ +@kotlin.jvm.JvmName("averageOfByte") +public fun Iterable.average(): Double { + val iterator = iterator() + var sum: Double = 0.0 + var count: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + count += 1 + } + return if (count == 0) 0.0 else sum / count +} + +/** + * Returns an average value of elements in the collection. + */ +@kotlin.jvm.JvmName("averageOfDouble") +public fun Iterable.average(): Double { + val iterator = iterator() + var sum: Double = 0.0 + var count: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + count += 1 + } + return if (count == 0) 0.0 else sum / count +} + +/** + * Returns an average value of elements in the collection. + */ +@kotlin.jvm.JvmName("averageOfFloat") +public fun Iterable.average(): Double { + val iterator = iterator() + var sum: Double = 0.0 + var count: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + count += 1 + } + return if (count == 0) 0.0 else sum / count +} + +/** + * Returns an average value of elements in the collection. + */ +@kotlin.jvm.JvmName("averageOfInt") +public fun Iterable.average(): Double { + val iterator = iterator() + var sum: Double = 0.0 + var count: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + count += 1 + } + return if (count == 0) 0.0 else sum / count +} + +/** + * Returns an average value of elements in the collection. + */ +@kotlin.jvm.JvmName("averageOfLong") +public fun Iterable.average(): Double { + val iterator = iterator() + var sum: Double = 0.0 + var count: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + count += 1 + } + return if (count == 0) 0.0 else sum / count +} + +/** + * Returns an average value of elements in the collection. + */ +@kotlin.jvm.JvmName("averageOfShort") +public fun Iterable.average(): Double { + val iterator = iterator() + var sum: Double = 0.0 + var count: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + count += 1 + } + return if (count == 0) 0.0 else sum / count +} + +/** + * Returns the sum of all elements in the collection. + */ +@kotlin.jvm.JvmName("sumOfByte") +public fun Iterable.sum(): Int { + val iterator = iterator() + var sum: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +@kotlin.jvm.JvmName("sumOfDouble") +public fun Iterable.sum(): Double { + val iterator = iterator() + var sum: Double = 0.0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +@kotlin.jvm.JvmName("sumOfFloat") +public fun Iterable.sum(): Float { + val iterator = iterator() + var sum: Float = 0.0f + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +@kotlin.jvm.JvmName("sumOfInt") +public fun Iterable.sum(): Int { + val iterator = iterator() + var sum: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +@kotlin.jvm.JvmName("sumOfLong") +public fun Iterable.sum(): Long { + val iterator = iterator() + var sum: Long = 0L + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +@kotlin.jvm.JvmName("sumOfShort") +public fun Iterable.sum(): Int { + val iterator = iterator() + var sum: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + diff --git a/libraries/stdlib/src/generated/_Comparables.kt b/libraries/stdlib/src/generated/_Comparables.kt deleted file mode 100644 index 84c898519df..00000000000 --- a/libraries/stdlib/src/generated/_Comparables.kt +++ /dev/null @@ -1,306 +0,0 @@ -package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// - -import kotlin.platform.* -import java.util.* - -import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js - -/** - * Ensures that this value is not less than the specified [minimumValue]. - * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. - */ -public fun > T.coerceAtLeast(minimumValue: T): T { - return if (this < minimumValue) minimumValue else this -} - -/** - * Ensures that this value is not less than the specified [minimumValue]. - * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. - */ -public fun Byte.coerceAtLeast(minimumValue: Byte): Byte { - return if (this < minimumValue) minimumValue else this -} - -/** - * Ensures that this value is not less than the specified [minimumValue]. - * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. - */ -public fun Double.coerceAtLeast(minimumValue: Double): Double { - return if (this < minimumValue) minimumValue else this -} - -/** - * Ensures that this value is not less than the specified [minimumValue]. - * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. - */ -public fun Float.coerceAtLeast(minimumValue: Float): Float { - return if (this < minimumValue) minimumValue else this -} - -/** - * Ensures that this value is not less than the specified [minimumValue]. - * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. - */ -public fun Int.coerceAtLeast(minimumValue: Int): Int { - return if (this < minimumValue) minimumValue else this -} - -/** - * Ensures that this value is not less than the specified [minimumValue]. - * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. - */ -public fun Long.coerceAtLeast(minimumValue: Long): Long { - return if (this < minimumValue) minimumValue else this -} - -/** - * Ensures that this value is not less than the specified [minimumValue]. - * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. - */ -public fun Short.coerceAtLeast(minimumValue: Short): Short { - return if (this < minimumValue) minimumValue else this -} - -/** - * Ensures that this value is not greater than the specified [maximumValue]. - * @return this value if it's greater than or equal to the [maximumValue] or the [maximumValue] otherwise. - */ -public fun > T.coerceAtMost(maximumValue: T): T { - return if (this > maximumValue) maximumValue else this -} - -/** - * Ensures that this value is not greater than the specified [maximumValue]. - * @return this value if it's greater than or equal to the [maximumValue] or the [maximumValue] otherwise. - */ -public fun Byte.coerceAtMost(maximumValue: Byte): Byte { - return if (this > maximumValue) maximumValue else this -} - -/** - * Ensures that this value is not greater than the specified [maximumValue]. - * @return this value if it's greater than or equal to the [maximumValue] or the [maximumValue] otherwise. - */ -public fun Double.coerceAtMost(maximumValue: Double): Double { - return if (this > maximumValue) maximumValue else this -} - -/** - * Ensures that this value is not greater than the specified [maximumValue]. - * @return this value if it's greater than or equal to the [maximumValue] or the [maximumValue] otherwise. - */ -public fun Float.coerceAtMost(maximumValue: Float): Float { - return if (this > maximumValue) maximumValue else this -} - -/** - * Ensures that this value is not greater than the specified [maximumValue]. - * @return this value if it's greater than or equal to the [maximumValue] or the [maximumValue] otherwise. - */ -public fun Int.coerceAtMost(maximumValue: Int): Int { - return if (this > maximumValue) maximumValue else this -} - -/** - * Ensures that this value is not greater than the specified [maximumValue]. - * @return this value if it's greater than or equal to the [maximumValue] or the [maximumValue] otherwise. - */ -public fun Long.coerceAtMost(maximumValue: Long): Long { - return if (this > maximumValue) maximumValue else this -} - -/** - * Ensures that this value is not greater than the specified [maximumValue]. - * @return this value if it's greater than or equal to the [maximumValue] or the [maximumValue] otherwise. - */ -public fun Short.coerceAtMost(maximumValue: Short): Short { - return if (this > maximumValue) maximumValue else this -} - -/** - * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. - * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. - */ -public fun > T.coerceIn(minimumValue: T?, maximumValue: T?): T { - if (minimumValue !== null && maximumValue !== null) { - if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") - if (this < minimumValue) return minimumValue - if (this > maximumValue) return maximumValue - } - else { - if (minimumValue !== null && this < minimumValue) return minimumValue - if (maximumValue !== null && this > maximumValue) return maximumValue - } - return this -} - -/** - * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. - * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. - */ -public fun Byte.coerceIn(minimumValue: Byte?, maximumValue: Byte?): Byte { - if (minimumValue !== null && maximumValue !== null) { - if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") - if (this < minimumValue) return minimumValue - if (this > maximumValue) return maximumValue - } - else { - if (minimumValue !== null && this < minimumValue) return minimumValue - if (maximumValue !== null && this > maximumValue) return maximumValue - } - return this -} - -/** - * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. - * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. - */ -public fun Double.coerceIn(minimumValue: Double?, maximumValue: Double?): Double { - if (minimumValue !== null && maximumValue !== null) { - if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") - if (this < minimumValue) return minimumValue - if (this > maximumValue) return maximumValue - } - else { - if (minimumValue !== null && this < minimumValue) return minimumValue - if (maximumValue !== null && this > maximumValue) return maximumValue - } - return this -} - -/** - * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. - * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. - */ -public fun Float.coerceIn(minimumValue: Float?, maximumValue: Float?): Float { - if (minimumValue !== null && maximumValue !== null) { - if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") - if (this < minimumValue) return minimumValue - if (this > maximumValue) return maximumValue - } - else { - if (minimumValue !== null && this < minimumValue) return minimumValue - if (maximumValue !== null && this > maximumValue) return maximumValue - } - return this -} - -/** - * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. - * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. - */ -public fun Int.coerceIn(minimumValue: Int?, maximumValue: Int?): Int { - if (minimumValue !== null && maximumValue !== null) { - if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") - if (this < minimumValue) return minimumValue - if (this > maximumValue) return maximumValue - } - else { - if (minimumValue !== null && this < minimumValue) return minimumValue - if (maximumValue !== null && this > maximumValue) return maximumValue - } - return this -} - -/** - * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. - * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. - */ -public fun Long.coerceIn(minimumValue: Long?, maximumValue: Long?): Long { - if (minimumValue !== null && maximumValue !== null) { - if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") - if (this < minimumValue) return minimumValue - if (this > maximumValue) return maximumValue - } - else { - if (minimumValue !== null && this < minimumValue) return minimumValue - if (maximumValue !== null && this > maximumValue) return maximumValue - } - return this -} - -/** - * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. - * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. - */ -public fun Short.coerceIn(minimumValue: Short?, maximumValue: Short?): Short { - if (minimumValue !== null && maximumValue !== null) { - if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") - if (this < minimumValue) return minimumValue - if (this > maximumValue) return maximumValue - } - else { - if (minimumValue !== null && this < minimumValue) return minimumValue - if (maximumValue !== null && this > maximumValue) return maximumValue - } - return this -} - -/** - * Ensures that this value lies in the specified [range]. - * @return this value if it's in the [range], or range.start if this value is less than range.start, or range.end if this value is greater than range.end. - */ -public fun > T.coerceIn(range: Range): T { - if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") - return if (this < range.start) range.start else if (this > range.end) range.end else this -} - -/** - * Ensures that this value lies in the specified [range]. - * @return this value if it's in the [range], or range.start if this value is less than range.start, or range.end if this value is greater than range.end. - */ -public fun Byte.coerceIn(range: Range): Byte { - if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") - return if (this < range.start) range.start else if (this > range.end) range.end else this -} - -/** - * Ensures that this value lies in the specified [range]. - * @return this value if it's in the [range], or range.start if this value is less than range.start, or range.end if this value is greater than range.end. - */ -public fun Double.coerceIn(range: Range): Double { - if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") - return if (this < range.start) range.start else if (this > range.end) range.end else this -} - -/** - * Ensures that this value lies in the specified [range]. - * @return this value if it's in the [range], or range.start if this value is less than range.start, or range.end if this value is greater than range.end. - */ -public fun Float.coerceIn(range: Range): Float { - if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") - return if (this < range.start) range.start else if (this > range.end) range.end else this -} - -/** - * Ensures that this value lies in the specified [range]. - * @return this value if it's in the [range], or range.start if this value is less than range.start, or range.end if this value is greater than range.end. - */ -public fun Int.coerceIn(range: Range): Int { - if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") - return if (this < range.start) range.start else if (this > range.end) range.end else this -} - -/** - * Ensures that this value lies in the specified [range]. - * @return this value if it's in the [range], or range.start if this value is less than range.start, or range.end if this value is greater than range.end. - */ -public fun Long.coerceIn(range: Range): Long { - if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") - return if (this < range.start) range.start else if (this > range.end) range.end else this -} - -/** - * Ensures that this value lies in the specified [range]. - * @return this value if it's in the [range], or range.start if this value is less than range.start, or range.end if this value is greater than range.end. - */ -public fun Short.coerceIn(range: Range): Short { - if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") - return if (this < range.start) range.start else if (this > range.end) range.end else this -} - diff --git a/libraries/stdlib/src/generated/_Elements.kt b/libraries/stdlib/src/generated/_Elements.kt deleted file mode 100644 index 016a66fd177..00000000000 --- a/libraries/stdlib/src/generated/_Elements.kt +++ /dev/null @@ -1,3444 +0,0 @@ -package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// - -import kotlin.platform.* -import java.util.* - -import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js - -/** - * Returns 1st *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun Array.component1(): T { - return get(0) -} - -/** - * Returns 1st *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun BooleanArray.component1(): Boolean { - return get(0) -} - -/** - * Returns 1st *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun ByteArray.component1(): Byte { - return get(0) -} - -/** - * Returns 1st *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun CharArray.component1(): Char { - return get(0) -} - -/** - * Returns 1st *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun DoubleArray.component1(): Double { - return get(0) -} - -/** - * Returns 1st *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun FloatArray.component1(): Float { - return get(0) -} - -/** - * Returns 1st *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun IntArray.component1(): Int { - return get(0) -} - -/** - * Returns 1st *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun LongArray.component1(): Long { - return get(0) -} - -/** - * Returns 1st *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun ShortArray.component1(): Short { - return get(0) -} - -/** - * Returns 1st *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun List.component1(): T { - return get(0) -} - -/** - * Returns 2nd *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun Array.component2(): T { - return get(1) -} - -/** - * Returns 2nd *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun BooleanArray.component2(): Boolean { - return get(1) -} - -/** - * Returns 2nd *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun ByteArray.component2(): Byte { - return get(1) -} - -/** - * Returns 2nd *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun CharArray.component2(): Char { - return get(1) -} - -/** - * Returns 2nd *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun DoubleArray.component2(): Double { - return get(1) -} - -/** - * Returns 2nd *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun FloatArray.component2(): Float { - return get(1) -} - -/** - * Returns 2nd *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun IntArray.component2(): Int { - return get(1) -} - -/** - * Returns 2nd *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun LongArray.component2(): Long { - return get(1) -} - -/** - * Returns 2nd *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun ShortArray.component2(): Short { - return get(1) -} - -/** - * Returns 2nd *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun List.component2(): T { - return get(1) -} - -/** - * Returns 3rd *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun Array.component3(): T { - return get(2) -} - -/** - * Returns 3rd *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun BooleanArray.component3(): Boolean { - return get(2) -} - -/** - * Returns 3rd *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun ByteArray.component3(): Byte { - return get(2) -} - -/** - * Returns 3rd *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun CharArray.component3(): Char { - return get(2) -} - -/** - * Returns 3rd *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun DoubleArray.component3(): Double { - return get(2) -} - -/** - * Returns 3rd *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun FloatArray.component3(): Float { - return get(2) -} - -/** - * Returns 3rd *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun IntArray.component3(): Int { - return get(2) -} - -/** - * Returns 3rd *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun LongArray.component3(): Long { - return get(2) -} - -/** - * Returns 3rd *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun ShortArray.component3(): Short { - return get(2) -} - -/** - * Returns 3rd *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun List.component3(): T { - return get(2) -} - -/** - * Returns 4th *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun Array.component4(): T { - return get(3) -} - -/** - * Returns 4th *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun BooleanArray.component4(): Boolean { - return get(3) -} - -/** - * Returns 4th *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun ByteArray.component4(): Byte { - return get(3) -} - -/** - * Returns 4th *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun CharArray.component4(): Char { - return get(3) -} - -/** - * Returns 4th *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun DoubleArray.component4(): Double { - return get(3) -} - -/** - * Returns 4th *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun FloatArray.component4(): Float { - return get(3) -} - -/** - * Returns 4th *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun IntArray.component4(): Int { - return get(3) -} - -/** - * Returns 4th *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun LongArray.component4(): Long { - return get(3) -} - -/** - * Returns 4th *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun ShortArray.component4(): Short { - return get(3) -} - -/** - * Returns 4th *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun List.component4(): T { - return get(3) -} - -/** - * Returns 5th *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun Array.component5(): T { - return get(4) -} - -/** - * Returns 5th *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun BooleanArray.component5(): Boolean { - return get(4) -} - -/** - * Returns 5th *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun ByteArray.component5(): Byte { - return get(4) -} - -/** - * Returns 5th *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun CharArray.component5(): Char { - return get(4) -} - -/** - * Returns 5th *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun DoubleArray.component5(): Double { - return get(4) -} - -/** - * Returns 5th *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun FloatArray.component5(): Float { - return get(4) -} - -/** - * Returns 5th *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun IntArray.component5(): Int { - return get(4) -} - -/** - * Returns 5th *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun LongArray.component5(): Long { - return get(4) -} - -/** - * Returns 5th *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun ShortArray.component5(): Short { - return get(4) -} - -/** - * Returns 5th *element* from the collection. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun List.component5(): T { - return get(4) -} - -/** - * Returns `true` if [element] is found in the collection. - */ -public operator fun Array.contains(element: T): Boolean { - return indexOf(element) >= 0 -} - -/** - * Returns `true` if [element] is found in the collection. - */ -public operator fun BooleanArray.contains(element: Boolean): Boolean { - return indexOf(element) >= 0 -} - -/** - * Returns `true` if [element] is found in the collection. - */ -public operator fun ByteArray.contains(element: Byte): Boolean { - return indexOf(element) >= 0 -} - -/** - * Returns `true` if [element] is found in the collection. - */ -public operator fun CharArray.contains(element: Char): Boolean { - return indexOf(element) >= 0 -} - -/** - * Returns `true` if [element] is found in the collection. - */ -public operator fun DoubleArray.contains(element: Double): Boolean { - return indexOf(element) >= 0 -} - -/** - * Returns `true` if [element] is found in the collection. - */ -public operator fun FloatArray.contains(element: Float): Boolean { - return indexOf(element) >= 0 -} - -/** - * Returns `true` if [element] is found in the collection. - */ -public operator fun IntArray.contains(element: Int): Boolean { - return indexOf(element) >= 0 -} - -/** - * Returns `true` if [element] is found in the collection. - */ -public operator fun LongArray.contains(element: Long): Boolean { - return indexOf(element) >= 0 -} - -/** - * Returns `true` if [element] is found in the collection. - */ -public operator fun ShortArray.contains(element: Short): Boolean { - return indexOf(element) >= 0 -} - -/** - * Returns `true` if [element] is found in the collection. - */ -public operator fun Iterable.contains(element: T): Boolean { - if (this is Collection) - return contains(element) - return indexOf(element) >= 0 -} - -/** - * Returns `true` if [element] is found in the collection. - */ -public operator fun Sequence.contains(element: T): Boolean { - return indexOf(element) >= 0 -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. - */ -public fun Array.elementAt(index: Int): T { - return get(index) -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. - */ -public fun BooleanArray.elementAt(index: Int): Boolean { - return get(index) -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. - */ -public fun ByteArray.elementAt(index: Int): Byte { - return get(index) -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. - */ -public fun CharArray.elementAt(index: Int): Char { - return get(index) -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. - */ -public fun DoubleArray.elementAt(index: Int): Double { - return get(index) -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. - */ -public fun FloatArray.elementAt(index: Int): Float { - return get(index) -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. - */ -public fun IntArray.elementAt(index: Int): Int { - return get(index) -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. - */ -public fun LongArray.elementAt(index: Int): Long { - return get(index) -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. - */ -public fun ShortArray.elementAt(index: Int): Short { - return get(index) -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. - */ -public fun Iterable.elementAt(index: Int): T { - if (this is List) - return get(index) - return elementAtOrElse(index) { throw IndexOutOfBoundsException("Collection doesn't contain element at index $index.") } -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. - */ -public fun List.elementAt(index: Int): T { - return get(index) -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. - */ -public fun Sequence.elementAt(index: Int): T { - return elementAtOrElse(index) { throw IndexOutOfBoundsException("Sequence doesn't contain element at index $index.") } -} - -/** - * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. - */ -public fun String.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 collection. - */ -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 collection. - */ -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 collection. - */ -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 collection. - */ -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 the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. - */ -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 collection. - */ -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 collection. - */ -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 collection. - */ -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 collection. - */ -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 collection. - */ -public fun Iterable.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T { - if (this is List) - return this.getOrElse(index, defaultValue) - if (index < 0) - return defaultValue(index) - val iterator = iterator() - var count = 0 - while (iterator.hasNext()) { - val element = iterator.next() - if (index == count++) - return element - } - return defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. - */ -public inline fun List.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. - */ -public fun Sequence.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T { - if (index < 0) - return defaultValue(index) - val iterator = iterator() - var count = 0 - while (iterator.hasNext()) { - val element = iterator.next() - if (index == count++) - return element - } - return defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. - */ -public inline fun String.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 collection. - */ -public fun Array.elementAtOrNull(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 collection. - */ -public fun BooleanArray.elementAtOrNull(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 collection. - */ -public fun ByteArray.elementAtOrNull(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 collection. - */ -public fun CharArray.elementAtOrNull(index: Int): Char? { - 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 collection. - */ -public fun DoubleArray.elementAtOrNull(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 collection. - */ -public fun FloatArray.elementAtOrNull(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 collection. - */ -public fun IntArray.elementAtOrNull(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 collection. - */ -public fun LongArray.elementAtOrNull(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 collection. - */ -public fun ShortArray.elementAtOrNull(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 collection. - */ -public fun Iterable.elementAtOrNull(index: Int): T? { - if (this is List) - return this.getOrNull(index) - if (index < 0) - return null - val iterator = iterator() - var count = 0 - while (iterator.hasNext()) { - val element = iterator.next() - if (index == count++) - return element - } - return null -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. - */ -public fun List.elementAtOrNull(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 collection. - */ -public fun Sequence.elementAtOrNull(index: Int): T? { - if (index < 0) - return null - val iterator = iterator() - var count = 0 - while (iterator.hasNext()) { - val element = iterator.next() - if (index == count++) - return element - } - return null -} - -/** - * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. - */ -public fun String.elementAtOrNull(index: Int): Char? { - return if (index >= 0 && index <= lastIndex) get(index) else null -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - */ -public inline fun Array.find(predicate: (T) -> Boolean): T? { - return firstOrNull(predicate) -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - */ -public inline fun BooleanArray.find(predicate: (Boolean) -> Boolean): Boolean? { - return firstOrNull(predicate) -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - */ -public inline fun ByteArray.find(predicate: (Byte) -> Boolean): Byte? { - return firstOrNull(predicate) -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - */ -public inline fun CharArray.find(predicate: (Char) -> Boolean): Char? { - return firstOrNull(predicate) -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - */ -public inline fun DoubleArray.find(predicate: (Double) -> Boolean): Double? { - return firstOrNull(predicate) -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - */ -public inline fun FloatArray.find(predicate: (Float) -> Boolean): Float? { - return firstOrNull(predicate) -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - */ -public inline fun IntArray.find(predicate: (Int) -> Boolean): Int? { - return firstOrNull(predicate) -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - */ -public inline fun LongArray.find(predicate: (Long) -> Boolean): Long? { - return firstOrNull(predicate) -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - */ -public inline fun ShortArray.find(predicate: (Short) -> Boolean): Short? { - return firstOrNull(predicate) -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - */ -public inline fun Iterable.find(predicate: (T) -> Boolean): T? { - return firstOrNull(predicate) -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - */ -public inline fun Sequence.find(predicate: (T) -> Boolean): T? { - return firstOrNull(predicate) -} - -/** - * Returns the first character matching the given [predicate], or `null` if character was not found. - */ -public inline fun String.find(predicate: (Char) -> Boolean): Char? { - return firstOrNull(predicate) -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -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. - */ -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. - */ -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. - */ -public inline fun CharArray.findLast(predicate: (Char) -> Boolean): Char? { - return lastOrNull(predicate) -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -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. - */ -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. - */ -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. - */ -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. - */ -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. - */ -public inline fun Iterable.findLast(predicate: (T) -> Boolean): T? { - return lastOrNull(predicate) -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -public inline fun List.findLast(predicate: (T) -> Boolean): T? { - return lastOrNull(predicate) -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -public inline fun Sequence.findLast(predicate: (T) -> Boolean): T? { - return lastOrNull(predicate) -} - -/** - * Returns the last character matching the given [predicate], or `null` if no such character was found. - */ -public inline fun String.findLast(predicate: (Char) -> Boolean): Char? { - return lastOrNull(predicate) -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the collection is empty. - */ -public fun Array.first(): T { - if (isEmpty()) - throw NoSuchElementException("Collection is empty.") - return this[0] -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the collection is empty. - */ -public fun BooleanArray.first(): Boolean { - if (isEmpty()) - throw NoSuchElementException("Collection is empty.") - return this[0] -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the collection is empty. - */ -public fun ByteArray.first(): Byte { - if (isEmpty()) - throw NoSuchElementException("Collection is empty.") - return this[0] -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the collection is empty. - */ -public fun CharArray.first(): Char { - if (isEmpty()) - throw NoSuchElementException("Collection is empty.") - return this[0] -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the collection is empty. - */ -public fun DoubleArray.first(): Double { - if (isEmpty()) - throw NoSuchElementException("Collection is empty.") - return this[0] -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the collection is empty. - */ -public fun FloatArray.first(): Float { - if (isEmpty()) - throw NoSuchElementException("Collection is empty.") - return this[0] -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the collection is empty. - */ -public fun IntArray.first(): Int { - if (isEmpty()) - throw NoSuchElementException("Collection is empty.") - return this[0] -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the collection is empty. - */ -public fun LongArray.first(): Long { - if (isEmpty()) - throw NoSuchElementException("Collection is empty.") - return this[0] -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the collection is empty. - */ -public fun ShortArray.first(): Short { - if (isEmpty()) - throw NoSuchElementException("Collection is empty.") - return this[0] -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the collection is empty. - */ -public fun Iterable.first(): T { - when (this) { - is List -> { - if (isEmpty()) - throw NoSuchElementException("Collection is empty.") - else - return this[0] - } - else -> { - val iterator = iterator() - if (!iterator.hasNext()) - throw NoSuchElementException("Collection is empty.") - return iterator.next() - } - } -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the collection is empty. - */ -public fun List.first(): T { - if (isEmpty()) - throw NoSuchElementException("Collection is empty.") - return this[0] -} - -/** - * Returns first element. - * @throws [NoSuchElementException] if the collection is empty. - */ -public fun Sequence.first(): T { - val iterator = iterator() - if (!iterator.hasNext()) - throw NoSuchElementException("Sequence is empty.") - return iterator.next() -} - -/** - * Returns first character. - * @throws [NoSuchElementException] if the string is empty. - */ -public fun String.first(): Char { - if (isEmpty()) - throw NoSuchElementException("Collection 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("No element matching predicate was found.") -} - -/** - * 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("No element matching predicate was found.") -} - -/** - * 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("No element matching predicate was found.") -} - -/** - * 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("No element matching predicate was found.") -} - -/** - * 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("No element matching predicate was found.") -} - -/** - * 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("No element matching predicate was found.") -} - -/** - * 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("No element matching predicate was found.") -} - -/** - * 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("No element matching predicate was found.") -} - -/** - * 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("No element matching predicate was found.") -} - -/** - * Returns the first element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun Iterable.first(predicate: (T) -> Boolean): T { - for (element in this) if (predicate(element)) return element - throw NoSuchElementException("No element matching predicate was found.") -} - -/** - * Returns the first element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun Sequence.first(predicate: (T) -> Boolean): T { - for (element in this) if (predicate(element)) return element - throw NoSuchElementException("No element matching predicate was found.") -} - -/** - * Returns the first character matching the given [predicate]. - * @throws [NoSuchElementException] if no such character is found. - */ -public inline fun String.first(predicate: (Char) -> Boolean): Char { - for (element in this) if (predicate(element)) return element - throw NoSuchElementException("No element matching predicate was found.") -} - -/** - * Returns the first element, or `null` if the collection is empty. - */ -public fun Array.firstOrNull(): T? { - return if (isEmpty()) null else this[0] -} - -/** - * Returns the first element, or `null` if the collection is empty. - */ -public fun BooleanArray.firstOrNull(): Boolean? { - return if (isEmpty()) null else this[0] -} - -/** - * Returns the first element, or `null` if the collection is empty. - */ -public fun ByteArray.firstOrNull(): Byte? { - return if (isEmpty()) null else this[0] -} - -/** - * Returns the first element, or `null` if the collection is empty. - */ -public fun CharArray.firstOrNull(): Char? { - return if (isEmpty()) null else this[0] -} - -/** - * Returns the first element, or `null` if the collection is empty. - */ -public fun DoubleArray.firstOrNull(): Double? { - return if (isEmpty()) null else this[0] -} - -/** - * Returns the first element, or `null` if the collection is empty. - */ -public fun FloatArray.firstOrNull(): Float? { - return if (isEmpty()) null else this[0] -} - -/** - * Returns the first element, or `null` if the collection is empty. - */ -public fun IntArray.firstOrNull(): Int? { - return if (isEmpty()) null else this[0] -} - -/** - * Returns the first element, or `null` if the collection is empty. - */ -public fun LongArray.firstOrNull(): Long? { - return if (isEmpty()) null else this[0] -} - -/** - * Returns the first element, or `null` if the collection is empty. - */ -public fun ShortArray.firstOrNull(): Short? { - return if (isEmpty()) null else this[0] -} - -/** - * Returns the first element, or `null` if the collection is empty. - */ -public fun Iterable.firstOrNull(): T? { - when (this) { - is List -> { - if (isEmpty()) - return null - else - return this[0] - } - else -> { - val iterator = iterator() - if (!iterator.hasNext()) - return null - return iterator.next() - } - } -} - -/** - * Returns the first element, or `null` if the collection is empty. - */ -public fun List.firstOrNull(): T? { - return if (isEmpty()) null else this[0] -} - -/** - * Returns the first element, or `null` if the collection is empty. - */ -public fun Sequence.firstOrNull(): T? { - val iterator = iterator() - if (!iterator.hasNext()) - return null - return iterator.next() -} - -/** - * Returns the first character, or `null` if string is empty. - */ -public fun String.firstOrNull(): Char? { - return if (isEmpty()) null else this[0] -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - */ -public inline fun Array.firstOrNull(predicate: (T) -> Boolean): T? { - for (element in this) if (predicate(element)) return element - return null -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - */ -public inline fun 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 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 CharArray.firstOrNull(predicate: (Char) -> Boolean): Char? { - 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 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 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 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 Iterable.firstOrNull(predicate: (T) -> Boolean): T? { - for (element in this) if (predicate(element)) return element - return null -} - -/** - * Returns the first element matching the given [predicate], or `null` if element was not found. - */ -public inline fun Sequence.firstOrNull(predicate: (T) -> Boolean): T? { - for (element in this) if (predicate(element)) return element - return null -} - -/** - * Returns the first character matching the given [predicate], or `null` if character was not found. - */ -public inline fun String.firstOrNull(predicate: (Char) -> Boolean): Char? { - for (element in this) if (predicate(element)) return element - return null -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. - */ -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 collection. - */ -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 collection. - */ -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 collection. - */ -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 the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. - */ -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 collection. - */ -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 collection. - */ -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 collection. - */ -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 collection. - */ -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 collection. - */ -public inline fun List.getOrElse(index: Int, defaultValue: (Int) -> T): T { - return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) -} - -/** - * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. - */ -public inline fun String.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 collection. - */ -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 collection. - */ -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 collection. - */ -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 collection. - */ -public fun CharArray.getOrNull(index: Int): Char? { - 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 collection. - */ -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 collection. - */ -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 collection. - */ -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 collection. - */ -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 collection. - */ -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 collection. - */ -public fun List.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 collection. - */ -public fun String.getOrNull(index: Int): Char? { - return if (index >= 0 && index <= lastIndex) get(index) else null -} - -/** - * Returns first index of [element], or -1 if the collection does not contain element. - */ -public fun Array.indexOf(element: T): Int { - if (element == null) { - for (index in indices) { - if (this[index] == null) { - return index - } - } - } else { - for (index in indices) { - if (element == this[index]) { - return index - } - } - } - return -1 -} - -/** - * Returns first index of [element], or -1 if the collection does not contain element. - */ -public fun BooleanArray.indexOf(element: Boolean): Int { - for (index in indices) { - if (element == this[index]) { - return index - } - } - return -1 -} - -/** - * Returns first index of [element], or -1 if the collection does not contain element. - */ -public fun ByteArray.indexOf(element: Byte): Int { - for (index in indices) { - if (element == this[index]) { - return index - } - } - return -1 -} - -/** - * Returns first index of [element], or -1 if the collection does not contain element. - */ -public fun CharArray.indexOf(element: Char): Int { - for (index in indices) { - if (element == this[index]) { - return index - } - } - return -1 -} - -/** - * Returns first index of [element], or -1 if the collection does not contain element. - */ -public fun DoubleArray.indexOf(element: Double): Int { - for (index in indices) { - if (element == this[index]) { - return index - } - } - return -1 -} - -/** - * Returns first index of [element], or -1 if the collection does not contain element. - */ -public fun FloatArray.indexOf(element: Float): Int { - for (index in indices) { - if (element == this[index]) { - return index - } - } - return -1 -} - -/** - * Returns first index of [element], or -1 if the collection does not contain element. - */ -public fun IntArray.indexOf(element: Int): Int { - for (index in indices) { - if (element == this[index]) { - return index - } - } - return -1 -} - -/** - * Returns first index of [element], or -1 if the collection does not contain element. - */ -public fun LongArray.indexOf(element: Long): Int { - for (index in indices) { - if (element == this[index]) { - return index - } - } - return -1 -} - -/** - * Returns first index of [element], or -1 if the collection does not contain element. - */ -public fun ShortArray.indexOf(element: Short): Int { - for (index in indices) { - if (element == this[index]) { - return index - } - } - return -1 -} - -/** - * Returns first index of [element], or -1 if the collection does not contain element. - */ -public fun Iterable.indexOf(element: T): Int { - var index = 0 - for (item in this) { - if (element == item) - return index - index++ - } - return -1 -} - -/** - * Returns first index of [element], or -1 if the collection does not contain element. - */ -public fun Sequence.indexOf(element: T): Int { - var index = 0 - for (item in this) { - if (element == item) - return index - index++ - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element. - */ -public inline fun Array.indexOfFirst(predicate: (T) -> Boolean): Int { - for (index in indices) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element. - */ -public inline fun BooleanArray.indexOfFirst(predicate: (Boolean) -> Boolean): Int { - for (index in indices) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element. - */ -public inline fun ByteArray.indexOfFirst(predicate: (Byte) -> Boolean): Int { - for (index in indices) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element. - */ -public inline fun CharArray.indexOfFirst(predicate: (Char) -> Boolean): Int { - for (index in indices) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element. - */ -public inline fun DoubleArray.indexOfFirst(predicate: (Double) -> Boolean): Int { - for (index in indices) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element. - */ -public inline fun FloatArray.indexOfFirst(predicate: (Float) -> Boolean): Int { - for (index in indices) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element. - */ -public inline fun IntArray.indexOfFirst(predicate: (Int) -> Boolean): Int { - for (index in indices) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element. - */ -public inline fun LongArray.indexOfFirst(predicate: (Long) -> Boolean): Int { - for (index in indices) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element. - */ -public inline fun ShortArray.indexOfFirst(predicate: (Short) -> Boolean): Int { - for (index in indices) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element. - */ -public inline fun Iterable.indexOfFirst(predicate: (T) -> Boolean): Int { - var index = 0 - for (item in this) { - if (predicate(item)) - return index - index++ - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element. - */ -public inline fun List.indexOfFirst(predicate: (T) -> Boolean): Int { - for (index in indices) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element. - */ -public inline fun Sequence.indexOfFirst(predicate: (T) -> Boolean): Int { - var index = 0 - for (item in this) { - if (predicate(item)) - return index - index++ - } - return -1 -} - -/** - * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element. - */ -public inline fun String.indexOfFirst(predicate: (Char) -> Boolean): Int { - for (index in indices) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element. - */ -public inline fun Array.indexOfLast(predicate: (T) -> Boolean): Int { - for (index in indices.reversed()) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element. - */ -public inline fun BooleanArray.indexOfLast(predicate: (Boolean) -> Boolean): Int { - for (index in indices.reversed()) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element. - */ -public inline fun ByteArray.indexOfLast(predicate: (Byte) -> Boolean): Int { - for (index in indices.reversed()) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element. - */ -public inline fun CharArray.indexOfLast(predicate: (Char) -> Boolean): Int { - for (index in indices.reversed()) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element. - */ -public inline fun DoubleArray.indexOfLast(predicate: (Double) -> Boolean): Int { - for (index in indices.reversed()) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element. - */ -public inline fun FloatArray.indexOfLast(predicate: (Float) -> Boolean): Int { - for (index in indices.reversed()) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element. - */ -public inline fun IntArray.indexOfLast(predicate: (Int) -> Boolean): Int { - for (index in indices.reversed()) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element. - */ -public inline fun LongArray.indexOfLast(predicate: (Long) -> Boolean): Int { - for (index in indices.reversed()) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element. - */ -public inline fun ShortArray.indexOfLast(predicate: (Short) -> Boolean): Int { - for (index in indices.reversed()) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element. - */ -public inline fun Iterable.indexOfLast(predicate: (T) -> Boolean): Int { - var lastIndex = -1 - var index = 0 - for (item in this) { - if (predicate(item)) - lastIndex = index - index++ - } - return lastIndex -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element. - */ -public inline fun List.indexOfLast(predicate: (T) -> Boolean): Int { - for (index in indices.reversed()) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element. - */ -public inline fun Sequence.indexOfLast(predicate: (T) -> Boolean): Int { - var lastIndex = -1 - var index = 0 - for (item in this) { - if (predicate(item)) - lastIndex = index - index++ - } - return lastIndex -} - -/** - * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element. - */ -public inline fun String.indexOfLast(predicate: (Char) -> Boolean): Int { - for (index in indices.reversed()) { - if (predicate(this[index])) { - return index - } - } - return -1 -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the collection is empty. - */ -public fun Array.last(): T { - if (isEmpty()) - throw NoSuchElementException("Collection is empty.") - return this[lastIndex] -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the collection is empty. - */ -public fun BooleanArray.last(): Boolean { - if (isEmpty()) - throw NoSuchElementException("Collection is empty.") - return this[lastIndex] -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the collection is empty. - */ -public fun ByteArray.last(): Byte { - if (isEmpty()) - throw NoSuchElementException("Collection is empty.") - return this[lastIndex] -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the collection is empty. - */ -public fun CharArray.last(): Char { - if (isEmpty()) - throw NoSuchElementException("Collection is empty.") - return this[lastIndex] -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the collection is empty. - */ -public fun DoubleArray.last(): Double { - if (isEmpty()) - throw NoSuchElementException("Collection is empty.") - return this[lastIndex] -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the collection is empty. - */ -public fun FloatArray.last(): Float { - if (isEmpty()) - throw NoSuchElementException("Collection is empty.") - return this[lastIndex] -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the collection is empty. - */ -public fun IntArray.last(): Int { - if (isEmpty()) - throw NoSuchElementException("Collection is empty.") - return this[lastIndex] -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the collection is empty. - */ -public fun LongArray.last(): Long { - if (isEmpty()) - throw NoSuchElementException("Collection is empty.") - return this[lastIndex] -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the collection is empty. - */ -public fun ShortArray.last(): Short { - if (isEmpty()) - throw NoSuchElementException("Collection is empty.") - return this[lastIndex] -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the collection is empty. - */ -public fun Iterable.last(): T { - when (this) { - is List -> { - if (isEmpty()) - throw NoSuchElementException("Collection is empty.") - else - return this[this.lastIndex] - } - else -> { - val iterator = iterator() - if (!iterator.hasNext()) - throw NoSuchElementException("Collection is empty.") - var last = iterator.next() - while (iterator.hasNext()) - last = iterator.next() - return last - } - } -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the collection is empty. - */ -public fun List.last(): T { - if (isEmpty()) - throw NoSuchElementException("Collection is empty.") - return this[lastIndex] -} - -/** - * Returns the last element. - * @throws [NoSuchElementException] if the collection is empty. - */ -public fun Sequence.last(): T { - val iterator = iterator() - if (!iterator.hasNext()) - throw NoSuchElementException("Sequence is empty.") - var last = iterator.next() - while (iterator.hasNext()) - last = iterator.next() - return last -} - -/** - * "Returns the last character. - * @throws [NoSuchElementException] if the string is empty. - */ -public fun String.last(): Char { - if (isEmpty()) - throw NoSuchElementException("Collection 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("Collection doesn't contain any 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("Collection doesn't contain any 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("Collection doesn't contain any 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("Collection doesn't contain any 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("Collection doesn't contain any 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("Collection doesn't contain any 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("Collection doesn't contain any 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("Collection doesn't contain any 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("Collection doesn't contain any element matching the predicate.") -} - -/** - * Returns the last element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun Iterable.last(predicate: (T) -> Boolean): T { - if (this is List) - return this.last(predicate) - var last: T? = null - var found = false - for (element in this) { - if (predicate(element)) { - last = element - found = true - } - } - if (!found) throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") - return last as T -} - -/** - * Returns the last element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun List.last(predicate: (T) -> Boolean): T { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") -} - -/** - * Returns the last element matching the given [predicate]. - * @throws [NoSuchElementException] if no such element is found. - */ -public inline fun Sequence.last(predicate: (T) -> Boolean): T { - var last: T? = null - var found = false - for (element in this) { - if (predicate(element)) { - last = element - found = true - } - } - if (!found) throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") - return last as T -} - -/** - * "Returns the last character matching the given [predicate]. - * @throws [NoSuchElementException] if no such character is found. - */ -public inline fun String.last(predicate: (Char) -> Boolean): Char { - var last: Char? = null - var found = false - for (element in this) { - if (predicate(element)) { - last = element - found = true - } - } - if (!found) throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") - return last as Char -} - -/** - * Returns last index of [element], or -1 if the collection does not contain element. - */ -public fun Array.lastIndexOf(element: T): Int { - if (element == null) { - for (index in indices.reversed()) { - if (this[index] == null) { - return index - } - } - } else { - for (index in indices.reversed()) { - if (element == this[index]) { - return index - } - } - } - return -1 -} - -/** - * Returns last index of [element], or -1 if the collection does not contain element. - */ -public fun BooleanArray.lastIndexOf(element: Boolean): Int { - for (index in indices.reversed()) { - if (element == this[index]) { - return index - } - } - return -1 -} - -/** - * Returns last index of [element], or -1 if the collection does not contain element. - */ -public fun ByteArray.lastIndexOf(element: Byte): Int { - for (index in indices.reversed()) { - if (element == this[index]) { - return index - } - } - return -1 -} - -/** - * Returns last index of [element], or -1 if the collection does not contain element. - */ -public fun CharArray.lastIndexOf(element: Char): Int { - for (index in indices.reversed()) { - if (element == this[index]) { - return index - } - } - return -1 -} - -/** - * Returns last index of [element], or -1 if the collection does not contain element. - */ -public fun DoubleArray.lastIndexOf(element: Double): Int { - for (index in indices.reversed()) { - if (element == this[index]) { - return index - } - } - return -1 -} - -/** - * Returns last index of [element], or -1 if the collection does not contain element. - */ -public fun FloatArray.lastIndexOf(element: Float): Int { - for (index in indices.reversed()) { - if (element == this[index]) { - return index - } - } - return -1 -} - -/** - * Returns last index of [element], or -1 if the collection does not contain element. - */ -public fun IntArray.lastIndexOf(element: Int): Int { - for (index in indices.reversed()) { - if (element == this[index]) { - return index - } - } - return -1 -} - -/** - * Returns last index of [element], or -1 if the collection does not contain element. - */ -public fun LongArray.lastIndexOf(element: Long): Int { - for (index in indices.reversed()) { - if (element == this[index]) { - return index - } - } - return -1 -} - -/** - * Returns last index of [element], or -1 if the collection does not contain element. - */ -public fun ShortArray.lastIndexOf(element: Short): Int { - for (index in indices.reversed()) { - if (element == this[index]) { - return index - } - } - return -1 -} - -/** - * Returns last index of [element], or -1 if the collection does not contain element. - */ -public fun Iterable.lastIndexOf(element: T): Int { - var lastIndex = -1 - var index = 0 - for (item in this) { - if (element == item) - lastIndex = index - index++ - } - return lastIndex -} - -/** - * Returns last index of [element], or -1 if the collection does not contain element. - */ -public fun Sequence.lastIndexOf(element: T): Int { - var lastIndex = -1 - var index = 0 - for (item in this) { - if (element == item) - lastIndex = index - index++ - } - return lastIndex -} - -/** - * Returns the last element, or `null` if the collection is empty. - */ -public fun Array.lastOrNull(): T? { - return if (isEmpty()) null else this[size() - 1] -} - -/** - * Returns the last element, or `null` if the collection is empty. - */ -public fun BooleanArray.lastOrNull(): Boolean? { - return if (isEmpty()) null else this[size() - 1] -} - -/** - * Returns the last element, or `null` if the collection is empty. - */ -public fun ByteArray.lastOrNull(): Byte? { - return if (isEmpty()) null else this[size() - 1] -} - -/** - * Returns the last element, or `null` if the collection is empty. - */ -public fun CharArray.lastOrNull(): Char? { - return if (isEmpty()) null else this[size() - 1] -} - -/** - * Returns the last element, or `null` if the collection is empty. - */ -public fun DoubleArray.lastOrNull(): Double? { - return if (isEmpty()) null else this[size() - 1] -} - -/** - * Returns the last element, or `null` if the collection is empty. - */ -public fun FloatArray.lastOrNull(): Float? { - return if (isEmpty()) null else this[size() - 1] -} - -/** - * Returns the last element, or `null` if the collection is empty. - */ -public fun IntArray.lastOrNull(): Int? { - return if (isEmpty()) null else this[size() - 1] -} - -/** - * Returns the last element, or `null` if the collection is empty. - */ -public fun LongArray.lastOrNull(): Long? { - return if (isEmpty()) null else this[size() - 1] -} - -/** - * Returns the last element, or `null` if the collection is empty. - */ -public fun ShortArray.lastOrNull(): Short? { - return if (isEmpty()) null else this[size() - 1] -} - -/** - * Returns the last element, or `null` if the collection is empty. - */ -public fun Iterable.lastOrNull(): T? { - when (this) { - is List -> return if (isEmpty()) null else this[size() - 1] - else -> { - val iterator = iterator() - if (!iterator.hasNext()) - return null - var last = iterator.next() - while (iterator.hasNext()) - last = iterator.next() - return last - } - } -} - -/** - * Returns the last element, or `null` if the collection is empty. - */ -public fun List.lastOrNull(): T? { - return if (isEmpty()) null else this[size() - 1] -} - -/** - * Returns the last element, or `null` if the collection is empty. - */ -public fun Sequence.lastOrNull(): T? { - val iterator = iterator() - if (!iterator.hasNext()) - return null - var last = iterator.next() - while (iterator.hasNext()) - last = iterator.next() - return last -} - -/** - * Returns the last character, or `null` if the string is empty. - */ -public fun String.lastOrNull(): Char? { - return if (isEmpty()) null else this[length() - 1] -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -public inline fun Array.lastOrNull(predicate: (T) -> Boolean): T? { - for (index in this.indices.reversed()) { - val element = this[index] - if (predicate(element)) return element - } - return null -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -public inline fun 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 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 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 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 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 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 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 Iterable.lastOrNull(predicate: (T) -> Boolean): T? { - if (this is List) - return this.lastOrNull(predicate) - var last: T? = null - for (element in this) { - if (predicate(element)) { - last = element - } - } - return last -} - -/** - * Returns the last element matching the given [predicate], or `null` if no such element was found. - */ -public inline fun List.lastOrNull(predicate: (T) -> Boolean): T? { - 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 Sequence.lastOrNull(predicate: (T) -> Boolean): T? { - var last: T? = null - for (element in this) { - if (predicate(element)) { - last = element - } - } - return last -} - -/** - * Returns the last character matching the given [predicate], or `null` if no such character was found. - */ -public inline fun String.lastOrNull(predicate: (Char) -> Boolean): Char? { - var last: Char? = null - for (element in this) { - if (predicate(element)) { - last = element - } - } - return last -} - -/** - * Returns the single element, or throws an exception if the collection is empty or has more than one element. - */ -public fun Array.single(): T { - return when (size()) { - 0 -> throw NoSuchElementException("Collection is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element.") - } -} - -/** - * Returns the single element, or throws an exception if the collection is empty or has more than one element. - */ -public fun BooleanArray.single(): Boolean { - return when (size()) { - 0 -> throw NoSuchElementException("Collection is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element.") - } -} - -/** - * Returns the single element, or throws an exception if the collection is empty or has more than one element. - */ -public fun ByteArray.single(): Byte { - return when (size()) { - 0 -> throw NoSuchElementException("Collection is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element.") - } -} - -/** - * Returns the single element, or throws an exception if the collection is empty or has more than one element. - */ -public fun CharArray.single(): Char { - return when (size()) { - 0 -> throw NoSuchElementException("Collection is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element.") - } -} - -/** - * Returns the single element, or throws an exception if the collection is empty or has more than one element. - */ -public fun DoubleArray.single(): Double { - return when (size()) { - 0 -> throw NoSuchElementException("Collection is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element.") - } -} - -/** - * Returns the single element, or throws an exception if the collection is empty or has more than one element. - */ -public fun FloatArray.single(): Float { - return when (size()) { - 0 -> throw NoSuchElementException("Collection is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element.") - } -} - -/** - * Returns the single element, or throws an exception if the collection is empty or has more than one element. - */ -public fun IntArray.single(): Int { - return when (size()) { - 0 -> throw NoSuchElementException("Collection is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element.") - } -} - -/** - * Returns the single element, or throws an exception if the collection is empty or has more than one element. - */ -public fun LongArray.single(): Long { - return when (size()) { - 0 -> throw NoSuchElementException("Collection is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element.") - } -} - -/** - * Returns the single element, or throws an exception if the collection is empty or has more than one element. - */ -public fun ShortArray.single(): Short { - return when (size()) { - 0 -> throw NoSuchElementException("Collection is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element.") - } -} - -/** - * Returns the single element, or throws an exception if the collection is empty or has more than one element. - */ -public fun Iterable.single(): T { - when (this) { - is List -> return when (size()) { - 0 -> throw NoSuchElementException("Collection is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element.") - } - else -> { - val iterator = iterator() - if (!iterator.hasNext()) - throw NoSuchElementException("Collection is empty.") - var single = iterator.next() - if (iterator.hasNext()) - throw IllegalArgumentException("Collection has more than one element.") - return single - } - } -} - -/** - * Returns the single element, or throws an exception if the collection is empty or has more than one element. - */ -public fun List.single(): T { - return when (size()) { - 0 -> throw NoSuchElementException("Collection is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element.") - } -} - -/** - * Returns the single element, or throws an exception if the collection is empty or has more than one element. - */ -public fun Sequence.single(): T { - val iterator = iterator() - if (!iterator.hasNext()) - throw NoSuchElementException("Sequence is empty.") - var single = iterator.next() - if (iterator.hasNext()) - throw IllegalArgumentException("Sequence has more than one element.") - return single -} - -/** - * Returns the single character, or throws an exception if the string is empty or has more than one character. - */ -public fun String.single(): Char { - return when (length()) { - 0 -> throw NoSuchElementException("Collection is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element.") - } -} - -/** - * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. - */ -public inline fun Array.single(predicate: (T) -> Boolean): T { - var single: T? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Collection contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") - return single as T -} - -/** - * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. - */ -public inline fun BooleanArray.single(predicate: (Boolean) -> Boolean): Boolean { - var single: Boolean? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Collection contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") - return single as Boolean -} - -/** - * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. - */ -public inline fun ByteArray.single(predicate: (Byte) -> Boolean): Byte { - var single: Byte? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Collection contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") - return single as Byte -} - -/** - * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. - */ -public inline fun CharArray.single(predicate: (Char) -> Boolean): Char { - var single: Char? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Collection contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") - return single as Char -} - -/** - * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. - */ -public inline fun DoubleArray.single(predicate: (Double) -> Boolean): Double { - var single: Double? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Collection contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") - return single as Double -} - -/** - * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. - */ -public inline fun FloatArray.single(predicate: (Float) -> Boolean): Float { - var single: Float? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Collection contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") - return single as Float -} - -/** - * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. - */ -public inline fun IntArray.single(predicate: (Int) -> Boolean): Int { - var single: Int? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Collection contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") - return single as Int -} - -/** - * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. - */ -public inline fun LongArray.single(predicate: (Long) -> Boolean): Long { - var single: Long? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Collection contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") - return single as Long -} - -/** - * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. - */ -public inline fun ShortArray.single(predicate: (Short) -> Boolean): Short { - var single: Short? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Collection contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") - return single as Short -} - -/** - * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. - */ -public inline fun Iterable.single(predicate: (T) -> Boolean): T { - var single: T? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Collection contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") - return single as T -} - -/** - * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. - */ -public inline fun Sequence.single(predicate: (T) -> Boolean): T { - var single: T? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Collection contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") - return single as T -} - -/** - * Returns the single character matching the given [predicate], or throws exception if there is no or more than one matching character. - */ -public inline fun String.single(predicate: (Char) -> Boolean): Char { - var single: Char? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) throw IllegalArgumentException("Collection contains more than one matching element.") - single = element - found = true - } - } - if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") - return single as Char -} - -/** - * Returns single element, or `null` if the collection is empty or has more than one element. - */ -public fun Array.singleOrNull(): T? { - return if (size() == 1) this[0] else null -} - -/** - * Returns single element, or `null` if the collection 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 collection is empty or has more than one element. - */ -public fun ByteArray.singleOrNull(): Byte? { - return if (size() == 1) this[0] else null -} - -/** - * Returns single element, or `null` if the collection is empty or has more than one element. - */ -public fun CharArray.singleOrNull(): Char? { - return if (size() == 1) this[0] else null -} - -/** - * Returns single element, or `null` if the collection 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 collection 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 collection 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 collection 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 collection 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 collection is empty or has more than one element. - */ -public fun Iterable.singleOrNull(): T? { - when (this) { - is List -> return if (size() == 1) this[0] else null - else -> { - val iterator = iterator() - if (!iterator.hasNext()) - return null - var single = iterator.next() - if (iterator.hasNext()) - return null - return single - } - } -} - -/** - * Returns single element, or `null` if the collection is empty or has more than one element. - */ -public fun List.singleOrNull(): T? { - return if (size() == 1) this[0] else null -} - -/** - * Returns single element, or `null` if the collection is empty or has more than one element. - */ -public fun Sequence.singleOrNull(): T? { - val iterator = iterator() - if (!iterator.hasNext()) - return null - var single = iterator.next() - if (iterator.hasNext()) - return null - return single -} - -/** - * Returns the single character, or `null` if the string is empty or has more than one character. - */ -public fun String.singleOrNull(): Char? { - return if (length() == 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 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 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 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 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 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 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 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 Iterable.singleOrNull(predicate: (T) -> Boolean): T? { - var single: T? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) return null - single = element - found = true - } - } - if (!found) return null - return single -} - -/** - * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. - */ -public inline fun Sequence.singleOrNull(predicate: (T) -> Boolean): T? { - var single: T? = null - var found = false - for (element in this) { - if (predicate(element)) { - if (found) return null - single = element - found = true - } - } - if (!found) return null - return single -} - -/** - * Returns the single character matching the given [predicate], or `null` if character was not found or more than one character was found. - */ -public inline fun String.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 -} - diff --git a/libraries/stdlib/src/generated/_Filtering.kt b/libraries/stdlib/src/generated/_Filtering.kt deleted file mode 100644 index 8dca320bcfe..00000000000 --- a/libraries/stdlib/src/generated/_Filtering.kt +++ /dev/null @@ -1,2023 +0,0 @@ -package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// - -import kotlin.platform.* -import java.util.* - -import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js - -/** - * 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." }) - if (n == 0) - return toList() - if (n >= size()) - return emptyList() - val list = ArrayList(size() - n) - for (index in n..size() - 1) { - list.add(this[index]) - } - return list -} - -/** - * 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." }) - if (n == 0) - return toList() - if (n >= size()) - return emptyList() - val list = ArrayList(size() - n) - for (index in n..size() - 1) { - list.add(this[index]) - } - return list -} - -/** - * 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." }) - if (n == 0) - return toList() - if (n >= size()) - return emptyList() - val list = ArrayList(size() - n) - for (index in n..size() - 1) { - list.add(this[index]) - } - return list -} - -/** - * 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." }) - if (n == 0) - return toList() - if (n >= size()) - return emptyList() - val list = ArrayList(size() - n) - for (index in n..size() - 1) { - list.add(this[index]) - } - return list -} - -/** - * 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." }) - if (n == 0) - return toList() - if (n >= size()) - return emptyList() - val list = ArrayList(size() - n) - for (index in n..size() - 1) { - list.add(this[index]) - } - return list -} - -/** - * 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." }) - if (n == 0) - return toList() - if (n >= size()) - return emptyList() - val list = ArrayList(size() - n) - for (index in n..size() - 1) { - list.add(this[index]) - } - return list -} - -/** - * 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." }) - if (n == 0) - return toList() - if (n >= size()) - return emptyList() - val list = ArrayList(size() - n) - for (index in n..size() - 1) { - list.add(this[index]) - } - return list -} - -/** - * 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." }) - if (n == 0) - return toList() - if (n >= size()) - return emptyList() - val list = ArrayList(size() - n) - for (index in n..size() - 1) { - list.add(this[index]) - } - return list -} - -/** - * 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." }) - if (n == 0) - return toList() - if (n >= size()) - return emptyList() - val list = ArrayList(size() - n) - for (index in n..size() - 1) { - list.add(this[index]) - } - return list -} - -/** - * Returns a list containing all elements except first [n] elements. - */ -public fun Iterable.drop(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) - if (n == 0) return toList() - val list: ArrayList - if (this is Collection<*>) { - val resultSize = size() - n - if (resultSize <= 0) - return emptyList() - list = ArrayList(resultSize) - if (this is List) { - for (index in n..size() - 1) { - list.add(this[index]) - } - return list - } - } - else { - list = ArrayList() - } - var count = 0 - for (item in this) { - if (count++ >= n) list.add(item) - } - return list -} - -/** - * Returns a sequence containing all elements except first [n] elements. - */ -public fun Sequence.drop(n: Int): Sequence { - require(n >= 0, { "Requested element count $n is less than zero." }) - return if (n == 0) this else DropSequence(this, n) -} - -/** - * Returns a string with the first [n] characters removed. - */ -public fun String.drop(n: Int): String { - return substring(Math.min(n, length())) -} - -/** - * 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 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 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 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 [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 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 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 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 List.dropLast(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) - return take((size() - n).coerceAtLeast(0)) -} - -/** - * Returns a string with the last [n] characters removed. - */ -public fun String.dropLast(n: Int): String { - require(n >= 0, { "Requested character count $n is less than zero." }) - return take((length() - n).coerceAtLeast(0)) -} - -/** - * Returns a 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 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 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 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 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 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 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 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 List.dropLastWhile(predicate: (T) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return take(index + 1) - } - } - return emptyList() -} - -/** - * Returns a string containing all characters except last characters that satisfy the given [predicate]. - */ -public inline fun String.dropLastWhile(predicate: (Char) -> Boolean): String { - return trimEnd(predicate) -} - -/** - * 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) - 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 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 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 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 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 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 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 Iterable.dropWhile(predicate: (T) -> Boolean): List { - var yielding = false - val list = ArrayList() - for (item in this) - if (yielding) - list.add(item) - else if (!predicate(item)) { - list.add(item) - yielding = true - } - return list -} - -/** - * Returns a sequence containing all elements except first elements that satisfy the given [predicate]. - */ -public fun Sequence.dropWhile(predicate: (T) -> Boolean): Sequence { - return DropWhileSequence(this, predicate) -} - -/** - * Returns a string containing all characters except first characters that satisfy the given [predicate]. - */ -public inline fun String.dropWhile(predicate: (Char) -> Boolean): String { - return trimStart(predicate) -} - -/** - * Returns a list containing all elements matching the given [predicate]. - */ -public inline fun Array.filter(predicate: (T) -> Boolean): List { - return filterTo(ArrayList(), predicate) -} - -/** - * Returns a list containing all elements matching the given [predicate]. - */ -public inline fun BooleanArray.filter(predicate: (Boolean) -> Boolean): List { - return filterTo(ArrayList(), predicate) -} - -/** - * Returns a list containing all elements matching the given [predicate]. - */ -public inline fun ByteArray.filter(predicate: (Byte) -> Boolean): List { - return filterTo(ArrayList(), predicate) -} - -/** - * Returns a list containing all elements matching the given [predicate]. - */ -public inline fun CharArray.filter(predicate: (Char) -> Boolean): List { - return filterTo(ArrayList(), predicate) -} - -/** - * Returns a list containing all elements matching the given [predicate]. - */ -public inline fun DoubleArray.filter(predicate: (Double) -> Boolean): List { - return filterTo(ArrayList(), predicate) -} - -/** - * Returns a list containing all elements matching the given [predicate]. - */ -public inline fun FloatArray.filter(predicate: (Float) -> Boolean): List { - return filterTo(ArrayList(), predicate) -} - -/** - * Returns a list containing all elements matching the given [predicate]. - */ -public inline fun IntArray.filter(predicate: (Int) -> Boolean): List { - return filterTo(ArrayList(), predicate) -} - -/** - * Returns a list containing all elements matching the given [predicate]. - */ -public inline fun LongArray.filter(predicate: (Long) -> Boolean): List { - return filterTo(ArrayList(), predicate) -} - -/** - * Returns a list containing all elements matching the given [predicate]. - */ -public inline fun ShortArray.filter(predicate: (Short) -> Boolean): List { - return filterTo(ArrayList(), predicate) -} - -/** - * Returns a list containing all elements matching the given [predicate]. - */ -public inline fun Iterable.filter(predicate: (T) -> Boolean): List { - return filterTo(ArrayList(), predicate) -} - -/** - * Returns a sequence containing all elements matching the given [predicate]. - */ -public fun Sequence.filter(predicate: (T) -> Boolean): Sequence { - return FilteringSequence(this, true, predicate) -} - -/** - * Returns a string containing only those characters from the original string that match the given [predicate]. - */ -public inline fun String.filter(predicate: (Char) -> Boolean): String { - return filterTo(StringBuilder(), predicate).toString() -} - -/** - * Returns a list containing all elements not matching the given [predicate]. - */ -public inline fun Array.filterNot(predicate: (T) -> Boolean): List { - return filterNotTo(ArrayList(), predicate) -} - -/** - * Returns a list containing all elements not matching the given [predicate]. - */ -public inline fun BooleanArray.filterNot(predicate: (Boolean) -> 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 CharArray.filterNot(predicate: (Char) -> 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 FloatArray.filterNot(predicate: (Float) -> 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 ShortArray.filterNot(predicate: (Short) -> Boolean): List { - return filterNotTo(ArrayList(), predicate) -} - -/** - * Returns a list containing all elements not matching the given [predicate]. - */ -public inline fun Iterable.filterNot(predicate: (T) -> Boolean): List { - return filterNotTo(ArrayList(), predicate) -} - -/** - * Returns a sequence containing all elements not matching the given [predicate]. - */ -public fun Sequence.filterNot(predicate: (T) -> Boolean): Sequence { - return FilteringSequence(this, false, predicate) -} - -/** - * Returns a string containing only those characters from the original string that do not match the given [predicate]. - */ -public inline fun String.filterNot(predicate: (Char) -> Boolean): String { - return filterNotTo(StringBuilder(), predicate).toString() -} - -/** - * Returns a list containing all elements that are not `null`. - */ -public fun Array.filterNotNull(): List { - return filterNotNullTo(ArrayList()) -} - -/** - * Returns a list containing all elements that are not `null`. - */ -public fun Iterable.filterNotNull(): List { - return filterNotNullTo(ArrayList()) -} - -/** - * Returns a sequence containing all elements that are not `null`. - */ -public fun Sequence.filterNotNull(): Sequence { - return filterNot { it == null } as Sequence -} - -/** - * Appends all elements that are not `null` to the given [destination]. - */ -public fun , T : Any> Array.filterNotNullTo(destination: C): C { - for (element in this) if (element != null) destination.add(element) - return destination -} - -/** - * Appends all elements that are not `null` to the given [destination]. - */ -public fun , T : Any> Iterable.filterNotNullTo(destination: C): C { - for (element in this) if (element != null) destination.add(element) - return destination -} - -/** - * Appends all elements that are not `null` to the given [destination]. - */ -public fun , T : Any> Sequence.filterNotNullTo(destination: C): C { - for (element in this) if (element != null) destination.add(element) - return destination -} - -/** - * Appends all elements not matching the given [predicate] to the given [destination]. - */ -public inline fun > Array.filterNotTo(destination: C, predicate: (T) -> Boolean): C { - for (element in this) if (!predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements not matching the given [predicate] to the given [destination]. - */ -public inline fun > 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 > 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 > CharArray.filterNotTo(destination: C, predicate: (Char) -> 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 > 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 > 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 > 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 > Iterable.filterNotTo(destination: C, predicate: (T) -> Boolean): C { - for (element in this) if (!predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements not matching the given [predicate] to the given [destination]. - */ -public inline fun > Sequence.filterNotTo(destination: C, predicate: (T) -> Boolean): C { - for (element in this) if (!predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all characters not matching the given [predicate] to the given [destination]. - */ -public inline fun String.filterNotTo(destination: C, predicate: (Char) -> Boolean): C { - for (element in this) if (!predicate(element)) destination.append(element) - return destination -} - -/** - * Appends all elements matching the given [predicate] into 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] into 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] into 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] into 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 -} - -/** - * Appends all elements matching the given [predicate] into 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] into 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] into 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] into 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] into 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] into the given [destination]. - */ -public inline fun > Iterable.filterTo(destination: C, predicate: (T) -> Boolean): C { - for (element in this) if (predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all elements matching the given [predicate] into the given [destination]. - */ -public inline fun > Sequence.filterTo(destination: C, predicate: (T) -> Boolean): C { - for (element in this) if (predicate(element)) destination.add(element) - return destination -} - -/** - * Appends all characters matching the given [predicate] to the given [destination]. - */ -public inline fun String.filterTo(destination: C, predicate: (Char) -> Boolean): C { - for (index in 0..length() - 1) { - val element = get(index) - if (predicate(element)) destination.append(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.end + 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.end + 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.end + 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.end + 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.end + 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.end + 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.end + 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.end + 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.end + 1).asList() -} - -/** - * Returns a list containing elements at indices in the specified [indices] range. - */ -public fun List.slice(indices: IntRange): List { - if (indices.isEmpty()) return listOf() - return this.subList(indices.start, indices.end + 1).toList() -} - -/** - * Returns a string containing characters at indices at the specified [indices]. - */ -public fun String.slice(indices: IntRange): String { - if (indices.isEmpty()) return "" - return substring(indices) -} - -/** - * Returns a list containing elements at specified [indices]. - */ -public fun Array.slice(indices: Iterable): List { - val size = indices.collectionSizeOrDefault(10) - if (size == 0) return listOf() - 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 listOf() - 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 listOf() - 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 listOf() - 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 listOf() - 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 listOf() - 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 listOf() - 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 listOf() - 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 listOf() - val list = ArrayList(size) - for (index in indices) { - list.add(get(index)) - } - return list -} - -/** - * Returns a list containing elements at specified [indices]. - */ -public fun List.slice(indices: Iterable): List { - val size = indices.collectionSizeOrDefault(10) - if (size == 0) return listOf() - val list = ArrayList(size) - for (index in indices) { - list.add(get(index)) - } - return list -} - -/** - * Returns a string containing characters at specified [indices]. - */ -public fun String.slice(indices: Iterable): String { - val size = indices.collectionSizeOrDefault(10) - if (size == 0) return "" - val result = StringBuilder(size) - for (i in indices) { - result.append(get(i)) - } - return result.toString() -} - -/** - * Returns an array containing elements of this array at specified [indices]. - */ -public fun Array.sliceArray(indices: Collection): Array { - val result = arrayOfNulls(this, indices.size()) as Array - 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 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 CharArray.sliceArray(indices: Collection): CharArray { - val result = CharArray(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 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 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 ShortArray.sliceArray(indices: Collection): ShortArray { - val result = ShortArray(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.end + 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.end + 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.end + 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.end + 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.end + 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.end + 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.end + 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.end + 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.end + 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() - 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() - 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() - 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() - 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() - 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() - 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() - 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() - 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() - 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 Iterable.take(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) - if (n == 0) return emptyList() - if (this is Collection && n >= size()) return toList() - var count = 0 - val list = ArrayList(n) - for (item in this) { - if (count++ == n) - break - list.add(item) - } - return list -} - -/** - * Returns a sequence containing first [n] elements. - */ -public fun Sequence.take(n: Int): Sequence { - require(n >= 0, { "Requested element count $n is less than zero." }) - return if (n == 0) emptySequence() else TakeSequence(this, n) -} - -/** - * Returns a string containing the first [n] characters from this string, or the entire string if this string is shorter. - */ -public fun String.take(n: Int): String { - require(n >= 0, { "Requested character count $n is less than zero." }) - return substring(0, Math.min(n, length())) -} - -/** - * 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() - 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() - 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() - 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() - 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() - 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() - 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() - 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() - 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() - 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 List.takeLast(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) - if (n == 0) return emptyList() - val size = size() - if (n >= size) return toList() - val list = ArrayList(n) - for (index in size - n .. size - 1) - list.add(this[index]) - return list -} - -/** - * Returns a string containing the last [n] characters from this string, or the entire string if this string is shorter. - */ -public fun String.takeLast(n: Int): String { - require(n >= 0, { "Requested character count $n is less than zero." }) - val length = length() - return substring(length - Math.min(n, length), length) -} - -/** - * 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 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 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 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 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 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 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 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 List.takeLastWhile(predicate: (T) -> Boolean): List { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return drop(index + 1) - } - } - return toList() -} - -/** - * Returns a string containing last characters that satisfy the given [predicate]. - */ -public inline fun String.takeLastWhile(predicate: (Char) -> Boolean): String { - for (index in lastIndex downTo 0) { - if (!predicate(this[index])) { - return substring(index + 1) - } - } - return this -} - -/** - * Returns a 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 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 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 CharArray.takeWhile(predicate: (Char) -> 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 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 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 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 Iterable.takeWhile(predicate: (T) -> Boolean): List { - val list = ArrayList() - for (item in this) { - if (!predicate(item)) - break; - list.add(item) - } - return list -} - -/** - * Returns a sequence containing first elements satisfying the given [predicate]. - */ -public fun Sequence.takeWhile(predicate: (T) -> Boolean): Sequence { - return TakeWhileSequence(this, predicate) -} - -/** - * Returns a string containing the first characters that satisfy the given [predicate]. - */ -public inline fun String.takeWhile(predicate: (Char) -> Boolean): String { - for (index in 0..length() - 1) - if (!predicate(get(index))) { - return substring(0, index) - } - return this -} - diff --git a/libraries/stdlib/src/generated/_Generators.kt b/libraries/stdlib/src/generated/_Generators.kt deleted file mode 100644 index 2e2d293ebb5..00000000000 --- a/libraries/stdlib/src/generated/_Generators.kt +++ /dev/null @@ -1,1112 +0,0 @@ -package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// - -import kotlin.platform.* -import java.util.* - -import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js - -/** - * 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.merge(array: Array, transform: (T, R) -> V): List { - val size = Math.min(size(), array.size()) - val list = ArrayList(size) - for (i in 0..size-1) { - list.add(transform(this[i], array[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.merge(array: Array, transform: (Boolean, R) -> V): List { - val size = Math.min(size(), array.size()) - val list = ArrayList(size) - for (i in 0..size-1) { - list.add(transform(this[i], array[i])) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun ByteArray.merge(array: Array, transform: (Byte, R) -> V): List { - val size = Math.min(size(), array.size()) - val list = ArrayList(size) - for (i in 0..size-1) { - list.add(transform(this[i], array[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.merge(array: Array, transform: (Char, R) -> V): List { - val size = Math.min(size(), array.size()) - val list = ArrayList(size) - for (i in 0..size-1) { - list.add(transform(this[i], array[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.merge(array: Array, transform: (Double, R) -> V): List { - val size = Math.min(size(), array.size()) - val list = ArrayList(size) - for (i in 0..size-1) { - list.add(transform(this[i], array[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.merge(array: Array, transform: (Float, R) -> V): List { - val size = Math.min(size(), array.size()) - val list = ArrayList(size) - for (i in 0..size-1) { - list.add(transform(this[i], array[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.merge(array: Array, transform: (Int, R) -> V): List { - val size = Math.min(size(), array.size()) - val list = ArrayList(size) - for (i in 0..size-1) { - list.add(transform(this[i], array[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.merge(array: Array, transform: (Long, R) -> V): List { - val size = Math.min(size(), array.size()) - val list = ArrayList(size) - for (i in 0..size-1) { - list.add(transform(this[i], array[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.merge(array: Array, transform: (Short, R) -> V): List { - val size = Math.min(size(), array.size()) - val list = ArrayList(size) - for (i in 0..size-1) { - list.add(transform(this[i], array[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 Iterable.merge(array: Array, transform: (T, R) -> V): List { - val arraySize = array.size() - val list = ArrayList(Math.min(collectionSizeOrDefault(10), arraySize)) - var i = 0 - for (element in this) { - if (i >= arraySize) break - list.add(transform(element, array[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.merge(array: BooleanArray, transform: (Boolean, Boolean) -> V): List { - val size = Math.min(size(), array.size()) - val list = ArrayList(size) - for (i in 0..size-1) { - list.add(transform(this[i], array[i])) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. - */ -public inline fun ByteArray.merge(array: ByteArray, transform: (Byte, Byte) -> V): List { - val size = Math.min(size(), array.size()) - val list = ArrayList(size) - for (i in 0..size-1) { - list.add(transform(this[i], array[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.merge(array: CharArray, transform: (Char, Char) -> V): List { - val size = Math.min(size(), array.size()) - val list = ArrayList(size) - for (i in 0..size-1) { - list.add(transform(this[i], array[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.merge(array: DoubleArray, transform: (Double, Double) -> V): List { - val size = Math.min(size(), array.size()) - val list = ArrayList(size) - for (i in 0..size-1) { - list.add(transform(this[i], array[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.merge(array: FloatArray, transform: (Float, Float) -> V): List { - val size = Math.min(size(), array.size()) - val list = ArrayList(size) - for (i in 0..size-1) { - list.add(transform(this[i], array[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.merge(array: IntArray, transform: (Int, Int) -> V): List { - val size = Math.min(size(), array.size()) - val list = ArrayList(size) - for (i in 0..size-1) { - list.add(transform(this[i], array[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.merge(array: LongArray, transform: (Long, Long) -> V): List { - val size = Math.min(size(), array.size()) - val list = ArrayList(size) - for (i in 0..size-1) { - list.add(transform(this[i], array[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.merge(array: ShortArray, transform: (Short, Short) -> V): List { - val size = Math.min(size(), array.size()) - val list = ArrayList(size) - for (i in 0..size-1) { - list.add(transform(this[i], array[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 Array.merge(other: Iterable, transform: (T, R) -> V): List { - val arraySize = size() - val list = ArrayList(Math.min(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.merge(other: Iterable, transform: (Boolean, R) -> V): List { - val arraySize = size() - val list = ArrayList(Math.min(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.merge(other: Iterable, transform: (Byte, R) -> V): List { - val arraySize = size() - val list = ArrayList(Math.min(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.merge(other: Iterable, transform: (Char, R) -> V): List { - val arraySize = size() - val list = ArrayList(Math.min(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.merge(other: Iterable, transform: (Double, R) -> V): List { - val arraySize = size() - val list = ArrayList(Math.min(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.merge(other: Iterable, transform: (Float, R) -> V): List { - val arraySize = size() - val list = ArrayList(Math.min(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.merge(other: Iterable, transform: (Int, R) -> V): List { - val arraySize = size() - val list = ArrayList(Math.min(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.merge(other: Iterable, transform: (Long, R) -> V): List { - val arraySize = size() - val list = ArrayList(Math.min(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.merge(other: Iterable, transform: (Short, R) -> V): List { - val arraySize = size() - val list = ArrayList(Math.min(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 Iterable.merge(other: Iterable, transform: (T, R) -> V): List { - val first = iterator() - val second = other.iterator() - val list = ArrayList(Math.min(collectionSizeOrDefault(10), other.collectionSizeOrDefault(10))) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) - } - return list -} - -/** - * Returns a sequence of values built from elements of both collections with same indexes using provided [transform]. Resulting sequence has length of shortest input sequences. - */ -public fun Sequence.merge(sequence: Sequence, transform: (T, R) -> V): Sequence { - return MergingSequence(this, sequence, transform) -} - -/** - * Returns a list containing all elements of the original collection except the elements contained in the given [array]. - */ -public operator fun Iterable.minus(array: Array): List { - if (array.isEmpty()) return this.toList() - val other = array.toHashSet() - return this.filterNot { it in other } -} - -/** - * Returns a sequence containing all elements of original sequence except the elements contained in the given [array]. - * Note that the source sequence and the array being subtracted are iterated only when an `iterator` is requested from - * the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. - */ -public operator fun Sequence.minus(array: Array): Sequence { - if (array.isEmpty()) return this - return object: Sequence { - override fun iterator(): Iterator { - val other = array.toHashSet() - return this@minus.filterNot { it in other }.iterator() - } - } -} - -/** - * Returns a set containing all elements of the original set except the elements contained in the given [array]. - */ -public operator fun Set.minus(array: Array): Set { - val result = LinkedHashSet(this) - result.removeAll(array) - return result -} - -/** - * Returns a list containing all elements of the original collection except the elements contained in the given [collection]. - */ -public operator fun Iterable.minus(collection: Iterable): List { - val other = collection.convertToSetForSetOperationWith(this) - if (other.isEmpty()) - return this.toList() - return this.filterNot { it in other } -} - -/** - * Returns a sequence containing all elements of original sequence except the elements contained in the given [collection]. - * Note that the source sequence and the collection being subtracted are iterated only when an `iterator` is requested from - * the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. - */ -public operator fun Sequence.minus(collection: Iterable): Sequence { - return object: Sequence { - override fun iterator(): Iterator { - val other = collection.convertToSetForSetOperation() - if (other.isEmpty()) - return this@minus.iterator() - else - return this@minus.filterNot { it in other }.iterator() - } - } -} - -/** - * Returns a set containing all elements of the original set except the elements contained in the given [collection]. - */ -public operator fun Set.minus(collection: Iterable): Set { - val other = collection.convertToSetForSetOperationWith(this) - if (other.isEmpty()) - return this.toSet() - if (other is Set) - return this.filterNotTo(LinkedHashSet()) { it in other } - val result = LinkedHashSet(this) - result.removeAll(other) - return result -} - -/** - * Returns a list containing all elements of the original collection without the first occurrence of the given [element]. - */ -public operator fun Iterable.minus(element: T): List { - val result = ArrayList(collectionSizeOrDefault(10)) - var removed = false - return this.filterTo(result) { if (!removed && it == element) { removed = true; false } else true } -} - -/** - * Returns a sequence containing all elements of the original sequence without the first occurrence of the given [element]. - */ -public operator fun Sequence.minus(element: T): Sequence { - return object: Sequence { - override fun iterator(): Iterator { - var removed = false - return this@minus.filter { if (!removed && it == element) { removed = true; false } else true }.iterator() - } - } -} - -/** - * Returns a set containing all elements of the original set except the given [element]. - */ -public operator fun Set.minus(element: T): Set { - val result = LinkedHashSet(mapCapacity(size())) - var removed = false - return this.filterTo(result) { if (!removed && it == element) { removed = true; false } else true } -} - -/** - * Returns a list containing all elements of the original collection except the elements contained in the given [sequence]. - */ -public operator fun Iterable.minus(sequence: Sequence): List { - val other = sequence.toHashSet() - if (other.isEmpty()) - return this.toList() - return this.filterNot { it in other } -} - -/** - * Returns a sequence containing all elements of original sequence except the elements contained in the given [sequence]. - * Note that the source sequence and the sequence being subtracted are iterated only when an `iterator` is requested from - * the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. - */ -public operator fun Sequence.minus(sequence: Sequence): Sequence { - return object: Sequence { - override fun iterator(): Iterator { - val other = sequence.toHashSet() - if (other.isEmpty()) - return this@minus.iterator() - else - return this@minus.filterNot { it in other }.iterator() - } - } -} - -/** - * Returns a set containing all elements of the original set except the elements contained in the given [sequence]. - */ -public operator fun Set.minus(sequence: Sequence): Set { - val result = LinkedHashSet(this) - result.removeAll(sequence) - return result -} - -/** - * Splits the original collection into pair of collections, - * where *first* collection contains elements for which [predicate] yielded `true`, - * while *second* collection 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 collection into pair of collections, - * where *first* collection contains elements for which [predicate] yielded `true`, - * while *second* collection 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 collection into pair of collections, - * where *first* collection contains elements for which [predicate] yielded `true`, - * while *second* collection 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 collection into pair of collections, - * where *first* collection contains elements for which [predicate] yielded `true`, - * while *second* collection 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) -} - -/** - * Splits the original collection into pair of collections, - * where *first* collection contains elements for which [predicate] yielded `true`, - * while *second* collection 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 collection into pair of collections, - * where *first* collection contains elements for which [predicate] yielded `true`, - * while *second* collection 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 collection into pair of collections, - * where *first* collection contains elements for which [predicate] yielded `true`, - * while *second* collection 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 collection into pair of collections, - * where *first* collection contains elements for which [predicate] yielded `true`, - * while *second* collection 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 collection into pair of collections, - * where *first* collection contains elements for which [predicate] yielded `true`, - * while *second* collection 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 collection into pair of collections, - * where *first* collection contains elements for which [predicate] yielded `true`, - * while *second* collection contains elements for which [predicate] yielded `false`. - */ -public inline fun Iterable.partition(predicate: (T) -> Boolean): Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Splits the original collection into pair of collections, - * where *first* collection contains elements for which [predicate] yielded `true`, - * while *second* collection contains elements for which [predicate] yielded `false`. - */ -public inline fun Sequence.partition(predicate: (T) -> Boolean): Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Splits the original string into pair of strings, - * where *first* string contains characters for which [predicate] yielded `true`, - * while *second* string contains characters for which [predicate] yielded `false`. - */ -public inline fun String.partition(predicate: (Char) -> Boolean): Pair { - val first = StringBuilder() - val second = StringBuilder() - for (element in this) { - if (predicate(element)) { - first.append(element) - } else { - second.append(element) - } - } - return Pair(first.toString(), second.toString()) -} - -/** - * Returns a list containing all elements of the original collection and then all elements of the given [array]. - */ -public operator fun Collection.plus(array: Array): List { - val result = ArrayList(this.size() + array.size()) - result.addAll(this) - result.addAll(array) - return result -} - -/** - * Returns a list containing all elements of the original collection and then all elements of the given [array]. - */ -public operator fun Iterable.plus(array: Array): List { - if (this is Collection) return this.plus(array) - val result = ArrayList() - result.addAll(this) - result.addAll(array) - return result -} - -/** - * Returns a sequence containing all elements of original sequence and then all elements of the given [array]. - * Note that the source sequence and the array being added are iterated only when an `iterator` is requested from - * the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. - */ -public operator fun Sequence.plus(array: Array): Sequence { - return this.plus(array.asList()) -} - -/** - * Returns a set containing all elements both of the original set and the given [array]. - */ -public operator fun Set.plus(array: Array): Set { - val result = LinkedHashSet(mapCapacity(this.size() + array.size())) - result.addAll(this) - result.addAll(array) - return result -} - -/** - * Returns a list containing all elements of the original collection and then all elements of the given [collection]. - */ -public operator fun Collection.plus(collection: Iterable): List { - if (collection is Collection) { - val result = ArrayList(this.size() + collection.size()) - result.addAll(this) - result.addAll(collection) - return result - } else { - val result = ArrayList(this) - result.addAll(collection) - return result - } -} - -/** - * Returns a list containing all elements of the original collection and then all elements of the given [collection]. - */ -public operator fun Iterable.plus(collection: Iterable): List { - if (this is Collection) return this.plus(collection) - val result = ArrayList() - result.addAll(this) - result.addAll(collection) - return result -} - -/** - * Returns a sequence containing all elements of original sequence and then all elements of the given [collection]. - * Note that the source sequence and the collection being added are iterated only when an `iterator` is requested from - * the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. - */ -public operator fun Sequence.plus(collection: Iterable): Sequence { - return sequenceOf(this, collection.asSequence()).flatten() -} - -/** - * Returns a set containing all elements both of the original set and the given [collection]. - */ -public operator fun Set.plus(collection: Iterable): Set { - val result = LinkedHashSet(mapCapacity(collection.collectionSizeOrNull()?.let { this.size() + it } ?: this.size() * 2)) - result.addAll(this) - result.addAll(collection) - return result -} - -/** - * Returns a list containing all elements of the original collection and then the given [element]. - */ -public operator fun Collection.plus(element: T): List { - val result = ArrayList(size() + 1) - result.addAll(this) - result.add(element) - return result -} - -/** - * Returns a list containing all elements of the original collection and then the given [element]. - */ -public operator fun Iterable.plus(element: T): List { - if (this is Collection) return this.plus(element) - val result = ArrayList() - result.addAll(this) - result.add(element) - return result -} - -/** - * Returns a sequence containing all elements of the original sequence and then the given [element]. - */ -public operator fun Sequence.plus(element: T): Sequence { - return sequenceOf(this, sequenceOf(element)).flatten() -} - -/** - * Returns a set containing all elements of the original set and then the given [element]. - */ -public operator fun Set.plus(element: T): Set { - val result = LinkedHashSet(mapCapacity(size() + 1)) - result.addAll(this) - result.add(element) - return result -} - -/** - * Returns a list containing all elements of the original collection and then all elements of the given [sequence]. - */ -public operator fun Collection.plus(sequence: Sequence): List { - val result = ArrayList(this.size() + 10) - result.addAll(this) - result.addAll(sequence) - return result -} - -/** - * Returns a list containing all elements of the original collection and then all elements of the given [sequence]. - */ -public operator fun Iterable.plus(sequence: Sequence): List { - val result = ArrayList() - result.addAll(this) - result.addAll(sequence) - return result -} - -/** - * Returns a sequence containing all elements of original sequence and then all elements of the given [sequence]. - * Note that the source sequence and the sequence being added are iterated only when an `iterator` is requested from - * the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. - */ -public operator fun Sequence.plus(sequence: Sequence): Sequence { - return sequenceOf(this, sequence).flatten() -} - -/** - * Returns a set containing all elements both of the original set and the given [sequence]. - */ -public operator fun Set.plus(sequence: Sequence): Set { - val result = LinkedHashSet(mapCapacity(this.size() * 2)) - result.addAll(this) - result.addAll(sequence) - return result -} - -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public fun Array.zip(array: Array): List> { - return merge(array) { 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 fun BooleanArray.zip(array: Array): List> { - return merge(array) { 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 fun ByteArray.zip(array: Array): List> { - return merge(array) { 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 fun CharArray.zip(array: Array): List> { - return merge(array) { 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 fun DoubleArray.zip(array: Array): List> { - return merge(array) { 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 fun FloatArray.zip(array: Array): List> { - return merge(array) { 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 fun IntArray.zip(array: Array): List> { - return merge(array) { 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 fun LongArray.zip(array: Array): List> { - return merge(array) { 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 fun ShortArray.zip(array: Array): List> { - return merge(array) { 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 fun Iterable.zip(array: Array): List> { - return merge(array) { 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 fun BooleanArray.zip(array: BooleanArray): List> { - return merge(array) { 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 fun ByteArray.zip(array: ByteArray): List> { - return merge(array) { 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 fun CharArray.zip(array: CharArray): List> { - return merge(array) { 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 fun DoubleArray.zip(array: DoubleArray): List> { - return merge(array) { 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 fun FloatArray.zip(array: FloatArray): List> { - return merge(array) { 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 fun IntArray.zip(array: IntArray): List> { - return merge(array) { 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 fun LongArray.zip(array: LongArray): List> { - return merge(array) { 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 fun ShortArray.zip(array: ShortArray): List> { - return merge(array) { 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 fun Array.zip(other: Iterable): List> { - return merge(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 fun BooleanArray.zip(other: Iterable): List> { - return merge(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 fun ByteArray.zip(other: Iterable): List> { - return merge(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 fun CharArray.zip(other: Iterable): List> { - return merge(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 fun DoubleArray.zip(other: Iterable): List> { - return merge(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 fun FloatArray.zip(other: Iterable): List> { - return merge(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 fun IntArray.zip(other: Iterable): List> { - return merge(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 fun LongArray.zip(other: Iterable): List> { - return merge(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 fun ShortArray.zip(other: Iterable): List> { - return merge(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 fun Iterable.zip(other: Iterable): List> { - return merge(other) { t1, t2 -> t1 to t2 } -} - -/** - * Returns a list of pairs built from characters of both strings with same indexes. List has length of shortest collection. - */ -public fun String.zip(other: String): List> { - val first = iterator() - val second = other.iterator() - val list = ArrayList>(length()) - while (first.hasNext() && second.hasNext()) { - list.add(first.next() to second.next()) - } - return list -} - -/** - * Returns a sequence of pairs built from elements of both collections with same indexes. - * Resulting sequence has length of shortest input sequences. - */ -public fun Sequence.zip(sequence: Sequence): Sequence> { - return MergingSequence(this, sequence) { t1, t2 -> t1 to t2 } -} - diff --git a/libraries/stdlib/src/generated/_Guards.kt b/libraries/stdlib/src/generated/_Guards.kt deleted file mode 100644 index 5c6af1b4d6b..00000000000 --- a/libraries/stdlib/src/generated/_Guards.kt +++ /dev/null @@ -1,55 +0,0 @@ -package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// - -import kotlin.platform.* -import java.util.* - -import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js - -/** - * 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.") - } - } - return this as Array -} - -/** - * Returns an original collection containing all the non-`null` elements, throwing an [IllegalArgumentException] if there are any `null` elements. - */ -public fun Iterable.requireNoNulls(): Iterable { - for (element in this) { - if (element == null) { - throw IllegalArgumentException("null element found in $this.") - } - } - return this as Iterable -} - -/** - * Returns an original collection containing all the non-`null` elements, throwing an [IllegalArgumentException] if there are any `null` elements. - */ -public fun List.requireNoNulls(): List { - for (element in this) { - if (element == null) { - throw IllegalArgumentException("null element found in $this.") - } - } - return this as List -} - -/** - * Returns an original collection containing all the non-`null` elements, throwing an [IllegalArgumentException] if there are any `null` elements. - */ -public fun Sequence.requireNoNulls(): Sequence { - return map { it ?: throw IllegalArgumentException("null element found in $this.") } -} - diff --git a/libraries/stdlib/src/generated/_Mapping.kt b/libraries/stdlib/src/generated/_Mapping.kt deleted file mode 100644 index a7a2395cdac..00000000000 --- a/libraries/stdlib/src/generated/_Mapping.kt +++ /dev/null @@ -1,1174 +0,0 @@ -package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// - -import kotlin.platform.* -import java.util.* - -import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js - -/** - * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original collection. - */ -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 collection. - */ -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 collection. - */ -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 collection. - */ -public inline fun CharArray.flatMap(transform: (Char) -> 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 collection. - */ -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 collection. - */ -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 collection. - */ -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 collection. - */ -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 collection. - */ -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 collection. - */ -public inline fun Iterable.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 collection. - */ -public inline fun Map.flatMap(transform: (Map.Entry) -> 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 collection. - */ -public inline fun String.flatMap(transform: (Char) -> Iterable): List { - return flatMapTo(ArrayList(), transform) -} - -/** - * Returns a single sequence of all elements from results of [transform] function being invoked on each element of original sequence. - */ -public fun Sequence.flatMap(transform: (T) -> Sequence): Sequence { - return FlatteningSequence(this, transform) -} - -/** - * Appends all elements yielded from results of [transform] function being invoked on each element of original collection, to the given [destination]. - */ -public inline fun > 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 collection, 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 collection, 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 collection, 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 -} - -/** - * Appends all elements yielded from results of [transform] function being invoked on each element of original collection, 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 collection, 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 collection, 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 collection, 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 collection, 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 collection, to the given [destination]. - */ -public inline fun > Iterable.flatMapTo(destination: C, transform: (T) -> Iterable): C { - for (element in this) { - val list = transform(element) - destination.addAll(list) - } - return destination -} - -/** - * Appends all elements yielded from results of [transform] function being invoked on each element of original collection, to the given [destination]. - */ -public inline fun > Map.flatMapTo(destination: C, transform: (Map.Entry) -> Iterable): C { - for (element in this) { - val list = transform(element) - destination.addAll(list) - } - return destination -} - -/** - * Appends all elements yielded from results of [transform] function being invoked on each element of original collection, to the given [destination]. - */ -public inline fun > String.flatMapTo(destination: C, transform: (Char) -> 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 sequence, to the given [destination]. - */ -public inline fun > Sequence.flatMapTo(destination: C, transform: (T) -> Sequence): C { - for (element in this) { - val list = transform(element) - destination.addAll(list) - } - return destination -} - -/** - * Returns a map of the elements in original collection grouped by the result of given [toKey] function. - */ -public inline fun Array.groupBy(toKey: (T) -> K): Map> { - return groupByTo(LinkedHashMap>(), toKey) -} - -/** - * Returns a map of the elements in original collection grouped by the result of given [toKey] function. - */ -public inline fun BooleanArray.groupBy(toKey: (Boolean) -> K): Map> { - return groupByTo(LinkedHashMap>(), toKey) -} - -/** - * Returns a map of the elements in original collection grouped by the result of given [toKey] function. - */ -public inline fun ByteArray.groupBy(toKey: (Byte) -> K): Map> { - return groupByTo(LinkedHashMap>(), toKey) -} - -/** - * Returns a map of the elements in original collection grouped by the result of given [toKey] function. - */ -public inline fun CharArray.groupBy(toKey: (Char) -> K): Map> { - return groupByTo(LinkedHashMap>(), toKey) -} - -/** - * Returns a map of the elements in original collection grouped by the result of given [toKey] function. - */ -public inline fun DoubleArray.groupBy(toKey: (Double) -> K): Map> { - return groupByTo(LinkedHashMap>(), toKey) -} - -/** - * Returns a map of the elements in original collection grouped by the result of given [toKey] function. - */ -public inline fun FloatArray.groupBy(toKey: (Float) -> K): Map> { - return groupByTo(LinkedHashMap>(), toKey) -} - -/** - * Returns a map of the elements in original collection grouped by the result of given [toKey] function. - */ -public inline fun IntArray.groupBy(toKey: (Int) -> K): Map> { - return groupByTo(LinkedHashMap>(), toKey) -} - -/** - * Returns a map of the elements in original collection grouped by the result of given [toKey] function. - */ -public inline fun LongArray.groupBy(toKey: (Long) -> K): Map> { - return groupByTo(LinkedHashMap>(), toKey) -} - -/** - * Returns a map of the elements in original collection grouped by the result of given [toKey] function. - */ -public inline fun ShortArray.groupBy(toKey: (Short) -> K): Map> { - return groupByTo(LinkedHashMap>(), toKey) -} - -/** - * Returns a map of the elements in original collection grouped by the result of given [toKey] function. - */ -public inline fun Iterable.groupBy(toKey: (T) -> K): Map> { - return groupByTo(LinkedHashMap>(), toKey) -} - -/** - * Returns a map of the elements in original collection grouped by the result of given [toKey] function. - */ -public inline fun Sequence.groupBy(toKey: (T) -> K): Map> { - return groupByTo(LinkedHashMap>(), toKey) -} - -/** - * Returns a map of the elements in original collection grouped by the result of given [toKey] function. - */ -public inline fun String.groupBy(toKey: (Char) -> K): Map> { - return groupByTo(LinkedHashMap>(), toKey) -} - -/** - * Appends elements from original collection grouped by the result of given [toKey] function to the given [map]. - */ -public inline fun Array.groupByTo(map: MutableMap>, toKey: (T) -> K): Map> { - for (element in this) { - val key = toKey(element) - val list = map.getOrPut(key) { ArrayList() } - list.add(element) - } - return map -} - -/** - * Appends elements from original collection grouped by the result of given [toKey] function to the given [map]. - */ -public inline fun BooleanArray.groupByTo(map: MutableMap>, toKey: (Boolean) -> K): Map> { - for (element in this) { - val key = toKey(element) - val list = map.getOrPut(key) { ArrayList() } - list.add(element) - } - return map -} - -/** - * Appends elements from original collection grouped by the result of given [toKey] function to the given [map]. - */ -public inline fun ByteArray.groupByTo(map: MutableMap>, toKey: (Byte) -> K): Map> { - for (element in this) { - val key = toKey(element) - val list = map.getOrPut(key) { ArrayList() } - list.add(element) - } - return map -} - -/** - * Appends elements from original collection grouped by the result of given [toKey] function to the given [map]. - */ -public inline fun CharArray.groupByTo(map: MutableMap>, toKey: (Char) -> K): Map> { - for (element in this) { - val key = toKey(element) - val list = map.getOrPut(key) { ArrayList() } - list.add(element) - } - return map -} - -/** - * Appends elements from original collection grouped by the result of given [toKey] function to the given [map]. - */ -public inline fun DoubleArray.groupByTo(map: MutableMap>, toKey: (Double) -> K): Map> { - for (element in this) { - val key = toKey(element) - val list = map.getOrPut(key) { ArrayList() } - list.add(element) - } - return map -} - -/** - * Appends elements from original collection grouped by the result of given [toKey] function to the given [map]. - */ -public inline fun FloatArray.groupByTo(map: MutableMap>, toKey: (Float) -> K): Map> { - for (element in this) { - val key = toKey(element) - val list = map.getOrPut(key) { ArrayList() } - list.add(element) - } - return map -} - -/** - * Appends elements from original collection grouped by the result of given [toKey] function to the given [map]. - */ -public inline fun IntArray.groupByTo(map: MutableMap>, toKey: (Int) -> K): Map> { - for (element in this) { - val key = toKey(element) - val list = map.getOrPut(key) { ArrayList() } - list.add(element) - } - return map -} - -/** - * Appends elements from original collection grouped by the result of given [toKey] function to the given [map]. - */ -public inline fun LongArray.groupByTo(map: MutableMap>, toKey: (Long) -> K): Map> { - for (element in this) { - val key = toKey(element) - val list = map.getOrPut(key) { ArrayList() } - list.add(element) - } - return map -} - -/** - * Appends elements from original collection grouped by the result of given [toKey] function to the given [map]. - */ -public inline fun ShortArray.groupByTo(map: MutableMap>, toKey: (Short) -> K): Map> { - for (element in this) { - val key = toKey(element) - val list = map.getOrPut(key) { ArrayList() } - list.add(element) - } - return map -} - -/** - * Appends elements from original collection grouped by the result of given [toKey] function to the given [map]. - */ -public inline fun Iterable.groupByTo(map: MutableMap>, toKey: (T) -> K): Map> { - for (element in this) { - val key = toKey(element) - val list = map.getOrPut(key) { ArrayList() } - list.add(element) - } - return map -} - -/** - * Appends elements from original collection grouped by the result of given [toKey] function to the given [map]. - */ -public inline fun Sequence.groupByTo(map: MutableMap>, toKey: (T) -> K): Map> { - for (element in this) { - val key = toKey(element) - val list = map.getOrPut(key) { ArrayList() } - list.add(element) - } - return map -} - -/** - * Appends elements from original collection grouped by the result of given [toKey] function to the given [map]. - */ -public inline fun String.groupByTo(map: MutableMap>, toKey: (Char) -> K): Map> { - for (element in this) { - val key = toKey(element) - val list = map.getOrPut(key) { ArrayList() } - list.add(element) - } - return map -} - -/** - * Returns a list containing the results of applying the given [transform] function to each element of the original collection. - */ -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 of the original collection. - */ -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 of the original collection. - */ -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 of the original collection. - */ -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 of the original collection. - */ -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 of the original collection. - */ -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 of the original collection. - */ -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 of the original collection. - */ -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 of the original collection. - */ -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 of the original collection. - */ -public inline fun Iterable.map(transform: (T) -> R): List { - return mapTo(ArrayList(collectionSizeOrDefault(10)), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function to each element of the original collection. - */ -public inline fun Map.map(transform: (Map.Entry) -> R): List { - return mapTo(ArrayList(size()), transform) -} - -/** - * Returns a sequence containing the results of applying the given [transform] function to each element of the original sequence. - */ -public fun Sequence.map(transform: (T) -> R): Sequence { - return TransformingSequence(this, transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function to each element of the original collection. - */ -public inline fun String.map(transform: (Char) -> R): List { - return mapTo(ArrayList(length()), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function to each element and its index of the original collection. - */ -public inline fun Array.mapIndexed(transform: (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 of the original collection. - */ -public inline fun BooleanArray.mapIndexed(transform: (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 of the original collection. - */ -public inline fun ByteArray.mapIndexed(transform: (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 of the original collection. - */ -public inline fun CharArray.mapIndexed(transform: (Int, Char) -> 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 of the original collection. - */ -public inline fun DoubleArray.mapIndexed(transform: (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 of the original collection. - */ -public inline fun FloatArray.mapIndexed(transform: (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 of the original collection. - */ -public inline fun IntArray.mapIndexed(transform: (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 of the original collection. - */ -public inline fun LongArray.mapIndexed(transform: (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 of the original collection. - */ -public inline fun ShortArray.mapIndexed(transform: (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 of the original collection. - */ -public inline fun Iterable.mapIndexed(transform: (Int, T) -> R): List { - return mapIndexedTo(ArrayList(collectionSizeOrDefault(10)), transform) -} - -/** - * Returns a sequence containing the results of applying the given [transform] function to each element and its index of the original sequence. - */ -public fun Sequence.mapIndexed(transform: (Int, T) -> R): Sequence { - return TransformingIndexedSequence(this, transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function to each element and its index of the original collection. - */ -public inline fun String.mapIndexed(transform: (Int, Char) -> R): List { - return mapIndexedTo(ArrayList(length()), transform) -} - -/** - * Appends transformed elements and their indices of the original collection using the given [transform] function - * to the given [destination]. - */ -public inline fun > Array.mapIndexedTo(destination: C, transform: (Int, T) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Appends transformed elements and their indices of the original collection using the given [transform] function - * to the given [destination]. - */ -public inline fun > BooleanArray.mapIndexedTo(destination: C, transform: (Int, Boolean) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Appends transformed elements and their indices of the original collection using the given [transform] function - * to the given [destination]. - */ -public inline fun > ByteArray.mapIndexedTo(destination: C, transform: (Int, Byte) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Appends transformed elements and their indices of the original collection using the given [transform] function - * to the given [destination]. - */ -public inline fun > CharArray.mapIndexedTo(destination: C, transform: (Int, Char) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Appends transformed elements and their indices of the original collection using the given [transform] function - * to the given [destination]. - */ -public inline fun > DoubleArray.mapIndexedTo(destination: C, transform: (Int, Double) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Appends transformed elements and their indices of the original collection using the given [transform] function - * to the given [destination]. - */ -public inline fun > FloatArray.mapIndexedTo(destination: C, transform: (Int, Float) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Appends transformed elements and their indices of the original collection using the given [transform] function - * to the given [destination]. - */ -public inline fun > IntArray.mapIndexedTo(destination: C, transform: (Int, Int) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Appends transformed elements and their indices of the original collection using the given [transform] function - * to the given [destination]. - */ -public inline fun > LongArray.mapIndexedTo(destination: C, transform: (Int, Long) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Appends transformed elements and their indices of the original collection using the given [transform] function - * to the given [destination]. - */ -public inline fun > ShortArray.mapIndexedTo(destination: C, transform: (Int, Short) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Appends transformed elements and their indices of the original collection using the given [transform] function - * to the given [destination]. - */ -public inline fun > Iterable.mapIndexedTo(destination: C, transform: (Int, T) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Appends transformed elements and their indices of the original collection using the given [transform] function - * to the given [destination]. - */ -public inline fun > Map.mapIndexedTo(destination: C, transform: (Int, Map.Entry) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Appends transformed elements and their indices of the original collection using the given [transform] function - * to the given [destination]. - */ -public inline fun > Sequence.mapIndexedTo(destination: C, transform: (Int, T) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Appends transformed elements and their indices of the original collection using the given [transform] function - * to the given [destination]. - */ -public inline fun > String.mapIndexedTo(destination: C, transform: (Int, Char) -> R): C { - var index = 0 - for (item in this) - destination.add(transform(index++, item)) - return destination -} - -/** - * Returns a list containing the results of applying the given [transform] function to each non-null element of the original collection. - */ -public inline fun Array.mapNotNull(transform: (T) -> R): List { - return mapNotNullTo(ArrayList(), transform) -} - -/** - * Returns a list containing the results of applying the given [transform] function to each non-null element of the original collection. - */ -public inline fun Iterable.mapNotNull(transform: (T) -> R): List { - return mapNotNullTo(ArrayList(), transform) -} - -/** - * Returns a sequence containing the results of applying the given [transform] function to each non-null element of the original sequence. - */ -public fun Sequence.mapNotNull(transform: (T) -> R): Sequence { - return TransformingSequence(FilteringSequence(this, false, { it == null }) as Sequence, transform) -} - -/** - * Appends transformed non-null elements of original collection using the given [transform] function - * to the given [destination]. - */ -public inline fun > Array.mapNotNullTo(destination: C, transform: (T) -> R): C { - for (element in this) { - if (element != null) { - destination.add(transform(element)) - } - } - return destination -} - -/** - * Appends transformed non-null elements of original collection using the given [transform] function - * to the given [destination]. - */ -public inline fun > Iterable.mapNotNullTo(destination: C, transform: (T) -> R): C { - for (element in this) { - if (element != null) { - destination.add(transform(element)) - } - } - return destination -} - -/** - * Appends transformed non-null elements of original collection using the given [transform] function - * to the given [destination]. - */ -public inline fun > Sequence.mapNotNullTo(destination: C, transform: (T) -> R): C { - for (element in this) { - if (element != null) { - destination.add(transform(element)) - } - } - return destination -} - -/** - * Appends transformed elements of the original collection using the given [transform] function - * 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 -} - -/** - * Appends transformed elements of the original collection using the given [transform] function - * 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 -} - -/** - * Appends transformed elements of the original collection using the given [transform] function - * 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 -} - -/** - * Appends transformed elements of the original collection using the given [transform] function - * 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 -} - -/** - * Appends transformed elements of the original collection using the given [transform] function - * 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 -} - -/** - * Appends transformed elements of the original collection using the given [transform] function - * 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 -} - -/** - * Appends transformed elements of the original collection using the given [transform] function - * 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 -} - -/** - * Appends transformed elements of the original collection using the given [transform] function - * 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 -} - -/** - * Appends transformed elements of the original collection using the given [transform] function - * 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 -} - -/** - * Appends transformed elements of the original collection using the given [transform] function - * to the given [destination]. - */ -public inline fun > Iterable.mapTo(destination: C, transform: (T) -> R): C { - for (item in this) - destination.add(transform(item)) - return destination -} - -/** - * Appends transformed elements of the original collection using the given [transform] function - * to the given [destination]. - */ -public inline fun > Map.mapTo(destination: C, transform: (Map.Entry) -> R): C { - for (item in this) - destination.add(transform(item)) - return destination -} - -/** - * Appends transformed elements of the original collection using the given [transform] function - * to the given [destination]. - */ -public inline fun > Sequence.mapTo(destination: C, transform: (T) -> R): C { - for (item in this) - destination.add(transform(item)) - return destination -} - -/** - * Appends transformed elements of the original collection using the given [transform] function - * to the given [destination]. - */ -public inline fun > String.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 collection. - */ -public fun Array.withIndex(): Iterable> { - return IndexingIterable { iterator() } -} - -/** - * Returns a lazy [Iterable] of [IndexedValue] for each element of the original collection. - */ -public fun BooleanArray.withIndex(): Iterable> { - return IndexingIterable { iterator() } -} - -/** - * Returns a lazy [Iterable] of [IndexedValue] for each element of the original collection. - */ -public fun ByteArray.withIndex(): Iterable> { - return IndexingIterable { iterator() } -} - -/** - * Returns a lazy [Iterable] of [IndexedValue] for each element of the original collection. - */ -public fun CharArray.withIndex(): Iterable> { - return IndexingIterable { iterator() } -} - -/** - * Returns a lazy [Iterable] of [IndexedValue] for each element of the original collection. - */ -public fun DoubleArray.withIndex(): Iterable> { - return IndexingIterable { iterator() } -} - -/** - * Returns a lazy [Iterable] of [IndexedValue] for each element of the original collection. - */ -public fun FloatArray.withIndex(): Iterable> { - return IndexingIterable { iterator() } -} - -/** - * Returns a lazy [Iterable] of [IndexedValue] for each element of the original collection. - */ -public fun IntArray.withIndex(): Iterable> { - return IndexingIterable { iterator() } -} - -/** - * Returns a lazy [Iterable] of [IndexedValue] for each element of the original collection. - */ -public fun LongArray.withIndex(): Iterable> { - return IndexingIterable { iterator() } -} - -/** - * Returns a lazy [Iterable] of [IndexedValue] for each element of the original collection. - */ -public fun ShortArray.withIndex(): Iterable> { - return IndexingIterable { iterator() } -} - -/** - * Returns a lazy [Iterable] of [IndexedValue] for each element of the original collection. - */ -public fun Iterable.withIndex(): Iterable> { - return IndexingIterable { iterator() } -} - -/** - * Returns a sequence of [IndexedValue] for each element of the original sequence. - */ -public fun Sequence.withIndex(): Sequence> { - return IndexingSequence(this) -} - -/** - * Returns a lazy [Iterable] of [IndexedValue] for each element of the original collection. - */ -public fun String.withIndex(): Iterable> { - return IndexingIterable { iterator() } -} - -/** - * Returns a list containing pairs of each element of the original collection and their index. - */ -@Deprecated("Use withIndex() instead.") -public fun Array.withIndices(): List> { - var index = 0 - return mapTo(ArrayList>(), { index++ to it }) -} - -/** - * Returns a list containing pairs of each element of the original collection and their index. - */ -@Deprecated("Use withIndex() instead.") -public fun BooleanArray.withIndices(): List> { - var index = 0 - return mapTo(ArrayList>(), { index++ to it }) -} - -/** - * Returns a list containing pairs of each element of the original collection and their index. - */ -@Deprecated("Use withIndex() instead.") -public fun ByteArray.withIndices(): List> { - var index = 0 - return mapTo(ArrayList>(), { index++ to it }) -} - -/** - * Returns a list containing pairs of each element of the original collection and their index. - */ -@Deprecated("Use withIndex() instead.") -public fun CharArray.withIndices(): List> { - var index = 0 - return mapTo(ArrayList>(), { index++ to it }) -} - -/** - * Returns a list containing pairs of each element of the original collection and their index. - */ -@Deprecated("Use withIndex() instead.") -public fun DoubleArray.withIndices(): List> { - var index = 0 - return mapTo(ArrayList>(), { index++ to it }) -} - -/** - * Returns a list containing pairs of each element of the original collection and their index. - */ -@Deprecated("Use withIndex() instead.") -public fun FloatArray.withIndices(): List> { - var index = 0 - return mapTo(ArrayList>(), { index++ to it }) -} - -/** - * Returns a list containing pairs of each element of the original collection and their index. - */ -@Deprecated("Use withIndex() instead.") -public fun IntArray.withIndices(): List> { - var index = 0 - return mapTo(ArrayList>(), { index++ to it }) -} - -/** - * Returns a list containing pairs of each element of the original collection and their index. - */ -@Deprecated("Use withIndex() instead.") -public fun LongArray.withIndices(): List> { - var index = 0 - return mapTo(ArrayList>(), { index++ to it }) -} - -/** - * Returns a list containing pairs of each element of the original collection and their index. - */ -@Deprecated("Use withIndex() instead.") -public fun ShortArray.withIndices(): List> { - var index = 0 - return mapTo(ArrayList>(), { index++ to it }) -} - -/** - * Returns a list containing pairs of each element of the original collection and their index. - */ -@Deprecated("Use withIndex() instead.") -public fun Iterable.withIndices(): List> { - var index = 0 - return mapTo(ArrayList>(), { index++ to it }) -} - -/** - * Returns a sequence containing pairs of each element of the original collection and their index. - */ -@Deprecated("Use withIndex() instead.") -public fun Sequence.withIndices(): Sequence> { - var index = 0 - return TransformingSequence(this, { index++ to it }) -} - -/** - * Returns a list containing pairs of each element of the original collection and their index. - */ -@Deprecated("Use withIndex() instead.") -public fun String.withIndices(): List> { - var index = 0 - return mapTo(ArrayList>(), { index++ to it }) -} - diff --git a/libraries/stdlib/src/generated/_Maps.kt b/libraries/stdlib/src/generated/_Maps.kt new file mode 100644 index 00000000000..78cb01a7a67 --- /dev/null +++ b/libraries/stdlib/src/generated/_Maps.kt @@ -0,0 +1,190 @@ +@file:kotlin.jvm.JvmMultifileClass +@file:kotlin.jvm.JvmName("MapsKt") + +package kotlin + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// + +import java.util.* + +import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js + +/** + * Returns a [List] containing all key-value pairs. + */ +public fun Map.toList(): List> { + val result = ArrayList>(size()) + for (item in this) + result.add(item.key to item.value) + return result +} + +/** + * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original collection. + */ +public inline fun Map.flatMap(transform: (Map.Entry) -> Iterable): List { + return flatMapTo(ArrayList(), transform) +} + +/** + * Appends all elements yielded from results of [transform] function being invoked on each element of original collection, to the given [destination]. + */ +public inline fun > Map.flatMapTo(destination: C, transform: (Map.Entry) -> Iterable): C { + for (element in this) { + val list = transform(element) + destination.addAll(list) + } + return destination +} + +/** + * Returns a list containing the results of applying the given [transform] function to each element of the original collection. + */ +public inline fun Map.map(transform: (Map.Entry) -> R): List { + return mapTo(ArrayList(size()), transform) +} + +/** + * Appends transformed elements and their indices of the original collection using the given [transform] function + * to the given [destination]. + */ +public inline fun > Map.mapIndexedTo(destination: C, transform: (Int, Map.Entry) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} + +/** + * Appends transformed elements of the original collection using the given [transform] function + * to the given [destination]. + */ +public inline fun > Map.mapTo(destination: C, transform: (Map.Entry) -> R): C { + for (item in this) + destination.add(transform(item)) + return destination +} + +/** + * Returns `true` if all elements match the given [predicate]. + */ +public inline fun Map.all(predicate: (Map.Entry) -> Boolean): Boolean { + for (element in this) if (!predicate(element)) return false + return true +} + +/** + * Returns `true` if collection has at least one element. + */ +public fun Map.any(): Boolean { + for (element in this) return true + return false +} + +/** + * Returns `true` if at least one element matches the given [predicate]. + */ +public inline fun Map.any(predicate: (Map.Entry) -> Boolean): Boolean { + for (element in this) if (predicate(element)) return true + return false +} + +/** + * Returns the number of elements in this collection. + */ +public fun Map.count(): Int { + return size() +} + +/** + * Returns the number of elements matching the given [predicate]. + */ +public inline fun Map.count(predicate: (Map.Entry) -> Boolean): Int { + var count = 0 + for (element in this) if (predicate(element)) count++ + return count +} + +/** + * Performs the given [operation] on each element. + */ +public inline fun Map.forEach(operation: (Map.Entry) -> Unit): Unit { + for (element in this) operation(element) +} + +/** + * Returns the first element yielding the largest value of the given function or `null` if there are no elements. + */ +public inline fun > Map.maxBy(f: (Map.Entry) -> R): Map.Entry? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var maxElem = iterator.next() + var maxValue = f(maxElem) + while (iterator.hasNext()) { + val e = iterator.next() + val v = f(e) + if (maxValue < v) { + maxElem = e + maxValue = v + } + } + return maxElem +} + +/** + * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. + */ +public inline fun > Map.minBy(f: (Map.Entry) -> R): Map.Entry? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var minElem = iterator.next() + var minValue = f(minElem) + while (iterator.hasNext()) { + val e = iterator.next() + val v = f(e) + if (minValue > v) { + minElem = e + minValue = v + } + } + return minElem +} + +/** + * Returns `true` if collection has no elements. + */ +public fun Map.none(): Boolean { + for (element in this) return false + return true +} + +/** + * Returns `true` if no elements match the given [predicate]. + */ +public inline fun Map.none(predicate: (Map.Entry) -> Boolean): Boolean { + for (element in this) if (predicate(element)) return false + return true +} + +/** + * Returns a sequence from the given collection. + */ +public fun Map.asSequence(): Sequence> { + return object : Sequence> { + override fun iterator(): Iterator> { + return this@asSequence.iterator() + } + } +} + +/** + * Returns a sequence from the given collection + */ +@Deprecated("Use asSequence() instead", ReplaceWith("asSequence()")) +public fun Map.sequence(): Sequence> { + return asSequence() +} + diff --git a/libraries/stdlib/src/generated/_Numeric.kt b/libraries/stdlib/src/generated/_Numeric.kt deleted file mode 100644 index ccc2447f7b9..00000000000 --- a/libraries/stdlib/src/generated/_Numeric.kt +++ /dev/null @@ -1,672 +0,0 @@ -package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// - -import kotlin.platform.* -import java.util.* - -import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js - -/** - * Returns an average value of elements in the collection. - */ -@kotlin.jvm.JvmName("averageOfByte") -public fun Array.average(): Double { - val iterator = iterator() - var sum: Double = 0.0 - var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - count += 1 - } - return if (count == 0) 0.0 else sum / count -} - -/** - * Returns an average value of elements in the collection. - */ -@kotlin.jvm.JvmName("averageOfDouble") -public fun Array.average(): Double { - val iterator = iterator() - var sum: Double = 0.0 - var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - count += 1 - } - return if (count == 0) 0.0 else sum / count -} - -/** - * Returns an average value of elements in the collection. - */ -@kotlin.jvm.JvmName("averageOfFloat") -public fun Array.average(): Double { - val iterator = iterator() - var sum: Double = 0.0 - var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - count += 1 - } - return if (count == 0) 0.0 else sum / count -} - -/** - * Returns an average value of elements in the collection. - */ -@kotlin.jvm.JvmName("averageOfInt") -public fun Array.average(): Double { - val iterator = iterator() - var sum: Double = 0.0 - var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - count += 1 - } - return if (count == 0) 0.0 else sum / count -} - -/** - * Returns an average value of elements in the collection. - */ -@kotlin.jvm.JvmName("averageOfLong") -public fun Array.average(): Double { - val iterator = iterator() - var sum: Double = 0.0 - var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - count += 1 - } - return if (count == 0) 0.0 else sum / count -} - -/** - * Returns an average value of elements in the collection. - */ -@kotlin.jvm.JvmName("averageOfShort") -public fun Array.average(): Double { - val iterator = iterator() - var sum: Double = 0.0 - var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - count += 1 - } - return if (count == 0) 0.0 else sum / count -} - -/** - * Returns an average value of elements in the collection. - */ -public fun ByteArray.average(): Double { - val iterator = iterator() - var sum: Double = 0.0 - var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - count += 1 - } - return if (count == 0) 0.0 else sum / count -} - -/** - * Returns an average value of elements in the collection. - */ -public fun DoubleArray.average(): Double { - val iterator = iterator() - var sum: Double = 0.0 - var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - count += 1 - } - return if (count == 0) 0.0 else sum / count -} - -/** - * Returns an average value of elements in the collection. - */ -public fun FloatArray.average(): Double { - val iterator = iterator() - var sum: Double = 0.0 - var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - count += 1 - } - return if (count == 0) 0.0 else sum / count -} - -/** - * Returns an average value of elements in the collection. - */ -public fun IntArray.average(): Double { - val iterator = iterator() - var sum: Double = 0.0 - var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - count += 1 - } - return if (count == 0) 0.0 else sum / count -} - -/** - * Returns an average value of elements in the collection. - */ -public fun LongArray.average(): Double { - val iterator = iterator() - var sum: Double = 0.0 - var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - count += 1 - } - return if (count == 0) 0.0 else sum / count -} - -/** - * Returns an average value of elements in the collection. - */ -public fun ShortArray.average(): Double { - val iterator = iterator() - var sum: Double = 0.0 - var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - count += 1 - } - return if (count == 0) 0.0 else sum / count -} - -/** - * Returns an average value of elements in the collection. - */ -@kotlin.jvm.JvmName("averageOfByte") -public fun Iterable.average(): Double { - val iterator = iterator() - var sum: Double = 0.0 - var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - count += 1 - } - return if (count == 0) 0.0 else sum / count -} - -/** - * Returns an average value of elements in the collection. - */ -@kotlin.jvm.JvmName("averageOfDouble") -public fun Iterable.average(): Double { - val iterator = iterator() - var sum: Double = 0.0 - var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - count += 1 - } - return if (count == 0) 0.0 else sum / count -} - -/** - * Returns an average value of elements in the collection. - */ -@kotlin.jvm.JvmName("averageOfFloat") -public fun Iterable.average(): Double { - val iterator = iterator() - var sum: Double = 0.0 - var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - count += 1 - } - return if (count == 0) 0.0 else sum / count -} - -/** - * Returns an average value of elements in the collection. - */ -@kotlin.jvm.JvmName("averageOfInt") -public fun Iterable.average(): Double { - val iterator = iterator() - var sum: Double = 0.0 - var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - count += 1 - } - return if (count == 0) 0.0 else sum / count -} - -/** - * Returns an average value of elements in the collection. - */ -@kotlin.jvm.JvmName("averageOfLong") -public fun Iterable.average(): Double { - val iterator = iterator() - var sum: Double = 0.0 - var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - count += 1 - } - return if (count == 0) 0.0 else sum / count -} - -/** - * Returns an average value of elements in the collection. - */ -@kotlin.jvm.JvmName("averageOfShort") -public fun Iterable.average(): Double { - val iterator = iterator() - var sum: Double = 0.0 - var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - count += 1 - } - return if (count == 0) 0.0 else sum / count -} - -/** - * Returns an average value of elements in the collection. - */ -@kotlin.jvm.JvmName("averageOfByte") -public fun Sequence.average(): Double { - val iterator = iterator() - var sum: Double = 0.0 - var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - count += 1 - } - return if (count == 0) 0.0 else sum / count -} - -/** - * Returns an average value of elements in the collection. - */ -@kotlin.jvm.JvmName("averageOfDouble") -public fun Sequence.average(): Double { - val iterator = iterator() - var sum: Double = 0.0 - var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - count += 1 - } - return if (count == 0) 0.0 else sum / count -} - -/** - * Returns an average value of elements in the collection. - */ -@kotlin.jvm.JvmName("averageOfFloat") -public fun Sequence.average(): Double { - val iterator = iterator() - var sum: Double = 0.0 - var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - count += 1 - } - return if (count == 0) 0.0 else sum / count -} - -/** - * Returns an average value of elements in the collection. - */ -@kotlin.jvm.JvmName("averageOfInt") -public fun Sequence.average(): Double { - val iterator = iterator() - var sum: Double = 0.0 - var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - count += 1 - } - return if (count == 0) 0.0 else sum / count -} - -/** - * Returns an average value of elements in the collection. - */ -@kotlin.jvm.JvmName("averageOfLong") -public fun Sequence.average(): Double { - val iterator = iterator() - var sum: Double = 0.0 - var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - count += 1 - } - return if (count == 0) 0.0 else sum / count -} - -/** - * Returns an average value of elements in the collection. - */ -@kotlin.jvm.JvmName("averageOfShort") -public fun Sequence.average(): Double { - val iterator = iterator() - var sum: Double = 0.0 - var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - count += 1 - } - return if (count == 0) 0.0 else sum / count -} - -/** - * Returns the sum of all elements in the collection. - */ -@kotlin.jvm.JvmName("sumOfByte") -public fun Array.sum(): Int { - val iterator = iterator() - var sum: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - } - return sum -} - -/** - * Returns the sum of all elements in the collection. - */ -@kotlin.jvm.JvmName("sumOfDouble") -public fun Array.sum(): Double { - val iterator = iterator() - var sum: Double = 0.0 - while (iterator.hasNext()) { - sum += iterator.next() - } - return sum -} - -/** - * Returns the sum of all elements in the collection. - */ -@kotlin.jvm.JvmName("sumOfFloat") -public fun Array.sum(): Float { - val iterator = iterator() - var sum: Float = 0.0f - while (iterator.hasNext()) { - sum += iterator.next() - } - return sum -} - -/** - * Returns the sum of all elements in the collection. - */ -@kotlin.jvm.JvmName("sumOfInt") -public fun Array.sum(): Int { - val iterator = iterator() - var sum: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - } - return sum -} - -/** - * Returns the sum of all elements in the collection. - */ -@kotlin.jvm.JvmName("sumOfLong") -public fun Array.sum(): Long { - val iterator = iterator() - var sum: Long = 0L - while (iterator.hasNext()) { - sum += iterator.next() - } - return sum -} - -/** - * Returns the sum of all elements in the collection. - */ -@kotlin.jvm.JvmName("sumOfShort") -public fun Array.sum(): Int { - val iterator = iterator() - var sum: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - } - return sum -} - -/** - * Returns the sum of all elements in the collection. - */ -public fun ByteArray.sum(): Int { - val iterator = iterator() - var sum: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - } - return sum -} - -/** - * Returns the sum of all elements in the collection. - */ -public fun DoubleArray.sum(): Double { - val iterator = iterator() - var sum: Double = 0.0 - while (iterator.hasNext()) { - sum += iterator.next() - } - return sum -} - -/** - * Returns the sum of all elements in the collection. - */ -public fun FloatArray.sum(): Float { - val iterator = iterator() - var sum: Float = 0.0f - while (iterator.hasNext()) { - sum += iterator.next() - } - return sum -} - -/** - * Returns the sum of all elements in the collection. - */ -public fun IntArray.sum(): Int { - val iterator = iterator() - var sum: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - } - return sum -} - -/** - * Returns the sum of all elements in the collection. - */ -public fun LongArray.sum(): Long { - val iterator = iterator() - var sum: Long = 0L - while (iterator.hasNext()) { - sum += iterator.next() - } - return sum -} - -/** - * Returns the sum of all elements in the collection. - */ -public fun ShortArray.sum(): Int { - val iterator = iterator() - var sum: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - } - return sum -} - -/** - * Returns the sum of all elements in the collection. - */ -@kotlin.jvm.JvmName("sumOfByte") -public fun Iterable.sum(): Int { - val iterator = iterator() - var sum: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - } - return sum -} - -/** - * Returns the sum of all elements in the collection. - */ -@kotlin.jvm.JvmName("sumOfDouble") -public fun Iterable.sum(): Double { - val iterator = iterator() - var sum: Double = 0.0 - while (iterator.hasNext()) { - sum += iterator.next() - } - return sum -} - -/** - * Returns the sum of all elements in the collection. - */ -@kotlin.jvm.JvmName("sumOfFloat") -public fun Iterable.sum(): Float { - val iterator = iterator() - var sum: Float = 0.0f - while (iterator.hasNext()) { - sum += iterator.next() - } - return sum -} - -/** - * Returns the sum of all elements in the collection. - */ -@kotlin.jvm.JvmName("sumOfInt") -public fun Iterable.sum(): Int { - val iterator = iterator() - var sum: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - } - return sum -} - -/** - * Returns the sum of all elements in the collection. - */ -@kotlin.jvm.JvmName("sumOfLong") -public fun Iterable.sum(): Long { - val iterator = iterator() - var sum: Long = 0L - while (iterator.hasNext()) { - sum += iterator.next() - } - return sum -} - -/** - * Returns the sum of all elements in the collection. - */ -@kotlin.jvm.JvmName("sumOfShort") -public fun Iterable.sum(): Int { - val iterator = iterator() - var sum: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - } - return sum -} - -/** - * Returns the sum of all elements in the collection. - */ -@kotlin.jvm.JvmName("sumOfByte") -public fun Sequence.sum(): Int { - val iterator = iterator() - var sum: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - } - return sum -} - -/** - * Returns the sum of all elements in the collection. - */ -@kotlin.jvm.JvmName("sumOfDouble") -public fun Sequence.sum(): Double { - val iterator = iterator() - var sum: Double = 0.0 - while (iterator.hasNext()) { - sum += iterator.next() - } - return sum -} - -/** - * Returns the sum of all elements in the collection. - */ -@kotlin.jvm.JvmName("sumOfFloat") -public fun Sequence.sum(): Float { - val iterator = iterator() - var sum: Float = 0.0f - while (iterator.hasNext()) { - sum += iterator.next() - } - return sum -} - -/** - * Returns the sum of all elements in the collection. - */ -@kotlin.jvm.JvmName("sumOfInt") -public fun Sequence.sum(): Int { - val iterator = iterator() - var sum: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - } - return sum -} - -/** - * Returns the sum of all elements in the collection. - */ -@kotlin.jvm.JvmName("sumOfLong") -public fun Sequence.sum(): Long { - val iterator = iterator() - var sum: Long = 0L - while (iterator.hasNext()) { - sum += iterator.next() - } - return sum -} - -/** - * Returns the sum of all elements in the collection. - */ -@kotlin.jvm.JvmName("sumOfShort") -public fun Sequence.sum(): Int { - val iterator = iterator() - var sum: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() - } - return sum -} - diff --git a/libraries/stdlib/src/generated/_Ordering.kt b/libraries/stdlib/src/generated/_Ordering.kt deleted file mode 100644 index 230df18d9f0..00000000000 --- a/libraries/stdlib/src/generated/_Ordering.kt +++ /dev/null @@ -1,1195 +0,0 @@ -package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// - -import kotlin.platform.* -import java.util.* - -import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js - -/** - * Returns a list with elements in reversed order. - */ -@Deprecated("reverse will change its behavior soon. Use reversed() instead.", ReplaceWith("reversed()")) -public fun Array.reverse(): List { - return reversed() -} - -/** - * Returns a list with elements in reversed order. - */ -@Deprecated("reverse will change its behavior soon. Use reversed() instead.", ReplaceWith("reversed()")) -public fun BooleanArray.reverse(): List { - return reversed() -} - -/** - * Returns a list with elements in reversed order. - */ -@Deprecated("reverse will change its behavior soon. Use reversed() instead.", ReplaceWith("reversed()")) -public fun ByteArray.reverse(): List { - return reversed() -} - -/** - * Returns a list with elements in reversed order. - */ -@Deprecated("reverse will change its behavior soon. Use reversed() instead.", ReplaceWith("reversed()")) -public fun CharArray.reverse(): List { - return reversed() -} - -/** - * Returns a list with elements in reversed order. - */ -@Deprecated("reverse will change its behavior soon. Use reversed() instead.", ReplaceWith("reversed()")) -public fun DoubleArray.reverse(): List { - return reversed() -} - -/** - * Returns a list with elements in reversed order. - */ -@Deprecated("reverse will change its behavior soon. Use reversed() instead.", ReplaceWith("reversed()")) -public fun FloatArray.reverse(): List { - return reversed() -} - -/** - * Returns a list with elements in reversed order. - */ -@Deprecated("reverse will change its behavior soon. Use reversed() instead.", ReplaceWith("reversed()")) -public fun IntArray.reverse(): List { - return reversed() -} - -/** - * Returns a list with elements in reversed order. - */ -@Deprecated("reverse will change its behavior soon. Use reversed() instead.", ReplaceWith("reversed()")) -public fun LongArray.reverse(): List { - return reversed() -} - -/** - * Returns a list with elements in reversed order. - */ -@Deprecated("reverse will change its behavior soon. Use reversed() instead.", ReplaceWith("reversed()")) -public fun ShortArray.reverse(): List { - return reversed() -} - -/** - * Returns a list with elements in reversed order. - */ -@Deprecated("reverse will change its behavior soon. Use reversed() instead.", ReplaceWith("reversed()")) -public fun Iterable.reverse(): List { - return reversed() -} - -/** - * Returns a list with elements in reversed order. - */ -@Deprecated("reverse will change its behavior soon. Use reversed() instead.", ReplaceWith("reversed()")) -public fun String.reverse(): String { - return reversed() -} - -/** - * Returns a list with elements in reversed order. - */ -public fun Array.reversed(): List { - if (isEmpty()) return emptyList() - val list = toArrayList() - Collections.reverse(list) - return list -} - -/** - * Returns a list with elements in reversed order. - */ -public fun BooleanArray.reversed(): List { - if (isEmpty()) return emptyList() - val list = toArrayList() - Collections.reverse(list) - return list -} - -/** - * Returns a list with elements in reversed order. - */ -public fun ByteArray.reversed(): List { - if (isEmpty()) return emptyList() - val list = toArrayList() - Collections.reverse(list) - return list -} - -/** - * Returns a list with elements in reversed order. - */ -public fun CharArray.reversed(): List { - if (isEmpty()) return emptyList() - val list = toArrayList() - Collections.reverse(list) - return list -} - -/** - * Returns a list with elements in reversed order. - */ -public fun DoubleArray.reversed(): List { - if (isEmpty()) return emptyList() - val list = toArrayList() - Collections.reverse(list) - return list -} - -/** - * Returns a list with elements in reversed order. - */ -public fun FloatArray.reversed(): List { - if (isEmpty()) return emptyList() - val list = toArrayList() - Collections.reverse(list) - return list -} - -/** - * Returns a list with elements in reversed order. - */ -public fun IntArray.reversed(): List { - if (isEmpty()) return emptyList() - val list = toArrayList() - Collections.reverse(list) - return list -} - -/** - * Returns a list with elements in reversed order. - */ -public fun LongArray.reversed(): List { - if (isEmpty()) return emptyList() - val list = toArrayList() - Collections.reverse(list) - return list -} - -/** - * Returns a list with elements in reversed order. - */ -public fun ShortArray.reversed(): List { - if (isEmpty()) return emptyList() - val list = toArrayList() - Collections.reverse(list) - return list -} - -/** - * Returns a list with elements in reversed order. - */ -public fun Iterable.reversed(): List { - if (this is Collection && isEmpty()) return emptyList() - val list = toArrayList() - Collections.reverse(list) - return list -} - -/** - * Returns a string with characters in reversed order. - */ -public fun String.reversed(): String { - return StringBuilder().append(this).reverse().toString() -} - -/** - * Returns an array with elements of this array in reversed order. - */ -public fun Array.reversedArray(): Array { - if (isEmpty()) return this - val result = arrayOfNulls(this, size()) as Array - 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 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 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 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 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 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 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 a sorted list of all elements. - */ -@Deprecated("This method may change its behavior soon. Use sorted() instead.", ReplaceWith("sorted()")) -public fun > Iterable.sort(): List { - val sortedList = toArrayList() - java.util.Collections.sort(sortedList) - return sortedList -} - -/** - * Returns a list of all elements, sorted by the specified [comparator]. - */ -@Deprecated("This method may change its behavior soon. Use sortedWith() instead.", ReplaceWith("sortedWith(comparator)")) -public fun Array.sortBy(comparator: Comparator): List { - val sortedList = toArrayList() - java.util.Collections.sort(sortedList, comparator) - return sortedList -} - -/** - * Returns a list of all elements, sorted by the specified [comparator]. - */ -@Deprecated("This method may change its behavior soon. Use sortedWith() instead.", ReplaceWith("sortedWith(comparator)")) -public fun Iterable.sortBy(comparator: Comparator): List { - val sortedList = toArrayList() - java.util.Collections.sort(sortedList, comparator) - return sortedList -} - -/** - * Returns a sorted list of all elements, ordered by results of specified [order] function. - */ -@Deprecated("This method may change its behavior soon. Use sortedBy() instead.", ReplaceWith("sortedBy(order)")) -public inline fun > Array.sortBy(crossinline order: (T) -> R): List { - val sortedList = toArrayList() - val sortBy: Comparator = compareBy(order) - java.util.Collections.sort(sortedList, sortBy) - return sortedList -} - -/** - * Returns a sorted list of all elements, ordered by results of specified [order] function. - */ -@Deprecated("This method may change its behavior soon. Use sortedBy() instead.", ReplaceWith("sortedBy(order)")) -public inline fun > Iterable.sortBy(crossinline order: (T) -> R): List { - val sortedList = toArrayList() - val sortBy: Comparator = compareBy(order) - java.util.Collections.sort(sortedList, sortBy) - return sortedList -} - -/** - * Returns a sorted list of all elements, in descending order. - */ -@Deprecated("This method may change its behavior soon. Use sortedDescending() instead.", ReplaceWith("sortedDescending()")) -public fun > Iterable.sortDescending(): List { - val sortedList = toArrayList() - java.util.Collections.sort(sortedList, comparator { x, y -> y.compareTo(x) }) - return sortedList -} - -/** - * Returns a sorted list of all elements, in descending order by results of specified [order] function. - */ -@Deprecated("This method may change its behavior soon. Use sortedByDescending() instead.", ReplaceWith("sortedByDescending(order)")) -public inline fun > Array.sortDescendingBy(crossinline order: (T) -> R): List { - val sortedList = toArrayList() - val sortBy: Comparator = compareByDescending(order) - java.util.Collections.sort(sortedList, sortBy) - return sortedList -} - -/** - * Returns a sorted list of all elements, in descending order by results of specified [order] function. - */ -@Deprecated("This method may change its behavior soon. Use sortedByDescending() instead.", ReplaceWith("sortedByDescending(order)")) -public inline fun > Iterable.sortDescendingBy(crossinline order: (T) -> R): List { - val sortedList = toArrayList() - val sortBy: Comparator = compareByDescending(order) - java.util.Collections.sort(sortedList, sortBy) - return sortedList -} - -/** - * Returns a list of all elements sorted according to their natural sort order. - */ -public fun > Array.sorted(): List { - val sortedList = toArrayList() - java.util.Collections.sort(sortedList) - return sortedList -} - -/** - * Returns a list of all elements sorted according to their natural sort order. - */ -public fun ByteArray.sorted(): List { - val sortedList = toArrayList() - java.util.Collections.sort(sortedList) - return sortedList -} - -/** - * Returns a list of all elements sorted according to their natural sort order. - */ -public fun CharArray.sorted(): List { - val sortedList = toArrayList() - java.util.Collections.sort(sortedList) - return sortedList -} - -/** - * Returns a list of all elements sorted according to their natural sort order. - */ -public fun DoubleArray.sorted(): List { - val sortedList = toArrayList() - java.util.Collections.sort(sortedList) - return sortedList -} - -/** - * Returns a list of all elements sorted according to their natural sort order. - */ -public fun FloatArray.sorted(): List { - val sortedList = toArrayList() - java.util.Collections.sort(sortedList) - return sortedList -} - -/** - * Returns a list of all elements sorted according to their natural sort order. - */ -public fun IntArray.sorted(): List { - val sortedList = toArrayList() - java.util.Collections.sort(sortedList) - return sortedList -} - -/** - * Returns a list of all elements sorted according to their natural sort order. - */ -public fun LongArray.sorted(): List { - val sortedList = toArrayList() - java.util.Collections.sort(sortedList) - return sortedList -} - -/** - * Returns a list of all elements sorted according to their natural sort order. - */ -public fun ShortArray.sorted(): List { - val sortedList = toArrayList() - java.util.Collections.sort(sortedList) - return sortedList -} - -/** - * Returns a list of all elements sorted according to their natural sort order. - */ -public fun > Iterable.sorted(): List { - val sortedList = toArrayList() - java.util.Collections.sort(sortedList) - return sortedList -} - -/** - * Returns a sequence that yields elements of this sequence sorted according to their natural sort order. - */ -public fun > Sequence.sorted(): Sequence { - return object : Sequence { - override fun iterator(): Iterator { - val sortedList = this@sorted.toArrayList() - java.util.Collections.sort(sortedList) - return sortedList.iterator() - } - } -} - -/** - * Returns an array with all elements of this array sorted according to their natural sort order. - */ -public fun > Array.sortedArray(): Array { - if (isEmpty()) return this - return this.copyOf().apply { sort() } -} - -/** - * Returns an array with all elements of this array sorted according to their natural sort order. - */ -public fun ByteArray.sortedArray(): ByteArray { - if (isEmpty()) return this - return this.copyOf().apply { sort() } -} - -/** - * Returns an array with all elements of this array sorted according to their natural sort order. - */ -public fun CharArray.sortedArray(): CharArray { - 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 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 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 ShortArray.sortedArray(): ShortArray { - if (isEmpty()) return this - return this.copyOf().apply { sort() } -} - -/** - * Returns an array with all elements of this array sorted descending according to their natural sort order. - */ -public fun > Array.sortedArrayDescending(): Array { - if (isEmpty()) return this - // TODO: Use reverseOrder() - return this.copyOf().apply { sortWith(comparator { a, b -> b.compareTo(a) }) } -} - -/** - * 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 - // TODO: Use in-place reverse - return this.copyOf().apply { sort() }.reversedArray() -} - -/** - * 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 - // TODO: Use in-place reverse - return this.copyOf().apply { sort() }.reversedArray() -} - -/** - * 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 - // TODO: Use in-place reverse - return this.copyOf().apply { sort() }.reversedArray() -} - -/** - * 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 - // TODO: Use in-place reverse - return this.copyOf().apply { sort() }.reversedArray() -} - -/** - * 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 - // TODO: Use in-place reverse - return this.copyOf().apply { sort() }.reversedArray() -} - -/** - * 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 - // TODO: Use in-place reverse - return this.copyOf().apply { sort() }.reversedArray() -} - -/** - * 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 - // TODO: Use in-place reverse - return this.copyOf().apply { sort() }.reversedArray() -} - -/** - * Returns an array with all elements of this array sorted according the specified [comparator]. - */ -public fun Array.sortedArrayWith(comparator: Comparator): Array { - if (isEmpty()) return this - return this.copyOf().apply { sortWith(comparator) } -} - -/** - * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > Array.sortedBy(crossinline selector: (T) -> R?): List { - return sortedWith(compareBy(selector)) -} - -/** - * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > 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 > 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 > CharArray.sortedBy(crossinline selector: (Char) -> 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 > 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 > 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 > 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 > Iterable.sortedBy(crossinline selector: (T) -> R?): List { - return sortedWith(compareBy(selector)) -} - -/** - * Returns a sequence that yields elements of this sequence sorted according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > Sequence.sortedBy(crossinline selector: (T) -> R?): Sequence { - return sortedWith(compareBy(selector)) -} - -/** - * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > Array.sortedByDescending(crossinline selector: (T) -> R?): List { - return sortedWith(compareByDescending(selector)) -} - -/** - * Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > 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 > 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 > CharArray.sortedByDescending(crossinline selector: (Char) -> 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 > 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 > 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 > 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 > Iterable.sortedByDescending(crossinline selector: (T) -> R?): List { - return sortedWith(compareByDescending(selector)) -} - -/** - * Returns a sequence that yields elements of this sequence sorted descending according to natural sort order of the value returned by specified [selector] function. - */ -public inline fun > Sequence.sortedByDescending(crossinline selector: (T) -> R?): Sequence { - return sortedWith(compareByDescending(selector)) -} - -/** - * Returns a list of all elements sorted descending according to their natural sort order. - */ -public fun > Array.sortedDescending(): List { - return sortedWith(comparator { x, y -> y.compareTo(x) }) -} - -/** - * 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 CharArray.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 FloatArray.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 ShortArray.sortedDescending(): List { - return copyOf().apply { sort() }.reversed() -} - -/** - * Returns a list of all elements sorted descending according to their natural sort order. - */ -public fun > Iterable.sortedDescending(): List { - return sortedWith(comparator { x, y -> y.compareTo(x) }) -} - -/** - * Returns a sequence that yields elements of this sequence sorted descending according to their natural sort order. - */ -public fun > Sequence.sortedDescending(): Sequence { - return sortedWith(comparator { x, y -> y.compareTo(x) }) -} - -/** - * Returns a list of all elements sorted according to the specified [comparator]. - */ -public fun Array.sortedWith(comparator: Comparator): List { - val sortedList = toArrayList() - java.util.Collections.sort(sortedList, comparator) - return sortedList -} - -/** - * Returns a list of all elements sorted according to the specified [comparator]. - */ -public fun BooleanArray.sortedWith(comparator: Comparator): List { - val sortedList = toArrayList() - java.util.Collections.sort(sortedList, comparator) - return sortedList -} - -/** - * Returns a list of all elements sorted according to the specified [comparator]. - */ -public fun ByteArray.sortedWith(comparator: Comparator): List { - val sortedList = toArrayList() - java.util.Collections.sort(sortedList, comparator) - return sortedList -} - -/** - * Returns a list of all elements sorted according to the specified [comparator]. - */ -public fun CharArray.sortedWith(comparator: Comparator): List { - val sortedList = toArrayList() - java.util.Collections.sort(sortedList, comparator) - return sortedList -} - -/** - * Returns a list of all elements sorted according to the specified [comparator]. - */ -public fun DoubleArray.sortedWith(comparator: Comparator): List { - val sortedList = toArrayList() - java.util.Collections.sort(sortedList, comparator) - return sortedList -} - -/** - * Returns a list of all elements sorted according to the specified [comparator]. - */ -public fun FloatArray.sortedWith(comparator: Comparator): List { - val sortedList = toArrayList() - java.util.Collections.sort(sortedList, comparator) - return sortedList -} - -/** - * Returns a list of all elements sorted according to the specified [comparator]. - */ -public fun IntArray.sortedWith(comparator: Comparator): List { - val sortedList = toArrayList() - java.util.Collections.sort(sortedList, comparator) - return sortedList -} - -/** - * Returns a list of all elements sorted according to the specified [comparator]. - */ -public fun LongArray.sortedWith(comparator: Comparator): List { - val sortedList = toArrayList() - java.util.Collections.sort(sortedList, comparator) - return sortedList -} - -/** - * Returns a list of all elements sorted according to the specified [comparator]. - */ -public fun ShortArray.sortedWith(comparator: Comparator): List { - val sortedList = toArrayList() - java.util.Collections.sort(sortedList, comparator) - return sortedList -} - -/** - * Returns a list of all elements sorted according to the specified [comparator]. - */ -public fun Iterable.sortedWith(comparator: Comparator): List { - val sortedList = toArrayList() - java.util.Collections.sort(sortedList, comparator) - return sortedList -} - -/** - * Returns a sequence that yields elements of this sequence sorted according to the specified [comparator]. - */ -public fun Sequence.sortedWith(comparator: Comparator): Sequence { - return object : Sequence { - override fun iterator(): Iterator { - val sortedList = this@sortedWith.toArrayList() - java.util.Collections.sort(sortedList, comparator) - return sortedList.iterator() - } - } -} - -/** - * Returns a sorted list of all elements. - */ -@Deprecated("Use sorted() instead.", ReplaceWith("sorted()")) -public fun > Array.toSortedList(): List { - val sortedList = toArrayList() - java.util.Collections.sort(sortedList) - return sortedList -} - -/** - * Returns a sorted list of all elements. - */ -@Deprecated("Use sorted() instead.", ReplaceWith("sorted()")) -public fun BooleanArray.toSortedList(): List { - val sortedList = toArrayList() - java.util.Collections.sort(sortedList) - return sortedList -} - -/** - * Returns a sorted list of all elements. - */ -@Deprecated("Use sorted() instead.", ReplaceWith("sorted()")) -public fun ByteArray.toSortedList(): List { - val sortedList = toArrayList() - java.util.Collections.sort(sortedList) - return sortedList -} - -/** - * Returns a sorted list of all elements. - */ -@Deprecated("Use sorted() instead.", ReplaceWith("sorted()")) -public fun CharArray.toSortedList(): List { - val sortedList = toArrayList() - java.util.Collections.sort(sortedList) - return sortedList -} - -/** - * Returns a sorted list of all elements. - */ -@Deprecated("Use sorted() instead.", ReplaceWith("sorted()")) -public fun DoubleArray.toSortedList(): List { - val sortedList = toArrayList() - java.util.Collections.sort(sortedList) - return sortedList -} - -/** - * Returns a sorted list of all elements. - */ -@Deprecated("Use sorted() instead.", ReplaceWith("sorted()")) -public fun FloatArray.toSortedList(): List { - val sortedList = toArrayList() - java.util.Collections.sort(sortedList) - return sortedList -} - -/** - * Returns a sorted list of all elements. - */ -@Deprecated("Use sorted() instead.", ReplaceWith("sorted()")) -public fun IntArray.toSortedList(): List { - val sortedList = toArrayList() - java.util.Collections.sort(sortedList) - return sortedList -} - -/** - * Returns a sorted list of all elements. - */ -@Deprecated("Use sorted() instead.", ReplaceWith("sorted()")) -public fun LongArray.toSortedList(): List { - val sortedList = toArrayList() - java.util.Collections.sort(sortedList) - return sortedList -} - -/** - * Returns a sorted list of all elements. - */ -@Deprecated("Use sorted() instead.", ReplaceWith("sorted()")) -public fun ShortArray.toSortedList(): List { - val sortedList = toArrayList() - java.util.Collections.sort(sortedList) - return sortedList -} - -/** - * Returns a sorted list of all elements. - */ -@Deprecated("Use sorted() instead.", ReplaceWith("sorted()")) -public fun > Iterable.toSortedList(): List { - val sortedList = toArrayList() - java.util.Collections.sort(sortedList) - return sortedList -} - -/** - * Returns a sorted list of all elements. - */ -@Deprecated("Use asIterable().sorted() instead.", ReplaceWith("asIterable().sorted()")) -public fun > Sequence.toSortedList(): List { - val sortedList = toArrayList() - java.util.Collections.sort(sortedList) - return sortedList -} - -/** - * Returns a sorted list of all elements, ordered by results of specified [order] function. - */ -@Deprecated("Use sortedBy(order) instead.", ReplaceWith("sortedBy(order)")) -public fun > Array.toSortedListBy(order: (T) -> V): List { - val sortedList = toArrayList() - val sortBy: Comparator = compareBy(order) - java.util.Collections.sort(sortedList, sortBy) - return sortedList -} - -/** - * Returns a sorted list of all elements, ordered by results of specified [order] function. - */ -@Deprecated("Use sortedBy(order) instead.", ReplaceWith("sortedBy(order)")) -public fun > BooleanArray.toSortedListBy(order: (Boolean) -> V): List { - val sortedList = toArrayList() - val sortBy: Comparator = compareBy(order) - java.util.Collections.sort(sortedList, sortBy) - return sortedList -} - -/** - * Returns a sorted list of all elements, ordered by results of specified [order] function. - */ -@Deprecated("Use sortedBy(order) instead.", ReplaceWith("sortedBy(order)")) -public fun > ByteArray.toSortedListBy(order: (Byte) -> V): List { - val sortedList = toArrayList() - val sortBy: Comparator = compareBy(order) - java.util.Collections.sort(sortedList, sortBy) - return sortedList -} - -/** - * Returns a sorted list of all elements, ordered by results of specified [order] function. - */ -@Deprecated("Use sortedBy(order) instead.", ReplaceWith("sortedBy(order)")) -public fun > CharArray.toSortedListBy(order: (Char) -> V): List { - val sortedList = toArrayList() - val sortBy: Comparator = compareBy(order) - java.util.Collections.sort(sortedList, sortBy) - return sortedList -} - -/** - * Returns a sorted list of all elements, ordered by results of specified [order] function. - */ -@Deprecated("Use sortedBy(order) instead.", ReplaceWith("sortedBy(order)")) -public fun > DoubleArray.toSortedListBy(order: (Double) -> V): List { - val sortedList = toArrayList() - val sortBy: Comparator = compareBy(order) - java.util.Collections.sort(sortedList, sortBy) - return sortedList -} - -/** - * Returns a sorted list of all elements, ordered by results of specified [order] function. - */ -@Deprecated("Use sortedBy(order) instead.", ReplaceWith("sortedBy(order)")) -public fun > FloatArray.toSortedListBy(order: (Float) -> V): List { - val sortedList = toArrayList() - val sortBy: Comparator = compareBy(order) - java.util.Collections.sort(sortedList, sortBy) - return sortedList -} - -/** - * Returns a sorted list of all elements, ordered by results of specified [order] function. - */ -@Deprecated("Use sortedBy(order) instead.", ReplaceWith("sortedBy(order)")) -public fun > IntArray.toSortedListBy(order: (Int) -> V): List { - val sortedList = toArrayList() - val sortBy: Comparator = compareBy(order) - java.util.Collections.sort(sortedList, sortBy) - return sortedList -} - -/** - * Returns a sorted list of all elements, ordered by results of specified [order] function. - */ -@Deprecated("Use sortedBy(order) instead.", ReplaceWith("sortedBy(order)")) -public fun > LongArray.toSortedListBy(order: (Long) -> V): List { - val sortedList = toArrayList() - val sortBy: Comparator = compareBy(order) - java.util.Collections.sort(sortedList, sortBy) - return sortedList -} - -/** - * Returns a sorted list of all elements, ordered by results of specified [order] function. - */ -@Deprecated("Use sortedBy(order) instead.", ReplaceWith("sortedBy(order)")) -public fun > ShortArray.toSortedListBy(order: (Short) -> V): List { - val sortedList = toArrayList() - val sortBy: Comparator = compareBy(order) - java.util.Collections.sort(sortedList, sortBy) - return sortedList -} - -/** - * Returns a sorted list of all elements, ordered by results of specified [order] function. - */ -@Deprecated("Use sortedBy(order) instead.", ReplaceWith("sortedBy(order)")) -public fun > Iterable.toSortedListBy(order: (T) -> V): List { - val sortedList = toArrayList() - val sortBy: Comparator = compareBy(order) - java.util.Collections.sort(sortedList, sortBy) - return sortedList -} - -/** - * Returns a sorted list of all elements, ordered by results of specified [order] function. - */ -@Deprecated("Use asIterable().sortedBy(order) instead.", ReplaceWith("asIterable().sortedBy(order)")) -public fun > Sequence.toSortedListBy(order: (T) -> V): List { - val sortedList = toArrayList() - val sortBy: Comparator = compareBy(order) - java.util.Collections.sort(sortedList, sortBy) - return sortedList -} - diff --git a/libraries/stdlib/src/generated/_Ranges.kt b/libraries/stdlib/src/generated/_Ranges.kt index 0a085b1cca1..a16323c22a7 100644 --- a/libraries/stdlib/src/generated/_Ranges.kt +++ b/libraries/stdlib/src/generated/_Ranges.kt @@ -1,3 +1,6 @@ +@file:kotlin.jvm.JvmMultifileClass +@file:kotlin.jvm.JvmName("RangesKt") + package kotlin // @@ -5,7 +8,6 @@ package kotlin // See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib // -import kotlin.platform.* import java.util.* import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js @@ -970,3 +972,297 @@ public fun Short.until(to: Short): ShortRange { return this .. to_ } +/** + * Ensures that this value is not less than the specified [minimumValue]. + * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. + */ +public fun > T.coerceAtLeast(minimumValue: T): T { + return if (this < minimumValue) minimumValue else this +} + +/** + * Ensures that this value is not less than the specified [minimumValue]. + * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. + */ +public fun Byte.coerceAtLeast(minimumValue: Byte): Byte { + return if (this < minimumValue) minimumValue else this +} + +/** + * Ensures that this value is not less than the specified [minimumValue]. + * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. + */ +public fun Double.coerceAtLeast(minimumValue: Double): Double { + return if (this < minimumValue) minimumValue else this +} + +/** + * Ensures that this value is not less than the specified [minimumValue]. + * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. + */ +public fun Float.coerceAtLeast(minimumValue: Float): Float { + return if (this < minimumValue) minimumValue else this +} + +/** + * Ensures that this value is not less than the specified [minimumValue]. + * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. + */ +public fun Int.coerceAtLeast(minimumValue: Int): Int { + return if (this < minimumValue) minimumValue else this +} + +/** + * Ensures that this value is not less than the specified [minimumValue]. + * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. + */ +public fun Long.coerceAtLeast(minimumValue: Long): Long { + return if (this < minimumValue) minimumValue else this +} + +/** + * Ensures that this value is not less than the specified [minimumValue]. + * @return this value if it's greater than or equal to the [minimumValue] or the [minimumValue] otherwise. + */ +public fun Short.coerceAtLeast(minimumValue: Short): Short { + return if (this < minimumValue) minimumValue else this +} + +/** + * Ensures that this value is not greater than the specified [maximumValue]. + * @return this value if it's greater than or equal to the [maximumValue] or the [maximumValue] otherwise. + */ +public fun > T.coerceAtMost(maximumValue: T): T { + return if (this > maximumValue) maximumValue else this +} + +/** + * Ensures that this value is not greater than the specified [maximumValue]. + * @return this value if it's greater than or equal to the [maximumValue] or the [maximumValue] otherwise. + */ +public fun Byte.coerceAtMost(maximumValue: Byte): Byte { + return if (this > maximumValue) maximumValue else this +} + +/** + * Ensures that this value is not greater than the specified [maximumValue]. + * @return this value if it's greater than or equal to the [maximumValue] or the [maximumValue] otherwise. + */ +public fun Double.coerceAtMost(maximumValue: Double): Double { + return if (this > maximumValue) maximumValue else this +} + +/** + * Ensures that this value is not greater than the specified [maximumValue]. + * @return this value if it's greater than or equal to the [maximumValue] or the [maximumValue] otherwise. + */ +public fun Float.coerceAtMost(maximumValue: Float): Float { + return if (this > maximumValue) maximumValue else this +} + +/** + * Ensures that this value is not greater than the specified [maximumValue]. + * @return this value if it's greater than or equal to the [maximumValue] or the [maximumValue] otherwise. + */ +public fun Int.coerceAtMost(maximumValue: Int): Int { + return if (this > maximumValue) maximumValue else this +} + +/** + * Ensures that this value is not greater than the specified [maximumValue]. + * @return this value if it's greater than or equal to the [maximumValue] or the [maximumValue] otherwise. + */ +public fun Long.coerceAtMost(maximumValue: Long): Long { + return if (this > maximumValue) maximumValue else this +} + +/** + * Ensures that this value is not greater than the specified [maximumValue]. + * @return this value if it's greater than or equal to the [maximumValue] or the [maximumValue] otherwise. + */ +public fun Short.coerceAtMost(maximumValue: Short): Short { + return if (this > maximumValue) maximumValue else this +} + +/** + * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. + * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. + */ +public fun > T.coerceIn(minimumValue: T?, maximumValue: T?): T { + if (minimumValue !== null && maximumValue !== null) { + if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") + if (this < minimumValue) return minimumValue + if (this > maximumValue) return maximumValue + } + else { + if (minimumValue !== null && this < minimumValue) return minimumValue + if (maximumValue !== null && this > maximumValue) return maximumValue + } + return this +} + +/** + * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. + * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. + */ +public fun Byte.coerceIn(minimumValue: Byte?, maximumValue: Byte?): Byte { + if (minimumValue !== null && maximumValue !== null) { + if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") + if (this < minimumValue) return minimumValue + if (this > maximumValue) return maximumValue + } + else { + if (minimumValue !== null && this < minimumValue) return minimumValue + if (maximumValue !== null && this > maximumValue) return maximumValue + } + return this +} + +/** + * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. + * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. + */ +public fun Double.coerceIn(minimumValue: Double?, maximumValue: Double?): Double { + if (minimumValue !== null && maximumValue !== null) { + if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") + if (this < minimumValue) return minimumValue + if (this > maximumValue) return maximumValue + } + else { + if (minimumValue !== null && this < minimumValue) return minimumValue + if (maximumValue !== null && this > maximumValue) return maximumValue + } + return this +} + +/** + * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. + * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. + */ +public fun Float.coerceIn(minimumValue: Float?, maximumValue: Float?): Float { + if (minimumValue !== null && maximumValue !== null) { + if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") + if (this < minimumValue) return minimumValue + if (this > maximumValue) return maximumValue + } + else { + if (minimumValue !== null && this < minimumValue) return minimumValue + if (maximumValue !== null && this > maximumValue) return maximumValue + } + return this +} + +/** + * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. + * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. + */ +public fun Int.coerceIn(minimumValue: Int?, maximumValue: Int?): Int { + if (minimumValue !== null && maximumValue !== null) { + if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") + if (this < minimumValue) return minimumValue + if (this > maximumValue) return maximumValue + } + else { + if (minimumValue !== null && this < minimumValue) return minimumValue + if (maximumValue !== null && this > maximumValue) return maximumValue + } + return this +} + +/** + * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. + * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. + */ +public fun Long.coerceIn(minimumValue: Long?, maximumValue: Long?): Long { + if (minimumValue !== null && maximumValue !== null) { + if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") + if (this < minimumValue) return minimumValue + if (this > maximumValue) return maximumValue + } + else { + if (minimumValue !== null && this < minimumValue) return minimumValue + if (maximumValue !== null && this > maximumValue) return maximumValue + } + return this +} + +/** + * Ensures that this value lies in the specified range [minimumValue]..[maximumValue]. + * @return this value if it's in the range, or [minimumValue] if this value is less than [minimumValue], or [maximumValue] if this value is greater than [maximumValue]. + */ +public fun Short.coerceIn(minimumValue: Short?, maximumValue: Short?): Short { + if (minimumValue !== null && maximumValue !== null) { + if (minimumValue > maximumValue) throw IllegalArgumentException("Cannot coerce value to an empty range: maximum $maximumValue is less than minimum $minimumValue.") + if (this < minimumValue) return minimumValue + if (this > maximumValue) return maximumValue + } + else { + if (minimumValue !== null && this < minimumValue) return minimumValue + if (maximumValue !== null && this > maximumValue) return maximumValue + } + return this +} + +/** + * Ensures that this value lies in the specified [range]. + * @return this value if it's in the [range], or range.start if this value is less than range.start, or range.end if this value is greater than range.end. + */ +public fun > T.coerceIn(range: Range): T { + if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") + return if (this < range.start) range.start else if (this > range.end) range.end else this +} + +/** + * Ensures that this value lies in the specified [range]. + * @return this value if it's in the [range], or range.start if this value is less than range.start, or range.end if this value is greater than range.end. + */ +public fun Byte.coerceIn(range: Range): Byte { + if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") + return if (this < range.start) range.start else if (this > range.end) range.end else this +} + +/** + * Ensures that this value lies in the specified [range]. + * @return this value if it's in the [range], or range.start if this value is less than range.start, or range.end if this value is greater than range.end. + */ +public fun Double.coerceIn(range: Range): Double { + if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") + return if (this < range.start) range.start else if (this > range.end) range.end else this +} + +/** + * Ensures that this value lies in the specified [range]. + * @return this value if it's in the [range], or range.start if this value is less than range.start, or range.end if this value is greater than range.end. + */ +public fun Float.coerceIn(range: Range): Float { + if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") + return if (this < range.start) range.start else if (this > range.end) range.end else this +} + +/** + * Ensures that this value lies in the specified [range]. + * @return this value if it's in the [range], or range.start if this value is less than range.start, or range.end if this value is greater than range.end. + */ +public fun Int.coerceIn(range: Range): Int { + if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") + return if (this < range.start) range.start else if (this > range.end) range.end else this +} + +/** + * Ensures that this value lies in the specified [range]. + * @return this value if it's in the [range], or range.start if this value is less than range.start, or range.end if this value is greater than range.end. + */ +public fun Long.coerceIn(range: Range): Long { + if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") + return if (this < range.start) range.start else if (this > range.end) range.end else this +} + +/** + * Ensures that this value lies in the specified [range]. + * @return this value if it's in the [range], or range.start if this value is less than range.start, or range.end if this value is greater than range.end. + */ +public fun Short.coerceIn(range: Range): Short { + if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") + return if (this < range.start) range.start else if (this > range.end) range.end else this +} + diff --git a/libraries/stdlib/src/generated/_Sequences.kt b/libraries/stdlib/src/generated/_Sequences.kt index f4f889b02bd..c5caf8ddcf1 100644 --- a/libraries/stdlib/src/generated/_Sequences.kt +++ b/libraries/stdlib/src/generated/_Sequences.kt @@ -1,3 +1,6 @@ +@file:kotlin.jvm.JvmMultifileClass +@file:kotlin.jvm.JvmName("SequencesKt") + package kotlin // @@ -5,139 +8,976 @@ package kotlin // See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib // -import kotlin.platform.* import java.util.* import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js /** - * Returns a sequence from the given collection. + * Returns `true` if [element] is found in the collection. */ -public fun Array.asSequence(): Sequence { - if (isEmpty()) return emptySequence() +public operator fun Sequence.contains(element: T): Boolean { + return indexOf(element) >= 0 +} + +/** + * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. + */ +public fun Sequence.elementAt(index: Int): T { + return elementAtOrElse(index) { throw IndexOutOfBoundsException("Sequence doesn't contain element at index $index.") } +} + +/** + * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. + */ +public fun Sequence.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T { + if (index < 0) + return defaultValue(index) + val iterator = iterator() + var count = 0 + while (iterator.hasNext()) { + val element = iterator.next() + if (index == count++) + return element + } + return defaultValue(index) +} + +/** + * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. + */ +public fun Sequence.elementAtOrNull(index: Int): T? { + if (index < 0) + return null + val iterator = iterator() + var count = 0 + while (iterator.hasNext()) { + val element = iterator.next() + if (index == count++) + return element + } + return null +} + +/** + * Returns the first element matching the given [predicate], or `null` if element was not found. + */ +public inline fun Sequence.find(predicate: (T) -> Boolean): T? { + return firstOrNull(predicate) +} + +/** + * Returns the last element matching the given [predicate], or `null` if no such element was found. + */ +public inline fun Sequence.findLast(predicate: (T) -> Boolean): T? { + return lastOrNull(predicate) +} + +/** + * Returns first element. + * @throws [NoSuchElementException] if the collection is empty. + */ +public fun Sequence.first(): T { + val iterator = iterator() + if (!iterator.hasNext()) + throw NoSuchElementException("Sequence is empty.") + return iterator.next() +} + +/** + * Returns the first element matching the given [predicate]. + * @throws [NoSuchElementException] if no such element is found. + */ +public inline fun Sequence.first(predicate: (T) -> Boolean): T { + for (element in this) if (predicate(element)) return element + throw NoSuchElementException("No element matching predicate was found.") +} + +/** + * Returns the first element, or `null` if the collection is empty. + */ +public fun Sequence.firstOrNull(): T? { + val iterator = iterator() + if (!iterator.hasNext()) + return null + return iterator.next() +} + +/** + * Returns the first element matching the given [predicate], or `null` if element was not found. + */ +public inline fun Sequence.firstOrNull(predicate: (T) -> Boolean): T? { + for (element in this) if (predicate(element)) return element + return null +} + +/** + * Returns first index of [element], or -1 if the collection does not contain element. + */ +public fun Sequence.indexOf(element: T): Int { + var index = 0 + for (item in this) { + if (element == item) + return index + index++ + } + return -1 +} + +/** + * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element. + */ +public inline fun Sequence.indexOfFirst(predicate: (T) -> Boolean): Int { + var index = 0 + for (item in this) { + if (predicate(item)) + return index + index++ + } + return -1 +} + +/** + * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element. + */ +public inline fun Sequence.indexOfLast(predicate: (T) -> Boolean): Int { + var lastIndex = -1 + var index = 0 + for (item in this) { + if (predicate(item)) + lastIndex = index + index++ + } + return lastIndex +} + +/** + * Returns the last element. + * @throws [NoSuchElementException] if the collection is empty. + */ +public fun Sequence.last(): T { + val iterator = iterator() + if (!iterator.hasNext()) + throw NoSuchElementException("Sequence is empty.") + var last = iterator.next() + while (iterator.hasNext()) + last = iterator.next() + return last +} + +/** + * Returns the last element matching the given [predicate]. + * @throws [NoSuchElementException] if no such element is found. + */ +public inline fun Sequence.last(predicate: (T) -> Boolean): T { + var last: T? = null + var found = false + for (element in this) { + if (predicate(element)) { + last = element + found = true + } + } + if (!found) throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") + return last as T +} + +/** + * Returns last index of [element], or -1 if the collection does not contain element. + */ +public fun Sequence.lastIndexOf(element: T): Int { + var lastIndex = -1 + var index = 0 + for (item in this) { + if (element == item) + lastIndex = index + index++ + } + return lastIndex +} + +/** + * Returns the last element, or `null` if the collection is empty. + */ +public fun Sequence.lastOrNull(): T? { + val iterator = iterator() + if (!iterator.hasNext()) + return null + var last = iterator.next() + while (iterator.hasNext()) + last = iterator.next() + return last +} + +/** + * Returns the last element matching the given [predicate], or `null` if no such element was found. + */ +public inline fun Sequence.lastOrNull(predicate: (T) -> Boolean): T? { + var last: T? = null + for (element in this) { + if (predicate(element)) { + last = element + } + } + return last +} + +/** + * Returns the single element, or throws an exception if the collection is empty or has more than one element. + */ +public fun Sequence.single(): T { + val iterator = iterator() + if (!iterator.hasNext()) + throw NoSuchElementException("Sequence is empty.") + var single = iterator.next() + if (iterator.hasNext()) + throw IllegalArgumentException("Sequence has more than one element.") + return single +} + +/** + * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. + */ +public inline fun Sequence.single(predicate: (T) -> Boolean): T { + var single: T? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) throw IllegalArgumentException("Collection contains more than one matching element.") + single = element + found = true + } + } + if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") + return single as T +} + +/** + * Returns single element, or `null` if the collection is empty or has more than one element. + */ +public fun Sequence.singleOrNull(): T? { + val iterator = iterator() + if (!iterator.hasNext()) + return null + var single = iterator.next() + if (iterator.hasNext()) + return null + return single +} + +/** + * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. + */ +public inline fun Sequence.singleOrNull(predicate: (T) -> Boolean): T? { + var single: T? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) return null + single = element + found = true + } + } + if (!found) return null + return single +} + +/** + * Returns a sequence containing all elements except first [n] elements. + */ +public fun Sequence.drop(n: Int): Sequence { + require(n >= 0, { "Requested element count $n is less than zero." }) + return if (n == 0) this else DropSequence(this, n) +} + +/** + * Returns a sequence containing all elements except first elements that satisfy the given [predicate]. + */ +public fun Sequence.dropWhile(predicate: (T) -> Boolean): Sequence { + return DropWhileSequence(this, predicate) +} + +/** + * Returns a sequence containing all elements matching the given [predicate]. + */ +public fun Sequence.filter(predicate: (T) -> Boolean): Sequence { + return FilteringSequence(this, true, predicate) +} + +/** + * Returns a sequence containing all elements not matching the given [predicate]. + */ +public fun Sequence.filterNot(predicate: (T) -> Boolean): Sequence { + return FilteringSequence(this, false, predicate) +} + +/** + * Returns a sequence containing all elements that are not `null`. + */ +public fun Sequence.filterNotNull(): Sequence { + return filterNot { it == null } as Sequence +} + +/** + * Appends all elements that are not `null` to the given [destination]. + */ +public fun , T : Any> Sequence.filterNotNullTo(destination: C): C { + for (element in this) if (element != null) destination.add(element) + return destination +} + +/** + * Appends all elements not matching the given [predicate] to the given [destination]. + */ +public inline fun > Sequence.filterNotTo(destination: C, predicate: (T) -> Boolean): C { + for (element in this) if (!predicate(element)) destination.add(element) + return destination +} + +/** + * Appends all elements matching the given [predicate] into the given [destination]. + */ +public inline fun > Sequence.filterTo(destination: C, predicate: (T) -> Boolean): C { + for (element in this) if (predicate(element)) destination.add(element) + return destination +} + +/** + * Returns a sequence containing first [n] elements. + */ +public fun Sequence.take(n: Int): Sequence { + require(n >= 0, { "Requested element count $n is less than zero." }) + return if (n == 0) emptySequence() else TakeSequence(this, n) +} + +/** + * Returns a sequence containing first elements satisfying the given [predicate]. + */ +public fun Sequence.takeWhile(predicate: (T) -> Boolean): Sequence { + return TakeWhileSequence(this, predicate) +} + +/** + * Returns a sequence that yields elements of this sequence sorted according to their natural sort order. + */ +public fun > Sequence.sorted(): Sequence { return object : Sequence { override fun iterator(): Iterator { - return this@asSequence.iterator() + val sortedList = this@sorted.toArrayList() + java.util.Collections.sort(sortedList) + return sortedList.iterator() } } } /** - * Returns a sequence from the given collection. + * Returns a sequence that yields elements of this sequence sorted according to natural sort order of the value returned by specified [selector] function. */ -public fun BooleanArray.asSequence(): Sequence { - if (isEmpty()) return emptySequence() - return object : Sequence { - override fun iterator(): Iterator { - return this@asSequence.iterator() - } - } +public inline fun > Sequence.sortedBy(crossinline selector: (T) -> R?): Sequence { + return sortedWith(compareBy(selector)) } /** - * Returns a sequence from the given collection. + * Returns a sequence that yields elements of this sequence sorted descending according to natural sort order of the value returned by specified [selector] function. */ -public fun ByteArray.asSequence(): Sequence { - if (isEmpty()) return emptySequence() - return object : Sequence { - override fun iterator(): Iterator { - return this@asSequence.iterator() - } - } +public inline fun > Sequence.sortedByDescending(crossinline selector: (T) -> R?): Sequence { + return sortedWith(compareByDescending(selector)) } /** - * Returns a sequence from the given collection. + * Returns a sequence that yields elements of this sequence sorted descending according to their natural sort order. */ -public fun CharArray.asSequence(): Sequence { - if (isEmpty()) return emptySequence() - return object : Sequence { - override fun iterator(): Iterator { - return this@asSequence.iterator() - } - } +public fun > Sequence.sortedDescending(): Sequence { + return sortedWith(comparator { x, y -> y.compareTo(x) }) } /** - * Returns a sequence from the given collection. + * Returns a sequence that yields elements of this sequence sorted according to the specified [comparator]. */ -public fun DoubleArray.asSequence(): Sequence { - if (isEmpty()) return emptySequence() - return object : Sequence { - override fun iterator(): Iterator { - return this@asSequence.iterator() - } - } -} - -/** - * Returns a sequence from the given collection. - */ -public fun FloatArray.asSequence(): Sequence { - if (isEmpty()) return emptySequence() - return object : Sequence { - override fun iterator(): Iterator { - return this@asSequence.iterator() - } - } -} - -/** - * Returns a sequence from the given collection. - */ -public fun IntArray.asSequence(): Sequence { - if (isEmpty()) return emptySequence() - return object : Sequence { - override fun iterator(): Iterator { - return this@asSequence.iterator() - } - } -} - -/** - * Returns a sequence from the given collection. - */ -public fun LongArray.asSequence(): Sequence { - if (isEmpty()) return emptySequence() - return object : Sequence { - override fun iterator(): Iterator { - return this@asSequence.iterator() - } - } -} - -/** - * Returns a sequence from the given collection. - */ -public fun ShortArray.asSequence(): Sequence { - if (isEmpty()) return emptySequence() - return object : Sequence { - override fun iterator(): Iterator { - return this@asSequence.iterator() - } - } -} - -/** - * Returns a sequence from the given collection. - */ -public fun Iterable.asSequence(): Sequence { +public fun Sequence.sortedWith(comparator: Comparator): Sequence { return object : Sequence { override fun iterator(): Iterator { - return this@asSequence.iterator() + val sortedList = this@sortedWith.toArrayList() + java.util.Collections.sort(sortedList, comparator) + return sortedList.iterator() } } } /** - * Returns a sequence from the given collection. + * Returns a sorted list of all elements. */ -public fun Map.asSequence(): Sequence> { - return object : Sequence> { - override fun iterator(): Iterator> { - return this@asSequence.iterator() +@Deprecated("Use asIterable().sorted() instead.", ReplaceWith("asIterable().sorted()")) +public fun > Sequence.toSortedList(): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList) + return sortedList +} + +/** + * Returns a sorted list of all elements, ordered by results of specified [order] function. + */ +@Deprecated("Use asIterable().sortedBy(order) instead.", ReplaceWith("asIterable().sortedBy(order)")) +public fun > Sequence.toSortedListBy(order: (T) -> V): List { + val sortedList = toArrayList() + val sortBy: Comparator = compareBy(order) + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Returns an [ArrayList] of all elements. + */ +public fun Sequence.toArrayList(): ArrayList { + return toCollection(ArrayList()) +} + +/** + * Appends all elements to the given [collection]. + */ +public fun > Sequence.toCollection(collection: C): C { + for (item in this) { + collection.add(item) + } + return collection +} + +/** + * Returns a [HashSet] of all elements. + */ +public fun Sequence.toHashSet(): HashSet { + return toCollection(HashSet()) +} + +/** + * Returns a [LinkedList] containing all elements. + */ +public fun Sequence.toLinkedList(): LinkedList { + return toCollection(LinkedList()) +} + +/** + * Returns a [List] containing all elements. + */ +public fun Sequence.toList(): List { + return this.toArrayList() +} + +/** + * Returns Map containing the values from the given collection indexed by [selector]. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun Sequence.toMap(selector: (T) -> K): Map { + val result = LinkedHashMap() + for (element in this) { + result.put(selector(element), element) + } + return result +} + +/** + * Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun Sequence.toMap(selector: (T) -> K, transform: (T) -> V): Map { + val result = LinkedHashMap() + for (element in this) { + result.put(selector(element), transform(element)) + } + return result +} + +/** + * Returns a [Set] of all elements. + */ +public fun Sequence.toSet(): Set { + return toCollection(LinkedHashSet()) +} + +/** + * Returns a [SortedSet] of all elements. + */ +public fun Sequence.toSortedSet(): SortedSet { + return toCollection(TreeSet()) +} + +/** + * Returns a single sequence of all elements from results of [transform] function being invoked on each element of original sequence. + */ +public fun Sequence.flatMap(transform: (T) -> Sequence): Sequence { + return FlatteningSequence(this, transform) +} + +/** + * Appends all elements yielded from results of [transform] function being invoked on each element of original sequence, to the given [destination]. + */ +public inline fun > Sequence.flatMapTo(destination: C, transform: (T) -> Sequence): C { + for (element in this) { + val list = transform(element) + destination.addAll(list) + } + return destination +} + +/** + * Returns a map of the elements in original collection grouped by the result of given [toKey] function. + */ +public inline fun Sequence.groupBy(toKey: (T) -> K): Map> { + return groupByTo(LinkedHashMap>(), toKey) +} + +/** + * Appends elements from original collection grouped by the result of given [toKey] function to the given [map]. + */ +public inline fun Sequence.groupByTo(map: MutableMap>, toKey: (T) -> K): Map> { + for (element in this) { + val key = toKey(element) + val list = map.getOrPut(key) { ArrayList() } + list.add(element) + } + return map +} + +/** + * Returns a sequence containing the results of applying the given [transform] function to each element of the original sequence. + */ +public fun Sequence.map(transform: (T) -> R): Sequence { + return TransformingSequence(this, transform) +} + +/** + * Returns a sequence containing the results of applying the given [transform] function to each element and its index of the original sequence. + */ +public fun Sequence.mapIndexed(transform: (Int, T) -> R): Sequence { + return TransformingIndexedSequence(this, transform) +} + +/** + * Appends transformed elements and their indices of the original collection using the given [transform] function + * to the given [destination]. + */ +public inline fun > Sequence.mapIndexedTo(destination: C, transform: (Int, T) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} + +/** + * Returns a sequence containing the results of applying the given [transform] function to each non-null element of the original sequence. + */ +public fun Sequence.mapNotNull(transform: (T) -> R): Sequence { + return TransformingSequence(FilteringSequence(this, false, { it == null }) as Sequence, transform) +} + +/** + * Appends transformed non-null elements of original collection using the given [transform] function + * to the given [destination]. + */ +public inline fun > Sequence.mapNotNullTo(destination: C, transform: (T) -> R): C { + for (element in this) { + if (element != null) { + destination.add(transform(element)) } } + return destination +} + +/** + * Appends transformed elements of the original collection using the given [transform] function + * to the given [destination]. + */ +public inline fun > Sequence.mapTo(destination: C, transform: (T) -> R): C { + for (item in this) + destination.add(transform(item)) + return destination +} + +/** + * Returns a sequence of [IndexedValue] for each element of the original sequence. + */ +public fun Sequence.withIndex(): Sequence> { + return IndexingSequence(this) +} + +/** + * Returns a sequence containing pairs of each element of the original collection and their index. + */ +@Deprecated("Use withIndex() instead.") +public fun Sequence.withIndices(): Sequence> { + var index = 0 + return TransformingSequence(this, { index++ to it }) +} + +/** + * Returns a sequence containing only distinct elements from the given sequence. + * The elements in the resulting sequence are in the same order as they were in the source sequence. + */ +public fun Sequence.distinct(): Sequence { + return this.distinctBy { it } +} + +/** + * Returns a sequence containing only distinct elements from the given sequence according to the [keySelector]. + * The elements in the resulting sequence are in the same order as they were in the source sequence. + */ +public fun Sequence.distinctBy(keySelector: (T) -> K): Sequence { + return DistinctSequence(this, keySelector) +} + +/** + * Returns a mutable set containing all distinct elements from the given sequence. + */ +public fun Sequence.toMutableSet(): MutableSet { + val set = LinkedHashSet() + for (item in this) set.add(item) + return set +} + +/** + * Returns `true` if all elements match the given [predicate]. + */ +public inline fun Sequence.all(predicate: (T) -> Boolean): Boolean { + for (element in this) if (!predicate(element)) return false + return true +} + +/** + * Returns `true` if collection has at least one element. + */ +public fun Sequence.any(): Boolean { + for (element in this) return true + return false +} + +/** + * Returns `true` if at least one element matches the given [predicate]. + */ +public inline fun Sequence.any(predicate: (T) -> Boolean): Boolean { + for (element in this) if (predicate(element)) return true + return false +} + +/** + * Returns the number of elements in this collection. + */ +public fun Sequence.count(): Int { + var count = 0 + for (element in this) count++ + return count +} + +/** + * Returns the number of elements matching the given [predicate]. + */ +public inline fun Sequence.count(predicate: (T) -> Boolean): Int { + var count = 0 + for (element in this) if (predicate(element)) count++ + return count +} + +/** + * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. + */ +public inline fun Sequence.fold(initial: R, operation: (R, T) -> R): R { + var accumulator = initial + for (element in this) accumulator = operation(accumulator, element) + return accumulator +} + +/** + * Performs the given [operation] on each element. + */ +public inline fun Sequence.forEach(operation: (T) -> Unit): Unit { + for (element in this) operation(element) +} + +/** + * Performs the given [operation] on each element, providing sequential index with the element. + */ +public inline fun Sequence.forEachIndexed(operation: (Int, T) -> Unit): Unit { + var index = 0 + for (item in this) operation(index++, item) +} + +/** + * Returns the largest element or `null` if there are no elements. + */ +public fun > Sequence.max(): T? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var max = iterator.next() + while (iterator.hasNext()) { + val e = iterator.next() + if (max < e) max = e + } + return max +} + +/** + * Returns the first element yielding the largest value of the given function or `null` if there are no elements. + */ +public inline fun , T : Any> Sequence.maxBy(f: (T) -> R): T? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var maxElem = iterator.next() + var maxValue = f(maxElem) + while (iterator.hasNext()) { + val e = iterator.next() + val v = f(e) + if (maxValue < v) { + maxElem = e + maxValue = v + } + } + return maxElem +} + +/** + * Returns the smallest element or `null` if there are no elements. + */ +public fun > Sequence.min(): T? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var min = iterator.next() + while (iterator.hasNext()) { + val e = iterator.next() + if (min > e) min = e + } + return min +} + +/** + * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. + */ +public inline fun , T : Any> Sequence.minBy(f: (T) -> R): T? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var minElem = iterator.next() + var minValue = f(minElem) + while (iterator.hasNext()) { + val e = iterator.next() + val v = f(e) + if (minValue > v) { + minElem = e + minValue = v + } + } + return minElem +} + +/** + * Returns `true` if collection has no elements. + */ +public fun Sequence.none(): Boolean { + for (element in this) return false + return true +} + +/** + * Returns `true` if no elements match the given [predicate]. + */ +public inline fun Sequence.none(predicate: (T) -> 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 Sequence.reduce(operation: (S, T) -> S): S { + val iterator = this.iterator() + if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") + var accumulator: S = iterator.next() + while (iterator.hasNext()) { + accumulator = operation(accumulator, iterator.next()) + } + return accumulator +} + +/** + * Returns the sum of all values produced by [transform] function from elements in the collection. + */ +public inline fun Sequence.sumBy(transform: (T) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [transform] function from elements in the collection. + */ +public inline fun Sequence.sumByDouble(transform: (T) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns an original collection containing all the non-`null` elements, throwing an [IllegalArgumentException] if there are any `null` elements. + */ +public fun Sequence.requireNoNulls(): Sequence { + return map { it ?: throw IllegalArgumentException("null element found in $this.") } +} + +/** + * Returns a sequence of values built from elements of both collections with same indexes using provided [transform]. Resulting sequence has length of shortest input sequences. + */ +public fun Sequence.merge(sequence: Sequence, transform: (T, R) -> V): Sequence { + return MergingSequence(this, sequence, transform) +} + +/** + * Returns a sequence containing all elements of original sequence except the elements contained in the given [array]. + * Note that the source sequence and the array being subtracted are iterated only when an `iterator` is requested from + * the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. + */ +public operator fun Sequence.minus(array: Array): Sequence { + if (array.isEmpty()) return this + return object: Sequence { + override fun iterator(): Iterator { + val other = array.toHashSet() + return this@minus.filterNot { it in other }.iterator() + } + } +} + +/** + * Returns a sequence containing all elements of original sequence except the elements contained in the given [collection]. + * Note that the source sequence and the collection being subtracted are iterated only when an `iterator` is requested from + * the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. + */ +public operator fun Sequence.minus(collection: Iterable): Sequence { + return object: Sequence { + override fun iterator(): Iterator { + val other = collection.convertToSetForSetOperation() + if (other.isEmpty()) + return this@minus.iterator() + else + return this@minus.filterNot { it in other }.iterator() + } + } +} + +/** + * Returns a sequence containing all elements of the original sequence without the first occurrence of the given [element]. + */ +public operator fun Sequence.minus(element: T): Sequence { + return object: Sequence { + override fun iterator(): Iterator { + var removed = false + return this@minus.filter { if (!removed && it == element) { removed = true; false } else true }.iterator() + } + } +} + +/** + * Returns a sequence containing all elements of original sequence except the elements contained in the given [sequence]. + * Note that the source sequence and the sequence being subtracted are iterated only when an `iterator` is requested from + * the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. + */ +public operator fun Sequence.minus(sequence: Sequence): Sequence { + return object: Sequence { + override fun iterator(): Iterator { + val other = sequence.toHashSet() + if (other.isEmpty()) + return this@minus.iterator() + else + return this@minus.filterNot { it in other }.iterator() + } + } +} + +/** + * Splits the original collection into pair of collections, + * where *first* collection contains elements for which [predicate] yielded `true`, + * while *second* collection contains elements for which [predicate] yielded `false`. + */ +public inline fun Sequence.partition(predicate: (T) -> Boolean): Pair, List> { + val first = ArrayList() + val second = ArrayList() + for (element in this) { + if (predicate(element)) { + first.add(element) + } else { + second.add(element) + } + } + return Pair(first, second) +} + +/** + * Returns a sequence containing all elements of original sequence and then all elements of the given [array]. + * Note that the source sequence and the array being added are iterated only when an `iterator` is requested from + * the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. + */ +public operator fun Sequence.plus(array: Array): Sequence { + return this.plus(array.asList()) +} + +/** + * Returns a sequence containing all elements of original sequence and then all elements of the given [collection]. + * Note that the source sequence and the collection being added are iterated only when an `iterator` is requested from + * the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. + */ +public operator fun Sequence.plus(collection: Iterable): Sequence { + return sequenceOf(this, collection.asSequence()).flatten() +} + +/** + * Returns a sequence containing all elements of the original sequence and then the given [element]. + */ +public operator fun Sequence.plus(element: T): Sequence { + return sequenceOf(this, sequenceOf(element)).flatten() +} + +/** + * Returns a sequence containing all elements of original sequence and then all elements of the given [sequence]. + * Note that the source sequence and the sequence being added are iterated only when an `iterator` is requested from + * the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. + */ +public operator fun Sequence.plus(sequence: Sequence): Sequence { + return sequenceOf(this, sequence).flatten() +} + +/** + * Returns a sequence of pairs built from elements of both collections with same indexes. + * Resulting sequence has length of shortest input sequences. + */ +public fun Sequence.zip(sequence: Sequence): Sequence> { + return MergingSequence(this, sequence) { t1, t2 -> t1 to t2 } +} + +/** + * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. + * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] + * elements will be appended, followed by the [truncated] string (which defaults to "..."). + */ +public fun Sequence.joinTo(buffer: A, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((T) -> String)? = null): A { + buffer.append(prefix) + var count = 0 + for (element in this) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (transform != null) transform(element) else if (element == null) "null" else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) + return buffer +} + +/** + * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. + * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] + * elements will be appended, followed by the [truncated] string (which defaults to "..."). + */ +public fun Sequence.joinToString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((T) -> String)? = null): String { + return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() } /** @@ -147,106 +987,6 @@ public fun Sequence.asSequence(): Sequence { return this } -/** - * Returns a sequence from the given collection. - */ -public fun String.asSequence(): Sequence { - if (isEmpty()) return emptySequence() - return object : Sequence { - override fun iterator(): Iterator { - return this@asSequence.iterator() - } - } -} - -/** - * Returns a sequence from the given collection - */ -@Deprecated("Use asSequence() instead", ReplaceWith("asSequence()")) -public fun Array.sequence(): Sequence { - return asSequence() -} - -/** - * Returns a sequence from the given collection - */ -@Deprecated("Use asSequence() instead", ReplaceWith("asSequence()")) -public fun BooleanArray.sequence(): Sequence { - return asSequence() -} - -/** - * Returns a sequence from the given collection - */ -@Deprecated("Use asSequence() instead", ReplaceWith("asSequence()")) -public fun ByteArray.sequence(): Sequence { - return asSequence() -} - -/** - * Returns a sequence from the given collection - */ -@Deprecated("Use asSequence() instead", ReplaceWith("asSequence()")) -public fun CharArray.sequence(): Sequence { - return asSequence() -} - -/** - * Returns a sequence from the given collection - */ -@Deprecated("Use asSequence() instead", ReplaceWith("asSequence()")) -public fun DoubleArray.sequence(): Sequence { - return asSequence() -} - -/** - * Returns a sequence from the given collection - */ -@Deprecated("Use asSequence() instead", ReplaceWith("asSequence()")) -public fun FloatArray.sequence(): Sequence { - return asSequence() -} - -/** - * Returns a sequence from the given collection - */ -@Deprecated("Use asSequence() instead", ReplaceWith("asSequence()")) -public fun IntArray.sequence(): Sequence { - return asSequence() -} - -/** - * Returns a sequence from the given collection - */ -@Deprecated("Use asSequence() instead", ReplaceWith("asSequence()")) -public fun LongArray.sequence(): Sequence { - return asSequence() -} - -/** - * Returns a sequence from the given collection - */ -@Deprecated("Use asSequence() instead", ReplaceWith("asSequence()")) -public fun ShortArray.sequence(): Sequence { - return asSequence() -} - -/** - * Returns a sequence from the given collection - */ -@Deprecated("Use asSequence() instead", ReplaceWith("asSequence()")) -public fun Iterable.sequence(): Sequence { - return asSequence() -} - -/** - * Returns a sequence from the given collection - */ -@Deprecated("Use asSequence() instead", ReplaceWith("asSequence()")) -public fun Map.sequence(): Sequence> { - return asSequence() -} - /** * Returns a sequence from the given collection */ @@ -256,10 +996,204 @@ public fun Sequence.sequence(): Sequence { } /** - * Returns a sequence from the given collection + * Returns a sequence containing all elements that are instances of specified type parameter R. */ -@Deprecated("Use asSequence() instead", ReplaceWith("asSequence()")) -public fun String.sequence(): Sequence { - return asSequence() +@kotlin.jvm.JvmVersion +public inline fun Sequence<*>.filterIsInstance(): Sequence { + return filter { it is R } as Sequence +} + +/** + * Returns a sequence containing all elements that are instances of specified class. + */ +@kotlin.jvm.JvmVersion +public fun Sequence<*>.filterIsInstance(klass: Class): Sequence { + return filter { klass.isInstance(it) } as Sequence +} + +/** + * Appends all elements that are instances of specified type parameter R to the given [destination]. + */ +@kotlin.jvm.JvmVersion +public inline fun > Sequence<*>.filterIsInstanceTo(destination: C): C { + for (element in this) if (element is R) destination.add(element) + return destination +} + +/** + * Appends all elements that are instances of specified class to the given [destination]. + */ +@kotlin.jvm.JvmVersion +public fun , R> Sequence<*>.filterIsInstanceTo(destination: C, klass: Class): C { + for (element in this) if (klass.isInstance(element)) destination.add(element as R) + return destination +} + +/** + * Returns an average value of elements in the collection. + */ +@kotlin.jvm.JvmName("averageOfByte") +public fun Sequence.average(): Double { + val iterator = iterator() + var sum: Double = 0.0 + var count: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + count += 1 + } + return if (count == 0) 0.0 else sum / count +} + +/** + * Returns an average value of elements in the collection. + */ +@kotlin.jvm.JvmName("averageOfDouble") +public fun Sequence.average(): Double { + val iterator = iterator() + var sum: Double = 0.0 + var count: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + count += 1 + } + return if (count == 0) 0.0 else sum / count +} + +/** + * Returns an average value of elements in the collection. + */ +@kotlin.jvm.JvmName("averageOfFloat") +public fun Sequence.average(): Double { + val iterator = iterator() + var sum: Double = 0.0 + var count: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + count += 1 + } + return if (count == 0) 0.0 else sum / count +} + +/** + * Returns an average value of elements in the collection. + */ +@kotlin.jvm.JvmName("averageOfInt") +public fun Sequence.average(): Double { + val iterator = iterator() + var sum: Double = 0.0 + var count: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + count += 1 + } + return if (count == 0) 0.0 else sum / count +} + +/** + * Returns an average value of elements in the collection. + */ +@kotlin.jvm.JvmName("averageOfLong") +public fun Sequence.average(): Double { + val iterator = iterator() + var sum: Double = 0.0 + var count: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + count += 1 + } + return if (count == 0) 0.0 else sum / count +} + +/** + * Returns an average value of elements in the collection. + */ +@kotlin.jvm.JvmName("averageOfShort") +public fun Sequence.average(): Double { + val iterator = iterator() + var sum: Double = 0.0 + var count: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + count += 1 + } + return if (count == 0) 0.0 else sum / count +} + +/** + * Returns the sum of all elements in the collection. + */ +@kotlin.jvm.JvmName("sumOfByte") +public fun Sequence.sum(): Int { + val iterator = iterator() + var sum: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +@kotlin.jvm.JvmName("sumOfDouble") +public fun Sequence.sum(): Double { + val iterator = iterator() + var sum: Double = 0.0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +@kotlin.jvm.JvmName("sumOfFloat") +public fun Sequence.sum(): Float { + val iterator = iterator() + var sum: Float = 0.0f + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +@kotlin.jvm.JvmName("sumOfInt") +public fun Sequence.sum(): Int { + val iterator = iterator() + var sum: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +@kotlin.jvm.JvmName("sumOfLong") +public fun Sequence.sum(): Long { + val iterator = iterator() + var sum: Long = 0L + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum +} + +/** + * Returns the sum of all elements in the collection. + */ +@kotlin.jvm.JvmName("sumOfShort") +public fun Sequence.sum(): Int { + val iterator = iterator() + var sum: Int = 0 + while (iterator.hasNext()) { + sum += iterator.next() + } + return sum } diff --git a/libraries/stdlib/src/generated/_Sets.kt b/libraries/stdlib/src/generated/_Sets.kt index d22ee123416..f44659d3d92 100644 --- a/libraries/stdlib/src/generated/_Sets.kt +++ b/libraries/stdlib/src/generated/_Sets.kt @@ -1,3 +1,6 @@ +@file:kotlin.jvm.JvmMultifileClass +@file:kotlin.jvm.JvmName("SetsKt") + package kotlin // @@ -5,624 +8,88 @@ package kotlin // See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib // -import kotlin.platform.* import java.util.* import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js /** - * Returns a list containing only distinct elements from the given collection. - * The elements in the resulting list are in the same order as they were in the source collection. + * Returns a set containing all elements of the original set except the elements contained in the given [array]. */ -public fun Array.distinct(): List { - return this.toMutableSet().toList() +public operator fun Set.minus(array: Array): Set { + val result = LinkedHashSet(this) + result.removeAll(array) + return result } /** - * Returns a list containing only distinct elements from the given collection. - * The elements in the resulting list are in the same order as they were in the source collection. + * Returns a set containing all elements of the original set except the elements contained in the given [collection]. */ -public fun BooleanArray.distinct(): List { - return this.toMutableSet().toList() +public operator fun Set.minus(collection: Iterable): Set { + val other = collection.convertToSetForSetOperationWith(this) + if (other.isEmpty()) + return this.toSet() + if (other is Set) + return this.filterNotTo(LinkedHashSet()) { it in other } + val result = LinkedHashSet(this) + result.removeAll(other) + return result } /** - * Returns a list containing only distinct elements from the given collection. - * The elements in the resulting list are in the same order as they were in the source collection. + * Returns a set containing all elements of the original set except the given [element]. */ -public fun ByteArray.distinct(): List { - return this.toMutableSet().toList() +public operator fun Set.minus(element: T): Set { + val result = LinkedHashSet(mapCapacity(size())) + var removed = false + return this.filterTo(result) { if (!removed && it == element) { removed = true; false } else true } } /** - * Returns a list containing only distinct elements from the given collection. - * The elements in the resulting list are in the same order as they were in the source collection. + * Returns a set containing all elements of the original set except the elements contained in the given [sequence]. */ -public fun CharArray.distinct(): List { - return this.toMutableSet().toList() +public operator fun Set.minus(sequence: Sequence): Set { + val result = LinkedHashSet(this) + result.removeAll(sequence) + return result } /** - * Returns a list containing only distinct elements from the given collection. - * The elements in the resulting list are in the same order as they were in the source collection. + * Returns a set containing all elements both of the original set and the given [array]. */ -public fun DoubleArray.distinct(): List { - return this.toMutableSet().toList() +public operator fun Set.plus(array: Array): Set { + val result = LinkedHashSet(mapCapacity(this.size() + array.size())) + result.addAll(this) + result.addAll(array) + return result } /** - * Returns a list containing only distinct elements from the given collection. - * The elements in the resulting list are in the same order as they were in the source collection. + * Returns a set containing all elements both of the original set and the given [collection]. */ -public fun FloatArray.distinct(): List { - return this.toMutableSet().toList() +public operator fun Set.plus(collection: Iterable): Set { + val result = LinkedHashSet(mapCapacity(collection.collectionSizeOrNull()?.let { this.size() + it } ?: this.size() * 2)) + result.addAll(this) + result.addAll(collection) + return result } /** - * Returns a list containing only distinct elements from the given collection. - * The elements in the resulting list are in the same order as they were in the source collection. + * Returns a set containing all elements of the original set and then the given [element]. */ -public fun IntArray.distinct(): List { - return this.toMutableSet().toList() +public operator fun Set.plus(element: T): Set { + val result = LinkedHashSet(mapCapacity(size() + 1)) + result.addAll(this) + result.add(element) + return result } /** - * Returns a list containing only distinct elements from the given collection. - * The elements in the resulting list are in the same order as they were in the source collection. + * Returns a set containing all elements both of the original set and the given [sequence]. */ -public fun LongArray.distinct(): List { - return this.toMutableSet().toList() -} - -/** - * Returns a list containing only distinct elements from the given collection. - * The elements in the resulting list are in the same order as they were in the source collection. - */ -public fun ShortArray.distinct(): List { - return this.toMutableSet().toList() -} - -/** - * Returns a list containing only distinct elements from the given collection. - * The elements in the resulting list are in the same order as they were in the source collection. - */ -public fun Iterable.distinct(): List { - return this.toMutableSet().toList() -} - -/** - * Returns a sequence containing only distinct elements from the given sequence. - * The elements in the resulting sequence are in the same order as they were in the source sequence. - */ -public fun Sequence.distinct(): Sequence { - return this.distinctBy { it } -} - -/** - * Returns a list containing only distinct elements from the given collection according to the [keySelector]. - * The elements in the resulting list are in the same order as they were in the source collection. - */ -public inline fun Array.distinctBy(keySelector: (T) -> K): List { - val set = HashSet() - val list = ArrayList() - for (e in this) { - val key = keySelector(e) - if (set.add(key)) - list.add(e) - } - return list -} - -/** - * Returns a list containing only distinct elements from the given collection according to the [keySelector]. - * The elements in the resulting list are in the same order as they were in the source collection. - */ -public inline fun BooleanArray.distinctBy(keySelector: (Boolean) -> K): List { - val set = HashSet() - val list = ArrayList() - for (e in this) { - val key = keySelector(e) - if (set.add(key)) - list.add(e) - } - return list -} - -/** - * Returns a list containing only distinct elements from the given collection according to the [keySelector]. - * The elements in the resulting list are in the same order as they were in the source collection. - */ -public inline fun ByteArray.distinctBy(keySelector: (Byte) -> K): List { - val set = HashSet() - val list = ArrayList() - for (e in this) { - val key = keySelector(e) - if (set.add(key)) - list.add(e) - } - return list -} - -/** - * Returns a list containing only distinct elements from the given collection according to the [keySelector]. - * The elements in the resulting list are in the same order as they were in the source collection. - */ -public inline fun CharArray.distinctBy(keySelector: (Char) -> K): List { - val set = HashSet() - val list = ArrayList() - for (e in this) { - val key = keySelector(e) - if (set.add(key)) - list.add(e) - } - return list -} - -/** - * Returns a list containing only distinct elements from the given collection according to the [keySelector]. - * The elements in the resulting list are in the same order as they were in the source collection. - */ -public inline fun DoubleArray.distinctBy(keySelector: (Double) -> K): List { - val set = HashSet() - val list = ArrayList() - for (e in this) { - val key = keySelector(e) - if (set.add(key)) - list.add(e) - } - return list -} - -/** - * Returns a list containing only distinct elements from the given collection according to the [keySelector]. - * The elements in the resulting list are in the same order as they were in the source collection. - */ -public inline fun FloatArray.distinctBy(keySelector: (Float) -> K): List { - val set = HashSet() - val list = ArrayList() - for (e in this) { - val key = keySelector(e) - if (set.add(key)) - list.add(e) - } - return list -} - -/** - * Returns a list containing only distinct elements from the given collection according to the [keySelector]. - * The elements in the resulting list are in the same order as they were in the source collection. - */ -public inline fun IntArray.distinctBy(keySelector: (Int) -> K): List { - val set = HashSet() - val list = ArrayList() - for (e in this) { - val key = keySelector(e) - if (set.add(key)) - list.add(e) - } - return list -} - -/** - * Returns a list containing only distinct elements from the given collection according to the [keySelector]. - * The elements in the resulting list are in the same order as they were in the source collection. - */ -public inline fun LongArray.distinctBy(keySelector: (Long) -> K): List { - val set = HashSet() - val list = ArrayList() - for (e in this) { - val key = keySelector(e) - if (set.add(key)) - list.add(e) - } - return list -} - -/** - * Returns a list containing only distinct elements from the given collection according to the [keySelector]. - * The elements in the resulting list are in the same order as they were in the source collection. - */ -public inline fun ShortArray.distinctBy(keySelector: (Short) -> K): List { - val set = HashSet() - val list = ArrayList() - for (e in this) { - val key = keySelector(e) - if (set.add(key)) - list.add(e) - } - return list -} - -/** - * Returns a list containing only distinct elements from the given collection according to the [keySelector]. - * The elements in the resulting list are in the same order as they were in the source collection. - */ -public inline fun Iterable.distinctBy(keySelector: (T) -> K): List { - val set = HashSet() - val list = ArrayList() - for (e in this) { - val key = keySelector(e) - if (set.add(key)) - list.add(e) - } - return list -} - -/** - * Returns a sequence containing only distinct elements from the given sequence according to the [keySelector]. - * The elements in the resulting sequence are in the same order as they were in the source sequence. - */ -public fun Sequence.distinctBy(keySelector: (T) -> K): Sequence { - return DistinctSequence(this, keySelector) -} - -/** - * Returns a set containing all elements that are contained by both this set and the specified collection. - */ -public 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. - */ -public 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. - */ -public 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. - */ -public 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 both this set and the specified collection. - */ -public 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. - */ -public 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. - */ -public 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. - */ -public 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. - */ -public 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. - */ -public fun Iterable.intersect(other: Iterable): Set { - val set = this.toMutableSet() - set.retainAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by this set and not contained by the specified collection. - */ -public 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 set and not contained by the specified collection. - */ -public 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 set and not contained by the specified collection. - */ -public 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 set and not contained by the specified collection. - */ -public fun CharArray.subtract(other: Iterable): Set { - val set = this.toMutableSet() - set.removeAll(other) - return set -} - -/** - * Returns a set containing all elements that are contained by this set and not contained by the specified collection. - */ -public 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 set and not contained by the specified collection. - */ -public 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 set and not contained by the specified collection. - */ -public 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 set and not contained by the specified collection. - */ -public 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 set and not contained by the specified collection. - */ -public 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 set and not contained by the specified collection. - */ -public fun Iterable.subtract(other: Iterable): Set { - val set = this.toMutableSet() - set.removeAll(other) - return set -} - -/** - * Returns a mutable set containing all distinct elements from the given collection. - */ -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 collection. - */ -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 collection. - */ -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 collection. - */ -public fun CharArray.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 collection. - */ -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 collection. - */ -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 collection. - */ -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 collection. - */ -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 collection. - */ -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 collection. - */ -public fun Iterable.toMutableSet(): MutableSet { - return when (this) { - is Collection -> LinkedHashSet(this) - else -> toCollection(LinkedHashSet()) - } -} - -/** - * Returns a mutable set containing all distinct elements from the given sequence. - */ -public fun Sequence.toMutableSet(): MutableSet { - val set = LinkedHashSet() - for (item in this) set.add(item) - return set -} - -/** - * Returns a set containing all distinct elements from both collections. - */ -public 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. - */ -public 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. - */ -public 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. - */ -public fun CharArray.union(other: Iterable): Set { - val set = this.toMutableSet() - set.addAll(other) - return set -} - -/** - * Returns a set containing all distinct elements from both collections. - */ -public 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. - */ -public 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. - */ -public 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. - */ -public 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. - */ -public 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. - */ -public fun Iterable.union(other: Iterable): Set { - val set = this.toMutableSet() - set.addAll(other) - return set +public operator fun Set.plus(sequence: Sequence): Set { + val result = LinkedHashSet(mapCapacity(this.size() * 2)) + result.addAll(this) + result.addAll(sequence) + return result } diff --git a/libraries/stdlib/src/generated/_Snapshots.kt b/libraries/stdlib/src/generated/_Snapshots.kt deleted file mode 100644 index e012804cf49..00000000000 --- a/libraries/stdlib/src/generated/_Snapshots.kt +++ /dev/null @@ -1,981 +0,0 @@ -package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// - -import kotlin.platform.* -import java.util.* - -import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js - -/** - * Returns an [ArrayList] of all elements. - */ -public fun Array.toArrayList(): ArrayList { - return this.asList().toArrayList() -} - -/** - * Returns an [ArrayList] of all elements. - */ -public fun BooleanArray.toArrayList(): ArrayList { - val list = ArrayList(size()) - for (item in this) list.add(item) - return list -} - -/** - * Returns an [ArrayList] of all elements. - */ -public fun ByteArray.toArrayList(): ArrayList { - val list = ArrayList(size()) - for (item in this) list.add(item) - return list -} - -/** - * Returns an [ArrayList] of all elements. - */ -public fun CharArray.toArrayList(): ArrayList { - val list = ArrayList(size()) - for (item in this) list.add(item) - return list -} - -/** - * Returns an [ArrayList] of all elements. - */ -public fun DoubleArray.toArrayList(): ArrayList { - val list = ArrayList(size()) - for (item in this) list.add(item) - return list -} - -/** - * Returns an [ArrayList] of all elements. - */ -public fun FloatArray.toArrayList(): ArrayList { - val list = ArrayList(size()) - for (item in this) list.add(item) - return list -} - -/** - * Returns an [ArrayList] of all elements. - */ -public fun IntArray.toArrayList(): ArrayList { - val list = ArrayList(size()) - for (item in this) list.add(item) - return list -} - -/** - * Returns an [ArrayList] of all elements. - */ -public fun LongArray.toArrayList(): ArrayList { - val list = ArrayList(size()) - for (item in this) list.add(item) - return list -} - -/** - * Returns an [ArrayList] of all elements. - */ -public fun ShortArray.toArrayList(): ArrayList { - val list = ArrayList(size()) - for (item in this) list.add(item) - return list -} - -/** - * Returns an [ArrayList] of all elements. - */ -public fun Collection.toArrayList(): ArrayList { - return ArrayList(this) -} - -/** - * Returns an [ArrayList] of all elements. - */ -public fun Iterable.toArrayList(): ArrayList { - if (this is Collection) - return this.toArrayList() - return toCollection(ArrayList()) -} - -/** - * Returns an [ArrayList] of all elements. - */ -public fun Sequence.toArrayList(): ArrayList { - return toCollection(ArrayList()) -} - -/** - * Returns an [ArrayList] of all elements. - */ -public fun String.toArrayList(): ArrayList { - return toCollection(ArrayList(length())) -} - -/** - * Appends all elements to the given [collection]. - */ -public fun > Array.toCollection(collection: C): C { - for (item in this) { - collection.add(item) - } - return collection -} - -/** - * Appends all elements to the given [collection]. - */ -public fun > BooleanArray.toCollection(collection: C): C { - for (item in this) { - collection.add(item) - } - return collection -} - -/** - * Appends all elements to the given [collection]. - */ -public fun > ByteArray.toCollection(collection: C): C { - for (item in this) { - collection.add(item) - } - return collection -} - -/** - * Appends all elements to the given [collection]. - */ -public fun > CharArray.toCollection(collection: C): C { - for (item in this) { - collection.add(item) - } - return collection -} - -/** - * Appends all elements to the given [collection]. - */ -public fun > DoubleArray.toCollection(collection: C): C { - for (item in this) { - collection.add(item) - } - return collection -} - -/** - * Appends all elements to the given [collection]. - */ -public fun > FloatArray.toCollection(collection: C): C { - for (item in this) { - collection.add(item) - } - return collection -} - -/** - * Appends all elements to the given [collection]. - */ -public fun > IntArray.toCollection(collection: C): C { - for (item in this) { - collection.add(item) - } - return collection -} - -/** - * Appends all elements to the given [collection]. - */ -public fun > LongArray.toCollection(collection: C): C { - for (item in this) { - collection.add(item) - } - return collection -} - -/** - * Appends all elements to the given [collection]. - */ -public fun > ShortArray.toCollection(collection: C): C { - for (item in this) { - collection.add(item) - } - return collection -} - -/** - * Appends all elements to the given [collection]. - */ -public fun > Iterable.toCollection(collection: C): C { - for (item in this) { - collection.add(item) - } - return collection -} - -/** - * Appends all elements to the given [collection]. - */ -public fun > Sequence.toCollection(collection: C): C { - for (item in this) { - collection.add(item) - } - return collection -} - -/** - * Appends all elements to the given [collection]. - */ -public fun > String.toCollection(collection: C): C { - for (item in this) { - collection.add(item) - } - return collection -} - -/** - * Returns a [HashSet] of all elements. - */ -public fun Array.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(size()))) -} - -/** - * Returns a [HashSet] of all elements. - */ -public fun BooleanArray.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(size()))) -} - -/** - * Returns a [HashSet] of all elements. - */ -public fun ByteArray.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(size()))) -} - -/** - * Returns a [HashSet] of all elements. - */ -public fun CharArray.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(size()))) -} - -/** - * Returns a [HashSet] of all elements. - */ -public fun DoubleArray.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(size()))) -} - -/** - * Returns a [HashSet] of all elements. - */ -public fun FloatArray.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(size()))) -} - -/** - * Returns a [HashSet] of all elements. - */ -public fun IntArray.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(size()))) -} - -/** - * Returns a [HashSet] of all elements. - */ -public fun LongArray.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(size()))) -} - -/** - * Returns a [HashSet] of all elements. - */ -public fun ShortArray.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(size()))) -} - -/** - * Returns a [HashSet] of all elements. - */ -public fun Iterable.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(collectionSizeOrDefault(12)))) -} - -/** - * Returns a [HashSet] of all elements. - */ -public fun Sequence.toHashSet(): HashSet { - return toCollection(HashSet()) -} - -/** - * Returns a [HashSet] of all elements. - */ -public fun String.toHashSet(): HashSet { - return toCollection(HashSet(mapCapacity(length()))) -} - -/** - * Returns a [LinkedList] containing all elements. - */ -public fun Array.toLinkedList(): LinkedList { - return toCollection(LinkedList()) -} - -/** - * Returns a [LinkedList] containing all elements. - */ -public fun BooleanArray.toLinkedList(): LinkedList { - return toCollection(LinkedList()) -} - -/** - * Returns a [LinkedList] containing all elements. - */ -public fun ByteArray.toLinkedList(): LinkedList { - return toCollection(LinkedList()) -} - -/** - * Returns a [LinkedList] containing all elements. - */ -public fun CharArray.toLinkedList(): LinkedList { - return toCollection(LinkedList()) -} - -/** - * Returns a [LinkedList] containing all elements. - */ -public fun DoubleArray.toLinkedList(): LinkedList { - return toCollection(LinkedList()) -} - -/** - * Returns a [LinkedList] containing all elements. - */ -public fun FloatArray.toLinkedList(): LinkedList { - return toCollection(LinkedList()) -} - -/** - * Returns a [LinkedList] containing all elements. - */ -public fun IntArray.toLinkedList(): LinkedList { - return toCollection(LinkedList()) -} - -/** - * Returns a [LinkedList] containing all elements. - */ -public fun LongArray.toLinkedList(): LinkedList { - return toCollection(LinkedList()) -} - -/** - * Returns a [LinkedList] containing all elements. - */ -public fun ShortArray.toLinkedList(): LinkedList { - return toCollection(LinkedList()) -} - -/** - * Returns a [LinkedList] containing all elements. - */ -public fun Iterable.toLinkedList(): LinkedList { - return toCollection(LinkedList()) -} - -/** - * Returns a [LinkedList] containing all elements. - */ -public fun Sequence.toLinkedList(): LinkedList { - return toCollection(LinkedList()) -} - -/** - * Returns a [LinkedList] containing all elements. - */ -public fun String.toLinkedList(): LinkedList { - return toCollection(LinkedList()) -} - -/** - * Returns a [List] containing all key-value pairs. - */ -public fun Map.toList(): List> { - val result = ArrayList>(size()) - for (item in this) - result.add(item.key to item.value) - return result -} - -/** - * Returns a [List] containing all elements. - */ -public fun Array.toList(): List { - return this.toArrayList() -} - -/** - * Returns a [List] containing all elements. - */ -public fun BooleanArray.toList(): List { - return this.toArrayList() -} - -/** - * Returns a [List] containing all elements. - */ -public fun ByteArray.toList(): List { - return this.toArrayList() -} - -/** - * Returns a [List] containing all elements. - */ -public fun CharArray.toList(): List { - return this.toArrayList() -} - -/** - * Returns a [List] containing all elements. - */ -public fun DoubleArray.toList(): List { - return this.toArrayList() -} - -/** - * Returns a [List] containing all elements. - */ -public fun FloatArray.toList(): List { - return this.toArrayList() -} - -/** - * Returns a [List] containing all elements. - */ -public fun IntArray.toList(): List { - return this.toArrayList() -} - -/** - * Returns a [List] containing all elements. - */ -public fun LongArray.toList(): List { - return this.toArrayList() -} - -/** - * Returns a [List] containing all elements. - */ -public fun ShortArray.toList(): List { - return this.toArrayList() -} - -/** - * Returns a [List] containing all elements. - */ -public fun Iterable.toList(): List { - return this.toArrayList() -} - -/** - * Returns a [List] containing all elements. - */ -public fun Sequence.toList(): List { - return this.toArrayList() -} - -/** - * Returns a [List] containing all elements. - */ -public fun String.toList(): List { - return this.toArrayList() -} - -/** - * Returns Map containing the values from the given collection indexed by [selector]. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ -public inline fun Array.toMap(selector: (T) -> K): Map { - val capacity = (size()/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), element) - } - return result -} - -/** - * Returns Map containing the values from the given collection indexed by [selector]. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ -public inline fun BooleanArray.toMap(selector: (Boolean) -> K): Map { - val capacity = (size()/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), element) - } - return result -} - -/** - * Returns Map containing the values from the given collection indexed by [selector]. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ -public inline fun ByteArray.toMap(selector: (Byte) -> K): Map { - val capacity = (size()/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), element) - } - return result -} - -/** - * Returns Map containing the values from the given collection indexed by [selector]. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ -public inline fun CharArray.toMap(selector: (Char) -> K): Map { - val capacity = (size()/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), element) - } - return result -} - -/** - * Returns Map containing the values from the given collection indexed by [selector]. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ -public inline fun DoubleArray.toMap(selector: (Double) -> K): Map { - val capacity = (size()/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), element) - } - return result -} - -/** - * Returns Map containing the values from the given collection indexed by [selector]. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ -public inline fun FloatArray.toMap(selector: (Float) -> K): Map { - val capacity = (size()/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), element) - } - return result -} - -/** - * Returns Map containing the values from the given collection indexed by [selector]. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ -public inline fun IntArray.toMap(selector: (Int) -> K): Map { - val capacity = (size()/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), element) - } - return result -} - -/** - * Returns Map containing the values from the given collection indexed by [selector]. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ -public inline fun LongArray.toMap(selector: (Long) -> K): Map { - val capacity = (size()/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), element) - } - return result -} - -/** - * Returns Map containing the values from the given collection indexed by [selector]. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ -public inline fun ShortArray.toMap(selector: (Short) -> K): Map { - val capacity = (size()/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), element) - } - return result -} - -/** - * Returns Map containing the values from the given collection indexed by [selector]. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ -public inline fun Iterable.toMap(selector: (T) -> K): Map { - val capacity = (collectionSizeOrDefault(10)/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), element) - } - return result -} - -/** - * Returns Map containing the values from the given collection indexed by [selector]. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ -public inline fun Sequence.toMap(selector: (T) -> K): Map { - val result = LinkedHashMap() - for (element in this) { - result.put(selector(element), element) - } - return result -} - -/** - * Returns Map containing the values from the given collection indexed by [selector]. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ -public inline fun String.toMap(selector: (Char) -> K): Map { - val capacity = (length()/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), element) - } - return result -} - -/** - * Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ -public inline fun Array.toMap(selector: (T) -> K, transform: (T) -> V): Map { - val capacity = (size()/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), transform(element)) - } - return result -} - -/** - * Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ -public inline fun BooleanArray.toMap(selector: (Boolean) -> K, transform: (Boolean) -> V): Map { - val capacity = (size()/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), transform(element)) - } - return result -} - -/** - * Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ -public inline fun ByteArray.toMap(selector: (Byte) -> K, transform: (Byte) -> V): Map { - val capacity = (size()/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), transform(element)) - } - return result -} - -/** - * Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ -public inline fun CharArray.toMap(selector: (Char) -> K, transform: (Char) -> V): Map { - val capacity = (size()/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), transform(element)) - } - return result -} - -/** - * Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ -public inline fun DoubleArray.toMap(selector: (Double) -> K, transform: (Double) -> V): Map { - val capacity = (size()/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), transform(element)) - } - return result -} - -/** - * Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ -public inline fun FloatArray.toMap(selector: (Float) -> K, transform: (Float) -> V): Map { - val capacity = (size()/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), transform(element)) - } - return result -} - -/** - * Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ -public inline fun IntArray.toMap(selector: (Int) -> K, transform: (Int) -> V): Map { - val capacity = (size()/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), transform(element)) - } - return result -} - -/** - * Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ -public inline fun LongArray.toMap(selector: (Long) -> K, transform: (Long) -> V): Map { - val capacity = (size()/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), transform(element)) - } - return result -} - -/** - * Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ -public inline fun ShortArray.toMap(selector: (Short) -> K, transform: (Short) -> V): Map { - val capacity = (size()/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), transform(element)) - } - return result -} - -/** - * Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ -public inline fun Iterable.toMap(selector: (T) -> K, transform: (T) -> V): Map { - val capacity = (collectionSizeOrDefault(10)/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), transform(element)) - } - return result -} - -/** - * Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ -public inline fun Sequence.toMap(selector: (T) -> K, transform: (T) -> V): Map { - val result = LinkedHashMap() - for (element in this) { - result.put(selector(element), transform(element)) - } - return result -} - -/** - * Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection. - * If any two elements would have the same key returned by [selector] the last one gets added to the map. - */ -public inline fun String.toMap(selector: (Char) -> K, transform: (Char) -> V): Map { - val capacity = (length()/.75f) + 1 - val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) - for (element in this) { - result.put(selector(element), transform(element)) - } - return result -} - -/** - * Returns a [Set] of all elements. - */ -public fun Array.toSet(): Set { - return toCollection(LinkedHashSet(mapCapacity(size()))) -} - -/** - * Returns a [Set] of all elements. - */ -public fun BooleanArray.toSet(): Set { - return toCollection(LinkedHashSet(mapCapacity(size()))) -} - -/** - * Returns a [Set] of all elements. - */ -public fun ByteArray.toSet(): Set { - return toCollection(LinkedHashSet(mapCapacity(size()))) -} - -/** - * Returns a [Set] of all elements. - */ -public fun CharArray.toSet(): Set { - return toCollection(LinkedHashSet(mapCapacity(size()))) -} - -/** - * Returns a [Set] of all elements. - */ -public fun DoubleArray.toSet(): Set { - return toCollection(LinkedHashSet(mapCapacity(size()))) -} - -/** - * Returns a [Set] of all elements. - */ -public fun FloatArray.toSet(): Set { - return toCollection(LinkedHashSet(mapCapacity(size()))) -} - -/** - * Returns a [Set] of all elements. - */ -public fun IntArray.toSet(): Set { - return toCollection(LinkedHashSet(mapCapacity(size()))) -} - -/** - * Returns a [Set] of all elements. - */ -public fun LongArray.toSet(): Set { - return toCollection(LinkedHashSet(mapCapacity(size()))) -} - -/** - * Returns a [Set] of all elements. - */ -public fun ShortArray.toSet(): Set { - return toCollection(LinkedHashSet(mapCapacity(size()))) -} - -/** - * Returns a [Set] of all elements. - */ -public fun Iterable.toSet(): Set { - return toCollection(LinkedHashSet(mapCapacity(collectionSizeOrDefault(12)))) -} - -/** - * Returns a [Set] of all elements. - */ -public fun Sequence.toSet(): Set { - return toCollection(LinkedHashSet()) -} - -/** - * Returns a [Set] of all elements. - */ -public fun String.toSet(): Set { - return toCollection(LinkedHashSet(mapCapacity(length()))) -} - -/** - * Returns a [SortedSet] of all elements. - */ -public fun Array.toSortedSet(): SortedSet { - return toCollection(TreeSet()) -} - -/** - * Returns a [SortedSet] of all elements. - */ -public fun BooleanArray.toSortedSet(): SortedSet { - return toCollection(TreeSet()) -} - -/** - * Returns a [SortedSet] of all elements. - */ -public fun ByteArray.toSortedSet(): SortedSet { - return toCollection(TreeSet()) -} - -/** - * Returns a [SortedSet] of all elements. - */ -public fun CharArray.toSortedSet(): SortedSet { - return toCollection(TreeSet()) -} - -/** - * Returns a [SortedSet] of all elements. - */ -public fun DoubleArray.toSortedSet(): SortedSet { - return toCollection(TreeSet()) -} - -/** - * Returns a [SortedSet] of all elements. - */ -public fun FloatArray.toSortedSet(): SortedSet { - return toCollection(TreeSet()) -} - -/** - * Returns a [SortedSet] of all elements. - */ -public fun IntArray.toSortedSet(): SortedSet { - return toCollection(TreeSet()) -} - -/** - * Returns a [SortedSet] of all elements. - */ -public fun LongArray.toSortedSet(): SortedSet { - return toCollection(TreeSet()) -} - -/** - * Returns a [SortedSet] of all elements. - */ -public fun ShortArray.toSortedSet(): SortedSet { - return toCollection(TreeSet()) -} - -/** - * Returns a [SortedSet] of all elements. - */ -public fun Iterable.toSortedSet(): SortedSet { - return toCollection(TreeSet()) -} - -/** - * Returns a [SortedSet] of all elements. - */ -public fun Sequence.toSortedSet(): SortedSet { - return toCollection(TreeSet()) -} - -/** - * Returns a [SortedSet] of all elements. - */ -public fun String.toSortedSet(): SortedSet { - return toCollection(TreeSet()) -} - diff --git a/libraries/stdlib/src/generated/_SpecialJVM.kt b/libraries/stdlib/src/generated/_SpecialJVM.kt deleted file mode 100644 index 70c49469264..00000000000 --- a/libraries/stdlib/src/generated/_SpecialJVM.kt +++ /dev/null @@ -1,990 +0,0 @@ -package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// - -import kotlin.platform.* -import java.util.* - -import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js - -/** - * Returns a [List] that wraps the original array. - */ -public fun Array.asList(): List { - return Arrays.asList(*this) -} - -/** - * Returns a [List] that wraps the original array. - */ -public fun BooleanArray.asList(): List { - return object : AbstractList(), RandomAccess { - override fun size(): Int = this@asList.size() - override fun isEmpty(): Boolean = this@asList.isEmpty() - override fun contains(o: Any?): Boolean = this@asList.contains(o as Boolean) - override fun iterator(): MutableIterator = this@asList.iterator() as MutableIterator - override fun get(index: Int): Boolean = this@asList[index] - override fun indexOf(o: Any?): Int = this@asList.indexOf(o as Boolean) - override fun lastIndexOf(o: Any?): Int = this@asList.lastIndexOf(o as Boolean) - } -} - -/** - * Returns a [List] that wraps the original array. - */ -public fun ByteArray.asList(): List { - return object : AbstractList(), RandomAccess { - override fun size(): Int = this@asList.size() - override fun isEmpty(): Boolean = this@asList.isEmpty() - override fun contains(o: Any?): Boolean = this@asList.contains(o as Byte) - override fun iterator(): MutableIterator = this@asList.iterator() as MutableIterator - override fun get(index: Int): Byte = this@asList[index] - override fun indexOf(o: Any?): Int = this@asList.indexOf(o as Byte) - override fun lastIndexOf(o: Any?): Int = this@asList.lastIndexOf(o as Byte) - } -} - -/** - * Returns a [List] that wraps the original array. - */ -public fun CharArray.asList(): List { - return object : AbstractList(), RandomAccess { - override fun size(): Int = this@asList.size() - override fun isEmpty(): Boolean = this@asList.isEmpty() - override fun contains(o: Any?): Boolean = this@asList.contains(o as Char) - override fun iterator(): MutableIterator = this@asList.iterator() as MutableIterator - override fun get(index: Int): Char = this@asList[index] - override fun indexOf(o: Any?): Int = this@asList.indexOf(o as Char) - override fun lastIndexOf(o: Any?): Int = this@asList.lastIndexOf(o as Char) - } -} - -/** - * Returns a [List] that wraps the original array. - */ -public fun DoubleArray.asList(): List { - return object : AbstractList(), RandomAccess { - override fun size(): Int = this@asList.size() - override fun isEmpty(): Boolean = this@asList.isEmpty() - override fun contains(o: Any?): Boolean = this@asList.contains(o as Double) - override fun iterator(): MutableIterator = this@asList.iterator() as MutableIterator - override fun get(index: Int): Double = this@asList[index] - override fun indexOf(o: Any?): Int = this@asList.indexOf(o as Double) - override fun lastIndexOf(o: Any?): Int = this@asList.lastIndexOf(o as Double) - } -} - -/** - * Returns a [List] that wraps the original array. - */ -public fun FloatArray.asList(): List { - return object : AbstractList(), RandomAccess { - override fun size(): Int = this@asList.size() - override fun isEmpty(): Boolean = this@asList.isEmpty() - override fun contains(o: Any?): Boolean = this@asList.contains(o as Float) - override fun iterator(): MutableIterator = this@asList.iterator() as MutableIterator - override fun get(index: Int): Float = this@asList[index] - override fun indexOf(o: Any?): Int = this@asList.indexOf(o as Float) - override fun lastIndexOf(o: Any?): Int = this@asList.lastIndexOf(o as Float) - } -} - -/** - * Returns a [List] that wraps the original array. - */ -public fun IntArray.asList(): List { - return object : AbstractList(), RandomAccess { - override fun size(): Int = this@asList.size() - override fun isEmpty(): Boolean = this@asList.isEmpty() - override fun contains(o: Any?): Boolean = this@asList.contains(o as Int) - override fun iterator(): MutableIterator = this@asList.iterator() as MutableIterator - override fun get(index: Int): Int = this@asList[index] - override fun indexOf(o: Any?): Int = this@asList.indexOf(o as Int) - override fun lastIndexOf(o: Any?): Int = this@asList.lastIndexOf(o as Int) - } -} - -/** - * Returns a [List] that wraps the original array. - */ -public fun LongArray.asList(): List { - return object : AbstractList(), RandomAccess { - override fun size(): Int = this@asList.size() - override fun isEmpty(): Boolean = this@asList.isEmpty() - override fun contains(o: Any?): Boolean = this@asList.contains(o as Long) - override fun iterator(): MutableIterator = this@asList.iterator() as MutableIterator - override fun get(index: Int): Long = this@asList[index] - override fun indexOf(o: Any?): Int = this@asList.indexOf(o as Long) - override fun lastIndexOf(o: Any?): Int = this@asList.lastIndexOf(o as Long) - } -} - -/** - * Returns a [List] that wraps the original array. - */ -public fun ShortArray.asList(): List { - return object : AbstractList(), RandomAccess { - override fun size(): Int = this@asList.size() - override fun isEmpty(): Boolean = this@asList.isEmpty() - override fun contains(o: Any?): Boolean = this@asList.contains(o as Short) - override fun iterator(): MutableIterator = this@asList.iterator() as MutableIterator - override fun get(index: Int): Short = this@asList[index] - override fun indexOf(o: Any?): Int = this@asList.indexOf(o as Short) - override fun lastIndexOf(o: Any?): Int = this@asList.lastIndexOf(o as Short) - } -} - -/** - * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted according to the specified [comparator]. - */ -public fun Array.binarySearch(element: T, comparator: Comparator, fromIndex: Int = 0, toIndex: Int = size()): Int { - return Arrays.binarySearch(this, fromIndex, toIndex, element, comparator) -} - -/** - * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. - */ -public fun Array.binarySearch(element: T, fromIndex: Int = 0, toIndex: Int = size()): Int { - return Arrays.binarySearch(this, fromIndex, toIndex, element) -} - -/** - * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. - */ -public fun ByteArray.binarySearch(element: Byte, fromIndex: Int = 0, toIndex: Int = size()): Int { - return Arrays.binarySearch(this, fromIndex, toIndex, element) -} - -/** - * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. - */ -public fun CharArray.binarySearch(element: Char, fromIndex: Int = 0, toIndex: Int = size()): Int { - return Arrays.binarySearch(this, fromIndex, toIndex, element) -} - -/** - * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. - */ -public fun DoubleArray.binarySearch(element: Double, fromIndex: Int = 0, toIndex: Int = size()): Int { - return Arrays.binarySearch(this, fromIndex, toIndex, element) -} - -/** - * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. - */ -public fun FloatArray.binarySearch(element: Float, fromIndex: Int = 0, toIndex: Int = size()): Int { - return Arrays.binarySearch(this, fromIndex, toIndex, element) -} - -/** - * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. - */ -public fun IntArray.binarySearch(element: Int, fromIndex: Int = 0, toIndex: Int = size()): Int { - return Arrays.binarySearch(this, fromIndex, toIndex, element) -} - -/** - * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. - */ -public fun LongArray.binarySearch(element: Long, fromIndex: Int = 0, toIndex: Int = size()): Int { - return Arrays.binarySearch(this, fromIndex, toIndex, element) -} - -/** - * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. - */ -public fun ShortArray.binarySearch(element: Short, fromIndex: Int = 0, toIndex: Int = size()): Int { - return Arrays.binarySearch(this, fromIndex, toIndex, element) -} - -/** - * Returns new array which is a copy of the original array. - */ -public fun Array.copyOf(): Array { - return Arrays.copyOf(this, size()) -} - -/** - * Returns new array which is a copy of the original array. - */ -public fun BooleanArray.copyOf(): BooleanArray { - return Arrays.copyOf(this, size()) -} - -/** - * Returns new array which is a copy of the original array. - */ -public fun ByteArray.copyOf(): ByteArray { - return Arrays.copyOf(this, size()) -} - -/** - * Returns new array which is a copy of the original array. - */ -public fun CharArray.copyOf(): CharArray { - return Arrays.copyOf(this, size()) -} - -/** - * Returns new array which is a copy of the original array. - */ -public fun DoubleArray.copyOf(): DoubleArray { - return Arrays.copyOf(this, size()) -} - -/** - * Returns new array which is a copy of the original array. - */ -public fun FloatArray.copyOf(): FloatArray { - return Arrays.copyOf(this, size()) -} - -/** - * Returns new array which is a copy of the original array. - */ -public fun IntArray.copyOf(): IntArray { - return Arrays.copyOf(this, size()) -} - -/** - * Returns new array which is a copy of the original array. - */ -public fun LongArray.copyOf(): LongArray { - return Arrays.copyOf(this, size()) -} - -/** - * Returns new array which is a copy of the original array. - */ -public fun ShortArray.copyOf(): ShortArray { - return Arrays.copyOf(this, size()) -} - -/** - * Returns new array which is a copy of the original array. - */ -@JvmName("mutableCopyOf") -public fun Array.copyOf(): Array { - return Arrays.copyOf(this, size()) -} - -/** - * Returns new array which is a copy of the original array. - */ -public fun Array.copyOf(newSize: Int): Array { - return Arrays.copyOf(this, newSize) -} - -/** - * Returns new array which is a copy of the original array. - */ -public fun BooleanArray.copyOf(newSize: Int): BooleanArray { - return Arrays.copyOf(this, newSize) -} - -/** - * Returns new array which is a copy of the original array. - */ -public fun ByteArray.copyOf(newSize: Int): ByteArray { - return Arrays.copyOf(this, newSize) -} - -/** - * Returns new array which is a copy of the original array. - */ -public fun CharArray.copyOf(newSize: Int): CharArray { - return Arrays.copyOf(this, newSize) -} - -/** - * Returns new array which is a copy of the original array. - */ -public fun DoubleArray.copyOf(newSize: Int): DoubleArray { - return Arrays.copyOf(this, newSize) -} - -/** - * Returns new array which is a copy of the original array. - */ -public fun FloatArray.copyOf(newSize: Int): FloatArray { - return Arrays.copyOf(this, newSize) -} - -/** - * Returns new array which is a copy of the original array. - */ -public fun IntArray.copyOf(newSize: Int): IntArray { - return Arrays.copyOf(this, newSize) -} - -/** - * Returns new array which is a copy of the original array. - */ -public fun LongArray.copyOf(newSize: Int): LongArray { - return Arrays.copyOf(this, newSize) -} - -/** - * Returns new array which is a copy of the original array. - */ -public fun ShortArray.copyOf(newSize: Int): ShortArray { - return Arrays.copyOf(this, newSize) -} - -/** - * Returns new array which is a copy of the original array. - */ -@JvmName("mutableCopyOf") -public fun Array.copyOf(newSize: Int): Array { - return Arrays.copyOf(this, newSize) -} - -/** - * Returns new array which is a copy of range of original array. - */ -public fun Array.copyOfRange(fromIndex: Int, toIndex: Int): Array { - return Arrays.copyOfRange(this, fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -public fun BooleanArray.copyOfRange(fromIndex: Int, toIndex: Int): BooleanArray { - return Arrays.copyOfRange(this, fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -public fun ByteArray.copyOfRange(fromIndex: Int, toIndex: Int): ByteArray { - return Arrays.copyOfRange(this, fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -public fun CharArray.copyOfRange(fromIndex: Int, toIndex: Int): CharArray { - return Arrays.copyOfRange(this, fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -public fun DoubleArray.copyOfRange(fromIndex: Int, toIndex: Int): DoubleArray { - return Arrays.copyOfRange(this, fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -public fun FloatArray.copyOfRange(fromIndex: Int, toIndex: Int): FloatArray { - return Arrays.copyOfRange(this, fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -public fun IntArray.copyOfRange(fromIndex: Int, toIndex: Int): IntArray { - return Arrays.copyOfRange(this, fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -public fun LongArray.copyOfRange(fromIndex: Int, toIndex: Int): LongArray { - return Arrays.copyOfRange(this, fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -public fun ShortArray.copyOfRange(fromIndex: Int, toIndex: Int): ShortArray { - return Arrays.copyOfRange(this, fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@JvmName("mutableCopyOfRange") -public fun Array.copyOfRange(fromIndex: Int, toIndex: Int): Array { - return Arrays.copyOfRange(this, fromIndex, toIndex) -} - -/** - * Fills original array with the provided value. - */ -public fun BooleanArray.fill(element: Boolean, fromIndex: Int = 0, toIndex: Int = size()): Unit { - Arrays.fill(this, fromIndex, toIndex, element) -} - -/** - * Fills original array with the provided value. - */ -public fun ByteArray.fill(element: Byte, fromIndex: Int = 0, toIndex: Int = size()): Unit { - Arrays.fill(this, fromIndex, toIndex, element) -} - -/** - * Fills original array with the provided value. - */ -public fun CharArray.fill(element: Char, fromIndex: Int = 0, toIndex: Int = size()): Unit { - Arrays.fill(this, fromIndex, toIndex, element) -} - -/** - * Fills original array with the provided value. - */ -public fun DoubleArray.fill(element: Double, fromIndex: Int = 0, toIndex: Int = size()): Unit { - Arrays.fill(this, fromIndex, toIndex, element) -} - -/** - * Fills original array with the provided value. - */ -public fun FloatArray.fill(element: Float, fromIndex: Int = 0, toIndex: Int = size()): Unit { - Arrays.fill(this, fromIndex, toIndex, element) -} - -/** - * Fills original array with the provided value. - */ -public fun IntArray.fill(element: Int, fromIndex: Int = 0, toIndex: Int = size()): Unit { - Arrays.fill(this, fromIndex, toIndex, element) -} - -/** - * Fills original array with the provided value. - */ -public fun LongArray.fill(element: Long, fromIndex: Int = 0, toIndex: Int = size()): Unit { - Arrays.fill(this, fromIndex, toIndex, element) -} - -/** - * Fills original array with the provided value. - */ -public fun ShortArray.fill(element: Short, fromIndex: Int = 0, toIndex: Int = size()): Unit { - Arrays.fill(this, fromIndex, toIndex, element) -} - -/** - * Fills original array with the provided value. - */ -public fun Array.fill(element: T, fromIndex: Int = 0, toIndex: Int = size()): Unit { - Arrays.fill(this, fromIndex, toIndex, element) -} - -/** - * Returns a list containing all elements that are instances of specified type parameter R. - */ -public inline fun Array<*>.filterIsInstance(): List { - return filterIsInstanceTo(ArrayList()) -} - -/** - * Returns a list containing all elements that are instances of specified type parameter R. - */ -public inline fun Iterable<*>.filterIsInstance(): List { - return filterIsInstanceTo(ArrayList()) -} - -/** - * Returns a sequence containing all elements that are instances of specified type parameter R. - */ -public inline fun Sequence<*>.filterIsInstance(): Sequence { - return filter { it is R } as Sequence -} - -/** - * Returns a list containing all elements that are instances of specified class. - */ -public fun Array<*>.filterIsInstance(klass: Class): List { - return filterIsInstanceTo(ArrayList(), klass) -} - -/** - * Returns a list containing all elements that are instances of specified class. - */ -public fun Iterable<*>.filterIsInstance(klass: Class): List { - return filterIsInstanceTo(ArrayList(), klass) -} - -/** - * Returns a sequence containing all elements that are instances of specified class. - */ -public fun Sequence<*>.filterIsInstance(klass: Class): Sequence { - return filter { klass.isInstance(it) } as Sequence -} - -/** - * Appends all elements that are instances of specified type parameter R to the given [destination]. - */ -public inline fun > Array<*>.filterIsInstanceTo(destination: C): C { - for (element in this) if (element is R) destination.add(element) - return destination -} - -/** - * Appends all elements that are instances of specified type parameter R to the given [destination]. - */ -public inline fun > Iterable<*>.filterIsInstanceTo(destination: C): C { - for (element in this) if (element is R) destination.add(element) - return destination -} - -/** - * Appends all elements that are instances of specified type parameter R to the given [destination]. - */ -public inline fun > Sequence<*>.filterIsInstanceTo(destination: C): C { - for (element in this) if (element is R) destination.add(element) - return destination -} - -/** - * Appends all elements that are instances of specified class to the given [destination]. - */ -public fun , R> Array<*>.filterIsInstanceTo(destination: C, klass: Class): C { - for (element in this) if (klass.isInstance(element)) destination.add(element as R) - return destination -} - -/** - * Appends all elements that are instances of specified class to the given [destination]. - */ -public fun , R> Iterable<*>.filterIsInstanceTo(destination: C, klass: Class): C { - for (element in this) if (klass.isInstance(element)) destination.add(element as R) - return destination -} - -/** - * Appends all elements that are instances of specified class to the given [destination]. - */ -public fun , R> Sequence<*>.filterIsInstanceTo(destination: C, klass: Class): C { - for (element in this) if (klass.isInstance(element)) destination.add(element as R) - return destination -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [array]. - */ -public operator fun BooleanArray.plus(array: BooleanArray): BooleanArray { - val thisSize = size() - val arraySize = array.size() - val result = Arrays.copyOf(this, thisSize + arraySize) - System.arraycopy(array, 0, result, thisSize, arraySize) - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [array]. - */ -public operator fun ByteArray.plus(array: ByteArray): ByteArray { - val thisSize = size() - val arraySize = array.size() - val result = Arrays.copyOf(this, thisSize + arraySize) - System.arraycopy(array, 0, result, thisSize, arraySize) - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [array]. - */ -public operator fun CharArray.plus(array: CharArray): CharArray { - val thisSize = size() - val arraySize = array.size() - val result = Arrays.copyOf(this, thisSize + arraySize) - System.arraycopy(array, 0, result, thisSize, arraySize) - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [array]. - */ -public operator fun DoubleArray.plus(array: DoubleArray): DoubleArray { - val thisSize = size() - val arraySize = array.size() - val result = Arrays.copyOf(this, thisSize + arraySize) - System.arraycopy(array, 0, result, thisSize, arraySize) - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [array]. - */ -public operator fun FloatArray.plus(array: FloatArray): FloatArray { - val thisSize = size() - val arraySize = array.size() - val result = Arrays.copyOf(this, thisSize + arraySize) - System.arraycopy(array, 0, result, thisSize, arraySize) - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [array]. - */ -public operator fun IntArray.plus(array: IntArray): IntArray { - val thisSize = size() - val arraySize = array.size() - val result = Arrays.copyOf(this, thisSize + arraySize) - System.arraycopy(array, 0, result, thisSize, arraySize) - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [array]. - */ -public operator fun LongArray.plus(array: LongArray): LongArray { - val thisSize = size() - val arraySize = array.size() - val result = Arrays.copyOf(this, thisSize + arraySize) - System.arraycopy(array, 0, result, thisSize, arraySize) - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [array]. - */ -public operator fun ShortArray.plus(array: ShortArray): ShortArray { - val thisSize = size() - val arraySize = array.size() - val result = Arrays.copyOf(this, thisSize + arraySize) - System.arraycopy(array, 0, result, thisSize, arraySize) - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [array]. - */ -public operator fun Array.plus(array: Array): Array { - val thisSize = size() - val arraySize = array.size() - val result = Arrays.copyOf(this, thisSize + arraySize) - System.arraycopy(array, 0, result, thisSize, arraySize) - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [collection]. - */ -public operator fun BooleanArray.plus(collection: Collection): BooleanArray { - var index = size() - val result = Arrays.copyOf(this, index + collection.size()) - for (element in collection) result[index++] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [collection]. - */ -public operator fun ByteArray.plus(collection: Collection): ByteArray { - var index = size() - val result = Arrays.copyOf(this, index + collection.size()) - for (element in collection) result[index++] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [collection]. - */ -public operator fun CharArray.plus(collection: Collection): CharArray { - var index = size() - val result = Arrays.copyOf(this, index + collection.size()) - for (element in collection) result[index++] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [collection]. - */ -public operator fun DoubleArray.plus(collection: Collection): DoubleArray { - var index = size() - val result = Arrays.copyOf(this, index + collection.size()) - for (element in collection) result[index++] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [collection]. - */ -public operator fun FloatArray.plus(collection: Collection): FloatArray { - var index = size() - val result = Arrays.copyOf(this, index + collection.size()) - for (element in collection) result[index++] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [collection]. - */ -public operator fun IntArray.plus(collection: Collection): IntArray { - var index = size() - val result = Arrays.copyOf(this, index + collection.size()) - for (element in collection) result[index++] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [collection]. - */ -public operator fun LongArray.plus(collection: Collection): LongArray { - var index = size() - val result = Arrays.copyOf(this, index + collection.size()) - for (element in collection) result[index++] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [collection]. - */ -public operator fun ShortArray.plus(collection: Collection): ShortArray { - var index = size() - val result = Arrays.copyOf(this, index + collection.size()) - for (element in collection) result[index++] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [collection]. - */ -public operator fun Array.plus(collection: Collection): Array { - var index = size() - val result = Arrays.copyOf(this, index + collection.size()) - for (element in collection) 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 = Arrays.copyOf(this, index + 1) - result[index] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public operator fun ByteArray.plus(element: Byte): ByteArray { - val index = size() - val result = Arrays.copyOf(this, index + 1) - result[index] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public operator fun CharArray.plus(element: Char): CharArray { - val index = size() - val result = Arrays.copyOf(this, index + 1) - result[index] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public operator fun DoubleArray.plus(element: Double): DoubleArray { - val index = size() - val result = Arrays.copyOf(this, index + 1) - result[index] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public operator fun FloatArray.plus(element: Float): FloatArray { - val index = size() - val result = Arrays.copyOf(this, index + 1) - result[index] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public operator fun IntArray.plus(element: Int): IntArray { - val index = size() - val result = Arrays.copyOf(this, index + 1) - result[index] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public operator fun LongArray.plus(element: Long): LongArray { - val index = size() - val result = Arrays.copyOf(this, index + 1) - result[index] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public operator fun ShortArray.plus(element: Short): ShortArray { - val index = size() - val result = Arrays.copyOf(this, index + 1) - result[index] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public operator fun Array.plus(element: T): Array { - val index = size() - val result = Arrays.copyOf(this, index + 1) - result[index] = element - return result -} - -/** - * Sorts array or range in array inplace. - */ -public fun Array.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { - Arrays.sort(this, fromIndex, toIndex) -} - -/** - * Sorts array or range in array inplace. - */ -public fun ByteArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { - Arrays.sort(this, fromIndex, toIndex) -} - -/** - * Sorts array or range in array inplace. - */ -public fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { - Arrays.sort(this, fromIndex, toIndex) -} - -/** - * Sorts array or range in array inplace. - */ -public fun DoubleArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { - Arrays.sort(this, fromIndex, toIndex) -} - -/** - * Sorts array or range in array inplace. - */ -public fun FloatArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { - Arrays.sort(this, fromIndex, toIndex) -} - -/** - * Sorts array or range in array inplace. - */ -public fun IntArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { - Arrays.sort(this, fromIndex, toIndex) -} - -/** - * Sorts array or range in array inplace. - */ -public fun LongArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { - Arrays.sort(this, fromIndex, toIndex) -} - -/** - * Sorts array or range in array inplace. - */ -public fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { - Arrays.sort(this, fromIndex, toIndex) -} - -/** - * Sorts array or range in array inplace. - */ -public fun Array.sortWith(comparator: Comparator, fromIndex: Int = 0, toIndex: Int = size()): Unit { - Arrays.sort(this, fromIndex, toIndex, comparator) -} - -/** - * 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] - return result as Array -} - -/** - * 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] - 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] - 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] - 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] - return result as Array -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun IntArray.toTypedArray(): Array { - val result = arrayOfNulls(size()) - for (index in indices) - result[index] = this[index] - 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] - 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] - return result as Array -} - diff --git a/libraries/stdlib/src/generated/_Strings.kt b/libraries/stdlib/src/generated/_Strings.kt index 81f939da5d2..1e303575980 100644 --- a/libraries/stdlib/src/generated/_Strings.kt +++ b/libraries/stdlib/src/generated/_Strings.kt @@ -1,3 +1,6 @@ +@file:kotlin.jvm.JvmMultifileClass +@file:kotlin.jvm.JvmName("StringsKt") + package kotlin // @@ -5,327 +8,774 @@ package kotlin // See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib // -import kotlin.platform.* import java.util.* import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js /** - * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). + * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. */ -public fun Array.joinTo(buffer: A, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((T) -> String)? = null): A { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - val text = if (transform != null) transform(element) else if (element == null) "null" else element.toString() - buffer.append(text) - } else break +public fun String.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 collection. + */ +public inline fun String.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 collection. + */ +public fun String.elementAtOrNull(index: Int): Char? { + return if (index >= 0 && index <= lastIndex) get(index) else null +} + +/** + * Returns the first character matching the given [predicate], or `null` if character was not found. + */ +public inline fun String.find(predicate: (Char) -> Boolean): Char? { + return firstOrNull(predicate) +} + +/** + * Returns the last character matching the given [predicate], or `null` if no such character was found. + */ +public inline fun String.findLast(predicate: (Char) -> Boolean): Char? { + return lastOrNull(predicate) +} + +/** + * Returns first character. + * @throws [NoSuchElementException] if the string is empty. + */ +public fun String.first(): Char { + if (isEmpty()) + throw NoSuchElementException("Collection is empty.") + return this[0] +} + +/** + * Returns the first character matching the given [predicate]. + * @throws [NoSuchElementException] if no such character is found. + */ +public inline fun String.first(predicate: (Char) -> Boolean): Char { + for (element in this) if (predicate(element)) return element + throw NoSuchElementException("No element matching predicate was found.") +} + +/** + * Returns the first character, or `null` if string is empty. + */ +public fun String.firstOrNull(): Char? { + return if (isEmpty()) null else this[0] +} + +/** + * Returns the first character matching the given [predicate], or `null` if character was not found. + */ +public inline fun String.firstOrNull(predicate: (Char) -> Boolean): Char? { + for (element in this) if (predicate(element)) return element + return null +} + +/** + * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. + */ +public inline fun String.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 collection. + */ +public fun String.getOrNull(index: Int): Char? { + return if (index >= 0 && index <= lastIndex) get(index) else null +} + +/** + * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element. + */ +public inline fun String.indexOfFirst(predicate: (Char) -> Boolean): Int { + for (index in indices) { + if (predicate(this[index])) { + return index + } } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) - return buffer + return -1 } /** - * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). + * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element. */ -public fun BooleanArray.joinTo(buffer: A, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Boolean) -> String)? = null): A { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - val text = if (transform != null) transform(element) else element.toString() - buffer.append(text) - } else break +public inline fun String.indexOfLast(predicate: (Char) -> Boolean): Int { + for (index in indices.reversed()) { + if (predicate(this[index])) { + return index + } } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) - return buffer + return -1 } /** - * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). + * "Returns the last character. + * @throws [NoSuchElementException] if the string is empty. */ -public fun ByteArray.joinTo(buffer: A, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Byte) -> String)? = null): A { - buffer.append(prefix) - var count = 0 +public fun String.last(): Char { + if (isEmpty()) + throw NoSuchElementException("Collection is empty.") + return this[lastIndex] +} + +/** + * "Returns the last character matching the given [predicate]. + * @throws [NoSuchElementException] if no such character is found. + */ +public inline fun String.last(predicate: (Char) -> Boolean): Char { + var last: Char? = null + var found = false for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - val text = if (transform != null) transform(element) else element.toString() - buffer.append(text) - } else break + if (predicate(element)) { + last = element + found = true + } } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) - return buffer + if (!found) throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") + return last as Char } /** - * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). + * Returns the last character, or `null` if the string is empty. */ -public fun CharArray.joinTo(buffer: A, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Char) -> String)? = null): A { - buffer.append(prefix) - var count = 0 +public fun String.lastOrNull(): Char? { + return if (isEmpty()) null else this[length() - 1] +} + +/** + * Returns the last character matching the given [predicate], or `null` if no such character was found. + */ +public inline fun String.lastOrNull(predicate: (Char) -> Boolean): Char? { + var last: Char? = null for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - val text = if (transform != null) transform(element) else element.toString() - buffer.append(text) - } else break + if (predicate(element)) { + last = element + } } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) - return buffer + return last } /** - * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). + * Returns the single character, or throws an exception if the string is empty or has more than one character. */ -public fun DoubleArray.joinTo(buffer: A, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Double) -> String)? = null): A { - buffer.append(prefix) - var count = 0 +public fun String.single(): Char { + return when (length()) { + 0 -> throw NoSuchElementException("Collection is empty.") + 1 -> this[0] + else -> throw IllegalArgumentException("Collection has more than one element.") + } +} + +/** + * Returns the single character matching the given [predicate], or throws exception if there is no or more than one matching character. + */ +public inline fun String.single(predicate: (Char) -> Boolean): Char { + var single: Char? = null + var found = false for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - val text = if (transform != null) transform(element) else element.toString() - buffer.append(text) - } else break + if (predicate(element)) { + if (found) throw IllegalArgumentException("Collection contains more than one matching element.") + single = element + found = true + } } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) - return buffer + if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") + return single as Char } /** - * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). + * Returns the single character, or `null` if the string is empty or has more than one character. */ -public fun FloatArray.joinTo(buffer: A, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Float) -> String)? = null): A { - buffer.append(prefix) - var count = 0 +public fun String.singleOrNull(): Char? { + return if (length() == 1) this[0] else null +} + +/** + * Returns the single character matching the given [predicate], or `null` if character was not found or more than one character was found. + */ +public inline fun String.singleOrNull(predicate: (Char) -> Boolean): Char? { + var single: Char? = null + var found = false for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - val text = if (transform != null) transform(element) else element.toString() - buffer.append(text) - } else break + if (predicate(element)) { + if (found) return null + single = element + found = true + } } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) - return buffer + if (!found) return null + return single } /** - * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). + * Returns a string with the first [n] characters removed. */ -public fun IntArray.joinTo(buffer: A, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Int) -> String)? = null): A { - buffer.append(prefix) - var count = 0 +public fun String.drop(n: Int): String { + return substring(Math.min(n, length())) +} + +/** + * Returns a string with the last [n] characters removed. + */ +public fun String.dropLast(n: Int): String { + require(n >= 0, { "Requested character count $n is less than zero." }) + return take((length() - n).coerceAtLeast(0)) +} + +/** + * Returns a string containing all characters except last characters that satisfy the given [predicate]. + */ +public inline fun String.dropLastWhile(predicate: (Char) -> Boolean): String { + return trimEnd(predicate) +} + +/** + * Returns a string containing all characters except first characters that satisfy the given [predicate]. + */ +public inline fun String.dropWhile(predicate: (Char) -> Boolean): String { + return trimStart(predicate) +} + +/** + * Returns a string containing only those characters from the original string that match the given [predicate]. + */ +public inline fun String.filter(predicate: (Char) -> Boolean): String { + return filterTo(StringBuilder(), predicate).toString() +} + +/** + * Returns a string containing only those characters from the original string that do not match the given [predicate]. + */ +public inline fun String.filterNot(predicate: (Char) -> Boolean): String { + return filterNotTo(StringBuilder(), predicate).toString() +} + +/** + * Appends all characters not matching the given [predicate] to the given [destination]. + */ +public inline fun String.filterNotTo(destination: C, predicate: (Char) -> Boolean): C { + for (element in this) if (!predicate(element)) destination.append(element) + return destination +} + +/** + * Appends all characters matching the given [predicate] to the given [destination]. + */ +public inline fun String.filterTo(destination: C, predicate: (Char) -> Boolean): C { + for (index in 0..length() - 1) { + val element = get(index) + if (predicate(element)) destination.append(element) + } + return destination +} + +/** + * Returns a string containing characters at indices at the specified [indices]. + */ +public fun String.slice(indices: IntRange): String { + if (indices.isEmpty()) return "" + return substring(indices) +} + +/** + * Returns a string containing characters at specified [indices]. + */ +public fun String.slice(indices: Iterable): String { + val size = indices.collectionSizeOrDefault(10) + if (size == 0) return "" + val result = StringBuilder(size) + for (i in indices) { + result.append(get(i)) + } + return result.toString() +} + +/** + * Returns a string containing the first [n] characters from this string, or the entire string if this string is shorter. + */ +public fun String.take(n: Int): String { + require(n >= 0, { "Requested character count $n is less than zero." }) + return substring(0, Math.min(n, length())) +} + +/** + * Returns a string containing the last [n] characters from this string, or the entire string if this string is shorter. + */ +public fun String.takeLast(n: Int): String { + require(n >= 0, { "Requested character count $n is less than zero." }) + val length = length() + return substring(length - Math.min(n, length), length) +} + +/** + * Returns a string containing last characters that satisfy the given [predicate]. + */ +public inline fun String.takeLastWhile(predicate: (Char) -> Boolean): String { + for (index in lastIndex downTo 0) { + if (!predicate(this[index])) { + return substring(index + 1) + } + } + return this +} + +/** + * Returns a string containing the first characters that satisfy the given [predicate]. + */ +public inline fun String.takeWhile(predicate: (Char) -> Boolean): String { + for (index in 0..length() - 1) + if (!predicate(get(index))) { + return substring(0, index) + } + return this +} + +/** + * Returns a list with elements in reversed order. + */ +@Deprecated("reverse will change its behavior soon. Use reversed() instead.", ReplaceWith("reversed()")) +public fun String.reverse(): String { + return reversed() +} + +/** + * Returns a string with characters in reversed order. + */ +public fun String.reversed(): String { + return StringBuilder().append(this).reverse().toString() +} + +/** + * Returns an [ArrayList] of all elements. + */ +public fun String.toArrayList(): ArrayList { + return toCollection(ArrayList(length())) +} + +/** + * Appends all elements to the given [collection]. + */ +public fun > String.toCollection(collection: C): C { + for (item in this) { + collection.add(item) + } + return collection +} + +/** + * Returns a [HashSet] of all elements. + */ +public fun String.toHashSet(): HashSet { + return toCollection(HashSet(mapCapacity(length()))) +} + +/** + * Returns a [LinkedList] containing all elements. + */ +public fun String.toLinkedList(): LinkedList { + return toCollection(LinkedList()) +} + +/** + * Returns a [List] containing all elements. + */ +public fun String.toList(): List { + return this.toArrayList() +} + +/** + * Returns Map containing the values from the given collection indexed by [selector]. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. + */ +public inline fun String.toMap(selector: (Char) -> K): Map { + val capacity = (length()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - val text = if (transform != null) transform(element) else element.toString() - buffer.append(text) - } else break + result.put(selector(element), element) } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) - return buffer + return result } /** - * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). + * Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection. + * If any two elements would have the same key returned by [selector] the last one gets added to the map. */ -public fun LongArray.joinTo(buffer: A, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Long) -> String)? = null): A { - buffer.append(prefix) - var count = 0 +public inline fun String.toMap(selector: (Char) -> K, transform: (Char) -> V): Map { + val capacity = (length()/.75f) + 1 + val result = LinkedHashMap(Math.max(capacity.toInt(), 16)) for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - val text = if (transform != null) transform(element) else element.toString() - buffer.append(text) - } else break + result.put(selector(element), transform(element)) } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) - return buffer + return result } /** - * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). + * Returns a [Set] of all elements. */ -public fun ShortArray.joinTo(buffer: A, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Short) -> String)? = null): A { - buffer.append(prefix) - var count = 0 +public fun String.toSet(): Set { + return toCollection(LinkedHashSet(mapCapacity(length()))) +} + +/** + * Returns a [SortedSet] of all elements. + */ +public fun String.toSortedSet(): SortedSet { + return toCollection(TreeSet()) +} + +/** + * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original collection. + */ +public inline fun String.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 collection, to the given [destination]. + */ +public inline fun > String.flatMapTo(destination: C, transform: (Char) -> Iterable): C { for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - val text = if (transform != null) transform(element) else element.toString() - buffer.append(text) - } else break + val list = transform(element) + destination.addAll(list) } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) - return buffer + return destination } /** - * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). + * Returns a map of the elements in original collection grouped by the result of given [toKey] function. */ -public fun Iterable.joinTo(buffer: A, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((T) -> String)? = null): A { - buffer.append(prefix) - var count = 0 +public inline fun String.groupBy(toKey: (Char) -> K): Map> { + return groupByTo(LinkedHashMap>(), toKey) +} + +/** + * Appends elements from original collection grouped by the result of given [toKey] function to the given [map]. + */ +public inline fun String.groupByTo(map: MutableMap>, toKey: (Char) -> K): Map> { for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - val text = if (transform != null) transform(element) else if (element == null) "null" else element.toString() - buffer.append(text) - } else break + val key = toKey(element) + val list = map.getOrPut(key) { ArrayList() } + list.add(element) } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) - return buffer + return map } /** - * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). + * Returns a list containing the results of applying the given [transform] function to each element of the original collection. */ -public fun Sequence.joinTo(buffer: A, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((T) -> String)? = null): A { - buffer.append(prefix) +public inline fun String.map(transform: (Char) -> R): List { + return mapTo(ArrayList(length()), transform) +} + +/** + * Returns a list containing the results of applying the given [transform] function to each element and its index of the original collection. + */ +public inline fun String.mapIndexed(transform: (Int, Char) -> R): List { + return mapIndexedTo(ArrayList(length()), transform) +} + +/** + * Appends transformed elements and their indices of the original collection using the given [transform] function + * to the given [destination]. + */ +public inline fun > String.mapIndexedTo(destination: C, transform: (Int, Char) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} + +/** + * Appends transformed elements of the original collection using the given [transform] function + * to the given [destination]. + */ +public inline fun > String.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 collection. + */ +public fun String.withIndex(): Iterable> { + return IndexingIterable { iterator() } +} + +/** + * Returns a list containing pairs of each element of the original collection and their index. + */ +@Deprecated("Use withIndex() instead.") +public fun String.withIndices(): List> { + var index = 0 + return mapTo(ArrayList>(), { index++ to it }) +} + +/** + * Returns `true` if all elements match the given [predicate]. + */ +public inline fun String.all(predicate: (Char) -> Boolean): Boolean { + for (element in this) if (!predicate(element)) return false + return true +} + +/** + * Returns `true` if collection has at least one element. + */ +public fun String.any(): Boolean { + for (element in this) return true + return false +} + +/** + * Returns `true` if at least one element matches the given [predicate]. + */ +public inline fun String.any(predicate: (Char) -> Boolean): Boolean { + for (element in this) if (predicate(element)) return true + return false +} + +/** + * Returns the length of this string. + */ +public fun String.count(): Int { + return length() +} + +/** + * Returns the number of elements matching the given [predicate]. + */ +public inline fun String.count(predicate: (Char) -> Boolean): Int { var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - val text = if (transform != null) transform(element) else if (element == null) "null" else element.toString() - buffer.append(text) - } else break + for (element in this) if (predicate(element)) count++ + return count +} + +/** + * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. + */ +public inline fun String.fold(initial: R, operation: (R, Char) -> R): R { + var accumulator = initial + for (element in this) accumulator = operation(accumulator, element) + return accumulator +} + +/** + * Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. + */ +public inline fun String.foldRight(initial: R, operation: (Char, R) -> R): R { + var index = lastIndex + var accumulator = initial + while (index >= 0) { + accumulator = operation(get(index--), accumulator) } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) - return buffer + return accumulator } /** - * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). + * Performs the given [operation] on each element. */ -public fun Array.joinToString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((T) -> String)? = null): String { - return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() +public inline fun String.forEach(operation: (Char) -> Unit): Unit { + for (element in this) operation(element) } /** - * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). + * Performs the given [operation] on each element, providing sequential index with the element. */ -public fun BooleanArray.joinToString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Boolean) -> String)? = null): String { - return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() +public inline fun String.forEachIndexed(operation: (Int, Char) -> Unit): Unit { + var index = 0 + for (item in this) operation(index++, item) } /** - * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). + * Returns the largest element or `null` if there are no elements. */ -public fun ByteArray.joinToString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Byte) -> String)? = null): String { - return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() +public fun String.max(): Char? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var max = iterator.next() + while (iterator.hasNext()) { + val e = iterator.next() + if (max < e) max = e + } + return max } /** - * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). + * Returns the first element yielding the largest value of the given function or `null` if there are no elements. */ -public fun CharArray.joinToString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Char) -> String)? = null): String { - return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() +public inline fun > String.maxBy(f: (Char) -> R): Char? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var maxElem = iterator.next() + var maxValue = f(maxElem) + while (iterator.hasNext()) { + val e = iterator.next() + val v = f(e) + if (maxValue < v) { + maxElem = e + maxValue = v + } + } + return maxElem } /** - * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). + * Returns the smallest element or `null` if there are no elements. */ -public fun DoubleArray.joinToString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Double) -> String)? = null): String { - return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() +public fun String.min(): Char? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var min = iterator.next() + while (iterator.hasNext()) { + val e = iterator.next() + if (min > e) min = e + } + return min } /** - * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). + * Returns the first element yielding the smallest value of the given function or `null` if there are no elements. */ -public fun FloatArray.joinToString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Float) -> String)? = null): String { - return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() +public inline fun > String.minBy(f: (Char) -> R): Char? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var minElem = iterator.next() + var minValue = f(minElem) + while (iterator.hasNext()) { + val e = iterator.next() + val v = f(e) + if (minValue > v) { + minElem = e + minValue = v + } + } + return minElem } /** - * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). + * Returns `true` if collection has no elements. */ -public fun IntArray.joinToString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Int) -> String)? = null): String { - return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() +public fun String.none(): Boolean { + for (element in this) return false + return true } /** - * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). + * Returns `true` if no elements match the given [predicate]. */ -public fun LongArray.joinToString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Long) -> String)? = null): String { - return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() +public inline fun String.none(predicate: (Char) -> Boolean): Boolean { + for (element in this) if (predicate(element)) return false + return true } /** - * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). + * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. */ -public fun ShortArray.joinToString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((Short) -> String)? = null): String { - return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() +public inline fun String.reduce(operation: (Char, Char) -> Char): Char { + val iterator = this.iterator() + if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") + var accumulator = iterator.next() + while (iterator.hasNext()) { + accumulator = operation(accumulator, iterator.next()) + } + return accumulator } /** - * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). + * Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. */ -public fun Iterable.joinToString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((T) -> String)? = null): String { - return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() +public inline fun String.reduceRight(operation: (Char, Char) -> Char): Char { + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.") + var accumulator = get(index--) + while (index >= 0) { + accumulator = operation(get(index--), accumulator) + } + return accumulator } /** - * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied. - * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit] - * elements will be appended, followed by the [truncated] string (which defaults to "..."). + * Returns the sum of all values produced by [transform] function from characters in the string. */ -public fun Sequence.joinToString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...", transform: ((T) -> String)? = null): String { - return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() +public inline fun String.sumBy(transform: (Char) -> Int): Int { + var sum: Int = 0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Returns the sum of all values produced by [transform] function from characters in the string. + */ +public inline fun String.sumByDouble(transform: (Char) -> Double): Double { + var sum: Double = 0.0 + for (element in this) { + sum += transform(element) + } + return sum +} + +/** + * Splits the original string into pair of strings, + * where *first* string contains characters for which [predicate] yielded `true`, + * while *second* string contains characters for which [predicate] yielded `false`. + */ +public inline fun String.partition(predicate: (Char) -> Boolean): Pair { + val first = StringBuilder() + val second = StringBuilder() + for (element in this) { + if (predicate(element)) { + first.append(element) + } else { + second.append(element) + } + } + return Pair(first.toString(), second.toString()) +} + +/** + * Returns a list of pairs built from characters of both strings with same indexes. List has length of shortest collection. + */ +public fun String.zip(other: String): List> { + val first = iterator() + val second = other.iterator() + val list = ArrayList>(length()) + while (first.hasNext() && second.hasNext()) { + list.add(first.next() to second.next()) + } + return list +} + +/** + * Returns a sequence from the given collection. + */ +public fun String.asSequence(): Sequence { + if (isEmpty()) return emptySequence() + return object : Sequence { + override fun iterator(): Iterator { + return this@asSequence.iterator() + } + } +} + +/** + * Returns a sequence from the given collection + */ +@Deprecated("Use asSequence() instead", ReplaceWith("asSequence()")) +public fun String.sequence(): Sequence { + return asSequence() }