From eb9134fda20a9c00a062c9f834bb4ebabbe7f7f6 Mon Sep 17 00:00:00 2001 From: ForteScarlet Date: Sun, 28 Jan 2024 22:49:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BA=E4=B8=A4=E4=B8=AA=E5=A4=9A=E5=B9=B3?= =?UTF-8?q?=E5=8F=B0Queue=E7=B1=BB=E5=9E=8B=E5=A2=9E=E5=8A=A0=20isEmpty?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../simbot/common/collection/concurrentQueues.kt | 15 +++++++++++++++ .../common/collection/concurrentQueues.js.kt | 8 ++++++++ .../common/collection/concurrentQueues.jvm.kt | 9 +++++++++ .../common/collection/concurrentQueues.native.kt | 10 ++++++++++ .../common/collection/concurrentQueues.wasmjs.kt | 8 ++++++++ 5 files changed, 50 insertions(+) diff --git a/simbot-commons/simbot-common-collection/src/commonMain/kotlin/love/forte/simbot/common/collection/concurrentQueues.kt b/simbot-commons/simbot-common-collection/src/commonMain/kotlin/love/forte/simbot/common/collection/concurrentQueues.kt index ddf911f2b..c7c87e663 100644 --- a/simbot-commons/simbot-common-collection/src/commonMain/kotlin/love/forte/simbot/common/collection/concurrentQueues.kt +++ b/simbot-commons/simbot-common-collection/src/commonMain/kotlin/love/forte/simbot/common/collection/concurrentQueues.kt @@ -37,6 +37,11 @@ public interface ConcurrentQueue : Iterable { */ public val size: Int + /** + * 当前队列是否为空 + */ + public fun isEmpty(): Boolean + /** * 添加指定元素 */ @@ -85,6 +90,16 @@ public interface PriorityConcurrentQueue : Iterable { */ public val size: Int + /** + * 指定优先级下的队列是否为空 + */ + public fun isEmpty(priority: Int): Boolean + + /** + * 整个队列是否为空 + */ + public fun isEmpty(): Boolean + /** * 将具有指定优先级的元素添加到集合中。 * diff --git a/simbot-commons/simbot-common-collection/src/jsMain/kotlin/love/forte/simbot/common/collection/concurrentQueues.js.kt b/simbot-commons/simbot-common-collection/src/jsMain/kotlin/love/forte/simbot/common/collection/concurrentQueues.js.kt index 162fa4095..7888f9c74 100644 --- a/simbot-commons/simbot-common-collection/src/jsMain/kotlin/love/forte/simbot/common/collection/concurrentQueues.js.kt +++ b/simbot-commons/simbot-common-collection/src/jsMain/kotlin/love/forte/simbot/common/collection/concurrentQueues.js.kt @@ -31,6 +31,9 @@ internal class ConcurrentQueueImpl : ConcurrentQueue { override val size: Int get() = list.size + override fun isEmpty(): Boolean = + list.isEmpty() + override fun add(value: T) { list.addLast(value) } @@ -60,6 +63,11 @@ internal class PriorityConcurrentQueueImpl : PriorityConcurrentQueue { override val size: Int get() = lists.values.sumOf { it.size } + override fun isEmpty(priority: Int): Boolean = + lists[priority]?.isEmpty() ?: true + + override fun isEmpty(): Boolean = lists.values.all { it.isEmpty() } + override fun add(priority: Int, value: T) { val list = lists.getOrPut(priority) { ArrayDeque() } list.add(value) diff --git a/simbot-commons/simbot-common-collection/src/jvmMain/kotlin/love/forte/simbot/common/collection/concurrentQueues.jvm.kt b/simbot-commons/simbot-common-collection/src/jvmMain/kotlin/love/forte/simbot/common/collection/concurrentQueues.jvm.kt index 1584a1b19..28bfa2ef9 100644 --- a/simbot-commons/simbot-common-collection/src/jvmMain/kotlin/love/forte/simbot/common/collection/concurrentQueues.jvm.kt +++ b/simbot-commons/simbot-common-collection/src/jvmMain/kotlin/love/forte/simbot/common/collection/concurrentQueues.jvm.kt @@ -33,6 +33,9 @@ internal class ConcurrentQueueImpl : ConcurrentQueue { override val size: Int get() = queue.size + override fun isEmpty(): Boolean = + queue.isEmpty() + override fun add(value: T) { queue.add(value) } @@ -65,6 +68,12 @@ internal class PriorityConcurrentQueueImpl : PriorityConcurrentQueue { override val size: Int get() = queueMap.values.sumOf { it.size } + override fun isEmpty(priority: Int): Boolean = + queueMap[priority]?.isEmpty() ?: true + + override fun isEmpty(): Boolean = + queueMap.values.all { it.isEmpty() } + override fun add(priority: Int, value: T) { val queue = queueMap.computeIfAbsent(priority) { ConcurrentLinkedQueue() } queue.add(value) diff --git a/simbot-commons/simbot-common-collection/src/nativeMain/kotlin/love/forte/simbot/common/collection/concurrentQueues.native.kt b/simbot-commons/simbot-common-collection/src/nativeMain/kotlin/love/forte/simbot/common/collection/concurrentQueues.native.kt index 167cb1b26..8a2301409 100644 --- a/simbot-commons/simbot-common-collection/src/nativeMain/kotlin/love/forte/simbot/common/collection/concurrentQueues.native.kt +++ b/simbot-commons/simbot-common-collection/src/nativeMain/kotlin/love/forte/simbot/common/collection/concurrentQueues.native.kt @@ -32,6 +32,9 @@ internal class ConcurrentQueueImpl : ConcurrentQueue { override val size: Int get() = listRef.value.size + override fun isEmpty(): Boolean = + listRef.value.isEmpty() + override fun add(value: T) { listRef.update { old -> when (old.size) { @@ -80,6 +83,13 @@ internal class PriorityConcurrentQueueImpl : PriorityConcurrentQueue { override val size: Int get() = lists.value.sumOf { it.list.value.size } + override fun isEmpty(priority: Int): Boolean = + lists.value.find { it.priority == priority }?.list?.value?.isEmpty() + ?: true + + override fun isEmpty(): Boolean = lists.value.all { it.list.value.isEmpty() } + + private fun findByPriority(priority: Int): ListWithPriority? = lists.value.find { it.priority == priority } diff --git a/simbot-commons/simbot-common-collection/src/wasmJsMain/kotlin/love/forte/simbot/common/collection/concurrentQueues.wasmjs.kt b/simbot-commons/simbot-common-collection/src/wasmJsMain/kotlin/love/forte/simbot/common/collection/concurrentQueues.wasmjs.kt index d8b5e2197..13cc8b80a 100644 --- a/simbot-commons/simbot-common-collection/src/wasmJsMain/kotlin/love/forte/simbot/common/collection/concurrentQueues.wasmjs.kt +++ b/simbot-commons/simbot-common-collection/src/wasmJsMain/kotlin/love/forte/simbot/common/collection/concurrentQueues.wasmjs.kt @@ -31,6 +31,9 @@ internal class ConcurrentQueueImpl : ConcurrentQueue { override val size: Int get() = list.size + override fun isEmpty(): Boolean = + list.isEmpty() + override fun add(value: T) { list.addLast(value) } @@ -60,6 +63,11 @@ internal class PriorityConcurrentQueueImpl : PriorityConcurrentQueue { override val size: Int get() = lists.values.sumOf { it.size } + override fun isEmpty(priority: Int): Boolean = + lists[priority]?.isEmpty() ?: true + + override fun isEmpty(): Boolean = lists.values.all { it.isEmpty() } + override fun add(priority: Int, value: T) { val list = lists.getOrPut(priority) { mutableListOf() } list.add(value)