Skip to content

Commit cc14b3a

Browse files
author
Abduqodiri Qurbonzoda
committed
Optimize persistent list batch updates
1 parent a6d45f5 commit cc14b3a

File tree

9 files changed

+652
-53
lines changed

9 files changed

+652
-53
lines changed

core/commonMain/src/implementations/immutableList/AbstractListIterator.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,29 @@ internal abstract class AbstractListIterator<out E>(var index: Int, var size: In
2121
override fun previousIndex(): Int {
2222
return index - 1
2323
}
24+
25+
internal fun checkHasNext() {
26+
if (!hasNext())
27+
throw NoSuchElementException()
28+
}
29+
30+
internal fun checkHasPrevious() {
31+
if (!hasPrevious())
32+
throw NoSuchElementException()
33+
}
34+
}
35+
36+
37+
internal class SingleElementListIterator<E>(private val element: E, index: Int): AbstractListIterator<E>(index, 1) {
38+
override fun next(): E {
39+
checkHasNext()
40+
index++
41+
return element
42+
}
43+
44+
override fun previous(): E {
45+
checkHasPrevious()
46+
index--
47+
return element
48+
}
2449
}

core/commonMain/src/implementations/immutableList/AbstractPersistentList.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,7 @@ abstract class AbstractPersistentList<E> : PersistentList<E>, AbstractList<E>()
3131
}
3232

3333
override fun removeAll(elements: Collection<E>): PersistentList<E> {
34-
return mutate { it.removeAll(elements) }
35-
}
36-
37-
override fun removeAll(predicate: (E) -> Boolean): PersistentList<E> {
38-
return mutate { it.removeAll(predicate) }
34+
return removeAll { elements.contains(it) }
3935
}
4036

4137
override fun clear(): PersistentList<E> {

core/commonMain/src/implementations/immutableList/PersistentVector.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import kotlinx.collections.immutable.PersistentList
99
import kotlinx.collections.immutable.internal.ListImplementation.checkElementIndex
1010
import kotlinx.collections.immutable.internal.ListImplementation.checkPositionIndex
1111
import kotlinx.collections.immutable.internal.assert
12+
import kotlinx.collections.immutable.mutate
1213

1314
/**
1415
* Persistent vector made of a trie of leaf buffers entirely filled with [MAX_BUFFER_SIZE] elements and a tail having
@@ -78,7 +79,6 @@ internal class PersistentVector<E>(private val root: Array<Any?>,
7879
return newRootNode
7980
}
8081

81-
8282
override fun add(index: Int, element: E): PersistentList<E> {
8383
checkPositionIndex(index, size)
8484
if (index == size) {
@@ -259,7 +259,11 @@ internal class PersistentVector<E>(private val root: Array<Any?>,
259259
return newRoot
260260
}
261261

262-
override fun builder(): PersistentList.Builder<E> {
262+
override fun removeAll(predicate: (E) -> Boolean): PersistentList<E> {
263+
return builder().also { it.removeAllWithPredicate(predicate) }.build()
264+
}
265+
266+
override fun builder(): PersistentVectorBuilder<E> {
263267
return PersistentVectorBuilder(this, root, tail, rootShift)
264268
}
265269

0 commit comments

Comments
 (0)