Implement asList, slice & sliceArray extension functions for UArrays
This commit is contained in:
committed by
Ilya Gorbunov
parent
114736c09b
commit
fc85781bfc
@@ -4040,7 +4040,7 @@ public fun CharArray.sliceArray(indices: Collection<Int>): CharArray {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at indices in the specified [indices] range.
|
||||
* Returns an array containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
public fun <T> Array<T>.sliceArray(indices: IntRange): Array<T> {
|
||||
if (indices.isEmpty()) return copyOfRange(0, 0)
|
||||
@@ -4048,7 +4048,7 @@ public fun <T> Array<T>.sliceArray(indices: IntRange): Array<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at indices in the specified [indices] range.
|
||||
* Returns an array containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
public fun ByteArray.sliceArray(indices: IntRange): ByteArray {
|
||||
if (indices.isEmpty()) return ByteArray(0)
|
||||
@@ -4056,7 +4056,7 @@ public fun ByteArray.sliceArray(indices: IntRange): ByteArray {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at indices in the specified [indices] range.
|
||||
* Returns an array containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
public fun ShortArray.sliceArray(indices: IntRange): ShortArray {
|
||||
if (indices.isEmpty()) return ShortArray(0)
|
||||
@@ -4064,7 +4064,7 @@ public fun ShortArray.sliceArray(indices: IntRange): ShortArray {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at indices in the specified [indices] range.
|
||||
* Returns an array containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
public fun IntArray.sliceArray(indices: IntRange): IntArray {
|
||||
if (indices.isEmpty()) return IntArray(0)
|
||||
@@ -4072,7 +4072,7 @@ public fun IntArray.sliceArray(indices: IntRange): IntArray {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at indices in the specified [indices] range.
|
||||
* Returns an array containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
public fun LongArray.sliceArray(indices: IntRange): LongArray {
|
||||
if (indices.isEmpty()) return LongArray(0)
|
||||
@@ -4080,7 +4080,7 @@ public fun LongArray.sliceArray(indices: IntRange): LongArray {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at indices in the specified [indices] range.
|
||||
* Returns an array containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
public fun FloatArray.sliceArray(indices: IntRange): FloatArray {
|
||||
if (indices.isEmpty()) return FloatArray(0)
|
||||
@@ -4088,7 +4088,7 @@ public fun FloatArray.sliceArray(indices: IntRange): FloatArray {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at indices in the specified [indices] range.
|
||||
* Returns an array containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
public fun DoubleArray.sliceArray(indices: IntRange): DoubleArray {
|
||||
if (indices.isEmpty()) return DoubleArray(0)
|
||||
@@ -4096,7 +4096,7 @@ public fun DoubleArray.sliceArray(indices: IntRange): DoubleArray {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at indices in the specified [indices] range.
|
||||
* Returns an array containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
public fun BooleanArray.sliceArray(indices: IntRange): BooleanArray {
|
||||
if (indices.isEmpty()) return BooleanArray(0)
|
||||
@@ -4104,7 +4104,7 @@ public fun BooleanArray.sliceArray(indices: IntRange): BooleanArray {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at indices in the specified [indices] range.
|
||||
* Returns an array containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
public fun CharArray.sliceArray(indices: IntRange): CharArray {
|
||||
if (indices.isEmpty()) return CharArray(0)
|
||||
|
||||
@@ -1383,6 +1383,178 @@ public inline fun UShortArray.singleOrNull(predicate: (UShort) -> Boolean): USho
|
||||
return single
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun UIntArray.slice(indices: IntRange): List<UInt> {
|
||||
if (indices.isEmpty()) return listOf()
|
||||
return copyOfRange(indices.start, indices.endInclusive + 1).asList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun ULongArray.slice(indices: IntRange): List<ULong> {
|
||||
if (indices.isEmpty()) return listOf()
|
||||
return copyOfRange(indices.start, indices.endInclusive + 1).asList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun UByteArray.slice(indices: IntRange): List<UByte> {
|
||||
if (indices.isEmpty()) return listOf()
|
||||
return copyOfRange(indices.start, indices.endInclusive + 1).asList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun UShortArray.slice(indices: IntRange): List<UShort> {
|
||||
if (indices.isEmpty()) return listOf()
|
||||
return copyOfRange(indices.start, indices.endInclusive + 1).asList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at specified [indices].
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun UIntArray.slice(indices: Iterable<Int>): List<UInt> {
|
||||
val size = indices.collectionSizeOrDefault(10)
|
||||
if (size == 0) return emptyList()
|
||||
val list = ArrayList<UInt>(size)
|
||||
for (index in indices) {
|
||||
list.add(get(index))
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at specified [indices].
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun ULongArray.slice(indices: Iterable<Int>): List<ULong> {
|
||||
val size = indices.collectionSizeOrDefault(10)
|
||||
if (size == 0) return emptyList()
|
||||
val list = ArrayList<ULong>(size)
|
||||
for (index in indices) {
|
||||
list.add(get(index))
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at specified [indices].
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun UByteArray.slice(indices: Iterable<Int>): List<UByte> {
|
||||
val size = indices.collectionSizeOrDefault(10)
|
||||
if (size == 0) return emptyList()
|
||||
val list = ArrayList<UByte>(size)
|
||||
for (index in indices) {
|
||||
list.add(get(index))
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at specified [indices].
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun UShortArray.slice(indices: Iterable<Int>): List<UShort> {
|
||||
val size = indices.collectionSizeOrDefault(10)
|
||||
if (size == 0) return emptyList()
|
||||
val list = ArrayList<UShort>(size)
|
||||
for (index in indices) {
|
||||
list.add(get(index))
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array containing elements of this array at specified [indices].
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun UIntArray.sliceArray(indices: Collection<Int>): UIntArray {
|
||||
return UIntArray(storage.sliceArray(indices))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array containing elements of this array at specified [indices].
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun ULongArray.sliceArray(indices: Collection<Int>): ULongArray {
|
||||
return ULongArray(storage.sliceArray(indices))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array containing elements of this array at specified [indices].
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun UByteArray.sliceArray(indices: Collection<Int>): UByteArray {
|
||||
return UByteArray(storage.sliceArray(indices))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array containing elements of this array at specified [indices].
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun UShortArray.sliceArray(indices: Collection<Int>): UShortArray {
|
||||
return UShortArray(storage.sliceArray(indices))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun UIntArray.sliceArray(indices: IntRange): UIntArray {
|
||||
return UIntArray(storage.sliceArray(indices))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun ULongArray.sliceArray(indices: IntRange): ULongArray {
|
||||
return ULongArray(storage.sliceArray(indices))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun UByteArray.sliceArray(indices: IntRange): UByteArray {
|
||||
return UByteArray(storage.sliceArray(indices))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array containing elements at indices in the specified [indices] range.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun UShortArray.sliceArray(indices: IntRange): UShortArray {
|
||||
return UShortArray(storage.sliceArray(indices))
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverses elements in the array in-place.
|
||||
*/
|
||||
@@ -1533,6 +1705,34 @@ public inline fun UIntArray.asIntArray(): IntArray {
|
||||
return storage
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [List] that wraps the original array.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public expect fun UIntArray.asList(): List<UInt>
|
||||
|
||||
/**
|
||||
* Returns a [List] that wraps the original array.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public expect fun ULongArray.asList(): List<ULong>
|
||||
|
||||
/**
|
||||
* Returns a [List] that wraps the original array.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public expect fun UByteArray.asList(): List<UByte>
|
||||
|
||||
/**
|
||||
* Returns a [List] that wraps the original array.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public expect fun UShortArray.asList(): List<UShort>
|
||||
|
||||
/**
|
||||
* Returns an array of type [LongArray], which is a view of this array where each element is a signed reinterpretation
|
||||
* of the corresponding element of this array.
|
||||
|
||||
Reference in New Issue
Block a user