// -----------------------------------------------------------------------
//
// TODO: Update copyright text.
//
// -----------------------------------------------------------------------
namespace Lucene.Net.Util
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
///
/// Utility class for generating hash codes using 31 as the default prime.
///
public class HashCodeUtil
{
///
/// Generates the hash code.
///
/// The append.
/// The initial.
/// The prime.
/// An instance of .
public static int GenerateHashCode(int append, int initial = 0, int prime = 31)
{
int code = initial;
code = (code * prime) + append;
return code;
}
///
/// Generates the hash code.
///
/// The type of the array.
/// The slices.
/// The offset.
/// The length.
/// The prime.
/// The code.
///
///
/// Unfortunately using the interface to constrain
/// to types that could be converted to would cause Lucene.Net to lose
/// CLS-compliance. And Microsoft will not create another interface to fix the issue:
/// http://connect.microsoft.com/VisualStudio/feedback/details/308902/iconvertable-breaks-cls-compliance-for-classes-that-have-generic-constraints-including-iconvertable
///
///
/// An instance of .
///
/// Thrown when items of the array of can not be
/// converted to Int32.
///
public static int GenerateHashCode(T[] slices, int offset = 0, int length = 0, int prime = 31, int code = 0)
{
if (length == 0)
length = slices.Length;
int end = offset + length;
for (int i = offset; i < end; i++)
{
code = (code * prime) + Convert.ToInt32(slices[i]);
}
return code;
}
}
}