Commit 999ce68c authored by Him188's avatar Him188

Fix addLastAll

parent 20d7bdb4
...@@ -142,13 +142,19 @@ open class LockFreeLinkedList<E> { ...@@ -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> { ...@@ -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)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment