clamp deprecation and Iterable#diff

This commit is contained in:
InsanusMokrassar 2021-09-08 12:10:32 +06:00
parent 925702d315
commit ef9af71960
3 changed files with 16 additions and 13 deletions

View File

@ -6,6 +6,8 @@
* `UUID`: `0.3.0` -> `0.3.1` * `UUID`: `0.3.0` -> `0.3.1`
* `Common`: * `Common`:
* New property `MPPFile#withoutSlashAtTheEnd` * New property `MPPFile#withoutSlashAtTheEnd`
* Extension `clamp` has been deprecated
* New extension `Iterable#diff`
## 0.5.24 ## 0.5.24

View File

@ -1,10 +1,5 @@
package dev.inmo.micro_utils.common package dev.inmo.micro_utils.common
@Deprecated("Redundant", ReplaceWith("coerceIn(min, max)"))
@Suppress("NOTHING_TO_INLINE") @Suppress("NOTHING_TO_INLINE")
inline fun <T : Comparable<T>> T.clamp(min: T, max: T): T { inline fun <T : Comparable<T>> T.clamp(min: T, max: T): T = coerceIn(min, max)
return when {
this < min -> min
this > max -> max
else -> this
}
}

View File

@ -2,6 +2,8 @@
package dev.inmo.micro_utils.common package dev.inmo.micro_utils.common
import kotlin.jvm.JvmInline
private inline fun <T> getObject( private inline fun <T> getObject(
additional: MutableList<T>, additional: MutableList<T>,
iterator: Iterator<T> iterator: Iterator<T>
@ -27,8 +29,8 @@ data class Diff<T> internal constructor(
private inline fun <T> performChanges( private inline fun <T> performChanges(
potentialChanges: MutableList<Pair<IndexedValue<T>?, IndexedValue<T>?>>, potentialChanges: MutableList<Pair<IndexedValue<T>?, IndexedValue<T>?>>,
additionalsInOld: MutableList<T>, additionsInOld: MutableList<T>,
additionalsInNew: MutableList<T>, additionsInNew: MutableList<T>,
changedList: MutableList<Pair<IndexedValue<T>, IndexedValue<T>>>, changedList: MutableList<Pair<IndexedValue<T>, IndexedValue<T>>>,
removedList: MutableList<IndexedValue<T>>, removedList: MutableList<IndexedValue<T>>,
addedList: MutableList<IndexedValue<T>>, addedList: MutableList<IndexedValue<T>>,
@ -52,20 +54,20 @@ private inline fun <T> performChanges(
newPotentials.first().second ?.let { addedList.add(it) } newPotentials.first().second ?.let { addedList.add(it) }
newPotentials.drop(1).take(newPotentials.size - 2).forEach { (oldOne, newOne) -> newPotentials.drop(1).take(newPotentials.size - 2).forEach { (oldOne, newOne) ->
addedList.add(newOne!!) addedList.add(newOne!!)
oldOne ?.let { additionalsInOld.add(oldOne.value) } oldOne ?.let { additionsInOld.add(oldOne.value) }
} }
if (newPotentials.size > 1) { if (newPotentials.size > 1) {
newPotentials.last().first ?.value ?.let { additionalsInOld.add(it) } newPotentials.last().first ?.value ?.let { additionsInOld.add(it) }
} }
} }
newOneEqualToOldObject -> { newOneEqualToOldObject -> {
newPotentials.first().first ?.let { removedList.add(it) } newPotentials.first().first ?.let { removedList.add(it) }
newPotentials.drop(1).take(newPotentials.size - 2).forEach { (oldOne, newOne) -> newPotentials.drop(1).take(newPotentials.size - 2).forEach { (oldOne, newOne) ->
removedList.add(oldOne!!) removedList.add(oldOne!!)
newOne ?.let { additionalsInNew.add(newOne.value) } newOne ?.let { additionsInNew.add(newOne.value) }
} }
if (newPotentials.size > 1) { 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 <T> Iterable<T>.calculateDiff(
return Diff(removedObjects.toList(), changedObjects.toList(), addedObjects.toList()) return Diff(removedObjects.toList(), changedObjects.toList(), addedObjects.toList())
} }
inline fun <T> Iterable<T>.diff(
other: Iterable<T>,
strictComparison: Boolean = false
): Diff<T> = calculateDiff(other, strictComparison)
inline fun <T> Diff(old: Iterable<T>, new: Iterable<T>) = old.calculateDiff(new) inline fun <T> Diff(old: Iterable<T>, new: Iterable<T>) = old.calculateDiff(new)
inline fun <T> StrictDiff(old: Iterable<T>, new: Iterable<T>) = old.calculateDiff(new, true) inline fun <T> StrictDiff(old: Iterable<T>, new: Iterable<T>) = old.calculateDiff(new, true)