Take into account collection sizes when possible #KT-6180

This commit is contained in:
Ilya Ryzhenkov
2014-12-22 23:34:46 +03:00
parent 4cd2ba1e30
commit d908678dcf
9 changed files with 97 additions and 123 deletions
@@ -33,7 +33,7 @@ fun filtering(): List<GenericFunction> {
body(Collections, ArraysOfObjects, ArraysOfPrimitives) {
"""
if (n >= size())
return ArrayList<T>()
return emptyList()
var count = 0
val list = ArrayList<T>(size() - n)
@@ -119,7 +119,7 @@ fun generators(): List<GenericFunction> {
}
templates add f("merge(other: Iterable<R>, transform: (T, R) -> V)") {
exclude(Streams)
exclude(Streams, Strings)
doc {
"""
Returns a list of values built from elements of both collections with same indexes using provided *transform*. List has length of shortest collection.
@@ -133,7 +133,18 @@ fun generators(): List<GenericFunction> {
"""
val first = iterator()
val second = other.iterator()
val list = arrayListOf<V>()
val list = ArrayList<V>(collectionSizeOrDefault(10))
while (first.hasNext() && second.hasNext()) {
list.add(transform(first.next(), second.next()))
}
return list
"""
}
body(ArraysOfObjects, ArraysOfPrimitives) {
"""
val first = iterator()
val second = other.iterator()
val list = ArrayList<V>(size())
while (first.hasNext() && second.hasNext()) {
list.add(transform(first.next(), second.next()))
}
@@ -143,7 +154,7 @@ fun generators(): List<GenericFunction> {
}
templates add f("merge(array: Array<out R>, transform: (T, R) -> V)") {
exclude(Streams)
exclude(Streams, Strings)
doc {
"""
Returns a list of values built from elements of both collections with same indexes using provided *transform*. List has length of shortest collection.
@@ -157,13 +168,25 @@ fun generators(): List<GenericFunction> {
"""
val first = iterator()
val second = array.iterator()
val list = arrayListOf<V>()
val list = ArrayList<V>(collectionSizeOrDefault(10))
while (first.hasNext() && second.hasNext()) {
list.add(transform(first.next(), second.next()))
}
return list
"""
}
body(ArraysOfObjects, ArraysOfPrimitives) {
"""
val first = iterator()
val second = array.iterator()
val list = ArrayList<V>(size())
while (first.hasNext() && second.hasNext()) {
list.add(transform(first.next(), second.next()))
}
return list
"""
}
}
@@ -186,7 +209,7 @@ fun generators(): List<GenericFunction> {
templates add f("zip(other: Iterable<R>)") {
exclude(Streams)
exclude(Streams, Strings)
doc {
"""
Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
@@ -213,7 +236,7 @@ fun generators(): List<GenericFunction> {
"""
val first = iterator()
val second = other.iterator()
val list = ArrayList<Pair<Char, Char>>()
val list = ArrayList<Pair<Char, Char>>(length())
while (first.hasNext() && second.hasNext()) {
list.add(first.next() to second.next())
}
@@ -223,7 +246,7 @@ fun generators(): List<GenericFunction> {
}
templates add f("zip(array: Array<out R>)") {
exclude(Streams)
exclude(Streams, Strings)
doc {
"""
Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
@@ -51,9 +51,14 @@ fun mapping(): List<GenericFunction> {
typeParam("R")
returns("List<R>")
body {
"return mapIndexedTo(ArrayList<R>(), transform)"
"return mapIndexedTo(ArrayList<R>(collectionSizeOrDefault(10)), transform)"
}
body(ArraysOfObjects, ArraysOfPrimitives) {
"return mapIndexedTo(ArrayList<R>(size()), transform)"
}
body(Strings) {
"return mapIndexedTo(ArrayList<R>(length()), transform)"
}
inline(false, Streams)
returns(Streams) { "Stream<R>" }
doc(Streams) { "Returns a stream containing the results of applying the given *transform* function to each element and its index of the original stream" }
@@ -40,16 +40,9 @@ fun snapshots(): List<GenericFunction> {
templates add f("toArrayList()") {
doc { "Returns an ArrayList of all elements" }
returns("ArrayList<T>")
body { "return toCollection(ArrayList<T>())" }
// ISSUE: JavaScript can't perform this operation
/*
body(Collections) {
"""
return ArrayList<T>(this)
"""
}
*/
body { "return toCollection(ArrayList<T>(collectionSizeOrDefault(10)))" }
body(Streams) { "return toCollection(ArrayList<T>())" }
body(Strings) { "return toCollection(ArrayList<T>(length()))" }
body(ArraysOfObjects, ArraysOfPrimitives) {
"""
val list = ArrayList<T>(size())
@@ -76,22 +69,10 @@ fun snapshots(): List<GenericFunction> {
templates add f("toList()") {
doc { "Returns a List containing all elements" }
returns("List<T>")
body { "return toCollection(ArrayList<T>())" }
// ISSUE: JavaScript can't perform this operations
/*
body(Collections) {
"""
return ArrayList<T>(this)
"""
}
body(ArraysOfObjects) {
"""
return ArrayList<T>(Arrays.asList(*this))
"""
}
*/
body(ArraysOfPrimitives) {
body { "return toCollection(ArrayList<T>(collectionSizeOrDefault(10)))" }
body(Streams) { "return toCollection(ArrayList<T>())" }
body(Strings) { "return toCollection(ArrayList<T>(length()))" }
body(ArraysOfObjects, ArraysOfPrimitives) {
"""
val list = ArrayList<T>(size())
for (item in this) list.add(item)