JinChanChan/Packages/com.arongranberg.astar/Utilities/AstarChecksum.cs

21 lines
692 B
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

namespace Pathfinding.Util {
/// <summary>Calculates checksums of byte arrays</summary>
internal class Checksum {
/// <summary>Calculate checksum for the byte array.</summary>
/// <param name="arr">Byte array to calculate checksum for. May be null.</param>
/// <param name="hash">Initial hash value. Default is 0. Can be used to chain checksums together.</param>
public static uint GetChecksum (byte[] arr, uint hash = 0) {
// Sort of implements the FowlerNollVo hash function
const int prime = 16777619;
hash ^= 2166136261U;
if (arr == null) return hash - 1;
for (int i = 0; i < arr.Length; i++)
hash = (hash ^ arr[i]) * prime;
return hash;
}
}
}