Drop deprecated primitive ranges and progressions: for Byte and Short

This commit is contained in:
Ilya Gorbunov
2015-11-27 18:44:55 +03:00
parent e8621cb738
commit 6dd8470835
11 changed files with 14 additions and 640 deletions
@@ -18,29 +18,6 @@
package kotlin.ranges
/**
* An iterator over a progression of values of type `Byte`.
* @property step the number by which the value is incremented on each step.
*/
internal class ByteProgressionIterator(first: Byte, last: Byte, val step: Int) : ByteIterator() {
private var next = first.toInt()
private val finalElement = last.toInt()
private var hasNext: Boolean = if (step > 0) first <= last else first >= last
override fun hasNext(): Boolean = hasNext
override fun nextByte(): Byte {
val value = next
if (value == finalElement) {
hasNext = false
}
else {
next += step
}
return value.toByte()
}
}
/**
* An iterator over a progression of values of type `Char`.
* @property step the number by which the value is incremented on each step.
@@ -64,29 +41,6 @@ internal class CharProgressionIterator(first: Char, last: Char, val step: Int) :
}
}
/**
* An iterator over a progression of values of type `Short`.
* @property step the number by which the value is incremented on each step.
*/
internal class ShortProgressionIterator(first: Short, last: Short, val step: Int) : ShortIterator() {
private var next = first.toInt()
private val finalElement = last.toInt()
private var hasNext: Boolean = if (step > 0) first <= last else first >= last
override fun hasNext(): Boolean = hasNext
override fun nextShort(): Short {
val value = next
if (value == finalElement) {
hasNext = false
}
else {
next += step
}
return value.toShort()
}
}
/**
* An iterator over a progression of values of type `Int`.
* @property step the number by which the value is incremented on each step.
-138
View File
@@ -20,75 +20,6 @@ package kotlin.ranges
import kotlin.internal.getProgressionLastElement
@Deprecated("Use IntProgression instead.", ReplaceWith("IntProgression"), level = DeprecationLevel.WARNING)
/**
* A progression of values of type `Byte`.
*/
public open class ByteProgression
@Deprecated("This constructor will become private soon. Use ByteProgression.fromClosedRange() instead.", ReplaceWith("ByteProgression.fromClosedRange(start, endInclusive, step)"))
public constructor
(
start: Byte,
endInclusive: Byte,
step: Int
) : Progression<Byte> /*, Iterable<Byte> */ {
init {
if (step == 0) throw IllegalArgumentException("Step must be non-zero")
}
/**
* The first element in the progression.
*/
public val first: Byte = start
/**
* The last element in the progression.
*/
public val last: Byte = getProgressionLastElement(start.toInt(), endInclusive.toInt(), step).toByte()
/**
* The step of the progression.
*/
public val step: Int = step
@Deprecated("Use 'first' property instead.", ReplaceWith("first"))
public override val start: Byte get() = first
/**
* The end value of the progression (inclusive).
*/
@Deprecated("Use 'last' property instead.")
public override val end: Byte = endInclusive
@Deprecated("Use 'step' property instead.", ReplaceWith("step"))
public override val increment: Int get() = step
override fun iterator(): ByteIterator = ByteProgressionIterator(first, last, step)
/** Checks if the progression is empty. */
public open fun isEmpty(): Boolean = if (step > 0) first > last else first < last
override fun equals(other: Any?): Boolean =
other is ByteProgression && (isEmpty() && other.isEmpty() ||
first == other.first && last == other.last && step == other.step)
override fun hashCode(): Int =
if (isEmpty()) -1 else (31 * (31 * first.toInt() + last.toInt()) + step)
override fun toString(): String = if (step > 0) "$first..$last step $step" else "$first downTo $last step ${-step}"
companion object {
/**
* Creates ByteProgression within the specified bounds of a closed range.
* The progression starts with the [rangeStart] value and goes toward the [rangeEnd] value not excluding it, with the specified [step].
* In order to go backwards the [step] must be negative.
*/
public fun fromClosedRange(rangeStart: Byte, rangeEnd: Byte, step: Int): ByteProgression = ByteProgression(rangeStart, rangeEnd, step)
}
}
/**
* A progression of values of type `Char`.
*/
@@ -157,75 +88,6 @@ public open class CharProgression
}
}
@Deprecated("Use IntProgression instead.", ReplaceWith("IntProgression"), level = DeprecationLevel.WARNING)
/**
* A progression of values of type `Short`.
*/
public open class ShortProgression
@Deprecated("This constructor will become private soon. Use ShortProgression.fromClosedRange() instead.", ReplaceWith("ShortProgression.fromClosedRange(start, endInclusive, step)"))
public constructor
(
start: Short,
endInclusive: Short,
step: Int
) : Progression<Short> /*, Iterable<Short> */ {
init {
if (step == 0) throw IllegalArgumentException("Step must be non-zero")
}
/**
* The first element in the progression.
*/
public val first: Short = start
/**
* The last element in the progression.
*/
public val last: Short = getProgressionLastElement(start.toInt(), endInclusive.toInt(), step).toShort()
/**
* The step of the progression.
*/
public val step: Int = step
@Deprecated("Use 'first' property instead.", ReplaceWith("first"))
public override val start: Short get() = first
/**
* The end value of the progression (inclusive).
*/
@Deprecated("Use 'last' property instead.")
public override val end: Short = endInclusive
@Deprecated("Use 'step' property instead.", ReplaceWith("step"))
public override val increment: Int get() = step
override fun iterator(): ShortIterator = ShortProgressionIterator(first, last, step)
/** Checks if the progression is empty. */
public open fun isEmpty(): Boolean = if (step > 0) first > last else first < last
override fun equals(other: Any?): Boolean =
other is ShortProgression && (isEmpty() && other.isEmpty() ||
first == other.first && last == other.last && step == other.step)
override fun hashCode(): Int =
if (isEmpty()) -1 else (31 * (31 * first.toInt() + last.toInt()) + step)
override fun toString(): String = if (step > 0) "$first..$last step $step" else "$first downTo $last step ${-step}"
companion object {
/**
* Creates ShortProgression within the specified bounds of a closed range.
* The progression starts with the [rangeStart] value and goes toward the [rangeEnd] value not excluding it, with the specified [step].
* In order to go backwards the [step] must be negative.
*/
public fun fromClosedRange(rangeStart: Short, rangeEnd: Short, step: Int): ShortProgression = ShortProgression(rangeStart, rangeEnd, step)
}
}
/**
* A progression of values of type `Int`.
*/
-60
View File
@@ -18,36 +18,6 @@
package kotlin.ranges
@Deprecated("Use IntRange instead.", ReplaceWith("IntRange"), level = DeprecationLevel.WARNING)
/**
* A range of values of type `Byte`.
*/
public class ByteRange(start: Byte, endInclusive: Byte) : ByteProgression(start, endInclusive, 1), ClosedRange<Byte> {
@Deprecated("Use endInclusive instead.", ReplaceWith("endInclusive"))
override val end: Byte get() = endInclusive
override val start: Byte get() = first
override val endInclusive: Byte get() = last
override fun contains(value: Byte): Boolean = start <= value && value <= endInclusive
override fun isEmpty(): Boolean = start > endInclusive
override fun equals(other: Any?): Boolean =
other is ByteRange && (isEmpty() && other.isEmpty() ||
start == other.start && endInclusive == other.endInclusive)
override fun hashCode(): Int =
if (isEmpty()) -1 else (31 * start.toInt() + endInclusive.toInt())
override fun toString(): String = "$start..$endInclusive"
companion object {
/** An empty range of values of type Byte. */
public val EMPTY: ByteRange = ByteRange(1, 0)
}
}
/**
* A range of values of type `Char`.
*/
@@ -77,36 +47,6 @@ public class CharRange(start: Char, endInclusive: Char) : CharProgression(start,
}
}
@Deprecated("Use IntRange instead.", ReplaceWith("IntRange"), level = DeprecationLevel.WARNING)
/**
* A range of values of type `Short`.
*/
public class ShortRange(start: Short, endInclusive: Short) : ShortProgression(start, endInclusive, 1), ClosedRange<Short> {
@Deprecated("Use endInclusive instead.", ReplaceWith("endInclusive"))
override val end: Short get() = endInclusive
override val start: Short get() = first
override val endInclusive: Short get() = last
override fun contains(value: Short): Boolean = start <= value && value <= endInclusive
override fun isEmpty(): Boolean = start > endInclusive
override fun equals(other: Any?): Boolean =
other is ShortRange && (isEmpty() && other.isEmpty() ||
start == other.start && endInclusive == other.endInclusive)
override fun hashCode(): Int =
if (isEmpty()) -1 else (31 * start.toInt() + endInclusive.toInt())
override fun toString(): String = "$start..$endInclusive"
companion object {
/** An empty range of values of type Short. */
public val EMPTY: ShortRange = ShortRange(1, 0)
}
}
/**
* A range of values of type `Int`.
*/
@@ -411,10 +411,9 @@ public abstract class KotlinBuiltIns {
@NotNull
public Set<DeclarationDescriptor> getIntegralRanges() {
return SetsKt.<DeclarationDescriptor>setOf(
getBuiltInClassByName("ByteRange", rangesPackageFragment),
getBuiltInClassByName("ShortRange", rangesPackageFragment),
getBuiltInClassByName("CharRange", rangesPackageFragment),
getBuiltInClassByName("IntRange", rangesPackageFragment)
// TODO: contains in LongRange should be optimized too
);
}
@@ -37,30 +37,19 @@ enum class PrimitiveType {
}
enum class ProgressionKind {
BYTE,
CHAR,
SHORT,
INT,
LONG,
FLOAT,
DOUBLE;
LONG;
val capitalized: String get() = name.toLowerCase().capitalize()
}
fun progressionIncrementType(kind: ProgressionKind) = when (kind) {
BYTE, CHAR, SHORT -> "Int"
CHAR -> "Int"
else -> kind.capitalized
}
fun areEqualNumbers(kind: ProgressionKind, v: String) = when (kind) {
FLOAT, DOUBLE -> "java.lang.${kind.capitalized}.compare($v, other.$v) == 0"
else -> "$v == other.$v"
}
fun areEqualNumbers(v: String) = "$v == other.$v"
fun hashLong(v: String) = "($v xor ($v ushr 32))"
fun floatToIntBits(v: String) = "java.lang.Float.floatToIntBits($v)"
fun doubleToLongBits(v: String) = "java.lang.Double.doubleToLongBits($v)"
@@ -27,7 +27,7 @@ fun integerProgressionIterator(kind: ProgressionKind): String {
val incrementType = progressionIncrementType(kind)
val (toInt, toType) = when (kind) {
BYTE, CHAR, SHORT -> ".toInt()" to ".to$t()"
CHAR -> ".toInt()" to ".to$t()"
else -> "" to ""
}
@@ -55,36 +55,12 @@ internal class ${t}ProgressionIterator(first: $t, last: $t, val step: $increment
}"""
}
fun floatingPointProgressionIterator(kind: ProgressionKind): String {
val t = kind.capitalized
return """/**
* An iterator over a progression of values of type `$t`.
* @property increment the number by which the value is incremented on each step.
*/
@Deprecated("This progression implementation has unclear semantics and will be removed soon.", level = DeprecationLevel.WARNING)
internal class ${t}ProgressionIterator(start: $t, val end: $t, val increment: $t) : ${t}Iterator() {
private var next = start
override fun hasNext(): Boolean = if (increment > 0) next <= end else next >= end
override fun next$t(): $t {
val value = next
next += increment
return value
}
}"""
}
class GenerateProgressionIterators(out: PrintWriter) : BuiltInsSourceGenerator(out) {
override fun getPackage() = "kotlin.ranges"
override fun generateBody() {
for (kind in ProgressionKind.values()) {
if (kind != FLOAT && kind != DOUBLE) {
out.println(integerProgressionIterator(kind))
}
else {
continue
}
out.println(integerProgressionIterator(kind))
out.println()
}
}
@@ -25,13 +25,11 @@ class GenerateProgressions(out: PrintWriter) : BuiltInsSourceGenerator(out) {
override fun getPackage() = "kotlin.ranges"
private fun generateDiscreteBody(kind: ProgressionKind) {
require(kind != FLOAT && kind != DOUBLE)
val t = kind.capitalized
val progression = "${t}Progression"
val incrementType = progressionIncrementType(kind)
fun compare(v: String) = areEqualNumbers(kind, v)
fun compare(v: String) = areEqualNumbers(v)
val zero = when (kind) {
LONG -> "0L"
@@ -40,7 +38,7 @@ class GenerateProgressions(out: PrintWriter) : BuiltInsSourceGenerator(out) {
val checkZero = "if (step == $zero) throw IllegalArgumentException(\"Step must be non-zero\")"
val hashCode = "=\n" + when (kind) {
BYTE, CHAR, SHORT ->
CHAR ->
" if (isEmpty()) -1 else (31 * (31 * first.toInt() + last.toInt()) + step)"
INT ->
" if (isEmpty()) -1 else (31 * (31 * first + last) + step)"
@@ -49,9 +47,6 @@ class GenerateProgressions(out: PrintWriter) : BuiltInsSourceGenerator(out) {
else -> throw IllegalArgumentException()
}
if (kind == SHORT || kind == BYTE) {
out.println("""@Deprecated("Use IntProgression instead.", ReplaceWith("IntProgression"), level = DeprecationLevel.WARNING)""")
}
out.println(
"""/**
* A progression of values of type `$t`.
@@ -123,83 +118,11 @@ public open class $progression
}
private fun generateFloatingPointBody(kind: ProgressionKind) {
require(kind == FLOAT || kind == DOUBLE)
val t = kind.capitalized
val progression = "${t}Progression"
val incrementType = progressionIncrementType(kind)
fun compare(v: String) = areEqualNumbers(kind, v)
val zero = when (kind) {
FLOAT -> "0.0f"
else -> "0.0"
}
val constructor = "if (java.lang.$t.isNaN(increment)) throw IllegalArgumentException(\"Increment must be not NaN\")\n" +
" if (increment == $zero) throw IllegalArgumentException(\"Increment must be non-zero\")"
val hashCode = when (kind) {
FLOAT -> "=\n" +
" if (isEmpty()) -1 else (31 * (31 * ${floatToIntBits("start")} + ${floatToIntBits("endInclusive")}) + ${floatToIntBits("increment")})"
else -> "{\n" +
" if (isEmpty()) return -1\n" +
" var temp = ${doubleToLongBits("start")}\n" +
" var result = ${hashLong("temp")}\n" +
" temp = ${doubleToLongBits("endInclusive")}\n" +
" result = 31 * result + ${hashLong("temp")}\n" +
" temp = ${doubleToLongBits("increment")}\n" +
" return (31 * result + ${hashLong("temp")}).toInt()\n" +
" }"
}
out.println(
"""/**
* A progression of values of type `$t`.
*/
@Deprecated("This progression implementation has unclear semantics and will be removed soon.", level = DeprecationLevel.WARNING)
public open class $progression(
override val start: $t,
val endInclusive: $t,
override val increment: $incrementType
) : Progression<$t> /*, Iterable<$t> */ {
init {
$constructor
}
/**
* The end value of the progression (inclusive).
*/
@Deprecated("Use endInclusive instead.", ReplaceWith("endInclusive"))
public override val end: $t get() = endInclusive
override fun iterator(): ${t}Iterator = ${t}ProgressionIterator(start, endInclusive, increment)
/** Checks if the progression is empty. */
public open fun isEmpty(): Boolean = if (increment > 0) start > endInclusive else start < endInclusive
override fun equals(other: Any?): Boolean =
other is $progression && (isEmpty() && other.isEmpty() ||
${compare("start")} && ${compare("endInclusive")} && ${compare("increment")})
override fun hashCode(): Int $hashCode
override fun toString(): String = ${"if (increment > 0) \"\$start..\$endInclusive step \$increment\" else \"\$start downTo \$endInclusive step \${-increment}\""}
}""")
out.println()
}
override fun generateBody() {
out.println("import kotlin.internal.getProgressionLastElement")
out.println()
for (kind in ProgressionKind.values()) {
if (kind == FLOAT || kind == DOUBLE)
continue
else
generateDiscreteBody(kind)
generateDiscreteBody(kind)
}
}
}
@@ -28,50 +28,26 @@ class GenerateRanges(out: PrintWriter) : BuiltInsSourceGenerator(out) {
val t = kind.capitalized
val range = "${t}Range"
val incrementType = progressionIncrementType(kind)
val increment = when (kind) {
FLOAT -> "1.0f"
DOUBLE -> "1.0"
else -> "1"
}
val increment = "1"
val emptyBounds = when (kind) {
CHAR -> "1.toChar(), 0.toChar()"
FLOAT -> "1.0f, 0.0f"
DOUBLE -> "1.0, 0.0"
else -> "1, 0"
}
fun compare(v: String) = areEqualNumbers(kind, v)
fun compare(v: String) = areEqualNumbers(v)
val hashCode = when (kind) {
BYTE, CHAR, SHORT -> "=\n" +
CHAR -> "=\n" +
" if (isEmpty()) -1 else (31 * start.toInt() + endInclusive.toInt())"
INT -> "=\n" +
" if (isEmpty()) -1 else (31 * start + endInclusive)"
LONG -> "=\n" +
" if (isEmpty()) -1 else (31 * ${hashLong("start")} + ${hashLong("endInclusive")}).toInt()"
FLOAT -> "=\n" +
" if (isEmpty()) -1 else (31 * ${floatToIntBits("start")} + ${floatToIntBits("endInclusive")})"
DOUBLE -> "{\n" +
" if (isEmpty()) return -1\n" +
" var temp = ${doubleToLongBits("start")}\n" +
" val result = ${hashLong("temp")}\n" +
" temp = ${doubleToLongBits("endInclusive")}\n" +
" return (31 * result + ${hashLong("temp")}).toInt()\n" +
" }"
}
val toString = "\"\$start..\$endInclusive\""
if (kind == FLOAT || kind == DOUBLE) {
continue
}
if (kind == SHORT || kind == BYTE) {
out.println("""@Deprecated("Use IntRange instead.", ReplaceWith("IntRange"), level = DeprecationLevel.WARNING)""")
}
out.println(
"""/**
* A range of values of type `$t`.
@@ -79,11 +55,10 @@ class GenerateRanges(out: PrintWriter) : BuiltInsSourceGenerator(out) {
public class $range(start: $t, endInclusive: $t) : ${t}Progression(start, endInclusive, $increment), ClosedRange<$t> {
@Deprecated("Use endInclusive instead.", ReplaceWith("endInclusive"))
override val end: $t get() = endInclusive
""" + (if (kind != FLOAT && kind != DOUBLE) """
override val start: $t get() = first
override val endInclusive: $t get() = last
""" else "") +
"""
override fun contains(value: $t): Boolean = start <= value && value <= endInclusive
override fun isEmpty(): Boolean = start > endInclusive
-130
View File
@@ -13,60 +13,6 @@ import java.util.*
import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
@[kotlin.jvm.JvmName("downTo") kotlin.jvm.JvmVersion]
public fun Byte.`-downTo`(to: Byte): ByteProgression {
return ByteProgression.fromClosedRange(this, to, -1)
}
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
@[kotlin.jvm.JvmName("downTo") kotlin.jvm.JvmVersion]
public fun Short.`-downTo`(to: Byte): ShortProgression {
return ShortProgression.fromClosedRange(this, to.toShort(), -1)
}
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
@[kotlin.jvm.JvmName("downTo") kotlin.jvm.JvmVersion]
public fun Byte.`-downTo`(to: Short): ShortProgression {
return ShortProgression.fromClosedRange(this.toShort(), to, -1)
}
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
@[kotlin.jvm.JvmName("downTo") kotlin.jvm.JvmVersion]
public fun Short.`-downTo`(to: Short): ShortProgression {
return ShortProgression.fromClosedRange(this, to, -1)
}
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
@[kotlin.jvm.JvmName("until") kotlin.jvm.JvmVersion]
public fun Byte.`-until`(to: Byte): ByteRange {
val to_ = (to - 1).toByte()
if (to_ > to) throw IllegalArgumentException("The to argument value '$to' was too small.")
return ByteRange(this, to_)
}
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
@[kotlin.jvm.JvmName("until") kotlin.jvm.JvmVersion]
public fun Short.`-until`(to: Byte): ShortRange {
return ShortRange(this, (to.toShort() - 1).toShort())
}
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
@[kotlin.jvm.JvmName("until") kotlin.jvm.JvmVersion]
public fun Byte.`-until`(to: Short): ShortRange {
val to_ = (to - 1).toShort()
if (to_ > to) throw IllegalArgumentException("The to argument value '$to' was too small.")
return ShortRange(this.toShort(), to_)
}
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
@[kotlin.jvm.JvmName("until") kotlin.jvm.JvmVersion]
public fun Short.`-until`(to: Short): ShortRange {
val to_ = (to - 1).toShort()
if (to_ > to) throw IllegalArgumentException("The to argument value '$to' was too small.")
return ShortRange(this, to_)
}
/**
* Checks if the specified [value] belongs to this range.
*/
@@ -488,42 +434,6 @@ public fun LongRange.reversed(): LongProgression {
return LongProgression.fromClosedRange(last, first, -1L)
}
/**
* Returns a progression that goes over the same range in the opposite direction with the same step.
*/
@Deprecated("This range implementation has unclear semantics and will be removed soon.")
@Suppress("DEPRECATION_ERROR")
public fun ByteProgression.reversed(): ByteProgression {
return ByteProgression.fromClosedRange(last, first, -step)
}
/**
* Returns a progression that goes over the same range in the opposite direction with the same step.
*/
@Deprecated("This range implementation has unclear semantics and will be removed soon.")
@Suppress("DEPRECATION_ERROR")
public fun ShortProgression.reversed(): ShortProgression {
return ShortProgression.fromClosedRange(last, first, -step)
}
/**
* Returns a progression that goes over this range in reverse direction.
*/
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
@Suppress("DEPRECATION_ERROR")
public fun ByteRange.reversed(): ByteProgression {
return ByteProgression.fromClosedRange(last, first, -1)
}
/**
* Returns a progression that goes over this range in reverse direction.
*/
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
@Suppress("DEPRECATION_ERROR")
public fun ShortRange.reversed(): ShortProgression {
return ShortProgression.fromClosedRange(last, first, -1)
}
/**
* Returns a progression that goes over the same range with the given step.
*/
@@ -575,46 +485,6 @@ public infix fun LongRange.step(step: Long): LongProgression {
return LongProgression.fromClosedRange(first, last, step)
}
/**
* Returns a progression that goes over the same range with the given step.
*/
@Deprecated("This range implementation has unclear semantics and will be removed soon.")
@Suppress("DEPRECATION_ERROR")
public infix fun ByteProgression.step(step: Int): ByteProgression {
checkStepIsPositive(step > 0, step)
return ByteProgression.fromClosedRange(first, last, if (this.step > 0) step else -step)
}
/**
* Returns a progression that goes over the same range with the given step.
*/
@Deprecated("This range implementation has unclear semantics and will be removed soon.")
@Suppress("DEPRECATION_ERROR")
public infix fun ShortProgression.step(step: Int): ShortProgression {
checkStepIsPositive(step > 0, step)
return ShortProgression.fromClosedRange(first, last, if (this.step > 0) step else -step)
}
/**
* Returns a progression that goes over this range with given step.
*/
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
@Suppress("DEPRECATION_ERROR")
public infix fun ByteRange.step(step: Int): ByteProgression {
checkStepIsPositive(step > 0, step)
return ByteProgression.fromClosedRange(first, last, step)
}
/**
* Returns a progression that goes over this range with given step.
*/
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
@Suppress("DEPRECATION_ERROR")
public infix fun ShortRange.step(step: Int): ShortProgression {
checkStepIsPositive(step > 0, step)
return ShortProgression.fromClosedRange(first, last, step)
}
/**
* Returns a range from this value up to but excluding the specified [to] value.
*/
@@ -57,8 +57,6 @@ public class RangeIterationTest : RangeIterationTestBase() {
@test fun emptyConstant() {
doTest(IntRange.EMPTY, 1, 0, 1, listOf())
doTest(ByteRange.EMPTY, 1.toByte(), 0.toByte(), 1, listOf())
doTest(ShortRange.EMPTY, 1.toShort(), 0.toShort(), 1, listOf())
doTest(LongRange.EMPTY, 1.toLong(), 0.toLong(), 1.toLong(), listOf())
doTest(CharRange.EMPTY, 1.toChar(), 0.toChar(), 1, listOf())
@@ -7,7 +7,6 @@ fun ranges(): List<GenericFunction> {
val templates = arrayListOf<GenericFunction>()
val rangePrimitives = listOf(PrimitiveType.Int, PrimitiveType.Long, PrimitiveType.Char)
val floatingPointPrimitives = listOf(PrimitiveType.Double, PrimitiveType.Float)
fun rangeElementType(fromType: PrimitiveType, toType: PrimitiveType)
= maxByCapacity(fromType, toType).let { if (it == PrimitiveType.Char) it else maxByCapacity(it, PrimitiveType.Int) }
@@ -28,23 +27,6 @@ fun ranges(): List<GenericFunction> {
}
}
templates add f("reversed()") {
only(RangesOfPrimitives, ProgressionsOfPrimitives)
only(defaultPrimitives - PrimitiveType.Boolean - rangePrimitives - floatingPointPrimitives)
doc(ProgressionsOfPrimitives) { "Returns a progression that goes over the same range in the opposite direction with the same step." }
doc(RangesOfPrimitives) { "Returns a progression that goes over this range in reverse direction." }
returns("TProgression")
deprecate(Deprecation("This range implementation has unclear semantics and will be removed soon.", level = DeprecationLevel.WARNING))
deprecate(RangesOfPrimitives) { forBinaryCompatibility }
annotations("""@Suppress("DEPRECATION_ERROR")""")
body(RangesOfPrimitives) {
"return TProgression.fromClosedRange(last, first, -ONE)"
}
body(ProgressionsOfPrimitives) {
"return TProgression.fromClosedRange(last, first, -step)"
}
}
templates add f("step(step: SUM)") {
infix(true)
@@ -68,39 +50,6 @@ fun ranges(): List<GenericFunction> {
}
}
templates add f("step(step: SUM)") {
infix(true)
only(RangesOfPrimitives, ProgressionsOfPrimitives)
only(defaultPrimitives - PrimitiveType.Boolean - rangePrimitives - floatingPointPrimitives)
doc(ProgressionsOfPrimitives) { "Returns a progression that goes over the same range with the given step." }
doc(RangesOfPrimitives) { "Returns a progression that goes over this range with given step." }
returns("TProgression")
deprecate(Deprecation("This range implementation has unclear semantics and will be removed soon.", level = DeprecationLevel.WARNING))
deprecate(RangesOfPrimitives) { forBinaryCompatibility }
annotations("""@Suppress("DEPRECATION_ERROR")""")
body(RangesOfPrimitives) {
"""
checkStepIsPositive(step > 0, step)
return TProgression.fromClosedRange(first, last, step)
"""
}
bodyForTypes(RangesOfPrimitives, PrimitiveType.Float, PrimitiveType.Double) {
"""
if (step.isNaN()) throw IllegalArgumentException("Step must not be NaN.")
checkStepIsPositive(step > 0, step)
return TProgression.fromClosedRange(start, end, step)
"""
}
body(ProgressionsOfPrimitives) {
"""
checkStepIsPositive(step > 0, step)
return TProgression.fromClosedRange(first, last, if (this.step > 0) step else -step)
"""
}
}
fun downTo(fromType: PrimitiveType, toType: PrimitiveType) = f("downTo(to: $toType)") {
infix(true)
@@ -118,32 +67,6 @@ fun ranges(): List<GenericFunction> {
"""
}
if (!fromType.isIntegral() || !toType.isIntegral()) {
deprecate(Deprecation("This range implementation has unclear semantics and will be removed soon.", level = DeprecationLevel.WARNING))
annotations("""@Suppress("DEPRECATION_ERROR")""")
}
val fromExpr = if (elementType == fromType) "this" else "this.to$elementType()"
val toExpr = if (elementType == toType) "to" else "to.to$elementType()"
val incrementExpr = when (elementType) {
PrimitiveType.Long -> "-1L"
PrimitiveType.Float -> "-1.0F"
PrimitiveType.Double -> "-1.0"
else -> "-1"
}
body { "return $progressionType.fromClosedRange($fromExpr, $toExpr, $incrementExpr)" }
}
fun downToDeprecated(fromType: PrimitiveType, toType: PrimitiveType) = f("`-downTo`(to: $toType)") {
deprecate { forBinaryCompatibility }
sourceFile(SourceFile.Ranges)
only(Primitives)
only(fromType)
val elementType = maxByCapacity(fromType, toType)
val progressionType = elementType.name + "Progression"
returns(progressionType)
annotations("""@[kotlin.jvm.JvmName("downTo") kotlin.jvm.JvmVersion]""")
val fromExpr = if (elementType == fromType) "this" else "this.to$elementType()"
val toExpr = if (elementType == toType) "to" else "to.to$elementType()"
val incrementExpr = when (elementType) {
@@ -162,7 +85,6 @@ fun ranges(): List<GenericFunction> {
val integralPermutations = primitivePermutations.filter { it.first.isIntegral() && it.second.isIntegral() }
templates addAll integralPermutations.map { downTo(it.first, it.second) }
templates addAll listOf(PrimitiveType.Byte, PrimitiveType.Short).permutations().map { downToDeprecated(it.first, it.second) }
fun until(fromType: PrimitiveType, toType: PrimitiveType) = f("until(to: $toType)") {
infix(true)
@@ -181,11 +103,6 @@ fun ranges(): List<GenericFunction> {
"""
}
if (!fromType.isIntegral() || !toType.isIntegral()) {
deprecate(Deprecation("This range implementation has unclear semantics and will be removed soon.", level = DeprecationLevel.WARNING))
annotations("""@Suppress("DEPRECATION_ERROR")""")
}
val fromExpr = if (elementType == fromType) "this" else "this.to$elementType()"
if (elementType == toType) {
@@ -207,36 +124,7 @@ fun ranges(): List<GenericFunction> {
}
}
fun untilDeprecated(fromType: PrimitiveType, toType: PrimitiveType) = f("`-until`(to: $toType)") {
deprecate { forBinaryCompatibility }
sourceFile(SourceFile.Ranges)
only(Primitives)
only(fromType)
val elementType = maxByCapacity(fromType, toType)
val progressionType = elementType.name + "Range"
returns(progressionType)
annotations("""@[kotlin.jvm.JvmName("until") kotlin.jvm.JvmVersion]""")
val fromExpr = if (elementType == fromType) "this" else "this.to$elementType()"
if (elementType == toType) {
// hack to work around incorrect char overflow behavior in JVM and int overflow behavior in JS
val toExpr = "to"
body {
"""
val to_ = ($toExpr - 1).to$elementType()
if (to_ > to) throw IllegalArgumentException("The to argument value '${'$'}to' was too small.")
return $progressionType($fromExpr, to_)
"""
}
} else {
body { "return $progressionType($fromExpr, (to.to$elementType() - 1).to$elementType())" }
}
}
templates addAll integralPermutations.map { until(it.first, it.second) }
templates addAll listOf(PrimitiveType.Byte, PrimitiveType.Short).permutations().map { untilDeprecated(it.first, it.second) }
fun contains(rangeType: PrimitiveType, itemType: PrimitiveType) = f("contains(value: $itemType)") {
operator(true)