scoop:scoop.std:0.1.0
src/collections/bulk.scoop
Selected production source · src/collections/bulk.scoop
1
package scoop.collections3
/**4
* Adds each supplied element to the destination. Set overloads report whether membership changed.5
*/6
public fun <T> MutableList<T>.addAll(elements: Iterable<T>) {7
val snapshot = elements.toList()8
for (element in snapshot) add(element)9
}11
/**12
* Adds each supplied element to the destination. Set overloads report whether membership changed.13
*/14
public fun <T> MutableSet<T>.addAll(elements: Iterable<T>): Boolean {15
val snapshot = elements.toList()16
var changed = false17
for (element in snapshot) {18
if (add(element)) changed = true19
}20
return changed21
}23
/**24
* Copies associations into this map; later occurrences replace the value for the same key.25
*/26
public fun <K, V> MutableMap<K, V>.putAll(entries: Iterable<(K, V)>) {27
val snapshot = entries.toList()28
for ((key, value) in snapshot) put(key, value)29
}31
/**32
* Copies associations into this map; later occurrences replace the value for the same key.33
*/34
public fun <K, V> MutableMap<K, V>.putAll(entries: Map<K, V>) {35
val snapshot = entries.toList()36
for ((key, value) in snapshot) put(key, value)37
}39
/**40
* Removes members equal to the supplied elements and reports whether this collection changed.41
*/42
public fun <T> MutableSet<T>.removeAll(elements: Iterable<T>): Boolean {43
val snapshot = elements.toList()44
var changed = false45
for (element in snapshot) {46
if (remove(element)) changed = true47
}48
return changed49
}51
internal fun <T> retainHashed(target: MutableSet<T>, elements: Iterable<T>): Boolean52
where T : Equals<T>, T : Hash {53
val snapshot = elements.toList()54
val keep = HashSet<T>()55
for (element in snapshot) keep.add(element)56
val original = target.toList()57
var changed = false58
for (element in original) {59
if (!keep.contains(element) && target.remove(element)) changed = true60
}61
return changed62
}64
private fun <T : Equals<T>> matchesAny(value: T, elements: List<T>): Boolean {65
for (element in elements) {66
if (value == element) return true67
}68
return false69
}71
private fun <T : Equals<T>> filterList(target: MutableList<T>, elements: Iterable<T>, retain: Boolean): Boolean {72
val snapshot = elements.toList()73
var index = 0L74
var changed = false75
while (index < target.size) {76
if (matchesAny<T>(target[index], snapshot) != retain) {77
target.removeAt(index)78
changed = true79
} else {80
index += 1L81
}82
}83
return changed84
}86
/**87
* Removes members equal to the supplied elements and reports whether this collection changed.88
*/89
public fun <T : Equals<T>> MutableList<T>.removeAll(elements: Iterable<T>): Boolean =90
filterList<T>(this, elements, false)92
/**93
* Removes elements not present in the supplied membership collection and reports whether this list94
* changed.95
*/96
public fun <T : Equals<T>> MutableList<T>.retainAll(elements: Iterable<T>): Boolean =97
filterList<T>(this, elements, true)