Advertisement
Guest User

HashTable-with-template-expanded

a guest
May 25th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1. // ConsoleApplication1.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include "pch.h"
  5. #include <iostream>
  6. #include <string>
  7.  
  8. template <typename K, typename V>
  9. struct Node
  10. {
  11.     K key;
  12.     V value;
  13.  
  14.     Node<K, V> *next;
  15.  
  16.     Node(K k, V v)
  17.     {
  18.         key = k;
  19.         value = v;
  20.         next = nullptr;
  21.     }
  22. };
  23.  
  24. template <typename K, typename V>
  25. struct HashTable
  26. {
  27.     int partitions_size;
  28.     Node<K, V> **partitions;
  29.  
  30.     HashTable(int ps)
  31.     {
  32.         partitions_size = ps;
  33.  
  34.         partitions = new Node<K, V> *[partitions_size];
  35.  
  36.         for (int i = 0; i < partitions_size; i++)
  37.         {
  38.             partitions[i] = nullptr;
  39.         }
  40.     }
  41.  
  42.     ~HashTable()
  43.     {
  44.         delete[] partitions;
  45.     }
  46.  
  47.     void insert(K key, V value)
  48.     {
  49.         int hash = hash_function(key);
  50.  
  51.         if (partitions[hash] == nullptr)
  52.         {
  53.             partitions[hash] = new Node<K, V>(key, value);
  54.         }
  55.         else
  56.         {
  57.             Node<K, V> *p = partitions[hash];
  58.  
  59.             while (p->next != nullptr)
  60.             {
  61.                 p = p->next;
  62.             }
  63.  
  64.             p->next = new Node<K, V>(key, value);
  65.         }
  66.     }
  67.  
  68.     void display()
  69.     {
  70.         int ct = 0;
  71.  
  72.         for (int i = 0; i < partitions_size; i++)
  73.         {
  74.             int c = 0;
  75.  
  76.             std::cout << "partitia " << i << ": ";
  77.  
  78.             if (partitions[i] == nullptr)
  79.             {
  80.                 std::cout << "(empty) ";
  81.             }
  82.             else
  83.             {
  84.                 Node<K, V> *p = partitions[i];
  85.  
  86.                 while (p != nullptr)
  87.                 {
  88.                     c++;
  89.  
  90.                     std::cout << "(k: " << p->key << ", v: " << p->value << ") ";
  91.  
  92.                     p = p->next;
  93.                 }
  94.  
  95.                 c = c - 1;
  96.             }
  97.  
  98.             std::cout << "- " << c << " coliziuni" << std::endl;
  99.  
  100.             ct += c;
  101.         }
  102.  
  103.         std::cout << ct << " coliziuni in total" << std::endl;
  104.     }
  105.  
  106.     int hash_function(int key)
  107.     {
  108.         return key % partitions_size;
  109.     }
  110.  
  111.     int hash_function(std::string key)
  112.     {
  113.         int hash = 0;
  114.  
  115.         for (int i = 0; i < key.length(); i++)
  116.         {
  117.             hash = hash ^ key[i];
  118.         }
  119.  
  120.         return hash % partitions_size;
  121.     }
  122. };
  123.  
  124. int main()
  125. {
  126.     HashTable<int, std::string> test1(5);
  127.  
  128.     test1.insert(1, "value 1");
  129.     test1.insert(2, "value 2");
  130.     test1.insert(3, "value 3");
  131.  
  132.     test1.display();
  133.  
  134.     HashTable<std::string, std::string> test2(5);
  135.  
  136.     test2.insert("key 1", "value 1");
  137.     test2.insert("key 2", "value 2");
  138.     test2.insert("key 3", "value 3");
  139.  
  140.     test2.display();
  141. }
  142.  
  143. // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
  144. // Debug program: F5 or Debug > Start Debugging menu
  145.  
  146. // Tips for Getting Started:
  147. //   1. Use the Solution Explorer window to add/manage files
  148. //   2. Use the Team Explorer window to connect to source control
  149. //   3. Use the Output window to see build output and other messages
  150. //   4. Use the Error List window to view errors
  151. //   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
  152. //   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement