Special case of slice for int ranges, sliceArray returning array.
#KT-8711
This commit is contained in:
@@ -69,6 +69,11 @@ public fun lazy<T>(mode: LazyThreadSafetyMode, initializer: () -> T): Lazy<T> =
|
||||
*/
|
||||
public fun lazy<T>(lock: Any?, initializer: () -> T): Lazy<T> = UnsafeLazyImpl(initializer)
|
||||
|
||||
|
||||
private fun <T> arrayOfNulls(reference: Array<out T>, size: Int): Array<T> {
|
||||
return arrayOfNulls<Any>(size) as Array<T>
|
||||
}
|
||||
|
||||
private fun arrayCopyResize(source: dynamic, newSize: Int, defaultValue: Any?): dynamic {
|
||||
val result = source.slice(0, newSize)
|
||||
var index: Int = source.length
|
||||
|
||||
@@ -995,11 +995,101 @@ public inline fun <C : Appendable> String.filterTo(destination: C, predicate: (C
|
||||
return destination
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
public fun <T> Array<out T>.slice(indices: IntRange): List<T> {
|
||||
if (indices.isEmpty()) return listOf()
|
||||
return copyOfRange(indices.start, indices.end + 1).asList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
public fun BooleanArray.slice(indices: IntRange): List<Boolean> {
|
||||
if (indices.isEmpty()) return listOf()
|
||||
return copyOfRange(indices.start, indices.end + 1).asList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
public fun ByteArray.slice(indices: IntRange): List<Byte> {
|
||||
if (indices.isEmpty()) return listOf()
|
||||
return copyOfRange(indices.start, indices.end + 1).asList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
public fun CharArray.slice(indices: IntRange): List<Char> {
|
||||
if (indices.isEmpty()) return listOf()
|
||||
return copyOfRange(indices.start, indices.end + 1).asList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
public fun DoubleArray.slice(indices: IntRange): List<Double> {
|
||||
if (indices.isEmpty()) return listOf()
|
||||
return copyOfRange(indices.start, indices.end + 1).asList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
public fun FloatArray.slice(indices: IntRange): List<Float> {
|
||||
if (indices.isEmpty()) return listOf()
|
||||
return copyOfRange(indices.start, indices.end + 1).asList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
public fun IntArray.slice(indices: IntRange): List<Int> {
|
||||
if (indices.isEmpty()) return listOf()
|
||||
return copyOfRange(indices.start, indices.end + 1).asList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
public fun LongArray.slice(indices: IntRange): List<Long> {
|
||||
if (indices.isEmpty()) return listOf()
|
||||
return copyOfRange(indices.start, indices.end + 1).asList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
public fun ShortArray.slice(indices: IntRange): List<Short> {
|
||||
if (indices.isEmpty()) return listOf()
|
||||
return copyOfRange(indices.start, indices.end + 1).asList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
public fun <T> List<T>.slice(indices: IntRange): List<T> {
|
||||
if (indices.isEmpty()) return listOf()
|
||||
return this.subList(indices.start, indices.end + 1).toList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string containing characters at indices at the specified [indices].
|
||||
*/
|
||||
public fun String.slice(indices: IntRange): String {
|
||||
if (indices.isEmpty()) return ""
|
||||
return substring(indices)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at specified [indices].
|
||||
*/
|
||||
public fun <T> Array<out T>.slice(indices: Iterable<Int>): List<T> {
|
||||
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))
|
||||
}
|
||||
@@ -1010,7 +1100,9 @@ public fun <T> Array<out T>.slice(indices: Iterable<Int>): List<T> {
|
||||
* Returns a list containing elements at specified [indices].
|
||||
*/
|
||||
public fun BooleanArray.slice(indices: Iterable<Int>): List<Boolean> {
|
||||
val list = ArrayList<Boolean>(indices.collectionSizeOrDefault(10))
|
||||
val size = indices.collectionSizeOrDefault(10)
|
||||
if (size == 0) return listOf()
|
||||
val list = ArrayList<Boolean>(size)
|
||||
for (index in indices) {
|
||||
list.add(get(index))
|
||||
}
|
||||
@@ -1021,7 +1113,9 @@ public fun BooleanArray.slice(indices: Iterable<Int>): List<Boolean> {
|
||||
* Returns a list containing elements at specified [indices].
|
||||
*/
|
||||
public fun ByteArray.slice(indices: Iterable<Int>): List<Byte> {
|
||||
val list = ArrayList<Byte>(indices.collectionSizeOrDefault(10))
|
||||
val size = indices.collectionSizeOrDefault(10)
|
||||
if (size == 0) return listOf()
|
||||
val list = ArrayList<Byte>(size)
|
||||
for (index in indices) {
|
||||
list.add(get(index))
|
||||
}
|
||||
@@ -1032,7 +1126,9 @@ public fun ByteArray.slice(indices: Iterable<Int>): List<Byte> {
|
||||
* Returns a list containing elements at specified [indices].
|
||||
*/
|
||||
public fun CharArray.slice(indices: Iterable<Int>): List<Char> {
|
||||
val list = ArrayList<Char>(indices.collectionSizeOrDefault(10))
|
||||
val size = indices.collectionSizeOrDefault(10)
|
||||
if (size == 0) return listOf()
|
||||
val list = ArrayList<Char>(size)
|
||||
for (index in indices) {
|
||||
list.add(get(index))
|
||||
}
|
||||
@@ -1043,7 +1139,9 @@ public fun CharArray.slice(indices: Iterable<Int>): List<Char> {
|
||||
* Returns a list containing elements at specified [indices].
|
||||
*/
|
||||
public fun DoubleArray.slice(indices: Iterable<Int>): List<Double> {
|
||||
val list = ArrayList<Double>(indices.collectionSizeOrDefault(10))
|
||||
val size = indices.collectionSizeOrDefault(10)
|
||||
if (size == 0) return listOf()
|
||||
val list = ArrayList<Double>(size)
|
||||
for (index in indices) {
|
||||
list.add(get(index))
|
||||
}
|
||||
@@ -1054,7 +1152,9 @@ public fun DoubleArray.slice(indices: Iterable<Int>): List<Double> {
|
||||
* Returns a list containing elements at specified [indices].
|
||||
*/
|
||||
public fun FloatArray.slice(indices: Iterable<Int>): List<Float> {
|
||||
val list = ArrayList<Float>(indices.collectionSizeOrDefault(10))
|
||||
val size = indices.collectionSizeOrDefault(10)
|
||||
if (size == 0) return listOf()
|
||||
val list = ArrayList<Float>(size)
|
||||
for (index in indices) {
|
||||
list.add(get(index))
|
||||
}
|
||||
@@ -1065,7 +1165,9 @@ public fun FloatArray.slice(indices: Iterable<Int>): List<Float> {
|
||||
* Returns a list containing elements at specified [indices].
|
||||
*/
|
||||
public fun IntArray.slice(indices: Iterable<Int>): List<Int> {
|
||||
val list = ArrayList<Int>(indices.collectionSizeOrDefault(10))
|
||||
val size = indices.collectionSizeOrDefault(10)
|
||||
if (size == 0) return listOf()
|
||||
val list = ArrayList<Int>(size)
|
||||
for (index in indices) {
|
||||
list.add(get(index))
|
||||
}
|
||||
@@ -1076,7 +1178,9 @@ public fun IntArray.slice(indices: Iterable<Int>): List<Int> {
|
||||
* Returns a list containing elements at specified [indices].
|
||||
*/
|
||||
public fun LongArray.slice(indices: Iterable<Int>): List<Long> {
|
||||
val list = ArrayList<Long>(indices.collectionSizeOrDefault(10))
|
||||
val size = indices.collectionSizeOrDefault(10)
|
||||
if (size == 0) return listOf()
|
||||
val list = ArrayList<Long>(size)
|
||||
for (index in indices) {
|
||||
list.add(get(index))
|
||||
}
|
||||
@@ -1087,7 +1191,9 @@ public fun LongArray.slice(indices: Iterable<Int>): List<Long> {
|
||||
* Returns a list containing elements at specified [indices].
|
||||
*/
|
||||
public fun ShortArray.slice(indices: Iterable<Int>): List<Short> {
|
||||
val list = ArrayList<Short>(indices.collectionSizeOrDefault(10))
|
||||
val size = indices.collectionSizeOrDefault(10)
|
||||
if (size == 0) return listOf()
|
||||
val list = ArrayList<Short>(size)
|
||||
for (index in indices) {
|
||||
list.add(get(index))
|
||||
}
|
||||
@@ -1098,7 +1204,9 @@ public fun ShortArray.slice(indices: Iterable<Int>): List<Short> {
|
||||
* Returns a list containing elements at specified [indices].
|
||||
*/
|
||||
public fun <T> List<T>.slice(indices: Iterable<Int>): List<T> {
|
||||
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))
|
||||
}
|
||||
@@ -1109,13 +1217,196 @@ public fun <T> List<T>.slice(indices: Iterable<Int>): List<T> {
|
||||
* Returns a string containing characters at specified [indices].
|
||||
*/
|
||||
public fun String.slice(indices: Iterable<Int>): String {
|
||||
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))
|
||||
}
|
||||
return result.toString()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array containing elements of this array at specified [indices].
|
||||
*/
|
||||
public fun <T, A: Array<out T>> A.sliceArray(indices: Collection<Int>): A {
|
||||
if (indices.isEmpty()) return arrayOfNulls(this, 0) as A
|
||||
val result = arrayOfNulls(this, indices.size()) as Array<T>
|
||||
var targetIndex = 0
|
||||
for (sourceIndex in indices) {
|
||||
result[targetIndex++] = this[sourceIndex]
|
||||
}
|
||||
return result as A
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array containing elements of this array at specified [indices].
|
||||
*/
|
||||
public fun BooleanArray.sliceArray(indices: Collection<Int>): BooleanArray {
|
||||
val result = BooleanArray(indices.size())
|
||||
var targetIndex = 0
|
||||
for (sourceIndex in indices) {
|
||||
result[targetIndex++] = this[sourceIndex]
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array containing elements of this array at specified [indices].
|
||||
*/
|
||||
public fun ByteArray.sliceArray(indices: Collection<Int>): ByteArray {
|
||||
val result = ByteArray(indices.size())
|
||||
var targetIndex = 0
|
||||
for (sourceIndex in indices) {
|
||||
result[targetIndex++] = this[sourceIndex]
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array containing elements of this array at specified [indices].
|
||||
*/
|
||||
public fun CharArray.sliceArray(indices: Collection<Int>): CharArray {
|
||||
val result = CharArray(indices.size())
|
||||
var targetIndex = 0
|
||||
for (sourceIndex in indices) {
|
||||
result[targetIndex++] = this[sourceIndex]
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array containing elements of this array at specified [indices].
|
||||
*/
|
||||
public fun DoubleArray.sliceArray(indices: Collection<Int>): DoubleArray {
|
||||
val result = DoubleArray(indices.size())
|
||||
var targetIndex = 0
|
||||
for (sourceIndex in indices) {
|
||||
result[targetIndex++] = this[sourceIndex]
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array containing elements of this array at specified [indices].
|
||||
*/
|
||||
public fun FloatArray.sliceArray(indices: Collection<Int>): FloatArray {
|
||||
val result = FloatArray(indices.size())
|
||||
var targetIndex = 0
|
||||
for (sourceIndex in indices) {
|
||||
result[targetIndex++] = this[sourceIndex]
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array containing elements of this array at specified [indices].
|
||||
*/
|
||||
public fun IntArray.sliceArray(indices: Collection<Int>): IntArray {
|
||||
val result = IntArray(indices.size())
|
||||
var targetIndex = 0
|
||||
for (sourceIndex in indices) {
|
||||
result[targetIndex++] = this[sourceIndex]
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array containing elements of this array at specified [indices].
|
||||
*/
|
||||
public fun LongArray.sliceArray(indices: Collection<Int>): LongArray {
|
||||
val result = LongArray(indices.size())
|
||||
var targetIndex = 0
|
||||
for (sourceIndex in indices) {
|
||||
result[targetIndex++] = this[sourceIndex]
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array containing elements of this array at specified [indices].
|
||||
*/
|
||||
public fun ShortArray.sliceArray(indices: Collection<Int>): ShortArray {
|
||||
val result = ShortArray(indices.size())
|
||||
var targetIndex = 0
|
||||
for (sourceIndex in indices) {
|
||||
result[targetIndex++] = this[sourceIndex]
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
public fun <T, A: Array<out T>> A.sliceArray(indices: IntRange): A {
|
||||
if (indices.isEmpty()) return copyOf(0) as A
|
||||
return copyOfRange(indices.start, indices.end + 1) as A
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
public fun BooleanArray.sliceArray(indices: IntRange): BooleanArray {
|
||||
if (indices.isEmpty()) return BooleanArray(0)
|
||||
return copyOfRange(indices.start, indices.end + 1)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
public fun ByteArray.sliceArray(indices: IntRange): ByteArray {
|
||||
if (indices.isEmpty()) return ByteArray(0)
|
||||
return copyOfRange(indices.start, indices.end + 1)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
public fun CharArray.sliceArray(indices: IntRange): CharArray {
|
||||
if (indices.isEmpty()) return CharArray(0)
|
||||
return copyOfRange(indices.start, indices.end + 1)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
public fun DoubleArray.sliceArray(indices: IntRange): DoubleArray {
|
||||
if (indices.isEmpty()) return DoubleArray(0)
|
||||
return copyOfRange(indices.start, indices.end + 1)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
public fun FloatArray.sliceArray(indices: IntRange): FloatArray {
|
||||
if (indices.isEmpty()) return FloatArray(0)
|
||||
return copyOfRange(indices.start, indices.end + 1)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
public fun IntArray.sliceArray(indices: IntRange): IntArray {
|
||||
if (indices.isEmpty()) return IntArray(0)
|
||||
return copyOfRange(indices.start, indices.end + 1)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
public fun LongArray.sliceArray(indices: IntRange): LongArray {
|
||||
if (indices.isEmpty()) return LongArray(0)
|
||||
return copyOfRange(indices.start, indices.end + 1)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
public fun ShortArray.sliceArray(indices: IntRange): ShortArray {
|
||||
if (indices.isEmpty()) return ShortArray(0)
|
||||
return copyOfRange(indices.start, indices.end + 1)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing first [n] elements.
|
||||
*/
|
||||
|
||||
@@ -210,16 +210,10 @@ public fun String.reversed(): String {
|
||||
*/
|
||||
public fun <T, A: Array<out T>> A.reversedArray(): A {
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
@@ -96,3 +96,7 @@ public inline fun <reified T> Collection<T>.toTypedArray(): Array<T> {
|
||||
/** Returns the array if it's not `null`, or an empty array otherwise. */
|
||||
public inline fun <reified T> Array<out T>?.orEmpty(): Array<out T> = this ?: arrayOf<T>()
|
||||
|
||||
/** Internal unsafe construction of array based on reference array type */
|
||||
private fun <T, A: Array<out T>> arrayOfNulls(reference: A, size: Int): A {
|
||||
return java.lang.reflect.Array.newInstance(reference.javaClass.componentType, size) as A
|
||||
}
|
||||
@@ -387,20 +387,37 @@ class ArraysTest {
|
||||
}
|
||||
|
||||
test fun slice() {
|
||||
val iter = listOf(3, 1, 2)
|
||||
val iter: Iterable<Int> = listOf(3, 1, 2)
|
||||
|
||||
assertEquals(listOf("B"), arrayOf("A", "B", "C").slice(1..1))
|
||||
assertEquals(listOf('E', 'B', 'C'), arrayOf('A', 'B', 'C', 'E').slice(iter))
|
||||
|
||||
assertEquals(listOf<Int>(), arrayOf<Int>().slice(5..4))
|
||||
assertEquals(listOf<Int>(), arrayOf(1, 2, 3).slice(5..1))
|
||||
assertEquals(listOf(2, 3, 9), arrayOf(2, 3, 9, 2, 3, 9).slice(iter))
|
||||
assertEquals(listOf(2.0, 3.0), arrayOf(2.0, 3.0, 9.0).slice(0..1))
|
||||
assertEquals(listOf(2f, 3f), arrayOf(2f, 3f, 9f).slice(0..1))
|
||||
assertEquals(listOf<Byte>(127, 100), arrayOf<Byte>(50, 100, 127).slice(2 downTo 1))
|
||||
assertEquals(listOf<Short>(200, 100), arrayOf<Short>(50, 100, 200).slice(2 downTo 1))
|
||||
assertEquals(listOf(100L, 200L, 30L), arrayOf(50L, 100L, 200L, 30L).slice(1..3))
|
||||
assertEquals(listOf(true, false, true), arrayOf(true, false, true, true).slice(iter))
|
||||
assertEquals(listOf<Int>(), intArrayOf(1, 2, 3).slice(5..1))
|
||||
assertEquals(listOf(2, 3, 9), intArrayOf(2, 3, 9, 2, 3, 9).slice(iter))
|
||||
assertEquals(listOf(2.0, 3.0), doubleArrayOf(2.0, 3.0, 9.0).slice(0..1))
|
||||
assertEquals(listOf(2f, 3f), floatArrayOf(2f, 3f, 9f).slice(0..1))
|
||||
assertEquals(listOf<Byte>(127, 100), byteArrayOf(50, 100, 127).slice(2 downTo 1))
|
||||
assertEquals(listOf<Short>(200, 100), shortArrayOf(50, 100, 200).slice(2 downTo 1))
|
||||
assertEquals(listOf(100L, 200L, 30L), longArrayOf(50L, 100L, 200L, 30L).slice(1..3))
|
||||
assertEquals(listOf(true, false, true), booleanArrayOf(true, false, true, true).slice(iter))
|
||||
}
|
||||
|
||||
test fun sliceArray() {
|
||||
val coll: Collection<Int> = listOf(3, 1, 2)
|
||||
|
||||
assertArrayNotSameButEquals(arrayOf("B"), arrayOf("A", "B", "C").sliceArray(1..1))
|
||||
assertArrayNotSameButEquals(arrayOf('E', 'B', 'C'), arrayOf('A', 'B', 'C', 'E').sliceArray(coll))
|
||||
|
||||
assertArrayNotSameButEquals(arrayOf<Int>(), arrayOf<Int>().sliceArray(5..4))
|
||||
assertArrayNotSameButEquals(intArrayOf(), intArrayOf(1, 2, 3).sliceArray(5..1))
|
||||
assertArrayNotSameButEquals(intArrayOf(2, 3, 9), intArrayOf(2, 3, 9, 2, 3, 9).sliceArray(coll))
|
||||
assertArrayNotSameButEquals(doubleArrayOf(2.0, 3.0), doubleArrayOf(2.0, 3.0, 9.0).sliceArray(0..1))
|
||||
assertArrayNotSameButEquals(floatArrayOf(2f, 3f), floatArrayOf(2f, 3f, 9f).sliceArray(0..1))
|
||||
// assertArrayNotSameButEquals(byteArrayOf(127, 100), byteArrayOf(50, 100, 127).sliceArray(2 downTo 1))
|
||||
// assertArrayNotSameButEquals(shortArrayOf(200, 100), shortArrayOf(50, 100, 200).sliceArray(2 downTo 1))
|
||||
assertArrayNotSameButEquals(longArrayOf(100L, 200L, 30L), longArrayOf(50L, 100L, 200L, 30L).sliceArray(1..3))
|
||||
assertArrayNotSameButEquals(booleanArrayOf(true, false, true), booleanArrayOf(true, false, true, true).sliceArray(coll))
|
||||
}
|
||||
|
||||
test fun asIterable() {
|
||||
|
||||
@@ -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
|
||||
"""
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user