From 1d18389e32e8afdbf5caa89808fb5af308fb5c8d Mon Sep 17 00:00:00 2001 From: Matthew Pope Date: Wed, 20 Mar 2024 14:17:57 -0700 Subject: [PATCH] Adds an empty input check to all addAll, removeAll, and retainAll methods --- .../implementations/immutableList/AbstractPersistentList.kt | 6 ++++++ .../immutableList/PersistentVectorBuilder.kt | 1 + .../src/implementations/immutableMap/PersistentHashMap.kt | 1 + .../immutableMap/PersistentHashMapBuilder.kt | 1 + .../src/implementations/immutableSet/PersistentHashSet.kt | 3 +++ .../immutableSet/PersistentHashSetBuilder.kt | 2 ++ .../persistentOrderedMap/PersistentOrderedMap.kt | 1 + .../persistentOrderedSet/PersistentOrderedSet.kt | 3 +++ 8 files changed, 18 insertions(+) diff --git a/core/commonMain/src/implementations/immutableList/AbstractPersistentList.kt b/core/commonMain/src/implementations/immutableList/AbstractPersistentList.kt index e018fe4e..525e59b5 100644 --- a/core/commonMain/src/implementations/immutableList/AbstractPersistentList.kt +++ b/core/commonMain/src/implementations/immutableList/AbstractPersistentList.kt @@ -8,6 +8,7 @@ package kotlinx.collections.immutable.implementations.immutableList import kotlinx.collections.immutable.ImmutableList import kotlinx.collections.immutable.PersistentList import kotlinx.collections.immutable.mutate +import kotlinx.collections.immutable.internal.ListImplementation.checkPositionIndex public abstract class AbstractPersistentList : PersistentList, AbstractList() { override fun subList(fromIndex: Int, toIndex: Int): ImmutableList { @@ -15,10 +16,13 @@ public abstract class AbstractPersistentList : PersistentList, AbstractLis } override fun addAll(elements: Collection): PersistentList { + if (elements.isEmpty()) return this return mutate { it.addAll(elements) } } override fun addAll(index: Int, c: Collection): PersistentList { + checkPositionIndex(index, size) + if (c.isEmpty()) return this return mutate { it.addAll(index, c) } } @@ -31,10 +35,12 @@ public abstract class AbstractPersistentList : PersistentList, AbstractLis } override fun removeAll(elements: Collection): PersistentList { + if (elements.isEmpty()) return this return removeAll { elements.contains(it) } } override fun retainAll(elements: Collection): PersistentList { + if (elements.isEmpty()) return persistentVectorOf() return removeAll { !elements.contains(it) } } diff --git a/core/commonMain/src/implementations/immutableList/PersistentVectorBuilder.kt b/core/commonMain/src/implementations/immutableList/PersistentVectorBuilder.kt index 6d788ab5..f4f56f71 100644 --- a/core/commonMain/src/implementations/immutableList/PersistentVectorBuilder.kt +++ b/core/commonMain/src/implementations/immutableList/PersistentVectorBuilder.kt @@ -669,6 +669,7 @@ internal class PersistentVectorBuilder(private var vector: PersistentList, } override fun removeAll(elements: Collection): Boolean { + if (elements.isEmpty()) return false return removeAllWithPredicate { elements.contains(it) } } diff --git a/core/commonMain/src/implementations/immutableMap/PersistentHashMap.kt b/core/commonMain/src/implementations/immutableMap/PersistentHashMap.kt index 332e5e40..e798d483 100644 --- a/core/commonMain/src/implementations/immutableMap/PersistentHashMap.kt +++ b/core/commonMain/src/implementations/immutableMap/PersistentHashMap.kt @@ -68,6 +68,7 @@ internal class PersistentHashMap(internal val node: TrieNode, } override fun putAll(m: Map): PersistentMap { + if (m.isEmpty()) return this return this.mutate { it.putAll(m) } } diff --git a/core/commonMain/src/implementations/immutableMap/PersistentHashMapBuilder.kt b/core/commonMain/src/implementations/immutableMap/PersistentHashMapBuilder.kt index 983a319f..eac1cac9 100644 --- a/core/commonMain/src/implementations/immutableMap/PersistentHashMapBuilder.kt +++ b/core/commonMain/src/implementations/immutableMap/PersistentHashMapBuilder.kt @@ -66,6 +66,7 @@ internal class PersistentHashMapBuilder(private var map: PersistentHashMap } override fun putAll(from: Map) { + if (from.isEmpty()) return val map = from as? PersistentHashMap ?: (from as? PersistentHashMapBuilder)?.build() if (map != null) @Suppress("UNCHECKED_CAST") { val intersectionCounter = DeltaCounter() diff --git a/core/commonMain/src/implementations/immutableSet/PersistentHashSet.kt b/core/commonMain/src/implementations/immutableSet/PersistentHashSet.kt index 8686b1ee..7ac2a461 100644 --- a/core/commonMain/src/implementations/immutableSet/PersistentHashSet.kt +++ b/core/commonMain/src/implementations/immutableSet/PersistentHashSet.kt @@ -21,6 +21,7 @@ internal class PersistentHashSet(internal val node: TrieNode, } override fun addAll(elements: Collection): PersistentSet { + if (elements.isEmpty()) return this return this.mutate { it.addAll(elements) } } @@ -31,6 +32,7 @@ internal class PersistentHashSet(internal val node: TrieNode, } override fun removeAll(elements: Collection): PersistentSet { + if (elements.isEmpty()) return this return mutate { it.removeAll(elements) } } @@ -39,6 +41,7 @@ internal class PersistentHashSet(internal val node: TrieNode, } override fun retainAll(elements: Collection): PersistentSet { + if (elements.isEmpty()) return PersistentHashSet.emptyOf() return mutate { it.retainAll(elements) } } diff --git a/core/commonMain/src/implementations/immutableSet/PersistentHashSetBuilder.kt b/core/commonMain/src/implementations/immutableSet/PersistentHashSetBuilder.kt index a9bce437..a5899156 100644 --- a/core/commonMain/src/implementations/immutableSet/PersistentHashSetBuilder.kt +++ b/core/commonMain/src/implementations/immutableSet/PersistentHashSetBuilder.kt @@ -45,6 +45,7 @@ internal class PersistentHashSetBuilder(private var set: PersistentHashSet } override fun addAll(elements: Collection): Boolean { + if (elements.isEmpty()) return false val set = elements as? PersistentHashSet ?: (elements as? PersistentHashSetBuilder)?.build() if (set !== null) { val deltaCounter = DeltaCounter() @@ -81,6 +82,7 @@ internal class PersistentHashSetBuilder(private var set: PersistentHashSet } override fun removeAll(elements: Collection): Boolean { + if (elements.isEmpty()) return false val set = elements as? PersistentHashSet ?: (elements as? PersistentHashSetBuilder)?.build() if (set !== null) { val counter = DeltaCounter() diff --git a/core/commonMain/src/implementations/persistentOrderedMap/PersistentOrderedMap.kt b/core/commonMain/src/implementations/persistentOrderedMap/PersistentOrderedMap.kt index 94d25929..5f7df084 100644 --- a/core/commonMain/src/implementations/persistentOrderedMap/PersistentOrderedMap.kt +++ b/core/commonMain/src/implementations/persistentOrderedMap/PersistentOrderedMap.kt @@ -117,6 +117,7 @@ internal class PersistentOrderedMap( } override fun putAll(m: Map): PersistentMap { + if (m.isEmpty()) return this return this.mutate { it.putAll(m) } } diff --git a/core/commonMain/src/implementations/persistentOrderedSet/PersistentOrderedSet.kt b/core/commonMain/src/implementations/persistentOrderedSet/PersistentOrderedSet.kt index a1f5dc1a..54bc6e72 100644 --- a/core/commonMain/src/implementations/persistentOrderedSet/PersistentOrderedSet.kt +++ b/core/commonMain/src/implementations/persistentOrderedSet/PersistentOrderedSet.kt @@ -53,6 +53,7 @@ internal class PersistentOrderedSet( } override fun addAll(elements: Collection): PersistentSet { + if (elements.isEmpty()) return this return this.mutate { it.addAll(elements) } } @@ -78,6 +79,7 @@ internal class PersistentOrderedSet( } override fun removeAll(elements: Collection): PersistentSet { + if (elements.isEmpty()) return this return mutate { it.removeAll(elements) } } @@ -86,6 +88,7 @@ internal class PersistentOrderedSet( } override fun retainAll(elements: Collection): PersistentSet { + if (elements.isEmpty()) return PersistentOrderedSet.emptyOf() return mutate { it.retainAll(elements) } }