/* * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ @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]) } } } } }