Hash 分布均衡算法

1.移位实现

      public static int GetIndex(string str, int count)
        {
            int hash = str.Aggregate(23, (current, c) => (current << 5) - current + c);
            if (hash < 0)
            {
                hash = Math.Abs(hash);
            }
            return hash % count;
        }

2.GetHashCode()

      public static int GetIndex2(string str, int count)
        {
            int hash =str.GetHashCode();
            if (hash < 0)
            {
                hash = Math.Abs(hash);
            }
            return hash % count;
        }

 

posted @ 2017-04-06 15:06  b̶i̶n̶g̶.̶  阅读(1121)  评论(1编辑  收藏  举报