Add vararg overloads for maxOf/minOf functions #KT-33906

This commit is contained in:
Abduqodiri Qurbonzoda
2020-02-03 20:46:48 +03:00
parent 5aae3b5d27
commit 1f721796b8
10 changed files with 782 additions and 1 deletions
@@ -129,6 +129,58 @@ public fun <T> maxOf(a: T, b: T, comparator: Comparator<in T>): T {
return if (comparator.compare(a, b) >= 0) a else b
}
/**
* Returns the greater of given values.
*/
@SinceKotlin("1.4")
public expect fun <T : Comparable<T>> maxOf(a: T, vararg other: T): T
/**
* Returns the greater of given values.
*/
@SinceKotlin("1.4")
public expect fun maxOf(a: Byte, vararg other: Byte): Byte
/**
* Returns the greater of given values.
*/
@SinceKotlin("1.4")
public expect fun maxOf(a: Short, vararg other: Short): Short
/**
* Returns the greater of given values.
*/
@SinceKotlin("1.4")
public expect fun maxOf(a: Int, vararg other: Int): Int
/**
* Returns the greater of given values.
*/
@SinceKotlin("1.4")
public expect fun maxOf(a: Long, vararg other: Long): Long
/**
* Returns the greater of given values.
*/
@SinceKotlin("1.4")
public expect fun maxOf(a: Float, vararg other: Float): Float
/**
* Returns the greater of given values.
*/
@SinceKotlin("1.4")
public expect fun maxOf(a: Double, vararg other: Double): Double
/**
* Returns the greater of given values according to the order specified by the given [comparator].
*/
@SinceKotlin("1.4")
public fun <T> maxOf(a: T, vararg other: T, comparator: Comparator<in T>): T {
var max = a
for (e in other) if (comparator.compare(max, e) < 0) max = e
return max
}
/**
* Returns the smaller of two values.
* If values are equal, returns the first one.
@@ -243,3 +295,55 @@ public fun <T> minOf(a: T, b: T, comparator: Comparator<in T>): T {
return if (comparator.compare(a, b) <= 0) a else b
}
/**
* Returns the smaller of given values.
*/
@SinceKotlin("1.4")
public expect fun <T : Comparable<T>> minOf(a: T, vararg other: T): T
/**
* Returns the smaller of given values.
*/
@SinceKotlin("1.4")
public expect fun minOf(a: Byte, vararg other: Byte): Byte
/**
* Returns the smaller of given values.
*/
@SinceKotlin("1.4")
public expect fun minOf(a: Short, vararg other: Short): Short
/**
* Returns the smaller of given values.
*/
@SinceKotlin("1.4")
public expect fun minOf(a: Int, vararg other: Int): Int
/**
* Returns the smaller of given values.
*/
@SinceKotlin("1.4")
public expect fun minOf(a: Long, vararg other: Long): Long
/**
* Returns the smaller of given values.
*/
@SinceKotlin("1.4")
public expect fun minOf(a: Float, vararg other: Float): Float
/**
* Returns the smaller of given values.
*/
@SinceKotlin("1.4")
public expect fun minOf(a: Double, vararg other: Double): Double
/**
* Returns the smaller of given values according to the order specified by the given [comparator].
*/
@SinceKotlin("1.4")
public fun <T> minOf(a: T, vararg other: T, comparator: Comparator<in T>): T {
var min = a
for (e in other) if (comparator.compare(min, e) > 0) min = e
return min
}
@@ -91,6 +91,50 @@ public inline fun maxOf(a: UShort, b: UShort, c: UShort): UShort {
return maxOf(a, maxOf(b, c))
}
/**
* Returns the greater of given values.
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
public fun maxOf(a: UInt, vararg other: UInt): UInt {
var max = a
for (e in other) max = maxOf(max, e)
return max
}
/**
* Returns the greater of given values.
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
public fun maxOf(a: ULong, vararg other: ULong): ULong {
var max = a
for (e in other) max = maxOf(max, e)
return max
}
/**
* Returns the greater of given values.
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
public fun maxOf(a: UByte, vararg other: UByte): UByte {
var max = a
for (e in other) max = maxOf(max, e)
return max
}
/**
* Returns the greater of given values.
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
public fun maxOf(a: UShort, vararg other: UShort): UShort {
var max = a
for (e in other) max = maxOf(max, e)
return max
}
/**
* Returns the smaller of two values.
*/
@@ -167,3 +211,47 @@ public inline fun minOf(a: UShort, b: UShort, c: UShort): UShort {
return minOf(a, minOf(b, c))
}
/**
* Returns the smaller of given values.
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
public fun minOf(a: UInt, vararg other: UInt): UInt {
var min = a
for (e in other) min = minOf(min, e)
return min
}
/**
* Returns the smaller of given values.
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
public fun minOf(a: ULong, vararg other: ULong): ULong {
var min = a
for (e in other) min = minOf(min, e)
return min
}
/**
* Returns the smaller of given values.
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
public fun minOf(a: UByte, vararg other: UByte): UByte {
var min = a
for (e in other) min = minOf(min, e)
return min
}
/**
* Returns the smaller of given values.
*/
@SinceKotlin("1.4")
@ExperimentalUnsignedTypes
public fun minOf(a: UShort, vararg other: UShort): UShort {
var min = a
for (e in other) min = minOf(min, e)
return min
}
@@ -147,6 +147,76 @@ public actual inline fun maxOf(a: Double, b: Double, c: Double): Double {
return Math.max(a, b, c)
}
/**
* Returns the greater of given values.
*/
@SinceKotlin("1.4")
public actual fun <T : Comparable<T>> maxOf(a: T, vararg other: T): T {
var max = a
for (e in other) max = maxOf(max, e)
return max
}
/**
* Returns the greater of given values.
*/
@SinceKotlin("1.4")
public actual fun maxOf(a: Byte, vararg other: Byte): Byte {
var max = a
for (e in other) max = maxOf(max, e)
return max
}
/**
* Returns the greater of given values.
*/
@SinceKotlin("1.4")
public actual fun maxOf(a: Short, vararg other: Short): Short {
var max = a
for (e in other) max = maxOf(max, e)
return max
}
/**
* Returns the greater of given values.
*/
@SinceKotlin("1.4")
public actual fun maxOf(a: Int, vararg other: Int): Int {
var max = a
for (e in other) max = maxOf(max, e)
return max
}
/**
* Returns the greater of given values.
*/
@SinceKotlin("1.4")
public actual fun maxOf(a: Long, vararg other: Long): Long {
var max = a
for (e in other) max = maxOf(max, e)
return max
}
/**
* Returns the greater of given values.
*/
@SinceKotlin("1.4")
public actual fun maxOf(a: Float, vararg other: Float): Float {
var max = a
for (e in other) max = maxOf(max, e)
return max
}
/**
* Returns the greater of given values.
*/
@SinceKotlin("1.4")
public actual fun maxOf(a: Double, vararg other: Double): Double {
var max = a
for (e in other) max = maxOf(max, e)
return max
}
/**
* Returns the smaller of two values.
* If values are equal, returns the first one.
@@ -282,3 +352,73 @@ public actual inline fun minOf(a: Double, b: Double, c: Double): Double {
return Math.min(a, b, c)
}
/**
* Returns the smaller of given values.
*/
@SinceKotlin("1.4")
public actual fun <T : Comparable<T>> minOf(a: T, vararg other: T): T {
var min = a
for (e in other) min = minOf(min, e)
return min
}
/**
* Returns the smaller of given values.
*/
@SinceKotlin("1.4")
public actual fun minOf(a: Byte, vararg other: Byte): Byte {
var min = a
for (e in other) min = minOf(min, e)
return min
}
/**
* Returns the smaller of given values.
*/
@SinceKotlin("1.4")
public actual fun minOf(a: Short, vararg other: Short): Short {
var min = a
for (e in other) min = minOf(min, e)
return min
}
/**
* Returns the smaller of given values.
*/
@SinceKotlin("1.4")
public actual fun minOf(a: Int, vararg other: Int): Int {
var min = a
for (e in other) min = minOf(min, e)
return min
}
/**
* Returns the smaller of given values.
*/
@SinceKotlin("1.4")
public actual fun minOf(a: Long, vararg other: Long): Long {
var min = a
for (e in other) min = minOf(min, e)
return min
}
/**
* Returns the smaller of given values.
*/
@SinceKotlin("1.4")
public actual fun minOf(a: Float, vararg other: Float): Float {
var min = a
for (e in other) min = minOf(min, e)
return min
}
/**
* Returns the smaller of given values.
*/
@SinceKotlin("1.4")
public actual fun minOf(a: Double, vararg other: Double): Double {
var min = a
for (e in other) min = minOf(min, e)
return min
}
@@ -147,6 +147,76 @@ public actual inline fun maxOf(a: Double, b: Double, c: Double): Double {
return Math.max(a, b, c)
}
/**
* Returns the greater of given values.
*/
@SinceKotlin("1.4")
public actual fun <T : Comparable<T>> maxOf(a: T, vararg other: T): T {
var max = a
for (e in other) max = maxOf(max, e)
return max
}
/**
* Returns the greater of given values.
*/
@SinceKotlin("1.4")
public actual fun maxOf(a: Byte, vararg other: Byte): Byte {
var max = a
for (e in other) max = maxOf(max, e)
return max
}
/**
* Returns the greater of given values.
*/
@SinceKotlin("1.4")
public actual fun maxOf(a: Short, vararg other: Short): Short {
var max = a
for (e in other) max = maxOf(max, e)
return max
}
/**
* Returns the greater of given values.
*/
@SinceKotlin("1.4")
public actual fun maxOf(a: Int, vararg other: Int): Int {
var max = a
for (e in other) max = maxOf(max, e)
return max
}
/**
* Returns the greater of given values.
*/
@SinceKotlin("1.4")
public actual fun maxOf(a: Long, vararg other: Long): Long {
var max = a
for (e in other) max = maxOf(max, e)
return max
}
/**
* Returns the greater of given values.
*/
@SinceKotlin("1.4")
public actual fun maxOf(a: Float, vararg other: Float): Float {
var max = a
for (e in other) max = maxOf(max, e)
return max
}
/**
* Returns the greater of given values.
*/
@SinceKotlin("1.4")
public actual fun maxOf(a: Double, vararg other: Double): Double {
var max = a
for (e in other) max = maxOf(max, e)
return max
}
/**
* Returns the smaller of two values.
* If values are equal, returns the first one.
@@ -282,3 +352,73 @@ public actual inline fun minOf(a: Double, b: Double, c: Double): Double {
return Math.min(a, b, c)
}
/**
* Returns the smaller of given values.
*/
@SinceKotlin("1.4")
public actual fun <T : Comparable<T>> minOf(a: T, vararg other: T): T {
var min = a
for (e in other) min = minOf(min, e)
return min
}
/**
* Returns the smaller of given values.
*/
@SinceKotlin("1.4")
public actual fun minOf(a: Byte, vararg other: Byte): Byte {
var min = a
for (e in other) min = minOf(min, e)
return min
}
/**
* Returns the smaller of given values.
*/
@SinceKotlin("1.4")
public actual fun minOf(a: Short, vararg other: Short): Short {
var min = a
for (e in other) min = minOf(min, e)
return min
}
/**
* Returns the smaller of given values.
*/
@SinceKotlin("1.4")
public actual fun minOf(a: Int, vararg other: Int): Int {
var min = a
for (e in other) min = minOf(min, e)
return min
}
/**
* Returns the smaller of given values.
*/
@SinceKotlin("1.4")
public actual fun minOf(a: Long, vararg other: Long): Long {
var min = a
for (e in other) min = minOf(min, e)
return min
}
/**
* Returns the smaller of given values.
*/
@SinceKotlin("1.4")
public actual fun minOf(a: Float, vararg other: Float): Float {
var min = a
for (e in other) min = minOf(min, e)
return min
}
/**
* Returns the smaller of given values.
*/
@SinceKotlin("1.4")
public actual fun minOf(a: Double, vararg other: Double): Double {
var min = a
for (e in other) min = minOf(min, e)
return min
}
@@ -139,6 +139,76 @@ public actual inline fun maxOf(a: Double, b: Double, c: Double): Double {
return maxOf(a, maxOf(b, c))
}
/**
* Returns the greater of given values.
*/
@SinceKotlin("1.4")
public actual fun <T : Comparable<T>> maxOf(a: T, vararg other: T): T {
var max = a
for (e in other) max = maxOf(max, e)
return max
}
/**
* Returns the greater of given values.
*/
@SinceKotlin("1.4")
public actual fun maxOf(a: Byte, vararg other: Byte): Byte {
var max = a
for (e in other) max = maxOf(max, e)
return max
}
/**
* Returns the greater of given values.
*/
@SinceKotlin("1.4")
public actual fun maxOf(a: Short, vararg other: Short): Short {
var max = a
for (e in other) max = maxOf(max, e)
return max
}
/**
* Returns the greater of given values.
*/
@SinceKotlin("1.4")
public actual fun maxOf(a: Int, vararg other: Int): Int {
var max = a
for (e in other) max = maxOf(max, e)
return max
}
/**
* Returns the greater of given values.
*/
@SinceKotlin("1.4")
public actual fun maxOf(a: Long, vararg other: Long): Long {
var max = a
for (e in other) max = maxOf(max, e)
return max
}
/**
* Returns the greater of given values.
*/
@SinceKotlin("1.4")
public actual fun maxOf(a: Float, vararg other: Float): Float {
var max = a
for (e in other) max = maxOf(max, e)
return max
}
/**
* Returns the greater of given values.
*/
@SinceKotlin("1.4")
public actual fun maxOf(a: Double, vararg other: Double): Double {
var max = a
for (e in other) max = maxOf(max, e)
return max
}
/**
* Returns the smaller of two values.
* If values are equal, returns the first one.
@@ -264,3 +334,73 @@ public actual inline fun minOf(a: Double, b: Double, c: Double): Double {
return minOf(a, minOf(b, c))
}
/**
* Returns the smaller of given values.
*/
@SinceKotlin("1.4")
public actual fun <T : Comparable<T>> minOf(a: T, vararg other: T): T {
var min = a
for (e in other) min = minOf(min, e)
return min
}
/**
* Returns the smaller of given values.
*/
@SinceKotlin("1.4")
public actual fun minOf(a: Byte, vararg other: Byte): Byte {
var min = a
for (e in other) min = minOf(min, e)
return min
}
/**
* Returns the smaller of given values.
*/
@SinceKotlin("1.4")
public actual fun minOf(a: Short, vararg other: Short): Short {
var min = a
for (e in other) min = minOf(min, e)
return min
}
/**
* Returns the smaller of given values.
*/
@SinceKotlin("1.4")
public actual fun minOf(a: Int, vararg other: Int): Int {
var min = a
for (e in other) min = minOf(min, e)
return min
}
/**
* Returns the smaller of given values.
*/
@SinceKotlin("1.4")
public actual fun minOf(a: Long, vararg other: Long): Long {
var min = a
for (e in other) min = minOf(min, e)
return min
}
/**
* Returns the smaller of given values.
*/
@SinceKotlin("1.4")
public actual fun minOf(a: Float, vararg other: Float): Float {
var min = a
for (e in other) min = minOf(min, e)
return min
}
/**
* Returns the smaller of given values.
*/
@SinceKotlin("1.4")
public actual fun minOf(a: Double, vararg other: Double): Double {
var min = a
for (e in other) min = minOf(min, e)
return min
}
+13
View File
@@ -20,6 +20,7 @@ class OrderingTest {
val v1 = Item("wine", 9)
val v2 = Item("beer", 10)
val v3 = Item("apple", 20)
val v4 = Item("grape", 22)
@Test
fun compareByCompareTo() {
@@ -154,44 +155,56 @@ class OrderingTest {
fun maxOf() {
assertEquals(Int.MAX_VALUE, maxOf(Int.MAX_VALUE, Int.MIN_VALUE))
assertEquals(Int.MAX_VALUE, maxOf(Int.MAX_VALUE, Int.MIN_VALUE, 0))
assertEquals(Int.MAX_VALUE, maxOf(Int.MAX_VALUE, Int.MIN_VALUE, 0, -1))
assertEquals(Long.MAX_VALUE, maxOf(Long.MAX_VALUE, Long.MIN_VALUE))
assertEquals(Long.MAX_VALUE, maxOf(Long.MAX_VALUE, Long.MIN_VALUE, 0))
assertEquals(Long.MAX_VALUE, maxOf(Long.MAX_VALUE, Long.MIN_VALUE, 0, -1))
assertEquals(Double.POSITIVE_INFINITY, maxOf(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY))
assertEquals(Double.POSITIVE_INFINITY, maxOf(Double.POSITIVE_INFINITY, Double.MAX_VALUE, Double.MIN_VALUE))
assertEquals(Double.POSITIVE_INFINITY, maxOf(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, Double.MAX_VALUE, Double.MIN_VALUE))
assertEquals(0.0, maxOf(0.0, -0.0))
assertEquals(0.0, maxOf(-0.0, 0.0))
assertEquals(0.0, maxOf(-0.0, 0.0, 0.0, -0.0))
}
@Test
fun maxOfWith() {
assertEquals(v1, maxOf(v1, v2, compareBy { it.name }))
assertEquals(v1, maxOf(v3, v2, v1, compareBy { it.name }))
assertEquals(v1, maxOf(v4, v3, v2, v1, comparator = compareBy { it.name }))
assertEquals(v2, maxOf(v1, v2, compareBy { it.rating }))
assertEquals(v3, maxOf(v1, v2, v3, compareBy { it.rating }))
assertEquals(v4, maxOf(v4, v1, v2, v3, comparator = compareBy { it.rating }))
}
@Test
fun minOf() {
assertEquals(Int.MIN_VALUE, minOf(Int.MAX_VALUE, Int.MIN_VALUE))
assertEquals(Int.MIN_VALUE, minOf(Int.MAX_VALUE, Int.MIN_VALUE, 0))
assertEquals(Int.MIN_VALUE, minOf(Int.MAX_VALUE, Int.MIN_VALUE, 0, -1))
assertEquals(Long.MIN_VALUE, minOf(Long.MAX_VALUE, Long.MIN_VALUE))
assertEquals(Long.MIN_VALUE, minOf(Long.MAX_VALUE, Long.MIN_VALUE, 0))
assertEquals(Long.MIN_VALUE, minOf(Long.MAX_VALUE, Long.MIN_VALUE, 0, -1))
assertEquals(Double.NEGATIVE_INFINITY, minOf(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY))
assertEquals(Double.MIN_VALUE, minOf(Double.POSITIVE_INFINITY, Double.MAX_VALUE, Double.MIN_VALUE))
assertEquals(Double.NEGATIVE_INFINITY, minOf(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.MAX_VALUE, Double.MIN_VALUE))
assertEquals(-0.0, minOf(0.0, -0.0))
assertEquals(-0.0, minOf(-0.0, 0.0))
assertEquals(-0.0, minOf(-0.0, 0.0, 0.0, -0.0))
}
@Test
fun minOfWith() {
assertEquals(v2, minOf(v1, v2, compareBy { it.name }))
assertEquals(v3, minOf(v3, v2, v1, compareBy { it.name }))
assertEquals(v3, minOf(v4, v2, v3, v1, comparator = compareBy { it.name }))
assertEquals(v1, minOf(v1, v2, compareBy { it.rating }))
assertEquals(v1, minOf(v1, v2, v3, compareBy { it.rating }))
assertEquals(v1, minOf(v4, v1, v2, v3, comparator = compareBy { it.rating }))
}
}
@@ -58,12 +58,30 @@ class NaNPropagationTest {
propagateOf3(::minOf, ::minOf, "minOf")
}
@Test
fun minOfVararg() {
propagateOf3(
{ a, b, c -> minOf(a, *doubleArrayOf(b, c)) },
{ a, b, c -> minOf(a, *floatArrayOf(b, c)) },
"minOf(a, vararg other)"
)
}
@Test
fun maxOf() {
propagateOf2(::maxOf, ::maxOf, "maxOf")
propagateOf3(::maxOf, ::maxOf, "maxOf")
}
@Test
fun maxOfVararg() {
propagateOf3(
{ a, b, c -> maxOf(a, *doubleArrayOf(b, c)) },
{ a, b, c -> maxOf(a, *floatArrayOf(b, c)) },
"maxOf(a, vararg other)"
)
}
@Test
fun arrayMin() {
propagateOf2(
@@ -26,6 +26,14 @@ class UComparisonsTest {
expect(42312uL) { minOf(42312uL, 42312uL, 42312uL) }
}
@Test
fun minOf_vararg() {
expect(1.toUByte()) { minOf(2.toUByte(), 1.toUByte(), 3.toUByte(), 2.toUByte(), 10.toUByte()) }
expect(55.toUShort()) { minOf(58.toUShort(), 32768.toUShort(), 55.toUShort(), 2423.toUShort()) }
expect(UInt.MIN_VALUE) { minOf(UInt.MIN_VALUE, UInt.MAX_VALUE, 0u, 1000u, (-1).toUInt(), Int.MAX_VALUE.toUInt()) }
expect(42312uL) { minOf(42312uL, 42312uL, 42312uL, 42312uL) }
}
@Test
fun maxOf_2() {
expect(2.toUByte()) { maxOf(2.toUByte(), 1.toUByte()) }
@@ -41,4 +49,12 @@ class UComparisonsTest {
expect(UInt.MAX_VALUE) { maxOf(UInt.MIN_VALUE, UInt.MAX_VALUE, 0u) }
expect(42312uL) { maxOf(42312uL, 42312uL, 42312uL) }
}
@Test
fun maxOf_vararg() {
expect(10.toUByte()) { maxOf(2.toUByte(), 1.toUByte(), 3.toUByte(), 2.toUByte(), 10.toUByte()) }
expect(32768.toUShort()) { maxOf(58.toUShort(), 32768.toUShort(), 55.toUShort(), 2423.toUShort()) }
expect(UInt.MAX_VALUE) { maxOf(UInt.MIN_VALUE, UInt.MAX_VALUE, 0u, 1000u, (-1).toUInt(), Int.MAX_VALUE.toUInt()) }
expect(42312uL) { maxOf(42312uL, 42312uL, 42312uL, 42312uL) }
}
}