void Hash<V>::remove(string key) { if(!table[hash(key)].empty()){ // Non empty spot, no need to remove if empty bool erased = false; typename list< Entry<V> >::iterator it; for(it = table[hash(key)].begin(); it != table[hash(key)].end() && erased == false; it++) { if(it->getKey() == key) { it = table[hash(key)].erase(it); // Erase it! erased == true; } } } }
V Hash<V>::get(string key) { int hashed = hash(key); if(!table[hashed].empty()) { // If the spot in the table is not empty... typename list< Entry<V> >::iterator it; // for(it = table[hashed].begin(); it != table[hashed].end(); it++) { // it = Entry obj in list std::cout << hashed << std::endl; if(it->getKey() == key) // And the key is the same... return it->getValue(); // return the value. } } //else the key was not present in the table return (V)0; }
void Hash<V>::print() { std::cout << "[ " ; for(int i = 0; i < height; i++) { std::cout << "{ " ; if(!table[i].empty()){ typename list< Entry<V> >::iterator it; for(it = table[i].begin(); it != table[i].end(); it++) { std::cout << "(" << it->getKey() << ", " << it->getValue() << ") " ; } } std::cout << " }"; if(i+1 != height) std::cout << std::endl; } std::cout << " ]" << std::endl; }
void Hash<V>::insert(string key, V v) { if(table[hash(key)].empty()) //empty spot table[hash(key)].push_back(Entry<V>(key,v)); else{ //non-empty spot typename list< Entry<V> >::iterator it; bool inserted = false; for(it = table[hash(key)].begin(); it != table[hash(key)].end() && inserted == false; it++) { if(it->getKey() == key) { it->setValue(v); inserted = true; } } if(inserted == false) { //non-empty and non-duplicate. table[hash(key)].push_back(Entry<V>(key,v)); } } }
void HashTable<V>::remove(string k) { int hash = hashFunction(k); list<Entry<V> > &hList = vecTable[hash]; typename list<Entry<V> >::iterator it; for (it=hList.begin(); it!=hList.end(); it++) { if (it->getKey() == k) { hList.erase(it); return; } } // if no key == k in the hash table return return; }
V* HashTable<V>::find(string k) { int hash = hashFunction(k); list<Entry<V> > &hList = vecTable[hash]; typename list<Entry<V> >::iterator it; for (it=hList.begin(); it!=hList.end(); it++) { if (it->getKey() == k) { // return &(it->getValue()); return it->getValuePtr(); } } // if no key == k in the hash table return 0; }
void HashTable<V>::insert(string k, V v) { int hash = hashFunction(k); list<Entry<V> > &hList = vecTable[hash]; typename list<Entry<V> >::iterator it; for (it=hList.begin(); it!=hList.end(); it++) { if (it->getKey() == k) { it->setValue(v); return; } } // if no key == k in the hash table create a new Entry Entry<V> newEntry(k,v); hList.push_back(newEntry); return; }
void HashTable<V>::print() { typename list<Entry<V> >::iterator it; cout << endl; for (int i=0; i<SIZE; i++) { cout << i << ": "; list<Entry<V> > &hList = vecTable[i]; for (it=hList.begin(); it!=hList.end(); it++) { cout << "[" << it->getKey() << "," << it->getValue() << "]"; } cout << endl; } cout << endl; return; }