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

io.rtron.math.range.RangeExtensions.kt Maven / Gradle / Ivy

The newest version!
package io.rtron.math.range

/**
 * Returns the intersecting [Range] of a provided set of ranges.
 *
 * @receiver provided set of ranges for which the intersecting range shall be found
 * @return maximum range that intersects all ranges within the set
 */
fun > Set>.intersectingRange(): Range =
    reduce { acc, element -> acc.intersection(element) }

/**
 * Union operation of a set of [Range] to a [RangeSet].
 */
fun > Set>.unionRanges(): RangeSet =
    map { RangeSet.of(it) }.toSet().unionRangeSets()

/**
 * Returns true, if the set of ranges contains intersecting [Range].
 */
fun > Set>.containsIntersectingRanges(): Boolean {
    val rangeSetsList = this.map { RangeSet.of(it) }
    return rangeSetsList.toSet().containsIntersectingRangeSets()
}

/**
 * Returns true, if the list of ranges contains consecutively following ranges that intersect.
 */
fun > List>.containsConsecutivelyIntersectingRanges(): Boolean =
    map { it.toRangeSet() }.zipWithNext().any { it.first.intersects(it.second) }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy