diff --git a/CHANGELOG.md b/CHANGELOG.md index bee7a514b15..7f3a78d427c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ * `UUID`: `0.3.0` -> `0.3.1` * `Common`: * New property `MPPFile#withoutSlashAtTheEnd` + * Extension `clamp` has been deprecated + * New extension `Iterable#diff` ## 0.5.24 diff --git a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Clamp.kt b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Clamp.kt index 98eab69372c..7d6159ca5f7 100644 --- a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Clamp.kt +++ b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Clamp.kt @@ -1,10 +1,5 @@ package dev.inmo.micro_utils.common +@Deprecated("Redundant", ReplaceWith("coerceIn(min, max)")) @Suppress("NOTHING_TO_INLINE") -inline fun > T.clamp(min: T, max: T): T { - return when { - this < min -> min - this > max -> max - else -> this - } -} +inline fun > T.clamp(min: T, max: T): T = coerceIn(min, max) diff --git a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/DiffUtils.kt b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/DiffUtils.kt index 0c63e30c786..221237b4fee 100644 --- a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/DiffUtils.kt +++ b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/DiffUtils.kt @@ -2,6 +2,8 @@ package dev.inmo.micro_utils.common +import kotlin.jvm.JvmInline + private inline fun getObject( additional: MutableList, iterator: Iterator @@ -27,8 +29,8 @@ data class Diff internal constructor( private inline fun performChanges( potentialChanges: MutableList?, IndexedValue?>>, - additionalsInOld: MutableList, - additionalsInNew: MutableList, + additionsInOld: MutableList, + additionsInNew: MutableList, changedList: MutableList, IndexedValue>>, removedList: MutableList>, addedList: MutableList>, @@ -52,20 +54,20 @@ private inline fun performChanges( newPotentials.first().second ?.let { addedList.add(it) } newPotentials.drop(1).take(newPotentials.size - 2).forEach { (oldOne, newOne) -> addedList.add(newOne!!) - oldOne ?.let { additionalsInOld.add(oldOne.value) } + oldOne ?.let { additionsInOld.add(oldOne.value) } } if (newPotentials.size > 1) { - newPotentials.last().first ?.value ?.let { additionalsInOld.add(it) } + newPotentials.last().first ?.value ?.let { additionsInOld.add(it) } } } newOneEqualToOldObject -> { newPotentials.first().first ?.let { removedList.add(it) } newPotentials.drop(1).take(newPotentials.size - 2).forEach { (oldOne, newOne) -> removedList.add(oldOne!!) - newOne ?.let { additionalsInNew.add(newOne.value) } + newOne ?.let { additionsInNew.add(newOne.value) } } if (newPotentials.size > 1) { - newPotentials.last().second ?.value ?.let { additionalsInNew.add(it) } + newPotentials.last().second ?.value ?.let { additionsInNew.add(it) } } } } @@ -139,6 +141,10 @@ fun Iterable.calculateDiff( return Diff(removedObjects.toList(), changedObjects.toList(), addedObjects.toList()) } +inline fun Iterable.diff( + other: Iterable, + strictComparison: Boolean = false +): Diff = calculateDiff(other, strictComparison) inline fun Diff(old: Iterable, new: Iterable) = old.calculateDiff(new) inline fun StrictDiff(old: Iterable, new: Iterable) = old.calculateDiff(new, true)