Implement unsigned comparisons

This commit is contained in:
Ilya Gorbunov
2018-05-04 07:39:39 +03:00
parent e4216099b3
commit 89e4fdfa9c
6 changed files with 32 additions and 17 deletions
+7 -1
View File
@@ -86,7 +86,13 @@ class UnsignedTypeGenerator(val type: UnsignedType, out: PrintWriter) : BuiltIns
*/""")
out.print(" public ")
if (otherType == type) out.print("override ")
out.println("operator fun compareTo(other: ${otherType.capitalized}): Int = TODO()")
out.print("operator fun compareTo(other: ${otherType.capitalized}): Int = ")
if (otherType == type && maxByDomainCapacity(type, UnsignedType.UINT) == type) {
out.println("${className.toLowerCase()}Compare(this.data, other.data)")
} else {
val ctype = maxByDomainCapacity(maxByDomainCapacity(type, otherType), UnsignedType.UINT)
out.println("${convert("this", type, ctype)}.compareTo(${convert("other", otherType, ctype)})")
}
}
out.println()
}
@@ -28,28 +28,28 @@ public inline class UByte internal constructor(private val data: Byte) : Compara
* Returns zero if this value is equal to the specified other value, a negative number if it's less than other,
* or a positive number if it's greater than other.
*/
public override operator fun compareTo(other: UByte): Int = TODO()
public override operator fun compareTo(other: UByte): Int = this.toUInt().compareTo(other.toUInt())
/**
* Compares this value with the specified value for order.
* Returns zero if this value is equal to the specified other value, a negative number if it's less than other,
* or a positive number if it's greater than other.
*/
public operator fun compareTo(other: UShort): Int = TODO()
public operator fun compareTo(other: UShort): Int = this.toUInt().compareTo(other.toUInt())
/**
* Compares this value with the specified value for order.
* Returns zero if this value is equal to the specified other value, a negative number if it's less than other,
* or a positive number if it's greater than other.
*/
public operator fun compareTo(other: UInt): Int = TODO()
public operator fun compareTo(other: UInt): Int = this.toUInt().compareTo(other)
/**
* Compares this value with the specified value for order.
* Returns zero if this value is equal to the specified other value, a negative number if it's less than other,
* or a positive number if it's greater than other.
*/
public operator fun compareTo(other: ULong): Int = TODO()
public operator fun compareTo(other: ULong): Int = this.toULong().compareTo(other)
/** Adds the other value to this value. */
public operator fun plus(other: UByte): UInt = TODO()
+4 -4
View File
@@ -28,28 +28,28 @@ public inline class UInt internal constructor(private val data: Int) : Comparabl
* Returns zero if this value is equal to the specified other value, a negative number if it's less than other,
* or a positive number if it's greater than other.
*/
public operator fun compareTo(other: UByte): Int = TODO()
public operator fun compareTo(other: UByte): Int = this.compareTo(other.toUInt())
/**
* Compares this value with the specified value for order.
* Returns zero if this value is equal to the specified other value, a negative number if it's less than other,
* or a positive number if it's greater than other.
*/
public operator fun compareTo(other: UShort): Int = TODO()
public operator fun compareTo(other: UShort): Int = this.compareTo(other.toUInt())
/**
* Compares this value with the specified value for order.
* Returns zero if this value is equal to the specified other value, a negative number if it's less than other,
* or a positive number if it's greater than other.
*/
public override operator fun compareTo(other: UInt): Int = TODO()
public override operator fun compareTo(other: UInt): Int = uintCompare(this.data, other.data)
/**
* Compares this value with the specified value for order.
* Returns zero if this value is equal to the specified other value, a negative number if it's less than other,
* or a positive number if it's greater than other.
*/
public operator fun compareTo(other: ULong): Int = TODO()
public operator fun compareTo(other: ULong): Int = this.toULong().compareTo(other)
/** Adds the other value to this value. */
public operator fun plus(other: UByte): UInt = TODO()
@@ -28,28 +28,28 @@ public inline class ULong internal constructor(private val data: Long) : Compara
* Returns zero if this value is equal to the specified other value, a negative number if it's less than other,
* or a positive number if it's greater than other.
*/
public operator fun compareTo(other: UByte): Int = TODO()
public operator fun compareTo(other: UByte): Int = this.compareTo(other.toULong())
/**
* Compares this value with the specified value for order.
* Returns zero if this value is equal to the specified other value, a negative number if it's less than other,
* or a positive number if it's greater than other.
*/
public operator fun compareTo(other: UShort): Int = TODO()
public operator fun compareTo(other: UShort): Int = this.compareTo(other.toULong())
/**
* Compares this value with the specified value for order.
* Returns zero if this value is equal to the specified other value, a negative number if it's less than other,
* or a positive number if it's greater than other.
*/
public operator fun compareTo(other: UInt): Int = TODO()
public operator fun compareTo(other: UInt): Int = this.compareTo(other.toULong())
/**
* Compares this value with the specified value for order.
* Returns zero if this value is equal to the specified other value, a negative number if it's less than other,
* or a positive number if it's greater than other.
*/
public override operator fun compareTo(other: ULong): Int = TODO()
public override operator fun compareTo(other: ULong): Int = ulongCompare(this.data, other.data)
/** Adds the other value to this value. */
public operator fun plus(other: UByte): ULong = TODO()
@@ -28,28 +28,28 @@ public inline class UShort internal constructor(private val data: Short) : Compa
* Returns zero if this value is equal to the specified other value, a negative number if it's less than other,
* or a positive number if it's greater than other.
*/
public operator fun compareTo(other: UByte): Int = TODO()
public operator fun compareTo(other: UByte): Int = this.toUInt().compareTo(other.toUInt())
/**
* Compares this value with the specified value for order.
* Returns zero if this value is equal to the specified other value, a negative number if it's less than other,
* or a positive number if it's greater than other.
*/
public override operator fun compareTo(other: UShort): Int = TODO()
public override operator fun compareTo(other: UShort): Int = this.toUInt().compareTo(other.toUInt())
/**
* Compares this value with the specified value for order.
* Returns zero if this value is equal to the specified other value, a negative number if it's less than other,
* or a positive number if it's greater than other.
*/
public operator fun compareTo(other: UInt): Int = TODO()
public operator fun compareTo(other: UInt): Int = this.toUInt().compareTo(other)
/**
* Compares this value with the specified value for order.
* Returns zero if this value is equal to the specified other value, a negative number if it's less than other,
* or a positive number if it's greater than other.
*/
public operator fun compareTo(other: ULong): Int = TODO()
public operator fun compareTo(other: ULong): Int = this.toULong().compareTo(other)
/** Adds the other value to this value. */
public operator fun plus(other: UByte): UInt = TODO()
@@ -0,0 +1,9 @@
/*
* Copyright 2010-2018 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
internal fun uintCompare(v1: Int, v2: Int): Int = (v1 xor Int.MIN_VALUE).compareTo(v2 xor Int.MIN_VALUE)
internal fun ulongCompare(v1: Long, v2: Long): Int = (v1 xor Long.MIN_VALUE).compareTo(v2 xor Long.MIN_VALUE)