diff --git a/libraries/stdlib/src/generated/_Collections.kt b/libraries/stdlib/src/generated/_Collections.kt index 22ec5a15a18..74576433513 100644 --- a/libraries/stdlib/src/generated/_Collections.kt +++ b/libraries/stdlib/src/generated/_Collections.kt @@ -241,6 +241,7 @@ public fun List.getOrNull(index: Int): T? { * Returns first index of [element], or -1 if the collection does not contain element. */ public fun Iterable.indexOf(element: T): Int { + if (this is List) return this.indexOf(element) var index = 0 for (item in this) { if (element == item) @@ -370,6 +371,7 @@ public inline fun List.last(predicate: (T) -> Boolean): T { * Returns last index of [element], or -1 if the collection does not contain element. */ public fun Iterable.lastIndexOf(element: T): Int { + if (this is List) return this.lastIndexOf(element) var lastIndex = -1 var index = 0 for (item in this) { diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt index 8732a83bde1..b2adaae7e95 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt @@ -29,8 +29,9 @@ fun elements(): List { exclude(Strings, Lists) // has native implementation doc { "Returns first index of [element], or -1 if the collection does not contain element." } returns("Int") - body { + body { f -> """ + ${if (f == Iterables) "if (this is List) return this.indexOf(element)" else ""} var index = 0 for (item in this) { if (element == item) @@ -75,8 +76,9 @@ fun elements(): List { exclude(Strings, Lists) // has native implementation doc { "Returns last index of [element], or -1 if the collection does not contain element." } returns("Int") - body { + body { f -> """ + ${if (f == Iterables) "if (this is List) return this.lastIndexOf(element)" else ""} var lastIndex = -1 var index = 0 for (item in this) { @@ -521,26 +523,14 @@ fun elements(): List { doc(Strings) { """"Returns the last character matching the given [predicate]. @throws [NoSuchElementException] if no such character is found.""" } returns("T") - body { - """ - var last: T? = null - var found = false - for (element in this) { - if (predicate(element)) { - last = element - found = true - } - } - if (!found) throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") - return last as T - """ - } - - body(Iterables) { + body { f -> + (if (f == Iterables) """ if (this is List) return this.last(predicate) - + """ + else "") + + """ var last: T? = null var found = false for (element in this) { @@ -570,23 +560,14 @@ fun elements(): List { doc { "Returns the last element matching the given [predicate], or `null` if no such element was found." } doc(Strings) { "Returns the last character matching the given [predicate], or `null` if no such character was found." } returns("T?") - body { - """ - var last: T? = null - for (element in this) { - if (predicate(element)) { - last = element - } - } - return last - """ - } - - body(Iterables) { + body { f -> + (if (f == Iterables) """ if (this is List) return this.lastOrNull(predicate) - + """ + else "") + + """ var last: T? = null for (element in this) { if (predicate(element)) { @@ -597,8 +578,6 @@ fun elements(): List { """ } - - body(ArraysOfPrimitives, ArraysOfObjects, Lists) { """ for (index in this.indices.reversed()) {