|
1 | 1 | package strawman.collection
|
2 | 2 |
|
3 |
| -import scala.{Any, Boolean, Int, IndexOutOfBoundsException} |
| 3 | +import scala.{Any, Boolean, Equals, IndexOutOfBoundsException, Int} |
4 | 4 | import strawman.collection.immutable.{List, Nil}
|
5 | 5 |
|
6 | 6 | import scala.annotation.unchecked.uncheckedVariance
|
| 7 | +import scala.util.hashing.MurmurHash3 |
7 | 8 |
|
8 | 9 | /** Base trait for sequence collections */
|
9 | 10 | trait Seq[+A] extends Iterable[A] with SeqLike[A, Seq] with ArrayLike[A]
|
@@ -51,6 +52,50 @@ trait IndexedSeq[+A] extends Seq[A] { self =>
|
51 | 52 | trait SeqLike[+A, +C[X] <: Seq[X]]
|
52 | 53 | extends IterableLike[A, C]
|
53 | 54 | with SeqMonoTransforms[A, C[A @uncheckedVariance]] // sound bcs of VarianceNote
|
| 55 | + with Equals { |
| 56 | + |
| 57 | + protected def coll: C[A @uncheckedVariance] |
| 58 | + |
| 59 | + /** Do the elements of this collection are the same (and in the same order) |
| 60 | + * as those of `that`? |
| 61 | + */ |
| 62 | + def sameElements[B >: A](that: IterableOnce[B]): Boolean = |
| 63 | + coll.iterator().sameElements(that) |
| 64 | + |
| 65 | + /** Method called from equality methods, so that user-defined subclasses can |
| 66 | + * refuse to be equal to other collections of the same kind. |
| 67 | + * @param that The object with which this $coll should be compared |
| 68 | + * @return `true`, if this $coll can possibly equal `that`, `false` otherwise. The test |
| 69 | + * takes into consideration only the run-time types of objects but ignores their elements. |
| 70 | + */ |
| 71 | + def canEqual(that: Any): Boolean = true |
| 72 | + |
| 73 | + override def equals(o: scala.Any): Boolean = |
| 74 | + o match { |
| 75 | + case it: Seq[A] => (it canEqual this) && sameElements(it) |
| 76 | + case _ => false |
| 77 | + } |
| 78 | + |
| 79 | + override def hashCode(): Int = |
| 80 | + Seq.stableIterableHash(coll) |
| 81 | + |
| 82 | +} |
| 83 | + |
| 84 | +// Temporary: TODO move to MurmurHash3.scala |
| 85 | +object Seq { |
| 86 | + |
| 87 | + final def stableIterableHash(xs: Seq[_]): Int = { |
| 88 | + var n = 0 |
| 89 | + var h = "Seq".## |
| 90 | + val it = xs.iterator() |
| 91 | + while (it.hasNext) { |
| 92 | + h = MurmurHash3.mix(h, it.next().##) |
| 93 | + n += 1 |
| 94 | + } |
| 95 | + MurmurHash3.finalizeHash(h, n) |
| 96 | + } |
| 97 | + |
| 98 | +} |
54 | 99 |
|
55 | 100 | /** Base trait for linear Seq operations */
|
56 | 101 | trait LinearSeqLike[+A, +C[X] <: LinearSeq[X]] extends SeqLike[A, C] {
|
|
0 commit comments