diff --git a/libraries/stdlib/src/generated/_Ordering.kt b/libraries/stdlib/src/generated/_Ordering.kt index 78e1d52c570..186290ac1dd 100644 --- a/libraries/stdlib/src/generated/_Ordering.kt +++ b/libraries/stdlib/src/generated/_Ordering.kt @@ -132,7 +132,7 @@ public fun Iterable.sortBy(comparator: Comparator): List { } /** - * Returns a list of all elements, sorted by results of specified *order* function. + * Returns a sorted list of all elements, ordered by results of specified *order* function. */ public inline fun > Array.sortBy(order: (T) -> R): List { val sortedList = toArrayList() @@ -142,7 +142,7 @@ public inline fun > Array.sortBy(order: (T) -> R): L } /** - * Returns a list of all elements, sorted by results of specified *order* function. + * Returns a sorted list of all elements, ordered by results of specified *order* function. */ public inline fun > Iterable.sortBy(order: (T) -> R): List { val sortedList = toArrayList() @@ -152,7 +152,7 @@ public inline fun > Iterable.sortBy(order: (T) -> R): Li } /** - * Returns a sorted list of all elements + * Returns a sorted list of all elements, in descending order */ public fun > Iterable.sortDescending(): List { val sortedList = toArrayList() @@ -162,7 +162,7 @@ public fun > Iterable.sortDescending(): List { } /** - * Returns a list of all elements, sorted by results of specified *order* function. + * Returns a sorted list of all elements, in descending order by results of specified *order* function. */ public inline fun > Array.sortDescendingBy(order: (T) -> R): List { val sortedList = toArrayList() @@ -172,7 +172,7 @@ public inline fun > Array.sortDescendingBy(order: (T } /** - * Returns a list of all elements, sorted by results of specified *order* function. + * Returns a sorted list of all elements, in descending order by results of specified *order* function. */ public inline fun > Iterable.sortDescendingBy(order: (T) -> R): List { val sortedList = toArrayList() @@ -181,3 +181,212 @@ public inline fun > Iterable.sortDescendingBy(order: (T) return sortedList } +/** + * Returns a sorted list of all elements + */ +public fun > Array.toSortedList(): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList) + return sortedList +} + +/** + * Returns a sorted list of all elements + */ +public fun BooleanArray.toSortedList(): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList) + return sortedList +} + +/** + * Returns a sorted list of all elements + */ +public fun ByteArray.toSortedList(): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList) + return sortedList +} + +/** + * Returns a sorted list of all elements + */ +public fun CharArray.toSortedList(): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList) + return sortedList +} + +/** + * Returns a sorted list of all elements + */ +public fun DoubleArray.toSortedList(): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList) + return sortedList +} + +/** + * Returns a sorted list of all elements + */ +public fun FloatArray.toSortedList(): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList) + return sortedList +} + +/** + * Returns a sorted list of all elements + */ +public fun IntArray.toSortedList(): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList) + return sortedList +} + +/** + * Returns a sorted list of all elements + */ +public fun LongArray.toSortedList(): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList) + return sortedList +} + +/** + * Returns a sorted list of all elements + */ +public fun ShortArray.toSortedList(): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList) + return sortedList +} + +/** + * Returns a sorted list of all elements + */ +public fun > Iterable.toSortedList(): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList) + return sortedList +} + +/** + * Returns a sorted list of all elements + */ +public fun > Stream.toSortedList(): List { + val sortedList = toArrayList() + java.util.Collections.sort(sortedList) + return sortedList +} + +/** + * Returns a sorted list of all elements, ordered by results of specified *order* function. + */ +public fun > Array.toSortedListBy(order: (T) -> V): List { + val sortedList = toArrayList() + val sortBy: Comparator = comparator {(x: T, y: T) -> order(x).compareTo(order(y)) } + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Returns a sorted list of all elements, ordered by results of specified *order* function. + */ +public fun > BooleanArray.toSortedListBy(order: (Boolean) -> V): List { + val sortedList = toArrayList() + val sortBy: Comparator = comparator {(x: Boolean, y: Boolean) -> order(x).compareTo(order(y)) } + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Returns a sorted list of all elements, ordered by results of specified *order* function. + */ +public fun > ByteArray.toSortedListBy(order: (Byte) -> V): List { + val sortedList = toArrayList() + val sortBy: Comparator = comparator {(x: Byte, y: Byte) -> order(x).compareTo(order(y)) } + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Returns a sorted list of all elements, ordered by results of specified *order* function. + */ +public fun > CharArray.toSortedListBy(order: (Char) -> V): List { + val sortedList = toArrayList() + val sortBy: Comparator = comparator {(x: Char, y: Char) -> order(x).compareTo(order(y)) } + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Returns a sorted list of all elements, ordered by results of specified *order* function. + */ +public fun > DoubleArray.toSortedListBy(order: (Double) -> V): List { + val sortedList = toArrayList() + val sortBy: Comparator = comparator {(x: Double, y: Double) -> order(x).compareTo(order(y)) } + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Returns a sorted list of all elements, ordered by results of specified *order* function. + */ +public fun > FloatArray.toSortedListBy(order: (Float) -> V): List { + val sortedList = toArrayList() + val sortBy: Comparator = comparator {(x: Float, y: Float) -> order(x).compareTo(order(y)) } + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Returns a sorted list of all elements, ordered by results of specified *order* function. + */ +public fun > IntArray.toSortedListBy(order: (Int) -> V): List { + val sortedList = toArrayList() + val sortBy: Comparator = comparator {(x: Int, y: Int) -> order(x).compareTo(order(y)) } + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Returns a sorted list of all elements, ordered by results of specified *order* function. + */ +public fun > LongArray.toSortedListBy(order: (Long) -> V): List { + val sortedList = toArrayList() + val sortBy: Comparator = comparator {(x: Long, y: Long) -> order(x).compareTo(order(y)) } + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Returns a sorted list of all elements, ordered by results of specified *order* function. + */ +public fun > ShortArray.toSortedListBy(order: (Short) -> V): List { + val sortedList = toArrayList() + val sortBy: Comparator = comparator {(x: Short, y: Short) -> order(x).compareTo(order(y)) } + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Returns a sorted list of all elements, ordered by results of specified *order* function. + */ +public fun > Iterable.toSortedListBy(order: (T) -> V): List { + val sortedList = toArrayList() + val sortBy: Comparator = comparator {(x: T, y: T) -> order(x).compareTo(order(y)) } + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Returns a sorted list of all elements, ordered by results of specified *order* function. + */ +public fun > Stream.toSortedListBy(order: (T) -> V): List { + val sortedList = toArrayList() + val sortBy: Comparator = comparator {(x: T, y: T) -> order(x).compareTo(order(y)) } + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + diff --git a/libraries/stdlib/src/generated/_Snapshots.kt b/libraries/stdlib/src/generated/_Snapshots.kt index e063bd3aa2d..ded2494a6d3 100644 --- a/libraries/stdlib/src/generated/_Snapshots.kt +++ b/libraries/stdlib/src/generated/_Snapshots.kt @@ -581,90 +581,6 @@ public fun String.toSet(): Set { return toCollection(LinkedHashSet()) } -/** - * Returns a sorted list of all elements - */ -public fun > Array.toSortedList(): List { - return toArrayList().sort() -} - -/** - * Returns a sorted list of all elements - */ -public fun BooleanArray.toSortedList(): List { - return toArrayList().sort() -} - -/** - * Returns a sorted list of all elements - */ -public fun ByteArray.toSortedList(): List { - return toArrayList().sort() -} - -/** - * Returns a sorted list of all elements - */ -public fun CharArray.toSortedList(): List { - return toArrayList().sort() -} - -/** - * Returns a sorted list of all elements - */ -public fun DoubleArray.toSortedList(): List { - return toArrayList().sort() -} - -/** - * Returns a sorted list of all elements - */ -public fun FloatArray.toSortedList(): List { - return toArrayList().sort() -} - -/** - * Returns a sorted list of all elements - */ -public fun IntArray.toSortedList(): List { - return toArrayList().sort() -} - -/** - * Returns a sorted list of all elements - */ -public fun LongArray.toSortedList(): List { - return toArrayList().sort() -} - -/** - * Returns a sorted list of all elements - */ -public fun ShortArray.toSortedList(): List { - return toArrayList().sort() -} - -/** - * Returns a sorted list of all elements - */ -public fun > Iterable.toSortedList(): List { - return sort() -} - -/** - * Returns a sorted list of all elements - */ -public fun > Stream.toSortedList(): List { - return toArrayList().sort() -} - -/** - * Returns a sorted list of all elements - */ -public fun String.toSortedList(): List { - return toArrayList().sort() -} - /** * Returns a SortedSet of all elements */ diff --git a/libraries/stdlib/src/kotlin/collections/Stream.kt b/libraries/stdlib/src/kotlin/collections/Stream.kt index 5ad48acb3f9..f1701e46c9a 100644 --- a/libraries/stdlib/src/kotlin/collections/Stream.kt +++ b/libraries/stdlib/src/kotlin/collections/Stream.kt @@ -1,12 +1,13 @@ package kotlin import kotlin.support.AbstractIterator +import java.util.* public trait Stream { public fun iterator(): Iterator } -public fun streamOf(vararg elements : T) : Stream = elements.stream() +public fun streamOf(vararg elements: T): Stream = elements.stream() public class FilteringStream(val stream: Stream, val sendWhen: Boolean = true, val predicate: (T) -> Boolean) : Stream { override fun iterator(): Iterator = object : AbstractIterator() { @@ -37,8 +38,8 @@ public class TransformingStream(val stream: Stream, val transformer: (T } } -class ZippingStream(val stream1: Stream, val stream2: Stream) : Stream> { - override fun iterator(): Iterator> = object : AbstractIterator>() { +class ZippingStream(val stream1: Stream, val stream2: Stream) : Stream> { + override fun iterator(): Iterator> = object : AbstractIterator>() { val iterator1 = stream1.iterator() val iterator2 = stream2.iterator() override fun computeNext() { diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt index 6d431a0380b..9c7291e47a9 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt @@ -45,15 +45,34 @@ fun ordering(): List { } exclude(Streams) - exclude(ArraysOfPrimitives) // TODO: resolve collision between inplace sort and this function + exclude(ArraysOfPrimitives) exclude(ArraysOfObjects) exclude(Strings) } + templates add f("toSortedList()") { + doc { + """ + Returns a sorted list of all elements + """ + } + returns("List") + typeParam("T : Comparable") + body { + """ + val sortedList = toArrayList() + java.util.Collections.sort(sortedList) + return sortedList + """ + } + + only(Streams, ArraysOfObjects, ArraysOfPrimitives, Iterables) + } + templates add f("sortDescending()") { doc { """ - Returns a sorted list of all elements + Returns a sorted list of all elements, in descending order """ } returns("List") @@ -68,7 +87,7 @@ fun ordering(): List { } exclude(Streams) - exclude(ArraysOfPrimitives) // TODO: resolve collision between inplace sort and this function + exclude(ArraysOfPrimitives) exclude(ArraysOfObjects) exclude(Strings) } @@ -78,7 +97,7 @@ fun ordering(): List { doc { """ - Returns a list of all elements, sorted by results of specified *order* function. + Returns a sorted list of all elements, ordered by results of specified *order* function. """ } returns("List") @@ -97,12 +116,33 @@ fun ordering(): List { exclude(Strings) } + templates add f("toSortedListBy(order: (T) -> V)") { + doc { + """ + Returns a sorted list of all elements, ordered by results of specified *order* function. + """ + } + returns("List") + typeParam("T") + typeParam("V : Comparable") + body { + """ + val sortedList = toArrayList() + val sortBy: Comparator = comparator {(x: T, y: T) -> order(x).compareTo(order(y)) } + java.util.Collections.sort(sortedList, sortBy) + return sortedList + """ + } + + only(Streams, ArraysOfObjects, ArraysOfPrimitives, Iterables) + } + templates add f("sortDescendingBy(order: (T) -> R)") { inline(true) doc { """ - Returns a list of all elements, sorted by results of specified *order* function. + Returns a sorted list of all elements, in descending order by results of specified *order* function. """ } returns("List") diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt index 6302bf29c38..a4745daddcd 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt @@ -92,13 +92,5 @@ fun snapshots(): List { body { "return toCollection(LinkedList())" } } - templates add f("toSortedList()") { - doc { "Returns a sorted list of all elements" } - typeParam("T : Comparable") - returns("List") - body { "return toArrayList().sort()" } - body(Iterables) { "return sort()" } - } - return templates } \ No newline at end of file