diff --git a/libraries/stdlib/src/generated/_Sets.kt b/libraries/stdlib/src/generated/_Sets.kt new file mode 100644 index 00000000000..961e19e8a9a --- /dev/null +++ b/libraries/stdlib/src/generated/_Sets.kt @@ -0,0 +1,440 @@ +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 set containing all distinct elements from the given collection. + */ +public fun Array.distinct(): Set { + return this.toMutableSet() +} + +/** + * Returns a set containing all distinct elements from the given collection. + */ +public fun BooleanArray.distinct(): Set { + return this.toMutableSet() +} + +/** + * Returns a set containing all distinct elements from the given collection. + */ +public fun ByteArray.distinct(): Set { + return this.toMutableSet() +} + +/** + * Returns a set containing all distinct elements from the given collection. + */ +public fun CharArray.distinct(): Set { + return this.toMutableSet() +} + +/** + * Returns a set containing all distinct elements from the given collection. + */ +public fun DoubleArray.distinct(): Set { + return this.toMutableSet() +} + +/** + * Returns a set containing all distinct elements from the given collection. + */ +public fun FloatArray.distinct(): Set { + return this.toMutableSet() +} + +/** + * Returns a set containing all distinct elements from the given collection. + */ +public fun IntArray.distinct(): Set { + return this.toMutableSet() +} + +/** + * Returns a set containing all distinct elements from the given collection. + */ +public fun LongArray.distinct(): Set { + return this.toMutableSet() +} + +/** + * Returns a set containing all distinct elements from the given collection. + */ +public fun ShortArray.distinct(): Set { + return this.toMutableSet() +} + +/** + * Returns a set containing all distinct elements from the given collection. + */ +public fun Iterable.distinct(): Set { + return this.toMutableSet() +} + +/** + * Returns a set containing all distinct elements from both collections. + */ +public fun Array.intersect(other: Iterable): Set { + val set = this.toMutableSet() + set.retainAll(other) + return set +} + +/** + * Returns a set containing all distinct elements from both collections. + */ +public fun BooleanArray.intersect(other: Iterable): Set { + val set = this.toMutableSet() + set.retainAll(other) + return set +} + +/** + * Returns a set containing all distinct elements from both collections. + */ +public fun ByteArray.intersect(other: Iterable): Set { + val set = this.toMutableSet() + set.retainAll(other) + return set +} + +/** + * Returns a set containing all distinct elements from both collections. + */ +public fun CharArray.intersect(other: Iterable): Set { + val set = this.toMutableSet() + set.retainAll(other) + return set +} + +/** + * Returns a set containing all distinct elements from both collections. + */ +public fun DoubleArray.intersect(other: Iterable): Set { + val set = this.toMutableSet() + set.retainAll(other) + return set +} + +/** + * Returns a set containing all distinct elements from both collections. + */ +public fun FloatArray.intersect(other: Iterable): Set { + val set = this.toMutableSet() + set.retainAll(other) + return set +} + +/** + * Returns a set containing all distinct elements from both collections. + */ +public fun IntArray.intersect(other: Iterable): Set { + val set = this.toMutableSet() + set.retainAll(other) + return set +} + +/** + * Returns a set containing all distinct elements from both collections. + */ +public fun LongArray.intersect(other: Iterable): Set { + val set = this.toMutableSet() + set.retainAll(other) + return set +} + +/** + * Returns a set containing all distinct elements from both collections. + */ +public fun ShortArray.intersect(other: Iterable): Set { + val set = this.toMutableSet() + set.retainAll(other) + return set +} + +/** + * Returns a set containing all distinct elements from both collections. + */ +public fun Iterable.intersect(other: Iterable): Set { + val set = this.toMutableSet() + set.retainAll(other) + return set +} + +/** + * Returns a set containing all distinct elements from both collections. + */ +public fun Array.subtract(other: Iterable): Set { + val set = this.toMutableSet() + set.removeAll(other) + return set +} + +/** + * Returns a set containing all distinct elements from both collections. + */ +public fun BooleanArray.subtract(other: Iterable): Set { + val set = this.toMutableSet() + set.removeAll(other) + return set +} + +/** + * Returns a set containing all distinct elements from both collections. + */ +public fun ByteArray.subtract(other: Iterable): Set { + val set = this.toMutableSet() + set.removeAll(other) + return set +} + +/** + * Returns a set containing all distinct elements from both collections. + */ +public fun CharArray.subtract(other: Iterable): Set { + val set = this.toMutableSet() + set.removeAll(other) + return set +} + +/** + * Returns a set containing all distinct elements from both collections. + */ +public fun DoubleArray.subtract(other: Iterable): Set { + val set = this.toMutableSet() + set.removeAll(other) + return set +} + +/** + * Returns a set containing all distinct elements from both collections. + */ +public fun FloatArray.subtract(other: Iterable): Set { + val set = this.toMutableSet() + set.removeAll(other) + return set +} + +/** + * Returns a set containing all distinct elements from both collections. + */ +public fun IntArray.subtract(other: Iterable): Set { + val set = this.toMutableSet() + set.removeAll(other) + return set +} + +/** + * Returns a set containing all distinct elements from both collections. + */ +public fun LongArray.subtract(other: Iterable): Set { + val set = this.toMutableSet() + set.removeAll(other) + return set +} + +/** + * Returns a set containing all distinct elements from both collections. + */ +public fun ShortArray.subtract(other: Iterable): Set { + val set = this.toMutableSet() + set.removeAll(other) + return set +} + +/** + * Returns a set containing all distinct elements from both collections. + */ +public fun Iterable.subtract(other: Iterable): Set { + val set = this.toMutableSet() + set.removeAll(other) + return set +} + +/** + * Returns a mutable set containing all distinct elements from the given collection. + */ +public fun Array.toMutableSet(): MutableSet { + val set = LinkedHashSet(size) + for (item in this) set.add(item) + return set +} + +/** + * Returns a mutable set containing all distinct elements from the given collection. + */ +public fun BooleanArray.toMutableSet(): MutableSet { + val set = LinkedHashSet(size) + for (item in this) set.add(item) + return set +} + +/** + * Returns a mutable set containing all distinct elements from the given collection. + */ +public fun ByteArray.toMutableSet(): MutableSet { + val set = LinkedHashSet(size) + for (item in this) set.add(item) + return set +} + +/** + * Returns a mutable set containing all distinct elements from the given collection. + */ +public fun CharArray.toMutableSet(): MutableSet { + val set = LinkedHashSet(size) + for (item in this) set.add(item) + return set +} + +/** + * Returns a mutable set containing all distinct elements from the given collection. + */ +public fun DoubleArray.toMutableSet(): MutableSet { + val set = LinkedHashSet(size) + for (item in this) set.add(item) + return set +} + +/** + * Returns a mutable set containing all distinct elements from the given collection. + */ +public fun FloatArray.toMutableSet(): MutableSet { + val set = LinkedHashSet(size) + for (item in this) set.add(item) + return set +} + +/** + * Returns a mutable set containing all distinct elements from the given collection. + */ +public fun IntArray.toMutableSet(): MutableSet { + val set = LinkedHashSet(size) + for (item in this) set.add(item) + return set +} + +/** + * Returns a mutable set containing all distinct elements from the given collection. + */ +public fun LongArray.toMutableSet(): MutableSet { + val set = LinkedHashSet(size) + for (item in this) set.add(item) + return set +} + +/** + * Returns a mutable set containing all distinct elements from the given collection. + */ +public fun ShortArray.toMutableSet(): MutableSet { + val set = LinkedHashSet(size) + for (item in this) set.add(item) + return set +} + +/** + * Returns a mutable set containing all distinct elements from the given collection. + */ +public fun Iterable.toMutableSet(): MutableSet { + return when (this) { + is Collection -> LinkedHashSet(this) + else -> toCollection(LinkedHashSet()) + } +} + +/** + * Returns a set containing all distinct elements from both collections. + */ +public fun Array.union(other: Iterable): Set { + val set = this.toMutableSet() + set.addAll(other) + return set +} + +/** + * Returns a set containing all distinct elements from both collections. + */ +public fun BooleanArray.union(other: Iterable): Set { + val set = this.toMutableSet() + set.addAll(other) + return set +} + +/** + * Returns a set containing all distinct elements from both collections. + */ +public fun ByteArray.union(other: Iterable): Set { + val set = this.toMutableSet() + set.addAll(other) + return set +} + +/** + * Returns a set containing all distinct elements from both collections. + */ +public fun CharArray.union(other: Iterable): Set { + val set = this.toMutableSet() + set.addAll(other) + return set +} + +/** + * Returns a set containing all distinct elements from both collections. + */ +public fun DoubleArray.union(other: Iterable): Set { + val set = this.toMutableSet() + set.addAll(other) + return set +} + +/** + * Returns a set containing all distinct elements from both collections. + */ +public fun FloatArray.union(other: Iterable): Set { + val set = this.toMutableSet() + set.addAll(other) + return set +} + +/** + * Returns a set containing all distinct elements from both collections. + */ +public fun IntArray.union(other: Iterable): Set { + val set = this.toMutableSet() + set.addAll(other) + return set +} + +/** + * Returns a set containing all distinct elements from both collections. + */ +public fun LongArray.union(other: Iterable): Set { + val set = this.toMutableSet() + set.addAll(other) + return set +} + +/** + * Returns a set containing all distinct elements from both collections. + */ +public fun ShortArray.union(other: Iterable): Set { + val set = this.toMutableSet() + set.addAll(other) + return set +} + +/** + * Returns a set containing all distinct elements from both collections. + */ +public fun Iterable.union(other: Iterable): Set { + val set = this.toMutableSet() + set.addAll(other) + return set +} + diff --git a/libraries/stdlib/src/kotlin/collections/MutableCollections.kt b/libraries/stdlib/src/kotlin/collections/MutableCollections.kt index ba221cfd04c..0aa9191f8d7 100644 --- a/libraries/stdlib/src/kotlin/collections/MutableCollections.kt +++ b/libraries/stdlib/src/kotlin/collections/MutableCollections.kt @@ -3,17 +3,64 @@ package kotlin /** * Adds all elements of the given *iterable* to this [[MutableCollection]] */ -public fun MutableCollection.addAll(iterable: Iterable): Unit { +public fun MutableCollection.addAll(iterable: Iterable) { when (iterable) { is Collection -> addAll(iterable) - else -> for (e in iterable) add(e) + else -> for (item in iterable) add(item) } } -public fun MutableCollection.addAll(stream: Stream): Unit { - for (e in stream) add(e) +/** + * Adds all elements of the given *stream* to this [[MutableCollection]] + */ +public fun MutableCollection.addAll(stream: Stream) { + for (item in stream) add(item) } -public fun MutableCollection.addAll(array: Array): Unit { - for (e in array) add(e) +/** + * Adds all elements of the given *array* to this [[MutableCollection]] + */ +public fun MutableCollection.addAll(array: Array) { + for (item in array) add(item) +} + +/** + * Removes all elements of the given *iterable* from this [[MutableCollection]] + */ +public fun MutableCollection.removeAll(iterable: Iterable) { + when (iterable) { + is Collection -> removeAll(iterable) + else -> for (item in iterable) remove(item) + } +} + +/** + * Removes all elements of the given *stream* from this [[MutableCollection]] + */ +public fun MutableCollection.removeAll(stream: Stream) { + for (item in stream) remove(item) +} + +/** + * Removes all elements of the given *array* from this [[MutableCollection]] + */ +public fun MutableCollection.removeAll(array: Array) { + for (item in array) remove(item) +} + +/** + * Retains only elements of the given *iterable* in this [[MutableCollection]] + */ +public fun MutableCollection.retainAll(iterable: Iterable) { + when (iterable) { + is Collection -> retainAll(iterable) + else -> retainAll(iterable.toSet()) + } +} + +/** + * Retains only elements of the given *array* in this [[MutableCollection]] + */ +public fun MutableCollection.retainAll(array: Array) { + retainAll(array.toSet()) } diff --git a/libraries/stdlib/test/collections/SetOperationsTest.kt b/libraries/stdlib/test/collections/SetOperationsTest.kt new file mode 100644 index 00000000000..6528a3bed34 --- /dev/null +++ b/libraries/stdlib/test/collections/SetOperationsTest.kt @@ -0,0 +1,30 @@ +package test.collections + +import kotlin.test.* +import org.junit.Test as test + +class SetOperationsTest { + test fun distinct() { + assertEquals(listOf(1, 3, 5), listOf(1, 3, 3, 1, 5, 1, 3).distinct().toList()) + assertEquals(listOf(), listOf().distinct().toList()) + } + + test fun union() { + assertEquals(listOf(1, 3, 5), listOf(1, 3).union(listOf(5)).toList()) + assertEquals(listOf(1), listOf().union(listOf(1)).toList()) + } + + test fun subtract() { + assertEquals(listOf(1, 3), listOf(1, 3).subtract(listOf(5)).toList()) + assertEquals(listOf(1, 3), listOf(1, 3, 5).subtract(listOf(5)).toList()) + assertEquals(listOf(), listOf(1, 3, 5).subtract(listOf(1, 3, 5)).toList()) + assertEquals(listOf(), listOf().subtract(listOf(1)).toList()) + } + + test fun intersect() { + assertEquals(listOf(), listOf(1, 3).intersect(listOf(5)).toList()) + assertEquals(listOf(5), listOf(1, 3, 5).intersect(listOf(5)).toList()) + assertEquals(listOf(1, 3, 5), listOf(1, 3, 5).intersect(listOf(1, 3, 5)).toList()) + assertEquals(listOf(), listOf().intersect(listOf(1)).toList()) + } +} \ No newline at end of file diff --git a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateCollections.kt b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateCollections.kt index 2e43193bbd4..bc4deb906ad 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateCollections.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateCollections.kt @@ -12,6 +12,7 @@ fun generateCollectionsAPI(outDir: File) { arrays().writeTo(File(outDir, "_Arrays.kt")) { build() } snapshots().writeTo(File(outDir, "_Snapshots.kt")) { build() } mapping().writeTo(File(outDir, "_Mapping.kt")) { build() } + sets().writeTo(File(outDir, "_Sets.kt")) { build() } aggregates().writeTo(File(outDir, "_Aggregates.kt")) { build() } guards().writeTo(File(outDir, "_Guards.kt")) { build() } generators().writeTo(File(outDir, "_Generators.kt")) { build() } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Sets.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Sets.kt new file mode 100644 index 00000000000..6009f4535ec --- /dev/null +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Sets.kt @@ -0,0 +1,81 @@ +package templates + +import templates.Family.* + +fun sets(): List { + val templates = arrayListOf() + + templates add f("toMutableSet()") { + exclude(Strings, Streams) + doc { "Returns a mutable set containing all distinct elements from the given collection." } + returns("MutableSet") + body { + """ + return when (this) { + is Collection -> LinkedHashSet(this) + else -> toCollection(LinkedHashSet()) + } + """ + } + body(ArraysOfObjects, ArraysOfPrimitives) { + """ + val set = LinkedHashSet(size) + for (item in this) set.add(item) + return set + """ + } + } + + templates add f("distinct()") { + exclude(Strings, Streams) + doc { "Returns a set containing all distinct elements from the given collection." } + + returns("Set") + body { + """ + return this.toMutableSet() + """ + } + } + + templates add f("union(other: Iterable)") { + exclude(Strings, Streams) + doc { "Returns a set containing all distinct elements from both collections." } + returns("Set") + body { + """ + val set = this.toMutableSet() + set.addAll(other) + return set + """ + } + } + + templates add f("intersect(other: Iterable)") { + exclude(Strings, Streams) + doc { "Returns a set containing all distinct elements from both collections." } + returns("Set") + body { + """ + val set = this.toMutableSet() + set.retainAll(other) + return set + """ + } + } + + templates add f("subtract(other: Iterable)") { + exclude(Strings, Streams) + doc { "Returns a set containing all distinct elements from both collections." } + returns("Set") + body { + """ + val set = this.toMutableSet() + set.removeAll(other) + return set + """ + } + } + + return templates +} \ No newline at end of file