Implement first, last & single extension functions for UArrays
This commit is contained in:
committed by
Ilya Gorbunov
parent
299fac8e2d
commit
92cd84682c
File diff suppressed because it is too large
Load Diff
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+16
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user