Array.plus — different implementations for JVM and JS.
Array.copyOf, copyOfRange available in JS.
This commit is contained in:
@@ -6,48 +6,37 @@ fun generators(): List<GenericFunction> {
|
||||
val templates = arrayListOf<GenericFunction>()
|
||||
|
||||
templates add f("plus(element: T)") {
|
||||
exclude(Strings)
|
||||
only(Iterables, Collections, Sets, Sequences)
|
||||
doc { "Returns a list containing all elements of the original collection and then the given [element]." }
|
||||
returns("List<T>")
|
||||
returns("SELF", ArraysOfObjects, ArraysOfPrimitives, Sets, Sequences)
|
||||
body {
|
||||
"""
|
||||
if (this is Collection) return this.plus(element)
|
||||
val answer = ArrayList<T>()
|
||||
answer.addAll(this)
|
||||
answer.add(element)
|
||||
return answer
|
||||
val result = ArrayList<T>()
|
||||
result.addAll(this)
|
||||
result.add(element)
|
||||
return result
|
||||
"""
|
||||
}
|
||||
body(Collections) {
|
||||
"""
|
||||
val answer = ArrayList<T>(size() + 1)
|
||||
answer.addAll(this)
|
||||
answer.add(element)
|
||||
return answer
|
||||
"""
|
||||
}
|
||||
|
||||
|
||||
doc(ArraysOfObjects, ArraysOfPrimitives) { "Returns an array containing all elements of the original array and then the given [element]." }
|
||||
body(ArraysOfObjects, ArraysOfPrimitives) {
|
||||
"""
|
||||
val answer = this.copyOf(size() + 1)
|
||||
answer[size()] = element
|
||||
return answer as SELF
|
||||
val result = ArrayList<T>(size() + 1)
|
||||
result.addAll(this)
|
||||
result.add(element)
|
||||
return result
|
||||
"""
|
||||
}
|
||||
|
||||
// TODO: use build scope function when available
|
||||
// TODO: use immutable sets when available
|
||||
// TODO: precalculate size
|
||||
returns("SELF", Sets, Sequences)
|
||||
doc(Sets) { "Returns a set containing all elements of the original set and then the given [element]." }
|
||||
body(Sets) {
|
||||
"""
|
||||
val copyOfSet = LinkedHashSet<T>(mapCapacity(size() + 1))
|
||||
copyOfSet.addAll(this)
|
||||
copyOfSet.add(element)
|
||||
return copyOfSet
|
||||
val result = LinkedHashSet<T>(mapCapacity(size() + 1))
|
||||
result.addAll(this)
|
||||
result.add(element)
|
||||
return result
|
||||
"""
|
||||
}
|
||||
|
||||
@@ -67,23 +56,24 @@ fun generators(): List<GenericFunction> {
|
||||
body {
|
||||
"""
|
||||
if (this is Collection) return this.plus(collection)
|
||||
val answer = ArrayList<T>(0)
|
||||
answer.addAll(this)
|
||||
answer.addAll(collection)
|
||||
return answer
|
||||
val result = ArrayList<T>()
|
||||
result.addAll(this)
|
||||
result.addAll(collection)
|
||||
return result
|
||||
"""
|
||||
}
|
||||
body(Collections) {
|
||||
"""
|
||||
if (collection is Collection) return this.plus(collection)
|
||||
val answer = ArrayList<T>(this)
|
||||
answer.addAll(collection)
|
||||
return answer
|
||||
val result = ArrayList<T>(this)
|
||||
result.addAll(collection)
|
||||
return result
|
||||
"""
|
||||
}
|
||||
|
||||
// TODO: try to precalculate size
|
||||
// TODO: use immutable set builder when available
|
||||
doc(Sets) { "Returns a set containing all elements both of the original set and the given [collection]." }
|
||||
body(Sets) {
|
||||
"""
|
||||
val result = LinkedHashSet<T>(mapCapacity(collection.collectionSizeOrNull()?.let { this.size() + it } ?: this.size() * 2))
|
||||
@@ -93,7 +83,7 @@ fun generators(): List<GenericFunction> {
|
||||
"""
|
||||
}
|
||||
|
||||
doc(Sequences) { "Returns a sequence containing all elements of original sequence and then all elements of the given [collection]" }
|
||||
doc(Sequences) { "Returns a sequence containing all elements of original sequence and then all elements of the given [collection]." }
|
||||
body(Sequences) {
|
||||
"""
|
||||
return sequenceOf(this, collection.asSequence()).flatten()
|
||||
@@ -102,52 +92,42 @@ fun generators(): List<GenericFunction> {
|
||||
}
|
||||
|
||||
templates add f("plus(collection: Collection<T>)") {
|
||||
only(Collections, ArraysOfPrimitives, ArraysOfObjects)
|
||||
only(Collections)
|
||||
doc { "Returns a list containing all elements of the original collection and then all elements of the given [collection]." }
|
||||
returns("List<T>")
|
||||
returns("SELF", ArraysOfObjects, ArraysOfPrimitives)
|
||||
body(Collections) {
|
||||
body {
|
||||
"""
|
||||
val answer = ArrayList<T>(this.size() + collection.size())
|
||||
answer.addAll(this)
|
||||
answer.addAll(collection)
|
||||
return answer
|
||||
"""
|
||||
}
|
||||
// ?
|
||||
body(ArraysOfObjects, ArraysOfPrimitives) {
|
||||
"""
|
||||
val thisSize = size()
|
||||
val answer = this.copyOf(thisSize + collection.size())
|
||||
collection.forEachIndexed { i, element ->
|
||||
answer[thisSize + i] = element
|
||||
}
|
||||
return answer as SELF
|
||||
val result = ArrayList<T>(this.size() + collection.size())
|
||||
result.addAll(this)
|
||||
result.addAll(collection)
|
||||
return result
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("plus(array: Array<out T>)") {
|
||||
only(Iterables, Collections, Sets, Sequences)
|
||||
doc { "Returns a list containing all elements of original collection and then all elements of the given [array]." }
|
||||
doc { "Returns a list containing all elements of the original collection and then all elements of the given [array]." }
|
||||
returns("List<T>")
|
||||
returns("SELF", Sets, Sequences)
|
||||
body {
|
||||
"""
|
||||
if (this is Collection) return this.plus(array)
|
||||
val answer = ArrayList<T>()
|
||||
answer.addAll(this)
|
||||
answer.addAll(array)
|
||||
return answer
|
||||
val result = ArrayList<T>()
|
||||
result.addAll(this)
|
||||
result.addAll(array)
|
||||
return result
|
||||
"""
|
||||
}
|
||||
body(Collections) {
|
||||
"""
|
||||
val answer = ArrayList<T>(this.size() + array.size())
|
||||
answer.addAll(this)
|
||||
answer.addAll(array)
|
||||
return answer
|
||||
val result = ArrayList<T>(this.size() + array.size())
|
||||
result.addAll(this)
|
||||
result.addAll(array)
|
||||
return result
|
||||
"""
|
||||
}
|
||||
doc(Sets) { "Returns a set containing all elements both of the original set and the given [array]." }
|
||||
body(Sets) {
|
||||
"""
|
||||
val result = LinkedHashSet<T>(mapCapacity(this.size() + array.size()))
|
||||
@@ -156,6 +136,7 @@ fun generators(): List<GenericFunction> {
|
||||
return result
|
||||
"""
|
||||
}
|
||||
doc(Sequences) { "Returns a sequence containing all elements of original sequence and then all elements of the given [array]." }
|
||||
body(Sequences) {
|
||||
"""
|
||||
return this.plus(array.asList())
|
||||
@@ -163,20 +144,6 @@ fun generators(): List<GenericFunction> {
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("plus(collection: SELF)") {
|
||||
only(ArraysOfObjects, ArraysOfPrimitives)
|
||||
returns("SELF")
|
||||
body {
|
||||
"""
|
||||
val thisSize = size()
|
||||
val answer = this.copyOf(thisSize + collection.size())
|
||||
collection.forEachIndexed { i, element ->
|
||||
answer[thisSize + i] = element
|
||||
}
|
||||
return answer as SELF
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("plus(sequence: Sequence<T>)") {
|
||||
only(Sequences)
|
||||
|
||||
@@ -21,5 +21,98 @@ fun specialJS(): List<GenericFunction> {
|
||||
body(ArraysOfPrimitives) {"""return (this as Array<T>).asList()"""}
|
||||
}
|
||||
|
||||
templates add f("copyOfRange(from: Int, to: Int)") {
|
||||
// TODO: Arguments checking as in java?
|
||||
only(ArraysOfObjects, ArraysOfPrimitives)
|
||||
doc { "Returns new array which is a copy of range of original array." }
|
||||
inline(true)
|
||||
annotations("""suppress("NOTHING_TO_INLINE")""")
|
||||
returns("SELF")
|
||||
returns(ArraysOfObjects) { "Array<T>" }
|
||||
body {
|
||||
"return (this: dynamic).slice(from, to)"
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("copyOf()") {
|
||||
only(ArraysOfObjects, ArraysOfPrimitives)
|
||||
doc { "Returns new array which is a copy of the original array." }
|
||||
inline(true)
|
||||
annotations("""suppress("NOTHING_TO_INLINE")""")
|
||||
returns("SELF")
|
||||
returns(ArraysOfObjects) { "Array<T>" }
|
||||
body {
|
||||
"return (this: dynamic).slice(0)"
|
||||
}
|
||||
}
|
||||
|
||||
val allArrays = PrimitiveType.defaultPrimitives.map { ArraysOfPrimitives to it } + (ArraysOfObjects to null)
|
||||
templates addAll allArrays.map {
|
||||
val (family, primitive) = it
|
||||
f("copyOf(newSize: Int)") {
|
||||
only(family)
|
||||
val defaultValue: String
|
||||
if (primitive != null) {
|
||||
returns("SELF")
|
||||
only(primitive)
|
||||
defaultValue = when (primitive) {
|
||||
PrimitiveType.Boolean -> false.toString()
|
||||
PrimitiveType.Char -> "'\\u0000'"
|
||||
else -> "ZERO"
|
||||
}
|
||||
} else {
|
||||
returns(ArraysOfObjects) { "Array<T?>" }
|
||||
defaultValue = "null"
|
||||
}
|
||||
doc { "Returns new array which is a copy of the original array." }
|
||||
body {
|
||||
"""
|
||||
return arrayCopyResize(this, newSize, $defaultValue)
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
templates add f("plus(element: T)") {
|
||||
only(ArraysOfObjects, ArraysOfPrimitives)
|
||||
returns("SELF")
|
||||
doc { "Returns an array containing all elements of the original array and then the given [element]." }
|
||||
body(ArraysOfObjects, ArraysOfPrimitives) {
|
||||
"""
|
||||
val result = this.copyOf()
|
||||
(result: dynamic).push(element)
|
||||
return result as SELF
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("plus(collection: Collection<T>)") {
|
||||
only(ArraysOfObjects, ArraysOfPrimitives)
|
||||
returns("SELF")
|
||||
doc { "Returns an array containing all elements of the original array and then all elements of the given [collection]." }
|
||||
body {
|
||||
"""
|
||||
return arrayPlusCollection(this, collection)
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
// This overload can cause nulls if array size is expanding, hence different return overload
|
||||
templates add f("plus(array: SELF)") {
|
||||
only(ArraysOfObjects, ArraysOfPrimitives)
|
||||
doc { "Returns an array containing all elements of the original array and then all elements of the given [array]." }
|
||||
inline(true)
|
||||
annotations("""suppress("NOTHING_TO_INLINE")""")
|
||||
returns("SELF")
|
||||
body {
|
||||
"""
|
||||
return (this: dynamic).concat(array)
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
return templates
|
||||
}
|
||||
@@ -5,6 +5,51 @@ import templates.Family.*
|
||||
fun specialJVM(): List<GenericFunction> {
|
||||
val templates = arrayListOf<GenericFunction>()
|
||||
|
||||
templates add f("plus(element: T)") {
|
||||
only(ArraysOfObjects, ArraysOfPrimitives)
|
||||
returns("SELF")
|
||||
doc { "Returns an array containing all elements of the original array and then the given [element]." }
|
||||
body(ArraysOfObjects, ArraysOfPrimitives) {
|
||||
"""
|
||||
val result = this.copyOf(size() + 1)
|
||||
result[size()] = element
|
||||
return result as SELF
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("plus(collection: Collection<T>)") {
|
||||
only(ArraysOfObjects, ArraysOfPrimitives)
|
||||
returns("SELF")
|
||||
doc { "Returns an array containing all elements of the original array and then all elements of the given [collection]." }
|
||||
body {
|
||||
"""
|
||||
val thisSize = size()
|
||||
val result = this.copyOf(thisSize + collection.size())
|
||||
collection.forEachIndexed { i, element ->
|
||||
result[thisSize + i] = element
|
||||
}
|
||||
return result as SELF
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("plus(array: SELF)") {
|
||||
only(ArraysOfObjects, ArraysOfPrimitives)
|
||||
doc { "Returns an array containing all elements of the original array and then all elements of the given [array]." }
|
||||
returns("SELF")
|
||||
body {
|
||||
"""
|
||||
val thisSize = size()
|
||||
val arraySize = array.size()
|
||||
val result = this.copyOf(thisSize + arraySize)
|
||||
System.arraycopy(array, 0, result, thisSize, arraySize)
|
||||
return result as SELF
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
templates add f("copyOfRange(from: Int, to: Int)") {
|
||||
only(ArraysOfObjects, ArraysOfPrimitives)
|
||||
doc { "Returns new array which is a copy of range of original array." }
|
||||
|
||||
Reference in New Issue
Block a user