using System.Collections.Generic;

namespace Boggle
{
    /// <summary>
    /// Equality comparer used for HashSet. Used to compare two words in the hash set and detect collisions.
    /// This is needed because we are making HashSet with a custom type and .NET will use expensive
    /// procedure to determine a unique hash (by treating it as an Object instance) if this is not provided.
    /// </summary>
    public class WordComparer : EqualityComparer<WordOnBoard>
    {
        public override bool Equals(WordOnBoard x, WordOnBoard y)
        {
            return string.Compare(x.Word, y.Word, true) == 0;
        }

        public override int GetHashCode(WordOnBoard obj)
        {
            int hash = 0;
            for (int i = 0; i < obj.Word.Length; i++)
            {
                hash += (int)obj.Word[i] * 10 ^ (obj.Word.Length - 1 - i);

                //cat would result in 99 * 10^2 + 97 * 10^1 + 116 * 10^0 = 10986
            }

            return hash;
        }
    }
}
