From 19e001afad2bfadb987d5a5ee807e11caf1c8e19 Mon Sep 17 00:00:00 2001 From: Abduqodiri Qurbonzoda Date: Fri, 6 Dec 2019 08:23:04 +0300 Subject: [PATCH] Commonize Float.rangeTo(Float) #KT-35299 --- libraries/stdlib/js/src/kotlin/ranges.kt | 11 --- .../stdlib/jvm/src/kotlin/ranges/RangesJVM.kt | 50 ---------- libraries/stdlib/src/kotlin/ranges/Ranges.kt | 96 +++++++++++++------ 3 files changed, 69 insertions(+), 88 deletions(-) delete mode 100644 libraries/stdlib/js/src/kotlin/ranges.kt delete mode 100644 libraries/stdlib/jvm/src/kotlin/ranges/RangesJVM.kt diff --git a/libraries/stdlib/js/src/kotlin/ranges.kt b/libraries/stdlib/js/src/kotlin/ranges.kt deleted file mode 100644 index b53cd2a305f..00000000000 --- a/libraries/stdlib/js/src/kotlin/ranges.kt +++ /dev/null @@ -1,11 +0,0 @@ -/* - * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. - * 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.ranges - -@SinceKotlin("1.1") -@kotlin.internal.InlineOnly -public inline operator fun Float.rangeTo(that: Float): ClosedFloatingPointRange = - this.toDouble().rangeTo(that.toDouble()).unsafeCast>() diff --git a/libraries/stdlib/jvm/src/kotlin/ranges/RangesJVM.kt b/libraries/stdlib/jvm/src/kotlin/ranges/RangesJVM.kt deleted file mode 100644 index 7fce91ee045..00000000000 --- a/libraries/stdlib/jvm/src/kotlin/ranges/RangesJVM.kt +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. - * 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("RangesKt") - -package kotlin.ranges - -/** - * A closed range of values of type `Float`. - * - * Numbers are compared with the ends of this range according to IEEE-754. - */ -private class ClosedFloatRange( - start: Float, - endInclusive: Float -) : ClosedFloatingPointRange { - private val _start = start - private val _endInclusive = endInclusive - override val start: Float get() = _start - override val endInclusive: Float get() = _endInclusive - - override fun lessThanOrEquals(a: Float, b: Float): Boolean = a <= b - - override fun contains(value: Float): Boolean = value >= _start && value <= _endInclusive - override fun isEmpty(): Boolean = !(_start <= _endInclusive) - - override fun equals(other: Any?): Boolean { - return other is ClosedFloatRange && (isEmpty() && other.isEmpty() || - _start == other._start && _endInclusive == other._endInclusive) - } - - override fun hashCode(): Int { - return if (isEmpty()) -1 else 31 * _start.hashCode() + _endInclusive.hashCode() - } - - override fun toString(): String = "$_start..$_endInclusive" -} - -/** - * Creates a range from this [Float] value to the specified [that] value. - * - * Numbers are compared with the ends of this range according to IEEE-754. - * @sample samples.ranges.Ranges.rangeFromFloat - */ -@SinceKotlin("1.1") -public operator fun Float.rangeTo(that: Float): ClosedFloatingPointRange = ClosedFloatRange(this, that) - diff --git a/libraries/stdlib/src/kotlin/ranges/Ranges.kt b/libraries/stdlib/src/kotlin/ranges/Ranges.kt index d3e4b58087e..06bdc54bd1f 100644 --- a/libraries/stdlib/src/kotlin/ranges/Ranges.kt +++ b/libraries/stdlib/src/kotlin/ranges/Ranges.kt @@ -8,24 +8,6 @@ package kotlin.ranges -/** - * Represents a range of floating point numbers. - * Extends [ClosedRange] interface providing custom operation [lessThanOrEquals] for comparing values of range domain type. - * - * This interface is implemented by floating point ranges returned by [Float.rangeTo] and [Double.rangeTo] operators to - * achieve IEEE-754 comparison order instead of total order of floating point numbers. - */ -@SinceKotlin("1.1") -public interface ClosedFloatingPointRange> : ClosedRange { - override fun contains(value: T): Boolean = lessThanOrEquals(start, value) && lessThanOrEquals(value, endInclusive) - override fun isEmpty(): Boolean = !lessThanOrEquals(start, endInclusive) - - /** - * Compares two values of range domain type and returns true if first is less than or equal to second. - */ - fun lessThanOrEquals(a: T, b: T): Boolean -} - /** * Represents a range of [Comparable] values. */ @@ -46,6 +28,34 @@ private open class ComparableRange>( override fun toString(): String = "$start..$endInclusive" } +/** + * Creates a range from this [Comparable] value to the specified [that] value. + * + * This value needs to be smaller than [that] value, otherwise the returned range will be empty. + * @sample samples.ranges.Ranges.rangeFromComparable + */ +public operator fun > T.rangeTo(that: T): ClosedRange = ComparableRange(this, that) + + +/** + * Represents a range of floating point numbers. + * Extends [ClosedRange] interface providing custom operation [lessThanOrEquals] for comparing values of range domain type. + * + * This interface is implemented by floating point ranges returned by [Float.rangeTo] and [Double.rangeTo] operators to + * achieve IEEE-754 comparison order instead of total order of floating point numbers. + */ +@SinceKotlin("1.1") +public interface ClosedFloatingPointRange> : ClosedRange { + override fun contains(value: T): Boolean = lessThanOrEquals(start, value) && lessThanOrEquals(value, endInclusive) + override fun isEmpty(): Boolean = !lessThanOrEquals(start, endInclusive) + + /** + * Compares two values of range domain type and returns true if first is less than or equal to second. + */ + fun lessThanOrEquals(a: T, b: T): Boolean +} + + /** * A closed range of values of type `Double`. * @@ -77,15 +87,6 @@ private class ClosedDoubleRange( override fun toString(): String = "$_start..$_endInclusive" } - -/** - * Creates a range from this [Comparable] value to the specified [that] value. - * - * This value needs to be smaller than [that] value, otherwise the returned range will be empty. - * @sample samples.ranges.Ranges.rangeFromComparable - */ -public operator fun > T.rangeTo(that: T): ClosedRange = ComparableRange(this, that) - /** * Creates a range from this [Double] value to the specified [that] value. * @@ -96,6 +97,47 @@ public operator fun > T.rangeTo(that: T): ClosedRange = Com public operator fun Double.rangeTo(that: Double): ClosedFloatingPointRange = ClosedDoubleRange(this, that) +/** + * A closed range of values of type `Float`. + * + * Numbers are compared with the ends of this range according to IEEE-754. + */ +private class ClosedFloatRange( + start: Float, + endInclusive: Float +) : ClosedFloatingPointRange { + private val _start = start + private val _endInclusive = endInclusive + override val start: Float get() = _start + override val endInclusive: Float get() = _endInclusive + + override fun lessThanOrEquals(a: Float, b: Float): Boolean = a <= b + + override fun contains(value: Float): Boolean = value >= _start && value <= _endInclusive + override fun isEmpty(): Boolean = !(_start <= _endInclusive) + + override fun equals(other: Any?): Boolean { + return other is ClosedFloatRange && (isEmpty() && other.isEmpty() || + _start == other._start && _endInclusive == other._endInclusive) + } + + override fun hashCode(): Int { + return if (isEmpty()) -1 else 31 * _start.hashCode() + _endInclusive.hashCode() + } + + override fun toString(): String = "$_start..$_endInclusive" +} + +/** + * Creates a range from this [Float] value to the specified [that] value. + * + * Numbers are compared with the ends of this range according to IEEE-754. + * @sample samples.ranges.Ranges.rangeFromFloat + */ +@SinceKotlin("1.1") +public operator fun Float.rangeTo(that: Float): ClosedFloatingPointRange = ClosedFloatRange(this, that) + + /** * Returns `true` if this iterable range contains the specified [element]. *