Created
May 8, 2018 10:26
-
-
Save missingfaktor/03d6d620f669c45159d867f84059fd40 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import scala.collection.generic.CanBuildFrom | |
| import scala.collection.mutable.ArrayBuffer | |
| import scala.collection.{IndexedSeqLike, mutable} | |
| /** | |
| * An ordered collection type that ensures it always contains unique elements. | |
| */ | |
| final class UniqueSeq[+A] private(_input: IndexedSeq[A]) | |
| extends IndexedSeq[A] | |
| with IndexedSeqLike[A, UniqueSeq[A]] { | |
| val underlying: IndexedSeq[A] = _input.distinct | |
| def length: Int = underlying.length | |
| def apply(index: Int): A = underlying(index) | |
| override protected[this] def newBuilder: mutable.Builder[A, UniqueSeq[A]] = { | |
| UniqueSeq.newBuilder | |
| } | |
| } | |
| object UniqueSeq { | |
| val empty: UniqueSeq[Nothing] = { | |
| new UniqueSeq(IndexedSeq.empty) | |
| } | |
| def apply[A](elements: A*): UniqueSeq[A] = { | |
| new UniqueSeq[A](elements.toIndexedSeq) | |
| } | |
| def newBuilder[A]: mutable.Builder[A, UniqueSeq[A]] = { | |
| new ArrayBuffer[A].mapResult(new UniqueSeq[A](_)) | |
| } | |
| implicit def canBuildFrom[A]: CanBuildFrom[UniqueSeq[_], A, UniqueSeq[A]] = { | |
| new CanBuildFrom[UniqueSeq[_], A, UniqueSeq[A]] { | |
| override def apply(from: UniqueSeq[_]): mutable.Builder[A, UniqueSeq[A]] = newBuilder[A] | |
| override def apply(): mutable.Builder[A, UniqueSeq[A]] = newBuilder[A] | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment