Inline-only in kotlin.collections

This commit is contained in:
Ilya Gorbunov
2016-01-27 21:53:22 +03:00
parent c7bd70732c
commit ce5fd3ee77
8 changed files with 87 additions and 51 deletions
@@ -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.
@@ -17,7 +17,8 @@ public operator fun <T> Enumeration<T>.iterator(): Iterator<T> = object : Iterat
/**
* Returns the given iterator itself. This allows to use an instance of iterator in a `for` loop.
*/
public operator fun <T> Iterator<T>.iterator(): Iterator<T> = this
@kotlin.internal.InlineOnly
public inline operator fun <T> Iterator<T>.iterator(): Iterator<T> = this
/**
* Returns an [Iterator] wrapping each value produced by this [Iterator] with the [IndexedValue],
@@ -64,7 +64,8 @@ public fun <T> emptyList(): List<T> = EmptyList
public fun <T> listOf(vararg elements: T): List<T> = if (elements.size > 0) elements.asList() else emptyList()
/** Returns an empty read-only list. The returned list is serializable (JVM). */
public fun <T> listOf(): List<T> = emptyList()
@kotlin.internal.InlineOnly
public inline fun <T> listOf(): List<T> = emptyList()
/**
* Returns an immutable list containing only the specified object [element].
@@ -112,25 +113,30 @@ public val <T> List<T>.lastIndex: Int
get() = this.size - 1
/** Returns `true` if the collection is not empty. */
public fun <T> Collection<T>.isNotEmpty(): Boolean = !isEmpty()
@kotlin.internal.InlineOnly
public inline fun <T> Collection<T>.isNotEmpty(): Boolean = !isEmpty()
/** Returns this Collection if it's not `null` and the empty list otherwise. */
public fun <T> Collection<T>?.orEmpty(): Collection<T> = this ?: emptyList()
@kotlin.internal.InlineOnly
public inline fun <T> Collection<T>?.orEmpty(): Collection<T> = this ?: emptyList()
/** Returns this List if it's not `null` and the empty list otherwise. */
public fun <T> List<T>?.orEmpty(): List<T> = this ?: emptyList()
@kotlin.internal.InlineOnly
public inline fun <T> List<T>?.orEmpty(): List<T> = this ?: emptyList()
/**
* Returns a list containing the elements returned by this enumeration
* in the order they are returned by the enumeration.
*/
@JvmVersion
public fun <T> Enumeration<T>.toList(): List<T> = Collections.list(this)
@kotlin.internal.InlineOnly
public inline fun <T> Enumeration<T>.toList(): List<T> = Collections.list(this)
/**
* Returns the size of this iterable if it is known, or `null` otherwise.
*/
public fun <T> Iterable<T>.collectionSizeOrNull(): Int? = if (this is Collection<*>) this.size else null
@kotlin.internal.InlineOnly
public inline fun <T> Iterable<T>.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.
+37 -26
View File
@@ -37,7 +37,8 @@ public fun <K, V> emptyMap(): Map<K, V> = EmptyMap as Map<K, V>
public fun <K, V> mapOf(vararg pairs: Pair<K, V>): Map<K, V> = if (pairs.size > 0) linkedMapOf(*pairs) else emptyMap()
/** Returns an empty read-only map. The returned map is serializable (JVM). */
public fun <K, V> mapOf(): Map<K, V> = emptyMap()
@kotlin.internal.InlineOnly
public inline fun <K, V> mapOf(): Map<K, V> = 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 <K, V> Map<K, V>.isNotEmpty(): Boolean = !isEmpty()
@kotlin.internal.InlineOnly
public inline fun <K, V> Map<K, V>.isNotEmpty(): Boolean = !isEmpty()
/**
* Returns the [Map] if its not `null`, or the empty [Map] otherwise.
*/
public fun <K,V> Map<K,V>?.orEmpty() : Map<K,V> = this ?: emptyMap()
@kotlin.internal.InlineOnly
public inline fun <K,V> Map<K,V>?.orEmpty() : Map<K,V> = 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<out K, V>.contains(key: K) : Boolean = containsKey(key)
@kotlin.internal.InlineOnly
public inline operator fun <@kotlin.internal.OnlyInputTypes K, V> Map<out K, V>.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<out K, V>.get(key: K): V? = (this as Map<K, V>).get(key)
@kotlin.internal.InlineOnly
public inline operator fun <@kotlin.internal.OnlyInputTypes K, V> Map<out K, V>.get(key: K): V? = (this as Map<K, V>).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<out K, *>.containsKey(key: K): Boolean = (this as Map<K, *>).containsKey(key)
@kotlin.internal.InlineOnly
public inline fun <@kotlin.internal.OnlyInputTypes K> Map<out K, *>.containsKey(key: K): Boolean = (this as Map<K, *>).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 <K, @kotlin.internal.OnlyInputTypes V> Map<K, V>.containsValue(value: V): Boolean = this.containsValue(value)
@kotlin.internal.InlineOnly
public inline fun <K, @kotlin.internal.OnlyInputTypes V> Map<K, V>.containsValue(value: V): Boolean = this.containsValue(value)
/**
@@ -133,7 +140,8 @@ public fun <K, @kotlin.internal.OnlyInputTypes V> Map<K, V>.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<out K, V>.remove(key: K): V? = (this as MutableMap<K, V>).remove(key)
@kotlin.internal.InlineOnly
public inline fun <@kotlin.internal.OnlyInputTypes K, V> MutableMap<out K, V>.remove(key: K): V? = (this as MutableMap<K, V>).remove(key)
/**
* Returns the key component of the map entry.
@@ -163,21 +171,17 @@ public inline operator fun <K, V> Map.Entry<K, V>.component2(): V = value
/**
* Converts entry to [Pair] with key being first component and value being second.
*/
public fun <K, V> Map.Entry<K, V>.toPair(): Pair<K, V> = Pair(key, value)
@kotlin.internal.InlineOnly
public inline fun <K, V> Map.Entry<K, V>.toPair(): Pair<K, V> = 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 <K, V> Map<K, V>.getOrElse(key: K, defaultValue: () -> V): V {
val value = get(key)
if (value == null) {
return defaultValue()
} else {
return value
}
}
@kotlin.internal.InlineOnly
public inline fun <K, V> Map<K, V>.getOrElse(key: K, defaultValue: () -> V): V = get(key) ?: defaultValue()
internal inline fun <K, V> Map<K, V>.getOrElseNullable(key: K, defaultValue: () -> V): V {
val value = get(key)
@@ -212,7 +216,8 @@ public inline fun <K, V> MutableMap<K, V>.getOrPut(key: K, defaultValue: () -> V
*
* @sample test.collections.MapTest.iterateWithProperties
*/
public operator fun <K, V> Map<K, V>.iterator(): Iterator<Map.Entry<K, V>> = entries.iterator()
@kotlin.internal.InlineOnly
public inline operator fun <K, V> Map<K, V>.iterator(): Iterator<Map.Entry<K, V>> = entries.iterator()
/**
* Returns a [MutableIterator] over the mutable entries in the [MutableMap].
@@ -220,14 +225,15 @@ public operator fun <K, V> Map<K, V>.iterator(): Iterator<Map.Entry<K, V>> = ent
*/
@JvmVersion
@JvmName("mutableIterator")
public operator fun <K, V> MutableMap<K, V>.iterator(): MutableIterator<MutableMap.MutableEntry<K, V>> = entries.iterator()
@kotlin.internal.InlineOnly
public inline operator fun <K, V> MutableMap<K, V>.iterator(): MutableIterator<MutableMap.MutableEntry<K, V>> = 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 <K, V, R, C : MutableMap<in K, in R>> Map<K, V>.mapValuesTo(destination: C, transform: (Map.Entry<K, V>) -> R): C {
return entries.associateByTo(destination, { it.key }, { transform(it) })
return entries.associateByTo(destination, { it.key }, transform)
}
/**
@@ -235,7 +241,7 @@ public inline fun <K, V, R, C : MutableMap<in K, in R>> Map<K, V>.mapValuesTo(de
* by applying the `transform` function to each entry in this [Map] and the values of this map.
*/
public inline fun <K, V, R, C : MutableMap<in R, in V>> Map<K, V>.mapKeysTo(destination: C, transform: (Map.Entry<K, V>) -> R): C {
return entries.associateByTo(destination, { transform(it) }, { it.value })
return entries.associateByTo(destination, transform, { it.value })
}
/**
@@ -429,35 +435,40 @@ public operator fun <K, V> Map<K, V>.plus(map: Map<K, V>): Map<K, V>
/**
* Appends or replaces the given [pair] in this mutable map.
*/
public operator fun <K, V> MutableMap<in K, in V>.plusAssign(pair: Pair<K, V>) {
@kotlin.internal.InlineOnly
public inline operator fun <K, V> MutableMap<in K, in V>.plusAssign(pair: Pair<K, V>) {
put(pair.first, pair.second)
}
/**
* Appends or replaces all pairs from the given collection of [pairs] in this mutable map.
*/
public operator fun <K, V> MutableMap<in K, in V>.plusAssign(pairs: Iterable<Pair<K, V>>) {
@kotlin.internal.InlineOnly
public inline operator fun <K, V> MutableMap<in K, in V>.plusAssign(pairs: Iterable<Pair<K, V>>) {
putAll(pairs)
}
/**
* Appends or replaces all pairs from the given array of [pairs] in this mutable map.
*/
public operator fun <K, V> MutableMap<in K, in V>.plusAssign(pairs: Array<out Pair<K, V>>) {
@kotlin.internal.InlineOnly
public inline operator fun <K, V> MutableMap<in K, in V>.plusAssign(pairs: Array<out Pair<K, V>>) {
putAll(pairs)
}
/**
* Appends or replaces all pairs from the given sequence of [pairs] in this mutable map.
*/
public operator fun <K, V> MutableMap<in K, in V>.plusAssign(pairs: Sequence<Pair<K, V>>) {
@kotlin.internal.InlineOnly
public inline operator fun <K, V> MutableMap<in K, in V>.plusAssign(pairs: Sequence<Pair<K, V>>) {
putAll(pairs)
}
/**
* Appends or replaces all entries from the given [map] in this mutable map.
*/
public operator fun <K, V> MutableMap<in K, in V>.plusAssign(map: Map<K, V>) {
@kotlin.internal.InlineOnly
public inline operator fun <K, V> MutableMap<in K, in V>.plusAssign(map: Map<K, V>) {
putAll(map)
}
@@ -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 <K, V> MutableMap<K, V>.set(key: K, value: V): Unit {
@kotlin.internal.InlineOnly
public inline operator fun <K, V> MutableMap<K, V>.set(key: K, value: V): Unit {
put(key, value)
}
@@ -72,6 +73,7 @@ public fun <K : Comparable<K>, V> sortedMapOf(vararg pairs: Pair<K, V>): SortedM
*
* @sample test.collections.MapJVMTest.toProperties
*/
public fun Map<String, String>.toProperties(): Properties
@kotlin.internal.InlineOnly
public inline fun Map<String, String>.toProperties(): Properties
= Properties().apply { putAll(this@toProperties) }
@@ -10,7 +10,8 @@ import java.util.*
*
* Allows to overcome type-safety restriction of `containsAll` that requires to pass a collection of type `Collection<E>`.
*/
public fun <@kotlin.internal.OnlyInputTypes T> Collection<T>.containsAll(elements: Collection<T>): Boolean = this.containsAll(elements)
@kotlin.internal.InlineOnly
public inline fun <@kotlin.internal.OnlyInputTypes T> Collection<T>.containsAll(elements: Collection<T>): 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<T>.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<out T>.remove(element: T): Boolean = (this as MutableCollection<T>).remove(element)
@kotlin.internal.InlineOnly
public inline fun <@kotlin.internal.OnlyInputTypes T> MutableCollection<out T>.remove(element: T): Boolean = (this as MutableCollection<T>).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<out T>.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<out T>.removeAll(elements: Collection<T>): Boolean = (this as MutableCollection<T>).removeAll(elements)
@kotlin.internal.InlineOnly
public inline fun <@kotlin.internal.OnlyInputTypes T> MutableCollection<out T>.removeAll(elements: Collection<T>): Boolean = (this as MutableCollection<T>).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<out T>.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<out T>.retainAll(elements: Collection<T>): Boolean = (this as MutableCollection<T>).retainAll(elements)
@kotlin.internal.InlineOnly
public inline fun <@kotlin.internal.OnlyInputTypes T> MutableCollection<out T>.retainAll(elements: Collection<T>): Boolean = (this as MutableCollection<T>).retainAll(elements)
/**
* Adds the specified [element] to this mutable collection.
*/
public operator fun <T> MutableCollection<in T>.plusAssign(element: T) {
@kotlin.internal.InlineOnly
public inline operator fun <T> MutableCollection<in T>.plusAssign(element: T) {
this.add(element)
}
/**
* Adds all elements of the given [elements] collection to this mutable collection.
*/
public operator fun <T> MutableCollection<in T>.plusAssign(elements: Iterable<T>) {
@kotlin.internal.InlineOnly
public inline operator fun <T> MutableCollection<in T>.plusAssign(elements: Iterable<T>) {
this.addAll(elements)
}
/**
* Adds all elements of the given [elements] array to this mutable collection.
*/
public operator fun <T> MutableCollection<in T>.plusAssign(elements: Array<T>) {
@kotlin.internal.InlineOnly
public inline operator fun <T> MutableCollection<in T>.plusAssign(elements: Array<T>) {
this.addAll(elements)
}
/**
* Adds all elements of the given [elements] sequence to this mutable collection.
*/
public operator fun <T> MutableCollection<in T>.plusAssign(elements: Sequence<T>) {
@kotlin.internal.InlineOnly
public inline operator fun <T> MutableCollection<in T>.plusAssign(elements: Sequence<T>) {
this.addAll(elements)
}
/**
* Removes a single instance of the specified [element] from this mutable collection.
*/
public operator fun <T> MutableCollection<in T>.minusAssign(element: T) {
@kotlin.internal.InlineOnly
public inline operator fun <T> MutableCollection<in T>.minusAssign(element: T) {
this.remove(element)
}
/**
* Removes all elements contained in the given [elements] collection from this mutable collection.
*/
public operator fun <T> MutableCollection<in T>.minusAssign(elements: Iterable<T>) {
@kotlin.internal.InlineOnly
public inline operator fun <T> MutableCollection<in T>.minusAssign(elements: Iterable<T>) {
this.removeAll(elements)
}
/**
* Removes all elements contained in the given [elements] array from this mutable collection.
*/
public operator fun <T> MutableCollection<in T>.minusAssign(elements: Array<T>) {
@kotlin.internal.InlineOnly
public inline operator fun <T> MutableCollection<in T>.minusAssign(elements: Array<T>) {
this.removeAll(elements)
}
/**
* Removes all elements contained in the given [elements] sequence from this mutable collection.
*/
public operator fun <T> MutableCollection<in T>.minusAssign(elements: Sequence<T>) {
@kotlin.internal.InlineOnly
public inline operator fun <T> MutableCollection<in T>.minusAssign(elements: Sequence<T>) {
this.removeAll(elements)
}
@@ -20,7 +20,8 @@ public fun <T> Iterator<T>.asSequence(): Sequence<T> {
/**
* Creates a sequence that returns all values from this enumeration. The sequence is constrained to be iterated only once.
*/
public fun<T> Enumeration<T>.asSequence(): Sequence<T> = this.iterator().asSequence()
@kotlin.internal.InlineOnly
public inline fun<T> Enumeration<T>.asSequence(): Sequence<T> = this.iterator().asSequence()
/**
* Creates a sequence that returns the specified values.
@@ -29,7 +29,8 @@ public fun <T> emptySet(): Set<T> = EmptySet
public fun <T> setOf(vararg elements: T): Set<T> = if (elements.size > 0) elements.toSet() else emptySet()
/** Returns an empty read-only set. The returned set is serializable (JVM). */
public fun <T> setOf(): Set<T> = emptySet()
@kotlin.internal.InlineOnly
public inline fun <T> setOf(): Set<T> = emptySet()
/** Returns a new [MutableSet] with the given elements. */
public fun <T> mutableSetOf(vararg elements: T): MutableSet<T> = elements.toCollection(LinkedHashSet(mapCapacity(elements.size)))
@@ -41,7 +42,8 @@ public fun <T> hashSetOf(vararg elements: T): HashSet<T> = elements.toCollection
public fun <T> linkedSetOf(vararg elements: T): LinkedHashSet<T> = elements.toCollection(LinkedHashSet(mapCapacity(elements.size)))
/** Returns this Set if it's not `null` and the empty set otherwise. */
public fun <T> Set<T>?.orEmpty(): Set<T> = this ?: emptySet()
@kotlin.internal.InlineOnly
public inline fun <T> Set<T>?.orEmpty(): Set<T> = this ?: emptySet()
/**
* Returns an immutable set containing only the specified object [element].