Skip to content

Commit

Permalink
Use index-loop collection methods. (#2035)
Browse files Browse the repository at this point in the history
* Use index-loop collection methods.

* Sort.
  • Loading branch information
colinrtwhite authored Jan 10, 2024
1 parent a984fb5 commit acbd555
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
5 changes: 3 additions & 2 deletions coil-core/src/commonMain/kotlin/coil3/ComponentRegistry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import coil3.intercept.Interceptor
import coil3.key.Keyer
import coil3.map.Mapper
import coil3.request.Options
import coil3.util.flatMapIndices
import coil3.util.forEachIndices
import coil3.util.toImmutableList
import kotlin.jvm.JvmOverloads
Expand All @@ -30,11 +31,11 @@ class ComponentRegistry private constructor(
constructor() : this(emptyList(), emptyList(), emptyList(), emptyList(), emptyList())

val fetcherFactories: List<Pair<Fetcher.Factory<out Any>, KClass<out Any>>> by lazy {
lazyFetcherFactories.flatMap { it() }.also { lazyFetcherFactories = emptyList() }
lazyFetcherFactories.flatMapIndices { it() }.also { lazyFetcherFactories = emptyList() }
}

val decoderFactories: List<Decoder.Factory> by lazy {
lazyDecoderFactories.flatMap { it() }.also { lazyDecoderFactories = emptyList() }
lazyDecoderFactories.flatMapIndices { it() }.also { lazyDecoderFactories = emptyList() }
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import coil3.util.SystemCallbacks
import coil3.util.emoji
import coil3.util.get
import coil3.util.log
import coil3.util.mapNotNullIndices
import kotlin.coroutines.coroutineContext
import kotlinx.atomicfu.atomic
import kotlinx.coroutines.CancellationException
Expand Down Expand Up @@ -254,15 +255,15 @@ internal fun ComponentRegistry.Builder.addServiceLoaderComponents(
if (options.serviceLoaderEnabled) {
// Delay reading the fetchers and decoders until the fetching/decoding stage.
addFetcherFactories {
ServiceLoaderComponentRegistry.fetchers.mapNotNull { target ->
ServiceLoaderComponentRegistry.fetchers.mapNotNullIndices { target ->
target as FetcherServiceLoaderTarget<Any>
val factory = target.factory() ?: return@mapNotNull null
val type = target.type() ?: return@mapNotNull null
val factory = target.factory() ?: return@mapNotNullIndices null
val type = target.type() ?: return@mapNotNullIndices null
factory to type
}
}
addDecoderFactories {
ServiceLoaderComponentRegistry.decoders.mapNotNull { target ->
ServiceLoaderComponentRegistry.decoders.mapNotNullIndices { target ->
target.factory()
}
}
Expand Down
36 changes: 24 additions & 12 deletions coil-core/src/commonMain/kotlin/coil3/util/collections.common.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,43 @@ internal expect fun <K, V> Map<K, V>.toImmutableMap(): Map<K, V>

internal expect fun <T> List<T>.toImmutableList(): List<T>

/**
* Functionally the same as [Iterable.forEach] except it generates
* an index-based loop that doesn't use an [Iterator].
*/
/** @see forEach */
@PublishedApi // Used by extension modules.
internal inline fun <T> List<T>.forEachIndices(action: (T) -> Unit) {
for (i in indices) {
action(get(i))
}
}

/**
* Functionally the same as [Iterable.forEachIndexed] except it generates
* an index-based loop that doesn't use an [Iterator].
*/
/** @see forEachIndices */
internal inline fun <T> List<T>.forEachIndexedIndices(action: (Int, T) -> Unit) {
for (i in indices) {
action(i, get(i))
}
}

/**
* Functionally the same as [Iterable.fold] except it generates
* an index-based loop that doesn't use an [Iterator].
*/
/** @see mapNotNull */
internal inline fun <T, R : Any> List<T>.mapNotNullIndices(transform: (T) -> R?): List<R> {
val destination = mutableListOf<R>()
for (i in indices) {
val value = transform(get(i))
if (value != null) {
destination += value
}
}
return destination
}

/** @see flatMap */
internal inline fun <T, R> List<T>.flatMapIndices(transform: (T) -> List<R>): List<R> {
val destination = mutableListOf<R>()
for (i in indices) {
destination += transform(get(i))
}
return destination
}

/** @see fold */
internal inline fun <T, R> List<T>.foldIndices(initial: R, operation: (R, T) -> R): R {
var accumulator = initial
for (i in indices) {
Expand Down

0 comments on commit acbd555

Please # to comment.