All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.internal.InlineList.kt Maven / Gradle / Ivy

@file:Suppress("UNCHECKED_CAST")

package kotlinx.coroutines.internal

import kotlinx.coroutines.assert
import kotlin.jvm.*

/*
 * Inline class that represents a mutable list, but does not allocate an underlying storage
 * for zero and one elements.
 * Cannot be parametrized with `List<*>`.
 */
@JvmInline
internal value class InlineList(private val holder: Any? = null) {
    operator fun plus(element: E): InlineList  {
        assert { element !is List<*> } // Lists are prohibited
        return when (holder) {
            null -> InlineList(element)
            is ArrayList<*> -> {
                (holder as ArrayList).add(element)
                InlineList(holder)
            }
            else -> {
                val list = ArrayList(4)
                list.add(holder as E)
                list.add(element)
                InlineList(list)
            }
        }
    }

    inline fun forEachReversed(action: (E) -> Unit) {
        when (holder) {
            null -> return
            !is ArrayList<*> -> action(holder as E)
            else -> {
                val list = holder as ArrayList
                for (i in (list.size - 1) downTo 0) {
                    action(list[i])
                }
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy