Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.50 KB | None | 0 0
  1.  
  2. #include "HashTable.h"
  3. #include <fstream>
  4.  
  5. int HashTable::hashFunc(const line& d) const //hash function (utilizes horner's method to prevent overflow on large strings)
  6. {
  7.     int hashVal=0,asc;
  8.   std::string s = d.name;
  9.     for(int i=0;i<s.size();i++)
  10.     {
  11.         asc=s[i]>96?s[i]-96:s[i]-64;
  12.         hashVal=(hashVal*32+asc)%arrSize;
  13.     }
  14.     return hashVal;
  15. }
  16.  
  17. int HashTable::getPrime(int n) const //return the smallest prime number >= 2*n
  18. {
  19.     int i=2*n;
  20.     while(!isPrime(i))
  21.         i++;
  22.     return i;
  23. }
  24.  
  25. bool HashTable::isPrime(int n) const //check whether n is prime, helper function for getPrime()
  26. {
  27.     bool isPrime=true;
  28.         for(int count=2;count<n && isPrime; count++)
  29.             if(n%count==0)
  30.                 isPrime=false;
  31.     return isPrime;
  32. }
  33.  
  34. void HashTable::deepCopy(const HashTable& h)
  35. {
  36.     if(h.arr!=NULL)
  37.     {
  38.         numOfItems=h.size();
  39.         arrSize=h.maxSize();
  40.         arr=new LinkedList[arrSize];
  41.         for(int i=0;i<arrSize;i++)
  42.             arr[i]=h.arr[i];
  43.     }
  44. }
  45.  
  46. std::vector<line> HashTable::get() const //returns a vector of all the lines in the hash table
  47. {
  48.     std::vector<line> v,tmp_v;
  49.     for(int i=0;i<maxSize();i++)
  50.     {
  51.         tmp_v=arr[i].get();
  52.         for(int j=0;j<tmp_v.size();j++)
  53.             v.push_back(tmp_v[j]);
  54.     }
  55.     return v;
  56. }
  57.  
  58. HashTable::HashTable() //default constructor
  59. {
  60.     arrSize=101;
  61.     arr=new LinkedList[arrSize];
  62.     numOfItems=0;
  63. }
  64.  
  65. HashTable::HashTable(int n) //creates a hash table to store n items where the size of the array is the smallest prime number >= 2*n
  66. {
  67.     arrSize=getPrime(n);
  68.     arr=new LinkedList[arrSize];
  69.     numOfItems=0;
  70. }
  71.  
  72.  
  73. HashTable::HashTable(const HashTable& h) //copy constructor
  74. {
  75.     deepCopy(h);
  76. }
  77.  
  78.  
  79. HashTable::~HashTable() //destructor
  80. {
  81.     delete[] arr;
  82. }
  83.  
  84. HashTable& HashTable::operator=(const HashTable& h) //assignment operator
  85. {
  86.     if(this!=&h)
  87.     {
  88.         if(h.arr!=NULL)
  89.             delete[] arr;
  90.         deepCopy(h);
  91.     }
  92.     return *this;
  93. }
  94.  
  95. bool HashTable::insert(const line& s) //inserts line s if it doesn't exist in the hash table and returns 1 if insertion successful, 0 otherwise
  96. {
  97.     int hash=hashFunc(s);
  98.     bool successOrFail=arr[hash].insert(s);
  99.     numOfItems++;
  100.     return successOrFail;
  101. }
  102.  
  103. bool HashTable::remove(const line& s) //removes line s if s exist in the hash table and returns 1 if removal successful, 0 otherwise
  104. {
  105.     int hash=hashFunc(s);
  106.     bool successOrFail=arr[hash].remove(s);
  107.     numOfItems--;
  108.     return successOrFail;
  109. }
  110.  
  111. bool HashTable::search(const line& s) const //returns 1 if s exist in the hash table, 0 otherwise
  112. {
  113.     int hash=hashFunc(s);
  114.     bool found=arr[hash].search(s);
  115.     return found;
  116. }
  117.  
  118.  
  119. int HashTable::size() const //returns numOfItems
  120. {
  121.     return numOfItems;
  122. }
  123.  
  124. int HashTable::maxSize() const //returns arrSize
  125. {
  126.     return arrSize;
  127. }
  128.  
  129. double HashTable::loadFactor() const //returns the load factor of the hash table
  130. {
  131.     return (numOfItems*1.0)/arrSize;
  132. }
  133.  
  134. void HashTable::print() const
  135. {
  136.         for(int i=0;i<arrSize;i++)
  137.         {
  138.                 if(arr[i].front != NULL)
  139.                 {
  140.                         arr[i].printList();
  141.                 }
  142.         }
  143. }
  144. void HashTable::HashTableToFile(const std::string &filename_items) const
  145. {
  146.         std::ofstream ofs(filename_items);
  147.         for(int i=0;i<arrSize;i++)
  148.         {
  149.                 if(arr[i].front != NULL)
  150.                 {
  151.                         ofs << arr[i];
  152.                 }
  153.         }
  154. }
  155.  
  156. std::vector<line> HashTable::intersection(const HashTable& h) const //returns a vector of line containing intersection of calling object's data and h's data
  157. {
  158.     std::vector<line> ret_v;
  159.     std::vector<line> v=this->get();
  160.     for(int i=0;i<v.size();i++)
  161.         if(h.search(v[i]))
  162.             ret_v.push_back(v[i]);
  163.     return ret_v;
  164. }
  165.  
  166. std::vector<line> HashTable::unions(const HashTable& h) const //returns a vector of line containing union of calling object's data and h's data
  167. {
  168.     std::vector<line> ret_v;
  169.     std::vector<line> v1=this->get();
  170.     std::vector<line> v2=h.get();
  171.     for(int i=0;i<v2.size();i++) //push_back all h elements
  172.         ret_v.push_back(v2[i]);
  173.     for(int i=0;i<v1.size();i++) //push_back caller elements that are not found in h
  174.         if(!h.search(v1[i]))
  175.             ret_v.push_back(v1[i]);
  176.     return ret_v;
  177. }
  178.  
  179. std::vector<line> HashTable::difference(const HashTable& h) const //returns a vector of line containing set difference of calling object's data and h's data
  180. {
  181.         std::vector<line> ret_v;
  182.     std::vector<line> v1=this->get();
  183.     std::vector<line> v2=h.get();
  184.     for(int i=0;i<v1.size();i++) //push_back caller's elements that are not found in h
  185.         if(!h.search(v1[i]))
  186.             ret_v.push_back(v1[i]);
  187.     for(int i=0;i<v2.size();i++) //push_back h's elements that are not found in caller
  188.         if(!this->search(v2[i]))
  189.             ret_v.push_back(v2[i]);
  190.     return ret_v;
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement