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:
@@ -230,11 +230,12 @@ public inline fun <T, R, C: MutableCollection<in R>> Array<out T>.mapTo(result:
|
|||||||
* Returns the largest element or null if there are no elements
|
* Returns the largest element or null if there are no elements
|
||||||
*/
|
*/
|
||||||
public fun <T: Comparable<T>> Array<out T>.max() : T? {
|
public fun <T: Comparable<T>> Array<out T>.max() : T? {
|
||||||
var max: T? = null
|
if (isEmpty()) return null
|
||||||
for (e in this) {
|
|
||||||
if (max == null || max!! < e) {
|
var max = this[0]
|
||||||
max = e
|
for (i in 1..lastIndex) {
|
||||||
}
|
val e = this[i]
|
||||||
|
if (max < e) max = e
|
||||||
}
|
}
|
||||||
return max
|
return max
|
||||||
}
|
}
|
||||||
@@ -262,11 +263,12 @@ public inline fun <R: Comparable<R>, T: Any> Array<out T>.maxBy(f: (T) -> R) : T
|
|||||||
* Returns the smallest element or null if there are no elements
|
* Returns the smallest element or null if there are no elements
|
||||||
*/
|
*/
|
||||||
public fun <T: Comparable<T>> Array<out T>.min() : T? {
|
public fun <T: Comparable<T>> Array<out T>.min() : T? {
|
||||||
var min: T? = null
|
if (isEmpty()) return null
|
||||||
for (e in this) {
|
|
||||||
if (min == null || min!! > e) {
|
var min = this[0]
|
||||||
min = e
|
for (i in 1..lastIndex) {
|
||||||
}
|
val e = this[i]
|
||||||
|
if (min > e) min = e
|
||||||
}
|
}
|
||||||
return min
|
return min
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -229,11 +229,12 @@ public inline fun <R, C: MutableCollection<in R>> ByteArray.mapTo(result: C, tra
|
|||||||
* Returns the largest element or null if there are no elements
|
* Returns the largest element or null if there are no elements
|
||||||
*/
|
*/
|
||||||
public fun ByteArray.max() : Byte? {
|
public fun ByteArray.max() : Byte? {
|
||||||
var max: Byte? = null
|
if (isEmpty()) return null
|
||||||
for (e in this) {
|
|
||||||
if (max == null || max!! < e) {
|
var max = this[0]
|
||||||
max = e
|
for (i in 1..lastIndex) {
|
||||||
}
|
val e = this[i]
|
||||||
|
if (max < e) max = e
|
||||||
}
|
}
|
||||||
return max
|
return max
|
||||||
}
|
}
|
||||||
@@ -261,11 +262,12 @@ public inline fun <R: Comparable<R>> ByteArray.maxBy(f: (Byte) -> R) : Byte? {
|
|||||||
* Returns the smallest element or null if there are no elements
|
* Returns the smallest element or null if there are no elements
|
||||||
*/
|
*/
|
||||||
public fun ByteArray.min() : Byte? {
|
public fun ByteArray.min() : Byte? {
|
||||||
var min: Byte? = null
|
if (isEmpty()) return null
|
||||||
for (e in this) {
|
|
||||||
if (min == null || min!! > e) {
|
var min = this[0]
|
||||||
min = e
|
for (i in 1..lastIndex) {
|
||||||
}
|
val e = this[i]
|
||||||
|
if (min > e) min = e
|
||||||
}
|
}
|
||||||
return min
|
return min
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -229,11 +229,12 @@ public inline fun <R, C: MutableCollection<in R>> DoubleArray.mapTo(result: C, t
|
|||||||
* Returns the largest element or null if there are no elements
|
* Returns the largest element or null if there are no elements
|
||||||
*/
|
*/
|
||||||
public fun DoubleArray.max() : Double? {
|
public fun DoubleArray.max() : Double? {
|
||||||
var max: Double? = null
|
if (isEmpty()) return null
|
||||||
for (e in this) {
|
|
||||||
if (max == null || max!! < e) {
|
var max = this[0]
|
||||||
max = e
|
for (i in 1..lastIndex) {
|
||||||
}
|
val e = this[i]
|
||||||
|
if (max < e) max = e
|
||||||
}
|
}
|
||||||
return max
|
return max
|
||||||
}
|
}
|
||||||
@@ -261,11 +262,12 @@ public inline fun <R: Comparable<R>> DoubleArray.maxBy(f: (Double) -> R) : Doubl
|
|||||||
* Returns the smallest element or null if there are no elements
|
* Returns the smallest element or null if there are no elements
|
||||||
*/
|
*/
|
||||||
public fun DoubleArray.min() : Double? {
|
public fun DoubleArray.min() : Double? {
|
||||||
var min: Double? = null
|
if (isEmpty()) return null
|
||||||
for (e in this) {
|
|
||||||
if (min == null || min!! > e) {
|
var min = this[0]
|
||||||
min = e
|
for (i in 1..lastIndex) {
|
||||||
}
|
val e = this[i]
|
||||||
|
if (min > e) min = e
|
||||||
}
|
}
|
||||||
return min
|
return min
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -229,11 +229,12 @@ public inline fun <R, C: MutableCollection<in R>> FloatArray.mapTo(result: C, tr
|
|||||||
* Returns the largest element or null if there are no elements
|
* Returns the largest element or null if there are no elements
|
||||||
*/
|
*/
|
||||||
public fun FloatArray.max() : Float? {
|
public fun FloatArray.max() : Float? {
|
||||||
var max: Float? = null
|
if (isEmpty()) return null
|
||||||
for (e in this) {
|
|
||||||
if (max == null || max!! < e) {
|
var max = this[0]
|
||||||
max = e
|
for (i in 1..lastIndex) {
|
||||||
}
|
val e = this[i]
|
||||||
|
if (max < e) max = e
|
||||||
}
|
}
|
||||||
return max
|
return max
|
||||||
}
|
}
|
||||||
@@ -261,11 +262,12 @@ public inline fun <R: Comparable<R>> FloatArray.maxBy(f: (Float) -> R) : Float?
|
|||||||
* Returns the smallest element or null if there are no elements
|
* Returns the smallest element or null if there are no elements
|
||||||
*/
|
*/
|
||||||
public fun FloatArray.min() : Float? {
|
public fun FloatArray.min() : Float? {
|
||||||
var min: Float? = null
|
if (isEmpty()) return null
|
||||||
for (e in this) {
|
|
||||||
if (min == null || min!! > e) {
|
var min = this[0]
|
||||||
min = e
|
for (i in 1..lastIndex) {
|
||||||
}
|
val e = this[i]
|
||||||
|
if (min > e) min = e
|
||||||
}
|
}
|
||||||
return min
|
return min
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -229,11 +229,12 @@ public inline fun <R, C: MutableCollection<in R>> IntArray.mapTo(result: C, tran
|
|||||||
* Returns the largest element or null if there are no elements
|
* Returns the largest element or null if there are no elements
|
||||||
*/
|
*/
|
||||||
public fun IntArray.max() : Int? {
|
public fun IntArray.max() : Int? {
|
||||||
var max: Int? = null
|
if (isEmpty()) return null
|
||||||
for (e in this) {
|
|
||||||
if (max == null || max!! < e) {
|
var max = this[0]
|
||||||
max = e
|
for (i in 1..lastIndex) {
|
||||||
}
|
val e = this[i]
|
||||||
|
if (max < e) max = e
|
||||||
}
|
}
|
||||||
return max
|
return max
|
||||||
}
|
}
|
||||||
@@ -261,11 +262,12 @@ public inline fun <R: Comparable<R>> IntArray.maxBy(f: (Int) -> R) : Int? {
|
|||||||
* Returns the smallest element or null if there are no elements
|
* Returns the smallest element or null if there are no elements
|
||||||
*/
|
*/
|
||||||
public fun IntArray.min() : Int? {
|
public fun IntArray.min() : Int? {
|
||||||
var min: Int? = null
|
if (isEmpty()) return null
|
||||||
for (e in this) {
|
|
||||||
if (min == null || min!! > e) {
|
var min = this[0]
|
||||||
min = e
|
for (i in 1..lastIndex) {
|
||||||
}
|
val e = this[i]
|
||||||
|
if (min > e) min = e
|
||||||
}
|
}
|
||||||
return min
|
return min
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -216,11 +216,13 @@ public inline fun <T, R, C: MutableCollection<in R>> Iterable<T>.mapTo(result: C
|
|||||||
* Returns the largest element or null if there are no elements
|
* Returns the largest element or null if there are no elements
|
||||||
*/
|
*/
|
||||||
public fun <T: Comparable<T>> Iterable<T>.max() : T? {
|
public fun <T: Comparable<T>> Iterable<T>.max() : T? {
|
||||||
var max: T? = null
|
val iterator = iterator()
|
||||||
for (e in this) {
|
if (!iterator.hasNext()) return null
|
||||||
if (max == null || max!! < e) {
|
|
||||||
max = e
|
var max = iterator.next()
|
||||||
}
|
while (iterator.hasNext()) {
|
||||||
|
val e = iterator.next()
|
||||||
|
if (max < e) max = e
|
||||||
}
|
}
|
||||||
return max
|
return max
|
||||||
}
|
}
|
||||||
@@ -249,11 +251,13 @@ public inline fun <R: Comparable<R>, T: Any> Iterable<T>.maxBy(f: (T) -> R) : T?
|
|||||||
* Returns the smallest element or null if there are no elements
|
* Returns the smallest element or null if there are no elements
|
||||||
*/
|
*/
|
||||||
public fun <T: Comparable<T>> Iterable<T>.min() : T? {
|
public fun <T: Comparable<T>> Iterable<T>.min() : T? {
|
||||||
var min: T? = null
|
val iterator = iterator()
|
||||||
for (e in this) {
|
if (!iterator.hasNext()) return null
|
||||||
if (min == null || min!! > e) {
|
|
||||||
min = e
|
var min = iterator.next()
|
||||||
}
|
while (iterator.hasNext()) {
|
||||||
|
val e = iterator.next()
|
||||||
|
if (min > e) min = e
|
||||||
}
|
}
|
||||||
return min
|
return min
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -216,11 +216,12 @@ public inline fun <T, R, C: MutableCollection<in R>> Iterator<T>.mapTo(result: C
|
|||||||
* Returns the largest element or null if there are no elements
|
* Returns the largest element or null if there are no elements
|
||||||
*/
|
*/
|
||||||
public fun <T: Comparable<T>> Iterator<T>.max() : T? {
|
public fun <T: Comparable<T>> Iterator<T>.max() : T? {
|
||||||
var max: T? = null
|
if (!hasNext()) return null
|
||||||
for (e in this) {
|
|
||||||
if (max == null || max!! < e) {
|
var max = next()
|
||||||
max = e
|
while (hasNext()) {
|
||||||
}
|
val e = next()
|
||||||
|
if (max < e) max = e
|
||||||
}
|
}
|
||||||
return max
|
return max
|
||||||
}
|
}
|
||||||
@@ -248,11 +249,12 @@ public inline fun <R: Comparable<R>, T: Any> Iterator<T>.maxBy(f: (T) -> R) : T?
|
|||||||
* Returns the smallest element or null if there are no elements
|
* Returns the smallest element or null if there are no elements
|
||||||
*/
|
*/
|
||||||
public fun <T: Comparable<T>> Iterator<T>.min() : T? {
|
public fun <T: Comparable<T>> Iterator<T>.min() : T? {
|
||||||
var min: T? = null
|
if (!hasNext()) return null
|
||||||
for (e in this) {
|
|
||||||
if (min == null || min!! > e) {
|
var min = next()
|
||||||
min = e
|
while (hasNext()) {
|
||||||
}
|
val e = next()
|
||||||
|
if (min > e) min = e
|
||||||
}
|
}
|
||||||
return min
|
return min
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -229,11 +229,12 @@ public inline fun <R, C: MutableCollection<in R>> LongArray.mapTo(result: C, tra
|
|||||||
* Returns the largest element or null if there are no elements
|
* Returns the largest element or null if there are no elements
|
||||||
*/
|
*/
|
||||||
public fun LongArray.max() : Long? {
|
public fun LongArray.max() : Long? {
|
||||||
var max: Long? = null
|
if (isEmpty()) return null
|
||||||
for (e in this) {
|
|
||||||
if (max == null || max!! < e) {
|
var max = this[0]
|
||||||
max = e
|
for (i in 1..lastIndex) {
|
||||||
}
|
val e = this[i]
|
||||||
|
if (max < e) max = e
|
||||||
}
|
}
|
||||||
return max
|
return max
|
||||||
}
|
}
|
||||||
@@ -261,11 +262,12 @@ public inline fun <R: Comparable<R>> LongArray.maxBy(f: (Long) -> R) : Long? {
|
|||||||
* Returns the smallest element or null if there are no elements
|
* Returns the smallest element or null if there are no elements
|
||||||
*/
|
*/
|
||||||
public fun LongArray.min() : Long? {
|
public fun LongArray.min() : Long? {
|
||||||
var min: Long? = null
|
if (isEmpty()) return null
|
||||||
for (e in this) {
|
|
||||||
if (min == null || min!! > e) {
|
var min = this[0]
|
||||||
min = e
|
for (i in 1..lastIndex) {
|
||||||
}
|
val e = this[i]
|
||||||
|
if (min > e) min = e
|
||||||
}
|
}
|
||||||
return min
|
return min
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -229,11 +229,12 @@ public inline fun <R, C: MutableCollection<in R>> ShortArray.mapTo(result: C, tr
|
|||||||
* Returns the largest element or null if there are no elements
|
* Returns the largest element or null if there are no elements
|
||||||
*/
|
*/
|
||||||
public fun ShortArray.max() : Short? {
|
public fun ShortArray.max() : Short? {
|
||||||
var max: Short? = null
|
if (isEmpty()) return null
|
||||||
for (e in this) {
|
|
||||||
if (max == null || max!! < e) {
|
var max = this[0]
|
||||||
max = e
|
for (i in 1..lastIndex) {
|
||||||
}
|
val e = this[i]
|
||||||
|
if (max < e) max = e
|
||||||
}
|
}
|
||||||
return max
|
return max
|
||||||
}
|
}
|
||||||
@@ -261,11 +262,12 @@ public inline fun <R: Comparable<R>> ShortArray.maxBy(f: (Short) -> R) : Short?
|
|||||||
* Returns the smallest element or null if there are no elements
|
* Returns the smallest element or null if there are no elements
|
||||||
*/
|
*/
|
||||||
public fun ShortArray.min() : Short? {
|
public fun ShortArray.min() : Short? {
|
||||||
var min: Short? = null
|
if (isEmpty()) return null
|
||||||
for (e in this) {
|
|
||||||
if (min == null || min!! > e) {
|
var min = this[0]
|
||||||
min = e
|
for (i in 1..lastIndex) {
|
||||||
}
|
val e = this[i]
|
||||||
|
if (min > e) min = e
|
||||||
}
|
}
|
||||||
return min
|
return min
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 = \"...\")") {
|
templates add f("appendString(buffer: Appendable, separator: String = \", \", prefix: String =\"\", postfix: String = \"\", limit: Int = -1, truncated: String = \"...\")") {
|
||||||
isInline = false
|
isInline = false
|
||||||
doc =
|
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)") {
|
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"
|
doc = "Returns the first element yielding the smallest value of the given function or null if there are no elements"
|
||||||
typeParam("R: Comparable<R>")
|
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)") {
|
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"
|
doc = "Returns the first element yielding the smallest value of the given function or null if there are no elements"
|
||||||
typeParam("R: Comparable<R>")
|
typeParam("R: Comparable<R>")
|
||||||
|
|||||||
Reference in New Issue
Block a user