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
+10 -10
View File
@@ -12,7 +12,7 @@ import java.util.*
*/
public fun <T> Array<out T>.drop(n: Int): List<T> {
if (n >= size())
return ArrayList<T>()
return emptyList()
var count = 0
val list = ArrayList<T>(size() - n)
for (item in this) {
@@ -26,7 +26,7 @@ public fun <T> Array<out T>.drop(n: Int): List<T> {
*/
public fun BooleanArray.drop(n: Int): List<Boolean> {
if (n >= size())
return ArrayList<Boolean>()
return emptyList()
var count = 0
val list = ArrayList<Boolean>(size() - n)
for (item in this) {
@@ -40,7 +40,7 @@ public fun BooleanArray.drop(n: Int): List<Boolean> {
*/
public fun ByteArray.drop(n: Int): List<Byte> {
if (n >= size())
return ArrayList<Byte>()
return emptyList()
var count = 0
val list = ArrayList<Byte>(size() - n)
for (item in this) {
@@ -54,7 +54,7 @@ public fun ByteArray.drop(n: Int): List<Byte> {
*/
public fun CharArray.drop(n: Int): List<Char> {
if (n >= size())
return ArrayList<Char>()
return emptyList()
var count = 0
val list = ArrayList<Char>(size() - n)
for (item in this) {
@@ -68,7 +68,7 @@ public fun CharArray.drop(n: Int): List<Char> {
*/
public fun DoubleArray.drop(n: Int): List<Double> {
if (n >= size())
return ArrayList<Double>()
return emptyList()
var count = 0
val list = ArrayList<Double>(size() - n)
for (item in this) {
@@ -82,7 +82,7 @@ public fun DoubleArray.drop(n: Int): List<Double> {
*/
public fun FloatArray.drop(n: Int): List<Float> {
if (n >= size())
return ArrayList<Float>()
return emptyList()
var count = 0
val list = ArrayList<Float>(size() - n)
for (item in this) {
@@ -96,7 +96,7 @@ public fun FloatArray.drop(n: Int): List<Float> {
*/
public fun IntArray.drop(n: Int): List<Int> {
if (n >= size())
return ArrayList<Int>()
return emptyList()
var count = 0
val list = ArrayList<Int>(size() - n)
for (item in this) {
@@ -110,7 +110,7 @@ public fun IntArray.drop(n: Int): List<Int> {
*/
public fun LongArray.drop(n: Int): List<Long> {
if (n >= size())
return ArrayList<Long>()
return emptyList()
var count = 0
val list = ArrayList<Long>(size() - n)
for (item in this) {
@@ -124,7 +124,7 @@ public fun LongArray.drop(n: Int): List<Long> {
*/
public fun ShortArray.drop(n: Int): List<Short> {
if (n >= size())
return ArrayList<Short>()
return emptyList()
var count = 0
val list = ArrayList<Short>(size() - n)
for (item in this) {
@@ -138,7 +138,7 @@ public fun ShortArray.drop(n: Int): List<Short> {
*/
public fun <T> Collection<T>.drop(n: Int): List<T> {
if (n >= size())
return ArrayList<T>()
return emptyList()
var count = 0
val list = ArrayList<T>(size() - n)
for (item in this) {
+21 -61
View File
@@ -13,7 +13,7 @@ import java.util.*
public inline fun <T, R, V> Array<out T>.merge(array: Array<out R>, transform: (T, R) -> V): List<V> {
val first = iterator()
val second = array.iterator()
val list = arrayListOf<V>()
val list = ArrayList<V>(size())
while (first.hasNext() && second.hasNext()) {
list.add(transform(first.next(), second.next()))
}
@@ -26,7 +26,7 @@ public inline fun <T, R, V> Array<out T>.merge(array: Array<out R>, transform: (
public inline fun <R, V> BooleanArray.merge(array: Array<out R>, transform: (Boolean, R) -> V): List<V> {
val first = iterator()
val second = array.iterator()
val list = arrayListOf<V>()
val list = ArrayList<V>(size())
while (first.hasNext() && second.hasNext()) {
list.add(transform(first.next(), second.next()))
}
@@ -39,7 +39,7 @@ public inline fun <R, V> BooleanArray.merge(array: Array<out R>, transform: (Boo
public inline fun <R, V> ByteArray.merge(array: Array<out R>, transform: (Byte, R) -> V): List<V> {
val first = iterator()
val second = array.iterator()
val list = arrayListOf<V>()
val list = ArrayList<V>(size())
while (first.hasNext() && second.hasNext()) {
list.add(transform(first.next(), second.next()))
}
@@ -52,7 +52,7 @@ public inline fun <R, V> ByteArray.merge(array: Array<out R>, transform: (Byte,
public inline fun <R, V> CharArray.merge(array: Array<out R>, transform: (Char, R) -> V): List<V> {
val first = iterator()
val second = array.iterator()
val list = arrayListOf<V>()
val list = ArrayList<V>(size())
while (first.hasNext() && second.hasNext()) {
list.add(transform(first.next(), second.next()))
}
@@ -65,7 +65,7 @@ public inline fun <R, V> CharArray.merge(array: Array<out R>, transform: (Char,
public inline fun <R, V> DoubleArray.merge(array: Array<out R>, transform: (Double, R) -> V): List<V> {
val first = iterator()
val second = array.iterator()
val list = arrayListOf<V>()
val list = ArrayList<V>(size())
while (first.hasNext() && second.hasNext()) {
list.add(transform(first.next(), second.next()))
}
@@ -78,7 +78,7 @@ public inline fun <R, V> DoubleArray.merge(array: Array<out R>, transform: (Doub
public inline fun <R, V> FloatArray.merge(array: Array<out R>, transform: (Float, R) -> V): List<V> {
val first = iterator()
val second = array.iterator()
val list = arrayListOf<V>()
val list = ArrayList<V>(size())
while (first.hasNext() && second.hasNext()) {
list.add(transform(first.next(), second.next()))
}
@@ -91,7 +91,7 @@ public inline fun <R, V> FloatArray.merge(array: Array<out R>, transform: (Float
public inline fun <R, V> IntArray.merge(array: Array<out R>, transform: (Int, R) -> V): List<V> {
val first = iterator()
val second = array.iterator()
val list = arrayListOf<V>()
val list = ArrayList<V>(size())
while (first.hasNext() && second.hasNext()) {
list.add(transform(first.next(), second.next()))
}
@@ -104,7 +104,7 @@ public inline fun <R, V> IntArray.merge(array: Array<out R>, transform: (Int, R)
public inline fun <R, V> LongArray.merge(array: Array<out R>, transform: (Long, R) -> V): List<V> {
val first = iterator()
val second = array.iterator()
val list = arrayListOf<V>()
val list = ArrayList<V>(size())
while (first.hasNext() && second.hasNext()) {
list.add(transform(first.next(), second.next()))
}
@@ -117,7 +117,7 @@ public inline fun <R, V> LongArray.merge(array: Array<out R>, transform: (Long,
public inline fun <R, V> ShortArray.merge(array: Array<out R>, transform: (Short, R) -> V): List<V> {
val first = iterator()
val second = array.iterator()
val list = arrayListOf<V>()
val list = ArrayList<V>(size())
while (first.hasNext() && second.hasNext()) {
list.add(transform(first.next(), second.next()))
}
@@ -130,20 +130,7 @@ public inline fun <R, V> ShortArray.merge(array: Array<out R>, transform: (Short
public inline fun <T, R, V> Iterable<T>.merge(array: Array<out R>, transform: (T, R) -> V): List<V> {
val first = iterator()
val second = array.iterator()
val list = arrayListOf<V>()
while (first.hasNext() && second.hasNext()) {
list.add(transform(first.next(), second.next()))
}
return list
}
/**
* Returns a list of values built from elements of both collections with same indexes using provided *transform*. List has length of shortest collection.
*/
public inline fun <R, V> String.merge(array: Array<out R>, transform: (Char, R) -> V): List<V> {
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()))
}
@@ -156,7 +143,7 @@ public inline fun <R, V> String.merge(array: Array<out R>, transform: (Char, R)
public inline fun <T, R, V> Array<out T>.merge(other: Iterable<R>, transform: (T, R) -> V): List<V> {
val first = iterator()
val second = other.iterator()
val list = arrayListOf<V>()
val list = ArrayList<V>(size())
while (first.hasNext() && second.hasNext()) {
list.add(transform(first.next(), second.next()))
}
@@ -169,7 +156,7 @@ public inline fun <T, R, V> Array<out T>.merge(other: Iterable<R>, transform: (T
public inline fun <R, V> BooleanArray.merge(other: Iterable<R>, transform: (Boolean, R) -> V): List<V> {
val first = iterator()
val second = other.iterator()
val list = arrayListOf<V>()
val list = ArrayList<V>(size())
while (first.hasNext() && second.hasNext()) {
list.add(transform(first.next(), second.next()))
}
@@ -182,7 +169,7 @@ public inline fun <R, V> BooleanArray.merge(other: Iterable<R>, transform: (Bool
public inline fun <R, V> ByteArray.merge(other: Iterable<R>, transform: (Byte, R) -> V): List<V> {
val first = iterator()
val second = other.iterator()
val list = arrayListOf<V>()
val list = ArrayList<V>(size())
while (first.hasNext() && second.hasNext()) {
list.add(transform(first.next(), second.next()))
}
@@ -195,7 +182,7 @@ public inline fun <R, V> ByteArray.merge(other: Iterable<R>, transform: (Byte, R
public inline fun <R, V> CharArray.merge(other: Iterable<R>, transform: (Char, R) -> V): List<V> {
val first = iterator()
val second = other.iterator()
val list = arrayListOf<V>()
val list = ArrayList<V>(size())
while (first.hasNext() && second.hasNext()) {
list.add(transform(first.next(), second.next()))
}
@@ -208,7 +195,7 @@ public inline fun <R, V> CharArray.merge(other: Iterable<R>, transform: (Char, R
public inline fun <R, V> DoubleArray.merge(other: Iterable<R>, transform: (Double, R) -> V): List<V> {
val first = iterator()
val second = other.iterator()
val list = arrayListOf<V>()
val list = ArrayList<V>(size())
while (first.hasNext() && second.hasNext()) {
list.add(transform(first.next(), second.next()))
}
@@ -221,7 +208,7 @@ public inline fun <R, V> DoubleArray.merge(other: Iterable<R>, transform: (Doubl
public inline fun <R, V> FloatArray.merge(other: Iterable<R>, transform: (Float, R) -> V): List<V> {
val first = iterator()
val second = other.iterator()
val list = arrayListOf<V>()
val list = ArrayList<V>(size())
while (first.hasNext() && second.hasNext()) {
list.add(transform(first.next(), second.next()))
}
@@ -234,7 +221,7 @@ public inline fun <R, V> FloatArray.merge(other: Iterable<R>, transform: (Float,
public inline fun <R, V> IntArray.merge(other: Iterable<R>, transform: (Int, R) -> V): List<V> {
val first = iterator()
val second = other.iterator()
val list = arrayListOf<V>()
val list = ArrayList<V>(size())
while (first.hasNext() && second.hasNext()) {
list.add(transform(first.next(), second.next()))
}
@@ -247,7 +234,7 @@ public inline fun <R, V> IntArray.merge(other: Iterable<R>, transform: (Int, R)
public inline fun <R, V> LongArray.merge(other: Iterable<R>, transform: (Long, R) -> V): List<V> {
val first = iterator()
val second = other.iterator()
val list = arrayListOf<V>()
val list = ArrayList<V>(size())
while (first.hasNext() && second.hasNext()) {
list.add(transform(first.next(), second.next()))
}
@@ -260,7 +247,7 @@ public inline fun <R, V> LongArray.merge(other: Iterable<R>, transform: (Long, R
public inline fun <R, V> ShortArray.merge(other: Iterable<R>, transform: (Short, R) -> V): List<V> {
val first = iterator()
val second = other.iterator()
val list = arrayListOf<V>()
val list = ArrayList<V>(size())
while (first.hasNext() && second.hasNext()) {
list.add(transform(first.next(), second.next()))
}
@@ -273,20 +260,7 @@ public inline fun <R, V> ShortArray.merge(other: Iterable<R>, transform: (Short,
public inline fun <T, R, V> Iterable<T>.merge(other: Iterable<R>, transform: (T, R) -> V): List<V> {
val first = iterator()
val second = other.iterator()
val list = arrayListOf<V>()
while (first.hasNext() && second.hasNext()) {
list.add(transform(first.next(), second.next()))
}
return list
}
/**
* Returns a list of values built from elements of both collections with same indexes using provided *transform*. List has length of shortest collection.
*/
public inline fun <R, V> String.merge(other: Iterable<R>, transform: (Char, R) -> V): List<V> {
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()))
}
@@ -877,13 +851,6 @@ public fun <T, R> Iterable<T>.zip(array: Array<out R>): List<Pair<T, R>> {
return merge(array) { (t1, t2) -> t1 to t2 }
}
/**
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
*/
public fun <R> String.zip(array: Array<out R>): List<Pair<Char, R>> {
return merge(array) { (t1, t2) -> t1 to t2 }
}
/**
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
*/
@@ -954,20 +921,13 @@ public fun <T, R> Iterable<T>.zip(other: Iterable<R>): List<Pair<T, R>> {
return merge(other) { (t1, t2) -> t1 to t2 }
}
/**
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
*/
public fun <R> String.zip(other: Iterable<R>): List<Pair<Char, R>> {
return merge(other) { (t1, t2) -> t1 to t2 }
}
/**
* Returns a list of pairs built from characters of both strings with same indexes. List has length of shortest collection.
*/
public fun String.zip(other: String): List<Pair<Char, Char>> {
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())
}
+11 -11
View File
@@ -564,70 +564,70 @@ public inline fun <R> String.map(transform: (Char) -> R): List<R> {
* Returns a list containing the results of applying the given *transform* function to each element and its index of the original collection
*/
public inline fun <T, R> Array<out T>.mapIndexed(transform: (Int, T) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(), transform)
return mapIndexedTo(ArrayList<R>(size()), transform)
}
/**
* Returns a list containing the results of applying the given *transform* function to each element and its index of the original collection
*/
public inline fun <R> BooleanArray.mapIndexed(transform: (Int, Boolean) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(), transform)
return mapIndexedTo(ArrayList<R>(size()), transform)
}
/**
* Returns a list containing the results of applying the given *transform* function to each element and its index of the original collection
*/
public inline fun <R> ByteArray.mapIndexed(transform: (Int, Byte) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(), transform)
return mapIndexedTo(ArrayList<R>(size()), transform)
}
/**
* Returns a list containing the results of applying the given *transform* function to each element and its index of the original collection
*/
public inline fun <R> CharArray.mapIndexed(transform: (Int, Char) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(), transform)
return mapIndexedTo(ArrayList<R>(size()), transform)
}
/**
* Returns a list containing the results of applying the given *transform* function to each element and its index of the original collection
*/
public inline fun <R> DoubleArray.mapIndexed(transform: (Int, Double) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(), transform)
return mapIndexedTo(ArrayList<R>(size()), transform)
}
/**
* Returns a list containing the results of applying the given *transform* function to each element and its index of the original collection
*/
public inline fun <R> FloatArray.mapIndexed(transform: (Int, Float) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(), transform)
return mapIndexedTo(ArrayList<R>(size()), transform)
}
/**
* Returns a list containing the results of applying the given *transform* function to each element and its index of the original collection
*/
public inline fun <R> IntArray.mapIndexed(transform: (Int, Int) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(), transform)
return mapIndexedTo(ArrayList<R>(size()), transform)
}
/**
* Returns a list containing the results of applying the given *transform* function to each element and its index of the original collection
*/
public inline fun <R> LongArray.mapIndexed(transform: (Int, Long) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(), transform)
return mapIndexedTo(ArrayList<R>(size()), transform)
}
/**
* Returns a list containing the results of applying the given *transform* function to each element and its index of the original collection
*/
public inline fun <R> ShortArray.mapIndexed(transform: (Int, Short) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(), transform)
return mapIndexedTo(ArrayList<R>(size()), transform)
}
/**
* Returns a list containing the results of applying the given *transform* function to each element and its index of the original collection
*/
public inline fun <T, R> Iterable<T>.mapIndexed(transform: (Int, T) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(), transform)
return mapIndexedTo(ArrayList<R>(collectionSizeOrDefault(10)), transform)
}
/**
@@ -641,7 +641,7 @@ public fun <T, R> Stream<T>.mapIndexed(transform: (Int, T) -> R): Stream<R> {
* Returns a list containing the results of applying the given *transform* function to each element and its index of the original collection
*/
public inline fun <R> String.mapIndexed(transform: (Int, Char) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(), transform)
return mapIndexedTo(ArrayList<R>(length()), transform)
}
/**
+7 -5
View File
@@ -92,7 +92,7 @@ public fun ShortArray.toArrayList(): ArrayList<Short> {
* Returns an ArrayList of all elements
*/
public fun <T> Iterable<T>.toArrayList(): ArrayList<T> {
return toCollection(ArrayList<T>())
return toCollection(ArrayList<T>(collectionSizeOrDefault(10)))
}
/**
@@ -106,7 +106,7 @@ public fun <T> Stream<T>.toArrayList(): ArrayList<T> {
* Returns an ArrayList of all elements
*/
public fun String.toArrayList(): ArrayList<Char> {
return toCollection(ArrayList<Char>())
return toCollection(ArrayList<Char>(length()))
}
/**
@@ -411,7 +411,9 @@ public fun <K, V> Map<K, V>.toList(): List<Pair<K, V>> {
* Returns a List containing all elements
*/
public fun <T> Array<out T>.toList(): List<T> {
return toCollection(ArrayList<T>())
val list = ArrayList<T>(size())
for (item in this) list.add(item)
return list
}
/**
@@ -490,7 +492,7 @@ public fun ShortArray.toList(): List<Short> {
* Returns a List containing all elements
*/
public fun <T> Iterable<T>.toList(): List<T> {
return toCollection(ArrayList<T>())
return toCollection(ArrayList<T>(collectionSizeOrDefault(10)))
}
/**
@@ -504,7 +506,7 @@ public fun <T> Stream<T>.toList(): List<T> {
* Returns a List containing all elements
*/
public fun String.toList(): List<Char> {
return toCollection(ArrayList<Char>())
return toCollection(ArrayList<Char>(length()))
}
/**