|
[size=14.399999618530273px]表面上是无规律的,实际上还是有规律的。
[size=14.399999618530273px]
[size=14.399999618530273px]- using System;
- using System.Collections;
- class MyClass
- {
- static readonly int[] primes =
- {
- 3, 7, 11, 17, 23, 29, 37, 47, 59, 71, 89, 107, 131, 163, 197, 239, 293, 353, 431, 521, 631, 761, 919,
- 1103, 1327, 1597, 1931, 2333, 2801, 3371, 4049, 4861, 5839, 7013, 8419, 10103, 12143, 14591,
- 17519, 21023, 25229, 30293, 36353, 43627, 52361, 62851, 75431, 90523, 108631, 130363, 156437,
- 187751, 225307, 270371, 324449, 389357, 467237, 560689, 672827, 807403, 968897, 1162687, 1395263,
- 1674319, 2009191, 2411033, 2893249, 3471899, 4166287, 4999559, 5999471, 7199369
- };
- static int GetHashcode(object key, int k, int hashsize)
- {
- //Hk(key) = [GetHash(key) + k * (1 + (((GetHash(key) >> 5) + 1) % (hashsize – 1)))] % hashsize
- int k_code = key.GetHashCode();
- return Math.Abs(k_code + k * (1 + (((k_code >> 5) + 1) % (hashsize - 1)))) % hashsize;
- }
- static void Print(Hashtable hs)
- {
- Console.WriteLine("============Hashtable Test=================");
- foreach (string key in hs.Keys)
- {
- Console.WriteLine("Hk(key):{0:0000},Key:{1}", GetHashcode(key, 1, 7), key);
- }
- }
- static void Main(string[] args)
- {
- //public Hashtable(int capacity, float loadFactor);
- //Hashtable hs = new Hashtable { 1, 2, 3, 4, 5, };
- //测试C
- Hashtable hashC = new Hashtable(7);
- hashC.Add("5", "顺序1");
- hashC.Add("1", "顺序1");
- hashC.Add("6", "顺序2");
- hashC.Add("4", "顺序2");
- hashC.Add("3", "顺序3");//<-EndPos(操作完后移动到下一个位置,即"4")
- Print(hashC);
- Console.WriteLine("add all");
- //0123456<==capacity(容量)=7
- // 1 3456<==最后一次操作的下一位置
- // ^^
- // 4 5123<==指针移动顺序
- hashC.Remove("0");
- Print(hashC);
- Console.WriteLine("hashC.Remove('0');");
- hashC.Remove("0");
- Print(hashC);
- Console.WriteLine("hashC.Remove('0');");
- hashC.Remove("4");//删除指针下移
- Print(hashC);
- Console.WriteLine("hashC.Remove('4');");
- hashC.Remove("0");
- Print(hashC);
- Console.WriteLine("hashC.Remove('4');");
- hashC.Add("2", "---");//添加不移动指针?!这次没动
- Print(hashC);
- Console.WriteLine("hashC.Add('2');");
-
- Console.ReadKey();
- }
- }
复制代码
[size=14.399999618530273px]
|
|