Do not create iterator in 'any', 'none', 'all' and 'count' unless necessary

#KT-19133 Fixed
This commit is contained in:
Ilya Gorbunov
2017-07-26 06:25:06 +03:00
parent ff676c050f
commit ca8bf395c3
11 changed files with 116 additions and 113 deletions
+18 -36
View File
@@ -8950,72 +8950,63 @@ public inline fun CharArray.all(predicate: (Char) -> Boolean): Boolean {
* Returns `true` if array has at least one element.
*/
public fun <T> Array<out T>.any(): Boolean {
for (element in this) return true
return false
return !isEmpty()
}
/**
* Returns `true` if array has at least one element.
*/
public fun ByteArray.any(): Boolean {
for (element in this) return true
return false
return !isEmpty()
}
/**
* Returns `true` if array has at least one element.
*/
public fun ShortArray.any(): Boolean {
for (element in this) return true
return false
return !isEmpty()
}
/**
* Returns `true` if array has at least one element.
*/
public fun IntArray.any(): Boolean {
for (element in this) return true
return false
return !isEmpty()
}
/**
* Returns `true` if array has at least one element.
*/
public fun LongArray.any(): Boolean {
for (element in this) return true
return false
return !isEmpty()
}
/**
* Returns `true` if array has at least one element.
*/
public fun FloatArray.any(): Boolean {
for (element in this) return true
return false
return !isEmpty()
}
/**
* Returns `true` if array has at least one element.
*/
public fun DoubleArray.any(): Boolean {
for (element in this) return true
return false
return !isEmpty()
}
/**
* Returns `true` if array has at least one element.
*/
public fun BooleanArray.any(): Boolean {
for (element in this) return true
return false
return !isEmpty()
}
/**
* Returns `true` if array has at least one element.
*/
public fun CharArray.any(): Boolean {
for (element in this) return true
return false
return !isEmpty()
}
/**
@@ -10704,72 +10695,63 @@ public fun CharArray.minWith(comparator: Comparator<in Char>): Char? {
* Returns `true` if the array has no elements.
*/
public fun <T> Array<out T>.none(): Boolean {
for (element in this) return false
return true
return isEmpty()
}
/**
* Returns `true` if the array has no elements.
*/
public fun ByteArray.none(): Boolean {
for (element in this) return false
return true
return isEmpty()
}
/**
* Returns `true` if the array has no elements.
*/
public fun ShortArray.none(): Boolean {
for (element in this) return false
return true
return isEmpty()
}
/**
* Returns `true` if the array has no elements.
*/
public fun IntArray.none(): Boolean {
for (element in this) return false
return true
return isEmpty()
}
/**
* Returns `true` if the array has no elements.
*/
public fun LongArray.none(): Boolean {
for (element in this) return false
return true
return isEmpty()
}
/**
* Returns `true` if the array has no elements.
*/
public fun FloatArray.none(): Boolean {
for (element in this) return false
return true
return isEmpty()
}
/**
* Returns `true` if the array has no elements.
*/
public fun DoubleArray.none(): Boolean {
for (element in this) return false
return true
return isEmpty()
}
/**
* Returns `true` if the array has no elements.
*/
public fun BooleanArray.none(): Boolean {
for (element in this) return false
return true
return isEmpty()
}
/**
* Returns `true` if the array has no elements.
*/
public fun CharArray.none(): Boolean {
for (element in this) return false
return true
return isEmpty()
}
/**
@@ -1371,6 +1371,7 @@ public infix fun <T> Iterable<T>.union(other: Iterable<T>): Set<T> {
* Returns `true` if all elements match the given [predicate].
*/
public inline fun <T> Iterable<T>.all(predicate: (T) -> Boolean): Boolean {
if (this is Collection && isEmpty()) return true
for (element in this) if (!predicate(element)) return false
return true
}
@@ -1379,14 +1380,15 @@ public inline fun <T> Iterable<T>.all(predicate: (T) -> Boolean): Boolean {
* Returns `true` if collection has at least one element.
*/
public fun <T> Iterable<T>.any(): Boolean {
for (element in this) return true
return false
if (this is Collection) return !isEmpty()
return iterator().hasNext()
}
/**
* Returns `true` if at least one element matches the given [predicate].
*/
public inline fun <T> Iterable<T>.any(predicate: (T) -> Boolean): Boolean {
if (this is Collection && isEmpty()) return false
for (element in this) if (predicate(element)) return true
return false
}
@@ -1395,6 +1397,7 @@ public inline fun <T> Iterable<T>.any(predicate: (T) -> Boolean): Boolean {
* Returns the number of elements in this collection.
*/
public fun <T> Iterable<T>.count(): Int {
if (this is Collection) return size
var count = 0
for (element in this) count++
return count
@@ -1412,6 +1415,7 @@ public inline fun <T> Collection<T>.count(): Int {
* Returns the number of elements matching the given [predicate].
*/
public inline fun <T> Iterable<T>.count(predicate: (T) -> Boolean): Int {
if (this is Collection && isEmpty()) return 0
var count = 0
for (element in this) if (predicate(element)) count++
return count
@@ -1663,14 +1667,15 @@ public fun <T> Iterable<T>.minWith(comparator: Comparator<in T>): T? {
* Returns `true` if the collection has no elements.
*/
public fun <T> Iterable<T>.none(): Boolean {
for (element in this) return false
return true
if (this is Collection) return isEmpty()
return !iterator().hasNext()
}
/**
* Returns `true` if no elements match the given [predicate].
*/
public inline fun <T> Iterable<T>.none(predicate: (T) -> Boolean): Boolean {
if (this is Collection && isEmpty()) return true
for (element in this) if (predicate(element)) return false
return true
}
+6 -4
View File
@@ -88,6 +88,7 @@ public inline fun <K, V, R, C : MutableCollection<in R>> Map<out K, V>.mapTo(des
* Returns `true` if all entries match the given [predicate].
*/
public inline fun <K, V> Map<out K, V>.all(predicate: (Map.Entry<K, V>) -> Boolean): Boolean {
if (isEmpty()) return true
for (element in this) if (!predicate(element)) return false
return true
}
@@ -96,14 +97,14 @@ public inline fun <K, V> Map<out K, V>.all(predicate: (Map.Entry<K, V>) -> Boole
* Returns `true` if map has at least one entry.
*/
public fun <K, V> Map<out K, V>.any(): Boolean {
for (element in this) return true
return false
return !isEmpty()
}
/**
* Returns `true` if at least one entry matches the given [predicate].
*/
public inline fun <K, V> Map<out K, V>.any(predicate: (Map.Entry<K, V>) -> Boolean): Boolean {
if (isEmpty()) return false
for (element in this) if (predicate(element)) return true
return false
}
@@ -120,6 +121,7 @@ public inline fun <K, V> Map<out K, V>.count(): Int {
* Returns the number of entries matching the given [predicate].
*/
public inline fun <K, V> Map<out K, V>.count(predicate: (Map.Entry<K, V>) -> Boolean): Int {
if (isEmpty()) return 0
var count = 0
for (element in this) if (predicate(element)) count++
return count
@@ -167,14 +169,14 @@ public fun <K, V> Map<out K, V>.minWith(comparator: Comparator<in Map.Entry<K, V
* Returns `true` if the map has no entries.
*/
public fun <K, V> Map<out K, V>.none(): Boolean {
for (element in this) return false
return true
return isEmpty()
}
/**
* Returns `true` if no entries match the given [predicate].
*/
public inline fun <K, V> Map<out K, V>.none(predicate: (Map.Entry<K, V>) -> Boolean): Boolean {
if (isEmpty()) return true
for (element in this) if (predicate(element)) return false
return true
}
+2 -4
View File
@@ -958,8 +958,7 @@ public inline fun <T> Sequence<T>.all(predicate: (T) -> Boolean): Boolean {
* The operation is _terminal_.
*/
public fun <T> Sequence<T>.any(): Boolean {
for (element in this) return true
return false
return iterator().hasNext()
}
/**
@@ -1237,8 +1236,7 @@ public fun <T> Sequence<T>.minWith(comparator: Comparator<in T>): T? {
* The operation is _terminal_.
*/
public fun <T> Sequence<T>.none(): Boolean {
for (element in this) return false
return true
return !iterator().hasNext()
}
/**
+2 -4
View File
@@ -823,8 +823,7 @@ public inline fun CharSequence.all(predicate: (Char) -> Boolean): Boolean {
* Returns `true` if char sequence has at least one character.
*/
public fun CharSequence.any(): Boolean {
for (element in this) return true
return false
return !isEmpty()
}
/**
@@ -1011,8 +1010,7 @@ public fun CharSequence.minWith(comparator: Comparator<in Char>): Char? {
* Returns `true` if the char sequence has no characters.
*/
public fun CharSequence.none(): Boolean {
for (element in this) return false
return true
return isEmpty()
}
/**