Skip to content

Commit

Permalink
Adds an empty input check to all addAll, removeAll, and retainAll met…
Browse files Browse the repository at this point in the history
…hods
  • Loading branch information
popematt authored and qurbonzoda committed Mar 20, 2024
1 parent a1d250a commit 1d18389
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@ 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<E> : PersistentList<E>, AbstractList<E>() {
override fun subList(fromIndex: Int, toIndex: Int): ImmutableList<E> {
return super<PersistentList>.subList(fromIndex, toIndex)
}

override fun addAll(elements: Collection<E>): PersistentList<E> {
if (elements.isEmpty()) return this
return mutate { it.addAll(elements) }
}

override fun addAll(index: Int, c: Collection<E>): PersistentList<E> {
checkPositionIndex(index, size)
if (c.isEmpty()) return this
return mutate { it.addAll(index, c) }
}

Expand All @@ -31,10 +35,12 @@ public abstract class AbstractPersistentList<E> : PersistentList<E>, AbstractLis
}

override fun removeAll(elements: Collection<E>): PersistentList<E> {
if (elements.isEmpty()) return this
return removeAll { elements.contains(it) }
}

override fun retainAll(elements: Collection<E>): PersistentList<E> {
if (elements.isEmpty()) return persistentVectorOf()
return removeAll { !elements.contains(it) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@ internal class PersistentVectorBuilder<E>(private var vector: PersistentList<E>,
}

override fun removeAll(elements: Collection<E>): Boolean {
if (elements.isEmpty()) return false
return removeAllWithPredicate { elements.contains(it) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ internal class PersistentHashMap<K, V>(internal val node: TrieNode<K, V>,
}

override fun putAll(m: Map<out K, @UnsafeVariance V>): PersistentMap<K, V> {
if (m.isEmpty()) return this
return this.mutate { it.putAll(m) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ internal class PersistentHashMapBuilder<K, V>(private var map: PersistentHashMap
}

override fun putAll(from: Map<out K, V>) {
if (from.isEmpty()) return
val map = from as? PersistentHashMap ?: (from as? PersistentHashMapBuilder)?.build()
if (map != null) @Suppress("UNCHECKED_CAST") {
val intersectionCounter = DeltaCounter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ internal class PersistentHashSet<E>(internal val node: TrieNode<E>,
}

override fun addAll(elements: Collection<E>): PersistentSet<E> {
if (elements.isEmpty()) return this
return this.mutate { it.addAll(elements) }
}

Expand All @@ -31,6 +32,7 @@ internal class PersistentHashSet<E>(internal val node: TrieNode<E>,
}

override fun removeAll(elements: Collection<E>): PersistentSet<E> {
if (elements.isEmpty()) return this
return mutate { it.removeAll(elements) }
}

Expand All @@ -39,6 +41,7 @@ internal class PersistentHashSet<E>(internal val node: TrieNode<E>,
}

override fun retainAll(elements: Collection<E>): PersistentSet<E> {
if (elements.isEmpty()) return PersistentHashSet.emptyOf<E>()
return mutate { it.retainAll(elements) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ internal class PersistentHashSetBuilder<E>(private var set: PersistentHashSet<E>
}

override fun addAll(elements: Collection<E>): Boolean {
if (elements.isEmpty()) return false
val set = elements as? PersistentHashSet ?: (elements as? PersistentHashSetBuilder)?.build()
if (set !== null) {
val deltaCounter = DeltaCounter()
Expand Down Expand Up @@ -81,6 +82,7 @@ internal class PersistentHashSetBuilder<E>(private var set: PersistentHashSet<E>
}

override fun removeAll(elements: Collection<E>): Boolean {
if (elements.isEmpty()) return false
val set = elements as? PersistentHashSet ?: (elements as? PersistentHashSetBuilder)?.build()
if (set !== null) {
val counter = DeltaCounter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ internal class PersistentOrderedMap<K, V>(
}

override fun putAll(m: Map<out K, @UnsafeVariance V>): PersistentMap<K, V> {
if (m.isEmpty()) return this
return this.mutate { it.putAll(m) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ internal class PersistentOrderedSet<E>(
}

override fun addAll(elements: Collection<E>): PersistentSet<E> {
if (elements.isEmpty()) return this
return this.mutate { it.addAll(elements) }
}

Expand All @@ -78,6 +79,7 @@ internal class PersistentOrderedSet<E>(
}

override fun removeAll(elements: Collection<E>): PersistentSet<E> {
if (elements.isEmpty()) return this
return mutate { it.removeAll(elements) }
}

Expand All @@ -86,6 +88,7 @@ internal class PersistentOrderedSet<E>(
}

override fun retainAll(elements: Collection<E>): PersistentSet<E> {
if (elements.isEmpty()) return PersistentOrderedSet.emptyOf<E>()
return mutate { it.retainAll(elements) }
}

Expand Down

0 comments on commit 1d18389

Please sign in to comment.