Migrate to release coroutines

This commit is contained in:
Dmitry Petrov
2018-09-18 14:11:52 +03:00
committed by Ilmir Usmanov
parent bbc73ec0e5
commit 0191e3d1cf
17 changed files with 46 additions and 55 deletions
@@ -17,13 +17,12 @@
package org.jetbrains.kotlin.utils
import java.util.*
import kotlin.coroutines.experimental.SequenceBuilder
fun <K, V> Iterable<K>.keysToMap(value: (K) -> V): Map<K, V> {
return associateBy({ it }, value)
}
fun <K, V: Any> Iterable<K>.keysToMapExceptNulls(value: (K) -> V?): Map<K, V> {
fun <K, V : Any> Iterable<K>.keysToMapExceptNulls(value: (K) -> V?): Map<K, V> {
val map = LinkedHashMap<K, V>()
for (k in this) {
val v = value(k)
@@ -47,45 +46,44 @@ inline fun <K, V> MutableMap<K, V>.getOrPutNullable(key: K, defaultValue: () ->
val answer = defaultValue()
put(key, answer)
answer
}
else {
} else {
get(key) as V
}
}
inline fun <T, C: Collection<T>> C.ifEmpty(body: () -> C): C = if (isEmpty()) body() else this
inline fun <T, C : Collection<T>> C.ifEmpty(body: () -> C): C = if (isEmpty()) body() else this
inline fun <K, V, M: Map<K, V>> M.ifEmpty(body: () -> M): M = if (isEmpty()) body() else this
inline fun <K, V, M : Map<K, V>> M.ifEmpty(body: () -> M): M = if (isEmpty()) body() else this
inline fun <T> Array<out T>.ifEmpty(body: () -> Array<out T>): Array<out T> = if (isEmpty()) body() else this
fun <T: Any> MutableCollection<T>.addIfNotNull(t: T?) {
fun <T : Any> MutableCollection<T>.addIfNotNull(t: T?) {
if (t != null) add(t)
}
suspend fun <T: Any> SequenceBuilder<T>.yieldIfNotNull(t: T?) = if (t != null) yield(t) else Unit
suspend fun <T : Any> SequenceScope<T>.yieldIfNotNull(t: T?) = if (t != null) yield(t) else Unit
fun <K, V> newHashMapWithExpectedSize(expectedSize: Int): HashMap<K, V> =
HashMap(capacity(expectedSize))
HashMap(capacity(expectedSize))
fun <E> newHashSetWithExpectedSize(expectedSize: Int): HashSet<E> =
HashSet(capacity(expectedSize))
HashSet(capacity(expectedSize))
fun <K, V> newLinkedHashMapWithExpectedSize(expectedSize: Int): LinkedHashMap<K, V> =
LinkedHashMap(capacity(expectedSize))
LinkedHashMap(capacity(expectedSize))
fun <E> newLinkedHashSetWithExpectedSize(expectedSize: Int): LinkedHashSet<E> =
LinkedHashSet(capacity(expectedSize))
LinkedHashSet(capacity(expectedSize))
private fun capacity(expectedSize: Int): Int =
if (expectedSize < 3) 3 else expectedSize + expectedSize / 3 + 1
if (expectedSize < 3) 3 else expectedSize + expectedSize / 3 + 1
fun <T> ArrayList<T>.compact(): List<T> =
when (size) {
0 -> emptyList()
1 -> listOf(first())
else -> apply { trimToSize() }
}
when (size) {
0 -> emptyList()
1 -> listOf(first())
else -> apply { trimToSize() }
}
fun <T> List<T>.indexOfFirst(startFrom: Int, predicate: (T) -> Boolean): Int {
for (index in startFrom..lastIndex) {