Skip to content
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

Fast InsertionOrderSet #2308

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 4 additions & 5 deletions jvm/core/src/main/scala/org/scalatest/InsertionOrderSet.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2001-2016 Artima, Inc.
* Copyright 2001-2024 Artima, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,9 +15,8 @@
*/
package org.scalatest

import org.scalactic.ColCompatHelper

private[scalatest] object InsertionOrderSet {
def apply[A](elements: List[A]): Set[A] =
scala.collection.immutable.SortedSet(elements: _*)(new Ordering[A] {
def compare(x: A, y: A): Int = elements.indexOf(x) compare elements.indexOf(y)
})
def apply[A](elements: List[A]): Set[A] = new ColCompatHelper.InsertionOrderSet[A](elements)
}
24 changes: 24 additions & 0 deletions project/GenColCompatHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ object GenColCompatHelper {
| def newBuilder[A, C](f: Factory[A, C]): scala.collection.mutable.Builder[A, C] = f.newBuilder
|
| type StringOps = scala.collection.StringOps
|
| class InsertionOrderSet[A](elements: List[A]) extends scala.collection.immutable.Set[A] {
| private val underlying = scala.collection.mutable.LinkedHashSet(elements: _*)
| def contains(elem: A): Boolean = underlying.contains(elem)
| def iterator: Iterator[A] = underlying.iterator
| def excl(elem: A): scala.collection.immutable.Set[A] = new InsertionOrderSet(elements.filter(_ != elem))
| def incl(elem: A): scala.collection.immutable.Set[A] =
| if (underlying.contains(elem))
| this
| else
| new InsertionOrderSet(elements :+ elem)
| }
|}
|
""".stripMargin
Expand Down Expand Up @@ -129,6 +141,18 @@ object GenColCompatHelper {
| def newBuilder[A, C](f: Factory[A, C]): scala.collection.mutable.Builder[A, C] = f.apply()
|
| type StringOps = scala.collection.immutable.StringOps
|
| class InsertionOrderSet[A](elements: List[A]) extends scala.collection.immutable.Set[A] {
| private val underlying = scala.collection.mutable.LinkedHashSet(elements: _*)
| def contains(elem: A): Boolean = underlying.contains(elem)
| def iterator: Iterator[A] = underlying.iterator
| def -(elem: A): scala.collection.immutable.Set[A] = new InsertionOrderSet(elements.filter(_ != elem))
| def +(elem: A): scala.collection.immutable.Set[A] =
| if (underlying.contains(elem))
| this
| else
| new InsertionOrderSet(elements :+ elem)
| }
|}
|
""".stripMargin
Expand Down