@@ -462,27 +462,31 @@ class SparseVector(
462462 " (%s,%s,%s)" .format(size, indices.mkString(" [" , " ," , " ]" ), values.mkString(" [" , " ," , " ]" ))
463463
464464 override def equals (other : Any ): Boolean = {
465- def nextNonzero (values : Array [Double ], from : Int ): Int = {
466- var index = from
467- while (index < values.size && values(index) == 0.0 ) index += 1
468- index
469- }
470-
471465 other match {
472466 case v : SparseVector => {
473467 if (this .size != v.size) { return false }
474- var k1 = nextNonzero(this .values, 0 )
475- var k2 = nextNonzero(v.values, 0 )
476-
477- while (k1 < this .values.size && k2 < v.values.size) {
478- if (this .indices(k1) != v.indices(k2) || this .values(k1) != v.values(k2)) {
479- return false
468+ val thisValues = this .values
469+ val thisIndices = this .indices
470+ val thisSize = thisValues.size
471+ val otherValues = v.values
472+ val otherIndices = v.indices
473+ val otherSize = otherValues.size
474+
475+ var k1 = 0
476+ var k2 = 0
477+ var allEqual = true
478+ while (allEqual) {
479+ while (k1 < thisSize && thisValues(k1) == 0 ) k1 += 1
480+ while (k2 < otherSize && otherValues(k2) == 0 ) k2 += 1
481+
482+ if (k1 >= thisSize || k2 >= otherSize) {
483+ return k1 >= thisSize && k2 >= otherSize // check end alignment
480484 }
481- k1 = nextNonzero(this .values, k1 + 1 )
482- k2 = nextNonzero(v.values, k2 + 1 )
485+ allEqual = thisIndices(k1) == otherIndices(k2) && thisValues(k1) == otherValues(k2)
486+ k1 += 1
487+ k2 += 1
483488 }
484-
485- return (k1 == this .values.size && k2 == v.values.size)
489+ allEqual
486490 }
487491 case _ => super .equals(other)
488492 }
0 commit comments