double result = 0;
if (x instanceof SequentialAccessSparseVector) {
// For sparse SeqAccVectors. do dot product without lookup in a linear fashion
Iterator<Element> myIter = iterateNonZero();
Iterator<Element> otherIter = x.iterateNonZero();
Element myCurrent = null;
Element otherCurrent = null;
while (myIter.hasNext() && otherIter.hasNext()) {
if (myCurrent == null) myCurrent = myIter.next();
if (otherCurrent == null) otherCurrent = otherIter.next();
int myIndex = myCurrent.index();
int otherIndex = otherCurrent.index();
if (myIndex < otherIndex) {
// due to the sparseness skipping occurs more hence checked before equality
myCurrent = null;
} else if (myIndex > otherIndex){
otherCurrent = null;
} else { // both are equal
result += myCurrent.get() * otherCurrent.get();
myCurrent = null;
otherCurrent = null;
}
}
return result;
} else { // seq.rand. seq.dense
Iterator<Element> iter = iterateNonZero();
while (iter.hasNext()) {
Element element = iter.next();
result += element.get() * x.getQuick(element.index());
}
return result;
}
}