Scoop API

scoop:scoop.std:0.1.0

src/collections/sorted_ranges.scoop

Selected production source · src/collections/sorted_ranges.scoop

1package scoop.collections
3/**
4 * Copies the comparator-bounded range into an independent read-only ordered collection. Bounds
5 * default to lower-inclusive and upper-exclusive; reversed bounds throw.
6 */
7public fun <K, V> SortedMap<K, V>.copyOfRange(
8 fromKey: K, toKey: K, fromInclusive: Boolean = true, toInclusive: Boolean = false
9): SortedMap<K, V> {
10 val order = comparator
11 if (order.compare(fromKey, toKey) > 0L) throw IllegalArgumentException(Some("reversed map bounds"))
12 val result = TreeMap<K, V>(order)
13 for ((key, value) in this) {
14 val from = order.compare(key, fromKey)
15 val to = order.compare(key, toKey)
16 if ((from > 0L || (fromInclusive && from == 0L)) && (to < 0L || (toInclusive && to == 0L))) {
17 result.put(key, value)
18 }
19 }
20 return SortedMapSnapshot<K, V>(result)
23/**
24 * Copies the comparator-bounded range into an independent read-only ordered collection. Bounds
25 * default to lower-inclusive and upper-exclusive; reversed bounds throw.
26 */
27public fun <T> SortedSet<T>.copyOfRange(
28 fromElement: T, toElement: T, fromInclusive: Boolean = true, toInclusive: Boolean = false
29): SortedSet<T> {
30 val order = comparator
31 if (order.compare(fromElement, toElement) > 0L) throw IllegalArgumentException(Some("reversed set bounds"))
32 val result = TreeSet<T>(order)
33 for (element in this) {
34 val from = order.compare(element, fromElement)
35 val to = order.compare(element, toElement)
36 if ((from > 0L || (fromInclusive && from == 0L)) && (to < 0L || (toInclusive && to == 0L))) {
37 result.add(element)
38 }
39 }
40 return SortedSetSnapshot<T>(result)