Scoop API

scoop:scoop.std:0.1.0

src/collections/sorted_set_factories.scoop

Selected production source · src/collections/sorted_set_factories.scoop

1package scoop.collections
3import scoop.comparisons.Comparator
4import scoop.comparisons.naturalOrder
6internal class SortedSetSnapshot<T> internal constructor(
7 private val elements: SortedSet<T>
8) : SortedSet<T> by elements
10/**
11 * Creates an ordered set using the supplied or natural comparator.
12 */
13public fun <T> sortedSetOf(comparator: Comparator<T>, vararg elements: T): TreeSet<T> {
14 val result = TreeSet<T>(comparator)
15 for (element in elements) result.add(element)
16 return result
19/**
20 * Creates an ordered set using the supplied or natural comparator.
21 */
22public fun <T : Compare<T>> sortedSetOf(vararg elements: T): TreeSet<T> =
23 sortedSetOf<T>(naturalOrder<T>(), *elements)
25/**
26 * Copies elements into a read-only ordered set using the selected ordering.
27 */
28public fun <T> Iterable<T>.toSortedSet(comparator: Comparator<T>): SortedSet<T> {
29 val result = TreeSet<T>(comparator)
30 for (element in this) result.add(element)
31 return SortedSetSnapshot<T>(result)
34/**
35 * Copies elements into a read-only ordered set using the selected ordering.
36 */
37public fun <T : Compare<T>> Iterable<T>.toSortedSet(): SortedSet<T> = toSortedSet<T>(naturalOrder<T>())