Borneq

Hash.ulam

Dec 19th, 2012
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.87 KB | None | 0 0
  1. enum TKeyAgain { kaEnable, kaException, kaSkip, kaOverwrite };
  2.  
  3. class HashTable<K,V>
  4. {
  5. public:
  6. struct HashItem{
  7.   uint hash;
  8.   K key;
  9.   V value;
  10.   HashItem *next;
  11. }
  12.  
  13. private:
  14.   unit FSize;
  15.   unit FCapacity
  16.   HashItem *[]FTab;
  17.   instantiation uint FHashFunc(K key);
  18. public:
  19.   TKeyAgain KeyAgain;
  20.   HashTable(uint ACapacity=256);
  21.   ~HashTable();
  22.   void Clear();
  23. }
  24.  
  25.  
  26. instantiation<K is TValue> uint FHashFunc(K key);
  27. {
  28.    return H(&key, sizeof(key));
  29. }
  30.  
  31. instantiation<K is TArray> uint FHashFunc(K key);
  32. {
  33.    return H(&key[], Length(key)*sizeof(key[]));
  34. }
  35.  
  36.  
  37. HashTable::Clear()
  38. {
  39.  for (int i=0; i<FCapacity; ++)
  40.  {
  41.     HashItem* item := FTab[i];
  42.     while (item != null)
  43.     {
  44.       HashItem* p = item;
  45.       item = item.next;
  46.       delete p;
  47.     }
  48.  }
  49. }
  50.  
  51. HashTable::HashTable(uint ACapacity=256)
  52. {
  53.   KeyAgain = kaEnable;
  54.   SetLength(FTab, FCapacity);
  55. }
  56.  
  57. HashTable::~HashTable()
  58. {
  59.    Clear();
  60. }
  61.  
  62.  
  63. uint H(byte*p, uint ByteCount: LongWord);
  64. {
  65.   result = 0xFFFFFFFF;
  66.   if (ByteCount > 0)
  67.     for (int i = 0; i<ByteCount; i++)
  68.     {
  69.       // new processors fast multiplies by constant
  70.       result = (result ^ p*) * 134775813 + 1;
  71.       p++;
  72.     }
  73. }
  74.  
  75. void HashTable::Put(K key, V value)
  76. {
  77.   uint hash := FHashFunc(key);
  78.   int bucketIdx := hash % Capacity;
  79.   HashItem* item;
  80.   new item;
  81.   item.hash = hash;
  82.   item.key = key;
  83.   item.value = value;
  84.   item.next = FTab[bucketIdx];
  85.   FTab[bucketIdx] = item;
  86. }
  87.  
  88. bool HashTable::RemoveKey(K key)
  89. {
  90.   result = false;
  91.   uint hash = FHashFunc(key);
  92.   int bucketIdx = hash % Capacity;
  93.   HashItem* item = FTab[bucketIdx];
  94.   HashItem* prevItem = null;
  95.   while (item != null)
  96.   {
  97.     if ((item.hash == hash) && (item.key == key))
  98.     {
  99.       HashItem* nextItem = item.next;
  100.       if (prevItem==null)
  101.         FTab[bucketIdx] = item.next;
  102.       else
  103.         prevItem.next = item.next;
  104.       delete item;
  105.       item = nextItem;
  106.       result = true;
  107.     }
  108.     else
  109.     {
  110.       prevItem = item;
  111.       item = item.next;
  112.     }
  113.   }
  114.   if (result) FSize--;
  115.   item = FTab[bucketIdx];
  116.   while (item != null)
  117.   {
  118.     Assert(item.key != key);
  119.     item = item.next;
  120.   }
  121. }
  122.  
  123. HashItem* HashTable::Get(K key)
  124. {
  125.   uint hash = FHashFunc(key);
  126.   bucketIdx := hash % Capacity;
  127.   result := FTab[bucketIdx];
  128.   while (result != nil)
  129.   {
  130.     if ((result.hash == hash) && (result.key == key))
  131.       return;
  132.     result := result.next;
  133.   }
  134. }
  135.  
  136. HashItem* HashTableII::GetPrior(HashItem* item)
  137. {
  138.   uint hash = item.hash;
  139.   K key = item.key;
  140.   result = item.next;
  141.   while (result != nil)
  142.   {
  143.     if (result.hash == hash) and (result.key == key)
  144.       return;
  145.     result = result.next;
  146.   }
  147. }
  148. ---------------------------
  149.  
  150. HashTable<string,string> hashTable = new HashTable()
  151. hashTable.Put("abc","def");
  152. hashTable.Put("123","456");
  153. HashTable<string,string>.HashItem *item = hashTable.Get("abc");
Advertisement
Add Comment
Please, Sign In to add comment