From 16b62b8e651e72f92005b507cc793926a846ffcd Mon Sep 17 00:00:00 2001 From: Abduqodiri Qurbonzoda Date: Tue, 16 Jun 2020 00:59:24 +0300 Subject: [PATCH] Introduce minWithOrNull and maxWithOrNull extension functions #KT-38854 --- .../stdlib/common/src/generated/_Arrays.kt | 404 +++++++++++------- .../common/src/generated/_Collections.kt | 16 +- .../stdlib/common/src/generated/_Maps.kt | 22 +- .../stdlib/common/src/generated/_Sequences.kt | 16 +- .../stdlib/common/src/generated/_Strings.kt | 16 +- .../stdlib/common/src/generated/_UArrays.kt | 156 ++++--- .../stdlib/test/collections/ArraysTest.kt | 28 +- .../stdlib/test/collections/CollectionTest.kt | 20 +- .../test/collections/UnsignedArraysTest.kt | 40 +- .../kotlin-stdlib-runtime-merged.txt | 32 ++ .../src/templates/Aggregates.kt | 112 ++--- 11 files changed, 545 insertions(+), 317 deletions(-) diff --git a/libraries/stdlib/common/src/generated/_Arrays.kt b/libraries/stdlib/common/src/generated/_Arrays.kt index 19be6616b33..3a657da6e20 100644 --- a/libraries/stdlib/common/src/generated/_Arrays.kt +++ b/libraries/stdlib/common/src/generated/_Arrays.kt @@ -15021,101 +15021,56 @@ public fun CharArray.maxOrNull(): Char? { return max } -/** - * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. - */ +@Deprecated("Use maxWithOrNull instead.", ReplaceWith("maxWithOrNull(comparator)")) public fun Array.maxWith(comparator: Comparator): T? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(max, e) < 0) max = e - } - return max + return maxWithOrNull(comparator) } -/** - * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. - */ +@Deprecated("Use maxWithOrNull instead.", ReplaceWith("maxWithOrNull(comparator)")) public fun ByteArray.maxWith(comparator: Comparator): Byte? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(max, e) < 0) max = e - } - return max + return maxWithOrNull(comparator) } -/** - * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. - */ +@Deprecated("Use maxWithOrNull instead.", ReplaceWith("maxWithOrNull(comparator)")) public fun ShortArray.maxWith(comparator: Comparator): Short? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(max, e) < 0) max = e - } - return max + return maxWithOrNull(comparator) } -/** - * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. - */ +@Deprecated("Use maxWithOrNull instead.", ReplaceWith("maxWithOrNull(comparator)")) public fun IntArray.maxWith(comparator: Comparator): Int? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(max, e) < 0) max = e - } - return max + return maxWithOrNull(comparator) } -/** - * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. - */ +@Deprecated("Use maxWithOrNull instead.", ReplaceWith("maxWithOrNull(comparator)")) public fun LongArray.maxWith(comparator: Comparator): Long? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(max, e) < 0) max = e - } - return max + return maxWithOrNull(comparator) } -/** - * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. - */ +@Deprecated("Use maxWithOrNull instead.", ReplaceWith("maxWithOrNull(comparator)")) public fun FloatArray.maxWith(comparator: Comparator): Float? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(max, e) < 0) max = e - } - return max + return maxWithOrNull(comparator) } -/** - * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. - */ +@Deprecated("Use maxWithOrNull instead.", ReplaceWith("maxWithOrNull(comparator)")) public fun DoubleArray.maxWith(comparator: Comparator): Double? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(max, e) < 0) max = e - } - return max + return maxWithOrNull(comparator) } -/** - * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. - */ +@Deprecated("Use maxWithOrNull instead.", ReplaceWith("maxWithOrNull(comparator)")) public fun BooleanArray.maxWith(comparator: Comparator): Boolean? { + return maxWithOrNull(comparator) +} + +@Deprecated("Use maxWithOrNull instead.", ReplaceWith("maxWithOrNull(comparator)")) +public fun CharArray.maxWith(comparator: Comparator): Char? { + return maxWithOrNull(comparator) +} + +/** + * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. + */ +@SinceKotlin("1.4") +public fun Array.maxWithOrNull(comparator: Comparator): T? { if (isEmpty()) return null var max = this[0] for (i in 1..lastIndex) { @@ -15128,7 +15083,106 @@ public fun BooleanArray.maxWith(comparator: Comparator): Boolean? { /** * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. */ -public fun CharArray.maxWith(comparator: Comparator): Char? { +@SinceKotlin("1.4") +public fun ByteArray.maxWithOrNull(comparator: Comparator): Byte? { + if (isEmpty()) return null + var max = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(max, e) < 0) max = e + } + return max +} + +/** + * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. + */ +@SinceKotlin("1.4") +public fun ShortArray.maxWithOrNull(comparator: Comparator): Short? { + if (isEmpty()) return null + var max = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(max, e) < 0) max = e + } + return max +} + +/** + * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. + */ +@SinceKotlin("1.4") +public fun IntArray.maxWithOrNull(comparator: Comparator): Int? { + if (isEmpty()) return null + var max = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(max, e) < 0) max = e + } + return max +} + +/** + * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. + */ +@SinceKotlin("1.4") +public fun LongArray.maxWithOrNull(comparator: Comparator): Long? { + if (isEmpty()) return null + var max = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(max, e) < 0) max = e + } + return max +} + +/** + * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. + */ +@SinceKotlin("1.4") +public fun FloatArray.maxWithOrNull(comparator: Comparator): Float? { + if (isEmpty()) return null + var max = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(max, e) < 0) max = e + } + return max +} + +/** + * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. + */ +@SinceKotlin("1.4") +public fun DoubleArray.maxWithOrNull(comparator: Comparator): Double? { + if (isEmpty()) return null + var max = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(max, e) < 0) max = e + } + return max +} + +/** + * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. + */ +@SinceKotlin("1.4") +public fun BooleanArray.maxWithOrNull(comparator: Comparator): Boolean? { + if (isEmpty()) return null + var max = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(max, e) < 0) max = e + } + return max +} + +/** + * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. + */ +@SinceKotlin("1.4") +public fun CharArray.maxWithOrNull(comparator: Comparator): Char? { if (isEmpty()) return null var max = this[0] for (i in 1..lastIndex) { @@ -17102,101 +17156,56 @@ public fun CharArray.minOrNull(): Char? { return min } -/** - * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. - */ +@Deprecated("Use minWithOrNull instead.", ReplaceWith("minWithOrNull(comparator)")) public fun Array.minWith(comparator: Comparator): T? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(min, e) > 0) min = e - } - return min + return minWithOrNull(comparator) } -/** - * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. - */ +@Deprecated("Use minWithOrNull instead.", ReplaceWith("minWithOrNull(comparator)")) public fun ByteArray.minWith(comparator: Comparator): Byte? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(min, e) > 0) min = e - } - return min + return minWithOrNull(comparator) } -/** - * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. - */ +@Deprecated("Use minWithOrNull instead.", ReplaceWith("minWithOrNull(comparator)")) public fun ShortArray.minWith(comparator: Comparator): Short? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(min, e) > 0) min = e - } - return min + return minWithOrNull(comparator) } -/** - * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. - */ +@Deprecated("Use minWithOrNull instead.", ReplaceWith("minWithOrNull(comparator)")) public fun IntArray.minWith(comparator: Comparator): Int? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(min, e) > 0) min = e - } - return min + return minWithOrNull(comparator) } -/** - * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. - */ +@Deprecated("Use minWithOrNull instead.", ReplaceWith("minWithOrNull(comparator)")) public fun LongArray.minWith(comparator: Comparator): Long? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(min, e) > 0) min = e - } - return min + return minWithOrNull(comparator) } -/** - * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. - */ +@Deprecated("Use minWithOrNull instead.", ReplaceWith("minWithOrNull(comparator)")) public fun FloatArray.minWith(comparator: Comparator): Float? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(min, e) > 0) min = e - } - return min + return minWithOrNull(comparator) } -/** - * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. - */ +@Deprecated("Use minWithOrNull instead.", ReplaceWith("minWithOrNull(comparator)")) public fun DoubleArray.minWith(comparator: Comparator): Double? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(min, e) > 0) min = e - } - return min + return minWithOrNull(comparator) } -/** - * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. - */ +@Deprecated("Use minWithOrNull instead.", ReplaceWith("minWithOrNull(comparator)")) public fun BooleanArray.minWith(comparator: Comparator): Boolean? { + return minWithOrNull(comparator) +} + +@Deprecated("Use minWithOrNull instead.", ReplaceWith("minWithOrNull(comparator)")) +public fun CharArray.minWith(comparator: Comparator): Char? { + return minWithOrNull(comparator) +} + +/** + * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. + */ +@SinceKotlin("1.4") +public fun Array.minWithOrNull(comparator: Comparator): T? { if (isEmpty()) return null var min = this[0] for (i in 1..lastIndex) { @@ -17209,7 +17218,106 @@ public fun BooleanArray.minWith(comparator: Comparator): Boolean? { /** * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. */ -public fun CharArray.minWith(comparator: Comparator): Char? { +@SinceKotlin("1.4") +public fun ByteArray.minWithOrNull(comparator: Comparator): Byte? { + if (isEmpty()) return null + var min = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(min, e) > 0) min = e + } + return min +} + +/** + * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. + */ +@SinceKotlin("1.4") +public fun ShortArray.minWithOrNull(comparator: Comparator): Short? { + if (isEmpty()) return null + var min = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(min, e) > 0) min = e + } + return min +} + +/** + * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. + */ +@SinceKotlin("1.4") +public fun IntArray.minWithOrNull(comparator: Comparator): Int? { + if (isEmpty()) return null + var min = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(min, e) > 0) min = e + } + return min +} + +/** + * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. + */ +@SinceKotlin("1.4") +public fun LongArray.minWithOrNull(comparator: Comparator): Long? { + if (isEmpty()) return null + var min = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(min, e) > 0) min = e + } + return min +} + +/** + * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. + */ +@SinceKotlin("1.4") +public fun FloatArray.minWithOrNull(comparator: Comparator): Float? { + if (isEmpty()) return null + var min = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(min, e) > 0) min = e + } + return min +} + +/** + * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. + */ +@SinceKotlin("1.4") +public fun DoubleArray.minWithOrNull(comparator: Comparator): Double? { + if (isEmpty()) return null + var min = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(min, e) > 0) min = e + } + return min +} + +/** + * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. + */ +@SinceKotlin("1.4") +public fun BooleanArray.minWithOrNull(comparator: Comparator): Boolean? { + if (isEmpty()) return null + var min = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(min, e) > 0) min = e + } + return min +} + +/** + * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. + */ +@SinceKotlin("1.4") +public fun CharArray.minWithOrNull(comparator: Comparator): Char? { if (isEmpty()) return null var min = this[0] for (i in 1..lastIndex) { diff --git a/libraries/stdlib/common/src/generated/_Collections.kt b/libraries/stdlib/common/src/generated/_Collections.kt index 40244bc48db..a271f5e69da 100644 --- a/libraries/stdlib/common/src/generated/_Collections.kt +++ b/libraries/stdlib/common/src/generated/_Collections.kt @@ -1989,10 +1989,16 @@ public fun > Iterable.maxOrNull(): T? { return max } +@Deprecated("Use maxWithOrNull instead.", ReplaceWith("maxWithOrNull(comparator)")) +public fun Iterable.maxWith(comparator: Comparator): T? { + return maxWithOrNull(comparator) +} + /** * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. */ -public fun Iterable.maxWith(comparator: Comparator): T? { +@SinceKotlin("1.4") +public fun Iterable.maxWithOrNull(comparator: Comparator): T? { val iterator = iterator() if (!iterator.hasNext()) return null var max = iterator.next() @@ -2273,10 +2279,16 @@ public fun > Iterable.minOrNull(): T? { return min } +@Deprecated("Use minWithOrNull instead.", ReplaceWith("minWithOrNull(comparator)")) +public fun Iterable.minWith(comparator: Comparator): T? { + return minWithOrNull(comparator) +} + /** * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. */ -public fun Iterable.minWith(comparator: Comparator): T? { +@SinceKotlin("1.4") +public fun Iterable.minWithOrNull(comparator: Comparator): T? { val iterator = iterator() if (!iterator.hasNext()) return null var min = iterator.next() diff --git a/libraries/stdlib/common/src/generated/_Maps.kt b/libraries/stdlib/common/src/generated/_Maps.kt index 3355fee1e74..99c09fc72cc 100644 --- a/libraries/stdlib/common/src/generated/_Maps.kt +++ b/libraries/stdlib/common/src/generated/_Maps.kt @@ -308,12 +308,19 @@ public inline fun Map.maxOfWithOrNull(comparator: Comparator return entries.maxOfWithOrNull(comparator, selector) } +@Deprecated("Use maxWithOrNull instead.", ReplaceWith("maxWithOrNull(comparator)")) +@kotlin.internal.InlineOnly +public inline fun Map.maxWith(comparator: Comparator>): Map.Entry? { + return maxWithOrNull(comparator) +} + /** * Returns the first entry having the largest value according to the provided [comparator] or `null` if there are no entries. */ +@SinceKotlin("1.4") @kotlin.internal.InlineOnly -public inline fun Map.maxWith(comparator: Comparator>): Map.Entry? { - return entries.maxWith(comparator) +public inline fun Map.maxWithOrNull(comparator: Comparator>): Map.Entry? { + return entries.maxWithOrNull(comparator) } @Deprecated("Use minByOrNull instead.", ReplaceWith("minByOrNull(selector)")) @@ -444,11 +451,18 @@ public inline fun Map.minOfWithOrNull(comparator: Comparator return entries.minOfWithOrNull(comparator, selector) } +@Deprecated("Use minWithOrNull instead.", ReplaceWith("minWithOrNull(comparator)")) +public fun Map.minWith(comparator: Comparator>): Map.Entry? { + return minWithOrNull(comparator) +} + /** * Returns the first entry having the smallest value according to the provided [comparator] or `null` if there are no entries. */ -public fun Map.minWith(comparator: Comparator>): Map.Entry? { - return entries.minWith(comparator) +@SinceKotlin("1.4") +@kotlin.internal.InlineOnly +public inline fun Map.minWithOrNull(comparator: Comparator>): Map.Entry? { + return entries.minWithOrNull(comparator) } /** diff --git a/libraries/stdlib/common/src/generated/_Sequences.kt b/libraries/stdlib/common/src/generated/_Sequences.kt index 7ac303236e4..35fd9c0817f 100644 --- a/libraries/stdlib/common/src/generated/_Sequences.kt +++ b/libraries/stdlib/common/src/generated/_Sequences.kt @@ -1460,12 +1460,18 @@ public fun > Sequence.maxOrNull(): T? { return max } +@Deprecated("Use maxWithOrNull instead.", ReplaceWith("maxWithOrNull(comparator)")) +public fun Sequence.maxWith(comparator: Comparator): T? { + return maxWithOrNull(comparator) +} + /** * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. * * The operation is _terminal_. */ -public fun Sequence.maxWith(comparator: Comparator): T? { +@SinceKotlin("1.4") +public fun Sequence.maxWithOrNull(comparator: Comparator): T? { val iterator = iterator() if (!iterator.hasNext()) return null var max = iterator.next() @@ -1770,12 +1776,18 @@ public fun > Sequence.minOrNull(): T? { return min } +@Deprecated("Use minWithOrNull instead.", ReplaceWith("minWithOrNull(comparator)")) +public fun Sequence.minWith(comparator: Comparator): T? { + return minWithOrNull(comparator) +} + /** * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. * * The operation is _terminal_. */ -public fun Sequence.minWith(comparator: Comparator): T? { +@SinceKotlin("1.4") +public fun Sequence.minWithOrNull(comparator: Comparator): T? { val iterator = iterator() if (!iterator.hasNext()) return null var min = iterator.next() diff --git a/libraries/stdlib/common/src/generated/_Strings.kt b/libraries/stdlib/common/src/generated/_Strings.kt index 564b56d45ba..cb69bbc3441 100644 --- a/libraries/stdlib/common/src/generated/_Strings.kt +++ b/libraries/stdlib/common/src/generated/_Strings.kt @@ -1303,10 +1303,16 @@ public fun CharSequence.maxOrNull(): Char? { return max } +@Deprecated("Use maxWithOrNull instead.", ReplaceWith("maxWithOrNull(comparator)")) +public fun CharSequence.maxWith(comparator: Comparator): Char? { + return maxWithOrNull(comparator) +} + /** * Returns the first character having the largest value according to the provided [comparator] or `null` if there are no characters. */ -public fun CharSequence.maxWith(comparator: Comparator): Char? { +@SinceKotlin("1.4") +public fun CharSequence.maxWithOrNull(comparator: Comparator): Char? { if (isEmpty()) return null var max = this[0] for (i in 1..lastIndex) { @@ -1531,10 +1537,16 @@ public fun CharSequence.minOrNull(): Char? { return min } +@Deprecated("Use minWithOrNull instead.", ReplaceWith("minWithOrNull(comparator)")) +public fun CharSequence.minWith(comparator: Comparator): Char? { + return minWithOrNull(comparator) +} + /** * Returns the first character having the smallest value according to the provided [comparator] or `null` if there are no characters. */ -public fun CharSequence.minWith(comparator: Comparator): Char? { +@SinceKotlin("1.4") +public fun CharSequence.minWithOrNull(comparator: Comparator): Char? { if (isEmpty()) return null var min = this[0] for (i in 1..lastIndex) { diff --git a/libraries/stdlib/common/src/generated/_UArrays.kt b/libraries/stdlib/common/src/generated/_UArrays.kt index bbdcedee0b9..fad236d2970 100644 --- a/libraries/stdlib/common/src/generated/_UArrays.kt +++ b/libraries/stdlib/common/src/generated/_UArrays.kt @@ -6626,42 +6626,40 @@ public fun UShortArray.maxOrNull(): UShort? { return max } -/** - * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. - */ +@Deprecated("Use maxWithOrNull instead.", ReplaceWith("maxWithOrNull(comparator)")) @SinceKotlin("1.3") @ExperimentalUnsignedTypes public fun UIntArray.maxWith(comparator: Comparator): UInt? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(max, e) < 0) max = e - } - return max + return maxWithOrNull(comparator) } -/** - * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. - */ +@Deprecated("Use maxWithOrNull instead.", ReplaceWith("maxWithOrNull(comparator)")) @SinceKotlin("1.3") @ExperimentalUnsignedTypes public fun ULongArray.maxWith(comparator: Comparator): ULong? { - if (isEmpty()) return null - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(max, e) < 0) max = e - } - return max + return maxWithOrNull(comparator) } -/** - * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. - */ +@Deprecated("Use maxWithOrNull instead.", ReplaceWith("maxWithOrNull(comparator)")) @SinceKotlin("1.3") @ExperimentalUnsignedTypes public fun UByteArray.maxWith(comparator: Comparator): UByte? { + return maxWithOrNull(comparator) +} + +@Deprecated("Use maxWithOrNull instead.", ReplaceWith("maxWithOrNull(comparator)")) +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun UShortArray.maxWith(comparator: Comparator): UShort? { + return maxWithOrNull(comparator) +} + +/** + * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. + */ +@SinceKotlin("1.4") +@ExperimentalUnsignedTypes +public fun UIntArray.maxWithOrNull(comparator: Comparator): UInt? { if (isEmpty()) return null var max = this[0] for (i in 1..lastIndex) { @@ -6674,9 +6672,39 @@ public fun UByteArray.maxWith(comparator: Comparator): UByte? { /** * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. */ -@SinceKotlin("1.3") +@SinceKotlin("1.4") @ExperimentalUnsignedTypes -public fun UShortArray.maxWith(comparator: Comparator): UShort? { +public fun ULongArray.maxWithOrNull(comparator: Comparator): ULong? { + if (isEmpty()) return null + var max = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(max, e) < 0) max = e + } + return max +} + +/** + * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. + */ +@SinceKotlin("1.4") +@ExperimentalUnsignedTypes +public fun UByteArray.maxWithOrNull(comparator: Comparator): UByte? { + if (isEmpty()) return null + var max = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(max, e) < 0) max = e + } + return max +} + +/** + * Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. + */ +@SinceKotlin("1.4") +@ExperimentalUnsignedTypes +public fun UShortArray.maxWithOrNull(comparator: Comparator): UShort? { if (isEmpty()) return null var max = this[0] for (i in 1..lastIndex) { @@ -7610,42 +7638,40 @@ public fun UShortArray.minOrNull(): UShort? { return min } -/** - * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. - */ +@Deprecated("Use minWithOrNull instead.", ReplaceWith("minWithOrNull(comparator)")) @SinceKotlin("1.3") @ExperimentalUnsignedTypes public fun UIntArray.minWith(comparator: Comparator): UInt? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(min, e) > 0) min = e - } - return min + return minWithOrNull(comparator) } -/** - * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. - */ +@Deprecated("Use minWithOrNull instead.", ReplaceWith("minWithOrNull(comparator)")) @SinceKotlin("1.3") @ExperimentalUnsignedTypes public fun ULongArray.minWith(comparator: Comparator): ULong? { - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(min, e) > 0) min = e - } - return min + return minWithOrNull(comparator) } -/** - * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. - */ +@Deprecated("Use minWithOrNull instead.", ReplaceWith("minWithOrNull(comparator)")) @SinceKotlin("1.3") @ExperimentalUnsignedTypes public fun UByteArray.minWith(comparator: Comparator): UByte? { + return minWithOrNull(comparator) +} + +@Deprecated("Use minWithOrNull instead.", ReplaceWith("minWithOrNull(comparator)")) +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun UShortArray.minWith(comparator: Comparator): UShort? { + return minWithOrNull(comparator) +} + +/** + * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. + */ +@SinceKotlin("1.4") +@ExperimentalUnsignedTypes +public fun UIntArray.minWithOrNull(comparator: Comparator): UInt? { if (isEmpty()) return null var min = this[0] for (i in 1..lastIndex) { @@ -7658,9 +7684,39 @@ public fun UByteArray.minWith(comparator: Comparator): UByte? { /** * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. */ -@SinceKotlin("1.3") +@SinceKotlin("1.4") @ExperimentalUnsignedTypes -public fun UShortArray.minWith(comparator: Comparator): UShort? { +public fun ULongArray.minWithOrNull(comparator: Comparator): ULong? { + if (isEmpty()) return null + var min = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(min, e) > 0) min = e + } + return min +} + +/** + * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. + */ +@SinceKotlin("1.4") +@ExperimentalUnsignedTypes +public fun UByteArray.minWithOrNull(comparator: Comparator): UByte? { + if (isEmpty()) return null + var min = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare(min, e) > 0) min = e + } + return min +} + +/** + * Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. + */ +@SinceKotlin("1.4") +@ExperimentalUnsignedTypes +public fun UShortArray.minWithOrNull(comparator: Comparator): UShort? { if (isEmpty()) return null var min = this[0] for (i in 1..lastIndex) { diff --git a/libraries/stdlib/test/collections/ArraysTest.kt b/libraries/stdlib/test/collections/ArraysTest.kt index 7cf9688159b..0e0b5d6bb68 100644 --- a/libraries/stdlib/test/collections/ArraysTest.kt +++ b/libraries/stdlib/test/collections/ArraysTest.kt @@ -390,26 +390,26 @@ class ArraysTest { expect('b', { charArrayOf('a', 'b').maxOrNull() }) } - @Test fun minWith() { - assertEquals(null, arrayOf().minWith(naturalOrder()) ) - assertEquals("a", arrayOf("a", "B").minWith(STRING_CASE_INSENSITIVE_ORDER)) + @Test fun minWithOrNull() { + assertEquals(null, arrayOf().minWithOrNull(naturalOrder())) + assertEquals("a", arrayOf("a", "B").minWithOrNull(STRING_CASE_INSENSITIVE_ORDER)) } - @Test fun minWithInPrimitiveArrays() { - expect(null, { intArrayOf().minWith(naturalOrder()) }) - expect(1, { intArrayOf(1).minWith(naturalOrder()) }) - expect(4, { intArrayOf(2, 3, 4).minWith(compareBy { it % 4 }) }) + @Test fun minWithOrNullInPrimitiveArrays() { + expect(null, { intArrayOf().minWithOrNull(naturalOrder()) }) + expect(1, { intArrayOf(1).minWithOrNull(naturalOrder()) }) + expect(4, { intArrayOf(2, 3, 4).minWithOrNull(compareBy { it % 4 }) }) } - @Test fun maxWith() { - assertEquals(null, arrayOf().maxWith(naturalOrder()) ) - assertEquals("B", arrayOf("a", "B").maxWith(STRING_CASE_INSENSITIVE_ORDER)) + @Test fun maxWithOrNull() { + assertEquals(null, arrayOf().maxWithOrNull(naturalOrder())) + assertEquals("B", arrayOf("a", "B").maxWithOrNull(STRING_CASE_INSENSITIVE_ORDER)) } - @Test fun maxWithInPrimitiveArrays() { - expect(null, { intArrayOf().maxWith(naturalOrder()) }) - expect(1, { intArrayOf(1).maxWith(naturalOrder()) }) - expect(-4, { intArrayOf(2, 3, -4).maxWith(compareBy { it*it }) }) + @Test fun maxWithOrNullInPrimitiveArrays() { + expect(null, { intArrayOf().maxWithOrNull(naturalOrder()) }) + expect(1, { intArrayOf(1).maxWithOrNull(naturalOrder()) }) + expect(-4, { intArrayOf(2, 3, -4).maxWithOrNull(compareBy { it * it }) }) } @Test fun minByOrNull() { diff --git a/libraries/stdlib/test/collections/CollectionTest.kt b/libraries/stdlib/test/collections/CollectionTest.kt index 9b3321ec175..3467bcf2700 100644 --- a/libraries/stdlib/test/collections/CollectionTest.kt +++ b/libraries/stdlib/test/collections/CollectionTest.kt @@ -851,18 +851,18 @@ class CollectionTest { assertIsPositiveZero(listOf(0.0F, -0.0F).shuffled().maxOrNull()!!.toDouble()) } - @Test fun minWith() { - expect(null, { listOf().minWith(naturalOrder()) }) - expect(1, { listOf(1).minWith(naturalOrder()) }) - expect("a", { listOf("a", "B").minWith(STRING_CASE_INSENSITIVE_ORDER) }) - expect("a", { listOf("a", "B").asSequence().minWith(STRING_CASE_INSENSITIVE_ORDER) }) + @Test fun minWithOrNull() { + expect(null, { listOf().minWithOrNull(naturalOrder()) }) + expect(1, { listOf(1).minWithOrNull(naturalOrder()) }) + expect("a", { listOf("a", "B").minWithOrNull(STRING_CASE_INSENSITIVE_ORDER) }) + expect("a", { listOf("a", "B").asSequence().minWithOrNull(STRING_CASE_INSENSITIVE_ORDER) }) } - @Test fun maxWith() { - expect(null, { listOf().maxWith(naturalOrder()) }) - expect(1, { listOf(1).maxWith(naturalOrder()) }) - expect("B", { listOf("a", "B").maxWith(STRING_CASE_INSENSITIVE_ORDER) }) - expect("B", { listOf("a", "B").asSequence().maxWith(STRING_CASE_INSENSITIVE_ORDER) }) + @Test fun maxWithOrNull() { + expect(null, { listOf().maxWithOrNull(naturalOrder()) }) + expect(1, { listOf(1).maxWithOrNull(naturalOrder()) }) + expect("B", { listOf("a", "B").maxWithOrNull(STRING_CASE_INSENSITIVE_ORDER) }) + expect("B", { listOf("a", "B").asSequence().maxWithOrNull(STRING_CASE_INSENSITIVE_ORDER) }) } @Test fun minByOrNull() { diff --git a/libraries/stdlib/test/collections/UnsignedArraysTest.kt b/libraries/stdlib/test/collections/UnsignedArraysTest.kt index 357e911e101..e8d82168310 100644 --- a/libraries/stdlib/test/collections/UnsignedArraysTest.kt +++ b/libraries/stdlib/test/collections/UnsignedArraysTest.kt @@ -451,35 +451,35 @@ class UnsignedArraysTest { } @Test - fun minWith() { - expect(null) { arrayOf().minWith(naturalOrder()) } - expect(1u) { arrayOf(1).minWith(naturalOrder()) } - expect(2u) { arrayOf(2, 3).minWith(naturalOrder()) } - expect(2uL) { arrayOf(3, 2).minWith(naturalOrder()) } + fun minWitOrNullh() { + expect(null) { arrayOf().minWithOrNull(naturalOrder()) } + expect(1u) { arrayOf(1).minWithOrNull(naturalOrder()) } + expect(2u) { arrayOf(2, 3).minWithOrNull(naturalOrder()) } + expect(2uL) { arrayOf(3, 2).minWithOrNull(naturalOrder()) } } @Test - fun minWithInUnsignedArrays() { - expect(null) { ubyteArrayOf().minWith(reverseOrder()) } - expect(1u) { ushortArrayOf(1).minWith(reverseOrder()) } - expect(3u) { uintArrayOf(2, 3).minWith(reverseOrder()) } - expect(3uL) { ulongArrayOf(3, 2).minWith(reverseOrder()) } + fun minWithOrNullInUnsignedArrays() { + expect(null) { ubyteArrayOf().minWithOrNull(reverseOrder()) } + expect(1u) { ushortArrayOf(1).minWithOrNull(reverseOrder()) } + expect(3u) { uintArrayOf(2, 3).minWithOrNull(reverseOrder()) } + expect(3uL) { ulongArrayOf(3, 2).minWithOrNull(reverseOrder()) } } @Test - fun maxWith() { - expect(null) { arrayOf().maxWith(naturalOrder()) } - expect(1u) { arrayOf(1).maxWith(naturalOrder()) } - expect(3u) { arrayOf(2, 3).maxWith(naturalOrder()) } - expect(3uL) { arrayOf(3, 2).maxWith(naturalOrder()) } + fun maxWithOrNull() { + expect(null) { arrayOf().maxWithOrNull(naturalOrder()) } + expect(1u) { arrayOf(1).maxWithOrNull(naturalOrder()) } + expect(3u) { arrayOf(2, 3).maxWithOrNull(naturalOrder()) } + expect(3uL) { arrayOf(3, 2).maxWithOrNull(naturalOrder()) } } @Test - fun maxWithInUnsignedArrays() { - expect(null) { ubyteArrayOf().maxWith(reverseOrder()) } - expect(1u) { ushortArrayOf(1).maxWith(reverseOrder()) } - expect(2u) { uintArrayOf(2, 3).maxWith(reverseOrder()) } - expect(2uL) { ulongArrayOf(3, 2).maxWith(reverseOrder()) } + fun maxWithOrNullInUnsignedArrays() { + expect(null) { ubyteArrayOf().maxWithOrNull(reverseOrder()) } + expect(1u) { ushortArrayOf(1).maxWithOrNull(reverseOrder()) } + expect(2u) { uintArrayOf(2, 3).maxWithOrNull(reverseOrder()) } + expect(2uL) { ulongArrayOf(3, 2).maxWithOrNull(reverseOrder()) } } @Test diff --git a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt index bcede062e20..8f81c74b385 100644 --- a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt +++ b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt @@ -1421,6 +1421,15 @@ public final class kotlin/collections/ArraysKt { public static final fun maxWith ([Ljava/lang/Object;Ljava/util/Comparator;)Ljava/lang/Object; public static final fun maxWith ([SLjava/util/Comparator;)Ljava/lang/Short; public static final fun maxWith ([ZLjava/util/Comparator;)Ljava/lang/Boolean; + public static final fun maxWithOrNull ([BLjava/util/Comparator;)Ljava/lang/Byte; + public static final fun maxWithOrNull ([CLjava/util/Comparator;)Ljava/lang/Character; + public static final fun maxWithOrNull ([DLjava/util/Comparator;)Ljava/lang/Double; + public static final fun maxWithOrNull ([FLjava/util/Comparator;)Ljava/lang/Float; + public static final fun maxWithOrNull ([ILjava/util/Comparator;)Ljava/lang/Integer; + public static final fun maxWithOrNull ([JLjava/util/Comparator;)Ljava/lang/Long; + public static final fun maxWithOrNull ([Ljava/lang/Object;Ljava/util/Comparator;)Ljava/lang/Object; + public static final fun maxWithOrNull ([SLjava/util/Comparator;)Ljava/lang/Short; + public static final fun maxWithOrNull ([ZLjava/util/Comparator;)Ljava/lang/Boolean; public static final fun min ([B)Ljava/lang/Byte; public static final fun min ([C)Ljava/lang/Character; public static final fun min ([D)Ljava/lang/Double; @@ -1468,6 +1477,15 @@ public final class kotlin/collections/ArraysKt { public static final fun minWith ([Ljava/lang/Object;Ljava/util/Comparator;)Ljava/lang/Object; public static final fun minWith ([SLjava/util/Comparator;)Ljava/lang/Short; public static final fun minWith ([ZLjava/util/Comparator;)Ljava/lang/Boolean; + public static final fun minWithOrNull ([BLjava/util/Comparator;)Ljava/lang/Byte; + public static final fun minWithOrNull ([CLjava/util/Comparator;)Ljava/lang/Character; + public static final fun minWithOrNull ([DLjava/util/Comparator;)Ljava/lang/Double; + public static final fun minWithOrNull ([FLjava/util/Comparator;)Ljava/lang/Float; + public static final fun minWithOrNull ([ILjava/util/Comparator;)Ljava/lang/Integer; + public static final fun minWithOrNull ([JLjava/util/Comparator;)Ljava/lang/Long; + public static final fun minWithOrNull ([Ljava/lang/Object;Ljava/util/Comparator;)Ljava/lang/Object; + public static final fun minWithOrNull ([SLjava/util/Comparator;)Ljava/lang/Short; + public static final fun minWithOrNull ([ZLjava/util/Comparator;)Ljava/lang/Boolean; public static final fun none ([B)Z public static final fun none ([BLkotlin/jvm/functions/Function1;)Z public static final fun none ([C)Z @@ -2238,6 +2256,7 @@ public final class kotlin/collections/CollectionsKt { public static final fun maxOrNull (Ljava/lang/Iterable;)Ljava/lang/Double; public static final fun maxOrNull (Ljava/lang/Iterable;)Ljava/lang/Float; public static final fun maxWith (Ljava/lang/Iterable;Ljava/util/Comparator;)Ljava/lang/Object; + public static final fun maxWithOrNull (Ljava/lang/Iterable;Ljava/util/Comparator;)Ljava/lang/Object; public static final fun min (Ljava/lang/Iterable;)Ljava/lang/Comparable; public static final fun min (Ljava/lang/Iterable;)Ljava/lang/Double; public static final fun min (Ljava/lang/Iterable;)Ljava/lang/Float; @@ -2247,6 +2266,7 @@ public final class kotlin/collections/CollectionsKt { public static final fun minOrNull (Ljava/lang/Iterable;)Ljava/lang/Double; public static final fun minOrNull (Ljava/lang/Iterable;)Ljava/lang/Float; public static final fun minWith (Ljava/lang/Iterable;Ljava/util/Comparator;)Ljava/lang/Object; + public static final fun minWithOrNull (Ljava/lang/Iterable;Ljava/util/Comparator;)Ljava/lang/Object; public static final fun minus (Ljava/lang/Iterable;Ljava/lang/Iterable;)Ljava/util/List; public static final fun minus (Ljava/lang/Iterable;Ljava/lang/Object;)Ljava/util/List; public static final fun minus (Ljava/lang/Iterable;Lkotlin/sequences/Sequence;)Ljava/util/List; @@ -2669,6 +2689,10 @@ public final class kotlin/collections/unsigned/UArraysKt { public static final fun maxWith-YmdZ_VM ([ILjava/util/Comparator;)Lkotlin/UInt; public static final fun maxWith-eOHTfZs ([SLjava/util/Comparator;)Lkotlin/UShort; public static final fun maxWith-zrEWJaI ([JLjava/util/Comparator;)Lkotlin/ULong; + public static final fun maxWithOrNull-XMRcp5o ([BLjava/util/Comparator;)Lkotlin/UByte; + public static final fun maxWithOrNull-YmdZ_VM ([ILjava/util/Comparator;)Lkotlin/UInt; + public static final fun maxWithOrNull-eOHTfZs ([SLjava/util/Comparator;)Lkotlin/UShort; + public static final fun maxWithOrNull-zrEWJaI ([JLjava/util/Comparator;)Lkotlin/ULong; public static final fun min--ajY-9A ([I)Lkotlin/UInt; public static final fun min-GBYM_sE ([B)Lkotlin/UByte; public static final fun min-QwZRm1k ([J)Lkotlin/ULong; @@ -2681,6 +2705,10 @@ public final class kotlin/collections/unsigned/UArraysKt { public static final fun minWith-YmdZ_VM ([ILjava/util/Comparator;)Lkotlin/UInt; public static final fun minWith-eOHTfZs ([SLjava/util/Comparator;)Lkotlin/UShort; public static final fun minWith-zrEWJaI ([JLjava/util/Comparator;)Lkotlin/ULong; + public static final fun minWithOrNull-XMRcp5o ([BLjava/util/Comparator;)Lkotlin/UByte; + public static final fun minWithOrNull-YmdZ_VM ([ILjava/util/Comparator;)Lkotlin/UInt; + public static final fun minWithOrNull-eOHTfZs ([SLjava/util/Comparator;)Lkotlin/UShort; + public static final fun minWithOrNull-zrEWJaI ([JLjava/util/Comparator;)Lkotlin/ULong; public static final fun plus-CFIt9YE ([ILjava/util/Collection;)[I public static final fun plus-kzHmqpY ([JLjava/util/Collection;)[J public static final fun plus-ojwP5H8 ([SLjava/util/Collection;)[S @@ -4897,6 +4925,7 @@ public final class kotlin/sequences/SequencesKt { public static final fun maxOrNull (Lkotlin/sequences/Sequence;)Ljava/lang/Double; public static final fun maxOrNull (Lkotlin/sequences/Sequence;)Ljava/lang/Float; public static final fun maxWith (Lkotlin/sequences/Sequence;Ljava/util/Comparator;)Ljava/lang/Object; + public static final fun maxWithOrNull (Lkotlin/sequences/Sequence;Ljava/util/Comparator;)Ljava/lang/Object; public static final fun min (Lkotlin/sequences/Sequence;)Ljava/lang/Comparable; public static final fun min (Lkotlin/sequences/Sequence;)Ljava/lang/Double; public static final fun min (Lkotlin/sequences/Sequence;)Ljava/lang/Float; @@ -4906,6 +4935,7 @@ public final class kotlin/sequences/SequencesKt { public static final fun minOrNull (Lkotlin/sequences/Sequence;)Ljava/lang/Double; public static final fun minOrNull (Lkotlin/sequences/Sequence;)Ljava/lang/Float; public static final fun minWith (Lkotlin/sequences/Sequence;Ljava/util/Comparator;)Ljava/lang/Object; + public static final fun minWithOrNull (Lkotlin/sequences/Sequence;Ljava/util/Comparator;)Ljava/lang/Object; public static final fun minus (Lkotlin/sequences/Sequence;Ljava/lang/Iterable;)Lkotlin/sequences/Sequence; public static final fun minus (Lkotlin/sequences/Sequence;Ljava/lang/Object;)Lkotlin/sequences/Sequence; public static final fun minus (Lkotlin/sequences/Sequence;Lkotlin/sequences/Sequence;)Lkotlin/sequences/Sequence; @@ -5306,11 +5336,13 @@ public final class kotlin/text/StringsKt { public static final fun maxByOrNull (Ljava/lang/CharSequence;Lkotlin/jvm/functions/Function1;)Ljava/lang/Character; public static final fun maxOrNull (Ljava/lang/CharSequence;)Ljava/lang/Character; public static final fun maxWith (Ljava/lang/CharSequence;Ljava/util/Comparator;)Ljava/lang/Character; + public static final fun maxWithOrNull (Ljava/lang/CharSequence;Ljava/util/Comparator;)Ljava/lang/Character; public static final fun min (Ljava/lang/CharSequence;)Ljava/lang/Character; public static final fun minBy (Ljava/lang/CharSequence;Lkotlin/jvm/functions/Function1;)Ljava/lang/Character; public static final fun minByOrNull (Ljava/lang/CharSequence;Lkotlin/jvm/functions/Function1;)Ljava/lang/Character; public static final fun minOrNull (Ljava/lang/CharSequence;)Ljava/lang/Character; public static final fun minWith (Ljava/lang/CharSequence;Ljava/util/Comparator;)Ljava/lang/Character; + public static final fun minWithOrNull (Ljava/lang/CharSequence;Ljava/util/Comparator;)Ljava/lang/Character; public static final fun none (Ljava/lang/CharSequence;)Z public static final fun none (Ljava/lang/CharSequence;Lkotlin/jvm/functions/Function1;)Z public static final fun onEach (Ljava/lang/CharSequence;Lkotlin/jvm/functions/Function1;)Ljava/lang/CharSequence; diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt index 572cf5f42e8..6c5a12be1f0 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt @@ -448,74 +448,56 @@ object Aggregates : TemplateGroupBase() { yield(def(op, nullable)) } - val f_minWith = fn("minWith(comparator: Comparator)") { - includeDefault() - include(Maps, CharSequences, ArraysOfUnsigned) - } builder { - doc { "Returns the first ${f.element} having the smallest value according to the provided [comparator] or `null` if there are no ${f.element.pluralize()}." } - returns("T?") - body { - """ - val iterator = iterator() - if (!iterator.hasNext()) return null + val f_minMaxWith = sequence { + fun def(op: String, nullable: Boolean, orNull: String = "OrNull".ifOrEmpty(nullable)) = + fn("$op$orNull(comparator: Comparator)") { + includeDefault() + include(Maps, CharSequences, ArraysOfUnsigned) + } builder { + specialFor(Maps) { if (op == "maxWith" || nullable) inlineOnly() } + returns("T?") - var min = iterator.next() - while (iterator.hasNext()) { - val e = iterator.next() - if (comparator.compare(min, e) > 0) min = e + if (!nullable) { + deprecate(Deprecation("Use ${op}OrNull instead.", "${op}OrNull(comparator)", DeprecationLevel.WARNING)) + body { "return ${op}OrNull(comparator)" } + return@builder + } + + since("1.4") + + doc { "Returns the first ${f.element} having the ${if (op == "maxWith") "largest" else "smallest"} value according to the provided [comparator] or `null` if there are no ${f.element.pluralize()}." } + + val (acc, cmp) = if (op == "minWith") Pair("min", ">") else Pair("max", "<") + body { + """ + val iterator = iterator() + if (!iterator.hasNext()) return null + + var $acc = iterator.next() + while (iterator.hasNext()) { + val e = iterator.next() + if (comparator.compare($acc, e) $cmp 0) $acc = e + } + return $acc + """ + } + body(CharSequences, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) { + """ + if (isEmpty()) return null + var $acc = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (comparator.compare($acc, e) $cmp 0) $acc = e + } + return $acc + """ + } + body(Maps) { "return entries.$op$orNull(comparator)" } } - return min - """ - } - body(CharSequences, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) { - """ - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(min, e) > 0) min = e - } - return min - """ - } - body(Maps) { "return entries.minWith(comparator)" } - } - val f_maxWith = fn("maxWith(comparator: Comparator)") { - includeDefault() - include(Maps, CharSequences, ArraysOfUnsigned) - } builder { - doc { "Returns the first ${f.element} having the largest value according to the provided [comparator] or `null` if there are no ${f.element.pluralize()}." } - returns("T?") - body { - """ - val iterator = iterator() - if (!iterator.hasNext()) return null - - var max = iterator.next() - while (iterator.hasNext()) { - val e = iterator.next() - if (comparator.compare(max, e) < 0) max = e - } - return max - """ - } - body(CharSequences, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) { - """ - if (isEmpty()) return null - - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (comparator.compare(max, e) < 0) max = e - } - return max - """ - } - specialFor(Maps) { - inlineOnly() - body { "return entries.maxWith(comparator)" } - } + for (op in listOf("minWith", "maxWith")) + for (nullable in listOf(false, true)) + yield(def(op, nullable)) } fun f_minMaxOf() = sequence {