From fb63de756870a3dd875a6ae57ab8c018b4500c23 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 17 Nov 2021 14:22:45 +0600 Subject: [PATCH] intersect --- CHANGELOG.md | 3 +++ .../micro_utils/common/RangeIntersection.kt | 19 +++++++++++++++++++ pagination/common/build.gradle | 10 ++++++++++ .../inmo/micro_utils/pagination/Pagination.kt | 7 +++++++ 4 files changed, 39 insertions(+) create mode 100644 common/src/commonMain/kotlin/dev/inmo/micro_utils/common/RangeIntersection.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b016dd9dab..410ddfff15d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,11 @@ ## 0.8.3 +* `Common`: + * Ranges intersection functionality * `Pagination`: * `Pagination` now extends `ClosedRange` + * `Pagination` intersection functionality ## 0.8.2 diff --git a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/RangeIntersection.kt b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/RangeIntersection.kt new file mode 100644 index 00000000000..e8bcf5a5750 --- /dev/null +++ b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/RangeIntersection.kt @@ -0,0 +1,19 @@ +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 +} diff --git a/pagination/common/build.gradle b/pagination/common/build.gradle index 7c54502f100..082ce99346a 100644 --- a/pagination/common/build.gradle +++ b/pagination/common/build.gradle @@ -5,3 +5,13 @@ plugins { } apply from: "$mppProjectWithSerializationPresetPath" + +kotlin { + sourceSets { + commonMain { + dependencies { + api project(":micro_utils.common") + } + } + } +} diff --git a/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/Pagination.kt b/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/Pagination.kt index 5afd2b1461e..b20a778af2f 100644 --- a/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/Pagination.kt +++ b/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/Pagination.kt @@ -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 { get() = lastIndex } +fun Pagination.intersect( + other: Pagination +): Pagination? = (this as ClosedRange).intersect(other as ClosedRange) ?.let { + PaginationByIndexes(it.first, it.second) +} + /** * Logical shortcut for comparison that page is 0 */