standard library: 'max' and 'min' functions added (KT-3714, KT-3843, KT-3126)

This commit is contained in:
nik
2013-11-27 23:09:30 +04:00
parent 47d3e2e1bb
commit bf36eb07e9
14 changed files with 340 additions and 0 deletions
+26
View File
@@ -226,6 +226,32 @@ public inline fun <T, R, C: MutableCollection<in R>> Array<out T>.mapTo(result:
return result
}
/**
* Returns the largest element or null if there are no elements
*/
public fun <T: Comparable<T>> Array<out T>.max() : T? {
var max: T? = null
for (e in this) {
if (max == null || max!! < e) {
max = e
}
}
return max
}
/**
* Returns the smallest element or null if there are no elements
*/
public fun <T: Comparable<T>> Array<out T>.min() : T? {
var min: T? = null
for (e in this) {
if (min == null || min!! > e) {
min = e
}
}
return min
}
/**
* Partitions this collection into a pair of collections
*/
@@ -211,6 +211,32 @@ public inline fun <R, C: MutableCollection<in R>> ByteArray.mapTo(result: C, tra
return result
}
/**
* Returns the largest element or null if there are no elements
*/
public fun ByteArray.max() : Byte? {
var max: Byte? = null
for (e in this) {
if (max == null || max!! < e) {
max = e
}
}
return max
}
/**
* Returns the smallest element or null if there are no elements
*/
public fun ByteArray.min() : Byte? {
var min: Byte? = null
for (e in this) {
if (min == null || min!! > e) {
min = e
}
}
return min
}
/**
* Partitions this collection into a pair of collections
*/
@@ -211,6 +211,32 @@ public inline fun <R, C: MutableCollection<in R>> DoubleArray.mapTo(result: C, t
return result
}
/**
* Returns the largest element or null if there are no elements
*/
public fun DoubleArray.max() : Double? {
var max: Double? = null
for (e in this) {
if (max == null || max!! < e) {
max = e
}
}
return max
}
/**
* Returns the smallest element or null if there are no elements
*/
public fun DoubleArray.min() : Double? {
var min: Double? = null
for (e in this) {
if (min == null || min!! > e) {
min = e
}
}
return min
}
/**
* Partitions this collection into a pair of collections
*/
@@ -211,6 +211,32 @@ public inline fun <R, C: MutableCollection<in R>> FloatArray.mapTo(result: C, tr
return result
}
/**
* Returns the largest element or null if there are no elements
*/
public fun FloatArray.max() : Float? {
var max: Float? = null
for (e in this) {
if (max == null || max!! < e) {
max = e
}
}
return max
}
/**
* Returns the smallest element or null if there are no elements
*/
public fun FloatArray.min() : Float? {
var min: Float? = null
for (e in this) {
if (min == null || min!! > e) {
min = e
}
}
return min
}
/**
* Partitions this collection into a pair of collections
*/
@@ -211,6 +211,32 @@ public inline fun <R, C: MutableCollection<in R>> IntArray.mapTo(result: C, tran
return result
}
/**
* Returns the largest element or null if there are no elements
*/
public fun IntArray.max() : Int? {
var max: Int? = null
for (e in this) {
if (max == null || max!! < e) {
max = e
}
}
return max
}
/**
* Returns the smallest element or null if there are no elements
*/
public fun IntArray.min() : Int? {
var min: Int? = null
for (e in this) {
if (min == null || min!! > e) {
min = e
}
}
return min
}
/**
* Partitions this collection into a pair of collections
*/
@@ -212,6 +212,32 @@ public inline fun <T, R, C: MutableCollection<in R>> Iterable<T>.mapTo(result: C
return result
}
/**
* Returns the largest element or null if there are no elements
*/
public fun <T: Comparable<T>> Iterable<T>.max() : T? {
var max: T? = null
for (e in this) {
if (max == null || max!! < e) {
max = e
}
}
return max
}
/**
* Returns the smallest element or null if there are no elements
*/
public fun <T: Comparable<T>> Iterable<T>.min() : T? {
var min: T? = null
for (e in this) {
if (min == null || min!! > e) {
min = e
}
}
return min
}
/**
* Partitions this collection into a pair of collections
*/
@@ -212,6 +212,32 @@ public inline fun <T, R, C: MutableCollection<in R>> Iterator<T>.mapTo(result: C
return result
}
/**
* Returns the largest element or null if there are no elements
*/
public fun <T: Comparable<T>> Iterator<T>.max() : T? {
var max: T? = null
for (e in this) {
if (max == null || max!! < e) {
max = e
}
}
return max
}
/**
* Returns the smallest element or null if there are no elements
*/
public fun <T: Comparable<T>> Iterator<T>.min() : T? {
var min: T? = null
for (e in this) {
if (min == null || min!! > e) {
min = e
}
}
return min
}
/**
* Partitions this collection into a pair of collections
*/
@@ -211,6 +211,32 @@ public inline fun <R, C: MutableCollection<in R>> LongArray.mapTo(result: C, tra
return result
}
/**
* Returns the largest element or null if there are no elements
*/
public fun LongArray.max() : Long? {
var max: Long? = null
for (e in this) {
if (max == null || max!! < e) {
max = e
}
}
return max
}
/**
* Returns the smallest element or null if there are no elements
*/
public fun LongArray.min() : Long? {
var min: Long? = null
for (e in this) {
if (min == null || min!! > e) {
min = e
}
}
return min
}
/**
* Partitions this collection into a pair of collections
*/
@@ -211,6 +211,32 @@ public inline fun <R, C: MutableCollection<in R>> ShortArray.mapTo(result: C, tr
return result
}
/**
* Returns the largest element or null if there are no elements
*/
public fun ShortArray.max() : Short? {
var max: Short? = null
for (e in this) {
if (max == null || max!! < e) {
max = e
}
}
return max
}
/**
* Returns the smallest element or null if there are no elements
*/
public fun ShortArray.min() : Short? {
var min: Short? = null
for (e in this) {
if (min == null || min!! > e) {
min = e
}
}
return min
}
/**
* Partitions this collection into a pair of collections
*/