Set operations (distinct, union, intersect, subtract)

This commit is contained in:
Ilya Ryzhenkov
2014-06-09 16:49:53 +04:00
committed by Andrey Breslav
parent 132f2a5fa8
commit ca7c3d7999
5 changed files with 605 additions and 6 deletions
+440
View File
@@ -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 <T> Array<out T>.distinct(): Set<T> {
return this.toMutableSet()
}
/**
* Returns a set containing all distinct elements from the given collection.
*/
public fun BooleanArray.distinct(): Set<Boolean> {
return this.toMutableSet()
}
/**
* Returns a set containing all distinct elements from the given collection.
*/
public fun ByteArray.distinct(): Set<Byte> {
return this.toMutableSet()
}
/**
* Returns a set containing all distinct elements from the given collection.
*/
public fun CharArray.distinct(): Set<Char> {
return this.toMutableSet()
}
/**
* Returns a set containing all distinct elements from the given collection.
*/
public fun DoubleArray.distinct(): Set<Double> {
return this.toMutableSet()
}
/**
* Returns a set containing all distinct elements from the given collection.
*/
public fun FloatArray.distinct(): Set<Float> {
return this.toMutableSet()
}
/**
* Returns a set containing all distinct elements from the given collection.
*/
public fun IntArray.distinct(): Set<Int> {
return this.toMutableSet()
}
/**
* Returns a set containing all distinct elements from the given collection.
*/
public fun LongArray.distinct(): Set<Long> {
return this.toMutableSet()
}
/**
* Returns a set containing all distinct elements from the given collection.
*/
public fun ShortArray.distinct(): Set<Short> {
return this.toMutableSet()
}
/**
* Returns a set containing all distinct elements from the given collection.
*/
public fun <T> Iterable<T>.distinct(): Set<T> {
return this.toMutableSet()
}
/**
* Returns a set containing all distinct elements from both collections.
*/
public fun <T> Array<out T>.intersect(other: Iterable<T>): Set<T> {
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<Boolean>): Set<Boolean> {
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<Byte>): Set<Byte> {
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<Char>): Set<Char> {
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<Double>): Set<Double> {
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<Float>): Set<Float> {
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<Int>): Set<Int> {
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<Long>): Set<Long> {
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<Short>): Set<Short> {
val set = this.toMutableSet()
set.retainAll(other)
return set
}
/**
* Returns a set containing all distinct elements from both collections.
*/
public fun <T> Iterable<T>.intersect(other: Iterable<T>): Set<T> {
val set = this.toMutableSet()
set.retainAll(other)
return set
}
/**
* Returns a set containing all distinct elements from both collections.
*/
public fun <T> Array<out T>.subtract(other: Iterable<T>): Set<T> {
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<Boolean>): Set<Boolean> {
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<Byte>): Set<Byte> {
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<Char>): Set<Char> {
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<Double>): Set<Double> {
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<Float>): Set<Float> {
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<Int>): Set<Int> {
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<Long>): Set<Long> {
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<Short>): Set<Short> {
val set = this.toMutableSet()
set.removeAll(other)
return set
}
/**
* Returns a set containing all distinct elements from both collections.
*/
public fun <T> Iterable<T>.subtract(other: Iterable<T>): Set<T> {
val set = this.toMutableSet()
set.removeAll(other)
return set
}
/**
* Returns a mutable set containing all distinct elements from the given collection.
*/
public fun <T> Array<out T>.toMutableSet(): MutableSet<T> {
val set = LinkedHashSet<T>(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<Boolean> {
val set = LinkedHashSet<Boolean>(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<Byte> {
val set = LinkedHashSet<Byte>(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<Char> {
val set = LinkedHashSet<Char>(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<Double> {
val set = LinkedHashSet<Double>(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<Float> {
val set = LinkedHashSet<Float>(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<Int> {
val set = LinkedHashSet<Int>(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<Long> {
val set = LinkedHashSet<Long>(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<Short> {
val set = LinkedHashSet<Short>(size)
for (item in this) set.add(item)
return set
}
/**
* Returns a mutable set containing all distinct elements from the given collection.
*/
public fun <T> Iterable<T>.toMutableSet(): MutableSet<T> {
return when (this) {
is Collection<T> -> LinkedHashSet(this)
else -> toCollection(LinkedHashSet<T>())
}
}
/**
* Returns a set containing all distinct elements from both collections.
*/
public fun <T> Array<out T>.union(other: Iterable<T>): Set<T> {
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<Boolean>): Set<Boolean> {
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<Byte>): Set<Byte> {
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<Char>): Set<Char> {
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<Double>): Set<Double> {
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<Float>): Set<Float> {
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<Int>): Set<Int> {
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<Long>): Set<Long> {
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<Short>): Set<Short> {
val set = this.toMutableSet()
set.addAll(other)
return set
}
/**
* Returns a set containing all distinct elements from both collections.
*/
public fun <T> Iterable<T>.union(other: Iterable<T>): Set<T> {
val set = this.toMutableSet()
set.addAll(other)
return set
}
@@ -3,17 +3,64 @@ package kotlin
/**
* Adds all elements of the given *iterable* to this [[MutableCollection]]
*/
public fun <T> MutableCollection<in T>.addAll(iterable: Iterable<T>): Unit {
public fun <T> MutableCollection<in T>.addAll(iterable: Iterable<T>) {
when (iterable) {
is Collection -> addAll(iterable)
else -> for (e in iterable) add(e)
else -> for (item in iterable) add(item)
}
}
public fun <T> MutableCollection<in T>.addAll(stream: Stream<T>): Unit {
for (e in stream) add(e)
/**
* Adds all elements of the given *stream* to this [[MutableCollection]]
*/
public fun <T> MutableCollection<in T>.addAll(stream: Stream<T>) {
for (item in stream) add(item)
}
public fun <T> MutableCollection<in T>.addAll(array: Array<T>): Unit {
for (e in array) add(e)
/**
* Adds all elements of the given *array* to this [[MutableCollection]]
*/
public fun <T> MutableCollection<in T>.addAll(array: Array<T>) {
for (item in array) add(item)
}
/**
* Removes all elements of the given *iterable* from this [[MutableCollection]]
*/
public fun <T> MutableCollection<in T>.removeAll(iterable: Iterable<T>) {
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 <T> MutableCollection<in T>.removeAll(stream: Stream<T>) {
for (item in stream) remove(item)
}
/**
* Removes all elements of the given *array* from this [[MutableCollection]]
*/
public fun <T> MutableCollection<in T>.removeAll(array: Array<T>) {
for (item in array) remove(item)
}
/**
* Retains only elements of the given *iterable* in this [[MutableCollection]]
*/
public fun <T> MutableCollection<in T>.retainAll(iterable: Iterable<T>) {
when (iterable) {
is Collection -> retainAll(iterable)
else -> retainAll(iterable.toSet())
}
}
/**
* Retains only elements of the given *array* in this [[MutableCollection]]
*/
public fun <T> MutableCollection<in T>.retainAll(array: Array<T>) {
retainAll(array.toSet())
}
@@ -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<Int>(), listOf<Int>().distinct().toList())
}
test fun union() {
assertEquals(listOf(1, 3, 5), listOf(1, 3).union(listOf(5)).toList())
assertEquals(listOf(1), listOf<Int>().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<Int>(), listOf(1, 3, 5).subtract(listOf(1, 3, 5)).toList())
assertEquals(listOf<Int>(), listOf<Int>().subtract(listOf(1)).toList())
}
test fun intersect() {
assertEquals(listOf<Int>(), 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<Int>(), listOf<Int>().intersect(listOf(1)).toList())
}
}
@@ -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() }
@@ -0,0 +1,81 @@
package templates
import templates.Family.*
fun sets(): List<GenericFunction> {
val templates = arrayListOf<GenericFunction>()
templates add f("toMutableSet()") {
exclude(Strings, Streams)
doc { "Returns a mutable set containing all distinct elements from the given collection." }
returns("MutableSet<T>")
body {
"""
return when (this) {
is Collection<T> -> LinkedHashSet(this)
else -> toCollection(LinkedHashSet<T>())
}
"""
}
body(ArraysOfObjects, ArraysOfPrimitives) {
"""
val set = LinkedHashSet<T>(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<T>")
body {
"""
return this.toMutableSet()
"""
}
}
templates add f("union(other: Iterable<T>)") {
exclude(Strings, Streams)
doc { "Returns a set containing all distinct elements from both collections." }
returns("Set<T>")
body {
"""
val set = this.toMutableSet()
set.addAll(other)
return set
"""
}
}
templates add f("intersect(other: Iterable<T>)") {
exclude(Strings, Streams)
doc { "Returns a set containing all distinct elements from both collections." }
returns("Set<T>")
body {
"""
val set = this.toMutableSet()
set.retainAll(other)
return set
"""
}
}
templates add f("subtract(other: Iterable<T>)") {
exclude(Strings, Streams)
doc { "Returns a set containing all distinct elements from both collections." }
returns("Set<T>")
body {
"""
val set = this.toMutableSet()
set.removeAll(other)
return set
"""
}
}
return templates
}