/// UInt32 range is 0 to 4,294,967,295
/// Use the fastest hashing that has the least duplicates
const UInt32 m = 0x5bd1e995;
const Int32 r = 24;
[StructLayout(LayoutKind.Explicit)]
struct BytetoUInt32Converter
{
[FieldOffset(0)]
public Byte[] Bytes;
[FieldOffset(0)]
public UInt32[] UInts;
}
public static UInt32 HashItem<T>(T item)
{
try{
List<byte> byteList = new List<byte>();
using (MemoryStream ms = new MemoryStream()){
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms, item);
ms.Flush();
ms.Position = 0;
byte[] buffer = new byte[100];
while(true)
{
var numRead = ms.Read(buffer, 0, buffer.Length);
if (numRead == 0)
break;
else{
for(var i = 0; i < numRead; i++){ byteList.Add(buffer[i]); }
}
}
}
return Hash(byteList.ToArray());
}
catch{ return 0;}
} |