Rename operand parameter of plus and minus for collections

This commit is contained in:
Ilya Gorbunov
2015-11-20 22:01:08 +03:00
parent 7c726bcaf9
commit 0dbbb6c19c
8 changed files with 587 additions and 587 deletions
@@ -50,51 +50,51 @@ fun generators(): List<GenericFunction> {
}
}
templates add f("plus(collection: Iterable<T>)") {
templates add f("plus(elements: Iterable<T>)") {
operator(true)
only(Iterables, Collections, Sets, Sequences)
doc { "Returns a list containing all elements of the original collection and then all elements of the given [collection]." }
doc { f -> "Returns a list containing all elements of the original ${f.collection} and then all elements of the given [elements] collection." }
returns("List<T>")
returns("SELF", Sets, Sequences)
body {
"""
if (this is Collection) return this.plus(collection)
if (this is Collection) return this.plus(elements)
val result = ArrayList<T>()
result.addAll(this)
result.addAll(collection)
result.addAll(elements)
return result
"""
}
body(Collections) {
"""
if (collection is Collection) {
val result = ArrayList<T>(this.size + collection.size)
if (elements is Collection) {
val result = ArrayList<T>(this.size + elements.size)
result.addAll(this)
result.addAll(collection)
result.addAll(elements)
return result
} else {
val result = ArrayList<T>(this)
result.addAll(collection)
result.addAll(elements)
return result
}
"""
}
// TODO: use immutable set builder when available
doc(Sets) { "Returns a set containing all elements both of the original set and the given [collection]." }
doc(Sets) { "Returns a set containing all elements both of the original set and the given [elements] collection." }
body(Sets) {
"""
val result = LinkedHashSet<T>(mapCapacity(collection.collectionSizeOrNull()?.let { this.size + it } ?: this.size * 2))
val result = LinkedHashSet<T>(mapCapacity(elements.collectionSizeOrNull()?.let { this.size + it } ?: this.size * 2))
result.addAll(this)
result.addAll(collection)
result.addAll(elements)
return result
"""
}
doc(Sequences) {
"""
Returns a sequence containing all elements of original sequence and then all elements of the given [collection].
Returns a sequence containing all elements of original sequence and then all elements of the given [elements] collection.
Note that the source sequence and the collection being added are iterated only when an `iterator` is requested from
the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result.
@@ -102,47 +102,47 @@ fun generators(): List<GenericFunction> {
}
body(Sequences) {
"""
return sequenceOf(this, collection.asSequence()).flatten()
return sequenceOf(this, elements.asSequence()).flatten()
"""
}
}
templates add f("plus(array: Array<out T>)") {
templates add f("plus(elements: Array<out T>)") {
operator(true)
only(Iterables, Collections, Sets, Sequences)
doc { "Returns a list containing all elements of the original collection and then all elements of the given [array]." }
doc { "Returns a list containing all elements of the original collection and then all elements of the given [elements] array." }
returns("List<T>")
returns("SELF", Sets, Sequences)
body {
"""
if (this is Collection) return this.plus(array)
if (this is Collection) return this.plus(elements)
val result = ArrayList<T>()
result.addAll(this)
result.addAll(array)
result.addAll(elements)
return result
"""
}
body(Collections) {
"""
val result = ArrayList<T>(this.size + array.size)
val result = ArrayList<T>(this.size + elements.size)
result.addAll(this)
result.addAll(array)
result.addAll(elements)
return result
"""
}
doc(Sets) { "Returns a set containing all elements both of the original set and the given [array]." }
doc(Sets) { "Returns a set containing all elements both of the original set and the given [elements] array." }
body(Sets) {
"""
val result = LinkedHashSet<T>(mapCapacity(this.size + array.size))
val result = LinkedHashSet<T>(mapCapacity(this.size + elements.size))
result.addAll(this)
result.addAll(array)
result.addAll(elements)
return result
"""
}
doc(Sequences) {
"""
Returns a sequence containing all elements of original sequence and then all elements of the given [array].
Returns a sequence containing all elements of original sequence and then all elements of the given [elements] array.
Note that the source sequence and the array being added are iterated only when an `iterator` is requested from
the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result.
@@ -150,24 +150,24 @@ fun generators(): List<GenericFunction> {
}
body(Sequences) {
"""
return this.plus(array.asList())
return this.plus(elements.asList())
"""
}
}
templates add f("plus(sequence: Sequence<T>)") {
templates add f("plus(elements: Sequence<T>)") {
operator(true)
only(Iterables, Sets, Sequences)
doc { "Returns a list containing all elements of the original collection and then all elements of the given [sequence]." }
doc { "Returns a list containing all elements of the original collection and then all elements of the given [elements] sequence." }
returns("List<T>")
returns("SELF", Sets, Sequences)
body {
"""
val result = ArrayList<T>()
result.addAll(this)
result.addAll(sequence)
result.addAll(elements)
return result
"""
}
@@ -175,25 +175,25 @@ fun generators(): List<GenericFunction> {
"""
val result = ArrayList<T>(this.size + 10)
result.addAll(this)
result.addAll(sequence)
result.addAll(elements)
return result
"""
}
// TODO: use immutable set builder when available
doc(Sets) { "Returns a set containing all elements both of the original set and the given [sequence]." }
doc(Sets) { "Returns a set containing all elements both of the original set and the given [elements] sequence." }
body(Sets) {
"""
val result = LinkedHashSet<T>(mapCapacity(this.size * 2))
result.addAll(this)
result.addAll(sequence)
result.addAll(elements)
return result
"""
}
doc(Sequences) {
"""
Returns a sequence containing all elements of original sequence and then all elements of the given [sequence].
Returns a sequence containing all elements of original sequence and then all elements of the given [elements] sequence.
Note that the source sequence and the sequence being added are iterated only when an `iterator` is requested from
the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result.
@@ -201,7 +201,7 @@ fun generators(): List<GenericFunction> {
}
body(Sequences) {
"""
return sequenceOf(this, sequence).flatten()
return sequenceOf(this, elements).flatten()
"""
}
}
@@ -245,16 +245,16 @@ fun generators(): List<GenericFunction> {
}
templates add f("minus(collection: Iterable<T>)") {
templates add f("minus(elements: Iterable<T>)") {
operator(true)
only(Iterables, Sets, Sequences)
doc { "Returns a list containing all elements of the original collection except the elements contained in the given [collection]." }
doc { "Returns a list containing all elements of the original collection except the elements contained in the given [elements] collection." }
returns("List<T>")
returns("SELF", Sets, Sequences)
body {
"""
val other = collection.convertToSetForSetOperationWith(this)
val other = elements.convertToSetForSetOperationWith(this)
if (other.isEmpty())
return this.toList()
@@ -262,10 +262,10 @@ fun generators(): List<GenericFunction> {
"""
}
doc(Sets) { "Returns a set containing all elements of the original set except the elements contained in the given [collection]." }
doc(Sets) { "Returns a set containing all elements of the original set except the elements contained in the given [elements] collection." }
body(Sets) {
"""
val other = collection.convertToSetForSetOperationWith(this)
val other = elements.convertToSetForSetOperationWith(this)
if (other.isEmpty())
return this.toSet()
if (other is Set)
@@ -279,7 +279,7 @@ fun generators(): List<GenericFunction> {
doc(Sequences) {
"""
Returns a sequence containing all elements of original sequence except the elements contained in the given [collection].
Returns a sequence containing all elements of original sequence except the elements contained in the given [elements] collection.
Note that the source sequence and the collection being subtracted are iterated only when an `iterator` is requested from
the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result.
@@ -289,7 +289,7 @@ fun generators(): List<GenericFunction> {
"""
return object: Sequence<T> {
override fun iterator(): Iterator<T> {
val other = collection.convertToSetForSetOperation()
val other = elements.convertToSetForSetOperation()
if (other.isEmpty())
return this@minus.iterator()
else
@@ -300,32 +300,32 @@ fun generators(): List<GenericFunction> {
}
}
templates add f("minus(array: Array<out T>)") {
templates add f("minus(elements: Array<out T>)") {
operator(true)
only(Iterables, Sets, Sequences)
doc { "Returns a list containing all elements of the original collection except the elements contained in the given [array]." }
doc { "Returns a list containing all elements of the original collection except the elements contained in the given [elements] array." }
returns("List<T>")
returns("SELF", Sets, Sequences)
body {
"""
if (array.isEmpty()) return this.toList()
val other = array.toHashSet()
if (elements.isEmpty()) return this.toList()
val other = elements.toHashSet()
return this.filterNot { it in other }
"""
}
doc(Sets) { "Returns a set containing all elements of the original set except the elements contained in the given [array]." }
doc(Sets) { "Returns a set containing all elements of the original set except the elements contained in the given [elements] array." }
body(Sets) {
"""
val result = LinkedHashSet<T>(this)
result.removeAll(array)
result.removeAll(elements)
return result
"""
}
doc(Sequences) {
"""
Returns a sequence containing all elements of original sequence except the elements contained in the given [array].
Returns a sequence containing all elements of original sequence except the elements contained in the given [elements] array.
Note that the source sequence and the array being subtracted are iterated only when an `iterator` is requested from
the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result.
@@ -333,10 +333,10 @@ fun generators(): List<GenericFunction> {
}
body(Sequences) {
"""
if (array.isEmpty()) return this
if (elements.isEmpty()) return this
return object: Sequence<T> {
override fun iterator(): Iterator<T> {
val other = array.toHashSet()
val other = elements.toHashSet()
return this@minus.filterNot { it in other }.iterator()
}
}
@@ -344,34 +344,34 @@ fun generators(): List<GenericFunction> {
}
}
templates add f("minus(sequence: Sequence<T>)") {
templates add f("minus(elements: Sequence<T>)") {
operator(true)
only(Iterables, Sets)
doc { "Returns a list containing all elements of the original collection except the elements contained in the given [sequence]." }
doc { "Returns a list containing all elements of the original collection except the elements contained in the given [elements] sequence." }
returns("List<T>")
returns("SELF", Sets, Sequences)
body {
"""
val other = sequence.toHashSet()
val other = elements.toHashSet()
if (other.isEmpty())
return this.toList()
return this.filterNot { it in other }
"""
}
doc(Sets) { "Returns a set containing all elements of the original set except the elements contained in the given [sequence]." }
doc(Sets) { "Returns a set containing all elements of the original set except the elements contained in the given [elements] sequence." }
body(Sets) {
"""
val result = LinkedHashSet<T>(this)
result.removeAll(sequence)
result.removeAll(elements)
return result
"""
}
doc(Sequences) {
"""
Returns a sequence containing all elements of original sequence except the elements contained in the given [sequence].
Returns a sequence containing all elements of original sequence except the elements contained in the given [elements] sequence.
Note that the source sequence and the sequence being subtracted are iterated only when an `iterator` is requested from
the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result.
@@ -381,7 +381,7 @@ fun generators(): List<GenericFunction> {
"""
return object: Sequence<T> {
override fun iterator(): Iterator<T> {
val other = sequence.toHashSet()
val other = elements.toHashSet()
if (other.isEmpty())
return this@minus.iterator()
else
@@ -104,29 +104,29 @@ fun specialJS(): List<GenericFunction> {
}
}
templates add f("plus(collection: Collection<T>)") {
templates add f("plus(elements: Collection<T>)") {
only(ArraysOfObjects, ArraysOfPrimitives)
returns("SELF")
returns(ArraysOfObjects) { "Array<T>" }
doc { "Returns an array containing all elements of the original array and then all elements of the given [collection]." }
doc { "Returns an array containing all elements of the original array and then all elements of the given [elements] collection." }
body {
"""
return arrayPlusCollection(this, collection)
return arrayPlusCollection(this, elements)
"""
}
}
// This overload can cause nulls if array size is expanding, hence different return overload
templates add f("plus(array: SELF)") {
templates add f("plus(elements: SELF)") {
only(ArraysOfObjects, ArraysOfPrimitives)
doc { "Returns an array containing all elements of the original array and then all elements of the given [array]." }
doc { "Returns an array containing all elements of the original array and then all elements of the given [elements] array." }
inline(true)
annotations("""@Suppress("NOTHING_TO_INLINE")""")
returns("SELF")
returns(ArraysOfObjects) { "Array<T>" }
body {
"""
return this.asDynamic().concat(array)
return this.asDynamic().concat(elements)
"""
}
}
@@ -21,35 +21,35 @@ fun specialJVM(): List<GenericFunction> {
}
}
templates add f("plus(collection: Collection<T>)") {
templates add f("plus(elements: Collection<T>)") {
operator(true)
only(InvariantArraysOfObjects, ArraysOfPrimitives)
returns("SELF")
doc { "Returns an array containing all elements of the original array and then all elements of the given [collection]." }
doc { "Returns an array containing all elements of the original array and then all elements of the given [elements] collection." }
body {
"""
var index = size
val result = Arrays.copyOf(this, index + collection.size)
for (element in collection) result[index++] = element
val result = Arrays.copyOf(this, index + elements.size)
for (element in elements) result[index++] = element
return result
"""
}
}
templates add f("plus(array: SELF)") {
templates add f("plus(elements: SELF)") {
operator(true)
only(InvariantArraysOfObjects, ArraysOfPrimitives)
customSignature(InvariantArraysOfObjects) { "plus(array: Array<out T>)" }
doc { "Returns an array containing all elements of the original array and then all elements of the given [array]." }
customSignature(InvariantArraysOfObjects) { "plus(elements: Array<out T>)" }
doc { "Returns an array containing all elements of the original array and then all elements of the given [elements] array." }
returns("SELF")
body {
"""
val thisSize = size
val arraySize = array.size
val arraySize = elements.size
val result = Arrays.copyOf(this, thisSize + arraySize)
System.arraycopy(array, 0, result, thisSize, arraySize)
System.arraycopy(elements, 0, result, thisSize, arraySize)
return result
"""
}