Commit f5261b25 authored by Him188's avatar Him188

Make member functions in LockFreeLinkedList open

parent ce6da7d1
...@@ -45,7 +45,7 @@ open class LockFreeLinkedList<E> { ...@@ -45,7 +45,7 @@ open class LockFreeLinkedList<E> {
@PublishedApi @PublishedApi
internal val head: Head<E> = Head(tail) internal val head: Head<E> = Head(tail)
fun removeFirst(): E { open fun removeFirst(): E {
while (true) { while (true) {
val currentFirst = head.nextNode val currentFirst = head.nextNode
if (!currentFirst.isValidElementNode()) { if (!currentFirst.isValidElementNode()) {
...@@ -57,11 +57,11 @@ open class LockFreeLinkedList<E> { ...@@ -57,11 +57,11 @@ open class LockFreeLinkedList<E> {
} }
} }
fun peekFirst(): E = head.nextNode.letValueIfValid { return it } ?: throw NoSuchElementException() open fun peekFirst(): E = head.nextNode.letValueIfValid { return it } ?: throw NoSuchElementException()
fun peekLast(): E = head.iterateBeforeFirst { it === tail }.letValueIfValid { return it } ?: throw NoSuchElementException() open fun peekLast(): E = head.iterateBeforeFirst { it === tail }.letValueIfValid { return it } ?: throw NoSuchElementException()
fun removeLast(): E { open fun removeLast(): E {
while (true) { while (true) {
val beforeLast = head.iterateBeforeFirst { it.nextNode === tail } val beforeLast = head.iterateBeforeFirst { it.nextNode === tail }
if (!beforeLast.isValidElementNode()) { if (!beforeLast.isValidElementNode()) {
...@@ -74,7 +74,7 @@ open class LockFreeLinkedList<E> { ...@@ -74,7 +74,7 @@ open class LockFreeLinkedList<E> {
} }
} }
fun addLast(element: E) { open fun addLast(element: E) {
val node = element.asNode(tail) val node = element.asNode(tail)
while (true) { while (true) {
...@@ -85,7 +85,7 @@ open class LockFreeLinkedList<E> { ...@@ -85,7 +85,7 @@ open class LockFreeLinkedList<E> {
} }
} }
operator fun plusAssign(element: E) = this.addLast(element) open operator fun plusAssign(element: E) = this.addLast(element)
inline fun filteringGetOrAdd(filter: (E) -> Boolean, noinline supplier: () -> E): E { inline fun filteringGetOrAdd(filter: (E) -> Boolean, noinline supplier: () -> E): E {
val node = LazyNode(tail, supplier) val node = LazyNode(tail, supplier)
...@@ -122,7 +122,7 @@ open class LockFreeLinkedList<E> { ...@@ -122,7 +122,7 @@ open class LockFreeLinkedList<E> {
}, { it !is Tail }) }, { it !is Tail })
}.dropLast(4) }.dropLast(4)
fun remove(element: E): Boolean { open fun remove(element: E): Boolean {
while (true) { while (true) {
val before = head.iterateBeforeNodeValue(element) val before = head.iterateBeforeNodeValue(element)
val toRemove = before.nextNode val toRemove = before.nextNode
...@@ -148,15 +148,15 @@ open class LockFreeLinkedList<E> { ...@@ -148,15 +148,15 @@ open class LockFreeLinkedList<E> {
val size: Int get() = head.countChildIterate<Node<E>>({ it.nextNode }, { it !is Tail }) - 1 // empty head is always included val size: Int get() = head.countChildIterate<Node<E>>({ it.nextNode }, { it !is Tail }) - 1 // empty head is always included
operator fun contains(element: E): Boolean { open operator fun contains(element: E): Boolean {
forEach { if (it == element) return true } forEach { if (it == element) return true }
return false return false
} }
@Suppress("unused") @Suppress("unused")
fun containsAll(elements: Collection<E>): Boolean = elements.all { contains(it) } open fun containsAll(elements: Collection<E>): Boolean = elements.all { contains(it) }
fun isEmpty(): Boolean = head.allMatching { it.isValidElementNode().not() } open fun isEmpty(): Boolean = head.allMatching { it.isValidElementNode().not() }
inline fun forEach(block: (E) -> Unit) { inline fun forEach(block: (E) -> Unit) {
var node: Node<E> = head var node: Node<E> = head
...@@ -167,10 +167,10 @@ open class LockFreeLinkedList<E> { ...@@ -167,10 +167,10 @@ open class LockFreeLinkedList<E> {
} }
} }
fun addAll(elements: Collection<E>) = elements.forEach { addLast(it) } open fun addAll(elements: Collection<E>) = elements.forEach { addLast(it) }
@Suppress("unused") @Suppress("unused")
fun clear() { open fun clear() {
val first = head.nextNode val first = head.nextNode
head.nextNode = tail head.nextNode = tail
first.childIterateReturnFirstUnsatisfying({ first.childIterateReturnFirstUnsatisfying({
...@@ -182,7 +182,7 @@ open class LockFreeLinkedList<E> { ...@@ -182,7 +182,7 @@ open class LockFreeLinkedList<E> {
} }
@Suppress("unused") @Suppress("unused")
fun removeAll(elements: Collection<E>): Boolean = elements.all { remove(it) } open fun removeAll(elements: Collection<E>): Boolean = elements.all { remove(it) }
/* /*
......
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