Skip to content

Commit

Permalink
Remove unused methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
GGG-KILLER committed Jan 28, 2024
1 parent 03910b7 commit 9175223
Showing 1 changed file with 0 additions and 317 deletions.
317 changes: 0 additions & 317 deletions Tsu.Trees.RedGreen/src/Utilities/Hash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// See the LICENSE file in the project root for more information.
// Sourced from: https://github.com/dotnet/roslyn/blob/cda55873bfd7f16ac6176f467f98ddf8f644e101/src/Compilers/Core/Portable/InternalUtilities/Hash.cs

using System.Collections.Immutable;
using System.ComponentModel;

namespace Tsu.Trees.RedGreen.Utilities;
Expand Down Expand Up @@ -74,25 +73,6 @@ public static int CombineValues<T>(IEnumerable<T>? values, int maxItemsToHash =
return hashCode;
}

public static int CombineValues<TKey, TValue>(ImmutableDictionary<TKey, TValue> values, int maxItemsToHash = int.MaxValue)
where TKey : notnull
{
if (values == null)
return 0;

var hashCode = 0;
var count = 0;
foreach (var value in values)
{
if (count++ >= maxItemsToHash)
break;

hashCode = Combine(value.GetHashCode(), hashCode);
}

return hashCode;
}

public static int CombineValues<T>(T[]? values, int maxItemsToHash = int.MaxValue)
{
if (values == null)
Expand All @@ -117,32 +97,6 @@ public static int CombineValues<T>(T[]? values, int maxItemsToHash = int.MaxValu
return hashCode;
}

public static int CombineValues<T>(ImmutableArray<T> values, int maxItemsToHash = int.MaxValue)
{
if (values.IsDefaultOrEmpty)
{
return 0;
}

var hashCode = 0;
var count = 0;
foreach (var value in values)
{
if (count++ >= maxItemsToHash)
{
break;
}

// Should end up with a constrained virtual call to object.GetHashCode (i.e. avoid boxing where possible).
if (value != null)
{
hashCode = Combine(value.GetHashCode(), hashCode);
}
}

return hashCode;
}

public static int CombineValues(IEnumerable<string?>? values, StringComparer stringComparer, int maxItemsToHash = int.MaxValue)
{
if (values == null)
Expand All @@ -167,275 +121,4 @@ public static int CombineValues(IEnumerable<string?>? values, StringComparer str

return hashCode;
}

public static int CombineValues(ImmutableArray<string> values, StringComparer stringComparer, int maxItemsToHash = int.MaxValue)
{
if (values == null)
return 0;

var hashCode = 0;
var count = 0;
foreach (var value in values)
{
if (count++ >= maxItemsToHash)
break;

if (value != null)
hashCode = Combine(stringComparer.GetHashCode(value), hashCode);
}

return hashCode;
}

/// <summary>
/// The offset bias value used in the FNV-1a algorithm
/// See http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
/// </summary>
internal const int FnvOffsetBias = unchecked((int) 2166136261);

/// <summary>
/// The generative factor used in the FNV-1a algorithm
/// See http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
/// </summary>
internal const int FnvPrime = 16777619;

/// <summary>
/// Compute the FNV-1a hash of a sequence of bytes
/// See http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
/// </summary>
/// <param name="data">The sequence of bytes</param>
/// <returns>The FNV-1a hash of <paramref name="data"/></returns>
public static int GetFNVHashCode(byte[] data)
{
int hashCode = FnvOffsetBias;

for (int i = 0; i < data.Length; i++)
{
hashCode = unchecked((hashCode ^ data[i]) * FnvPrime);
}

return hashCode;
}

/// <summary>
/// Compute the FNV-1a hash of a sequence of bytes and determines if the byte
/// sequence is valid ASCII and hence the hash code matches a char sequence
/// encoding the same text.
/// See http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
/// </summary>
/// <param name="data">The sequence of bytes that are likely to be ASCII text.</param>
/// <param name="isAscii">True if the sequence contains only characters in the ASCII range.</param>
/// <returns>The FNV-1a hash of <paramref name="data"/></returns>
public static int GetFNVHashCode(ReadOnlySpan<byte> data, out bool isAscii)
{
int hashCode = FnvOffsetBias;

byte asciiMask = 0;

for (int i = 0; i < data.Length; i++)
{
byte b = data[i];
asciiMask |= b;
hashCode = unchecked((hashCode ^ b) * FnvPrime);
}

isAscii = (asciiMask & 0x80) == 0;
return hashCode;
}

/// <summary>
/// Compute the FNV-1a hash of a sequence of bytes
/// See http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
/// </summary>
/// <param name="data">The sequence of bytes</param>
/// <returns>The FNV-1a hash of <paramref name="data"/></returns>
public static int GetFNVHashCode(ImmutableArray<byte> data)
{
int hashCode = FnvOffsetBias;

for (int i = 0; i < data.Length; i++)
{
hashCode = unchecked((hashCode ^ data[i]) * FnvPrime);
}

return hashCode;
}

/// <summary>
/// Compute the hashcode of a sub-string using FNV-1a
/// See http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
/// Note: FNV-1a was developed and tuned for 8-bit sequences. We're using it here
/// for 16-bit Unicode chars on the understanding that the majority of chars will
/// fit into 8-bits and, therefore, the algorithm will retain its desirable traits
/// for generating hash codes.
/// </summary>
public static int GetFNVHashCode(ReadOnlySpan<char> data)
{
return CombineFNVHash(FnvOffsetBias, data);
}

/// <summary>
/// Compute the hashcode of a sub-string using FNV-1a
/// See http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
/// Note: FNV-1a was developed and tuned for 8-bit sequences. We're using it here
/// for 16-bit Unicode chars on the understanding that the majority of chars will
/// fit into 8-bits and, therefore, the algorithm will retain its desirable traits
/// for generating hash codes.
/// </summary>
/// <param name="text">The input string</param>
/// <param name="start">The start index of the first character to hash</param>
/// <param name="length">The number of characters, beginning with <paramref name="start"/> to hash</param>
/// <returns>The FNV-1a hash code of the substring beginning at <paramref name="start"/> and ending after <paramref name="length"/> characters.</returns>
public static int GetFNVHashCode(string text, int start, int length)
=> GetFNVHashCode(text.AsSpan(start, length));

public static int GetCaseInsensitiveFNVHashCode(string text)
{
return GetCaseInsensitiveFNVHashCode(text.AsSpan(0, text.Length));
}

public static int GetCaseInsensitiveFNVHashCode(ReadOnlySpan<char> data)
{
int hashCode = FnvOffsetBias;

for (int i = 0; i < data.Length; i++)
{
hashCode = unchecked((hashCode ^ char.ToLower(data[i])) * FnvPrime);
}

return hashCode;
}

/// <summary>
/// Compute the hashcode of a sub-string using FNV-1a
/// See http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
/// </summary>
/// <param name="text">The input string</param>
/// <param name="start">The start index of the first character to hash</param>
/// <returns>The FNV-1a hash code of the substring beginning at <paramref name="start"/> and ending at the end of the string.</returns>
public static int GetFNVHashCode(string text, int start)
{
return GetFNVHashCode(text, start, length: text.Length - start);
}

/// <summary>
/// Compute the hashcode of a string using FNV-1a
/// See http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
/// </summary>
/// <param name="text">The input string</param>
/// <returns>The FNV-1a hash code of <paramref name="text"/></returns>
public static int GetFNVHashCode(string text)
{
return CombineFNVHash(FnvOffsetBias, text);
}

/// <summary>
/// Compute the hashcode of a string using FNV-1a
/// See http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
/// </summary>
/// <param name="text">The input string</param>
/// <returns>The FNV-1a hash code of <paramref name="text"/></returns>
public static int GetFNVHashCode(System.Text.StringBuilder text)
{
int hashCode = FnvOffsetBias;

#if NETCOREAPP3_1_OR_GREATER
foreach (var chunk in text.GetChunks())
{
hashCode = CombineFNVHash(hashCode, chunk.Span);
}
#else
// StringBuilder.GetChunks is not available in this target framework. Since there is no other direct access
// to the underlying storage spans of StringBuilder, we fall back to using slower per-character operations.
int end = text.Length;

for (int i = 0; i < end; i++)
{
hashCode = unchecked((hashCode ^ text[i]) * Hash.FnvPrime);
}
#endif

return hashCode;
}

/// <summary>
/// Compute the hashcode of a sub string using FNV-1a
/// See http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
/// </summary>
/// <param name="text">The input string as a char array</param>
/// <param name="start">The start index of the first character to hash</param>
/// <param name="length">The number of characters, beginning with <paramref name="start"/> to hash</param>
/// <returns>The FNV-1a hash code of the substring beginning at <paramref name="start"/> and ending after <paramref name="length"/> characters.</returns>
public static int GetFNVHashCode(char[] text, int start, int length)
{
int hashCode = FnvOffsetBias;
int end = start + length;

for (int i = start; i < end; i++)
{
hashCode = unchecked((hashCode ^ text[i]) * FnvPrime);
}

return hashCode;
}

/// <summary>
/// Compute the hashcode of a single character using the FNV-1a algorithm
/// See http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
/// Note: In general, this isn't any more useful than "char.GetHashCode". However,
/// it may be needed if you need to generate the same hash code as a string or
/// substring with just a single character.
/// </summary>
/// <param name="ch">The character to hash</param>
/// <returns>The FNV-1a hash code of the character.</returns>
public static int GetFNVHashCode(char ch)
{
return CombineFNVHash(FnvOffsetBias, ch);
}

/// <summary>
/// Combine a string with an existing FNV-1a hash code
/// See http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
/// </summary>
/// <param name="hashCode">The accumulated hash code</param>
/// <param name="text">The string to combine</param>
/// <returns>The result of combining <paramref name="hashCode"/> with <paramref name="text"/> using the FNV-1a algorithm</returns>
public static int CombineFNVHash(int hashCode, string text)
{
foreach (char ch in text)
{
hashCode = unchecked((hashCode ^ ch) * FnvPrime);
}

return hashCode;
}

/// <summary>
/// Combine a char with an existing FNV-1a hash code
/// See http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
/// </summary>
/// <param name="hashCode">The accumulated hash code</param>
/// <param name="ch">The new character to combine</param>
/// <returns>The result of combining <paramref name="hashCode"/> with <paramref name="ch"/> using the FNV-1a algorithm</returns>
public static int CombineFNVHash(int hashCode, char ch)
{
return unchecked((hashCode ^ ch) * FnvPrime);
}

/// <summary>
/// Combine a string with an existing FNV-1a hash code
/// See http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
/// </summary>
/// <param name="hashCode">The accumulated hash code</param>
/// <param name="data">The string to combine</param>
/// <returns>The result of combining <paramref name="hashCode"/> with <paramref name="data"/> using the FNV-1a algorithm</returns>
public static int CombineFNVHash(int hashCode, ReadOnlySpan<char> data)
{
for (int i = 0; i < data.Length; i++)
{
hashCode = unchecked((hashCode ^ data[i]) * FnvPrime);
}

return hashCode;
}
}

0 comments on commit 9175223

Please sign in to comment.