Browse Source

LA: Vectors: delegate GetHashCode fully to storage

v2
Christoph Ruegg 14 years ago
parent
commit
c5009df2e0
  1. 22
      src/Numerics/LinearAlgebra/Complex/SparseVector.cs
  2. 22
      src/Numerics/LinearAlgebra/Complex32/SparseVector.cs
  3. 22
      src/Numerics/LinearAlgebra/Double/SparseVector.cs
  4. 4
      src/Numerics/LinearAlgebra/Generic/Vector.BCL.cs
  5. 22
      src/Numerics/LinearAlgebra/Single/SparseVector.cs
  6. 21
      src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs

22
src/Numerics/LinearAlgebra/Complex/SparseVector.cs

@ -1103,28 +1103,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return base.ToString(format, formatProvider); return base.ToString(format, formatProvider);
} }
/// <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(_storage.ValueCount, 20);
long hash = 0;
for (var i = 0; i < hashNum; i++)
{
#if PORTABLE
hash ^= Precision.DoubleToInt64Bits(_storage.Values[i].GetHashCode());
#else
hash ^= BitConverter.DoubleToInt64Bits(_storage.Values[i].GetHashCode());
#endif
}
return BitConverter.ToInt32(BitConverter.GetBytes(hash), 4);
}
#endregion #endregion
/// <summary> /// <summary>

22
src/Numerics/LinearAlgebra/Complex32/SparseVector.cs

@ -1103,28 +1103,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return base.ToString(format, formatProvider); return base.ToString(format, formatProvider);
} }
/// <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(_storage.ValueCount, 20);
long hash = 0;
for (var i = 0; i < hashNum; i++)
{
#if PORTABLE
hash ^= Precision.DoubleToInt64Bits(_storage.Values[i].GetHashCode());
#else
hash ^= BitConverter.DoubleToInt64Bits(_storage.Values[i].GetHashCode());
#endif
}
return BitConverter.ToInt32(BitConverter.GetBytes(hash), 4);
}
#endregion #endregion
/// <summary> /// <summary>

22
src/Numerics/LinearAlgebra/Double/SparseVector.cs

@ -1166,28 +1166,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return base.ToString(format, formatProvider); return base.ToString(format, formatProvider);
} }
/// <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(_storage.ValueCount, 20);
long hash = 0;
for (var i = 0; i < hashNum; i++)
{
#if PORTABLE
hash ^= Precision.DoubleToInt64Bits(_storage.Values[i].GetHashCode());
#else
hash ^= BitConverter.DoubleToInt64Bits(_storage.Values[i].GetHashCode());
#endif
}
return BitConverter.ToInt32(BitConverter.GetBytes(hash), 4);
}
#endregion #endregion
/// <summary> /// <summary>

4
src/Numerics/LinearAlgebra/Generic/Vector.BCL.cs

@ -63,7 +63,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// <returns> /// <returns>
/// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>. /// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
/// </returns> /// </returns>
public override bool Equals(object obj) public override sealed bool Equals(object obj)
{ {
var other = obj as Vector<T>; var other = obj as Vector<T>;
return other != null && Storage.Equals(other.Storage); return other != null && Storage.Equals(other.Storage);
@ -75,7 +75,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// <returns> /// <returns>
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns> /// </returns>
public override int GetHashCode() public override sealed int GetHashCode()
{ {
return Storage.GetHashCode(); return Storage.GetHashCode();
} }

22
src/Numerics/LinearAlgebra/Single/SparseVector.cs

@ -1164,28 +1164,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return base.ToString(format, formatProvider); return base.ToString(format, formatProvider);
} }
/// <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(_storage.ValueCount, 20);
long hash = 0;
for (var i = 0; i < hashNum; i++)
{
#if PORTABLE
hash ^= Precision.DoubleToInt64Bits(_storage.Values[i].GetHashCode());
#else
hash ^= BitConverter.DoubleToInt64Bits(_storage.Values[i].GetHashCode());
#endif
}
return BitConverter.ToInt32(BitConverter.GetBytes(hash), 4);
}
#endregion #endregion
/// <summary> /// <summary>

21
src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs

@ -233,6 +233,27 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return true; return true;
} }
/// <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 values = Values;
var hashNum = Math.Min(ValueCount, 25);
int hash = 17;
unchecked
{
for (var i = 0; i < hashNum; i++)
{
hash = hash * 31 + values[i].GetHashCode();
}
}
return hash;
}
internal override void CopyToUnchecked(VectorStorage<T> target, bool skipClearing = false) internal override void CopyToUnchecked(VectorStorage<T> target, bool skipClearing = false)
{ {
var sparseTarget = target as SparseVectorStorage<T>; var sparseTarget = target as SparseVectorStorage<T>;

Loading…
Cancel
Save