mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-12-22 00:27:15 +00:00
intersect
This commit is contained in:
parent
aa45a4ab13
commit
fb63de7568
@ -2,8 +2,11 @@
|
||||
|
||||
## 0.8.3
|
||||
|
||||
* `Common`:
|
||||
* Ranges intersection functionality
|
||||
* `Pagination`:
|
||||
* `Pagination` now extends `ClosedRange<Int>`
|
||||
* `Pagination` intersection functionality
|
||||
|
||||
## 0.8.2
|
||||
|
||||
|
@ -0,0 +1,19 @@
|
||||
package dev.inmo.micro_utils.common
|
||||
|
||||
fun <T : Comparable<T>> ClosedRange<T>.intersect(other: ClosedRange<T>): Pair<T, T>? = 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<Int>).intersect(other as ClosedRange<Int>) ?.let {
|
||||
it.first .. it.second
|
||||
}
|
||||
|
||||
fun LongRange.intersect(
|
||||
other: LongRange
|
||||
): LongRange? = (this as ClosedRange<Long>).intersect(other as ClosedRange<Long>) ?.let {
|
||||
it.first .. it.second
|
||||
}
|
@ -5,3 +5,13 @@ plugins {
|
||||
}
|
||||
|
||||
apply from: "$mppProjectWithSerializationPresetPath"
|
||||
|
||||
kotlin {
|
||||
sourceSets {
|
||||
commonMain {
|
||||
dependencies {
|
||||
api project(":micro_utils.common")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package dev.inmo.micro_utils.pagination
|
||||
|
||||
import dev.inmo.micro_utils.common.intersect
|
||||
import kotlin.math.ceil
|
||||
import kotlin.math.floor
|
||||
|
||||
@ -27,6 +28,12 @@ interface Pagination : ClosedRange<Int> {
|
||||
get() = lastIndex
|
||||
}
|
||||
|
||||
fun Pagination.intersect(
|
||||
other: Pagination
|
||||
): Pagination? = (this as ClosedRange<Int>).intersect(other as ClosedRange<Int>) ?.let {
|
||||
PaginationByIndexes(it.first, it.second)
|
||||
}
|
||||
|
||||
/**
|
||||
* Logical shortcut for comparison that page is 0
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user