diff --git a/libraries/stdlib/common/src/generated/_UArrays.kt b/libraries/stdlib/common/src/generated/_UArrays.kt index c0b1c2c18f8..617103af758 100644 --- a/libraries/stdlib/common/src/generated/_UArrays.kt +++ b/libraries/stdlib/common/src/generated/_UArrays.kt @@ -215,6 +215,478 @@ public inline operator fun UShortArray.component5(): UShort { return get(4) } +/** + * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. + * + * @sample samples.collections.Collections.Elements.elementAt + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.elementAt(index: Int): UInt { + return get(index) +} + +/** + * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. + * + * @sample samples.collections.Collections.Elements.elementAt + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.elementAt(index: Int): ULong { + return get(index) +} + +/** + * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. + * + * @sample samples.collections.Collections.Elements.elementAt + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.elementAt(index: Int): UByte { + return get(index) +} + +/** + * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. + * + * @sample samples.collections.Collections.Elements.elementAt + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.elementAt(index: Int): UShort { + return get(index) +} + +/** + * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. + * + * @sample samples.collections.Collections.Elements.elementAtOrElse + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.elementAtOrElse(index: Int, defaultValue: (Int) -> UInt): UInt { + return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) +} + +/** + * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. + * + * @sample samples.collections.Collections.Elements.elementAtOrElse + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.elementAtOrElse(index: Int, defaultValue: (Int) -> ULong): ULong { + return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) +} + +/** + * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. + * + * @sample samples.collections.Collections.Elements.elementAtOrElse + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.elementAtOrElse(index: Int, defaultValue: (Int) -> UByte): UByte { + return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) +} + +/** + * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. + * + * @sample samples.collections.Collections.Elements.elementAtOrElse + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.elementAtOrElse(index: Int, defaultValue: (Int) -> UShort): UShort { + return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) +} + +/** + * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. + * + * @sample samples.collections.Collections.Elements.elementAtOrNull + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.elementAtOrNull(index: Int): UInt? { + return this.getOrNull(index) +} + +/** + * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. + * + * @sample samples.collections.Collections.Elements.elementAtOrNull + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.elementAtOrNull(index: Int): ULong? { + return this.getOrNull(index) +} + +/** + * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. + * + * @sample samples.collections.Collections.Elements.elementAtOrNull + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.elementAtOrNull(index: Int): UByte? { + return this.getOrNull(index) +} + +/** + * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. + * + * @sample samples.collections.Collections.Elements.elementAtOrNull + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.elementAtOrNull(index: Int): UShort? { + return this.getOrNull(index) +} + +/** + * Returns the first element matching the given [predicate], or `null` if no such element was found. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.find(predicate: (UInt) -> Boolean): UInt? { + return firstOrNull(predicate) +} + +/** + * Returns the first element matching the given [predicate], or `null` if no such element was found. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.find(predicate: (ULong) -> Boolean): ULong? { + return firstOrNull(predicate) +} + +/** + * Returns the first element matching the given [predicate], or `null` if no such element was found. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.find(predicate: (UByte) -> Boolean): UByte? { + return firstOrNull(predicate) +} + +/** + * Returns the first element matching the given [predicate], or `null` if no such element was found. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.find(predicate: (UShort) -> Boolean): UShort? { + return firstOrNull(predicate) +} + +/** + * Returns the last element matching the given [predicate], or `null` if no such element was found. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.findLast(predicate: (UInt) -> Boolean): UInt? { + return lastOrNull(predicate) +} + +/** + * Returns the last element matching the given [predicate], or `null` if no such element was found. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.findLast(predicate: (ULong) -> Boolean): ULong? { + return lastOrNull(predicate) +} + +/** + * Returns the last element matching the given [predicate], or `null` if no such element was found. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.findLast(predicate: (UByte) -> Boolean): UByte? { + return lastOrNull(predicate) +} + +/** + * Returns the last element matching the given [predicate], or `null` if no such element was found. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.findLast(predicate: (UShort) -> Boolean): UShort? { + return lastOrNull(predicate) +} + +/** + * Returns first element. + * @throws [NoSuchElementException] if the array is empty. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.first(): UInt { + return storage.first().toUInt() +} + +/** + * Returns first element. + * @throws [NoSuchElementException] if the array is empty. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.first(): ULong { + return storage.first().toULong() +} + +/** + * Returns first element. + * @throws [NoSuchElementException] if the array is empty. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.first(): UByte { + return storage.first().toUByte() +} + +/** + * Returns first element. + * @throws [NoSuchElementException] if the array is empty. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.first(): UShort { + return storage.first().toUShort() +} + +/** + * Returns the first element matching the given [predicate]. + * @throws [NoSuchElementException] if no such element is found. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.first(predicate: (UInt) -> Boolean): UInt { + for (element in this) if (predicate(element)) return element + throw NoSuchElementException("Array contains no element matching the predicate.") +} + +/** + * Returns the first element matching the given [predicate]. + * @throws [NoSuchElementException] if no such element is found. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.first(predicate: (ULong) -> Boolean): ULong { + for (element in this) if (predicate(element)) return element + throw NoSuchElementException("Array contains no element matching the predicate.") +} + +/** + * Returns the first element matching the given [predicate]. + * @throws [NoSuchElementException] if no such element is found. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.first(predicate: (UByte) -> Boolean): UByte { + for (element in this) if (predicate(element)) return element + throw NoSuchElementException("Array contains no element matching the predicate.") +} + +/** + * Returns the first element matching the given [predicate]. + * @throws [NoSuchElementException] if no such element is found. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.first(predicate: (UShort) -> Boolean): UShort { + for (element in this) if (predicate(element)) return element + throw NoSuchElementException("Array contains no element matching the predicate.") +} + +/** + * Returns the first element, or `null` if the array is empty. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun UIntArray.firstOrNull(): UInt? { + return if (isEmpty()) null else this[0] +} + +/** + * Returns the first element, or `null` if the array is empty. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun ULongArray.firstOrNull(): ULong? { + return if (isEmpty()) null else this[0] +} + +/** + * Returns the first element, or `null` if the array is empty. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun UByteArray.firstOrNull(): UByte? { + return if (isEmpty()) null else this[0] +} + +/** + * Returns the first element, or `null` if the array is empty. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun UShortArray.firstOrNull(): UShort? { + return if (isEmpty()) null else this[0] +} + +/** + * Returns the first element matching the given [predicate], or `null` if element was not found. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.firstOrNull(predicate: (UInt) -> Boolean): UInt? { + for (element in this) if (predicate(element)) return element + return null +} + +/** + * Returns the first element matching the given [predicate], or `null` if element was not found. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.firstOrNull(predicate: (ULong) -> Boolean): ULong? { + for (element in this) if (predicate(element)) return element + return null +} + +/** + * Returns the first element matching the given [predicate], or `null` if element was not found. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.firstOrNull(predicate: (UByte) -> Boolean): UByte? { + for (element in this) if (predicate(element)) return element + return null +} + +/** + * Returns the first element matching the given [predicate], or `null` if element was not found. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.firstOrNull(predicate: (UShort) -> Boolean): UShort? { + for (element in this) if (predicate(element)) return element + return null +} + +/** + * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.getOrElse(index: Int, defaultValue: (Int) -> UInt): UInt { + return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) +} + +/** + * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.getOrElse(index: Int, defaultValue: (Int) -> ULong): ULong { + return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) +} + +/** + * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.getOrElse(index: Int, defaultValue: (Int) -> UByte): UByte { + return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) +} + +/** + * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.getOrElse(index: Int, defaultValue: (Int) -> UShort): UShort { + return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) +} + +/** + * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun UIntArray.getOrNull(index: Int): UInt? { + return if (index >= 0 && index <= lastIndex) get(index) else null +} + +/** + * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun ULongArray.getOrNull(index: Int): ULong? { + return if (index >= 0 && index <= lastIndex) get(index) else null +} + +/** + * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun UByteArray.getOrNull(index: Int): UByte? { + return if (index >= 0 && index <= lastIndex) get(index) else null +} + +/** + * Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun UShortArray.getOrNull(index: Int): UShort? { + return if (index >= 0 && index <= lastIndex) get(index) else null +} + /** * Returns first index of [element], or -1 if the array does not contain element. */ @@ -335,6 +807,110 @@ public inline fun UShortArray.indexOfLast(predicate: (UShort) -> Boolean): Int { return storage.indexOfLast { predicate(it.toUShort()) } } +/** + * Returns the last element. + * @throws [NoSuchElementException] if the array is empty. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.last(): UInt { + return storage.last().toUInt() +} + +/** + * Returns the last element. + * @throws [NoSuchElementException] if the array is empty. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.last(): ULong { + return storage.last().toULong() +} + +/** + * Returns the last element. + * @throws [NoSuchElementException] if the array is empty. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.last(): UByte { + return storage.last().toUByte() +} + +/** + * Returns the last element. + * @throws [NoSuchElementException] if the array is empty. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.last(): UShort { + return storage.last().toUShort() +} + +/** + * Returns the last element matching the given [predicate]. + * @throws [NoSuchElementException] if no such element is found. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.last(predicate: (UInt) -> Boolean): UInt { + for (index in this.indices.reversed()) { + val element = this[index] + if (predicate(element)) return element + } + throw NoSuchElementException("Array contains no element matching the predicate.") +} + +/** + * Returns the last element matching the given [predicate]. + * @throws [NoSuchElementException] if no such element is found. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.last(predicate: (ULong) -> Boolean): ULong { + for (index in this.indices.reversed()) { + val element = this[index] + if (predicate(element)) return element + } + throw NoSuchElementException("Array contains no element matching the predicate.") +} + +/** + * Returns the last element matching the given [predicate]. + * @throws [NoSuchElementException] if no such element is found. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.last(predicate: (UByte) -> Boolean): UByte { + for (index in this.indices.reversed()) { + val element = this[index] + if (predicate(element)) return element + } + throw NoSuchElementException("Array contains no element matching the predicate.") +} + +/** + * Returns the last element matching the given [predicate]. + * @throws [NoSuchElementException] if no such element is found. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.last(predicate: (UShort) -> Boolean): UShort { + for (index in this.indices.reversed()) { + val element = this[index] + if (predicate(element)) return element + } + throw NoSuchElementException("Array contains no element matching the predicate.") +} + /** * Returns last index of [element], or -1 if the array does not contain element. */ @@ -375,6 +951,98 @@ public inline fun UShortArray.lastIndexOf(element: UShort): Int { return storage.lastIndexOf(element.toShort()) } +/** + * Returns the last element, or `null` if the array is empty. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun UIntArray.lastOrNull(): UInt? { + return if (isEmpty()) null else this[size - 1] +} + +/** + * Returns the last element, or `null` if the array is empty. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun ULongArray.lastOrNull(): ULong? { + return if (isEmpty()) null else this[size - 1] +} + +/** + * Returns the last element, or `null` if the array is empty. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun UByteArray.lastOrNull(): UByte? { + return if (isEmpty()) null else this[size - 1] +} + +/** + * Returns the last element, or `null` if the array is empty. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun UShortArray.lastOrNull(): UShort? { + return if (isEmpty()) null else this[size - 1] +} + +/** + * Returns the last element matching the given [predicate], or `null` if no such element was found. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.lastOrNull(predicate: (UInt) -> Boolean): UInt? { + for (index in this.indices.reversed()) { + val element = this[index] + if (predicate(element)) return element + } + return null +} + +/** + * Returns the last element matching the given [predicate], or `null` if no such element was found. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.lastOrNull(predicate: (ULong) -> Boolean): ULong? { + for (index in this.indices.reversed()) { + val element = this[index] + if (predicate(element)) return element + } + return null +} + +/** + * Returns the last element matching the given [predicate], or `null` if no such element was found. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.lastOrNull(predicate: (UByte) -> Boolean): UByte? { + for (index in this.indices.reversed()) { + val element = this[index] + if (predicate(element)) return element + } + return null +} + +/** + * Returns the last element matching the given [predicate], or `null` if no such element was found. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.lastOrNull(predicate: (UShort) -> Boolean): UShort? { + for (index in this.indices.reversed()) { + val element = this[index] + if (predicate(element)) return element + } + return null +} + /** * Returns a random element from this array. * @@ -475,6 +1143,246 @@ public fun UShortArray.random(random: Random): UShort { return get(random.nextInt(size)) } +/** + * Returns the single element, or throws an exception if the array is empty or has more than one element. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.single(): UInt { + return storage.single().toUInt() +} + +/** + * Returns the single element, or throws an exception if the array is empty or has more than one element. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.single(): ULong { + return storage.single().toULong() +} + +/** + * Returns the single element, or throws an exception if the array is empty or has more than one element. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.single(): UByte { + return storage.single().toUByte() +} + +/** + * Returns the single element, or throws an exception if the array is empty or has more than one element. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.single(): UShort { + return storage.single().toUShort() +} + +/** + * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.single(predicate: (UInt) -> Boolean): UInt { + var single: UInt? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) throw IllegalArgumentException("Array contains more than one matching element.") + single = element + found = true + } + } + if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") + @Suppress("UNCHECKED_CAST") + return single as UInt +} + +/** + * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.single(predicate: (ULong) -> Boolean): ULong { + var single: ULong? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) throw IllegalArgumentException("Array contains more than one matching element.") + single = element + found = true + } + } + if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") + @Suppress("UNCHECKED_CAST") + return single as ULong +} + +/** + * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.single(predicate: (UByte) -> Boolean): UByte { + var single: UByte? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) throw IllegalArgumentException("Array contains more than one matching element.") + single = element + found = true + } + } + if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") + @Suppress("UNCHECKED_CAST") + return single as UByte +} + +/** + * Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.single(predicate: (UShort) -> Boolean): UShort { + var single: UShort? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) throw IllegalArgumentException("Array contains more than one matching element.") + single = element + found = true + } + } + if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") + @Suppress("UNCHECKED_CAST") + return single as UShort +} + +/** + * Returns single element, or `null` if the array is empty or has more than one element. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun UIntArray.singleOrNull(): UInt? { + return if (size == 1) this[0] else null +} + +/** + * Returns single element, or `null` if the array is empty or has more than one element. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun ULongArray.singleOrNull(): ULong? { + return if (size == 1) this[0] else null +} + +/** + * Returns single element, or `null` if the array is empty or has more than one element. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun UByteArray.singleOrNull(): UByte? { + return if (size == 1) this[0] else null +} + +/** + * Returns single element, or `null` if the array is empty or has more than one element. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun UShortArray.singleOrNull(): UShort? { + return if (size == 1) this[0] else null +} + +/** + * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.singleOrNull(predicate: (UInt) -> Boolean): UInt? { + var single: UInt? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) return null + single = element + found = true + } + } + if (!found) return null + return single +} + +/** + * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.singleOrNull(predicate: (ULong) -> Boolean): ULong? { + var single: ULong? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) return null + single = element + found = true + } + } + if (!found) return null + return single +} + +/** + * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.singleOrNull(predicate: (UByte) -> Boolean): UByte? { + var single: UByte? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) return null + single = element + found = true + } + } + if (!found) return null + return single +} + +/** + * Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.singleOrNull(predicate: (UShort) -> Boolean): UShort? { + var single: UShort? = null + var found = false + for (element in this) { + if (predicate(element)) { + if (found) return null + single = element + found = true + } + } + if (!found) return null + return single +} + /** * Returns an array of type [ByteArray], which is a view of this array where each element is a signed reinterpretation * of the corresponding element of this array. @@ -1297,7 +2205,7 @@ public inline fun ShortArray.toUShortArray(): UShortArray { /** * Returns `true` if all elements match the given [predicate]. - * + * * @sample samples.collections.Collections.Aggregates.all */ @SinceKotlin("1.3") @@ -1310,7 +2218,7 @@ public inline fun UIntArray.all(predicate: (UInt) -> Boolean): Boolean { /** * Returns `true` if all elements match the given [predicate]. - * + * * @sample samples.collections.Collections.Aggregates.all */ @SinceKotlin("1.3") @@ -1323,7 +2231,7 @@ public inline fun ULongArray.all(predicate: (ULong) -> Boolean): Boolean { /** * Returns `true` if all elements match the given [predicate]. - * + * * @sample samples.collections.Collections.Aggregates.all */ @SinceKotlin("1.3") @@ -1336,7 +2244,7 @@ public inline fun UByteArray.all(predicate: (UByte) -> Boolean): Boolean { /** * Returns `true` if all elements match the given [predicate]. - * + * * @sample samples.collections.Collections.Aggregates.all */ @SinceKotlin("1.3") @@ -1349,7 +2257,7 @@ public inline fun UShortArray.all(predicate: (UShort) -> Boolean): Boolean { /** * Returns `true` if array has at least one element. - * + * * @sample samples.collections.Collections.Aggregates.any */ @SinceKotlin("1.3") @@ -1361,7 +2269,7 @@ public inline fun UIntArray.any(): Boolean { /** * Returns `true` if array has at least one element. - * + * * @sample samples.collections.Collections.Aggregates.any */ @SinceKotlin("1.3") @@ -1373,7 +2281,7 @@ public inline fun ULongArray.any(): Boolean { /** * Returns `true` if array has at least one element. - * + * * @sample samples.collections.Collections.Aggregates.any */ @SinceKotlin("1.3") @@ -1385,7 +2293,7 @@ public inline fun UByteArray.any(): Boolean { /** * Returns `true` if array has at least one element. - * + * * @sample samples.collections.Collections.Aggregates.any */ @SinceKotlin("1.3") @@ -1397,7 +2305,7 @@ public inline fun UShortArray.any(): Boolean { /** * Returns `true` if at least one element matches the given [predicate]. - * + * * @sample samples.collections.Collections.Aggregates.anyWithPredicate */ @SinceKotlin("1.3") @@ -1410,7 +2318,7 @@ public inline fun UIntArray.any(predicate: (UInt) -> Boolean): Boolean { /** * Returns `true` if at least one element matches the given [predicate]. - * + * * @sample samples.collections.Collections.Aggregates.anyWithPredicate */ @SinceKotlin("1.3") @@ -1423,7 +2331,7 @@ public inline fun ULongArray.any(predicate: (ULong) -> Boolean): Boolean { /** * Returns `true` if at least one element matches the given [predicate]. - * + * * @sample samples.collections.Collections.Aggregates.anyWithPredicate */ @SinceKotlin("1.3") @@ -1436,7 +2344,7 @@ public inline fun UByteArray.any(predicate: (UByte) -> Boolean): Boolean { /** * Returns `true` if at least one element matches the given [predicate]. - * + * * @sample samples.collections.Collections.Aggregates.anyWithPredicate */ @SinceKotlin("1.3") @@ -1497,7 +2405,7 @@ public inline fun UShortArray.count(predicate: (UShort) -> Boolean): Int { /** * Returns `true` if the array has no elements. - * + * * @sample samples.collections.Collections.Aggregates.none */ @SinceKotlin("1.3") @@ -1509,7 +2417,7 @@ public inline fun UIntArray.none(): Boolean { /** * Returns `true` if the array has no elements. - * + * * @sample samples.collections.Collections.Aggregates.none */ @SinceKotlin("1.3") @@ -1521,7 +2429,7 @@ public inline fun ULongArray.none(): Boolean { /** * Returns `true` if the array has no elements. - * + * * @sample samples.collections.Collections.Aggregates.none */ @SinceKotlin("1.3") @@ -1533,7 +2441,7 @@ public inline fun UByteArray.none(): Boolean { /** * Returns `true` if the array has no elements. - * + * * @sample samples.collections.Collections.Aggregates.none */ @SinceKotlin("1.3") @@ -1545,7 +2453,7 @@ public inline fun UShortArray.none(): Boolean { /** * Returns `true` if no elements match the given [predicate]. - * + * * @sample samples.collections.Collections.Aggregates.noneWithPredicate */ @SinceKotlin("1.3") @@ -1558,7 +2466,7 @@ public inline fun UIntArray.none(predicate: (UInt) -> Boolean): Boolean { /** * Returns `true` if no elements match the given [predicate]. - * + * * @sample samples.collections.Collections.Aggregates.noneWithPredicate */ @SinceKotlin("1.3") @@ -1571,7 +2479,7 @@ public inline fun ULongArray.none(predicate: (ULong) -> Boolean): Boolean { /** * Returns `true` if no elements match the given [predicate]. - * + * * @sample samples.collections.Collections.Aggregates.noneWithPredicate */ @SinceKotlin("1.3") @@ -1584,7 +2492,7 @@ public inline fun UByteArray.none(predicate: (UByte) -> Boolean): Boolean { /** * Returns `true` if no elements match the given [predicate]. - * + * * @sample samples.collections.Collections.Aggregates.noneWithPredicate */ @SinceKotlin("1.3") diff --git a/libraries/stdlib/jvm/src/generated/_UArraysJvm.kt b/libraries/stdlib/jvm/src/generated/_UArraysJvm.kt index 89cde43e0f9..6a4ccd19d6c 100644 --- a/libraries/stdlib/jvm/src/generated/_UArraysJvm.kt +++ b/libraries/stdlib/jvm/src/generated/_UArraysJvm.kt @@ -17,9 +17,9 @@ package kotlin.collections /** * Searches the array or the range of the array for the provided [element] using the binary search algorithm. * The array is expected to be sorted, otherwise the result is undefined. - * + * * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. - * + * * @return the index of the element, if it is contained in the array within the specified range; * otherwise, the inverted insertion point `(-insertion point - 1)`. * The insertion point is defined as the index at which the element should be inserted, @@ -49,9 +49,9 @@ public fun UIntArray.binarySearch(element: UInt, fromIndex: Int = 0, toIndex: In /** * Searches the array or the range of the array for the provided [element] using the binary search algorithm. * The array is expected to be sorted, otherwise the result is undefined. - * + * * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. - * + * * @return the index of the element, if it is contained in the array within the specified range; * otherwise, the inverted insertion point `(-insertion point - 1)`. * The insertion point is defined as the index at which the element should be inserted, @@ -81,9 +81,9 @@ public fun ULongArray.binarySearch(element: ULong, fromIndex: Int = 0, toIndex: /** * Searches the array or the range of the array for the provided [element] using the binary search algorithm. * The array is expected to be sorted, otherwise the result is undefined. - * + * * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. - * + * * @return the index of the element, if it is contained in the array within the specified range; * otherwise, the inverted insertion point `(-insertion point - 1)`. * The insertion point is defined as the index at which the element should be inserted, @@ -113,9 +113,9 @@ public fun UByteArray.binarySearch(element: UByte, fromIndex: Int = 0, toIndex: /** * Searches the array or the range of the array for the provided [element] using the binary search algorithm. * The array is expected to be sorted, otherwise the result is undefined. - * + * * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. - * + * * @return the index of the element, if it is contained in the array within the specified range; * otherwise, the inverted insertion point `(-insertion point - 1)`. * The insertion point is defined as the index at which the element should be inserted, @@ -176,4 +176,5 @@ public fun UByteArray.fill(element: UByte, fromIndex: Int = 0, toIndex: Int = si @ExperimentalUnsignedTypes public fun UShortArray.fill(element: UShort, fromIndex: Int = 0, toIndex: Int = size): Unit { storage.fill(element.toShort(), fromIndex, toIndex) -} \ No newline at end of file +} + diff --git a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt index 67ee17b87be..f90b6579e04 100644 --- a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt +++ b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt @@ -2315,6 +2315,18 @@ public final class kotlin/collections/UArraysKt { public static synthetic fun fill-K6DWlUc$default ([JJIIILjava/lang/Object;)V public static final fun fill-WpHrYlw ([BBII)V public static synthetic fun fill-WpHrYlw$default ([BBIIILjava/lang/Object;)V + public static final fun firstOrNull--ajY-9A ([I)Lkotlin/UInt; + public static final fun firstOrNull-GBYM_sE ([B)Lkotlin/UByte; + public static final fun firstOrNull-QwZRm1k ([J)Lkotlin/ULong; + public static final fun firstOrNull-rL5Bavg ([S)Lkotlin/UShort; + public static final fun getOrNull-PpDY95g ([BI)Lkotlin/UByte; + public static final fun getOrNull-nggk6HY ([SI)Lkotlin/UShort; + public static final fun getOrNull-qFRl0hI ([II)Lkotlin/UInt; + public static final fun getOrNull-r7IrZao ([JI)Lkotlin/ULong; + public static final fun lastOrNull--ajY-9A ([I)Lkotlin/UInt; + public static final fun lastOrNull-GBYM_sE ([B)Lkotlin/UByte; + public static final fun lastOrNull-QwZRm1k ([J)Lkotlin/ULong; + public static final fun lastOrNull-rL5Bavg ([S)Lkotlin/UShort; public static final fun plus-CFIt9YE ([ILjava/util/Collection;)[I public static final fun plus-kzHmqpY ([JLjava/util/Collection;)[J public static final fun plus-ojwP5H8 ([SLjava/util/Collection;)[S @@ -2323,6 +2335,10 @@ public final class kotlin/collections/UArraysKt { public static final fun random-JzugnMA ([JLkotlin/random/Random;)J public static final fun random-oSF2wD8 ([BLkotlin/random/Random;)B public static final fun random-s5X_as8 ([SLkotlin/random/Random;)S + public static final fun singleOrNull--ajY-9A ([I)Lkotlin/UInt; + public static final fun singleOrNull-GBYM_sE ([B)Lkotlin/UByte; + public static final fun singleOrNull-QwZRm1k ([J)Lkotlin/ULong; + public static final fun singleOrNull-rL5Bavg ([S)Lkotlin/UShort; 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; diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt index a94d9d3161f..0db30d377ad 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt @@ -260,7 +260,7 @@ object Elements : TemplateGroupBase() { val f_elementAt = fn("elementAt(index: Int)") { includeDefault() - include(CharSequences, Lists) + include(CharSequences, Lists, ArraysOfUnsigned) } builder { val index = '$' + "index" doc { "Returns ${f.element.prefixWithArticle()} at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this ${f.collection}." } @@ -280,7 +280,7 @@ object Elements : TemplateGroupBase() { """ } - specialFor(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) { + specialFor(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) { inlineOnly() body { "return get(index)" } } @@ -288,7 +288,7 @@ object Elements : TemplateGroupBase() { val f_elementAtOrElse = fn("elementAtOrElse(index: Int, defaultValue: (Int) -> T)") { includeDefault() - include(CharSequences, Lists) + include(CharSequences, Lists, ArraysOfUnsigned) } builder { doc { "Returns ${f.element.prefixWithArticle()} at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this ${f.collection}." } sample("samples.collections.Collections.Elements.elementAtOrElse") @@ -323,7 +323,7 @@ object Elements : TemplateGroupBase() { return defaultValue(index) """ } - specialFor(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) { + specialFor(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) { inlineOnly() body { """ @@ -334,7 +334,7 @@ object Elements : TemplateGroupBase() { } val f_getOrElse = fn("getOrElse(index: Int, defaultValue: (Int) -> T)") { - include(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) + include(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) } builder { doc { "Returns ${f.element.prefixWithArticle()} at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this ${f.collection}." } returns("T") @@ -348,7 +348,7 @@ object Elements : TemplateGroupBase() { val f_elementAtOrNull = fn("elementAtOrNull(index: Int)") { includeDefault() - include(CharSequences, Lists) + include(CharSequences, Lists, ArraysOfUnsigned) } builder { doc { "Returns ${f.element.prefixWithArticle()} at the given [index] or `null` if the [index] is out of bounds of this ${f.collection}." } sample("samples.collections.Collections.Elements.elementAtOrNull") @@ -383,14 +383,14 @@ object Elements : TemplateGroupBase() { return null """ } - specialFor(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) { + specialFor(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) { inlineOnly() body { "return this.getOrNull(index)" } } } val f_getOrNull = fn("getOrNull(index: Int)") { - include(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) + include(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) } builder { doc { "Returns ${f.element.prefixWithArticle()} at the given [index] or `null` if the [index] is out of bounds of this ${f.collection}." } returns("T?") @@ -403,7 +403,7 @@ object Elements : TemplateGroupBase() { val f_first = fn("first()") { includeDefault() - include(CharSequences, Lists) + include(CharSequences, Lists, ArraysOfUnsigned) } builder { doc { """Returns first ${f.element}. @throws [NoSuchElementException] if the ${f.collection} is empty. @@ -437,11 +437,16 @@ object Elements : TemplateGroupBase() { return iterator.next() """ } + + specialFor(ArraysOfUnsigned) { + inlineOnly() + body { "return storage.first().to${primitive!!.name}()" } + } } val f_firstOrNull = fn("firstOrNull()") { includeDefault() - include(CharSequences, Lists) + include(CharSequences, Lists, ArraysOfUnsigned) } builder { doc { "Returns the first ${f.element}, or `null` if the ${f.collection} is empty." } returns("T?") @@ -463,7 +468,7 @@ object Elements : TemplateGroupBase() { } """ } - body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) { + body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) { """ return if (isEmpty()) null else this[0] """ @@ -480,12 +485,15 @@ object Elements : TemplateGroupBase() { val f_first_predicate = fn("first(predicate: (T) -> Boolean)") { includeDefault() - include(CharSequences) + include(CharSequences, ArraysOfUnsigned) } builder { inline() + specialFor(ArraysOfUnsigned) { inlineOnly() } + doc { """Returns the first ${f.element} matching the given [predicate]. @throws [NoSuchElementException] if no such ${f.element} is found.""" } returns("T") + body { """ for (element in this) if (predicate(element)) return element @@ -496,9 +504,10 @@ object Elements : TemplateGroupBase() { val f_firstOrNull_predicate = fn("firstOrNull(predicate: (T) -> Boolean)") { includeDefault() - include(CharSequences) + include(CharSequences, ArraysOfUnsigned) } builder { inline() + specialFor(ArraysOfUnsigned) { inlineOnly() } doc { "Returns the first ${f.element} matching the given [predicate], or `null` if ${f.element} was not found." } returns("T?") @@ -512,7 +521,7 @@ object Elements : TemplateGroupBase() { val f_find = fn("find(predicate: (T) -> Boolean)") { includeDefault() - include(CharSequences) + include(CharSequences, ArraysOfUnsigned) } builder { inline(Inline.Only) doc { "Returns the first ${f.element} matching the given [predicate], or `null` if no such ${f.element} was found." } @@ -523,7 +532,7 @@ object Elements : TemplateGroupBase() { val f_last = fn("last()") { includeDefault() - include(CharSequences, Lists) + include(CharSequences, Lists, ArraysOfUnsigned) } builder { doc { """Returns the last ${f.element}. @throws [NoSuchElementException] if the ${f.collection} is empty.""" } @@ -562,11 +571,16 @@ object Elements : TemplateGroupBase() { return this[lastIndex] """ } + + specialFor(ArraysOfUnsigned) { + inlineOnly() + body { "return storage.last().to${primitive!!.name}()" } + } } val f_lastOrNull = fn("lastOrNull()") { includeDefault() - include(Lists, CharSequences) + include(Lists, CharSequences, ArraysOfUnsigned) } builder { doc { "Returns the last ${f.element}, or `null` if the ${f.collection} is empty." } returns("T?") @@ -602,7 +616,7 @@ object Elements : TemplateGroupBase() { return if (isEmpty()) null else this[length - 1] """ } - body(Lists, ArraysOfObjects, ArraysOfPrimitives) { + body(Lists, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) { """ return if (isEmpty()) null else this[size - 1] """ @@ -611,9 +625,10 @@ object Elements : TemplateGroupBase() { val f_last_predicate = fn("last(predicate: (T) -> Boolean)") { includeDefault() - include(Lists, CharSequences) + include(Lists, CharSequences, ArraysOfUnsigned) } builder { inline() + specialFor(ArraysOfUnsigned) { inlineOnly() } doc { """Returns the last ${f.element} matching the given [predicate]. @throws [NoSuchElementException] if no such ${f.element} is found.""" } @@ -634,7 +649,7 @@ object Elements : TemplateGroupBase() { """ } - body(CharSequences, ArraysOfPrimitives, ArraysOfObjects) { + body(CharSequences, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) { """ for (index in this.indices.reversed()) { val element = this[index] @@ -657,9 +672,11 @@ object Elements : TemplateGroupBase() { val f_lastOrNull_predicate = fn("lastOrNull(predicate: (T) -> Boolean)") { includeDefault() - include(Lists, CharSequences) + include(Lists, CharSequences, ArraysOfUnsigned) } builder { inline() + specialFor(ArraysOfUnsigned) { inlineOnly() } + doc { "Returns the last ${f.element} matching the given [predicate], or `null` if no such ${f.element} was found." } returns("T?") body { @@ -674,7 +691,7 @@ object Elements : TemplateGroupBase() { """ } - body(CharSequences, ArraysOfPrimitives, ArraysOfObjects) { + body(CharSequences, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) { """ for (index in this.indices.reversed()) { val element = this[index] @@ -698,7 +715,7 @@ object Elements : TemplateGroupBase() { val f_findLast = fn("findLast(predicate: (T) -> Boolean)") { includeDefault() - include(Lists, CharSequences) + include(Lists, CharSequences, ArraysOfUnsigned) } builder { inline(Inline.Only) doc { "Returns the last ${f.element} matching the given [predicate], or `null` if no such ${f.element} was found." } @@ -708,7 +725,7 @@ object Elements : TemplateGroupBase() { val f_single = fn("single()") { includeDefault() - include(Lists, CharSequences) + include(Lists, CharSequences, ArraysOfUnsigned) } builder { doc { "Returns the single ${f.element}, or throws an exception if the ${f.collection} is empty or has more than one ${f.element}." } returns("T") @@ -748,11 +765,16 @@ object Elements : TemplateGroupBase() { } """ } + + specialFor(ArraysOfUnsigned) { + inlineOnly() + body { "return storage.single().to${primitive!!.name}()" } + } } val f_singleOrNull = fn("singleOrNull()") { includeDefault() - include(Lists, CharSequences) + include(Lists, CharSequences, ArraysOfUnsigned) } builder { doc { "Returns single ${f.element}, or `null` if the ${f.collection} is empty or has more than one ${f.element}." } returns("T?") @@ -788,7 +810,7 @@ object Elements : TemplateGroupBase() { return if (length == 1) this[0] else null """ } - body(Lists, ArraysOfObjects, ArraysOfPrimitives) { + body(Lists, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) { """ return if (size == 1) this[0] else null """ @@ -797,11 +819,14 @@ object Elements : TemplateGroupBase() { val f_single_predicate = fn("single(predicate: (T) -> Boolean)") { includeDefault() - include(CharSequences) + include(CharSequences, ArraysOfUnsigned) } builder { inline() + specialFor(ArraysOfUnsigned) { inlineOnly() } + doc { "Returns the single ${f.element} matching the given [predicate], or throws exception if there is no or more than one matching ${f.element}." } returns("T") + body { """ var single: T? = null @@ -822,11 +847,14 @@ object Elements : TemplateGroupBase() { val f_singleOrNull_predicate = fn("singleOrNull(predicate: (T) -> Boolean)") { includeDefault() - include(CharSequences) + include(CharSequences, ArraysOfUnsigned) } builder { inline() + specialFor(ArraysOfUnsigned) { inlineOnly() } + doc { "Returns the single ${f.element} matching the given [predicate], or `null` if ${f.element} was not found or more than one ${f.element} was found." } returns("T?") + body { """ var single: T? = null diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/dsl/FamilyProperties.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/dsl/FamilyProperties.kt index 4bdb0053ab8..70ed8014c6b 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/dsl/FamilyProperties.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/dsl/FamilyProperties.kt @@ -15,7 +15,7 @@ val Family.DocExtension.element: String val Family.CodeExtension.size: String get() = when (family) { - Iterables, Collections, Lists, Sets, Maps, InvariantArraysOfObjects, ArraysOfObjects, ArraysOfPrimitives -> "size" + Iterables, Collections, Lists, Sets, Maps, InvariantArraysOfObjects, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned -> "size" CharSequences, Strings -> "length" else -> error("size property isn't supported for $family") }