scoop:scoop.std:0.1.0
src/collections/sorting.scoop
Selected production source · src/collections/sorting.scoop
1
package scoop.collections3
import scoop.comparisons.Comparator4
import scoop.comparisons.comparatorOf5
import scoop.comparisons.naturalOrder6
import scoop.comparisons.reverseOrder8
private fun <T> mergeCollectionRuns(source: List<T>, target: MutableList<T>, width: Long, comparator: Comparator<T>) {9
var start = 0L10
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 = start14
var right = middle15
var output = start16
while (output < end) {17
if (right == end || (left < middle && comparator.compare(source[left], source[right]) <= 0L)) {18
target[output] = source[left]19
left += 1L20
} else {21
target[output] = source[right]22
right += 1L23
}24
output += 1L25
}26
start = end27
}28
}30
private fun <T> sortedCollectionElements(input: Iterable<T>, comparator: Comparator<T>): MutableList<T> {31
var source = input.toMutableList()32
if (source.size < 2L) return source33
var target = source.toMutableList()34
var width = 1L35
while (width < source.size) {36
mergeCollectionRuns<T>(source, target, width, comparator)37
val old = source38
source = target39
target = old40
width = if (width > source.size / 2L) source.size else width * 2L41
}42
return source43
}45
/**46
* Returns a stable sorted copy using the supplied comparator.47
*/48
public 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
*/54
public fun <T : Compare<T>> Iterable<T>.sorted(): List<T> = sortedWith<T>(naturalOrder<T>())55
/**56
* Returns a stable copy in natural descending order.57
*/58
public fun <T : Compare<T>> Iterable<T>.sortedDescending(): List<T> = sortedWith<T>(reverseOrder<T>())60
private 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
*/65
public 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 }69
}71
/**72
* Stably sorts this mutable list in place using the supplied comparator.73
*/74
public fun <T> MutableList<T>.sortWith(comparator: Comparator<T>) {75
val sorted = sortedCollectionElements<T>(this, comparator)76
var index = 0L77
for (element in sorted) { this[index] = element; index += 1L }78
}80
/**81
* Stably sorts this mutable list in place in natural ascending order.82
*/83
public fun <T : Compare<T>> MutableList<T>.sort() { sortWith<T>(naturalOrder<T>()) }