V hash_map<K,V>::remove(K key) { // get index by hashing int hash_code = ::hash(key); int index = hash_code % total_buckets; // search list for link containing key, keep track of previous link for deletion. link* current; link* previous = NULL; for(current = buckets[index]; current != NULL && (current->element).key != key; current = current->next) previous = current; // key not found: precondition violated. if(current == NULL) throw key_not_found_exception(); V tmp = current->element.value; if(previous == NULL) // remove buckets[i] buckets[index] = current->next; else // remove within linked list previous->next = current->next; delete current; map_size--; modified = true; return tmp; }
//@todo throw meaningful exceptions, handle exceptions parse_cgi::key_value_data::value_type parse_cgi::key_value_data::pop_value(const parse_cgi::key_value_data::key_type &key) { try { //the value we will return key_value_container_type::iterator it = this->key_value_container.find(key); //if key_value_container.end() is returned, we didn't find it if(it != key_value_container.end()) { //we found it, save it value_type ret = it->second; //kill it this->key_value_container.erase(it); //return what we found return ret; } else { throw key_not_found_exception(); } } catch(const std::exception &e) { throw; } }
//@todo throw meaningful exceptions, handle exceptions parse_cgi::key_value_data::value_type parse_cgi::key_value_data::get_value(const parse_cgi::key_value_data::key_type &key) { try { //the iterator to the key-value pair we are looking for key_value_container_type::const_iterator it = this->key_value_container.find(key); //if key_value_container.end() is returned, we didn't find it if(it != key_value_container.end()) { //we found it, return the value return it->second; } //we didn't find it else { throw key_not_found_exception(); } } catch(const std::exception &e) { //somebody else's problem... throw; } }
// Handle request for the pair. // Throws an exception if pair is not found. std::string handle_request_for_pair( const msg_request_by_key & what ) { auto r = m_values.find( what.m_key ); if( r == m_values.end() ) throw key_not_found_exception( "key is not found in the storage: " + what.m_key ); return r->second; }
V hash_map<K,V>::get(K key) { // get index by hashing int hash_code = ::hash(key); int index = hash_code % total_buckets; // search list for the link containing key link* current; for(current = buckets[index]; current != NULL && (current->element).key != key; current = current->next); // throw if key not found: precondition violated. if(current == NULL) throw key_not_found_exception(); return (current->element).value; }