Skip to content

Commit 45316d4

Browse files
Abduqodiri Qurbonzodaqurbonzoda
authored andcommitted
Fix creation of SmallPersistentList from mutable root buffer
1 parent c4df377 commit 45316d4

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ internal class PersistentVector<E>(private val root: Array<Any?>,
180180
*/
181181
private fun pullLastBufferFromRoot(root: Array<Any?>, rootSize: Int, shift: Int): PersistentList<E> {
182182
if (shift == 0) {
183-
return SmallPersistentVector(root)
183+
val buffer = if (root.size == MUTABLE_BUFFER_SIZE) root.copyOf(MAX_BUFFER_SIZE) else root
184+
return SmallPersistentVector(buffer)
184185
}
185186
val tailCarry = ObjectRef(null)
186187
val newRoot = pullLastBuffer(root, shift, rootSize - 1, tailCarry)!!

core/commonTest/src/contract/list/ImmutableListTest.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ class ImmutableListTest {
9696
assertEquals(emptyList<Char>(), list.clear())
9797
}
9898

99+
@Test
100+
fun smallPersistentListFromMutableBuffer() {
101+
val list = List(33) { it }
102+
var vector = persistentListOf<Int>().mutate { it.addAll(list) }
103+
vector = vector.removeAt(vector.lastIndex)
104+
assertEquals(list.dropLast(1), vector)
105+
}
106+
99107
@Test fun subList() {
100108
val list = "abcxaxyz12".toImmutableList()
101109
val subList = list.subList(2, 5) // 2, 3, 4

0 commit comments

Comments
 (0)