Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template <typename T>
- class HashTable
- {
- public:
- HashTable(int amount)
- {
- size = amount;
- values = new T*[size];
- for (int i = 0; i < size; i++)
- {
- *(values + i) = nullptr;
- }
- }
- ~HashTable()
- {
- if(values != nullptr)
- {
- for (int i = 0; i < size; i++)
- {
- if(*(values + i) != nullptr)
- {
- delete *(values + i);
- *(values + i) = nullptr;
- }
- }
- delete[] values;
- }
- }
- int hash(const std::string & key)
- {
- int value = 0;
- for (unsigned int i = 0; i < key.length(); i++)
- {
- value += key[i] * (i + 1);
- }
- return value % size;
- }
- void addValue(const std::string & key, T value)
- {
- int location = hash(key);
- if (*(values + location) == nullptr)
- {
- *(values + location) = new T;
- }
- **(values + location) = value;
- }
- void printValues()
- {
- for (int i = 0; i < size; i++)
- {
- if (*(values + i) != nullptr)
- {
- printf("value : %i\n", **(values + i));
- }
- }
- }
- T* getValue(const std::string & key)
- {
- return *(values + hash(key))
- }
- int size;
- T ** values = nullptr;
- };
Advertisement
Add Comment
Please, Sign In to add comment