Scoop API

scoop:scoop.std:0.1.0

src/collections/sorted_map_factories.scoop

Selected production source · src/collections/sorted_map_factories.scoop

1package scoop.collections
3import scoop.comparisons.Comparator
4import scoop.comparisons.naturalOrder
6internal class SortedMapSnapshot<K, V> internal constructor(
7 private val elements: SortedMap<K, V>
8) : SortedMap<K, V> by elements
10/**
11 * Creates an ordered map using the supplied or natural comparator; comparator-equivalent keys
12 * share one entry.
13 */
14public fun <K, V> sortedMapOf(comparator: Comparator<K>, vararg entries: (K, V)): TreeMap<K, V> {
15 val result = TreeMap<K, V>(comparator)
16 for ((key, value) in entries) result.put(key, value)
17 return result
20/**
21 * Creates an ordered map using the supplied or natural comparator; comparator-equivalent keys
22 * share one entry.
23 */
24public fun <K : Compare<K>, V> sortedMapOf(vararg entries: (K, V)): TreeMap<K, V> =
25 sortedMapOf<K, V>(naturalOrder<K>(), *entries)
27/**
28 * Copies associations into a read-only ordered map using the selected ordering.
29 */
30public fun <K, V> Iterable<(K, V)>.toSortedMap(comparator: Comparator<K>): SortedMap<K, V> {
31 val result = TreeMap<K, V>(comparator)
32 for ((key, value) in this) result.put(key, value)
33 return SortedMapSnapshot<K, V>(result)
36/**
37 * Copies associations into a read-only ordered map using the selected ordering.
38 */
39public fun <K : Compare<K>, V> Iterable<(K, V)>.toSortedMap(): SortedMap<K, V> =
40 toSortedMap<K, V>(naturalOrder<K>())
42/**
43 * Copies associations into a read-only ordered map using the selected ordering.
44 */
45public fun <K, V> Map<K, V>.toSortedMap(comparator: Comparator<K>): SortedMap<K, V> {
46 val result = TreeMap<K, V>(comparator)
47 for ((key, value) in this) result.put(key, value)
48 return SortedMapSnapshot<K, V>(result)
51/**
52 * Copies associations into a read-only ordered map using the selected ordering.
53 */
54public fun <K : Compare<K>, V> Map<K, V>.toSortedMap(): SortedMap<K, V> =
55 toSortedMap<K, V>(naturalOrder<K>())