Implement drop, take & filter extension functions for UArrays

This commit is contained in:
Abduqodiri Qurbonzoda
2019-02-15 15:45:39 +03:00
committed by Ilya Gorbunov
parent abb275775e
commit c42dbb34ca
4 changed files with 973 additions and 17 deletions
@@ -1383,6 +1383,530 @@ public inline fun UShortArray.singleOrNull(predicate: (UShort) -> Boolean): USho
return single
}
/**
* Returns a list containing all elements except first [n] elements.
*
* @sample samples.collections.Collections.Transformations.drop
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UIntArray.drop(n: Int): List<UInt> {
require(n >= 0) { "Requested element count $n is less than zero." }
return takeLast((size - n).coerceAtLeast(0))
}
/**
* Returns a list containing all elements except first [n] elements.
*
* @sample samples.collections.Collections.Transformations.drop
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun ULongArray.drop(n: Int): List<ULong> {
require(n >= 0) { "Requested element count $n is less than zero." }
return takeLast((size - n).coerceAtLeast(0))
}
/**
* Returns a list containing all elements except first [n] elements.
*
* @sample samples.collections.Collections.Transformations.drop
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UByteArray.drop(n: Int): List<UByte> {
require(n >= 0) { "Requested element count $n is less than zero." }
return takeLast((size - n).coerceAtLeast(0))
}
/**
* Returns a list containing all elements except first [n] elements.
*
* @sample samples.collections.Collections.Transformations.drop
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UShortArray.drop(n: Int): List<UShort> {
require(n >= 0) { "Requested element count $n is less than zero." }
return takeLast((size - n).coerceAtLeast(0))
}
/**
* Returns a list containing all elements except last [n] elements.
*
* @sample samples.collections.Collections.Transformations.drop
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UIntArray.dropLast(n: Int): List<UInt> {
require(n >= 0) { "Requested element count $n is less than zero." }
return take((size - n).coerceAtLeast(0))
}
/**
* Returns a list containing all elements except last [n] elements.
*
* @sample samples.collections.Collections.Transformations.drop
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun ULongArray.dropLast(n: Int): List<ULong> {
require(n >= 0) { "Requested element count $n is less than zero." }
return take((size - n).coerceAtLeast(0))
}
/**
* Returns a list containing all elements except last [n] elements.
*
* @sample samples.collections.Collections.Transformations.drop
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UByteArray.dropLast(n: Int): List<UByte> {
require(n >= 0) { "Requested element count $n is less than zero." }
return take((size - n).coerceAtLeast(0))
}
/**
* Returns a list containing all elements except last [n] elements.
*
* @sample samples.collections.Collections.Transformations.drop
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UShortArray.dropLast(n: Int): List<UShort> {
require(n >= 0) { "Requested element count $n is less than zero." }
return take((size - n).coerceAtLeast(0))
}
/**
* Returns a list containing all elements except last elements that satisfy the given [predicate].
*
* @sample samples.collections.Collections.Transformations.drop
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UIntArray.dropLastWhile(predicate: (UInt) -> Boolean): List<UInt> {
for (index in lastIndex downTo 0) {
if (!predicate(this[index])) {
return take(index + 1)
}
}
return emptyList()
}
/**
* Returns a list containing all elements except last elements that satisfy the given [predicate].
*
* @sample samples.collections.Collections.Transformations.drop
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun ULongArray.dropLastWhile(predicate: (ULong) -> Boolean): List<ULong> {
for (index in lastIndex downTo 0) {
if (!predicate(this[index])) {
return take(index + 1)
}
}
return emptyList()
}
/**
* Returns a list containing all elements except last elements that satisfy the given [predicate].
*
* @sample samples.collections.Collections.Transformations.drop
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UByteArray.dropLastWhile(predicate: (UByte) -> Boolean): List<UByte> {
for (index in lastIndex downTo 0) {
if (!predicate(this[index])) {
return take(index + 1)
}
}
return emptyList()
}
/**
* Returns a list containing all elements except last elements that satisfy the given [predicate].
*
* @sample samples.collections.Collections.Transformations.drop
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UShortArray.dropLastWhile(predicate: (UShort) -> Boolean): List<UShort> {
for (index in lastIndex downTo 0) {
if (!predicate(this[index])) {
return take(index + 1)
}
}
return emptyList()
}
/**
* Returns a list containing all elements except first elements that satisfy the given [predicate].
*
* @sample samples.collections.Collections.Transformations.drop
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UIntArray.dropWhile(predicate: (UInt) -> Boolean): List<UInt> {
var yielding = false
val list = ArrayList<UInt>()
for (item in this)
if (yielding)
list.add(item)
else if (!predicate(item)) {
list.add(item)
yielding = true
}
return list
}
/**
* Returns a list containing all elements except first elements that satisfy the given [predicate].
*
* @sample samples.collections.Collections.Transformations.drop
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun ULongArray.dropWhile(predicate: (ULong) -> Boolean): List<ULong> {
var yielding = false
val list = ArrayList<ULong>()
for (item in this)
if (yielding)
list.add(item)
else if (!predicate(item)) {
list.add(item)
yielding = true
}
return list
}
/**
* Returns a list containing all elements except first elements that satisfy the given [predicate].
*
* @sample samples.collections.Collections.Transformations.drop
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UByteArray.dropWhile(predicate: (UByte) -> Boolean): List<UByte> {
var yielding = false
val list = ArrayList<UByte>()
for (item in this)
if (yielding)
list.add(item)
else if (!predicate(item)) {
list.add(item)
yielding = true
}
return list
}
/**
* Returns a list containing all elements except first elements that satisfy the given [predicate].
*
* @sample samples.collections.Collections.Transformations.drop
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UShortArray.dropWhile(predicate: (UShort) -> Boolean): List<UShort> {
var yielding = false
val list = ArrayList<UShort>()
for (item in this)
if (yielding)
list.add(item)
else if (!predicate(item)) {
list.add(item)
yielding = true
}
return list
}
/**
* Returns a list containing only elements matching the given [predicate].
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UIntArray.filter(predicate: (UInt) -> Boolean): List<UInt> {
return filterTo(ArrayList<UInt>(), predicate)
}
/**
* Returns a list containing only elements matching the given [predicate].
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun ULongArray.filter(predicate: (ULong) -> Boolean): List<ULong> {
return filterTo(ArrayList<ULong>(), predicate)
}
/**
* Returns a list containing only elements matching the given [predicate].
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UByteArray.filter(predicate: (UByte) -> Boolean): List<UByte> {
return filterTo(ArrayList<UByte>(), predicate)
}
/**
* Returns a list containing only elements matching the given [predicate].
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UShortArray.filter(predicate: (UShort) -> Boolean): List<UShort> {
return filterTo(ArrayList<UShort>(), predicate)
}
/**
* Returns a list containing only elements matching the given [predicate].
* @param [predicate] function that takes the index of an element and the element itself
* and returns the result of predicate evaluation on the element.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UIntArray.filterIndexed(predicate: (index: Int, UInt) -> Boolean): List<UInt> {
return filterIndexedTo(ArrayList<UInt>(), predicate)
}
/**
* Returns a list containing only elements matching the given [predicate].
* @param [predicate] function that takes the index of an element and the element itself
* and returns the result of predicate evaluation on the element.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun ULongArray.filterIndexed(predicate: (index: Int, ULong) -> Boolean): List<ULong> {
return filterIndexedTo(ArrayList<ULong>(), predicate)
}
/**
* Returns a list containing only elements matching the given [predicate].
* @param [predicate] function that takes the index of an element and the element itself
* and returns the result of predicate evaluation on the element.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UByteArray.filterIndexed(predicate: (index: Int, UByte) -> Boolean): List<UByte> {
return filterIndexedTo(ArrayList<UByte>(), predicate)
}
/**
* Returns a list containing only elements matching the given [predicate].
* @param [predicate] function that takes the index of an element and the element itself
* and returns the result of predicate evaluation on the element.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UShortArray.filterIndexed(predicate: (index: Int, UShort) -> Boolean): List<UShort> {
return filterIndexedTo(ArrayList<UShort>(), predicate)
}
/**
* Appends all elements matching the given [predicate] to the given [destination].
* @param [predicate] function that takes the index of an element and the element itself
* and returns the result of predicate evaluation on the element.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <C : MutableCollection<in UInt>> UIntArray.filterIndexedTo(destination: C, predicate: (index: Int, UInt) -> Boolean): C {
forEachIndexed { index, element ->
if (predicate(index, element)) destination.add(element)
}
return destination
}
/**
* Appends all elements matching the given [predicate] to the given [destination].
* @param [predicate] function that takes the index of an element and the element itself
* and returns the result of predicate evaluation on the element.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <C : MutableCollection<in ULong>> ULongArray.filterIndexedTo(destination: C, predicate: (index: Int, ULong) -> Boolean): C {
forEachIndexed { index, element ->
if (predicate(index, element)) destination.add(element)
}
return destination
}
/**
* Appends all elements matching the given [predicate] to the given [destination].
* @param [predicate] function that takes the index of an element and the element itself
* and returns the result of predicate evaluation on the element.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <C : MutableCollection<in UByte>> UByteArray.filterIndexedTo(destination: C, predicate: (index: Int, UByte) -> Boolean): C {
forEachIndexed { index, element ->
if (predicate(index, element)) destination.add(element)
}
return destination
}
/**
* Appends all elements matching the given [predicate] to the given [destination].
* @param [predicate] function that takes the index of an element and the element itself
* and returns the result of predicate evaluation on the element.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <C : MutableCollection<in UShort>> UShortArray.filterIndexedTo(destination: C, predicate: (index: Int, UShort) -> Boolean): C {
forEachIndexed { index, element ->
if (predicate(index, element)) destination.add(element)
}
return destination
}
/**
* Returns a list containing all elements not matching the given [predicate].
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UIntArray.filterNot(predicate: (UInt) -> Boolean): List<UInt> {
return filterNotTo(ArrayList<UInt>(), predicate)
}
/**
* Returns a list containing all elements not matching the given [predicate].
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun ULongArray.filterNot(predicate: (ULong) -> Boolean): List<ULong> {
return filterNotTo(ArrayList<ULong>(), predicate)
}
/**
* Returns a list containing all elements not matching the given [predicate].
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UByteArray.filterNot(predicate: (UByte) -> Boolean): List<UByte> {
return filterNotTo(ArrayList<UByte>(), predicate)
}
/**
* Returns a list containing all elements not matching the given [predicate].
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UShortArray.filterNot(predicate: (UShort) -> Boolean): List<UShort> {
return filterNotTo(ArrayList<UShort>(), predicate)
}
/**
* Appends all elements not matching the given [predicate] to the given [destination].
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <C : MutableCollection<in UInt>> UIntArray.filterNotTo(destination: C, predicate: (UInt) -> Boolean): C {
for (element in this) if (!predicate(element)) destination.add(element)
return destination
}
/**
* Appends all elements not matching the given [predicate] to the given [destination].
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <C : MutableCollection<in ULong>> ULongArray.filterNotTo(destination: C, predicate: (ULong) -> Boolean): C {
for (element in this) if (!predicate(element)) destination.add(element)
return destination
}
/**
* Appends all elements not matching the given [predicate] to the given [destination].
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <C : MutableCollection<in UByte>> UByteArray.filterNotTo(destination: C, predicate: (UByte) -> Boolean): C {
for (element in this) if (!predicate(element)) destination.add(element)
return destination
}
/**
* Appends all elements not matching the given [predicate] to the given [destination].
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <C : MutableCollection<in UShort>> UShortArray.filterNotTo(destination: C, predicate: (UShort) -> Boolean): C {
for (element in this) if (!predicate(element)) destination.add(element)
return destination
}
/**
* Appends all elements matching the given [predicate] to the given [destination].
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <C : MutableCollection<in UInt>> UIntArray.filterTo(destination: C, predicate: (UInt) -> Boolean): C {
for (element in this) if (predicate(element)) destination.add(element)
return destination
}
/**
* Appends all elements matching the given [predicate] to the given [destination].
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <C : MutableCollection<in ULong>> ULongArray.filterTo(destination: C, predicate: (ULong) -> Boolean): C {
for (element in this) if (predicate(element)) destination.add(element)
return destination
}
/**
* Appends all elements matching the given [predicate] to the given [destination].
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <C : MutableCollection<in UByte>> UByteArray.filterTo(destination: C, predicate: (UByte) -> Boolean): C {
for (element in this) if (predicate(element)) destination.add(element)
return destination
}
/**
* Appends all elements matching the given [predicate] to the given [destination].
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun <C : MutableCollection<in UShort>> UShortArray.filterTo(destination: C, predicate: (UShort) -> Boolean): C {
for (element in this) if (predicate(element)) destination.add(element)
return destination
}
/**
* Returns a list containing elements at indices in the specified [indices] range.
*/
@@ -1555,6 +2079,310 @@ public fun UShortArray.sliceArray(indices: IntRange): UShortArray {
return UShortArray(storage.sliceArray(indices))
}
/**
* Returns a list containing first [n] elements.
*
* @sample samples.collections.Collections.Transformations.take
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UIntArray.take(n: Int): List<UInt> {
require(n >= 0) { "Requested element count $n is less than zero." }
if (n == 0) return emptyList()
if (n >= size) return toList()
if (n == 1) return listOf(this[0])
var count = 0
val list = ArrayList<UInt>(n)
for (item in this) {
if (count++ == n)
break
list.add(item)
}
return list
}
/**
* Returns a list containing first [n] elements.
*
* @sample samples.collections.Collections.Transformations.take
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun ULongArray.take(n: Int): List<ULong> {
require(n >= 0) { "Requested element count $n is less than zero." }
if (n == 0) return emptyList()
if (n >= size) return toList()
if (n == 1) return listOf(this[0])
var count = 0
val list = ArrayList<ULong>(n)
for (item in this) {
if (count++ == n)
break
list.add(item)
}
return list
}
/**
* Returns a list containing first [n] elements.
*
* @sample samples.collections.Collections.Transformations.take
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UByteArray.take(n: Int): List<UByte> {
require(n >= 0) { "Requested element count $n is less than zero." }
if (n == 0) return emptyList()
if (n >= size) return toList()
if (n == 1) return listOf(this[0])
var count = 0
val list = ArrayList<UByte>(n)
for (item in this) {
if (count++ == n)
break
list.add(item)
}
return list
}
/**
* Returns a list containing first [n] elements.
*
* @sample samples.collections.Collections.Transformations.take
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UShortArray.take(n: Int): List<UShort> {
require(n >= 0) { "Requested element count $n is less than zero." }
if (n == 0) return emptyList()
if (n >= size) return toList()
if (n == 1) return listOf(this[0])
var count = 0
val list = ArrayList<UShort>(n)
for (item in this) {
if (count++ == n)
break
list.add(item)
}
return list
}
/**
* Returns a list containing last [n] elements.
*
* @sample samples.collections.Collections.Transformations.take
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UIntArray.takeLast(n: Int): List<UInt> {
require(n >= 0) { "Requested element count $n is less than zero." }
if (n == 0) return emptyList()
val size = size
if (n >= size) return toList()
if (n == 1) return listOf(this[size - 1])
val list = ArrayList<UInt>(n)
for (index in size - n until size)
list.add(this[index])
return list
}
/**
* Returns a list containing last [n] elements.
*
* @sample samples.collections.Collections.Transformations.take
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun ULongArray.takeLast(n: Int): List<ULong> {
require(n >= 0) { "Requested element count $n is less than zero." }
if (n == 0) return emptyList()
val size = size
if (n >= size) return toList()
if (n == 1) return listOf(this[size - 1])
val list = ArrayList<ULong>(n)
for (index in size - n until size)
list.add(this[index])
return list
}
/**
* Returns a list containing last [n] elements.
*
* @sample samples.collections.Collections.Transformations.take
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UByteArray.takeLast(n: Int): List<UByte> {
require(n >= 0) { "Requested element count $n is less than zero." }
if (n == 0) return emptyList()
val size = size
if (n >= size) return toList()
if (n == 1) return listOf(this[size - 1])
val list = ArrayList<UByte>(n)
for (index in size - n until size)
list.add(this[index])
return list
}
/**
* Returns a list containing last [n] elements.
*
* @sample samples.collections.Collections.Transformations.take
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public fun UShortArray.takeLast(n: Int): List<UShort> {
require(n >= 0) { "Requested element count $n is less than zero." }
if (n == 0) return emptyList()
val size = size
if (n >= size) return toList()
if (n == 1) return listOf(this[size - 1])
val list = ArrayList<UShort>(n)
for (index in size - n until size)
list.add(this[index])
return list
}
/**
* Returns a list containing last elements satisfying the given [predicate].
*
* @sample samples.collections.Collections.Transformations.take
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UIntArray.takeLastWhile(predicate: (UInt) -> Boolean): List<UInt> {
for (index in lastIndex downTo 0) {
if (!predicate(this[index])) {
return drop(index + 1)
}
}
return toList()
}
/**
* Returns a list containing last elements satisfying the given [predicate].
*
* @sample samples.collections.Collections.Transformations.take
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun ULongArray.takeLastWhile(predicate: (ULong) -> Boolean): List<ULong> {
for (index in lastIndex downTo 0) {
if (!predicate(this[index])) {
return drop(index + 1)
}
}
return toList()
}
/**
* Returns a list containing last elements satisfying the given [predicate].
*
* @sample samples.collections.Collections.Transformations.take
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UByteArray.takeLastWhile(predicate: (UByte) -> Boolean): List<UByte> {
for (index in lastIndex downTo 0) {
if (!predicate(this[index])) {
return drop(index + 1)
}
}
return toList()
}
/**
* Returns a list containing last elements satisfying the given [predicate].
*
* @sample samples.collections.Collections.Transformations.take
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UShortArray.takeLastWhile(predicate: (UShort) -> Boolean): List<UShort> {
for (index in lastIndex downTo 0) {
if (!predicate(this[index])) {
return drop(index + 1)
}
}
return toList()
}
/**
* Returns a list containing first elements satisfying the given [predicate].
*
* @sample samples.collections.Collections.Transformations.take
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UIntArray.takeWhile(predicate: (UInt) -> Boolean): List<UInt> {
val list = ArrayList<UInt>()
for (item in this) {
if (!predicate(item))
break
list.add(item)
}
return list
}
/**
* Returns a list containing first elements satisfying the given [predicate].
*
* @sample samples.collections.Collections.Transformations.take
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun ULongArray.takeWhile(predicate: (ULong) -> Boolean): List<ULong> {
val list = ArrayList<ULong>()
for (item in this) {
if (!predicate(item))
break
list.add(item)
}
return list
}
/**
* Returns a list containing first elements satisfying the given [predicate].
*
* @sample samples.collections.Collections.Transformations.take
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UByteArray.takeWhile(predicate: (UByte) -> Boolean): List<UByte> {
val list = ArrayList<UByte>()
for (item in this) {
if (!predicate(item))
break
list.add(item)
}
return list
}
/**
* Returns a list containing first elements satisfying the given [predicate].
*
* @sample samples.collections.Collections.Transformations.take
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UShortArray.takeWhile(predicate: (UShort) -> Boolean): List<UShort> {
val list = ArrayList<UShort>()
for (item in this) {
if (!predicate(item))
break
list.add(item)
}
return list
}
/**
* Reverses elements in the array in-place.
*/
@@ -823,4 +823,104 @@ class UnsignedArraysTest {
)
}
@Test
fun drop() {
expect(listOf(1.toUByte())) { ubyteArrayOf(1).drop(0) }
expect(listOf()) { ushortArrayOf().drop(1) }
expect(listOf()) { uintArrayOf(1).drop(1) }
expect(listOf(3uL)) { ulongArrayOf(2, 3).drop(1) }
assertFails {
uintArrayOf(2).drop(-1)
}
}
@Test
fun dropLast() {
expect(listOf()) { ubyteArrayOf().dropLast(1) }
expect(listOf()) { ushortArrayOf(1).dropLast(1) }
expect(listOf(1u)) { uintArrayOf(1).dropLast(0) }
expect(listOf(2uL)) { ulongArrayOf(2, 3).dropLast(1) }
assertFails {
ulongArrayOf(1).dropLast(-1)
}
}
@Test
fun dropWhile() {
expect(listOf(3.toUByte(), 1.toUByte())) { ubyteArrayOf(2, 3, 1).dropWhile { it < 3 } }
expect(listOf()) { ushortArrayOf().dropWhile { it < 3 } }
expect(listOf()) { uintArrayOf(1).dropWhile { it < 3 } }
expect(listOf(3uL, 1uL)) { ulongArrayOf(2, 3, 1).dropWhile { it < 3 } }
}
@Test
fun dropLastWhile() {
expect(listOf(2.toUByte(), 3.toUByte())) { ubyteArrayOf(2, 3, 1).dropLastWhile { it < 3 } }
expect(listOf()) { ushortArrayOf().dropLastWhile { it < 3 } }
expect(listOf()) { uintArrayOf(1).dropLastWhile { it < 3 } }
expect(listOf(2uL, 3uL)) { ulongArrayOf(2, 3, 1).dropLastWhile { it < 3 } }
}
@Test
fun take() {
expect(listOf()) { ubyteArrayOf().take(1) }
expect(listOf()) { ushortArrayOf(1).take(0) }
expect(listOf(1u)) { uintArrayOf(1).take(1) }
expect(listOf(2uL)) { ulongArrayOf(2, 3).take(1) }
assertFails {
ubyteArrayOf(1).take(-1)
}
}
@Test
fun takeLast() {
expect(listOf()) { ubyteArrayOf().takeLast(1) }
expect(listOf()) { ushortArrayOf(1).takeLast(0) }
expect(listOf(1u)) { uintArrayOf(1).takeLast(1) }
expect(listOf(3uL)) { ulongArrayOf(2, 3).takeLast(1) }
assertFails {
ushortArrayOf(1).takeLast(-1)
}
}
@Test
fun takeWhile() {
expect(listOf(2.toUByte())) { ubyteArrayOf(2, 3, 1).takeWhile { it < 3 } }
expect(listOf()) { ushortArrayOf().takeWhile { it < 3 } }
expect(listOf(1u)) { uintArrayOf(1).takeWhile { it < 3 } }
expect(listOf(2uL)) { ulongArrayOf(2, 3, 1).takeWhile { it < 3 } }
}
@Test
fun takeLastWhile() {
expect(listOf()) { ubyteArrayOf().takeLastWhile { it < 3 } }
expect(listOf(1.toUShort())) { ushortArrayOf(1).takeLastWhile { it < 3 } }
expect(listOf(1u)) { uintArrayOf(2, 3, 1).takeLastWhile { it < 3 } }
expect(listOf(1uL)) { ulongArrayOf(2, 3, 1).takeLastWhile { it < 3 } }
}
@Test
fun filter() {
expect(listOf(3.toByte())) { byteArrayOf(2, 3).filter { it > 2 } }
expect(listOf()) { ushortArrayOf().filter { it > 2 } }
expect(listOf()) { uintArrayOf(1).filter { it > 2 } }
expect(listOf(3uL)) { ulongArrayOf(2, 3).filter { it > 2 } }
}
@Test
fun filterIndexed() {
expect(listOf<UByte>(2, 5, 8)) { ubyteArrayOf(2, 4, 3, 5, 8).filterIndexed { index, value -> index % 2 == (value % 2).toInt() } }
expect(listOf()) { ushortArrayOf().filterIndexed { i, v -> i > v.toInt() } }
expect(listOf<UInt>(2, 5, 8)) { uintArrayOf(2, 4, 3, 5, 8).filterIndexed { index, value -> index % 2 == (value % 2).toInt() } }
expect(listOf<ULong>(2, 5, 8)) { ulongArrayOf(2, 4, 3, 5, 8).filterIndexed { index, value -> index % 2 == (value % 2).toInt() } }
}
@Test
fun filterNot() {
expect(listOf(2.toUByte())) { ubyteArrayOf(2, 3).filterNot { it > 2 } }
expect(listOf()) { ushortArrayOf().filterNot { it > 2 } }
expect(listOf(1u)) { uintArrayOf(1).filterNot { it > 2 } }
expect(listOf(2uL)) { ulongArrayOf(2, 3).filterNot { it > 2 } }
}
}
@@ -2311,6 +2311,14 @@ public final class kotlin/collections/UArraysKt {
public static final fun contentToString-GBYM_sE ([B)Ljava/lang/String;
public static final fun contentToString-QwZRm1k ([J)Ljava/lang/String;
public static final fun contentToString-rL5Bavg ([S)Ljava/lang/String;
public static final fun drop-PpDY95g ([BI)Ljava/util/List;
public static final fun drop-nggk6HY ([SI)Ljava/util/List;
public static final fun drop-qFRl0hI ([II)Ljava/util/List;
public static final fun drop-r7IrZao ([JI)Ljava/util/List;
public static final fun dropLast-PpDY95g ([BI)Ljava/util/List;
public static final fun dropLast-nggk6HY ([SI)Ljava/util/List;
public static final fun dropLast-qFRl0hI ([II)Ljava/util/List;
public static final fun dropLast-r7IrZao ([JI)Ljava/util/List;
public static final fun fill-2fe2U9s ([IIII)V
public static synthetic fun fill-2fe2U9s$default ([IIIIILjava/lang/Object;)V
public static final fun fill-EtDCXyQ ([SSII)V
@@ -2391,6 +2399,14 @@ public final class kotlin/collections/UArraysKt {
public static final fun sumOfUInt ([Lkotlin/UInt;)I
public static final fun sumOfULong ([Lkotlin/ULong;)J
public static final fun sumOfUShort ([Lkotlin/UShort;)I
public static final fun take-PpDY95g ([BI)Ljava/util/List;
public static final fun take-nggk6HY ([SI)Ljava/util/List;
public static final fun take-qFRl0hI ([II)Ljava/util/List;
public static final fun take-r7IrZao ([JI)Ljava/util/List;
public static final fun takeLast-PpDY95g ([BI)Ljava/util/List;
public static final fun takeLast-nggk6HY ([SI)Ljava/util/List;
public static final fun takeLast-qFRl0hI ([II)Ljava/util/List;
public static final fun takeLast-r7IrZao ([JI)Ljava/util/List;
public static final fun toTypedArray--ajY-9A ([I)[Lkotlin/UInt;
public static final fun toTypedArray-GBYM_sE ([B)[Lkotlin/UByte;
public static final fun toTypedArray-QwZRm1k ([J)[Lkotlin/ULong;
@@ -39,7 +39,7 @@ object Filtering : TemplateGroupBase() {
val f_drop = fn("drop(n: Int)") {
includeDefault()
include(CharSequences, Strings)
include(CharSequences, Strings, ArraysOfUnsigned)
} builder {
val n = "\$n"
doc {
@@ -115,7 +115,7 @@ object Filtering : TemplateGroupBase() {
"""
}
body(ArraysOfObjects, ArraysOfPrimitives) {
body(ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) {
"""
require(n >= 0) { "Requested element count $n is less than zero." }
return takeLast((size - n).coerceAtLeast(0))
@@ -125,7 +125,7 @@ object Filtering : TemplateGroupBase() {
val f_take = fn("take(n: Int)") {
includeDefault()
include(CharSequences, Strings)
include(CharSequences, Strings, ArraysOfUnsigned)
} builder {
val n = "\$n"
doc {
@@ -185,7 +185,7 @@ object Filtering : TemplateGroupBase() {
"""
}
body(ArraysOfObjects, ArraysOfPrimitives) {
body(ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) {
"""
require(n >= 0) { "Requested element count $n is less than zero." }
if (n == 0) return emptyList()
@@ -204,7 +204,7 @@ object Filtering : TemplateGroupBase() {
}
val f_dropLast = fn("dropLast(n: Int)") {
include(Lists, ArraysOfObjects, ArraysOfPrimitives, CharSequences, Strings)
include(Lists, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned, CharSequences, Strings)
} builder {
val n = "\$n"
@@ -234,7 +234,7 @@ object Filtering : TemplateGroupBase() {
}
val f_takeLast = fn("takeLast(n: Int)") {
include(Lists, ArraysOfObjects, ArraysOfPrimitives, CharSequences, Strings)
include(Lists, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned, CharSequences, Strings)
} builder {
val n = "\$n"
doc {
@@ -262,7 +262,7 @@ object Filtering : TemplateGroupBase() {
"""
}
body(ArraysOfObjects, ArraysOfPrimitives) {
body(ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) {
"""
require(n >= 0) { "Requested element count $n is less than zero." }
if (n == 0) return emptyList()
@@ -299,9 +299,10 @@ object Filtering : TemplateGroupBase() {
val f_dropWhile = fn("dropWhile(predicate: (T) -> Boolean)") {
includeDefault()
include(CharSequences, Strings)
include(CharSequences, Strings, ArraysOfUnsigned)
} builder {
inline()
specialFor(ArraysOfUnsigned) { inlineOnly() }
doc {
"""
@@ -355,9 +356,10 @@ object Filtering : TemplateGroupBase() {
val f_takeWhile = fn("takeWhile(predicate: (T) -> Boolean)") {
includeDefault()
include(CharSequences, Strings)
include(CharSequences, Strings, ArraysOfUnsigned)
} builder {
inline()
specialFor(ArraysOfUnsigned) { inlineOnly() }
doc {
"""
@@ -406,9 +408,11 @@ object Filtering : TemplateGroupBase() {
}
val f_dropLastWhile = fn("dropLastWhile(predicate: (T) -> Boolean)") {
include(Lists, ArraysOfObjects, ArraysOfPrimitives, CharSequences, Strings)
include(Lists, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned, CharSequences, Strings)
} builder {
inline()
specialFor(ArraysOfUnsigned) { inlineOnly() }
doc {
"""
Returns a list containing all elements except last elements that satisfy the given [predicate].
@@ -462,9 +466,11 @@ object Filtering : TemplateGroupBase() {
}
val f_takeLastWhile = fn("takeLastWhile(predicate: (T) -> Boolean)") {
include(Lists, ArraysOfObjects, ArraysOfPrimitives, CharSequences, Strings)
include(Lists, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned, CharSequences, Strings)
} builder {
inline()
specialFor(ArraysOfUnsigned) { inlineOnly() }
doc {
"""
Returns a list containing last elements satisfying the given [predicate].
@@ -528,9 +534,10 @@ object Filtering : TemplateGroupBase() {
val f_filter = fn("filter(predicate: (T) -> Boolean)") {
includeDefault()
include(CharSequences, Strings)
include(CharSequences, Strings, ArraysOfUnsigned)
} builder {
inline()
specialFor(ArraysOfUnsigned) { inlineOnly() }
doc { "Returns a ${f.mapResult} containing only ${f.element.pluralize()} matching the given [predicate]." }
returns("List<T>")
@@ -555,9 +562,10 @@ object Filtering : TemplateGroupBase() {
val f_filterTo = fn("filterTo(destination: C, predicate: (T) -> Boolean)") {
includeDefault()
include(CharSequences)
include(CharSequences, ArraysOfUnsigned)
} builder {
inline()
specialFor(ArraysOfUnsigned) { inlineOnly() }
doc { "Appends all ${f.element.pluralize()} matching the given [predicate] to the given [destination]." }
typeParam("C : TCollection")
@@ -583,9 +591,10 @@ object Filtering : TemplateGroupBase() {
val f_filterIndexed = fn("filterIndexed(predicate: (index: Int, T) -> Boolean)") {
includeDefault()
include(CharSequences, Strings)
include(CharSequences, Strings, ArraysOfUnsigned)
} builder {
inline()
specialFor(ArraysOfUnsigned) { inlineOnly() }
doc {
"""
@@ -627,9 +636,10 @@ object Filtering : TemplateGroupBase() {
val f_filterIndexedTo = fn("filterIndexedTo(destination: C, predicate: (index: Int, T) -> Boolean)") {
includeDefault()
include(CharSequences)
include(CharSequences, ArraysOfUnsigned)
} builder {
inline()
specialFor(ArraysOfUnsigned) { inlineOnly() }
doc {
"""
@@ -652,9 +662,10 @@ object Filtering : TemplateGroupBase() {
val f_filterNot = fn("filterNot(predicate: (T) -> Boolean)") {
includeDefault()
include(CharSequences, Strings)
include(CharSequences, Strings, ArraysOfUnsigned)
} builder {
inline()
specialFor(ArraysOfUnsigned) { inlineOnly() }
doc { "Returns a list containing all elements not matching the given [predicate]." }
returns("List<T>")
@@ -680,9 +691,10 @@ object Filtering : TemplateGroupBase() {
val f_filterNotTo = fn("filterNotTo(destination: C, predicate: (T) -> Boolean)") {
includeDefault()
include(CharSequences)
include(CharSequences, ArraysOfUnsigned)
} builder {
inline()
specialFor(ArraysOfUnsigned) { inlineOnly() }
doc { "Appends all elements not matching the given [predicate] to the given [destination]." }
typeParam("C : TCollection")