add applyDiff in Diff Utils and update korlibs klock version

This commit is contained in:
2022-02-23 22:55:56 +06:00
parent 4dc8d30c52
commit 8a8f568b9a
4 changed files with 100 additions and 2 deletions

View File

@@ -153,3 +153,22 @@ inline fun <T> StrictDiff(old: Iterable<T>, new: Iterable<T>) = old.calculateDif
inline fun <T> Iterable<T>.calculateStrictDiff(
other: Iterable<T>
) = calculateDiff(other, strictComparison = true)
/**
* This method call [calculateDiff] with strict mode [strictComparison] and then apply differences to [this]
* mutable list
*/
fun <T> MutableList<T>.applyDiff(
source: Iterable<T>,
strictComparison: Boolean = false
) = calculateDiff(source, strictComparison).let {
for (i in it.removed.indices.sortedDescending()) {
removeAt(it.removed[i].index)
}
it.replaced.forEach { (_, new) ->
set(new.index, new.value)
}
it.added.forEach { (i, t) ->
add(i, t)
}
}