Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.79 KB | None | 0 0
  1. ////////////////////////////////////////// hashtable cpp ////////////
  2.  
  3. #include "HashTable.h"
  4. #include <fstream>
  5.  
  6. int HashTable::hashFunc(const line& d) const //hash function (utilizes horner's method to prevent overflow on large strings)
  7. {
  8.     int hashVal=0,asc;
  9.   std::string s = d.name;
  10.     for(int i=0;i<s.size();i++)
  11.     {
  12.         asc=s[i]>96?s[i]-96:s[i]-64;
  13.         hashVal=(hashVal*32+asc)%arrSize;
  14.     }
  15.     return hashVal;
  16. }
  17.  
  18. int HashTable::getPrime(int n) const //return the smallest prime number >= 2*n
  19. {
  20.     int i=2*n;
  21.     while(!isPrime(i))
  22.         i++;
  23.     return i;
  24. }
  25.  
  26. bool HashTable::isPrime(int n) const //check whether n is prime, helper function for getPrime()
  27. {
  28.     bool isPrime=true;
  29.         for(int count=2;count<n && isPrime; count++)
  30.             if(n%count==0)
  31.                 isPrime=false;
  32.     return isPrime;
  33. }
  34.  
  35. void HashTable::deepCopy(const HashTable& h)
  36. {
  37.     if(h.arr!=NULL)
  38.     {
  39.         numOfItems=h.size();
  40.         arrSize=h.maxSize();
  41.         arr=new LinkedList[arrSize];
  42.         for(int i=0;i<arrSize;i++)
  43.             arr[i]=h.arr[i];
  44.     }
  45. }
  46.  
  47. std::vector<line> HashTable::get() const //returns a vector of all the lines in the hash table
  48. {
  49.     std::vector<line> v,tmp_v;
  50.     for(int i=0;i<maxSize();i++)
  51.     {
  52.         tmp_v=arr[i].get();
  53.         for(int j=0;j<tmp_v.size();j++)
  54.             v.push_back(tmp_v[j]);
  55.     }
  56.     return v;
  57. }
  58.  
  59. HashTable::HashTable() //default constructor
  60. {
  61.     arrSize=101;
  62.     arr=new LinkedList[arrSize];
  63.     numOfItems=0;
  64. }
  65.  
  66. 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
  67. {
  68.     arrSize=getPrime(n);
  69.     arr=new LinkedList[arrSize];
  70.     numOfItems=0;
  71. }
  72.  
  73.  
  74. HashTable::HashTable(const HashTable& h) //copy constructor
  75. {
  76.     deepCopy(h);
  77. }
  78.  
  79.  
  80. HashTable::~HashTable() //destructor
  81. {
  82.     delete[] arr;
  83. }
  84.  
  85. HashTable& HashTable::operator=(const HashTable& h) //assignment operator
  86. {
  87.     if(this!=&h)
  88.     {
  89.         if(h.arr!=NULL)
  90.             delete[] arr;
  91.         deepCopy(h);
  92.     }
  93.     return *this;
  94. }
  95.  
  96. 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
  97. {
  98.     int hash=hashFunc(s);
  99.     bool successOrFail=arr[hash].insert(s);
  100.     numOfItems++;
  101.     return successOrFail;
  102. }
  103.  
  104. bool HashTable::remove(const line& s) //removes line s if s exist in the hash table and returns 1 if removal successful, 0 otherwise
  105. {
  106.     int hash=hashFunc(s);
  107.     bool successOrFail=arr[hash].remove(s);
  108.     numOfItems--;
  109.     return successOrFail;
  110. }
  111.  
  112. bool HashTable::search(const line& s) const //returns 1 if s exist in the hash table, 0 otherwise
  113. {
  114.     int hash=hashFunc(s);
  115.     bool found=arr[hash].search(s);
  116.     return found;
  117. }
  118.  
  119.  
  120. int HashTable::size() const //returns numOfItems
  121. {
  122.     return numOfItems;
  123. }
  124.  
  125. int HashTable::maxSize() const //returns arrSize
  126. {
  127.     return arrSize;
  128. }
  129.  
  130. double HashTable::loadFactor() const //returns the load factor of the hash table
  131. {
  132.     return (numOfItems*1.0)/arrSize;
  133. }
  134.  
  135. void HashTable::print() const
  136. {
  137.         for(int i=0;i<arrSize;i++)
  138.         {
  139.                 if(arr[i].front != NULL)
  140.                 {
  141.                         arr[i].printList();
  142.                 }
  143.         }
  144. }
  145. void HashTable::HashTableToFile(const std::string &filename_items) const
  146. {
  147.         std::ofstream ofs(filename_items);
  148.         for(int i=0;i<arrSize;i++)
  149.         {
  150.                 if(arr[i].front != NULL)
  151.                 {
  152.                         ofs << arr[i];
  153.                 }
  154.         }
  155. }
  156.  
  157. 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
  158. {
  159.     std::vector<line> ret_v;
  160.     std::vector<line> v=this->get();
  161.     for(int i=0;i<v.size();i++)
  162.         if(h.search(v[i]))
  163.             ret_v.push_back(v[i]);
  164.     return ret_v;
  165. }
  166.  
  167. 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
  168. {
  169.     std::vector<line> ret_v;
  170.     std::vector<line> v1=this->get();
  171.     std::vector<line> v2=h.get();
  172.     for(int i=0;i<v2.size();i++) //push_back all h elements
  173.         ret_v.push_back(v2[i]);
  174.     for(int i=0;i<v1.size();i++) //push_back caller elements that are not found in h
  175.         if(!h.search(v1[i]))
  176.             ret_v.push_back(v1[i]);
  177.     return ret_v;
  178. }
  179.  
  180. 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
  181. {
  182.         std::vector<line> ret_v;
  183.     std::vector<line> v1=this->get();
  184.     std::vector<line> v2=h.get();
  185.     for(int i=0;i<v1.size();i++) //push_back caller's elements that are not found in h
  186.         if(!h.search(v1[i]))
  187.             ret_v.push_back(v1[i]);
  188.     for(int i=0;i<v2.size();i++) //push_back h's elements that are not found in caller
  189.         if(!this->search(v2[i]))
  190.             ret_v.push_back(v2[i]);
  191.     return ret_v;
  192. }
  193.  
  194.  
  195.  
  196. /////////////////////////////hashtable h////////////////////////
  197. #ifndef HASHTABLE_H_INCLUDED
  198. #define HASHTABLE_H_INCLUDED
  199.  
  200. #include "LinkedList.h"
  201.  
  202. class HashTable
  203. {
  204. public:
  205.     HashTable(); //default constructor
  206.     HashTable(int); //one parameter constructor
  207.     HashTable(const HashTable&); //copy constructor
  208.     ~HashTable(); //destructor
  209.     HashTable& operator=(const HashTable&); //assignment operator
  210.     bool insert(const line&);
  211.     bool remove(const line&);
  212.     bool search(const line&) const;
  213.     int size() const; //return numOfItems
  214.     int maxSize() const; //return arrSize
  215.     double loadFactor() const;
  216.     std::vector<line> intersection(const HashTable&) const;
  217.     std::vector<line> unions(const HashTable&) const;
  218.     std::vector<line> difference(const HashTable&) const;
  219.     void HashTableToFile(const std::string &filename_items) const;
  220.     void print() const;
  221.  
  222. private:
  223.     LinkedList* arr;
  224.     int arrSize;
  225.     int numOfItems;
  226.     int hashFunc(const line&) const;
  227.     int getPrime(int) const;
  228.     bool isPrime(int) const;
  229.     void deepCopy(const HashTable& h);
  230.     std::vector<line> get() const; //returns a vector of all the lines in the HashTable
  231. };
  232. #endif
  233.  
  234.  
  235.  
  236. ///////////////////////////////////////////LinkedList cpp //////////////////////////
  237.  
  238. #include "LinkedList.h"
  239. #include <iostream>
  240.  
  241. LinkedList::LinkedList() //default constructor
  242. {
  243.     front=NULL;
  244. }
  245.  
  246. LinkedList::LinkedList(const LinkedList& ls) //copy constructor
  247. {
  248.     deepCopy(ls);
  249. }
  250.  
  251. LinkedList::~LinkedList() //destructor
  252. {
  253.     deleteList();
  254. }
  255.  
  256. LinkedList& LinkedList::operator=(const LinkedList& ls) //assignment operator
  257. {
  258.     if(this!=&ls)
  259.     {
  260.         deleteList();
  261.         deepCopy(ls);
  262.     }
  263.     return *this;
  264. }
  265.  
  266. bool LinkedList::insert(const line& s)
  267. {
  268.         Node* temp=front;
  269.         while(temp!=NULL) //Traverse list
  270.         {
  271.             if(temp->data.name == s.name)
  272.             {
  273.                     temp->data.quant+=s.quant;
  274.                     return true;
  275.             }
  276.             temp = temp->next;
  277.         }
  278.         front=new Node(s,front);
  279.         return true;
  280. }
  281.  
  282. bool LinkedList::remove(const line& s)
  283. {
  284.     Node* temp=front;
  285.     if(temp==NULL) //list is empty
  286.         return false;
  287.     if(temp->data==s) //s is first string in list
  288.     {
  289.         front=temp->next;
  290.         delete temp;
  291.         return true;
  292.     }
  293.     else
  294.     {
  295.         while(temp->next!=NULL){
  296.             if(temp->next->data==s)
  297.             {
  298.                 Node* deletedNode=temp->next;
  299.                 temp->next=temp->next->next;
  300.                 delete deletedNode;
  301.                 return true;
  302.             }
  303.             temp=temp->next;
  304.         }
  305.         return false;
  306.     }
  307. }
  308.  
  309. void  LinkedList::printList() const
  310. {
  311.     Node* temp=front;
  312.     while(temp!=NULL)
  313.     {
  314.         std::cout << temp->data.name << " v kolichesve " <<  temp->data.quant <<  std::endl;
  315.         temp = temp->next;
  316.     }
  317. }
  318.  
  319. std::ostream& operator<< (std::ostream &out, const LinkedList &p)
  320. {
  321.         LinkedList::Node* temp=p.front;
  322.         while(temp!=NULL)
  323.         {
  324.                 out << temp->data.name <<", " <<  temp->data.quant <<  std::endl;
  325.                 temp = temp->next;
  326.             }
  327.  
  328.     return out;
  329. }
  330.  
  331. bool LinkedList::search(const line& s) const
  332. {
  333.     Node* temp=front;
  334.     while(temp!=NULL) //Traverse list
  335.     {
  336.         if(temp->data.name ==s.name)
  337.             return true;
  338.         temp = temp->next;
  339.     }
  340.     return false;
  341. }
  342.  
  343. std::vector<line> LinkedList::get() const
  344. {
  345.     std::vector<line> str_vec;
  346.     for(Node* temp=front;temp!=NULL;temp=temp->next)
  347.         str_vec.push_back(temp->data);
  348.     return str_vec;
  349. }
  350.  
  351. void LinkedList::deepCopy(const LinkedList& ls)
  352. {
  353.     front=NULL;
  354.     if(ls.front!=NULL) //Only copy if ls is non-empty
  355.     {
  356.         Node* original=ls.front;
  357.         Node* copy;
  358.         copy=new Node(original->data, NULL);
  359.         front=copy;
  360.         original=original->next;
  361.         while(original!=NULL) //Traverse the original copying each node
  362.         {
  363.             copy->next=new Node(original->data, NULL);
  364.             copy=copy->next;
  365.             original=original->next;
  366.         }
  367.     }
  368. }
  369.  
  370.  
  371. void LinkedList::deleteList()
  372. {
  373.     Node* tmp;
  374.     while(front!=NULL){
  375.         tmp=front->next;
  376.         delete front;
  377.         front=tmp;
  378.     }
  379. }
  380.  
  381.  
  382. //////////////////////////////linkedlist h///////////////////////////////
  383. #ifndef LINKEDLIST_H_INCLUDED
  384. #define LINKEDLIST_H_INCLUDED
  385.  
  386. #include <cstdlib>
  387. #include <vector>
  388. #include <string>
  389.  
  390.  
  391. class line
  392. {
  393. public:
  394.   std::string name;
  395.   int quant;
  396.   line(){}
  397.   line(const std::string namedet,int x)
  398.   {
  399.       name = namedet;
  400.       quant = x;
  401.   }
  402.   line(const line &rhs)
  403.   {
  404.       name = rhs.name;
  405.       quant = rhs.quant;
  406.   }
  407.   line& operator=(const line& rhs)
  408.   {
  409.       name = rhs.name;
  410.       quant = rhs.quant;
  411.       return *this;
  412.   }
  413.   bool  operator==(const line &rhs)
  414.   {
  415.       return name == rhs.name;
  416.   }
  417. };
  418.  
  419. class LinkedList
  420. {
  421. public:
  422.     LinkedList(); //default constructor
  423.     LinkedList(const LinkedList& ls);//copy constructor
  424.     ~LinkedList(); //destructor
  425.     LinkedList& operator=(const LinkedList&); //assignment operator
  426.     bool insert(const line&);
  427.     bool remove(const line&);
  428.     bool search(const line&) const;
  429.     std::vector<line> get() const;
  430.   void printList() const;
  431.  
  432. private:
  433.     class Node
  434.     {
  435.     public:
  436.         line data;
  437.         Node* next;
  438.         Node(line s)
  439.     {
  440.         data = s;
  441.         next = NULL;
  442.     }
  443.         Node(line s,Node* nd)
  444.     {
  445.         data = s;
  446.         next = nd;
  447.     }
  448.     };
  449.     Node* front;
  450.     void deepCopy(const LinkedList& ls);
  451.     void deleteList();
  452.   friend class HashTable;
  453.   friend std::ostream& operator<< (std::ostream &out, const LinkedList &point);
  454. };
  455.  
  456. #endif
  457. /////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement