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
@@ -8880,72 +8880,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()
}
/**
@@ -10634,72 +10625,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()
}
/**
@@ -1361,6 +1361,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
}
@@ -1369,14 +1370,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
}
@@ -1385,6 +1387,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
@@ -1402,6 +1405,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
@@ -1653,14 +1657,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
}
@@ -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
}
@@ -936,8 +936,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()
}
/**
@@ -1215,8 +1214,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()
}
/**
@@ -815,8 +815,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()
}
/**
@@ -1003,8 +1002,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()
}
/**
+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()
}
/**
@@ -10,8 +10,13 @@ fun aggregates(): List<GenericFunction> {
inline(true)
doc { f -> "Returns `true` if all ${f.element.pluralize()} match the given [predicate]." }
returns("Boolean")
body {
body { f ->
"""
${when (f) {
Iterables -> "if (this is Collection && isEmpty()) return true"
Maps -> "if (isEmpty()) return true"
else -> ""
}}
for (element in this) if (!predicate(element)) return false
return true
"""
@@ -24,8 +29,13 @@ fun aggregates(): List<GenericFunction> {
doc { f -> "Returns `true` if no ${f.element.pluralize()} match the given [predicate]." }
returns("Boolean")
body {
body { f ->
"""
${when (f) {
Iterables -> "if (this is Collection && isEmpty()) return true"
Maps -> "if (isEmpty()) return true"
else -> ""
}}
for (element in this) if (predicate(element)) return false
return true
"""
@@ -37,11 +47,17 @@ fun aggregates(): List<GenericFunction> {
doc { f -> "Returns `true` if the ${f.collection} has no ${f.element.pluralize()}." }
returns("Boolean")
body {
"return !iterator().hasNext()"
}
body(Iterables) {
"""
for (element in this) return false
return true
if (this is Collection) return isEmpty()
return !iterator().hasNext()
"""
}
body(Maps, CharSequences, ArraysOfObjects, ArraysOfPrimitives) {
"return isEmpty()"
}
include(Maps, CharSequences)
}
@@ -50,8 +66,13 @@ fun aggregates(): List<GenericFunction> {
doc { f -> "Returns `true` if at least one ${f.element} matches the given [predicate]." }
returns("Boolean")
body {
body { f ->
"""
${when (f) {
Iterables -> "if (this is Collection && isEmpty()) return false"
Maps -> "if (isEmpty()) return false"
else -> ""
}}
for (element in this) if (predicate(element)) return true
return false
"""
@@ -63,11 +84,17 @@ fun aggregates(): List<GenericFunction> {
doc { f -> "Returns `true` if ${f.collection} has at least one ${f.element}." }
returns("Boolean")
body {
"return iterator().hasNext()"
}
body(Iterables) {
"""
for (element in this) return true
return false
if (this is Collection) return !isEmpty()
return iterator().hasNext()
"""
}
body(Maps, CharSequences, ArraysOfObjects, ArraysOfPrimitives) {
"return !isEmpty()"
}
include(Maps, CharSequences)
}
@@ -76,8 +103,13 @@ fun aggregates(): List<GenericFunction> {
doc { f -> "Returns the number of ${f.element.pluralize()} matching the given [predicate]." }
returns("Int")
body {
body { f ->
"""
${when (f) {
Iterables -> "if (this is Collection && isEmpty()) return 0"
Maps -> "if (isEmpty()) return 0"
else -> ""
}}
var count = 0
for (element in this) if (predicate(element)) count++
return count
@@ -89,8 +121,9 @@ fun aggregates(): List<GenericFunction> {
templates add f("count()") {
doc { f -> "Returns the number of ${f.element.pluralize()} in this ${f.collection}." }
returns("Int")
body {
body { f ->
"""
${if (f == Iterables) "if (this is Collection) return size" else "" }
var count = 0
for (element in this) count++
return count