Fix equals and hashCode for empty ranges and progressions

Empty range is equal to any other empty range
This commit is contained in:
Alexander Udalov
2014-06-16 19:12:00 +04:00
parent 751f062f23
commit ad23a2d05b
6 changed files with 134 additions and 38 deletions
@@ -45,11 +45,16 @@ class GenerateProgressions(out: PrintWriter) : BuiltInsSourceGenerator(out) {
val constructor = checkNaN + checkZero
val hashCode = when (kind) {
BYTE, CHAR, SHORT -> "= 31 * (31 * start.toInt() + end) + increment"
INT -> "= 31 * (31 * start + end) + increment"
LONG -> "= (31 * (31 * ${hashLong("start")} + ${hashLong("end")}) + ${hashLong("increment")}).toInt()"
FLOAT -> "= 31 * (31 * ${floatToIntBits("start")} + ${floatToIntBits("end")}) + ${floatToIntBits("increment")}"
BYTE, CHAR, SHORT -> "=\n" +
" if (isEmpty()) -1 else (31 * (31 * start.toInt() + end) + increment)"
INT -> "=\n" +
" if (isEmpty()) -1 else (31 * (31 * start + end) + increment)"
LONG -> "=\n" +
" if (isEmpty()) -1 else (31 * (31 * ${hashLong("start")} + ${hashLong("end")}) + ${hashLong("increment")}).toInt()"
FLOAT -> "=\n" +
" if (isEmpty()) -1 else (31 * (31 * ${floatToIntBits("start")} + ${floatToIntBits("end")}) + ${floatToIntBits("increment")})"
DOUBLE -> "{\n" +
" if (isEmpty()) return -1\n" +
" var temp = ${doubleToLongBits("start")}\n" +
" var result = ${hashLong("temp")}\n" +
" temp = ${doubleToLongBits("end")}\n" +
@@ -74,7 +79,8 @@ class GenerateProgressions(out: PrintWriter) : BuiltInsSourceGenerator(out) {
public fun isEmpty(): Boolean = if (increment > 0) start > end else start < end
override fun equals(other: Any?): Boolean =
other is $progression && ${compare("start")} && ${compare("end")} && ${compare("increment")}
other is $progression && (isEmpty() && other.isEmpty() ||
${compare("start")} && ${compare("end")} && ${compare("increment")})
override fun hashCode(): Int $hashCode
@@ -45,11 +45,16 @@ class GenerateRanges(out: PrintWriter) : BuiltInsSourceGenerator(out) {
fun compare(v: String) = areEqualNumbers(kind, v)
val hashCode = when (kind) {
BYTE, CHAR, SHORT -> "= 31 * start.toInt() + end"
INT -> "= 31 * start + end"
LONG -> "= (31 * ${hashLong("start")} + ${hashLong("end")}).toInt()"
FLOAT -> "= 31 * ${floatToIntBits("start")} + ${floatToIntBits("end")}"
BYTE, CHAR, SHORT -> "=\n" +
" if (isEmpty()) -1 else (31 * start.toInt() + end)"
INT -> "=\n" +
" if (isEmpty()) -1 else (31 * start + end)"
LONG -> "=\n" +
" if (isEmpty()) -1 else (31 * ${hashLong("start")} + ${hashLong("end")}).toInt()"
FLOAT -> "=\n" +
" if (isEmpty()) -1 else (31 * ${floatToIntBits("start")} + ${floatToIntBits("end")})"
DOUBLE -> "{\n" +
" if (isEmpty()) return -1\n" +
" var temp = ${doubleToLongBits("start")}\n" +
" val result = ${hashLong("temp")}\n" +
" temp = ${doubleToLongBits("end")}\n" +
@@ -69,7 +74,8 @@ class GenerateRanges(out: PrintWriter) : BuiltInsSourceGenerator(out) {
override fun isEmpty(): Boolean = start > end
override fun equals(other: Any?): Boolean =
other is $range && ${compare("start")} && ${compare("end")}
other is $range && (isEmpty() && other.isEmpty() ||
${compare("start")} && ${compare("end")})
override fun hashCode(): Int $hashCode