Scoop API

scoop:scoop.std:0.1.0

src/collections/bulk.scoop

Selected production source · src/collections/bulk.scoop

1package scoop.collections
3/**
4 * Adds each supplied element to the destination. Set overloads report whether membership changed.
5 */
6public 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 */
14public fun <T> MutableSet<T>.addAll(elements: Iterable<T>): Boolean {
15 val snapshot = elements.toList()
16 var changed = false
17 for (element in snapshot) {
18 if (add(element)) changed = true
19 }
20 return changed
23/**
24 * Copies associations into this map; later occurrences replace the value for the same key.
25 */
26public 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)
31/**
32 * Copies associations into this map; later occurrences replace the value for the same key.
33 */
34public 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)
39/**
40 * Removes members equal to the supplied elements and reports whether this collection changed.
41 */
42public fun <T> MutableSet<T>.removeAll(elements: Iterable<T>): Boolean {
43 val snapshot = elements.toList()
44 var changed = false
45 for (element in snapshot) {
46 if (remove(element)) changed = true
47 }
48 return changed
51internal fun <T> retainHashed(target: MutableSet<T>, elements: Iterable<T>): Boolean
52 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 = false
58 for (element in original) {
59 if (!keep.contains(element) && target.remove(element)) changed = true
60 }
61 return changed
64private fun <T : Equals<T>> matchesAny(value: T, elements: List<T>): Boolean {
65 for (element in elements) {
66 if (value == element) return true
67 }
68 return false
71private fun <T : Equals<T>> filterList(target: MutableList<T>, elements: Iterable<T>, retain: Boolean): Boolean {
72 val snapshot = elements.toList()
73 var index = 0L
74 var changed = false
75 while (index < target.size) {
76 if (matchesAny<T>(target[index], snapshot) != retain) {
77 target.removeAt(index)
78 changed = true
79 } else {
80 index += 1L
81 }
82 }
83 return changed
86/**
87 * Removes members equal to the supplied elements and reports whether this collection changed.
88 */
89public 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 list
94 * changed.
95 */
96public fun <T : Equals<T>> MutableList<T>.retainAll(elements: Iterable<T>): Boolean =
97 filterList<T>(this, elements, true)