diff --git a/js/js.libraries/src/core/generated/_ArraysJs.kt b/js/js.libraries/src/core/generated/_ArraysJs.kt index c2eb9a7571e..eeccf2de9a6 100644 --- a/js/js.libraries/src/core/generated/_ArraysJs.kt +++ b/js/js.libraries/src/core/generated/_ArraysJs.kt @@ -9720,6 +9720,38 @@ public inline fun CharArray.forEachIndexed(action: (Int, Char) -> Unit): Unit { for (item in this) action(index++, item) } +/** + * Returns the largest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public fun Array.max(): Double? { + if (isEmpty()) return null + var max = this[0] + if (max.isNaN()) return max + for (i in 1..lastIndex) { + val e = this[i] + if (e.isNaN()) return e + if (max < e) max = e + } + return max +} + +/** + * Returns the largest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public fun Array.max(): Float? { + if (isEmpty()) return null + var max = this[0] + if (max.isNaN()) return max + for (i in 1..lastIndex) { + val e = this[i] + if (e.isNaN()) return e + if (max < e) max = e + } + return max +} + /** * Returns the largest element or `null` if there are no elements. */ @@ -9791,8 +9823,10 @@ public fun LongArray.max(): Long? { public fun FloatArray.max(): Float? { if (isEmpty()) return null var max = this[0] + if (max.isNaN()) return max for (i in 1..lastIndex) { val e = this[i] + if (e.isNaN()) return e if (max < e) max = e } return max @@ -9804,8 +9838,10 @@ public fun FloatArray.max(): Float? { public fun DoubleArray.max(): Double? { if (isEmpty()) return null var max = this[0] + if (max.isNaN()) return max for (i in 1..lastIndex) { val e = this[i] + if (e.isNaN()) return e if (max < e) max = e } return max @@ -10103,6 +10139,38 @@ public fun CharArray.maxWith(comparator: Comparator): Char? { return max } +/** + * Returns the smallest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public fun Array.min(): Double? { + if (isEmpty()) return null + var min = this[0] + if (min.isNaN()) return min + for (i in 1..lastIndex) { + val e = this[i] + if (e.isNaN()) return e + if (min > e) min = e + } + return min +} + +/** + * Returns the smallest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public fun Array.min(): Float? { + if (isEmpty()) return null + var min = this[0] + if (min.isNaN()) return min + for (i in 1..lastIndex) { + val e = this[i] + if (e.isNaN()) return e + if (min > e) min = e + } + return min +} + /** * Returns the smallest element or `null` if there are no elements. */ @@ -10174,8 +10242,10 @@ public fun LongArray.min(): Long? { public fun FloatArray.min(): Float? { if (isEmpty()) return null var min = this[0] + if (min.isNaN()) return min for (i in 1..lastIndex) { val e = this[i] + if (e.isNaN()) return e if (min > e) min = e } return min @@ -10187,8 +10257,10 @@ public fun FloatArray.min(): Float? { public fun DoubleArray.min(): Double? { if (isEmpty()) return null var min = this[0] + if (min.isNaN()) return min for (i in 1..lastIndex) { val e = this[i] + if (e.isNaN()) return e if (min > e) min = e } return min diff --git a/js/js.libraries/src/core/generated/_CollectionsJs.kt b/js/js.libraries/src/core/generated/_CollectionsJs.kt index 5daff1d2c3c..538cd6f4abd 100644 --- a/js/js.libraries/src/core/generated/_CollectionsJs.kt +++ b/js/js.libraries/src/core/generated/_CollectionsJs.kt @@ -1462,6 +1462,40 @@ public inline fun Iterable.forEachIndexed(action: (Int, T) -> Unit): Unit for (item in this) action(index++, item) } +/** + * Returns the largest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public fun Iterable.max(): Double? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var max = iterator.next() + if (max.isNaN()) return max + while (iterator.hasNext()) { + val e = iterator.next() + if (e.isNaN()) return e + if (max < e) max = e + } + return max +} + +/** + * Returns the largest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public fun Iterable.max(): Float? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var max = iterator.next() + if (max.isNaN()) return max + while (iterator.hasNext()) { + val e = iterator.next() + if (e.isNaN()) return e + if (max < e) max = e + } + return max +} + /** * Returns the largest element or `null` if there are no elements. */ @@ -1509,6 +1543,40 @@ public fun Iterable.maxWith(comparator: Comparator): T? { return max } +/** + * Returns the smallest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public fun Iterable.min(): Double? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var min = iterator.next() + if (min.isNaN()) return min + while (iterator.hasNext()) { + val e = iterator.next() + if (e.isNaN()) return e + if (min > e) min = e + } + return min +} + +/** + * Returns the smallest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public fun Iterable.min(): Float? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var min = iterator.next() + if (min.isNaN()) return min + while (iterator.hasNext()) { + val e = iterator.next() + if (e.isNaN()) return e + if (min > e) min = e + } + return min +} + /** * Returns the smallest element or `null` if there are no elements. */ diff --git a/js/js.libraries/src/core/generated/_SequencesJs.kt b/js/js.libraries/src/core/generated/_SequencesJs.kt index 1774a61b686..a12ef08a191 100644 --- a/js/js.libraries/src/core/generated/_SequencesJs.kt +++ b/js/js.libraries/src/core/generated/_SequencesJs.kt @@ -841,6 +841,40 @@ public inline fun Sequence.forEachIndexed(action: (Int, T) -> Unit): Unit for (item in this) action(index++, item) } +/** + * Returns the largest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public fun Sequence.max(): Double? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var max = iterator.next() + if (max.isNaN()) return max + while (iterator.hasNext()) { + val e = iterator.next() + if (e.isNaN()) return e + if (max < e) max = e + } + return max +} + +/** + * Returns the largest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public fun Sequence.max(): Float? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var max = iterator.next() + if (max.isNaN()) return max + while (iterator.hasNext()) { + val e = iterator.next() + if (e.isNaN()) return e + if (max < e) max = e + } + return max +} + /** * Returns the largest element or `null` if there are no elements. */ @@ -888,6 +922,40 @@ public fun Sequence.maxWith(comparator: Comparator): T? { return max } +/** + * Returns the smallest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public fun Sequence.min(): Double? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var min = iterator.next() + if (min.isNaN()) return min + while (iterator.hasNext()) { + val e = iterator.next() + if (e.isNaN()) return e + if (min > e) min = e + } + return min +} + +/** + * Returns the smallest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public fun Sequence.min(): Float? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var min = iterator.next() + if (min.isNaN()) return min + while (iterator.hasNext()) { + val e = iterator.next() + if (e.isNaN()) return e + if (min > e) min = e + } + return min +} + /** * Returns the smallest element or `null` if there are no elements. */ diff --git a/libraries/stdlib/common/src/generated/_Arrays.kt b/libraries/stdlib/common/src/generated/_Arrays.kt index 62778d3e8e0..312a52928eb 100644 --- a/libraries/stdlib/common/src/generated/_Arrays.kt +++ b/libraries/stdlib/common/src/generated/_Arrays.kt @@ -5751,6 +5751,18 @@ public header inline fun BooleanArray.forEachIndexed(action: (Int, Boolean) -> U */ public header inline fun CharArray.forEachIndexed(action: (Int, Char) -> Unit): Unit +/** + * Returns the largest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public header fun Array.max(): Double? + +/** + * Returns the largest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public header fun Array.max(): Float? + /** * Returns the largest element or `null` if there are no elements. */ @@ -5881,6 +5893,18 @@ public header fun BooleanArray.maxWith(comparator: Comparator): Bool */ public header fun CharArray.maxWith(comparator: Comparator): Char? +/** + * Returns the smallest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public header fun Array.min(): Double? + +/** + * Returns the smallest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public header fun Array.min(): Float? + /** * Returns the smallest element or `null` if there are no elements. */ diff --git a/libraries/stdlib/common/src/generated/_Collections.kt b/libraries/stdlib/common/src/generated/_Collections.kt index 8165d410512..5f6ef05850b 100644 --- a/libraries/stdlib/common/src/generated/_Collections.kt +++ b/libraries/stdlib/common/src/generated/_Collections.kt @@ -765,6 +765,18 @@ public header inline fun Iterable.forEach(action: (T) -> Unit): Unit */ public header inline fun Iterable.forEachIndexed(action: (Int, T) -> Unit): Unit +/** + * Returns the largest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public header fun Iterable.max(): Double? + +/** + * Returns the largest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public header fun Iterable.max(): Float? + /** * Returns the largest element or `null` if there are no elements. */ @@ -780,6 +792,18 @@ public header inline fun > Iterable.maxBy(selector: (T) */ public header fun Iterable.maxWith(comparator: Comparator): T? +/** + * Returns the smallest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public header fun Iterable.min(): Double? + +/** + * Returns the smallest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public header fun Iterable.min(): Float? + /** * Returns the smallest element or `null` if there are no elements. */ diff --git a/libraries/stdlib/common/src/generated/_Sequences.kt b/libraries/stdlib/common/src/generated/_Sequences.kt index 2a4b0b2ce2c..ccfe14fd14b 100644 --- a/libraries/stdlib/common/src/generated/_Sequences.kt +++ b/libraries/stdlib/common/src/generated/_Sequences.kt @@ -492,6 +492,18 @@ public header inline fun Sequence.forEach(action: (T) -> Unit): Unit */ public header inline fun Sequence.forEachIndexed(action: (Int, T) -> Unit): Unit +/** + * Returns the largest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public header fun Sequence.max(): Double? + +/** + * Returns the largest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public header fun Sequence.max(): Float? + /** * Returns the largest element or `null` if there are no elements. */ @@ -507,6 +519,18 @@ public header inline fun > Sequence.maxBy(selector: (T) */ public header fun Sequence.maxWith(comparator: Comparator): T? +/** + * Returns the smallest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public header fun Sequence.min(): Double? + +/** + * Returns the smallest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public header fun Sequence.min(): Float? + /** * Returns the smallest element or `null` if there are no elements. */ diff --git a/libraries/stdlib/src/generated/_Arrays.kt b/libraries/stdlib/src/generated/_Arrays.kt index d34da684667..8c7c316aca8 100644 --- a/libraries/stdlib/src/generated/_Arrays.kt +++ b/libraries/stdlib/src/generated/_Arrays.kt @@ -9803,6 +9803,38 @@ public inline fun CharArray.forEachIndexed(action: (Int, Char) -> Unit): Unit { for (item in this) action(index++, item) } +/** + * Returns the largest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public fun Array.max(): Double? { + if (isEmpty()) return null + var max = this[0] + if (max.isNaN()) return max + for (i in 1..lastIndex) { + val e = this[i] + if (e.isNaN()) return e + if (max < e) max = e + } + return max +} + +/** + * Returns the largest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public fun Array.max(): Float? { + if (isEmpty()) return null + var max = this[0] + if (max.isNaN()) return max + for (i in 1..lastIndex) { + val e = this[i] + if (e.isNaN()) return e + if (max < e) max = e + } + return max +} + /** * Returns the largest element or `null` if there are no elements. */ @@ -9874,8 +9906,10 @@ public fun LongArray.max(): Long? { public fun FloatArray.max(): Float? { if (isEmpty()) return null var max = this[0] + if (max.isNaN()) return max for (i in 1..lastIndex) { val e = this[i] + if (e.isNaN()) return e if (max < e) max = e } return max @@ -9887,8 +9921,10 @@ public fun FloatArray.max(): Float? { public fun DoubleArray.max(): Double? { if (isEmpty()) return null var max = this[0] + if (max.isNaN()) return max for (i in 1..lastIndex) { val e = this[i] + if (e.isNaN()) return e if (max < e) max = e } return max @@ -10186,6 +10222,38 @@ public fun CharArray.maxWith(comparator: Comparator): Char? { return max } +/** + * Returns the smallest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public fun Array.min(): Double? { + if (isEmpty()) return null + var min = this[0] + if (min.isNaN()) return min + for (i in 1..lastIndex) { + val e = this[i] + if (e.isNaN()) return e + if (min > e) min = e + } + return min +} + +/** + * Returns the smallest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public fun Array.min(): Float? { + if (isEmpty()) return null + var min = this[0] + if (min.isNaN()) return min + for (i in 1..lastIndex) { + val e = this[i] + if (e.isNaN()) return e + if (min > e) min = e + } + return min +} + /** * Returns the smallest element or `null` if there are no elements. */ @@ -10257,8 +10325,10 @@ public fun LongArray.min(): Long? { public fun FloatArray.min(): Float? { if (isEmpty()) return null var min = this[0] + if (min.isNaN()) return min for (i in 1..lastIndex) { val e = this[i] + if (e.isNaN()) return e if (min > e) min = e } return min @@ -10270,8 +10340,10 @@ public fun FloatArray.min(): Float? { public fun DoubleArray.min(): Double? { if (isEmpty()) return null var min = this[0] + if (min.isNaN()) return min for (i in 1..lastIndex) { val e = this[i] + if (e.isNaN()) return e if (min > e) min = e } return min diff --git a/libraries/stdlib/src/generated/_Collections.kt b/libraries/stdlib/src/generated/_Collections.kt index 1b91dceb18f..cf570779215 100644 --- a/libraries/stdlib/src/generated/_Collections.kt +++ b/libraries/stdlib/src/generated/_Collections.kt @@ -1473,6 +1473,40 @@ public inline fun Iterable.forEachIndexed(action: (Int, T) -> Unit): Unit for (item in this) action(index++, item) } +/** + * Returns the largest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public fun Iterable.max(): Double? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var max = iterator.next() + if (max.isNaN()) return max + while (iterator.hasNext()) { + val e = iterator.next() + if (e.isNaN()) return e + if (max < e) max = e + } + return max +} + +/** + * Returns the largest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public fun Iterable.max(): Float? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var max = iterator.next() + if (max.isNaN()) return max + while (iterator.hasNext()) { + val e = iterator.next() + if (e.isNaN()) return e + if (max < e) max = e + } + return max +} + /** * Returns the largest element or `null` if there are no elements. */ @@ -1520,6 +1554,40 @@ public fun Iterable.maxWith(comparator: Comparator): T? { return max } +/** + * Returns the smallest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public fun Iterable.min(): Double? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var min = iterator.next() + if (min.isNaN()) return min + while (iterator.hasNext()) { + val e = iterator.next() + if (e.isNaN()) return e + if (min > e) min = e + } + return min +} + +/** + * Returns the smallest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public fun Iterable.min(): Float? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var min = iterator.next() + if (min.isNaN()) return min + while (iterator.hasNext()) { + val e = iterator.next() + if (e.isNaN()) return e + if (min > e) min = e + } + return min +} + /** * Returns the smallest element or `null` if there are no elements. */ diff --git a/libraries/stdlib/src/generated/_Sequences.kt b/libraries/stdlib/src/generated/_Sequences.kt index a1367cafddb..dfb391e6ddf 100644 --- a/libraries/stdlib/src/generated/_Sequences.kt +++ b/libraries/stdlib/src/generated/_Sequences.kt @@ -860,6 +860,40 @@ public inline fun Sequence.forEachIndexed(action: (Int, T) -> Unit): Unit for (item in this) action(index++, item) } +/** + * Returns the largest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public fun Sequence.max(): Double? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var max = iterator.next() + if (max.isNaN()) return max + while (iterator.hasNext()) { + val e = iterator.next() + if (e.isNaN()) return e + if (max < e) max = e + } + return max +} + +/** + * Returns the largest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public fun Sequence.max(): Float? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var max = iterator.next() + if (max.isNaN()) return max + while (iterator.hasNext()) { + val e = iterator.next() + if (e.isNaN()) return e + if (max < e) max = e + } + return max +} + /** * Returns the largest element or `null` if there are no elements. */ @@ -907,6 +941,40 @@ public fun Sequence.maxWith(comparator: Comparator): T? { return max } +/** + * Returns the smallest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public fun Sequence.min(): Double? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var min = iterator.next() + if (min.isNaN()) return min + while (iterator.hasNext()) { + val e = iterator.next() + if (e.isNaN()) return e + if (min > e) min = e + } + return min +} + +/** + * Returns the smallest element or `null` if there are no elements. + */ +@SinceKotlin("1.1") +public fun Sequence.min(): Float? { + val iterator = iterator() + if (!iterator.hasNext()) return null + var min = iterator.next() + if (min.isNaN()) return min + while (iterator.hasNext()) { + val e = iterator.next() + if (e.isNaN()) return e + if (min > e) min = e + } + return min +} + /** * Returns the smallest element or `null` if there are no elements. */ diff --git a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib.txt b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib.txt index c2cb60031bd..a9aa4cbf6d1 100644 --- a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib.txt +++ b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib.txt @@ -827,6 +827,8 @@ public final class kotlin/collections/ArraysKt { public static final fun max ([I)Ljava/lang/Integer; public static final fun max ([J)Ljava/lang/Long; public static final fun max ([Ljava/lang/Comparable;)Ljava/lang/Comparable; + public static final fun max ([Ljava/lang/Double;)Ljava/lang/Double; + public static final fun max ([Ljava/lang/Float;)Ljava/lang/Float; public static final fun max ([S)Ljava/lang/Short; public static final fun maxBy ([BLkotlin/jvm/functions/Function1;)Ljava/lang/Byte; public static final fun maxBy ([CLkotlin/jvm/functions/Function1;)Ljava/lang/Character; @@ -853,6 +855,8 @@ public final class kotlin/collections/ArraysKt { public static final fun min ([I)Ljava/lang/Integer; public static final fun min ([J)Ljava/lang/Long; public static final fun min ([Ljava/lang/Comparable;)Ljava/lang/Comparable; + public static final fun min ([Ljava/lang/Double;)Ljava/lang/Double; + public static final fun min ([Ljava/lang/Float;)Ljava/lang/Float; public static final fun min ([S)Ljava/lang/Short; public static final fun minBy ([BLkotlin/jvm/functions/Function1;)Ljava/lang/Byte; public static final fun minBy ([CLkotlin/jvm/functions/Function1;)Ljava/lang/Character; @@ -1501,9 +1505,13 @@ public final class kotlin/collections/CollectionsKt { public static final fun mapNotNullTo (Ljava/lang/Iterable;Ljava/util/Collection;Lkotlin/jvm/functions/Function1;)Ljava/util/Collection; public static final fun mapTo (Ljava/lang/Iterable;Ljava/util/Collection;Lkotlin/jvm/functions/Function1;)Ljava/util/Collection; public static final fun max (Ljava/lang/Iterable;)Ljava/lang/Comparable; + public static final fun max (Ljava/lang/Iterable;)Ljava/lang/Double; + public static final fun max (Ljava/lang/Iterable;)Ljava/lang/Float; public static final fun maxBy (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public static final fun maxWith (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; public static final fun minBy (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public static final fun minWith (Ljava/lang/Iterable;Ljava/util/Comparator;)Ljava/lang/Object; public static final fun minus (Ljava/lang/Iterable;Ljava/lang/Iterable;)Ljava/util/List; @@ -2098,9 +2106,13 @@ public final class kotlin/sequences/SequencesKt { public static final fun mapNotNullTo (Lkotlin/sequences/Sequence;Ljava/util/Collection;Lkotlin/jvm/functions/Function1;)Ljava/util/Collection; public static final fun mapTo (Lkotlin/sequences/Sequence;Ljava/util/Collection;Lkotlin/jvm/functions/Function1;)Ljava/util/Collection; public static final fun max (Lkotlin/sequences/Sequence;)Ljava/lang/Comparable; + public static final fun max (Lkotlin/sequences/Sequence;)Ljava/lang/Double; + public static final fun max (Lkotlin/sequences/Sequence;)Ljava/lang/Float; public static final fun maxBy (Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public static final fun maxWith (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; public static final fun minBy (Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public static final fun minWith (Lkotlin/sequences/Sequence;Ljava/util/Comparator;)Ljava/lang/Object; public static final fun minus (Lkotlin/sequences/Sequence;Ljava/lang/Iterable;)Lkotlin/sequences/Sequence; diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt index c0f8809cc55..2a431b1646a 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt @@ -137,36 +137,65 @@ fun aggregates(): List { } } - templates add f("min()") { - doc { f -> "Returns the smallest ${f.element} or `null` if there are no ${f.element.pluralize()}." } - returns("T?") - exclude(PrimitiveType.Boolean) - typeParam("T : Comparable") - body { - """ - val iterator = iterator() - if (!iterator.hasNext()) return null - var min = iterator.next() - while (iterator.hasNext()) { - val e = iterator.next() - if (min > e) min = e + templates addAll listOf("min", "max").flatMap { op -> + val genericSpecializations = PrimitiveType.numericPrimitives.filterNot { it.isIntegral() } + listOf(null) + + listOf( + Iterables to genericSpecializations, + Sequences to genericSpecializations, + ArraysOfObjects to genericSpecializations, + ArraysOfPrimitives to (PrimitiveType.defaultPrimitives - PrimitiveType.Boolean), + CharSequences to setOf(null) + ).map { (f, primitives) -> primitives.map { primitive -> + f("$op()") { + val isFloat = primitive?.isIntegral() == false + val isGeneric = f in listOf(Iterables, Sequences, ArraysOfObjects) + + only(f) + typeParam("T : Comparable") + if (primitive != null) { + onlyPrimitives(f, primitive) + if (isFloat && isGeneric) + since("1.1") + } + doc { f -> "Returns the ${if (op == "max") "largest" else "smallest"} ${f.element} or `null` if there are no ${f.element.pluralize()}." } + returns("T?") + + body { + if (f == ArraysOfObjects || f == ArraysOfPrimitives || f == CharSequences) { + """ + if (isEmpty()) return null + var $op = this[0] + ${if (isFloat) "if ($op.isNaN()) return $op" else "\\"} + + for (i in 1..lastIndex) { + val e = this[i] + ${if (isFloat) "if (e.isNaN()) return e" else "\\"} + if ($op ${if (op == "max") "<" else ">"} e) $op = e + } + return $op + """ + } + else { + """ + val iterator = iterator() + if (!iterator.hasNext()) return null + var $op = iterator.next() + ${if (isFloat) "if ($op.isNaN()) return $op" else "\\"} + + while (iterator.hasNext()) { + val e = iterator.next() + ${if (isFloat) "if (e.isNaN()) return e" else "\\"} + if ($op ${if (op == "max") "<" else ">"} e) $op = e + } + return $op + """ + }.replace(Regex("""^\s+\\\n""", RegexOption.MULTILINE), "") // trim lines ending with \ + } } - return min - """ - } - body(CharSequences, ArraysOfObjects, ArraysOfPrimitives) { - """ - if (isEmpty()) return null - var min = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (min > e) min = e - } - return min - """ - } - } + }} + }.flatten() templates add f("minBy(selector: (T) -> R)") { inline(true) @@ -242,39 +271,6 @@ fun aggregates(): List { body(Maps) { "return entries.minWith(comparator)" } } - templates add f("max()") { - doc { f -> "Returns the largest ${f.element} or `null` if there are no ${f.element.pluralize()}." } - returns("T?") - exclude(PrimitiveType.Boolean) - typeParam("T : Comparable") - body { - """ - 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 - """ - } - - body(CharSequences, ArraysOfObjects, ArraysOfPrimitives) { - """ - if (isEmpty()) return null - - var max = this[0] - for (i in 1..lastIndex) { - val e = this[i] - if (max < e) max = e - } - return max - """ - } - } - templates add f("maxBy(selector: (T) -> R)") { inline(true)