mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2026-06-18 17:25:01 +00:00
Compare commits
4 Commits
13d0e1b682
...
0.18.1
| Author | SHA1 | Date | |
|---|---|---|---|
| 4b26a92b37 | |||
| 0a8453b4d2 | |||
| c9872a61b6 | |||
| 149a1aa278 |
@@ -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
|
||||||
|
)
|
||||||
|
|||||||
@@ -76,15 +76,12 @@ fun <K, V> Map<K, V>.diff(
|
|||||||
)
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Will apply changes with [other] map into [this] one
|
* Will apply [mapDiff] to [this] [MutableMap]
|
||||||
*
|
|
||||||
* @param compareFun Will be used to determine changed values
|
|
||||||
*/
|
*/
|
||||||
fun <K, V> MutableMap<K, V>.applyDiff(
|
fun <K, V> MutableMap<K, V>.applyDiff(
|
||||||
from: Map<K, V>,
|
mapDiff: MapDiff<K, V>
|
||||||
compareFun: (K, V, V) -> Boolean = { _, first, second -> first == second }
|
|
||||||
) {
|
) {
|
||||||
diff(from, compareFun).apply {
|
mapDiff.apply {
|
||||||
removed.keys.forEach { remove(it) }
|
removed.keys.forEach { remove(it) }
|
||||||
changed.forEach { (k, oldNew) ->
|
changed.forEach { (k, oldNew) ->
|
||||||
put(k, oldNew.second)
|
put(k, oldNew.second)
|
||||||
@@ -96,15 +93,43 @@ fun <K, V> MutableMap<K, V>.applyDiff(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Will apply changes with [other] map into [this] one
|
* Will apply changes with [from] map into [this] one
|
||||||
|
*
|
||||||
|
* @param compareFun Will be used to determine changed values
|
||||||
|
*
|
||||||
|
* @return [MapDiff] applied to [this] [MutableMap]
|
||||||
|
*/
|
||||||
|
fun <K, V> MutableMap<K, V>.applyDiff(
|
||||||
|
from: Map<K, V>,
|
||||||
|
compareFun: (K, V, V) -> Boolean
|
||||||
|
): MapDiff<K, V> {
|
||||||
|
return diff(from, compareFun).also {
|
||||||
|
applyDiff(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Will apply changes with [from] map into [this] one
|
||||||
*
|
*
|
||||||
* @param strictComparison If true, will use strict (===) comparison for the values' comparison. Otherwise, standard
|
* @param strictComparison If true, will use strict (===) comparison for the values' comparison. Otherwise, standard
|
||||||
* `equals` will be used
|
* `equals` will be used
|
||||||
|
*
|
||||||
|
* @return [MapDiff] applied to [this] [MutableMap]
|
||||||
*/
|
*/
|
||||||
fun <K, V> MutableMap<K, V>.applyDiff(
|
fun <K, V> MutableMap<K, V>.applyDiff(
|
||||||
other: Map<K, V>,
|
from: Map<K, V>,
|
||||||
strictComparison: Boolean = false
|
strictComparison: Boolean = false
|
||||||
) = applyDiff(
|
): MapDiff<K, V> = applyDiff(
|
||||||
other,
|
from,
|
||||||
compareFun = createCompareFun(strictComparison)
|
compareFun = createCompareFun(strictComparison)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse [this] [MapDiff]. Result will contain [MapDiff.added] on [MapDiff.removed] (and vice-verse), all the
|
||||||
|
* [MapDiff.changed] values will be reversed too
|
||||||
|
*/
|
||||||
|
fun <K, V> MapDiff<K, V>.reversed(): MapDiff<K, V> = MapDiff(
|
||||||
|
removed = added,
|
||||||
|
changed = changed.mapValues { (_, oldNew) -> oldNew.second to oldNew.first },
|
||||||
|
added = removed
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user