Advertisement
Guest User

HashTable M4

a guest
Nov 15th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.70 KB | None | 0 0
  1. #ifndef MY_HASH_TABLE
  2. #define MY_HASH_TABLE
  3.  
  4. #include "HashNode.h"
  5. #include <vector>
  6. #include <list>
  7. #include <iostream>
  8.  
  9. using namespace std;
  10.  
  11. enum HashTableError { OUT_OF_MEMORY, KEY_NOT_FOUND, DUPLICATE_KEY }; // extend if necessary
  12.  
  13. typedef unsigned long ulint;
  14.  
  15. class HashTable {
  16.   typedef vector <list<HashNode> > Table;
  17.   Table *table; // size of table is stored in the Table data structure
  18.   size_t num;   // number of entries in the HashTable;
  19.  
  20. public:
  21.   HashTable();       // constructor, initializes table of size 1;
  22.   HashTable(size_t); // constructor, requires size of table as arg
  23.   ~HashTable();      // deconstructor
  24.  
  25.   size_t size(); // returns size of the hash table (number of buckets)
  26.   size_t hash_function(ulint);  // the table's hash function
  27.   ulint getValue(ulint);    // find and return data associated with key
  28.  
  29.   void insert(ulint,ulint); // insert data associated with key into table
  30.   void erase(ulint);        // remove key and associated data from table
  31.  
  32.   void rehash(size_t); // sets a new size for the hash table, rehashes the hash table
  33.  
  34.   // extend if necessary
  35. };
  36.  
  37. HashTable::HashTable() {
  38.  
  39.   try
  40.   {
  41.      table = new (Table);
  42.      table->resize(11);
  43.      num = 0;
  44.      
  45.   }
  46.   catch(std::bad_alloc& e)
  47.   {
  48.       cout<<e.what()<<endl;
  49.       throw HashTableError (OUT_OF_MEMORY);
  50.   }
  51.  
  52. }
  53.  
  54. HashTable::HashTable (size_t size) {
  55.  
  56.   try
  57.   {
  58.      table = new (Table);
  59.      table->resize(size);
  60.      num = 0;
  61.      
  62.   }
  63.   catch(std::bad_alloc& e)
  64.   {
  65.       cout<<e.what()<<endl;
  66.       throw HashTableError (OUT_OF_MEMORY);
  67.   }
  68.  
  69. }
  70.  
  71. HashTable::~HashTable() {
  72.   delete(table);
  73. }
  74.  
  75. size_t HashTable::hash_function(ulint key) {
  76.   return key % table->size();
  77. }
  78.  
  79. void HashTable::insert(ulint key,ulint value) {
  80.   try
  81.   {
  82.     getValue(key);
  83.     throw HashTableError (DUPLICATE_KEY);
  84.   }
  85.   catch(HashTableError &e)
  86.   {
  87.     ulint index = hash_function(key);
  88.     HashNode node = HashNode();
  89.     node.assign(key, value);
  90.     table->at(index).push_back(node)
  91.     num = num + 1;
  92.  
  93.     if((num / static_cast<float>(size())) > 0.9)
  94.     {
  95.     rehash(2 * size());
  96.     }
  97.   }
  98.  
  99.  
  100. }
  101.  
  102. ulint HashTable::getValue(ulint key) {
  103.  
  104.   ulint index = hash_function(key);
  105.   list<HashNode> nodeList = table->at(index);
  106.   list<HashNode>::iterator it;
  107.   for  (it = nodeList.begin(); it != nodeList.end(); ++it) {
  108.    
  109.     if (it->getKey() == key)
  110.     {
  111.       return it->getValue();
  112.     }
  113.   }
  114.  
  115.   throw HashTableError (KEY NOT FOUND);
  116.  
  117.  
  118.  
  119. }
  120.  
  121. size_t HashTable::size() {
  122.  
  123.   return table->size();
  124. }
  125.  
  126. void HashTable::erase(ulint key) {
  127.  
  128.    try
  129.   {
  130.     getValue(key);
  131.     throw HashTableError (DUPLICATE_KEY);
  132.  
  133.   }
  134.   catch(HashTableError &e)
  135.   {
  136.     ulint index = hash_function(key);
  137.     list<HashNode> *nodeList = &table->at(index);
  138.     list<HashNode>::iterator it;
  139.  
  140.     for  (it = nodeList->begin(); it != nodeList->end(); ++it) {
  141.  
  142.       if (it->getKey() == key)
  143.       {
  144.         nodeList->erase(it)
  145.         num--;
  146.         break;
  147.       }
  148.  
  149.     }
  150.  
  151.   }
  152. }
  153.  
  154. void HashTable::rehash(size_t num) {
  155.  
  156.   try
  157.   {
  158.     Table tmp = *table;
  159.     table->clear();
  160.     table->resize(num);
  161.     vector<list<HashNode>>::iterator it;
  162.     for  (it = tmp.begin(); it != tmp.end(); ++it) {
  163.    
  164.       list<HashNode> nodes = *it;
  165.       list<HashNode>::iterator it2;
  166.       for (it2 = node.begin(); it2 != node.end(); it2++)
  167.       {
  168.         insert(it2->getKey(), it2->getValue())
  169.       }
  170.  
  171.  
  172.   }
  173.     catch(std::bad_alloc& e)
  174.     {
  175.       cout<<e.what()<<endl;
  176.       throw HashTableError(OUT_OF_MEMORY);
  177.     }
  178.   }
  179. }
  180.  
  181.  
  182.  
  183. /* Implement the
  184. - Constructors, Destructor
  185. - hash_function, insert, getValue methods
  186. - erase, and rehash methods
  187. */
  188.  
  189. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement