-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve performance and behavior of ListMap and ListSet #5103
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
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 `ListSet` iterator return the elements in their insertion order, as `ListMap` already does. 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, `ListSet` has a custom builder that avoids creation in O(N^2) time. However, this significantly reduces its performance in the creation of small sets, as its requires the instantiation and usage of an auxilliary HashSet. As `ListMap` and `ListSet` are only suitable for small sizes do to their performance characteristics, the builder is removed, the default `SetBuilder` being used instead.
- Loading branch information
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package scala.collection.immutable | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add copyright header. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most, if not all, JUnit tests lack a copyright header. I already wrote several tests for this repository before and the copyright never was a requirement... Are you sure it's mandatory? @Ichoran, can you tell what is the "official" policy in these cases? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirming this was an assumption I had made. Please wait for the official policy. |
||
|
||
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 t7445(): Unit = { | ||
val m = ListMap(1 -> 1, 2 -> 2, 3 -> 3, 4 -> 4, 5 -> 5) | ||
assertEquals(ListMap(2 -> 2, 3 -> 3, 4 -> 4, 5 -> 5), m.tail) | ||
} | ||
|
||
@Test | ||
def hasCorrectBuilder(): Unit = { | ||
val m = ListMap("a" -> "1", "b" -> "2", "c" -> "3", "b" -> "2.2", "d" -> "4") | ||
assertEquals(List("a" -> "1", "c" -> "3", "b" -> "2.2", "d" -> "4"), m.toList) | ||
} | ||
|
||
@Test | ||
def hasCorrectHeadTailLastInit(): Unit = { | ||
val m = ListMap(1 -> 1, 2 -> 2, 3 -> 3) | ||
assertEquals(1 -> 1, m.head) | ||
assertEquals(ListMap(2 -> 2, 3 -> 3), m.tail) | ||
assertEquals(3 -> 3, m.last) | ||
assertEquals(ListMap(1 -> 1, 2 -> 2), m.init) | ||
} | ||
|
||
@Test | ||
def hasCorrectAddRemove(): Unit = { | ||
val m = ListMap(1 -> 1, 2 -> 2, 3 -> 3) | ||
assertEquals(ListMap(1 -> 1, 2 -> 2, 3 -> 3, 4 -> 4), m + (4 -> 4)) | ||
assertEquals(ListMap(1 -> 1, 3 -> 3, 2 -> 4), m + (2 -> 4)) | ||
assertEquals(ListMap(1 -> 1, 2 -> 2, 3 -> 3), m + (2 -> 2)) | ||
assertEquals(ListMap(2 -> 2, 3 -> 3), m - 1) | ||
assertEquals(ListMap(1 -> 1, 3 -> 3), m - 2) | ||
assertEquals(ListMap(1 -> 1, 2 -> 2, 3 -> 3), m - 4) | ||
} | ||
|
||
@Test | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could squash this commit into the previous one |
||
def hasCorrectIterator(): Unit = { | ||
val m = ListMap(1 -> 1, 2 -> 2, 3 -> 3, 5 -> 5, 4 -> 4) | ||
assertEquals(List(1 -> 1, 2 -> 2, 3 -> 3, 5 -> 5, 4 -> 4), m.iterator.toList) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package scala.collection.immutable | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add copyright header. |
||
|
||
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 t7445(): Unit = { | ||
val s = ListSet(1, 2, 3, 4, 5) | ||
assertEquals(ListSet(2, 3, 4, 5), s.tail) | ||
} | ||
|
||
@Test | ||
def hasCorrectBuilder(): Unit = { | ||
val m = ListSet("a", "b", "c", "b", "d") | ||
assertEquals(List("a", "b", "c", "d"), m.toList) | ||
} | ||
|
||
@Test | ||
def hasTailRecursiveDelete(): Unit = { | ||
val s = ListSet(1 to 50000: _*) | ||
try s - 25000 catch { case e: StackOverflowError => fail("A stack overflow occurred") } | ||
} | ||
|
||
@Test | ||
def hasCorrectHeadTailLastInit(): Unit = { | ||
val m = ListSet(1, 2, 3) | ||
assertEquals(1, m.head) | ||
assertEquals(ListSet(2, 3), m.tail) | ||
assertEquals(3, m.last) | ||
assertEquals(ListSet(1, 2), m.init) | ||
} | ||
|
||
@Test | ||
def hasCorrectAddRemove(): Unit = { | ||
val m = ListSet(1, 2, 3) | ||
assertEquals(ListSet(1, 2, 3, 4), m + 4) | ||
assertEquals(ListSet(1, 2, 3), m + 2) | ||
assertEquals(ListSet(2, 3), m - 1) | ||
assertEquals(ListSet(1, 3), m - 2) | ||
assertEquals(ListSet(1, 2, 3), m - 4) | ||
} | ||
|
||
@Test | ||
def hasCorrectIterator(): Unit = { | ||
val s = ListSet(1, 2, 3, 5, 4) | ||
assertEquals(List(1, 2, 3, 5, 4), s.iterator.toList) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you mention in the commit message that this commit also fixes SI-7445 for ListSet?