From 999ce68cecd1aaacb4062a09b55f4c62d5e137bf Mon Sep 17 00:00:00 2001 From: Him188 Date: Wed, 12 Feb 2020 20:47:30 +0800 Subject: [PATCH] Fix addLastAll --- .../utils/LockFreeLinkedList.kt | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/LockFreeLinkedList.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/LockFreeLinkedList.kt index c6fd8ed7c..43635ef0a 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/LockFreeLinkedList.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/LockFreeLinkedList.kt @@ -142,13 +142,19 @@ open class LockFreeLinkedList { */ @Suppress("DuplicatedCode") open fun addLastAll(iterable: Iterable) { + var firstNode: Node? = null + var currentNode: Node? = null iterable.forEach { val nextNode = it.asNode(tail) + if (firstNode == null) { + firstNode = nextNode + } currentNode?.nextNode = nextNode currentNode = nextNode } - addLastNode(currentNode ?: error("iterable is empty")) + + firstNode?.let { addLastNode(it) } } /** @@ -156,15 +162,19 @@ open class LockFreeLinkedList { */ @Suppress("DuplicatedCode") open fun addLastAll(iterable: Sequence) { + var firstNode: Node? = null + var currentNode: Node? = null iterable.forEach { val nextNode = it.asNode(tail) - if (currentNode != null) { // do not use `?.` because atomicfu cannot transform properly: IllegalArgumentException: null passed - currentNode!!.nextNodeRef.value = nextNode + if (firstNode == null) { + firstNode = nextNode } + currentNode?.nextNode = nextNode currentNode = nextNode } - addLastNode(currentNode ?: error("iterable is empty")) + + firstNode?.let { addLastNode(it) } } open operator fun plusAssign(element: E) = this.addLast(element)