mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-22 13:46:13 +08:00
Fix addLastAll
This commit is contained in:
parent
20d7bdb4e4
commit
999ce68cec
@ -142,13 +142,19 @@ open class LockFreeLinkedList<E> {
|
|||||||
*/
|
*/
|
||||||
@Suppress("DuplicatedCode")
|
@Suppress("DuplicatedCode")
|
||||||
open fun addLastAll(iterable: Iterable<E>) {
|
open fun addLastAll(iterable: Iterable<E>) {
|
||||||
|
var firstNode: Node<E>? = null
|
||||||
|
|
||||||
var currentNode: Node<E>? = null
|
var currentNode: Node<E>? = null
|
||||||
iterable.forEach {
|
iterable.forEach {
|
||||||
val nextNode = it.asNode(tail)
|
val nextNode = it.asNode(tail)
|
||||||
|
if (firstNode == null) {
|
||||||
|
firstNode = nextNode
|
||||||
|
}
|
||||||
currentNode?.nextNode = nextNode
|
currentNode?.nextNode = nextNode
|
||||||
currentNode = nextNode
|
currentNode = nextNode
|
||||||
}
|
}
|
||||||
addLastNode(currentNode ?: error("iterable is empty"))
|
|
||||||
|
firstNode?.let { addLastNode(it) }
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -156,15 +162,19 @@ open class LockFreeLinkedList<E> {
|
|||||||
*/
|
*/
|
||||||
@Suppress("DuplicatedCode")
|
@Suppress("DuplicatedCode")
|
||||||
open fun addLastAll(iterable: Sequence<E>) {
|
open fun addLastAll(iterable: Sequence<E>) {
|
||||||
|
var firstNode: Node<E>? = null
|
||||||
|
|
||||||
var currentNode: Node<E>? = null
|
var currentNode: Node<E>? = null
|
||||||
iterable.forEach {
|
iterable.forEach {
|
||||||
val nextNode = it.asNode(tail)
|
val nextNode = it.asNode(tail)
|
||||||
if (currentNode != null) { // do not use `?.` because atomicfu cannot transform properly: IllegalArgumentException: null passed
|
if (firstNode == null) {
|
||||||
currentNode!!.nextNodeRef.value = nextNode
|
firstNode = nextNode
|
||||||
}
|
}
|
||||||
|
currentNode?.nextNode = nextNode
|
||||||
currentNode = nextNode
|
currentNode = nextNode
|
||||||
}
|
}
|
||||||
addLastNode(currentNode ?: error("iterable is empty"))
|
|
||||||
|
firstNode?.let { addLastNode(it) }
|
||||||
}
|
}
|
||||||
|
|
||||||
open operator fun plusAssign(element: E) = this.addLast(element)
|
open operator fun plusAssign(element: E) = this.addLast(element)
|
||||||
|
Loading…
Reference in New Issue
Block a user