Scoop API

scoop:scoop.std:0.1.0

src/collections/sorting.scoop

Selected production source · src/collections/sorting.scoop

1package scoop.collections
3import scoop.comparisons.Comparator
4import scoop.comparisons.comparatorOf
5import scoop.comparisons.naturalOrder
6import scoop.comparisons.reverseOrder
8private fun <T> mergeCollectionRuns(source: List<T>, target: MutableList<T>, width: Long, comparator: Comparator<T>) {
9 var start = 0L
10 while (start < source.size) {
11 val middle = start + (if (width < source.size - start) width else source.size - start)
12 val end = middle + (if (width < source.size - middle) width else source.size - middle)
13 var left = start
14 var right = middle
15 var output = start
16 while (output < end) {
17 if (right == end || (left < middle && comparator.compare(source[left], source[right]) <= 0L)) {
18 target[output] = source[left]
19 left += 1L
20 } else {
21 target[output] = source[right]
22 right += 1L
23 }
24 output += 1L
25 }
26 start = end
27 }
30private fun <T> sortedCollectionElements(input: Iterable<T>, comparator: Comparator<T>): MutableList<T> {
31 var source = input.toMutableList()
32 if (source.size < 2L) return source
33 var target = source.toMutableList()
34 var width = 1L
35 while (width < source.size) {
36 mergeCollectionRuns<T>(source, target, width, comparator)
37 val old = source
38 source = target
39 target = old
40 width = if (width > source.size / 2L) source.size else width * 2L
41 }
42 return source
45/**
46 * Returns a stable sorted copy using the supplied comparator.
47 */
48public fun <T> Iterable<T>.sortedWith(comparator: Comparator<T>): List<T> =
49 ListSnapshot<T>(sortedCollectionElements<T>(this, comparator).toArray())
51/**
52 * Returns a stable copy in natural ascending order.
53 */
54public fun <T : Compare<T>> Iterable<T>.sorted(): List<T> = sortedWith<T>(naturalOrder<T>())
55/**
56 * Returns a stable copy in natural descending order.
57 */
58public fun <T : Compare<T>> Iterable<T>.sortedDescending(): List<T> = sortedWith<T>(reverseOrder<T>())
60private struct CollectionSortKey<T, K>(val value: T, val key: K)
62/**
63 * Returns a stable copy ordered by the selector's natural key order.
64 */
65public fun <T, K : Compare<K>> Iterable<T>.sortedBy(selector: (T) -> K): List<T> {
66 val decorated = map<T, CollectionSortKey<T, K>> { value -> CollectionSortKey<T, K>(value, selector(value)) }
67 val order = comparatorOf<CollectionSortKey<T, K>> { left, right -> left.key.compareTo(right.key) }
68 return decorated.sortedWith(order).map<CollectionSortKey<T, K>, T> { entry -> entry.value }
71/**
72 * Stably sorts this mutable list in place using the supplied comparator.
73 */
74public fun <T> MutableList<T>.sortWith(comparator: Comparator<T>) {
75 val sorted = sortedCollectionElements<T>(this, comparator)
76 var index = 0L
77 for (element in sorted) { this[index] = element; index += 1L }
80/**
81 * Stably sorts this mutable list in place in natural ascending order.
82 */
83public fun <T : Compare<T>> MutableList<T>.sort() { sortWith<T>(naturalOrder<T>()) }