Last active
April 2, 2022 00:47
-
-
Save aozturk/2368896 to your computer and use it in GitHub Desktop.
Revisions
-
aozturk revised this gist
Nov 6, 2013 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -22,8 +22,6 @@ class HashMap { delete [] table; } bool get(const K &key, V &value) { unsigned long hashValue = hashFunc(key); HashNode<K, V> *entry = table[hashValue]; -
aozturk renamed this gist
Nov 6, 2013 . 1 changed file with 0 additions and 51 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,54 +1,3 @@ // Hash map class template template <typename K, typename V, typename F = KeyHash<K>> class HashMap { -
Abdullah Ozturk revised this gist
Nov 6, 2013 . 1 changed file with 60 additions and 45 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,25 +1,26 @@ // Author: Abdullah Ozturk // Description: Simple Hash Map implementation in C++ // Configurable table size constant const int TABLE_SIZE = 128; // Hash node class template template <typename K, typename V> class HashNode { public: HashNode(const K &key, const V &value) : key(key), value(value), next(NULL) { } K getKey() const { return key; } V getValue() const { return value; } void setValue(V value) { HashNode::value = value; } @@ -32,91 +33,104 @@ class HashNode { } private: // key-value pair K key; V value; // next bucket HashNode *next; }; // Default hash function class template <typename K> struct KeyHash { unsigned long operator()(const K& key) const { return reinterpret_cast<unsigned long>(key) % TABLE_SIZE; } }; // Hash map class template template <typename K, typename V, typename F = KeyHash<K>> class HashMap { public: HashMap() { // construct zero initialized hash table of size table = new HashNode<K, V> *[TABLE_SIZE](); } ~HashMap() { // destroy all buckets one by one for (int i = 0; i < TABLE_SIZE; ++i) { HashNode<K, V> *entry = table[i]; while (entry != NULL) { HashNode<K, V> *prev = entry; entry = entry->getNext(); delete prev; } table[i] = NULL; } // destroy the hash table delete [] table; } bool get(const K &key, V &value) { unsigned long hashValue = hashFunc(key); HashNode<K, V> *entry = table[hashValue]; while (entry != NULL) { if (entry->getKey() == key) { value = entry->getValue(); return true; } entry = entry->getNext(); } return false; } void put(const K &key, const V &value) { unsigned long hashValue = hashFunc(key); HashNode<K, V> *prev = NULL; HashNode<K, V> *entry = table[hashValue]; while (entry != NULL && entry->getKey() != key) { prev = entry; entry = entry->getNext(); } if (entry == NULL) { entry = new HashNode<K, V>(key, value); if (prev == NULL) { // insert as first bucket table[hashValue] = entry; } else { prev->setNext(entry); } } else { // just update the value entry->setValue(value); } } void remove(const K &key) { unsigned long hashValue = hashFunc(key); HashNode<K, V> *prev = NULL; HashNode<K, V> *entry = table[hashValue]; while (entry != NULL && entry->getKey() != key) { prev = entry; entry = entry->getNext(); } if (entry == NULL) { // key not found return; } else { if (prev == NULL) { // remove first bucket of the list table[hashValue] = entry->getNext(); } else { prev->setNext(entry->getNext()); } @@ -125,6 +139,7 @@ class HashMap { } private: // hash table HashNode<K, V> **table; F hashFunc; }; -
Abdullah Ozturk revised this gist
Nov 6, 2013 . 1 changed file with 53 additions and 49 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ // Author: Abdullah Ozturk // Description: Very simple yet complete Hash Map implementation in C++ // Configurable table size constant const int TABLE_SIZE = 128; @@ -8,119 +8,123 @@ const int TABLE_SIZE = 128; class HashNode { public: HashNode(int key, int value) : key(key), value(value), next(NULL) { } int getKey() const { return key; } int getValue() const { return value; } void setValue(int value) { HashNode::value = value; } HashNode *getNext() const { return next; } void setNext(HashNode *next) { HashNode::next = next; } private: // Key-value pair int key; int value; HashNode* next; }; // Hash map class class HashMap { public: HashMap() { table = new HashNode*[TABLE_SIZE]; for (int i = 0; i < TABLE_SIZE; ++i) table[i] = NULL; } ~HashMap() { for (int i = 0; i < TABLE_SIZE; ++i) { HashNode* entry = table[i]; while (entry != NULL) { HashNode* prev = entry; entry = entry->getNext(); delete prev; } } delete [] table; } // Should be optimized according to specific needs int HashFunc(int key) { return key % TABLE_SIZE; } int get(int key) { int hash_val = HashFunc(key); HashNode* entry = table[hash_val]; while (entry != NULL) { if (entry->getKey() == key) { return entry->getValue(); } entry = entry->getNext(); } return -1; } void put(int key, int value) { int hash_val = HashFunc(key); HashNode* prev = NULL; HashNode* entry = table[hash_val]; while (entry != NULL && entry->getKey() != key) { prev = entry; entry = entry->getNext(); } if (entry == NULL) { entry = new HashNode(key, value); if (prev == NULL) { table[hash_val] = entry; } else { prev->setNext(entry); } } else { entry->setValue(value); } } void remove(int key) { int hash_val = HashFunc(key); HashNode* entry = table[hash_val]; HashNode* prev = NULL; while (entry != NULL) { if (entry->getKey() == key) { break; } prev = entry; entry = entry->getNext(); } if (entry == NULL) return; else { if (prev == NULL) { table[hash_val] = entry->getNext(); } else { prev->setNext(entry->getNext()); } delete entry; } } private: // Hash table HashNode** table; }; -
aozturk revised this gist
Aug 11, 2013 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -52,6 +52,8 @@ class HashMap { delete prev; } } delete [] _table; } // Should be optimized according to specific needs -
aozturk revised this gist
Apr 12, 2012 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,6 @@ // Very simple yet complete Hash Map implementation in C++ // // Configurable table size constant const int TABLE_SIZE = 128; -
aozturk revised this gist
Apr 12, 2012 . 1 changed file with 96 additions and 96 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,118 +4,118 @@ const int TABLE_SIZE = 128; // Hash node class class HashNode { public: HashNode(int key, int value) : _key(key), _value(value), _next(0) { } int key() { return _key; } int value() { return _value; } HashNode* next() { return _next; } void set_value(int value) { _value = value; } void set_next(HashNode* next) { _next = next; } private: // Key-value pair int _key; int _value; HashNode* _next; }; // Hash map class class HashMap { public: HashMap() { _table = new HashNode*[TABLE_SIZE]; for (int i = 0; i < TABLE_SIZE; ++i) _table[i] = 0; } ~HashMap() { for (int i = 0; i < TABLE_SIZE; ++i) { HashNode* entry = _table[i]; while (entry != 0) { HashNode* prev = entry; entry = entry->next(); delete prev; } } } // Should be optimized according to specific needs int HashFunc(int key) { return key % TABLE_SIZE; } int Get(int key) { int hash_val = HashFunc(key); HashNode* entry = _table[hash_val]; while (entry != 0) { if (entry->key() == key) { return entry->value(); } entry = entry->next(); } return -1; } void Put(int key, int value) { int hash_val = HashFunc(key); HashNode* prev = 0; HashNode* entry = _table[hash_val]; while (entry != 0 && entry->key() != key) { prev = entry; entry = entry->next(); } if (entry == 0) { entry = new HashNode(key, value); if (prev == 0) { _table[hash_val] = entry; } else { prev->set_next(entry); } } else { entry->set_value(value); } } void Remove(int key) { int hash_val = HashFunc(key); HashNode* entry = _table[hash_val]; HashNode* prev = 0; while (entry != 0) { if (entry->key() == key) { break; } prev = entry; entry = entry->next(); } if (entry == 0) return; else { if (prev == 0) { _table[hash_val] = entry->next(); } else { prev->set_next(entry->next()); } delete entry; } } private: // Hash table HashNode** _table; }; -
aozturk created this gist
Apr 12, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,121 @@ // Configurable table size constant const int TABLE_SIZE = 128; // Hash node class class HashNode { public: HashNode(int key, int value) : _key(key), _value(value), _next(0) { } int key() { return _key; } int value() { return _value; } HashNode* next() { return _next; } void set_value(int value) { _value = value; } void set_next(HashNode* next) { _next = next; } private: // Key-value pair int _key; int _value; HashNode* _next; }; // Hash map class class HashMap { public: HashMap() { _table = new HashNode*[TABLE_SIZE]; for (int i = 0; i < TABLE_SIZE; ++i) _table[i] = 0; } ~HashMap() { for (int i = 0; i < TABLE_SIZE; ++i) { HashNode* entry = _table[i]; while (entry != 0) { HashNode* prev = entry; entry = entry->next(); delete prev; } } } // Should be optimized according to specific needs int HashFunc(int key) { return key % TABLE_SIZE; } int Get(int key) { int hash_val = HashFunc(key); HashNode* entry = _table[hash_val]; while (entry != 0) { if (entry->key() == key) { return entry->value(); } entry = entry->next(); } return -1; } void Put(int key, int value) { int hash_val = HashFunc(key); HashNode* prev = 0; HashNode* entry = _table[hash_val]; while (entry != 0 && entry->key() != key) { prev = entry; entry = entry->next(); } if (entry == 0) { entry = new HashNode(key, value); if (prev == 0) { _table[hash_val] = entry; } else { prev->set_next(entry); } } else { entry->set_value(value); } } void Remove(int key) { int hash_val = HashFunc(key); HashNode* entry = _table[hash_val]; HashNode* prev = 0; while (entry != 0) { if (entry->key() == key) { break; } prev = entry; entry = entry->next(); } if (entry == 0) return; else { if (prev == 0) { _table[hash_val] = entry->next(); } else { prev->set_next(entry->next()); } delete entry; } } private: // Hash table HashNode** _table; };