Rewrite array and specialized operations in new DSL
This commit is contained in:
@@ -15,7 +15,7 @@ fun main(args: Array<String>) {
|
|||||||
Elements,
|
Elements,
|
||||||
Filtering,
|
Filtering,
|
||||||
Ordering,
|
Ordering,
|
||||||
// ArrayOps,
|
ArrayOps,
|
||||||
Snapshots,
|
Snapshots,
|
||||||
Mapping,
|
Mapping,
|
||||||
SetOps,
|
SetOps,
|
||||||
@@ -27,10 +27,9 @@ fun main(args: Array<String>) {
|
|||||||
RangeOps,
|
RangeOps,
|
||||||
Numeric,
|
Numeric,
|
||||||
ComparableOps,
|
ComparableOps,
|
||||||
// CommonArrays,
|
CommonArrays,
|
||||||
// PlatformSpecialized,
|
PlatformSpecialized,
|
||||||
// PlatformSpecializedJS,
|
PlatformSpecializedJS
|
||||||
{ emptySequence() }
|
|
||||||
)
|
)
|
||||||
|
|
||||||
require(args.size == 1) { "Expecting Kotlin project home path as an argument" }
|
require(args.size == 1) { "Expecting Kotlin project home path as an argument" }
|
||||||
|
|||||||
@@ -1,13 +1,29 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2017 JetBrains s.r.o.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
package templates
|
package templates
|
||||||
|
|
||||||
import templates.Family.*
|
import templates.Family.*
|
||||||
|
|
||||||
fun arrays(): List<GenericFunction> {
|
object ArrayOps : TemplateGroupBase() {
|
||||||
val templates = arrayListOf<GenericFunction>()
|
|
||||||
|
|
||||||
templates add f("isEmpty()") {
|
val f_isEmpty = fn("isEmpty()") {
|
||||||
inline(Inline.Only)
|
include(ArraysOfObjects, ArraysOfPrimitives)
|
||||||
only(ArraysOfObjects, ArraysOfPrimitives)
|
} builder {
|
||||||
|
inlineOnly()
|
||||||
doc { "Returns `true` if the array is empty." }
|
doc { "Returns `true` if the array is empty." }
|
||||||
returns("Boolean")
|
returns("Boolean")
|
||||||
body {
|
body {
|
||||||
@@ -15,9 +31,10 @@ fun arrays(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("isNotEmpty()") {
|
val f_isNotEmpty = fn("isNotEmpty()") {
|
||||||
inline(Inline.Only)
|
include(ArraysOfObjects, ArraysOfPrimitives)
|
||||||
only(ArraysOfObjects, ArraysOfPrimitives)
|
} builder {
|
||||||
|
inlineOnly()
|
||||||
doc { "Returns `true` if the array is not empty." }
|
doc { "Returns `true` if the array is not empty." }
|
||||||
returns("Boolean")
|
returns("Boolean")
|
||||||
body {
|
body {
|
||||||
@@ -26,8 +43,9 @@ fun arrays(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
templates add pval("lastIndex") {
|
val f_lastIndex = pval("lastIndex") {
|
||||||
only(ArraysOfObjects, ArraysOfPrimitives)
|
include(ArraysOfObjects, ArraysOfPrimitives)
|
||||||
|
} builder {
|
||||||
doc { "Returns the last valid index for the array." }
|
doc { "Returns the last valid index for the array." }
|
||||||
returns("Int")
|
returns("Int")
|
||||||
body {
|
body {
|
||||||
@@ -35,8 +53,9 @@ fun arrays(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add pval("indices") {
|
val f_indices = pval("indices") {
|
||||||
only(ArraysOfObjects, ArraysOfPrimitives)
|
include(ArraysOfObjects, ArraysOfPrimitives)
|
||||||
|
} builder {
|
||||||
doc { "Returns the range of valid indices for the array." }
|
doc { "Returns the range of valid indices for the array." }
|
||||||
returns("IntRange")
|
returns("IntRange")
|
||||||
body {
|
body {
|
||||||
@@ -44,10 +63,10 @@ fun arrays(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("contentEquals(other: SELF)") {
|
val f_contentEquals = fn("contentEquals(other: SELF)") {
|
||||||
only(ArraysOfObjects, ArraysOfPrimitives)
|
include(ArraysOfObjects, ArraysOfPrimitives)
|
||||||
|
} builder {
|
||||||
since("1.1")
|
since("1.1")
|
||||||
inline(Platform.JVM, Inline.Only)
|
|
||||||
infix(true)
|
infix(true)
|
||||||
doc {
|
doc {
|
||||||
"""
|
"""
|
||||||
@@ -56,20 +75,22 @@ fun arrays(): List<GenericFunction> {
|
|||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
returns("Boolean")
|
returns("Boolean")
|
||||||
body(Platform.JVM) { "return java.util.Arrays.equals(this, other)" }
|
on(Platform.JVM) {
|
||||||
|
inlineOnly()
|
||||||
annotations(Platform.JS, """
|
body { "return java.util.Arrays.equals(this, other)" }
|
||||||
@library("arrayEquals")
|
}
|
||||||
@Suppress("UNUSED_PARAMETER")
|
|
||||||
""")
|
|
||||||
body(Platform.JS) { "definedExternally" }
|
|
||||||
|
|
||||||
|
on(Platform.JS) {
|
||||||
|
annotation("""@library("arrayEquals")""")
|
||||||
|
annotation("""@Suppress("UNUSED_PARAMETER")""")
|
||||||
|
body { "definedExternally" }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("contentDeepEquals(other: SELF)") {
|
val f_contentDeepEquals = fn("contentDeepEquals(other: SELF)") {
|
||||||
only(ArraysOfObjects)
|
include(ArraysOfObjects)
|
||||||
|
} builder {
|
||||||
since("1.1")
|
since("1.1")
|
||||||
inline(Platform.JVM, Inline.Only)
|
|
||||||
infix(true)
|
infix(true)
|
||||||
doc {
|
doc {
|
||||||
"""
|
"""
|
||||||
@@ -81,18 +102,21 @@ fun arrays(): List<GenericFunction> {
|
|||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
returns("Boolean")
|
returns("Boolean")
|
||||||
body(Platform.JVM) { "return java.util.Arrays.deepEquals(this, other)" }
|
on(Platform.JVM) {
|
||||||
annotations(Platform.JS, """
|
inlineOnly()
|
||||||
@library("arrayDeepEquals")
|
body { "return java.util.Arrays.deepEquals(this, other)" }
|
||||||
@Suppress("UNUSED_PARAMETER")
|
}
|
||||||
""")
|
on(Platform.JS) {
|
||||||
body(Platform.JS) { "definedExternally" }
|
annotation("""@library("arrayDeepEquals")""")
|
||||||
|
annotation("""@Suppress("UNUSED_PARAMETER")""")
|
||||||
|
body { "definedExternally" }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("contentToString()") {
|
val f_contentToString = fn("contentToString()") {
|
||||||
only(ArraysOfObjects, ArraysOfPrimitives)
|
include(ArraysOfObjects, ArraysOfPrimitives)
|
||||||
|
} builder {
|
||||||
since("1.1")
|
since("1.1")
|
||||||
inline(Platform.JVM, Inline.Only)
|
|
||||||
doc {
|
doc {
|
||||||
"""
|
"""
|
||||||
Returns a string representation of the contents of the specified array as if it is [List].
|
Returns a string representation of the contents of the specified array as if it is [List].
|
||||||
@@ -101,15 +125,20 @@ fun arrays(): List<GenericFunction> {
|
|||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
returns("String")
|
returns("String")
|
||||||
body(Platform.JVM) { "return java.util.Arrays.toString(this)" }
|
on(Platform.JVM) {
|
||||||
annotations(Platform.JS, """@library("arrayToString")""")
|
inlineOnly()
|
||||||
body(Platform.JS) { "definedExternally" }
|
body { "return java.util.Arrays.toString(this)" }
|
||||||
|
}
|
||||||
|
on(Platform.JS) {
|
||||||
|
annotation("""@library("arrayToString")""")
|
||||||
|
body { "definedExternally" }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("contentDeepToString()") {
|
val f_contentDeepToString = fn("contentDeepToString()") {
|
||||||
only(ArraysOfObjects)
|
include(ArraysOfObjects)
|
||||||
|
} builder {
|
||||||
since("1.1")
|
since("1.1")
|
||||||
inline(Platform.JVM, Inline.Only)
|
|
||||||
doc {
|
doc {
|
||||||
"""
|
"""
|
||||||
Returns a string representation of the contents of this array as if it is a [List].
|
Returns a string representation of the contents of this array as if it is a [List].
|
||||||
@@ -122,28 +151,38 @@ fun arrays(): List<GenericFunction> {
|
|||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
returns("String")
|
returns("String")
|
||||||
body(Platform.JVM) { "return java.util.Arrays.deepToString(this)" }
|
on(Platform.JVM) {
|
||||||
annotations(Platform.JS, """@library("arrayDeepToString")""")
|
inlineOnly()
|
||||||
body(Platform.JS) { "definedExternally" }
|
body { "return java.util.Arrays.deepToString(this)" }
|
||||||
|
}
|
||||||
|
on(Platform.JS) {
|
||||||
|
annotation("""@library("arrayDeepToString")""")
|
||||||
|
body { "definedExternally" }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("contentHashCode()") {
|
val f_contentHashCode = fn("contentHashCode()") {
|
||||||
only(ArraysOfObjects, ArraysOfPrimitives)
|
include(ArraysOfObjects, ArraysOfPrimitives)
|
||||||
|
} builder {
|
||||||
since("1.1")
|
since("1.1")
|
||||||
inline(Platform.JVM, Inline.Only)
|
|
||||||
doc {
|
doc {
|
||||||
"Returns a hash code based on the contents of this array as if it is [List]."
|
"Returns a hash code based on the contents of this array as if it is [List]."
|
||||||
}
|
}
|
||||||
returns("Int")
|
returns("Int")
|
||||||
body(Platform.JVM) { "return java.util.Arrays.hashCode(this)" }
|
on(Platform.JVM) {
|
||||||
annotations(Platform.JS, """@library("arrayHashCode")""")
|
inlineOnly()
|
||||||
body(Platform.JS) { "definedExternally" }
|
body { "return java.util.Arrays.hashCode(this)" }
|
||||||
|
}
|
||||||
|
on(Platform.JS) {
|
||||||
|
annotation("""@library("arrayHashCode")""")
|
||||||
|
body { "definedExternally" }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("contentDeepHashCode()") {
|
val f_contentDeepHashCode = fn("contentDeepHashCode()") {
|
||||||
only(ArraysOfObjects)
|
include(ArraysOfObjects)
|
||||||
|
} builder {
|
||||||
since("1.1")
|
since("1.1")
|
||||||
inline(Platform.JVM, Inline.Only)
|
|
||||||
doc {
|
doc {
|
||||||
"""
|
"""
|
||||||
Returns a hash code based on the contents of this array as if it is [List].
|
Returns a hash code based on the contents of this array as if it is [List].
|
||||||
@@ -153,20 +192,27 @@ fun arrays(): List<GenericFunction> {
|
|||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
returns("Int")
|
returns("Int")
|
||||||
body(Platform.JVM) { "return java.util.Arrays.deepHashCode(this)" }
|
on(Platform.JVM) {
|
||||||
annotations(Platform.JS, """@library("arrayDeepHashCode")""")
|
inlineOnly()
|
||||||
body(Platform.JS) { "definedExternally" }
|
body { "return java.util.Arrays.deepHashCode(this)" }
|
||||||
|
}
|
||||||
|
on(Platform.JS) {
|
||||||
|
annotation("""@library("arrayDeepHashCode")""")
|
||||||
|
body { "definedExternally" }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates addAll PrimitiveType.defaultPrimitives.map { primitive ->
|
val f_toPrimitiveArray = fn("toPrimitiveArray()") {
|
||||||
|
include(ArraysOfObjects, PrimitiveType.defaultPrimitives)
|
||||||
|
include(Collections, PrimitiveType.defaultPrimitives)
|
||||||
|
} builder {
|
||||||
|
val primitive = checkNotNull(primitive)
|
||||||
val arrayType = primitive.name + "Array"
|
val arrayType = primitive.name + "Array"
|
||||||
f("to$arrayType()") {
|
signature("to$arrayType()")
|
||||||
only(ArraysOfObjects, Collections)
|
returns(arrayType)
|
||||||
buildFamilies.default!!.forEach { family -> onlyPrimitives(family, primitive) }
|
// TODO: Use different implementations for JS
|
||||||
doc(ArraysOfObjects) { "Returns an array of ${primitive.name} containing all of the elements of this generic array." }
|
specialFor(ArraysOfObjects) {
|
||||||
doc(Collections) { "Returns an array of ${primitive.name} containing all of the elements of this collection." }
|
doc { "Returns an array of ${primitive.name} containing all of the elements of this generic array." }
|
||||||
returns(arrayType)
|
|
||||||
// TODO: Use different implementations for JS
|
|
||||||
body {
|
body {
|
||||||
"""
|
"""
|
||||||
val result = $arrayType(size)
|
val result = $arrayType(size)
|
||||||
@@ -175,7 +221,10 @@ fun arrays(): List<GenericFunction> {
|
|||||||
return result
|
return result
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
body(Collections) {
|
}
|
||||||
|
specialFor(Collections) {
|
||||||
|
doc { "Returns an array of ${primitive.name} containing all of the elements of this collection." }
|
||||||
|
body {
|
||||||
"""
|
"""
|
||||||
val result = $arrayType(size)
|
val result = $arrayType(size)
|
||||||
var index = 0
|
var index = 0
|
||||||
@@ -186,6 +235,4 @@ fun arrays(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return templates
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,34 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2010-2016 JetBrains s.r.o.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package templates
|
|
||||||
|
|
||||||
import templates.Family.*
|
|
||||||
|
|
||||||
fun specialJS(): List<GenericFunction> {
|
|
||||||
val templates = arrayListOf<GenericFunction>()
|
|
||||||
|
|
||||||
templates add f("sort(noinline comparison: (a: T, b: T) -> Int)") {
|
|
||||||
only(ArraysOfObjects, ArraysOfPrimitives)
|
|
||||||
exclude(PrimitiveType.Boolean)
|
|
||||||
inline(Inline.Only)
|
|
||||||
returns("Unit")
|
|
||||||
doc { "Sorts the array in-place according to the order specified by the given [comparison] function." }
|
|
||||||
body { "asDynamic().sort(comparison)" }
|
|
||||||
}
|
|
||||||
|
|
||||||
return templates
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2016 JetBrains s.r.o.
|
* Copyright 2010-2017 JetBrains s.r.o.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -18,13 +18,19 @@ package templates
|
|||||||
|
|
||||||
import templates.Family.*
|
import templates.Family.*
|
||||||
|
|
||||||
fun specialJVM(): List<GenericFunction> {
|
object PlatformSpecialized : TemplateGroupBase() {
|
||||||
val templates = arrayListOf<GenericFunction>()
|
init {
|
||||||
|
defaultBuilder {
|
||||||
|
jvmOnly = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
templates add f("fill(element: T, fromIndex: Int = 0, toIndex: Int = size)") {
|
val f_fill = fn("fill(element: T, fromIndex: Int = 0, toIndex: Int = size)") {
|
||||||
only(InvariantArraysOfObjects, ArraysOfPrimitives)
|
platforms(Platform.JVM)
|
||||||
|
include(InvariantArraysOfObjects, ArraysOfPrimitives)
|
||||||
|
} builder {
|
||||||
doc { "Fills original array with the provided value." }
|
doc { "Fills original array with the provided value." }
|
||||||
returns { "Unit" }
|
returns("Unit")
|
||||||
body {
|
body {
|
||||||
"""
|
"""
|
||||||
java.util.Arrays.fill(this, fromIndex, toIndex, element)
|
java.util.Arrays.fill(this, fromIndex, toIndex, element)
|
||||||
@@ -32,9 +38,11 @@ fun specialJVM(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("binarySearch(element: T, fromIndex: Int = 0, toIndex: Int = size)") {
|
val f_binarySearch = fn("binarySearch(element: T, fromIndex: Int = 0, toIndex: Int = size)") {
|
||||||
only(ArraysOfObjects, ArraysOfPrimitives)
|
platforms(Platform.JVM)
|
||||||
|
include(ArraysOfObjects, ArraysOfPrimitives)
|
||||||
exclude(PrimitiveType.Boolean)
|
exclude(PrimitiveType.Boolean)
|
||||||
|
} builder {
|
||||||
doc {
|
doc {
|
||||||
"""
|
"""
|
||||||
Searches the array or the range of the array for the provided [element] using the binary search algorithm.
|
Searches the array or the range of the array for the provided [element] using the binary search algorithm.
|
||||||
@@ -54,8 +62,10 @@ fun specialJVM(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("binarySearch(element: T, comparator: Comparator<in T>, fromIndex: Int = 0, toIndex: Int = size)") {
|
val f_binarySearch_comparator = fn("binarySearch(element: T, comparator: Comparator<in T>, fromIndex: Int = 0, toIndex: Int = size)") {
|
||||||
only(ArraysOfObjects)
|
platforms(Platform.JVM)
|
||||||
|
include(ArraysOfObjects)
|
||||||
|
} builder {
|
||||||
doc {
|
doc {
|
||||||
"""
|
"""
|
||||||
Searches the array or the range of the array for the provided [element] using the binary search algorithm.
|
Searches the array or the range of the array for the provided [element] using the binary search algorithm.
|
||||||
@@ -76,9 +86,11 @@ fun specialJVM(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
templates add f("sort()") {
|
val f_sort = fn("sort()") {
|
||||||
// left with more generic signature for JVM only
|
// left with more generic signature for JVM only
|
||||||
only(ArraysOfObjects)
|
platforms(Platform.JVM)
|
||||||
|
include(ArraysOfObjects)
|
||||||
|
} builder {
|
||||||
doc {
|
doc {
|
||||||
"""
|
"""
|
||||||
Sorts the array in-place according to the natural order of its elements.
|
Sorts the array in-place according to the natural order of its elements.
|
||||||
@@ -92,9 +104,11 @@ fun specialJVM(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("sort(fromIndex: Int = 0, toIndex: Int = size)") {
|
val f_sort_range = fn("sort(fromIndex: Int = 0, toIndex: Int = size)") {
|
||||||
only(ArraysOfObjects, ArraysOfPrimitives)
|
platforms(Platform.JVM)
|
||||||
|
include(ArraysOfObjects, ArraysOfPrimitives)
|
||||||
exclude(PrimitiveType.Boolean)
|
exclude(PrimitiveType.Boolean)
|
||||||
|
} builder {
|
||||||
doc { "Sorts a range in the array in-place." }
|
doc { "Sorts a range in the array in-place." }
|
||||||
returns("Unit")
|
returns("Unit")
|
||||||
body {
|
body {
|
||||||
@@ -102,8 +116,10 @@ fun specialJVM(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("sortWith(comparator: Comparator<in T>, fromIndex: Int = 0, toIndex: Int = size)") {
|
val f_sortWith = fn("sortWith(comparator: Comparator<in T>, fromIndex: Int = 0, toIndex: Int = size)") {
|
||||||
only(ArraysOfObjects)
|
platforms(Platform.JVM)
|
||||||
|
include(ArraysOfObjects)
|
||||||
|
} builder {
|
||||||
doc { "Sorts a range in the array in-place with the given [comparator]." }
|
doc { "Sorts a range in the array in-place with the given [comparator]." }
|
||||||
returns("Unit")
|
returns("Unit")
|
||||||
body {
|
body {
|
||||||
@@ -111,13 +127,15 @@ fun specialJVM(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("filterIsInstanceTo(destination: C, klass: Class<R>)") {
|
val f_filterIsInstanceTo = fn("filterIsInstanceTo(destination: C, klass: Class<R>)") {
|
||||||
|
platforms(Platform.JVM)
|
||||||
|
include(Iterables, ArraysOfObjects, Sequences)
|
||||||
|
} builder {
|
||||||
doc { "Appends all elements that are instances of specified class to the given [destination]." }
|
doc { "Appends all elements that are instances of specified class to the given [destination]." }
|
||||||
receiverAsterisk(true)
|
receiverAsterisk = true
|
||||||
typeParam("C : MutableCollection<in R>")
|
typeParam("C : MutableCollection<in R>")
|
||||||
typeParam("R")
|
typeParam("R")
|
||||||
returns("C")
|
returns("C")
|
||||||
exclude(ArraysOfPrimitives, Strings)
|
|
||||||
body {
|
body {
|
||||||
"""
|
"""
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
@@ -127,9 +145,12 @@ fun specialJVM(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("filterIsInstance(klass: Class<R>)") {
|
val f_filterIsInstance = fn("filterIsInstance(klass: Class<R>)") {
|
||||||
|
platforms(Platform.JVM)
|
||||||
|
include(Iterables, ArraysOfObjects, Sequences)
|
||||||
|
} builder {
|
||||||
doc { "Returns a list containing all elements that are instances of specified class." }
|
doc { "Returns a list containing all elements that are instances of specified class." }
|
||||||
receiverAsterisk(true)
|
receiverAsterisk= true
|
||||||
typeParam("R")
|
typeParam("R")
|
||||||
returns("List<R>")
|
returns("List<R>")
|
||||||
body {
|
body {
|
||||||
@@ -137,10 +158,11 @@ fun specialJVM(): List<GenericFunction> {
|
|||||||
return filterIsInstanceTo(ArrayList<R>(), klass)
|
return filterIsInstanceTo(ArrayList<R>(), klass)
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
exclude(ArraysOfPrimitives, Strings)
|
|
||||||
|
|
||||||
doc(Sequences) { "Returns a sequence containing all elements that are instances of specified class." }
|
specialFor(Sequences) {
|
||||||
returns(Sequences) { "Sequence<R>" }
|
doc { "Returns a sequence containing all elements that are instances of specified class." }
|
||||||
|
returns("Sequence<R>")
|
||||||
|
}
|
||||||
body(Sequences) {
|
body(Sequences) {
|
||||||
"""
|
"""
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
@@ -148,339 +170,318 @@ fun specialJVM(): List<GenericFunction> {
|
|||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates.forEach { it.apply { jvmOnly(true) } }
|
|
||||||
|
|
||||||
return templates
|
|
||||||
}
|
}
|
||||||
|
|
||||||
object CommonArrays {
|
object PlatformSpecializedJS : TemplateGroupBase() {
|
||||||
|
val f_sort = fn("sort(noinline comparison: (a: T, b: T) -> Int)") {
|
||||||
|
platforms(Platform.JS)
|
||||||
|
include(ArraysOfObjects, ArraysOfPrimitives)
|
||||||
|
exclude(PrimitiveType.Boolean)
|
||||||
|
} builder {
|
||||||
|
inlineOnly()
|
||||||
|
returns("Unit")
|
||||||
|
doc { "Sorts the array in-place according to the order specified by the given [comparison] function." }
|
||||||
|
body { "asDynamic().sort(comparison)" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun f_plusElement() = f("plusElement(element: T)") {
|
object CommonArrays : TemplateGroupBase() {
|
||||||
inline(Platform.JVM, Inline.Only)
|
|
||||||
inline(Platform.JS, Inline.Yes)
|
|
||||||
annotations(Platform.JS, """@Suppress("NOTHING_TO_INLINE")""")
|
|
||||||
|
|
||||||
only(InvariantArraysOfObjects)
|
|
||||||
only(Platform.JS, ArraysOfObjects)
|
|
||||||
|
|
||||||
|
val f_plusElement = fn("plusElement(element: T)") {
|
||||||
|
include(InvariantArraysOfObjects)
|
||||||
|
} builder {
|
||||||
returns("SELF")
|
returns("SELF")
|
||||||
returns(Platform.JS, ArraysOfObjects) { "Array<T>" }
|
|
||||||
doc { "Returns an array containing all elements of the original array and then the given [element]." }
|
doc { "Returns an array containing all elements of the original array and then the given [element]." }
|
||||||
body(Platform.JVM) { "return plus(element)" }
|
|
||||||
body(Platform.JS) {
|
on(Platform.JVM) {
|
||||||
"""
|
inlineOnly()
|
||||||
return this.asDynamic().concat(arrayOf(element))
|
body { "return plus(element)" }
|
||||||
"""
|
}
|
||||||
|
on(Platform.JS) {
|
||||||
|
family = ArraysOfObjects
|
||||||
|
inline(suppressWarning = true)
|
||||||
|
returns("Array<T>")
|
||||||
|
body {
|
||||||
|
"""
|
||||||
|
return this.asDynamic().concat(arrayOf(element))
|
||||||
|
"""
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun f_plusElementOperator() =
|
val f_plus = fn("plus(element: T)") {
|
||||||
(listOf(InvariantArraysOfObjects to null) + PrimitiveType.defaultPrimitives.map { ArraysOfPrimitives to it }).map {
|
include(InvariantArraysOfObjects, ArraysOfPrimitives)
|
||||||
val (family, primitive) = it
|
} builderWith { primitive ->
|
||||||
f("plus(element: T)") {
|
doc { "Returns an array containing all elements of the original array and then the given [element]." }
|
||||||
operator(true)
|
operator()
|
||||||
|
returns("SELF")
|
||||||
|
|
||||||
only(family)
|
on(Platform.JVM) {
|
||||||
if (family == InvariantArraysOfObjects) {
|
body {
|
||||||
only(Platform.JS, ArraysOfObjects)
|
"""
|
||||||
}
|
val index = size
|
||||||
|
val result = java.util.Arrays.copyOf(this, index + 1)
|
||||||
|
result[index] = element
|
||||||
|
return result
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
inline(Platform.JS, Inline.Yes)
|
on(Platform.JS) {
|
||||||
annotations(Platform.JS, """@Suppress("NOTHING_TO_INLINE")""")
|
inline(suppressWarning = true)
|
||||||
|
specialFor(InvariantArraysOfObjects) {
|
||||||
returns("SELF")
|
family = ArraysOfObjects
|
||||||
returns(Platform.JS, ArraysOfObjects) { "Array<T>" }
|
returns("Array<T>")
|
||||||
doc { "Returns an array containing all elements of the original array and then the given [element]." }
|
|
||||||
body(Platform.JVM) {
|
|
||||||
"""
|
|
||||||
val index = size
|
|
||||||
val result = java.util.Arrays.copyOf(this, index + 1)
|
|
||||||
result[index] = element
|
|
||||||
return result
|
|
||||||
"""
|
|
||||||
}
|
|
||||||
|
|
||||||
if (primitive == null) {
|
|
||||||
body(Platform.JS) { "return this.asDynamic().concat(arrayOf(element))" }
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
only(primitive)
|
|
||||||
body(Platform.JS, ArraysOfPrimitives) { "return plus(${primitive.name.toLowerCase()}ArrayOf(element))" }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun f_plusCollection() =
|
body {
|
||||||
(listOf(InvariantArraysOfObjects to null) + PrimitiveType.defaultPrimitives.map { ArraysOfPrimitives to it }).map {
|
if (primitive == null)
|
||||||
val (family, primitive) = it
|
"return this.asDynamic().concat(arrayOf(element))"
|
||||||
f("plus(elements: Collection<T>)") {
|
else
|
||||||
operator(true)
|
"return plus(${primitive.name.toLowerCase()}ArrayOf(element))"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
only(family)
|
|
||||||
if (family == InvariantArraysOfObjects) {
|
|
||||||
only(Platform.JS, ArraysOfObjects)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: inline arrayPlusCollection when @PublishedAPI is available
|
val f_plus_collection = fn("plus(elements: Collection<T>)") {
|
||||||
|
include(InvariantArraysOfObjects, ArraysOfPrimitives)
|
||||||
|
} builder {
|
||||||
|
operator()
|
||||||
|
returns("SELF")
|
||||||
|
doc { "Returns an array containing all elements of the original array and then all elements of the given [elements] collection." }
|
||||||
|
on(Platform.JVM) {
|
||||||
|
body {
|
||||||
|
"""
|
||||||
|
var index = size
|
||||||
|
val result = java.util.Arrays.copyOf(this, index + elements.size)
|
||||||
|
for (element in elements) result[index++] = element
|
||||||
|
return result
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
on(Platform.JS) {
|
||||||
|
// TODO: inline arrayPlusCollection when @PublishedAPI is available
|
||||||
// inline(Platform.JS, Inline.Yes)
|
// inline(Platform.JS, Inline.Yes)
|
||||||
// annotations(Platform.JS, """@Suppress("NOTHING_TO_INLINE")""")
|
// annotations(Platform.JS, """@Suppress("NOTHING_TO_INLINE")""")
|
||||||
|
specialFor(InvariantArraysOfObjects) {
|
||||||
returns("SELF")
|
family = ArraysOfObjects
|
||||||
returns(Platform.JS, ArraysOfObjects) { "Array<T>" }
|
returns("Array<T>")
|
||||||
doc { "Returns an array containing all elements of the original array and then all elements of the given [elements] collection." }
|
|
||||||
body(Platform.JVM) {
|
|
||||||
"""
|
|
||||||
var index = size
|
|
||||||
val result = java.util.Arrays.copyOf(this, index + elements.size)
|
|
||||||
for (element in elements) result[index++] = element
|
|
||||||
return result
|
|
||||||
"""
|
|
||||||
}
|
|
||||||
if (primitive != null) {
|
|
||||||
only(primitive)
|
|
||||||
}
|
|
||||||
when (primitive) {
|
|
||||||
null, PrimitiveType.Boolean, PrimitiveType.Long ->
|
|
||||||
body(Platform.JS) { "return arrayPlusCollection(this, elements)" }
|
|
||||||
else ->
|
|
||||||
body(Platform.JS) { "return fillFromCollection(this.copyOf(size + elements.size), this.size, elements)" }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
when (primitive) {
|
||||||
|
null, PrimitiveType.Boolean, PrimitiveType.Long ->
|
||||||
|
body { "return arrayPlusCollection(this, elements)" }
|
||||||
|
else ->
|
||||||
|
body { "return fillFromCollection(this.copyOf(size + elements.size), this.size, elements)" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun f_plusArray() = f("plus(elements: SELF)") {
|
val f_plus_array = fn("plus(elements: SELF)") {
|
||||||
|
include(InvariantArraysOfObjects, ArraysOfPrimitives)
|
||||||
|
} builder {
|
||||||
operator(true)
|
operator(true)
|
||||||
|
|
||||||
inline(Platform.JS, Inline.Yes)
|
|
||||||
annotations(Platform.JS, """@Suppress("NOTHING_TO_INLINE")""")
|
|
||||||
|
|
||||||
only(InvariantArraysOfObjects, ArraysOfPrimitives)
|
|
||||||
only(Platform.JS, ArraysOfObjects, ArraysOfPrimitives)
|
|
||||||
customSignature(InvariantArraysOfObjects) { "plus(elements: Array<out T>)" }
|
|
||||||
|
|
||||||
doc { "Returns an array containing all elements of the original array and then all elements of the given [elements] array." }
|
doc { "Returns an array containing all elements of the original array and then all elements of the given [elements] array." }
|
||||||
returns("SELF")
|
returns("SELF")
|
||||||
returns(Platform.JS, ArraysOfObjects) { "Array<T>" }
|
specialFor(InvariantArraysOfObjects) {
|
||||||
|
signature("plus(elements: Array<out T>)", notForSorting = true)
|
||||||
body(Platform.JVM) {
|
|
||||||
"""
|
|
||||||
val thisSize = size
|
|
||||||
val arraySize = elements.size
|
|
||||||
val result = java.util.Arrays.copyOf(this, thisSize + arraySize)
|
|
||||||
System.arraycopy(elements, 0, result, thisSize, arraySize)
|
|
||||||
return result
|
|
||||||
"""
|
|
||||||
}
|
|
||||||
body(Platform.JS) {
|
|
||||||
"""
|
|
||||||
return this.asDynamic().concat(elements)
|
|
||||||
"""
|
|
||||||
}
|
|
||||||
body(Platform.JS, ArraysOfPrimitives) {
|
|
||||||
"""
|
|
||||||
return primitiveArrayConcat(this, elements)
|
|
||||||
"""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
on(Platform.JVM) {
|
||||||
|
body {
|
||||||
|
"""
|
||||||
|
val thisSize = size
|
||||||
|
val arraySize = elements.size
|
||||||
|
val result = java.util.Arrays.copyOf(this, thisSize + arraySize)
|
||||||
|
System.arraycopy(elements, 0, result, thisSize, arraySize)
|
||||||
|
return result
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
on(Platform.JS) {
|
||||||
|
inline(suppressWarning = true)
|
||||||
|
specialFor(InvariantArraysOfObjects) {
|
||||||
|
family = ArraysOfObjects
|
||||||
|
returns("Array<T>")
|
||||||
|
body { """return this.asDynamic().concat(elements)""" }
|
||||||
|
}
|
||||||
|
specialFor(ArraysOfPrimitives) {
|
||||||
|
body { """return primitiveArrayConcat(this, elements)""" }
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun f_copyOfRange() = (listOf(InvariantArraysOfObjects to null) + PrimitiveType.defaultPrimitives.map { ArraysOfPrimitives to it }).map {
|
|
||||||
val (family, primitive) = it
|
val f_copyOfRange = fn("copyOfRange(fromIndex: Int, toIndex: Int)") {
|
||||||
f("copyOfRange(fromIndex: Int, toIndex: Int)") {
|
include(InvariantArraysOfObjects, ArraysOfPrimitives)
|
||||||
only(family)
|
} builderWith { primitive ->
|
||||||
if (family == InvariantArraysOfObjects) {
|
doc { "Returns new array which is a copy of range of original array." }
|
||||||
only(Platform.JS, ArraysOfObjects)
|
returns("SELF")
|
||||||
|
|
||||||
|
on(Platform.JS) {
|
||||||
|
specialFor(InvariantArraysOfObjects) {
|
||||||
|
family = ArraysOfObjects
|
||||||
|
returns("Array<T>")
|
||||||
}
|
}
|
||||||
|
when(primitive) {
|
||||||
inline(Platform.JVM, Inline.Only)
|
PrimitiveType.Char, PrimitiveType.Boolean, PrimitiveType.Long ->
|
||||||
|
body { "return withType(\"${primitive}Array\", this.asDynamic().slice(fromIndex, toIndex))" }
|
||||||
doc { "Returns new array which is a copy of range of original array." }
|
else -> {
|
||||||
returns("SELF")
|
inline(suppressWarning = true)
|
||||||
returns(Platform.JS, ArraysOfObjects) { "Array<T>" }
|
body { "return this.asDynamic().slice(fromIndex, toIndex)" }
|
||||||
body(Platform.JVM) {
|
}
|
||||||
"return java.util.Arrays.copyOfRange(this, fromIndex, toIndex)"
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
on(Platform.JVM) {
|
||||||
|
inlineOnly()
|
||||||
|
body { "return java.util.Arrays.copyOfRange(this, fromIndex, toIndex)" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (primitive != null) {
|
val f_copyOf = fn("copyOf()") {
|
||||||
only(primitive)
|
include(InvariantArraysOfObjects)
|
||||||
|
include(ArraysOfPrimitives, PrimitiveType.defaultPrimitives)
|
||||||
|
} builder {
|
||||||
|
doc { "Returns new array which is a copy of the original array." }
|
||||||
|
returns("SELF")
|
||||||
|
on(Platform.JVM) {
|
||||||
|
inlineOnly()
|
||||||
|
body { "return java.util.Arrays.copyOf(this, size)" }
|
||||||
|
}
|
||||||
|
on(Platform.JS) {
|
||||||
|
specialFor(InvariantArraysOfObjects) {
|
||||||
|
family = ArraysOfObjects
|
||||||
|
returns("Array<T>")
|
||||||
}
|
}
|
||||||
when (primitive) {
|
when (primitive) {
|
||||||
|
null -> {
|
||||||
|
inline(suppressWarning = true)
|
||||||
|
body { "return this.asDynamic().slice()" }
|
||||||
|
}
|
||||||
PrimitiveType.Char, PrimitiveType.Boolean, PrimitiveType.Long ->
|
PrimitiveType.Char, PrimitiveType.Boolean, PrimitiveType.Long ->
|
||||||
body(Platform.JS) { "return withType(\"${primitive}Array\", this.asDynamic().slice(fromIndex, toIndex))" }
|
body { "return withType(\"${primitive}Array\", this.asDynamic().slice())" }
|
||||||
else -> {
|
else -> {
|
||||||
annotations(Platform.JS, """@Suppress("NOTHING_TO_INLINE")""")
|
inline(suppressWarning = true)
|
||||||
inline(Platform.JS, Inline.Yes)
|
body { "return this.asDynamic().slice()" }
|
||||||
body(Platform.JS) { "return this.asDynamic().slice(fromIndex, toIndex)" }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun f_copyOf() = (listOf(InvariantArraysOfObjects to null) + PrimitiveType.defaultPrimitives.map { ArraysOfPrimitives to it }).map {
|
val f_copyOf_newSize = fn("copyOf(newSize: Int)") {
|
||||||
val (family, primitive) = it
|
include(ArraysOfPrimitives, PrimitiveType.defaultPrimitives)
|
||||||
f("copyOf()") {
|
include(InvariantArraysOfObjects)
|
||||||
only(family)
|
} builder {
|
||||||
if (family == InvariantArraysOfObjects) {
|
doc { "Returns new array which is a copy of the original array, resized to the given [newSize]." }
|
||||||
only(Platform.JS, ArraysOfObjects)
|
specialFor(ArraysOfPrimitives) {
|
||||||
}
|
|
||||||
|
|
||||||
inline(Platform.JVM, Inline.Only)
|
|
||||||
|
|
||||||
doc { "Returns new array which is a copy of the original array." }
|
|
||||||
returns("SELF")
|
returns("SELF")
|
||||||
returns(Platform.JS, ArraysOfObjects) { "Array<T>" }
|
on(Platform.JS) {
|
||||||
|
when (primitive!!) {
|
||||||
body(Platform.JVM) {
|
PrimitiveType.Boolean ->
|
||||||
"return java.util.Arrays.copyOf(this, size)"
|
body { "return withType(\"BooleanArray\", arrayCopyResize(this, newSize, false))" }
|
||||||
}
|
PrimitiveType.Char ->
|
||||||
body(Platform.JS) {
|
body { "return withType(\"CharArray\", fillFrom(this, ${primitive}Array(newSize)))" }
|
||||||
"return this.asDynamic().slice()"
|
PrimitiveType.Long ->
|
||||||
}
|
body { "return withType(\"LongArray\", arrayCopyResize(this, newSize, ZERO))" }
|
||||||
|
else ->
|
||||||
if (primitive != null) {
|
body { "return fillFrom(this, ${primitive}Array(newSize))" }
|
||||||
only(primitive)
|
|
||||||
}
|
|
||||||
when (primitive) {
|
|
||||||
PrimitiveType.Char, PrimitiveType.Boolean, PrimitiveType.Long ->
|
|
||||||
body(Platform.JS) { "return withType(\"${primitive}Array\", this.asDynamic().slice())" }
|
|
||||||
else -> {
|
|
||||||
annotations(Platform.JS, """@Suppress("NOTHING_TO_INLINE")""")
|
|
||||||
inline(Platform.JS, Inline.Yes)
|
|
||||||
body(Platform.JS) { "return this.asDynamic().slice()" }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
specialFor(InvariantArraysOfObjects) {
|
||||||
|
returns("Array<T?>")
|
||||||
|
on(Platform.JS) {
|
||||||
|
family = ArraysOfObjects
|
||||||
|
body { "return arrayCopyResize(this, newSize, null)" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
on(Platform.JVM) {
|
||||||
|
inlineOnly()
|
||||||
|
body {
|
||||||
|
"return java.util.Arrays.copyOf(this, newSize)"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun f_copyOfResized() =
|
fun f_sort() = fn("sort()") {
|
||||||
(PrimitiveType.defaultPrimitives.map { ArraysOfPrimitives to it } + (InvariantArraysOfObjects to null)).map {
|
include(ArraysOfPrimitives, PrimitiveType.numericPrimitives + PrimitiveType.Char)
|
||||||
val (family, primitive) = it
|
include(ArraysOfObjects)
|
||||||
f("copyOf(newSize: Int)") {
|
} builder {
|
||||||
only(family)
|
typeParam("T: Comparable<T>")
|
||||||
if (family == InvariantArraysOfObjects) {
|
doc { "Sorts the array in-place according to the natural order of its elements." }
|
||||||
only(Platform.JS, ArraysOfObjects)
|
specialFor(ArraysOfPrimitives) {
|
||||||
}
|
doc { "Sorts the array in-place." }
|
||||||
|
}
|
||||||
|
|
||||||
inline(Platform.JVM, Inline.Only)
|
returns("Unit")
|
||||||
doc { "Returns new array which is a copy of the original array, resized to the given [newSize]." }
|
on(Platform.JS) {
|
||||||
|
body {
|
||||||
if (primitive != null) {
|
"""
|
||||||
only(primitive)
|
if (size > 1)
|
||||||
returns("SELF")
|
sort { a: T, b: T -> a.compareTo(b) }
|
||||||
when (primitive) {
|
"""
|
||||||
PrimitiveType.Boolean ->
|
}
|
||||||
body(Platform.JS) { "return withType(\"BooleanArray\", arrayCopyResize(this, newSize, false))" }
|
specialFor(ArraysOfPrimitives) {
|
||||||
PrimitiveType.Char ->
|
if (primitive != PrimitiveType.Long) {
|
||||||
body(Platform.JS) { "return withType(\"CharArray\", fillFrom(this, ${primitive}Array(newSize)))" }
|
annotation("""@library("primitiveArraySort")""")
|
||||||
PrimitiveType.Long ->
|
body { "definedExternally" }
|
||||||
body(Platform.JS) { "return withType(\"LongArray\", arrayCopyResize(this, newSize, ZERO))" }
|
|
||||||
else ->
|
|
||||||
body(Platform.JS) { "return fillFrom(this, ${primitive}Array(newSize))" }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
returns { "Array<T?>" }
|
|
||||||
body(Platform.JS) { "return arrayCopyResize(this, newSize, null)" }
|
|
||||||
}
|
|
||||||
|
|
||||||
body(Platform.JVM) {
|
|
||||||
"return java.util.Arrays.copyOf(this, newSize)"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
fun f_sortPrimitives() =
|
on(Platform.JVM) {
|
||||||
(PrimitiveType.numericPrimitives + PrimitiveType.Char).map { primitive ->
|
specialFor(ArraysOfObjects) {
|
||||||
f("sort()") {
|
inlineOnly()
|
||||||
only(ArraysOfPrimitives)
|
body {
|
||||||
only(primitive)
|
"""
|
||||||
doc { "Sorts the array in-place." }
|
@Suppress("UNCHECKED_CAST")
|
||||||
returns("Unit")
|
(this as Array<Any?>).sort()
|
||||||
body(Platform.JVM) {
|
"""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
specialFor(ArraysOfPrimitives) {
|
||||||
|
body {
|
||||||
"if (size > 1) java.util.Arrays.sort(this)"
|
"if (size > 1) java.util.Arrays.sort(this)"
|
||||||
}
|
}
|
||||||
if (primitive != PrimitiveType.Long) {
|
|
||||||
annotations(Platform.JS, """@library("primitiveArraySort")""")
|
|
||||||
body(Platform.JS) { "definedExternally" }
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
body(Platform.JS) {
|
|
||||||
"""
|
|
||||||
if (size > 1)
|
|
||||||
sort { a: T, b: T -> a.compareTo(b) }
|
|
||||||
"""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun f_sort() = f("sort()") {
|
|
||||||
only(ArraysOfObjects)
|
|
||||||
typeParam("T: Comparable<T>")
|
|
||||||
doc {
|
|
||||||
"""
|
|
||||||
Sorts the array in-place according to the natural order of its elements.
|
|
||||||
"""
|
|
||||||
}
|
|
||||||
returns("Unit")
|
|
||||||
inline(Platform.JVM, Inline.Only)
|
|
||||||
body(Platform.JVM) {
|
|
||||||
"""
|
|
||||||
@Suppress("UNCHECKED_CAST")
|
|
||||||
(this as Array<Any?>).sort()
|
|
||||||
"""
|
|
||||||
}
|
|
||||||
body(Platform.JS) {
|
|
||||||
"""
|
|
||||||
if (size > 1)
|
|
||||||
sort { a: T, b: T -> a.compareTo(b) }
|
|
||||||
"""
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun f_sortWith() = f("sortWith(comparator: Comparator<in T>)") {
|
fun f_sortWith() = fn("sortWith(comparator: Comparator<in T>)") {
|
||||||
only(ArraysOfObjects)
|
include(ArraysOfObjects)
|
||||||
buildFamilyPrimitives(Platform.JS, buildFamilyPrimitives.default!! - PrimitiveType.Boolean)
|
} builder {
|
||||||
doc { "Sorts the array in-place according to the order specified by the given [comparator]." }
|
doc { "Sorts the array in-place according to the order specified by the given [comparator]." }
|
||||||
returns("Unit")
|
returns("Unit")
|
||||||
body(Platform.JVM) {
|
on(Platform.JVM) {
|
||||||
"if (size > 1) java.util.Arrays.sort(this, comparator)"
|
body {
|
||||||
|
"if (size > 1) java.util.Arrays.sort(this, comparator)"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
body(Platform.JS) {
|
on(Platform.JS) {
|
||||||
"""
|
body {
|
||||||
if (size > 1)
|
"""
|
||||||
sort { a, b -> comparator.compare(a, b) }
|
if (size > 1)
|
||||||
"""
|
sort { a, b -> comparator.compare(a, b) }
|
||||||
|
"""
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun f_asList() = f("asList()") {
|
fun f_asList() = fn("asList()") {
|
||||||
only(ArraysOfObjects)
|
include(ArraysOfObjects, ArraysOfPrimitives)
|
||||||
|
} builder {
|
||||||
doc { "Returns a [List] that wraps the original array." }
|
doc { "Returns a [List] that wraps the original array." }
|
||||||
returns("List<T>")
|
returns("List<T>")
|
||||||
body(Platform.JVM) {
|
on(Platform.JVM) {
|
||||||
"""
|
body { """return ArraysUtilJVM.asList(this)""" }
|
||||||
return ArraysUtilJVM.asList(this)
|
}
|
||||||
"""
|
on(Platform.JS) {
|
||||||
|
body { """return ArrayList<T>(this.unsafeCast<Array<Any?>>())""" }
|
||||||
}
|
}
|
||||||
|
|
||||||
body(Platform.JS) {
|
specialFor(ArraysOfPrimitives) {
|
||||||
"""
|
val objectLiteralImpl = """
|
||||||
return ArrayList<T>(this.unsafeCast<Array<Any?>>())
|
|
||||||
"""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun f_asListPrimitives() =
|
|
||||||
PrimitiveType.defaultPrimitives.map { primitive ->
|
|
||||||
f("asList()") {
|
|
||||||
only(ArraysOfPrimitives)
|
|
||||||
only(primitive)
|
|
||||||
doc { "Returns a [List] that wraps the original array." }
|
|
||||||
returns("List<T>")
|
|
||||||
|
|
||||||
val objectLiteralImpl = """
|
|
||||||
return object : AbstractList<T>(), RandomAccess {
|
return object : AbstractList<T>(), RandomAccess {
|
||||||
override val size: Int get() = this@asList.size
|
override val size: Int get() = this@asList.size
|
||||||
override fun isEmpty(): Boolean = this@asList.isEmpty()
|
override fun isEmpty(): Boolean = this@asList.isEmpty()
|
||||||
@@ -490,62 +491,51 @@ object CommonArrays {
|
|||||||
override fun lastIndexOf(element: T): Int = this@asList.lastIndexOf(element)
|
override fun lastIndexOf(element: T): Int = this@asList.lastIndexOf(element)
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
|
on(Platform.JVM) {
|
||||||
body(Platform.JVM) { objectLiteralImpl }
|
body { objectLiteralImpl }
|
||||||
|
}
|
||||||
if (primitive == PrimitiveType.Char) {
|
on(Platform.JS) {
|
||||||
body(Platform.JS) { objectLiteralImpl }
|
if (primitive == PrimitiveType.Char) {
|
||||||
}
|
body { objectLiteralImpl }
|
||||||
else {
|
}
|
||||||
inline(Platform.JS, Inline.Only, ArraysOfPrimitives)
|
else {
|
||||||
body(Platform.JS) { "return this.unsafeCast<Array<T>>().asList()" }
|
inlineOnly()
|
||||||
}
|
body { "return this.unsafeCast<Array<T>>().asList()" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun f_toTypedArray() =
|
fun f_toTypedArray() = fn("toTypedArray()") {
|
||||||
PrimitiveType.defaultPrimitives.map { primitive ->
|
include(ArraysOfPrimitives)
|
||||||
f("toTypedArray()") {
|
} builder {
|
||||||
only(ArraysOfPrimitives)
|
returns("Array<T>")
|
||||||
only(primitive)
|
doc {
|
||||||
returns("Array<T>")
|
"""
|
||||||
doc {
|
Returns a *typed* object array containing all of the elements of this primitive array.
|
||||||
"""
|
"""
|
||||||
Returns a *typed* object array containing all of the elements of this primitive array.
|
}
|
||||||
"""
|
on(Platform.JVM) {
|
||||||
}
|
body {
|
||||||
body(Platform.JVM) {
|
"""
|
||||||
"""
|
val result = arrayOfNulls<T>(size)
|
||||||
val result = arrayOfNulls<T>(size)
|
for (index in indices)
|
||||||
for (index in indices)
|
result[index] = this[index]
|
||||||
result[index] = this[index]
|
@Suppress("UNCHECKED_CAST")
|
||||||
@Suppress("UNCHECKED_CAST")
|
return result as Array<T>
|
||||||
return result as Array<T>
|
"""
|
||||||
"""
|
}
|
||||||
}
|
}
|
||||||
when (primitive) {
|
on(Platform.JS) {
|
||||||
PrimitiveType.Char ->
|
when (primitive) {
|
||||||
body(Platform.JS) { "return Array<Char>(size, { i -> this[i] })" }
|
PrimitiveType.Char ->
|
||||||
PrimitiveType.Boolean, PrimitiveType.Long ->
|
body { "return Array<Char>(size, { i -> this[i] })" }
|
||||||
body(Platform.JS) { "return copyOf().unsafeCast<Array<T>>()" }
|
PrimitiveType.Boolean, PrimitiveType.Long ->
|
||||||
else ->
|
body { "return copyOf().unsafeCast<Array<T>>()" }
|
||||||
body(Platform.JS) { "return js(\"[]\").slice.call(this)" }
|
else ->
|
||||||
}
|
body { "return js(\"[]\").slice.call(this)" }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
// TODO: use reflection later to get all functions of matching type
|
}
|
||||||
fun templates() =
|
}
|
||||||
listOf(f_plusElement()) +
|
|
||||||
f_plusElementOperator() +
|
|
||||||
f_plusCollection() +
|
|
||||||
listOf(f_plusArray()) +
|
|
||||||
f_copyOf() +
|
|
||||||
f_copyOfRange() +
|
|
||||||
f_copyOfResized() +
|
|
||||||
f_sortPrimitives() +
|
|
||||||
listOf(f_sort(), f_sortWith(), f_asList()) +
|
|
||||||
f_asListPrimitives() +
|
|
||||||
f_toTypedArray()
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user