forked from JetBrains/kotlin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Numeric.kt
90 lines (76 loc) · 3.03 KB
/
Numeric.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package templates
import templates.Family.*
object Numeric : TemplateGroupBase() {
init {
defaultBuilder {
sequenceClassification(SequenceClass.terminal)
specialFor(ArraysOfUnsigned) {
sinceAtLeast("1.3")
annotation("@ExperimentalUnsignedTypes")
}
}
}
private val numericPrimitivesDefaultOrder = PrimitiveType.defaultPrimitives intersect PrimitiveType.numericPrimitives
private val summablePrimitives = numericPrimitivesDefaultOrder + PrimitiveType.unsignedPrimitives
val f_sum = fn("sum()") {
listOf(Iterables, Sequences, ArraysOfObjects).forEach { include(it, summablePrimitives) }
include(ArraysOfPrimitives, numericPrimitivesDefaultOrder)
include(ArraysOfUnsigned)
} builder {
val p = primitive!!
doc { "Returns the sum of all elements in the ${f.collection}." }
returns(p.sumType().name)
specialFor(ArraysOfUnsigned) {
inlineOnly()
body {
if (p == p.sumType())
"return storage.sum().to${p.sumType().name}()"
else
"return sumOf { it.to${p.sumType().name}() }"
}
}
specialFor(Iterables, Sequences, ArraysOfObjects, ArraysOfPrimitives) {
platformName("sumOf<T>")
if (p.isUnsigned()) {
require(f != ArraysOfPrimitives) { "Arrays of unsigneds are separate from arrays of primitives." }
specialFor(Iterables) { sourceFile(SourceFile.UCollections) }
specialFor(Sequences) { sourceFile(SourceFile.USequences) }
specialFor(ArraysOfObjects) { sourceFile(SourceFile.UArrays) }
since("1.5")
wasExperimental("ExperimentalUnsignedTypes")
}
body {
"""
var sum: ${p.sumType().name} = ${p.sumType().zero()}
for (element in this) {
sum += element
}
return sum
"""
}
}
}
val f_average = fn("average()") {
Family.defaultFamilies.forEach { family -> include(family, numericPrimitivesDefaultOrder) }
} builder {
doc { "Returns an average value of elements in the ${f.collection}."}
returns("Double")
platformName("averageOf<T>")
body {
fun checkOverflow(value: String) = if (f == Family.Sequences || f == Family.Iterables) "checkCountOverflow($value)" else value
"""
var sum: Double = 0.0
var count: Int = 0
for (element in this) {
sum += element
${checkOverflow("++count")}
}
return if (count == 0) Double.NaN else sum / count
"""
}
}
}