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