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)