mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-09 01:30:17 +08:00
LockFreeLinkedList: fix peekFirst
This commit is contained in:
parent
be63ec48fa
commit
dc02278c11
@ -111,9 +111,13 @@ open class LockFreeLinkedList<E> {
|
||||
}
|
||||
}
|
||||
|
||||
open fun peekFirst(): E = head.nextNode.letValueIfValid { return it } ?: throw NoSuchElementException()
|
||||
|
||||
open fun peekLast(): E = head.iterateBeforeFirst { it === tail }.letValueIfValid { return it } ?: throw NoSuchElementException()
|
||||
open fun peekFirst(): E {
|
||||
return head
|
||||
.iterateBeforeFirst { it === tail }
|
||||
.takeUnless { it.isTail() }
|
||||
?.nodeValue
|
||||
?: throw NoSuchElementException()
|
||||
}
|
||||
|
||||
open fun removeLast(): E {
|
||||
while (true) {
|
||||
@ -770,11 +774,7 @@ open class LockFreeLinkedListNode<E>(
|
||||
internal val nextNodeRef: AtomicRef<LockFreeLinkedListNode<E>> = atomic(nextNode ?: this)
|
||||
|
||||
inline fun <R> letValueIfValid(block: (E) -> R): R? {
|
||||
if (!this.isValidElementNode()) {
|
||||
return null
|
||||
}
|
||||
val value = this.nodeValue
|
||||
return if (value !== null) block(value) else null
|
||||
return this.takeIf { isValidElementNode() }?.nodeValue?.let(block)
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user