Generate sum() methods for arrays, iterables and sequences of Byte and Short. These overloads got explicit platformName.

Heuristics to decide when to set platformName.
#KT-3714
This commit is contained in:
Ilya Gorbunov
2015-04-15 23:14:46 +03:00
parent ccc3646bb8
commit 9759be5493
21 changed files with 270 additions and 145 deletions
@@ -5,6 +5,7 @@ package kotlin
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.platform.*
import java.util.*
import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js
@@ -5,6 +5,7 @@ package kotlin
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.platform.*
import java.util.*
import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js
@@ -5,6 +5,7 @@ package kotlin
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.platform.*
import java.util.*
import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js
@@ -5,6 +5,7 @@ package kotlin
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.platform.*
import java.util.*
import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js
@@ -5,6 +5,7 @@ package kotlin
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.platform.*
import java.util.*
import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js
@@ -5,6 +5,7 @@ package kotlin
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.platform.*
import java.util.*
import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js
@@ -5,6 +5,7 @@ package kotlin
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.platform.*
import java.util.*
import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js
@@ -5,6 +5,7 @@ package kotlin
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.platform.*
import java.util.*
import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js
@@ -5,6 +5,7 @@ package kotlin
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.platform.*
import java.util.*
import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js
+235 -134
View File
@@ -5,13 +5,15 @@ package kotlin
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.platform.*
import java.util.*
import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js
/**
* Returns the sum of all elements in the collection
* Returns the sum of all elements in the collection.
*/
platformName("sumOfInt")
public fun Iterable<Int>.sum(): Int {
val iterator = iterator()
var sum: Int = 0
@@ -22,8 +24,9 @@ public fun Iterable<Int>.sum(): Int {
}
/**
* Returns the sum of all elements in the collection
* Returns the sum of all elements in the collection.
*/
platformName("sumOfInt")
public fun Sequence<Int>.sum(): Int {
val iterator = iterator()
var sum: Int = 0
@@ -36,8 +39,9 @@ public fun Sequence<Int>.sum(): Int {
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns the sum of all elements in the collection
* Returns the sum of all elements in the collection.
*/
platformName("sumOfInt")
public fun Stream<Int>.sum(): Int {
val iterator = iterator()
var sum: Int = 0
@@ -48,8 +52,34 @@ public fun Stream<Int>.sum(): Int {
}
/**
* Returns the sum of all elements in the collection
* Returns the sum of all elements in the collection.
*/
platformName("sumOfInt")
public fun Array<out Int>.sum(): Int {
val iterator = iterator()
var sum: Int = 0
while (iterator.hasNext()) {
sum += iterator.next()
}
return sum
}
/**
* Returns the sum of all elements in the collection.
*/
public fun IntArray.sum(): Int {
val iterator = iterator()
var sum: Int = 0
while (iterator.hasNext()) {
sum += iterator.next()
}
return sum
}
/**
* Returns the sum of all elements in the collection.
*/
platformName("sumOfLong")
public fun Iterable<Long>.sum(): Long {
val iterator = iterator()
var sum: Long = 0
@@ -60,8 +90,9 @@ public fun Iterable<Long>.sum(): Long {
}
/**
* Returns the sum of all elements in the collection
* Returns the sum of all elements in the collection.
*/
platformName("sumOfLong")
public fun Sequence<Long>.sum(): Long {
val iterator = iterator()
var sum: Long = 0
@@ -74,8 +105,9 @@ public fun Sequence<Long>.sum(): Long {
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns the sum of all elements in the collection
* Returns the sum of all elements in the collection.
*/
platformName("sumOfLong")
public fun Stream<Long>.sum(): Long {
val iterator = iterator()
var sum: Long = 0
@@ -86,8 +118,166 @@ public fun Stream<Long>.sum(): Long {
}
/**
* Returns the sum of all elements in the collection
* Returns the sum of all elements in the collection.
*/
platformName("sumOfLong")
public fun Array<out Long>.sum(): Long {
val iterator = iterator()
var sum: Long = 0
while (iterator.hasNext()) {
sum += iterator.next()
}
return sum
}
/**
* Returns the sum of all elements in the collection.
*/
public fun LongArray.sum(): Long {
val iterator = iterator()
var sum: Long = 0
while (iterator.hasNext()) {
sum += iterator.next()
}
return sum
}
/**
* Returns the sum of all elements in the collection.
*/
platformName("sumOfByte")
public fun Iterable<Byte>.sum(): Int {
val iterator = iterator()
var sum: Int = 0
while (iterator.hasNext()) {
sum += iterator.next()
}
return sum
}
/**
* Returns the sum of all elements in the collection.
*/
platformName("sumOfByte")
public fun Sequence<Byte>.sum(): Int {
val iterator = iterator()
var sum: Int = 0
while (iterator.hasNext()) {
sum += iterator.next()
}
return sum
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns the sum of all elements in the collection.
*/
platformName("sumOfByte")
public fun Stream<Byte>.sum(): Int {
val iterator = iterator()
var sum: Int = 0
while (iterator.hasNext()) {
sum += iterator.next()
}
return sum
}
/**
* Returns the sum of all elements in the collection.
*/
platformName("sumOfByte")
public fun Array<out Byte>.sum(): Int {
val iterator = iterator()
var sum: Int = 0
while (iterator.hasNext()) {
sum += iterator.next()
}
return sum
}
/**
* Returns the sum of all elements in the collection.
*/
public fun ByteArray.sum(): Int {
val iterator = iterator()
var sum: Int = 0
while (iterator.hasNext()) {
sum += iterator.next()
}
return sum
}
/**
* Returns the sum of all elements in the collection.
*/
platformName("sumOfShort")
public fun Iterable<Short>.sum(): Int {
val iterator = iterator()
var sum: Int = 0
while (iterator.hasNext()) {
sum += iterator.next()
}
return sum
}
/**
* Returns the sum of all elements in the collection.
*/
platformName("sumOfShort")
public fun Sequence<Short>.sum(): Int {
val iterator = iterator()
var sum: Int = 0
while (iterator.hasNext()) {
sum += iterator.next()
}
return sum
}
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns the sum of all elements in the collection.
*/
platformName("sumOfShort")
public fun Stream<Short>.sum(): Int {
val iterator = iterator()
var sum: Int = 0
while (iterator.hasNext()) {
sum += iterator.next()
}
return sum
}
/**
* Returns the sum of all elements in the collection.
*/
platformName("sumOfShort")
public fun Array<out Short>.sum(): Int {
val iterator = iterator()
var sum: Int = 0
while (iterator.hasNext()) {
sum += iterator.next()
}
return sum
}
/**
* Returns the sum of all elements in the collection.
*/
public fun ShortArray.sum(): Int {
val iterator = iterator()
var sum: Int = 0
while (iterator.hasNext()) {
sum += iterator.next()
}
return sum
}
/**
* Returns the sum of all elements in the collection.
*/
platformName("sumOfDouble")
public fun Iterable<Double>.sum(): Double {
val iterator = iterator()
var sum: Double = 0.0
@@ -98,8 +288,9 @@ public fun Iterable<Double>.sum(): Double {
}
/**
* Returns the sum of all elements in the collection
* Returns the sum of all elements in the collection.
*/
platformName("sumOfDouble")
public fun Sequence<Double>.sum(): Double {
val iterator = iterator()
var sum: Double = 0.0
@@ -112,8 +303,9 @@ public fun Sequence<Double>.sum(): Double {
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns the sum of all elements in the collection
* Returns the sum of all elements in the collection.
*/
platformName("sumOfDouble")
public fun Stream<Double>.sum(): Double {
val iterator = iterator()
var sum: Double = 0.0
@@ -124,8 +316,34 @@ public fun Stream<Double>.sum(): Double {
}
/**
* Returns the sum of all elements in the collection
* Returns the sum of all elements in the collection.
*/
platformName("sumOfDouble")
public fun Array<out Double>.sum(): Double {
val iterator = iterator()
var sum: Double = 0.0
while (iterator.hasNext()) {
sum += iterator.next()
}
return sum
}
/**
* Returns the sum of all elements in the collection.
*/
public fun DoubleArray.sum(): Double {
val iterator = iterator()
var sum: Double = 0.0
while (iterator.hasNext()) {
sum += iterator.next()
}
return sum
}
/**
* Returns the sum of all elements in the collection.
*/
platformName("sumOfFloat")
public fun Iterable<Float>.sum(): Float {
val iterator = iterator()
var sum: Float = 0.0f
@@ -136,8 +354,9 @@ public fun Iterable<Float>.sum(): Float {
}
/**
* Returns the sum of all elements in the collection
* Returns the sum of all elements in the collection.
*/
platformName("sumOfFloat")
public fun Sequence<Float>.sum(): Float {
val iterator = iterator()
var sum: Float = 0.0f
@@ -150,8 +369,9 @@ public fun Sequence<Float>.sum(): Float {
deprecated("Migrate to using Sequence<T> and respective functions")
/**
* Returns the sum of all elements in the collection
* Returns the sum of all elements in the collection.
*/
platformName("sumOfFloat")
public fun Stream<Float>.sum(): Float {
val iterator = iterator()
var sum: Float = 0.0f
@@ -162,128 +382,9 @@ public fun Stream<Float>.sum(): Float {
}
/**
* Returns the sum of all elements in the collection
*/
public fun Array<out Int>.sum(): Int {
val iterator = iterator()
var sum: Int = 0
while (iterator.hasNext()) {
sum += iterator.next()
}
return sum
}
/**
* Returns the sum of all elements in the collection
*/
public fun IntArray.sum(): Int {
val iterator = iterator()
var sum: Int = 0
while (iterator.hasNext()) {
sum += iterator.next()
}
return sum
}
/**
* Returns the sum of all elements in the collection
*/
public fun Array<out Long>.sum(): Long {
val iterator = iterator()
var sum: Long = 0
while (iterator.hasNext()) {
sum += iterator.next()
}
return sum
}
/**
* Returns the sum of all elements in the collection
*/
public fun LongArray.sum(): Long {
val iterator = iterator()
var sum: Long = 0
while (iterator.hasNext()) {
sum += iterator.next()
}
return sum
}
/**
* Returns the sum of all elements in the collection
*/
public fun Array<out Byte>.sum(): Int {
val iterator = iterator()
var sum: Int = 0
while (iterator.hasNext()) {
sum += iterator.next()
}
return sum
}
/**
* Returns the sum of all elements in the collection
*/
public fun ByteArray.sum(): Int {
val iterator = iterator()
var sum: Int = 0
while (iterator.hasNext()) {
sum += iterator.next()
}
return sum
}
/**
* Returns the sum of all elements in the collection
*/
public fun Array<out Short>.sum(): Int {
val iterator = iterator()
var sum: Int = 0
while (iterator.hasNext()) {
sum += iterator.next()
}
return sum
}
/**
* Returns the sum of all elements in the collection
*/
public fun ShortArray.sum(): Int {
val iterator = iterator()
var sum: Int = 0
while (iterator.hasNext()) {
sum += iterator.next()
}
return sum
}
/**
* Returns the sum of all elements in the collection
*/
public fun Array<out Double>.sum(): Double {
val iterator = iterator()
var sum: Double = 0.0
while (iterator.hasNext()) {
sum += iterator.next()
}
return sum
}
/**
* Returns the sum of all elements in the collection
*/
public fun DoubleArray.sum(): Double {
val iterator = iterator()
var sum: Double = 0.0
while (iterator.hasNext()) {
sum += iterator.next()
}
return sum
}
/**
* Returns the sum of all elements in the collection
* Returns the sum of all elements in the collection.
*/
platformName("sumOfFloat")
public fun Array<out Float>.sum(): Float {
val iterator = iterator()
var sum: Float = 0.0f
@@ -294,7 +395,7 @@ public fun Array<out Float>.sum(): Float {
}
/**
* Returns the sum of all elements in the collection
* Returns the sum of all elements in the collection.
*/
public fun FloatArray.sum(): Float {
val iterator = iterator()
@@ -5,6 +5,7 @@ package kotlin
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.platform.*
import java.util.*
import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js
@@ -5,6 +5,7 @@ package kotlin
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.platform.*
import java.util.*
import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js
@@ -5,6 +5,7 @@ package kotlin
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.platform.*
import java.util.*
import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js
+1
View File
@@ -5,6 +5,7 @@ package kotlin
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.platform.*
import java.util.*
import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js
@@ -5,6 +5,7 @@ package kotlin
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.platform.*
import java.util.*
import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js
@@ -5,6 +5,7 @@ package kotlin
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.platform.*
import java.util.*
import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js
@@ -5,6 +5,7 @@ package kotlin
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.platform.*
import java.util.*
import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js
@@ -23,16 +23,10 @@ fun generateCollectionsAPI(outDir: File) {
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(PrimitiveType.Int, PrimitiveType.Long, /*Byte, Short, */ PrimitiveType.Double, PrimitiveType.Float)) {
build(builder, Iterables, numeric)
build(builder, Sequences, numeric)
}
for (numeric in numericPrimitives)
for (family in buildFamilies)
build(builder, family, numeric)
for (numeric in numericPrimitives) {
build(builder, ArraysOfObjects, numeric)
build(builder, ArraysOfPrimitives, numeric)
}
builder.toString()
}
@@ -43,6 +43,7 @@ fun List<GenericFunction>.writeTo(file: File, builder: GenericFunction.() -> Str
its.use {
its.append("package kotlin\n\n")
its.append("$COMMON_AUTOGENERATED_WARNING\n\n")
its.append("import kotlin.platform.*\n")
its.append("import java.util.*\n\n")
its.append("import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js\n\n")
for (t in this.sort()) {
@@ -6,6 +6,7 @@ import java.util.ArrayList
import java.util.HashMap
import java.util.HashSet
import java.util.StringTokenizer
import kotlin.properties.Delegates
enum class Family {
Sequences
@@ -20,6 +21,8 @@ enum class Family {
ProgressionsOfPrimitives
Primitives
Generic
val isPrimitiveSpecialization: Boolean by Delegates.lazy { this in listOf(ArraysOfPrimitives, RangesOfPrimitives, ProgressionsOfPrimitives, Primitives) }
}
enum class PrimitiveType(val name: String) {
@@ -169,7 +172,7 @@ class GenericFunction(val signature: String, val keyword: String = "fun") : Comp
}
fun build(builder: StringBuilder, f: Family) {
if (f == ArraysOfPrimitives || f == RangesOfPrimitives || f == ProgressionsOfPrimitives || f == Primitives) {
if (f.isPrimitiveSpecialization) {
for (primitive in buildPrimitives.sortBy { it.name() })
build(builder, f, primitive)
} else {
@@ -308,6 +311,11 @@ class GenericFunction(val signature: String, val keyword: String = "fun") : Comp
}
}
fun getPlatformName(primitive: PrimitiveType): String {
val name = signature.substringBefore('(').trim()
return "${name}Of${primitive.name}"
}
val methodDoc = docs[f] ?: doc
if (methodDoc != "") {
builder.append("/**\n")
@@ -324,6 +332,10 @@ class GenericFunction(val signature: String, val keyword: String = "fun") : Comp
builder.append("deprecated(\"$deprecated\")\n")
}
// heuristics to define that platform overloads are likely to clash
if (!f.isPrimitiveSpecialization && primitive != null && ((returnType == returnType.renderType() && returnType != "T") || returnType == "SUM"))
builder.append("platformName(\"${getPlatformName(primitive!!)}\")\n")
builder.append("public ")
if (inlineFamilies[f] ?: defaultInline)
builder.append("inline ")
@@ -6,7 +6,8 @@ fun numeric(): List<GenericFunction> {
val templates = arrayListOf<GenericFunction>()
templates add f("sum()") {
doc { "Returns the sum of all elements in the collection" }
exclude(Strings)
doc { "Returns the sum of all elements in the collection." }
returns("SUM")
body {
"""