diego-gomez-olvera
07/17/2020, 11:18 AMpublic fun <T> MutableList<T>.removeAll(predicate: (T) -> Boolean): Boolean = filterInPlace(predicate, true)
private fun <T> MutableList<T>.filterInPlace(predicate: (T) -> Boolean, predicateResultToRemove: Boolean): Boolean {
if (this !is RandomAccess)
return (this as MutableIterable<T>).filterInPlace(predicate, predicateResultToRemove)
var writeIndex: Int = 0
for (readIndex in 0..lastIndex) {
val element = this[readIndex]
...
if (writeIndex != readIndex)
this[writeIndex] = element
...
}
It seems CopyOnWriteArrayList.removeAll
does not copy on write but mutates in place insteadgian
07/17/2020, 3:03 PMCopyOnWriteArrayList::set
copying the array before mutation?diego-gomez-olvera
07/21/2020, 7:49 AMdiego-gomez-olvera
07/21/2020, 7:49 AMjava.lang.ArrayIndexOutOfBoundsException: length=0; index=0
at java.util.concurrent.CopyOnWriteArrayList.get(CopyOnWriteArrayList.java:385)
at java.util.concurrent.CopyOnWriteArrayList.get(CopyOnWriteArrayList.java:398)
at kotlin.collections.CollectionsKt__MutableCollectionsKt.filterInPlace$CollectionsKt__MutableCollectionsKt(MutableCollections.kt:190)
at kotlin.collections.CollectionsKt__MutableCollectionsKt.removeAll(MutableCollections.kt:177)
diego-gomez-olvera
07/21/2020, 7:52 AMlastIndex
should be recalculated