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

Optimize SmallPersistentVector.removeAll #134

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,31 +49,37 @@ internal class SmallPersistentVector<E>(private val buffer: Array<Any?>) : Immut
}

override fun removeAll(predicate: (E) -> Boolean): PersistentList<E> {
var newBuffer = buffer
var newSize = size

var anyRemoved = false
var leftRetained = 0
var rightRetained = 0
var rightRetainedBuffer: Array<Any?>? = null

for (index in 0 until size) {
@Suppress("UNCHECKED_CAST")
val element = buffer[index] as E

if (predicate(element)) {
if (!anyRemoved) {
newBuffer = buffer.copyOf()
newSize = index

anyRemoved = true
if (rightRetainedBuffer == null) {
leftRetained = index
rightRetainedBuffer = arrayOfNulls(size - index - 1)
}
} else if (anyRemoved) {
newBuffer[newSize++] = element
} else if (rightRetainedBuffer != null) {
rightRetainedBuffer[rightRetained++] = element
}
}
return when (newSize) {
size -> this
0 -> EMPTY
else -> SmallPersistentVector(newBuffer.copyOfRange(0, newSize))

if (rightRetainedBuffer == null) {
return this
}

val newSize = leftRetained + rightRetained
if (newSize == 0) {
return EMPTY
}

val newBuffer = bufferOfSize(newSize)
buffer.copyInto(newBuffer, 0, 0, leftRetained)
rightRetainedBuffer.copyInto(newBuffer, leftRetained, 0, rightRetained)
return SmallPersistentVector(newBuffer)
}

override fun addAll(index: Int, c: Collection<E>): PersistentList<E> {
Expand Down Expand Up @@ -158,4 +164,4 @@ internal class SmallPersistentVector<E>(private val buffer: Array<Any?>) : Immut
companion object {
val EMPTY = SmallPersistentVector<Nothing>(emptyArray())
}
}
}