Redistribute generated code between files.

This commit is contained in:
Ilya Gorbunov
2015-09-16 15:51:20 +03:00
parent 3e018f3a9f
commit 60c2f0fe0b
19 changed files with 15648 additions and 15641 deletions
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -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<out Boolean>.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<Boolean>.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<out Byte>.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<Byte>.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<out Char>.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<Char>.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<out Double>.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<Double>.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<out Float>.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<Float>.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<out Int>.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<Int>.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<out Long>.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<Long>.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<out Short>.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<Short>.toShortArray(): ShortArray {
val result = ShortArray(size())
var index = 0
for (element in this)
result[index++] = element
return result
}
File diff suppressed because it is too large Load Diff
@@ -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: Comparable<T>> 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: Comparable<T>> 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: Comparable<T>> 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: Comparable<T>> T.coerceIn(range: Range<T>): 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>): 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>): 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>): 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>): 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>): 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>): 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
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
-55
View File
@@ -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 <T : Any> Array<out T?>.requireNoNulls(): Array<out T> {
for (element in this) {
if (element == null) {
throw IllegalArgumentException("null element found in $this.")
}
}
return this as Array<out T>
}
/**
* Returns an original collection containing all the non-`null` elements, throwing an [IllegalArgumentException] if there are any `null` elements.
*/
public fun <T : Any> Iterable<T?>.requireNoNulls(): Iterable<T> {
for (element in this) {
if (element == null) {
throw IllegalArgumentException("null element found in $this.")
}
}
return this as Iterable<T>
}
/**
* Returns an original collection containing all the non-`null` elements, throwing an [IllegalArgumentException] if there are any `null` elements.
*/
public fun <T : Any> List<T?>.requireNoNulls(): List<T> {
for (element in this) {
if (element == null) {
throw IllegalArgumentException("null element found in $this.")
}
}
return this as List<T>
}
/**
* Returns an original collection containing all the non-`null` elements, throwing an [IllegalArgumentException] if there are any `null` elements.
*/
public fun <T : Any> Sequence<T?>.requireNoNulls(): Sequence<T> {
return map { it ?: throw IllegalArgumentException("null element found in $this.") }
}
File diff suppressed because it is too large Load Diff
+190
View File
@@ -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 <K, V> Map<K, V>.toList(): List<Pair<K, V>> {
val result = ArrayList<Pair<K, V>>(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 <K, V, R> Map<K, V>.flatMap(transform: (Map.Entry<K, V>) -> Iterable<R>): List<R> {
return flatMapTo(ArrayList<R>(), 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 <K, V, R, C : MutableCollection<in R>> Map<K, V>.flatMapTo(destination: C, transform: (Map.Entry<K, V>) -> Iterable<R>): 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 <K, V, R> Map<K, V>.map(transform: (Map.Entry<K, V>) -> R): List<R> {
return mapTo(ArrayList<R>(size()), transform)
}
/**
* Appends transformed elements and their indices of the original collection using the given [transform] function
* to the given [destination].
*/
public inline fun <K, V, R, C : MutableCollection<in R>> Map<K, V>.mapIndexedTo(destination: C, transform: (Int, Map.Entry<K, V>) -> 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 <K, V, R, C : MutableCollection<in R>> Map<K, V>.mapTo(destination: C, transform: (Map.Entry<K, V>) -> R): C {
for (item in this)
destination.add(transform(item))
return destination
}
/**
* Returns `true` if all elements match the given [predicate].
*/
public inline fun <K, V> Map<K, V>.all(predicate: (Map.Entry<K, V>) -> Boolean): Boolean {
for (element in this) if (!predicate(element)) return false
return true
}
/**
* Returns `true` if collection has at least one element.
*/
public fun <K, V> Map<K, V>.any(): Boolean {
for (element in this) return true
return false
}
/**
* Returns `true` if at least one element matches the given [predicate].
*/
public inline fun <K, V> Map<K, V>.any(predicate: (Map.Entry<K, V>) -> Boolean): Boolean {
for (element in this) if (predicate(element)) return true
return false
}
/**
* Returns the number of elements in this collection.
*/
public fun <K, V> Map<K, V>.count(): Int {
return size()
}
/**
* Returns the number of elements matching the given [predicate].
*/
public inline fun <K, V> Map<K, V>.count(predicate: (Map.Entry<K, V>) -> 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 <K, V> Map<K, V>.forEach(operation: (Map.Entry<K, V>) -> 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 <K, V, R : Comparable<R>> Map<K, V>.maxBy(f: (Map.Entry<K, V>) -> R): Map.Entry<K, V>? {
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 <K, V, R : Comparable<R>> Map<K, V>.minBy(f: (Map.Entry<K, V>) -> R): Map.Entry<K, V>? {
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 <K, V> Map<K, V>.none(): Boolean {
for (element in this) return false
return true
}
/**
* Returns `true` if no elements match the given [predicate].
*/
public inline fun <K, V> Map<K, V>.none(predicate: (Map.Entry<K, V>) -> Boolean): Boolean {
for (element in this) if (predicate(element)) return false
return true
}
/**
* Returns a sequence from the given collection.
*/
public fun <K, V> Map<K, V>.asSequence(): Sequence<Map.Entry<K, V>> {
return object : Sequence<Map.Entry<K, V>> {
override fun iterator(): Iterator<Map.Entry<K, V>> {
return this@asSequence.iterator()
}
}
}
/**
* Returns a sequence from the given collection
*/
@Deprecated("Use asSequence() instead", ReplaceWith("asSequence()"))
public fun <K, V> Map<K, V>.sequence(): Sequence<Map.Entry<K, V>> {
return asSequence()
}
-672
View File
@@ -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<out Byte>.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<out Double>.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<out Float>.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<out Int>.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<out Long>.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<out Short>.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<Byte>.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<Double>.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<Float>.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<Int>.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<Long>.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<Short>.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<Byte>.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<Double>.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<Float>.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<Int>.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<Long>.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<Short>.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<out Byte>.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<out Double>.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<out Float>.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<out Int>.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<out Long>.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<out Short>.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<Byte>.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<Double>.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<Float>.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<Int>.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<Long>.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<Short>.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<Byte>.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<Double>.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<Float>.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<Int>.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<Long>.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<Short>.sum(): Int {
val iterator = iterator()
var sum: Int = 0
while (iterator.hasNext()) {
sum += iterator.next()
}
return sum
}
File diff suppressed because it is too large Load Diff
+297 -1
View File
@@ -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: Comparable<T>> 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: Comparable<T>> 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: Comparable<T>> 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: Comparable<T>> T.coerceIn(range: Range<T>): 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>): 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>): 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>): 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>): 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>): 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>): 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
}
File diff suppressed because it is too large Load Diff
+52 -585
View File
@@ -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 <T> Array<out T>.distinct(): List<T> {
return this.toMutableSet().toList()
public operator fun <T> Set<T>.minus(array: Array<out T>): Set<T> {
val result = LinkedHashSet<T>(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<Boolean> {
return this.toMutableSet().toList()
public operator fun <T> Set<T>.minus(collection: Iterable<T>): Set<T> {
val other = collection.convertToSetForSetOperationWith(this)
if (other.isEmpty())
return this.toSet()
if (other is Set)
return this.filterNotTo(LinkedHashSet<T>()) { it in other }
val result = LinkedHashSet<T>(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<Byte> {
return this.toMutableSet().toList()
public operator fun <T> Set<T>.minus(element: T): Set<T> {
val result = LinkedHashSet<T>(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<Char> {
return this.toMutableSet().toList()
public operator fun <T> Set<T>.minus(sequence: Sequence<T>): Set<T> {
val result = LinkedHashSet<T>(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<Double> {
return this.toMutableSet().toList()
public operator fun <T> Set<T>.plus(array: Array<out T>): Set<T> {
val result = LinkedHashSet<T>(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<Float> {
return this.toMutableSet().toList()
public operator fun <T> Set<T>.plus(collection: Iterable<T>): Set<T> {
val result = LinkedHashSet<T>(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<Int> {
return this.toMutableSet().toList()
public operator fun <T> Set<T>.plus(element: T): Set<T> {
val result = LinkedHashSet<T>(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<Long> {
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<Short> {
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 <T> Iterable<T>.distinct(): List<T> {
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 <T> Sequence<T>.distinct(): Sequence<T> {
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 <T, K> Array<out T>.distinctBy(keySelector: (T) -> K): List<T> {
val set = HashSet<K>()
val list = ArrayList<T>()
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 <K> BooleanArray.distinctBy(keySelector: (Boolean) -> K): List<Boolean> {
val set = HashSet<K>()
val list = ArrayList<Boolean>()
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 <K> ByteArray.distinctBy(keySelector: (Byte) -> K): List<Byte> {
val set = HashSet<K>()
val list = ArrayList<Byte>()
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 <K> CharArray.distinctBy(keySelector: (Char) -> K): List<Char> {
val set = HashSet<K>()
val list = ArrayList<Char>()
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 <K> DoubleArray.distinctBy(keySelector: (Double) -> K): List<Double> {
val set = HashSet<K>()
val list = ArrayList<Double>()
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 <K> FloatArray.distinctBy(keySelector: (Float) -> K): List<Float> {
val set = HashSet<K>()
val list = ArrayList<Float>()
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 <K> IntArray.distinctBy(keySelector: (Int) -> K): List<Int> {
val set = HashSet<K>()
val list = ArrayList<Int>()
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 <K> LongArray.distinctBy(keySelector: (Long) -> K): List<Long> {
val set = HashSet<K>()
val list = ArrayList<Long>()
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 <K> ShortArray.distinctBy(keySelector: (Short) -> K): List<Short> {
val set = HashSet<K>()
val list = ArrayList<Short>()
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 <T, K> Iterable<T>.distinctBy(keySelector: (T) -> K): List<T> {
val set = HashSet<K>()
val list = ArrayList<T>()
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 <T, K> Sequence<T>.distinctBy(keySelector: (T) -> K): Sequence<T> {
return DistinctSequence(this, keySelector)
}
/**
* Returns a set containing all elements that are contained by both this set and the specified collection.
*/
public fun <T> Array<out T>.intersect(other: Iterable<T>): Set<T> {
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<Boolean>): Set<Boolean> {
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<Byte>): Set<Byte> {
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<Char>): Set<Char> {
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<Double>): Set<Double> {
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<Float>): Set<Float> {
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<Int>): Set<Int> {
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<Long>): Set<Long> {
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<Short>): Set<Short> {
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 <T> Iterable<T>.intersect(other: Iterable<T>): Set<T> {
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 <T> Array<out T>.subtract(other: Iterable<T>): Set<T> {
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<Boolean>): Set<Boolean> {
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<Byte>): Set<Byte> {
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<Char>): Set<Char> {
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<Double>): Set<Double> {
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<Float>): Set<Float> {
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<Int>): Set<Int> {
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<Long>): Set<Long> {
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<Short>): Set<Short> {
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 <T> Iterable<T>.subtract(other: Iterable<T>): Set<T> {
val set = this.toMutableSet()
set.removeAll(other)
return set
}
/**
* Returns a mutable set containing all distinct elements from the given collection.
*/
public fun <T> Array<out T>.toMutableSet(): MutableSet<T> {
val set = LinkedHashSet<T>(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<Boolean> {
val set = LinkedHashSet<Boolean>(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<Byte> {
val set = LinkedHashSet<Byte>(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<Char> {
val set = LinkedHashSet<Char>(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<Double> {
val set = LinkedHashSet<Double>(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<Float> {
val set = LinkedHashSet<Float>(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<Int> {
val set = LinkedHashSet<Int>(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<Long> {
val set = LinkedHashSet<Long>(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<Short> {
val set = LinkedHashSet<Short>(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 <T> Iterable<T>.toMutableSet(): MutableSet<T> {
return when (this) {
is Collection<T> -> LinkedHashSet(this)
else -> toCollection(LinkedHashSet<T>())
}
}
/**
* Returns a mutable set containing all distinct elements from the given sequence.
*/
public fun <T> Sequence<T>.toMutableSet(): MutableSet<T> {
val set = LinkedHashSet<T>()
for (item in this) set.add(item)
return set
}
/**
* Returns a set containing all distinct elements from both collections.
*/
public fun <T> Array<out T>.union(other: Iterable<T>): Set<T> {
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<Boolean>): Set<Boolean> {
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<Byte>): Set<Byte> {
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<Char>): Set<Char> {
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<Double>): Set<Double> {
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<Float>): Set<Float> {
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<Int>): Set<Int> {
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<Long>): Set<Long> {
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<Short>): Set<Short> {
val set = this.toMutableSet()
set.addAll(other)
return set
}
/**
* Returns a set containing all distinct elements from both collections.
*/
public fun <T> Iterable<T>.union(other: Iterable<T>): Set<T> {
val set = this.toMutableSet()
set.addAll(other)
return set
public operator fun <T> Set<T>.plus(sequence: Sequence<T>): Set<T> {
val result = LinkedHashSet<T>(mapCapacity(this.size() * 2))
result.addAll(this)
result.addAll(sequence)
return result
}
@@ -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 <T> Array<out T>.toArrayList(): ArrayList<T> {
return this.asList().toArrayList()
}
/**
* Returns an [ArrayList] of all elements.
*/
public fun BooleanArray.toArrayList(): ArrayList<Boolean> {
val list = ArrayList<Boolean>(size())
for (item in this) list.add(item)
return list
}
/**
* Returns an [ArrayList] of all elements.
*/
public fun ByteArray.toArrayList(): ArrayList<Byte> {
val list = ArrayList<Byte>(size())
for (item in this) list.add(item)
return list
}
/**
* Returns an [ArrayList] of all elements.
*/
public fun CharArray.toArrayList(): ArrayList<Char> {
val list = ArrayList<Char>(size())
for (item in this) list.add(item)
return list
}
/**
* Returns an [ArrayList] of all elements.
*/
public fun DoubleArray.toArrayList(): ArrayList<Double> {
val list = ArrayList<Double>(size())
for (item in this) list.add(item)
return list
}
/**
* Returns an [ArrayList] of all elements.
*/
public fun FloatArray.toArrayList(): ArrayList<Float> {
val list = ArrayList<Float>(size())
for (item in this) list.add(item)
return list
}
/**
* Returns an [ArrayList] of all elements.
*/
public fun IntArray.toArrayList(): ArrayList<Int> {
val list = ArrayList<Int>(size())
for (item in this) list.add(item)
return list
}
/**
* Returns an [ArrayList] of all elements.
*/
public fun LongArray.toArrayList(): ArrayList<Long> {
val list = ArrayList<Long>(size())
for (item in this) list.add(item)
return list
}
/**
* Returns an [ArrayList] of all elements.
*/
public fun ShortArray.toArrayList(): ArrayList<Short> {
val list = ArrayList<Short>(size())
for (item in this) list.add(item)
return list
}
/**
* Returns an [ArrayList] of all elements.
*/
public fun <T> Collection<T>.toArrayList(): ArrayList<T> {
return ArrayList(this)
}
/**
* Returns an [ArrayList] of all elements.
*/
public fun <T> Iterable<T>.toArrayList(): ArrayList<T> {
if (this is Collection<T>)
return this.toArrayList()
return toCollection(ArrayList<T>())
}
/**
* Returns an [ArrayList] of all elements.
*/
public fun <T> Sequence<T>.toArrayList(): ArrayList<T> {
return toCollection(ArrayList<T>())
}
/**
* Returns an [ArrayList] of all elements.
*/
public fun String.toArrayList(): ArrayList<Char> {
return toCollection(ArrayList<Char>(length()))
}
/**
* Appends all elements to the given [collection].
*/
public fun <T, C : MutableCollection<in T>> Array<out T>.toCollection(collection: C): C {
for (item in this) {
collection.add(item)
}
return collection
}
/**
* Appends all elements to the given [collection].
*/
public fun <C : MutableCollection<in Boolean>> BooleanArray.toCollection(collection: C): C {
for (item in this) {
collection.add(item)
}
return collection
}
/**
* Appends all elements to the given [collection].
*/
public fun <C : MutableCollection<in Byte>> ByteArray.toCollection(collection: C): C {
for (item in this) {
collection.add(item)
}
return collection
}
/**
* Appends all elements to the given [collection].
*/
public fun <C : MutableCollection<in Char>> CharArray.toCollection(collection: C): C {
for (item in this) {
collection.add(item)
}
return collection
}
/**
* Appends all elements to the given [collection].
*/
public fun <C : MutableCollection<in Double>> DoubleArray.toCollection(collection: C): C {
for (item in this) {
collection.add(item)
}
return collection
}
/**
* Appends all elements to the given [collection].
*/
public fun <C : MutableCollection<in Float>> FloatArray.toCollection(collection: C): C {
for (item in this) {
collection.add(item)
}
return collection
}
/**
* Appends all elements to the given [collection].
*/
public fun <C : MutableCollection<in Int>> IntArray.toCollection(collection: C): C {
for (item in this) {
collection.add(item)
}
return collection
}
/**
* Appends all elements to the given [collection].
*/
public fun <C : MutableCollection<in Long>> LongArray.toCollection(collection: C): C {
for (item in this) {
collection.add(item)
}
return collection
}
/**
* Appends all elements to the given [collection].
*/
public fun <C : MutableCollection<in Short>> ShortArray.toCollection(collection: C): C {
for (item in this) {
collection.add(item)
}
return collection
}
/**
* Appends all elements to the given [collection].
*/
public fun <T, C : MutableCollection<in T>> Iterable<T>.toCollection(collection: C): C {
for (item in this) {
collection.add(item)
}
return collection
}
/**
* Appends all elements to the given [collection].
*/
public fun <T, C : MutableCollection<in T>> Sequence<T>.toCollection(collection: C): C {
for (item in this) {
collection.add(item)
}
return collection
}
/**
* Appends all elements to the given [collection].
*/
public fun <C : MutableCollection<in Char>> String.toCollection(collection: C): C {
for (item in this) {
collection.add(item)
}
return collection
}
/**
* Returns a [HashSet] of all elements.
*/
public fun <T> Array<out T>.toHashSet(): HashSet<T> {
return toCollection(HashSet<T>(mapCapacity(size())))
}
/**
* Returns a [HashSet] of all elements.
*/
public fun BooleanArray.toHashSet(): HashSet<Boolean> {
return toCollection(HashSet<Boolean>(mapCapacity(size())))
}
/**
* Returns a [HashSet] of all elements.
*/
public fun ByteArray.toHashSet(): HashSet<Byte> {
return toCollection(HashSet<Byte>(mapCapacity(size())))
}
/**
* Returns a [HashSet] of all elements.
*/
public fun CharArray.toHashSet(): HashSet<Char> {
return toCollection(HashSet<Char>(mapCapacity(size())))
}
/**
* Returns a [HashSet] of all elements.
*/
public fun DoubleArray.toHashSet(): HashSet<Double> {
return toCollection(HashSet<Double>(mapCapacity(size())))
}
/**
* Returns a [HashSet] of all elements.
*/
public fun FloatArray.toHashSet(): HashSet<Float> {
return toCollection(HashSet<Float>(mapCapacity(size())))
}
/**
* Returns a [HashSet] of all elements.
*/
public fun IntArray.toHashSet(): HashSet<Int> {
return toCollection(HashSet<Int>(mapCapacity(size())))
}
/**
* Returns a [HashSet] of all elements.
*/
public fun LongArray.toHashSet(): HashSet<Long> {
return toCollection(HashSet<Long>(mapCapacity(size())))
}
/**
* Returns a [HashSet] of all elements.
*/
public fun ShortArray.toHashSet(): HashSet<Short> {
return toCollection(HashSet<Short>(mapCapacity(size())))
}
/**
* Returns a [HashSet] of all elements.
*/
public fun <T> Iterable<T>.toHashSet(): HashSet<T> {
return toCollection(HashSet<T>(mapCapacity(collectionSizeOrDefault(12))))
}
/**
* Returns a [HashSet] of all elements.
*/
public fun <T> Sequence<T>.toHashSet(): HashSet<T> {
return toCollection(HashSet<T>())
}
/**
* Returns a [HashSet] of all elements.
*/
public fun String.toHashSet(): HashSet<Char> {
return toCollection(HashSet<Char>(mapCapacity(length())))
}
/**
* Returns a [LinkedList] containing all elements.
*/
public fun <T> Array<out T>.toLinkedList(): LinkedList<T> {
return toCollection(LinkedList<T>())
}
/**
* Returns a [LinkedList] containing all elements.
*/
public fun BooleanArray.toLinkedList(): LinkedList<Boolean> {
return toCollection(LinkedList<Boolean>())
}
/**
* Returns a [LinkedList] containing all elements.
*/
public fun ByteArray.toLinkedList(): LinkedList<Byte> {
return toCollection(LinkedList<Byte>())
}
/**
* Returns a [LinkedList] containing all elements.
*/
public fun CharArray.toLinkedList(): LinkedList<Char> {
return toCollection(LinkedList<Char>())
}
/**
* Returns a [LinkedList] containing all elements.
*/
public fun DoubleArray.toLinkedList(): LinkedList<Double> {
return toCollection(LinkedList<Double>())
}
/**
* Returns a [LinkedList] containing all elements.
*/
public fun FloatArray.toLinkedList(): LinkedList<Float> {
return toCollection(LinkedList<Float>())
}
/**
* Returns a [LinkedList] containing all elements.
*/
public fun IntArray.toLinkedList(): LinkedList<Int> {
return toCollection(LinkedList<Int>())
}
/**
* Returns a [LinkedList] containing all elements.
*/
public fun LongArray.toLinkedList(): LinkedList<Long> {
return toCollection(LinkedList<Long>())
}
/**
* Returns a [LinkedList] containing all elements.
*/
public fun ShortArray.toLinkedList(): LinkedList<Short> {
return toCollection(LinkedList<Short>())
}
/**
* Returns a [LinkedList] containing all elements.
*/
public fun <T> Iterable<T>.toLinkedList(): LinkedList<T> {
return toCollection(LinkedList<T>())
}
/**
* Returns a [LinkedList] containing all elements.
*/
public fun <T> Sequence<T>.toLinkedList(): LinkedList<T> {
return toCollection(LinkedList<T>())
}
/**
* Returns a [LinkedList] containing all elements.
*/
public fun String.toLinkedList(): LinkedList<Char> {
return toCollection(LinkedList<Char>())
}
/**
* Returns a [List] containing all key-value pairs.
*/
public fun <K, V> Map<K, V>.toList(): List<Pair<K, V>> {
val result = ArrayList<Pair<K, V>>(size())
for (item in this)
result.add(item.key to item.value)
return result
}
/**
* Returns a [List] containing all elements.
*/
public fun <T> Array<out T>.toList(): List<T> {
return this.toArrayList()
}
/**
* Returns a [List] containing all elements.
*/
public fun BooleanArray.toList(): List<Boolean> {
return this.toArrayList()
}
/**
* Returns a [List] containing all elements.
*/
public fun ByteArray.toList(): List<Byte> {
return this.toArrayList()
}
/**
* Returns a [List] containing all elements.
*/
public fun CharArray.toList(): List<Char> {
return this.toArrayList()
}
/**
* Returns a [List] containing all elements.
*/
public fun DoubleArray.toList(): List<Double> {
return this.toArrayList()
}
/**
* Returns a [List] containing all elements.
*/
public fun FloatArray.toList(): List<Float> {
return this.toArrayList()
}
/**
* Returns a [List] containing all elements.
*/
public fun IntArray.toList(): List<Int> {
return this.toArrayList()
}
/**
* Returns a [List] containing all elements.
*/
public fun LongArray.toList(): List<Long> {
return this.toArrayList()
}
/**
* Returns a [List] containing all elements.
*/
public fun ShortArray.toList(): List<Short> {
return this.toArrayList()
}
/**
* Returns a [List] containing all elements.
*/
public fun <T> Iterable<T>.toList(): List<T> {
return this.toArrayList()
}
/**
* Returns a [List] containing all elements.
*/
public fun <T> Sequence<T>.toList(): List<T> {
return this.toArrayList()
}
/**
* Returns a [List] containing all elements.
*/
public fun String.toList(): List<Char> {
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 <T, K> Array<out T>.toMap(selector: (T) -> K): Map<K, T> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, T>(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 <K> BooleanArray.toMap(selector: (Boolean) -> K): Map<K, Boolean> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, Boolean>(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 <K> ByteArray.toMap(selector: (Byte) -> K): Map<K, Byte> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, Byte>(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 <K> CharArray.toMap(selector: (Char) -> K): Map<K, Char> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, Char>(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 <K> DoubleArray.toMap(selector: (Double) -> K): Map<K, Double> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, Double>(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 <K> FloatArray.toMap(selector: (Float) -> K): Map<K, Float> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, Float>(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 <K> IntArray.toMap(selector: (Int) -> K): Map<K, Int> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, Int>(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 <K> LongArray.toMap(selector: (Long) -> K): Map<K, Long> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, Long>(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 <K> ShortArray.toMap(selector: (Short) -> K): Map<K, Short> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, Short>(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 <T, K> Iterable<T>.toMap(selector: (T) -> K): Map<K, T> {
val capacity = (collectionSizeOrDefault(10)/.75f) + 1
val result = LinkedHashMap<K, T>(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 <T, K> Sequence<T>.toMap(selector: (T) -> K): Map<K, T> {
val result = LinkedHashMap<K, T>()
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 <K> String.toMap(selector: (Char) -> K): Map<K, Char> {
val capacity = (length()/.75f) + 1
val result = LinkedHashMap<K, Char>(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 <T, K, V> Array<out T>.toMap(selector: (T) -> K, transform: (T) -> V): Map<K, V> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, V>(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 <K, V> BooleanArray.toMap(selector: (Boolean) -> K, transform: (Boolean) -> V): Map<K, V> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, V>(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 <K, V> ByteArray.toMap(selector: (Byte) -> K, transform: (Byte) -> V): Map<K, V> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, V>(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 <K, V> CharArray.toMap(selector: (Char) -> K, transform: (Char) -> V): Map<K, V> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, V>(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 <K, V> DoubleArray.toMap(selector: (Double) -> K, transform: (Double) -> V): Map<K, V> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, V>(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 <K, V> FloatArray.toMap(selector: (Float) -> K, transform: (Float) -> V): Map<K, V> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, V>(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 <K, V> IntArray.toMap(selector: (Int) -> K, transform: (Int) -> V): Map<K, V> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, V>(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 <K, V> LongArray.toMap(selector: (Long) -> K, transform: (Long) -> V): Map<K, V> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, V>(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 <K, V> ShortArray.toMap(selector: (Short) -> K, transform: (Short) -> V): Map<K, V> {
val capacity = (size()/.75f) + 1
val result = LinkedHashMap<K, V>(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 <T, K, V> Iterable<T>.toMap(selector: (T) -> K, transform: (T) -> V): Map<K, V> {
val capacity = (collectionSizeOrDefault(10)/.75f) + 1
val result = LinkedHashMap<K, V>(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 <T, K, V> Sequence<T>.toMap(selector: (T) -> K, transform: (T) -> V): Map<K, V> {
val result = LinkedHashMap<K, V>()
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 <K, V> String.toMap(selector: (Char) -> K, transform: (Char) -> V): Map<K, V> {
val capacity = (length()/.75f) + 1
val result = LinkedHashMap<K, V>(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 <T> Array<out T>.toSet(): Set<T> {
return toCollection(LinkedHashSet<T>(mapCapacity(size())))
}
/**
* Returns a [Set] of all elements.
*/
public fun BooleanArray.toSet(): Set<Boolean> {
return toCollection(LinkedHashSet<Boolean>(mapCapacity(size())))
}
/**
* Returns a [Set] of all elements.
*/
public fun ByteArray.toSet(): Set<Byte> {
return toCollection(LinkedHashSet<Byte>(mapCapacity(size())))
}
/**
* Returns a [Set] of all elements.
*/
public fun CharArray.toSet(): Set<Char> {
return toCollection(LinkedHashSet<Char>(mapCapacity(size())))
}
/**
* Returns a [Set] of all elements.
*/
public fun DoubleArray.toSet(): Set<Double> {
return toCollection(LinkedHashSet<Double>(mapCapacity(size())))
}
/**
* Returns a [Set] of all elements.
*/
public fun FloatArray.toSet(): Set<Float> {
return toCollection(LinkedHashSet<Float>(mapCapacity(size())))
}
/**
* Returns a [Set] of all elements.
*/
public fun IntArray.toSet(): Set<Int> {
return toCollection(LinkedHashSet<Int>(mapCapacity(size())))
}
/**
* Returns a [Set] of all elements.
*/
public fun LongArray.toSet(): Set<Long> {
return toCollection(LinkedHashSet<Long>(mapCapacity(size())))
}
/**
* Returns a [Set] of all elements.
*/
public fun ShortArray.toSet(): Set<Short> {
return toCollection(LinkedHashSet<Short>(mapCapacity(size())))
}
/**
* Returns a [Set] of all elements.
*/
public fun <T> Iterable<T>.toSet(): Set<T> {
return toCollection(LinkedHashSet<T>(mapCapacity(collectionSizeOrDefault(12))))
}
/**
* Returns a [Set] of all elements.
*/
public fun <T> Sequence<T>.toSet(): Set<T> {
return toCollection(LinkedHashSet<T>())
}
/**
* Returns a [Set] of all elements.
*/
public fun String.toSet(): Set<Char> {
return toCollection(LinkedHashSet<Char>(mapCapacity(length())))
}
/**
* Returns a [SortedSet] of all elements.
*/
public fun <T> Array<out T>.toSortedSet(): SortedSet<T> {
return toCollection(TreeSet<T>())
}
/**
* Returns a [SortedSet] of all elements.
*/
public fun BooleanArray.toSortedSet(): SortedSet<Boolean> {
return toCollection(TreeSet<Boolean>())
}
/**
* Returns a [SortedSet] of all elements.
*/
public fun ByteArray.toSortedSet(): SortedSet<Byte> {
return toCollection(TreeSet<Byte>())
}
/**
* Returns a [SortedSet] of all elements.
*/
public fun CharArray.toSortedSet(): SortedSet<Char> {
return toCollection(TreeSet<Char>())
}
/**
* Returns a [SortedSet] of all elements.
*/
public fun DoubleArray.toSortedSet(): SortedSet<Double> {
return toCollection(TreeSet<Double>())
}
/**
* Returns a [SortedSet] of all elements.
*/
public fun FloatArray.toSortedSet(): SortedSet<Float> {
return toCollection(TreeSet<Float>())
}
/**
* Returns a [SortedSet] of all elements.
*/
public fun IntArray.toSortedSet(): SortedSet<Int> {
return toCollection(TreeSet<Int>())
}
/**
* Returns a [SortedSet] of all elements.
*/
public fun LongArray.toSortedSet(): SortedSet<Long> {
return toCollection(TreeSet<Long>())
}
/**
* Returns a [SortedSet] of all elements.
*/
public fun ShortArray.toSortedSet(): SortedSet<Short> {
return toCollection(TreeSet<Short>())
}
/**
* Returns a [SortedSet] of all elements.
*/
public fun <T> Iterable<T>.toSortedSet(): SortedSet<T> {
return toCollection(TreeSet<T>())
}
/**
* Returns a [SortedSet] of all elements.
*/
public fun <T> Sequence<T>.toSortedSet(): SortedSet<T> {
return toCollection(TreeSet<T>())
}
/**
* Returns a [SortedSet] of all elements.
*/
public fun String.toSortedSet(): SortedSet<Char> {
return toCollection(TreeSet<Char>())
}
@@ -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 <T> Array<out T>.asList(): List<T> {
return Arrays.asList(*this)
}
/**
* Returns a [List] that wraps the original array.
*/
public fun BooleanArray.asList(): List<Boolean> {
return object : AbstractList<Boolean>(), 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<Boolean> = this@asList.iterator() as MutableIterator<Boolean>
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<Byte> {
return object : AbstractList<Byte>(), 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<Byte> = this@asList.iterator() as MutableIterator<Byte>
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<Char> {
return object : AbstractList<Char>(), 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<Char> = this@asList.iterator() as MutableIterator<Char>
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<Double> {
return object : AbstractList<Double>(), 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<Double> = this@asList.iterator() as MutableIterator<Double>
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<Float> {
return object : AbstractList<Float>(), 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<Float> = this@asList.iterator() as MutableIterator<Float>
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<Int> {
return object : AbstractList<Int>(), 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<Int> = this@asList.iterator() as MutableIterator<Int>
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<Long> {
return object : AbstractList<Long>(), 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<Long> = this@asList.iterator() as MutableIterator<Long>
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<Short> {
return object : AbstractList<Short>(), 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<Short> = this@asList.iterator() as MutableIterator<Short>
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 <T> Array<out T>.binarySearch(element: T, comparator: Comparator<in T>, 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 <T> Array<out T>.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 <T> Array<out T>.copyOf(): Array<out T> {
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 <T> Array<T>.copyOf(): Array<T> {
return Arrays.copyOf(this, size())
}
/**
* Returns new array which is a copy of the original array.
*/
public fun <T> Array<out T>.copyOf(newSize: Int): Array<out T?> {
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 <T> Array<T>.copyOf(newSize: Int): Array<T?> {
return Arrays.copyOf(this, newSize)
}
/**
* Returns new array which is a copy of range of original array.
*/
public fun <T> Array<out T>.copyOfRange(fromIndex: Int, toIndex: Int): Array<out T> {
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 <T> Array<T>.copyOfRange(fromIndex: Int, toIndex: Int): Array<T> {
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 <T> Array<T>.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 <reified R> Array<*>.filterIsInstance(): List<R> {
return filterIsInstanceTo(ArrayList<R>())
}
/**
* Returns a list containing all elements that are instances of specified type parameter R.
*/
public inline fun <reified R> Iterable<*>.filterIsInstance(): List<R> {
return filterIsInstanceTo(ArrayList<R>())
}
/**
* Returns a sequence containing all elements that are instances of specified type parameter R.
*/
public inline fun <reified R> Sequence<*>.filterIsInstance(): Sequence<R> {
return filter { it is R } as Sequence<R>
}
/**
* Returns a list containing all elements that are instances of specified class.
*/
public fun <R> Array<*>.filterIsInstance(klass: Class<R>): List<R> {
return filterIsInstanceTo(ArrayList<R>(), klass)
}
/**
* Returns a list containing all elements that are instances of specified class.
*/
public fun <R> Iterable<*>.filterIsInstance(klass: Class<R>): List<R> {
return filterIsInstanceTo(ArrayList<R>(), klass)
}
/**
* Returns a sequence containing all elements that are instances of specified class.
*/
public fun <R> Sequence<*>.filterIsInstance(klass: Class<R>): Sequence<R> {
return filter { klass.isInstance(it) } as Sequence<R>
}
/**
* Appends all elements that are instances of specified type parameter R to the given [destination].
*/
public inline fun <reified R, C : MutableCollection<in R>> 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 <reified R, C : MutableCollection<in R>> 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 <reified R, C : MutableCollection<in R>> 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 <C : MutableCollection<in R>, R> Array<*>.filterIsInstanceTo(destination: C, klass: Class<R>): 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 <C : MutableCollection<in R>, R> Iterable<*>.filterIsInstanceTo(destination: C, klass: Class<R>): 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 <C : MutableCollection<in R>, R> Sequence<*>.filterIsInstanceTo(destination: C, klass: Class<R>): 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 <T> Array<T>.plus(array: Array<out T>): Array<T> {
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<Boolean>): 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<Byte>): 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<Char>): 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<Double>): 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<Float>): 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<Int>): 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<Long>): 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<Short>): 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 <T> Array<T>.plus(collection: Collection<T>): Array<T> {
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 <T> Array<T>.plus(element: T): Array<T> {
val index = size()
val result = Arrays.copyOf(this, index + 1)
result[index] = element
return result
}
/**
* Sorts array or range in array inplace.
*/
public fun <T> Array<out T>.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 <T> Array<out T>.sortWith(comparator: Comparator<in T>, 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<Boolean> {
val result = arrayOfNulls<Boolean>(size())
for (index in indices)
result[index] = this[index]
return result as Array<Boolean>
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
public fun ByteArray.toTypedArray(): Array<Byte> {
val result = arrayOfNulls<Byte>(size())
for (index in indices)
result[index] = this[index]
return result as Array<Byte>
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
public fun CharArray.toTypedArray(): Array<Char> {
val result = arrayOfNulls<Char>(size())
for (index in indices)
result[index] = this[index]
return result as Array<Char>
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
public fun DoubleArray.toTypedArray(): Array<Double> {
val result = arrayOfNulls<Double>(size())
for (index in indices)
result[index] = this[index]
return result as Array<Double>
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
public fun FloatArray.toTypedArray(): Array<Float> {
val result = arrayOfNulls<Float>(size())
for (index in indices)
result[index] = this[index]
return result as Array<Float>
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
public fun IntArray.toTypedArray(): Array<Int> {
val result = arrayOfNulls<Int>(size())
for (index in indices)
result[index] = this[index]
return result as Array<Int>
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
public fun LongArray.toTypedArray(): Array<Long> {
val result = arrayOfNulls<Long>(size())
for (index in indices)
result[index] = this[index]
return result as Array<Long>
}
/**
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
public fun ShortArray.toTypedArray(): Array<Short> {
val result = arrayOfNulls<Short>(size())
for (index in indices)
result[index] = this[index]
return result as Array<Short>
}
+662 -212
View File
@@ -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 <T, A : Appendable> Array<out T>.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 <A : Appendable> 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 <A : Appendable> 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 <A : Appendable> 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 <A : Appendable> 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 <A : Appendable> 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 <A : Appendable> 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 <C : Appendable> 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 <C : Appendable> 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<Int>): 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<Char> {
return toCollection(ArrayList<Char>(length()))
}
/**
* Appends all elements to the given [collection].
*/
public fun <C : MutableCollection<in Char>> 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<Char> {
return toCollection(HashSet<Char>(mapCapacity(length())))
}
/**
* Returns a [LinkedList] containing all elements.
*/
public fun String.toLinkedList(): LinkedList<Char> {
return toCollection(LinkedList<Char>())
}
/**
* Returns a [List] containing all elements.
*/
public fun String.toList(): List<Char> {
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 <K> String.toMap(selector: (Char) -> K): Map<K, Char> {
val capacity = (length()/.75f) + 1
val result = LinkedHashMap<K, Char>(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 <A : Appendable> 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 <K, V> String.toMap(selector: (Char) -> K, transform: (Char) -> V): Map<K, V> {
val capacity = (length()/.75f) + 1
val result = LinkedHashMap<K, V>(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 <A : Appendable> 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<Char> {
return toCollection(LinkedHashSet<Char>(mapCapacity(length())))
}
/**
* Returns a [SortedSet] of all elements.
*/
public fun String.toSortedSet(): SortedSet<Char> {
return toCollection(TreeSet<Char>())
}
/**
* Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original collection.
*/
public inline fun <R> String.flatMap(transform: (Char) -> Iterable<R>): List<R> {
return flatMapTo(ArrayList<R>(), 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 <R, C : MutableCollection<in R>> String.flatMapTo(destination: C, transform: (Char) -> Iterable<R>): 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 <T, A : Appendable> Iterable<T>.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 <K> String.groupBy(toKey: (Char) -> K): Map<K, List<Char>> {
return groupByTo(LinkedHashMap<K, MutableList<Char>>(), toKey)
}
/**
* Appends elements from original collection grouped by the result of given [toKey] function to the given [map].
*/
public inline fun <K> String.groupByTo(map: MutableMap<K, MutableList<Char>>, toKey: (Char) -> K): Map<K, MutableList<Char>> {
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<Char>() }
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 <T, A : Appendable> Sequence<T>.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 <R> String.map(transform: (Char) -> R): List<R> {
return mapTo(ArrayList<R>(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 <R> String.mapIndexed(transform: (Int, Char) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(length()), transform)
}
/**
* Appends transformed elements and their indices of the original collection using the given [transform] function
* to the given [destination].
*/
public inline fun <R, C : MutableCollection<in R>> 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 <R, C : MutableCollection<in R>> 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<IndexedValue<Char>> {
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<Pair<Int, Char>> {
var index = 0
return mapTo(ArrayList<Pair<Int, Char>>(), { 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 <R> 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 <R> 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 <T> Array<out T>.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 <R : Comparable<R>> 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 <R : Comparable<R>> 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 <T> Iterable<T>.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 <T> Sequence<T>.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<String, String> {
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<Pair<Char, Char>> {
val first = iterator()
val second = other.iterator()
val list = ArrayList<Pair<Char, Char>>(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<Char> {
if (isEmpty()) return emptySequence()
return object : Sequence<Char> {
override fun iterator(): Iterator<Char> {
return this@asSequence.iterator()
}
}
}
/**
* Returns a sequence from the given collection
*/
@Deprecated("Use asSequence() instead", ReplaceWith("asSequence()"))
public fun String.sequence(): Sequence<Char> {
return asSequence()
}