From ce5fd3ee77f8cc2b55f3cda3e7f149c8425a4ed1 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Wed, 27 Jan 2016 21:53:22 +0300 Subject: [PATCH] Inline-only in kotlin.collections --- .../src/kotlin/collections/ArraysJVM.kt | 3 +- .../src/kotlin/collections/Iterators.kt | 3 +- .../stdlib/src/kotlin/collections/JUtil.kt | 18 ++++-- .../stdlib/src/kotlin/collections/Maps.kt | 63 +++++++++++-------- .../stdlib/src/kotlin/collections/MapsJVM.kt | 6 +- .../kotlin/collections/MutableCollections.kt | 36 +++++++---- .../src/kotlin/collections/Sequences.kt | 3 +- .../stdlib/src/kotlin/collections/Sets.kt | 6 +- 8 files changed, 87 insertions(+), 51 deletions(-) diff --git a/libraries/stdlib/src/kotlin/collections/ArraysJVM.kt b/libraries/stdlib/src/kotlin/collections/ArraysJVM.kt index 47bbb87b1f9..510b7e5aa2f 100644 --- a/libraries/stdlib/src/kotlin/collections/ArraysJVM.kt +++ b/libraries/stdlib/src/kotlin/collections/ArraysJVM.kt @@ -15,7 +15,8 @@ public fun ByteArray.toString(charset: String): String = String(this, charset(ch /** * Converts the contents of this byte array to a string using the specified [charset]. */ -public fun ByteArray.toString(charset: Charset): String = String(this, charset) +@kotlin.internal.InlineOnly +public inline fun ByteArray.toString(charset: Charset): String = String(this, charset) /** * Returns a *typed* array containing all of the elements of this collection. diff --git a/libraries/stdlib/src/kotlin/collections/Iterators.kt b/libraries/stdlib/src/kotlin/collections/Iterators.kt index 1345e6789e2..5f1bb2b0cbe 100644 --- a/libraries/stdlib/src/kotlin/collections/Iterators.kt +++ b/libraries/stdlib/src/kotlin/collections/Iterators.kt @@ -17,7 +17,8 @@ public operator fun Enumeration.iterator(): Iterator = object : Iterat /** * Returns the given iterator itself. This allows to use an instance of iterator in a `for` loop. */ -public operator fun Iterator.iterator(): Iterator = this +@kotlin.internal.InlineOnly +public inline operator fun Iterator.iterator(): Iterator = this /** * Returns an [Iterator] wrapping each value produced by this [Iterator] with the [IndexedValue], diff --git a/libraries/stdlib/src/kotlin/collections/JUtil.kt b/libraries/stdlib/src/kotlin/collections/JUtil.kt index 355e8ddfe1a..623ebacdbd2 100644 --- a/libraries/stdlib/src/kotlin/collections/JUtil.kt +++ b/libraries/stdlib/src/kotlin/collections/JUtil.kt @@ -64,7 +64,8 @@ public fun emptyList(): List = EmptyList public fun listOf(vararg elements: T): List = if (elements.size > 0) elements.asList() else emptyList() /** Returns an empty read-only list. The returned list is serializable (JVM). */ -public fun listOf(): List = emptyList() +@kotlin.internal.InlineOnly +public inline fun listOf(): List = emptyList() /** * Returns an immutable list containing only the specified object [element]. @@ -112,25 +113,30 @@ public val List.lastIndex: Int get() = this.size - 1 /** Returns `true` if the collection is not empty. */ -public fun Collection.isNotEmpty(): Boolean = !isEmpty() +@kotlin.internal.InlineOnly +public inline fun Collection.isNotEmpty(): Boolean = !isEmpty() /** Returns this Collection if it's not `null` and the empty list otherwise. */ -public fun Collection?.orEmpty(): Collection = this ?: emptyList() +@kotlin.internal.InlineOnly +public inline fun Collection?.orEmpty(): Collection = this ?: emptyList() /** Returns this List if it's not `null` and the empty list otherwise. */ -public fun List?.orEmpty(): List = this ?: emptyList() +@kotlin.internal.InlineOnly +public inline fun List?.orEmpty(): List = this ?: emptyList() /** * Returns a list containing the elements returned by this enumeration * in the order they are returned by the enumeration. */ @JvmVersion -public fun Enumeration.toList(): List = Collections.list(this) +@kotlin.internal.InlineOnly +public inline fun Enumeration.toList(): List = Collections.list(this) /** * Returns the size of this iterable if it is known, or `null` otherwise. */ -public fun Iterable.collectionSizeOrNull(): Int? = if (this is Collection<*>) this.size else null +@kotlin.internal.InlineOnly +public inline fun Iterable.collectionSizeOrNull(): Int? = if (this is Collection<*>) this.size else null /** * Returns the size of this iterable if it is known, or the specified [default] value otherwise. diff --git a/libraries/stdlib/src/kotlin/collections/Maps.kt b/libraries/stdlib/src/kotlin/collections/Maps.kt index ffdce6e983c..dd6e3b83824 100644 --- a/libraries/stdlib/src/kotlin/collections/Maps.kt +++ b/libraries/stdlib/src/kotlin/collections/Maps.kt @@ -37,7 +37,8 @@ public fun emptyMap(): Map = EmptyMap as Map public fun mapOf(vararg pairs: Pair): Map = if (pairs.size > 0) linkedMapOf(*pairs) else emptyMap() /** Returns an empty read-only map. The returned map is serializable (JVM). */ -public fun mapOf(): Map = emptyMap() +@kotlin.internal.InlineOnly +public inline fun mapOf(): Map = emptyMap() /** * Returns an immutable map, mapping only the specified key to the @@ -93,37 +94,43 @@ internal fun mapCapacity(expectedSize: Int): Int { } /** Returns `true` if this map is not empty. */ -public fun Map.isNotEmpty(): Boolean = !isEmpty() +@kotlin.internal.InlineOnly +public inline fun Map.isNotEmpty(): Boolean = !isEmpty() /** * Returns the [Map] if its not `null`, or the empty [Map] otherwise. */ -public fun Map?.orEmpty() : Map = this ?: emptyMap() +@kotlin.internal.InlineOnly +public inline fun Map?.orEmpty() : Map = this ?: emptyMap() /** * Checks if the map contains the given key. This method allows to use the `x in map` syntax for checking * whether an object is contained in the map. */ -public operator fun <@kotlin.internal.OnlyInputTypes K, V> Map.contains(key: K) : Boolean = containsKey(key) +@kotlin.internal.InlineOnly +public inline operator fun <@kotlin.internal.OnlyInputTypes K, V> Map.contains(key: K) : Boolean = containsKey(key) /** * Returns the value corresponding to the given [key], or `null` if such a key is not present in the map. */ -public operator fun <@kotlin.internal.OnlyInputTypes K, V> Map.get(key: K): V? = (this as Map).get(key) +@kotlin.internal.InlineOnly +public inline operator fun <@kotlin.internal.OnlyInputTypes K, V> Map.get(key: K): V? = (this as Map).get(key) /** * Returns `true` if the map contains the specified [key]. * * Allows to overcome type-safety restriction of `containsKey` that requires to pass a key of type `K`. */ -public fun <@kotlin.internal.OnlyInputTypes K> Map.containsKey(key: K): Boolean = (this as Map).containsKey(key) +@kotlin.internal.InlineOnly +public inline fun <@kotlin.internal.OnlyInputTypes K> Map.containsKey(key: K): Boolean = (this as Map).containsKey(key) /** * Returns `true` if the map maps one or more keys to the specified [value]. * * Allows to overcome type-safety restriction of `containsValue` that requires to pass a value of type `V`. */ -public fun Map.containsValue(value: V): Boolean = this.containsValue(value) +@kotlin.internal.InlineOnly +public inline fun Map.containsValue(value: V): Boolean = this.containsValue(value) /** @@ -133,7 +140,8 @@ public fun Map.containsValue(value: * Allows to overcome type-safety restriction of `remove` that requires to pass a key of type `K`. */ -public fun <@kotlin.internal.OnlyInputTypes K, V> MutableMap.remove(key: K): V? = (this as MutableMap).remove(key) +@kotlin.internal.InlineOnly +public inline fun <@kotlin.internal.OnlyInputTypes K, V> MutableMap.remove(key: K): V? = (this as MutableMap).remove(key) /** * Returns the key component of the map entry. @@ -163,21 +171,17 @@ public inline operator fun Map.Entry.component2(): V = value /** * Converts entry to [Pair] with key being first component and value being second. */ -public fun Map.Entry.toPair(): Pair = Pair(key, value) +@kotlin.internal.InlineOnly +public inline fun Map.Entry.toPair(): Pair = Pair(key, value) /** * Returns the value for the given key, or the result of the [defaultValue] function if there was no entry for the given key. * * @sample test.collections.MapTest.getOrElse */ -public inline fun Map.getOrElse(key: K, defaultValue: () -> V): V { - val value = get(key) - if (value == null) { - return defaultValue() - } else { - return value - } -} +@kotlin.internal.InlineOnly +public inline fun Map.getOrElse(key: K, defaultValue: () -> V): V = get(key) ?: defaultValue() + internal inline fun Map.getOrElseNullable(key: K, defaultValue: () -> V): V { val value = get(key) @@ -212,7 +216,8 @@ public inline fun MutableMap.getOrPut(key: K, defaultValue: () -> V * * @sample test.collections.MapTest.iterateWithProperties */ -public operator fun Map.iterator(): Iterator> = entries.iterator() +@kotlin.internal.InlineOnly +public inline operator fun Map.iterator(): Iterator> = entries.iterator() /** * Returns a [MutableIterator] over the mutable entries in the [MutableMap]. @@ -220,14 +225,15 @@ public operator fun Map.iterator(): Iterator> = ent */ @JvmVersion @JvmName("mutableIterator") -public operator fun MutableMap.iterator(): MutableIterator> = entries.iterator() +@kotlin.internal.InlineOnly +public inline operator fun MutableMap.iterator(): MutableIterator> = entries.iterator() /** * Populates the given `destination` [Map] with entries having the keys of this map and the values obtained * by applying the `transform` function to each entry in this [Map]. */ public inline fun > Map.mapValuesTo(destination: C, transform: (Map.Entry) -> R): C { - return entries.associateByTo(destination, { it.key }, { transform(it) }) + return entries.associateByTo(destination, { it.key }, transform) } /** @@ -235,7 +241,7 @@ public inline fun > Map.mapValuesTo(de * by applying the `transform` function to each entry in this [Map] and the values of this map. */ public inline fun > Map.mapKeysTo(destination: C, transform: (Map.Entry) -> R): C { - return entries.associateByTo(destination, { transform(it) }, { it.value }) + return entries.associateByTo(destination, transform, { it.value }) } /** @@ -429,35 +435,40 @@ public operator fun Map.plus(map: Map): Map /** * Appends or replaces the given [pair] in this mutable map. */ -public operator fun MutableMap.plusAssign(pair: Pair) { +@kotlin.internal.InlineOnly +public inline operator fun MutableMap.plusAssign(pair: Pair) { put(pair.first, pair.second) } /** * Appends or replaces all pairs from the given collection of [pairs] in this mutable map. */ -public operator fun MutableMap.plusAssign(pairs: Iterable>) { +@kotlin.internal.InlineOnly +public inline operator fun MutableMap.plusAssign(pairs: Iterable>) { putAll(pairs) } /** * Appends or replaces all pairs from the given array of [pairs] in this mutable map. */ -public operator fun MutableMap.plusAssign(pairs: Array>) { +@kotlin.internal.InlineOnly +public inline operator fun MutableMap.plusAssign(pairs: Array>) { putAll(pairs) } /** * Appends or replaces all pairs from the given sequence of [pairs] in this mutable map. */ -public operator fun MutableMap.plusAssign(pairs: Sequence>) { +@kotlin.internal.InlineOnly +public inline operator fun MutableMap.plusAssign(pairs: Sequence>) { putAll(pairs) } /** * Appends or replaces all entries from the given [map] in this mutable map. */ -public operator fun MutableMap.plusAssign(map: Map) { +@kotlin.internal.InlineOnly +public inline operator fun MutableMap.plusAssign(map: Map) { putAll(map) } diff --git a/libraries/stdlib/src/kotlin/collections/MapsJVM.kt b/libraries/stdlib/src/kotlin/collections/MapsJVM.kt index 53970bccc97..e730cf7547d 100644 --- a/libraries/stdlib/src/kotlin/collections/MapsJVM.kt +++ b/libraries/stdlib/src/kotlin/collections/MapsJVM.kt @@ -14,7 +14,8 @@ import java.util.concurrent.ConcurrentMap * Allows to use the index operator for storing values in a mutable map. */ // this code is JVM-specific, because JS has native set function -public operator fun MutableMap.set(key: K, value: V): Unit { +@kotlin.internal.InlineOnly +public inline operator fun MutableMap.set(key: K, value: V): Unit { put(key, value) } @@ -72,6 +73,7 @@ public fun , V> sortedMapOf(vararg pairs: Pair): SortedM * * @sample test.collections.MapJVMTest.toProperties */ -public fun Map.toProperties(): Properties +@kotlin.internal.InlineOnly +public inline fun Map.toProperties(): Properties = Properties().apply { putAll(this@toProperties) } diff --git a/libraries/stdlib/src/kotlin/collections/MutableCollections.kt b/libraries/stdlib/src/kotlin/collections/MutableCollections.kt index 6b3280d345a..c602a66373d 100644 --- a/libraries/stdlib/src/kotlin/collections/MutableCollections.kt +++ b/libraries/stdlib/src/kotlin/collections/MutableCollections.kt @@ -10,7 +10,8 @@ import java.util.* * * Allows to overcome type-safety restriction of `containsAll` that requires to pass a collection of type `Collection`. */ -public fun <@kotlin.internal.OnlyInputTypes T> Collection.containsAll(elements: Collection): Boolean = this.containsAll(elements) +@kotlin.internal.InlineOnly +public inline fun <@kotlin.internal.OnlyInputTypes T> Collection.containsAll(elements: Collection): Boolean = this.containsAll(elements) /** * Removes a single instance of the specified element from this @@ -20,7 +21,8 @@ public fun <@kotlin.internal.OnlyInputTypes T> Collection.containsAll(element * * @return `true` if the element has been successfully removed; `false` if it was not present in the collection. */ -public fun <@kotlin.internal.OnlyInputTypes T> MutableCollection.remove(element: T): Boolean = (this as MutableCollection).remove(element) +@kotlin.internal.InlineOnly +public inline fun <@kotlin.internal.OnlyInputTypes T> MutableCollection.remove(element: T): Boolean = (this as MutableCollection).remove(element) /** * Removes all of this collection's elements that are also contained in the specified collection. @@ -29,7 +31,8 @@ public fun <@kotlin.internal.OnlyInputTypes T> MutableCollection.remove(e * * @return `true` if any of the specified elements was removed from the collection, `false` if the collection was not modified. */ -public fun <@kotlin.internal.OnlyInputTypes T> MutableCollection.removeAll(elements: Collection): Boolean = (this as MutableCollection).removeAll(elements) +@kotlin.internal.InlineOnly +public inline fun <@kotlin.internal.OnlyInputTypes T> MutableCollection.removeAll(elements: Collection): Boolean = (this as MutableCollection).removeAll(elements) /** * Retains only the elements in this collection that are contained in the specified collection. @@ -38,61 +41,70 @@ public fun <@kotlin.internal.OnlyInputTypes T> MutableCollection.removeAl * * @return `true` if any element was removed from the collection, `false` if the collection was not modified. */ -public fun <@kotlin.internal.OnlyInputTypes T> MutableCollection.retainAll(elements: Collection): Boolean = (this as MutableCollection).retainAll(elements) +@kotlin.internal.InlineOnly +public inline fun <@kotlin.internal.OnlyInputTypes T> MutableCollection.retainAll(elements: Collection): Boolean = (this as MutableCollection).retainAll(elements) /** * Adds the specified [element] to this mutable collection. */ -public operator fun MutableCollection.plusAssign(element: T) { +@kotlin.internal.InlineOnly +public inline operator fun MutableCollection.plusAssign(element: T) { this.add(element) } /** * Adds all elements of the given [elements] collection to this mutable collection. */ -public operator fun MutableCollection.plusAssign(elements: Iterable) { +@kotlin.internal.InlineOnly +public inline operator fun MutableCollection.plusAssign(elements: Iterable) { this.addAll(elements) } /** * Adds all elements of the given [elements] array to this mutable collection. */ -public operator fun MutableCollection.plusAssign(elements: Array) { +@kotlin.internal.InlineOnly +public inline operator fun MutableCollection.plusAssign(elements: Array) { this.addAll(elements) } /** * Adds all elements of the given [elements] sequence to this mutable collection. */ -public operator fun MutableCollection.plusAssign(elements: Sequence) { +@kotlin.internal.InlineOnly +public inline operator fun MutableCollection.plusAssign(elements: Sequence) { this.addAll(elements) } /** * Removes a single instance of the specified [element] from this mutable collection. */ -public operator fun MutableCollection.minusAssign(element: T) { +@kotlin.internal.InlineOnly +public inline operator fun MutableCollection.minusAssign(element: T) { this.remove(element) } /** * Removes all elements contained in the given [elements] collection from this mutable collection. */ -public operator fun MutableCollection.minusAssign(elements: Iterable) { +@kotlin.internal.InlineOnly +public inline operator fun MutableCollection.minusAssign(elements: Iterable) { this.removeAll(elements) } /** * Removes all elements contained in the given [elements] array from this mutable collection. */ -public operator fun MutableCollection.minusAssign(elements: Array) { +@kotlin.internal.InlineOnly +public inline operator fun MutableCollection.minusAssign(elements: Array) { this.removeAll(elements) } /** * Removes all elements contained in the given [elements] sequence from this mutable collection. */ -public operator fun MutableCollection.minusAssign(elements: Sequence) { +@kotlin.internal.InlineOnly +public inline operator fun MutableCollection.minusAssign(elements: Sequence) { this.removeAll(elements) } diff --git a/libraries/stdlib/src/kotlin/collections/Sequences.kt b/libraries/stdlib/src/kotlin/collections/Sequences.kt index 6bb5e120259..e0726d2f982 100644 --- a/libraries/stdlib/src/kotlin/collections/Sequences.kt +++ b/libraries/stdlib/src/kotlin/collections/Sequences.kt @@ -20,7 +20,8 @@ public fun Iterator.asSequence(): Sequence { /** * Creates a sequence that returns all values from this enumeration. The sequence is constrained to be iterated only once. */ -public fun Enumeration.asSequence(): Sequence = this.iterator().asSequence() +@kotlin.internal.InlineOnly +public inline fun Enumeration.asSequence(): Sequence = this.iterator().asSequence() /** * Creates a sequence that returns the specified values. diff --git a/libraries/stdlib/src/kotlin/collections/Sets.kt b/libraries/stdlib/src/kotlin/collections/Sets.kt index f8b177a5cff..46815ea5c9e 100644 --- a/libraries/stdlib/src/kotlin/collections/Sets.kt +++ b/libraries/stdlib/src/kotlin/collections/Sets.kt @@ -29,7 +29,8 @@ public fun emptySet(): Set = EmptySet public fun setOf(vararg elements: T): Set = if (elements.size > 0) elements.toSet() else emptySet() /** Returns an empty read-only set. The returned set is serializable (JVM). */ -public fun setOf(): Set = emptySet() +@kotlin.internal.InlineOnly +public inline fun setOf(): Set = emptySet() /** Returns a new [MutableSet] with the given elements. */ public fun mutableSetOf(vararg elements: T): MutableSet = elements.toCollection(LinkedHashSet(mapCapacity(elements.size))) @@ -41,7 +42,8 @@ public fun hashSetOf(vararg elements: T): HashSet = elements.toCollection public fun linkedSetOf(vararg elements: T): LinkedHashSet = elements.toCollection(LinkedHashSet(mapCapacity(elements.size))) /** Returns this Set if it's not `null` and the empty set otherwise. */ -public fun Set?.orEmpty(): Set = this ?: emptySet() +@kotlin.internal.InlineOnly +public inline fun Set?.orEmpty(): Set = this ?: emptySet() /** * Returns an immutable set containing only the specified object [element].