首页 > 代码库 > 分布均衡算法

分布均衡算法

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;
        }

 

分布均衡算法