From de86e901035d9a6eb98f47ed1a354bf34c1078f8 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Thu, 26 Nov 2015 06:05:52 +0300 Subject: [PATCH] Generate indexOf and lastIndexOf extensions for List. --- .../stdlib/src/generated/_Collections.kt | 36 +++++++++++++++++++ .../kotlin/collections/MutableCollections.kt | 25 ------------- .../src/templates/Elements.kt | 10 +++--- 3 files changed, 42 insertions(+), 29 deletions(-) diff --git a/libraries/stdlib/src/generated/_Collections.kt b/libraries/stdlib/src/generated/_Collections.kt index 5fcbdcd0012..33905f1991c 100644 --- a/libraries/stdlib/src/generated/_Collections.kt +++ b/libraries/stdlib/src/generated/_Collections.kt @@ -271,6 +271,13 @@ public fun <@kotlin.internal.OnlyInputTypes T> Iterable.indexOf(element: T): return -1 } +/** + * Returns first index of [element], or -1 if the list does not contain element. + */ +public fun <@kotlin.internal.OnlyInputTypes T> List.indexOf(element: T): Int { + return indexOf(element) +} + /** * Returns first index of [element], or -1 if the collection does not contain element. */ @@ -282,6 +289,17 @@ public fun Iterable.indexOf(element: T): Int { return indexOf(element as Any?) } +/** + * Returns first index of [element], or -1 if the collection does not contain element. + */ +@Deprecated("List and element have incompatible types. Upcast element to Any? if you're sure.", ReplaceWith("indexOf(element as Any?)")) +@kotlin.jvm.JvmName("indexOfAny") +@kotlin.internal.LowPriorityInOverloadResolution +@Suppress("NOTHING_TO_INLINE") +public fun List.indexOf(element: T): Int { + return indexOf(element as Any?) +} + /** * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element. */ @@ -433,6 +451,13 @@ public fun <@kotlin.internal.OnlyInputTypes T> Iterable.lastIndexOf(element: return lastIndex } +/** + * Returns last index of [element], or -1 if the list does not contain element. + */ +public fun <@kotlin.internal.OnlyInputTypes T> List.lastIndexOf(element: T): Int { + return lastIndexOf(element) +} + /** * Returns last index of [element], or -1 if the collection does not contain element. */ @@ -444,6 +469,17 @@ public fun Iterable.lastIndexOf(element: T): Int { return lastIndexOf(element as Any?) } +/** + * Returns last index of [element], or -1 if the collection does not contain element. + */ +@Deprecated("List and element have incompatible types. Upcast element to Any? if you're sure.", ReplaceWith("lastIndexOf(element as Any?)")) +@kotlin.jvm.JvmName("lastIndexOfAny") +@kotlin.internal.LowPriorityInOverloadResolution +@Suppress("NOTHING_TO_INLINE") +public fun List.lastIndexOf(element: T): Int { + return lastIndexOf(element as Any?) +} + /** * Returns last index of [element], or -1 if the collection does not contain element. * Allows to overcome type-safety restriction of `lastIndexOf` that requires to pass an element of type `T`. diff --git a/libraries/stdlib/src/kotlin/collections/MutableCollections.kt b/libraries/stdlib/src/kotlin/collections/MutableCollections.kt index 271c3c9c041..adeda1dc826 100644 --- a/libraries/stdlib/src/kotlin/collections/MutableCollections.kt +++ b/libraries/stdlib/src/kotlin/collections/MutableCollections.kt @@ -77,31 +77,6 @@ public inline fun MutableCollection.retainAllRaw(elements: Collection MutableCollection.retainAll(elements: Collection): Boolean = retainAll(elements as Collection) -/** - * Returns the index of the first occurrence of the specified element in the list, or -1 if the specified - * element is not contained in the list. - * Allows to overcome type-safety restriction of `indexOf` that requires to pass an element of type `T`. - */ -public fun <@kotlin.internal.OnlyInputTypes T> List.indexOf(element: T): Int = indexOf(element) - -@Deprecated("List and element have incompatible types. Upcast element to Any? if you're sure.", ReplaceWith("indexOf(element as Any?)")) -@kotlin.jvm.JvmName("indexOfAny") -@kotlin.internal.LowPriorityInOverloadResolution -public fun List.indexOf(element: Any?): Int = indexOf(element as Any?) - -/** - * Returns the index of the last occurrence of the specified element in the list, or -1 if the specified - * element is not contained in the list. - * Allows to overcome type-safety restriction of `lastIndexOf` that requires to pass an element of type `T`. - */ -public fun <@kotlin.internal.OnlyInputTypes T> List.lastIndexOf(element: T): Int = lastIndexOf(element) - -@Deprecated("List and element have incompatible types. Upcast element to Any? if you're sure.", ReplaceWith("lastIndexOf(element as Any?)")) -@kotlin.jvm.JvmName("lastIndexOfAny") -@kotlin.internal.LowPriorityInOverloadResolution -public fun List.lastIndexOf(element: Any?): Int = lastIndexOf(element as Any?) - - @Deprecated("Use operator 'get' instead", ReplaceWith("this[index]")) public fun CharSequence.charAt(index: Int): Char = this[index] diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt index c5cb26110e7..abf4064f6ba 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt @@ -57,7 +57,7 @@ fun elements(): List { } templates add f("indexOf(element: T)") { - only(Iterables, Sequences, ArraysOfObjects, ArraysOfPrimitives) + only(Iterables, Sequences, ArraysOfObjects, ArraysOfPrimitives, Lists) doc { f -> "Returns first index of [element], or -1 if the ${f.collection} does not contain element." } typeParam("@kotlin.internal.OnlyInputTypes T") returns("Int") @@ -102,10 +102,11 @@ fun elements(): List { return -1 """ } + body(Lists) { "return indexOf(element)" } } templates add f("indexOf(element: T)") { - only(Iterables, Sequences, ArraysOfObjects) + only(Iterables, Sequences, ArraysOfObjects, Lists) doc { "Returns first index of [element], or -1 if the collection does not contain element." } returns("Int") deprecate { f -> with(DocExtensions) { Deprecation("${f.collection.capitalize()} and element have incompatible types. Upcast element to Any? if you're sure.", "indexOf(element as Any?)") } } @@ -133,7 +134,7 @@ fun elements(): List { } templates add f("lastIndexOf(element: T)") { - only(Iterables, Sequences, ArraysOfObjects, ArraysOfPrimitives) + only(Iterables, Sequences, ArraysOfObjects, ArraysOfPrimitives, Lists) doc { f -> "Returns last index of [element], or -1 if the ${f.collection} does not contain element." } typeParam("@kotlin.internal.OnlyInputTypes T") returns("Int") @@ -179,10 +180,11 @@ fun elements(): List { return -1 """ } + body(Lists) { "return lastIndexOf(element)" } } templates add f("lastIndexOf(element: T)") { - only(Iterables, Sequences, ArraysOfObjects) + only(Iterables, Sequences, ArraysOfObjects, Lists) doc { "Returns last index of [element], or -1 if the collection does not contain element." } returns("Int") deprecate { f -> with(DocExtensions) { Deprecation("${f.collection.capitalize()} and element have incompatible types. Upcast element to Any? if you're sure.", "lastIndexOf(element as Any?)") } }