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
@@ -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
}