stdlib: Improve ArrayList.removeAll implementation.

This patch replaces ArrayList.removeAll implementation with a
faster one. It also adds the AbstractMutableCollection class
containing default implementations for addAll, removeAll and
retainAll methods and fixes tests to check if removeAll removes
all occurrences found or not.
This commit is contained in:
Ilya Matveev
2017-04-14 12:50:22 +07:00
committed by ilmat192
parent 69a49f987c
commit 321386d95e
7 changed files with 139 additions and 46 deletions
+5
View File
@@ -790,6 +790,11 @@ task lateinit_notInitialized(type: RunKonanTest) {
source = "codegen/lateinit/notInitialized.kt"
}
task AbstractMutableCollectionRemoveAll(type: RunKonanTest) {
expectedExitStatus = 0
source = "runtime/collections/AbstractMutableCollectionRemoveAll.kt"
}
task array0(type: RunKonanTest) {
goldValue = "5\n6\n7\n8\n9\n10\n11\n12\n13\n"
source = "runtime/collections/array0.kt"
@@ -0,0 +1,70 @@
class TestCollection(): AbstractMutableCollection<Int>() {
companion object {
const val SIZE = 7
}
private val array = IntArray(SIZE)
private var len = 0
override val size: Int
get() = len
override fun add(element: Int): Boolean {
if (len >= SIZE) return false
array[len++] = element
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<Int> = object: MutableIterator<Int> {
var nextIndex = 0
override fun hasNext() = nextIndex < len
override fun next() = array[nextIndex++]
override fun remove() {
if (nextIndex == 0) throw IllegalStateException()
for (i in nextIndex..len - 1) {
array[i - 1] = array[i]
}
len--
nextIndex--
}
}
override fun clear() {
len = 0
}
}
fun assertEquals(a: TestCollection, b: List<Int>) {
if (a.size != b.size) {
throw AssertionError()
}
val aIt = a.iterator()
val bIt = b.iterator()
while (aIt.hasNext()) {
if (aIt.next() != bIt.next()) throw AssertionError("TestCollection contains wrong elements. Expected: $b, actual: $a.")
}
}
fun main(args: Array<String>) {
val c = TestCollection()
if (!c.addAll(listOf(1, 2, 3, 2, 4, 5, 4))) throw AssertionError("addAll is false when it must be true.")
if (c.addAll(listOf(1, 2)) != false) throw AssertionError("addAll is true when it must be false.")
c.removeAll(listOf(1, 2))
assertEquals(c, listOf(3, 4, 5, 4))
c.retainAll(listOf(4, 5))
assertEquals(c, listOf(4, 5, 4))
}
@@ -85,9 +85,9 @@ fun testRemove() {
}
fun testRemoveAll() {
val a = ArrayList(listOf("1", "2", "3", "4", "5"))
val a = ArrayList(listOf("1", "2", "3", "4", "5", "1"))
assertFalse(a.removeAll(listOf("6", "7", "8")))
assertEquals(listOf("1", "2", "3", "4", "5"), a)
assertEquals(listOf("1", "2", "3", "4", "5", "1"), a)
assertTrue(a.removeAll(listOf("5", "3", "1")))
assertEquals(listOf("2", "4"), a)
}
@@ -277,9 +277,9 @@ fun testSubListSubListRemoveAt() {
}
fun testSubListRemoveAll() {
val a = ArrayList(listOf("1", "2", "3", "4", "5"))
val s = a.subList(1, 4)
assertEquals(listOf("2", "3", "4"), s)
val a = ArrayList(listOf("1", "2", "3", "3", "4", "5"))
val s = a.subList(1, 5)
assertEquals(listOf("2", "3", "3", "4"), s)
assertTrue(s.removeAll(listOf("3", "5")))
assertEquals(listOf("2", "4"), s)
@@ -103,9 +103,9 @@ fun testContainsAll() {
}
fun testRemoveAll() {
val s = HashSet(listOf("1", "2", "3", "4", "5"))
val s = HashSet(listOf("1", "2", "3", "4", "5", "1"))
assertFalse(s.removeAll(listOf("6", "7", "8")))
assertEquals(setOf("1", "2", "3", "4", "5"), s)
assertEquals(setOf("1", "2", "3", "4", "5", "1"), s)
assertTrue(s.removeAll(listOf("5", "3", "1")))
assertEquals(setOf("2", "4"), s)
}