Implement min max minOf maxOf functions for unsigned types (KT-30035)

This commit is contained in:
Abduqodiri Qurbonzoda
2019-02-26 15:33:20 +03:00
committed by Ilya Gorbunov
parent bf83f0e070
commit be6c2d8c7d
8 changed files with 341 additions and 20 deletions
@@ -121,6 +121,7 @@ val reducedRuntimeSources = fullRuntimeSources - listOfKtFilesFrom(
"libraries/stdlib/unsigned",
"libraries/stdlib/common/src/generated/_Arrays.kt",
"libraries/stdlib/common/src/generated/_Collections.kt",
"libraries/stdlib/common/src/generated/_Comparisons.kt",
"libraries/stdlib/common/src/generated/_Maps.kt",
"libraries/stdlib/common/src/generated/_Sequences.kt",
"libraries/stdlib/common/src/generated/_Sets.kt",
@@ -128,9 +129,11 @@ val reducedRuntimeSources = fullRuntimeSources - listOfKtFilesFrom(
"libraries/stdlib/common/src/generated/_UArrays.kt",
"libraries/stdlib/common/src/generated/_URanges.kt",
"libraries/stdlib/common/src/generated/_UCollections.kt",
"libraries/stdlib/common/src/generated/_UComparisons.kt",
"libraries/stdlib/common/src/generated/_USequences.kt",
"libraries/stdlib/common/src/kotlin/SequencesH.kt",
"libraries/stdlib/common/src/kotlin/TextH.kt",
"libraries/stdlib/common/src/kotlin/UMath.kt",
"libraries/stdlib/common/src/kotlin/collections/",
"libraries/stdlib/common/src/kotlin/ioH.kt",
"libraries/stdlib/js/irRuntime/collectionsHacks.kt",
@@ -158,8 +161,7 @@ val reducedRuntimeSources = fullRuntimeSources - listOfKtFilesFrom(
"libraries/stdlib/src/kotlin/random/URandom.kt",
"libraries/stdlib/src/kotlin/text/",
"libraries/stdlib/src/kotlin/util/KotlinVersion.kt",
"libraries/stdlib/src/kotlin/util/Tuples.kt",
"libraries/stdlib/common/src/generated/_Comparisons.kt"
"libraries/stdlib/src/kotlin/util/Tuples.kt"
) + listOfKtFilesFrom(
"libraries/stdlib/js/irRuntime/smallRuntimeMissingDeclarations.kt"
)
@@ -0,0 +1,169 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("UComparisonsKt")
package kotlin.comparisons
//
// NOTE: THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.random.*
/**
* Returns the greater of two values.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun maxOf(a: UInt, b: UInt): UInt {
return if (a >= b) a else b
}
/**
* Returns the greater of two values.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun maxOf(a: ULong, b: ULong): ULong {
return if (a >= b) a else b
}
/**
* Returns the greater of two values.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun maxOf(a: UByte, b: UByte): UByte {
return if (a >= b) a else b
}
/**
* Returns the greater of two values.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun maxOf(a: UShort, b: UShort): UShort {
return if (a >= b) a else b
}
/**
* Returns the greater of three values.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun maxOf(a: UInt, b: UInt, c: UInt): UInt {
return maxOf(a, maxOf(b, c))
}
/**
* Returns the greater of three values.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun maxOf(a: ULong, b: ULong, c: ULong): ULong {
return maxOf(a, maxOf(b, c))
}
/**
* Returns the greater of three values.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun maxOf(a: UByte, b: UByte, c: UByte): UByte {
return maxOf(a, maxOf(b, c))
}
/**
* Returns the greater of three values.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun maxOf(a: UShort, b: UShort, c: UShort): UShort {
return maxOf(a, maxOf(b, c))
}
/**
* Returns the smaller of two values.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun minOf(a: UInt, b: UInt): UInt {
return if (a <= b) a else b
}
/**
* Returns the smaller of two values.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun minOf(a: ULong, b: ULong): ULong {
return if (a <= b) a else b
}
/**
* Returns the smaller of two values.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun minOf(a: UByte, b: UByte): UByte {
return if (a <= b) a else b
}
/**
* Returns the smaller of two values.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun minOf(a: UShort, b: UShort): UShort {
return if (a <= b) a else b
}
/**
* Returns the smaller of three values.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun minOf(a: UInt, b: UInt, c: UInt): UInt {
return minOf(a, minOf(b, c))
}
/**
* Returns the smaller of three values.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun minOf(a: ULong, b: ULong, c: ULong): ULong {
return minOf(a, minOf(b, c))
}
/**
* Returns the smaller of three values.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun minOf(a: UByte, b: UByte, c: UByte): UByte {
return minOf(a, minOf(b, c))
}
/**
* Returns the smaller of three values.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun minOf(a: UShort, b: UShort, c: UShort): UShort {
return minOf(a, minOf(b, c))
}
@@ -0,0 +1,44 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package unsigned
import kotlin.test.Test
import kotlin.test.expect
class UComparisonsTest {
@Test
fun minOf_2() {
expect(1.toUByte()) { minOf(2.toUByte(), 1.toUByte()) }
expect(58.toUShort()) { minOf(58.toUShort(), 32768.toUShort()) }
expect(UInt.MIN_VALUE) { minOf(UInt.MIN_VALUE, UInt.MAX_VALUE) }
expect(42312uL) { minOf(42312uL, 42312uL) }
}
@Test
fun minOf_3() {
expect(1.toUByte()) { minOf(2.toUByte(), 1.toUByte(), 3.toUByte()) }
expect(55.toUShort()) { minOf(58.toUShort(), 32768.toUShort(), 55.toUShort()) }
expect(UInt.MIN_VALUE) { minOf(UInt.MIN_VALUE, UInt.MAX_VALUE, 0u) }
expect(42312uL) { minOf(42312uL, 42312uL, 42312uL) }
}
@Test
fun maxOf_2() {
expect(2.toUByte()) { maxOf(2.toUByte(), 1.toUByte()) }
expect(32768.toUShort()) { maxOf(58.toUShort(), 32768.toUShort()) }
expect(UInt.MAX_VALUE) { maxOf(UInt.MIN_VALUE, UInt.MAX_VALUE) }
expect(42312uL) { maxOf(42312uL, 42312uL) }
}
@Test
fun maxOf_3() {
expect(3.toUByte()) { maxOf(2.toUByte(), 1.toUByte(), 3.toUByte()) }
expect(32768.toUShort()) { maxOf(58.toUShort(), 32768.toUShort(), 55.toUShort()) }
expect(UInt.MAX_VALUE) { maxOf(UInt.MIN_VALUE, UInt.MAX_VALUE, 0u) }
expect(42312uL) { maxOf(42312uL, 42312uL, 42312uL) }
}
}
@@ -0,0 +1,30 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package test.unsigned
import kotlin.math.max
import kotlin.math.min
import kotlin.test.Test
import kotlin.test.expect
class UMathTest {
@Test
fun min() {
expect(1uL) { min(2uL, 1uL) }
expect(ULong.MIN_VALUE) { min(ULong.MIN_VALUE, ULong.MAX_VALUE) }
expect(58u) { min(58u, 1u shl 31) }
expect(42312u) { min(42312u, 42312u) }
}
@Test
fun max() {
expect(2u) { max(2u, 1u) }
expect(UInt.MAX_VALUE) { maxOf(UInt.MIN_VALUE, UInt.MAX_VALUE) }
expect(1uL shl 63) { max(58uL, 1uL shl 63) }
expect(42312uL) { maxOf(42312uL, 42312uL) }
}
}
@@ -0,0 +1,46 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package kotlin.math
/**
* Returns the smaller of two values.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun min(a: UInt, b: UInt): UInt {
return minOf(a, b)
}
/**
* Returns the smaller of two values.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun min(a: ULong, b: ULong): ULong {
return minOf(a, b)
}
/**
* Returns the greater of two values.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun max(a: UInt, b: UInt): UInt {
return maxOf(a, b)
}
/**
* Returns the greater of two values.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun max(a: ULong, b: ULong): ULong {
return maxOf(a, b)
}
@@ -2521,6 +2521,17 @@ public final class kotlin/comparisons/ComparisonsKt {
public static final fun thenDescending (Ljava/util/Comparator;Ljava/util/Comparator;)Ljava/util/Comparator;
}
public final class kotlin/comparisons/UComparisonsKt {
public static final fun maxOf-5PvTz6A (SS)S
public static final fun maxOf-J1ME1BU (II)I
public static final fun maxOf-Kr8caGY (BB)B
public static final fun maxOf-eb3DHEI (JJ)J
public static final fun minOf-5PvTz6A (SS)S
public static final fun minOf-J1ME1BU (II)I
public static final fun minOf-Kr8caGY (BB)B
public static final fun minOf-eb3DHEI (JJ)J
}
public final class kotlin/concurrent/ThreadsKt {
public static final fun thread (ZZLjava/lang/ClassLoader;Ljava/lang/String;ILkotlin/jvm/functions/Function0;)Ljava/lang/Thread;
public static synthetic fun thread$default (ZZLjava/lang/ClassLoader;Ljava/lang/String;ILkotlin/jvm/functions/Function0;ILjava/lang/Object;)Ljava/lang/Thread;
@@ -9,6 +9,16 @@ import templates.Family.*
object ComparableOps : TemplateGroupBase() {
init {
defaultBuilder {
specialFor(Unsigned) {
since("1.3")
annotation("@ExperimentalUnsignedTypes")
sourceFile(SourceFile.UComparisons)
}
}
}
private val numericPrimitives = PrimitiveType.numericPrimitives.sortedBy { it.capacity }.toSet()
private val intPrimitives = setOf(PrimitiveType.Int, PrimitiveType.Long)
private val shortIntPrimitives = setOf(PrimitiveType.Byte, PrimitiveType.Short)
@@ -133,6 +143,7 @@ object ComparableOps : TemplateGroupBase() {
val f_minOf_2 = fn("minOf(a: T, b: T)") {
include(Generic)
include(Primitives, numericPrimitives)
include(Unsigned)
} builder {
sourceFile(SourceFile.Comparisons)
since("1.1")
@@ -146,18 +157,19 @@ object ComparableOps : TemplateGroupBase() {
"""
}
val defaultImpl = "if (a <= b) a else b"
body { "return $defaultImpl" }
specialFor(Primitives, Unsigned) {
doc { "Returns the smaller of two values." }
}
// TODO: Add a note about NaN propagation for floats.
specialFor(Primitives) {
inlineOnly()
doc {
"""Returns the smaller of two values."""
}
var convertBack = "to$primitive()"
on(Platform.JS) {
suppress("DEPRECATION_ERROR")
convertBack = "unsafeCast<$primitive>()"
}
body { "return $defaultImpl" }
on(Platform.JVM) {
body { "return Math.min(a, b)" }
}
@@ -188,22 +200,22 @@ object ComparableOps : TemplateGroupBase() {
}
}
}
on(Platform.JS) { /* just to make expect, KT-22520 */ }
body(Generic) {
"return $defaultImpl"
specialFor(Generic) {
on(Platform.JS) { /* just to make expect, KT-22520 */ }
}
}
val f_minOf_3 = fn("minOf(a: T, b: T, c: T)") {
include(Generic)
include(Primitives, numericPrimitives)
include(Unsigned)
} builder {
sourceFile(SourceFile.Comparisons)
since("1.1")
typeParam("T : Comparable<T>")
returns("T")
receiver("")
specialFor(Primitives) { inlineOnly() }
specialFor(Primitives, Unsigned) { inlineOnly() }
// TODO: Add a note about NaN propagation for floats.
doc {
"""
@@ -213,7 +225,9 @@ object ComparableOps : TemplateGroupBase() {
body {
"return minOf(a, minOf(b, c))"
}
on(Platform.JS) { /* just to make expect, KT-22520 */ }
specialFor(Primitives, Generic) {
on(Platform.JS) { /* just to make expect, KT-22520 */ }
}
specialFor(Primitives) {
if (primitive in shortIntPrimitives) {
body { "return minOf(a.toInt(), minOf(b.toInt(), c.toInt())).to$primitive()" }
@@ -272,6 +286,7 @@ object ComparableOps : TemplateGroupBase() {
val f_maxOf_2 = fn("maxOf(a: T, b: T)") {
include(Generic)
include(Primitives, numericPrimitives)
include(Unsigned)
} builder {
sourceFile(SourceFile.Comparisons)
since("1.1")
@@ -285,18 +300,19 @@ object ComparableOps : TemplateGroupBase() {
"""
}
val defaultImpl = "if (a >= b) a else b"
body { "return $defaultImpl" }
specialFor(Primitives, Unsigned) {
doc { "Returns the greater of two values." }
}
// TODO: Add a note about NaN propagation for floats.
specialFor(Primitives) {
inlineOnly()
doc {
"""Returns the greater of two values."""
}
var convertBack = "to$primitive()"
on(Platform.JS) {
suppress("DEPRECATION_ERROR")
convertBack = "unsafeCast<$primitive>()"
}
body { "return $defaultImpl" }
on(Platform.JVM) {
body { "return Math.max(a, b)" }
}
@@ -323,22 +339,22 @@ object ComparableOps : TemplateGroupBase() {
}
}
}
on(Platform.JS) { /* just to make expect, KT-22520 */ }
body(Generic) {
"return $defaultImpl"
specialFor(Generic) {
on(Platform.JS) { /* just to make expect, KT-22520 */ }
}
}
val f_maxOf_3 = fn("maxOf(a: T, b: T, c: T)") {
include(Generic)
include(Primitives, numericPrimitives)
include(Unsigned)
} builder {
sourceFile(SourceFile.Comparisons)
since("1.1")
typeParam("T : Comparable<T>")
returns("T")
receiver("")
specialFor(Primitives) { inlineOnly() }
specialFor(Primitives, Unsigned) { inlineOnly() }
// TODO: Add a note about NaN propagation for floats.
doc {
"""
@@ -348,7 +364,9 @@ object ComparableOps : TemplateGroupBase() {
body {
"return maxOf(a, maxOf(b, c))"
}
on(Platform.JS) { /* just to make expect, KT-22520 */ }
specialFor(Primitives, Generic) {
on(Platform.JS) { /* just to make expect, KT-22520 */ }
}
specialFor(Primitives) {
if (primitive in shortIntPrimitives) {
body { "return maxOf(a.toInt(), maxOf(b.toInt(), c.toInt())).to$primitive()" }
@@ -18,6 +18,7 @@ enum class SourceFile(jvmClassName: String? = null, val multifile: Boolean = tru
Ranges(packageName = "kotlin.ranges"),
URanges(packageName = "kotlin.ranges"),
Comparisons(packageName = "kotlin.comparisons"),
UComparisons(packageName = "kotlin.comparisons"),
Strings(packageName = "kotlin.text"),
Misc(),
;