Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "Hash.h"
- int main(){
- structure<int>* str = new structure<int>;
- str->key = "qwertyuiop";
- str->value = 10;
- Hash< structure<int> >* hash0 = new Hash< structure<int> >();
- hash0->addElement(str);
- delete hash0;
- return 0;
- }
- #ifndef HASH_H
- #define HASH_H
- #include <iostream>
- template<typename H>struct structure{
- std::string key;
- H value;
- };
- template<typename T>class Hash{
- private:
- int maxSize;
- int currentSize;
- int expandRatio;
- double fillLimit;
- T hArray;
- int getHash(std::string keyArg);
- public:
- Hash();
- void addElement(const T *dataArg);
- T* getElementKey(const int &index);
- };
- template<typename T> Hash<T>::getHash(std::string keyArg){
- int q = keyArg.length();
- int ret = 0;
- for(unsigned int i=0; i<keyArg.length(); i++){
- ret += keyArg[i]*(31^q);
- q--;
- }
- return ret % maxSize;
- }
- template<typename T> Hash<T>::Hash(){
- maxSize = 4;
- currentSize = 0;
- expandRatio = 2;
- fillLimit = 0.75;
- hArray = new T[currentSize];
- for(int i=0; i<maxSize; i++) hArray[i] = nullptr;
- }
- template<typename T> void Hash<T>::addElement(const T *dataArg){
- int hashIndex = getHash(dataArg->key);
- while(hArray[hashIndex]!=nullptr && hArray[hashIndex]->key != key && hArray[hashIndex]->key != -1){
- hashIndex++;
- hashIndex %= maxSize;
- }
- }
- }
- #endif // HASH_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement