From 4352023d6021f2a3ec1ce06159f67ba894fa47b5 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Tue, 10 Feb 2015 14:31:20 +0100 Subject: [PATCH 1/2] generate more methods and properties in the standard library --- libraries/stdlib/src/generated/_Arrays.kt | 108 +++++++++ libraries/stdlib/src/generated/_Ranges.kt | 223 ++++++++++++++++++ .../stdlib/src/kotlin/collections/Arrays.kt | 55 ----- libraries/stdlib/src/kotlin/util/Ranges.kt | 133 ----------- .../src/generators/GenerateCollections.kt | 5 +- .../kotlin-stdlib-gen/src/templates/Arrays.kt | 18 ++ .../kotlin-stdlib-gen/src/templates/Engine.kt | 55 ++++- .../kotlin-stdlib-gen/src/templates/Ranges.kt | 50 ++++ 8 files changed, 449 insertions(+), 198 deletions(-) create mode 100644 libraries/stdlib/src/generated/_Ranges.kt create mode 100644 libraries/tools/kotlin-stdlib-gen/src/templates/Ranges.kt diff --git a/libraries/stdlib/src/generated/_Arrays.kt b/libraries/stdlib/src/generated/_Arrays.kt index 74262fc0fe8..15748d5caa6 100644 --- a/libraries/stdlib/src/generated/_Arrays.kt +++ b/libraries/stdlib/src/generated/_Arrays.kt @@ -9,6 +9,60 @@ import java.util.* import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js +/** + * Returns the range of valid indices for the array + */ +public val Array.indices: IntRange + get() = IntRange(0, lastIndex) + +/** + * Returns the range of valid indices for the array + */ +public val BooleanArray.indices: IntRange + get() = IntRange(0, lastIndex) + +/** + * Returns the range of valid indices for the array + */ +public val ByteArray.indices: IntRange + get() = IntRange(0, lastIndex) + +/** + * Returns the range of valid indices for the array + */ +public val CharArray.indices: IntRange + get() = IntRange(0, lastIndex) + +/** + * Returns the range of valid indices for the array + */ +public val DoubleArray.indices: IntRange + get() = IntRange(0, lastIndex) + +/** + * Returns the range of valid indices for the array + */ +public val FloatArray.indices: IntRange + get() = IntRange(0, lastIndex) + +/** + * Returns the range of valid indices for the array + */ +public val IntArray.indices: IntRange + get() = IntRange(0, lastIndex) + +/** + * Returns the range of valid indices for the array + */ +public val LongArray.indices: IntRange + get() = IntRange(0, lastIndex) + +/** + * Returns the range of valid indices for the array + */ +public val ShortArray.indices: IntRange + get() = IntRange(0, lastIndex) + /** * Returns true if the array is empty */ @@ -135,3 +189,57 @@ public fun ShortArray.isNotEmpty(): Boolean { return !isEmpty() } +/** + * Returns the last valid index for the array + */ +public val Array.lastIndex: Int + get() = size - 1 + +/** + * Returns the last valid index for the array + */ +public val BooleanArray.lastIndex: Int + get() = size - 1 + +/** + * Returns the last valid index for the array + */ +public val ByteArray.lastIndex: Int + get() = size - 1 + +/** + * Returns the last valid index for the array + */ +public val CharArray.lastIndex: Int + get() = size - 1 + +/** + * Returns the last valid index for the array + */ +public val DoubleArray.lastIndex: Int + get() = size - 1 + +/** + * Returns the last valid index for the array + */ +public val FloatArray.lastIndex: Int + get() = size - 1 + +/** + * Returns the last valid index for the array + */ +public val IntArray.lastIndex: Int + get() = size - 1 + +/** + * Returns the last valid index for the array + */ +public val LongArray.lastIndex: Int + get() = size - 1 + +/** + * Returns the last valid index for the array + */ +public val ShortArray.lastIndex: Int + get() = size - 1 + diff --git a/libraries/stdlib/src/generated/_Ranges.kt b/libraries/stdlib/src/generated/_Ranges.kt new file mode 100644 index 00000000000..33c8b5a327d --- /dev/null +++ b/libraries/stdlib/src/generated/_Ranges.kt @@ -0,0 +1,223 @@ +package kotlin + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// + +import java.util.* + +import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js + +/** + * Returns a progression that goes over the same range in the opposite direction with the same step + */ +public fun ByteProgression.reversed(): ByteProgression { + return ByteProgression(end, start, -increment) +} + +/** + * Returns a progression that goes over the same range in the opposite direction with the same step + */ +public fun CharProgression.reversed(): CharProgression { + return CharProgression(end, start, -increment) +} + +/** + * Returns a progression that goes over the same range in the opposite direction with the same step + */ +public fun DoubleProgression.reversed(): DoubleProgression { + return DoubleProgression(end, start, -increment) +} + +/** + * Returns a progression that goes over the same range in the opposite direction with the same step + */ +public fun FloatProgression.reversed(): FloatProgression { + return FloatProgression(end, start, -increment) +} + +/** + * Returns a progression that goes over the same range in the opposite direction with the same step + */ +public fun IntProgression.reversed(): IntProgression { + return IntProgression(end, start, -increment) +} + +/** + * Returns a progression that goes over the same range in the opposite direction with the same step + */ +public fun LongProgression.reversed(): LongProgression { + return LongProgression(end, start, -increment) +} + +/** + * Returns a progression that goes over the same range in the opposite direction with the same step + */ +public fun ShortProgression.reversed(): ShortProgression { + return ShortProgression(end, start, -increment) +} + +/** + * Returns a progression that goes over this range in reverse direction + */ +public fun ByteRange.reversed(): ByteProgression { + return ByteProgression(end, start, -1) +} + +/** + * Returns a progression that goes over this range in reverse direction + */ +public fun CharRange.reversed(): CharProgression { + return CharProgression(end, start, -1) +} + +/** + * Returns a progression that goes over this range in reverse direction + */ +public fun DoubleRange.reversed(): DoubleProgression { + return DoubleProgression(end, start, -1.0) +} + +/** + * Returns a progression that goes over this range in reverse direction + */ +public fun FloatRange.reversed(): FloatProgression { + return FloatProgression(end, start, -1.0f) +} + +/** + * Returns a progression that goes over this range in reverse direction + */ +public fun IntRange.reversed(): IntProgression { + return IntProgression(end, start, -1) +} + +/** + * Returns a progression that goes over this range in reverse direction + */ +public fun LongRange.reversed(): LongProgression { + return LongProgression(end, start, -1.toLong()) +} + +/** + * Returns a progression that goes over this range in reverse direction + */ +public fun ShortRange.reversed(): ShortProgression { + return ShortProgression(end, start, -1) +} + +/** + * Returns a progression that goes over the same range with the given step + */ +public fun ByteProgression.step(step: Int): ByteProgression { + checkStepIsPositive(step > 0, step) + return ByteProgression(start, end, if (increment > 0) step else -step) +} + +/** + * Returns a progression that goes over the same range with the given step + */ +public fun CharProgression.step(step: Int): CharProgression { + checkStepIsPositive(step > 0, step) + return CharProgression(start, end, if (increment > 0) step else -step) +} + +/** + * Returns a progression that goes over the same range with the given step + */ +public fun DoubleProgression.step(step: Double): DoubleProgression { + checkStepIsPositive(step > 0, step) + return DoubleProgression(start, end, if (increment > 0) step else -step) +} + +/** + * Returns a progression that goes over the same range with the given step + */ +public fun FloatProgression.step(step: Float): FloatProgression { + checkStepIsPositive(step > 0, step) + return FloatProgression(start, end, if (increment > 0) step else -step) +} + +/** + * Returns a progression that goes over the same range with the given step + */ +public fun IntProgression.step(step: Int): IntProgression { + checkStepIsPositive(step > 0, step) + return IntProgression(start, end, if (increment > 0) step else -step) +} + +/** + * Returns a progression that goes over the same range with the given step + */ +public fun LongProgression.step(step: Long): LongProgression { + checkStepIsPositive(step > 0, step) + return LongProgression(start, end, if (increment > 0) step else -step) +} + +/** + * Returns a progression that goes over the same range with the given step + */ +public fun ShortProgression.step(step: Int): ShortProgression { + checkStepIsPositive(step > 0, step) + return ShortProgression(start, end, if (increment > 0) step else -step) +} + +/** + * Returns a progression that goes over this range with given step + */ +public fun ByteRange.step(step: Int): ByteProgression { + checkStepIsPositive(step > 0, step) + return ByteProgression(start, end, step) +} + +/** + * Returns a progression that goes over this range with given step + */ +public fun CharRange.step(step: Int): CharProgression { + checkStepIsPositive(step > 0, step) + return CharProgression(start, end, step) +} + +/** + * Returns a progression that goes over this range with given step + */ +public fun DoubleRange.step(step: Double): DoubleProgression { + if (step.isNaN()) throw IllegalArgumentException("Step must not be NaN") + checkStepIsPositive(step > 0, step) + return DoubleProgression(start, end, step) +} + +/** + * Returns a progression that goes over this range with given step + */ +public fun FloatRange.step(step: Float): FloatProgression { + if (step.isNaN()) throw IllegalArgumentException("Step must not be NaN") + checkStepIsPositive(step > 0, step) + return FloatProgression(start, end, step) +} + +/** + * Returns a progression that goes over this range with given step + */ +public fun IntRange.step(step: Int): IntProgression { + checkStepIsPositive(step > 0, step) + return IntProgression(start, end, step) +} + +/** + * Returns a progression that goes over this range with given step + */ +public fun LongRange.step(step: Long): LongProgression { + checkStepIsPositive(step > 0, step) + return LongProgression(start, end, step) +} + +/** + * Returns a progression that goes over this range with given step + */ +public fun ShortRange.step(step: Int): ShortProgression { + checkStepIsPositive(step > 0, step) + return ShortProgression(start, end, step) +} + diff --git a/libraries/stdlib/src/kotlin/collections/Arrays.kt b/libraries/stdlib/src/kotlin/collections/Arrays.kt index 5adf4874fdd..854e0fafce0 100644 --- a/libraries/stdlib/src/kotlin/collections/Arrays.kt +++ b/libraries/stdlib/src/kotlin/collections/Arrays.kt @@ -9,58 +9,3 @@ public inline fun Array(size: Int, init: (Int) -> T): Array { return result as Array } - -public val BooleanArray.lastIndex: Int - get() = size() - 1 - -public val ByteArray.lastIndex: Int - get() = size() - 1 - -public val ShortArray.lastIndex: Int - get() = size() - 1 - -public val IntArray.lastIndex: Int - get() = size() - 1 - -public val LongArray.lastIndex: Int - get() = size() - 1 - -public val FloatArray.lastIndex: Int - get() = size() - 1 - -public val DoubleArray.lastIndex: Int - get() = size() - 1 - -public val CharArray.lastIndex: Int - get() = size() - 1 - -public val Array<*>.lastIndex: Int - get() = size() - 1 - - -public val BooleanArray.indices: IntRange - get() = IntRange(0, lastIndex) - -public val ByteArray.indices: IntRange - get() = IntRange(0, lastIndex) - -public val ShortArray.indices: IntRange - get() = IntRange(0, lastIndex) - -public val IntArray.indices: IntRange - get() = IntRange(0, lastIndex) - -public val LongArray.indices: IntRange - get() = IntRange(0, lastIndex) - -public val FloatArray.indices: IntRange - get() = IntRange(0, lastIndex) - -public val DoubleArray.indices: IntRange - get() = IntRange(0, lastIndex) - -public val CharArray.indices: IntRange - get() = IntRange(0, lastIndex) - -public val Array<*>.indices: IntRange - get() = IntRange(0, lastIndex) diff --git a/libraries/stdlib/src/kotlin/util/Ranges.kt b/libraries/stdlib/src/kotlin/util/Ranges.kt index fe780618edd..ba536401b1a 100644 --- a/libraries/stdlib/src/kotlin/util/Ranges.kt +++ b/libraries/stdlib/src/kotlin/util/Ranges.kt @@ -23,139 +23,6 @@ public fun > T.rangeTo(that: T): ComparableRange { } - -public fun CharProgression.reversed(): CharProgression { - return CharProgression(end, start, -increment) -} - -public fun ByteProgression.reversed(): ByteProgression { - return ByteProgression(end, start, -increment) -} - -public fun ShortProgression.reversed(): ShortProgression { - return ShortProgression(end, start, -increment) -} - -public fun IntProgression.reversed(): IntProgression { - return IntProgression(end, start, -increment) -} - -public fun FloatProgression.reversed(): FloatProgression { - return FloatProgression(end, start, -increment) -} - -public fun LongProgression.reversed(): LongProgression { - return LongProgression(end, start, -increment) -} - -public fun DoubleProgression.reversed(): DoubleProgression { - return DoubleProgression(end, start, -increment) -} - - -public fun CharRange.reversed(): CharProgression { - return CharProgression(end, start, -1) -} - -public fun ByteRange.reversed(): ByteProgression { - return ByteProgression(end, start, -1) -} - -public fun ShortRange.reversed(): ShortProgression { - return ShortProgression(end, start, -1) -} - -public fun IntRange.reversed(): IntProgression { - return IntProgression(end, start, -1) -} - -public fun FloatRange.reversed(): FloatProgression { - return FloatProgression(end, start, -1.0.toFloat()) -} - -public fun LongRange.reversed(): LongProgression { - return LongProgression(end, start, -1.toLong()) -} - -public fun DoubleRange.reversed(): DoubleProgression { - return DoubleProgression(end, start, -1.0) -} - - -public fun IntProgression.step(step: Int): IntProgression { - checkStepIsPositive(step > 0, step) - return IntProgression(start, end, if (increment > 0) step else -step) -} - -public fun CharProgression.step(step: Int): CharProgression { - checkStepIsPositive(step > 0, step) - return CharProgression(start, end, if (increment > 0) step else -step) -} - -public fun ByteProgression.step(step: Int): ByteProgression { - checkStepIsPositive(step > 0, step) - return ByteProgression(start, end, if (increment > 0) step else -step) -} - -public fun ShortProgression.step(step: Int): ShortProgression { - checkStepIsPositive(step > 0, step) - return ShortProgression(start, end, if (increment > 0) step else -step) -} - -public fun LongProgression.step(step: Long): LongProgression { - checkStepIsPositive(step > 0, step) - return LongProgression(start, end, if (increment > 0) step else -step) -} - -public fun FloatProgression.step(step: Float): FloatProgression { - checkStepIsPositive(step > 0, step) - return FloatProgression(start, end, if (increment > 0) step else -step) -} - -public fun DoubleProgression.step(step: Double): DoubleProgression { - checkStepIsPositive(step > 0, step) - return DoubleProgression(start, end, if (increment > 0) step else -step) -} - - -public fun IntRange.step(step: Int): IntProgression { - checkStepIsPositive(step > 0, step) - return IntProgression(start, end, step) -} - -public fun CharRange.step(step: Int): CharProgression { - checkStepIsPositive(step > 0, step) - return CharProgression(start, end, step) -} - -public fun ByteRange.step(step: Int): ByteProgression { - checkStepIsPositive(step > 0, step) - return ByteProgression(start, end, step) -} - -public fun ShortRange.step(step: Int): ShortProgression { - checkStepIsPositive(step > 0, step) - return ShortProgression(start, end, step) -} - -public fun LongRange.step(step: Long): LongProgression { - checkStepIsPositive(step > 0, step) - return LongProgression(start, end, step) -} - -public fun FloatRange.step(step: Float): FloatProgression { - if (step.isNaN()) throw IllegalArgumentException("Step must not be NaN") - checkStepIsPositive(step > 0, step) - return FloatProgression(start, end, step) -} - -public fun DoubleRange.step(step: Double): DoubleProgression { - if (step.isNaN()) throw IllegalArgumentException("Step must not be NaN") - checkStepIsPositive(step > 0, step) - return DoubleProgression(start, end, step) -} - - private fun checkStepIsPositive(isPositive: Boolean, step: Number) { if (!isPositive) throw IllegalArgumentException("Step must be positive, was: $step") } diff --git a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateCollections.kt b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateCollections.kt index 2d0891dc9df..d75a3a4fe78 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateCollections.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateCollections.kt @@ -19,16 +19,17 @@ fun generateCollectionsAPI(outDir: File) { strings().writeTo(File(outDir, "_Strings.kt")) { build() } streams().writeTo(File(outDir, "_Streams.kt")) { build() } specialJVM().writeTo(File(outDir, "_SpecialJVM.kt")) { build() } + ranges().writeTo(File(outDir, "_Ranges.kt")) { build() } numeric().writeTo(File(outDir, "_Numeric.kt")) { val builder = StringBuilder() // TODO: decide if sum for byte and short is needed and how to make it work - for (numeric in listOf(Int, Long, /*Byte, Short, */ Double, Float)) { + for (numeric in listOf(PrimitiveType.Int, PrimitiveType.Long, /*Byte, Short, */ PrimitiveType.Double, PrimitiveType.Float)) { build(builder, Iterables, numeric) build(builder, Streams, numeric) } - for (numeric in listOf(Int, Long, Byte, Short, Double, Float)) { + for (numeric in listOf(PrimitiveType.Int, PrimitiveType.Long, PrimitiveType.Byte, PrimitiveType.Short, PrimitiveType.Double, PrimitiveType.Float)) { build(builder, ArraysOfObjects, numeric) build(builder, ArraysOfPrimitives, numeric) } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt index 9c194bc5e96..43d369b0a98 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt @@ -23,5 +23,23 @@ fun arrays(): List { } } + templates add pval("lastIndex") { + only(ArraysOfObjects, ArraysOfPrimitives) + doc { "Returns the last valid index for the array" } + returns("Int") + body { + "get() = size - 1" + } + } + + templates add pval("indices") { + only(ArraysOfObjects, ArraysOfPrimitives) + doc { "Returns the range of valid indices for the array" } + returns("IntRange") + body { + "get() = IntRange(0, lastIndex)" + } + } + return templates } \ No newline at end of file diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt index fab7590798e..7e8c711ec6f 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt @@ -16,6 +16,8 @@ enum class Family { ArraysOfObjects ArraysOfPrimitives Strings + RangesOfPrimitives + ProgressionsOfPrimitives } enum class PrimitiveType(val name: String) { @@ -30,7 +32,7 @@ enum class PrimitiveType(val name: String) { } -class GenericFunction(val signature: String) : Comparable { +class GenericFunction(val signature: String, val keyword: String = "fun") : Comparable { val defaultFamilies = array(Iterables, Streams, ArraysOfObjects, ArraysOfPrimitives, Strings) var toNullableT: Boolean = false @@ -39,7 +41,7 @@ class GenericFunction(val signature: String) : Comparable { var receiverAsterisk = false val inlineFamilies = HashMap() - val buildFamilies = HashSet(defaultFamilies.toList()) + val buildFamilies = HashSet(defaultFamilies.filter { it != RangesOfPrimitives && it != ProgressionsOfPrimitives }.toList()) private val buildPrimitives = HashSet(PrimitiveType.values().toList()) var deprecate: String = "" @@ -50,6 +52,7 @@ class GenericFunction(val signature: String) : Comparable { var defaultBody: String = "" val bodies = HashMap() + val customPrimitiveBodies = HashMap, String>() var defaultReturnType = "" val returnTypes = HashMap() @@ -67,6 +70,13 @@ class GenericFunction(val signature: String) : Comparable { } } + fun bodyForTypes(family: Family, vararg primitiveTypes: PrimitiveType, b: () -> String) { + include(family) + for (f in primitiveTypes) { + customPrimitiveBodies.put(family to f, b()) + } + } + fun doc(vararg families: Family, b: () -> String) { if (families.isEmpty()) doc = b() @@ -149,7 +159,7 @@ class GenericFunction(val signature: String) : Comparable { } fun build(builder: StringBuilder, f: Family) { - if (f == ArraysOfPrimitives) { + if (f == ArraysOfPrimitives || f == RangesOfPrimitives || f == ProgressionsOfPrimitives) { for (primitive in buildPrimitives.sortBy { it.name() }) build(builder, f, primitive) } else { @@ -172,6 +182,8 @@ class GenericFunction(val signature: String) : Comparable { ArraysOfObjects -> "Array<$isAsteriskOrT>" Strings -> "String" ArraysOfPrimitives -> primitive?.let { it.name() + "Array" } ?: throw IllegalArgumentException("Primitive array should specify primitive type") + RangesOfPrimitives -> primitive?.let { it.name() + "Range" } ?: throw IllegalArgumentException("Primitive range should specify primitive type") + ProgressionsOfPrimitives -> primitive?.let { it.name() + "Progression" } ?: throw IllegalArgumentException("Primitive progression should specify primitive type") else -> throw IllegalStateException("Invalid family") } @@ -187,7 +199,7 @@ class GenericFunction(val signature: String) : Comparable { "PRIMITIVE" -> primitive?.name() ?: token "SUM" -> { when (primitive) { - PrimitiveType.Byte, PrimitiveType.Short -> "Int" + PrimitiveType.Byte, PrimitiveType.Short, PrimitiveType.Char -> "Int" else -> primitive } } @@ -196,6 +208,18 @@ class GenericFunction(val signature: String) : Comparable { PrimitiveType.Float -> "0.0f" else -> "0" } + "ONE" -> when (primitive) { + PrimitiveType.Double -> "1.0" + PrimitiveType.Float -> "1.0f" + PrimitiveType.Long -> "1.toLong()" + else -> "1" + } + "-ONE" -> when (primitive) { + PrimitiveType.Double -> "-1.0" + PrimitiveType.Float -> "-1.0f" + PrimitiveType.Long -> "-1.toLong()" + else -> "-1" + } "TCollection" -> { when (f) { Strings -> "Appendable" @@ -209,6 +233,7 @@ class GenericFunction(val signature: String) : Comparable { else -> primitive?.name() ?: token } } + "TProgression" -> primitive!!.name + "Progression" else -> token }) } @@ -253,7 +278,7 @@ class GenericFunction(val signature: String) : Comparable { if (inlineFamilies[f] ?: defaultInline) builder.append("inline ") - builder.append("fun ") + builder.append("$keyword ") val types = effectiveTypeParams() if (!types.isEmpty()) { @@ -267,9 +292,10 @@ class GenericFunction(val signature: String) : Comparable { builder.append(receiverType) - builder.append(".${signature.renderType()}: ${returnType.renderType()} {") + builder.append(".${signature.renderType()}: ${returnType.renderType()}") + if (keyword == "fun") builder.append(" {") - val body = (bodies[f] ?: defaultBody).trim("\n") + val body = (customPrimitiveBodies[f to primitive] ?: bodies[f] ?: defaultBody).trim("\n") val indent: Int = body.takeWhile { it == ' ' }.length() builder.append('\n') @@ -281,7 +307,8 @@ class GenericFunction(val signature: String) : Comparable { builder.append("\n") } } - builder.append("}\n\n") + if (keyword == "fun") builder.append("}\n") + builder.append("\n") } public override fun compareTo(other: GenericFunction): Int = this.signature.compareTo(other.signature) @@ -298,3 +325,15 @@ fun f(signature: String, init: GenericFunction.() -> Unit): GenericFunction { gf.init() return gf } + +fun pval(signature: String, init: GenericFunction.() -> Unit): GenericFunction { + val gf = GenericFunction(signature, "val") + gf.init() + return gf +} + +fun pvar(signature: String, init: GenericFunction.() -> Unit): GenericFunction { + val gf = GenericFunction(signature, "var") + gf.init() + return gf +} diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Ranges.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Ranges.kt new file mode 100644 index 00000000000..e33eae56d05 --- /dev/null +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Ranges.kt @@ -0,0 +1,50 @@ +package templates + +import templates.Family.* + +fun ranges(): List { + val templates = arrayListOf() + + templates add f("reversed()") { + only(RangesOfPrimitives, ProgressionsOfPrimitives) + exclude(PrimitiveType.Boolean) + 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") + body(RangesOfPrimitives) { + "return TProgression(end, start, -ONE)" + } + body(ProgressionsOfPrimitives) { + "return TProgression(end, start, -increment)" + } + } + + templates add f("step(step: SUM)") { + only(RangesOfPrimitives, ProgressionsOfPrimitives) + exclude(PrimitiveType.Boolean) + 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") + body(RangesOfPrimitives) { + """ + checkStepIsPositive(step > 0, step) + return TProgression(start, end, step) + """ + } + bodyForTypes(RangesOfPrimitives, PrimitiveType.Float, PrimitiveType.Double) { + """ + if (step.isNaN()) throw IllegalArgumentException("Step must not be NaN") + checkStepIsPositive(step > 0, step) + return TProgression(start, end, step) + """ + } + body(ProgressionsOfPrimitives) { + """ + checkStepIsPositive(step > 0, step) + return TProgression(start, end, if (increment > 0) step else -step) + """ + } + } + + return templates +} From ff49310b7ee2273693734237427fb53aa5420e83 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Wed, 11 Feb 2015 12:28:23 +0100 Subject: [PATCH 2/2] remove redundant filter() --- libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt index 7e8c711ec6f..a4f7a415e47 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt @@ -41,7 +41,7 @@ class GenericFunction(val signature: String, val keyword: String = "fun") : Comp var receiverAsterisk = false val inlineFamilies = HashMap() - val buildFamilies = HashSet(defaultFamilies.filter { it != RangesOfPrimitives && it != ProgressionsOfPrimitives }.toList()) + val buildFamilies = HashSet(defaultFamilies.toList()) private val buildPrimitives = HashSet(PrimitiveType.values().toList()) var deprecate: String = ""