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
@@ -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