mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-22 16:23:50 +00:00
improve old diff API
This commit is contained in:
parent
0a8453b4d2
commit
4b26a92b37
@ -200,20 +200,18 @@ inline fun <T> Iterable<T>.calculateStrictDiff(
|
|||||||
) = calculateDiff(other, strictComparison = true)
|
) = calculateDiff(other, strictComparison = true)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method call [calculateDiff] with strict mode [strictComparison] and then apply differences to [this]
|
* Applies [diff] to [this] [MutableList]
|
||||||
* mutable list
|
|
||||||
*/
|
*/
|
||||||
fun <T> MutableList<T>.applyDiff(
|
fun <T> MutableList<T>.applyDiff(
|
||||||
source: Iterable<T>,
|
diff: Diff<T>
|
||||||
strictComparison: Boolean = false
|
) {
|
||||||
): Diff<T> = calculateDiff(source, strictComparison).also {
|
for (i in diff.removed.indices.sortedDescending()) {
|
||||||
for (i in it.removed.indices.sortedDescending()) {
|
removeAt(diff.removed[i].index)
|
||||||
removeAt(it.removed[i].index)
|
|
||||||
}
|
}
|
||||||
it.added.forEach { (i, t) ->
|
diff.added.forEach { (i, t) ->
|
||||||
add(i, t)
|
add(i, t)
|
||||||
}
|
}
|
||||||
it.replaced.forEach { (_, new) ->
|
diff.replaced.forEach { (_, new) ->
|
||||||
set(new.index, new.value)
|
set(new.index, new.value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -222,17 +220,30 @@ fun <T> MutableList<T>.applyDiff(
|
|||||||
* This method call [calculateDiff] with strict mode [strictComparison] and then apply differences to [this]
|
* This method call [calculateDiff] with strict mode [strictComparison] and then apply differences to [this]
|
||||||
* mutable list
|
* mutable list
|
||||||
*/
|
*/
|
||||||
|
fun <T> MutableList<T>.applyDiff(
|
||||||
|
source: Iterable<T>,
|
||||||
|
strictComparison: Boolean = false
|
||||||
|
): Diff<T> = calculateDiff(source, strictComparison).also {
|
||||||
|
applyDiff(it)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method call [calculateDiff] and then apply differences to [this]
|
||||||
|
* mutable list
|
||||||
|
*/
|
||||||
fun <T> MutableList<T>.applyDiff(
|
fun <T> MutableList<T>.applyDiff(
|
||||||
source: Iterable<T>,
|
source: Iterable<T>,
|
||||||
comparisonFun: (T?, T?) -> Boolean
|
comparisonFun: (T?, T?) -> Boolean
|
||||||
): Diff<T> = calculateDiff(source, comparisonFun).also {
|
): Diff<T> = calculateDiff(source, comparisonFun).also {
|
||||||
for (i in it.removed.indices.sortedDescending()) {
|
applyDiff(it)
|
||||||
removeAt(it.removed[i].index)
|
|
||||||
}
|
|
||||||
it.added.forEach { (i, t) ->
|
|
||||||
add(i, t)
|
|
||||||
}
|
|
||||||
it.replaced.forEach { (_, new) ->
|
|
||||||
set(new.index, new.value)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse [this] [Diff]. Result will contain [Diff.added] on [Diff.removed] (and vice-verse), all the
|
||||||
|
* [Diff.replaced] values will be reversed too
|
||||||
|
*/
|
||||||
|
fun <T> Diff<T>.reversed() = Diff(
|
||||||
|
removed = added,
|
||||||
|
replaced = replaced.map { it.second to it.first },
|
||||||
|
added = removed
|
||||||
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user