1 @file:Suppress("UNCHECKED_CAST") 2 3 package kotlinx.coroutines.internal 4 5 import kotlinx.coroutines.assert 6 import kotlin.jvm.* 7 8 /* 9 * Inline class that represents a mutable list, but does not allocate an underlying storage 10 * for zero and one elements. 11 * Cannot be parametrized with `List<*>`. 12 */ 13 @JvmInline 14 internal value class InlineList<E>(private val holder: Any? = null) { plusnull15 operator fun plus(element: E): InlineList<E> { 16 assert { element !is List<*> } // Lists are prohibited 17 return when (holder) { 18 null -> InlineList(element) 19 is ArrayList<*> -> { 20 (holder as ArrayList<E>).add(element) 21 InlineList(holder) 22 } 23 else -> { 24 val list = ArrayList<E>(4) 25 list.add(holder as E) 26 list.add(element) 27 InlineList(list) 28 } 29 } 30 } 31 forEachReversednull32 inline fun forEachReversed(action: (E) -> Unit) { 33 when (holder) { 34 null -> return 35 !is ArrayList<*> -> action(holder as E) 36 else -> { 37 val list = holder as ArrayList<E> 38 for (i in (list.size - 1) downTo 0) { 39 action(list[i]) 40 } 41 } 42 } 43 } 44 } 45