From c5009df2e01c8975136926f04d4b3a5efa171d13 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sat, 23 Feb 2013 19:39:22 +0100 Subject: [PATCH] LA: Vectors: delegate GetHashCode fully to storage --- .../LinearAlgebra/Complex/SparseVector.cs | 22 ------------------- .../LinearAlgebra/Complex32/SparseVector.cs | 22 ------------------- .../LinearAlgebra/Double/SparseVector.cs | 22 ------------------- .../LinearAlgebra/Generic/Vector.BCL.cs | 4 ++-- .../LinearAlgebra/Single/SparseVector.cs | 22 ------------------- .../Storage/SparseVectorStorage.cs | 21 ++++++++++++++++++ 6 files changed, 23 insertions(+), 90 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs index 6b225a20..4bb26717 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs @@ -1103,28 +1103,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return base.ToString(format, formatProvider); } - /// - /// Returns a hash code for this instance. - /// - /// - /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. - /// - 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 /// diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs index 46e812fa..6b50585c 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs @@ -1103,28 +1103,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return base.ToString(format, formatProvider); } - /// - /// Returns a hash code for this instance. - /// - /// - /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. - /// - 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 /// diff --git a/src/Numerics/LinearAlgebra/Double/SparseVector.cs b/src/Numerics/LinearAlgebra/Double/SparseVector.cs index f8e5ea61..b30b698b 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseVector.cs @@ -1166,28 +1166,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double return base.ToString(format, formatProvider); } - /// - /// Returns a hash code for this instance. - /// - /// - /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. - /// - 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 /// diff --git a/src/Numerics/LinearAlgebra/Generic/Vector.BCL.cs b/src/Numerics/LinearAlgebra/Generic/Vector.BCL.cs index 4675be64..2547660e 100644 --- a/src/Numerics/LinearAlgebra/Generic/Vector.BCL.cs +++ b/src/Numerics/LinearAlgebra/Generic/Vector.BCL.cs @@ -63,7 +63,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// /// true if the specified is equal to this instance; otherwise, false. /// - public override bool Equals(object obj) + public override sealed bool Equals(object obj) { var other = obj as Vector; return other != null && Storage.Equals(other.Storage); @@ -75,7 +75,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. /// - public override int GetHashCode() + public override sealed int GetHashCode() { return Storage.GetHashCode(); } diff --git a/src/Numerics/LinearAlgebra/Single/SparseVector.cs b/src/Numerics/LinearAlgebra/Single/SparseVector.cs index 3e6b51f9..837c05ec 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseVector.cs @@ -1164,28 +1164,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single return base.ToString(format, formatProvider); } - /// - /// Returns a hash code for this instance. - /// - /// - /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. - /// - 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 /// diff --git a/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs b/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs index 5a302487..8f3159e9 100644 --- a/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs @@ -233,6 +233,27 @@ namespace MathNet.Numerics.LinearAlgebra.Storage return true; } + /// + /// Returns a hash code for this instance. + /// + /// + /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. + /// + 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 target, bool skipClearing = false) { var sparseTarget = target as SparseVectorStorage;