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
}