Reintroduce min/max operations for non-empty collections KT-50146
This commit is contained in:
@@ -1180,16 +1180,49 @@ public inline fun CharSequence.forEachIndexed(action: (index: Int, Char) -> Unit
|
||||
for (item in this) action(index++, item)
|
||||
}
|
||||
|
||||
@Deprecated("Use maxOrNull instead.", ReplaceWith("this.maxOrNull()"))
|
||||
@DeprecatedSinceKotlin(warningSince = "1.4", errorSince = "1.5", hiddenSince = "1.6")
|
||||
public fun CharSequence.max(): Char? {
|
||||
return maxOrNull()
|
||||
/**
|
||||
* Returns the largest character.
|
||||
*
|
||||
* @throws NoSuchElementException if the char sequence is empty.
|
||||
*/
|
||||
@SinceKotlin("1.7")
|
||||
@kotlin.jvm.JvmName("maxOrThrow")
|
||||
@Suppress("CONFLICTING_OVERLOADS")
|
||||
public fun CharSequence.max(): Char {
|
||||
if (isEmpty()) throw NoSuchElementException()
|
||||
var max = this[0]
|
||||
for (i in 1..lastIndex) {
|
||||
val e = this[i]
|
||||
if (max < e) max = e
|
||||
}
|
||||
return max
|
||||
}
|
||||
|
||||
@Deprecated("Use maxByOrNull instead.", ReplaceWith("this.maxByOrNull(selector)"))
|
||||
@DeprecatedSinceKotlin(warningSince = "1.4", errorSince = "1.5", hiddenSince = "1.6")
|
||||
public inline fun <R : Comparable<R>> CharSequence.maxBy(selector: (Char) -> R): Char? {
|
||||
return maxByOrNull(selector)
|
||||
/**
|
||||
* Returns the first character yielding the largest value of the given function.
|
||||
*
|
||||
* @throws NoSuchElementException if the char sequence is empty.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.maxBy
|
||||
*/
|
||||
@SinceKotlin("1.7")
|
||||
@kotlin.jvm.JvmName("maxByOrThrow")
|
||||
@Suppress("CONFLICTING_OVERLOADS")
|
||||
public inline fun <R : Comparable<R>> CharSequence.maxBy(selector: (Char) -> R): Char {
|
||||
if (isEmpty()) throw NoSuchElementException()
|
||||
var maxElem = this[0]
|
||||
val lastIndex = this.lastIndex
|
||||
if (lastIndex == 0) return maxElem
|
||||
var maxValue = selector(maxElem)
|
||||
for (i in 1..lastIndex) {
|
||||
val e = this[i]
|
||||
val v = selector(e)
|
||||
if (maxValue < v) {
|
||||
maxElem = e
|
||||
maxValue = v
|
||||
}
|
||||
}
|
||||
return maxElem
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1397,10 +1430,22 @@ public fun CharSequence.maxOrNull(): Char? {
|
||||
return max
|
||||
}
|
||||
|
||||
@Deprecated("Use maxWithOrNull instead.", ReplaceWith("this.maxWithOrNull(comparator)"))
|
||||
@DeprecatedSinceKotlin(warningSince = "1.4", errorSince = "1.5", hiddenSince = "1.6")
|
||||
public fun CharSequence.maxWith(comparator: Comparator<in Char>): Char? {
|
||||
return maxWithOrNull(comparator)
|
||||
/**
|
||||
* Returns the first character having the largest value according to the provided [comparator].
|
||||
*
|
||||
* @throws NoSuchElementException if the char sequence is empty.
|
||||
*/
|
||||
@SinceKotlin("1.7")
|
||||
@kotlin.jvm.JvmName("maxWithOrThrow")
|
||||
@Suppress("CONFLICTING_OVERLOADS")
|
||||
public fun CharSequence.maxWith(comparator: Comparator<in Char>): Char {
|
||||
if (isEmpty()) throw NoSuchElementException()
|
||||
var max = this[0]
|
||||
for (i in 1..lastIndex) {
|
||||
val e = this[i]
|
||||
if (comparator.compare(max, e) < 0) max = e
|
||||
}
|
||||
return max
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1417,16 +1462,49 @@ public fun CharSequence.maxWithOrNull(comparator: Comparator<in Char>): Char? {
|
||||
return max
|
||||
}
|
||||
|
||||
@Deprecated("Use minOrNull instead.", ReplaceWith("this.minOrNull()"))
|
||||
@DeprecatedSinceKotlin(warningSince = "1.4", errorSince = "1.5", hiddenSince = "1.6")
|
||||
public fun CharSequence.min(): Char? {
|
||||
return minOrNull()
|
||||
/**
|
||||
* Returns the smallest character.
|
||||
*
|
||||
* @throws NoSuchElementException if the char sequence is empty.
|
||||
*/
|
||||
@SinceKotlin("1.7")
|
||||
@kotlin.jvm.JvmName("minOrThrow")
|
||||
@Suppress("CONFLICTING_OVERLOADS")
|
||||
public fun CharSequence.min(): Char {
|
||||
if (isEmpty()) throw NoSuchElementException()
|
||||
var min = this[0]
|
||||
for (i in 1..lastIndex) {
|
||||
val e = this[i]
|
||||
if (min > e) min = e
|
||||
}
|
||||
return min
|
||||
}
|
||||
|
||||
@Deprecated("Use minByOrNull instead.", ReplaceWith("this.minByOrNull(selector)"))
|
||||
@DeprecatedSinceKotlin(warningSince = "1.4", errorSince = "1.5", hiddenSince = "1.6")
|
||||
public inline fun <R : Comparable<R>> CharSequence.minBy(selector: (Char) -> R): Char? {
|
||||
return minByOrNull(selector)
|
||||
/**
|
||||
* Returns the first character yielding the smallest value of the given function.
|
||||
*
|
||||
* @throws NoSuchElementException if the char sequence is empty.
|
||||
*
|
||||
* @sample samples.collections.Collections.Aggregates.minBy
|
||||
*/
|
||||
@SinceKotlin("1.7")
|
||||
@kotlin.jvm.JvmName("minByOrThrow")
|
||||
@Suppress("CONFLICTING_OVERLOADS")
|
||||
public inline fun <R : Comparable<R>> CharSequence.minBy(selector: (Char) -> R): Char {
|
||||
if (isEmpty()) throw NoSuchElementException()
|
||||
var minElem = this[0]
|
||||
val lastIndex = this.lastIndex
|
||||
if (lastIndex == 0) return minElem
|
||||
var minValue = selector(minElem)
|
||||
for (i in 1..lastIndex) {
|
||||
val e = this[i]
|
||||
val v = selector(e)
|
||||
if (minValue > v) {
|
||||
minElem = e
|
||||
minValue = v
|
||||
}
|
||||
}
|
||||
return minElem
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1634,10 +1712,22 @@ public fun CharSequence.minOrNull(): Char? {
|
||||
return min
|
||||
}
|
||||
|
||||
@Deprecated("Use minWithOrNull instead.", ReplaceWith("this.minWithOrNull(comparator)"))
|
||||
@DeprecatedSinceKotlin(warningSince = "1.4", errorSince = "1.5", hiddenSince = "1.6")
|
||||
public fun CharSequence.minWith(comparator: Comparator<in Char>): Char? {
|
||||
return minWithOrNull(comparator)
|
||||
/**
|
||||
* Returns the first character having the smallest value according to the provided [comparator].
|
||||
*
|
||||
* @throws NoSuchElementException if the char sequence is empty.
|
||||
*/
|
||||
@SinceKotlin("1.7")
|
||||
@kotlin.jvm.JvmName("minWithOrThrow")
|
||||
@Suppress("CONFLICTING_OVERLOADS")
|
||||
public fun CharSequence.minWith(comparator: Comparator<in Char>): Char {
|
||||
if (isEmpty()) throw NoSuchElementException()
|
||||
var min = this[0]
|
||||
for (i in 1..lastIndex) {
|
||||
val e = this[i]
|
||||
if (comparator.compare(min, e) > 0) min = e
|
||||
}
|
||||
return min
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user