Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. #include <iostream>
  2. #include "Hash.h"
  3.  
  4.  
  5. int main(){
  6.  
  7.     structure<int>* str = new structure<int>;
  8.  
  9.     str->key = "qwertyuiop";
  10.     str->value = 10;
  11.  
  12.     Hash< structure<int> >* hash0 = new Hash< structure<int> >();
  13.  
  14.     hash0->addElement(str);
  15.  
  16.     delete hash0;
  17.  
  18.     return 0;
  19. }
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27. #ifndef HASH_H
  28. #define HASH_H
  29.  
  30. #include <iostream>
  31.  
  32. template<typename H>struct structure{
  33.     std::string key;
  34.     H value;
  35. };
  36.  
  37.  
  38.  
  39. template<typename T>class Hash{
  40. private:
  41.     int maxSize;
  42.     int currentSize;
  43.     int expandRatio;
  44.     double fillLimit;
  45.     T hArray;
  46.     int getHash(std::string keyArg);
  47. public:
  48.     Hash();
  49.     void addElement(const T *dataArg);
  50.  
  51.     T* getElementKey(const int &index);
  52. };
  53.  
  54.  
  55. template<typename T> Hash<T>::getHash(std::string keyArg){
  56.     int q = keyArg.length();
  57.     int ret = 0;
  58.  
  59.     for(unsigned int i=0; i<keyArg.length(); i++){
  60.         ret += keyArg[i]*(31^q);
  61.         q--;
  62.     }
  63.     return ret % maxSize;
  64. }
  65.  
  66.  
  67. template<typename T> Hash<T>::Hash(){
  68.     maxSize = 4;
  69.     currentSize = 0;
  70.     expandRatio = 2;
  71.     fillLimit = 0.75;
  72.     hArray = new T[currentSize];
  73.  
  74.     for(int i=0; i<maxSize; i++) hArray[i] = nullptr;
  75. }
  76.  
  77.  
  78.  
  79. template<typename T> void Hash<T>::addElement(const T *dataArg){
  80.     int hashIndex = getHash(dataArg->key);
  81.     while(hArray[hashIndex]!=nullptr && hArray[hashIndex]->key != key && hArray[hashIndex]->key != -1){
  82.             hashIndex++;
  83.             hashIndex %= maxSize;
  84.     }
  85. }
  86.  
  87.  
  88. }
  89.  
  90.  
  91. #endif // HASH_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement