Special case of slice for int ranges, sliceArray returning array.

#KT-8711
This commit is contained in:
Ilya Gorbunov
2015-08-28 20:41:55 +03:00
parent faa26cdb25
commit c4b18d8fb8
7 changed files with 423 additions and 42 deletions
@@ -460,7 +460,9 @@ fun filtering(): List<GenericFunction> {
returns("List<T>")
body {
"""
val list = ArrayList<T>(indices.collectionSizeOrDefault(10))
val size = indices.collectionSizeOrDefault(10)
if (size == 0) return listOf()
val list = ArrayList<T>(size)
for (index in indices) {
list.add(get(index))
}
@@ -472,7 +474,9 @@ fun filtering(): List<GenericFunction> {
returns(Strings) { "String" }
body(Strings) {
"""
val result = StringBuilder(indices.collectionSizeOrDefault(10))
val size = indices.collectionSizeOrDefault(10)
if (size == 0) return ""
val result = StringBuilder(size)
for (i in indices) {
result.append(get(i))
}
@@ -481,5 +485,77 @@ fun filtering(): List<GenericFunction> {
}
}
templates add f("slice(indices: IntRange)") {
only(Strings, Lists, ArraysOfPrimitives, ArraysOfObjects)
doc { "Returns a list containing elements at indices in the specified [indices] range." }
returns("List<T>")
body(Lists) {
"""
if (indices.isEmpty()) return listOf()
return this.subList(indices.start, indices.end + 1).toList()
"""
}
body(ArraysOfPrimitives, ArraysOfObjects) {
"""
if (indices.isEmpty()) return listOf()
return copyOfRange(indices.start, indices.end + 1).asList()
"""
}
doc(Strings) { "Returns a string containing characters at indices at the specified [indices]." }
returns(Strings) { "String" }
body(Strings) {
"""
if (indices.isEmpty()) return ""
return substring(indices)
"""
}
}
templates add f("sliceArray(indices: Collection<Int>)") {
only(ArraysOfObjectsSubtype, ArraysOfPrimitives)
doc { "Returns an array containing elements of this array at specified [indices]." }
returns("SELF")
body(ArraysOfObjectsSubtype) {
"""
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
"""
}
body(ArraysOfPrimitives) {
"""
val result = SELF(indices.size())
var targetIndex = 0
for (sourceIndex in indices) {
result[targetIndex++] = this[sourceIndex]
}
return result
"""
}
}
templates add f("sliceArray(indices: IntRange)") {
only(ArraysOfObjectsSubtype, ArraysOfPrimitives)
doc { "Returns a list containing elements at indices in the specified [indices] range." }
returns("SELF")
body(ArraysOfObjectsSubtype) {
"""
if (indices.isEmpty()) return copyOf(0) as SELF
return copyOfRange(indices.start, indices.end + 1) as SELF
"""
}
body(ArraysOfPrimitives) {
"""
if (indices.isEmpty()) return SELF(0)
return copyOfRange(indices.start, indices.end + 1)
"""
}
}
return templates
}
@@ -58,16 +58,10 @@ fun ordering(): List<GenericFunction> {
body(ArraysOfObjectsSubtype) {
"""
if (isEmpty()) return this
val result = this.copyOf() as Array<T>
var i1 = 0
var i2 = lastIndex
while (i1 < i2) {
val tmp = result[i1]
result[i1] = result[i2]
result[i2] = tmp
i1 += 1
i2 -= 1
}
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
"""
}