Commit bf645717 authored by Him188's avatar Him188

Improve LockFreeLinkedList

parent 42221c5d
......@@ -24,10 +24,12 @@ class ContactList<C : Contact>(@MiraiInternalAPI val delegate: LockFreeLinkedLis
operator fun get(id: Long): C = delegate[id]
fun getOrNull(id: Long): C? = delegate.getOrNull(id)
fun containsId(id: Long): Boolean = delegate.getOrNull(id) != null
@Deprecated("Use contains instead", ReplaceWith("contains(id)"))
fun containsId(id: Long): Boolean = contains(id)
val size: Int get() = delegate.size
operator fun contains(element: C): Boolean = delegate.contains(element)
operator fun contains(id: Long): Boolean = delegate.getOrNull(id) != null
fun containsAll(elements: Collection<C>): Boolean = elements.all { contains(it) }
fun isEmpty(): Boolean = delegate.isEmpty()
inline fun forEach(block: (C) -> Unit) = delegate.forEach(block)
......@@ -50,5 +52,3 @@ inline fun <C : Contact> LockFreeLinkedList<C>.filteringGetOrNull(filter: (C) ->
return null
}
fun <C : Contact> LockFreeLinkedList<C>.getOrAdd(id: Long, supplier: () -> C): C =
filteringGetOrAdd({ it.id == id }, supplier)
......@@ -19,12 +19,12 @@ inline fun <E> LockFreeLinkedList<E>.joinToString(
}.dropLast(separator.length) + postfix
/**
* Returns a [List] containing all the elements in [this] in the same order
* Collect all the elements into a [MutableList] then cast it as a [List]
*/
fun <E> LockFreeLinkedList<E>.toList(): List<E> = toMutableList()
/**
* Returns a [MutableList] containing all the elements in [this] in the same order
* Collect all the elements into a [MutableList].
*/
fun <E> LockFreeLinkedList<E>.toMutableList(): MutableList<E> {
val list = mutableListOf<E>()
......@@ -32,6 +32,33 @@ fun <E> LockFreeLinkedList<E>.toMutableList(): MutableList<E> {
return list
}
/**
* Collect all the elements into a [MutableSet] then cast it as a [Set]
*/
fun <E> LockFreeLinkedList<E>.toSet(): Set<E> = toMutableSet()
/**
* Collect all the elements into a [MutableSet].
*/
fun <E> LockFreeLinkedList<E>.toMutableSet(): MutableSet<E> {
val list = mutableSetOf<E>()
this.forEach { list.add(it) }
return list
}
/**
* Builds a [Sequence] containing all the elements in [this] in the same order.
*
* Note that the sequence is dynamic, that is, elements are yielded atomically only when it is required
*/
fun <E> LockFreeLinkedList<E>.asSequence(): Sequence<E> {
return sequence {
forEach {
yield(it)
}
}
}
/**
* Implementation of lock-free LinkedList.
*
......@@ -87,6 +114,9 @@ open class LockFreeLinkedList<E> {
open operator fun plusAssign(element: E) = this.addLast(element)
/**
* 过滤并获取, 获取不到则添加一个元素.
*/
inline fun filteringGetOrAdd(filter: (E) -> Boolean, noinline supplier: () -> E): E {
val node = LazyNode(tail, supplier)
......@@ -149,6 +179,9 @@ open class LockFreeLinkedList<E> {
}
}
/**
* 动态计算的大小
*/
val size: Int get() = head.countChildIterate<Node<E>>({ it.nextNode }, { it !is Tail }) - 1 // empty head is always included
open operator fun contains(element: E): Boolean {
......
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