diff --git a/libraries/stdlib/src/generated/_Generators.kt b/libraries/stdlib/src/generated/_Generators.kt index caf7711255a..84cace31caa 100644 --- a/libraries/stdlib/src/generated/_Generators.kt +++ b/libraries/stdlib/src/generated/_Generators.kt @@ -420,9 +420,7 @@ public fun Iterable.plus(collection: Iterable) : List { * Returns a stream containing all elements of original stream and then all elements of the given *collection* */ public fun Stream.plus(collection: Iterable) : Stream { - val answer = toArrayList() - answer.addAll(collection) - return answer.stream() + return Multistream(streamOf(this, collection.stream())) } @@ -530,9 +528,7 @@ public fun Iterable.plus(element: T) : List { * Returns a stream containing all elements of original stream and then the given element */ public fun Stream.plus(element: T) : Stream { - val answer = toArrayList() - answer.add(element) - return answer.stream() + return Multistream(streamOf(this, streamOf(element))) } @@ -540,9 +536,7 @@ public fun Stream.plus(element: T) : Stream { * Returns a stream containing all elements of original stream and then all elements of the given *stream* */ public fun Stream.plus(stream: Stream) : Stream { - val answer = toArrayList() - answer.addAll(stream) - return answer.stream() + return Multistream(streamOf(this, stream)) } diff --git a/libraries/stdlib/src/generated/_Streams.kt b/libraries/stdlib/src/generated/_Streams.kt new file mode 100644 index 00000000000..dbb5189ce86 --- /dev/null +++ b/libraries/stdlib/src/generated/_Streams.kt @@ -0,0 +1,96 @@ +package kotlin + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// + +import java.util.* + +/** + * Returns a stream from the given collection + */ +public fun Array.stream() : Stream { + return object : Stream { override fun iterator() : Iterator { return this@stream.iterator() } } + +} + +/** + * Returns a stream from the given collection + */ +public fun BooleanArray.stream() : Stream { + return object : Stream { override fun iterator() : Iterator { return this@stream.iterator() } } + +} + +/** + * Returns a stream from the given collection + */ +public fun ByteArray.stream() : Stream { + return object : Stream { override fun iterator() : Iterator { return this@stream.iterator() } } + +} + +/** + * Returns a stream from the given collection + */ +public fun CharArray.stream() : Stream { + return object : Stream { override fun iterator() : Iterator { return this@stream.iterator() } } + +} + +/** + * Returns a stream from the given collection + */ +public fun DoubleArray.stream() : Stream { + return object : Stream { override fun iterator() : Iterator { return this@stream.iterator() } } + +} + +/** + * Returns a stream from the given collection + */ +public fun FloatArray.stream() : Stream { + return object : Stream { override fun iterator() : Iterator { return this@stream.iterator() } } + +} + +/** + * Returns a stream from the given collection + */ +public fun IntArray.stream() : Stream { + return object : Stream { override fun iterator() : Iterator { return this@stream.iterator() } } + +} + +/** + * Returns a stream from the given collection + */ +public fun LongArray.stream() : Stream { + return object : Stream { override fun iterator() : Iterator { return this@stream.iterator() } } + +} + +/** + * Returns a stream from the given collection + */ +public fun ShortArray.stream() : Stream { + return object : Stream { override fun iterator() : Iterator { return this@stream.iterator() } } + +} + +/** + * Returns a stream from the given collection + */ +public fun Iterable.stream() : Stream { + return object : Stream { override fun iterator() : Iterator { return this@stream.iterator() } } +} + +/** + * Returns a stream from the given collection + */ +public fun Stream.stream() : Stream { + return this + +} + diff --git a/libraries/stdlib/src/kotlin/support/AbstractIterator.kt b/libraries/stdlib/src/kotlin/collections/AbstractIterator.kt similarity index 100% rename from libraries/stdlib/src/kotlin/support/AbstractIterator.kt rename to libraries/stdlib/src/kotlin/collections/AbstractIterator.kt diff --git a/libraries/stdlib/src/kotlin/collections/Stream.kt b/libraries/stdlib/src/kotlin/collections/Stream.kt index 5c92ee5a07b..5ad48acb3f9 100644 --- a/libraries/stdlib/src/kotlin/collections/Stream.kt +++ b/libraries/stdlib/src/kotlin/collections/Stream.kt @@ -6,11 +6,7 @@ public trait Stream { public fun iterator(): Iterator } -public fun Iterable.stream(): Stream = object : Stream { - override fun iterator(): Iterator { - return this@stream.iterator() - } -} +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() { @@ -84,6 +80,35 @@ public class FlatteningStream(val stream: Stream, val transformer: (T) } } +public class Multistream(val streams: Stream>) : Stream { + override fun iterator(): Iterator = object : AbstractIterator() { + val iterator = streams.iterator() + var streamIterator: Iterator? = null + override fun computeNext() { + while (streamIterator == null) { + if (!iterator.hasNext()) { + done() + break; + } else { + val stream = iterator.next() + val nextStreamIterator = stream.iterator() + if (nextStreamIterator.hasNext()) + streamIterator = nextStreamIterator + } + } + + val currentStreamIterator = streamIterator + if (currentStreamIterator == null) { + done() + } else { + setNext(currentStreamIterator.next()) + if (!currentStreamIterator.hasNext()) + streamIterator = null + } + } + } +} + public class LimitedStream(val stream: Stream, val stopWhen: Boolean = true, val predicate: (T) -> Boolean) : Stream { override fun iterator(): Iterator = object : AbstractIterator() { val iterator = stream.iterator() diff --git a/libraries/stdlib/src/kotlin/dom/Dom.kt b/libraries/stdlib/src/kotlin/dom/Dom.kt index 3a840a601c5..94508b1f3e6 100644 --- a/libraries/stdlib/src/kotlin/dom/Dom.kt +++ b/libraries/stdlib/src/kotlin/dom/Dom.kt @@ -1,6 +1,5 @@ package kotlin.dom -import kotlin.* import kotlin.support.* import java.util.* import org.w3c.dom.* diff --git a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateCollections.kt b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateCollections.kt index 7422d52ca01..c76b1cbb71a 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateCollections.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateCollections.kt @@ -16,6 +16,7 @@ fun generateCollectionsAPI(outDir : File) { guards().writeTo(File(outDir, "_Guards.kt")) { build() } generators().writeTo(File(outDir, "_Generators.kt")) { build() } strings().writeTo(File(outDir, "_Strings.kt")) { build() } + streams().writeTo(File(outDir, "_Streams.kt")) { build() } specialJVM().writeTo(File(outDir, "_SpecialJVM.kt")) { build() } numeric().writeTo(File(outDir, "_Numeric.kt")) { diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt index d317e31034e..1bc50a79f5f 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt @@ -18,12 +18,9 @@ fun generators(): List { doc(Streams) { "Returns a stream containing all elements of original stream and then the given element" } returns(Streams) { "Stream" } - // TODO: Implement lazy behavior body(Streams) { """ - val answer = toArrayList() - answer.add(element) - return answer.stream() + return Multistream(streamOf(this, streamOf(element))) """ } } @@ -32,7 +29,6 @@ fun generators(): List { exclude(Streams) doc { "Returns a list containing all elements of original collection and then all elements of the given *collection*" } returns("List") - body { """ val answer = toArrayList() @@ -46,7 +42,6 @@ fun generators(): List { exclude(Streams) doc { "Returns a list containing all elements of original collection and then all elements of the given *collection*" } returns("List") - body { """ val answer = toArrayList() @@ -60,13 +55,9 @@ fun generators(): List { only(Streams) doc { "Returns a stream containing all elements of original stream and then all elements of the given *collection*" } returns("Stream") - - // TODO: Implement lazy behavior body { """ - val answer = toArrayList() - answer.addAll(collection) - return answer.stream() + return Multistream(streamOf(this, collection.stream())) """ } } @@ -76,11 +67,8 @@ fun generators(): List { doc { "Returns a stream containing all elements of original stream and then all elements of the given *stream*" } returns("Stream") body { - // TODO: Implement lazy behavior """ - val answer = toArrayList() - answer.addAll(stream) - return answer.stream() + return Multistream(streamOf(this, stream)) """ } } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Stream.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Stream.kt new file mode 100644 index 00000000000..a6dc7cc6e68 --- /dev/null +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Stream.kt @@ -0,0 +1,25 @@ +package templates + +import templates.Family.* + +fun streams(): List { + val templates = arrayListOf() + + templates add f("stream()") { + doc { "Returns a stream from the given collection" } + returns("Stream") + body { + """ + return object : Stream { override fun iterator() : Iterator { return this@stream.iterator() } } + """ + } + + body(Streams) { + """ + return this + """ + } + } + + return templates +} \ No newline at end of file