From 7bf9669f57a20780194c915d2ecaa02fe736de29 Mon Sep 17 00:00:00 2001 From: Ilya Matveev Date: Tue, 18 Apr 2017 11:11:30 +0700 Subject: [PATCH] stdlib: Add clear and remove methods in AbstractMutableCollection --- backend.native/tests/build.gradle | 4 +- .../CollectionTest/sortByInPlace.kt | 4 +- ...oveAll.kt => AbstractMutableCollection.kt} | 15 ++------ .../kotlin/collections/AbstractCollection.kt | 38 ++++++++++++------- 4 files changed, 32 insertions(+), 29 deletions(-) rename backend.native/tests/runtime/collections/{AbstractMutableCollectionRemoveAll.kt => AbstractMutableCollection.kt} (86%) diff --git a/backend.native/tests/build.gradle b/backend.native/tests/build.gradle index be1e640871a..be293a924bd 100644 --- a/backend.native/tests/build.gradle +++ b/backend.native/tests/build.gradle @@ -790,9 +790,9 @@ task lateinit_notInitialized(type: RunKonanTest) { source = "codegen/lateinit/notInitialized.kt" } -task AbstractMutableCollectionRemoveAll(type: RunKonanTest) { +task AbstractMutableCollection(type: RunKonanTest) { expectedExitStatus = 0 - source = "runtime/collections/AbstractMutableCollectionRemoveAll.kt" + source = "runtime/collections/AbstractMutableCollection.kt" } task array0(type: RunKonanTest) { diff --git a/backend.native/tests/external/stdlib/collections/CollectionTest/sortByInPlace.kt b/backend.native/tests/external/stdlib/collections/CollectionTest/sortByInPlace.kt index d0b473256a1..b07d759a29f 100644 --- a/backend.native/tests/external/stdlib/collections/CollectionTest/sortByInPlace.kt +++ b/backend.native/tests/external/stdlib/collections/CollectionTest/sortByInPlace.kt @@ -8,7 +8,7 @@ inline fun > assertSorted(list: List, message: Strin } val it = list.iterator() var prev = selector(it.next()) - while(it.hasNext()) { + while (it.hasNext()) { val cur = selector(it.next()) assertTrue(prev.compareTo(cur) <= 0, message) prev = cur @@ -21,7 +21,7 @@ inline fun > assertSortedDescending(list: List, mess } val it = list.iterator() var prev = selector(it.next()) - while(it.hasNext()) { + while (it.hasNext()) { val cur = selector(it.next()) assertTrue(prev.compareTo(cur) >= 0, message) prev = cur diff --git a/backend.native/tests/runtime/collections/AbstractMutableCollectionRemoveAll.kt b/backend.native/tests/runtime/collections/AbstractMutableCollection.kt similarity index 86% rename from backend.native/tests/runtime/collections/AbstractMutableCollectionRemoveAll.kt rename to backend.native/tests/runtime/collections/AbstractMutableCollection.kt index c91a7353076..dcd22160883 100644 --- a/backend.native/tests/runtime/collections/AbstractMutableCollectionRemoveAll.kt +++ b/backend.native/tests/runtime/collections/AbstractMutableCollection.kt @@ -15,17 +15,6 @@ class TestCollection(): AbstractMutableCollection() { return true } - override fun remove(element: Int): Boolean { - val it = iterator() - while(it.hasNext()) { - if (it.next() == element) { - it.remove() - return true - } - } - return false - } - override fun iterator(): MutableIterator = object: MutableIterator { var nextIndex = 0 @@ -67,4 +56,8 @@ fun main(args: Array) { assertEquals(c, listOf(3, 4, 5, 4)) c.retainAll(listOf(4, 5)) assertEquals(c, listOf(4, 5, 4)) + c.remove(4) + assertEquals(c, listOf(5, 4)) + c.clear() + assertEquals(c, listOf()) } \ No newline at end of file diff --git a/runtime/src/main/kotlin/kotlin/collections/AbstractCollection.kt b/runtime/src/main/kotlin/kotlin/collections/AbstractCollection.kt index 60e2392079c..e185bdd0991 100644 --- a/runtime/src/main/kotlin/kotlin/collections/AbstractCollection.kt +++ b/runtime/src/main/kotlin/kotlin/collections/AbstractCollection.kt @@ -61,40 +61,50 @@ public abstract class AbstractMutableCollection protected constructor(): Muta override public fun addAll(elements: Collection): Boolean { var changed = false for (v in elements) { - if(add(v)) changed = true + if (add(v)) changed = true } return changed } + /** + * Removes a single instance of the specified element from this + * collection, if it is present. + * + * @return `true` if the element has been successfully removed; `false` if it was not present in the collection. + */ + override fun remove(element: E): Boolean { + val it = iterator() + while (it.hasNext()) { + if (it.next() == element) { + it.remove() + return true + } + } + return false + } + /** * Removes all of this collection's elements that are also contained in the specified collection. * * @return `true` if any of the specified elements was removed from the collection, `false` if the collection was not modified. */ - override public fun removeAll(elements: Collection): Boolean = removeAll(elements, true) + override public fun removeAll(elements: Collection): Boolean = (this as MutableIterable).removeAll { it in elements } /** * Retains only the elements in this collection that are contained in the specified collection. * * @return `true` if any element was removed from the collection, `false` if the collection was not modified. */ - override public fun retainAll(elements: Collection): Boolean = removeAll(elements, false) + override public fun retainAll(elements: Collection): Boolean = (this as MutableIterable).retainAll { it in elements } /** - * Removes the elements in this collection that are contained (if [contained] == true) or - * not contained (if [contained] == false) in [elements]. - * - * @return `true` if the collection has been modified and `false` otherwise. + * Removes all elements from this collection. */ - protected fun removeAll(elements: Collection, contained: Boolean): Boolean { + override fun clear(): Unit { val it = iterator() - var changed = false while (it.hasNext()) { - if (elements.contains(it.next()) == contained) { - it.remove() - changed = true - } + it.next() + it.remove() } - return changed } }