downTo functions return sequence instead of range now.

This commit is contained in:
Evgeny Gerashchenko
2013-01-23 13:47:08 +04:00
parent 4736b8d6e7
commit 0051cb9553
2 changed files with 112 additions and 110 deletions
+98 -98
View File
@@ -6,198 +6,198 @@ package kotlin
// //
public inline fun Byte.downTo(to: Byte): ByteRange { public inline fun Byte.downTo(to: Byte): ByteSequence {
return if (this >= to) ByteRange(this, to - this - 1) else ByteRange.EMPTY return ByteSequence(this, to, -1)
} }
public inline fun Byte.downTo(to: Char): CharRange { public inline fun Byte.downTo(to: Char): CharacterSequence {
return if (this >= to) CharRange(this.toChar(), to - this - 1) else CharRange.EMPTY return CharacterSequence(this.toChar(), to, -1)
} }
public inline fun Byte.downTo(to: Short): ShortRange { public inline fun Byte.downTo(to: Short): ShortSequence {
return if (this >= to) ShortRange(this.toShort(), to - this - 1) else ShortRange.EMPTY return ShortSequence(this.toShort(), to, -1)
} }
public inline fun Byte.downTo(to: Int): IntRange { public inline fun Byte.downTo(to: Int): IntSequence {
return if (this >= to) IntRange(this.toInt(), to - this - 1) else IntRange.EMPTY return IntSequence(this.toInt(), to, -1)
} }
public inline fun Byte.downTo(to: Long): LongRange { public inline fun Byte.downTo(to: Long): LongSequence {
return if (this >= to) LongRange(this.toLong(), to - this - 1) else LongRange.EMPTY return LongSequence(this.toLong(), to, -1.toLong())
} }
public inline fun Byte.downTo(to: Float): FloatRange { public inline fun Byte.downTo(to: Float): FloatSequence {
return FloatRange(this.toFloat(), to - this) return FloatSequence(this.toFloat(), to, -1.toFloat())
} }
public inline fun Byte.downTo(to: Double): DoubleRange { public inline fun Byte.downTo(to: Double): DoubleSequence {
return DoubleRange(this.toDouble(), to - this) return DoubleSequence(this.toDouble(), to, -1.0)
} }
public inline fun Char.downTo(to: Byte): CharRange { public inline fun Char.downTo(to: Byte): CharacterSequence {
return if (this >= to) CharRange(this, to - this - 1) else CharRange.EMPTY return CharacterSequence(this, to.toChar(), -1)
} }
public inline fun Char.downTo(to: Char): CharRange { public inline fun Char.downTo(to: Char): CharacterSequence {
return if (this >= to) CharRange(this, to - this - 1) else CharRange.EMPTY return CharacterSequence(this, to, -1)
} }
public inline fun Char.downTo(to: Short): ShortRange { public inline fun Char.downTo(to: Short): ShortSequence {
return if (this >= to) ShortRange(this.toShort(), to - this - 1) else ShortRange.EMPTY return ShortSequence(this.toShort(), to, -1)
} }
public inline fun Char.downTo(to: Int): IntRange { public inline fun Char.downTo(to: Int): IntSequence {
return if (this >= to) IntRange(this.toInt(), to - this - 1) else IntRange.EMPTY return IntSequence(this.toInt(), to, -1)
} }
public inline fun Char.downTo(to: Long): LongRange { public inline fun Char.downTo(to: Long): LongSequence {
return if (this >= to) LongRange(this.toLong(), to - this - 1) else LongRange.EMPTY return LongSequence(this.toLong(), to, -1.toLong())
} }
public inline fun Char.downTo(to: Float): FloatRange { public inline fun Char.downTo(to: Float): FloatSequence {
return FloatRange(this.toFloat(), to - this) return FloatSequence(this.toFloat(), to, -1.toFloat())
} }
public inline fun Char.downTo(to: Double): DoubleRange { public inline fun Char.downTo(to: Double): DoubleSequence {
return DoubleRange(this.toDouble(), to - this) return DoubleSequence(this.toDouble(), to, -1.0)
} }
public inline fun Short.downTo(to: Byte): ShortRange { public inline fun Short.downTo(to: Byte): ShortSequence {
return if (this >= to) ShortRange(this, to - this - 1) else ShortRange.EMPTY return ShortSequence(this, to.toShort(), -1)
} }
public inline fun Short.downTo(to: Char): ShortRange { public inline fun Short.downTo(to: Char): ShortSequence {
return if (this >= to) ShortRange(this, to - this - 1) else ShortRange.EMPTY return ShortSequence(this, to.toShort(), -1)
} }
public inline fun Short.downTo(to: Short): ShortRange { public inline fun Short.downTo(to: Short): ShortSequence {
return if (this >= to) ShortRange(this, to - this - 1) else ShortRange.EMPTY return ShortSequence(this, to, -1)
} }
public inline fun Short.downTo(to: Int): IntRange { public inline fun Short.downTo(to: Int): IntSequence {
return if (this >= to) IntRange(this.toInt(), to - this - 1) else IntRange.EMPTY return IntSequence(this.toInt(), to, -1)
} }
public inline fun Short.downTo(to: Long): LongRange { public inline fun Short.downTo(to: Long): LongSequence {
return if (this >= to) LongRange(this.toLong(), to - this - 1) else LongRange.EMPTY return LongSequence(this.toLong(), to, -1.toLong())
} }
public inline fun Short.downTo(to: Float): FloatRange { public inline fun Short.downTo(to: Float): FloatSequence {
return FloatRange(this.toFloat(), to - this) return FloatSequence(this.toFloat(), to, -1.toFloat())
} }
public inline fun Short.downTo(to: Double): DoubleRange { public inline fun Short.downTo(to: Double): DoubleSequence {
return DoubleRange(this.toDouble(), to - this) return DoubleSequence(this.toDouble(), to, -1.0)
} }
public inline fun Int.downTo(to: Byte): IntRange { public inline fun Int.downTo(to: Byte): IntSequence {
return if (this >= to) IntRange(this, to - this - 1) else IntRange.EMPTY return IntSequence(this, to.toInt(), -1)
} }
public inline fun Int.downTo(to: Char): IntRange { public inline fun Int.downTo(to: Char): IntSequence {
return if (this >= to) IntRange(this, to - this - 1) else IntRange.EMPTY return IntSequence(this, to.toInt(), -1)
} }
public inline fun Int.downTo(to: Short): IntRange { public inline fun Int.downTo(to: Short): IntSequence {
return if (this >= to) IntRange(this, to - this - 1) else IntRange.EMPTY return IntSequence(this, to.toInt(), -1)
} }
public inline fun Int.downTo(to: Int): IntRange { public inline fun Int.downTo(to: Int): IntSequence {
return if (this >= to) IntRange(this, to - this - 1) else IntRange.EMPTY return IntSequence(this, to, -1)
} }
public inline fun Int.downTo(to: Long): LongRange { public inline fun Int.downTo(to: Long): LongSequence {
return if (this >= to) LongRange(this.toLong(), to - this - 1) else LongRange.EMPTY return LongSequence(this.toLong(), to, -1.toLong())
} }
public inline fun Int.downTo(to: Float): FloatRange { public inline fun Int.downTo(to: Float): FloatSequence {
return FloatRange(this.toFloat(), to - this) return FloatSequence(this.toFloat(), to, -1.toFloat())
} }
public inline fun Int.downTo(to: Double): DoubleRange { public inline fun Int.downTo(to: Double): DoubleSequence {
return DoubleRange(this.toDouble(), to - this) return DoubleSequence(this.toDouble(), to, -1.0)
} }
public inline fun Long.downTo(to: Byte): LongRange { public inline fun Long.downTo(to: Byte): LongSequence {
return if (this >= to) LongRange(this, to - this - 1) else LongRange.EMPTY return LongSequence(this, to.toLong(), -1.toLong())
} }
public inline fun Long.downTo(to: Char): LongRange { public inline fun Long.downTo(to: Char): LongSequence {
return if (this >= to) LongRange(this, to - this - 1) else LongRange.EMPTY return LongSequence(this, to.toLong(), -1.toLong())
} }
public inline fun Long.downTo(to: Short): LongRange { public inline fun Long.downTo(to: Short): LongSequence {
return if (this >= to) LongRange(this, to - this - 1) else LongRange.EMPTY return LongSequence(this, to.toLong(), -1.toLong())
} }
public inline fun Long.downTo(to: Int): LongRange { public inline fun Long.downTo(to: Int): LongSequence {
return if (this >= to) LongRange(this, to - this - 1) else LongRange.EMPTY return LongSequence(this, to.toLong(), -1.toLong())
} }
public inline fun Long.downTo(to: Long): LongRange { public inline fun Long.downTo(to: Long): LongSequence {
return if (this >= to) LongRange(this, to - this - 1) else LongRange.EMPTY return LongSequence(this, to, -1.toLong())
} }
public inline fun Long.downTo(to: Float): FloatRange { public inline fun Long.downTo(to: Float): FloatSequence {
return FloatRange(this.toFloat(), to - this) return FloatSequence(this.toFloat(), to, -1.toFloat())
} }
public inline fun Long.downTo(to: Double): DoubleRange { public inline fun Long.downTo(to: Double): DoubleSequence {
return DoubleRange(this.toDouble(), to - this) return DoubleSequence(this.toDouble(), to, -1.0)
} }
public inline fun Float.downTo(to: Byte): FloatRange { public inline fun Float.downTo(to: Byte): FloatSequence {
return FloatRange(this, to - this) return FloatSequence(this, to.toFloat(), -1.toFloat())
} }
public inline fun Float.downTo(to: Char): FloatRange { public inline fun Float.downTo(to: Char): FloatSequence {
return FloatRange(this, to - this) return FloatSequence(this, to.toFloat(), -1.toFloat())
} }
public inline fun Float.downTo(to: Short): FloatRange { public inline fun Float.downTo(to: Short): FloatSequence {
return FloatRange(this, to - this) return FloatSequence(this, to.toFloat(), -1.toFloat())
} }
public inline fun Float.downTo(to: Int): FloatRange { public inline fun Float.downTo(to: Int): FloatSequence {
return FloatRange(this, to - this) return FloatSequence(this, to.toFloat(), -1.toFloat())
} }
public inline fun Float.downTo(to: Long): FloatRange { public inline fun Float.downTo(to: Long): FloatSequence {
return FloatRange(this, to - this) return FloatSequence(this, to.toFloat(), -1.toFloat())
} }
public inline fun Float.downTo(to: Float): FloatRange { public inline fun Float.downTo(to: Float): FloatSequence {
return FloatRange(this, to - this) return FloatSequence(this, to, -1.toFloat())
} }
public inline fun Float.downTo(to: Double): DoubleRange { public inline fun Float.downTo(to: Double): DoubleSequence {
return DoubleRange(this.toDouble(), to - this) return DoubleSequence(this.toDouble(), to, -1.0)
} }
public inline fun Double.downTo(to: Byte): DoubleRange { public inline fun Double.downTo(to: Byte): DoubleSequence {
return DoubleRange(this, to - this) return DoubleSequence(this, to.toDouble(), -1.0)
} }
public inline fun Double.downTo(to: Char): DoubleRange { public inline fun Double.downTo(to: Char): DoubleSequence {
return DoubleRange(this, to - this) return DoubleSequence(this, to.toDouble(), -1.0)
} }
public inline fun Double.downTo(to: Short): DoubleRange { public inline fun Double.downTo(to: Short): DoubleSequence {
return DoubleRange(this, to - this) return DoubleSequence(this, to.toDouble(), -1.0)
} }
public inline fun Double.downTo(to: Int): DoubleRange { public inline fun Double.downTo(to: Int): DoubleSequence {
return DoubleRange(this, to - this) return DoubleSequence(this, to.toDouble(), -1.0)
} }
public inline fun Double.downTo(to: Long): DoubleRange { public inline fun Double.downTo(to: Long): DoubleSequence {
return DoubleRange(this, to - this) return DoubleSequence(this, to.toDouble(), -1.0)
} }
public inline fun Double.downTo(to: Float): DoubleRange { public inline fun Double.downTo(to: Float): DoubleSequence {
return DoubleRange(this, to - this) return DoubleSequence(this, to.toDouble(), -1.0)
} }
public inline fun Double.downTo(to: Double): DoubleRange { public inline fun Double.downTo(to: Double): DoubleSequence {
return DoubleRange(this, to - this) return DoubleSequence(this, to, -1.0)
} }
@@ -20,19 +20,21 @@ private fun generateDownTos(outputFile: File, header: String) {
fun generateDownTo(writer: PrintWriter, fromType: String, toType: String) { fun generateDownTo(writer: PrintWriter, fromType: String, toType: String) {
val elementType = getMaxType(fromType, toType) val elementType = getMaxType(fromType, toType)
val rangeType = elementType + "Range" val rangeType = if (elementType == "Char") "CharacterSequence" else elementType + "Sequence"
val fromExpr = "this${ if (elementType == fromType) "" else ".to$elementType()" }"
if (elementType == "Float" || elementType == "Double") { val fromExpr = if (elementType == fromType) "this" else "this.to$elementType()"
writer.println(""" val toExpr = if (elementType == toType) "to" else "to.to$elementType()"
public inline fun $fromType.downTo(to: $toType): $rangeType { val incrementExpr = when (elementType) {
return $rangeType($fromExpr, to - this) "Long" -> "-1.toLong()"
}""") "Float" -> "-1.toFloat()"
} else { "Double" -> "-1.0"
writer.println(""" else -> "-1"
public inline fun $fromType.downTo(to: $toType): $rangeType {
return if (this >= to) $rangeType($fromExpr, to - this - 1) else $rangeType.EMPTY
}""")
} }
writer.println("""
public inline fun $fromType.downTo(to: $toType): $rangeType {
return $rangeType($fromExpr, $toExpr, $incrementExpr)
}""")
} }
println("Writing $outputFile") println("Writing $outputFile")