-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve performance and behavior of ListMap and ListSet
Makes the immutable `ListMap` and `ListSet` collections more alike one another, both in their semantics and in their performance. In terms of semantics, makes the `ListMap` iterator return the elements in reverse order, as `ListSet` already does (improving its performance as a side-effect). While, as mentioned in SI-8985, `ListMap` and `ListSet` doesn't seem to make any guarantees in terms of iteration order, I believe users expect `ListSet` and `ListMap` to behave in the same way, particularly when they are implemented in the exact same way. In terms of performance, `ListMap` is given a custom builder that avoids creation in O(N^2) time using a strategy similar to the one already applied in `ListSet`. `ListSet`'s element removal method was not tail-recursive as the `ListMap` one, so a tail-recursive implementation was added.
- Loading branch information
1 parent
4c4c5e6
commit bb05a4d
Showing
4 changed files
with
81 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package scala.collection.immutable | ||
|
||
import org.junit.Assert._ | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.junit.runners.JUnit4 | ||
|
||
@RunWith(classOf[JUnit4]) | ||
class ListMapTest { | ||
|
||
@Test | ||
def hasCorrectBuilder(): Unit = { | ||
val m = ListMap[String, String]("a" -> "1", "b" -> "2", "c" -> "3", "b" -> "2.2", "d" -> "4") | ||
assertEquals(List("d" -> "4", "b" -> "2.2", "c" -> "3", "a" -> "1"), m.toList) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package scala.collection.immutable | ||
|
||
import org.junit.Assert._ | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.junit.runners.JUnit4 | ||
|
||
@RunWith(classOf[JUnit4]) | ||
class ListSetTest { | ||
|
||
@Test | ||
def hasTailRecursiveDelete(): Unit = { | ||
val s = ListSet[Int](1 to 50000: _*) | ||
try s - 25000 catch { case e: StackOverflowError => fail("A stack overflow occurred") } | ||
} | ||
} |