From 0a06f6b4ca929f519cc19814b54ab4703b8f583f Mon Sep 17 00:00:00 2001 From: Ilya Matveev Date: Wed, 3 May 2017 13:07:22 +0700 Subject: [PATCH] stdlib: Add more minOf/maxOf extensions --- .../kotlin/kotlin/comparisons/Comparisons.kt | 273 ++++++++++++++++++ 1 file changed, 273 insertions(+) diff --git a/runtime/src/main/kotlin/kotlin/comparisons/Comparisons.kt b/runtime/src/main/kotlin/kotlin/comparisons/Comparisons.kt index 64bada21be8..81502baa469 100644 --- a/runtime/src/main/kotlin/kotlin/comparisons/Comparisons.kt +++ b/runtime/src/main/kotlin/kotlin/comparisons/Comparisons.kt @@ -321,10 +321,12 @@ private object ReverseOrderComparator: Comparator> { } // From _Comparisions.kt. + /** * Returns the greater of two values. * If values are equal, returns the first one. */ +@SinceKotlin("1.1") public fun > maxOf(a: T, b: T): T { return if (a >= b) a else b } @@ -332,11 +334,147 @@ public fun > maxOf(a: T, b: T): T { /** * Returns the greater of two values. */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline fun maxOf(a: Byte, b: Byte): Byte { + return maxOf(a.toInt(), b.toInt()).toByte() +} + +/** + * Returns the greater of two values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline fun maxOf(a: Short, b: Short): Short { + return maxOf(a.toInt(), b.toInt()).toShort() +} + +/** + * Returns the greater of two values. + */ +@SinceKotlin("1.1") @kotlin.internal.InlineOnly public inline fun maxOf(a: Int, b: Int): Int { return if (a >= b) a else b } +/** + * Returns the greater of two values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline fun maxOf(a: Long, b: Long): Long { + return if (a >= b) a else b +} + +/** + * Returns the greater of two values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline fun maxOf(a: Float, b: Float): Float { + // According to http://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#max-float-float- + // return NaN if one of the args is NaN. + // TODO: Check +/-0.0 + return when { + a.isNaN() -> a + b.isNaN() -> b + else -> if (a >= b) a else b + } +} + +/** + * Returns the greater of two values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline fun maxOf(a: Double, b: Double): Double { + return when { + a.isNaN() -> a + b.isNaN() -> b + else -> if (a >= b) a else b + } +} + +/** + * Returns the greater of three values. + */ +@SinceKotlin("1.1") +public fun > maxOf(a: T, b: T, c: T): T { + return maxOf(a, maxOf(b, c)) +} + +/** + * Returns the greater of three values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline fun maxOf(a: Byte, b: Byte, c: Byte): Byte { + return maxOf(a.toInt(), maxOf(b.toInt(), c.toInt())).toByte() +} + +/** + * Returns the greater of three values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline fun maxOf(a: Short, b: Short, c: Short): Short { + return maxOf(a.toInt(), maxOf(b.toInt(), c.toInt())).toShort() +} + +/** + * Returns the greater of three values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline fun maxOf(a: Int, b: Int, c: Int): Int { + return maxOf(a, maxOf(b, c)) +} + +/** + * Returns the greater of three values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline fun maxOf(a: Long, b: Long, c: Long): Long { + return maxOf(a, maxOf(b, c)) +} + +/** + * Returns the greater of three values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline fun maxOf(a: Float, b: Float, c: Float): Float { + return maxOf(a, maxOf(b, c)) +} + +/** + * Returns the greater of three values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline fun maxOf(a: Double, b: Double, c: Double): Double { + return maxOf(a, maxOf(b, c)) +} + +/** + * Returns the greater of three values according to the order specified by the given [comparator]. + */ +@SinceKotlin("1.1") +public fun maxOf(a: T, b: T, c: T, comparator: Comparator): T { + return maxOf(a, maxOf(b, c, comparator), comparator) +} + +/** + * Returns the greater of two values according to the order specified by the given [comparator]. + * If values are equal, returns the first one. + */ +@SinceKotlin("1.1") +public fun maxOf(a: T, b: T, comparator: Comparator): T { + return if (comparator.compare(a, b) >= 0) a else b +} + /** * Returns the smaller of two values. * If values are equal, returns the first one. @@ -349,7 +487,142 @@ public fun > minOf(a: T, b: T): T { /** * Returns the smaller of two values. */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline fun minOf(a: Byte, b: Byte): Byte { + return minOf(a.toInt(), b.toInt()).toByte() +} + +/** + * Returns the smaller of two values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline fun minOf(a: Short, b: Short): Short { + return minOf(a.toInt(), b.toInt()).toShort() +} + +/** + * Returns the smaller of two values. + */ +@SinceKotlin("1.1") @kotlin.internal.InlineOnly public inline fun minOf(a: Int, b: Int): Int { return if (a <= b) a else b } + +/** + * Returns the smaller of two values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline fun minOf(a: Long, b: Long): Long { + return if (a <= b) a else b +} + +/** + * Returns the smaller of two values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline fun minOf(a: Float, b: Float): Float { + // According to http://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#min-float-float- + // return NaN if one of the args is NaN. + return when { + a.isNaN() -> a + b.isNaN() -> b + else -> if (a <= b) a else b + } +} + +/** + * Returns the smaller of two values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline fun minOf(a: Double, b: Double): Double { + return when { + a.isNaN() -> a + b.isNaN() -> b + else -> if (a <= b) a else b + } +} + +/** + * Returns the smaller of three values. + */ +@SinceKotlin("1.1") +public fun > minOf(a: T, b: T, c: T): T { + return minOf(a, minOf(b, c)) +} + +/** + * Returns the smaller of three values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline fun minOf(a: Byte, b: Byte, c: Byte): Byte { + return minOf(a.toInt(), minOf(b.toInt(), c.toInt())).toByte() +} + +/** + * Returns the smaller of three values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline fun minOf(a: Short, b: Short, c: Short): Short { + return minOf(a.toInt(), minOf(b.toInt(), c.toInt())).toShort() +} + +/** + * Returns the smaller of three values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline fun minOf(a: Int, b: Int, c: Int): Int { + return minOf(a, minOf(b, c)) +} + +/** + * Returns the smaller of three values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline fun minOf(a: Long, b: Long, c: Long): Long { + return minOf(a, minOf(b, c)) +} + +/** + * Returns the smaller of three values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline fun minOf(a: Float, b: Float, c: Float): Float { + return minOf(a, minOf(b, c)) +} + +/** + * Returns the smaller of three values. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline fun minOf(a: Double, b: Double, c: Double): Double { + return minOf(a, minOf(b, c)) +} + +/** + * Returns the smaller of three values according to the order specified by the given [comparator]. + */ +@SinceKotlin("1.1") +public fun minOf(a: T, b: T, c: T, comparator: Comparator): T { + return minOf(a, minOf(b, c, comparator), comparator) +} + +/** + * Returns the smaller of two values according to the order specified by the given [comparator]. + * If values are equal, returns the first one. + */ +@SinceKotlin("1.1") +public fun minOf(a: T, b: T, comparator: Comparator): T { + return if (comparator.compare(a, b) <= 0) a else b +}