Add close(onEach)

This commit is contained in:
Him188 2020-03-22 15:57:14 +08:00
parent 1d34f3e8a0
commit 9b191f6763

View File

@ -14,6 +14,7 @@ package net.mamoe.mirai.utils
import kotlinx.atomicfu.AtomicRef import kotlinx.atomicfu.AtomicRef
import kotlinx.atomicfu.atomic import kotlinx.atomicfu.atomic
import kotlinx.atomicfu.loop import kotlinx.atomicfu.loop
import kotlin.jvm.JvmOverloads
/** /**
* Collect all the elements into a [MutableList] then cast it as a [List] * Collect all the elements into a [MutableList] then cast it as a [List]
@ -306,14 +307,16 @@ open class LockFreeLinkedList<E> {
} }
} }
@JvmOverloads
@Suppress("unused") @Suppress("unused")
open fun clear() { open fun clear(onEach: ((E) -> Unit)? = null) {
val first = head.nextNode val first = head.nextNode
head.nextNode = tail head.nextNode = tail
first.childIterateReturnFirstUnsatisfying({ first.childIterateReturnFirstUnsatisfying({
val n = it.nextNode val n = it.nextNode
it.nextNode = tail it.nextNode = tail
it.removed.value = true it.removed.value = true
onEach?.invoke(n.nodeValue)
n n
}, { it !== tail }) // clear the link structure, help GC. }, { it !== tail }) // clear the link structure, help GC.
} }