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