All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fuookami.ospf.kotlin.utils.math.ordinary.MinMax.kt Maven / Gradle / Ivy

There is a newer version: 1.0.29
Show newest version
package fuookami.ospf.kotlin.utils.math.ordinary

import fuookami.ospf.kotlin.utils.operator.*
import fuookami.ospf.kotlin.utils.functional.*

fun > min(lhs: T, rhs: T): T = if (lhs < rhs) lhs else rhs

fun > min(lhs: T, vararg rhs: T): T {
    var min = lhs
    for (e in rhs) {
        if (e leq min) {
            min = e
        }
    }
    return min
}

inline fun , U> minOf(
    lhs: U, vararg rhs: U,
    crossinline extractor: Extractor
): T {
    var min = extractor(lhs)
    for (e in rhs) {
        val v = extractor(e)
        if (v leq min) {
            min = v
        }
    }
    return min
}

fun > max(lhs: T, rhs: T): T = if (lhs > rhs) lhs else rhs

fun > max(lhs: T, vararg rhs: T): T {
    var max = lhs
    for (e in rhs) {
        if (e geq max) {
            max = e
        }
    }
    return max
}

inline fun , U> maxOf(
    lhs: U,
    vararg rhs: U,
    crossinline extractor: Extractor
): T {
    var max = extractor(lhs)
    for (e in rhs) {
        val v = extractor(e)
        if (v gr max) {
            max = v
        }
    }
    return max
}

fun > minmax(lhs: T, rhs: T): Pair = Pair(min(lhs, rhs), max(lhs, rhs))

fun > minMax(lhs: T, vararg rhs: T): Pair {
    var min = lhs
    var max = lhs
    for (e in rhs) {
        if (e ls min) {
            min = e
        }
        if (e gr max) {
            max = e
        }
    }
    return Pair(min, max)
}

inline fun , U> minMaxOf(
    lhs: U,
    vararg rhs: U,
    crossinline extractor: Extractor
): Pair {
    var min = extractor(lhs)
    var max = min
    for (e in rhs) {
        val v = extractor(e)
        if (v ls min) {
            min = v
        }
        if (v gr max) {
            max = v
        }
    }
    return Pair(min, max)
}

fun > Iterable.minMax(): Pair {
    val iterator = this.iterator()
    if (!iterator.hasNext()) {
        throw NoSuchElementException()
    }

    var min = iterator.next()
    var max = min
    while (iterator.hasNext()) {
        val v = iterator.next()
        if (v ls min) {
            min = v
        }
        if (v gr max) {
            max = v
        }
    }
    return Pair(min, max)
}

fun > Iterable.minMaxOrNull(): Pair? {
    val iterator = this.iterator()
    if (!iterator.hasNext()) {
        return null
    }

    var min = iterator.next()
    var max = min
    while (iterator.hasNext()) {
        val v = iterator.next()
        if (v ls min) {
            min = v
        }
        if (v gr max) {
            max = v
        }
    }
    return Pair(min, max)
}

inline fun , U> Iterable.minMaxBy(
    crossinline extractor: Extractor
): Pair {
    val iterator = this.iterator()
    var minE = iterator().next()
    var maxE = minE
    var min = extractor(minE)
    var max = min
    while (iterator.hasNext()) {
        val e = iterator.next()
        val v = extractor(e)
        if (v leq min) {
            minE = e
            min = v
        }
        if (v geq max) {
            maxE = e
            max = v
        }
    }
    return Pair(minE, maxE)
}

inline fun , U> Iterable.minMaxByOrNull(
    crossinline extractor: Extractor
): Pair? {
    val iterator = this.iterator()
    if (!iterator.hasNext()) {
        return null
    }
    var minE = iterator().next()
    var maxE = minE
    var min = extractor(minE)
    var max = min
    while (iterator.hasNext()) {
        val e = iterator.next()
        val v = extractor(e)
        if (v leq min) {
            minE = e
            min = v
        }
        if (v geq max) {
            maxE = e
            max = v
        }
    }
    return Pair(minE, maxE)
}

inline fun , U> Iterable.minMaxOf(
    crossinline extractor: Extractor
): Pair {
    val iterator = this.iterator()
    var min = extractor(iterator().next())
    var max = min
    while (iterator.hasNext()) {
        val v = extractor(iterator.next())
        if (v leq min) {
            min = v
        }
        if (v geq max) {
            max = v
        }
    }
    return Pair(min, max)
}

inline fun , U> Iterable.minMaxOfOrNull(
    crossinline extractor: Extractor
): Pair? {
    val iterator = this.iterator()
    if (!iterator.hasNext()) {
        return null
    }
    var min = extractor(iterator().next())
    var max = min
    while (iterator.hasNext()) {
        val v = extractor(iterator.next())
        if (v leq min) {
            min = v
        }
        if (v geq max) {
            max = v
        }
    }
    return Pair(min, max)
}

fun  Iterable.minMaxWith(comparator: kotlin.Comparator): Pair {
    val iterator = this.iterator()
    var min = iterator().next()
    var max = min
    while (iterator.hasNext()) {
        val v = iterator.next()
        if (comparator.compare(v, min) < 0) {
            min = v
        }
        if (comparator.compare(v, max) > 0) {
            max = v
        }
    }
    return Pair(min, max)
}

fun  Iterable.minMaxWithOrNull(comparator: kotlin.Comparator): Pair? {
    val iterator = this.iterator()
    if (!iterator.hasNext()) {
        return null
    }
    var min = iterator().next()
    var max = min
    while (iterator.hasNext()) {
        val v = iterator.next()
        if (comparator.compare(v, min) < 0) {
            min = v
        }
        if (comparator.compare(v, max) > 0) {
            max = v
        }
    }
    return Pair(min, max)
}

inline fun  Iterable.minMaxOfWith(
    comparator: kotlin.Comparator,
    crossinline extractor: Extractor
): Pair {
    val iterator = this.iterator()
    var min = extractor(iterator().next())
    var max = min
    while (iterator.hasNext()) {
        val v = extractor(iterator.next())
        if (comparator.compare(v, min) < 0) {
            min = v
        }
        if (comparator.compare(v, max) > 0) {
            max = v
        }
    }
    return Pair(min, max)
}

inline fun  Iterable.minMaxOfWithOrNull(
    comparator: kotlin.Comparator,
    crossinline extractor: Extractor
): Pair? {
    val iterator = this.iterator()
    if (!iterator.hasNext()) {
        return null
    }
    var min = extractor(iterator().next())
    var max = min
    while (iterator.hasNext()) {
        val v = extractor(iterator.next())
        if (comparator.compare(v, min) < 0) {
            min = v
        }
        if (comparator.compare(v, max) > 0) {
            max = v
        }
    }
    return Pair(min, max)
}