Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 298 lines (265 sloc) 8.833 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
namespace Logos.Utility
{
/// <summary>
/// Provides methods for manipulating and creating hash codes.
/// </summary>
/// <remarks>
/// <para>This code is based on Bob Jenkins' public domain <a href="http://burtleburtle.net/bob/c/lookup3.c">lookup3.c</a> code.</para>
/// <para>This work is hereby released into the Public Domain. To view a copy of the public domain dedication,
/// visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
/// Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.</para>
/// </remarks>
public static class HashCodeUtility
{
/// <summary>
/// Combines the specified hash codes.
/// </summary>
/// <param name="hashCode1">The first hash code.</param>
/// <returns>The combined hash code.</returns>
/// <remarks>This is a specialization of <see cref="CombineHashCodes(int[])"/> for efficiency.</remarks>
public static int CombineHashCodes(int hashCode1)
{
unchecked
{
uint a = 0xdeadbeef + 4;
uint b = a;
uint c = a;

a += (uint) hashCode1;
FinalizeHash(ref a, ref b, ref c);

return (int) c;
}
}

/// <summary>
/// Combines the specified hash codes.
/// </summary>
/// <param name="hashCode1">The first hash code.</param>
/// <param name="hashCode2">The second hash code.</param>
/// <returns>The combined hash code.</returns>
/// <remarks>This is a specialization of <see cref="CombineHashCodes(int[])"/> for efficiency.</remarks>
public static int CombineHashCodes(int hashCode1, int hashCode2)
{
unchecked
{
uint a = 0xdeadbeef + 8;
uint b = a;
uint c = a;

a += (uint) hashCode1;
b += (uint) hashCode2;
FinalizeHash(ref a, ref b, ref c);

return (int) c;
}
}

/// <summary>
/// Combines the specified hash codes.
/// </summary>
/// <param name="hashCode1">The first hash code.</param>
/// <param name="hashCode2">The second hash code.</param>
/// <param name="hashCode3">The third hash code.</param>
/// <returns>The combined hash code.</returns>
/// <remarks>This is a specialization of <see cref="CombineHashCodes(int[])"/> for efficiency.</remarks>
public static int CombineHashCodes(int hashCode1, int hashCode2, int hashCode3)
{
unchecked
{
uint a = 0xdeadbeef + 12;
uint b = a;
uint c = a;

a += (uint) hashCode1;
b += (uint) hashCode2;
c += (uint) hashCode3;
FinalizeHash(ref a, ref b, ref c);

return (int) c;
}
}

/// <summary>
/// Combines the specified hash codes.
/// </summary>
/// <param name="hashCode1">The first hash code.</param>
/// <param name="hashCode2">The second hash code.</param>
/// <param name="hashCode3">The third hash code.</param>
/// <param name="hashCode4">The fourth hash code.</param>
/// <returns>The combined hash code.</returns>
/// <remarks>This is a specialization of <see cref="CombineHashCodes(int[])"/> for efficiency.</remarks>
public static int CombineHashCodes(int hashCode1, int hashCode2, int hashCode3, int hashCode4)
{
unchecked
{
uint a = 0xdeadbeef + 16;
uint b = a;
uint c = a;

a += (uint) hashCode1;
b += (uint) hashCode2;
c += (uint) hashCode3;
MixHash(ref a, ref b, ref c);

a += (uint) hashCode4;
FinalizeHash(ref a, ref b, ref c);

return (int) c;
}
}

/// <summary>
/// Combines the specified hash codes.
/// </summary>
/// <param name="hashCodes">An array of hash codes.</param>
/// <returns>The combined hash code.</returns>
/// <remarks>This method is based on the "hashword" function at http://burtleburtle.net/bob/c/lookup3.c. It attempts to thoroughly
/// mix all the bits in the input hash codes.</remarks>
public static int CombineHashCodes(params int[] hashCodes)
{
unchecked
{
// check for null
if (hashCodes == null)
return 0x0d608219;

int length = hashCodes.Length;

uint a = 0xdeadbeef + (((uint) length) << 2);
uint b = a;
uint c = a;

int index = 0;
while (length - index > 3)
{
a += (uint) hashCodes[index];
b += (uint) hashCodes[index + 1];
c += (uint) hashCodes[index + 2];
MixHash(ref a, ref b, ref c);
index += 3;
}

if (length - index > 2)
c += (uint) hashCodes[index + 2];
if (length - index > 1)
b += (uint) hashCodes[index + 1];

if (length - index > 0)
{
a += (uint) hashCodes[index];
FinalizeHash(ref a, ref b, ref c);
}

return (int) c;
}
}

/// <summary>
/// Gets a hash code for the specified <see cref="bool"/>; this hash code is guaranteed not to change in the future.
/// </summary>
/// <param name="value">The <see cref="bool"/> to hash.</param>
/// <returns>A hash code for the specified <see cref="bool"/>.</returns>
public static int GetPersistentHashCode(bool value)
{
// these values are the persistent hash codes for 0 and 1
return value ? -1266253386 : 1800329511;
}

/// <summary>
/// Gets a hash code for the specified <see cref="int"/>; this hash code is guaranteed not to change in the future.
/// </summary>
/// <param name="value">The <see cref="int"/> to hash.</param>
/// <returns>A hash code for the specified <see cref="int"/>.</returns>
/// <remarks>Based on <a href="http://www.concentric.net/~Ttwang/tech/inthash.htm">Robert Jenkins' 32 bit integer hash function</a>.</remarks>
public static int GetPersistentHashCode(int value)
{
unchecked
{
uint hash = (uint) value;
hash = (hash + 0x7ed55d16) + (hash << 12);
hash = (hash ^ 0xc761c23c) ^ (hash >> 19);
hash = (hash + 0x165667b1) + (hash << 5);
hash = (hash + 0xd3a2646c) ^ (hash << 9);
hash = (hash + 0xfd7046c5) + (hash << 3);
hash = (hash ^ 0xb55a4f09) ^ (hash >> 16);
return (int) hash;
}
}

/// <summary>
/// Gets a hash code for the specified <see cref="long"/>; this hash code is guaranteed not to change in the future.
/// </summary>
/// <param name="value">The <see cref="long"/> to hash.</param>
/// <returns>A hash code for the specified <see cref="long"/>.</returns>
/// <remarks>Based on <a href="http://www.concentric.net/~Ttwang/tech/inthash.htm">64 bit to 32 bit Hash Functions</a>.</remarks>
public static int GetPersistentHashCode(long value)
{
unchecked
{
ulong hash = (ulong) value;
hash = (~hash) + (hash << 18);
hash = hash ^ (hash >> 31);
hash = hash * 21;
hash = hash ^ (hash >> 11);
hash = hash + (hash << 6);
hash = hash ^ (hash >> 22);
return (int) hash;
}
}

/// <summary>
/// Gets a hash code for the specified <see cref="string"/>; this hash code is guaranteed not to change in the future.
/// </summary>
/// <param name="value">The <see cref="string"/> to hash.</param>
/// <returns>A hash code for the specified <see cref="string"/>.</returns>
/// <remarks>Based on <a href="http://www.azillionmonkeys.com/qed/hash.html">SuperFastHash</a>.</remarks>
public static int GetPersistentHashCode(string value)
{
unchecked
{
// check for degenerate input
if (string.IsNullOrEmpty(value))
return 0;

int length = value.Length;
uint hash = (uint) length;

int remainder = length & 1;
length >>= 1;

// main loop
int index = 0;
for (; length > 0; length--)
{
hash += value[index];
uint temp = (uint) (value[index + 1] << 11) ^ hash;
hash = (hash << 16) ^ temp;
index += 2;
hash += hash >> 11;
}

// handle odd string length
if (remainder == 1)
{
hash += value[index];
hash ^= hash << 11;
hash += hash >> 17;
}

// force "avalanching" of final 127 bits
hash ^= hash << 3;
hash += hash >> 5;
hash ^= hash << 4;
hash += hash >> 17;
hash ^= hash << 25;
hash += hash >> 6;

return (int) hash;
}
}

// The "rot()" macro from http://burtleburtle.net/bob/c/lookup3.c
private static uint Rotate(uint x, int k)
{
return (x << k) | (x >> (32 - k));
}

// The "mix()" macro from http://burtleburtle.net/bob/c/lookup3.c
private static void MixHash(ref uint a, ref uint b, ref uint c)
{
unchecked
{
a -= c; a ^= Rotate(c, 4); c += b;
b -= a; b ^= Rotate(a, 6); a += c;
c -= b; c ^= Rotate(b, 8); b += a;
a -= c; a ^= Rotate(c, 16); c += b;
b -= a; b ^= Rotate(a, 19); a += c;
c -= b; c ^= Rotate(b, 4); b += a;
}
}

// The "final()" macro from http://burtleburtle.net/bob/c/lookup3.c
private static void FinalizeHash(ref uint a, ref uint b, ref uint c)
{
unchecked
{
c ^= b; c -= Rotate(b, 14);
a ^= c; a -= Rotate(c, 11);
b ^= a; b -= Rotate(a, 25);
c ^= b; c -= Rotate(b, 16);
a ^= c; a -= Rotate(c, 4);
b ^= a; b -= Rotate(a, 14);
c ^= b; c -= Rotate(b, 24);
}
}
}
}
Something went wrong with that request. Please try again.