scoop:scoop.std:0.1.0
src/collections/maps.scoop
Selected production source · src/collections/maps.scoop
1
package scoop.collections3
/**4
* Read access to key-value associations. Missing keys return None; iteration and views expose5
* entries in the implementation's order.6
*/7
public interface Map<K, V> : IndexGet<K, V?>, Contains<K>, Iterable<Map.Entry<K, V>> {8
/**9
* The number of key-value associations.10
*/11
public val size: Long12
/**13
* Returns the value associated with the key, or None when absent.14
*/15
public override operator fun get(key: K): V?16
/**17
* Tests whether the key has an association.18
*/19
public fun containsKey(key: K): Boolean20
/**21
* Tests key membership, not value membership.22
*/23
public override operator fun contains(value: K): Boolean = containsKey(value)24
/**25
* A read-only view of the map's current keys.26
*/27
public val keys: Set<K>28
/**29
* A read-only view of current values, retaining duplicates.30
*/31
public val values: Collection<V>32
/**33
* A read-only view of current key-value entries.34
*/35
public val entries: Collection<Map.Entry<K, V>>37
/**38
* A key and its associated value, captured together as one map entry.39
*/40
public struct Entry<K, V>(val key: K, val value: V)41
}43
/**44
* A map supporting insertion, replacement, removal, and clearing.45
*/46
public interface MutableMap<K, V> : Map<K, V>, IndexSet<K, V> {47
/**48
* Associates the value with the key and returns the previous value, or None for a new key.49
*/50
public fun put(key: K, value: V): V?51
/**52
* Associates the value with the key, discarding any previous value.53
*/54
public override operator fun set(key: K, value: V) { put(key, value) }55
/**56
* Removes the key and returns its former value, or None if absent.57
*/58
public fun remove(key: K): V?59
/**60
* Removes every association.61
*/62
public fun clear(): Unit63
}65
/**66
* Returns whether the collection contains no entries or elements.67
*/68
public fun <K, V> Map<K, V>.isEmpty(): Boolean = size == 0L70
/**71
* Returns whether the collection contains at least one entry or element.72
*/73
public fun <K, V> Map<K, V>.isNotEmpty(): Boolean = size != 0L75
/**76
* Scans values for one equal to the argument.77
*/78
public fun <K, V : Equals<V>> Map<K, V>.containsValue(value: V): Boolean {79
for (entry in this) {80
if (entry.value == value) return true81
}82
return false83
}85
/**86
* Returns the mapped value or throws NoSuchElementException when the key is absent.87
*/88
public fun <K, V> Map<K, V>.getValue(key: K): V = when (get(key)) {89
case Some(val value) -> value90
case None -> throw NoSuchElementException("key is absent")91
}93
/**94
* Returns the mapped value, or the supplied fallback when absent.95
*/96
public fun <K, V> Map<K, V>.getOrDefault(key: K, defaultValue: V): V = when (get(key)) {97
case Some(val value) -> value98
case None -> defaultValue99
}101
/**102
* Returns the mapped value, computing the fallback only when the key is absent.103
*/104
public fun <K, V> Map<K, V>.getOrElse(key: K, defaultValue: () -> V): V = when (get(key)) {105
case Some(val value) -> value106
case None -> defaultValue()107
}109
/**110
* Returns an existing value, or computes, inserts, and returns one for an absent key. This111
* operation is not a concurrent-map primitive.112
*/113
public fun <K, V> MutableMap<K, V>.getOrPut(key: K, defaultValue: () -> V): V = when (get(key)) {114
case Some(val value) -> value115
case None -> {116
val value = defaultValue()117
put(key, value)118
value119
}120
}