Implement asList, slice & sliceArray extension functions for UArrays
This commit is contained in:
committed by
Ilya Gorbunov
parent
114736c09b
commit
fc85781bfc
@@ -1147,7 +1147,7 @@ object ArrayOps : TemplateGroupBase() {
|
||||
|
||||
|
||||
val f_asList = fn("asList()") {
|
||||
include(ArraysOfObjects, ArraysOfPrimitives)
|
||||
include(ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned)
|
||||
} builder {
|
||||
doc { "Returns a [List] that wraps the original array." }
|
||||
returns("List<T>")
|
||||
@@ -1168,13 +1168,33 @@ object ArrayOps : TemplateGroupBase() {
|
||||
override fun lastIndexOf(element: T): Int = this@asList.lastIndexOf(element)
|
||||
}
|
||||
"""
|
||||
specialFor(ArraysOfPrimitives) {
|
||||
specialFor(ArraysOfPrimitives, ArraysOfUnsigned) {
|
||||
on(Platform.JVM) {
|
||||
body { objectLiteralImpl }
|
||||
}
|
||||
on(Platform.JS) {
|
||||
if (primitive == PrimitiveType.Char) {
|
||||
body { objectLiteralImpl }
|
||||
if (primitive == PrimitiveType.Char || primitive in PrimitiveType.unsignedPrimitives) {
|
||||
body {
|
||||
"""
|
||||
return object : AbstractList<T>(), RandomAccess {
|
||||
override val size: Int get() = this@asList.size
|
||||
override fun isEmpty(): Boolean = this@asList.isEmpty()
|
||||
override fun contains(element: T): Boolean = this@asList.contains(element)
|
||||
override fun get(index: Int): T {
|
||||
AbstractList.checkElementIndex(index, size)
|
||||
return this@asList[index]
|
||||
}
|
||||
override fun indexOf(element: T): Int {
|
||||
if ((element as Any?) !is T) return -1
|
||||
return this@asList.indexOf(element)
|
||||
}
|
||||
override fun lastIndexOf(element: T): Int {
|
||||
if ((element as Any?) !is T) return -1
|
||||
return this@asList.lastIndexOf(element)
|
||||
}
|
||||
}
|
||||
"""
|
||||
}
|
||||
}
|
||||
else {
|
||||
inlineOnly()
|
||||
|
||||
@@ -17,6 +17,11 @@ object Filtering : TemplateGroupBase() {
|
||||
sequenceClassification(terminal)
|
||||
else
|
||||
sequenceClassification(intermediate, stateless)
|
||||
|
||||
specialFor(ArraysOfUnsigned) {
|
||||
since("1.3")
|
||||
annotation("@ExperimentalUnsignedTypes")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -832,7 +837,7 @@ object Filtering : TemplateGroupBase() {
|
||||
|
||||
|
||||
val f_slice = fn("slice(indices: Iterable<Int>)") {
|
||||
include(CharSequences, Strings, Lists, ArraysOfObjects, ArraysOfPrimitives)
|
||||
include(CharSequences, Strings, Lists, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned)
|
||||
} builder {
|
||||
doc { "Returns a list containing elements at specified [indices]." }
|
||||
returns("List<T>")
|
||||
@@ -870,7 +875,7 @@ object Filtering : TemplateGroupBase() {
|
||||
}
|
||||
|
||||
val f_slice_range = fn("slice(indices: IntRange)") {
|
||||
include(CharSequences, Strings, Lists, ArraysOfObjects, ArraysOfPrimitives)
|
||||
include(CharSequences, Strings, Lists, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned)
|
||||
} builder {
|
||||
doc { "Returns a list containing elements at indices in the specified [indices] range." }
|
||||
returns("List<T>")
|
||||
@@ -880,7 +885,7 @@ object Filtering : TemplateGroupBase() {
|
||||
return this.subList(indices.start, indices.endInclusive + 1).toList()
|
||||
"""
|
||||
}
|
||||
body(ArraysOfPrimitives, ArraysOfObjects) {
|
||||
body(ArraysOfPrimitives, ArraysOfObjects, ArraysOfUnsigned) {
|
||||
"""
|
||||
if (indices.isEmpty()) return listOf()
|
||||
return copyOfRange(indices.start, indices.endInclusive + 1).asList()
|
||||
@@ -900,7 +905,7 @@ object Filtering : TemplateGroupBase() {
|
||||
}
|
||||
|
||||
val f_sliceArray = fn("sliceArray(indices: Collection<Int>)") {
|
||||
include(InvariantArraysOfObjects, ArraysOfPrimitives)
|
||||
include(InvariantArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned)
|
||||
} builder {
|
||||
doc { "Returns an array containing elements of this array at specified [indices]." }
|
||||
returns("SELF")
|
||||
@@ -924,12 +929,17 @@ object Filtering : TemplateGroupBase() {
|
||||
return result
|
||||
"""
|
||||
}
|
||||
body(ArraysOfUnsigned) {
|
||||
"""
|
||||
return SELF(storage.sliceArray(indices))
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
val f_sliceArrayRange = fn("sliceArray(indices: IntRange)") {
|
||||
include(InvariantArraysOfObjects, ArraysOfPrimitives)
|
||||
include(InvariantArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned)
|
||||
} builder {
|
||||
doc { "Returns a list containing elements at indices in the specified [indices] range." }
|
||||
doc { "Returns an array containing elements at indices in the specified [indices] range." }
|
||||
returns("SELF")
|
||||
body(InvariantArraysOfObjects) {
|
||||
"""
|
||||
@@ -943,5 +953,10 @@ object Filtering : TemplateGroupBase() {
|
||||
return copyOfRange(indices.start, indices.endInclusive + 1)
|
||||
"""
|
||||
}
|
||||
body(ArraysOfUnsigned) {
|
||||
"""
|
||||
return SELF(storage.sliceArray(indices))
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user