standard library: 'max' and 'min' functions reimplemented to avoid unnecessary null checks

(as proposed for minBy/maxBy in https://github.com/JetBrains/kotlin/pull/324)
This commit is contained in:
nik
2013-12-02 23:40:02 +04:00
committed by Evgeny Gerashchenko
parent ba96538a14
commit f439604b57
12 changed files with 218 additions and 128 deletions
@@ -416,44 +416,6 @@ fun commons(): ArrayList<GenericFunction> {
}
}
templates add f("min()") {
doc = "Returns the smallest element or null if there are no elements"
returns("T?")
absentFor(PrimitiveType.Boolean, PrimitiveType.Char)//currently there are no sane way to compare Char? with something (KT-4251)
typeParam("T: Comparable<T>")
isInline = false
body {
"""
var min: T? = null
for (e in this) {
if (min == null || min!! > e) {
min = e
}
}
return min
"""
}
}
templates add f("max()") {
doc = "Returns the largest element or null if there are no elements"
returns("T?")
absentFor(PrimitiveType.Boolean, PrimitiveType.Char)//currently there are no sane way to compare Char? with something (KT-4251)
typeParam("T: Comparable<T>")
isInline = false
body {
"""
var max: T? = null
for (e in this) {
if (max == null || max!! < e) {
max = e
}
}
return max
"""
}
}
templates add f("appendString(buffer: Appendable, separator: String = \", \", prefix: String =\"\", postfix: String = \"\", limit: Int = -1, truncated: String = \"...\")") {
isInline = false
doc =
@@ -146,6 +146,76 @@ fun iterables(): ArrayList<GenericFunction> {
}
}
templates add f("min()") {
doc = "Returns the smallest element or null if there are no elements"
returns("T?")
absentFor(PrimitiveType.Boolean, PrimitiveType.Char)//currently there are no sane way to compare Char? with something (KT-4251)
typeParam("T: Comparable<T>")
isInline = false
Iterables.body {
"""
val iterator = iterator()
if (!iterator.hasNext()) return null
var min = iterator.next()
while (iterator.hasNext()) {
val e = iterator.next()
if (min > e) min = e
}
return min
"""
}
listOf(Arrays, PrimitiveArrays).forEach {
it.body {
"""
if (isEmpty()) return null
var min = this[0]
for (i in 1..lastIndex) {
val e = this[i]
if (min > e) min = e
}
return min
"""
}
}
}
templates add f("max()") {
doc = "Returns the largest element or null if there are no elements"
returns("T?")
absentFor(PrimitiveType.Boolean, PrimitiveType.Char)//currently there are no sane way to compare Char? with something (KT-4251)
typeParam("T: Comparable<T>")
isInline = false
Iterables.body {
"""
val iterator = iterator()
if (!iterator.hasNext()) return null
var max = iterator.next()
while (iterator.hasNext()) {
val e = iterator.next()
if (max < e) max = e
}
return max
"""
}
listOf(Arrays, PrimitiveArrays).forEach {
it.body {
"""
if (isEmpty()) return null
var max = this[0]
for (i in 1..lastIndex) {
val e = this[i]
if (max < e) max = e
}
return max
"""
}
}
}
templates add f("minBy(f: (T) -> R)") {
doc = "Returns the first element yielding the smallest value of the given function or null if there are no elements"
typeParam("R: Comparable<R>")
@@ -132,6 +132,44 @@ fun iterators(): List<GenericFunction> {
}
}
templates add f("min()") {
doc = "Returns the smallest element or null if there are no elements"
returns("T?")
typeParam("T: Comparable<T>")
isInline = false
body {
"""
if (!hasNext()) return null
var min = next()
while (hasNext()) {
val e = next()
if (min > e) min = e
}
return min
"""
}
}
templates add f("max()") {
doc = "Returns the largest element or null if there are no elements"
returns("T?")
typeParam("T: Comparable<T>")
isInline = false
body {
"""
if (!hasNext()) return null
var max = next()
while (hasNext()) {
val e = next()
if (max < e) max = e
}
return max
"""
}
}
templates add f("minBy(f: (T) -> R)") {
doc = "Returns the first element yielding the smallest value of the given function or null if there are no elements"
typeParam("R: Comparable<R>")