Commonize Float.rangeTo(Float) #KT-35299
(cherry picked from commit ae819b82974e585040c9c889e0cd8376bc2efdb7)
This commit is contained in:
committed by
Vasily Levchenko
parent
e1bb426e55
commit
fd1a0804f3
@@ -82,40 +82,3 @@ public class LongRange(start: Long, endInclusive: Long) : LongProgression(start,
|
||||
public val EMPTY: LongRange = LongRange(1, 0)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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<Float> {
|
||||
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.
|
||||
*/
|
||||
public operator fun Float.rangeTo(that: Float): ClosedFloatingPointRange<Float> = ClosedFloatRange(this, that)
|
||||
|
||||
Reference in New Issue
Block a user