From 26c227ab3ef8bab3c078ffe1ed5c38d9f1df00fd Mon Sep 17 00:00:00 2001 From: Ilya Matveev Date: Thu, 13 Apr 2017 16:52:18 +0700 Subject: [PATCH] stdlib: Remove all occurrences in Collection.removeAll method --- .../src/main/kotlin/kotlin/collections/ArrayList.kt | 7 +++++-- .../src/main/kotlin/kotlin/collections/HashMap.kt | 12 ++++++++---- .../src/main/kotlin/kotlin/collections/HashSet.kt | 6 ++++-- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/runtime/src/main/kotlin/kotlin/collections/ArrayList.kt b/runtime/src/main/kotlin/kotlin/collections/ArrayList.kt index d5ce146864b..541cfb88e7c 100644 --- a/runtime/src/main/kotlin/kotlin/collections/ArrayList.kt +++ b/runtime/src/main/kotlin/kotlin/collections/ArrayList.kt @@ -132,9 +132,12 @@ class ArrayList private constructor( override fun removeAll(elements: Collection): Boolean { var changed = false - val it = elements.iterator() + val it = iterator() while (it.hasNext()) { - if (remove(it.next())) changed = true + if (elements.contains(it.next())) { + it.remove() + changed = true + } } return changed } diff --git a/runtime/src/main/kotlin/kotlin/collections/HashMap.kt b/runtime/src/main/kotlin/kotlin/collections/HashMap.kt index 50512327ea5..9669bb6e0fb 100644 --- a/runtime/src/main/kotlin/kotlin/collections/HashMap.kt +++ b/runtime/src/main/kotlin/kotlin/collections/HashMap.kt @@ -422,11 +422,13 @@ class HashMap private constructor( internal fun removeAllEntries(elements: Collection>): Boolean { if (elements.isEmpty()) return false - val it = elements.iterator() + val it = entriesIterator() var updated = false while (it.hasNext()) { - if (removeEntry(it.next())) + if (elements.contains(it.next())) { + it.remove() updated = true + } } return updated } @@ -460,11 +462,13 @@ class HashMap private constructor( } internal fun removeAllValues(elements: Collection): Boolean { - val it = elements.iterator() + val it = valuesIterator() var updated = false while (it.hasNext()) { - if (removeValue(it.next())) + if (elements.contains(it.next())) { + it.remove() updated = true + } } return updated } diff --git a/runtime/src/main/kotlin/kotlin/collections/HashSet.kt b/runtime/src/main/kotlin/kotlin/collections/HashSet.kt index 05c728554e6..5bec9cde9e4 100644 --- a/runtime/src/main/kotlin/kotlin/collections/HashSet.kt +++ b/runtime/src/main/kotlin/kotlin/collections/HashSet.kt @@ -56,11 +56,13 @@ class HashSet internal constructor( } override fun removeAll(elements: Collection): Boolean { - val it = elements.iterator() + val it = iterator() var updated = false while (it.hasNext()) { - if (remove(it.next())) + if (elements.contains(it.next())) { + it.remove() updated = true + } } return updated }