Skip to content
Browse files

Sparse Linear Algebra: complex vectors now override right Equals. gh-20.

Previously sparse vectors for Complex and Complex32 did override
Object.Equals instead of the generic IEquatable<Vector<T>>.Equals.
Now only overrides the generic version to ensure that the right version
is called in all code paths.
  • Loading branch information...
1 parent 5710c5b commit 7484cabad3283c1852b077c1ec1e5708217e2e0b @cdrnet cdrnet committed Oct 11, 2011
View
79 src/Numerics/LinearAlgebra/Complex/SparseVector.cs
@@ -1271,28 +1271,65 @@ public override string ToString(string format, IFormatProvider formatProvider)
}
/// <summary>
- /// Check equality. If this is regular vector, then check by base implementation. If Sparse - use own method.
+ /// Returns a hash code for this instance.
/// </summary>
- /// <param name="obj">Object to compare</param>
/// <returns>
- /// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
+ /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns>
- public override bool Equals(object obj)
+ public override int GetHashCode()
{
- var sparseVector = obj as SparseVector;
+ var hashNum = Math.Min(NonZerosCount, 20);
+ long hash = 0;
+ for (var i = 0; i < hashNum; i++)
+ {
+#if SILVERLIGHT
+ hash ^= Precision.DoubleToInt64Bits(this._nonZeroValues[i].GetHashCode());
+#else
+ hash ^= BitConverter.DoubleToInt64Bits(_nonZeroValues[i].GetHashCode());
+#endif
+ }
- if (sparseVector == null)
+ return BitConverter.ToInt32(BitConverter.GetBytes(hash), 4);
+ }
+
+ #endregion
+
+ /// <summary>
+ /// Indicates whether the current object is equal to another object of the same type.
+ /// </summary>
+ /// <param name="other">
+ /// An object to compare with this object.
+ /// </param>
+ /// <returns>
+ /// <c>true</c> if the current object is equal to the <paramref name="other"/> parameter; otherwise, <c>false</c>.
+ /// </returns>
+ public override bool Equals(Vector<Complex> other)
+ {
+ // Reject equality when the argument is null or has a different length.
+ if (other == null)
{
- return base.Equals(obj);
+ return false;
+ }
+
+ if (Count != other.Count)
+ {
+ return false;
}
// Accept if the argument is the same object as this.
- if (ReferenceEquals(this, sparseVector))
+ if (ReferenceEquals(this, other))
{
return true;
}
- if ((Count != sparseVector.Count) || (NonZerosCount != sparseVector.NonZerosCount))
+ var sparseVector = other as SparseVector;
+
+ if (sparseVector == null)
+ {
+ return base.Equals(other);
+ }
+
+ if (NonZerosCount != sparseVector.NonZerosCount)
{
return false;
}
@@ -1310,30 +1347,6 @@ public override bool Equals(object obj)
}
/// <summary>
- /// Returns a hash code for this instance.
- /// </summary>
- /// <returns>
- /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
- /// </returns>
- public override int GetHashCode()
- {
- var hashNum = Math.Min(NonZerosCount, 20);
- long hash = 0;
- for (var i = 0; i < hashNum; i++)
- {
-#if SILVERLIGHT
- hash ^= Precision.DoubleToInt64Bits(this._nonZeroValues[i].GetHashCode());
-#else
- hash ^= BitConverter.DoubleToInt64Bits(_nonZeroValues[i].GetHashCode());
-#endif
- }
-
- return BitConverter.ToInt32(BitConverter.GetBytes(hash), 4);
- }
-
- #endregion
-
- /// <summary>
/// Returns an <see cref="IEnumerator{T}"/> that contains the position and value of the element.
/// </summary>
/// <returns>
View
79 src/Numerics/LinearAlgebra/Complex32/SparseVector.cs
@@ -1301,28 +1301,65 @@ public override string ToString(string format, IFormatProvider formatProvider)
}
/// <summary>
- /// Check equality. If this is regular vector, then check by base implementation. If Sparse - use own method.
+ /// Returns a hash code for this instance.
/// </summary>
- /// <param name="obj">Object to compare</param>
/// <returns>
- /// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
+ /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns>
- public override bool Equals(object obj)
+ public override int GetHashCode()
{
- var sparseVector = obj as SparseVector;
+ var hashNum = Math.Min(NonZerosCount, 20);
+ long hash = 0;
+ for (var i = 0; i < hashNum; i++)
+ {
+#if SILVERLIGHT
+ hash ^= Precision.DoubleToInt64Bits(this._nonZeroValues[i].GetHashCode());
+#else
+ hash ^= BitConverter.DoubleToInt64Bits(_nonZeroValues[i].GetHashCode());
+#endif
+ }
- if (sparseVector == null)
+ return BitConverter.ToInt32(BitConverter.GetBytes(hash), 4);
+ }
+
+ #endregion
+
+ /// <summary>
+ /// Indicates whether the current object is equal to another object of the same type.
+ /// </summary>
+ /// <param name="other">
+ /// An object to compare with this object.
+ /// </param>
+ /// <returns>
+ /// <c>true</c> if the current object is equal to the <paramref name="other"/> parameter; otherwise, <c>false</c>.
+ /// </returns>
+ public override bool Equals(Vector<Complex32> other)
+ {
+ // Reject equality when the argument is null or has a different length.
+ if (other == null)
{
- return base.Equals(obj);
+ return false;
+ }
+
+ if (Count != other.Count)
+ {
+ return false;
}
// Accept if the argument is the same object as this.
- if (ReferenceEquals(this, sparseVector))
+ if (ReferenceEquals(this, other))
{
return true;
}
- if ((Count != sparseVector.Count) || (NonZerosCount != sparseVector.NonZerosCount))
+ var sparseVector = other as SparseVector;
+
+ if (sparseVector == null)
+ {
+ return base.Equals(other);
+ }
+
+ if (NonZerosCount != sparseVector.NonZerosCount)
{
return false;
}
@@ -1340,30 +1377,6 @@ public override bool Equals(object obj)
}
/// <summary>
- /// Returns a hash code for this instance.
- /// </summary>
- /// <returns>
- /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
- /// </returns>
- public override int GetHashCode()
- {
- var hashNum = Math.Min(NonZerosCount, 20);
- long hash = 0;
- for (var i = 0; i < hashNum; i++)
- {
-#if SILVERLIGHT
- hash ^= Precision.DoubleToInt64Bits(this._nonZeroValues[i].GetHashCode());
-#else
- hash ^= BitConverter.DoubleToInt64Bits(_nonZeroValues[i].GetHashCode());
-#endif
- }
-
- return BitConverter.ToInt32(BitConverter.GetBytes(hash), 4);
- }
-
- #endregion
-
- /// <summary>
/// Returns an <see cref="IEnumerator{T}"/> that contains the position and value of the element.
/// </summary>
/// <returns>

0 comments on commit 7484cab

Please sign in to comment.
Something went wrong with that request. Please try again.