From 683bc4bd019b6aa982afe922817ee42e24bfd248 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Thu, 15 Oct 2015 19:59:54 +0300 Subject: [PATCH] Generate *Raw methods. --- libraries/stdlib/src/generated/_Arrays.kt | 24 +++++++++++ .../stdlib/src/generated/_Collections.kt | 40 +++++++++++++++++++ libraries/stdlib/src/generated/_Sequences.kt | 24 +++++++++++ .../src/templates/Elements.kt | 32 +++++++++++++++ 4 files changed, 120 insertions(+) diff --git a/libraries/stdlib/src/generated/_Arrays.kt b/libraries/stdlib/src/generated/_Arrays.kt index 275c8323fba..593beba3351 100644 --- a/libraries/stdlib/src/generated/_Arrays.kt +++ b/libraries/stdlib/src/generated/_Arrays.kt @@ -435,6 +435,14 @@ public operator fun ShortArray.contains(element: Short): Boolean { return indexOf(element) >= 0 } +/** + * Returns `true` if [element] is found in the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun Array.containsRaw(element: Any?): Boolean { + return contains(element) +} + /** * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. */ @@ -1514,6 +1522,14 @@ public inline fun ShortArray.indexOfLast(predicate: (Short) -> Boolean): Int { return -1 } +/** + * Returns first index of [element], or -1 if the collection does not contain element. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun Array.indexOfRaw(element: Any?): Int { + return indexOf(element) +} + /** * Returns the last element. * @throws [NoSuchElementException] if the collection is empty. @@ -1828,6 +1844,14 @@ public fun ShortArray.lastIndexOf(element: Short): Int { return -1 } +/** + * Returns last index of [element], or -1 if the collection does not contain element. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun Array.lastIndexOfRaw(element: Any?): Int { + return lastIndexOf(element) +} + /** * Returns the last element, or `null` if the collection is empty. */ diff --git a/libraries/stdlib/src/generated/_Collections.kt b/libraries/stdlib/src/generated/_Collections.kt index 74576433513..56f233eecc2 100644 --- a/libraries/stdlib/src/generated/_Collections.kt +++ b/libraries/stdlib/src/generated/_Collections.kt @@ -61,6 +61,14 @@ public operator fun Iterable.contains(element: T): Boolean { return indexOf(element) >= 0 } +/** + * Returns `true` if [element] is found in the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun Iterable<*>.containsRaw(element: Any?): Boolean { + return contains(element) +} + /** * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. */ @@ -302,6 +310,22 @@ public inline fun List.indexOfLast(predicate: (T) -> Boolean): Int { return -1 } +/** + * Returns first index of [element], or -1 if the collection does not contain element. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun Iterable<*>.indexOfRaw(element: Any?): Int { + return indexOf(element) +} + +/** + * Returns first index of [element], or -1 if the collection does not contain element. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun List.indexOfRaw(element: Any?): Int { + return (this as List).indexOf(element) +} + /** * Returns the last element. * @throws [NoSuchElementException] if the collection is empty. @@ -382,6 +406,22 @@ public fun Iterable.lastIndexOf(element: T): Int { return lastIndex } +/** + * Returns last index of [element], or -1 if the collection does not contain element. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun Iterable<*>.lastIndexOfRaw(element: Any?): Int { + return lastIndexOf(element) +} + +/** + * Returns last index of [element], or -1 if the collection does not contain element. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun List.lastIndexOfRaw(element: Any?): Int { + return (this as List).lastIndexOf(element) +} + /** * Returns the last element, or `null` if the collection is empty. */ diff --git a/libraries/stdlib/src/generated/_Sequences.kt b/libraries/stdlib/src/generated/_Sequences.kt index 30f70b7826a..e8c327eb0a8 100644 --- a/libraries/stdlib/src/generated/_Sequences.kt +++ b/libraries/stdlib/src/generated/_Sequences.kt @@ -19,6 +19,14 @@ public operator fun Sequence.contains(element: T): Boolean { return indexOf(element) >= 0 } +/** + * Returns `true` if [element] is found in the collection. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun Sequence<*>.containsRaw(element: Any?): Boolean { + return contains(element) +} + /** * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. */ @@ -150,6 +158,14 @@ public inline fun Sequence.indexOfLast(predicate: (T) -> Boolean): Int { return lastIndex } +/** + * Returns first index of [element], or -1 if the collection does not contain element. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun Sequence<*>.indexOfRaw(element: Any?): Int { + return indexOf(element) +} + /** * Returns the last element. * @throws [NoSuchElementException] if the collection is empty. @@ -195,6 +211,14 @@ public fun Sequence.lastIndexOf(element: T): Int { return lastIndex } +/** + * Returns last index of [element], or -1 if the collection does not contain element. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun Sequence<*>.lastIndexOfRaw(element: Any?): Int { + return lastIndexOf(element) +} + /** * Returns the last element, or `null` if the collection is empty. */ diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt index b2adaae7e95..abd15317067 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt @@ -25,6 +25,16 @@ fun elements(): List { } } + templates add f("containsRaw(element: Any?)") { + only(Iterables, Sequences, ArraysOfObjects) + doc { "Returns `true` if [element] is found in the collection." } + receiverAsterisk(Iterables, Sequences) { true } + inline(true) + annotations("""@Suppress("NOTHING_TO_INLINE")""") + returns("Boolean") + body { "return contains(element)" } + } + templates add f("indexOf(element: T)") { exclude(Strings, Lists) // has native implementation doc { "Returns first index of [element], or -1 if the collection does not contain element." } @@ -72,6 +82,17 @@ fun elements(): List { } } + templates add f("indexOfRaw(element: Any?)") { + only(Iterables, Sequences, ArraysOfObjects, Lists) + doc { "Returns first index of [element], or -1 if the collection does not contain element." } + receiverAsterisk(Iterables, Sequences) { true } + inline(true) + annotations("""@Suppress("NOTHING_TO_INLINE")""") + returns("Int") + body { "return indexOf(element)" } + body(Lists) { "return (this as List).indexOf(element)" } + } + templates add f("lastIndexOf(element: T)") { exclude(Strings, Lists) // has native implementation doc { "Returns last index of [element], or -1 if the collection does not contain element." } @@ -120,6 +141,17 @@ fun elements(): List { } } + templates add f("lastIndexOfRaw(element: Any?)") { + only(Iterables, Sequences, ArraysOfObjects, Lists) + doc { "Returns last index of [element], or -1 if the collection does not contain element." } + receiverAsterisk(Iterables, Sequences) { true } + inline(true) + annotations("""@Suppress("NOTHING_TO_INLINE")""") + returns("Int") + body { "return lastIndexOf(element)" } + body(Lists) { "return (this as List).lastIndexOf(element)" } + } + templates add f("indexOfFirst(predicate: (T) -> Boolean)") { inline(true)