Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. //
  2. // Created by reviy on 15.12.2019.
  3. //
  4.  
  5. #ifndef KNUTH_6_4_21_TEMPLATE_HASHMAP_W_O_DELETING_H
  6. #define KNUTH_6_4_21_TEMPLATE_HASHMAP_W_O_DELETING_H
  7.  
  8.  
  9. template<typename K, typename V>
  10.  
  11. class hash_node
  12. {
  13. public:
  14. V value;
  15. K key;
  16.  
  17. hash_node(K key, V value):key(key),value(value){};
  18. };
  19.  
  20. template<typename K, typename V>
  21.  
  22. class hash_map {
  23. public:
  24. hash_node<K,V> **arr;
  25. int capacity;
  26. int size;
  27. int indexOfPrevAdded;
  28. int hash_function(K key){
  29. return key; // :)
  30. }
  31. public:
  32. hash_map(int capacity=9):capacity(capacity),size(0){
  33. arr = new hash_node<K,V>*[capacity];
  34. for(int iter = 0; iter < capacity; ++iter){
  35. arr[iter] = nullptr;
  36. }
  37. }
  38. void insert_node(K key, V value) {
  39. int hash_index = hash_function(key);
  40. int iter(hash_index);
  41. while (arr[iter] != nullptr && iter < capacity)
  42. iter++;
  43. if(iter == capacity){
  44. iter = 0;
  45. while(arr[iter] != nullptr && iter < hash_index)
  46. iter++;
  47. }
  48. arr[iter] = new hash_node<K,V>(iter, value);;
  49. size++;
  50. }
  51. void print(std::string key="key", std::string value="value"){
  52. std::cout<<"hash_map:"<<std::endl;
  53. key+=':'; value+=':';
  54. for(int iter = 0; iter < capacity; ++iter)
  55. if(arr[iter] != nullptr) {
  56. std::cout << key << arr[iter]->key << ' '
  57. << value << arr[iter]->value
  58. << std::endl;
  59. }
  60. std::cout<<std::endl;
  61. }
  62. void removeLastAddedNode(){
  63. int indexOfLast;
  64. for(indexOfLast = capacity-1; indexOfLast>0;indexOfLast--)
  65. if(arr[indexOfLast]!= nullptr)
  66. break;
  67. delete arr[indexOfLast];
  68. arr[indexOfLast]= nullptr;
  69. size--;
  70. }
  71. bool isLastEmpty(){
  72. return arr[capacity-1]==nullptr;
  73. }
  74.  
  75. };
  76.  
  77.  
  78. #endif //KNUTH_6_4_21_TEMPLATE_HASHMAP_W_O_DELETING_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement