Revert unification of operations on Array<T> and Array<out T> (copyOf, copyOfRange) which return the same type as the receiver.

Leave sortedArray, reversedArray and sliceArray only for covariant projection of array as the receiver.
This commit is contained in:
Ilya Gorbunov
2015-09-17 18:12:21 +03:00
committed by Alexander Udalov
parent c12f44fa22
commit e95be9096e
9 changed files with 73 additions and 68 deletions
@@ -16,7 +16,6 @@ enum class Family {
ArraysOfObjects,
ArraysOfPrimitives,
InvariantArraysOfObjects,
ArraysOfObjectsSubtype,
Strings,
Ranges,
RangesOfPrimitives,
@@ -263,7 +262,6 @@ class GenericFunction(val signature: String, val keyword: String = "fun") : Comp
Sets -> "Set<$isAsteriskOrT>"
Sequences -> "Sequence<$isAsteriskOrT>"
InvariantArraysOfObjects -> "Array<T>"
ArraysOfObjectsSubtype -> "A"
ArraysOfObjects -> "Array<${isAsteriskOrT.replace("T", "out T")}>"
Strings -> "String"
Ranges -> "Range<$isAsteriskOrT>"
@@ -287,9 +285,6 @@ class GenericFunction(val signature: String, val keyword: String = "fun") : Comp
// TODO: Model for type parameter
val types = ArrayList(typeParams)
if (f == ArraysOfObjectsSubtype) {
types.add("A: Array<out T>")
}
if (f == Generic) {
// ensure type parameter T, if it's not added to typeParams before
if (!types.any { it == "T" || it.startsWith("T:")}) {
@@ -513,18 +513,17 @@ fun filtering(): List<GenericFunction> {
}
templates add f("sliceArray(indices: Collection<Int>)") {
only(ArraysOfObjectsSubtype, ArraysOfPrimitives)
only(ArraysOfObjects, ArraysOfPrimitives)
doc { "Returns an array containing elements of this array at specified [indices]." }
returns("SELF")
body(ArraysOfObjectsSubtype) {
body(ArraysOfObjects) {
"""
if (indices.isEmpty()) return arrayOfNulls(this, 0) as SELF
val result = arrayOfNulls(this, indices.size()) as Array<T>
var targetIndex = 0
for (sourceIndex in indices) {
result[targetIndex++] = this[sourceIndex]
}
return result as SELF
return result
"""
}
body(ArraysOfPrimitives) {
@@ -540,13 +539,13 @@ fun filtering(): List<GenericFunction> {
}
templates add f("sliceArray(indices: IntRange)") {
only(ArraysOfObjectsSubtype, ArraysOfPrimitives)
only(ArraysOfObjects, ArraysOfPrimitives)
doc { "Returns a list containing elements at indices in the specified [indices] range." }
returns("SELF")
body(ArraysOfObjectsSubtype) {
body(ArraysOfObjects) {
"""
if (indices.isEmpty()) return copyOf(0) as SELF
return copyOfRange(indices.start, indices.end + 1) as SELF
if (indices.isEmpty()) return copyOfRange(0, 0)
return copyOfRange(indices.start, indices.end + 1)
"""
}
body(ArraysOfPrimitives) {
@@ -53,16 +53,16 @@ fun ordering(): List<GenericFunction> {
templates add f("reversedArray()") {
doc { "Returns an array with elements of this array in reversed order." }
only(ArraysOfObjectsSubtype, ArraysOfPrimitives)
only(ArraysOfObjects, ArraysOfPrimitives)
returns("SELF")
body(ArraysOfObjectsSubtype) {
body(ArraysOfObjects) {
"""
if (isEmpty()) return this
val result = arrayOfNulls(this, size()) as Array<T>
val lastIndex = lastIndex
for (i in 0..lastIndex)
result[lastIndex - i] = this[i]
return result as A
return result
"""
}
body(ArraysOfPrimitives) {
@@ -114,7 +114,7 @@ fun ordering(): List<GenericFunction> {
}
templates add f("sortedArray()") {
only(ArraysOfObjectsSubtype, ArraysOfPrimitives)
only(ArraysOfObjects, ArraysOfPrimitives)
exclude(PrimitiveType.Boolean)
doc {
"Returns an array with all elements of this array sorted according to their natural sort order."
@@ -124,7 +124,7 @@ fun ordering(): List<GenericFunction> {
body() {
"""
if (isEmpty()) return this
return this.copyOf().apply { sort() } as SELF
return this.copyOf().apply { sort() }
"""
}
}
@@ -163,18 +163,18 @@ fun ordering(): List<GenericFunction> {
}
templates add f("sortedArrayDescending()") {
only(ArraysOfObjectsSubtype, ArraysOfPrimitives)
only(ArraysOfObjects, ArraysOfPrimitives)
exclude(PrimitiveType.Boolean)
doc {
"Returns an array with all elements of this array sorted descending according to their natural sort order."
}
typeParam("T : Comparable<T>")
returns("SELF")
body(ArraysOfObjectsSubtype) {
body(ArraysOfObjects) {
"""
if (isEmpty()) return this
// TODO: Use reverseOrder<T>()
return this.copyOf().apply { sortWith(comparator { a, b -> b.compareTo(a) }) } as SELF
return this.copyOf().apply { sortWith(comparator { a, b -> b.compareTo(a) }) }
"""
}
@@ -182,7 +182,7 @@ fun ordering(): List<GenericFunction> {
"""
if (isEmpty()) return this
// TODO: Use in-place reverse
return this.copyOf().apply { sort() }.reversedArray() as SELF
return this.copyOf().apply { sort() }.reversedArray()
"""
}
}
@@ -221,7 +221,7 @@ fun ordering(): List<GenericFunction> {
}
templates add f("sortedArrayWith(comparator: Comparator<in T>)") {
only(ArraysOfObjectsSubtype)
only(ArraysOfObjects)
doc {
"Returns an array with all elements of this array sorted according the specified [comparator]."
}
@@ -229,7 +229,7 @@ fun ordering(): List<GenericFunction> {
body() {
"""
if (isEmpty()) return this
return this.copyOf().apply { sortWith(comparator) } as SELF
return this.copyOf().apply { sortWith(comparator) }
"""
}
}
@@ -51,27 +51,23 @@ fun specialJVM(): List<GenericFunction> {
templates add f("copyOfRange(fromIndex: Int, toIndex: Int)") {
only(ArraysOfObjectsSubtype, ArraysOfPrimitives)
only(ArraysOfObjects, InvariantArraysOfObjects, ArraysOfPrimitives)
doc { "Returns new array which is a copy of range of original array." }
returns("SELF")
annotations(InvariantArraysOfObjects) { """platformName("mutableCopyOfRange")"""}
body {
"return Arrays.copyOfRange(this, fromIndex, toIndex)"
}
body(ArraysOfObjectsSubtype) {
"return Arrays.copyOfRange(this, fromIndex, toIndex) as A"
}
}
templates add f("copyOf()") {
only(ArraysOfObjectsSubtype, ArraysOfPrimitives)
only(ArraysOfObjects, InvariantArraysOfObjects, ArraysOfPrimitives)
doc { "Returns new array which is a copy of the original array." }
returns("SELF")
annotations(InvariantArraysOfObjects) { """platformName("mutableCopyOf")"""}
body {
"return Arrays.copyOf(this, size())"
}
body(ArraysOfObjectsSubtype) {
"return Arrays.copyOf(this, size()) as A"
}
}
// This overload can cause nulls if array size is expanding, hence different return overload