Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Created by reviy on 15.12.2019.
- //
- #ifndef KNUTH_6_4_21_TEMPLATE_HASHMAP_W_O_DELETING_H
- #define KNUTH_6_4_21_TEMPLATE_HASHMAP_W_O_DELETING_H
- template<typename K, typename V>
- class hash_node
- {
- public:
- V value;
- K key;
- hash_node(K key, V value):key(key),value(value){};
- };
- template<typename K, typename V>
- class hash_map {
- public:
- hash_node<K,V> **arr;
- int capacity;
- int size;
- int indexOfPrevAdded;
- int hash_function(K key){
- return key; // :)
- }
- public:
- hash_map(int capacity=9):capacity(capacity),size(0){
- arr = new hash_node<K,V>*[capacity];
- for(int iter = 0; iter < capacity; ++iter){
- arr[iter] = nullptr;
- }
- }
- void insert_node(K key, V value) {
- int hash_index = hash_function(key);
- int iter(hash_index);
- while (arr[iter] != nullptr && iter < capacity)
- iter++;
- if(iter == capacity){
- iter = 0;
- while(arr[iter] != nullptr && iter < hash_index)
- iter++;
- }
- arr[iter] = new hash_node<K,V>(iter, value);;
- size++;
- }
- void print(std::string key="key", std::string value="value"){
- std::cout<<"hash_map:"<<std::endl;
- key+=':'; value+=':';
- for(int iter = 0; iter < capacity; ++iter)
- if(arr[iter] != nullptr) {
- std::cout << key << arr[iter]->key << ' '
- << value << arr[iter]->value
- << std::endl;
- }
- std::cout<<std::endl;
- }
- void removeLastAddedNode(){
- int indexOfLast;
- for(indexOfLast = capacity-1; indexOfLast>0;indexOfLast--)
- if(arr[indexOfLast]!= nullptr)
- break;
- delete arr[indexOfLast];
- arr[indexOfLast]= nullptr;
- size--;
- }
- bool isLastEmpty(){
- return arr[capacity-1]==nullptr;
- }
- };
- #endif //KNUTH_6_4_21_TEMPLATE_HASHMAP_W_O_DELETING_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement