Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

为两个多平台Queue类型增加 isEmpty #765

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public interface ConcurrentQueue<T> : Iterable<T> {
*/
public val size: Int

/**
* 当前队列是否为空
*/
public fun isEmpty(): Boolean

/**
* 添加指定元素
*/
Expand Down Expand Up @@ -85,6 +90,16 @@ public interface PriorityConcurrentQueue<T> : Iterable<T> {
*/
public val size: Int

/**
* 指定优先级下的队列是否为空
*/
public fun isEmpty(priority: Int): Boolean

/**
* 整个队列是否为空
*/
public fun isEmpty(): Boolean

/**
* 将具有指定优先级的元素添加到集合中。
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ internal class ConcurrentQueueImpl<T> : ConcurrentQueue<T> {
override val size: Int
get() = list.size

override fun isEmpty(): Boolean =
list.isEmpty()

override fun add(value: T) {
list.addLast(value)
}
Expand Down Expand Up @@ -60,6 +63,11 @@ internal class PriorityConcurrentQueueImpl<T> : PriorityConcurrentQueue<T> {
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ internal class ConcurrentQueueImpl<T> : ConcurrentQueue<T> {
override val size: Int
get() = queue.size

override fun isEmpty(): Boolean =
queue.isEmpty()

override fun add(value: T) {
queue.add(value)
}
Expand Down Expand Up @@ -65,6 +68,12 @@ internal class PriorityConcurrentQueueImpl<T> : PriorityConcurrentQueue<T> {
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ internal class ConcurrentQueueImpl<T> : ConcurrentQueue<T> {
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) {
Expand Down Expand Up @@ -80,6 +83,13 @@ internal class PriorityConcurrentQueueImpl<T> : PriorityConcurrentQueue<T> {
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<T>? =
lists.value.find { it.priority == priority }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ internal class ConcurrentQueueImpl<T> : ConcurrentQueue<T> {
override val size: Int
get() = list.size

override fun isEmpty(): Boolean =
list.isEmpty()

override fun add(value: T) {
list.addLast(value)
}
Expand Down Expand Up @@ -60,6 +63,11 @@ internal class PriorityConcurrentQueueImpl<T> : PriorityConcurrentQueue<T> {
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)
Expand Down
Loading