Skip to content

Commit 2af14fa

Browse files
committed
Remove StableIterable
1 parent 382633b commit 2af14fa

File tree

2 files changed

+54
-64
lines changed

2 files changed

+54
-64
lines changed

src/main/scala/strawman/collection/Seq.scala

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package strawman.collection
22

3-
import scala.{Any, Boolean, Int, IndexOutOfBoundsException}
3+
import scala.{Any, Boolean, Equals, IndexOutOfBoundsException, Int}
44
import strawman.collection.immutable.{List, Nil}
55

66
import scala.annotation.unchecked.uncheckedVariance
7+
import scala.util.hashing.MurmurHash3
78

89
/** Base trait for sequence collections */
910
trait Seq[+A] extends Iterable[A] with SeqLike[A, Seq] with ArrayLike[A]
@@ -51,7 +52,58 @@ trait IndexedSeq[+A] extends Seq[A] { self =>
5152
trait SeqLike[+A, +C[X] <: Seq[X]]
5253
extends IterableLike[A, C]
5354
with SeqMonoTransforms[A, C[A @uncheckedVariance]] // sound bcs of VarianceNote
54-
with StableIterable[A]
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+
val these = coll.iterator()
64+
val those = that.iterator()
65+
while (these.hasNext && those.hasNext)
66+
if (these.next() != those.next())
67+
return false
68+
// At that point we know that *at least one* iterator has no next element
69+
// If *both* of them have no elements then the collections are the same
70+
these.hasNext == those.hasNext
71+
}
72+
73+
/** Method called from equality methods, so that user-defined subclasses can
74+
* refuse to be equal to other collections of the same kind.
75+
* @param that The object with which this $coll should be compared
76+
* @return `true`, if this $coll can possibly equal `that`, `false` otherwise. The test
77+
* takes into consideration only the run-time types of objects but ignores their elements.
78+
*/
79+
def canEqual(that: Any): Boolean = true
80+
81+
override def equals(o: scala.Any): Boolean =
82+
o match {
83+
case it: Seq[A] => (it canEqual this) && sameElements(it)
84+
case _ => false
85+
}
86+
87+
override def hashCode(): Int =
88+
Seq.stableIterableHash(coll)
89+
90+
}
91+
92+
// Temporary: TODO move to MurmurHash3.scala
93+
object Seq {
94+
95+
final def stableIterableHash(xs: Seq[_]): Int = {
96+
var n = 0
97+
var h = "Seq".##
98+
val it = xs.iterator()
99+
while (it.hasNext) {
100+
h = MurmurHash3.mix(h, it.next().##)
101+
n += 1
102+
}
103+
MurmurHash3.finalizeHash(h, n)
104+
}
105+
106+
}
55107

56108
/** Base trait for linear Seq operations */
57109
trait LinearSeqLike[+A, +C[X] <: LinearSeq[X]] extends SeqLike[A, C] {

src/main/scala/strawman/collection/StableIterable.scala

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)