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

commonMain.dev.inmo.micro_utils.common.RangeIntersection.kt Maven / Gradle / Ivy

package dev.inmo.micro_utils.common

fun > ClosedRange.intersect(other: ClosedRange): Pair? = when {
    start == other.start && endInclusive == other.endInclusive -> start to endInclusive
    start > other.endInclusive || other.start > endInclusive -> null
    else -> maxOf(start, other.start) to minOf(endInclusive, other.endInclusive)
}

fun IntRange.intersect(
    other: IntRange
): IntRange? = (this as ClosedRange).intersect(other as ClosedRange) ?.let {
    it.first .. it.second
}

fun LongRange.intersect(
    other: LongRange
): LongRange? = (this as ClosedRange).intersect(other as ClosedRange) ?.let {
    it.first .. it.second
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy